def anygoal(s): anygoal.goals, local_goals = it.tee(anygoal.goals) def f(goals): for goal in goals: try: yield goaleval(reify(goal, s))(s) except EarlyGoalError: pass return unique(interleave(f(local_goals), [EarlyGoalError]), key=dicthash)
def allgoal(s): for i, g in enumerate(goals): try: goal = goaleval(reify(g, s)) except EarlyGoalError: continue other_goals = tuple(goals[:i] + goals[i+1:]) return unique(interleave(goaleval( reify((lallfirst,) + other_goals, ss))(ss) for ss in goal(s)), key=dicthash) else: raise EarlyGoalError()
def run(n, x, *goals, **kwargs): """ Run a logic program. Obtain n solutions to satisfy goals. n - number of desired solutions. See ``take`` 0 for all None for a lazy sequence x - Output variable goals - a sequence of goals. All must be true >>> from logpy import run, var, eq >>> x = var() >>> run(1, x, eq(x, 1)) (1,) """ results = (reify(x, s) for s in goaleval(lallearly(*goals))({})) return take(n, unique(results, key=multihash))
def allgoal(s): g = goaleval(reify(goals[0], s)) return unique(interleave( goaleval(reify((lall,) + tuple(goals[1:]), ss))(ss) for ss in g(s)), key=dicthash)
def test_unique_not_hashable(): assert tuple(unique(([1], [1])))
def test_unique_dict(): assert tuple(unique(({1: 2}, {2: 3}), key=dicthash)) == ({1: 2}, {2: 3}) assert tuple(unique(({1: 2}, {1: 2}), key=dicthash)) == ({1: 2},)
def test_unique(): assert tuple(unique((1,2,3))) == (1,2,3) assert tuple(unique((1,2,1,3))) == (1,2,3)