Debian Codegraph @

kernel/esubst.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Bruno Barras for Coq V7.0, Mar 2001 *)
12
13 (* Support for explicit substitutions *)
14
15 open Util
16
17 (*********************)
18 (* Lifting *)
19 (*********************)
20
21 (* Explicit lifts and basic operations *)
22 (* Invariant to preserve in this module: no lift contains an [ELLFT] of [ELID],
23 two consecutive [ELLFT] or two consecutive [ELSHFT]. *)
24
25 (* Terminology comes from substitution calculi (see e.g. Hardin et al.).
26 That is, what is called a lift in Rocq is made of what is called in
27 substitution calculi a shift (the shift to add) and of what is
28 called a lift (the threshold above which to apply the shift), which
29 can be iterated as represented in the type [lift] *)
30 type lift =
31 | ELID
32 (** [id] in substitution calculi; for all [Γ], [Γ ⊢ ELID : Γ] *)
33
34 | ELSHFT of lift * int
35 (** [↑^n ∘ σ] in substitution calculi (i.e. a shift of [n], then [σ]);
36 assuming [Γ ⊢ σ : Δ, Ξ] and [n = |Ξ|], then [Γ ⊢ ELSHFT (σ, n) : Δ] *)
37
38 | ELLFT of int * lift
39 (** [⇑^n(σ)] in substitution calculi (i.e. [σ] under [n] binders);
40 assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ ELLFT (n, σ) : Δ, Ξ] *)
41
42 let el_id = ELID
43
44 let rec eq_lift a b = match a, b with
45 | ELID, ELID -> true
46 | ELID, (ELSHFT _ | ELLFT _) -> false
47 | ELSHFT (a, i), ELSHFT (b, j) -> Int.equal i j && eq_lift a b
48 | ELSHFT _, (ELID | ELLFT _) -> false
49 | ELLFT (i, a), ELLFT (j, b) -> Int.equal i j && eq_lift a b
50 | ELLFT _, (ELID | ELSHFT _) -> false
51
52 (* compose a relocation of magnitude n *)
53 let el_shft_rec n = function
54 | ELSHFT(el,k) -> ELSHFT(el,k+n)
55 | el -> ELSHFT(el,n)
56 let el_shft n el = if Int.equal n 0 then el else el_shft_rec n el
57
58 (* cross n binders *)
59 let el_liftn_rec n = function
60 | ELID -> ELID
61 | ELLFT(k,el) -> ELLFT(n+k, el)
62 | el -> ELLFT(n, el)
63 let el_liftn n el = if Int.equal n 0 then el else el_liftn_rec n el
64
65 let el_lift el = el_liftn_rec 1 el
66
67 (* relocation of de Bruijn n in an explicit lift *)
68 let rec reloc_rel n = function
69 | ELID -> n
70 | ELLFT(k,el) ->
71 if n <= k then n else (reloc_rel (n-k) el) + k
72 | ELSHFT(el,k) -> (reloc_rel (n+k) el)
73
74 let rec is_lift_id = function
75 | ELID -> true
76 | ELSHFT(e,n) -> Int.equal n 0 && is_lift_id e
77 | ELLFT (_,e) -> is_lift_id e
78
79 (*********************)
80 (* Substitutions *)
81 (*********************)
82
83 (* Variant of skewed lists enriched w.r.t. a monoid. See the Range module.
84
85 In addition to the indexed data, every node contains a monoid element, in our
86 case, integers. It corresponds to the number of partial shifts to apply when
87 reaching this subtree. The total shift is obtained by summing all the partial
88 shifts encountered in the tree traversal. For efficiency, we also cache the
89 sum of partial shifts of the whole subtree as the last argument of the [Node]
90 constructor.
91
92 A more intuitive but inefficient representation of this data structure would
93 be a list of terms interspeded with shifts, as in
94
95 type 'a subst = NIL | CONS of 'a or_var * 'a subst | SHIFT of 'a subst
96
97 On this inefficient representation, the typing rules would be:
98
99 · ⊢ NIL : ·
100 Γ ⊢ σ : Δ and Γ ⊢ t : A{σ} implies Γ ⊢ CONS (t, σ) : Δ, A
101 Γ ⊢ σ : Δ implies Γ, A ⊢ SHIFT σ : Δ
102
103 The efficient representation is isomorphic to this naive variant, except that
104 shifts are grouped together, and we use skewed lists instead of lists.
105
106 *)
107
108 type shf = int
109 let cmp n m = n + m
110 let idn = 0
111
112 type 'a or_var = Arg of 'a | Var of int
113
114 type 'a tree =
115 | Leaf of shf * 'a or_var
116 | Node of shf * 'a or_var * 'a tree * 'a tree * shf
117 (*
118 Invariants:
119 - All trees are complete.
120 - Define get_shift inductively as [get_shift (Leaf (w, _)) := w] and
121 [get_shift (Node (w, _, t1, t2, _)) := w + t1 + t2] then for every tree
122 of the form Node (_, _, t1, t2, sub), we must have
123 sub = get_shift t1 + get_shift t2.
124
125 In the naive semantics:
126
127 Leaf (w, x) := SHIFT^w (CONS (x, NIL))
128 Node (w, x, t1, t2, _) := SHIFT^w (CONS (x, t1 @ t2))
129
130 *)
131
132 type 'a subs = Nil of shf * int | Cons of int * 'a tree * 'a subs
133 (*
134 In the naive semantics mentioned above, we have the following.
135
136 Nil (w, n) stands for SHIFT^w (ID n) where ID n is a compact form of identity
137 substitution, defined inductively as
138
139 ID 0 := NIL
140 ID (S n) := CONS (Var 1, SHIFT (ID n))
141
142 Cons (h, t, s) stands for (t @ s) and h is the total number of values in the
143 tree t. In particular, it is always of the form 2^n - 1 for some n.
144 *)
145
146 (* Returns the number of shifts contained in the whole tree. *)
147 let eval = function
148 | Leaf (w, _) -> w
149 | Node (w1, _, _, _, w2) -> cmp w1 w2
150
151 let leaf x = Leaf (idn, x)
152 let node x t1 t2 = Node (idn, x, t1, t2, cmp (eval t1) (eval t2))
153
154 let rec tree_get h w t i = match t with
155 | Leaf (w', x) ->
156 let w = cmp w w' in
157 if i = 0 then w, Inl x else assert false
158 | Node (w', x, t1, t2, _) ->
159 let w = cmp w w' in
160 if i = 0 then w, Inl x
161 else
162 let h = h / 2 in
163 if i <= h then tree_get h w t1 (i - 1)
164 else tree_get h (cmp w (eval t1)) t2 (i - h - 1)
165
166 let rec get w l i = match l with
167 | Nil (w', n) ->
168 let w = cmp w w' in
169 if i < n then w, Inl (Var (i + 1))
170 else n + w, Inr (i - n) (* FIXME: double check *)
171 | Cons (h, t, rem) ->
172 if i < h then tree_get h w t i else get (cmp (eval t) w) rem (i - h)
173
174 let get l i = get idn l i
175
176 let tree_write w = function
177 | Leaf (w', x) -> Leaf (cmp w w', x)
178 | Node (w', x, t1, t2, wt) -> Node (cmp w w', x, t1, t2, wt)
179
180 let write w l = match l with
181 | Nil (w', n) -> Nil (cmp w w', n)
182 | Cons (h, t, rem) -> Cons (h, tree_write w t, rem)
183
184 let cons x l = match l with
185 | Cons (h1, t1, Cons (h2, t2, rem)) ->
186 if Int.equal h1 h2 then Cons (1 + h1 + h2, node x t1 t2, rem)
187 else Cons (1, leaf x, l)
188 | _ -> Cons (1, leaf x, l)
189
190 let expand_rel n s =
191 let k, v = get s (n - 1) in
192 match v with
193 | Inl (Arg v) -> Inl (k, v)
194 | Inl (Var i) -> Inr (k + i, None)
195 | Inr i -> Inr (k + i + 1, Some (i + 1))
196
197 let is_subs_id = function
198 | Nil (w, _) -> Int.equal w 0
199 | Cons (_, _, _) -> false
200
201 let subs_cons v s = cons (Arg v) s
202
203 let rec push_vars_until i k s =
204 if Int.equal k i then s
205 else push_vars_until i (pred k) (cons (Var k) s)
206
207 let subs_liftn n s =
208 if Int.equal n 0 then s
209 else match s with
210 | Nil (0, m) -> Nil (0, m + n) (* Preserve identity substitutions *)
211 | Nil _ | Cons _ ->
212 let s = write n s in
213 push_vars_until 0 n s
214
215 let subs_lift s = match s with
216 | Nil (0, m) -> Nil (0, m + 1) (* Preserve identity substitutions *)
217 | Nil _ | Cons _ ->
218 cons (Var 1) (write 1 s)
219
220 let subs_id n = Nil (0, n)
221
222 let subs_shft (n, s) = write n s
223
224 (* pop is the n-ary tailrec variant of a function whose typing rules would be
225 given as follows. Assume Γ ⊢ e : Δ, A, then
226 - Γ := Ξ, A, Ω for some Ξ and Ω with |Ω| := fst (pop e)
227 - Ξ ⊢ snd (pop e) : Δ
228 *)
229 let rec pop n i e =
230 if Int.equal n 0 then i, e
231 else match e with
232 | ELID -> i, e
233 | ELLFT (k, e) ->
234 if k <= n then pop (n - k) i e
235 else i, ELLFT (k - n, e)
236 | ELSHFT (e, k) -> pop (n + k) (i + k) e
237
238 let apply mk e = function
239 | Var i -> Var (reloc_rel i e)
240 | Arg v -> Arg (mk e v)
241
242 let rec tree_map mk e = function
243 | Leaf (w, x) ->
244 let (n, e) = pop w 0 e in
245 Leaf (w + n, apply mk e x), e
246 | Node (w, x, t1, t2, _) ->
247 let (n, e) = pop w 0 e in
248 let x = apply mk e x in
249 let t1, e = tree_map mk e t1 in
250 let t2, e = tree_map mk e t2 in
251 Node (w + n, x, t1, t2, cmp (eval t1) (eval t2)), e
252
253 let rec lift_id e i n = match e with
254 | ELID -> Nil (i, n - i)
255 | ELSHFT (e, k) -> lift_id e (i + k) (n + k)
256 | ELLFT (k, e) ->
257 if k <= i then
258 write k (lift_id e (i - k) (n - k))
259 else if k <= n then
260 let s = lift_id e 0 (n - k) in
261 let s = write k s in
262 push_vars_until i k s
263 else assert false
264
265 let rec lift_subst mk e s = match s with
266 | Nil (w, m) ->
267 let (n, e) = pop w 0 e in
268 write (w + n) (lift_id e 0 m)
269 | Cons (h, t, rem) ->
270 let t, e = tree_map mk e t in
271 let rem = lift_subst mk e rem in
272 Cons (h, t, rem)
273
274 module Internal =
275 struct
276
277 (** More intuitive representation for weakenings
278 Instead of using ELSHFT (s ⟼ ↑^n ∘ s), uses WEAK (s ⟼ s ∘ ↑^n) *)
279 type weakening =
280 | ID
281 (** [id] in substitution calculi; for all [Γ], [Γ ⊢ ID : Γ] *)
282
283 | LIFT of int * weakening
284 (** [⇑^n(σ)] in substitution calculi (i.e. [σ] under [n] binders);
285 assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ LIFT (n, σ) : Δ, Ξ] *)
286
287 | WEAK of int * weakening
288 (** [σ ∘ ↑^n] in substitution calculi (i.e. [σ], then a shift of [n]);
289 assuming [Γ ⊢ σ : Δ] and [n = |Ξ|], then [Γ, Ξ ⊢ WEAK (n, σ) : Δ] *)
290
291 (* compose a relocation of magnitude n *)
292 let weak n = function
293 | WEAK (k, w) -> WEAK (k+n, w)
294 | w -> WEAK (n, w)
295
296 (** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ weak n σ : Δ] *)
297 let weak n w = if Int.equal n 0 then w else weak n w
298
299 (* cross n binders *)
300 let lift n = function
301 | ID -> ID
302 | LIFT (k, w) -> LIFT (n+k, w)
303 | w -> LIFT (n, w)
304
305 (** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ lift n σ : Δ, Ξ] *)
306 let lift n w = if Int.equal n 0 then w else lift n w
307
308 let rec weakening_of_lift pending =
309 function
310 | ELID -> ID
311 | ELSHFT (el, k) -> weak k (weakening_of_lift (pending+k) el)
312 | ELLFT (k, el) ->
313 if k > pending then
314 lift (k - pending) (weakening_of_lift 0 el)
315 else
316 weakening_of_lift (pending - k) el
317 let weakening_of_lift el = weakening_of_lift 0 el
318
319 let rec weakening_to_lift =
320 function
321 | ID -> ELID
322 | LIFT (k, w) -> el_shft_rec k (weakening_to_lift w)
323 | WEAK (k, w) ->
324 el_shft_rec k (el_liftn_rec k (weakening_to_lift w))
325 [@@warning "-unused-value-declaration"]
326
327 let rec pp_weakening =
328 let open Pp in
329 function
330 | ID -> str "keep..]"
331 | LIFT (k, w) ->
332 str "keep " ++ int k ++ str ";" ++ spc () ++ pp_weakening w
333 | WEAK (k, w) ->
334 str "drop " ++ int k ++ str ";" ++ spc () ++ pp_weakening w
335 let pp_weakening w = Pp.(str "[" ++ pp_weakening w)
336
337 let pp_lift w = pp_weakening (weakening_of_lift w)
338
339
340 type 'a or_rel = REL of int | VAL of int * 'a
341
342 let to_rel shift = function
343 | Var i -> REL (i + shift)
344 | Arg v -> VAL (shift, v)
345
346 let rec get_tree_subst shift accu = function
347 | Leaf (w, x) ->
348 to_rel (shift + w) x :: accu
349 | Node (w, x, l, r, _) ->
350 let accu = get_tree_subst (shift + w + eval l) accu r in
351 let accu = get_tree_subst (shift + w) accu l in
352 to_rel (shift + w) x :: accu
353
354 let rec get_subst shift accu = function
355 | Nil (w, n) ->
356 List.init n (fun i -> REL (w + i + shift + 1))
357 | Cons (_, t, s) ->
358 let accu = get_subst (shift + eval t) accu s in
359 get_tree_subst shift accu t
360
361 let rec get_shift accu = function
362 | Nil (w, n) -> accu + w + n
363 | Cons (_, t, s) -> get_shift (eval t + accu) s
364
365 let repr (s : 'a subs) =
366 let shift = get_shift 0 s in
367 let subs = get_subst 0 [] s in
368 subs, shift
369
370 end