-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproof_generation.pl
1411 lines (1239 loc) · 47.1 KB
/
proof_generation.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
% -*- Mode: Prolog -*-
:- module(proof_generation, [generate_proof/2,
generate_sequent_proof/2,
generate_natural_deduction_proof/2,
generate_nd_proof/2,
generate_displacement_proof/2,
generate_hybrid_proof/2,
eta_reduce/2]).
:- use_module(latex, [latex_proof/1,
latex_nd/1,
latex_hybrid/1,
latex_displacement/1]).
:- use_module(replace, [rename_bound_variable/4,
rename_bound_variables/2,
replace_proofs_labels/4]).
:- use_module(auxiliaries, [select_formula/4,
subproofs/2,
rulename/2,
is_axiom/1,
antecedent/2,
identical_prefix/3,
identical_postfix/3,
identical_lists/2,
split_list/4]).
:- use_module(translations, [linear_to_hybrid/2,
linear_to_hybrid/3,
linear_to_hybrid/4,
linear_to_lambek/3,
linear_to_displacement/3,
displacement_sort/2,
simple_to_pure/4,
pure_to_simple/3,
compute_pros_term/5,
formula_type/2]).
:- use_module(options, [generate_diagnostics/1,eta_short/1]).
:- dynamic free_var/2, d_hyp/2.
% =======================================
% = Proof generation =
% =======================================
% = generate_sequent_proof(+InitialGraph, +ProofTrace)
%
% generate a sequent proof from the initial graph and proof trace
% given as arguments, and set the output to LaTeX.
generate_sequent_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
latex_proof(Proof).
% = generate_natural_deduction_proof(+InitialGraph, +ProofTrace)
%
% generate a natural deduction proof from the initial graph and proof trace
% given as arguments, and set the output to LaTeX.
generate_natural_deduction_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
sequent_to_nd(Proof, NDProof),
latex_proof(NDProof).
% = generate_nd_proof(+InitialGraph, +ProofTrace)
%
% as generate_natural_deduction_proof/2, but output the natural deduction
% proof with implicit antecedents and explicit hypothesis cancellation.
generate_nd_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
sequent_to_nd(Proof, NDProof),
latex_nd(NDProof).
% = generate_hybrid_proof(+InitialGraph, +ProofTrace)
%
% generate a proof in hybrid type-logical grammars based on the first-order
% linear logic proof. This predicate presupposed the lexical entries are
% all hybrid lexical entries.
generate_hybrid_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
sequent_to_nd(Proof, NDProof),
nd_to_hybrid(NDProof, HProof),
latex_hybrid(HProof).
% = generate_displacement_proof(+InitialGraph, +ProofTrace)
%
% generate a displacement calculus proof (in natural deduction)
% from the first-order natural deduction proof
generate_displacement_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
sequent_to_nd(Proof, NDProof),
latex_nd(NDProof),
nd_to_displacement(NDProof, DProof),
latex_displacement(DProof).
% = generate_proof(+InitialGraph, +ProofTrace)
%
% generate a sequent proof, natural deduction proof and hybrid proof of the
% same (hybrid) sequent.
generate_proof(Graph, Trace) :-
node_proofs(Graph, Proofs),
combine_proofs(Trace, Proofs, Proof),
sequent_to_nd(Proof, NDProof),
(
/* use presence of (hybrid grammar) lex/4 predicate as indication */
/* that the current grammar is a hybrid grammar */
current_predicate(lex/4),
nd_to_hybrid(NDProof, HProof)
->
latex_hybrid(HProof)
;
true
),
(
nd_to_displacement(NDProof, DisplacementProof)
->
latex_displacement(DisplacementProof)
;
true
),
latex_nd(NDProof),
latex_proof(Proof).
combine_proofs([], [Proof], Proof).
combine_proofs([N0-par(N1)|Rest], Ps0, Proof) :-
selectchk(N0-P0, Ps0, Ps1),
selectchk(N1-P1, Ps1, Ps2),
combine(P0, P1, N0, N1, P2),
replace_proofs_labels([P2|Ps2], N0, N1, Ps),
!,
combine_proofs(Rest, Ps, Proof).
combine_proofs([N0-univ(V,N1)|Rest], Ps0, Proof) :-
selectchk(N0-P0, Ps0, Ps1),
selectchk(N1-P1, Ps1, Ps2),
combine_univ(P0, P1, N0, N1, V, P2),
replace_proofs_labels([P2|Ps2], N0, N1, Ps),
!,
combine_proofs(Rest, Ps, Proof).
combine_proofs([ax(N1,AtV1,AtO1,N0,AtV0,AtO0)|Rest], Ps0, Proof) :-
select_neg_axiom(Ps0, Ps1, AtV1, AtO1, _-CL, A, _-LeftProof),
A = _-at(_, AtV0, AtO0, _),
proof_diagnostics('~NNeg (~D) ~D,~D:~2n', [N0, AtV0,AtO0], LeftProof),
select_pos_axiom(Ps1, Ps2, AtV0, AtO0, Gamma, _-CR, _-RightProof),
Gamma = [_-at(_, AtV1, AtO1, _)],
proof_diagnostics('~NPos (~D) ~D,~D:~2n', [N1, AtV1, AtO1], RightProof),
RightProof = rule(_, Ant, D, _),
LeftProof = rule(_, Gamma0, _, _),
split_antecedent(Ant, AtV1, AtO1, Delta1, Delta2),
append(Delta1, Gamma0, GDP1),
append(GDP1, Delta2, GDP),
print_diagnostics('~NUnifier: ~@~2n', [print_unifier(CL,CR)]),
unify_atoms(_-CL, _-CR),
try_cut_elimination_right(LeftProof, RightProof, GDP, D, Gamma0, _-CL, _-CR, Rule),
proof_diagnostics('~NResults (~D):~2n', [N0], Rule),
diagnostics_rule,
replace_proofs_labels([N0-Rule|Ps2], N1, N0, Ps),
!,
combine_proofs(Rest, Ps, Proof).
combine_proofs([Next|_], CurrentProofs, Proof) :-
/* dump all partial proofs in case of failure (useful for inspection) */
format(user_error, '~N{Error: proof generation failed!}~nNext:~p~2n', [Next]),
numbervars(CurrentProofs, 0, _),
keysort(CurrentProofs, SortedProofs),
/* exploits the fact that the top-level is a failure-driven loop */
member(N-Proof, SortedProofs),
format(latex, '~2n ~D. ', [N]).
% = split_antecedent(+GammaADelta, +V, +O, Gamma, Delta)
%
% split antecedent GammaADelta at the atomic formula indicated by identifier V, O returning
% the formulas before it in Gamma and thos after it in Delta.
split_antecedent([A|As], V, O, Bs, Delta) :-
(
A = _-at(_,AtV,AtO,_),
AtV == V,
AtO == O
->
Bs = [],
Delta = As
;
Bs = [A|Bs0],
split_antecedent(As, V, O, Bs0, Delta)
).
% = split_antecedent(+GammaADelta, +A, Gamma, Delta)
%
% split antecedent GammaADelta at the formula A returning
% the formulas before it in Gamma and thos after it in Delta.
split_antecedent([A0|As], A, Bs, Delta) :-
(
same_formula(A0, A)
->
Bs = [],
Delta = As
;
Bs = [A0|Bs0],
split_antecedent(As, A, Bs0, Delta)
).
% = trivial_cut_elimination(+LeftSubProof, +RightSubProof, +ConclusionAntecedent, +ConclusionSuccedent, -NewProof)
trivial_cut_elimination(P1, P2, GDP, C, rule(Nm, GDP, C, R)) :-
is_axiom(P1),
!,
rulename(P2, Nm),
subproofs(P2, R).
trivial_cut_elimination(P1, P2, GDP, C, rule(Nm, GDP, C, R)) :-
is_axiom(P2),
!,
rulename(P1, Nm),
subproofs(P1, R).
trivial_cut_elimination(P1, P2, GDP, C, rule(cut, GDP, C, [P2,P1])).
% = try_cut_elimination_left(+LeftProof, +RightProof, +GammaDelta, +Delta1, +Delta2, +A, +CL, +CR, -Proof)
%
% try to perform cut elimination of C (occurring as CL in LeftProof and as CR in RightProof) obtaining a Proof
% which conclusion GammaDelta |- A.
% More specifically, the RightProof has conclusion Delta1, CR, Delta2 |- A, LeftProof has conclusion Gamma |- CL
% and GammaDelta is equal to Delta1, Gamma, Delta2
try_cut_elimination_left(LeftProof, RightProof, GammaDelta, Delta1, Delta2, A, _-CL, _-CR, Proof) :-
turbo_cut_elimination_left(LeftProof, RightProof, Delta1, Delta2, A, CL, CR, Proof0),
!,
rulename(LeftProof, LeftN),
rulename(RightProof, RightN),
update_proof_cheat(Proof0, GammaDelta, A, Proof, LeftN, RightN).
try_cut_elimination_left(LeftProof, RightProof, GammaDelta, _, _, A, _, _, rule(cut, GammaDelta, A, [LeftProof,RightProof])).
% = try_cut_elimination_right(+LeftProof, +RightProof, +GammaDelta, +A, +Gamma, +CL, +CR, -Proof)
%
% try to perform cut elimination of C (occurring as CL in LeftProof and as CR in RightProof) obtaining a Proof
% with conclusion GammaDelta |- A.
% The subproofs are of Gamma |- CL and of Delta1, CR, Delta2 |- A
try_cut_elimination_right(LeftProof, RightProof, GammaDelta, A, Gamma, _-CL, _-CR, Proof) :-
turbo_cut_elimination_right(RightProof, LeftProof, Gamma, CL, CR, Proof0),
!,
rulename(LeftProof, LeftN),
rulename(RightProof, RightN),
update_proof_cheat(Proof0, GammaDelta, A, Proof, LeftN, RightN).
try_cut_elimination_right(LeftProof, RightProof, GammaDelta, A, _Gamma, _CL, _CR, rule(cut, GammaDelta, A, [LeftProof,RightProof])).
% = as the name indicates, this predicate needs to be subsumed by the main cut elimination predicates later
update_proof_cheat(rule(Nm, Gamma0, _, Rs), Gamma, A, rule(Nm, Gamma, A, Rs), LeftN, RightN) :-
(
generate_diagnostics(true),
Gamma0 \= Gamma
->
format(user_error, '~n= Computed ~w ~w =~n', [LeftN,RightN]),
print_antecedent(Gamma0),
format(user_error, '~n= Correct =~n', []),
print_antecedent(Gamma)
;
true
).
print_antecedent(Ant) :-
print_antecedent(Ant, 0).
print_antecedent([], _).
print_antecedent([A|As], N0) :-
copy_term(A, AA),
numbervars(AA, N0, N),
format(user_error, '~p~n', [AA]),
print_antecedent(As, N).
% CL |- CL Gamma |- CR
% . .
% . .
% Gamma |- CR Delta, CL |- A Gamma, Delta |- A
%
% the proof on the left keeps CL constant in its right branch; in the proof of the right, we replace
% CL by Gamma throughout (and CL in the succedent by CR).
turbo_cut_elimination_left(rule(Nm, Gamma, _-CL, Rs0), RightProof, Delta1, Delta2, A, CL, CR, Proof) :-
(
Gamma = [_-CL], Rs0 = []
->
/* reached axiom */
Proof = RightProof
;
/* add Delta to Gamma and replace CL by A */
append(Delta1, Gamma, GammaDelta1),
append(GammaDelta1, Delta2, GammaDelta),
Proof = rule(Nm, GammaDelta, A, Rs),
turbo_cut_elimination_left1(Rs0, RightProof, Delta1, Delta2, A, CL, CR, Rs)
).
turbo_cut_elimination_left1([R0|Rs0], RightProof, Delta1, Delta2, A, CL0, CR, [R|Rs]) :-
(
R0 = rule(_, _,_-CL, _),
same_formula1(CL0, CL)
->
Rs = Rs0,
turbo_cut_elimination_left(R0, RightProof, Delta1, Delta2, A, CL, CR, R)
;
R = R0,
turbo_cut_elimination_left1(Rs0, RightProof, Delta1, Delta2, A, CL0, CR, Rs)
).
% CR |- CR Delta, CL |- A
% . .
% . .
% Gamma |- CR Delta, CL |- A Delta, Gamma |- A
%
% the proof on the left transforms CR to Gamma without changing the succedent; we can
% use this same sequence of rules on the right to transform (replace) CL by the
% successive antecedents of the left proof
turbo_cut_elimination_right(rule(Nm, Delta, A, Rs0), LeftProof, Gamma, CL, CR, Proof) :-
(
Delta = [_-CL0], same_formula1(CL0, CL), Rs0 = []
->
/* reached axiom */
Proof = LeftProof
;
/* replace CL in the antecedent by Gamma */
split_antecedent(Delta, _-CL, Delta1, Delta2),
%append(Delta1, [_-CL|Delta2], Delta),
append(Delta1, Gamma, GammaDelta1),
append(GammaDelta1, Delta2, GammaDelta),
Proof = rule(Nm, GammaDelta, A, Rs),
turbo_cut_elimination_right1(Rs0, LeftProof, Gamma, CL, CR, Rs)
).
% = proceed to the subproof containing CR
turbo_cut_elimination_right1([R0|Rs0], LeftProof, Gamma, CL0, CR, [R|Rs]) :-
(
antecedent_member(CL0, CL, R0)
->
Rs = Rs0,
turbo_cut_elimination_right(R0, LeftProof, Gamma, CL, CR, R)
;
R = R0,
turbo_cut_elimination_right1(Rs0, LeftProof, Gamma, CL0, CR, Rs)
).
antecedent_member(F, rule(_, Gamma, _, _)) :-
antecedent_member(F, _, Gamma).
antecedent_member(F0, F, rule(_, Gamma, _, _)) :-
antecedent_member1(Gamma, F0, F).
antecedent_member1([_-G|Gs], F0, F) :-
(
same_formula1(F0, G)
->
F = G
;
antecedent_member1(Gs, F0, F)
).
% = combine_univ(+Proof1, +Proof2, +Node1, +Node2, +VariableNumber, -Proof)
%
% combine Proof1 and Proof2 into Proof using a unary par contraction (with eigenvariable
% VariableNumber) which links Node1 to Node2
% = left rule for existential quantifier
combine_univ(P1, P2, N0, N1, V, N1-Rule) :-
P1 = rule(_, Gamma, N0-exists(var(V),N1-A), _),
P2 = rule(_, Delta0, C, _),
!,
/* TODO: guarantee this is the same formula occurrence, split_antecedent is too strict of a condition */
/* Q: are the node numbers enough to guarantee this? Verify! */
append(Delta1, [N1-A|Delta2], Delta0),
%split_antecedent(Delta0, _-A, Delta1, Delta2),
append(Delta1, [N1-exists(var(V),N1-A)|Delta2], Delta),
append(Delta1, Gamma, GD1),
append(GD1, Delta2, GD),
/* try to create a cut-free proof */
try_cut_elimination_left(P1, rule(el, Delta, C, [P2]), GD, Delta1, Delta2, C, N0-exists(var(V),N1-A), N0-exists(var(V),N1-A), Rule).
% = right rule for universal quantifier
combine_univ(P1, P2, N0, N1, V, N1-Rule) :-
P2 = rule(_, Gamma, N1-A, _),
P1 = rule(_, Delta, C, _),
/* TODO: guarantee this is the same formula occurrence, split_antecedent is too strict of a condition */
/* Q: are the node numbers enough to guarantee this? Verify! */
append(Delta0, [N0-forall(var(V),N1-A)|Delta1], Delta),
%split_antecedent(Delta, _-forall(var(V),N1-A), Delta0, Delta1),
append(Delta0, Gamma, GD0),
append(GD0, Delta1, GD),
/* try to create a cut-free proof */
try_cut_elimination_right(rule(fr,Gamma,N1-forall(var(V),N1-A), [P2]), P1, GD, C, Gamma, N0-forall(var(V),N1-A), N0-forall(var(V),N1-A), Rule).
% = combine(+Proof1, +Proof2, +Node1, +Node2, -Proof)
%
% combine Proof1 and Proof2 into Proof using a binary par contraction which links Node1
% to Node2 (since this is a valid contraction, the two edges leaving Node1 must arrive
% in the same node Node2)
% = left rule for product
combine(P1, P2, N0, N1, N1-Rule) :-
P1 = rule(_, Gamma, N0-p(N1-A, N1-B), _),
P2 = rule(_, Delta0, C, _),
!,
/* TODO: guarantee this is the same formula occurrence, split_antecedent is too strict of a condition */
/* Q: are the node numbers enough to guarantee this? Verify! */
select_same_formula(N1, B, Delta0, Delta1),
select_same_formula(N1, A, Delta1, Delta),
replace_formula(A, N1, N1-p(N1-A,N1-B), Delta1, Delta2),
append(Delta3, [N1-p(N1-A,N1-B)|Delta4], Delta2),
append(Gamma, Delta, GD),
/* try to create a cut-free proof */
try_cut_elimination_left(P1, rule(pl, Delta2, C, [P2]), GD, Delta3, Delta4, C, N0-p(N1-A, N1-B), N0-p(N1-A, N1-B), Rule).
% = right rule for implication
combine(P1, P2, N0, N1, N1-Rule) :-
P1 = rule(_, Gamma, A, _),
P2 = rule(_, Delta0, N1-D, _),
/* TODO: guarantee this is the same formula occurrence, split_antecedent is too strict of a condition */
/* Q: are the node numbers enough to guarantee this? Verify! */
append(Gamma0, [N0-impl(N1-C,N1-D)|Gamma1], Gamma),
% split_antecedent(Gamma, N0-impl(N1-C,N1-D), Gamma0, Gamma1),
select_same_formula(N1, C, Delta0, Delta),
append(Gamma0, Delta, GD0),
append(GD0, Gamma1, GD),
/* try to create a cut-free proof */
try_cut_elimination_right(rule(ir, Delta, N0-impl(N1-C,N1-D), [P2]), P1, GD, A, Delta, N0-impl(N1-C,N1-D), N0-impl(N1-C,N1-D), Rule).
% = unify_atoms(+Atom1, +Atom2)
%
% true if Atom1 and Atom2 unify when disregarding the unique two-integer
% identifiers
unify_atoms(_-at(A, _, _, Vs), _-at(A, _, _, Vs)).
% = same_formula(+Formula1, +Formula2)
%
% true if Formula1 and Formula2 are the same when disregarding the subformula
% numbering
same_formula(_-F0, _-F) :-
same_formula1(F0, F).
same_formula1(F0, F) :-
/* variable subformulas unify */
var(F0),
!,
F0 = F.
same_formula1(F0, F) :-
var(F),
!,
F = F0.
same_formula1(at(A,Id1,Id2,_), at(A,Id3,Id4,_)) :-
/* demand strict identity of atoms */
Id1 == Id3,
Id2 == Id4.
same_formula1(forall(X,F0), forall(Y,F)) :-
X == Y,
same_formula(F0, F).
same_formula1(exists(X,F0), exists(Y,F)) :-
X == Y,
same_formula(F0, F).
same_formula1(impl(A0,B0), impl(A,B)) :-
same_formula(A0, A),
same_formula(B0, B).
same_formula1(p(A0,B0), p(A,B)) :-
same_formula(A0, A),
same_formula(B0, B).
%
same_formula2(F0, F) :-
/* variable subformulas unify */
var(F0),
!,
F0 = F.
same_formula2(F0, F) :-
var(F),
!,
F = F0.
same_formula2(at(A,Id1,Id2,Vs), at(A,Id3,Id4,Vs)) :-
/* demand strict identity of atoms and unify variables */
Id1 == Id3,
Id2 == Id4.
same_formula2(forall(X,_-F0), forall(Y,_-F)) :-
X = Y,
same_formula2(F0, F).
same_formula2(exists(X,_-F0), exists(Y,_-F)) :-
X = Y,
same_formula2(F0, F).
same_formula2(impl(_-A0,_-B0), impl(_-A,_-B)) :-
same_formula2(A0, A),
same_formula2(B0, B).
same_formula2(p(_-A0,_-B0), p(_-A,_-B)) :-
same_formula2(A0, A),
same_formula2(B0, B).
%
select_same_formula(N, F, L0, L) :-
select(N-F0, L0, L),
same_formula2(F0, F),
!.
% = select_neg_axiom(+InProofs, -OutProofs, +Vertex, +Order, -C, -A, -Proof)
%
% select the negative atomic formula indicated by the unique identifier Vertex-Order from InProofs, that is
% we are seeking a Proof with axiom
%
% C |- A
%
% such that C is the formula indicated by Vertex-Order and OutProofs are the other proofs.
select_neg_axiom([Proof|Ps], Ps, V, O, C, A, Proof) :-
select_neg_axiom1(Proof, V, O, C, A),
!.
select_neg_axiom([P|Ps], [P|Rs], V, O, C, A, Proof) :-
select_neg_axiom(Ps, Rs, V, O, C, A, Proof).
select_neg_axiom1(_-P, V, O, C, A) :-
select_neg_axiom1(P, V, O, C, A).
select_neg_axiom1(rule(ax, [M-at(At,V2,O2,Vars)], N-at(At,V1,O1,Vars), []), V, O, M-at(At,V2,O2,Vars), N-at(At,V1,O1,Vars)) :-
V2 == V,
O2 == O,
!.
select_neg_axiom1(rule(_, _, _, Rs), V, O, C, A) :-
select_neg_axiom_list(Rs, V, O, C, A).
select_neg_axiom_list([R|_], V, O, C, A) :-
select_neg_axiom1(R, V, O, C, A),
!.
select_neg_axiom_list([_|Rs], V, O, C, A) :-
select_neg_axiom_list(Rs, V, O, C, A).
% = select_pos_axiom(+InProofs, -Outproof, +Vertex, +Order, -Delta, -A, -Proof)
%
% select the positive atomic indicated by the unique identifier Vertex-Order from InProofs, that is
% we are seeking a Proof with conclusion
%
% Delta |- A
%
% such that A is the formula indicated by Vertex-Order and OutProofs are the other proofs.
select_pos_axiom([Proof|Ps], Ps, V, O, Delta, A, Proof) :-
select_pos_axiom1(Proof, V, O, Delta, A),
!.
select_pos_axiom([P|Ps], [P|Rs], V, O, Delta, A, Proof) :-
select_pos_axiom(Ps, Rs, V, O, Delta, A, Proof).
select_pos_axiom1(_-P, V, O, Delta, A) :-
select_pos_axiom1(P, V, O, Delta, A).
select_pos_axiom1(rule(ax, [M-at(At,V2,O2,Vars)], N-at(At,V1,O1,Vars), []), V, O, [M-at(At,V2,O2,Vars)], N-at(At,V1,O1,Vars)) :-
V1 == V,
O1 == O,
!.
select_pos_axiom1(rule(_, _, _, Rs), V, O, Delta, A) :-
select_pos_axiom_list(Rs, V, O, Delta, A).
select_pos_axiom_list([R|_], V, O, Delta, A) :-
select_pos_axiom1(R, V, O, Delta, A),
!.
select_pos_axiom_list([_|Rs], V, O, Delta, A) :-
select_pos_axiom_list(Rs, V, O, Delta, A).
% = select_ant_formula(+Antecedent, +Vertex, +Order, -Gamma, -A, -Delta)
%
% select the (negative) atomic formula indicated by Vertex-Order from the given Antecedent,
% dividing the antecedent into Gamma, A, Delta (Gamma is represented as a difference list)
select_ant_formula([N-F|Delta], V, O, [], N-at(At,V,O,Vars), Delta) :-
max_neg(F, at(At,V1,O1,Vars)),
V1 == V,
O1 == O,
!.
select_ant_formula([G|Gs], V, O, [G|Gamma], A, Delta) :-
select_ant_formula(Gs, V, O, Gamma, A, Delta).
% = node_proofs(+Graph, -Proofs)
%
% for each of the nodes in Graph, retrieve the stored formula and polarity of the node and
% compute the corresponding sequent proof
node_proofs([V|Vs], [P|Ps]) :-
node_proof1(V, P),
node_proofs(Vs, Ps).
node_proofs([], []) :-
diagnostics_rule.
node_proof1(vertex(N0, As, _, _), N0-Proof) :-
node_formula(N0, Pol, F),
node_proof2(As, F, N0, Pol, Proof),
proof_diagnostics('~w. ', [N0], Proof),
!.
% = node_proof2(+Atoms, +Formula, +NodeNumber, +Polarity, -Proof)
%
% create a Proof using matching Formula to its Atoms
node_proof2([], F, N, _, rule(ax, [N-F], N-F, [])).
node_proof2([A|As], F, N, Pol, Proof) :-
node_proof3(Pol, [A|As], F, N, Proof).
node_proof3(pos, L, F, N, Proof) :-
create_pos_proof(F, N, L, [], Proof).
node_proof3(neg, L, F, N, Proof) :-
max_neg_noid(F, MN0),
rename_bound_variables(MN0, MN),
create_neg_proof(F, N, L, [], MN, Proof).
% = max_neg(+Formula, -Conclusion)
%
% given a negative (antecedent) formula, compute the Conclusion of the proof we are computing for it;
% this is (maximal) subformula we reach following a path of negative subformulas.
max_neg(impl(_,_-F0), F) :-
!,
max_neg(F0, F).
max_neg(forall(_,_-F0), F) :-
!,
max_neg(F0, F).
max_neg(F, F).
% = max_neg(+Formula, -Conclusion)
%
% as max_neg, but erases the node id if the maximal negative formula is an atom
max_neg_noid(at(At, _, _, FVs), at(At, _, _, FVs)) :-
!.
max_neg_noid(impl(_,_-F0), F) :-
!,
max_neg_noid(F0, F).
max_neg_noid(forall(_,_-F0), F) :-
!,
max_neg_noid(F0, F).
max_neg_noid(F, F).
% = create_pos_proof(+NumberedPositiveFormula, +/-AtomsDL, -Proof)
create_pos_proof(N-A, L0, L, Proof) :-
create_pos_proof(A, N, L0, L, Proof).
% = create_pos_proof(+PositiveFormula, +NodeNumber, +/-AtomsDL, -Proof)
create_pos_proof(at(A,C,N,Vars), M, [pos(A,C,N,_,Vars)|L], L, rule(ax,[M-at(A,_C,_N,Vars)], M-at(A,C,N,Vars), [])) :-
!.
create_pos_proof(exists(X,N-A), N, L0, L, rule(er, Gamma, N-exists(Y,N-A3), [ProofA])) :-
!,
/* rename to make sure bound variable isn't unified */
rename_bound_variables(A, A2),
create_pos_proof(A, N, L0, L, ProofA),
ProofA = rule(_, Gamma, N-A2, _),
rename_bound_variable(exists(X,N-A2), X, Y, exists(Y,N-A3)).
create_pos_proof(p(N-A,N-B), N, L0, L, rule(pr, GD, N-p(N-A2,N-B2), [P1,P2])) :-
!,
create_pos_proof(A, N, L0, L1, P1),
create_pos_proof(B, N, L1, L, P2),
P1 = rule(_, Gamma, N-A2, _),
P2 = rule(_, Delta, N-B2, _),
append(Gamma, Delta, GD).
% complex (negative) subformula
create_pos_proof(F, N, L, L, rule(ax, [N-F], N-F, [])).
% = create_neg_proof(+NumberedNegativeFormula, +/-AtomsDL, +Goal, -Proof)
create_neg_proof(N-A, L0, L, Neg, Proof) :-
create_neg_proof(A, N, L0, L, Neg, Proof).
% = create_neg_proof(+NegativeFormula, +NodeNumber, +/-AtomsDL, +Goal, -Proof)
create_neg_proof(at(A,C,N,Vars), M, [neg(A,C,N,_,Vars)|L], L, at(A,C1,N1,Vars), rule(ax, [M-at(A,C,N,Vars)], M-at(A,C1,N1,Vars), [])) :-
!.
create_neg_proof(impl(N-A0,N-B), N, L0, L, Neg, rule(il, GD, N-Neg, [ProofA,ProofB])) :-
!,
remove_formula_indices(A0, A),
rename_bound_variables(A, AA),
create_pos_proof(AA, N, L0, L1, rule(Nm, Gamma, N-A0, Rs)),
create_neg_proof(B, N, L1, L, Neg, ProofB),
rename_bound_variables(B, B2),
/* put back the formula indices for the conclusion */
ProofA = rule(Nm, Gamma, N-A0, Rs),
ProofB = rule(_, Delta, _, _),
select_formula(B2, N, Delta, Delta_B),
append(Gamma, [N-impl(N-A0,N-B2)|Delta_B], GD).
create_neg_proof(forall(X,N-A), N, L0, L, Neg, rule(fl, GammaP, N-Neg, [ProofA])) :-
!,
rename_bound_variables(A, A2),
create_neg_proof(A, N, L0, L, Neg, ProofA),
ProofA = rule(_, Gamma, _C, _),
/* rename to make sure bound variables aren't unified */
replace_formula(A2, N, N-forall(Y,N-A3), Gamma, GammaP),
rename_bound_variable(forall(X,N-A2), X, Y, forall(Y,N-A3)).
% complex (positive) subformula
create_neg_proof(F, N, L, L, _, rule(ax, [N-F], N-F, [])).
% = remove_formula_indices(+FormulaId, -FormulaNoId)
%
% remove node identifier information on the atomic subformulas of FormulaId
% to produce FormulaNoId
remove_formula_indices(N-F0, N-F) :-
remove_formula_indices(F0, F).
remove_formula_indices(at(A,_,_,Vars), at(A,_,_,Vars)).
remove_formula_indices(forall(X, A0), forall(X, A)) :-
remove_formula_indices(A0, A).
remove_formula_indices(exists(X, A0), exists(X, A)) :-
remove_formula_indices(A0, A).
remove_formula_indices(impl(A0, B0), impl(A, B)) :-
remove_formula_indices(A0, A),
remove_formula_indices(B0, B).
remove_formula_indices(p(A0, B0), p(A, B)) :-
remove_formula_indices(A0, A),
remove_formula_indices(B0, B).
remove_formula_nodes(_-F0, F) :-
remove_formula_nodes(F0, F).
remove_formula_nodes(at(A,_,_,Vars), at(A,Vars)).
remove_formula_nodes(forall(X,A0), forall(X, A)) :-
remove_formula_nodes(A0, A).
remove_formula_nodes(exists(X,A0), exists(X, A)) :-
remove_formula_nodes(A0, A).
remove_formula_nodes(impl(A0,B0), impl(A,B)) :-
remove_formula_nodes(A0, A),
remove_formula_nodes(B0, B).
remove_formula_nodes(p(A0,B0), p(A,B)) :-
remove_formula_nodes(A0, A),
remove_formula_nodes(B0, B).
% = sequent_to_nd(+SequentProof, -NaturalDeductionProof)
%
% translate a sequent proof to a natural deduction proof
sequent_to_nd(SequentProof, NDproof) :-
sequent_to_nd(SequentProof, NDproof, 1, _NewIndex).
sequent_to_nd(_-R0, R, I0, I) :-
sequent_to_nd(R0, R, I0, I).
sequent_to_nd(rule(ax, [M-A1], N-A2, []), rule(ax, [M-A1], N-A2, []), I, I).
sequent_to_nd(rule(el, Gamma, C, [R]), rule(ee(I1), Gamma, C, [rule(ax,[N1-exists(X,N0-B0)], N1-exists(X,N0-B0), []), Proof]), I0, I) :-
member(N1-exists(X,N0-B0), Gamma),
antecedent_member(B0, _B1, R),
!,
sequent_to_nd(R, Proof0, I0, I1),
I is I1 + 1,
withdraw_hypothesis(Proof0, I1, B0, Proof).
sequent_to_nd(rule(er, Gamma, N-A, [R0]), rule(ei, Gamma, N-A, [R]), I0, I) :-
sequent_to_nd(R0, R, I0, I).
% R forall(X,B) |- forall(X,B) Proof0
% --------------------------
% Gamma, B |- C forall(X,B) |- B Gamma, B |- C
% ----------------------- -------------------------------------------
% Gamma, forall(X,B) |- C Gamma, forall(X,B) |- C
%
sequent_to_nd(rule(fl, Gamma0, C, [R]), Proof, I0, I) :-
% find a formula which is of the form forall(X,B) in the conclusion of the rule
% and B in the premiss of the rule.
member(N1-forall(X,N0-B0), Gamma0),
antecedent_member(B0, B1, R),
!,
sequent_to_nd(R, Proof0, I0, I),
antecedent(Proof0, GammaB),
select_same_formula(_NB, B1, GammaB, Gamma),
GammaDelta = [N1-forall(X,N0-B0)|Gamma],
try_cut_elimination_right(rule(fe, [N-forall(X,N0-B0)], N0-B1, [rule(ax, [N-forall(X,N-B0)], N1-forall(X,N0-B0), [])]),
Proof0, GammaDelta, C, [N-forall(X,N0-B0)], N-B0, N-B1, Proof).
sequent_to_nd(rule(fr, Gamma, N-A, [R0]), rule(fi, Gamma, N-A, [R]), I0, I) :-
sequent_to_nd(R0, R, I0, I).
% ProofA ProofC
%
% R1 R2 Delta |- A A -o B |- A -o B
% -----------------------------
% Delta |- A Gamma, B |- C Delta, A -o B |- B Gamma, B |- C
% -------------------------- ---------------------------------------
% Gamma, Delta, A -o B |- C Gamma, Delta, A -o B |- C
%
sequent_to_nd(rule(il, Ant, C, [R1,R2]), Proof, I0, I) :-
sequent_to_nd(R1, ProofA, I0, I1),
sequent_to_nd(R2, ProofC, I1, I),
ProofA = rule(_, Delta, _NA0, _),
ProofC = rule(_, GammaB, _, _),
member(M-impl(N-A,N-B0), Ant),
antecedent_member(B0, B, R2),
select_same_formula(_NB, B, GammaB, Gamma),
append(Delta, [M-impl(N-A,N-B0)], DeltaAB),
append(Gamma, DeltaAB, GammaDeltaAB),
try_cut_elimination_right(rule(ie, DeltaAB, N-B0, [ProofA,rule(ax, [M-impl(N-A,N-B0)], M-impl(N-A,N-B0), [])]),
ProofC, GammaDeltaAB, C, DeltaAB, N-B0, N-B, Proof).
sequent_to_nd(rule(ir, Gamma, _-impl(_-A,_-B), [R0]), rule(ii(I1), Gamma, impl(A,B), [R]), I0, I) :-
sequent_to_nd(R0, R1, I0, I1),
I is I1 + 1,
withdraw_hypothesis(R1, I1, A, R).
sequent_to_nd(rule(pl, Gamma, C, [R]), rule(pe(I1), Gamma, C, [rule(ax,[N0-p(N1-A0,N2-B0)], N0-p(N1-A,N2-B), []), Proof]), I0, I) :-
member(N0-p(N1-A0,N2-B0), Gamma),
antecedent_member(A0, A, R),
antecedent_member(B0, B, R),
!,
sequent_to_nd(R, Proof0, I0, I1),
I is I1 + 1,
withdraw_hypothesis(Proof0, I1, A, Proof1),
withdraw_hypothesis(Proof1, I1, B, Proof).
sequent_to_nd(rule(pr, Gamma, C, [R1,R2]), rule(pi, Gamma, C, [Proof1, Proof2]), I0, I) :-
sequent_to_nd(R1, Proof1, I0, I1),
sequent_to_nd(R2, Proof2, I1, I).
% = eta_reduce(+NDProof, -EtaShortNDProof)
%
% true if EtaShortNDProof is a eta normal form natural deduction proof of
% (first order linear logic) NDProof.
eta_reduce(InProof, OutProof) :-
(
eta_short(true)
->
eta_reduce_all(InProof, OutProof)
;
OutProof = InProof
).
% iterate eta_reduce1/2 until the proof stays the same
eta_reduce_all(Proof0, Proof) :-
eta_reduce1(Proof0, Proof1),
(
Proof1 == Proof0
->
Proof = Proof1
;
eta_reduce_all(Proof1, Proof)
).
eta_reduce1(N-Proof0, N-Proof) :-
eta_reduce1(Proof0, Proof).
% TODO: generalize to other product/existential quantifier-like rules
eta_reduce1(rule(ee(I), _, _, [ProofE, ProofC]), Proof) :-
replace_proof(ProofC, rule(ei, _, _, [rule(hyp(I),_,_, [])]), ProofE, Proof),
!.
eta_reduce1(rule(pe(I),_, _, [ProofP, ProofC]), Proof) :-
replace_proof(ProofC, rule(pi, _, _, [rule(hyp(I),_,_, []), rule(hyp(I),_,_,[])]), ProofP, Proof),
!.
eta_reduce1(rule(NmI, _, _, [rule(NmE,_,_, [Rule])]), Rule) :-
eta_pair_unary(NmI, NmE),
!.
eta_reduce1(rule(NmI, _, _, [rule(NmE, _, _, [rule(hyp(I), _, _, []),Rule])]), Rule) :-
eta_pair_binary(NmI, I, NmE),
!.
eta_reduce1(rule(NmI, _, _, [rule(NmE, _, _, [Rule,rule(hyp(I), _, _, [])])]), Rule) :-
eta_pair_binary(NmI, I, NmE),
!.
eta_reduce1(rule(Nm, Gamma, C, Ps0), rule(Nm, Gamma, C, Ps)) :-
eta_reduce_list(Ps0, Ps).
eta_reduce_list([], []).
eta_reduce_list([P0|Ps0], [P|Ps]) :-
eta_reduce1(P0, P),
eta_reduce_list(Ps0, Ps).
% = eta_pair_unary(?IntroductionRuleName, ?EliminationRuleName)
%
% pairs of introduction/elimination rules with a single premiss (for the elimination rule)
eta_pair_unary(fi, fe).
% = eta_pair_binary(?IntroductionRuleName, ?HypothesisIndex, ?EliminationRuleName)
%
% pairs of introduction/elimination rules with two premisses (for the elimination rule)
% HypothesisIndex is the unique integer corresponding to the withdrawn hypothesis
eta_pair_binary(ii(I), I, ie).
eta_pair_binary(dri(I), I, dre).
eta_pair_binary(dli(I), I, dle).
eta_pair_binary(dri(D,I), I, dre(D)).
eta_pair_binary(dli(D,I), I, dle(D)).
eta_pair_binary(hi(I), I, he).
% = replace_proof(+InProof, +ProofA, +ProofB, -OutProof)
%
% true if OutProof is InProof with the first occurrence of ProofA replaced by ProofB
% fails if there are no occurrences of ProofA in InProof.
replace_proof(Proof1, Proof1, Proof2, Proof2) :-
!.
replace_proof(rule(Nm, Gamma, C, Rs0), Proof1, Proof2, rule(Nm, Gamma, C, Rs)) :-
replace_proof_list(Rs0, Proof1, Proof2, Rs).
replace_proof_list([P0|Ps], A, B, [P|Ps]) :-
replace_proof(P0, A, B, P),
!.
replace_proof_list([P|Ps0], A, B, [P|Ps]) :-
replace_proof_list(Ps0, A, B, Ps).
% = max_hypothesis(+Proof, +MaxIn, -MaxOut)
%
% find the hypothesis with the maximal integer index in Proof
max_hypothesis(rule(hyp(I), _, _, _), Max0, Max) :-
!,
Max is max(I,Max0).
max_hypothesis(rule(_, _, _, Rs), Max0, Max) :-
max_hypothesis_list(Rs, Max0, Max).
max_hypothesis_list([], Max, Max).
max_hypothesis_list([R|Rs], Max0, Max) :-
max_hypothesis(R, Max0, Max1),
max_hypothesis_list(Rs, Max1, Max).
% = nd_to_hybrid(+NaturalDeductionProof, -HybridProof).
%
% translate a natural deduction first-order linear logic proof into a proof of
% hybrid type-logical grammars.
nd_to_hybrid(Proof0, Proof) :-
max_hypothesis(Proof0, 0, Max0),
Max1 is Max0 + 1,
nd_to_hybrid(Proof0, Max1, Max, Proof),
numbervars(Proof, Max, _).
nd_to_hybrid(rule(hyp(I), _, C0, []), Max, Max, rule(hyp(I), '$VAR'(I), HF, [])) :-
/* withdrawn hypothesis with unique identifier I */
remove_formula_nodes(C0, C),
linear_to_hybrid(C, HF),
/* compute the Church type of hybrid formula HF */
formula_type(HF, Type),
retractall(free_var(I, _)),
assert(free_var(I, Type)).
nd_to_hybrid(rule(ax, _, C0, []), Max0, Max, Rule) :-
/* HTLG lexicon rule */
remove_formula_nodes(C0, C),
/* recover lexical lambda term here */
linear_to_hybrid(C, VarList, _, HF),
numbervars(VarList, 0, _),
/* complex manipulations to allow HTLG grammars to use first-order */
/* quantifiers for things like case */
get_positions(VarList, N0, _R),
lexicon:hybrid_lookup(N0, HF0, Lambda0),
compute_pros_term(Lambda0, HF0, Lambda, Max0, Max),
!,
(
HF0 == HF
->
Rule = rule(ax, Lambda, HF, [])
;
Rule = rule(fe, Lambda, HF, [rule(ax, Lambda, HF0, [])])
).
nd_to_hybrid(rule(ie, _, _, [P1,rule(fe, _, _, [P2])]), Max0, Max, Rule) :-
!,
/* Lambek calculus elimination rule */
P2 = rule(_, _, C0, _),
nd_to_hybrid(P1, Max0, Max1, Proof1),
nd_to_hybrid(P2, Max1, Max2, Proof2),
antecedent(Proof1, Term1),
antecedent(Proof2, Term2),
remove_formula_nodes(C0, C),
/* compute the Lambek calculus formula LF corresponding to C */
linear_to_lambek(C, [_, _], LF),
/* use this formula LF to infer the relevant rule application */
lambek_rule(LF, Term1, Term2, Max2, Max, Proof1, Proof2, Rule).
nd_to_hybrid(rule(fi, _, C0, [rule(ii(I), _, _, [P1])]), Max0, Max, rule(Nm, Term, LF, [Proof1])) :-
/* Lambek calculus introduction rule */
remove_formula_nodes(C0, C),
linear_to_lambek(C, [_, _], LF),
nd_to_hybrid(P1, Max0, Max1, Proof1),
antecedent(Proof1, Term1),
retractall(free_var(I, _)),
compute_pros_term(appl(lambda('$VAR'(I),Term1),epsilon), LF, Term, Max1, Max),
/* use the main connective of the Lambek calculus formula LF to */
/* determine the rule name */
lambek_rule_name(LF, I, Nm).
nd_to_hybrid(rule(ie, _, C0, [P1,P2]), Max0, Max, rule(he, Term, HF, [Proof1,Proof2])) :-
remove_formula_nodes(C0, C),
linear_to_hybrid(C, HF),
nd_to_hybrid(P1, Max0, Max1, Proof1),
nd_to_hybrid(P2, Max1, Max2, Proof2),
antecedent(Proof1, Term1),
antecedent(Proof2, Term2),
compute_pros_term(appl(Term2,Term1), HF, Term, Max2, Max).
nd_to_hybrid(rule(ii(I), _, C0, [P1]), Max0, Max, rule(hi(I), Term, HF, [Proof1])) :-
remove_formula_nodes(C0, C),
linear_to_hybrid(C, HF),
nd_to_hybrid(P1, Max0, Max1, Proof1),
antecedent(Proof1, Term0),
retractall(free_var(I, _)),
compute_pros_term(lambda('$VAR'(I),Term0), HF, Term, Max1, Max).