def test_reify(): x, y, z = var(), var(), var() s = {x: 1, y: 2, z: (x, y)} assert reify(x, s) == 1 assert reify(10, s) == 10 assert reify((1, y), s) == (1, 2) assert reify((1, (x, (y, 2))), s) == (1, (1, (2, 2))) assert reify(z, s) == (1, 2)
def test_unify_isinstance_list(): class Foo2(Foo): pass x = var('x') y = var('y') f, g = Foo2(1, 2), Foo2(x, y) unify_isinstance_list.append(((Foo, Foo), unify_object)) reify_isinstance_list.append((Foo, reify_object)) assert unify(f, g, {}) assert reify(g, {x: 1, y: 2}) == f unify_isinstance_list.pop() reify_isinstance_list.pop()
def test_objects_full(): unify_dispatch[(Foo, Foo)] = unify_object unify_dispatch[(Bar, Bar)] = unify_object reify_dispatch[Foo] = reify_object reify_dispatch[Bar] = reify_object assert unify_object(Foo(1, Bar(2)), Foo(1, Bar(var(3))), {}) == {var(3): 2} assert reify(Foo(var('a'), Bar(Foo(var('b'), 3))), {var('a'): 1, var('b'): 2}) == Foo(1, Bar(Foo(2, 3))) del reify_dispatch[Bar] del reify_dispatch[Foo] del unify_dispatch[(Foo, Foo)] del unify_dispatch[(Bar, Bar)]
def test_objects(): from logpy.unify import seq_registry, reify class add(object): def __init__(self, *args): self.args = tuple(args) def seq_add(x): return (type(x),) + x.args seq_registry.append((add, seq_add)) assert seq_add(add(1, 2, 3)) == (add, 1, 2, 3) assert goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}) x = var('x') assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({})) == ({x: 3},) fact(commutative, add) fact(associative, add) assert reify(x, next(goaleval(eq_assoccomm(add(1, 2, 3), add(x, 2, 1)))({}))) == 3 seq_registry.pop()
def test_reify_complex(): x, y = var(), var() s = {x: 2, y: 4} e = {1: [x], 3: (y, 5)} assert reify(e, s) == {1: [2], 3: (4, 5)}