def assocunify(u, v, s, eq=core.eq): """ Associative Unification See Also: eq_assoccomm """ res = unify(u, v, s) if res is not False: return (res,) # TODO: iterate through all possibilities if isinstance(u, tuple) and isinstance(v, tuple): uop, u = u[0], u[1:] vop, v = v[0], v[1:] s = unify(uop, vop, s) if s is False: raise StopIteration() op = walk(uop, s) sm, lg = (u, v) if len(u) <= len(v) else (v, u) parts = (groupsizes_to_partition(*gsizes) for gsizes in groupsizes(len(lg), len(sm))) ops = (makeops(op, partition(lg, part)) for part in parts) goal = condeseq([(eq, a, b) for a, b in zip(sm, lg2)] for lg2 in ops) return goaleval(goal)(s) return ()
def unify_assoccomm(u, v, s, ordering=None): u = walk(u, s) v = walk(v, s) res = unify(u, v, s) if res is not False: yield res if isinstance(u, tuple) and isinstance(v, tuple): uop, u = u[0], u[1:] vop, v = v[0], v[1:] s = unify(uop, vop, s) if s is False: raise StopIteration() op = walk(uop, s) sm, lg = (u, v) if len(u) <= len(v) else (v, u) for part in kbins(range(len(lg)), len(sm), ordering): lg2 = makeops(op, partition(lg, part)) # TODO: we use logpy code within python within logpy # There must be a more elegant way for res in conde((eq_assoccomm, a, b) for a, b in zip(sm, lg2))(s): yield res
def assocunify(u, v, s, eq=core.eq, n=None): """ Associative Unification See Also: eq_assoccomm """ if not isinstance(u, tuple) and not isinstance(v, tuple): res = unify(u, v, s) if res is not False: return (res,) # TODO: iterate through all possibilities if isinstance(u, tuple) and isinstance(v, tuple): uop, u = u[0], u[1:] vop, v = v[0], v[1:] s = unify(uop, vop, s) if s is False: raise StopIteration() op = walk(uop, s) sm, lg = (u, v) if len(u) <= len(v) else (v, u) ops = assocsized(op, lg, len(sm)) goal = condeseq([(eq, a, b) for a, b, in zip(sm, lg2)] for lg2 in ops) return goaleval(goal)(s) if isinstance(u, tuple): a, b = u, v if isinstance(v, tuple): a, b = v, u op, tail = a[0], a[1:] ns = [n] if n else range(2, len(a)) knowns = (((op,) + x) for n in ns for x in assocsized(op, tail, n)) goal = condeseq([(core.eq, b, k)] for k in knowns) return goaleval(goal)(s)
def test_deep_walk(): """ Page 30 of Byrd thesis """ s = {z: 6, y: 5, x: (y, z)} assert walk(x, s) == (y, z) assert walkstar(x, s) == (5, 6)
def test_walk(): s = {1: 2, 2: 3} assert walk(2, s) == 3 assert walk(1, s) == 3 assert walk(4, s) == 4