| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open EConstr |
| 13 |
open CErrors |
| 14 |
open Util |
| 15 |
open Typeclasses_errors |
| 16 |
open Typeclasses |
| 17 |
open Libnames |
| 18 |
open Globnames |
| 19 |
open Constrintern |
| 20 |
open Constrexpr |
| 21 |
open Context.Rel.Declaration |
| 22 |
open Class_tactics |
| 23 |
open Libobject |
| 24 |
|
| 25 |
module RelDecl = Context.Rel.Declaration |
| 26 |
|
| 27 |
let warn_default_mode = CWarnings.create ~name:"class-declaration-default-mode" ~category:CWarnings.CoreCategories.automation |
| 28 |
~default:CWarnings.Disabled |
| 29 |
Pp.(fun (gr, m) -> hov 2 (str "Using an inferred default mode: " ++ prlist_with_sep spc Hints.pp_hint_mode m ++ |
| 30 |
spc () ++ str "for" ++ spc () ++ Printer.pr_global gr)) |
| 31 |
|
| 32 |
let set_typeclass_transparency ?typeclasses_db ~locality c b = |
| 33 |
let db_name = match typeclasses_db with |
| 34 |
| None -> Class_tactics.typeclasses_db |
| 35 |
| Some s -> s |
| 36 |
in |
| 37 |
let () = check_typeclasses_db () in |
| 38 |
Hints.add_hints ~locality [db_name] |
| 39 |
(Hints.HintsTransparencyEntry (Hints.HintsReferences c, b)) |
| 40 |
|
| 41 |
let set_typeclass_transparency_com ~locality refs b = |
| 42 |
let refs = List.map |
| 43 |
(fun x -> Tacred.evaluable_of_global_reference |
| 44 |
(Global.env ()) |
| 45 |
(Smartlocate.global_with_alias x)) |
| 46 |
refs |
| 47 |
in |
| 48 |
set_typeclass_transparency ~locality refs b |
| 49 |
|
| 50 |
let set_typeclass_mode ?(typeclasses_db=typeclasses_db) ~locality c b = |
| 51 |
let () = check_typeclasses_db () in |
| 52 |
Hints.add_hints ~locality [typeclasses_db] |
| 53 |
(Hints.HintsModeEntry (c, b)) |
| 54 |
|
| 55 |
let add_instance_hint ?(typeclasses_db=typeclasses_db) gr ~locality info = |
| 56 |
let () = check_typeclasses_db () in |
| 57 |
Flags.silently (fun () -> |
| 58 |
Hints.add_hints ~locality [typeclasses_db] |
| 59 |
(Hints.HintsResolveEntry |
| 60 |
[info, false, gr])) () |
| 61 |
|
| 62 |
|
| 63 |
type locality = Hints.hint_locality = Local | Export | SuperGlobal |
| 64 |
|
| 65 |
type instance = { |
| 66 |
class_name : GlobRef.t; |
| 67 |
instance : GlobRef.t; |
| 68 |
info : Typeclasses.hint_info; |
| 69 |
|
| 70 |
|
| 71 |
locality : Hints.hint_locality; |
| 72 |
} |
| 73 |
|
| 74 |
let add_instance_base inst = |
| 75 |
let locality = |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if Global.sections_are_opened () then Local |
| 80 |
else inst.locality |
| 81 |
in |
| 82 |
add_instance_hint inst.instance ~locality inst.info |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
let perform_instance i = |
| 88 |
let i = { is_class = i.class_name; is_info = i.info; is_impl = i.instance } in |
| 89 |
Typeclasses.load_instance (Global.env ()) i |
| 90 |
|
| 91 |
let cache_instance inst = |
| 92 |
perform_instance inst; |
| 93 |
add_instance_base inst |
| 94 |
|
| 95 |
let load_instance _ inst = match inst.locality with |
| 96 |
| Local -> assert false |
| 97 |
| SuperGlobal -> perform_instance inst |
| 98 |
| Export -> () |
| 99 |
|
| 100 |
let open_instance i inst = match inst.locality with |
| 101 |
| Local -> assert false |
| 102 |
| SuperGlobal -> () |
| 103 |
| Export -> if Int.equal i 1 then perform_instance inst |
| 104 |
|
| 105 |
let subst_instance (subst, inst) = |
| 106 |
{ inst with |
| 107 |
class_name = fst (subst_global subst inst.class_name); |
| 108 |
instance = fst (subst_global subst inst.instance) } |
| 109 |
|
| 110 |
let discharge_instance inst = |
| 111 |
match inst.locality with |
| 112 |
| Local -> None |
| 113 |
| SuperGlobal | Export -> |
| 114 |
assert (not (isVarRef inst.instance)); |
| 115 |
Some inst |
| 116 |
|
| 117 |
let classify_instance inst = match inst.locality with |
| 118 |
| Local -> Dispose |
| 119 |
| SuperGlobal | Export -> Substitute |
| 120 |
|
| 121 |
let instance_input : instance -> obj = |
| 122 |
declare_object |
| 123 |
{ (default_object "type classes instances state") with |
| 124 |
cache_function = cache_instance; |
| 125 |
load_function = load_instance; |
| 126 |
|
| 127 |
open_function = filtered_open ~cat:Hints.hint_cat open_instance; |
| 128 |
classify_function = classify_instance; |
| 129 |
discharge_function = discharge_instance; |
| 130 |
subst_function = subst_instance } |
| 131 |
|
| 132 |
module Event = struct |
| 133 |
type t = |
| 134 |
| NewClass of typeclass |
| 135 |
| NewInstance of instance |
| 136 |
end |
| 137 |
|
| 138 |
type observer = string |
| 139 |
|
| 140 |
let observers = ref CString.Map.empty |
| 141 |
|
| 142 |
let active_observers = Summary.ref ~name:"active typeclass observers" [] |
| 143 |
|
| 144 |
let register_observer ~name ?(override=false) o = |
| 145 |
if not override && CString.Map.mem name !observers then |
| 146 |
CErrors.anomaly Pp.(str "Typeclass observer " ++ str name ++ str " already registered."); |
| 147 |
observers := CString.Map.add name o !observers; |
| 148 |
name |
| 149 |
|
| 150 |
let deactivate_observer name = |
| 151 |
active_observers := List.remove String.equal name !active_observers |
| 152 |
|
| 153 |
let activate_observer name = |
| 154 |
assert (CString.Map.mem name !observers); |
| 155 |
deactivate_observer name; |
| 156 |
active_observers := name :: !active_observers |
| 157 |
|
| 158 |
let observe event = |
| 159 |
List.iter (fun name -> (CString.Map.get name !observers) event) !active_observers |
| 160 |
|
| 161 |
let add_instance cl info global impl = |
| 162 |
let () = match global with |
| 163 |
| Local -> () |
| 164 |
| SuperGlobal -> |
| 165 |
if Lib.sections_are_opened () && isVarRef impl then |
| 166 |
CErrors.user_err (Pp.str "Cannot set Global an instance referring to a section variable.") |
| 167 |
| Export -> |
| 168 |
if Lib.sections_are_opened () && isVarRef impl then |
| 169 |
CErrors.user_err (Pp.str "The export attribute cannot be applied to an instance referring to a section variable.") |
| 170 |
in |
| 171 |
let i = { |
| 172 |
class_name = cl.cl_impl; |
| 173 |
info = info ; |
| 174 |
locality = global ; |
| 175 |
instance = impl; |
| 176 |
} in |
| 177 |
Lib.add_leaf (instance_input i); |
| 178 |
observe (Event.NewInstance { class_name = cl.cl_impl; instance = impl; info; locality = global }) |
| 179 |
|
| 180 |
let warning_not_a_class = |
| 181 |
CWarnings.create ~name:"not-a-class" (fun (n, ty) -> |
| 182 |
let env = Global.env () in |
| 183 |
let evd = Evd.from_env env in |
| 184 |
Pp.(str "Ignored instance declaration for “" |
| 185 |
++ Nametab.pr_global_env Id.Set.empty n |
| 186 |
++ str "”: “" |
| 187 |
++ Termops.Internal.print_constr_env env evd (EConstr.of_constr ty) |
| 188 |
++ str "” is not a class") |
| 189 |
) |
| 190 |
|
| 191 |
let declare_instance ?(warn = false) env sigma info local glob = |
| 192 |
let ty, _ = Typeops.type_of_global_in_context env glob in |
| 193 |
let info = Option.default {hint_priority = None; hint_pattern = None} info in |
| 194 |
match class_of_constr env sigma (EConstr.of_constr ty) with |
| 195 |
| Some (rels, ((tc,_), args) as _cl) -> |
| 196 |
add_instance tc info local glob |
| 197 |
| None -> if warn then warning_not_a_class (glob, ty) |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
let cache_class c = load_class (Global.env ()) c |
| 204 |
|
| 205 |
let subst_class (subst,cl) = |
| 206 |
let do_subst_con c = Mod_subst.subst_constant subst c |
| 207 |
and do_subst c = Mod_subst.subst_mps subst c |
| 208 |
and do_subst_gr gr = fst (subst_global subst gr) in |
| 209 |
let do_subst_ctx = List.Smart.map (RelDecl.map_constr do_subst) in |
| 210 |
let do_subst_meth m = |
| 211 |
let c = Option.Smart.map do_subst_con m.meth_const in |
| 212 |
if c == m.meth_const then m |
| 213 |
else |
| 214 |
{ |
| 215 |
meth_name = m.meth_name; |
| 216 |
meth_const = c; |
| 217 |
} |
| 218 |
in |
| 219 |
let do_subst_projs projs = List.Smart.map do_subst_meth projs in |
| 220 |
{ cl_univs = cl.cl_univs; |
| 221 |
cl_impl = do_subst_gr cl.cl_impl; |
| 222 |
cl_context = do_subst_ctx cl.cl_context; |
| 223 |
cl_trivial = cl.cl_trivial; |
| 224 |
cl_props = do_subst_ctx cl.cl_props; |
| 225 |
cl_projs = do_subst_projs cl.cl_projs; |
| 226 |
cl_strict = cl.cl_strict; |
| 227 |
cl_unique = cl.cl_unique } |
| 228 |
|
| 229 |
let discharge_class cl = |
| 230 |
try |
| 231 |
let info = Global.section_segment_of_reference cl.cl_impl in |
| 232 |
let info, _, cl_univs' = Cooking.lift_poly_univs info cl.cl_univs in |
| 233 |
let nprops = List.length cl.cl_props in |
| 234 |
let props, context = List.chop nprops (Discharge.cook_rel_context info (cl.cl_props @ cl.cl_context)) in |
| 235 |
let discharge_proj x = x in |
| 236 |
{ cl_univs = cl_univs'; |
| 237 |
cl_impl = cl.cl_impl; |
| 238 |
cl_context = context; |
| 239 |
cl_trivial = cl.cl_trivial; |
| 240 |
cl_props = props; |
| 241 |
cl_projs = List.Smart.map discharge_proj cl.cl_projs; |
| 242 |
cl_strict = cl.cl_strict; |
| 243 |
cl_unique = cl.cl_unique |
| 244 |
} |
| 245 |
with Not_found -> |
| 246 |
cl |
| 247 |
|
| 248 |
let class_input : typeclass -> obj = |
| 249 |
declare_object |
| 250 |
{ (default_object "type classes state") with |
| 251 |
cache_function = cache_class; |
| 252 |
load_function = (fun _ -> cache_class); |
| 253 |
classify_function = (fun x -> Substitute); |
| 254 |
discharge_function = (fun a -> Some (discharge_class a)); |
| 255 |
subst_function = subst_class; |
| 256 |
} |
| 257 |
|
| 258 |
let add_class cl = |
| 259 |
Lib.add_leaf (class_input cl); |
| 260 |
observe (Event.NewClass cl) |
| 261 |
|
| 262 |
let intern_info {hint_priority;hint_pattern} = |
| 263 |
let env = Global.env() in |
| 264 |
let sigma = Evd.from_env env in |
| 265 |
let hint_pattern = Option.map (Constrintern.interp_constr_pattern env sigma) hint_pattern in |
| 266 |
{hint_priority;hint_pattern} |
| 267 |
|
| 268 |
|
| 269 |
let existing_instance ?loc glob c info = |
| 270 |
let info = Option.default Hints.empty_hint_info info in |
| 271 |
let info = intern_info info in |
| 272 |
let env = Global.env() in |
| 273 |
let sigma = Evd.from_env env in |
| 274 |
let instance, _ = Typeops.type_of_global_in_context env c in |
| 275 |
let ctx, r = Term.decompose_prod_decls instance in |
| 276 |
match class_of_constr (Environ.push_rel_context ctx env) sigma (EConstr.of_constr r) with |
| 277 |
| Some (_, ((tc,u), _)) -> add_instance tc info glob c |
| 278 |
| None -> user_err ?loc (Pp.str "Constant does not build instances of a declared type class.") |
| 279 |
|
| 280 |
type typeclass_in_univs = { |
| 281 |
clu_impl : GlobRef.t; |
| 282 |
clu_univs : EInstance.t; |
| 283 |
clu_context : EConstr.rel_context; |
| 284 |
clu_trivial : bool; |
| 285 |
clu_isstruct : Structures.Structure.t option; |
| 286 |
clu_props : EConstr.rel_context; |
| 287 |
clu_projs : class_method list; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
let type_ctx_instance ~program_mode env sigma ctx inst subst = |
| 293 |
let open Vars in |
| 294 |
let rec aux (sigma, subst) l ctx = match ctx, l with |
| 295 |
| LocalAssum _ :: _, [] | [], _ :: _ -> assert false |
| 296 |
| LocalAssum (_,t) :: ctx, c :: l -> |
| 297 |
let t' = substl subst t in |
| 298 |
let (sigma, c') = |
| 299 |
interp_casted_constr_evars ~program_mode env sigma c t' |
| 300 |
in |
| 301 |
aux (sigma, c' :: subst) l ctx |
| 302 |
| LocalDef (_,b,_) :: ctx, l -> |
| 303 |
let c' = substl subst b in |
| 304 |
aux (sigma, c' :: subst) l ctx |
| 305 |
| [], [] -> sigma, subst |
| 306 |
in |
| 307 |
aux (sigma, subst) inst (List.rev ctx) |
| 308 |
|
| 309 |
let instance_hook info global ?hook cst = |
| 310 |
let info = intern_info info in |
| 311 |
let env = Global.env () in |
| 312 |
let sigma = Evd.from_env env in |
| 313 |
declare_instance env sigma (Some info) global cst; |
| 314 |
(match hook with Some h -> h cst | None -> ()) |
| 315 |
|
| 316 |
let declare_instance_constant iinfo global impargs ?hook (name:lident) udecl poly sigma term termtype = |
| 317 |
let kind = Decls.(IsDefinition Instance) in |
| 318 |
let cinfo = Declare.CInfo.make ?loc:name.loc ~name:name.v ~impargs ~typ:(Some termtype) () in |
| 319 |
let info = Declare.Info.make ~kind ~poly ~udecl () in |
| 320 |
let kn = Declare.declare_definition ~cinfo ~info ~opaque:false ~body:term sigma in |
| 321 |
instance_hook iinfo global ?hook kn |
| 322 |
|
| 323 |
let instance_type cl args = |
| 324 |
let lenpars = List.count is_local_assum cl.clu_context in |
| 325 |
let pars = List.firstn lenpars args in |
| 326 |
applist (mkRef (cl.clu_impl,cl.clu_univs), pars) |
| 327 |
|
| 328 |
let do_declare_instance sigma ~locality ~poly k ctx ctx' pri udecl impargs subst (name:lident) = |
| 329 |
let subst = List.fold_left2 |
| 330 |
(fun subst' s decl -> if is_local_assum decl then s :: subst' else subst') |
| 331 |
[] subst k.clu_context |
| 332 |
in |
| 333 |
let ty_constr = instance_type k subst in |
| 334 |
let termtype = it_mkProd_or_LetIn ty_constr (ctx' @ ctx) in |
| 335 |
let sigma, entry = Declare.prepare_parameter ~poly sigma ~udecl ~types:termtype in |
| 336 |
let cst = Declare.declare_constant ?loc:name.loc ~name:name.v |
| 337 |
~kind:Decls.(IsAssumption Logical) (Declare.ParameterEntry entry) in |
| 338 |
let cst = (GlobRef.ConstRef cst) in |
| 339 |
Impargs.maybe_declare_manual_implicits false cst impargs; |
| 340 |
instance_hook pri locality cst |
| 341 |
|
| 342 |
let declare_instance_program pm env sigma ~locality ~poly {CAst.v=name;loc} pri impargs udecl term termtype = |
| 343 |
let hook { Declare.Hook.S.scope; dref; _ } = |
| 344 |
let cst = match dref with GlobRef.ConstRef kn -> kn | _ -> assert false in |
| 345 |
let pri = intern_info pri in |
| 346 |
let env = Global.env () in |
| 347 |
let sigma = Evd.from_env env in |
| 348 |
declare_instance env sigma (Some pri) locality (GlobRef.ConstRef cst) |
| 349 |
in |
| 350 |
let obls, _, body, typ = RetrieveObl.retrieve_obligations env name sigma 0 term termtype in |
| 351 |
let hook = Declare.Hook.make hook in |
| 352 |
let uctx = Evd.ustate sigma in |
| 353 |
let kind = Decls.IsDefinition Decls.Instance in |
| 354 |
let cinfo = Declare.CInfo.make ?loc ~name ~typ ~impargs () in |
| 355 |
let info = Declare.Info.make ~udecl ~poly ~kind ~hook () in |
| 356 |
Declare.Obls.add_definition ~pm ~info ~cinfo ~opaque:false ~uctx ~body obls |
| 357 |
|
| 358 |
let declare_instance_open sigma ?hook ~tac ~locality ~poly (id:lident) pri impargs udecl ids term termtype = |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
let future_goals, sigma = Evd.pop_future_goals sigma in |
| 364 |
let gls = List.rev (Evd.FutureGoals.comb future_goals) in |
| 365 |
let sigma = Evd.push_future_goals sigma in |
| 366 |
let kind = Decls.(IsDefinition Instance) in |
| 367 |
let hook = Declare.Hook.(make (fun { S.dref ; _ } -> instance_hook pri locality ?hook dref)) in |
| 368 |
let info = Declare.Info.make ~hook ~kind ~udecl ~poly () in |
| 369 |
|
| 370 |
|
| 371 |
let termtype = Evarutil.nf_evar sigma termtype in |
| 372 |
let cinfo = Declare.CInfo.make ?loc:id.loc ~name:id.v ~impargs ~typ:termtype () in |
| 373 |
let lemma = Declare.Proof.start ~cinfo ~info sigma in |
| 374 |
|
| 375 |
let lemma = |
| 376 |
match term with |
| 377 |
| Some term -> |
| 378 |
let init_refine = |
| 379 |
Tacticals.tclTHENLIST [ |
| 380 |
Refine.refine ~typecheck:false (fun sigma -> sigma, term); |
| 381 |
Proofview.Unsafe.tclNEWGOALS (CList.map Proofview.with_empty_state gls); |
| 382 |
Tactics.reduce_after_refine; |
| 383 |
] |
| 384 |
in |
| 385 |
let lemma, _ = Declare.Proof.by (Global.env ()) init_refine lemma in |
| 386 |
lemma |
| 387 |
| None -> |
| 388 |
let lemma, _ = Declare.Proof.by (Global.env ()) (Tactics.auto_intros_tac ids) lemma in |
| 389 |
lemma |
| 390 |
in |
| 391 |
match tac with |
| 392 |
| Some tac -> |
| 393 |
let lemma, _ = Declare.Proof.by (Global.env ()) tac lemma in |
| 394 |
lemma |
| 395 |
| None -> |
| 396 |
lemma |
| 397 |
|
| 398 |
let instance_constructor cl args = |
| 399 |
match cl.clu_isstruct with |
| 400 |
| Some s -> |
| 401 |
applist (mkConstructUi ((s.name,cl.clu_univs), 1), args) |
| 402 |
| None -> |
| 403 |
List.last args |
| 404 |
|
| 405 |
let do_instance_subst_constructor_and_ty subst k ctx = |
| 406 |
let subst = |
| 407 |
List.fold_left2 (fun subst' s decl -> |
| 408 |
if is_local_assum decl then s :: subst' else subst') |
| 409 |
[] subst (k.clu_props @ k.clu_context) |
| 410 |
in |
| 411 |
let ty_constr = instance_type k subst in |
| 412 |
let app = instance_constructor k subst in |
| 413 |
let termtype = it_mkProd_or_LetIn ty_constr ctx in |
| 414 |
let term = it_mkLambda_or_LetIn app ctx in |
| 415 |
term, termtype |
| 416 |
|
| 417 |
let do_instance_resolve_TC ~poly termtype sigma env = |
| 418 |
let sigma = Evarutil.nf_evar_map sigma in |
| 419 |
let sigma = Typeclasses.resolve_typeclasses ~filter:Typeclasses.no_goals_or_obligations ~fail:true env sigma in |
| 420 |
|
| 421 |
let sigma = Typeclasses.resolve_typeclasses ~filter:Typeclasses.all_evars ~fail:false env sigma in |
| 422 |
let sigma = Evarutil.nf_evar_map_undefined sigma in |
| 423 |
|
| 424 |
let sigma = Evd.minimize_universes sigma in |
| 425 |
|
| 426 |
Pretyping.check_evars env sigma termtype; |
| 427 |
termtype, sigma |
| 428 |
|
| 429 |
let do_instance_type_ctx_instance props k env' ctx' sigma ~program_mode subst = |
| 430 |
let get_id qid = CAst.make ?loc:qid.CAst.loc @@ qualid_basename qid in |
| 431 |
let props, rest = |
| 432 |
List.fold_left |
| 433 |
(fun (props, rest) decl -> |
| 434 |
if is_local_assum decl then |
| 435 |
try |
| 436 |
let is_id (id', _) = match RelDecl.get_name decl, get_id id' with |
| 437 |
| Name id, {CAst.v=id'} -> Id.equal id id' |
| 438 |
| Anonymous, _ -> false |
| 439 |
in |
| 440 |
let (loc_mid, c) = List.find is_id rest in |
| 441 |
let rest' = List.filter (fun v -> not (is_id v)) rest |
| 442 |
in |
| 443 |
let {CAst.loc;v=mid} = get_id loc_mid in |
| 444 |
let opt_proj = List.find_opt (fun m -> Name.equal m.meth_name (Name mid)) k.clu_projs in |
| 445 |
opt_proj |> Option.iter (fun x -> |
| 446 |
x.meth_const |> Option.iter (fun x -> Dumpglob.add_glob ?loc (GlobRef.ConstRef x))); |
| 447 |
c :: props, rest' |
| 448 |
with Not_found -> |
| 449 |
((CAst.make @@ CHole (None)) :: props), rest |
| 450 |
else props, rest) |
| 451 |
([], props) k.clu_props |
| 452 |
in |
| 453 |
match rest with |
| 454 |
| (n, _) :: _ -> |
| 455 |
unbound_method env' sigma k.clu_impl (get_id n) |
| 456 |
| [] -> |
| 457 |
let sigma, res = |
| 458 |
type_ctx_instance ~program_mode |
| 459 |
(push_rel_context ctx' env') sigma k.clu_props props subst in |
| 460 |
res, sigma |
| 461 |
|
| 462 |
let interp_props ~program_mode env' cty k ctx ctx' subst sigma = function |
| 463 |
| (true, { CAst.v = CRecord fs; loc }) -> |
| 464 |
check_duplicate ?loc fs; |
| 465 |
let subst, sigma = do_instance_type_ctx_instance fs k env' ctx' sigma ~program_mode subst in |
| 466 |
let term, termtype = |
| 467 |
do_instance_subst_constructor_and_ty subst k (ctx' @ ctx) in |
| 468 |
term, termtype, sigma |
| 469 |
| (_, term) -> |
| 470 |
let sigma, def = |
| 471 |
interp_casted_constr_evars ~program_mode env' sigma term cty in |
| 472 |
let termtype = it_mkProd_or_LetIn cty ctx in |
| 473 |
let term = it_mkLambda_or_LetIn def ctx in |
| 474 |
term, termtype, sigma |
| 475 |
|
| 476 |
let do_instance_interactive env env' sigma ?hook ~tac ~locality ~poly cty k ctx ctx' pri decl imps subst id opt_props = |
| 477 |
let term, termtype, sigma = match opt_props with |
| 478 |
| Some props -> |
| 479 |
on_pi1 (fun x -> Some x) |
| 480 |
(interp_props ~program_mode:false env' cty k ctx ctx' subst sigma props) |
| 481 |
| None -> |
| 482 |
let term, termtype = |
| 483 |
if k.clu_trivial then |
| 484 |
let term, termtype = |
| 485 |
do_instance_subst_constructor_and_ty subst k (ctx' @ ctx) in |
| 486 |
Some term, termtype |
| 487 |
else |
| 488 |
None, it_mkProd_or_LetIn cty ctx |
| 489 |
in |
| 490 |
let termtype, sigma = do_instance_resolve_TC ~poly termtype sigma env in |
| 491 |
term, termtype, sigma |
| 492 |
in |
| 493 |
Flags.silently (fun () -> |
| 494 |
declare_instance_open sigma ?hook ~tac ~locality ~poly |
| 495 |
id pri imps decl (List.map RelDecl.get_name ctx) term termtype) |
| 496 |
() |
| 497 |
|
| 498 |
let do_instance env env' sigma ?hook ~locality ~poly cty k ctx ctx' pri decl imps subst id props = |
| 499 |
let term, termtype, sigma = |
| 500 |
interp_props ~program_mode:false env' cty k ctx ctx' subst sigma props |
| 501 |
in |
| 502 |
let termtype, sigma = do_instance_resolve_TC ~poly termtype sigma env in |
| 503 |
Pretyping.check_evars_are_solved ~program_mode:false env sigma; |
| 504 |
declare_instance_constant pri locality imps ?hook id decl poly sigma term termtype |
| 505 |
|
| 506 |
let do_instance_program ~pm env env' sigma ?hook ~locality ~poly cty k ctx ctx' pri decl imps subst id opt_props = |
| 507 |
let term, termtype, sigma = |
| 508 |
match opt_props with |
| 509 |
| Some props -> |
| 510 |
interp_props ~program_mode:true env' cty k ctx ctx' subst sigma props |
| 511 |
| None -> |
| 512 |
let subst, sigma = |
| 513 |
do_instance_type_ctx_instance [] k env' ctx' sigma ~program_mode:true subst in |
| 514 |
let term, termtype = |
| 515 |
do_instance_subst_constructor_and_ty subst k (ctx' @ ctx) in |
| 516 |
term, termtype, sigma in |
| 517 |
let termtype, sigma = do_instance_resolve_TC ~poly termtype sigma env in |
| 518 |
if not (Evd.has_undefined sigma) && not (Option.is_empty opt_props) then |
| 519 |
let () = declare_instance_constant pri locality imps ?hook id decl poly |
| 520 |
sigma term termtype in |
| 521 |
pm |
| 522 |
else |
| 523 |
declare_instance_program pm env sigma ~locality ~poly id pri imps decl term termtype |
| 524 |
|
| 525 |
let typeclass_univ_instance env (cl, u) = |
| 526 |
assert (UVars.eq_sizes (UVars.AbstractContext.size cl.cl_univs) (EInstance.length u)); |
| 527 |
let subst_ctx c = Context.Rel.map (Vars.subst_instance_constr u) (EConstr.of_rel_context c) in |
| 528 |
let clu_isstruct = match cl.cl_impl with |
| 529 |
| ConstRef _ -> None |
| 530 |
| ConstructRef _ | VarRef _ -> assert false |
| 531 |
| IndRef ind -> match Structures.Structure.find env ind with |
| 532 |
| exception Not_found -> None |
| 533 |
| s -> Some s |
| 534 |
in |
| 535 |
{ clu_impl = cl.cl_impl; |
| 536 |
clu_univs = u; |
| 537 |
clu_context = subst_ctx cl.cl_context; |
| 538 |
clu_props = subst_ctx cl.cl_props; |
| 539 |
clu_isstruct; |
| 540 |
clu_trivial = cl.cl_trivial; |
| 541 |
clu_projs = cl.cl_projs; |
| 542 |
} |
| 543 |
|
| 544 |
let interp_instance_context ~program_mode ~poly env ctx pl tclass = |
| 545 |
let sigma, decl = interp_univ_decl_opt env pl in |
| 546 |
let sigma, (impls, ((env', ctx), imps, _locs)) = interp_context_evars ~program_mode ~poly env sigma ctx in |
| 547 |
let flags = Pretyping.{ all_no_fail_flags with program_mode ; poly } in |
| 548 |
let sigma, (c', imps') = interp_type_evars_impls ~flags ~impls env' sigma tclass in |
| 549 |
let imps = imps @ imps' in |
| 550 |
let ctx', c = decompose_prod_decls sigma c' in |
| 551 |
let ctx'' = ctx' @ ctx in |
| 552 |
let (k, u), args = Typeclasses.dest_class_app (push_rel_context ctx'' env) sigma c in |
| 553 |
let cl = typeclass_univ_instance env (k, u) in |
| 554 |
let args = List.map of_constr args in |
| 555 |
let _, args = |
| 556 |
List.fold_right (fun decl (args, args') -> |
| 557 |
match decl with |
| 558 |
| LocalAssum _ -> (List.tl args, List.hd args :: args') |
| 559 |
| LocalDef (_,b,_) -> (args, Vars.substl args' b :: args')) |
| 560 |
cl.clu_context (args, []) |
| 561 |
in |
| 562 |
let sigma = Evarutil.nf_evar_map sigma in |
| 563 |
let sigma = resolve_typeclasses ~filter:Typeclasses.all_evars ~fail:true env sigma in |
| 564 |
sigma, cl, u, c', ctx', ctx, imps, args, decl |
| 565 |
|
| 566 |
let id_of_class env ref = |
| 567 |
let open GlobRef in |
| 568 |
match ref with |
| 569 |
| ConstRef kn -> Constant.label kn |
| 570 |
| IndRef (kn,i) -> |
| 571 |
let mip = (Environ.lookup_mind kn env).Declarations.mind_packets in |
| 572 |
mip.(0).Declarations.mind_typename |
| 573 |
| _ -> assert false |
| 574 |
|
| 575 |
let new_instance_common ~program_mode ~poly env instid ctx cl = |
| 576 |
let (instid, pl) = instid in |
| 577 |
let sigma, k, u, cty, ctx', ctx, imps, subst, decl = |
| 578 |
interp_instance_context ~program_mode ~poly env ctx pl cl |
| 579 |
in |
| 580 |
|
| 581 |
let id = instid |> CAst.map (function |
| 582 |
| Name id -> id |
| 583 |
| Anonymous -> |
| 584 |
let i = Nameops.add_suffix (id_of_class env k.clu_impl) "_instance_0" in |
| 585 |
Namegen.next_global_ident_away (Global.safe_env ()) i (Termops.vars_of_env env)) |
| 586 |
in |
| 587 |
let env' = push_rel_context ctx env in |
| 588 |
id, env', sigma, k, u, cty, ctx', ctx, imps, subst, decl |
| 589 |
|
| 590 |
let new_instance_interactive ~locality ~poly instid ctx cl |
| 591 |
?(tac:unit Proofview.tactic option) ?hook |
| 592 |
pri opt_props = |
| 593 |
let env = Global.env() in |
| 594 |
let id, env', sigma, k, u, cty, ctx', ctx, imps, subst, decl = |
| 595 |
new_instance_common ~program_mode:false ~poly env instid ctx cl in |
| 596 |
id, do_instance_interactive env env' sigma ?hook ~tac ~locality ~poly |
| 597 |
cty k ctx ctx' pri decl imps subst id opt_props |
| 598 |
|
| 599 |
let new_instance_program ~locality ~pm ~poly instid ctx cl opt_props ?hook pri = |
| 600 |
let env = Global.env() in |
| 601 |
let id, env', sigma, k, u, cty, ctx', ctx, imps, subst, decl = |
| 602 |
new_instance_common ~program_mode:true ~poly env instid ctx cl in |
| 603 |
let pm = |
| 604 |
do_instance_program ~pm env env' sigma ?hook ~locality ~poly |
| 605 |
cty k ctx ctx' pri decl imps subst id opt_props in |
| 606 |
pm, id |
| 607 |
|
| 608 |
let new_instance ~locality ~poly instid ctx cl props ?hook pri = |
| 609 |
let env = Global.env() in |
| 610 |
let id, env', sigma, k, u, cty, ctx', ctx, imps, subst, decl = |
| 611 |
new_instance_common ~program_mode:false ~poly env instid ctx cl in |
| 612 |
do_instance env env' sigma ?hook ~locality ~poly |
| 613 |
cty k ctx ctx' pri decl imps subst id props; |
| 614 |
id |
| 615 |
|
| 616 |
let declare_new_instance ~locality ~program_mode ~poly instid ctx cl pri = |
| 617 |
let env = Global.env() in |
| 618 |
let (instid, pl) = instid in |
| 619 |
let sigma, k, u, cty, ctx', ctx, imps, subst, decl = |
| 620 |
interp_instance_context ~program_mode ~poly env ctx pl cl |
| 621 |
in |
| 622 |
do_declare_instance sigma ~locality ~poly k ctx ctx' pri decl imps subst instid |
| 623 |
|
| 624 |
let refine_att = |
| 625 |
let open Attributes in |
| 626 |
let open Notations in |
| 627 |
attribute_of_list ["refine",single_key_parser ~name:"refine" ~key:"refine" ()] >>= function |
| 628 |
| None -> return false |
| 629 |
| Some () -> return true |
| 630 |
|
| 631 |
module Internal = struct |
| 632 |
let add_instance = add_instance |
| 633 |
end |