def test_goaleval(): x, y = var("x"), var("y") g = eq(x, 2) assert goaleval(g) == g assert callable(goaleval((eq, x, 2))) raises(EarlyGoalError, lambda: goaleval((membero, x, y))) assert callable(goaleval((lall, (eq, x, 2))))
def test_goaleval(): x, y = var('x'), var('y') g = eq(x, 2) assert goaleval(g) == g assert callable(goaleval((eq, x, 2))) raises(EarlyGoalError, lambda: goaleval((membero, x, y))) assert callable(goaleval((lall, (eq, x, 2))))
def test_goaleval(): x, y = var('x'), var('y') g = eq(x, 2) assert goaleval(g) == g assert callable(goaleval((eq, x, 2))) with raises(EarlyGoalError): goaleval((membero, x, y)) assert callable(goaleval((lallgreedy, (eq, x, 2))))
def anyfail(s, goals): """ Short circuit if any of the goals fail """ if any(goaleval(goal) is fail for goal in goals): return (fail,) if any(goaleval(goal)(s) is () for goal in goals): return (fail,) else: return goals
def test_lanyseq(): x = var('x') g = lanyseq(((eq, x, i) for i in range(3))) assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}] assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}] # Test lanyseq with an infinite number of goals. assert set(run(3, x, lanyseq(((eq, x, i) for i in count())))) == {0, 1, 2} assert set(run(3, x, (lanyseq, ((eq, x, i) for i in count())))) == \ {0, 1, 2}
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 bindstrat(s, goals, strats, joiner=interleave): """ Strategic bind """ if not goals: return (s,) for strat in strats: goals = strat(s, goals) stream = goaleval(goals[0])(s) return bindstarstrat(stream, goals[1:], strats, joiner)
def test_objects(): from logpy import variables, reify, assoccomm assoccomm.op_registry.extend(op_registry) 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') print tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, 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) print reify(v, next(goaleval(eq_assoccomm(v, x))({}))) assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x del assoccomm.op_registry[-1]
def earlysafe(s, goals): """ Place first goal at end if it raises an EarlyGoalError """ if not goals: return goals goal = goaleval(goals[0]) try: goal(s) return goals except EarlyGoalError: regoals = goals[1:] + (goals[0],) return earlysafe(s, regoals)
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 assocunify(u, v, s, eq=core.eq, n=None): """ Associative Unification See Also: eq_assoccomm """ uop, uargs = op_args(u) vop, vargs = op_args(v) if not uop and not vop: res = unify(u, v, s) if res is not False: return (res,) # TODO: iterate through all possibilities if uop and vop: s = unify(uop, vop, s) if s is False: raise StopIteration() op = walk(uop, s) sm, lg = (uargs, vargs) if len(uargs) <= len(vargs) else (vargs, uargs) 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 uop: op, tail = uop, uargs b = v if vop: op, tail = vop, vargs b = u ns = [n] if n else range(2, len(tail)+1) knowns = (build(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 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_objects(): from logpy import variables, reify 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') print(tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, 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) print(reify(v, next(goaleval(eq_assoccomm(v, x))({})))) assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
def test_objects(): from logpy import variables, reify, assoccomm 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') print(tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, 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) print(reify(v, next(goaleval(eq_assoccomm(v, x))({})))) assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
def primo(x): return goaleval((_primo, x))
def test_membero_can_be_reused(): x = var('x') g = membero(x, (0, 1, 2)) assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}] assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}]
def results(g, s={}): return tuple(goaleval(g)(s))
def test_lanyseq(): x = var('x') g = lanyseq(((eq, x, i) for i in range(3))) assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}] assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}]