-
Notifications
You must be signed in to change notification settings - Fork 4
/
nProvers.pro
284 lines (222 loc) · 6.97 KB
/
nProvers.pro
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
handprove(T0):-
%nl,ppp(T0),
expand_equiv(T0,T1),
%ppp(T1),
toAndHorn(T1,T),
%ppp(T),
ljand(T).
ljand(A):-ljand(A,[]).
%ljand(A,Vs):-ppp((Vs-->A)),fail. % just to trace steps
ljand(_,Vs):-memberchk(false,Vs),!.
ljand([],_):-!.
ljand([X|Xs],Vs):-!,ljand(X,Vs),ljand(Xs,Vs).
ljand(true,_):-!.
ljand(A,Vs):-memberchk(A,Vs),!.
ljand((B:-As),Vs1):-!,append(As,Vs1,Vs2),ljand(B,Vs2).
ljand(G,Vs0):- % atomic(G), G not on Vs1
fix_heads(Vs0,Vs1),
select((B:-As),Vs1,Vs2), % outer select loop
select(A,As,Bs), % inner select loop
ljand_imp(A,B,Vs2), % A element of the body of B
!,
trimmed((B:-Bs),NewB), % trim off empty bodies
ljand(G,[NewB|Vs2]).
ljand_imp(([]:-_),_,_):-!.
ljand_imp((D:-Cs),B,Vs):- !,ljand((D:-Cs),[(B:-[D])|Vs]).
ljand_imp(true,_B,_Vs):-!.
ljand_imp(A,_B,Vs):-memberchk(A,Vs).
fix_head(([H|Hs]:-Bs),HBss):-!,distribute_head([H|Hs],Bs,HBss).
fix_head(X,[X]).
%fix_heads(Xs,Ys):-maplist(fix_head,Xs,Yss),flatten_it(Yss,Ys).
fix_heads(Xs,Ys):-maplist(fix_head,Xs,Yss),append(Yss,Ys).
ljxprove(T0):-
%nl,ppp(T0),
expand_equiv(T0,T1),
%ppp(T1),
toAndHorn(T1,T),
%ppp(T),
ljx(T).
ljx(A):-ljx(A,[]).
%ljx(A,Vs):-ppp((Vs-->A)),fail. % just to trace steps
ljx(_,Vs):-memberchk(false,Vs),!.
ljx([],_):-!.
ljx([X|Xs],Vs):-!,ljx(X,Vs),ljx(Xs,Vs).
ljx(true,_):-!.
ljx(A,Vs):-memberchk(A,Vs),!.
%ljx((Bs:-As),Vs):-is_list(Bs),!,fix_head((Bs:-As),Xss),ljx(Xss,Vs).
ljx((B:-As),Vs1):-!,append(As,Vs1,Vs2),ljx(B,Vs2).
ljx(G,Vs0):- % atomic(G), G not on Vs1
fix_heads(Vs0,Vs0_),sort(Vs0_,Vs1),
select((B:-As),Vs1,Vs2),
select((B:-As),Vs1,Vs2),
select(A,As,Bs), % inner select loop
ljx_imp(A,B,Vs2), % A element of the body of B
!,
trimmed((B:-Bs),NewB), % trim off empty bodies
ljx(G,[NewB|Vs2]).
ljxs([],_).
ljxs([X|Xs],Vs):-ljx(X,Vs),ljxs(Xs,Vs).
ljx_imp(([]:-_),_,_):-!.
ljx_imp((D:-Cs),B,Vs):- !,ljx((D:-Cs),[(B:-[D])|Vs]).
ljx_imp(true,_B,_Vs):-!.
ljx_imp(A,_B,Vs):-memberchk(A,Vs).
selfix((H:-Bs),Vs1,Vs3):-
select((Hx:-Bs),Vs1,Vs2),
fix_head((Hx:-Bs),Xss),
select((H:-Bs),Xss,Yss),
append(Yss,Vs2,Vs3).
nhornprove(T0):-toNestedHorn(T0,T),
%ppp(T0),ppp(T),nl,
nhprs(T).
nhprs(A):-nhprs(A,[]),!.
%icljh(A,Vs):-ppp((Vs-->A)),fail. % just to trace steps
nhprs([],_):-!.
nhprs([C|Cs],Vs):-!,nhorn(C,Vs),nhprs(Cs,Vs).
nhprs(X,Vs):-nhorn(X,Vs).
% nested Horn + false + true - compiled from all except disjunction
nhorn(A):-nhorn(A,[]).
%nhorn(A,Vs):-ppp((Vs-->A)),fail. % just to trace steps
nhorn(true,_).
nhorn(A,Vs):-A\==true,member(A,Vs).
nhorn(G,Vs):- G\==false,member(false,Vs).
nhorn((B:-As),Vs1):-append(As,Vs1,Vs2),nhorn(B,Vs2).
nhorn(G,Vs1):- % primitive(G), % atomic(G), G not on Vs1
\+ \+ membtest(G,Vs1),
select((B:-As),Vs1,Vs2), % outer select loop
select(A,As,Bs), % inner select loop
nhorn_imp(A,B,Vs2), % A element of the body of B
trimmed((B:-Bs),NewB), % trim empty bodies
nhorn(G,[NewB|Vs2]).
%nhorn_imp(G,_B,_Vs):-ppp(nhorn_imp(G,B)),fail.
nhorn_imp(true,_B,_Vs).
nhorn_imp(A,_B,Vs):-A\==true,member(A,Vs).
nhorn_imp((D:-Cs),B,Vs):-nhorn((D:-Cs),[(B:-[D])|Vs]).
hbug:-nhornprove(
~ (0<->(0<-> ~0))
).
% N-Prolog's algorithm - no loop checking seems to pass our tests
npro(G):-
%ppp(G),
toHorn(G,H),npro(H,[],[]),!.
npro([],_,_):-!.
npro([X|Xs],Vs,Hs):-!,npro(X,Vs,Hs),npro(Xs,Vs,Hs).
npro((H:-Bs),Vs,Hs):-!,
append(Bs,Vs,NewVs),
npro(H,NewVs,Hs).
npro(G,Vs,_):-memberchk(G,Vs),!.
npro(G,Vs,Hs):- \+ memberchk(G,Hs),!, %ppp(G+Vs+Hs),
member((G:-Bs),Vs),
npro(Bs,Vs,[G|Hs]).
npro(G,Vs1,Hs):- % atomic(G), G not on Vs1
memberchk((G:-_),Vs1), % if not, we just fail
select((B:-As),Vs1,Vs2), % outer select loop
select(A,As,Bs), % inner select loop
ljh_imp_(A,B,Vs2,Hs), % A element of the body of B
!,
trimmed((B:-Bs),NewB), % trim off empty bodies
npro(G,[NewB|Vs2],Hs).
ljh_imp_((D:-Cs),B,Vs,Hs):- !,npro((D:-Cs),[(B:-[D])|Vs],Hs).
ljh_imp_(A,_B,Vs,_):-memberchk(A,Vs).
nproloop:-
G=(((0->1)->1)->1),
npro(G).
npromiss:-
G=((0->1)->((0->1)->0)->1),
npro(G).
% full prover, after Mints' transform
redprove(G):-
expand_full_neg(G,F),
mints(F,H,Bs),
ljfa(H,Bs).
% all but disj, after Mints' transformation to canonical form
fredprove(G):-
expand_full_neg(G,F),
mints(F,H,Bs),
ljfred(H,Bs).
%ljfred(G,Vs):-spy('ljf',G,Vs),fail.
ljfred(A,Vs):-memberchk(A,Vs),!.
ljfred(_,Vs):-memberchk(false,Vs),!.
ljfred((A->B),Vs):-!,ljfred(B,[A|Vs]).
ljfred(G,Vs1):- % atomic or false
%rmembtest(G,Vs1),
select((A->B),Vs1,Vs2),
ljfred_imp(A,B,Vs2,Vs3),
!,
ljfred(G,Vs3).
%ljfred_imp(CD,_,_,_):-ppp(imp:CD),fail.
ljfred_imp((C->D),B,Vs,[B|Vs]):-!,ljfred((C->D),[(D->B)|Vs]).
ljfred_imp(A,B,Vs,[B|Vs]):-memberchk(A,Vs). % atomic
rmembtest(G,Vs):-memberchk((_ -> G),Vs),!. % if not, we just fail
rmembtest(G,Vs):-memberchk((_ -> (_ ->G)),Vs),!. % if not, we just fail
rmembtest(_,Vs):-memberchk((_->false),Vs),!. % could still be infered from false
rmembtest(_,Vs):-memberchk((_ -> (_ ->false)),Vs),!. % if not, we just fail
% all but disj, after Mints' transformation to canonical form
gredprove(G):-
expand_full_neg(G,F),
mints(F,H,Bs),
ljgred(H,Bs).
%ljgred(G,Vs):-spy('ljf',G,Vs),fail.
ljgred(A,Vs):-memberchk(A,Vs),!.
ljgred(_,Vs):-memberchk(false,Vs),!.
ljgred((A->B),Vs):-!,ljgred(B,[A|Vs]).
ljgred(G,Vs1):- % atomic or false
rmembtest(G,Vs1),
prioritize(Red,A,B),
select(Red,Vs1,Vs2),
primitive(A),primitive(B),
% select((A->B),Vs1,Vs2),
ljgred_imp(Red,Vs2,Vs3),
!,
ljgred(G,Vs3).
prioritize(R,A,B):-
(
R=(A->B)
; R=(A->(_->B))
; R=((A->_)->B)
).
%ljgred_imp(CD,_,_,_):-ppp(imp:CD),fail.
ljgred_imp((C->D)->B,Vs,[B|Vs]):-!,ljgred((C->D),[(D->B)|Vs]).
ljgred_imp(A->B,Vs,[B|Vs]):-memberchk(A,Vs). % atomic
xfprove(T0):-xljf(T0),!.
xljf(T0):-
%ppp(T0),
expand_full_neg(T0,T),
%ppp(T),
xljf(T,[]).
%xljf(G,Vs):-ppp('xljf'(G,Vs)),fail.
xljf(_,Vs):-memberchk(false,Vs),!.
xljf(?A,Vs):-member(?A,Vs).
xljf(A<->B,Vs):-!,xljf(B,[A|Vs]),xljf(A,[B|Vs]).
xljf((A->B),Vs):-!,xljf(B,[A|Vs]).
xljf(A & B,Vs):-!,xljf(A,Vs),xljf(B,Vs).
xljf(A v B, Vs):-(xljf(A,Vs);xljf(B,Vs)).
xljf(G,Vs1):-is_head(G),
select(Red,Vs1,Vs2),
xljf_reduce(Red,G,Vs2,Vs3),
!,
xljf(G,Vs3).
is_head(? _).
is_head(false).
is_head(_ v _).
%xljf_reduce(AB,G,Vs,Vs):-compound(AB),ppp(reduce(G):(AB:-Vs)),fail.
xljf_reduce((A->B),_,Vs1,Vs2):-xljf_imp(A,B,Vs1,Vs2).
xljf_reduce((A & B),_,Vs,[A,B|Vs]).
xljf_reduce((A<->B),_,Vs,[(A->B),(B->A)|Vs]).
xljf_reduce((A v B),G,Vs,[B|Vs]):-xljf(G,[A|Vs]).
%xljf_imp(CD,_,_,_):-ppp(imp:CD),fail.
xljf_imp(?A,B,Vs,[B|Vs]):-memberchk(?A,Vs).
xljf_imp((C->D),B,Vs,[B|Vs]):-xljf((C->D),[(D->B)|Vs]).
xljf_imp((C & D),B,Vs,[(C->(D->B))|Vs]).
xljf_imp((C v D),B,Vs,[(C->B),(D->B)|Vs]).
xljf_imp((C<->D),B,Vs,[((C->D)->((D->C)->B))|Vs]).
% no bug anymore
xbug:-
%T=((?0<->(?0<-> ?0)->false)->false),
%T=(?0<->(?1<->(?0<->(?1<-> ?0)))),
T=(?0<->(?0<->(?1<-> ?2))->false),
xljf(T,[]).
% end
%xljf(?0,[?0,?1,?0,?1,?1,?0])
%xljf(?0,[?1,?0,?0,?1,?1,?0])
%xljf(?0,[?0,?0,?1,?1,?1,?0])
%xljf(?0,[?0,?1,?0,?1,?1,?0]) <---