| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open CErrors |
| 13 |
open Term |
| 14 |
open Util |
| 15 |
open Names |
| 16 |
open Constr |
| 17 |
open Context |
| 18 |
open Environ |
| 19 |
open Declarations |
| 20 |
open Entries |
| 21 |
open Type_errors |
| 22 |
open Constrexpr |
| 23 |
open Constrexpr_ops |
| 24 |
open Context.Rel.Declaration |
| 25 |
open Structures |
| 26 |
|
| 27 |
module RelDecl = Context.Rel.Declaration |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
let { Goptions.get = typeclasses_strict } = |
| 32 |
Goptions.declare_bool_option_and_ref |
| 33 |
~key:["Typeclasses";"Strict";"Resolution"] |
| 34 |
~value:false |
| 35 |
() |
| 36 |
|
| 37 |
let { Goptions.get = typeclasses_unique } = |
| 38 |
Goptions.declare_bool_option_and_ref |
| 39 |
~key:["Typeclasses";"Unique";"Instances"] |
| 40 |
~value:false |
| 41 |
() |
| 42 |
|
| 43 |
let { Goptions.get = typeclasses_default_mode } = |
| 44 |
Goptions.declare_interpreted_string_option_and_ref Hints.parse_mode Hints.string_of_mode |
| 45 |
~key:["Typeclasses";"Default";"Mode"] |
| 46 |
~value:Hints.ModeOutput |
| 47 |
() |
| 48 |
|
| 49 |
let interp_fields_evars ~poly env sigma ~ninds ~nparams impls_env nots l = |
| 50 |
let _, sigma, impls, locs, newfs, _ = |
| 51 |
List.fold_left2 |
| 52 |
(fun (env, sigma, uimpls, locs, params, impls_env) no d -> |
| 53 |
let sigma, (i, b, t), impl, loc = match d with |
| 54 |
| Vernacexpr.AssumExpr({CAst.v=id; loc},bl,t) -> |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
let t = if bl = [] then t else mkCProdN bl t in |
| 59 |
let sigma, t, impl = |
| 60 |
ComAssumption.interp_assumption ~program_mode:false ~poly env sigma impls_env [] t in |
| 61 |
sigma, (id, None, t), impl, loc |
| 62 |
| Vernacexpr.DefExpr({CAst.v=id; loc},bl,b,t) -> |
| 63 |
let sigma, (b, t), impl = |
| 64 |
ComDefinition.interp_definition ~program_mode:false ~poly env sigma impls_env bl None b t in |
| 65 |
let t = match t with Some t -> t | None -> Retyping.get_type_of env sigma b in |
| 66 |
sigma, (id, Some b, t), impl, loc |
| 67 |
in |
| 68 |
let r = Retyping.relevance_of_type env sigma t in |
| 69 |
let impls_env = |
| 70 |
match i with |
| 71 |
| Anonymous -> impls_env |
| 72 |
| Name id -> |
| 73 |
Id.Map.add id (Constrintern.compute_internalization_data env sigma id Constrintern.Method t impl) impls_env |
| 74 |
in |
| 75 |
let d = match b with |
| 76 |
| None -> LocalAssum (make_annot i r,t) |
| 77 |
| Some b -> LocalDef (make_annot i r,b,t) |
| 78 |
in |
| 79 |
List.iter (Metasyntax.set_notation_for_interpretation env impls_env) no; |
| 80 |
(EConstr.push_rel d env, sigma, impl :: uimpls, loc :: locs, d::params, impls_env)) |
| 81 |
(env, sigma, [], [], [], impls_env) nots l |
| 82 |
in |
| 83 |
let _, _, sigma = Context.Rel.fold_outside ~init:(env,0,sigma) (fun f (env,k,sigma) -> |
| 84 |
let sigma = RelDecl.fold_constr (fun c sigma -> |
| 85 |
ComInductive.maybe_unify_params_in env sigma ~ninds ~nparams ~binders:k c) |
| 86 |
f sigma |
| 87 |
in |
| 88 |
EConstr.push_rel f env, k+1, sigma) |
| 89 |
newfs |
| 90 |
in |
| 91 |
sigma, (impls, locs, newfs) |
| 92 |
|
| 93 |
let check_anonymous_type ind = |
| 94 |
match ind with |
| 95 |
| { CAst.v = CSort s } -> Constrexpr_ops.(sort_expr_eq expr_Type_sort s) |
| 96 |
| _ -> false |
| 97 |
|
| 98 |
let error_parameters_must_be_named bk {CAst.loc; v=name} = |
| 99 |
match bk, name with |
| 100 |
| Default _, Anonymous -> |
| 101 |
CErrors.user_err ?loc (str "Record parameters must be named.") |
| 102 |
| _ -> () |
| 103 |
|
| 104 |
let check_parameters_must_be_named = function |
| 105 |
| CLocalDef (b, _, _, _) -> |
| 106 |
error_parameters_must_be_named default_binder_kind b |
| 107 |
| CLocalAssum (ls, _, bk, _ce) -> |
| 108 |
List.iter (error_parameters_must_be_named bk) ls |
| 109 |
| CLocalPattern {CAst.loc} -> |
| 110 |
Loc.raise ?loc (Gramlib.Grammar.ParseError "pattern with quote not allowed in record parameters") |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
module DataI = struct |
| 116 |
type t = |
| 117 |
{ name : lident |
| 118 |
; constructor_name : Id.t |
| 119 |
; arity : Constrexpr.constr_expr option |
| 120 |
|
| 121 |
; nots : Metasyntax.notation_interpretation_decl list list |
| 122 |
|
| 123 |
; fs : Vernacexpr.local_decl_expr list |
| 124 |
; default_inhabitant_id : Id.t option |
| 125 |
} |
| 126 |
end |
| 127 |
|
| 128 |
module Data = struct |
| 129 |
|
| 130 |
type coercion_flags = { |
| 131 |
coe_local : bool; |
| 132 |
coe_reversible : bool; |
| 133 |
} |
| 134 |
type instance_flags = { |
| 135 |
inst_locality : Hints.hint_locality; |
| 136 |
inst_priority : int option; |
| 137 |
} |
| 138 |
type projection_flags = { |
| 139 |
pf_coercion: coercion_flags option; |
| 140 |
pf_instance: instance_flags option; |
| 141 |
pf_canonical: bool; |
| 142 |
} |
| 143 |
type t = |
| 144 |
{ is_coercion : Vernacexpr.coercion_flag |
| 145 |
; proj_flags : projection_flags list |
| 146 |
} |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
let is_sort_variable sigma s = |
| 151 |
match EConstr.ESorts.kind sigma s with |
| 152 |
| SProp | Prop | Set -> None |
| 153 |
| Type u | QSort (_, u) -> match Univ.Universe.level u with |
| 154 |
| None -> None |
| 155 |
| Some l -> |
| 156 |
if Univ.Level.Set.mem l (fst (Evd.universe_context_set sigma)) |
| 157 |
then Some l |
| 158 |
else None |
| 159 |
|
| 160 |
let build_type_telescope ~unconstrained_sorts newps env0 sigma { DataI.arity; _ } = match arity with |
| 161 |
| None -> |
| 162 |
let sigma, s = Evd.new_sort_variable Evd.univ_flexible_alg sigma in |
| 163 |
sigma, (EConstr.mkSort s, s) |
| 164 |
| Some { CAst.v = CSort s; loc } when Constrexpr_ops.(sort_expr_eq expr_Type_sort s) -> |
| 165 |
|
| 166 |
|
| 167 |
let sigma, s = Evd.new_sort_variable ?loc UState.univ_flexible_alg sigma in |
| 168 |
sigma, (EConstr.mkSort s, s) |
| 169 |
| Some t -> |
| 170 |
let env = EConstr.push_rel_context newps env0 in |
| 171 |
let impls = Constrintern.empty_internalization_env in |
| 172 |
let sigma, s = |
| 173 |
let t = Constrintern.intern_gen IsType ~impls env sigma t in |
| 174 |
let flags = { Pretyping.all_no_fail_flags with program_mode = false; unconstrained_sorts } in |
| 175 |
Pretyping.understand_tcc ~flags env sigma ~expected_type:IsType t |
| 176 |
in |
| 177 |
let sred = Reductionops.whd_allnolet env sigma s in |
| 178 |
(match EConstr.kind sigma sred with |
| 179 |
| Sort s' -> (sigma, (s, s')) |
| 180 |
| _ -> user_err ?loc:(constr_loc t) (str"Sort expected.")) |
| 181 |
|
| 182 |
module DefClassEntry = struct |
| 183 |
|
| 184 |
type t = { |
| 185 |
univs : UState.named_universes_entry; |
| 186 |
name : lident; |
| 187 |
projname : lident; |
| 188 |
params : Constr.rel_context; |
| 189 |
sort : Sorts.t; |
| 190 |
typ : Constr.t; |
| 191 |
projtyp : Constr.t; |
| 192 |
inhabitant_id : Id.t; |
| 193 |
impls : Impargs.manual_implicits; |
| 194 |
projimpls : Impargs.manual_implicits; |
| 195 |
} |
| 196 |
|
| 197 |
end |
| 198 |
|
| 199 |
module RecordEntry = struct |
| 200 |
|
| 201 |
type one_ind_info = { |
| 202 |
|
| 203 |
inhabitant_id : Id.t; |
| 204 |
default_dep_elim : DeclareInd.default_dep_elim; |
| 205 |
|
| 206 |
implfs : Impargs.manual_implicits list; |
| 207 |
fieldlocs : Loc.t option list; |
| 208 |
} |
| 209 |
|
| 210 |
let make_ind_infos id elims implfs fieldlocs = |
| 211 |
{ inhabitant_id = id; |
| 212 |
default_dep_elim = elims; |
| 213 |
implfs; |
| 214 |
fieldlocs |
| 215 |
} |
| 216 |
|
| 217 |
type t = { |
| 218 |
global_univs : Univ.ContextSet.t; |
| 219 |
ubinders : UState.named_universes_entry; |
| 220 |
mie : Entries.mutual_inductive_entry; |
| 221 |
ind_infos : one_ind_info list; |
| 222 |
param_impls : Impargs.manual_implicits; |
| 223 |
} |
| 224 |
|
| 225 |
end |
| 226 |
|
| 227 |
type defclass_or_record = |
| 228 |
| DefclassEntry of DefClassEntry.t |
| 229 |
| RecordEntry of RecordEntry.t |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
let def_class_levels ~def ~env_ar_params sigma aritysorts ctors = |
| 234 |
let s, ctor = match aritysorts, ctors with |
| 235 |
| [s], [ctor] -> begin match ctor with |
| 236 |
| [LocalAssum (na,t)] -> s, t |
| 237 |
| _ -> assert false |
| 238 |
end |
| 239 |
| _ -> CErrors.user_err Pp.(str "Mutual definitional classes are not supported.") |
| 240 |
in |
| 241 |
let ctor_sort = Retyping.get_sort_of env_ar_params sigma ctor in |
| 242 |
let is_prop_ctor = EConstr.ESorts.is_prop sigma ctor_sort in |
| 243 |
let sigma = Evd.set_leq_sort sigma ctor_sort s in |
| 244 |
if Option.cata (Evd.is_flexible_level sigma) false (is_sort_variable sigma s) |
| 245 |
&& is_prop_ctor |
| 246 |
then |
| 247 |
|
| 248 |
let sigma = Evd.set_eq_sort sigma EConstr.ESorts.set s in |
| 249 |
sigma, EConstr.ESorts.prop, ctor |
| 250 |
else |
| 251 |
sigma, s, ctor |
| 252 |
|
| 253 |
let finalize_def_class env sigma ~params ~sort ~projtyp = |
| 254 |
let sigma, (params, sort, typ, projtyp) = |
| 255 |
Evarutil.finalize ~abort_on_undefined_evars:false sigma (fun nf -> |
| 256 |
let typ = EConstr.it_mkProd_or_LetIn (EConstr.mkSort sort) params in |
| 257 |
let typ = nf typ in |
| 258 |
|
| 259 |
let params, typ = Term.decompose_prod_decls typ in |
| 260 |
let projtyp = nf projtyp in |
| 261 |
let sort = destSort (nf (EConstr.mkSort sort)) in |
| 262 |
params, sort, typ, projtyp) |
| 263 |
in |
| 264 |
let ce t = Pretyping.check_evars env sigma (EConstr.of_constr t) in |
| 265 |
|
| 266 |
let () = Context.Rel.iter ce params in |
| 267 |
let () = ce projtyp in |
| 268 |
let () = |
| 269 |
if not (Vars.closedn (List.length params) projtyp) then |
| 270 |
CErrors.user_err Pp.(str "Definitional classes cannot be recursive.") |
| 271 |
in |
| 272 |
sigma, params, sort, typ, projtyp |
| 273 |
|
| 274 |
let adjust_field_implicits ~isclass (params,param_impls) (impls:Impargs.manual_implicits) = |
| 275 |
let main_arg = if isclass then Some (Anonymous, true) else None in |
| 276 |
let param_impls = if isclass then |
| 277 |
List.rev (List.filter_map (fun d -> |
| 278 |
if RelDecl.is_local_def d then None |
| 279 |
else Some (CAst.make (Some (RelDecl.get_name d, true)))) |
| 280 |
params) |
| 281 |
else param_impls |
| 282 |
in |
| 283 |
param_impls @ (CAst.make main_arg :: impls) |
| 284 |
|
| 285 |
type kind_class = NotClass | RecordClass | DefClass |
| 286 |
|
| 287 |
|
| 288 |
let canonical_inhabitant_id ~isclass ind_id = |
| 289 |
if isclass then ind_id |
| 290 |
else Id.of_string (Unicode.lowercase_first_char (Id.to_string ind_id)) |
| 291 |
|
| 292 |
|
| 293 |
let rec add_bound_names_constr (names : Id.Set.t) (t : constr) : Id.Set.t = |
| 294 |
match destProd t with |
| 295 |
| (b, _, t) -> |
| 296 |
let names = |
| 297 |
match b.binder_name with |
| 298 |
| Name.Anonymous -> names |
| 299 |
| Name.Name n -> Id.Set.add n names |
| 300 |
in add_bound_names_constr names t |
| 301 |
| exception DestKO -> names |
| 302 |
|
| 303 |
|
| 304 |
let bound_names_ind_entry (ind:Entries.one_inductive_entry) : Id.Set.t = |
| 305 |
let ctor = match ind.mind_entry_lc with |
| 306 |
| [ctor] -> ctor |
| 307 |
| _ -> assert false |
| 308 |
in |
| 309 |
let fields, _ = Term.decompose_prod_decls ctor in |
| 310 |
let add_names names field = add_bound_names_constr names (RelDecl.get_type field) in |
| 311 |
List.fold_left add_names Id.Set.empty fields |
| 312 |
|
| 313 |
let inhabitant_id ~isclass bound_names ind {DataI.default_inhabitant_id=id; name} = |
| 314 |
match id with |
| 315 |
| Some id -> id |
| 316 |
| None -> |
| 317 |
let canonical_inhabitant_id = canonical_inhabitant_id ~isclass name.v in |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
Namegen.next_ident_away canonical_inhabitant_id (bound_names ind) |
| 323 |
|
| 324 |
let fix_entry_record ~isclass ~primitive_proj records mie = |
| 325 |
let ids = List.map2 (inhabitant_id ~isclass bound_names_ind_entry) mie.mind_entry_inds records in |
| 326 |
if not primitive_proj then |
| 327 |
ids, { mie with mind_entry_record = Some None } |
| 328 |
else |
| 329 |
ids, { mie with mind_entry_record = Some (Some (Array.of_list ids)) } |
| 330 |
|
| 331 |
let typecheck_params_and_fields ~kind ~(flags:ComInductive.flags) ~primitive_proj udecl params (records : DataI.t list) = |
| 332 |
let def = kind = DefClass in |
| 333 |
let isclass = kind != NotClass in |
| 334 |
let env0 = Global.env () in |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
let is_template = |
| 339 |
List.exists (fun { DataI.arity; _} -> Option.cata check_anonymous_type true arity) records in |
| 340 |
let unconstrained_sorts = not (PolyFlags.univ_poly flags.poly) && not def && is_template in |
| 341 |
let sigma, udecl, variances = Constrintern.interp_cumul_univ_decl_opt env0 udecl in |
| 342 |
let () = List.iter check_parameters_must_be_named params in |
| 343 |
let sigma, (impls_env, ((_env1,params), impls, _paramlocs)) = |
| 344 |
Constrintern.interp_context_evars ~program_mode:false ~unconstrained_sorts env0 sigma params in |
| 345 |
let sigma, typs = |
| 346 |
List.fold_left_map (build_type_telescope ~unconstrained_sorts params env0) sigma records in |
| 347 |
let typs, aritysorts = List.split typs in |
| 348 |
let arities = List.map (fun typ -> EConstr.it_mkProd_or_LetIn typ params) typs in |
| 349 |
let relevances = List.map (fun s -> EConstr.ESorts.relevance_of_sort s) aritysorts in |
| 350 |
let fold accu { DataI.name; _ } arity r = |
| 351 |
EConstr.push_rel (LocalAssum (make_annot (Name name.v) r,arity)) accu in |
| 352 |
let env_ar_params = EConstr.push_rel_context params (List.fold_left3 fold env0 records arities relevances) in |
| 353 |
let impls_env = |
| 354 |
let ids = List.map (fun { DataI.name; _ } -> name.v) records in |
| 355 |
let impls = List.map (fun _ -> impls) arities in |
| 356 |
Constrintern.compute_internalization_env env0 sigma ~impls:impls_env Constrintern.Inductive ids arities impls |
| 357 |
in |
| 358 |
let ninds = List.length arities in |
| 359 |
let nparams = List.length params in |
| 360 |
let fold sigma { DataI.nots; fs; _ } = |
| 361 |
interp_fields_evars ~poly:flags.poly env_ar_params sigma ~ninds ~nparams impls_env nots fs |
| 362 |
in |
| 363 |
let (sigma, fields) = List.fold_left_map fold sigma records in |
| 364 |
let field_impls, locs, fields = List.split3 fields in |
| 365 |
let field_impls = List.map (List.map (adjust_field_implicits ~isclass (params,impls))) field_impls in |
| 366 |
let sigma = |
| 367 |
Pretyping.solve_remaining_evars Pretyping.all_and_fail_flags env_ar_params sigma in |
| 368 |
if def then |
| 369 |
|
| 370 |
let sigma, sort, projtyp = def_class_levels ~def ~env_ar_params sigma aritysorts fields in |
| 371 |
let sigma, params, sort, typ, projtyp = |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
finalize_def_class env_ar_params sigma ~params ~sort ~projtyp |
| 376 |
in |
| 377 |
let name, projname = match records with |
| 378 |
| [{name; fs=[AssumExpr (projname, _, _)]}] -> name, projname |
| 379 |
| _ -> assert false |
| 380 |
in |
| 381 |
let projname = CAst.map Nameops.Name.get_id projname in |
| 382 |
let univs = Evd.check_univ_decl ~poly:flags.poly sigma udecl in |
| 383 |
|
| 384 |
|
| 385 |
let projimpls = match field_impls with |
| 386 |
| [[x]] -> x |
| 387 |
| _ -> assert false |
| 388 |
in |
| 389 |
let inhabitant_id = |
| 390 |
inhabitant_id ~isclass (add_bound_names_constr Id.Set.empty) projtyp (List.hd records) |
| 391 |
in |
| 392 |
DefclassEntry { |
| 393 |
univs; |
| 394 |
name; |
| 395 |
projname; |
| 396 |
params; |
| 397 |
sort; |
| 398 |
typ; |
| 399 |
projtyp; |
| 400 |
inhabitant_id; |
| 401 |
impls; |
| 402 |
projimpls; |
| 403 |
} |
| 404 |
else |
| 405 |
|
| 406 |
let ninds = List.length arities in |
| 407 |
let nparams = List.length params in |
| 408 |
let constructors = List.map2_i (fun i record fields -> |
| 409 |
let open EConstr in |
| 410 |
let nfields = List.length fields in |
| 411 |
let ind_args = Context.Rel.instance_list mkRel nfields params in |
| 412 |
let ind = applist (mkRel (ninds - i + nparams + nfields), ind_args) in |
| 413 |
let ctor = it_mkProd_or_LetIn ind fields in |
| 414 |
[record.DataI.constructor_name], [ctor]) |
| 415 |
0 records fields |
| 416 |
in |
| 417 |
let indnames = List.map (fun x -> x.DataI.name.v) records in |
| 418 |
let arities_explicit = List.map (fun x -> Option.has_some x.DataI.arity) records in |
| 419 |
let template_syntax = List.map (fun typ -> |
| 420 |
if EConstr.isArity sigma typ then |
| 421 |
ComInductive.SyntaxAllowsTemplatePoly |
| 422 |
else ComInductive.SyntaxNoTemplatePoly) |
| 423 |
typs |
| 424 |
in |
| 425 |
let env_ar = Environ.pop_rel_context nparams env_ar_params in |
| 426 |
let default_dep_elim, mie, ubinders, global_univs = |
| 427 |
ComInductive.interp_mutual_inductive_constr ~sigma ~flags ~udecl ~variances |
| 428 |
~ctx_params:params ~indnames ~arities_explicit ~arities:typs ~constructors |
| 429 |
~template_syntax ~env_ar ~private_ind:false |
| 430 |
in |
| 431 |
let ids, mie = fix_entry_record ~isclass ~primitive_proj records mie in |
| 432 |
RecordEntry { |
| 433 |
mie; |
| 434 |
global_univs; |
| 435 |
ubinders; |
| 436 |
ind_infos = List.map4 RecordEntry.make_ind_infos ids default_dep_elim field_impls locs; |
| 437 |
param_impls = impls; |
| 438 |
} |
| 439 |
|
| 440 |
type record_error = |
| 441 |
| MissingProj of Id.t * Id.t list |
| 442 |
| BadTypedProj of Id.t * env * Type_errors.type_error |
| 443 |
|
| 444 |
let warn_cannot_define_projection = |
| 445 |
CWarnings.create ~name:"cannot-define-projection" ~category:CWarnings.CoreCategories.records |
| 446 |
(fun msg -> hov 0 msg) |
| 447 |
|
| 448 |
type arity_error = |
| 449 |
| NonInformativeToInformative |
| 450 |
|
| 451 |
let error_elim_explain kp ki = |
| 452 |
let open Sorts.Quality in |
| 453 |
match kp,ki with |
| 454 |
| QConstant QType, QConstant QProp -> Some NonInformativeToInformative |
| 455 |
| _ -> None |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
let warning_or_error ?loc ~info flags indsp err = |
| 461 |
let st = match err with |
| 462 |
| MissingProj (fi,projs) -> |
| 463 |
let s,have = if List.length projs > 1 then "s","were" else "","was" in |
| 464 |
(Id.print fi ++ |
| 465 |
strbrk" cannot be defined because the projection" ++ str s ++ spc () ++ |
| 466 |
prlist_with_sep pr_comma Id.print projs ++ spc () ++ str have ++ |
| 467 |
strbrk " not defined.") |
| 468 |
| BadTypedProj (fi,env,te) -> |
| 469 |
let err = match te with |
| 470 |
| ElimArity (_, _, Some s) -> |
| 471 |
error_elim_explain (Sorts.quality s) |
| 472 |
(Inductiveops.elim_sort (Global.lookup_inductive indsp)) |
| 473 |
| _ -> None |
| 474 |
in |
| 475 |
match err with |
| 476 |
| Some NonInformativeToInformative -> |
| 477 |
(Id.print fi ++ |
| 478 |
strbrk" cannot be defined because it is informative and " ++ |
| 479 |
Printer.pr_inductive (Global.env()) indsp ++ |
| 480 |
strbrk " is not.") |
| 481 |
| None -> |
| 482 |
(Id.print fi ++ str " cannot be defined because it is not typable:" ++ spc() ++ |
| 483 |
Himsg.explain_type_error env (Evd.from_env env) |
| 484 |
(Pretype_errors.of_type_error te)) |
| 485 |
in |
| 486 |
|
| 487 |
if Option.has_some flags.Data.pf_coercion || Option.has_some flags.Data.pf_instance then |
| 488 |
user_err ?loc ~info st; |
| 489 |
warn_cannot_define_projection ?loc (hov 0 st) |
| 490 |
|
| 491 |
type field_status = |
| 492 |
| NoProjection of Name.t |
| 493 |
| Projection of constr |
| 494 |
|
| 495 |
exception NotDefinable of record_error |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
let subst_projection fid l c = |
| 503 |
let lv = List.length l in |
| 504 |
let bad_projs = ref [] in |
| 505 |
let rec substrec depth c = match Constr.kind c with |
| 506 |
| Rel k -> |
| 507 |
|
| 508 |
if k <= depth+1 then |
| 509 |
c |
| 510 |
else if k-depth-1 <= lv then |
| 511 |
match List.nth l (k-depth-2) with |
| 512 |
| Projection t -> lift depth t |
| 513 |
| NoProjection (Name id) -> bad_projs := id :: !bad_projs; mkRel k |
| 514 |
| NoProjection Anonymous -> |
| 515 |
user_err (str "Field " ++ Id.print fid ++ |
| 516 |
str " depends on the " ++ pr_nth (k-depth-1) ++ str |
| 517 |
" field which has no name.") |
| 518 |
else |
| 519 |
mkRel (k-lv) |
| 520 |
| _ -> Constr.map_with_binders succ substrec depth c |
| 521 |
in |
| 522 |
let c' = lift 1 c in |
| 523 |
let c'' = substrec 0 c' in |
| 524 |
if not (List.is_empty !bad_projs) then |
| 525 |
raise (NotDefinable (MissingProj (fid,List.rev !bad_projs))); |
| 526 |
c'' |
| 527 |
|
| 528 |
let instantiate_possibly_recursive_type ind u ntypes paramdecls fields = |
| 529 |
let subst = List.map_i (fun i _ -> mkRel i) 1 paramdecls in |
| 530 |
let subst' = List.init ntypes (fun i -> mkIndU ((ind, ntypes - i - 1), u)) in |
| 531 |
Vars.substl_rel_context (subst @ subst') fields |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
let declare_proj_coercion_instance ~flags ref from = |
| 538 |
let () = match flags.Data.pf_coercion with |
| 539 |
| None -> () |
| 540 |
| Some { coe_local=local; coe_reversible=reversible } -> |
| 541 |
let cl = ComCoercion.class_of_global from in |
| 542 |
ComCoercion.try_add_new_coercion_with_source ref ~local ~reversible ~source:cl |
| 543 |
in |
| 544 |
let () = match flags.Data.pf_instance with |
| 545 |
| None -> () |
| 546 |
| Some { inst_locality; inst_priority } -> |
| 547 |
let env = Global.env () in |
| 548 |
let sigma = Evd.from_env env in |
| 549 |
let info = Typeclasses.{ hint_priority = inst_priority; hint_pattern = None } in |
| 550 |
Classes.declare_instance ~warn:true env sigma (Some info) inst_locality ref |
| 551 |
in |
| 552 |
() |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
let build_named_proj ~primitive ~flags ~univs ~uinstance ~kind env paramdecls |
| 562 |
paramargs decl impls {CAst.v=fid; loc} subst nfi ti i indsp mib lifted_fields x rp = |
| 563 |
let ccl = subst_projection fid subst ti in |
| 564 |
let body, p_opt = match decl with |
| 565 |
| LocalDef (_,ci,_) -> subst_projection fid subst ci, None |
| 566 |
| LocalAssum ({binder_relevance=rci},_) -> |
| 567 |
|
| 568 |
|
| 569 |
if primitive then |
| 570 |
let p = Projection.Repr.make indsp |
| 571 |
~proj_npars:mib.mind_nparams ~proj_arg:i fid in |
| 572 |
mkProj (Projection.make p false, rci, mkRel 1), Some (p,rci) |
| 573 |
else |
| 574 |
let ccl' = liftn 1 2 ccl in |
| 575 |
let p = mkLambda (x, lift 1 rp, ccl') in |
| 576 |
let branch = it_mkLambda_or_LetIn (mkRel nfi) lifted_fields in |
| 577 |
let ci = Inductiveops.make_case_info env indsp LetStyle in |
| 578 |
|
| 579 |
|
| 580 |
mkCase (Inductive.contract_case env (ci, (p, rci), NoInvert, mkRel 1, [|branch|])), None |
| 581 |
in |
| 582 |
let proj = it_mkLambda_or_LetIn (mkLambda (x,rp,body)) paramdecls in |
| 583 |
let projtyp = it_mkProd_or_LetIn (mkProd (x,rp,ccl)) paramdecls in |
| 584 |
let entry = Declare.definition_entry ~univs ~types:projtyp proj in |
| 585 |
let kind = Decls.IsDefinition kind in |
| 586 |
let kn = |
| 587 |
|
| 588 |
try Declare.declare_constant ?loc ~name:fid ~kind (Declare.DefinitionEntry entry) |
| 589 |
with Type_errors.TypeError (ctx,te) as exn when not primitive -> |
| 590 |
let _, info = Exninfo.capture exn in |
| 591 |
Exninfo.iraise (NotDefinable (BadTypedProj (fid,ctx,te)),info) |
| 592 |
in |
| 593 |
Declare.definition_message fid; |
| 594 |
let term = match p_opt with |
| 595 |
| Some (p,r) -> |
| 596 |
let _ = DeclareInd.declare_primitive_projection p kn in |
| 597 |
mkProj (Projection.make p false, r, mkRel 1) |
| 598 |
| None -> |
| 599 |
let proj_args = paramargs@[mkRel 1] in |
| 600 |
match decl with |
| 601 |
| LocalDef _ when primitive -> body |
| 602 |
| _ -> applist (mkConstU (kn,uinstance),proj_args) |
| 603 |
in |
| 604 |
let refi = GlobRef.ConstRef kn in |
| 605 |
Impargs.maybe_declare_manual_implicits false refi impls; |
| 606 |
declare_proj_coercion_instance ~flags refi (GlobRef.IndRef indsp); |
| 607 |
let i = if is_local_assum decl then i+1 else i in |
| 608 |
(Some kn, i, Projection term::subst) |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
let build_proj env mib indsp primitive x rp lifted_fields paramdecls paramargs ~uinstance ~kind ~univs |
| 613 |
(nfi,i,kinds,subst) flags loc decl impls = |
| 614 |
let fi = RelDecl.get_name decl in |
| 615 |
let ti = RelDecl.get_type decl in |
| 616 |
let (sp_proj,i,subst) = |
| 617 |
match fi with |
| 618 |
| Anonymous -> |
| 619 |
(None,i,NoProjection fi::subst) |
| 620 |
| Name fid -> |
| 621 |
let fid = CAst.make ?loc fid in |
| 622 |
try build_named_proj |
| 623 |
~primitive ~flags ~univs ~uinstance ~kind env paramdecls paramargs decl impls fid |
| 624 |
subst nfi ti i indsp mib lifted_fields x rp |
| 625 |
with NotDefinable why as exn -> |
| 626 |
let _, info = Exninfo.capture exn in |
| 627 |
warning_or_error ?loc ~info flags indsp why; |
| 628 |
(None,i,NoProjection fi::subst) |
| 629 |
in |
| 630 |
(nfi - 1, i, |
| 631 |
{ Structure.proj_name = fi |
| 632 |
; proj_true = is_local_assum decl |
| 633 |
; proj_canonical = flags.Data.pf_canonical |
| 634 |
; proj_body = sp_proj } :: kinds |
| 635 |
, subst) |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
let declare_projections indsp ~kind ~inhabitant_id flags ?fieldlocs fieldimpls = |
| 640 |
let env = Global.env() in |
| 641 |
let (mib,mip) = Global.lookup_inductive indsp in |
| 642 |
let uinstance = |
| 643 |
UVars.Instance.abstract_instance @@ |
| 644 |
UVars.AbstractContext.size @@ |
| 645 |
Declareops.inductive_polymorphic_context mib |
| 646 |
in |
| 647 |
let univs = match mib.mind_universes with |
| 648 |
| Monomorphic -> UState.Monomorphic_entry Univ.ContextSet.empty |
| 649 |
| Polymorphic auctx -> UState.Polymorphic_entry (UVars.AbstractContext.repr auctx) |
| 650 |
in |
| 651 |
let univs = univs, UnivNames.empty_binders in |
| 652 |
let fields, _ = mip.mind_nf_lc.(0) in |
| 653 |
let fields = List.firstn mip.mind_consnrealdecls.(0) fields in |
| 654 |
let paramdecls = Inductive.inductive_paramdecls (mib, uinstance) in |
| 655 |
let r = mkIndU (indsp,uinstance) in |
| 656 |
let rp = applist (r, Context.Rel.instance_list mkRel 0 paramdecls) in |
| 657 |
let paramargs = Context.Rel.instance_list mkRel 1 paramdecls in |
| 658 |
let x = make_annot (Name inhabitant_id) (Inductive.relevance_of_ind_body mip uinstance) in |
| 659 |
let fields = instantiate_possibly_recursive_type (fst indsp) uinstance (Declareops.mind_ntypes mib) paramdecls fields in |
| 660 |
let lifted_fields = Vars.lift_rel_context 1 fields in |
| 661 |
let primitive = |
| 662 |
match mip.mind_record with |
| 663 |
| PrimRecord _ -> true |
| 664 |
| FakeRecord | NotRecord -> false |
| 665 |
in |
| 666 |
let fieldlocs = match fieldlocs with |
| 667 |
| None -> List.make (List.length fields) None |
| 668 |
| Some fieldlocs -> fieldlocs |
| 669 |
in |
| 670 |
let (_,_,canonical_projections,_) = |
| 671 |
List.fold_left4 |
| 672 |
(build_proj env mib indsp primitive x rp lifted_fields paramdecls paramargs ~uinstance ~kind ~univs) |
| 673 |
(List.length fields,0,[],[]) flags (List.rev fieldlocs) (List.rev fields) (List.rev fieldimpls) |
| 674 |
in |
| 675 |
List.rev canonical_projections |
| 676 |
|
| 677 |
open Typeclasses |
| 678 |
|
| 679 |
let load_structure _ structure = Structure.register structure |
| 680 |
|
| 681 |
let cache_structure o = load_structure 1 o |
| 682 |
|
| 683 |
let subst_structure (subst, obj) = Structure.subst subst obj |
| 684 |
|
| 685 |
let discharge_structure x = Some x |
| 686 |
|
| 687 |
let rebuild_structure s = Structure.rebuild (Global.env()) s |
| 688 |
|
| 689 |
let inStruc : Structure.t -> Libobject.obj = |
| 690 |
let open Libobject in |
| 691 |
declare_object {(default_object "STRUCTURE") with |
| 692 |
cache_function = cache_structure; |
| 693 |
load_function = load_structure; |
| 694 |
subst_function = subst_structure; |
| 695 |
classify_function = (fun _ -> Substitute); |
| 696 |
discharge_function = discharge_structure; |
| 697 |
rebuild_function = rebuild_structure; } |
| 698 |
|
| 699 |
let declare_structure_entry o = |
| 700 |
Lib.add_leaf (inStruc o) |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
module Record_decl = struct |
| 718 |
type t = { |
| 719 |
entry : RecordEntry.t; |
| 720 |
records : Data.t list; |
| 721 |
projections_kind : Decls.definition_object_kind; |
| 722 |
indlocs : DeclareInd.indlocs; |
| 723 |
poly : PolyFlags.t |
| 724 |
} |
| 725 |
end |
| 726 |
|
| 727 |
module Ast = struct |
| 728 |
open Vernacexpr |
| 729 |
type t = |
| 730 |
{ name : Names.lident |
| 731 |
; is_coercion : coercion_flag |
| 732 |
; binders: local_binder_expr list |
| 733 |
; cfs : (local_decl_expr * Data.projection_flags * notation_declaration list) list |
| 734 |
; idbuild : lident |
| 735 |
; sort : constr_expr option |
| 736 |
; default_inhabitant_id : Id.t option |
| 737 |
} |
| 738 |
|
| 739 |
let to_datai { name; idbuild; cfs; sort; default_inhabitant_id; } = |
| 740 |
let fs = List.map pi1 cfs in |
| 741 |
{ DataI.name = name |
| 742 |
; constructor_name = idbuild.CAst.v |
| 743 |
; arity = sort |
| 744 |
; nots = List.map (fun (_, _, rf_notation) -> List.map Metasyntax.prepare_where_notation rf_notation) cfs |
| 745 |
; fs |
| 746 |
; default_inhabitant_id |
| 747 |
} |
| 748 |
end |
| 749 |
|
| 750 |
let check_unique_names ~def records = |
| 751 |
let extract_name acc (rf_decl, _, _) = match rf_decl with |
| 752 |
Vernacexpr.AssumExpr({CAst.v=Name id},_,_) -> id::acc |
| 753 |
| Vernacexpr.DefExpr ({CAst.v=Name id},_,_,_) -> id::acc |
| 754 |
| _ -> acc in |
| 755 |
let indlocs = |
| 756 |
records |> List.map (fun { Ast.name; idbuild; _ } -> name, idbuild ) in |
| 757 |
let fields_names = |
| 758 |
records |> List.fold_left (fun acc { Ast.cfs; _ } -> |
| 759 |
List.fold_left extract_name acc cfs) [] in |
| 760 |
let allnames = |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
let indnames = indlocs |> List.concat_map (fun (x,y) -> |
| 765 |
x.CAst.v :: if def then [] else [y.CAst.v]) |
| 766 |
in |
| 767 |
fields_names @ indnames |
| 768 |
in |
| 769 |
match List.duplicates Id.equal allnames with |
| 770 |
| [] -> List.map (fun (x,y) -> x.CAst.loc, [y.CAst.loc]) indlocs |
| 771 |
| id :: _ -> user_err (str "Two objects have the same name" ++ spc () ++ quote (Id.print id) ++ str ".") |
| 772 |
|
| 773 |
let kind_class = |
| 774 |
let open Vernacexpr in |
| 775 |
function Class true -> DefClass | Class false -> RecordClass |
| 776 |
| Inductive_kw | CoInductive | Variant | Record | Structure -> NotClass |
| 777 |
|
| 778 |
let extract_record_data records = |
| 779 |
let data = List.map Ast.to_datai records in |
| 780 |
let decl_data = List.map (fun { Ast.is_coercion; cfs } -> |
| 781 |
let proj_flags = List.map (fun (_,rf,_) -> rf) cfs in |
| 782 |
{ Data.is_coercion; proj_flags }) |
| 783 |
records |
| 784 |
in |
| 785 |
let ps = match records with |
| 786 |
| [] -> CErrors.anomaly (str "Empty record block.") |
| 787 |
| r :: rem -> |
| 788 |
let eq_local_binders bl1 bl2 = List.equal local_binder_eq bl1 bl2 in |
| 789 |
match List.find_opt (fun r' -> not @@ eq_local_binders r.Ast.binders r'.Ast.binders) rem with |
| 790 |
| None -> r.Ast.binders |
| 791 |
| Some r' -> |
| 792 |
ComInductive.Internal.error_differing_params ~kind:"record" |
| 793 |
(r.name, (r.binders,None)) |
| 794 |
(r'.name, (r'.binders,None)) |
| 795 |
in |
| 796 |
ps, data, decl_data |
| 797 |
|
| 798 |
let pre_process_structure udecl kind ~flags ~primitive_proj (records : Ast.t list) = |
| 799 |
let def = (kind = Vernacexpr.Class true) in |
| 800 |
let indlocs = check_unique_names ~def records in |
| 801 |
let ps, interp_data, decl_data = extract_record_data records in |
| 802 |
let entry = |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
Vernacstate.System.protect (fun () -> |
| 809 |
typecheck_params_and_fields ~primitive_proj ~kind:(kind_class kind) ~flags udecl ps interp_data) () |
| 810 |
in |
| 811 |
let projections_kind = |
| 812 |
Decls.(match kind_class kind with NotClass -> StructureComponent | _ -> Method) in |
| 813 |
entry, projections_kind, decl_data, indlocs |
| 814 |
|
| 815 |
let interp_structure_core (entry:RecordEntry.t) ~projections_kind ~indlocs ~poly data = |
| 816 |
let open Record_decl in |
| 817 |
{ entry; |
| 818 |
projections_kind; |
| 819 |
records = data; |
| 820 |
indlocs; |
| 821 |
poly |
| 822 |
} |
| 823 |
|
| 824 |
let interp_structure ~flags udecl kind ~primitive_proj records = |
| 825 |
assert (kind <> Vernacexpr.Class true); |
| 826 |
let entry, projections_kind, data, indlocs = |
| 827 |
pre_process_structure udecl kind ~flags ~primitive_proj records in |
| 828 |
match entry with |
| 829 |
| DefclassEntry _ -> assert false |
| 830 |
| RecordEntry entry -> |
| 831 |
interp_structure_core entry ~projections_kind ~indlocs ~poly:flags.poly data |
| 832 |
|
| 833 |
module Declared = struct |
| 834 |
type t = |
| 835 |
| Defclass of { class_kn : Constant.t; proj_kn : Constant.t; } |
| 836 |
| Record of MutInd.t |
| 837 |
end |
| 838 |
|
| 839 |
let declare_structure (decl:Record_decl.t) ~schemes = |
| 840 |
let () = Global.push_context_set decl.entry.global_univs in |
| 841 |
|
| 842 |
let impls = List.make (List.length decl.entry.mie.mind_entry_inds) (decl.entry.param_impls, []) in |
| 843 |
let default_dep_elim = List.map (fun x -> x.RecordEntry.default_dep_elim) decl.entry.ind_infos in |
| 844 |
let kn = |
| 845 |
DeclareInd.declare_mutual_inductive_with_eliminations decl.entry.mie |
| 846 |
decl.entry.ubinders |
| 847 |
impls |
| 848 |
~indlocs:decl.indlocs |
| 849 |
~default_dep_elim |
| 850 |
~schemes |
| 851 |
in |
| 852 |
let map i ({ RecordEntry.inhabitant_id; implfs; fieldlocs }, { Data.is_coercion; proj_flags; }) = |
| 853 |
let rsp = (kn, i) in |
| 854 |
let cstr = (rsp, 1) in |
| 855 |
let kind = decl.projections_kind in |
| 856 |
let projections = declare_projections rsp ~kind ~inhabitant_id proj_flags ~fieldlocs implfs in |
| 857 |
let build = GlobRef.ConstructRef cstr in |
| 858 |
let () = match is_coercion with |
| 859 |
| NoCoercion -> () |
| 860 |
| AddCoercion -> ComCoercion.try_add_new_coercion build ~local:false ~reversible:false |
| 861 |
in |
| 862 |
let struc = Structure.make (Global.env ()) rsp projections in |
| 863 |
let () = declare_structure_entry struc in |
| 864 |
GlobRef.IndRef rsp |
| 865 |
in |
| 866 |
let data = List.combine decl.entry.ind_infos decl.records in |
| 867 |
let inds = List.mapi map data in |
| 868 |
Declared.Record kn, inds |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
let declare_class_constant entry (data:Data.t) = |
| 873 |
let { DefClassEntry.univs; name; projname; params; sort; typ; projtyp; |
| 874 |
inhabitant_id; impls; projimpls; } |
| 875 |
= entry |
| 876 |
in |
| 877 |
let {Data.is_coercion; proj_flags} = data in |
| 878 |
let proj_flags = match proj_flags with |
| 879 |
| [x] -> x |
| 880 |
| _ -> assert false |
| 881 |
in |
| 882 |
let () = |
| 883 |
|
| 884 |
match is_coercion with |
| 885 |
| NoCoercion -> () |
| 886 |
| AddCoercion -> assert false |
| 887 |
in |
| 888 |
let class_body = it_mkLambda_or_LetIn projtyp params in |
| 889 |
let class_type = it_mkProd_or_LetIn typ params in |
| 890 |
let class_entry = |
| 891 |
Declare.definition_entry ~types:class_type ~univs class_body in |
| 892 |
let cst = Declare.declare_constant ?loc:name.loc ~name:name.v |
| 893 |
(Declare.DefinitionEntry class_entry) ~kind:Decls.(IsDefinition Definition) |
| 894 |
in |
| 895 |
let inst, univs = match univs with |
| 896 |
| UState.Monomorphic_entry _, ubinders -> |
| 897 |
UVars.Instance.empty, (UState.Monomorphic_entry Univ.ContextSet.empty, ubinders) |
| 898 |
| UState.Polymorphic_entry uctx, _ -> |
| 899 |
UVars.UContext.instance uctx, univs |
| 900 |
in |
| 901 |
let cstu = (cst, inst) in |
| 902 |
let binder = |
| 903 |
let r = Sorts.relevance_of_sort sort in |
| 904 |
{ Context.binder_name = Name inhabitant_id; binder_relevance = r } |
| 905 |
in |
| 906 |
let inst_type = appvectc (mkConstU cstu) (Context.Rel.instance mkRel 0 params) in |
| 907 |
let proj_type = |
| 908 |
it_mkProd_or_LetIn (mkProd(binder, inst_type, lift 1 projtyp)) params in |
| 909 |
let proj_body = |
| 910 |
it_mkLambda_or_LetIn (mkLambda (binder, inst_type, mkRel 1)) params in |
| 911 |
let proj_entry = Declare.definition_entry ~types:proj_type ~univs proj_body in |
| 912 |
let proj_cst = Declare.declare_constant ?loc:projname.loc ~name:projname.v |
| 913 |
(Declare.DefinitionEntry proj_entry) ~kind:Decls.(IsDefinition Definition) |
| 914 |
in |
| 915 |
let cref = GlobRef.ConstRef cst in |
| 916 |
Impargs.declare_manual_implicits false cref impls; |
| 917 |
Impargs.declare_manual_implicits false (GlobRef.ConstRef proj_cst) projimpls; |
| 918 |
Classes.set_typeclass_transparency ~locality:Hints.SuperGlobal |
| 919 |
[Evaluable.EvalConstRef cst] false; |
| 920 |
let () = |
| 921 |
declare_proj_coercion_instance ~flags:proj_flags (GlobRef.ConstRef proj_cst) cref in |
| 922 |
Declared.Defclass { class_kn = cst; proj_kn = proj_cst }, [cref] |
| 923 |
|
| 924 |
let set_class_mode ref mode ctx = |
| 925 |
let modes = |
| 926 |
match mode with |
| 927 |
| Some (Some m) -> Some m |
| 928 |
| _ -> |
| 929 |
let ctxl = Context.Rel.nhyps ctx in |
| 930 |
let def = typeclasses_default_mode () in |
| 931 |
let mode = match def with |
| 932 |
| Hints.ModeOutput -> None |
| 933 |
| Hints.ModeInput -> |
| 934 |
Some (List.init ctxl (fun _ -> Hints.ModeInput)) |
| 935 |
| Hints.ModeNoHeadEvar -> |
| 936 |
Some (List.init ctxl (fun _ -> Hints.ModeNoHeadEvar)) |
| 937 |
in |
| 938 |
let wm = List.init ctxl (fun _ -> def) in |
| 939 |
Classes.warn_default_mode (ref, wm); |
| 940 |
mode |
| 941 |
in |
| 942 |
match modes with |
| 943 |
| None -> () |
| 944 |
| Some modes -> Classes.set_typeclass_mode ~locality:Hints.SuperGlobal ref modes |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
let declare_class ?mode declared = |
| 950 |
let env = Global.env() in |
| 951 |
let impl, univs, params, fields, projs = match declared with |
| 952 |
| Declared.Defclass { class_kn; proj_kn } -> |
| 953 |
let class_cb = Environ.lookup_constant class_kn env in |
| 954 |
let proj_cb = Environ.lookup_constant proj_kn env in |
| 955 |
let univs = Declareops.constant_polymorphic_context class_cb in |
| 956 |
let class_body = match class_cb.const_body with |
| 957 |
| Def c -> c |
| 958 |
| Undef _ | OpaqueDef _ | Primitive _ | Symbol _ -> assert false |
| 959 |
in |
| 960 |
let params, field = Term.decompose_lambda_decls class_body in |
| 961 |
let fname = Name (Constant.label proj_kn) in |
| 962 |
let frelevance = proj_cb.const_relevance in |
| 963 |
let fields = [ RelDecl.LocalAssum ({binder_name=fname; binder_relevance=frelevance}, field) ] in |
| 964 |
let proj = { |
| 965 |
Typeclasses.meth_name = fname; |
| 966 |
meth_const = Some proj_kn; |
| 967 |
} |
| 968 |
in |
| 969 |
GlobRef.ConstRef class_kn, univs, params, fields, [proj] |
| 970 |
| Declared.Record mind -> |
| 971 |
let mib, mip = Inductive.lookup_mind_specif env (mind,0) in |
| 972 |
let univs = Declareops.inductive_polymorphic_context mib in |
| 973 |
let ctor_args, _ = mip.mind_nf_lc.(0) in |
| 974 |
let fields = List.firstn mip.mind_consnrealdecls.(0) ctor_args in |
| 975 |
let make_proj decl kn = { |
| 976 |
Typeclasses.meth_name = RelDecl.get_name decl; |
| 977 |
meth_const = kn; |
| 978 |
} |
| 979 |
in |
| 980 |
let projs = List.map2 make_proj (List.rev fields) (Structure.find_projections env (mind,0)) in |
| 981 |
GlobRef.IndRef (mind, 0), univs, mib.mind_params_ctxt, fields, projs |
| 982 |
in |
| 983 |
let k = { |
| 984 |
cl_univs = univs; |
| 985 |
cl_impl = impl; |
| 986 |
cl_strict = typeclasses_strict (); |
| 987 |
cl_unique = typeclasses_unique (); |
| 988 |
cl_context = params; |
| 989 |
cl_trivial = CList.is_empty fields; |
| 990 |
cl_props = fields; |
| 991 |
cl_projs = projs; |
| 992 |
} |
| 993 |
in |
| 994 |
Classes.add_class k; |
| 995 |
set_class_mode impl mode params |
| 996 |
|
| 997 |
let add_constant_class cst = |
| 998 |
let env = Global.env () in |
| 999 |
let ty, univs = Typeops.type_of_global_in_context env (GlobRef.ConstRef cst) in |
| 1000 |
let r = (Environ.lookup_constant cst env).const_relevance in |
| 1001 |
let ctx, _ = decompose_prod_decls ty in |
| 1002 |
let args = Context.Rel.instance Constr.mkRel 0 ctx in |
| 1003 |
let t = mkApp (mkConstU (cst, UVars.make_abstract_instance univs), args) in |
| 1004 |
let tc = |
| 1005 |
{ cl_univs = univs; |
| 1006 |
cl_impl = GlobRef.ConstRef cst; |
| 1007 |
cl_context = ctx; |
| 1008 |
cl_trivial = false; |
| 1009 |
cl_props = [LocalAssum (make_annot Anonymous r, t)]; |
| 1010 |
cl_projs = []; |
| 1011 |
cl_strict = typeclasses_strict (); |
| 1012 |
cl_unique = typeclasses_unique () |
| 1013 |
} |
| 1014 |
in |
| 1015 |
Classes.add_class tc; |
| 1016 |
Classes.set_typeclass_transparency ~locality:Hints.SuperGlobal |
| 1017 |
[Evaluable.EvalConstRef cst] false |
| 1018 |
|
| 1019 |
let add_inductive_class ind = |
| 1020 |
let env = Global.env () in |
| 1021 |
let mind, oneind = Inductive.lookup_mind_specif env ind in |
| 1022 |
let ctx = oneind.mind_arity_ctxt in |
| 1023 |
let univs = Declareops.inductive_polymorphic_context mind in |
| 1024 |
let props, projs = |
| 1025 |
match Structure.find env ind with |
| 1026 |
| exception Not_found -> |
| 1027 |
let r = oneind.mind_relevance in |
| 1028 |
let args = Context.Rel.instance mkRel 0 ctx in |
| 1029 |
let ty = mkApp (mkIndU (ind, UVars.make_abstract_instance univs), args) in |
| 1030 |
[LocalAssum (make_annot Anonymous r, ty)], [] |
| 1031 |
| s -> |
| 1032 |
let props, _ = oneind.mind_nf_lc.(0) in |
| 1033 |
let props = List.firstn oneind.mind_consnrealdecls.(0) props in |
| 1034 |
let projs = s.projections |> List.map (fun (p:Structure.projection) -> |
| 1035 |
{ meth_name = p.proj_name; meth_const = p.proj_body }) |
| 1036 |
in |
| 1037 |
props, projs |
| 1038 |
in |
| 1039 |
let k = { |
| 1040 |
cl_univs = univs; |
| 1041 |
cl_impl = GlobRef.IndRef ind; |
| 1042 |
cl_context = ctx; |
| 1043 |
cl_trivial = false; |
| 1044 |
cl_props = props; |
| 1045 |
cl_projs = projs; |
| 1046 |
cl_strict = typeclasses_strict (); |
| 1047 |
cl_unique = typeclasses_unique (); |
| 1048 |
} |
| 1049 |
in |
| 1050 |
Classes.add_class k |
| 1051 |
|
| 1052 |
let warn_already_existing_class = |
| 1053 |
CWarnings.create ~name:"already-existing-class" ~category:CWarnings.CoreCategories.automation Pp.(fun g -> |
| 1054 |
Printer.pr_global g ++ str " is already declared as a typeclass.") |
| 1055 |
|
| 1056 |
let declare_existing_class g = |
| 1057 |
if Typeclasses.is_class (Global.env ()) g then warn_already_existing_class g |
| 1058 |
else |
| 1059 |
match g with |
| 1060 |
| GlobRef.ConstRef x -> add_constant_class x |
| 1061 |
| GlobRef.IndRef x -> add_inductive_class x |
| 1062 |
| _ -> user_err |
| 1063 |
(Pp.str"Unsupported class type, only constants and inductives are allowed.") |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
let definition_structure ~flags udecl kind ~primitive_proj (records : Ast.t list) |
| 1070 |
: GlobRef.t list = |
| 1071 |
let entry, projections_kind, data, indlocs = |
| 1072 |
pre_process_structure udecl kind ~flags ~primitive_proj records |
| 1073 |
in |
| 1074 |
let declared, inds = match entry with |
| 1075 |
| DefclassEntry entry -> |
| 1076 |
let data = match data with [x] -> x | _ -> assert false in |
| 1077 |
declare_class_constant entry data |
| 1078 |
| RecordEntry entry -> |
| 1079 |
let structure = interp_structure_core entry ~projections_kind ~indlocs |
| 1080 |
~poly:flags.poly data in |
| 1081 |
declare_structure structure ~schemes:flags.schemes |
| 1082 |
in |
| 1083 |
if kind_class kind <> NotClass then declare_class ~mode:flags.mode declared; |
| 1084 |
inds |
| 1085 |
|
| 1086 |
module Internal = struct |
| 1087 |
let declare_projections = declare_projections |
| 1088 |
let declare_structure_entry = declare_structure_entry |
| 1089 |
end |