rubylog 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +18 -0
  4. data/Gemfile.lock +64 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +96 -0
  7. data/Rakefile +53 -0
  8. data/VERSION +1 -0
  9. data/examples/4queens.rb +10 -0
  10. data/examples/calculation.rb +12 -0
  11. data/examples/concepts.rb +46 -0
  12. data/examples/factorial.rb +16 -0
  13. data/examples/fp.rb +56 -0
  14. data/examples/hello.rb +9 -0
  15. data/examples/historia_de_espana.rb +31 -0
  16. data/examples/idea.rb +143 -0
  17. data/examples/lists.rb +5 -0
  18. data/examples/mechanika.rb +409 -0
  19. data/examples/parse.rb +15 -0
  20. data/examples/theory.rb +20 -0
  21. data/lib/array.rb +24 -0
  22. data/lib/class.rb +11 -0
  23. data/lib/method.rb +4 -0
  24. data/lib/object.rb +5 -0
  25. data/lib/proc.rb +4 -0
  26. data/lib/rubylog/builtins.rb +193 -0
  27. data/lib/rubylog/callable.rb +20 -0
  28. data/lib/rubylog/clause.rb +113 -0
  29. data/lib/rubylog/composite_term.rb +38 -0
  30. data/lib/rubylog/dsl/constants.rb +15 -0
  31. data/lib/rubylog/dsl/first_order_functors.rb +9 -0
  32. data/lib/rubylog/dsl/global_functors.rb +3 -0
  33. data/lib/rubylog/dsl/second_order_functors.rb +8 -0
  34. data/lib/rubylog/dsl.rb +52 -0
  35. data/lib/rubylog/errors.rb +18 -0
  36. data/lib/rubylog/internal_helpers.rb +16 -0
  37. data/lib/rubylog/predicate.rb +34 -0
  38. data/lib/rubylog/proc_method_additions.rb +69 -0
  39. data/lib/rubylog/term.rb +20 -0
  40. data/lib/rubylog/theory.rb +133 -0
  41. data/lib/rubylog/unifiable.rb +19 -0
  42. data/lib/rubylog/variable.rb +97 -0
  43. data/lib/rubylog.rb +39 -0
  44. data/lib/symbol.rb +35 -0
  45. data/rubylog.gemspec +187 -0
  46. data/script/inriasuite2spec +0 -0
  47. data/script/inriasuite2spec.pl +22 -0
  48. data/spec/bartak_guide_spec.rb +91 -0
  49. data/spec/inriasuite/README +122 -0
  50. data/spec/inriasuite/abolish +18 -0
  51. data/spec/inriasuite/and +9 -0
  52. data/spec/inriasuite/arg +32 -0
  53. data/spec/inriasuite/arith_diff +10 -0
  54. data/spec/inriasuite/arith_eq +10 -0
  55. data/spec/inriasuite/arith_gt +10 -0
  56. data/spec/inriasuite/arith_gt= +10 -0
  57. data/spec/inriasuite/arith_lt +10 -0
  58. data/spec/inriasuite/arith_lt= +10 -0
  59. data/spec/inriasuite/asserta +18 -0
  60. data/spec/inriasuite/assertz +16 -0
  61. data/spec/inriasuite/atom +12 -0
  62. data/spec/inriasuite/atom_chars +19 -0
  63. data/spec/inriasuite/atom_codes +15 -0
  64. data/spec/inriasuite/atom_concat +19 -0
  65. data/spec/inriasuite/atom_length +12 -0
  66. data/spec/inriasuite/atomic +11 -0
  67. data/spec/inriasuite/bagof +31 -0
  68. data/spec/inriasuite/call +19 -0
  69. data/spec/inriasuite/catch-and-throw +16 -0
  70. data/spec/inriasuite/char_code +13 -0
  71. data/spec/inriasuite/clause +16 -0
  72. data/spec/inriasuite/compound +12 -0
  73. data/spec/inriasuite/copy_term +25 -0
  74. data/spec/inriasuite/current_input +5 -0
  75. data/spec/inriasuite/current_output +5 -0
  76. data/spec/inriasuite/current_predicate +16 -0
  77. data/spec/inriasuite/current_prolog_flag +12 -0
  78. data/spec/inriasuite/cut +9 -0
  79. data/spec/inriasuite/fail +15 -0
  80. data/spec/inriasuite/file_manip +8 -0
  81. data/spec/inriasuite/findall +22 -0
  82. data/spec/inriasuite/float +10 -0
  83. data/spec/inriasuite/functor +41 -0
  84. data/spec/inriasuite/functor-bis +41 -0
  85. data/spec/inriasuite/halt +7 -0
  86. data/spec/inriasuite/if-then +10 -0
  87. data/spec/inriasuite/if-then-else +12 -0
  88. data/spec/inriasuite/inriasuite.obp +0 -0
  89. data/spec/inriasuite/inriasuite.pl +836 -0
  90. data/spec/inriasuite/integer +10 -0
  91. data/spec/inriasuite/is +11 -0
  92. data/spec/inriasuite/junk +0 -0
  93. data/spec/inriasuite/nonvar +11 -0
  94. data/spec/inriasuite/not_provable +12 -0
  95. data/spec/inriasuite/not_unify +15 -0
  96. data/spec/inriasuite/number +10 -0
  97. data/spec/inriasuite/number_chars +22 -0
  98. data/spec/inriasuite/number_codes +19 -0
  99. data/spec/inriasuite/once +11 -0
  100. data/spec/inriasuite/or +9 -0
  101. data/spec/inriasuite/repeat +5 -0
  102. data/spec/inriasuite/retract +10 -0
  103. data/spec/inriasuite/set_prolog_flag +21 -0
  104. data/spec/inriasuite/setof +36 -0
  105. data/spec/inriasuite/sub_atom +30 -0
  106. data/spec/inriasuite/t +1 -0
  107. data/spec/inriasuite/t_foo.pl +4 -0
  108. data/spec/inriasuite/term_diff +13 -0
  109. data/spec/inriasuite/term_eq +12 -0
  110. data/spec/inriasuite/term_gt +12 -0
  111. data/spec/inriasuite/term_gt= +12 -0
  112. data/spec/inriasuite/term_lt +12 -0
  113. data/spec/inriasuite/term_lt= +12 -0
  114. data/spec/inriasuite/true +7 -0
  115. data/spec/inriasuite/unify +18 -0
  116. data/spec/inriasuite.rb +20 -0
  117. data/spec/recursion_spec.rb +18 -0
  118. data/spec/rubylog/builtins/splits_to.rb +18 -0
  119. data/spec/rubylog/clause_spec.rb +81 -0
  120. data/spec/rubylog/variable_spec.rb +25 -0
  121. data/spec/rubylog_spec.rb +914 -0
  122. data/spec/spec_helper.rb +12 -0
  123. data/spec/theory_spec.rb +1 -0
  124. metadata +339 -0
@@ -0,0 +1,836 @@
1
+
2
+ %%%%%%%%%%%%%%%%%%%%%%%
3
+ %
4
+ %
5
+ % inriasuite.pl
6
+ %
7
+ % Author J.P.E. Hodgson
8
+ % date 9 february 1999
9
+ %
10
+ % Version 0.9
11
+ %
12
+ % This is to be a batch version of
13
+ % Ali's tests. It will read lines from a file
14
+ % that are in the form [ Goal, Substs].
15
+ %
16
+ %
17
+ % Modified 1999/02/24 to read several files and
18
+ % report on them one by one. Output results
19
+ % only when the result is not the expected one.
20
+ %
21
+ % A more readable output can be obtained if the processor
22
+ % supports numbervars/3 by restoring the commented out
23
+ % line in write_if_wrong/5.
24
+ %
25
+ % Revised April 8 1999.
26
+ %
27
+ % Matching of solutions is not yet perfected.
28
+ %
29
+
30
+
31
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%
32
+ %
33
+ % Operators required for the
34
+ % tests.
35
+ %
36
+
37
+
38
+ :- op( 20, xfx, <-- ).
39
+
40
+
41
+
42
+ %%%%%%%%%%%%%%%%%%%%
43
+ %
44
+ % score/3 is dynamic.
45
+ %
46
+ % score(File, total(Tests), wrong(PossibleErrors)
47
+ %
48
+
49
+
50
+
51
+ :- dynamic(score/3).
52
+
53
+
54
+ %%%%%%%%%%%%%%%%%%%%%%%%%
55
+ %
56
+ % dynamic directives needed for the compiled
57
+ % version of the tests.
58
+ %
59
+
60
+ :- dynamic(bar/1). % for asserta
61
+ :- dynamic(foo/1). % for assertz
62
+
63
+ %%%%%%%%%%%%%%%%
64
+ %
65
+ % run_all_tests/0
66
+ %
67
+ % Driver.
68
+
69
+
70
+ run_all_tests :-
71
+ findall(F, file(F), Files),
72
+ test_all(Files),
73
+ write_results, !.
74
+
75
+
76
+ test_all([]).
77
+ test_all([F|Fs]) :-
78
+ run_tests(F),
79
+ test_all(Fs).
80
+
81
+ %%%%%%%%%%%%%%%%%%%%%%%%%
82
+ %
83
+ % write_results/0.
84
+ %
85
+
86
+ write_results :-
87
+ findall(F, inerror(F), ErrorBips),
88
+ write('--------------------'), nl,
89
+ (
90
+ ErrorBips = []
91
+ ->
92
+ (
93
+ write('All bips passed -------------'), nl
94
+ )
95
+ ;
96
+ (nl, write('The following BIPs gave unexpected answers:'),nl,
97
+ write('The results should be examined carefully.'), nl,
98
+ nl,
99
+ display_list(ErrorBips))
100
+ ).
101
+
102
+
103
+
104
+
105
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
106
+ %
107
+ % result(+Goal, -Result)
108
+ %
109
+ % evaluates the Goal and gives all the substitutions
110
+ %
111
+
112
+ result(G, Res) :-
113
+ get_all_subs(G, Subs),
114
+ special_ans_forms(Subs,Res).
115
+
116
+
117
+
118
+ %%%%%%%%%%%%%%%%%%%%%%%%%
119
+ %
120
+ % certain substitutions appear in
121
+ % a simplified form.
122
+ %
123
+
124
+
125
+ special_ans_forms([success], success) :- !.
126
+ special_ans_forms([failure], failure) :- !.
127
+ special_ans_forms([Error], Error) :-
128
+ Error =..[E |_],error_type(E), !.
129
+ special_ans_forms(X,X).
130
+
131
+ %%%%%%%%%%%%%%%%
132
+ %
133
+ % error_type(+E).
134
+ %
135
+
136
+ error_type(instantiation_error).
137
+ error_type(type_error).
138
+ error_type(domain_error).
139
+ error_type(existence_error).
140
+ error_type(permission_error).
141
+ error_type(representation_error).
142
+ error_type(evaluation_error).
143
+ error_type(resource_error).
144
+ error_type(syntax_error).
145
+ error_type(system_error).
146
+ error_type(unexpected_ball). % for uncaught errors.
147
+
148
+
149
+
150
+
151
+
152
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
+ %
154
+ % Extract the variables from a term
155
+ %
156
+ % vars_int_term(+Term, -Vars)
157
+ %
158
+
159
+ vars_in_term(T,V) :-
160
+ vars_in_term(T, [], V).
161
+
162
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
+ %
164
+ % vars_in_term(+Term, +AlreadyCollected, -Variables).
165
+ %
166
+
167
+ % atoms (includes []).
168
+
169
+ vars_in_term(Term,VarsIn, VarsOut) :-
170
+ atomic(Term), !, VarsOut= VarsIn.
171
+ % Term is a variable
172
+
173
+ vars_in_term(Term, VarsIn, VarsOut) :-
174
+ var(Term) ,!,
175
+ (already_appears(Term, VarsIn)
176
+ ->
177
+ VarsOut=VarsIn
178
+ ;
179
+ append(VarsIn, [Term], VarsOut)
180
+ ).
181
+
182
+ % Term is a list.
183
+
184
+ vars_in_term([A|B], VarsIn, Vars) :-
185
+ !,
186
+ vars_in_term(A, VarsIn, V1),
187
+ vars_in_term(B, V1, Vars).
188
+
189
+ % Term is a functor.
190
+
191
+ vars_in_term(T,VarsIn, VarList) :-
192
+ T =.. [_F,A|Args],
193
+ vars_in_term(A, VarsIn, Inter),
194
+ vars_in_term(Args, Inter, VarList).
195
+
196
+ %%%%%%%
197
+ %
198
+ % already_appears(+Var,+VarList)
199
+ %
200
+ % The variable Var is in the list VarList
201
+ %
202
+
203
+
204
+ already_appears(Var, [V1 | _Vlist] ) :-
205
+ Var == V1.
206
+ already_appears(Var, [_V1 | Vlist] ) :-
207
+ already_appears(Var, Vlist).
208
+
209
+
210
+
211
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212
+ %
213
+ %
214
+ % call_goal_get_subs(+Goal, -Sub)
215
+ %
216
+ % call a goal Goal and get the substitutions
217
+ % associated to success.
218
+ %
219
+
220
+
221
+ call_goal_get_subs(G, Sub) :-
222
+ copy_term(G,GT),
223
+ vars_in_term(G,Vars),
224
+ vars_in_term(GT, GVars),
225
+ call(GT),
226
+ make_subs_list1(Vars, GVars, Sub).
227
+
228
+
229
+
230
+
231
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232
+ %
233
+ % make_subs_list1(+OldVars, +Result, -Sub)
234
+ %
235
+ % handles the speical cases else hands off
236
+ % to make_subs_list(OldVars, Result, Sub)
237
+ % and compress to handle [X <-- A, Y <-- A].
238
+ %
239
+
240
+ % special cases
241
+
242
+ make_subs_list1(_V, success, success).
243
+ make_subs_list1(_V, failure, failure).
244
+ make_subs_list1(_V, impl_def, impl_def).
245
+ make_subs_list1(_V, undefined, undefined).
246
+ make_subs_list1(_V, Error, Error) :-
247
+ Error =.. [E|_],
248
+ error_type(E), !.
249
+
250
+ make_subs_list1(Vs,GVs,Sub) :-
251
+ make_subs_list(Vs, GVs, S),
252
+ compress_sub_list(Vs, S, Sub).
253
+
254
+
255
+
256
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%
257
+ %
258
+ % make_subs_list(+Vars, +Result, -Subs).
259
+
260
+
261
+ make_subs_list([],[], []).
262
+
263
+ % no instantiation.
264
+
265
+ make_subs_list([V | Rest], [Ans |ARest], Subs) :-
266
+ V == Ans , !,
267
+ make_subs_list(Rest, ARest, Subs).
268
+
269
+ % Instantiation.
270
+
271
+ make_subs_list([V | Rest], [Ans |ARest], [ V <-- Ans | SubsRest]) :-
272
+ make_subs_list(Rest, ARest, SubsRest).
273
+
274
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275
+ %
276
+ % list_make_subs(+Vars, +GTVars, -Subs).
277
+ %
278
+ % Make substitution lists for Vars according to
279
+ % the set of instantiations given in GTVars.
280
+ %
281
+
282
+ list_make_subs_list(_, [], [failure]) :- !.
283
+ list_make_subs_list(V, GTV,S) :-
284
+ list_make_subs_list_aux(V,GTV, S).
285
+
286
+ list_make_subs_list_aux(_Vars, [], []).
287
+ list_make_subs_list_aux(Vars, [GV1 |GVRest], [Sub1 |SubRest]) :-
288
+ make_subs_list1(Vars, GV1, Sub1),
289
+ list_make_subs_list_aux(Vars, GVRest, SubRest).
290
+
291
+
292
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
+ %
294
+ %
295
+ % call_with_result(G,R)
296
+ %
297
+
298
+ call_with_result(G,R ) :-
299
+ call_goal_get_subs(G, Sub),
300
+ ( Sub = [] -> R = success; R = Sub).
301
+ call_with_result(_G, failure).
302
+
303
+
304
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%
305
+ %
306
+ %
307
+ % protected_call_results(G,R)
308
+ %
309
+
310
+ protected_call_results(G,R) :-
311
+ catch(call_with_result(G,R), B, R = B).
312
+
313
+
314
+ %%%%%%%%%%%%%%%%%%%%%%%%%%
315
+ %
316
+ %
317
+ %
318
+ % get_all_subs(G, AllSubs)
319
+ %
320
+ % No errors
321
+ %
322
+ % Find all the substitutions for the goal G.
323
+ %
324
+
325
+ get_all_subs(G, AllSubs) :-
326
+ copy_term(G,GT),
327
+ vars_in_term(G, GVars),
328
+ findall(GTAns, protect_call_result(GT, GTAns), GTAnsList),
329
+ list_make_subs_list(GVars, GTAnsList, AllSubs).
330
+
331
+
332
+
333
+ %%%%%%%%%%%%%%
334
+ %
335
+ % call_result(+Goal, -VarsAfterCall).
336
+ % instantiates VarsAfterCall to the values
337
+ % of the variables in the goal after a call of the goal.
338
+ %
339
+
340
+ call_result(G,R) :-
341
+
342
+ vars_in_term(G, GVars),
343
+ call(G),
344
+
345
+ R = GVars.
346
+
347
+ %%%%%%%%%%%%%%%%%%%%%%%%%
348
+ %
349
+ %
350
+ % protect_call_result(G,R)
351
+ %
352
+ % protected version of call_result/2.
353
+ %
354
+
355
+ protect_call_result(G,R) :-
356
+ catch(call_result(G,R), B, extract_error(B,R)).
357
+
358
+
359
+ %%%%%%%%%%%%%%%
360
+ %
361
+ % extract_error(+Ball, -Error)
362
+ %
363
+
364
+ extract_error(error(R, _), R) :- !.
365
+ extract_error(B, unexpected_ball(B)).
366
+
367
+
368
+
369
+
370
+ %%%%%%%%%%%%%%%%%%%%%
371
+ %
372
+ % compress_sub_list(+Vars, +LIn, -LOut)
373
+ %
374
+ % to replace pairs [X <--A, Y <-- A] by [Y <-- X]
375
+ % when A is not one of the original variables.
376
+
377
+
378
+ compress_sub_list(_, [], success).
379
+ compress_sub_list(Vars, [X <-- A], [X <-- A]) :- X \== A, in_vars(A, Vars).
380
+ compress_sub_list(Vars,LIn, LOut) :-
381
+ split_list(X <-- A, Before, After, LIn),
382
+ var(A),!,
383
+ sub(X <-- A, Before, BN),
384
+ sub(X <-- A, After, AN),
385
+ append(BN,AN, L1),
386
+ compress_sub_list(Vars, L1, LOut).
387
+
388
+ compress_sub_list(_,L,L).
389
+
390
+ %%%%%%%%%%%%%%%%%%%%%%%%
391
+ %
392
+ % in_vars(Var, VarList)
393
+ %
394
+
395
+ in_vars(V, [V1 |_Vs]) :-
396
+ V == V1, !.
397
+ in_vars(V, [_V1 |Vs]) :-
398
+ in_vars(V, Vs).
399
+
400
+
401
+
402
+ %%%%%%%%%%%%%%%%%%%
403
+ %
404
+ % sub(X <-- A, OldList, NewList)
405
+ %
406
+ % substitute A for X in OldList giving NewList.
407
+ %
408
+
409
+ sub(_X <-- _A, [], []).
410
+ sub(X <-- A, [H|T], [H1|T1]) :-
411
+ sub1(X <-- A, H,H1),
412
+ sub( X <-- A, T,T1).
413
+
414
+ %%%%%%%%%%%%%%%%%%%%%
415
+ %
416
+ %
417
+ % sub1(X <-- A, Y <-- Old, Y <-- New)
418
+ %
419
+ % perform a single substitution.
420
+ %
421
+
422
+ sub1(X <-- A, Y <-- Old, Y <-- New) :-
423
+ exp_sub(X<-- A, Old, New).
424
+
425
+ exp_sub(X <-- A, B, New) :-
426
+ var(B), B== A, !,
427
+ New = X.
428
+ exp_sub(_X <-- _A, B, New) :-
429
+ var(B), !,
430
+ New = B.
431
+ exp_sub(_X <-- _A, B, New) :-
432
+ atomic(B), !,
433
+ New = B.
434
+ exp_sub(X <-- A, B, New) :-
435
+ B = [_|_],!,
436
+ list_exp_sub(X <-- A, B, New).
437
+ exp_sub(X <-- A, B, New) :-
438
+ B =.. [F|L],
439
+ list_exp_sub(X <-- A, L,L1),
440
+ New =.. [F|L1].
441
+
442
+ list_exp_sub(_S, [],[]).
443
+ list_exp_sub(S, [E|ER], [EN|ERN]) :-
444
+ exp_sub(S, E, EN),
445
+ list_exp_sub(S, ER, ERN).
446
+
447
+
448
+
449
+ %%%%%%%%%%%%%%%%%%%%%%
450
+ %
451
+ %
452
+ % split_list(?Element,-Before, -After, +List)
453
+ %
454
+ % split a list List at a given Element.
455
+ %
456
+
457
+
458
+ split_list(Element, Before, After, List) :-
459
+ append(Before, [Element | After], List).
460
+
461
+
462
+
463
+
464
+
465
+ %%%%%%%%%%%%%%%%%%%%%%%
466
+ %
467
+ %
468
+ % compare_subst_lists(+First,
469
+ % +Second,
470
+ % +InFirstButNotSecond,
471
+ % +InSecondButNotFirst
472
+ % )
473
+ % compare two substitution lists.
474
+ %
475
+
476
+ % special cases
477
+ compare_subst_lists(F,S, [],[]) :-
478
+ \+ (F = [_|_]),
479
+ \+ (S = [_|_]),
480
+ F = S, !.
481
+ compare_subst_lists(F,S, F,S) :-
482
+ \+ (F = [_|_]),
483
+ \+ (S = [_|_]), !.
484
+ compare_subst_lists(F,S, FNS, SNF) :-
485
+ \+(F = [_|_]), !,
486
+ del_item(F, S, SNF),
487
+ (member(F,S) -> FNS =[]; FNS = F).
488
+ compare_subst_lists(F,S, FNS,SNF) :-
489
+ \+( S = [_|_]), !,
490
+ del_item(S, F, FNS),
491
+ (member(S,F) -> SNF =[]; SNF = S).
492
+
493
+ compare_subst_lists(F, S, [], []) :-
494
+ F= [F1], S = [S1],
495
+ same_subst(F1, S1), !.
496
+
497
+ compare_subst_lists(F, S, F, S) :-
498
+ length(F,1),
499
+ length(S,1), !.
500
+
501
+ compare_subst_lists(F,S, FNS,SNF) :-
502
+ length(F,1),!,
503
+ del_item(F, S, SNF),
504
+ (member(F,S) -> FNS =[]; FNS = F).
505
+ compare_subst_lists(F,S, FNS,SNF) :-
506
+ length(S,1),
507
+ del_item(S, F, FNS),
508
+ (member(S,F) -> SNF =[]; SNF = S).
509
+
510
+
511
+ compare_subst_lists(F,S, FNS, SNF) :-
512
+ list_del_item(F,S, SNF),
513
+ list_del_item(S,F, FNS).
514
+
515
+
516
+ %list_del_item(L1, L2, L2LessL1)
517
+
518
+ list_del_item([], L,L).
519
+ list_del_item([It|R], L1, Left) :-
520
+ del_item(It, L1, LInter),
521
+ list_del_item(R, LInter, Left).
522
+
523
+ del_item(_Item, [],[]).
524
+ del_item(Item, [It |R], R) :-
525
+ same_subst(Item, It), ! .
526
+ % del_item(Item, Rest, R).
527
+ del_item(Item, [It|Rest], [It |R]) :-
528
+ del_item(Item, Rest, R).
529
+
530
+ %%
531
+ % same_subst(Sub1, Sub2)
532
+ %
533
+ % Sub1 and Sub2 represent the same subst.
534
+ %
535
+
536
+ same_subst([],[]).
537
+ same_subst([S1|SRest], Subs) :-
538
+ delmemb(S1, Subs, Subs1),
539
+ same_subst(SRest, Subs1).
540
+
541
+ %%%%%%%%%%
542
+ %
543
+ % delmemb(Item, List, ListMinusItem)
544
+ %
545
+ % special delete for substitutions.
546
+ %
547
+
548
+ delmemb(_E, [], []).
549
+ delmemb(E <-- E1 , [F <-- F1| R], R) :-
550
+ E == F,
551
+ copy_term(E <-- E1 ,F <-- F1). % only when LHS's are eq.
552
+ delmemb(E, [F|R], [F|R1]) :-
553
+ delmemb(E,R,R1).
554
+
555
+
556
+ %%%%%%%%%%%%%%%%%%%%
557
+ %
558
+ % read_test(-Extra,-Missing)
559
+ %
560
+ % read a test [G,Expected] from standard in
561
+ % and find the Missing and Extra substitutions.
562
+ %
563
+
564
+ read_test(Extra, Missing) :-
565
+ read(X),
566
+ X = [G, Expected],
567
+ result(G, R),
568
+ compare_subst_lists(R, Expected, Extra, Missing),
569
+ write('Extra Solutions found: '), write(Extra), nl,
570
+ write('Solutions Missing: '), write(Missing).
571
+
572
+
573
+ %%%%%%%%%
574
+ %
575
+ % read tests from a file
576
+ %
577
+
578
+
579
+ run_tests(File) :-
580
+ asserta(score(File, total(0), wrong(0))),
581
+ open(File, read, S),
582
+ loop_through(File,S),
583
+ close(S).
584
+
585
+ %%%%%%%%%%%%%%%%%%%%
586
+ %
587
+ % loop_through(+File,+Source)
588
+ %
589
+ % read a term from the file and test the term
590
+ % the catch is for syntax errors
591
+ % (which will be errors in the processor).
592
+ %
593
+
594
+ loop_through(F, S) :-
595
+ catch(read(S,X), B, X = B),
596
+ (
597
+ X = end_of_file
598
+ -> true
599
+ ;
600
+ reset_flags,
601
+ test(F,X),
602
+ loop_through(F,S)
603
+ ).
604
+
605
+ %%%%%%%%%%%%%%%%%%%
606
+ %
607
+ % test(+File, +TermRead)
608
+ %
609
+ % do the tests. Handles syntax erros in input and end_of_file
610
+ %
611
+
612
+ test(_,end_of_file).
613
+ test(F, error(R, _)) :- !,
614
+ write('Error in Input: '), write(R), nl,nl,
615
+ update_score(F, non_null, non_null).
616
+
617
+ test(F,[G,Expected]) :-
618
+ result(G,R),
619
+ compare_subst_lists(R, Expected, Extra, Missing),
620
+ write_if_wrong(F, G, Expected, Extra, Missing),
621
+ update_score(F, Missing, Extra).
622
+
623
+ test(F, [G, ProgFile, Expected]) :-
624
+ [ProgFile],
625
+ result(G,R),
626
+ compare_subst_lists(R, Expected, Extra, Missing),
627
+ write_if_wrong(F, G, Expected, Extra, Missing),
628
+ update_score(F, Missing, Extra).
629
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630
+ %
631
+ % write_if_wrong(+File, +Goal, +Expected, +Extra, +Missing)
632
+ %
633
+ % If Either Extra or Missing are non empty write
634
+ % an appropriate message.
635
+ %
636
+ % A more legant output is possible if the processor supports
637
+ % numbervars/3, insert ther commented out line.
638
+ %
639
+
640
+ write_if_wrong(_,_,_,[],[]):- !.
641
+ write_if_wrong(F, G,Expected, Extra, Missing) :-
642
+ fake_numbervars([G,Expected, Missing], 0, _),
643
+ write('In file: '), write(F), nl,
644
+ write('possible error in Goal: '),
645
+ write(G), nl,
646
+ write('Expected: '), write(Expected), nl,
647
+ write('Extra Solutions found: '), write(Extra), nl,
648
+ write('Solutions Missing: '), write(Missing),nl,nl.
649
+
650
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651
+ %
652
+ % update_score(+File, +Missing, +Extra)
653
+ %
654
+ % add 1 to total in all cases.
655
+ % If Missing or Extra are non empty add one to wrong.
656
+ %
657
+
658
+
659
+ update_score(F,[],[]) :- !,
660
+ retract(score(F,total(T), wrong(W))),
661
+ T1 is T +1,
662
+ asserta(score(F,total(T1), wrong(W))).
663
+ update_score(F,_,_) :-
664
+ retract(score(F,total(T), wrong(W))),
665
+ T1 is T +1, W1 is W + 1,
666
+ asserta(score(F,total(T1), wrong(W1))).
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+ %%%%%%%%%%%%%
675
+ %
676
+ % inerror(?F)
677
+ %
678
+ % One of the tests in the file gave an
679
+ % unexpected answer.
680
+ %
681
+
682
+
683
+
684
+ inerror(F) :-
685
+ score(F, total(_X), wrong(Y)),
686
+ Y =\= 0.
687
+
688
+
689
+
690
+ %%%%%%%%%
691
+ %
692
+ % list all the files
693
+ % of tests.
694
+ %
695
+
696
+ file(fail).
697
+ file(abolish).
698
+ file(and).
699
+ file(arg).
700
+ file(arith_diff).
701
+ file(arith_eq).
702
+ file(arith_gt).
703
+ file('arith_gt=').
704
+ file(arith_lt).
705
+ file('arith_lt=').
706
+ file(asserta).
707
+ file(assertz).
708
+ file(atom).
709
+ file(atom_chars).
710
+ file(atom_codes).
711
+ file(atom_concat).
712
+ file(atom_length).
713
+ file(atomic).
714
+ file(bagof).
715
+ file(call).
716
+ file('catch-and-throw').
717
+ file(char_code).
718
+ file(clause).
719
+ file(compound).
720
+ file(copy_term).
721
+ file(current_input). % default names of input are imp-def.
722
+ file(current_output).
723
+ file(current_predicate).
724
+ file(current_prolog_flag).
725
+ file(cut).
726
+ %file(file_manip). % needs complete rewite.
727
+ file(findall).
728
+ file(float).
729
+ file(functor).
730
+ file('if-then').
731
+ file('if-then-else').
732
+ file(integer).
733
+ file(is).
734
+ file(nonvar).
735
+ file(not_provable).
736
+ file(not_unify).
737
+ file(number).
738
+ file(number_chars).
739
+ file(number_codes).
740
+ file(once).
741
+ file(or).
742
+ file(repeat).
743
+ file(retract).
744
+ file(set_prolog_flag).
745
+ file(setof).
746
+ file(sub_atom).
747
+ file(term_diff).
748
+ file(term_eq).
749
+ file(term_gt).
750
+ file('term_gt=').
751
+ file(term_lt).
752
+ file('term_lt=').
753
+ file(true).
754
+ file(unify).
755
+
756
+
757
+
758
+ %%%%%%%%%%%
759
+ %
760
+ % display_list(+List)
761
+ %
762
+
763
+
764
+ display_list([]) :- nl.
765
+ display_list([H|T]) :-
766
+ write(H), nl,
767
+ display_list(T).
768
+
769
+
770
+ %%%%%%%%%%%%%%%%%
771
+ %
772
+ % reset_flags
773
+ %
774
+ % some tests reset the prolog flags.
775
+ % in order to fix this we restore them to their default values.
776
+ % This is why fail is the first test.
777
+
778
+
779
+ reset_flags :-
780
+ set_prolog_flag(unknown, error).
781
+
782
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783
+ %
784
+ % tests to see if a given predicate (a bip)
785
+ % exists. Used for current_input and current_output
786
+ % since they don't have default values for the
787
+ % streams.
788
+
789
+ exists(P/I) :-
790
+ make_list(I,List),
791
+ G =.. [P|List],
792
+ set_prolog_flag(unknown, fail),
793
+ catch(call(G),_ , true),
794
+ reset_flags,!.
795
+ exists(P/I) :-
796
+ write('Predicate: '), write(P/I), write(' not implemented'), nl,
797
+ reset_flags.
798
+
799
+
800
+
801
+ %%%%%%%%%%%%%
802
+ %
803
+ % make_list(Len, List).
804
+ %
805
+ make_list(N,L) :-
806
+ N >= 0,
807
+ make_list1(N,L).
808
+ make_list1(0,[]).
809
+ make_list1(N, [_|L1]) :-
810
+ N1 is N -1,
811
+ make_list(N1, L1).
812
+
813
+
814
+ %%%%%%%%%%%%%%%%%%%%%%%%%
815
+ %
816
+ % fake_numbervars/3
817
+ %
818
+ % Like numbervars
819
+ %
820
+
821
+ fake_numbervars(X,N,M) :-
822
+ var(X), !,
823
+ X =.. ['$VAR', N],
824
+ M is N + 1.
825
+ fake_numbervars(X, N,N) :-
826
+ atomic(X), !.
827
+ fake_numbervars([H|T], N, M) :- !,
828
+ fake_numbervars(H, N, N1),
829
+ fake_numbervars(T, N1, M).
830
+ fake_numbervars(T, N, M) :-
831
+ T =.. [_F |Args],
832
+ fake_numbervars(Args, N,M).
833
+
834
+
835
+ % :-initialization((run_all_tests, halt)).
836
+ %:- initialization(run_all_tests).