| 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 | open Names |
| 12 | open Constr |
| 13 | |
| 14 | (** {6 Occur checks } *) |
| 15 | |
| 16 | (** [closedn n M] is true iff [M] is a (de Bruijn) closed term under n binders *) |
| 17 | val closedn : int -> constr -> bool |
| 18 | |
| 19 | (** [closed0 M] is true iff [M] is a (de Bruijn) closed term *) |
| 20 | val closed0 : constr -> bool |
| 21 | |
| 22 | (** [noccurn n M] returns true iff [Rel n] does NOT occur in term [M] *) |
| 23 | val noccurn : int -> constr -> bool |
| 24 | |
| 25 | (** [noccur_between n m M] returns true iff [Rel p] does NOT occur in term [M] |
| 26 | for n <= p < n+m *) |
| 27 | val noccur_between : int -> int -> constr -> bool |
| 28 | |
| 29 | (** Checking function for terms containing existential- or |
| 30 | meta-variables. The function [noccur_with_meta] does not consider |
| 31 | meta-variables applied to some terms (intended to be its local |
| 32 | context) (for existential variables, it is necessarily the case) *) |
| 33 | val noccur_with_meta : int -> int -> constr -> bool |
| 34 | |
| 35 | (** {6 Relocation and substitution } *) |
| 36 | |
| 37 | (** [exliftn el c] lifts [c] with arbitrary complex lifting [el] *) |
| 38 | val exliftn : Esubst.lift -> constr -> constr |
| 39 | |
| 40 | (** [liftn n k c] lifts by [n] indices greater than or equal to [k] in [c] |
| 41 | Note that with respect to substitution calculi's terminology, [n] |
| 42 | is the _shift_ and [k] is the _lift_. *) |
| 43 | val liftn : int -> int -> constr -> constr |
| 44 | |
| 45 | (** [lift n c] lifts by [n] the positive indexes in [c] *) |
| 46 | val lift : int -> constr -> constr |
| 47 | |
| 48 | (** Same as [liftn] for a context *) |
| 49 | val liftn_rel_context : int -> int -> rel_context -> rel_context |
| 50 | |
| 51 | (** Same as [lift] for a context *) |
| 52 | val lift_rel_context : int -> rel_context -> rel_context |
| 53 | |
| 54 | (** The type [substl] is the type of substitutions [u₁..un] of type |
| 55 | some well-typed context Δ and defined in some environment Γ. |
| 56 | Typing of substitutions is defined by: |
| 57 | - Γ ⊢ ∅ : ∅, |
| 58 | - Γ ⊢ u₁..u{_n-1} : Δ and Γ ⊢ u{_n} : An\[u₁..u{_n-1}\] implies |
| 59 | Γ ⊢ u₁..u{_n} : Δ,x{_n}:A{_n} |
| 60 | - Γ ⊢ u₁..u{_n-1} : Δ and Γ ⊢ un : A{_n}\[u₁..u{_n-1}\] implies |
| 61 | Γ ⊢ u₁..u{_n} : Δ,x{_n}:=c{_n}:A{_n} when Γ ⊢ u{_n} ≡ c{_n}\[u₁..u{_n-1}\] |
| 62 | |
| 63 | Note that [u₁..un] is represented as a list with [un] at the head of |
| 64 | the list, i.e. as [[un;...;u₁]]. |
| 65 | |
| 66 | A [substl] differs from an [instance] in that it includes the |
| 67 | terms bound by lets while the latter does not. Also, their |
| 68 | internal representations are in opposite order. *) |
| 69 | |
| 70 | type substl = constr list |
| 71 | |
| 72 | (** The type [instance] is the type of instances [u₁..un] of a |
| 73 | well-typed context Δ (relatively to some environment Γ). Typing of |
| 74 | instances is defined by: |
| 75 | - Γ ⊢ ∅ : ∅, |
| 76 | - Γ ⊢ u₁..u{_n} : Δ and Γ ⊢ u{_n+1} : A{_n+1}\[ϕ(Δ,u₁..u{_n})\] implies |
| 77 | Γ ⊢ u₁..u{_n+1} : Δ,x{_n+1}:A{_n+1} |
| 78 | - Γ ⊢ u₁..u{_n} : Δ implies |
| 79 | Γ ⊢ u₁..u{_n} : Δ,x{_n+1}:=c{_n+1}:A{_n+1} |
| 80 | where [ϕ(Δ,u₁..u{_n})] is the substitution obtained by adding lets |
| 81 | of Δ to the instance so as to get a substitution (see |
| 82 | [subst_of_rel_context_instance] below). |
| 83 | |
| 84 | Note that [u₁..un] is represented as an array with [u1] at the |
| 85 | head of the array, i.e. as [[u₁;...;un]]. In particular, it can |
| 86 | directly be used with [mkApp] to build an applicative term |
| 87 | [f u₁..un] whenever [f] is of some type [forall Δ, T]. |
| 88 | |
| 89 | An [instance] differs from a [substl] in that it does not include |
| 90 | the terms bound by lets while the latter does. Also, their |
| 91 | internal representations are in opposite order. |
| 92 | |
| 93 | An [instance_list] is the same as an [instance] but using a list |
| 94 | instead of an array. *) |
| 95 | |
| 96 | type instance = constr array |
| 97 | type instance_list = constr list |
| 98 | |
| 99 | (** Let [Γ] be a context interleaving declarations [x₁:T₁..xn:Tn] |
| 100 | and definitions [y₁:=c₁..yp:=cp] in some context [Γ₀]. Let |
| 101 | [u₁..un] be an {e instance} of [Γ], i.e. an instance in [Γ₀] |
| 102 | of the [xi]. Then, [subst_of_rel_context_instance_list Γ u₁..un] |
| 103 | returns the corresponding {e substitution} of [Γ], i.e. the |
| 104 | appropriate interleaving [σ] of the [u₁..un] with the [c₁..cp], |
| 105 | all of them in [Γ₀], so that a derivation [Γ₀, Γ, Γ₁|- t:T] |
| 106 | can be instantiated into a derivation [Γ₀, Γ₁ |- t[σ]:T[σ]] using |
| 107 | [substnl σ |Γ₁| t]. |
| 108 | Note that the instance [u₁..un] is represented starting with [u₁], |
| 109 | as if usable in [applist] while the substitution is |
| 110 | represented the other way round, i.e. ending with either [u₁] or |
| 111 | [c₁], as if usable for [substl]. *) |
| 112 | val subst_of_rel_context_instance : Constr.rel_context -> instance -> substl |
| 113 | val subst_of_rel_context_instance_list : Constr.rel_context -> instance_list -> substl |
| 114 | |
| 115 | (** Take an index in an instance of a context and returns its index wrt to |
| 116 | the full context (e.g. 2 in [x:A;y:=b;z:C] is 3, i.e. a reference to z) *) |
| 117 | val adjust_rel_to_rel_context : ('a, 'b, 'r) Context.Rel.pt -> int -> int |
| 118 | |
| 119 | (** [substnl [a₁;...;an] k c] substitutes in parallel [a₁],...,[an] |
| 120 | for respectively [Rel(k+1)],...,[Rel(k+n)] in [c]; it relocates |
| 121 | accordingly indexes in [an],...,[a1] and [c]. In terms of typing, if |
| 122 | Γ ⊢ a{_n}..a₁ : Δ and Γ, Δ, Γ' ⊢ c : T with |Γ'|=k, then |
| 123 | Γ, Γ' ⊢ [substnl [a₁;...;an] k c] : [substnl [a₁;...;an] k T]. *) |
| 124 | val substnl : substl -> int -> constr -> constr |
| 125 | |
| 126 | (** [substl σ c] is a short-hand for [substnl σ 0 c] *) |
| 127 | val substl : substl -> constr -> constr |
| 128 | |
| 129 | (** [substl a c] is a short-hand for [substnl [a] 0 c] *) |
| 130 | val subst1 : constr -> constr -> constr |
| 131 | |
| 132 | (** [substnl_decl [a₁;...;an] k Ω] substitutes in parallel [a₁], ..., [an] for |
| 133 | respectively [Rel(k+1)], ..., [Rel(k+n)] in a declaration [Ω]; it relocates |
| 134 | accordingly indexes in [a₁],...,[an] and [c]. In terms of typing, if |
| 135 | Γ ⊢ a{_n}..a₁ : Δ and Γ, Δ, Γ', Ω ⊢ with |Γ'|=[k], then |
| 136 | Γ, Γ', [substnl_decl [a₁;...;an]] k Ω ⊢. *) |
| 137 | val substnl_decl : substl -> int -> Constr.rel_declaration -> Constr.rel_declaration |
| 138 | |
| 139 | (** [substl_decl σ Ω] is a short-hand for [substnl_decl σ 0 Ω] *) |
| 140 | val substl_decl : substl -> Constr.rel_declaration -> Constr.rel_declaration |
| 141 | |
| 142 | (** [subst1_decl a Ω] is a short-hand for [substnl_decl [a] 0 Ω] *) |
| 143 | val subst1_decl : constr -> Constr.rel_declaration -> Constr.rel_declaration |
| 144 | |
| 145 | (** [substnl_rel_context [a₁;...;an] k Ω] substitutes in parallel [a₁], ..., [an] |
| 146 | for respectively [Rel(k+1)], ..., [Rel(k+n)] in a context [Ω]; it relocates |
| 147 | accordingly indexes in [a₁],...,[an] and [c]. In terms of typing, if |
| 148 | Γ ⊢ a{_n}..a₁ : Δ and Γ, Δ, Γ', Ω ⊢ with |Γ'|=[k], then |
| 149 | Γ, Γ', [substnl_rel_context [a₁;...;an]] k Ω ⊢. *) |
| 150 | val substnl_rel_context : substl -> int -> Constr.rel_context -> Constr.rel_context |
| 151 | |
| 152 | (** [substl_rel_context σ Ω] is a short-hand for [substnl_rel_context σ 0 Ω] *) |
| 153 | val substl_rel_context : substl -> Constr.rel_context -> Constr.rel_context |
| 154 | |
| 155 | (** [subst1_rel_context a Ω] is a short-hand for [substnl_rel_context [a] 0 Ω] *) |
| 156 | val subst1_rel_context : constr -> Constr.rel_context -> Constr.rel_context |
| 157 | |
| 158 | (** [esubst lift σ c] substitutes [c] with arbitrary complex substitution [σ], |
| 159 | using [lift] to lift subterms where necessary. *) |
| 160 | val esubst : (int -> 'a -> constr) -> 'a Esubst.subs -> constr -> constr |
| 161 | |
| 162 | (** [replace_vars k [(id₁,c₁);...;(idn,cn)] t] substitutes [Var idj] by |
| 163 | [cj] in [t]. *) |
| 164 | val replace_vars : (Id.t * constr) list -> constr -> constr |
| 165 | |
| 166 | (** [substn_vars k [id₁;...;idn] t] substitutes [Var idj] by [Rel j+k-1] in [t]. |
| 167 | If two names are identical, the one of least index is kept. In terms of |
| 168 | typing, if Γ,x{_n}:U{_n},...,x₁:U₁,Γ' ⊢ t:T, together with id{_j}:T{_j} and |
| 169 | Γ,x{_n}:U{_n},...,x₁:U₁,Γ' ⊢ T{_j}\[id{_j+1}..id{_n}:=x{_j+1}..x{_n}\] ≡ Uj, |
| 170 | then Γ\\{id₁,...,id{_n}\},x{_n}:U{_n},...,x₁:U₁,Γ' ⊢ [substn_vars |
| 171 | (|Γ'|+1) [id₁;...;idn] t] : [substn_vars (|Γ'|+1) [id₁;...;idn] |
| 172 | T]. *) |
| 173 | val substn_vars : int -> Id.t list -> constr -> constr |
| 174 | |
| 175 | (** [subst_vars [id1;...;idn] t] is a short-hand for [substn_vars |
| 176 | [id1;...;idn] 1 t]: it substitutes [Var idj] by [Rel j] in [t]. If |
| 177 | two names are identical, the one of least index is kept. *) |
| 178 | val subst_vars : Id.t list -> constr -> constr |
| 179 | |
| 180 | (** [subst_var id t] is a short-hand for [substn_vars [id] 1 t]: it |
| 181 | substitutes [Var id] by [Rel 1] in [t]. *) |
| 182 | val subst_var : Id.t -> constr -> constr |
| 183 | |
| 184 | (** Expand lets in context *) |
| 185 | val smash_rel_context : rel_context -> rel_context |
| 186 | |
| 187 | (** {3 Substitution of universes} *) |
| 188 | |
| 189 | open UVars |
| 190 | |
| 191 | (** Level substitutions for polymorphism. *) |
| 192 | |
| 193 | val subst_univs_level_constr : sort_level_subst -> constr -> constr |
| 194 | val subst_univs_level_context : sort_level_subst -> Constr.rel_context -> Constr.rel_context |
| 195 | |
| 196 | (** Instance substitution for polymorphism. *) |
| 197 | val subst_instance_constr : Instance.t -> constr -> constr |
| 198 | val subst_instance_context : Instance.t -> Constr.rel_context -> Constr.rel_context |
| 199 | |
| 200 | val univ_instantiate_constr : Instance.t -> constr univ_abstracted -> constr |
| 201 | (** Ignores the constraints carried by [univ_abstracted]. *) |
| 202 | |
| 203 | val map_constr_relevance : (Sorts.relevance -> Sorts.relevance) -> Constr.t -> Constr.t |
| 204 | (** Modifies the relevances in the head node (not in subterms) *) |
| 205 | |
| 206 | val sort_and_universes_of_constr : ?init:Sorts.QVar.Set.t * Univ.Level.Set.t -> constr -> Sorts.QVar.Set.t * Univ.Level.Set.t |
| 207 | |
| 208 | val universes_of_constr : ?init:Univ.Level.Set.t -> constr -> Univ.Level.Set.t |
| 209 | |
| 210 | type ('a,'s,'u,'r) univ_visitor = { |
| 211 | visit_sort : 'a -> 's -> 'a; |
| 212 | visit_instance : 'a -> 'u -> 'a; |
| 213 | visit_relevance : 'a -> 'r -> 'a; |
| 214 | } |
| 215 | |
| 216 | val visit_kind_univs : ('acc, 'sort, 'instance, 'relevance) univ_visitor -> |
| 217 | 'acc -> |
| 218 | (_, _, 'sort, 'instance, 'relevance) Constr.kind_of_term -> |
| 219 | 'acc |
| 220 | |
| 221 | (** {3 Low-level cached lift type} *) |
| 222 | |
| 223 | type substituend |
| 224 | val make_substituend : constr -> substituend |
| 225 | val lift_substituend : int -> substituend -> constr |