-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.pl
1977 lines (1747 loc) · 63 KB
/
engine.pl
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
% ============================================================
% Engine
% ============================================================
tex(X,Goal) :-
tex(X,Goal,_).
% = tex(+ListOfWords,+GoalType,?NumSolutions)
tex(X,Goal,File,Num,_) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,'set_cursor watch left_ptr',_),
retractall('latex output'(_,_,_,_,_,_)),
telling(Stream),
init(X,Goal),
init_db(X,Goal,Meaning0,S1,Subst,NVAR,File,Num,[E|Es],[],TwoRules0,Hyp0,Con0,XOff),
count_check([E|Es]),
estimate([E|Es],[],EstL),
create_estimate_window(Interp,EstL,Max),
copy_term(t([E|Es],S1,TwoRules0,Hyp0,Con0),
t([E0|Es0],S0,TwoRules,Hyp,Con1)),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_),
prove(Es,E,Es0,E0,EstL,Max,XOff),
substitute_sem(Subst,Meaning0,Meaning1,NVAR,_),
reduce_sem(Meaning1,Meaning),
(
normalize(S1,S) -> true
;
no_solution(S1,Meaning)
),
found_solution(Meaning,S0,S,N),
(
latex_output_format(none) -> true
;
tcl_eval(Interp,'.stats.txt configure -text "Generating Output"',_),
tcl_eval(Interp,'update idletasks',_),
my_once((normalize(S0,S,OneRules,[]),
generate_nd(Hyp,OneRules,TwoRules,Con1,Con,ND0))),
assert('latex output'(N,Meaning0,ND0,Con,Subst,NVAR)),
eta_reduce(ND0,ND1),
collapse(ND1,ND),
/* LaTeX output */
tell_texout('eg.tex'),
latex_output(N,Meaning,ND,Con,Subst,NVAR),
tell(Stream)
),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_),
fail.
tex(_,_,_,_,N) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,'set runningstate "done"\n\
set rewritestate "done"\n\
set_cursor left_ptr left_ptr\n\
.stats.c dtag node\n\
grab release .stats\n\
if {[winfo exists .stats.ind]} {\n\
.stats.ind delete bar\n\
}\n\
if {[winfo exists .rewrite]} {\n\
.rewrite.exit configure -state disabled\n\
grab release .rewrite\n\
}\n\
.stats.exit configure -state disabled\n\
focus $oldFocus',_),
/* Screen */
write_solutions(N),
write_statistics,
/* LaTeX */
tell_texout('eg.tex'),
nl,
( N>0 -> tcl_eval(Interp,'.stats.txt configure -text "Done"',_),
told,latex
; N<0 -> tcl_eval(Interp,'.stats.txt configure -text "Lookup Failed"',_),
told,fail
; tcl_eval(Interp,'.stats.txt configure -text "Failed"',_),
told
).
% retex
% reload options.pl and generate the latex output corresponding
% to the previous query.
retex :-
( latex_output_format(none) ->
format1('~nNo LaTeX output~n',[])
;
( tex_updated ->
format1('~nCalling LaTeX...~n',[]),
retractall('options changed'),
retex1
;
format1('~nNo changes to eg.tex~n',[])
)
).
retex1 :-
tell_texout('eg.tex'),
'latex output'(N,Meaning0,ND0,Con,Subst,NV),
eta_reduce(ND0,ND1),
collapse(ND1,ND),
substitute_sem(Subst,Meaning0,Meaning1,NV,_),
reduce_sem(Meaning1,Meaning),
latex_output(N,Meaning,ND,Con,Subst,NV),
fail.
retex1 :-
told,
latex.
% =
init_db(X,G0,Meaning0,S0,Subst,N,File,SNum,[vertex(0,Bs,Ps)|Es0],Es,Rules0,H0,C0,XOff) :-
unknown_words(X),
create_proofnet_window,
my_tk_interpreter(Interp),
tcl_eval(Interp,'fontWidget .stats.c create text 0 0 -font $pnfont -text "M"',MItem),
tcl_eval(Interp,format('.stats.c bbox ~s',[MItem]),BBox),
quadruple(Interp,BBox,Left,Bot,Right,Top),
tcl_eval(Interp,'fontWidget .stats.c create text 0 0 -font $indexfont -text "M"',MItem2),
tcl_eval(Interp,format('.stats.c bbox ~s',[MItem2]),BBox2),
quadruple(Interp,BBox2,Left2,Bot2,Right2,Top2),
XOff is max(Right-Left,Right2-Left2),
YOff is 2*min(Bot-Top,Bot2-Top2),
Down is -(XOff+6),
tcl_eval(Interp,'.stats.c delete all',_),
lookup(X,File,SNum,0,M,1,N0,Rules0,Rules1,H0,H1,C0,C1,Subst,[],Es0,Es1,0,Num0,10,X0,Down,Y0,XOff,YOff,0,NV),
macro_expand(G0,G),
link0(G,Meaning0,S0,0,M,N0,N,Num0,Num,Rules1,[],H1,[],
C1,[],Bs,[],Ps,[],Es1,Es),
tcl_eval(Interp,'.stats.c delete lx',_),
copy_term(G,G1),
numbervars(G1,NV,_),
retractall('pn goal'(_)),
assert('pn goal'(G1)),
paint_formula0(G1,Interp,x,Num0,_Num,X0,X1,Down,Y1,XOff,YOff,_),
Y is min(Y0,Y1),
tcl_eval(Interp,format('.stats.c configure -width [expr ~w+30]',[X1]),_),
tcl_eval(Interp,format('.stats.c configure -height [expr (-~w)+30+(~w * (~w/4))]',[Y,Num,XOff]),_),
tcl_eval(Interp,format('.stats.c configure -scrollregion "0 [expr ~w-10-(~w*(~w/4))] [expr ~w+32] 20"',[Y,Num,XOff,X1]),_),
tcl_eval(Interp,format('.stats.c lower [.stats.c create rectangle 0 [expr ~w-10-(~w*(~w/4))] [expr ~w+32] 20 -fill $grail_bg -outline $grail_bg]',[Y,Num,XOff,X1]),_),
tcl_eval(Interp,'if {$interactive !="auto"} {\n\
wm geometry .stats {}\n\
}',_),
tcl_eval(Interp,'incr lookups',Lookup),
format1('~nLookup: ~s~n',[Lookup]),
tcl_eval(Interp,'.stats.txt configure -text "Lookup $lookups"',_),
tcl_eval(Interp,'.stats.ind delete all',_),
('solutions found'(NumS),
NumS<1 ->
retractall('solutions found'(_)),
assert('solutions found'(0))
;
true),
wait_running1(Interp,_Runningstate).
% = redraw database after font selection
redraw_db :-
my_tk_interpreter(Interp),
( tcl_eval(Interp,'winfo exists .stats.c',"1") ->
tcl_eval(Interp,'fontWidget .stats.c create text 0 0 -font $pnfont -text "M"',MItem),
tcl_eval(Interp,format('.stats.c bbox ~s',[MItem]),BBox),
quadruple(Interp,BBox,Left,Bot,Right,Top),
tcl_eval(Interp,'fontWidget .stats.c create text 0 0 -font $indexfont -text "M"',MItem2),
tcl_eval(Interp,format('.stats.c bbox ~s',[MItem2]),BBox2),
quadruple(Interp,BBox2,Left2,Bot2,Right2,Top2),
XOff is max(Right-Left,Right2-Left2),
YOff is 2*min(Bot-Top,Bot2-Top2),
Down is -(XOff+6),
tcl_eval(Interp,'.stats.c delete all',_),
findall(Num-(Word-Form),'pn form'(Num,Word,Form),List0),
keysort(List0,List),
redraw_db(List,Interp,0,Num0,10,X0,Down,Y0,XOff,YOff),
'pn goal'(G1),
paint_formula0(G1,Interp,x,Num0,Num,X0,X1,Down,Y1,XOff,YOff,_),
Y is min(Y0,Y1),
tcl_eval(Interp,format('.stats.c configure -width [expr ~w+30]',[X1]),_),
tcl_eval(Interp,format('.stats.c configure -height [expr (-~w)+30+(~w * (~w/4))]',[Y,Num,XOff]),_),
tcl_eval(Interp,format('.stats.c configure -scrollregion "0 [expr ~w-10-(~w*(~w/4))] [expr ~w+32] 20"',[Y,Num,XOff,X1]),_),
tcl_eval(Interp,format('.stats.c lower [.stats.c create rectangle 0 [expr ~w-10-(~w*(~w/4))] [expr ~w+32] 20 -fill $grail_bg -outline $grail_bg]',[Y,Num,XOff,X1]),_)
;
true).
redraw_db([],_,Num,Num,X,X,Y,Y,_,_).
redraw_db([M0-(Word-Form)|Rest],Interp,Num0,Num,X0,X,Y0,Y,XOff,YOff) :-
Down is -(XOff+6),
paint_formula1(Form,Interp,M0,Num0,Num1,X0,X1,Down,Y1,XOff,YOff,t(MidX,_)),
paint_word(Word,Interp,M0,MidX,0,LeftX,X2),
Diff is X0-LeftX,
(Diff >0 ->
tcl_eval(Interp,format('.stats.c move l~w ~w 0',[M0,Diff]),_),
Move = XOff+Diff
;
Move = XOff),
X3 is max(X1,X2),
X4 is X3+Move,
Y2 is min(Y0,Y1),
redraw_db(Rest,Interp,Num1,Num,X4,X,Y2,Y,XOff,YOff).
lex(A,B,C, _, _, _) :-
lex(A, B, C).
% ============================================================
% Formula decomposition. Has extra difference list pairs for
% information necessary to reconstruct natural deduction proof
% ============================================================
% = antecedent types
link1(lit(A),V,S,Pos0,Pos1,N,N,Id0,Id,Rules,Rules,H,H,
Con,Con,[neg(A,V,S,Id0,Pos0,Pos1)|As],As,Ps,Ps,Es,Es) :-
Id is Id0+1.
link1(dia(J,X),V,R,Pos0,Pos1,N0,N,Id0,Id,Rules0,Rules,
[rule(hyp(_),unzip(J,R),X,'$VAR'(N0),[])|H0],H,
[cf(unzip(J,R),dedia(V),'$VAR'(N0))|Con0],Con,
As0,As,Ps0,Ps,Es0,Es) :-
(continuous_dia(J) -> Pos2=Pos0,Pos3=Pos1 ; true),
N1 is N0+1,
link1(X,dedia(V),unzip(J,R),Pos2,Pos3,N1,N,Id0,Id,Rules0,Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es).
link1(box(J,X),V,R,Pos0,Pos1,N0,N,Id0,Id,[box(J,zip(J,R)-X)|Rules0],Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es) :-
(continuous_dia(J) -> Pos2=Pos0,Pos3=Pos1 ; true),
link1(X,debox(V),zip(J,R),Pos2,Pos3,N0,N,Id0,Id,Rules0,Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es).
link1(dr(J,X,Y),U,R,Pos0,Pos1,N0,N,Id0,Id,[dr(J,p(J,R,S)-X,S-Y)|Rules0],Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=Pos5 ; true),
link1(X,appl(U,V),p(J,R,S),Pos2,Pos5,N0,N1,Id0,Id1,Rules0,Rules1,
H0,H1,Con0,Con1,As0,As1,Ps0,Ps1,Es0,Es1),
link0(Y,V,S,Pos3,Pos4,N1,N,Id1,Id,Rules1,Rules,
H1,H,Con1,Con,As1,As,Ps1,Ps,Es1,Es).
link1(dl(J,Y,X),U,R,Pos0,Pos1,N0,N,Id0,Id,[dl(J,S-Y,p(J,S,R)-X)|Rules0],Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=Pos5 ; true),
link0(Y,V,S,Pos5,Pos2,N0,N1,Id0,Id1,Rules0,Rules1,
H0,H1,Con0,Con1,As0,As1,Ps0,Ps1,Es0,Es1),
link1(X,appl(U,V),p(J,S,R),Pos4,Pos3,N1,N,Id1,Id,
Rules1,Rules,H1,H,Con1,Con,As1,As,Ps1,Ps,Es1,Es).
link1(p(J,X,Y),V,R,Pos0,Pos1,N0,N,Id0,Id,Rules0,Rules,
[rule(hyp(_),l(J,R),X,'$VAR'(N0),[]),
rule(hyp(_),r(J,R),Y,'$VAR'(N1),[])|H0],H,
[cf(l(J,R),fst(V),'$VAR'(N0)),cf(r(J,R),snd(V),'$VAR'(N1))|Con0],Con,
As,As,[N0-N1|Ps],Ps,
[vertex(N0,Bs,Qs),vertex(N1,Cs,Rs)|Es0],Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=c(N0),Pos5=c(N0) ; true),
N1 is N0+1,
N2 is N1+1,
link1(X,fst(V),l(J,R),Pos2,Pos4,N2,N3,Id0,Id1,
Rules0,Rules1,H0,H1,Con0,Con1,Bs,[],Qs,[],Es0,Es1),
link1(Y,snd(V),r(J,R),Pos5,Pos3,N3,N,Id1,Id,
Rules1,Rules,H1,H,Con1,Con,Cs,[],Rs,[],Es1,Es).
% = succedent types
link0(lit(A),V,S,Pos0,Pos1,N,N,Id0,Id,Rules,Rules,H,H,
Con,Con,[pos(A,V,S,Id0,Pos0,Pos1)|As],As,Ps,Ps,Es,Es) :-
Id is Id0+1.
link0(dia(J,X),condia(V),zip(J,R),Pos0,Pos1,N0,N,Id0,Id,
[dia(J,R-X)|Rules0],Rules,H0,H,Con0,Con,
As0,As,Ps0,Ps,Es0,Es) :-
(continuous_dia(J) -> Pos2=Pos0,Pos3=Pos1 ; true),
link0(X,V,R,Pos2,Pos3,N0,N,Id0,Id,Rules0,Rules,H0,H,
Con0,Con,As0,As,Ps0,Ps,Es0,Es).
link0(box(J,X),conbox(V),unpack(J,R),Pos0,Pos1,N0,N,Id0,Id,Rules0,Rules,
H0,H,Con0,Con,As0,As,Ps0,Ps,Es0,Es) :-
(continuous_dia(J) -> Pos2=Pos0,Pos3=Pos1 ; true),
link0(X,V,R,Pos2,Pos3,N0,N,Id0,Id,Rules0,Rules,H0,H,
Con0,Con,As0,As,Ps0,Ps,Es0,Es).
link0(dr(J,X,Y),lambda('$VAR'(N0),V),dr(J,R,x-'$VAR'(N0)),Pos0,Pos1,N0,N,Id0,Id,
Rules0,Rules,[rule(hyp(_),x-'$VAR'(N0),Y,'$VAR'(N0),[])|H0],H,Con0,Con,
As,As,[N0-N1|Ps],Ps,
[vertex(N0,Bs,Qs),vertex(N1,Cs,Rs)|Es0],Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=c(N0),Pos5=c(N0) ; true),
N1 is N0+1,
N2 is N1+1,
link1(Y,'$VAR'(N0),x-'$VAR'(N0),Pos3,Pos4,N2,N3,Id0,Id1,
Rules0,Rules1,H0,H1,Con0,Con1,Bs,[],Qs,[],Es0,Es1),
link0(X,V,R,Pos2,Pos5,N3,N,Id1,Id,Rules1,Rules,
H1,H,Con1,Con,Cs,[],Rs,[],Es1,Es).
link0(dl(J,Y,X),lambda('$VAR'(N0),V),dl(J,x-'$VAR'(N0),R),Pos0,Pos1,N0,N,Id0,Id,
Rules0,Rules,[rule(hyp(_),x-'$VAR'(N0),Y,'$VAR'(N0),[])|H0],H,Con0,Con,
As,As,[N0-N1|Ps],Ps,
[vertex(N0,Bs,Qs),vertex(N1,Cs,Rs)|Es0],Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=c(N0),Pos5=c(N0) ; true),
N1 is N0+1,
N2 is N1+1,
link0(X,V,R,Pos5,Pos3,N2,N3,Id0,Id1,Rules0,Rules1,
H0,H1,Con0,Con1,Bs,[],Qs,[],Es0,Es1),
link1(Y,'$VAR'(N0),x-'$VAR'(N0),Pos4,Pos2,N3,N,Id1,Id,
Rules1,Rules,H1,H,Con1,Con,Cs,[],Rs,[],Es1,Es).
link0(p(J,X,Y),pair(U,V),p(J,R,S),Pos0,Pos1,N0,N,Id0,Id,
[p(J,R-X,S-Y)|Rules0],Rules,H0,H,Con0,Con,
As0,As,Ps0,Ps,Es0,Es) :-
(continuous(J) -> Pos2=Pos0,Pos3=Pos1,Pos4=Pos5 ; true),
link0(Y,V,S,Pos5,Pos3,N0,N1,Id0,Id1,Rules0,Rules1,
H0,H1,Con0,Con1,As0,As1,Ps0,Ps1,Es0,Es1),
link0(X,U,R,Pos2,Pos4,N1,N,Id1,Id,Rules1,Rules,
H1,H,Con1,Con,As1,As,Ps1,Ps,Es1,Es).
% ============================================================
% Graph Reductions
% ============================================================
reduce_graph(G0,G) :-
select(vertex(N,As,Ps0),G0,G1),
select(M-M,Ps0,Ps),
!,
select(vertex(M,Bs,Qs),G1,G2),
merge_vertices(vertex(N,As,Ps),vertex(M,Bs,Qs),G2,G3),
reduce_graph(G3,G).
reduce_graph(G,G) :-
connected(G).
replace_edges([],_,_,[]).
replace_edges([vertex(N,As,Ps)|Ls],X,Y,[vertex(N,As,Qs)|Ms]) :-
replace_edges1(Ps,X,Y,Qs),
replace_edges(Ls,X,Y,Ms).
replace_edges1([],_,_,[]).
replace_edges1([P1-P2|Ps],X,Y,[Q1-Q2|Qs]) :-
replace_vertex(P1,X,Y,Q1),
replace_vertex(P2,X,Y,Q2),
replace_edges1(Ps,X,Y,Qs).
replace_vertex(V,X,Y,W) :-
( V=X ->
W=Y
;
W=V).
connected([_]) :- !.
connected(L) :-
connected1(L).
connected1([vertex(_,As,Ps)|R]) :-
(As = [] ->
Ps = [_|_],connected1(R)
;
connected1(R)
).
connected1([]).
% = cyclic(+Vertex1,+Vertex2,+Graph)
% true if linking Vertex1 and Vertex2 in Graph will produce
% a cycle for some switching of Graph.
% - If Vertex1 and Vertex2 don't have a common ancestor, linking
% them won't produce a cycle.
% - If Vertex1 and Vertex2 have a common ancestor, linking them
% will produce a cycle unless they are on different branches
% of a par link.
cyclic(E1,E2,G0) :-
select(A,[E1,E2|G0],G1),
ancestor(A,E1,G1,_,Path1,[]),
ancestor(A,E2,G1,_,Path2,[]),
!,
\+ branches(Path1,Path2).
branches([X|Xs],[Y|Ys]) :-
X=l(P),Y=r(P) -> true
; X=r(P),Y=l(P) -> true
; X=Y ->
branches(Xs,Ys).
ancestor(vertex(N,_,Ps),vertex(M,_,Rs),G0,G,L0,L) :-
( N=M,
L=L0
;
Ps=[P1-P2|Ps0],
( select(vertex(P1,_,Qs),G0,G1),
L0=[l(P1-P2)|L1],
ancestor(vertex(P1,_,Qs),vertex(M,_,Rs),G1,G,L1,L)
;
select(vertex(P2,_,Qs),G0,G1),
L0=[r(P1-P2)|L1],
ancestor(vertex(P2,_,Qs),vertex(M,_,Rs),G1,G,L1,L)
;
ancestor(vertex(N,_,Ps0),vertex(M,_,Rs),G0,G,L0,L)
)
).
merge_vertices(vertex(N,As,Ps),vertex(M,Bs,Qs),G0,[vertex(N,Cs,Rs)|G]) :-
append(Bs,As,Cs),
append(Qs,Ps,Rs),
replace_edges(G0,M,N,G).
prove([],vertex(_,[],[]),_,_,_,_,_).
prove([G0|Gs0],G,[H0|Hs0],H,EstL0,Max,XOff) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,'update',_),
portray_estimate(Interp,EstL0,Max),
tcl_eval(Interp,'set runningstate',Running),
( Running = "selected" ->
/* user selected atomic formula */
tcl_eval(Interp,'set atomid1',AtomS1),
number_codes(Id0,AtomS1),
select_atom_id(Id0,Atom0,Vertex0,Atom1,Vertex1,
[G,G0|Gs0],Gs1,[H,H0|Hs0],Hs1)
;
/* select ground formula */
select_atom_id1(Id0,Atom0,Vertex0,Atom1,Vertex1,
[G,G0|Gs0],Gs1,[H,H0|Hs0],Hs1)
),
atom_formula(Atom0,Form),
tcl_eval(Interp,format('detailedtext $runningstate "Linking ~w"',[Form]),_),
find_conj_ids(Atom0,Gs1,Ids),
tcl_eval(Interp,format('focus .stats.c\n\
set tags [.stats.c gettags v~w]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+(n)[0-9]+$}]\n\
set nums [lindex $tags $list_ind]\n\
set axs [split $nums n]\n\
.stats.c addtag node withtag n[lindex $axs 1]\n\
.stats.c addtag node withtag n[lindex $axs 2]\n\
.stats.c delete v~w',[Id0,Id0]),_),
get_estimate(Atom0,f(EA,EM,_EN0),EstL0,EstL),
select_conj_ids(Ids,Id0,Atom0,Vertex2,Atom1,Vertex3,
Gs1,Gs2,Hs1,Hs2,EA,EM,EN,EstL,Max,XOff),
( cyclic(Vertex0,Vertex2,Gs2) ->
tcl_eval(Interp,'detailedtext $runningstate "Cyclic!"',_),
wait_running(Interp,_),
fail
;
true
),
merge_vertices(Vertex0,Vertex2,Gs2,Gs3),
merge_vertices(Vertex1,Vertex3,Hs2,Hs3),
( reduce_graph(Gs3,[G4|Gs4]) ->
reduce_graph(Hs3,[H4|Hs4]),
tcl_eval(Interp,'incr acclinks',_),
tcl_eval(Interp,format('detailedtext $runningstate "Linked ~w"',[Form]),_),
wait_running(Interp,_),
tcl_eval(Interp,'set eagerl',Eager),
( Eager = "none" ->
G5=G4,Gs5=Gs4
;
remove_all_divisions([G4|Gs4],[G5|Gs5])
),
tcl_eval(Interp,'incr labellinks',_),
prove(Gs5,G5,Hs4,H4,[f(EA,EM,EN)|EstL],Max,XOff)
;
tcl_eval(Interp,'detailedtext $runningstate "Disconnected!"',_),
wait_running(Interp,_),
fail
).
select_atom_id(Id,Atom0,vertex(N,As,Ps),Atom1,vertex(N,Bs,Qs),G0,G,H0,H) :-
duo_select(vertex(N,As0,Ps),vertex(N,Bs0,Qs),G0,G,H0,H),
duo_select(Atom0,Atom1,As0,As,Bs0,Bs),
atom_id(Atom0,Id),
!.
select_atom_id1(Id,Atom0,vertex(N,As,Ps),Atom1,vertex(N,Bs,Qs),G0,G,H0,H) :-
duo_select(vertex(N,As0,Ps),vertex(N,Bs0,Qs),G0,G,H0,H),
duo_select(Atom0,Atom1,As0,As,Bs0,Bs),
atom_label(Atom0,Label),
is_ground(Label),
atom_id(Atom0,Id),
!.
select_conj_ids([],Id0,_,_,_,_,_,_,_,_,_,_,_,_,_,_) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,format('focus .stats.c\n\
set tags [.stats.c gettags v~w]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+(n)[0-9]+$}]\n\
set nums [lindex $tags $list_ind]\n\
set axs [split $nums n]\n\
.stats.c addtag node withtag n[lindex $axs 1]\n\
.stats.c addtag node withtag n[lindex $axs 2]\n\
.stats.c delete v~w',[Id0,Id0]),_),
fail.
select_conj_ids([Id1|Ids0],Id0,Atom0,vertex(N,As,Ps),Atom1,vertex(N,Bs,Ps),G0,G,H0,H,EA,EM,EN,EstL,Max,XOff) :-
my_tk_interpreter(Interp),
atom_formula(Atom0,Form),
tcl_eval(Interp,format('detailedtext $runningstate "Linking ~p"',[Form]),_),
tcl_eval(Interp,format('focus .stats.c\n\
set tags [.stats.c gettags v~w]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+(n)[0-9]+$}]\n\
set nums [lindex $tags $list_ind]\n\
set axs [split $nums n]\n\
.stats.c addtag node withtag n[lindex $axs 1]\n\
.stats.c addtag node withtag n[lindex $axs 2]\n\
.stats.c delete v~w',[Id0,Id0]),_),
tcl_eval(Interp,format('.stats.c delete selectbox\n\
set tags [.stats.c gettags [.stats.c find withtag "n~w"]]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+$}]\n\
set path [lindex $tags $list_ind]\n\
set box [.stats.c bbox $path]\n\
set boxl [expr [lindex $box 0] -1]\n\
set boxr [expr [lindex $box 2] +1]\n\
set boxu [expr [lindex $box 1] -1]\n\
set boxd [expr [lindex $box 3] +1]\n\
set s_tags {}\n\
lappend s_tags selectbox $path\n\
.stats.c create line $boxl $boxu $boxr $boxu -tags $s_tags\n\
.stats.c create line $boxl $boxd $boxr $boxd -tags $s_tags\n\
.stats.c create line $boxl $boxd $boxl $boxu -tags $s_tags\n\
.stats.c create line $boxr $boxu $boxr $boxd -tags $s_tags',[Id0]),_),
/* draw white box around possible conjugates */
box_ids([Id1|Ids0],Interp,white),
wait_running(Interp,Running2),
( Running2 = "linked" ->
tcl_eval(Interp,'set runningstate "creep"',_),
tcl_eval(Interp,'activate .stats',_),
tcl_eval(Interp,'set atomid1',AtomS3),
tcl_eval(Interp,'set atomid2',AtomS2),
number_codes(Id3,AtomS3),
number_codes(Id2,AtomS2),
(Id2=Id0 -> Id=Id3 ; Id=Id2),
Ids1 = [Id1|Ids0]
;
Running2 = "commit" ->
tcl_eval(Interp,'set runningstate "creep"',_),
tcl_eval(Interp,'activate .stats',_),
tcl_eval(Interp,'set atomid1',AtomS3),
tcl_eval(Interp,'set atomid2',AtomS2),
number_codes(Id3,AtomS3),
number_codes(Id2,AtomS2),
(Id2=Id0 -> Id=Id3 ; Id=Id2),
Ids1 = [Id]
;
/* user did not select conjugate; try first one */
Id=Id1,
Ids1 = [Id1|Ids0]
),
length(Ids1,EN0),
portray_estimate(Interp,[f(EA,EM,EN0)|EstL],Max),
( select(Id,Ids1,Ids) ->
( Id0 < Id -> IdS = Id0, IdE = Id
; IdS = Id, IdE = Id0),
my_tk_interpreter(Interp),
tcl_eval(Interp,'incr totallinks',_),
tcl_eval(Interp,format('focus .stats.c\n\
.stats.c delete selectbox\n\
set tags [.stats.c gettags v~w]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+(n)[0-9]+$}]\n\
set nums [lindex $tags $list_ind]\n\
set axs [split $nums n]\n\
.stats.c addtag node withtag n[lindex $axs 1]\n\
.stats.c addtag node withtag n[lindex $axs 2]\n\
.stats.c delete v~w',[Id0,Id0]),_),
tcl_eval(Interp,format('focus .stats.c\n\
set box1 [.stats.c bbox n~w]\n\
set box2 [.stats.c bbox n~w]',[Id0,Id]),_),
tcl_eval(Interp,format('set skip [expr ~w/2]\n\
set box1x [expr ([lindex $box1 0] + [lindex $box1 2])/2]\n\
set box2x [expr ([lindex $box2 0] + [lindex $box2 2])/2]\n\
set box1y [expr [lindex $box1 1]+2]\n\
set box2y [expr [lindex $box2 1]+2]\n\
set maxy [expr $box1y]\n\
for {set x ~w} {$x <= ~w} {incr x} {\n\
if {$height($x) < $maxy} {\n\
set maxy $height($x)\n\
}}\n\
set maxy [expr $maxy-$skip]\n\
set continue "yes"\n\
if {$box1x < $box2x} {\n\
set boxl $box1x\n\
set boxr $box2x\n\
} else {\n\
set boxl $box2x\n\
set boxr $box1x\n\
}\n\
while {$continue == "yes"} {\n\
set continue "no"\n\
foreach i [.stats.c find withtag top] {\n\
set boxi [.stats.c bbox $i]\n\
set boxil [expr [lindex $boxi 0]]\n\
set boxir [expr [lindex $boxi 2]]\n\
set boxit [expr [lindex $boxi 1]+2]\n\
if {($boxir > $boxl) && ($boxr > $boxil) && abs($boxit - $maxy) < $skip} {\n\
set maxy [expr $maxy-$skip]\n\
set continue "yes"\n\
}\n\
}}\n\
',[XOff,IdS,IdE]),_),
tcl_eval(Interp,format('.stats.c dtag n~w node\n\
.stats.c dtag n~w node',[Id0,Id]),_),
tcl_eval(Interp,format('.stats.c create line $box1x $box1y $box1x $maxy -tags "ax n~wn~w v~w"',[Id0,Id,Id0]),_),
tcl_eval(Interp,format('.stats.c create line $box2x $box2y $box2x $maxy -tags "ax n~wn~w v~w"',[Id0,Id,Id0]),_),
tcl_eval(Interp,format('.stats.c create line $box1x $maxy $box2x $maxy -tags "top ax n~wn~w v~w"',[Id0,Id,Id0]),_),
(duo_select(vertex(N,[A|As0],Ps),vertex(N,[B|Bs0],Ps),G0,G,H0,H),
duo_select(Conj0,Conj1,[A|As0],As,[B|Bs0],Bs),
atom_id(Conj0,Id),
unify_atoms(Atom0,Conj0),
unify_atoms(Atom1,Conj1),
EN = EN0,
tcl_eval(Interp,'incr planarlinks',_)
;
/* on backtracking try alternative ids */
select_conj_ids(Ids,Id0,Atom0,vertex(N,As,Ps),Atom1,vertex(N,Bs,Ps),G0,G,H0,H,EA,EM,EN,EstL,Max,XOff)
)
;
/* user made invalid selection, retry */
select_conj_ids([Id1|Ids0],Id0,Atom0,vertex(N,As,Ps),Atom1,vertex(N,Bs,Ps),G0,G,H0,H,EA,EM,EN,EstL,Max,XOff)
).
find_conj_ids(Atom0,Gs,Ids) :-
findall(Id,(select(vertex(_,As0,_),Gs,_),
select(Atom,As0,_),
conjugates_id(Atom0,Atom,Id)),Ids).
box_ids([],_,_).
box_ids([N|Ns],Interp,Color) :-
tcl_eval(Interp,format('set tags [.stats.c gettags [.stats.c find withtag "n~w"]]\n\
set list_ind [lsearch -regexp $tags {^(n)[0-9]+$}]\n\
set path [lindex $tags $list_ind]\n\
set box [.stats.c bbox $path]\n\
set boxl [expr [lindex $box 0] -1]\n\
set boxr [expr [lindex $box 2] +1]\n\
set boxu [expr [lindex $box 1] -1]\n\
set boxd [expr [lindex $box 3] +1]\n\
set s_tags {}\n\
lappend s_tags selectbox $path\n\
.stats.c create line $boxl $boxu $boxr $boxu -fill ~w -tags $s_tags\n\
.stats.c create line $boxl $boxd $boxr $boxd -fill ~w -tags $s_tags\n\
.stats.c create line $boxl $boxd $boxl $boxu -fill ~w -tags $s_tags\n\
.stats.c create line $boxr $boxu $boxr $boxd -fill ~w -tags $s_tags',[N,Color,Color,Color,Color]),_),
box_ids(Ns,Interp,Color).
unify_atoms(neg(B,V,S,_,P0,P1),pos(B,V,S,_,P2,P3)) :-
(P0=P2,P1=P3 ->
true
;
my_tk_interpreter(Interp),
tcl_eval(Interp,'detailedtext $runningstate "Failed on Continuity"',_),
wait_running(Interp,_),
fail).
unify_atoms(pos(B,V,S,_,P0,P1),neg(B,V,S,_,P2,P3)) :-
(P0=P2,P1=P3 ->
true
;
my_tk_interpreter(Interp),
tcl_eval(Interp,'detailedtext $runningstate "Failed on Continuity"',_),
wait_running(Interp,_),
fail).
atom_id(neg(_,_,_,Id,_,_),Id).
atom_id(pos(_,_,_,Id,_,_),Id).
atom_formula(neg(Form,_,_,_,_,_),Form).
atom_formula(pos(Form,_,_,_,_,_),Form).
atom_label(neg(_,_,Label,_,_,_),Label).
atom_label(pos(_,_,Label,_,_,_),Label).
conjugates_id(neg(Form,_,_,_,_,_),pos(Form,_,_,Id,_,_),Id).
conjugates_id(pos(Form,_,_,_,_,_),neg(Form,_,_,Id,_,_),Id).
% ============================================================
% Label Reductions
% ============================================================
% remove_all_divisions(+ProofNet,-NewNet)
% true if NewNet is ProofNet with remove_divisions applied
% to all ground labels.
remove_all_divisions([],[]).
remove_all_divisions([vertex(N,As0,Ps)|Rest0],[vertex(N,As,Ps)|Rest])
:-
remove_all_divisions1(As0,As),
remove_all_divisions(Rest0,Rest).
remove_all_divisions1([],[]).
remove_all_divisions1([A0|As0],[A|As]) :-
remove_all_divisions2(A0,A),
remove_all_divisions1(As0,As).
remove_all_divisions2(neg(A,B,Label0,C,D,E),neg(A,B,Label,C,D,E)) :-
( is_ground(Label0) ->
( check_lp(Label0) ->
Label = Label0
;
my_tk_interpreter(Interp),
tcl_eval(Interp,'.stats.txt configure -text "Checking Label Constraints"',_),
( remove_divisions(Label0,Label) ->
tcl_eval(Interp,'.stats.txt configure -text "Label Constraints Satisfied"',_),
wait_rewrite(Interp,_),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_)
;
/* if eager evaluation fails, show user failing label */
tcl_eval(Interp,'detailedtext $runningstate "Failed on Label Constraints"',_),
wait_running(Interp,_),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_),
fail
)
)
;
Label=Label0
).
remove_all_divisions2(pos(A,B,Label0,C,D,E),pos(A,B,Label,C,D,E)) :-
( is_ground(Label0) ->
( check_lp(Label0) ->
Label = Label0
;
trace,
my_tk_interpreter(Interp),
tcl_eval(Interp,'.stats.txt configure -text "Checking Label Constraints"',_),
( remove_divisions(Label0,Label) ->
tcl_eval(Interp,'.stats.txt configure -text "Label Constraints Satisfied"',_),
wait_rewrite(Interp,_),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_)
;
/* if eager evaluation fails, show user failing label */
tcl_eval(Interp,'detailedtext $runningstate "Failed on Label Constraints"',_),
wait_running(Interp,_),
tcl_eval(Interp,'.stats.txt configure -text "Linking"',_),
fail
)
)
;
Label=Label0
).
% remove_divisions(+Label,-DivFreeLabel)
% remove all dr, dl occurences from label
% first select_divisions/4 is called, which returns a (possibly
% non-ground) DivFreeLabel and a difference-list containing
% Division-Hole pairs
remove_divisions(S0,S) :-
create_rewrite_window,
my_tk_interpreter(Interp),
tcl_eval(Interp,'if {$rewritestate != "nonstop"} {\n\
if {$eagerl != "auto"} {\n\
set rewritestate $defaultrewrite\n\
} else {\n\
set rewritestate "continue"\n\
}\n\
}',_),
tcl_eval(Interp,'update\n\
set rewritestate',Rewrite),
((Rewrite = "nonstop";Rewrite = "continue") ->
display_rd_label(S0,0),
wait_rewrite(Interp,_),
remove_redexes(S0,Interp,S),
display_rd_label(S,0),
wait_rewrite(Interp,_)
;
tcl_eval(Interp,'incr labelgen',_),
breadth_star([],1,[1-S0|B],B,node(S0,0,empty,empty),Interp,[],S)
).
remove_divisions(_,_) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,'.stats.txt configure -text "Linking"\n\
if {[winfo exists .rewrite]} {\n\
.rewrite.c dtag node\n\
.rewrite.exit configure -state disabled\n\
}',_),
fail.
% search is breadth first with closed set, and succeeds only once
%
% solution(Label) = check_lp(Label)
% child(Parent,Child) = rewrite(Parent,Child)
% = breadth_star(+NewChildren,+QLength,+QFront,+QBack,+Closed,?Answer)
breadth_star([],N0,[Depth0-Node|F],B,Closed,Interp,Undo,Answer) :-
N0 > 0,
N is N0-1,
Depth is Depth0+1,
tcl_eval(Interp,'incr labelcount',_),
update_undo(Undo,Interp),
display_rd_label(Node,1),
tcl_eval(Interp,format('if {[winfo exists .rewrite]} {\n\
.rewrite.text configure -text "Depth: ~w Queue: ~w"}',[Depth0,N]),_),
tcl_eval(Interp,'update idletasks',_),
(wait_rewrite(Interp,Rewrite) ->
( Rewrite \== "stepped",
Rewrite \== "up",
Rewrite \== "undo",
check_lp(Node)
->
Answer = Node
;
( Rewrite = "undo" ->
/* undo previous rewrite step */
( Undo = [] ->
breadth_star([],N0,[Depth0-Node|F],B,Closed,Interp,Undo,Answer)
;
Undo=[U|Undo0],
breadth_star([],N0,[U|F],B,Closed,Interp,Undo0,Answer)
)
;
Rewrite = "up" ->
/* remove all items at current depth or below from queue */
remove_from_queue(N,F,B,Depth0,N1,F1,B1),
breadth_star([],N1,F1,B1,Closed,Interp,[],Answer)
;
Rewrite = "stepped" ->
/* user selected rewrite step */
'current label'(Label),
children(Node,Children),
union(Children,Closed,Closed1,Children1,[]),
( select(Label,Children1,Children2) ->
true
;
Children2=Children1
),
add_depth(Children2,Depth,Children3),
add_to_front([Depth-Label|Children3],Interp,N,F,N1,F1),
breadth_star([],N1,F1,B,Closed1,Interp,[Depth0-Node|Undo],Answer)
;
children(Node,Children),
union(Children,Closed,Closed1,Children1,[]),
add_depth(Children1,Depth,Children2),
breadth_star(Children2,N,F,B,Closed1,Interp,[],Answer)
)
)
;
breadth_star([],N,F,B,Closed,Interp,[],Answer)
).
breadth_star([X|Xs],N0,F,[X|B],Closed,Interp,Undo,Answer) :-
N is N0+1,
tcl_eval(Interp,'incr labelgen',_),
breadth_star(Xs,N,F,B,Closed,Interp,Undo,Answer).
% = children/2 will generate a set of children from a given parent.
children(ParentNode,ChildrenNodes) :-
findall(ChildNode,rewrite(ParentNode,ChildNode),ChildrenNodes).
% = normalize(+Label,-NormalLabel)
% perform label reductions and rewrite according to
% structural postulates until a normal label results.
% search is breadth first with closed set, and succeeds only once.
% solution(Label) = normal(Label)
% child(Parent,Child) = rewrite(Parent,Child)
% normalize(+Label,-NormalLabel)
% normalize Label with user interaction
normalize(Start0,Answer) :-
remove_divisions(Start0,Start),
create_rewrite_window,
my_tk_interpreter(Interp),
tcl_eval(Interp,'if {$rewritestate != "nonstop"} {\n\
set rewritestate $defaultrewrite\n\
}',_),
display_label(Start,0),
tcl_eval(Interp,'.stats.txt configure -text "Rewriting"',_),
tcl_eval(Interp,'incr labelgen',_),
breadth_star1([],1,[1-Start|B],B,node(Start,0,empty,empty),Interp,[],Answer),
display_label(Answer,0),
tcl_eval(Interp,'if {[winfo exists .rewrite]} {\n\
.rewrite.c dtag node\n\
}',_).
normalize(_,_) :-
my_tk_interpreter(Interp),
tcl_eval(Interp,'.stats.txt configure -text "Linking"\n\
if {[winfo exists .rewrite]} {\n\
.rewrite.c dtag node\n\
.rewrite.exit configure -state disabled\n\
}',_),
fail.
breadth_star1([],N0,[Depth0-Node|F],B,Closed,Interp,Undo,Answer) :-
N0 > 0,
N is N0-1,
Depth is Depth0+1,
tcl_eval(Interp,'incr labelcount',_),
display_label(Node,1),
update_undo(Undo,Interp),
tcl_eval(Interp,format('if {[winfo exists .rewrite]} {\n\
.rewrite.text configure -text "Depth: ~w Queue: ~w"}',[Depth0,N]),_),
tcl_eval(Interp,'update idletasks',_),
(wait_rewrite(Interp,Rewrite) ->
( Rewrite \== "stepped",
Rewrite \== "up",
Rewrite \== "undo",
normal(Node)
->
Answer = Node
;
( Rewrite = "undo" ->
/* undo previous rewrite step */
( Undo = [] ->
breadth_star1([],N0,[Depth0-Node|F],B,Closed,Interp,Undo,Answer)
;
Undo=[U|Undo0],
breadth_star1([],N0,[U|F],B,Closed,Interp,Undo0,Answer)
)
;
Rewrite = "up" ->
/* remove all items at current depth or below from queue */
remove_from_queue(N,F,B,Depth0,N1,F1,B1),
breadth_star1([],N1,F1,B1,Closed,Interp,[],Answer)
;
Rewrite = "stepped" ->
/* user selected rewrite step */
'current label'(Label),
children(Node,Children),
union(Children,Closed,Closed1,Children1,[]),
( select(Label,Children1,Children2) ->
true
;
Children2=Children1
),
add_depth(Children2,Depth,Children3),
add_to_front([Depth-Label|Children3],Interp,N,F,N1,F1),
breadth_star1([],N1,F1,B,Closed1,Interp,[Depth0-Node|Undo],Answer)
;
children(Node,Children),
union(Children,Closed,Closed1,Children1,[]),
add_depth(Children1,Depth,Children2),
breadth_star1(Children2,N,F,B,Closed1,Interp,[],Answer)
)
)
;
breadth_star1([],N,F,B,Closed,Interp,[],Answer)
).
breadth_star1([X|Xs],N0,F,[X|B],Closed,Interp,Undo,Answer) :-
N is N0+1,
tcl_eval(Interp,'incr labelgen',_),
breadth_star1(Xs,N,F,B,Closed,Interp,Undo,Answer).
% add_to_front(+List,+Length,+Front,-Length,-Front)
% add items in List to the front of the input queue.
add_to_front([],_,N,F,N,F).
add_to_front([X|Xs],Interp,N0,F0,N,[X|F]) :-
N1 is N0+1,
tcl_eval(Interp,'incr labelgen',_),
add_to_front(Xs,Interp,N1,F0,N,F).
% remove_from_queue(+L,+F,+B,+Depth,-L,-F,-B)
% remove items of depth above or equal to Depth
% from the queue.
remove_from_queue(0,B,B,_,0,B,B).
remove_from_queue(N0,[D0-X|F0],B0,D,N,F,B) :-
N0 > 0,
N1 is N0-1,
(D0>=D ->
remove_from_queue(N1,F0,B0,D,N,F,B)
;
F=[D0-X|F1],
remove_from_queue(N1,F0,B0,D,N2,F1,B),
N is N2+1).
add_depth([],_,[]).
add_depth([L|Ls0],D,[D-L|Ls]) :-
add_depth(Ls0,D,Ls).
% rewrite(+Label0,?Label)
% true if Label0 is obtainable from Label in a single residuation
% reduction or postulate rewrite
rewrite(D,D1) :-
reduce(D,D1).
rewrite(D,D1) :-
postulate(D,D1,_Name).
rewrite(unpack(J,D),unpack(J,D1)) :-
rewrite(D,D1).
rewrite(zip(J,D),zip(J,D1)) :-
rewrite(D,D1).
rewrite(p(J,D,G),p(J,D1,G)) :-
rewrite(D,D1).
rewrite(p(J,G,D),p(J,G,D1)) :-
rewrite(D,D1).
rewrite(dr(J,D,G),dr(J,D1,G)) :-
rewrite(D,D1).
rewrite(dl(J,G,D),dl(J,G,D1)) :-
rewrite(D,D1).
% = Residuation reductions
reduce(p(J,l(J,D),r(J,D)),D). % product
reduce(dr(J,p(J,G,D),D),G). % division right
reduce(dl(J,D,p(J,D,G)),G). % division left
reduce(zip(J,unzip(J,D)),D). % diamond
reduce(unpack(J,zip(J,D)),D). % box
% = Linear precedence
check_lp(Node) :-