Skip to content

Commit 3a88917

Browse files
Updates for miniKanren package v1
1 parent bf9f823 commit 3a88917

File tree

7 files changed

+15
-174
lines changed

7 files changed

+15
-174
lines changed

setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ def get_long_description():
3636
"Theano>=1.0.4",
3737
"tf-nightly-2.0-preview==2.0.0.dev20191002",
3838
"tf-nightly==2.1.0.dev20191003",
39-
"tensorflow-estimator-2.0-preview>=1.14.0.dev2019090801",
39+
"tf-estimator-nightly==2.0.0.dev2019100301",
40+
"tensorflow-estimator-2.0-preview==1.14.0.dev2019090801",
4041
"tfp-nightly==0.9.0.dev20191003",
4142
"multipledispatch>=0.6.0",
42-
"logical-unification>=0.2.2",
43-
"miniKanren>=0.4.0",
44-
"cons>=0.1.3",
43+
"logical-unification>=0.4.3",
44+
"miniKanren>=1.0.1",
45+
"etuples>=0.3.1",
46+
"cons>=0.4.0",
4547
"toolz>=0.9.0",
4648
"sympy>=1.3",
4749
"cachetools",

symbolic_pymc/relations/__init__.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
from itertools import tee, chain
2-
from functools import reduce
3-
4-
from toolz import interleave
5-
6-
from kanren.core import goaleval
71
from kanren.facts import Relation
82

93
from unification import unify, reify, Var
@@ -34,58 +28,3 @@ def concat_goal(S):
3428
yield S
3529

3630
return concat_goal
37-
38-
39-
def ldisj_seq(goals):
40-
"""Produce a goal that returns the appended state stream from all successful goal arguments.
41-
42-
In other words, it behaves like logical disjunction/OR for goals.
43-
"""
44-
45-
def ldisj_seq_goal(S):
46-
nonlocal goals
47-
48-
goals, _goals = tee(goals)
49-
50-
yield from interleave(goaleval(g)(S) for g in _goals)
51-
52-
return ldisj_seq_goal
53-
54-
55-
def lconj_seq(goals):
56-
"""Produce a goal that returns the appended state stream in which all goals are necessarily successful.
57-
58-
In other words, it behaves like logical conjunction/AND for goals.
59-
"""
60-
61-
def lconj_seq_goal(S):
62-
nonlocal goals
63-
64-
goals, _goals = tee(goals)
65-
66-
g0 = next(iter(_goals), None)
67-
68-
if g0 is None:
69-
return
70-
71-
z0 = goaleval(g0)(S)
72-
73-
yield from reduce(lambda z, g: chain.from_iterable(map(goaleval(g), z)), _goals, z0)
74-
75-
return lconj_seq_goal
76-
77-
78-
def ldisj(*goals):
79-
return ldisj_seq(goals)
80-
81-
82-
def lconj(*goals):
83-
return lconj_seq(goals)
84-
85-
86-
def conde(*goals):
87-
return ldisj_seq(lconj_seq(g) for g in goals)
88-
89-
90-
lall = lconj
91-
lany = ldisj

symbolic_pymc/unify.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
from kanren.term import arguments, operator
77

8-
from unification.more import unify
98
from unification.variable import Var
10-
from unification.core import _reify, _unify, reify
9+
from unification.core import _reify, _unify, reify, unify
1110

1211
from etuples import etuple
1312

tests/test_relations.py

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from unification import var
22

3-
from kanren import eq, run
3+
from kanren import run
44

5-
from symbolic_pymc.relations import lconj, lconj_seq, ldisj, ldisj_seq, conde, concat
5+
from symbolic_pymc.relations import concat
66

77

88
def test_concat():
@@ -11,81 +11,3 @@ def test_concat():
1111
assert not run(0, q, concat("a", "b", "bc"))
1212
assert not run(0, q, concat(1, "b", "bc"))
1313
assert run(0, q, concat(q, "b", "bc")) == (q,)
14-
15-
16-
def test_lconj_basics():
17-
18-
res = list(lconj(eq(1, var("a")), eq(2, var("b")))({}))
19-
assert res == [{var("a"): 1, var("b"): 2}]
20-
21-
res = list(lconj(eq(1, var("a")))({}))
22-
assert res == [{var("a"): 1}]
23-
24-
res = list(lconj_seq([])({}))
25-
assert res == []
26-
27-
res = list(lconj(eq(1, var("a")), eq(2, var("a")))({}))
28-
assert res == []
29-
30-
res = list(lconj(eq(1, 2))({}))
31-
assert res == []
32-
33-
res = list(lconj(eq(1, 1))({}))
34-
assert res == [{}]
35-
36-
37-
def test_ldisj_basics():
38-
39-
res = list(ldisj(eq(1, var("a")))({}))
40-
assert res == [{var("a"): 1}]
41-
42-
res = list(ldisj(eq(1, 2))({}))
43-
assert res == []
44-
45-
res = list(ldisj(eq(1, 1))({}))
46-
assert res == [{}]
47-
48-
res = list(ldisj(eq(1, var("a")), eq(1, var("a")))({}))
49-
assert res == [{var("a"): 1}, {var("a"): 1}]
50-
51-
res = list(ldisj(eq(1, var("a")), eq(2, var("a")))({}))
52-
assert res == [{var("a"): 1}, {var("a"): 2}]
53-
54-
res = list(ldisj_seq([])({}))
55-
assert res == []
56-
57-
58-
def test_conde_basics():
59-
60-
res = list(conde([eq(1, var("a")), eq(2, var("b"))], [eq(1, var("b")), eq(2, var("a"))])({}))
61-
assert res == [{var("a"): 1, var("b"): 2}, {var("b"): 1, var("a"): 2}]
62-
63-
res = list(conde([eq(1, var("a")), eq(2, 1)], [eq(1, var("b")), eq(2, var("a"))])({}))
64-
assert res == [{var("b"): 1, var("a"): 2}]
65-
66-
res = list(
67-
conde(
68-
[eq(1, var("a")), conde([eq(11, var("aa"))], [eq(12, var("ab"))])],
69-
[
70-
eq(1, var("b")),
71-
conde([eq(111, var("ba")), eq(112, var("bb"))], [eq(121, var("bc"))]),
72-
],
73-
)({})
74-
)
75-
assert res == [
76-
{var("a"): 1, var("aa"): 11},
77-
{var("b"): 1, var("ba"): 111, var("bb"): 112},
78-
{var("a"): 1, var("ab"): 12},
79-
{var("b"): 1, var("bc"): 121},
80-
]
81-
82-
res = list(conde([eq(1, 2)], [eq(1, 1)])({}))
83-
assert res == [{}]
84-
85-
assert list(lconj(eq(1, 1))({})) == [{}]
86-
87-
res = list(lconj(conde([eq(1, 2)], [eq(1, 1)]))({}))
88-
assert res == [{}]
89-
90-
res = list(lconj(conde([eq(1, 2)], [eq(1, 1)]), conde([eq(1, 2)], [eq(1, 1)]))({}))
91-
assert res == [{}]

tests/theano/test_kanren.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_kanren():
7474
Y_mt = mt.MvNormalRV(E_y_mt, V_lv, y_size_lv, y_rng_lv, name=y_name_lv)
7575

7676
with variables(Y_mt):
77-
(res,) = run(0, Y_mt, (eq, Y_rv, Y_mt))
77+
(res,) = run(0, Y_mt, eq(Y_rv, Y_mt))
7878
assert res.reify() == Y_rv
7979

8080

tests/theano/test_opt.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from unification import var
55

66
from kanren import eq
7-
from kanren.core import lallgreedy
7+
from kanren.core import lall
88

99
from etuples import etuple, etuplize
1010

@@ -36,13 +36,11 @@ def test_kanren_opt():
3636
assert isinstance(fgraph.outputs[0].owner.op, tt.Dot)
3737

3838
def distributes(in_lv, out_lv):
39-
return (
40-
lallgreedy,
39+
return lall(
4140
# lhs == A * (x + b)
42-
(eq, etuple(mt.dot, var("A"), etuple(mt.add, var("x"), var("b"))), etuplize(in_lv)),
41+
eq(etuple(mt.dot, var("A"), etuple(mt.add, var("x"), var("b"))), etuplize(in_lv)),
4342
# rhs == A * x + A * b
44-
(
45-
eq,
43+
eq(
4644
etuple(
4745
mt.add, etuple(mt.dot, var("A"), var("x")), etuple(mt.dot, var("A"), var("b"))
4846
),

tests/theano/test_unify.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import theano.tensor as tt
44

5-
from unification import unify, reify, var, variables
5+
from unification import reify, unify, var
66

77
from cons.core import ConsError
88

@@ -37,25 +37,6 @@ def test_unification():
3737
assert res[x_l].reify() == b
3838
assert res[y_l].reify() == a
3939

40-
# TODO: This produces a `DimShuffle` so that the scalar constant `1`
41-
# will match the dimensions of the vector `b`. That `DimShuffle` isn't
42-
# handled by the logic variable form.
43-
# assert unify(mt.add(x_l, 1), mt.add(b_l, 1))[x] == b
44-
45-
with variables(x):
46-
assert unify(x + 1, b + 1)[x].reify() == b
47-
48-
assert unify(mt.add(x_l, a), mt.add(b, a))[x_l].reify() == b
49-
with variables(x):
50-
assert unify(x, b)[x] == b
51-
assert unify([x], [b])[x] == b
52-
assert unify((x,), (b,))[x] == b
53-
assert unify(x + 1, b + 1)[x].reify() == b
54-
assert unify(x + a, b + a)[x].reify() == b
55-
56-
with variables(x):
57-
assert unify(a + b, a + x)[x].reify() == b
58-
5940
mt_expr_add = mt.add(x_l, y_l)
6041

6142
# The parameters are vectors

0 commit comments

Comments
 (0)