| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open Entries |
| 13 |
|
| 14 |
type indlocs = (Loc.t option * Loc.t option list) list |
| 15 |
|
| 16 |
|
| 17 |
let declare_inductive_argument_scopes kn mie = |
| 18 |
List.iteri (fun i {mind_entry_consnames=lc} -> |
| 19 |
Notation.declare_ref_arguments_scope (GlobRef.IndRef (kn,i)); |
| 20 |
for j=1 to List.length lc do |
| 21 |
Notation.declare_ref_arguments_scope (GlobRef.ConstructRef ((kn,i),j)); |
| 22 |
done) mie.mind_entry_inds |
| 23 |
|
| 24 |
type inductive_obj = { |
| 25 |
ind_names : (lident * lident list) list |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
let inductive_names sp kn obj = |
| 30 |
let (dp,_) = Libnames.repr_path sp in |
| 31 |
let kn = Global.mind_of_delta_kn kn in |
| 32 |
let names, _ = |
| 33 |
List.fold_left |
| 34 |
(fun (names, n) ({CAst.v=typename; loc=typeloc}, consnames) -> |
| 35 |
let ind_p = (kn,n) in |
| 36 |
let names, _ = |
| 37 |
List.fold_left |
| 38 |
(fun (names, p) {CAst.v=l; loc} -> |
| 39 |
let sp = Libnames.make_path dp l in |
| 40 |
((loc, sp, GlobRef.ConstructRef (ind_p,p)) :: names, p+1)) |
| 41 |
(names, 1) consnames |
| 42 |
in |
| 43 |
let sp = Libnames.make_path dp typename in |
| 44 |
((typeloc, sp, GlobRef.IndRef ind_p) :: names, n+1)) |
| 45 |
([], 0) obj.ind_names |
| 46 |
in names |
| 47 |
|
| 48 |
let load_inductive i ((sp, kn), names) = |
| 49 |
let names = inductive_names sp kn names in |
| 50 |
List.iter (fun (loc, sp, ref) -> |
| 51 |
Nametab.push (Nametab.Until i) sp ref; |
| 52 |
Option.iter (Nametab.set_cci_src_loc (TrueGlobal ref)) loc) |
| 53 |
names |
| 54 |
|
| 55 |
let open_inductive i ((sp, kn), names) = |
| 56 |
let names = inductive_names sp kn names in |
| 57 |
List.iter (fun (_, sp, ref) -> Nametab.push (Nametab.Exactly i) sp ref) names |
| 58 |
|
| 59 |
let cache_inductive o = |
| 60 |
|
| 61 |
load_inductive 1 o |
| 62 |
|
| 63 |
let discharge_inductive names = |
| 64 |
Some names |
| 65 |
|
| 66 |
let objInductive : (Id.t * inductive_obj) Libobject.Dyn.tag = |
| 67 |
let open Libobject in |
| 68 |
declare_named_object_full {(default_object "INDUCTIVE") with |
| 69 |
cache_function = cache_inductive; |
| 70 |
load_function = load_inductive; |
| 71 |
open_function = filtered_open open_inductive; |
| 72 |
classify_function = (fun a -> Substitute); |
| 73 |
subst_function = ident_subst_function; |
| 74 |
discharge_function = discharge_inductive; |
| 75 |
} |
| 76 |
|
| 77 |
let inInductive v = Libobject.Dyn.Easy.inj v objInductive |
| 78 |
|
| 79 |
let cache_prim (p,c) = Structures.PrimitiveProjections.register p c |
| 80 |
|
| 81 |
let load_prim _ p = cache_prim p |
| 82 |
|
| 83 |
let subst_prim (subst,(p,c)) = Mod_subst.subst_proj_repr subst p, Mod_subst.subst_constant subst c |
| 84 |
|
| 85 |
let discharge_prim (p,c) = Some (Global.discharge_proj_repr p, c) |
| 86 |
|
| 87 |
let inPrim : (Projection.Repr.t * Constant.t) -> Libobject.obj = |
| 88 |
let open Libobject in |
| 89 |
declare_object { |
| 90 |
(default_object "PRIMPROJS") with |
| 91 |
cache_function = cache_prim ; |
| 92 |
load_function = load_prim; |
| 93 |
subst_function = subst_prim; |
| 94 |
classify_function = (fun x -> Substitute); |
| 95 |
discharge_function = discharge_prim } |
| 96 |
|
| 97 |
let declare_primitive_projection p c = Lib.add_leaf (inPrim (p,c)) |
| 98 |
|
| 99 |
let feedback_axiom () = Feedback.(feedback AddedAxiom) |
| 100 |
|
| 101 |
let is_unsafe_typing_flags () = |
| 102 |
let open Declarations in |
| 103 |
let flags = Environ.typing_flags (Global.env()) in |
| 104 |
not (flags.check_universes && flags.check_guarded && flags.check_positive) |
| 105 |
|
| 106 |
|
| 107 |
let declare_mind ?typing_flags ~indlocs mie = |
| 108 |
let id = match mie.mind_entry_inds with |
| 109 |
| ind::_ -> ind.mind_entry_typename |
| 110 |
| [] -> CErrors.anomaly (Pp.str "cannot declare an empty list of inductives.") in |
| 111 |
let indlocs = Array.of_list indlocs in |
| 112 |
let map_names i mip = |
| 113 |
let typloc, conslocs = if Array.length indlocs <= i then None, [] |
| 114 |
else indlocs.(i) |
| 115 |
in |
| 116 |
let typloc = if Option.has_some typloc then typloc else Loc.get_current_command_loc() in |
| 117 |
let typ = CAst.make ?loc:typloc mip.mind_entry_typename in |
| 118 |
let conslocs = Array.of_list conslocs in |
| 119 |
let map_cons j na = |
| 120 |
let consloc = if Array.length conslocs <= j then None |
| 121 |
else conslocs.(j) |
| 122 |
in |
| 123 |
let consloc = if Option.has_some consloc then consloc else typloc in |
| 124 |
CAst.make ?loc:consloc na |
| 125 |
in |
| 126 |
let consl = List.mapi map_cons mip.mind_entry_consnames in |
| 127 |
(typ, consl) |
| 128 |
in |
| 129 |
let names = List.mapi map_names mie.mind_entry_inds in |
| 130 |
let check_exists id = |
| 131 |
if Decls.variable_exists id || Global.exists_objlabel id then |
| 132 |
raise (DeclareUniv.AlreadyDeclared (None, id)) |
| 133 |
in |
| 134 |
List.iter (fun ({CAst.v=typ}, cons) -> |
| 135 |
check_exists typ; |
| 136 |
List.iter (fun {CAst.v} -> check_exists v) cons) names; |
| 137 |
let mind, why_not_prim_record = Global.add_mind ?typing_flags id mie in |
| 138 |
let () = Lib.add_leaf (inInductive (id, { ind_names = names })) in |
| 139 |
let () = UState.add_template_default_univs (Global.env ()) mind in |
| 140 |
if is_unsafe_typing_flags() then feedback_axiom (); |
| 141 |
Impargs.declare_mib_implicits mind; |
| 142 |
declare_inductive_argument_scopes mind mie; |
| 143 |
mind, why_not_prim_record |
| 144 |
|
| 145 |
let is_recursive mie = |
| 146 |
let open Constr in |
| 147 |
let rec is_recursive_constructor lift n typ = |
| 148 |
match Constr.kind typ with |
| 149 |
| Prod (_,arg,rest) -> |
| 150 |
not (Vars.noccur_between lift n arg) || |
| 151 |
is_recursive_constructor (lift+1) n rest |
| 152 |
| LetIn (na,b,t,rest) -> is_recursive_constructor (lift+1) n rest |
| 153 |
| _ -> false |
| 154 |
in |
| 155 |
let nind = List.length mie.mind_entry_inds in |
| 156 |
let nparams = List.length mie.mind_entry_params in |
| 157 |
List.exists (fun ind -> List.exists (fun t -> is_recursive_constructor (nparams+1) nind t) ind.mind_entry_lc) mie.mind_entry_inds |
| 158 |
|
| 159 |
let explain_not_prim_record reason = |
| 160 |
let open IndTyping.NotPrimRecordReason in |
| 161 |
let open Pp in |
| 162 |
match reason with |
| 163 |
| MustNotBeSquashed -> strbrk "it is squashed" |
| 164 |
| MustHaveRelevantProj -> strbrk "it is not in SProp but all projections may be irrelevant" |
| 165 |
| MustHaveProj -> strbrk "it has no projections" |
| 166 |
| MustNotHaveAnonProj -> strbrk "it has an anonymous projection" |
| 167 |
|
| 168 |
let warn_non_primitive_record = |
| 169 |
CWarnings.create ~name:"non-primitive-record" ~category:CWarnings.CoreCategories.records |
| 170 |
Pp.(fun (mind,why_not_prim_record) -> |
| 171 |
hov 0 |
| 172 |
(str "The record " ++ Nametab.pr_global_env Id.Set.empty (GlobRef.IndRef (mind,0)) ++ |
| 173 |
strbrk" could not be defined as a primitive record because " ++ |
| 174 |
explain_not_prim_record why_not_prim_record ++ str ".")) |
| 175 |
|
| 176 |
let minductive_message = function |
| 177 |
| [] -> CErrors.user_err Pp.(str "No inductive definition.") |
| 178 |
| [x] -> Pp.(Id.print x ++ str " is defined") |
| 179 |
| l -> Pp.(hov 0 (prlist_with_sep pr_comma Id.print l ++ |
| 180 |
spc () ++ str "are defined")) |
| 181 |
|
| 182 |
type one_inductive_impls = |
| 183 |
Impargs.manual_implicits * |
| 184 |
Impargs.manual_implicits list |
| 185 |
|
| 186 |
let { Goptions.get = default_prop_dep_elim } = |
| 187 |
Goptions.declare_bool_option_and_ref ~key:["Dependent";"Proposition";"Eliminators"] ~value:false () |
| 188 |
|
| 189 |
type default_dep_elim = DefaultElim | PropButDepElim |
| 190 |
|
| 191 |
type declare_schemes = None | Default |
| 192 |
|
| 193 |
let schemes_attr = |
| 194 |
let values = [("default", Default); ("none", None)] in |
| 195 |
Attributes.key_value_attribute ~key:"schemes" ?empty:None ~values |
| 196 |
|> Attributes.Notations.map (Option.default Default) |
| 197 |
|
| 198 |
let rec declare_mutual_inductive_with_eliminations |
| 199 |
?typing_flags ?(indlocs=[]) ?default_dep_elim ?(schemes=Default) |
| 200 |
?all_depth mie ubinders impls = |
| 201 |
|
| 202 |
|
| 203 |
begin match mie.mind_entry_finite with |
| 204 |
| Declarations.BiFinite -> |
| 205 |
if is_recursive mie then |
| 206 |
if Option.has_some mie.mind_entry_record then |
| 207 |
CErrors.user_err Pp.(strbrk "Records declared with the keywords Record or Structure cannot be recursive. You can, however, define recursive records using the Inductive or CoInductive command.") |
| 208 |
else |
| 209 |
CErrors.user_err Pp.(strbrk "Types declared with the keyword Variant cannot be recursive. Recursive types are defined with the Inductive and CoInductive command."); |
| 210 |
if not (Int.equal (List.length mie.mind_entry_inds) 1) then |
| 211 |
if Option.has_some mie.mind_entry_record then |
| 212 |
CErrors.user_err Pp.(strbrk "Keywords Record and Structure are to define a single type at once.") |
| 213 |
else |
| 214 |
CErrors.user_err Pp.(strbrk "Keyword Variant is to define a single type at once.") |
| 215 |
| _ -> () |
| 216 |
end; |
| 217 |
let names = List.map (fun e -> e.mind_entry_typename) mie.mind_entry_inds in |
| 218 |
let mind, why_not_prim_record = declare_mind ?typing_flags ~indlocs mie in |
| 219 |
why_not_prim_record |> Option.iter (fun why_not_prim_record -> |
| 220 |
warn_non_primitive_record (mind,why_not_prim_record)); |
| 221 |
let () = match fst ubinders with |
| 222 |
| UState.Polymorphic_entry _ -> () |
| 223 |
| UState.Monomorphic_entry ctx -> |
| 224 |
DeclareUniv.add_constraint_source (IndRef (mind,0)) ctx |
| 225 |
in |
| 226 |
DeclareUniv.declare_univ_binders (GlobRef.IndRef (mind,0)) ubinders; |
| 227 |
List.iteri (fun i (indimpls, constrimpls) -> |
| 228 |
let ind = (mind,i) in |
| 229 |
let gr = GlobRef.IndRef ind in |
| 230 |
Impargs.maybe_declare_manual_implicits false gr indimpls; |
| 231 |
List.iteri |
| 232 |
(fun j impls -> |
| 233 |
Impargs.maybe_declare_manual_implicits false |
| 234 |
(GlobRef.ConstructRef (ind, succ j)) impls) |
| 235 |
constrimpls) |
| 236 |
impls; |
| 237 |
|
| 238 |
let () = match default_dep_elim with |
| 239 |
| None -> () |
| 240 |
| Some defaults -> |
| 241 |
List.iteri (fun i default -> |
| 242 |
let prop_but_default_dep_elim = |
| 243 |
match default with |
| 244 |
| PropButDepElim -> true |
| 245 |
| DefaultElim -> |
| 246 |
default_prop_dep_elim () && |
| 247 |
let _, mip = Global.lookup_inductive (mind, i) in |
| 248 |
Sorts.is_prop mip.mind_sort |
| 249 |
in |
| 250 |
if prop_but_default_dep_elim |
| 251 |
then Elimschemes.declare_prop_but_default_dependent_elim (mind, i) |
| 252 |
) defaults |
| 253 |
in |
| 254 |
Flags.if_verbose Feedback.msg_info (minductive_message names); |
| 255 |
let indlocs = List.map fst indlocs in |
| 256 |
let locmap = Ind_tables.Locmap.make mind indlocs in |
| 257 |
let () = match schemes with |
| 258 |
| None -> () |
| 259 |
| Default -> |
| 260 |
if Option.has_some mie.mind_entry_private then () |
| 261 |
else |
| 262 |
let declare_mind ?all_depth entry univs = |
| 263 |
declare_mutual_inductive_with_eliminations ?typing_flags ?all_depth entry univs [] |
| 264 |
in |
| 265 |
Indschemes.declare_default_schemes ?all_depth ~declare_mind mind ~locmap |
| 266 |
in |
| 267 |
mind |
| 268 |
|
| 269 |
let do_scheme_all id strpos = |
| 270 |
let declare_mind ?all_depth entry univs = |
| 271 |
declare_mutual_inductive_with_eliminations ?all_depth entry univs [] |
| 272 |
in |
| 273 |
Indschemes.Internal.do_scheme_all ~user_call_scheme:true ~declare_mind id strpos |
| 274 |
|
| 275 |
module Internal = |
| 276 |
struct |
| 277 |
type nonrec inductive_obj = inductive_obj |
| 278 |
let objInductive = objInductive |
| 279 |
end |