|
| 1 | +(************************************************************************) |
| 2 | +(* * The Coq Proof Assistant / The Coq Development Team *) |
| 3 | +(* v * INRIA, CNRS and contributors - Copyright 1999-2018 *) |
| 4 | +(* <O___,, * (see CREDITS file for the list of authors) *) |
| 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 Sorts |
| 12 | +open Names |
| 13 | +open Constr |
| 14 | +open Environ |
| 15 | +open Univ |
| 16 | + |
| 17 | +(* Generator of levels *) |
| 18 | +type universe_id = DirPath.t * int |
| 19 | + |
| 20 | +let new_univ_id, set_remote_new_univ_id = |
| 21 | + RemoteCounter.new_counter ~name:"Universes" 0 ~incr:((+) 1) |
| 22 | + ~build:(fun n -> Global.current_dirpath (), n) |
| 23 | + |
| 24 | +let new_univ_level () = |
| 25 | + let dp, id = new_univ_id () in |
| 26 | + Univ.Level.make dp id |
| 27 | + |
| 28 | +let fresh_level () = new_univ_level () |
| 29 | + |
| 30 | +(* TODO: remove *) |
| 31 | +let new_univ dp = Univ.Universe.make (new_univ_level dp) |
| 32 | +let new_Type dp = mkType (new_univ dp) |
| 33 | +let new_Type_sort dp = Type (new_univ dp) |
| 34 | + |
| 35 | +let fresh_universe_instance ctx = |
| 36 | + let init _ = new_univ_level () in |
| 37 | + Instance.of_array (Array.init (AUContext.size ctx) init) |
| 38 | + |
| 39 | +let fresh_instance_from_context ctx = |
| 40 | + let inst = fresh_universe_instance ctx in |
| 41 | + let constraints = AUContext.instantiate inst ctx in |
| 42 | + inst, constraints |
| 43 | + |
| 44 | +let fresh_instance ctx = |
| 45 | + let ctx' = ref LSet.empty in |
| 46 | + let init _ = |
| 47 | + let u = new_univ_level () in |
| 48 | + ctx' := LSet.add u !ctx'; u |
| 49 | + in |
| 50 | + let inst = Instance.of_array (Array.init (AUContext.size ctx) init) |
| 51 | + in !ctx', inst |
| 52 | + |
| 53 | +let existing_instance ctx inst = |
| 54 | + let () = |
| 55 | + let len1 = Array.length (Instance.to_array inst) |
| 56 | + and len2 = AUContext.size ctx in |
| 57 | + if not (len1 == len2) then |
| 58 | + CErrors.user_err ~hdr:"Universes" |
| 59 | + Pp.(str "Polymorphic constant expected " ++ int len2 ++ |
| 60 | + str" levels but was given " ++ int len1) |
| 61 | + else () |
| 62 | + in LSet.empty, inst |
| 63 | + |
| 64 | +let fresh_instance_from ctx inst = |
| 65 | + let ctx', inst = |
| 66 | + match inst with |
| 67 | + | Some inst -> existing_instance ctx inst |
| 68 | + | None -> fresh_instance ctx |
| 69 | + in |
| 70 | + let constraints = AUContext.instantiate inst ctx in |
| 71 | + inst, (ctx', constraints) |
| 72 | + |
| 73 | +(** Fresh universe polymorphic construction *) |
| 74 | + |
| 75 | +let fresh_constant_instance env c inst = |
| 76 | + let cb = lookup_constant c env in |
| 77 | + match cb.Declarations.const_universes with |
| 78 | + | Declarations.Monomorphic_const _ -> ((c,Instance.empty), ContextSet.empty) |
| 79 | + | Declarations.Polymorphic_const auctx -> |
| 80 | + let inst, ctx = |
| 81 | + fresh_instance_from auctx inst |
| 82 | + in |
| 83 | + ((c, inst), ctx) |
| 84 | + |
| 85 | +let fresh_inductive_instance env ind inst = |
| 86 | + let mib, mip = Inductive.lookup_mind_specif env ind in |
| 87 | + match mib.Declarations.mind_universes with |
| 88 | + | Declarations.Monomorphic_ind _ -> |
| 89 | + ((ind,Instance.empty), ContextSet.empty) |
| 90 | + | Declarations.Polymorphic_ind uactx -> |
| 91 | + let inst, ctx = (fresh_instance_from uactx) inst in |
| 92 | + ((ind,inst), ctx) |
| 93 | + | Declarations.Cumulative_ind acumi -> |
| 94 | + let inst, ctx = |
| 95 | + fresh_instance_from (Univ.ACumulativityInfo.univ_context acumi) inst |
| 96 | + in ((ind,inst), ctx) |
| 97 | + |
| 98 | +let fresh_constructor_instance env (ind,i) inst = |
| 99 | + let mib, mip = Inductive.lookup_mind_specif env ind in |
| 100 | + match mib.Declarations.mind_universes with |
| 101 | + | Declarations.Monomorphic_ind _ -> (((ind,i),Instance.empty), ContextSet.empty) |
| 102 | + | Declarations.Polymorphic_ind auctx -> |
| 103 | + let inst, ctx = fresh_instance_from auctx inst in |
| 104 | + (((ind,i),inst), ctx) |
| 105 | + | Declarations.Cumulative_ind acumi -> |
| 106 | + let inst, ctx = fresh_instance_from (ACumulativityInfo.univ_context acumi) inst in |
| 107 | + (((ind,i),inst), ctx) |
| 108 | + |
| 109 | +open Globnames |
| 110 | + |
| 111 | +let fresh_global_instance ?names env gr = |
| 112 | + match gr with |
| 113 | + | VarRef id -> mkVar id, ContextSet.empty |
| 114 | + | ConstRef sp -> |
| 115 | + let c, ctx = fresh_constant_instance env sp names in |
| 116 | + mkConstU c, ctx |
| 117 | + | ConstructRef sp -> |
| 118 | + let c, ctx = fresh_constructor_instance env sp names in |
| 119 | + mkConstructU c, ctx |
| 120 | + | IndRef sp -> |
| 121 | + let c, ctx = fresh_inductive_instance env sp names in |
| 122 | + mkIndU c, ctx |
| 123 | + |
| 124 | +let fresh_constant_instance env sp = |
| 125 | + fresh_constant_instance env sp None |
| 126 | + |
| 127 | +let fresh_inductive_instance env sp = |
| 128 | + fresh_inductive_instance env sp None |
| 129 | + |
| 130 | +let fresh_constructor_instance env sp = |
| 131 | + fresh_constructor_instance env sp None |
| 132 | + |
| 133 | +let constr_of_global gr = |
| 134 | + let c, ctx = fresh_global_instance (Global.env ()) gr in |
| 135 | + if not (Univ.ContextSet.is_empty ctx) then |
| 136 | + if Univ.LSet.is_empty (Univ.ContextSet.levels ctx) then |
| 137 | + (* Should be an error as we might forget constraints, allow for now |
| 138 | + to make firstorder work with "using" clauses *) |
| 139 | + c |
| 140 | + else CErrors.user_err ~hdr:"constr_of_global" |
| 141 | + Pp.(str "globalization of polymorphic reference " ++ Nametab.pr_global_env Id.Set.empty gr ++ |
| 142 | + str " would forget universes.") |
| 143 | + else c |
| 144 | + |
| 145 | +let constr_of_global_univ (gr,u) = |
| 146 | + match gr with |
| 147 | + | VarRef id -> mkVar id |
| 148 | + | ConstRef sp -> mkConstU (sp,u) |
| 149 | + | ConstructRef sp -> mkConstructU (sp,u) |
| 150 | + | IndRef sp -> mkIndU (sp,u) |
| 151 | + |
| 152 | +let fresh_global_or_constr_instance env = function |
| 153 | + | IsConstr c -> c, ContextSet.empty |
| 154 | + | IsGlobal gr -> fresh_global_instance env gr |
| 155 | + |
| 156 | +let global_of_constr c = |
| 157 | + match kind c with |
| 158 | + | Const (c, u) -> ConstRef c, u |
| 159 | + | Ind (i, u) -> IndRef i, u |
| 160 | + | Construct (c, u) -> ConstructRef c, u |
| 161 | + | Var id -> VarRef id, Instance.empty |
| 162 | + | _ -> raise Not_found |
| 163 | + |
| 164 | +open Declarations |
| 165 | + |
| 166 | +let type_of_reference env r = |
| 167 | + match r with |
| 168 | + | VarRef id -> Environ.named_type id env, ContextSet.empty |
| 169 | + | ConstRef c -> |
| 170 | + let cb = Environ.lookup_constant c env in |
| 171 | + let ty = cb.const_type in |
| 172 | + begin |
| 173 | + match cb.const_universes with |
| 174 | + | Monomorphic_const _ -> ty, ContextSet.empty |
| 175 | + | Polymorphic_const auctx -> |
| 176 | + let inst, ctx = fresh_instance_from auctx None in |
| 177 | + Vars.subst_instance_constr inst ty, ctx |
| 178 | + end |
| 179 | + | IndRef ind -> |
| 180 | + let (mib, oib as specif) = Inductive.lookup_mind_specif env ind in |
| 181 | + begin |
| 182 | + match mib.mind_universes with |
| 183 | + | Monomorphic_ind _ -> |
| 184 | + let ty = Inductive.type_of_inductive env (specif, Univ.Instance.empty) in |
| 185 | + ty, ContextSet.empty |
| 186 | + | Polymorphic_ind auctx -> |
| 187 | + let inst, ctx = fresh_instance_from auctx None in |
| 188 | + let ty = Inductive.type_of_inductive env (specif, inst) in |
| 189 | + ty, ctx |
| 190 | + | Cumulative_ind cumi -> |
| 191 | + let inst, ctx = |
| 192 | + fresh_instance_from (ACumulativityInfo.univ_context cumi) None |
| 193 | + in |
| 194 | + let ty = Inductive.type_of_inductive env (specif, inst) in |
| 195 | + ty, ctx |
| 196 | + end |
| 197 | + |
| 198 | + | ConstructRef cstr -> |
| 199 | + let (mib,oib as specif) = |
| 200 | + Inductive.lookup_mind_specif env (inductive_of_constructor cstr) |
| 201 | + in |
| 202 | + begin |
| 203 | + match mib.mind_universes with |
| 204 | + | Monomorphic_ind _ -> |
| 205 | + Inductive.type_of_constructor (cstr,Instance.empty) specif, ContextSet.empty |
| 206 | + | Polymorphic_ind auctx -> |
| 207 | + let inst, ctx = fresh_instance_from auctx None in |
| 208 | + Inductive.type_of_constructor (cstr,inst) specif, ctx |
| 209 | + | Cumulative_ind cumi -> |
| 210 | + let inst, ctx = |
| 211 | + fresh_instance_from (ACumulativityInfo.univ_context cumi) None |
| 212 | + in |
| 213 | + Inductive.type_of_constructor (cstr,inst) specif, ctx |
| 214 | + end |
| 215 | + |
| 216 | +let type_of_global t = type_of_reference (Global.env ()) t |
| 217 | + |
| 218 | +let fresh_sort_in_family env = function |
| 219 | + | InProp -> Sorts.prop, ContextSet.empty |
| 220 | + | InSet -> Sorts.set, ContextSet.empty |
| 221 | + | InType -> |
| 222 | + let u = fresh_level () in |
| 223 | + Type (Univ.Universe.make u), ContextSet.singleton u |
| 224 | + |
| 225 | +let new_sort_in_family sf = |
| 226 | + fst (fresh_sort_in_family (Global.env ()) sf) |
| 227 | + |
| 228 | +let extend_context (a, ctx) (ctx') = |
| 229 | + (a, ContextSet.union ctx ctx') |
| 230 | + |
| 231 | +let new_global_univ () = |
| 232 | + let u = fresh_level () in |
| 233 | + (Univ.Universe.make u, ContextSet.singleton u) |
| 234 | + |
| 235 | +let fresh_universe_context_set_instance ctx = |
| 236 | + if ContextSet.is_empty ctx then LMap.empty, ctx |
| 237 | + else |
| 238 | + let (univs, cst) = ContextSet.levels ctx, ContextSet.constraints ctx in |
| 239 | + let univs',subst = LSet.fold |
| 240 | + (fun u (univs',subst) -> |
| 241 | + let u' = fresh_level () in |
| 242 | + (LSet.add u' univs', LMap.add u u' subst)) |
| 243 | + univs (LSet.empty, LMap.empty) |
| 244 | + in |
| 245 | + let cst' = subst_univs_level_constraints subst cst in |
| 246 | + subst, (univs', cst') |
0 commit comments