-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeoryToolbox2.pl
366 lines (298 loc) · 12.4 KB
/
theoryToolbox2.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
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% INFO
% © Jean-Christophe Rohner 2019
% This is experimental software. Use at your own risk.
% See the LICENSE file for more information.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% SETUP
:- use_module(library(clpr)).
:- op(1200, xfx, ⇐).
:- op(1000, xfy, ∧).
:- op(1100, xfy, ∨).
:- op(900, fy, ¬).
term_expansion(A ⇐ B, A:- B).
goal_expansion(A ∧ B, (A, B)).
goal_expansion(A ∨ B, (A; B)).
goal_expansion(¬A, \+A).
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% PROVABLE
provable(G, I, G):- provable0(G, I).
provable0(true, _):- !.
provable0((G1, G2), I):- !, provable0(G1, I), provable0(G2, I).
provable0((G1; G2), I):- !, (provable0(G1, I); provable0(G2, I)).
provable0(G, I):- G = \+(G0), !, \+provable0(G0, I).
provable0(G, _):- predicate_property(G, imported_from(_)), !, call(G).
provable0(G, I):- copy_term(I, I1), member(G, I1).
provable0(H, I):- clause(H, Body), provable0(Body, I).
showProvable(G):- formatOutputTerm(G, G1), writeln(G1).
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% PROVE
prove(true, _, true):- !.
prove((G1, G2), I, (P1, P2)):- !, prove(G1, I, P1), prove(G2, I, P2).
prove((G1; G2), I, (P1; P2)):- !, (prove(G1, I, P1); prove(G2, I, P2)).
prove(G, I, P):- G = \+(G0), !, \+prove(G0, I, _), P = subproof(¬G0, true).
prove(G, _, P):- predicate_property(G, imported_from(_)), !, call(G), P = subproof(G, true).
prove(G, I, P):- copy_term(I, I1), member(G, I1), P = subproof(G, true).
prove(H, I, subproof(H, Subproof)):- clause(H, Body), prove(Body, I, Subproof).
showProof0(X, SUB, monochrome):- X = subproof(G, P), P \= true, writeNTabs(SUB), format('~w~w~n', [G, ' ⇐']), SUB1 is SUB + 1, showProof0(P, SUB1, monochrome).
showProof0(X, SUB, color):- X = subproof(G, P), P \= true, writeNTabs(SUB), color(SUB, C), ansi_format([C], '~w~w~n', [G, ' ⇐']), SUB1 is SUB + 1, showProof0(P, SUB1, color).
showProof0(X, SUB, lanes):- X = subproof(G, P), P \= true, writeNTabs(SUB, lanes), format('~w~w~n', [G, ' ⇐']), SUB1 is SUB + 1, showProof0(P, SUB1, lanes).
showProof0(X, SUB, O):- X = ','(A, B), A \= true, B \= true, showProof0(A, SUB, O), showProof0(B, SUB, O).
showProof0(X, SUB, O):- X = ';'(A, _), A \= true, showProof0(A, SUB, O).
showProof0(X, SUB, O):- X = ';'(_, B), B \= true, showProof0(B, SUB, O).
showProof0(X, SUB, monochrome):- X = subproof(G, true), writeNTabs(SUB), format('~w~w~n', [G, ' ⇐ true']).
showProof0(X, SUB, color):- X = subproof(G, true), writeNTabs(SUB), color(SUB, C), ansi_format([C], '~w~w~n', [G, ' ⇐ true']).
showProof0(X, SUB, lanes):- X = subproof(G, true), writeNTabs(SUB, lanes), format('~w~w~n', [G, ' ⇐ true']).
showProof(P, O):- formatOutputTerm(P, P1), nl, writeln('PROOF'), nl, showProof0(P1, 0, O).
color(0, fg(255, 196, 126)).
color(1, fg(255, 250, 127)).
color(2, fg(164, 255, 157)).
color(3, fg(152, 245, 255)).
color(4, fg(213, 172, 255)).
color(5, fg(255, 156, 153)).
color(6, fg(255, 196, 126)).
color(7, fg(255, 250, 127)).
color(8, fg(164, 255, 157)).
color(9, fg(152, 245, 255)).
color(10, fg(213, 172, 255)).
color(11, fg(255, 156, 153)).
color(12, fg(255, 196, 126)).
color(13, fg(255, 250, 127)).
color(14, fg(164, 255, 157)).
color(15, fg(152, 245, 255)).
color(16, fg(213, 172, 255)).
color(17, fg(255, 156, 153)).
color(X, fg(255, 196, 126)):- X > 17.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% MAXVALUE
maxValue(Y, G, I, R):-
findall(
Y,
provable0(G, I),
L
),
max_list(L, MAXY),
Y = MAXY,
prove(G, I, P),
R = [P].
showMaxValue(R, O):-
[P] = R,
formatOutputTerm(P, P1),
nl,
writeln('MAX VALUE (PROOF)'), nl,
showProof0(P1, 0, O), nl.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% MINVALUE
minValue(Y, G, I, R):-
findall(
Y,
provable0(G, I),
L
),
min_list(L, MINY),
Y = MINY,
prove(G, I, P),
R = [P].
showMinValue(R, O):-
[P] = R,
formatOutputTerm(P, P1),
nl,
writeln('MIN VALUE (PROOF)'), nl,
showProof0(P1, 0, O), nl.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% INCOHERENCE
incoherence(G1, G2, I, T, X1, X2, R):-
provable0(G1, I),
provable0(G2, I),
{abs(X1 - X2) > T},
R = [I, G1, G2, T],
!.
showIncoherence(R, O):-
[I, G1, G2, T] = R,
nl,
writeln('INCOHERENCE'), nl,
formatOutputTerm(I, I1),
writeln('Given inputs:'), maplist(writeln, I1), nl,
write('These goals are incoherent at the threshold '), write(T), writeln('.'), nl,
writeln('PROOFS'), nl,
prove(G1, I, P1),
prove(G2, I, P2),
formatOutputTerm(P1, P12),
formatOutputTerm(P2, P22),
showProof0(P12, 0, O),
nl,
showProof0(P22, 0, O),
nl.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% FALSIFIABILITY
falsifiability(G, I, R):-
findall(G, provable0(G, I), L),
sort(L, L0),
length(L0, N),
R = [G, I, N].
showFalsifiability(R):-
[G, I, N] = R,
nl,
formatOutputTerm(G, G1),
formatOutputTerm(I, I1),
findall(G, provable0(G, I), L),
formatOutputTerm(L, L1),
writeln('FALSIFIABILITY'), nl,
writeln('Given inputs:'), maplist(writeln, I1), nl,
write('There are '), write(N), write(' predictions for the goal '), write(G1), write('.'), nl, nl,
writeln('These are: '), maplist(writeln, L1), nl.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% SUBSUMES
subsumes(SUPER, SUB, G, I, R):-
append(I, [SUPER], ISUPER),
append(I, [SUB], ISUB),
findall(G, provable0(G, ISUPER), LSUPER),
findall(G, provable0(G, ISUB), LSUB),
sort(LSUPER, LSUPER1),
sort(LSUB, LSUB1),
subset(LSUB1, LSUPER1),
subtract(LSUPER1, LSUB1, EXTRA),
R = [SUPER, SUB, G, EXTRA].
showSubsumes(R):-
[SUPER, SUB, G, EXTRA] = R,
formatOutputTerm(G, G1),
formatOutputTerm(EXTRA, EXTRA1),
nl,
writeln('SUBSUMES'), nl,
write('Given the goal '), write(G1), write(', '), write(SUPER), write(' subsumes '), write(SUB), nl, nl,
write('There are '), length(EXTRA1, N), write(N), write(' predictions in '), write(SUPER), write(' that are not in '), write(SUB), write(', namely:'), nl,
maplist(writeln, EXTRA1), nl.
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% ALL CLAUSES
allClauses(G, I, R):-
findall(
c(G, A),
(
clause(G, A),
provable0(A, I)
),
R
).
showAllClauses(R):-
formatOutputTerm(R, R1),
maplist(clauseToFolSyntax, R1, R2),
nl, writeln('ALL CLAUSES'), nl,
maplist(writeln, R2).
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% GENERATE RANDOM OBJECTS
generateRandomObjects(NF, NS, PN, R):-
randset(NS, NF, L),
findall(
X,
(
member(X1, L),
atomic_list_concat([PN, '(', X1, ')'], X)
),
L1
),
maplist(term_string, R, L1).
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% SAMPLE CLAUSES
sampleClauses(G, I, N, SP, R):-
SP = [],
findall(
c(G, A),
(
clause(G, A),
provable0(A, I)
),
L
),
length(L, NT),
randomSampleFromList(N, L, S),
R = [NT, S].
sampleClauses(G, I, N, SP, R):-
SP \= [],
maplist(getGroundings(I), SP, GSP),
getStrataDefinitions(GSP, SD),
maplist(makeStratum(G, I), SD, STRA),
maplist(length, STRA, NSSTRA),
sum_list(NSSTRA, NCLA),
maplist(randomSampleFromList(N), STRA, SSTRA),
flatten(SSTRA, SSTRA2),
R = [NCLA, SD, SSTRA2].
showSampleClauses(R):-
R = [NT, R1],
maplist(formatOutputTerm, R1, R2),
maplist(clauseToFolSyntax, R2, R3),
nl, writeln('SAMPLE CLAUSES'), nl,
write('The total number of clauses is '), write(NT), nl, nl,
write('Simple random sample of clauses:'), nl,
maplist(writeln, R3).
showSampleClauses(R):-
R = [NCLA, SD, SSTRA],
nl, writeln('SAMPLE CLAUSES'), nl,
write('The total number of clauses is '), writeln(NCLA), nl,
writeln('Strata definitions:'),
maplist(writeln, SD), nl,
maplist(formatOutputTerm, SSTRA, SSTRA1),
maplist(clauseToFolSyntax, SSTRA1, SSTRA2),
writeln('Stratified random sample of clauses:'),
maplist(writeln, SSTRA2).
getGroundings(I, G, L):-
findall(G, provable0(G, I), L).
getStrataDefinitions(GSP, SD):-
GSP = [L],
findall([X], member(X, L), SD).
getStrataDefinitions(GSP, SD):-
GSP = [L1, L2],
findall([X1, X2], (member(X1, L1), member(X2, L2)), SD).
getStrataDefinitions(GSP, SD):-
GSP = [L1, L2, L3],
findall([X1, X2, X3], (member(X1, L1), member(X2, L2), member(X3, L3)), SD).
getStrataDefinitions(GSP, SD):-
GSP = [L1, L2, L3, L4],
findall([X1, X2, X3, X4], (member(X1, L1), member(X2, L2), member(X3, L3), member(X4, L4)), SD).
makeStratum(G, I, SD, STRM):-
SD = [STR],
findall(c(G, A), (clause(G, A), provable0(A, I), inBody(STR, A)), STRM).
makeStratum(G, I, SD, STRM):-
SD = [STR1, STR2],
findall(c(G, A), (clause(G, A), provable0(A, I), inBody(STR1, A), inBody(STR2, A)), STRM).
makeStratum(G, I, SD, STRM):-
SD = [STR1, STR2, STR3],
findall(c(G, A), (clause(G, A), provable0(A, I), inBody(STR1, A), inBody(STR2, A), inBody(STR3, A)), STRM).
makeStratum(G, I, SD, STRM):-
SD = [STR1, STR2, STR3, STR4],
findall(c(G, A), (clause(G, A), provable0(A, I), inBody(STR1, A), inBody(STR2, A), inBody(STR3, A), inBody(STR4, A)),STRM).
% -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% UTILITY PREDICATES
writeNTabs(N):- findall(' ', between(1, N, _), L), maplist(write, L).
writeNTabs(N, lanes):- findall(' |', between(1, N, _), L), maplist(writeLane, L).
writeLane(X):- ansi_format(fg(100, 100, 100), '~w', [X]).
formatOutputTerm(I, O):- I =.. L, maplist(parse_arg, L, L1), I2 =.. L1, copy_term_nat(I2, O), numbervars(O, 0, _).
parse_arg(I, O):- float(I), format(atom(O), '~3f', [I]).
parse_arg(I, O):- integer(I), O = I.
parse_arg(I, O):- \+number(I), \+compound(I), O = I.
parse_arg(I, O):- compound(I), I =.. L, maplist(parse_arg, L, L1), O =.. L1.
inBody(X, B):- B = ','(B1, B2), (inBody(X, B1); inBody(X, B2)).
inBody(X, B):- B = ';'(B1, B2), (inBody(X, B1); inBody(X, B2)).
inBody(X, B):- X = B.
inBody(X, B):- \+(X) = B.
clauseToFolSyntax(I, O):-
I = c(C, A),
antecedentToFolSyntax(A, A1),
O1 = (C ⇐ A1),
termToString(O1, O2),
stringReplace('∧', ' ∧ ', O2, O3),
stringReplace('∨', ' ∨ ', O3, O4),
stringReplace('⇐', ' ⇐ ', O4, O).
antecedentToFolSyntax(I, O):- I = ','(I1, I2), antecedentToFolSyntax(I1, O1), antecedentToFolSyntax(I2, O2), O = ∧(O1, O2).
antecedentToFolSyntax(I, O):- I = ';'(I1, I2), antecedentToFolSyntax(I1, O1), antecedentToFolSyntax(I2, O2), O = ∨(O1, O2).
antecedentToFolSyntax(I, O):- I = \+(I1), O = ¬(I1).
antecedentToFolSyntax(I, O):- I \= ','(_, _), I \= ';'(_, _), I \= \+(_), O = I.
termToString(I, O):-
term_string(I, O, [numbervars(true)]).
stringReplace(IS, OS, I, O) :-
atomic_list_concat(L, IS, I),
atomic_list_concat(L, OS, O).
randomSampleFromList(N, I, O):-
length(I, NI),
randset(N, NI, RS),
maplist(pickNthFromList(I), RS, O).
pickNthFromList(L, N, O):- nth1(N, L, O).