| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CErrors |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Context |
| 15 |
open Constrintern |
| 16 |
open Impargs |
| 17 |
open Pretyping |
| 18 |
|
| 19 |
module NamedDecl = Context.Named.Declaration |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
let declare_local ~coe ~try_assum_as_instance ~kind ~univs ~impargs ~impl ~name body typ = |
| 30 |
let decl = match body with |
| 31 |
| None -> |
| 32 |
Declare.SectionLocalAssum {typ; impl; univs} |
| 33 |
| Some b -> |
| 34 |
Declare.SectionLocalDef {clearbody = false; entry = Declare.definition_entry ~univs ~types:typ b} in |
| 35 |
let () = Declare.declare_variable ~name ~kind ~typing_flags:None decl in |
| 36 |
let () = if body = None then Declare.assumption_message name else Declare.definition_message name in |
| 37 |
let r = GlobRef.VarRef name in |
| 38 |
let () = maybe_declare_manual_implicits true r impargs in |
| 39 |
let _ = if try_assum_as_instance && Option.is_empty body then |
| 40 |
let env = Global.env () in |
| 41 |
let sigma = Evd.from_env env in |
| 42 |
Classes.declare_instance env sigma None Hints.Local r in |
| 43 |
let () = |
| 44 |
if coe = Vernacexpr.AddCoercion then |
| 45 |
ComCoercion.try_add_new_coercion |
| 46 |
r ~local:true ~reversible:false in |
| 47 |
(r, UVars.Instance.empty) |
| 48 |
|
| 49 |
let declare_variable ~coe ~kind ~univs ~impargs ~impl ~name typ = |
| 50 |
declare_local ~coe ~try_assum_as_instance:true ~kind:(Decls.IsAssumption kind) ~univs ~impargs ~impl ~name None typ |
| 51 |
|
| 52 |
let instance_of_univ_entry = function |
| 53 |
| UState.Polymorphic_entry univs -> UVars.UContext.instance univs |
| 54 |
| UState.Monomorphic_entry _ -> UVars.Instance.empty |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
let declare_global ~coe ~try_assum_as_instance ~local ~kind ?user_warns ~univs ~impargs ~inline |
| 63 |
~name:{CAst.v=name; loc} body typ = |
| 64 |
let (uentry, ubinders) = univs in |
| 65 |
let inl = let open Declaremods in match inline with |
| 66 |
| NoInline -> None |
| 67 |
| DefaultInline -> Declaremods.default_inline_level() |
| 68 |
| InlineAt i -> Some i |
| 69 |
in |
| 70 |
let decl = match body with |
| 71 |
| None -> Declare.ParameterEntry (Declare.parameter_entry ~univs:(uentry, ubinders) ?inline:inl typ) |
| 72 |
| Some b -> Declare.DefinitionEntry (Declare.definition_entry ~univs ~types:typ b) in |
| 73 |
let kn = Declare.declare_constant ?loc ~name ~local ~kind ?user_warns decl in |
| 74 |
let gr = GlobRef.ConstRef kn in |
| 75 |
let () = maybe_declare_manual_implicits false gr impargs in |
| 76 |
let () = match body with None -> Declare.assumption_message name | Some _ -> Declare.definition_message name in |
| 77 |
let local = match local with |
| 78 |
| Locality.ImportNeedQualified -> true |
| 79 |
| Locality.ImportDefaultBehavior -> false |
| 80 |
in |
| 81 |
let () = if try_assum_as_instance && Option.is_empty body then |
| 82 |
|
| 83 |
let env = Global.env () in |
| 84 |
let sigma = Evd.from_env env in |
| 85 |
Classes.declare_instance env sigma None Hints.SuperGlobal gr in |
| 86 |
let () = |
| 87 |
if coe = Vernacexpr.AddCoercion then |
| 88 |
ComCoercion.try_add_new_coercion |
| 89 |
gr ~local ~reversible:false in |
| 90 |
let inst = instance_of_univ_entry uentry in |
| 91 |
(gr,inst) |
| 92 |
|
| 93 |
let declare_axiom ~coe ~local ~kind ?user_warns ~univs ~impargs ~inline ~name typ = |
| 94 |
declare_global ~coe ~try_assum_as_instance:false ~local ~kind:(Decls.IsAssumption kind) ?user_warns ~univs ~impargs ~inline ~name None typ |
| 95 |
|
| 96 |
let interp_assumption ~program_mode ~poly env sigma impl_env bl c = |
| 97 |
let flags = { Pretyping.all_no_fail_flags with program_mode; poly } in |
| 98 |
let sigma, (impls, ((env_bl, ctx), impls1, _locs)) = interp_context_evars ~program_mode ~poly ~impl_env env sigma bl in |
| 99 |
let sigma, (ty, impls2) = interp_type_evars_impls ~flags env_bl sigma ~impls c in |
| 100 |
let ty = EConstr.it_mkProd_or_LetIn ty ctx in |
| 101 |
sigma, ty, impls1@impls2 |
| 102 |
|
| 103 |
let empty_poly_univ_entry = UState.Polymorphic_entry UVars.UContext.empty, UnivNames.empty_binders |
| 104 |
let empty_mono_univ_entry = UState.Monomorphic_entry Univ.ContextSet.empty, UnivNames.empty_binders |
| 105 |
let empty_univ_entry poly = if poly then empty_poly_univ_entry else empty_mono_univ_entry |
| 106 |
|
| 107 |
let clear_univs scope univ = |
| 108 |
match scope, univ with |
| 109 |
| Locality.Global _, (UState.Polymorphic_entry _, _ as univs) -> univs |
| 110 |
| _, (UState.Monomorphic_entry _, _) -> empty_univ_entry false |
| 111 |
| Locality.Discharge, (UState.Polymorphic_entry _, _) -> empty_univ_entry true |
| 112 |
|
| 113 |
let context_subst subst (id,b,t,infos) = |
| 114 |
id, Option.map (Vars.replace_vars subst) b, Vars.replace_vars subst t, infos |
| 115 |
|
| 116 |
let declare_context ~try_global_assum_as_instance ~scope ~univs ?user_warns ~inline ctx = |
| 117 |
let fn i subst d = |
| 118 |
let (name,b,t,(impl,kind,coe,impargs)) = context_subst subst d in |
| 119 |
let univs = if i = 0 then univs else clear_univs scope univs in |
| 120 |
let refu = match scope with |
| 121 |
| Locality.Discharge -> declare_local ~coe ~try_assum_as_instance:true ~kind ~univs ~impargs ~impl ~name:name.CAst.v b t |
| 122 |
| Locality.Global local -> declare_global ~coe ~try_assum_as_instance:try_global_assum_as_instance ~local ~kind ?user_warns ~univs ~impargs ~inline ~name b t in |
| 123 |
(name.v, Constr.mkRef refu) :: subst |
| 124 |
in |
| 125 |
let _ = List.fold_left_i fn 0 [] ctx in |
| 126 |
() |
| 127 |
|
| 128 |
let error_extra_universe_decl ?loc () = |
| 129 |
user_err ?loc |
| 130 |
Pp.(strbrk "When declaring multiple assumptions in one command, " ++ |
| 131 |
strbrk "only the first name is allowed to mention a universe binder " ++ |
| 132 |
strbrk "(which will be shared by the whole block).") |
| 133 |
|
| 134 |
let extract_assumption_names = function |
| 135 |
| ({CAst.loc;v=id}, Some _) -> error_extra_universe_decl ?loc () |
| 136 |
| (id, None) -> id |
| 137 |
|
| 138 |
let process_assumptions_udecls = function |
| 139 |
| (coe, ((id, udecl)::ids, c))::assums -> |
| 140 |
let ids = List.map extract_assumption_names ids in |
| 141 |
let assums = List.map (fun (coe, (idl, c)) -> (coe, (List.map extract_assumption_names idl, c))) assums in |
| 142 |
udecl, (coe,(id::ids,c))::assums |
| 143 |
| (_, ([], _))::_ | [] -> assert false |
| 144 |
|
| 145 |
let error_polymorphic_section_variable ?loc () = |
| 146 |
user_err ?loc (Pp.str "Section variables cannot be polymorphic.") |
| 147 |
|
| 148 |
let process_assumptions_no_udecls l = |
| 149 |
List.map (fun (coe, (ids, c)) -> |
| 150 |
(coe, (List.map (function |
| 151 |
| ({CAst.loc}, Some _) -> error_polymorphic_section_variable ?loc () |
| 152 |
| (id, None) -> id) ids, c))) l |
| 153 |
|
| 154 |
let extract_manual_implicit e = |
| 155 |
CAst.make (match e with |
| 156 |
| Some {impl_pos = (na,_,_); impl_expl = Manual; impl_max = max} -> Some (na,max) |
| 157 |
| Some {impl_expl = (DepFlexAndRigid _ | DepFlex _ | DepRigid _ )} | None -> None) |
| 158 |
|
| 159 |
let find_implicits id ienv = |
| 160 |
try |
| 161 |
let impls = implicits_of_decl_in_internalization_env id ienv in |
| 162 |
List.map extract_manual_implicit impls |
| 163 |
with Not_found -> [] |
| 164 |
|
| 165 |
let local_binders_of_decls ~poly l = |
| 166 |
let coercions, l = |
| 167 |
List.fold_left_map (fun coercions (is_coe,(idl,c)) -> |
| 168 |
let coercions = match is_coe with |
| 169 |
| Vernacexpr.NoCoercion -> coercions |
| 170 |
| Vernacexpr.AddCoercion -> List.fold_right (fun id -> Id.Set.add id.CAst.v) idl coercions in |
| 171 |
let make_name id = CAst.make ?loc:id.CAst.loc (Name id.CAst.v) in |
| 172 |
let make_assum idl = Constrexpr.(CLocalAssum (List.map make_name idl,None,Default Glob_term.Explicit,c)) in |
| 173 |
let decl = if PolyFlags.univ_poly poly then |
| 174 |
|
| 175 |
List.map (fun id -> make_assum [id]) idl |
| 176 |
else |
| 177 |
[make_assum idl] in |
| 178 |
(coercions,decl)) Id.Set.empty l in |
| 179 |
coercions, List.flatten l |
| 180 |
|
| 181 |
let find_binding_kind id impls = |
| 182 |
let open Glob_term in |
| 183 |
let find x = match x.CAst.v with |
| 184 |
| Some (Name id',max) when Id.equal id id' -> Some (if max then MaxImplicit else NonMaxImplicit) |
| 185 |
| _ -> None in |
| 186 |
Option.default Explicit (CList.find_map find impls) |
| 187 |
|
| 188 |
let interp_context_gen ~program_mode ~poly ~kind ~autoimp_enable ~coercions env sigma l = |
| 189 |
let initial = sigma in |
| 190 |
let sigma, (ienv, ((env, ctx), impls, locs)) = interp_named_context_evars ~program_mode ~poly ~autoimp_enable env sigma l in |
| 191 |
|
| 192 |
let sigma = solve_remaining_evars all_and_fail_flags env ~initial sigma in |
| 193 |
let sigma, ctx = Evarutil.finalize sigma @@ fun nf -> |
| 194 |
List.map (NamedDecl.map_constr_het (fun x -> x) nf) ctx |
| 195 |
in |
| 196 |
|
| 197 |
let ctx = List.map2 (fun loc d -> |
| 198 |
let {binder_name=id}, b, t = NamedDecl.to_tuple d in |
| 199 |
let impl = find_binding_kind id impls in |
| 200 |
let kind = Decls.(if b = None then IsAssumption kind else IsDefinition (match kind with Context -> LetContext | _ -> Let)) in |
| 201 |
let is_coe = if Id.Set.mem id coercions then Vernacexpr.AddCoercion else Vernacexpr.NoCoercion in |
| 202 |
let impls = if autoimp_enable then find_implicits id ienv else [] in |
| 203 |
let data = (impl,kind,is_coe,impls) in |
| 204 |
(CAst.make ?loc id,b,t,data)) |
| 205 |
locs ctx |
| 206 |
in |
| 207 |
sigma, List.rev ctx |
| 208 |
|
| 209 |
let do_assumptions ~program_mode ~poly ~scope ~kind ?user_warns ~inline l = |
| 210 |
let sec = Lib.sections_are_opened () in |
| 211 |
if Dumpglob.dump () then begin |
| 212 |
List.iter (fun (_,(idl,_)) -> |
| 213 |
List.iter (fun (lid, _) -> |
| 214 |
let ty = if sec then "var" else "ax" in |
| 215 |
Dumpglob.dump_definition lid sec ty) idl) l end; |
| 216 |
let env = Global.env () in |
| 217 |
let udecl, l = match scope with |
| 218 |
| Locality.Global import_behavior -> process_assumptions_udecls l |
| 219 |
| Locality.Discharge -> None, process_assumptions_no_udecls l in |
| 220 |
let sigma, udecl = interp_univ_decl_opt env udecl in |
| 221 |
let coercions, ctx = local_binders_of_decls ~poly l in |
| 222 |
let sigma, ctx = interp_context_gen ~program_mode ~poly ~kind ~autoimp_enable:true ~coercions env sigma ctx in |
| 223 |
let univs = Evd.check_univ_decl ~poly sigma udecl in |
| 224 |
declare_context ~try_global_assum_as_instance:false ~scope ~univs ?user_warns ~inline ctx |
| 225 |
|
| 226 |
let warn_context_outside_section = |
| 227 |
CWarnings.create ~name:"context-outside-section" |
| 228 |
~category:CWarnings.CoreCategories.vernacular |
| 229 |
~default:CWarnings.AsError |
| 230 |
Pp.(fun () -> strbrk "Use of \"Context\" outside sections behaves \ |
| 231 |
as \"#[local] Parameter\" or \"#[local] \ |
| 232 |
Axiom\" followed by \"Existing Instance\" \ |
| 233 |
for typeclasses.") |
| 234 |
|
| 235 |
let do_context ~program_mode ~poly ctx = |
| 236 |
let sec = Lib.sections_are_opened () in |
| 237 |
if not sec then warn_context_outside_section (); |
| 238 |
if Dumpglob.dump () then begin |
| 239 |
let l = List.map (function |
| 240 |
| Constrexpr.CLocalAssum (l, _, _, _) -> |
| 241 |
let ty = if sec then "var "else "ax" in List.map (fun n -> ty, n) l |
| 242 |
| Constrexpr.CLocalDef (n, _, _, _) -> |
| 243 |
let ty = if sec then "var "else "def" in [ty, n] |
| 244 |
| Constrexpr.CLocalPattern _ -> []) |
| 245 |
ctx in |
| 246 |
List.iter (function |
| 247 |
| ty, {CAst.v = Names.Name.Anonymous; _} -> () |
| 248 |
| ty, {CAst.v = Names.Name.Name id; loc} -> |
| 249 |
Dumpglob.dump_definition (CAst.make ?loc id) sec ty) |
| 250 |
(List.flatten l) end; |
| 251 |
let env = Global.env() in |
| 252 |
let sigma = Evd.from_env env in |
| 253 |
let scope = |
| 254 |
let open Locality in |
| 255 |
if sec then Discharge |
| 256 |
else Global (if Lib.is_modtype () then ImportDefaultBehavior else ImportNeedQualified) |
| 257 |
in |
| 258 |
let sigma, ctx = interp_context_gen ~program_mode ~poly ~kind:Context ~autoimp_enable:false ~coercions:Id.Set.empty env sigma ctx in |
| 259 |
let univs = Evd.univ_entry ~poly sigma in |
| 260 |
declare_context ~try_global_assum_as_instance:true ~scope ~univs ~inline:Declaremods.NoInline ctx |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
let interp_context env sigma ctx = |
| 265 |
let reverse_rel_context_of_reverse_named_context ctx = |
| 266 |
List.rev (snd (List.fold_left_i (fun n (subst, ctx) (id,b,t,impl) -> |
| 267 |
let decl = (id, Option.map (Vars.subst_vars subst) b, Vars.subst_vars subst t, impl) in |
| 268 |
(id :: subst, decl :: ctx)) 1 ([],[]) ctx)) in |
| 269 |
let poly = PolyFlags.default in |
| 270 |
let sigma, ctx = interp_context_gen ~program_mode:false ~poly ~kind:Context ~autoimp_enable:false ~coercions:Id.Set.empty env sigma ctx in |
| 271 |
let ctx = List.map (fun (id,b,t,(impl,_,_,_)) -> (id.CAst.v,b,t,impl)) ctx in |
| 272 |
sigma, reverse_rel_context_of_reverse_named_context ctx |