-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcclause.pro
More file actions
executable file
·1670 lines (1446 loc) · 55.6 KB
/
cclause.pro
File metadata and controls
executable file
·1670 lines (1446 loc) · 55.6 KB
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%-*-Prolog-*-
% cclause indented on 12/23/2001 by 'JOLI' 1.0.
%------------------------------------------------------------------------
%
% cclause.pro -- compile a clause
%
% Copyright (c) 1992-2002 by Amzi! inc. All Rights Reserved.
%
% $Log: cclause.pro,v $
% Revision 1.2 2004/05/05 16:31:44 dennis
% added tcltk stuff
%
% Revision 1.1.1.1 2003/09/11 02:15:10 dennis
% Starting release 7.0
%
% Revision 1.15 2003/08/23 03:27:31 dennis
% cut working for debug64 compiled code
%
% Revision 1.14 2002/09/27 00:27:23 dennis
% fix another retract pulls the rug out from goal bug
%
% Revision 1.13 2002/05/15 16:59:06 dennis
% Final fixes for last 6.1 build, 80
%
% Revision 1.12 2002/04/25 03:42:22 dennis
% more documentation, logicbase.htm, and some fiddling with sources
%
% Revision 1.11 2002/04/19 19:41:42 dennis
% fixed retract bug with sorted/indexed clauses, implemented abolish for
% those types as well
%
% Revision 1.10 2002/03/19 00:30:30 dennis
% add infinite compiles by breaking up predicates every 1000 clauses,
% used discontiguous feature, so no problem with huge numbers of clauses.
%
% Revision 1.9 2002/02/21 21:08:31 dennis
% changed to floats/single for number defaults
%
% Revision 1.8 2002/01/20 20:48:05 ray
% revised real divide, printReal
%
% Revision 1.7 2001/10/19 01:37:59 dennis
% compiler bugs, still not found, but noted that X registers
% are really restricted to 255 because of flewrite in assemb.pro,
% should change some day.
%
% Revision 1.6 2001/09/11 04:34:55 dennis
% cleaned up some io stuff, got consult working, etc.
%
% Revision 1.5 2001/06/27 15:15:09 dennis
% miscellaneous changes and bug fixes, work with leak detection
%
% Revision 1.4 2001/03/16 00:29:06 dennis
% compiled metapredicates
%
% Revision 1.3 2001/02/08 22:56:45 dennis
% string bug fixes, modularized compiler and listener
%
% Revision 1.2 2001/01/30 16:47:28 dennis
% Made, after many trials, alib into amzi_system module.
%
% Revision 1.1.1.1 2000/12/29 02:18:05 dennis
% moved to a6
%
% Revision 1.9 2000/11/30 15:47:27 ray
% made arg, nth and prime bilateral.
% made real length and exponent 12 bits for maximum range
%
% Revision 1.8 2000/10/07 17:47:19 ray
% added q_cf and fourierPrime
%
% Revision 1.7 2000/09/25 02:11:18 dennis
% first version of modules working, runs the modular version of
% duck world. still needs import and export. release 6.1.1
%
% Revision 1.6 2000/09/15 21:42:24 dennis
% 12->13
%
% Revision 1.5 2000/03/28 23:47:48 dennis
% Changed all tabs to three spaces, and also changed Logic Server
% to use void* for TERM externally and cast to Cell* in LEngine
% implementation.
%
% Revision 1.4 2000/03/28 01:05:14 dennis
% merged Ray's changes with bigdig. bigdig is at point where
% new Cell class is used, but there are no modules in the system.
%
% Revision 1.3.2.1 2000/02/26 20:56:12 dennis
% Removed local atoms from compiler, and old module support, so
% compiler and listener are all global for now. Also made member/2
% and friends built-ins as well as the bug predicates.
%
%
% 09/13/99 Ray rewrote conflict to not use is_member (for)
%
% 1 CCLAUSE.PRO 9-Nov-92 "Version 2.0 32-bit Alpha"
% $Log: cclause.pro,v $
% Revision 1.2 2004/05/05 16:31:44 dennis
% added tcltk stuff
%
% Revision 1.1.1.1 2003/09/11 02:15:10 dennis
% Starting release 7.0
%
% Revision 1.15 2003/08/23 03:27:31 dennis
% cut working for debug64 compiled code
%
% Revision 1.14 2002/09/27 00:27:23 dennis
% fix another retract pulls the rug out from goal bug
%
% Revision 1.13 2002/05/15 16:59:06 dennis
% Final fixes for last 6.1 build, 80
%
% Revision 1.12 2002/04/25 03:42:22 dennis
% more documentation, logicbase.htm, and some fiddling with sources
%
% Revision 1.11 2002/04/19 19:41:42 dennis
% fixed retract bug with sorted/indexed clauses, implemented abolish for
% those types as well
%
% Revision 1.10 2002/03/19 00:30:30 dennis
% add infinite compiles by breaking up predicates every 1000 clauses,
% used discontiguous feature, so no problem with huge numbers of clauses.
%
% Revision 1.9 2002/02/21 21:08:31 dennis
% changed to floats/single for number defaults
%
% Revision 1.8 2002/01/20 20:48:05 ray
% revised real divide, printReal
%
% Revision 1.7 2001/10/19 01:37:59 dennis
% compiler bugs, still not found, but noted that X registers
% are really restricted to 255 because of flewrite in assemb.pro,
% should change some day.
%
% Revision 1.6 2001/09/11 04:34:55 dennis
% cleaned up some io stuff, got consult working, etc.
%
% Revision 1.5 2001/06/27 15:15:09 dennis
% miscellaneous changes and bug fixes, work with leak detection
%
% Revision 1.4 2001/03/16 00:29:06 dennis
% compiled metapredicates
%
% Revision 1.3 2001/02/08 22:56:45 dennis
% string bug fixes, modularized compiler and listener
%
% Revision 1.2 2001/01/30 16:47:28 dennis
% Made, after many trials, alib into amzi_system module.
%
% Revision 1.1.1.1 2000/12/29 02:18:05 dennis
% moved to a6
%
% Revision 1.9 2000/11/30 15:47:27 ray
% made arg, nth and prime bilateral.
% made real length and exponent 12 bits for maximum range
%
% Revision 1.8 2000/10/07 17:47:19 ray
% added q_cf and fourierPrime
%
% Revision 1.7 2000/09/25 02:11:18 dennis
% first version of modules working, runs the modular version of
% duck world. still needs import and export. release 6.1.1
%
% Revision 1.6 2000/09/15 21:42:24 dennis
% 12->13
%
% Revision 1.5 2000/03/28 23:47:48 dennis
% Changed all tabs to three spaces, and also changed Logic Server
% to use void* for TERM externally and cast to Cell* in LEngine
% implementation.
%
% Revision 1.4 2000/03/28 01:05:14 dennis
% merged Ray's changes with bigdig. bigdig is at point where
% new Cell class is used, but there are no modules in the system.
%
% Revision 1.3.2.1 2000/02/26 20:56:12 dennis
% Removed local atoms from compiler, and old module support, so
% compiler and listener are all global for now. Also made member/2
% and friends built-ins as well as the bug predicates.
%
% Revision 1.3 2000/01/20 10:25:41 dennis
% Put in Ray's 1999-11-11 release, which included the fix for
% large predicates, which also requires 32-bit alignment. r5-0-2
%
% Revision 4.3 1998/01/16 04:14:09 dennis
% RCS Test2 - It works pretty good!
%
% Revision 4.2 1998/01/16 04:11:24 dennis
% RCS test
%
% Revision 4.1 1998/01/16 04:03:26 dennis
% Fixed bug compiling -> ; structures caused by change in precedence of
% the -> operator to comply with ISO standard.
%
%------------------------------------------------------------------------
/************************************************************************
* compile one clause : use several passes writing out
* intermediate files and then failing to collect global stack !!
************************************************************************/
/***********************************************************************
* Version 1.1 -- modularized the code
* Version 1.1A
************************************************************************/
:- body(amzi_compiler).
%% Consider 3 cases for Clause ..
%% Head
%% Head :- OneGoal
%% then we quick compile it - no need for perm vars, and all the code
%% (should) fit in one pass
%%
%% Head :- Body
%% Run the three passes. Use Dis (bound to dis if a disjunction in the
%% body) to optionally run initvars() if there is a disjunction in
%% the body
get_head((H :- Body), H) :- !.
get_head(H, H).
compileclause(Clause, Code) :-
pretrans(Clause, Pretrans), %!,
compc(Pretrans, Code), !.
compc((Head :- Body), Code) :-
functor(Body, Func, _),
Func \= ',',
Func \= ';', !,
quickcomp((Head :- Body), Code).
compc((Head :- Body), Code) :- !,
% this is where the various passes talk to each other
reserve$(Heap),
repeat,
(pass1(Heap, (Head :- Body)) ; pass2(Heap, Code) ; pass3(Heap, Code)), !.
compc(Clause, Code) :-
quickcomp(Clause, Code).
% Ram everything into one pass - no permanent variables
quickcomp(Clause, Code) :-
get_head(Clause, Head),
functor(Head, N, A),
cntr_get(1, ClauseN), write_l(ClauseN),
write_l($ |-- $), % keep user informed
write_l(N/A),
write_l($\n$),
unravel(Clause, Unravel, _),
partobj(Unravel, Partobj, []),
valvar(Partobj, Head, []),
% ? time_it(varlist(Unravel, VarList)), no need to time?
varlist(Unravel, VarList),
lifetime(VarList, LifeList, _, _),
tempalloc(VarList, LifeList),
objcode(Partobj, Objcode),
peephole(Objcode, Code), !.
pass1(H, Clause) :-
get_head(Clause, Head), % src->src translations
functor(Head, N, A),
cntr_get(1, ClauseN), write_l(ClauseN),
write_l($ |-- $), % keep user informed
write_l(N/A),
write_l($\n$),
permvars(Clause, Perms), % list of Yi s
unravel(Clause, Unravel, Disjunction),
partobj(Unravel, Partobj, Perms),
valvar(Partobj, Head, Perms),
permalloc(Perms),
% time_it(varlist(Unravel, Varlist)),
varlist(Unravel, Varlist),
stash$(H, pass$(Varlist, Partobj, Disjunction)), !,
fail.
pass2(H, Code) :-
get$(H, [pass$(VarList, Partobj, Dis)]),
lifetime(VarList, LifeList, Forward, Backward),
(
Dis == dis -> % Go onto third pass
varinit(Forward, Backward, Partobj, Newobj),
tempalloc(VarList, LifeList),
stash$(H, pass$(Newobj)),
fail ; % Else can fit it all in 2nd pass
tempalloc(VarList, LifeList),
objcode(Partobj, Objcode),
peephole(Objcode, Code)
), !.
pass3(H, Code) :-
get$(H, [pass$(Newobj)]),
objcode(Newobj, Objcode),
peephole(Objcode, Code), !.
/************************************************************************
* pretransformations - source->source transformations
************************************************************************/
pretrans((Head :- Body), (Head :- PB)) :- !,
xpretrans(Body, PB).
pretrans(Head, Head).
% the case where a variable is a goal, meant to be
% called when instantiated, poor style I think,
% an explicit call is better in the user's code,
% but what the customer wants.
xpretrans(A, call(A)) :-
var(A), !.
/* Conditional */
/* It turns out that the cut used here should work the way
Allan intended the cutd to work, which is incorrect for
disjunctions, but gives the desired (but maybe not correct?)
behavior of A->B;C constructs. So, we preserve the cutd
for this case.
*/
/* Well, its even worse. If the user puts cuts within any of the
goals, A, B, or C, then those cuts should be opaque, that is, they
shouldn't affect the predicate they are in. (As opposed to cuts
within disjunctions, which are transparent, affecting the predicate
they are in.) So, rather than keep these optimizations, it's
better to simply use the builtins in amzilib.pro, which define these
as separate predicates, so it all works ok. */
/* And still worse. If we use amzilib.pro, then goals that are local
predicates aren't seen, and fail. So not(foo) doesn't work if foo
is a local predicate. Argghhh. What are the fixes? Make a cutdd or
something that behaves correctly when cuts are included in -> ; constructs.
They must, of course, be different than real disjunctions which use
transparent cuts. The other fix is to get rid of local predicates
and modules altoghether, as they're not standard anyway. Not sure
I like that either. */
/* In 4.01 we changed the priority of -> to be (1050) less than
the 1100 of ;. It used to be 1150, so it was necessary to add
the extra parentheses around A->B to correct an awful mess. */
%/*
xpretrans(((A -> B) ; C), (If_Then ; Else)) :-
cut_check(A),
cut_check(B),
cut_check(C),
xpretrans(A, PA),
xpretrans(B, PB),
% conj_append(PA, (!, PB), If_Then),
conj_append(PA, (cut$d, PB), If_Then), % cutbug fix
xpretrans(C, Else), !.
xpretrans((A -> B), (If_Then ; fail)) :-
cut_check(A),
cut_check(B),
xpretrans(A, PA),
xpretrans(B, PB),
% conj_append(PA, (!, PB), If_Then),
conj_append(PA, (cut$d, PB), If_Then), !. % cutbug fix
%*/
% simple arithmetic (+ or - constant, X + Y)
xpretrans((X is Y), (X is Y)) :-
var(Y), !.
/*
xpretrans((X is A - Const), arith_plus(X, A, NegConst)) :-
integer(Const),
NegConst is - Const, !.
xpretrans((X is A + B), arith_plus(X, A, B)) :- !.
*/
/* These too must be replaced with the kludge cut$d
xpretrans(not(A), (NA ; true)) :-
xpretrans(A, PA),
conj_append(PA, (!, fail), NA),
!.
xpretrans(\+(A), (NA ; true)) :-
xpretrans(A, PA),
conj_append(PA, (!, fail), NA),
!.
*/
/* experimentally, lets take these out and let the definitions
in amzilib.pro take over --- experiment a success, these
optimizations will remain out. Nope, looks like back in.*/
xpretrans(not(A), (NA ; true)) :-
cut_check(A),
xpretrans(A, PA),
conj_append(PA, (cut$d, fail), NA), !.
xpretrans(\+(A), (NA ; true)) :-
cut_check(A),
xpretrans(A, PA),
conj_append(PA, (cut$d, fail), NA), !.
xpretrans((Goal, Body), PGB) :-
xpretrans(Goal, PG),
xpretrans(Body, PB),
conj_append(PG, PB, PGB), !.
xpretrans((Choice ; Others), (PC ; PO)) :-
xpretrans(Choice, PC),
xpretrans(Others, PO), !.
%xpretrans( \=(X, Y), (X = Y, !, fail ; true)) :- !.
% this too needs the cut$d fix, but is now not used
xpretrans(\=(X, Y), (X = Y, cut$d, fail ; true)) :- !.
xpretrans(M : GIN, GOAL) :-
amzi_system:is$meta(M, GIN, DM), !,
(
amzi_system:mcheck$convert(M, GIN, GOUT, DM) ->
GOAL = mod$call(M, GOUT) ;
GOAL = (meta$convert(M, GIN, GOUT2, DM), call_nometa(M : GOUT2))
).
xpretrans(Mod : Pred, mod$call(Mod, Pred)) :- !.
% mcheck does a convert of the arguments, unless it runs into
% unbound variables, in which case it fails. So we either do
% the job now, or put the call in so it gets automatically done
% at runtime.
xpretrans(GIN, GOAL) :-
loading_module(CM),
amzi_system:is$meta(CM, GIN, DM),
(
amzi_system:mcheck$convert(CM, GIN, GOUT, DM) ->
GOAL = GOUT ;
GOAL = (meta$convert(CM, GIN, GOUT2, DM), call_nometa(GOUT2))
), !.
%write(trans2out-GOAL),nl,
xpretrans(A, A). % join together two goal sequences
conj_append((A, L1), L2, (A, L3)) :-
conj_append(L1, L2, L3).
conj_append(A, L, (A, L)).
cut_check(!) :-
cut_warning, !.
cut_check((!, _)) :-
cut_warning, !.
cut_check((A, B)) :-
cut_check(B).
cut_check((A ; B)) :-
cut_check(A),
cut_check(B).
cut_check(_).
cut_warning :-
write_l($Warning: Dangerous cut in 'not' or 'if-then-else'\n$).
/**************************************************************************
*** Identify and number permanent variables **********
***************************************************************************/
% find all permanent variables
permvars((Head :- Body), Perms) :- !,
varsof(Head, HeadVars),
xpermvars(Body, [HeadVars, [], []], [_, _, Perms]), !.
permvars(Head, []).
xpermvars(X, SoFar, Out) :- % disjunction
Dis = (_ ; _),
(X = (Dis, Rest) ; X = Dis), !,
disxpermvars(Dis, SoFar, NewSoFar),
(nonvar(Rest) -> xpermvars(Rest, NewSoFar, Out) ; Out = NewSoFar).
xpermvars(X, SoFar, Out) :- % conjunction
(X = (A, Rest) ; X = A),
SoFar = [Vars, Half, Perms],
varsof(A, AVars),
intersectv(AVars, Half, P), !,
unionv(Perms, P, NewPerms),
unionv(AVars, Vars, NewVars),
(builtin(A) -> NewHalf = Half ; unionv(NewVars, Half, NewHalf)),
NewSoFar = [NewVars, NewHalf, NewPerms],
(nonvar(Rest) -> xpermvars(Rest, NewSoFar, Out) ; Out = NewSoFar).
disxpermvars((A ; B), SoFar, Out) :- !,
xpermvars(A, SoFar, OutA),
disxpermvars(B, SoFar, OutB), !,
map_unionv(OutA, OutB, Out), !. % unionv puts newvars at end of Perms
disxpermvars(B, SoFar, Out) :-
xpermvars(B, SoFar, Out), !.
permalloc(PermVars) :- % trivial permalloc, vars at end are numbered lowest
permalloc(PermVars, _).
permalloc([y(I)|Vars], I) :-
permalloc(Vars, I1),
I is I1 + 1.
permalloc([], 0).
/************************************************************************
** All structures are unravelled into unify goals
** All unify goals are of the form Var1 = (Var2 or Atom or Struc)
** Where var1 is temp. or permanent. and where Struc
** has only variables and atoms as arguments
** If var1 is permanent so is var2
** Preexisisting Unify goals are converted to this type
** ; remains - only the content is unravelled
** If body contains ; then Dis == dis else var(Dis)
************************************************************************/
unravel(X, [NewHead|Ravel], Dis) :-
(X = (Head :- Body) ; X = Head),
top_spread(Head, NewHead, Ravel, L),
(nonvar(Body) -> xunravel(Body, L, [], Dis) ; L = []), !.
xunravel(X, [DRavel|Ravel], Link, dis) :- % dis flags a disjunction
Dis = (_ ; _),
(X = (Dis, Rest) ; X = Dis), !,
disunravel(Dis, DRavel), !,
(nonvar(Rest) -> xunravel(Rest, Ravel, Link, _) ; Ravel = Link), !.
xunravel(X, Ravel, Link, Dis) :-
Goal = (_ = _),
(X = (Goal, Rest) ; X = Goal), !,
varunify(Goal, Ravel - L),
(nonvar(Rest) -> xunravel(Rest, L, Link, Dis) ; L = Link), !.
xunravel(X, Ravel, Link, Dis) :-
(X = (Goal, Rest) ; X = Goal), !,
top_spread(Goal, NewGoal, Ravel, L), !,
L = [NewGoal|L2],
(nonvar(Rest) -> xunravel(Rest, L2, Link, Dis) ; L2 = Link), !.
disunravel((A ; B), (ARavel ; BRavel)) :- !,
xunravel(A, ARavel, [], _), !,
disunravel(B, BRavel).
disunravel(A, ARavel) :-
xunravel(A, ARavel, [], _).
/************************************************************************
** Unification optimization
** Turn simple goal X = Y into sequence of simpler unifications of
** the form Var1 = (Var2 or atom or struc),
** where Var1 is temp or perm and where struc has only atoms
** and variables as args
************************************************************************/
varunify(X = Y, Code - Link) :-
(xvarunify(X = Y, Code, Link) ; Code = [fail|Link]), !.
xvarunify(A = B, [(A = NewB)|L], Link) :- % or one or the other is a var
var(A), !,
spread(B, NewB, L, Link).
xvarunify(A = B, [(B = NewA)|L], Link) :-
var(B), !,
spread(A, NewA, L, Link).
xvarunify(A = B, Code, Link) :- % both args are nonvars
atomic(A), !,
atomic(B),
(A = B -> Code = Link ; fail).
xvarunify(A = B, Code, Link) :-
(
atomic(B) ->
fail ; % A & B are strucs
A =.. [Func|ArgsA],
B =.. [Func|ArgsB],
lvarunify(ArgsA, ArgsB, Code, Link)
).
lvarunify([A|ArgsA], [B|ArgsB], Code, Link) :-
xvarunify(A = B, Code, L), !,
lvarunify(ArgsA, ArgsB, L, Link).
lvarunify([], [], Link, Link).
/************************************************************************
** Take a (possibly nested) structure apart into
** 1 a simple structure
** 2 a series of unify goals.
** A list is considered as a structure with variable arity.
*************************************************************************/
top_spread(List, SimpleList, Rest, Link) :-
list(List), !,
argspread(CdrUnify, List, SimpleList, Ravel, Link), !,
(CdrUnify = none -> Rest = Ravel ; Rest = [CdrUnify|Ravel]).
top_spread(mod$call(Mod, Goal), mod$call(Mod, SimpleStruc), R, L) :-
functor(Goal, Name, _),
Goal =.. [_|Args], !,
argspread(_, Args, VArgs, R, L), !,
SimpleStruc =.. [Name|VArgs].
top_spread(Struc, SimpleStruc, R, L) :-
functor(Struc, Name, _),
Struc =.. [_|Args], !,
argspread(_, Args, VArgs, R, L), !,
SimpleStruc =.. [Name|VArgs].
top_spread(Other, Other, Link, Link).
spread(List, SimpleList, Rest, Link) :-
list(List), !,
argspread(CdrUnify, List, SimpleList, Ravel, Link), !,
(CdrUnify = none -> Rest = Ravel ; Rest = [CdrUnify|Ravel]).
spread(Struc, SimpleStruc, R, L) :-
functor(Struc, Name, _),
Struc =.. [_|Args], !,
argspread(_, Args, VArgs, R, L), !,
SimpleStruc =.. [Name|VArgs].
spread(Other, Other, Link, Link).
argspread(CdrUnify, Cdr, T, R, L) :-
nonlist(Cdr), !,
(
(var(Cdr) ; Cdr = []) ->
CdrUnify = none,
T = Cdr,
R = L ;
spread(Cdr, SimpleCdr, R, L),
CdrUnify = (T = SimpleCdr)
), !.
argspread(CdrUnify, [A|Args], [A|VArgs], R, L) :-
(atomic(A) ; var(A)), !,
argspread(CdrUnify, Args, VArgs, R, L).
argspread(CdrUnify, [S|Args], [T|VArgs], Ravel, Link) :-
Ravel = [(T = V)|L],
spread(S, V, L, L2), !,
argspread(CdrUnify, Args, VArgs, L2, Link).
% Convert unravelled code to partial object code
partobj([Head|BodyGoals], [HeadObj|BodyObj], Perms) :-
Head =.. [_|Args],
gp_block(get, Args, HeadObj, 1),
xpartobj(BodyGoals, Perms, BodyObj, yes), !.
xpartobj([], _, [], _) :- !.
xpartobj([Dis|Rest], Perms, Result, Flag) :-
Dis = (_ ; _), !,
( % Initialize permanent variables just before first disjunction
Flag = yes ->
initblock(Perms, PermInit),
Result = [PermInit, DisCode|RestCode] ;
Result = [DisCode|RestCode]
),
dispartobj(Dis, Perms, DisCode), !,
xpartobj(Rest, Perms, RestCode, no).
xpartobj([Goal|Rest], Perms, [GoalCode|RestCode], Flag) :-
goalpartobj(Goal, Perms, GoalCode), !,
xpartobj(Rest, Perms, RestCode, Flag).
dispartobj((A ; B), Perms, (ACode ; BCode)) :- !,
xpartobj(A, Perms, ACode, no), !,
dispartobj(B, Perms, BCode).
dispartobj(A, Perms, ACode) :-
xpartobj(A, Perms, ACode, no).
/*
convert goals to object code
recognizes !, true, fail, unify goals and calls with simple arguments
*/
goalpartobj(!, _, cut) :- !.
goalpartobj(debug64_cut, _, cut64) :- !.
/* this line added to preserve the cutd needed in A->B;C constructs,
probably not necessary except for clarity of the fix to the kludge. */
goalpartobj(cut$d, _, cut$d) :- !. % cutdbug fix
goalpartobj(true, _, Link - Link) :- !.
goalpartobj(V = W, Perms, [put(_, V, Temp)|Code] - Link) :-
(is_member(V, Perms) -> Temp = x(0 /*temp*/) ; Temp = V), !,
get_the_get(W, Code, Temp, Link). % other goals
goalpartobj(mod$call(Mod, Goal), _, Code - Link) :-
functor(Goal, Name, Arity),
Goal =.. [_|Args],
gp_block(put, Args, Code - L, 1), !,
L = [mod_call(Mod, Name/Arity, _)|Link].
goalpartobj(Goal, _, Code - Link) :-
functor(Goal, Name, Arity),
Goal =.. [_|Args],
gp_block(put, Args, Code - L, 1), !,
(
builtin(Name, Arity) ->
L = [(Name/Arity)|Link] ;
L = [call((Name/Arity), _)|Link]
). % figure out which get instruction
get_the_get(W, Code, Temp, Link) :-
var(W), !,
Code = [get(_, W, Temp)|Link].
get_the_get(W, Code, Temp, Link) :-
atomic(W), !,
Code = [get(constant, W, Temp)|Link].
get_the_get(W, Code, Temp, Link) :-
( % at this point a list is special structure functor '.'
list(W) ->
W = Args,
Name/Arity = '.'/2,
Type = list ;
functor(W, Name, Arity),
W =.. [_|Args],
Type = nonlist
),
Code = [get(structure, Name/Arity, Temp)|L], !,
% and generate unify sequence
unifyblock(Type, Args, L - Link).
% variable initialisation - register MACRO_MAXVARS used as holder
initblock([], Link - Link).
initblock([V|Vars], [put(_, V, x(0 /*temp*/))|Rest] - Link) :-
initblock(Vars, Rest - Link).
% get or put all of head arguments (if type is get or put)
gp_block(Type, [A|Args], [X|Rest] - Link, N) :- !,
X =.. [Type, T, A, x(N)],
(atomic(A) -> T = constant ; true),
N1 is N + 1, !,
gp_block(Type, Args, Rest - Link, N1).
gp_block(_, [], Link - Link, _).
% block of unify instructions to unify structures or lists
unifyblock(nonlist, [], Link - Link) :- !.
unifyblock(nonlist, [A|Args], [unify(T, A)|Rest] - Link) :-
(atomic(A) -> T = constant ; true), !,
unifyblock(Type, Args, Rest - Link).
unifyblock(list, V, Rest - Link) :-
(
(atomic(V), Rest = [unify(constant, V)|Link]) ; % cdr
(var(V), Rest = [unify(T, V)|Link]) ; % cdr
( % cons
V = [A|Args],
unifyblock(list, A, Rest - L2), % do cons
( % cdr a list
list(Args) ->
L2 =
[unify(variable, x(0 /*temp*/)),
get(structure, '.'/2, x(0 /*temp*/))|L3] ;
L2 = L3 % no
), !,
unifyblock(list, Args, L3 - Link)
) ;
Rest = [unify(T, V)|Link]
), !.
/************************************************************************/
/*
Add initialisation instructions in disjunctions to variables
which need it. Results in modified partobj.
Must be used before tempalloc
*/
varinit(Forward, Backward, Partobj, Newobj) :-
xvarinit(Forward, Backward, Partobj, Newobj - []), !.
xvarinit([_], _, X, R - L) :-
linkify(X, R - L), !.
% 1st 2 clauses traverse Forward, backward and PartObj till disjunction
xvarinit([_, FIn|Forward], [_, BIn|Backward], PartObj, NewObj) :-
not(FIn = (_ ; _)), !,
% since Forward and Backward have identical structure only one must be tested
xvarinit([FIn|Forward], [BIn|Backward], PartObj, NewObj), !.
xvarinit(Forward, Backward, [G|PartObj], [G|NewObj] - Link) :-
not(G = (_ ; _)), !,
xvarinit(Forward, Backward, PartObj, NewObj - Link), !.
% at this stage all threee args have disjunctions
xvarinit([FLeft, (FA ; FB), FRight|Forward],
[_, (BA ; BB), BRight|Backward], [(A ; B)|PartObj],
[(NA ; NB)|NewObj] - Link) :- !,
diffv(FRight, FLeft, T),
intersectv(T, BRight, V),
dis_varinit(V, (FA ; FB), (BA ; BB), (A ; B), (NA ; NB)), !,
xvarinit([FRight|Forward], [BRight|Backward], PartObj, NewObj - Link).
dis_varinit(V, (FA ; FB), (BA ; BB), (A ; B), (NA ; NB)) :- !,
one_choice(V, FA, BA, A, NA), !,
dis_varinit(V, FB, BB, B, NB).
dis_varinit(V, FA, BA, A, NA) :-
one_choice(V, FA, BA, A, NA).
one_choice(V, FA, BA, A, NA) :-
xvarinit(FA, BA, A, NA - Link),
last(FA, FLast),
diffv(V, FLast, InitVars),
(
InitVars = [] ->
Link = [] ;
init_list(InitVars, InitInstr),
Link = [InitInstr]
), !.
init_list([V|Vars], [put(variable, V, V)|Rest] - Link) :-
init_list(Vars, Rest - Link).
init_list([], Link - Link).
/************************************************************************/
/*
Turn partial obj code which still contains a hierarchy of goals
and disjunctions into a uniform list.
The control instructions for disjunctions are compiled and
the labels for the cut are instantiated
*/
objcode(PartObj, ObjCode) :-
xobjcode(PartObj, ObjCode, [], proc, _), !.
/* cutd, which was only used inside of disjunctions,
was the cause of more than one bug. First, two
cutd's in a row to the same label would cause a
GPF has the engine searched in vain for the label
after the first cutd had already removed it from
the choice point stack.
Second, cutd's in nested or's only cut up to the
choice points on the disjunction in question, failing
to freeze choices all the way back to the outer-most
or.
What is disturbing is, the fix seems to be just to
use regular cuts in all places. Am I missing
something here? Why was cutd implemented for
disjunctions in the first place? What problem did
it solve? We only know the problems it created.
Ahh, at least one problem is A->B;C. It puts in a
cut which is supposed to behave (although we haven't
confirmed this with the ISO standard, it is how the
Cogent version of this is designed) just as the cutd
is implemented.
*/
xobjcode([], Link, Link, _, _).
/* this line was changed as well, seeing as it's only the
disjunction guy who cares, and only cutd needs the label */
%xobjcode( [cut|RestCode], Code, Link, CutLbl, yes) :-
/* distinguish between two kinds of cut */
/* Why not make them all regular cuts? */
/*
(
CutLbl == proc ->
Code = [cut|C] ;
Code = [cutd(CutLbl) | C]
),
*/
xobjcode([cut|RestCode], Code, Link, CutLbl, _) :-
Code = [cut|C], !,
% write($cutting\n$),
% (CutLbl == proc -> true;
% write($***warning: disjunctive cut ***$),write(CutLbl),nl),
xobjcode(RestCode, C, Link, CutLbl, _).
xobjcode([cut64|RestCode], Code, Link, CutLbl, _) :-
Code = [cut64|C], !,
xobjcode(RestCode, C, Link, CutLbl, _).
/* this clause added for the cut$d case for A->B;C cuts
which are supposed to behave like cutds. */
xobjcode([cut$d|RestCode], Code, Link, CutLbl, yes) :-
( % distinguish between 2 kinds of cut. In this case we want to distinguish
CutLbl == proc ->
Code = [cut|C]
;
Code = [cutd(CutLbl)|C]
), !,
% write($d_cutting\n$),
% (CutLbl == proc -> true;
% write($***warning: disjunctive d_cut ***$),write(CutLbl),nl),
xobjcode(RestCode, C, Link, CutLbl, _).
xobjcode([(Code - L)|RestCode], Code, Link, CutLbl, IsCut) :- !,
xobjcode(RestCode, L, Link, CutLbl, IsCut).
xobjcode([(X ; Choices)|RestCode], [tryor(NTV, else, L1)|ChCode], Link,
CutLbl, IsCut) :-
xobjcode(X, ChCode, ChLink, L1, _),
ChLink = [goto(EndLbl), label(L1)|C3],
xdiscode(Choices, C3 - L, EndLbl), !,
xobjcode(RestCode, L, Link, CutLbl, IsCut).
xdiscode((X ; Choices), [retry(else, L2)|ChCode] - Link, EndLbl) :-
xobjcode(X, ChCode, ChLink, L2, _),
ChLink = [goto(EndLbl), label(L2)|C3], !,
xdiscode(Choices, C3 - Link, EndLbl).
xdiscode(LastChoice, Code - Link, EndLbl) :-
xobjcode(LastChoice, ChCode, ChLink, CutLbl, IsCut),
(
IsCut == yes ->
Code = [retry(else, CutLbl)|ChCode],
ChLink = [goto(EndLbl), label(CutLbl), trust(else, fail), (fail/0)|L] ;
Code = [trust(else, fail)|ChCode],
ChLink = L
), !,
L = [label(EndLbl)|Link].
/*
Value-variable annotation
Assumes that initialisation of variables that needed it have been
added to the code
Pass1: First occurence of all variables are marked 'variable'.
All unsafe variables (permanent variables first occurring in
put) are collected.
Pass2: Do a reverse pass. First encounters of unsafe variables
are marked 'unsafe_value' unless they are already marked
variable. All other encounters with
such variables are marked 'value'
Must be done before temp allocation and after calculation
of permanent variables
Vars encountered so far are kept in the set SoFar in both passes.
This set is passed in parallel across disjunctions and the different
SoFars are united upon exiting disjunction
*/
% Top level
valvar(PartObj, Head, Perms) :-
varsof(Head, HeadVars),
diffv(Perms, HeadVars, PossUnsafe),
valvar1(PartObj, _, PossUnsafe, [], UnSafe, [], _), !,
valvar2(PartObj, _, UnSafe, [], _). % Pass 1
valvar1(V, _, _, UnSafe, UnSafe, SF, SF) :-
var(V), !.
valvar1([], _, _, UnSafe, UnSafe, SF, SF) :- !.
valvar1([H|RestCode], _, PossUS, InUS, OutUS, SoFar, OutSF) :-
valvar1x(H, RestCode, PossUS, InUS, OutUS, SoFar, OutSF).
valvar1x((A ; B), RestCode, PossUS, InUS, OutUS, SoFar, OutSF) :-
disvalvar1((A ; B), PossUS, InUS, US1, SoFar, NewSF), !,
valvar1(RestCode, _, PossUS, US1, OutUS, NewSF, OutSF).
valvar1x((G - _), RestCode, PossUS, InUS, OutUS, SoFar, OutSF) :-
valvar1(G, _, PossUS, InUS, US1, SoFar, NewSF), !,
valvar1(RestCode, _, PossUS, US1, OutUS, NewSF, OutSF).
valvar1x(unify(T, X), RestInstr, PossUS, InUS, OutUS, SoFar, OutSF) :-
(
notin(X, SoFar) ->
NewSF = [X|SoFar],
(T = variable ; true) ;
NewSF = SoFar
), !,
valvar1(RestInstr, _, PossUS, InUS, OutUS, NewSF, OutSF).
valvar1x(put(T, X, _), RestInstr, PossUS, InUS, OutUS, SoFar, OutSF) :-
(
notin(X, SoFar) ->
NewSF = [X|SoFar],
(T = variable ; true),
(is_member(X, PossUS) -> US1 = [X|InUS] ; US1 = InUS) ;
(NewSF = SoFar, US1 = InUS)
), !,
valvar1(RestInstr, _, PossUS, US1, OutUS, NewSF, OutSF).
valvar1x(get(T, X, _), RestInstr, PossUS, InUS, OutUS, SoFar, OutSF) :-
(
notin(X, SoFar) ->
NewSF = [X|SoFar],
(T = variable ; true),
(is_member(X, PossUS) -> US1 = [X|InUS] ; US1 = InUS) ;
(NewSF = SoFar, US1 = InUS)
), !,
valvar1(RestInstr, _, PossUS, US1, OutUS, NewSF, OutSF).
valvar1x(_, RestInstr, PossUS, InUS, OutUS, SoFar, OutSF) :-
valvar1(RestInstr, _, PossUS, InUS, OutUS, SoFar, OutSF).
disvalvar1((A ; B), PossUS, InUS, OutUS, SoFar, OutSF) :- !,
valvar1(A, _, PossUS, InUS, US1, SoFar, Out1),
disvalvar1(B, PossUS, US1, OutUS, SoFar, Out2), !,
unionv(Out1, Out2, OutSF).
disvalvar1(B, PossUS, InUS, OutUS, SoFar, OutSF) :-
valvar1(B, _, PossUS, InUS, OutUS, SoFar, OutSF). % pass 2
valvar2(V, _, _, SF, SF) :-
var(V), !.
valvar2([], _, _, SF, SF) :- !.
valvar2([H|T], _, UnSafe, SoFar, OutSF) :-
valvar2x(H, T, UnSafe, SoFar, OutSF).
valvar2x((A ; B), RestCode, UnSafe, SoFar, OutSF) :- !,