-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswipl_stt_judgements4.pl
370 lines (252 loc) · 7.58 KB
/
swipl_stt_judgements4.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
:- module(swipl_stt,[judgement/2, substitute/4, example/1]).
:- use_module(library(gensym)).
:- discontiguous judgement/2.
:- op(999, xfx, user:'~>'). % one step normalization
:- op(999, xfx, user:'~>>'). % full normalization
% CAPTURE-AVOIDING SUBSTITUTION
substitute(x(X), x(X), For, For) :- !.
substitute(x(X), x(Y), _, x(X)) :- !, X \= Y.
substitute([],_,_,[]) :- !.
substitute([X | Rest], x(V), For, [SubX | SubRest]) :-
!,
substitute(X, x(V), For, SubX),
substitute(Rest, x(V), For, SubRest).
substitute(bind(x(X),Expr),x(Y), For, bind(x(Fresh),ExprSub)) :-
!,
gensym(x,Fresh),
substitute(Expr, x(X), x(Fresh), ExprFresh),
substitute(ExprFresh, x(Y), For, ExprSub).
substitute(Term, x(X), For, TermSub) :-
Term =.. [F | Args],
substitute(Args, x(X), For, ArgsSub),
TermSub =.. [F | ArgsSub].
% HYPOTHESIS RULE
judgement(x(V):X, [x(V):X|_]).
judgement(x(V):X, [x(W):_|G]) :- V \= W, judgement(x(V):X,G).
% EMPTY / FALSE type ; nullary sum
% formation
judgement(type(empty),_).
% introduction
% -- no introduction rules!
% elimination
judgement(explosion(F):C, G) :-
judgement(F:empty, G),
judgement(type(C), G).
% beta?
% -- no beta rules because no introduction rules
% eta?
judgement(explosion(F) ~> F, G) :-
judgement(F:empty, G).
/*
* Notes:
* They use "abort_C" instead of "explosion" on ncatlab: https://ncatlab.org/nlab/show/empty+type
* but, why should the label for the eliminator vary depending on the type that it's eliminating into?
* We don't do this for any of the other types ex.. `unit_elim`, `if_then_else`, `case`, `apply`, ...
*
* It seems to be implying that we need a different abort_C for every type C to make sure that the object
* abort_C(F) / explosion_C(F) actually has type C, and since there are different types we can't have the
* same abort_C on the same object F for every C... but... if we have a proof of False then the system is
* inconsistent and all the types should reduce to each other anyway, i.e. explosion(F) has every type in
* any context where F:empty
*
* They also have a different eta rule, i.e. abort_C(F):C <--eta--> c:C for any c:C;
* this should be logically equivalent to my F <--eta--> explosion(F) though, by similar logic as above?
*
*
* Trying to do proof-search by running bidirectionally: it immediately loops on the eliminator here
* (funny enough that means its search strategy is to try to prove everything via proving the logic
* inconsistent...)
*
*/
% UNIT / TRUE / TOP / 1-member type ; nullary product)
% formation
judgement(type(unit),_).
% introduction
judgement(null:unit,_).
% elimination
judgement(unit_elim(U, X):C, G) :-
judgement(U:unit, G),
judgement(type(C), G),
judgement(X:C, G).
% beta
judgement(unit_elim(null, X) ~> X, _).
% eta
judgement(unit_elim(U,null) ~> U, G) :-
judgement(U:unit, G).
% BOOL / 2-member type
% formation
judgement(type(bool),_).
% introduction
judgement(true:bool,_).
judgement(false:bool,_).
% elimination
judgement(if_then_else(B, X, Y):C, G) :-
judgement(B:bool, G),
judgement(type(C), G),
judgement(X:C, G),
judgement(Y:C, G).
% beta
judgement(if_then_else(true, X, _) ~> X, _).
judgement(if_then_else(false, _, Y) ~> Y, _).
% eta
judgement(if_then_else(B, true, false) ~> B, G) :-
judgement(B:bool, G).
/*
* Notes:
* Can construct any finite enumeration type in similar fashion, but we can't construct
* the actual type *family* of finite enumerations, i.e. `Fin`, due to lack of dependent types.
*
*/
% PAIR / PRODUCT / CONJUNCTION / "AND" type
% formation
judgement(type(pair(A,B)), G) :-
judgement(type(A), G),
judgement(type(B), G).
% introduction
judgement((X,Y):pair(A,B), G) :-
judgement(type(pair(A,B)), G),
judgement(X:A, G),
judgement(Y:B, G).
% elimination
judgement(first(P):A, G) :-
judgement(P:pair(A,_), G).
judgement(second(P):B, G) :-
judgement(P:pair(_,B), G).
% beta
judgement(first((X,_)) ~> X, _).
judgement(second((_,Y)) ~> Y, _).
% eta
judgement((first(P), second(P)) ~> P, G) :-
judgement(P:pair(_,_), G).
% DISJOINT UNION / DISJUNCTION / "OR" TYPE
% formation
judgement(type(union(A, B)), G) :-
judgement(type(A), G),
judgement(type(B), G).
% introduction
judgement(left(X):union(A, B), G) :-
judgement(type(union(A, B)), G),
judgement(X:A, G).
judgement(right(Y):union(A,B), G) :-
judgement(type(union(A,B)), G),
judgement(Y:B, G).
% elimination
judgement(case(P, bind(x(V),L), bind(x(W),R)):C, G) :-
judgement(P:union(A,B), G),
judgement(type(C), G),
judgement(L:C, [x(V):A | G]),
judgement(R:C, [x(W):B | G]).
% beta
judgement(case(left(X), bind(x(V),L), _) ~> LSub, _) :-
substitute(L, x(V), X, LSub).
judgement(case(right(Y), _, bind(x(V),R)) ~> RSub, _) :-
substitute(R, x(V), Y, RSub).
% eta
judgement(case(C, bind(x(V),left(x(V))), bind(x(W),right(x(W)))) ~> C, G) :-
judgement(C:union(_,_), G).
% IMPLICATION / FUNCTION TYPE
% formation
judgement(type(function(A, B)), G) :-
judgement(type(A), G),
judgement(type(B), G).
% introduction
judgement(lambda(bind(x(V),Expr)):function(A,B), G) :-
judgement(type(function(A,B)), G),
judgement(Expr:B, [ x(V):A | G]).
% elimination
judgement(apply(F,X):B, G) :-
judgement(F:function(A,B), G),
judgement(X:A, G).
% beta
judgement(apply(lambda(bind(x(V), Expr)), X) ~> FX, _) :-
substitute(Expr, x(V), X, FX).
% eta
judgement(lambda(bind(x(V),apply(F,x(V)))) ~> F, G) :-
judgement(F:function(_,_),G).
% NATURAL NUMBERS
% formation
judgement(type(nat), _).
% introduction
judgement(0:nat, _).
judgement(suc(N):nat, G) :-
judgement(N:nat, G).
% elimination
judgement(nat_rec(N,Z,bind(x(V),S)):C, G) :-
judgement(N:nat, G),
judgement(type(C), G),
judgement(Z:C, G),
judgement(S:C, [x(V):nat|G]).
% beta
judgement(nat_rec(0, Z, _) ~> Z, _).
judgement(nat_rec(suc(N), _, bind(x(V),S)) ~> S_Sub, _) :-
substitute(S, x(V), N, S_Sub).
% eta
judgement(nat_rec(N, 0, bind(x(V),suc(x(V)))) ~> N, G) :-
judgement(N:nat, G).
% LIST TYPE
% formation
judgement(type(list(A)), G) :-
judgement(type(A), G).
% introduction
judgement([]:list(A), G) :-
judgement(type(list(A)), G).
judgement([X | Xs]:list(A), G) :-
judgement(type(list(A)), G),
judgement(X:A, G),
judgement(Xs:list(A), G).
% elimination
judgement(list_rec(L, Last, bind(x(V),bind(x(W),F))):C, G) :-
judgement(type(C), G),
judgement(L:list(_), G),
judgement(Last:C, G),
judgement(F:C, [x(V):A,x(W):list(A)|G]).
% beta
judgement(list_rec([], Nil, _) ~> Nil, _).
judgement(list_rec([X|Xs], _, bind(x(V),bind(x(W),Cons))) ~> Cons_Sub, _) :-
substitute(Cons, x(V), X, Cons_Sub1),
substitute(Cons_Sub1, x(W), Xs, Cons_Sub).
% eta
judgement(list_rec(L, [], bind(x(V),bind(x(W),[x(V)|x(W)]))) ~> L, G) :-
judgement(L:list(_), G).
/*
* Notes:
* Can make a Vector type for any N, just can't construct the type *family* itself due to
* lack of dependent types.
*
*/
% CONGRUENCE RULE
judgement(T1 ~> T2, G) :-
T1 =.. [F | Args_1],
cong(Args_1, Args_2, G),
T2 =.. [F | Args_2].
cong([Arg_1 | Args], [Arg_2 | Args], G) :-
judgement(Arg_1 ~> Arg_2, G).
cong([Arg | Args_1], [Arg | Args_2], G) :-
\+judgement(Arg ~> _, G),
cong(Args_1, Args_2, G).
% NORMALIZATION
judgement(T1 ~>> NF, G) :-
judgement(T1 ~> T2, G),
judgement(T2 ~>> NF, G).
judgement(NF ~>> NF, G) :-
\+judgement(NF ~> _, G).
/*
* Notes:
* The rules extend the one-step beta/eta rules to give an actual evaluation/reduction strategy.
* Different formulations of these rules will give variations on the evaluation strategy, ex.. we
* can get call-by-value, call-by-name, etc...
*
* I haven't thought deeply on this yet.
*
*/
example(X) :-
judgement(
apply(
apply(
lambda(bind(x(1), lambda(bind(x(2), x(1))))),
"hi"
),
"bye"
) ~>> X,
[]
).