1010from kanren .goals import isinstanceo
1111
1212from symbolic_pymc .etuple import etuple , ExpressionTuple
13- from symbolic_pymc .relations .graph import reduceo , lapply_anyo , graph_applyo
13+ from symbolic_pymc .relations .graph import reduceo , seq_apply_anyo , graph_applyo
1414
1515
1616class OrderedFunction (object ):
@@ -88,52 +88,61 @@ def test_reduceo():
8888 assert res [1 ] == etuple (log , etuple (exp , etuple (log , etuple (exp , 1 ))))
8989
9090
91- def test_lapply_anyo_types ():
91+ def test_seq_apply_anyo_types ():
9292 """Make sure that `applyo` preserves the types between its arguments."""
9393 q_lv = var ()
94- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), [1 ], q_lv ))
94+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), [1 ], q_lv ))
9595 assert res [0 ] == [1 ]
96- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), (1 ,), q_lv ))
96+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), (1 ,), q_lv ))
9797 assert res [0 ] == (1 ,)
98- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), etuple (1 ,), q_lv , skip_op = False ))
98+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), etuple (1 ,), q_lv , skip_op = False ))
9999 assert res [0 ] == etuple (1 ,)
100- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), q_lv , (1 ,)))
100+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), q_lv , (1 ,)))
101101 assert res [0 ] == (1 ,)
102- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), q_lv , [1 ]))
102+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), q_lv , [1 ]))
103103 assert res [0 ] == [1 ]
104- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), q_lv , etuple (1 ), skip_op = False ))
104+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), q_lv , etuple (1 ), skip_op = False ))
105105 assert res [0 ] == etuple (1 )
106- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], [1 , 2 ]))
106+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], [1 , 2 ]))
107107 assert len (res ) == 1
108- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], [1 , 3 ]))
108+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], [1 , 3 ]))
109109 assert len (res ) == 0
110- res = run (1 , q_lv , lapply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], (1 , 2 )))
110+ res = run (1 , q_lv , seq_apply_anyo (lambda x , y : eq (x , y ), [1 , 2 ], (1 , 2 )))
111111 assert len (res ) == 0
112- res = run (0 , q_lv , lapply_anyo (lambda x , y : eq (y , etuple (mul , 2 , x )),
112+ res = run (0 , q_lv , seq_apply_anyo (lambda x , y : eq (y , etuple (mul , 2 , x )),
113113 etuple (add , 1 , 2 ), q_lv , skip_op = True ))
114114 assert len (res ) == 3
115115 assert all (r [0 ] == add for r in res )
116116
117117
118- def test_lapply_misc ():
118+ def test_seq_apply_anyo_misc ():
119119 q_lv = var ('q' )
120120
121- assert len (run (0 , q_lv , lapply_anyo (eq , [1 , 2 , 3 ], [1 , 2 , 3 ]))) == 1
121+ assert len (run (0 , q_lv , seq_apply_anyo (eq , [1 , 2 , 3 ], [1 , 2 , 3 ]))) == 1
122122
123- assert len (run (0 , q_lv , lapply_anyo (eq , [1 , 2 , 3 ], [1 , 3 , 3 ]))) == 0
123+ assert len (run (0 , q_lv , seq_apply_anyo (eq , [1 , 2 , 3 ], [1 , 3 , 3 ]))) == 0
124124
125- assert len (run (4 , q_lv , lapply_anyo (math_reduceo , [etuple (mul , 2 , var ('x' ))], q_lv ))) == 0
125+ def one_to_threeo (x , y ):
126+ return conde ([eq (x , 1 ), eq (y , 3 )])
126127
127- test_res = run (4 , q_lv , lapply_anyo (math_reduceo , [etuple (add , 2 , 2 ), 1 ], q_lv ))
128+ res = run (0 , q_lv , seq_apply_anyo (one_to_threeo ,
129+ [1 , 2 , 4 , 1 , 4 , 1 , 1 ],
130+ q_lv ))
131+
132+ assert res [0 ] == [3 , 2 , 4 , 3 , 4 , 3 , 3 ]
133+
134+ assert len (run (4 , q_lv , seq_apply_anyo (math_reduceo , [etuple (mul , 2 , var ('x' ))], q_lv ))) == 0
135+
136+ test_res = run (4 , q_lv , seq_apply_anyo (math_reduceo , [etuple (add , 2 , 2 ), 1 ], q_lv ))
128137 assert test_res == ([etuple (mul , 2 , 2 ), 1 ],)
129138
130- test_res = run (4 , q_lv , lapply_anyo (math_reduceo , [1 , etuple (add , 2 , 2 )], q_lv ))
139+ test_res = run (4 , q_lv , seq_apply_anyo (math_reduceo , [1 , etuple (add , 2 , 2 )], q_lv ))
131140 assert test_res == ([1 , etuple (mul , 2 , 2 )],)
132141
133- test_res = run (4 , q_lv , lapply_anyo (math_reduceo , q_lv , var ('z' )))
142+ test_res = run (4 , q_lv , seq_apply_anyo (math_reduceo , q_lv , var ('z' )))
134143 assert all (isinstance (r , list ) for r in test_res )
135144
136- test_res = run (4 , q_lv , lapply_anyo (math_reduceo , q_lv , var ('z' ), tuple ()))
145+ test_res = run (4 , q_lv , seq_apply_anyo (math_reduceo , q_lv , var ('z' ), tuple ()))
137146 assert all (isinstance (r , tuple ) for r in test_res )
138147
139148
@@ -153,11 +162,11 @@ def test_lapply_misc():
153162 ([etuple (mul , 2 , 1 ), 5 ],
154163 [etuple (add , 1 , 1 ), 5 ],
155164 [etuple (mul , 2 , 1 ), etuple (log , etuple (exp , 5 ))]))])
156- def test_lapply_anyo (test_input , test_output ):
157- """Test `lapply_anyo ` with fully ground terms (i.e. no logic variables)."""
165+ def test_seq_apply_anyo (test_input , test_output ):
166+ """Test `seq_apply_anyo ` with fully ground terms (i.e. no logic variables)."""
158167 q_lv = var ()
159168 test_res = run (0 , q_lv ,
160- (lapply_anyo , full_math_reduceo , test_input , q_lv ))
169+ (seq_apply_anyo , full_math_reduceo , test_input , q_lv ))
161170
162171 assert len (test_res ) == len (test_output )
163172
@@ -175,18 +184,18 @@ def test_lapply_anyo(test_input, test_output):
175184 assert test_res == test_output
176185
177186
178- def test_lapply_anyo_reverse ():
179- """Test `lapply_anyo ` in "reverse" (i.e. specify the reduced form and generate the un-reduced form)."""
187+ def test_seq_apply_anyo_reverse ():
188+ """Test `seq_apply_anyo ` in "reverse" (i.e. specify the reduced form and generate the un-reduced form)."""
180189 # Unbounded reverse
181190 q_lv = var ()
182191 rev_input = [etuple (mul , 2 , 1 )]
183- test_res = run (4 , q_lv , (lapply_anyo , math_reduceo , q_lv , rev_input ))
192+ test_res = run (4 , q_lv , (seq_apply_anyo , math_reduceo , q_lv , rev_input ))
184193 assert test_res == ([etuple (add , 1 , 1 )],
185194 [etuple (log , etuple (exp , etuple (mul , 2 , 1 )))])
186195
187196 # Guided reverse
188197 test_res = run (4 , q_lv ,
189- (lapply_anyo , math_reduceo ,
198+ (seq_apply_anyo , math_reduceo ,
190199 [etuple (add , q_lv , 1 )],
191200 [etuple (mul , 2 , 1 )]))
192201
@@ -203,6 +212,15 @@ def test_graph_applyo_misc():
203212
204213 assert len (run (0 , q_lv , graph_applyo (eq , etuple (), etuple (), preprocess_graph = None ))) == 1
205214
215+ def one_to_threeo (x , y ):
216+ return conde ([eq (x , 1 ), eq (y , 3 )])
217+
218+ res = run (0 , q_lv , graph_applyo (one_to_threeo ,
219+ [1 , [1 , 2 , 4 ], 2 , [[4 , 1 , 1 ]], 1 ],
220+ q_lv , preprocess_graph = None ))
221+
222+ assert res [0 ] == [3 , [3 , 2 , 4 ], 2 , [[4 , 3 , 3 ]], 3 ]
223+
206224
207225@pytest .mark .parametrize (
208226 'test_input, test_output' ,
0 commit comments