def test_assoccomm_objects(): fact(commutative, Add) fact(associative, Add) x = var() assert run(0, True, eq_assoccomm(add(1, 2, 3), add(3, 1, 2))) == (True,) assert run(0, x, eq_assoccomm(add(1, 2, 3), add(1, 2, x))) == (3,) assert run(0, x, eq_assoccomm(add(1, 2, 3), add(x, 2, 1))) == (3,)
def test_objects(): fact(commutative, Add) fact(associative, Add) assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({})) assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({})) x = var('x') assert reify(x, tuple(goaleval(eq_assoccomm( add(1, 2, 3), add(1, 2, x)))({}))[0]) == 3 assert reify(x, next(goaleval(eq_assoccomm( add(1, 2, 3), add(x, 2, 1)))({}))) == 3 v = add(1, 2, 3) with variables(v): x = add(5, 6) assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
def test_eq_comm_object(): x = var("x") fact(commutative, Add) fact(associative, Add) assert run(0, x, eq_comm(add(1, 2, 3), add(3, 1, x))) == (2, ) assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1))) assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == set( (add(2, 3), add(3, 2)))
def test_objects(): fact(commutative, Add) fact(associative, Add) assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({})) assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({})) x = var('x') assert reify( x, tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({}))[0]) == 3 assert reify(x, next(goaleval(eq_assoccomm(add(1, 2, 3), add(x, 2, 1)))({}))) == 3 v = add(1, 2, 3) with variables(v): x = add(5, 6) assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
def test_eq_comm_object(): x = var('x') fact(commutative, Add) fact(associative, Add) assert run(0, x, eq_comm(add(1, 2, 3), add(3, 1, x))) == (2, ) assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1))) assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == \ set((add(2, 3), add(3, 2)))
def test_expr(): add = 'add' mul = 'mul' fact(commutative, add) fact(associative, add) fact(commutative, mul) fact(associative, mul) x, y = var('x'), var('y') pattern = (mul, (add, 1, x), y) # (1 + x) * y expr = (mul, 2, (add, 3, 1)) # 2 * (3 + 1) assert run(0, (x, y), eq_assoccomm(pattern, expr)) == ((3, 2), )
def distributes(in_lv, out_lv): A_lv, x_lv, y_lv, z_lv = vars(4) return lall( # lhs == A * (x + y + z) eq_assoccomm( etuple(_dot, A_lv, etuple(at.add, x_lv, etuple(at.add, y_lv, z_lv))), in_lv, ), # This relation does nothing but provide us with a means of # generating associative-commutative matches in the `kanren` # output. eq((A_lv, x_lv, y_lv, z_lv), out_lv), )
def test_assoccomm_algebra(): add = "add" mul = "mul" fact(commutative, add) fact(associative, add) fact(commutative, mul) fact(associative, mul) x, y = var(), var() pattern = (mul, (add, 1, x), y) # (1 + x) * y expr = (mul, 2, (add, 3, 1)) # 2 * (3 + 1) assert run(0, (x, y), eq_assoccomm(pattern, expr)) == ((3, 2),)
from kanren import run, var, fact import kanren.assoccomm as ka # Define mathematical operations add = 'addition' mul = 'multiplication' # Deckare that these operations are commutative # using the facts system fact(ka.commutative, mul) fact(ka.commutative, add) fact(ka.associative, mul) fact(ka.associative, add) # Define some variables a, b, c = var('a'), var('b'), var('c') # Generate expressions expression_orig = (add, (mul, 3, -2), (mul, (add, 1, (mul, 2, 3)), -1)) expression1 = (add, (mul, (add, 1, (mul, 2, a)), b), (mul, 3, c)) expression2 = (add, (mul, c, 3), (mul, b, (add, (mul, 2, a), 1))) expression3 = (add, (add, (mul, (mul, 2, a), b), b), (mul, 3, c)) # Compare expressions print(run(0, (a, b, c), ka.eq_assoccomm(expression1, expression_orig))) print(run(0, (a, b, c), ka.eq_assoccomm(expression2, expression_orig))) print(run(0, (a, b, c), ka.eq_assoccomm(expression3, expression_orig)))
def test_eq_assoccomm(): x, y = var(), var() ac = "commassoc_op" commutative.index.clear() commutative.facts.clear() fact(commutative, ac) fact(associative, ac) assert run(0, True, eq_assoccomm(1, 1)) == (True, ) assert run(0, True, eq_assoccomm((1, ), (1, ))) == (True, ) assert run(0, True, eq_assoccomm(x, (1, ))) == (True, ) assert run(0, True, eq_assoccomm((1, ), x)) == (True, ) # Assoc only assert run(0, True, eq_assoccomm((ac, 1, (ac, 2, 3)), (ac, (ac, 1, 2), 3))) == (True, ) # Commute only assert run(0, True, eq_assoccomm((ac, 1, (ac, 2, 3)), (ac, (ac, 3, 2), 1))) == (True, ) # Both assert run(0, True, eq_assoccomm((ac, 1, (ac, 3, 2)), (ac, (ac, 1, 2), 3))) == (True, ) exp_res = set(( (ac, 1, 3, 2), (ac, 1, 2, 3), (ac, 2, 1, 3), (ac, 2, 3, 1), (ac, 3, 1, 2), (ac, 3, 2, 1), (ac, 1, (ac, 2, 3)), (ac, 1, (ac, 3, 2)), (ac, 2, (ac, 1, 3)), (ac, 2, (ac, 3, 1)), (ac, 3, (ac, 1, 2)), (ac, 3, (ac, 2, 1)), (ac, (ac, 2, 3), 1), (ac, (ac, 3, 2), 1), (ac, (ac, 1, 3), 2), (ac, (ac, 3, 1), 2), (ac, (ac, 1, 2), 3), (ac, (ac, 2, 1), 3), )) assert set(run(0, x, eq_assoccomm((ac, 1, (ac, 2, 3)), x))) == exp_res assert set(run(0, x, eq_assoccomm((ac, 1, 3, 2), x))) == exp_res assert set(run(0, x, eq_assoccomm((ac, 2, (ac, 3, 1)), x))) == exp_res # LHS variations assert set(run(0, x, eq_assoccomm(x, (ac, 1, (ac, 2, 3))))) == exp_res assert run(0, (x, y), eq_assoccomm((ac, (ac, 1, x), y), (ac, 2, (ac, 3, 1)))) == ( (2, 3), (3, 2), ) assert run(0, True, eq_assoccomm((ac, (ac, 1, 2), 3), (ac, 1, 2, 3))) == (True, ) assert run(0, True, eq_assoccomm((ac, 3, (ac, 1, 2)), (ac, 1, 2, 3))) == (True, ) assert run(0, True, eq_assoccomm((ac, 1, 1), ("other_op", 1, 1))) == () assert run(0, x, eq_assoccomm((ac, 3, (ac, 1, 2)), (ac, 1, x, 3))) == (2, ) # Both arguments unground op_lv = var() z = var() res = run(4, (x, y), eq_assoccomm(x, y)) exp_res_form = ( (etuple(op_lv, x, y), etuple(op_lv, y, x)), (y, y), ( etuple(etuple(op_lv, x, y)), etuple(etuple(op_lv, y, x)), ), ( etuple(op_lv, x, y, z), etuple(op_lv, etuple(op_lv, x, y), z), ), ) for a, b in zip(res, exp_res_form): s = unify(a, b) assert (op_lv not in s or (s[op_lv], ) in associative.facts or (s[op_lv], ) in commutative.facts) assert s is not False, (a, b) assert all(isvar(i) for i in reify((x, y, z), s))