| 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 | (** Explicit substitutions *) |
| 12 | |
| 13 | (** {6 Explicit substitutions } *) |
| 14 | (** Explicit substitutions for some type of terms ['a]. |
| 15 | |
| 16 | Assuming terms enjoy a notion of typability [Γ ⊢ t : A], where [Γ] is a |
| 17 | telescope and [A] a type, substitutions can be typed as [Γ ⊢ σ : Δ], where |
| 18 | as a first approximation [σ] is a list of terms [[u₁; ...; uₙ]] s.t. |
| 19 | [Δ := (x₁ : A₁), ..., (xₙ : Aₙ)] and [Γ ⊢ uᵢ : Aᵢ{u₁...uᵢ₋₁}] |
| 20 | for all [1 ≤ i ≤ n]. |
| 21 | |
| 22 | Substitutions can be applied to terms as follows, and furthermore |
| 23 | if [Γ ⊢ σ : Δ] and [Δ ⊢ t : A], then [Γ ⊢ t{σ} : A{σ}]. |
| 24 | |
| 25 | We make the typing rules explicit below, but we omit the explicit De Bruijn |
| 26 | fidgetting and leave relocations implicit in terms and types. |
| 27 | |
| 28 | *) |
| 29 | type 'a subs |
| 30 | |
| 31 | (** Derived constructors granting basic invariants *) |
| 32 | |
| 33 | (** Assuming [|Γ| = n], [Γ ⊢ subs_id n : Γ] *) |
| 34 | val subs_id : int -> 'a subs |
| 35 | |
| 36 | (** Assuming [Γ ⊢ σ : Δ] and [Γ ⊢ t : A{σ}], then [Γ ⊢ subs_cons t σ : Δ, A] *) |
| 37 | val subs_cons: 'a -> 'a subs -> 'a subs |
| 38 | |
| 39 | (** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ subs_shft (n, σ) : Δ] *) |
| 40 | val subs_shft: int * 'a subs -> 'a subs |
| 41 | |
| 42 | (** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ subs_liftn n σ : Δ, Ξ] *) |
| 43 | val subs_liftn: int -> 'a subs -> 'a subs |
| 44 | |
| 45 | (** Unary variant of {!subst_liftn}. *) |
| 46 | val subs_lift: 'a subs -> 'a subs |
| 47 | |
| 48 | (** [expand_rel k subs] expands de Bruijn [k] in the explicit substitution |
| 49 | [subs]. The result is either [Inl (lams, v)] when the variable is |
| 50 | substituted by value [v] under [lams] binders (i.e. [v] *has* to be |
| 51 | shifted by [lams]), or [Inr (k', p)] when the variable [k] is just |
| 52 | relocated as [k']; [p] is [None] if the variable points inside [subs] |
| 53 | and [Some k] if the variable points [k] bindings beyond [subs] |
| 54 | (cf argument of [ESID]). |
| 55 | *) |
| 56 | val expand_rel: int -> 'a subs -> (int * 'a, int * int option) Util.union |
| 57 | |
| 58 | (** Tests whether a substitution behaves like the identity *) |
| 59 | val is_subs_id: 'a subs -> bool |
| 60 | |
| 61 | (** {6 Compact representation} *) |
| 62 | (** Compact representation of explicit relocations |
| 63 | - [ELID]: identity relocation [id] |
| 64 | - [ELSHFT (σ, n)]: shift of [n], then [σ]; [↑^n ∘ σ] in sigma calculi |
| 65 | - [ELLFT (n, σ)]: apply [σ] to de Bruijn > [n], i.e under [n] binders; |
| 66 | [⇑^n(σ)] in sigma calculi |
| 67 | |
| 68 | Invariant ensured by the private flag: no lift contains an [ELLFT] of [ELID], |
| 69 | two consecutive [ELLFT] or two consecutive [ELSHFT]. |
| 70 | |
| 71 | Relocations are a particular kind of substitutions that only contain |
| 72 | variables. In particular, [el_*] enjoys similar typing rules as the |
| 73 | equivalent substitution function [subs_*]. |
| 74 | *) |
| 75 | type lift = private |
| 76 | | ELID |
| 77 | | ELSHFT of lift * int |
| 78 | | ELLFT of int * lift |
| 79 | |
| 80 | (** For arbitrary Γ, [Γ ⊢ el_id : Γ] *) |
| 81 | val el_id : lift |
| 82 | |
| 83 | (** Assuming [Γ ⊢ σ : Δ, Ξ] and [|Ξ| = n], then [Γ ⊢ el_shft n σ : Δ] *) |
| 84 | val el_shft : int -> lift -> lift |
| 85 | |
| 86 | (** Assuming [Γ ⊢ σ : Δ] and [|Ξ| = n], then [Γ, Ξ ⊢ el_liftn n σ : Δ, Ξ] *) |
| 87 | val el_liftn : int -> lift -> lift |
| 88 | |
| 89 | (** Unary variant of {!el_liftn}. *) |
| 90 | val el_lift : lift -> lift |
| 91 | |
| 92 | (** Assuming [Γ₁, A, Γ₂ ⊢ σ : Δ₁, A, Δ₂] and [Δ₁, A, Δ₂ ⊢ n : A], |
| 93 | then [Γ₁, A, Γ₂ ⊢ reloc_rel n σ : A] *) |
| 94 | val reloc_rel : int -> lift -> int |
| 95 | |
| 96 | val is_lift_id : lift -> bool |
| 97 | |
| 98 | (** Lift applied to substitution: [lift_subst mk_clos el s] computes a |
| 99 | substitution equivalent to applying [el] then [s]. Argument |
| 100 | [mk_clos] is used when a closure has to be created, i.e. when |
| 101 | [el] is applied on an element of [s]. |
| 102 | |
| 103 | That is, if [Γ ⊢ e : Δ] and [Δ ⊢ σ : Ξ], then [Γ ⊢ lift_subst mk e σ : Ξ]. |
| 104 | *) |
| 105 | val lift_subst : (lift -> 'a -> 'b) -> lift -> 'a subs -> 'b subs |
| 106 | |
| 107 | (** Structural equality for lifts *) |
| 108 | val eq_lift : lift -> lift -> bool |
| 109 | |
| 110 | (** Debugging utilities *) |
| 111 | module Internal : |
| 112 | sig |
| 113 | |
| 114 | (** If [Γ ⊢ lift : Δ], [pp_lift lift] prints [Γ] as a list of whether |
| 115 | the corresponding element appears in [Δ] *) |
| 116 | val pp_lift : lift -> Pp.t |
| 117 | |
| 118 | |
| 119 | type 'a or_rel = REL of int | VAL of int * 'a |
| 120 | |
| 121 | (** High-level representation of a substitution. The first component is a list |
| 122 | that associates a value to an index, and the second component is the |
| 123 | relocation shift that must be applied to any variable pointing outside of |
| 124 | the substitution. *) |
| 125 | val repr : 'a subs -> 'a or_rel list * int |
| 126 | end |