def _convert_list(x): if len(x) >= 2 and x[-1] is Ellipsis: p = convert(x[-2]) x = x[:-2] else: p = null for e in reversed(x): p = Pair(convert(e), p) return p
def no_item(val, pred): val = convert(val) def _goal(state): a = walk(val, state[0]) if type(a) is Var: cons = state[2] cons[a].append(_goal) return Unit(state) if pred(a): return MZero() stream = Unit(state) if isinstance(a, tuple): for e in a: stream = stream.bind(no_item(e, pred)) return stream return _goal
def no_item(val, pred): val = convert(val) def _goal(state): a = walk(val, state[0]) if type(a) is Var: cons = state[2] cons[a].append(_goal) return Unit(state) if type(a) is Pair: return Unit(state).\ bind(no_item(a.car, pred)).\ bind(no_item(a.cdr, pred)) if pred(a): return MZero() return Unit(state) return _goal
u: 1, v: 2 }), ((u, v), (1, 2), { u: 1 }, [v], { u: 1, v: 2 }), ((u, v), (1, 2), { u: 2 }, None, None), (u, (1, u), {}, None, None), ([u, v], [1, 2], {}, [u, v], { u: 1, v: 2 }), ([u, v, ...], [1, 2], {}, [u, v], { u: 1, v: convert([2]) }), ([u, v], (1, 2), {}, None, None), ] @pytest.mark.parametrize("u, v, subst, expected, extended", UNIFY_DATA) def test_unify(u, v, subst, expected, extended): assert unify(u, v, subst) == expected if expected is not None: assert subst == extended
b = Var() GOALS_DATA = [ (eq(a, 1), [{a: 1}]), (disj(eq(a, 1), eq(a, 2)), [{a: 1}, {a: 2}]), (conj(eq(a, 1), eq(a, 1)), [{a: 1}]), (conj(eq(a, 1), eq(a, 2)), []), (eqt(a, int), [{}]), (conj(eq(a, 1), eqt(a, int)), [{a: 1}]), (conj(eqt(a, int), eq(a, 1)), [{a: 1}]), (conj(eq(a, 1), eqt(a, str)), []), (conj(eqt(a, str), eq(a, 1)), []), (conj(eqt(a, int), eqt(a, int)), [{}]), (conj(eqt(a, int), eqt(a, str)), []), (conj(conj(eqt(a, int), eqt(b, int)), eq(a, b)), [{a: b}]), (conj(conj(eqt(a, int), eqt(b, str)), eq(a, b)), []), (conj(conj(eq(a, [1]), eq(b, 1)), eq(a, [b])), [{a: convert([1]), b: 1}]), (conj(conj(eq(a, [b]), eq(b, 1)), eq(a, [1])), [{a: convert([b]), b: 1}]), (conj(conj(eq(a, [b]), eq(b, 1)), eq(a, [2])), []) ] @pytest.mark.parametrize("goal, expected", GOALS_DATA) def test_goals(goal, expected): assert [s for s, _, _ in unfold(goal(initial()))] == expected