Source file esubst.ml
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
open Util
type lift =
| ELID
(** [id] in substitution calculi; for all [Γ], [Γ ⊢ ELID : Γ] *)
| ELSHFT of lift * int
(** [↑^n ∘ σ] in substitution calculi (i.e. a shift of [n], then [σ]);
assuming [Γ ⊢ σ : Δ, Ξ] and [n = |Ξ|], then [Γ ⊢ ELSHFT (σ, n) : Δ] *)
| ELLFT of int * lift
(** [⇑^n(σ)] in substitution calculi (i.e. [σ] under [n] binders);
assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ ELLFT (n, σ) : Δ, Ξ] *)
let el_id = ELID
let rec eq_lift a b = match a, b with
| ELID, ELID -> true
| ELID, (ELSHFT _ | ELLFT _) -> false
| ELSHFT (a, i), ELSHFT (b, j) -> Int.equal i j && eq_lift a b
| ELSHFT _, (ELID | ELLFT _) -> false
| ELLFT (i, a), ELLFT (j, b) -> Int.equal i j && eq_lift a b
| ELLFT _, (ELID | ELSHFT _) -> false
let el_shft_rec n = function
| ELSHFT(el,k) -> ELSHFT(el,k+n)
| el -> ELSHFT(el,n)
let el_shft n el = if Int.equal n 0 then el else el_shft_rec n el
let el_liftn_rec n = function
| ELID -> ELID
| ELLFT(k,el) -> ELLFT(n+k, el)
| el -> ELLFT(n, el)
let el_liftn n el = if Int.equal n 0 then el else el_liftn_rec n el
let el_lift el = el_liftn_rec 1 el
let rec reloc_rel n = function
| ELID -> n
| ELLFT(k,el) ->
if n <= k then n else (reloc_rel (n-k) el) + k
| ELSHFT(el,k) -> (reloc_rel (n+k) el)
let rec is_lift_id = function
| ELID -> true
| ELSHFT(e,n) -> Int.equal n 0 && is_lift_id e
| ELLFT (_,e) -> is_lift_id e
type shf = int
let cmp n m = n + m
let idn = 0
type 'a or_var = Arg of 'a | Var of int
type 'a tree =
| Leaf of shf * 'a or_var
| Node of shf * 'a or_var * 'a tree * 'a tree * shf
type 'a subs = Nil of shf * int | Cons of int * 'a tree * 'a subs
let eval = function
| Leaf (w, _) -> w
| Node (w1, _, _, _, w2) -> cmp w1 w2
let leaf x = Leaf (idn, x)
let node x t1 t2 = Node (idn, x, t1, t2, cmp (eval t1) (eval t2))
let rec tree_get h w t i = match t with
| Leaf (w', x) ->
let w = cmp w w' in
if i = 0 then w, Inl x else assert false
| Node (w', x, t1, t2, _) ->
let w = cmp w w' in
if i = 0 then w, Inl x
else
let h = h / 2 in
if i <= h then tree_get h w t1 (i - 1)
else tree_get h (cmp w (eval t1)) t2 (i - h - 1)
let rec get w l i = match l with
| Nil (w', n) ->
let w = cmp w w' in
if i < n then w, Inl (Var (i + 1))
else n + w, Inr (i - n)
| Cons (h, t, rem) ->
if i < h then tree_get h w t i else get (cmp (eval t) w) rem (i - h)
let get l i = get idn l i
let tree_write w = function
| Leaf (w', x) -> Leaf (cmp w w', x)
| Node (w', x, t1, t2, wt) -> Node (cmp w w', x, t1, t2, wt)
let write w l = match l with
| Nil (w', n) -> Nil (cmp w w', n)
| Cons (h, t, rem) -> Cons (h, tree_write w t, rem)
let cons x l = match l with
| Cons (h1, t1, Cons (h2, t2, rem)) ->
if Int.equal h1 h2 then Cons (1 + h1 + h2, node x t1 t2, rem)
else Cons (1, leaf x, l)
| _ -> Cons (1, leaf x, l)
let expand_rel n s =
let k, v = get s (n - 1) in
match v with
| Inl (Arg v) -> Inl (k, v)
| Inl (Var i) -> Inr (k + i, None)
| Inr i -> Inr (k + i + 1, Some (i + 1))
let is_subs_id = function
| Nil (w, _) -> Int.equal w 0
| Cons (_, _, _) -> false
let subs_cons v s = cons (Arg v) s
let rec push_vars_until i k s =
if Int.equal k i then s
else push_vars_until i (pred k) (cons (Var k) s)
let subs_liftn n s =
if Int.equal n 0 then s
else match s with
| Nil (0, m) -> Nil (0, m + n)
| Nil _ | Cons _ ->
let s = write n s in
push_vars_until 0 n s
let subs_lift s = match s with
| Nil (0, m) -> Nil (0, m + 1)
| Nil _ | Cons _ ->
cons (Var 1) (write 1 s)
let subs_id n = Nil (0, n)
let subs_shft (n, s) = write n s
let rec tree_pop h n i rem s =
if Int.equal n 0 then i, Cons (h, s, rem)
else match s with
| Leaf _ ->
if Int.equal n 1 then
i, rem
else assert false
| Node (w, _, t1, t2, _) ->
let h = h lsr 1 in
let n = n - 1 in
let i = cmp w i in
if n - 1 >= h then
tree_pop h (n - h) (cmp (eval t1) i) rem t2
else
tree_pop h n i (Cons (h, t2, rem)) t1
let rec subs_pop_rec n i s =
if Int.equal n 0 then
i, s
else match s with
| Nil (w, m) ->
i + n, Nil (w, m - n)
| Cons (h, t, rem) ->
if n >= h then
subs_pop_rec (n - h) (cmp (eval t) i) rem
else
tree_pop h n i rem t
let subs_popn n e =
let k, e = subs_pop_rec n 0 e in
write k e
let subs_pop e =
let k, e = subs_pop_rec 1 0 e in
write k e
let rec pop n i e =
if Int.equal n 0 then i, e
else match e with
| ELID -> i, e
| ELLFT (k, e) ->
if k <= n then pop (n - k) i e
else i, ELLFT (k - n, e)
| ELSHFT (e, k) -> pop (n + k) (i + k) e
let apply_lift mk e = function
| Var i -> Var (reloc_rel i e)
| Arg v -> Arg (mk e v)
let rec lift_tree mk e = function
| Leaf (w, x) ->
let (n, e) = pop w 0 e in
Leaf (w + n, apply_lift mk e x), e
| Node (w, x, t1, t2, _) ->
let (n, e) = pop w 0 e in
let x = apply_lift mk e x in
let t1, e = lift_tree mk e t1 in
let t2, e = lift_tree mk e t2 in
Node (w + n, x, t1, t2, cmp (eval t1) (eval t2)), e
let rec lift_id e i n = match e with
| ELID -> Nil (i, n - i)
| ELSHFT (e, k) -> lift_id e (i + k) (n + k)
| ELLFT (k, e) ->
if k <= i then
write k (lift_id e (i - k) (n - k))
else if k <= n then
let s = lift_id e 0 (n - k) in
let s = write k s in
push_vars_until i k s
else assert false
let rec lift_subst mk e s = match s with
| Nil (w, m) ->
let (n, e) = pop w 0 e in
write (w + n) (lift_id e 0 m)
| Cons (h, t, rem) ->
let t, e = lift_tree mk e t in
let rem = lift_subst mk e rem in
Cons (h, t, rem)
let rec resize m = function
| Nil (w, _) ->
if m < 0 then assert false
else Nil (w, m)
| Cons (h, t, rem) ->
Cons (h, t, resize (m - h) rem)
let apply_subs mk lft s1 = function
| Arg v -> Arg (mk s1 v)
| Var i ->
begin match expand_rel i s1 with
| Inl (k, x) -> Arg (lft k x)
| Inr (i, _) -> Var i
end
let rec tree_comp mk lft s1 = function
| Leaf (w, x) ->
let n, s1 = subs_pop_rec w 0 s1 in
let x = apply_subs mk lft s1 x in
Leaf (n, x), s1
| Node (w, x, t1, t2, _) ->
let n, s1 = subs_pop_rec w 0 s1 in
let x = apply_subs mk lft s1 x in
let t1, s1 = tree_comp mk lft s1 t1 in
let t2, s1 = tree_comp mk lft s1 t2 in
Node (n, x, t1, t2, cmp (eval t1) (eval t2)), s1
let rec comp mk lft s1 = function
| Nil (w, m) ->
let n, s1 = subs_pop_rec w 0 s1 in
write n (resize m s1)
| Cons (h, t, rem) ->
let t, s1 = tree_comp mk lft s1 t in
let rem = comp mk lft s1 rem in
Cons (h, t, rem)
module Internal =
struct
(** More intuitive representation for weakenings
Instead of using ELSHFT (s ⟼ ↑^n ∘ s), uses WEAK (s ⟼ s ∘ ↑^n) *)
type weakening =
| ID
(** [id] in substitution calculi; for all [Γ], [Γ ⊢ ID : Γ] *)
| LIFT of int * weakening
(** [⇑^n(σ)] in substitution calculi (i.e. [σ] under [n] binders);
assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ LIFT (n, σ) : Δ, Ξ] *)
| WEAK of int * weakening
(** [σ ∘ ↑^n] in substitution calculi (i.e. [σ], then a shift of [n]);
assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ WEAK (n, σ) : Δ] *)
let weak n = function
| WEAK (k, w) -> WEAK (k+n, w)
| w -> WEAK (n, w)
(** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ weak n σ : Δ] *)
let weak n w = if Int.equal n 0 then w else weak n w
let lift n = function
| ID -> ID
| LIFT (k, w) -> LIFT (n+k, w)
| w -> LIFT (n, w)
(** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ lift n σ : Δ, Ξ] *)
let lift n w = if Int.equal n 0 then w else lift n w
let rec weakening_of_lift pending =
function
| ELID -> ID
| ELSHFT (el, k) -> weak k (weakening_of_lift (pending+k) el)
| ELLFT (k, el) ->
if k > pending then
lift (k - pending) (weakening_of_lift 0 el)
else
weakening_of_lift (pending - k) el
let weakening_of_lift el = weakening_of_lift 0 el
let rec weakening_to_lift =
function
| ID -> ELID
| LIFT (k, w) -> el_shft_rec k (weakening_to_lift w)
| WEAK (k, w) ->
el_shft_rec k (el_liftn_rec k (weakening_to_lift w))
[@@warning "-unused-value-declaration"]
let rec pp_weakening =
let open Pp in
function
| ID -> str "keep..]"
| LIFT (k, w) ->
str "keep " ++ int k ++ str ";" ++ spc () ++ pp_weakening w
| WEAK (k, w) ->
str "drop " ++ int k ++ str ";" ++ spc () ++ pp_weakening w
let pp_weakening w = Pp.(str "[" ++ pp_weakening w)
let pp_lift w = pp_weakening (weakening_of_lift w)
type 'a or_rel = REL of int | VAL of int * 'a
let to_rel shift = function
| Var i -> REL (i + shift)
| Arg v -> VAL (shift, v)
let rec get_tree_subst shift accu = function
| Leaf (w, x) ->
to_rel (shift + w) x :: accu
| Node (w, x, l, r, _) ->
let accu = get_tree_subst (shift + w + eval l) accu r in
let accu = get_tree_subst (shift + w) accu l in
to_rel (shift + w) x :: accu
let rec get_subst shift accu = function
| Nil (w, n) ->
List.init n (fun i -> REL (w + i + shift + 1))
| Cons (_, t, s) ->
let accu = get_subst (shift + eval t) accu s in
get_tree_subst shift accu t
let rec get_shift accu = function
| Nil (w, n) -> accu + w + n
| Cons (_, t, s) -> get_shift (eval t + accu) s
let repr (s : 'a subs) =
let shift = get_shift 0 s in
let subs = get_subst 0 [] s in
subs, shift
end