Skip to content

Commit

Permalink
Resolves #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-nocentini committed May 8, 2017
1 parent 615e889 commit e197a70
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
22 changes: 5 additions & 17 deletions src/microkanren_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,12 @@ def fives(x):
self.assertEqual( [state(sub={var(0, 'x'): 5}, next_index=1)]*10,
[next(α) for _ in range(10)])

def test_ηinverse_and_snooze(self):

def sixes_η(x):
return disj(unify(x, 6), fresh(lambda: sixes_η(x)))

def sixes_snooze(x):
return disj(unify(x, 6), snooze(sixes_snooze, [x]))

with states_stream(fresh(sixes_η)) as α,\
states_stream(fresh(sixes_snooze)) as β:
self.assertEqual( [next(α) for _ in range(10)],
[next(β) for _ in range(10)])


def test_interleaving_of_5s_and_6s(self):

def ns(x, n):
return disj(unify(x, n), snooze(ns, [x, n]))
return disj(unify(x, n), fresh(lambda: ns(x, n)))

with states_stream(fresh(lambda x: disj(ns(x, 5), ns(x, 6)))) as α:
numbers = [state(sub={var(0, 'x'): 5}, next_index=1),
Expand All @@ -62,7 +50,7 @@ def ns(x, n):
def test_nats(self):

def ns(x, n):
return disj(unify(x, n), snooze(ns, [x, n+1]))
return disj(unify(x, n), fresh(lambda: ns(x, n+1)))

with states_stream(fresh(lambda x: ns(x, 0))) as α:
nats = [state(sub={var(0, 'x'): i}, next_index=1) for i in range(10)]
Expand All @@ -71,7 +59,7 @@ def ns(x, n):
def test_nats_by_evens_and_odds(self):

def ns(x, n):
return disj(unify(x, n), snooze(ns, [x, n+2]))
return disj(unify(x, n), fresh(lambda: ns(x, n+2)))

with states_stream(fresh(lambda x: disj(ns(x, 0), ns(x, 1)))) as α:
nats = [state(sub={var(0, 'x'): i}, next_index=1) for i in range(10)]
Expand All @@ -80,14 +68,14 @@ def ns(x, n):
def test_fail_goal(self):

def ns(x, n):
return disj(unify(x, n), snooze(ns, [x, n]))
return disj(unify(x, n), fresh(lambda: ns(x, n)))

self.assertEqual(run(fresh(lambda x: conj(fail, ns(x, 5)))), [])

def test_divergent_goal_despite_fail_in_conj(self):

def ns(x, n):
return disj(unify(x, n), snooze(ns, [x, n]))
return disj(unify(x, n), fresh(lambda: ns(x, n)))

with self.assertRaises(RecursionError):
run(fresh(lambda x: conj(ns(x, 5), fail)))
Expand Down
3 changes: 0 additions & 3 deletions src/muk/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from muk.utils import *


def snooze(f, formal_vars):
return fresh(lambda: f(*formal_vars))

# DISJ, CONJ, CONJI {{{

def disj(*goals, interleaving=True):
Expand Down

0 comments on commit e197a70

Please sign in to comment.