def test_logic_expand(): t = And(Or('a', 'b'), 'c') assert t.expand() == Or(And('a', 'c'), And('b', 'c')) t = And(Or('a', Not('b')), 'b') assert t.expand() == And('a', 'b') t = And(Or('a', 'b'), Or('c', 'd')) assert t.expand() == \ Or(And('a', 'c'), And('a', 'd'), And('b', 'c'), And('b', 'd'))
def test_logic_onearg(): assert And() is True assert Or() is False assert And(T) == T assert And(F) == F assert Or(T) == T assert Or(F) == F assert And('a') == 'a' assert Or('a') == 'a'
def test_logic_not(): assert Not(False) is True assert Not('a') != '~a' assert Not('~a') != 'a' # NOTE: we may want to change default Not behaviour and put this # functionality into some method. assert Not(And('a', 'b')) == Or(Not('a'), Not('b')) assert Not(Or('a', 'b')) == And(Not('a'), Not('b')) pytest.raises(ValueError, lambda: Not(ValueError))
def test_FactRules_parse(): f = FactRules('a -> b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('a -> ~b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~a -> b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~a -> ~b') assert f.prereq == {'b': {'a'}, 'a': {'b'}} f = FactRules('~z == nz') assert f.prereq == {'z': {'nz'}, 'nz': {'z'}} pytest.raises(ValueError, lambda: FactRules('a ? b')) f = FactRules('a -> b & ~b') # trivial test assert not f.prereq # tautologies assert not FactRules('a -> a | b').prereq # XXX: We don't support non-atomic left operands, so use Prover directly p = Prover() p.process_rule(And('a', 'b'), 'a') p.process_rule(Or('a', 'b'), 'a') assert not p.proved_rules
def test_logic_cmp(): l1 = And('a', Not('b')) l2 = And('a', Not('b')) assert hash(l1) == hash(l2) assert (l1 == l2) == T assert (l1 != l2) == F assert And('a', 'b', 'c') == And('b', 'a', 'c') assert And('a', 'b', 'c') == And('c', 'b', 'a') assert And('a', 'b', 'c') == And('c', 'a', 'b')
def test_logic_fromstring(): s = Logic.fromstring assert s('a') == 'a' assert s('~a') == Not('a') assert s('a & b') == And('a', 'b') assert s('a | b') == Or('a', 'b') assert s('a | b & c') == And(Or('a', 'b'), 'c') assert s('a & b | c') == Or(And('a', 'b'), 'c') assert s('a & b & c') == And('a', 'b', 'c') assert s('a | b | c') == Or('a', 'b', 'c') pytest.raises(ValueError, lambda: s('| a')) pytest.raises(ValueError, lambda: s('& a')) pytest.raises(ValueError, lambda: s('a | | b')) pytest.raises(ValueError, lambda: s('a | & b')) pytest.raises(ValueError, lambda: s('a & & b')) pytest.raises(ValueError, lambda: s('a |')) pytest.raises(ValueError, lambda: s('a|b')) pytest.raises(ValueError, lambda: s('~')) pytest.raises(ValueError, lambda: s('~ a')) pytest.raises(ValueError, lambda: s('a b')) pytest.raises(ValueError, lambda: s(''))
def test_logic_eval_TF(): assert And(F, F) == F assert And(F, T) == F assert And(T, F) == F assert And(T, T) == T assert Or(F, F) == F assert Or(F, T) == T assert Or(T, F) == T assert Or(T, T) == T assert And('a', T) == 'a' assert And('a', F) == F assert Or('a', T) == T assert Or('a', F) == 'a'
def test_apply_beta_to_alpha_route(): APPLY = apply_beta_to_alpha_route pytest.raises(TypeError, lambda: APPLY({}, [(Not('a'), 'b')])) # indicates empty alpha-chain with attached beta-rule #bidx def Q(bidx): return set(), [bidx] # x -> a &(a,b) -> x -- x -> a A = {'x': {'a'}} B = [(And('a', 'b'), 'x')] assert APPLY(A, B) == {'x': ({'a'}, []), 'a': Q(0), 'b': Q(0)} # x -> a &(a,~x) -> b -- x -> a A = {'x': {'a'}} B = [(And('a', Not('x')), 'b')] assert APPLY(A, B) == {'x': ({'a'}, []), Not('x'): Q(0), 'a': Q(0)} # x -> a b &(a,b) -> c -- x -> a b c A = {'x': {'a', 'b'}} B = [(And('a', 'b'), 'c')] assert APPLY(A, B) == \ {'x': ({'a', 'b', 'c'}, []), 'a': Q(0), 'b': Q(0)} # x -> a &(a,b) -> y -- x -> a [#0] A = {'x': {'a'}} B = [(And('a', 'b'), 'y')] assert APPLY(A, B) == {'x': ({'a'}, [0]), 'a': Q(0), 'b': Q(0)} # x -> a b c &(a,b) -> c -- x -> a b c A = {'x': {'a', 'b', 'c'}} B = [(And('a', 'b'), 'c')] assert APPLY(A, B) == \ {'x': ({'a', 'b', 'c'}, []), 'a': Q(0), 'b': Q(0)} # x -> a b &(a,b,c) -> y -- x -> a b [#0] A = {'x': {'a', 'b'}} B = [(And('a', 'b', 'c'), 'y')] assert APPLY(A, B) == \ {'x': ({'a', 'b'}, [0]), 'a': Q(0), 'b': Q(0), 'c': Q(0)} # x -> a b &(a,b) -> c -- x -> a b c d # c -> d c -> d A = {'x': {'a', 'b'}, 'c': {'d'}} B = [(And('a', 'b'), 'c')] assert APPLY(A, B) == { 'x': ({'a', 'b', 'c', 'd'}, []), 'c': ({'d'}, []), 'a': Q(0), 'b': Q(0) } # x -> a b &(a,b) -> c -- x -> a b c d e # c -> d &(c,d) -> e c -> d e A = {'x': {'a', 'b'}, 'c': {'d'}} B = [(And('a', 'b'), 'c'), (And('c', 'd'), 'e')] assert APPLY(A, B) == { 'x': ({'a', 'b', 'c', 'd', 'e'}, []), 'c': ({'d', 'e'}, []), 'a': Q(0), 'b': Q(0), 'd': Q(1) } # x -> a b &(a,y) -> z -- x -> a b y z # &(a,b) -> y A = {'x': {'a', 'b'}} B = [(And('a', 'y'), 'z'), (And('a', 'b'), 'y')] assert APPLY(A, B) == { 'x': ({'a', 'b', 'y', 'z'}, []), 'a': (set(), [0, 1]), 'y': Q(0), 'b': Q(1) } # x -> a b &(a,~b) -> c -- x -> a b A = {'x': {'a', 'b'}} B = [(And('a', Not('b')), 'c')] assert APPLY(A, B) == \ {'x': ({'a', 'b'}, []), 'a': Q(0), Not('b'): Q(0)} # ~x -> ~a ~b &(~a,b) -> c -- ~x -> ~a ~b A = {Not('x'): {Not('a'), Not('b')}} B = [(And(Not('a'), 'b'), 'c')] assert APPLY(A, B) == \ {Not('x'): ({Not('a'), Not('b')}, []), Not('a'): Q(0), 'b': Q(0)} # x -> a b &(b,c) -> ~a -- x -> a b A = {'x': {'a', 'b'}} B = [(And('b', 'c'), Not('a'))] assert APPLY(A, B) == {'x': ({'a', 'b'}, []), 'b': Q(0), 'c': Q(0)} # x -> a b &(a, b) -> c -- x -> a b c p # c -> p a A = {'x': {'a', 'b'}, 'c': {'p', 'a'}} B = [(And('a', 'b'), 'c')] assert APPLY(A, B) == { 'x': ({'a', 'b', 'c', 'p'}, []), 'c': ({'p', 'a'}, []), 'a': Q(0), 'b': Q(0) }
def test_logic_xnotx(): assert And('a', Not('a')) == F assert Or('a', Not('a')) == T
def test_logic_combine_args(): assert And('a', 'b', 'a') == And('a', 'b') assert Or('a', 'b', 'a') == Or('a', 'b') assert And(And('a', 'b'), And('c', 'd')) == And('a', 'b', 'c', 'd') assert Or(Or('a', 'b'), Or('c', 'd')) == Or('a', 'b', 'c', 'd') assert Or('t', And('n', 'p', 'r'), And('n', 'r'), And('n', 'p', 'r'), 't', And('n', 'r')) == \ Or('t', And('n', 'p', 'r'), And('n', 'r'))