<commit_before>"""
Boolean Logic Expressions

Interface Functions:
    var

    factor
    simplify

    Nor, Nand
    OneHot0, OneHot

    f_not
    f_or, f_nor
    f_and, f_nand
    f_xor, f_xnor
    f_equal
    f_implies

Interface Classes:
    Expression
        Literal
            Variable
            Complement
        OrAnd
            Or
            And
        Not
        Exclusive
            Xor
            Xnor
        Equal
        Implies
        ITE
"""

from collections import deque, OrderedDict

from pyeda import boolfunc
from pyeda import sat
from pyeda.common import bit_on, parity, boolify, cached_property

B = {0, 1}

def var(name, indices=None, namespace=None):
    """Return a variable expression.

    Parameters
    ----------

    name : str
        The variable's identifier string.
    indices : int or tuple[int], optional
        One or more integer suffixes for variables that are part of a
        multi-dimensional bit-vector, eg x[1], x[1][2][3]
    namespace : str, optional
        A container for a set of variables. Since a Variable instance is global,
        a namespace can be used for local scoping.
    """
    return Variable(name, indices, namespace)

def factor(expr, conj=False):
    """Return a factored expression.

    Parameters
    ----------

    expr : Expression
    conj : bool
        Always choose a conjunctive form when there's a choice
    """
    if expr in B:
        return expr
    else:
        return expr.factor(conj)

def simplify(expr):
    """Return a simplified expression."""
    if expr in B:
        return expr
    else:
        return expr.simplify()

# convenience functions
def Nor(*args):
    """Alias for Not Or"""
    return Not(Or(*args))

def Nand(*args):
    """Alias for Not And"""
    return Not(And(*args))

def OneHot0(*args):
    """
    Return an expression that means:
        At most one input variable is true.
    """
    nargs = len(args)
    return And(*[Or(Not(args[i]), Not(args[j]))
                 for i in range(nargs-1) for j in range(i+1, nargs)])

def OneHot(*args):
    """
    Return an expression that means:
        Exactly one input variable is true.
    """
    return And(Or(*args), OneHot0(*args))

# factored operators
def f_not(arg, conj=False):
    """Return factored NOT expression."""
    f = Not(arg).factor(conj)
    return f if f in B else f.factor(conj)

def f_or(*args, conj=False):
    """Return factored OR expression."""
    f = Or(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_nor(*args, conj=False):
    """Return factored NOR expression."""
    f = Nor(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_and(*args, conj=False):
    """Return factored AND expression."""
    f = And(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_nand(*args, conj=False):
    """Return factored NAND expression."""
    f = Nand(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_xor(*args, conj=False):
    """Return factored XOR expression."""
    f = Xor(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_xnor(*args, conj=False):
    """Return factored XNOR expression."""
    f = Xnor(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_equal(*args, conj=False):
    """Return factored Equal expression."""
    f = Equal(*args).factor(conj)
    return f if f in B else f.factor(conj)

def f_implies(p, q, conj=False):
    """Return factored Implies expression."""
    f = Implies(p, q).factor(conj)
    return f if f in B else f.factor(conj)

def f_ite(s, a, b, conj=False):
    """Return factored if-then-else expression."""
    f = ITE(s, a, b).factor(conj)
    return f if f in B else f.factor(conj)


class Expression(boolfunc.Function):
    """Boolean function represented by a logic expression"""
<commit_msg>Fix some Python 2.7 problems<commit_after>"""
Boolean Logic Expressions

Interface Functions:
    var

    factor
    simplify

    Nor, Nand
    OneHot0, OneHot

    f_not
    f_or, f_nor
    f_and, f_nand
    f_xor, f_xnor
    f_equal
    f_implies

Interface Classes:
    Expression
        Literal
            Variable
            Complement
        OrAnd
            Or
            And
        Not
        Exclusive
            Xor
            Xnor
        Equal
        Implies
        ITE
"""

from collections import deque, OrderedDict

from pyeda import boolfunc
from pyeda import sat
from pyeda.common import bit_on, parity, boolify, cached_property

B = {0, 1}

def var(name, indices=None, namespace=None):
    """Return a variable expression.

    Parameters
    ----------

    name : str
        The variable's identifier string.
    indices : int or tuple[int], optional
        One or more integer suffixes for variables that are part of a
        multi-dimensional bit-vector, eg x[1], x[1][2][3]
    namespace : str, optional
        A container for a set of variables. Since a Variable instance is global,
        a namespace can be used for local scoping.
    """
    return Variable(name, indices, namespace)

def factor(expr, conj=False):
    """Return a factored expression.

    Parameters
    ----------

    expr : Expression
    conj : bool
        Always choose a conjunctive form when there's a choice
    """
    if expr in B:
        return expr
    else:
        return expr.factor(conj)

def simplify(expr):
    """Return a simplified expression."""
    if expr in B:
        return expr
    else:
        return expr.simplify()

# convenience functions
def Nor(*args):
    """Alias for Not Or"""
    return Not(Or(*args))

def Nand(*args):
    """Alias for Not And"""
    return Not(And(*args))

def OneHot0(*args):
    """
    Return an expression that means:
        At most one input variable is true.
    """
    nargs = len(args)
    return And(*[Or(Not(args[i]), Not(args[j]))
                 for i in range(nargs-1) for j in range(i+1, nargs)])

def OneHot(*args):
    """
    Return an expression that means:
        Exactly one input variable is true.
    """
    return And(Or(*args), OneHot0(*args))

# factored operators
def f_not(arg):
    """Return factored NOT expression."""
    f = Not(arg).factor()
    return f if f in B else f.factor()

def f_or(*args):
    """Return factored OR expression."""
    f = Or(*args).factor()
    return f if f in B else f.factor()

def f_nor(*args):
    """Return factored NOR expression."""
    f = Nor(*args).factor()
    return f if f in B else f.factor()

def f_and(*args):
    """Return factored AND expression."""
    f = And(*args).factor()
    return f if f in B else f.factor()

def f_nand(*args):
    """Return factored NAND expression."""
    f = Nand(*args).factor()
    return f if f in B else f.factor()

def f_xor(*args):
    """Return factored XOR expression."""
    f = Xor(*args).factor()
    return f if f in B else f.factor()

def f_xnor(*args):
    """Return factored XNOR expression."""
    f = Xnor(*args).factor()
    return f if f in B else f.factor()

def f_equal(*args):
    """Return factored Equal expression."""
    f = Equal(*args).factor()
    return f if f in B else f.factor()

def f_implies(p, q):
    """Return factored Implies expression."""
    f = Implies(p, q).factor()
    return f if f in B else f.factor()

def f_ite(s, a, b):
    """Return factored if-then-else expression."""
    f = ITE(s, a, b).factor()
    return f if f in B else f.factor()


class Expression(boolfunc.Function):
    """Boolean function represented by a logic expression"""

{'fraction_non_alphanumeric': 0.11824817518248175, 'fraction_numerical': 0.0031851360318513604, 'mean_word_length': 2.472811059907834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7078197', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2316, 'n_words': 834}