-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauxiliaries.pl
More file actions
387 lines (337 loc) · 9.06 KB
/
Copy pathauxiliaries.pl
File metadata and controls
387 lines (337 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
:- module(auxiliaries, [select_formula/4,
is_ll1_formula/1,
is_ll1_formula/2,
count_check/4,
subproofs/2,
rulename/2,
is_axiom/1,
merge_fvs/3,
universal_closure/2,
universal_disclosure/2,
free_vars_n/2,
free_vars_p/2,
free_vars/2,
antecedent/2,
non_member/2,
identical_prefix/3,
identical_postfix/3,
identical_lists/2,
split_list/4]).
:- use_module(ordset, [ord_union/3, ord_delete/3, ord_insert/3]).
% = is_ll1_formula(+Term)
%
% true if Term is a correct first-order linear logic formula; outputs error message and fails if it is not
% proposition
is_ll1_formula(at(_)) :-
!.
% predicate
is_ll1_formula(at(_, _)) :-
!.
is_ll1_formula(forall(_,A)) :-
!,
is_ll1_formula(A).
is_ll1_formula(exists(_,A)) :-
!,
is_ll1_formula(A).
is_ll1_formula(impl(A,B)) :-
!,
is_ll1_formula(A),
is_ll1_formula(B).
is_ll1_formula(p(A,B)) :-
!,
is_ll1_formula(A),
is_ll1_formula(B).
is_ll1_formula(Term) :-
functor(Term, F, A),
format(user_error, '{Error: unknown term ~w/~w used in first-order linear logic entry!}', [F,A]),
fail.
% = is_ll1_formula(+Term, +Word)
%
% true if Term is a correct first-order linear logic formula; outputs error message and fails if it is not
% proposition
is_ll1_formula(at(_), _) :-
!.
% predicate
is_ll1_formula(at(_, _), _) :-
!.
is_ll1_formula(forall(_,A), W) :-
!,
is_ll1_formula(A, W).
is_ll1_formula(exists(_,A), W) :-
!,
is_ll1_formula(A, W).
is_ll1_formula(impl(A,B), W) :-
!,
is_ll1_formula(A, W),
is_ll1_formula(B, W).
is_ll1_formula(p(A,B), W) :-
!,
is_ll1_formula(A, W),
is_ll1_formula(B, W).
is_ll1_formula(Term, W) :-
functor(Term, F, A),
format(user_error, '{Error: unknown term ~w/~w used in first-order linear logic entry for "~w"!}', [F,A,W]),
fail.
% = select_formula(+Formula, +Index, +List, -Rest)
%
% selects Index-Formula pair from list, using forced-choice
% determinism
select_formula(F, N, L0, L) :-
select(N-F, L0, L),
!.
count_check(LN, LP, AtName, Rest) :-
Count is LP - LN,
(
Count =:= 0
->
true
;
format(user_error, '~NCount check failure: count(~w)= ~w', [AtName, Count]),
print_count_offenders(Rest),
fail
).
print_count_offenders([]) :-
nl(user_error).
print_count_offenders([AtName-atoms(Pos, Neg)|Rest]) :-
length(Pos, LP),
length(Neg, LN),
Count is LP - LN,
(
Count =:= 0
->
true
;
format(user_error, ', count(~w)= ~w', [AtName, Count])
),
print_count_offenders(Rest).
% = print_diff_lists(+List1, +List2)
%
% supposing List1 and List2 have the same length, we print the members of the two lists
% side-by-side, marking by * the elements which differ between the two lists
print_diff_lists([], []).
print_diff_lists([A|As], [B|Bs]) :-
copy_term(A, AA),
copy_term(B, BB),
numbervars(AA, 0, _),
numbervars(BB, 0, _),
( A == B -> C = ' ' ; C = '*' ),
format(user_error, '~w~t~w~50|~t~w~100|~w~n', [C,AA,BB,C]),
print_diff_lists(As, Bs).
% = sub_proofs(+Proof, ?SubProofList)
%
% true if SubProofList is the list of premisses of the current rule
subproofs(_-R, S) :-
subproofs(R, S).
subproofs(rule(_,_,_,S), S).
% = rule(+Proof, ?RuleName)
%
% true if RuleName is the name of the current rule
rulename(_-R, N) :-
rulename(R, N).
rulename(rule(N,_,_,_), N).
% = antecedent(+Proof, ?Antecedent)
%
% true if Antecedent is the antecedent of the conclusion of the
% given Proof.
antecedent(_-R, Ant) :-
antecedent(R, Ant).
antecedent(rule(_,Ant,_,_), Ant).
% = is_axiom(+Proof)
%
% true if Proof is an axiom rule
is_axiom(_-R) :-
is_axiom(R).
is_axiom(rule(ax,_,_,_)).
% merge two sets of free variables
% remove variables already instantiated (these have an integer value)
% we need to sort again (since variable instantiations/unifications may
% have changed term order)
merge_fvs(Vs0, Ws0, Zs) :-
reduce_fvs(Vs0, Vs1),
sort(Vs1, Vs),
reduce_fvs(Ws0, Ws1),
sort(Ws1, Ws),
ord_union(Vs, Ws, Zs).
reduce_fvs([], []).
reduce_fvs([V|Vs], Ws) :-
(
integer(V)
->
reduce_fvs(Vs, Ws)
;
Ws = [V|Ws0],
reduce_fvs(Vs, Ws0)
).
% = universal_closure(+Formula, +ClosedFormula)
universal_closure(Formula, ClosedFormula) :-
free_vars(Formula, FreeVars),
universal_closure(FreeVars, Formula, ClosedFormula).
universal_closure([], Formula, Formula).
universal_closure([X|Xs], Formula0, Formula) :-
(
var(X)
->
universal_closure(Xs, forall(X,Formula0), Formula)
;
universal_closure(Xs, Formula0, Formula)
).
% =
universal_disclosure(forall(_,A0), A) :-
!,
universal_disclosure(A0, A).
universal_disclosure(A, A).
% = free_vars_n(+Formula, -SetOfFreeVars)
%
% true if Formula (of negative polariy) has
% SetOfFreeVars, but with a slight twist: all
% variables bound by a tensor prefix are
% considered free. For example, a prefix of
% universal quantifiers is removed (and, in
% general, any negative forall/impl and
% postive exists/prod); this is the implicit
% tensor contraction rule).
free_vars_n(_-A, Vars) :-
free_vars_n(A, Vars).
free_vars_n(at(_, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars_n(at(_, _, _, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars_n(p(A,B), Vars) :-
free_vars(A, Vars1),
free_vars(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars_n(impl(A,B), Vars) :-
free_vars_p(A, Vars1),
free_vars_n(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars_n(forall(_,A), Vars) :-
free_vars_n(A, Vars).
free_vars_n(exists(X,A), Vars) :-
free_vars(A, Vars0),
ord_delete(Vars0, X, Vars).
is_variable(X) :-
(
var(X)
->
true
;
X = var(_)
->
true
;
X = '$VAR'(_)
->
true
).
free_vars_list(Vs, Ws) :-
free_vars_list(Vs, [], Ws).
free_vars_list([], Ws, Ws).
free_vars_list([V|Vs], Ws0, Ws) :-
(
is_variable(V)
->
ord_insert(Ws0, V, Ws2)
;
atomic(V)
->
Ws2 = Ws0
;
compound(V)
->
V =..[_|As],
free_vars_list(As, [], Ws1),
ord_union(Ws0, Ws1, Ws2)
),
free_vars_list(Vs, Ws2, Ws).
% = free_vars_p(+Formula, -SetOfFreeVars)
%
% true if Formula (of positive polariy) has
% SetOfFreeVars, but with a slight twist: all
% variables bound by a tensor prefix are
% considered free (this is the implicit tensor
% contraction rule).
free_vars_p(_-A, Vars) :-
free_vars_p(A, Vars).
free_vars_p(at(_, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars_p(at(_, _, _, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars_p(p(A,B), Vars) :-
free_vars_p(A, Vars1),
free_vars_p(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars_p(impl(A,B), Vars) :-
free_vars(A, Vars1),
free_vars(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars_p(exists(_,A), Vars) :-
free_vars_p(A, Vars).
free_vars_p(forall(X,A), Vars) :-
free_vars(A, Vars0),
ord_delete(Vars0, X, Vars).
% = free_vars(+Formula, -SetOfFreeVars)
%
% true if Formula has SetOfFreeVars under the
% standard interpretation of free/bound.
free_vars(_-A, Vars) :-
free_vars(A, Vars).
free_vars(at(_, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars(at(_, _, _, Vars0), Vars) :-
free_vars_list(Vars0, Vars).
free_vars(p(A,B), Vars) :-
free_vars(A, Vars1),
free_vars(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars(impl(A,B), Vars) :-
free_vars(A, Vars1),
free_vars(B, Vars2),
ord_union(Vars1, Vars2, Vars).
free_vars(exists(X,A), Vars) :-
free_vars(A, Vars0),
ord_delete(Vars0, X, Vars).
free_vars(forall(X,A), Vars) :-
free_vars(A, Vars0),
ord_delete(Vars0, X, Vars).
% = non_member(+Element, +List)
%
% true if Element is not a member of List (that is if it is not strictly
% equal to one of the members of List; member_chk(Element, List) must fail).
non_member(_, []).
non_member(X, [Y|Ys]) :-
X \== Y,
non_member(X, Ys).
% = identical_prefix(+Prefix, -PostFix, +List)
%
% true if Prefix is a prefix of List using strict identity instead
% of unifiability; returns the PostFix
identical_prefix([], Ys, Ys).
identical_prefix([X|Xs], Zs, [Y|Ys]) :-
X == Y,
identical_prefix(Xs, Zs, Ys).
% = identical_postfix(-Prefix, +PostFix, +List)
%
% true if Postfix is a postfix of List using strict identity instead
% of unifiability; returns the Prefix
identical_postfix(Xs, Ys, Zs) :-
length(Ys, N),
length(PostFix, N),
append(Xs, PostFix, Zs),
identical_lists(PostFix, Ys).
% = identical_lists(+List1, +List2)
%
% true if List1 and List2 are strictly identical (essentially List1 == List2,
% but checks that both arguments are proper lists)
identical_lists([], []).
identical_lists([X|Xs], [Y|Ys]) :-
X == Y,
identical_lists(Xs, Ys).
% = split_list(+List, +Element, -Before, -After)
%
% as select/3, but returns two lists, one with the prefix of List before Element
% and one with the suffix of List after Element
% in other words, append(Before, [Element|After], List) is true
split_list([A|As], A, [], As) :-
!.
split_list([A|As0], C, [A|As], Bs) :-
split_list(As0, C, As, Bs).