Source

Pyrolog / prolog / interpreter / test / test_builtin.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import py
from prolog.interpreter.parsing import parse_file, TermBuilder
from prolog.interpreter.parsing import parse_query_term, get_engine
from prolog.interpreter.error import UnificationFailed
from prolog.interpreter.continuation import Heap, Engine
from prolog.interpreter import error
from prolog.interpreter.test.tool import collect_all, assert_false, assert_true
from prolog.interpreter.test.tool import prolog_raises

def test_fail():
    e = get_engine("""
        g(a).
        f(X) :- g(X), fail.
        f(a).
    """)
    heaps = collect_all(e, "f(X).")
    assert len(heaps) == 1

def test_not():
    assert_true("not(fail).")
    assert_false("not(true).")
    e = get_engine("""
        g(a, a).
        g(b, a).
        g(b, b).

        m(o, a).
        m(o, b).
        m(o, c).
        same(X, X).

        sibling(X, Y) :- m(Z, X), m(Z, Y), \\+same(X, Y).
    """)
    assert_true("not(g(b, c)).", e)
    assert_false("not(g(a, a)).", e)
    assert_true("\\+(g(b, c)).", e)
    assert_false("\\+(g(a, a)).", e)

    heaps = collect_all(e, "sibling(a, X).")
    assert len(heaps) == 2

def test_and():
    assert_false("fail, X.")
    prolog_raises("type_error(callable, 1)", "(fail, 1)")

def test_nonvar():
    e = get_engine("""
        g(X) :- nonvar(X).
        g(x, X) :- nonvar(x), nonvar(X).
        f(X, Y) :- nonvar(X), nonvar(Y).
    """)
    assert_true("g(a).", e)
    assert_false("g(X).", e)
    assert_true("g(x).", e)
    assert_true("g(x, a).", e)
    assert_true("g(X, X).", e)
    assert_false("f(X, X).", e)

def test_consult():
    p = py.test.ensuretemp("prolog")
    f = p.join("test.pl")
    f.write("g(a, a). g(a, b).")
    e = get_engine("g(c, c).")
    assert_true("g(c, c).", e)
    assert_true("consult('%s')." % (f, ), e)
    assert_true("g(c, c).", e)
    assert_true("g(a, a).", e)
    assert_true("g(a, b).", e)
    prolog_raises("_", "consult('/hopefully/does/not/exist')")

def test_assert_retract():
    e = get_engine("g(b, b).")
    assert_true("g(B, B).", e)
    assert_true("assert(g(c, d)).", e)
    assert_true("assert(g(a, b)).", e)
    assert_true("assert(g(a, b)).", e) # assert the same rule multiple times
    assert_true("g(B, B).", e)
    assert_true("g(a, b).", e)
    assert_true("g(c, d).", e)
    assert_true("retract(g(B, B)).", e)
    assert_false("g(B, B).", e)
    assert_true("retract(g(a, b)).", e)
    assert_true("g(a, b).", e)
    assert_true("retract(g(a, b)).", e)
    assert_false("retract(g(a, b)).", e)
    assert_false("g(a, b).", e)
    assert_true("g(c, d).", e)
    e = get_engine("""
        g(b, b).
        f(X) :- g(X, b).
        f(a).
    """)
    assert_true("f(b).", e)
    assert_true("f(a).", e)
    assert_true("retract(f(X) :- g(X, Y)), Y == b.", e)
    assert_false("f(b).", e)
    assert_true("f(a).", e)
    prolog_raises("permission_error(X, Y, Z)", "retract(atom(X))")

def test_assert_at_right_end():
    e = get_engine("g(b, b). f(b, b). h(b, b).")
    assert_true("assert(g(a, a)).", e)
    assert_true("assertz(f(a, a)).", e)
    assert_true("A = a, asserta(h(A, A)).", e)
    f = assert_true("g(B, B).", e)
    assert f['B'].name()== "b"
    f = assert_true("f(B, B).", e)
    assert f['B'].name()== "b"
    assert_false("h(c, c).", e)
    f = assert_true("h(B, B).", e)
    assert f['B'].name()== "a"

def test_assert_logical_update_view():
    e = get_engine("""
        g(a).
        g(c) :- assertz(g(d)).
        g(b).
    """)
    heaps = collect_all(e, "g(X).")
    assert len(heaps) == 3
    e = get_engine("""
        p :- assertz(p), fail.
        p :- fail.
    """)
    assert_false("p.", e)
    e = get_engine("""
        q :- fail.
        q :- assertz(q), fail.
    """)
    assert_false("q.", e)

def test_retract_logical_update_view():
    e = get_engine("""
        p :- retract(p :- true), fail.
        p :- true.
    """)
    assert_true("p.", e)
    assert_false("p.", e)

def test_abolish():
    e = get_engine("g(b, b). g(c, c). g(a). f(b, b). h(b, b).")
    assert_true("abolish(g/2).", e)
    assert_true("g(a).", e)
    prolog_raises("existence_error(X, Y)", "g(A, B)", e)
    prolog_raises("type_error(predicate_indicator, a)", "abolish(a)", e)

def test_unify():
    assert_true("g(b, B) = g(b, b).")
    assert_true("X = Y.")
    assert_true("X = f(X).")
    assert_false("g(b, B) \\= g(b, b).")
    assert_false("X \\= Y.")
    assert_false("X \\= f(X).")
    assert_true("x \\= y.")
    assert_true("f(X, b) \\= f(a, c), X = c.")
    assert_true("unify_with_occurs_check(X, Y).")
    assert_true("unify_with_occurs_check(X, X).")
    assert_false("unify_with_occurs_check(X, f(X)).")
    assert_false("unify_with_occurs_check(X, f(g(h(a, b, c, d(X, e), e)))).")
    assert_false("unify_with_occurs_check(g(X), X).")
    assert_false("X = Y, unify_with_occurs_check(X, f(d(Y), Y)).")

def test_call():
    e = get_engine("g(b, b).")
    assert_true("call(g(X, X)).", e)
    assert_true("X =.. [g, b, b], call(X).", e)
    e = get_engine("""
        g(X) :- call(f(X)).
        g(a).
        g(b).
        f(X) :- !, h(X).
        f(a).
        f(b).
        h(X) :- fail.
        withcut(X) :- call(!), fail.
        withcut(a).
        """)
    heaps = collect_all(e, "g(X).")
    assert len(heaps) == 2
    assert_true("withcut(a).", e)
    assert_true("call((!, true)).")

def test_cut():
    e = get_engine("""
        f(0).
        f(X) :- Y is X - 1, !, f(Y).
        f(X) :- Y is X - 2, !, f(Y).
    """)
    assert_true("f(20).", e)

def test_call_cut():
    py.test.skip("cuts don't work properly in the presence of calls right now")
    e = get_engine("""
        f(X) :- call(X).
        f(!).
    """)
    heaps = collect_all(e, "f(!).")
    assert len(heaps) == 1

def test_or_and_call_with_cut():
    e = get_engine("""
        f :- (!, fail); true.
        g :- (call(!), fail); true.
    """)
    assert_false("f.", e)
    assert_true("g.", e)

def test_or_with_cut():
    e = get_engine("""
        f(X) :- ((X = 1, !); X = 2), X = 2.
        g(X) :- ((X = 1, !); X = 2), X = 1.
    """)
    assert_false("f(X).", e)
    assert_true("g(X).", e)


def test_cut1():
    e = get_engine("""
        g(a).
        g(b).
        a(a).
        b(b).
        f(X) :- g(X),!,b(X).
        f(x).
        f(y).
    """)
    heaps = collect_all(e, "f(X).")
    assert len(heaps) == 0
    assert_true("!.")

def test_cut2():
    e = get_engine("""
        g(a).
        g(b).
        h(a, x).
        h(a, y).
        f(X, Y) :- g(X), !, !, !, !, !, h(X, Y).
    """)
    heaps = collect_all(e, "f(X, Y).")
    assert len(heaps) == 2

def test_cut3():
    e = get_engine("""
        member(H, [H | _]).
        member(H, [_ | T]) :- member(H, T).

        s(a, L) :- !, fail.
        s(b, L).
        s(X, L) :-
            member(Y, L),
            L = [_| S],
            s(Y, S).
    """)
    #    import pdb; pdb.set_trace()
    assert_true("s(d, [a, b]).", e)


def test_rule_with_cut_calling_rule_with_cut():
    e = get_engine("""
        e(a).
        e(b).
        f(b) :- e(_), !.
        f(c).
        g(X) :- f(X), !.
        g(a).
    """)
    heaps = collect_all(e, "g(X).")
    assert len(heaps) == 1

def test_not_with_cut():
    assert_true("not((!, fail)).")
    assert_false("not(!).")

    e = get_engine("""
    p1 :- \\+ q1.
    q1 :- fail.
    q1 :- true.
    
    p2:- \\+ q2.
    q2 :- !, fail.
    q2 :- true.
    """)
    assert_false("p1.", e)
    assert_true("p2.", e)

def test_two_cuts():
    e = get_engine("""
        f(>, X) :- X > 0, !.
        f(=, X) :- X = 0, !.
        f(<, _).
    """)
    assert_true("f(X, 1), X = '>'.", e)
    assert_true("f(X, 0), X = '='.", e)
    assert_true("f(X, -1), X = '<'.", e)

def test_listify():
    e = get_engine("""
        listify(_X, _X) :-
            (var(_X); atomic(_X)), !.
        listify(_Expr, [_Op|_LArgs]) :-
            functor(_Expr, _Op, N),
            listify_list(1, N, _Expr, _LArgs).

        listify_list(I, N, _, []) :- I>N, !.
        listify_list(I, N, _Expr, [_LA|_LArgs]) :- I=<N, !,
            arg(I, _Expr, _A),
            listify(_A, _LA),
            I1 is I+1,
            listify_list(I1, N, _Expr, _LArgs).
    """)
    assert_true("listify(f(X), Y), Y = [f, X].", e)
    assert_true("listify(f(X, g(1)), Y).", e)
    assert_true("listify(f(X, 1, g(1)), Y), Y = [f, X, 1, [g, 1]].", e)


def test_univ():
    assert_true("g(a, b, c) =.. [G, A, B, C].")
    assert_true("g(a, b, c) =.. [g, a, b, c].")
    assert_true("X =.. [g, a, b, c], X = g(a, b, c).")

def test_arg():
    assert_true("arg(1, g(a, b, c), a).")
    assert_true("arg(2, g(a, b, c), b).")
    assert_true("arg(3, g(a, b, c), c).")
    assert_false("arg(3, g(a, b, c), d).")
    assert_false("arg(0, g(a, b, c), X).")
    assert_false("arg(10, g(a, b, c), X).")
    assert_true("arg(1, g(a, b, c), X), X = a.")
    assert_true("arg(2, f(a, b, c), X), X = b.")
    assert_true("arg(3, h(a, b, c), X), X = c.")
    e = get_engine("""
        f(1, a).
        f(2, b).
        f(3, c).
    """)
    heaps = collect_all(e, "arg(X, g(a, b, c), A), f(X, A).")
    assert len(heaps) == 3
    assert_true("arg(X, h(a, b, c), b), X = 2.")
    assert_true("arg(X, h(a, b, g(X, b)), g(3, B)), X = 3, B = b.")

def test_copy_term():
    assert_true("copy_term(X, Y), X = 1, Y = 2.")
    assert_true("copy_term(a, a).")
    assert_false("copy_term(f(X), g(X)).")
    assert_true("copy_term(f(X), f(a)), X = b.")

def test_type_checks():
    assert_true("integer(123).")
    assert_false("integer(a).")
    assert_false("integer(X).")
    assert_true("float(123.12).")
    assert_false("float(a).")
    assert_false("float(12).")
    assert_true("number(123).")
    assert_true("number(42.42).")
    assert_false("number(abc).")
    assert_false("integer(a).")
    assert_false("integer(X).")
    assert_true("var(X).")
    assert_false("X = a, var(X).")
    assert_true("compound(g(a)).")
    assert_false("compound(gxx).")
    assert_false("compound(123).")
    assert_false("compound([]).")
    assert_false("compound(X).")
    assert_true("atom(a).")
    assert_true("atom('asdf').")
    assert_false("atom(12).")
    assert_false("atom(X).")
    assert_true("atomic('asdf').")
    assert_true("atomic(12.5).")
    assert_false("atomic(f(1, 2, 3)).")
    assert_false("atomic(X).")
    assert_false("callable(X).")
    assert_false("callable(1).")
    assert_true("callable(asdf).")
    assert_true("callable(asdf(a, b, c, d, e, f)).")
    assert_true("ground(a).")
    assert_true("ground(t(a, b, f(a, b, g(a, b)))).")
    assert_false("ground(t(a, b, f(a, b, g(a, X)))).")
    assert_true("X = 13, ground(t(a, b, f(a, b, g(a, X)))).")
    assert_false("ground(X).")

def test_repeat():
    assert_true("repeat, true.")
    py.test.raises(UnificationFailed,
        Engine().run, parse_query_term("repeat, !, fail."))
    # hard to test repeat differently
    e = get_engine('f :- repeat, !, fail.')
    assert_false('f.', e)
    assert_true('f; true.', e)
    

def test_exception_handling():
    assert_true("catch(f, E, true).")
    assert_true("catch(throw(error), E, true).")
    prolog_raises("_", "catch(true, E, fail), f")
    prolog_raises("_", "catch(throw(error(x)), error(failure), fail)")
    assert_true("catch(catch(throw(error), failure, fail), error, true).")

def test_between():
    assert_true("between(12, 15, 12).")
    assert_true("between(-5, 15, 0).")
    assert_false("between(12, 15, 6).")
    assert_false("between(12, 15, 16).")
    heaps = collect_all(Engine(), "between(1, 4, X).")
    assert len(heaps) == 4
    assert heaps[0]['X'].num == 1
    assert heaps[-1]['X'].num == 4

def test_is():
    assert_true("5 is 1 + 1 + 1 + 1 + 1.")

@py.test.mark.xfail
def test_parser_access():
    assert_true("current_op(200, xfx, **).")
    f = collect_all(Engine(), "current_op(200, Form, X).")
    assert len(f) == 2
    e = get_engine("""
        foo(a, b).
    """)
    assert_true("op(450, xfy, foo).", e)
    assert_true("a foo b.", e)
    assert_true("op(0, xfy, foo).", e)
    # XXX really a ParseError
    py.test.raises(Exception, assert_false, "a foo b.", e) 
    # change precedence of + for funny results :-)
    assert_true("14 is 2 + 3 * 4.", e)
    assert_true("op(350, xfy, +).", e)
    assert_true("20 is 2 + 3 * 4.", e)
    assert_true("op(500, xfy, +).", e)

def test_functor():
    assert_true("functor(f(a, b, c), f, 3).")
    assert_true("functor(f(a, b, c), X, Y), X=f, Y=3.")
    assert_true("functor(f, X, Y), X=f, Y=0.")
    assert_true("functor(1, X, Y), X=1, Y=0.")
    assert_true("functor(F, a, 0), F=a.")
    assert_true("functor(F, 12, 0), F=12.")
    assert_true("functor(F, 12.5, 0), F=12.5.")
    assert_true("functor(F, f, 4), F=f(1, 2, 3, 4).")
    assert_true("functor(F, g, 1), F=g(asdf).")
    assert_true("functor(F, g, 3), F=g(X, Y, 1), X = 12, Y = 34, ground(F).")

def test_standard_comparison():
    assert_true("X = Y, f(X, Y, X, Y) == f(X, X, Y, Y).")
    assert_true("X = Y, f(X, Y, X, Z) \\== f(X, X, Y, Y).")
    assert_true("""X \\== Y, ((X @< Y, X @=< X, X @=< Y, Y @> X);
                              (X @> Y, X @>= X, X @>= Y, Y @< X)).""")
    assert_true("'\\\\=='(f(X, Y), 12).")
    assert_true("X = f(a), Y = f(b), Y @> X.")

def test_compare():
    assert_true("X = Y, compare(R, f(X, Y, X, Y), f(X, X, Y, Y)), R == '='.")
    assert_true("X = f(a), Y = f(b), compare(R, Y, X), R == '>'.")

def test_atom_length():
    assert_true("atom_length('abc', 3).")
    assert_true("atom_length('\\\\', 1).")
    assert_true("atom_length('abc', X), X = 3.")

def test_atom_concat():
    assert_true("atom_concat(ab, cdef, abcdef).")
    assert_true("atom_concat(ab, cdef, X), X = abcdef.")
    assert_true("atom_concat(ab, X, abcdef), X = cdef.")
    assert_true("atom_concat(X, cdef, abcdef), X = ab.")
    assert_true("atom_concat(1, Y, '1def'), Y = def.")
    heaps = collect_all(
        Engine(),
        "atom_concat(X, Y, abcd), atom(X), atom(Y).")
    assert len(heaps) == 5

def test_sub_atom():
    assert_true("sub_atom(abc, B, L, A, bc), B=1, L=2, A=0.")
def test_sub_atom2():
    assert_false("sub_atom(abc, B, 1, A, bc).")
def test_sub_atom3():
    assert_true("sub_atom(abcabcabc, 3, 3, A, abc), A=3.")
def test_sub_atom4():
    assert_true("sub_atom(abcabcabc, B, L, 3, abc), B=3, L=3.")

def test_sub_atom_with_non_var_sub():
    assert_true("sub_atom(abcabc, Before, Length, After, a), Before=3, Length=1, After=2.")
    assert_false("sub_atom(abcabc, Before, Length, After, b), Before==3, Length==1, After==2.")

def test_sub_atom_with_var_after():
    assert_true("sub_atom(abcabd, 2, 1, After, Sub), After=3, Sub=c.")
    assert_true("sub_atom(abcabc, Before, Length, After, Sub), Before=1, Length=3, After=2, Sub=bca.")
    assert_false("sub_atom(abcabc, 1, 3, After, Sub), Sub=abc.")

def test_sub_atom_var_sub_and_non_var_after():
    assert_true("sub_atom(abcabd, 2, 1, 3, Sub), Sub=c.")
    assert_true("sub_atom(abcabc, Before, Length, 2, Sub), Before=1, Length=3, Sub=bca.")
    assert_false("sub_atom(abcabc, 1, 3, 2, Sub), Sub=abc.")

def test_findall():
    assert_true("findall(X, (X = a; X = b; X = c), L), L == [a, b, c].")
    assert_true("findall(X + Y, (X = 1; X = 2), [1+A, 2+B]), A \== B.")
    e = get_engine("""
        app([], X, X).
        app([H | T1], T2, [H | T3]) :-
            app(T1, T2, T3).
    """)
    assert_true("findall(X+Y, app(X, Y, [1, 2, 3]), L), L == [[]+[1, 2, 3], [1]+[2, 3], [1, 2]+[3], [1, 2, 3]+[]].", e)


def test_ifthenelse():
    assert_false("true -> fail.")
    assert_false("true -> fail ; true.")
    assert_true("fail -> fail ; true.")
    assert_true("fail -> true ; true.")
    assert_true("(true -> fail ; true) ; true.")

    e = get_engine("f(x). f(y). f(z).")
    assert_false("f(c) -> true.", e)
    assert_false("f(X) -> X \\= x; f(z).", e)
    assert_true("f(X) -> X == x; f(z).", e)

def test_once():
    assert_true("once(repeat).")

def test_write_term():
    py.test.skip("test behaves funnily")
    prolog_raises("domain_error(write_option, E)",
                  "write_term(a, [quoted(af)])")
    prolog_raises("type_error(list, E)",
                  "write_term(a, asdf)")