| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
open Pp |
| 20 |
open Names |
| 21 |
open Util |
| 22 |
open Declarations |
| 23 |
open Term |
| 24 |
open Goptions |
| 25 |
open Vernacexpr |
| 26 |
open Ind_tables |
| 27 |
open Auto_ind_decl |
| 28 |
open Eqschemes |
| 29 |
open Elimschemes |
| 30 |
open Sorts |
| 31 |
|
| 32 |
|
| 33 |
type resolved_scheme = Names.Id.t CAst.t * Indrec.dep_flag * Names.inductive * UnivGen.QualityOrSet.t |
| 34 |
|
| 35 |
|
| 36 |
type internal_flag = |
| 37 |
| UserAutomaticRequest |
| 38 |
| UserIndividualRequest |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
let elim_flag = ref true |
| 43 |
let () = |
| 44 |
declare_bool_option |
| 45 |
{ optstage = Summary.Stage.Interp; |
| 46 |
optdepr = None; |
| 47 |
optkey = ["Elimination";"Schemes"]; |
| 48 |
optread = (fun () -> !elim_flag) ; |
| 49 |
optwrite = (fun b -> elim_flag := b) } |
| 50 |
|
| 51 |
let bifinite_elim_flag = ref false |
| 52 |
let () = |
| 53 |
declare_bool_option |
| 54 |
{ optstage = Summary.Stage.Interp; |
| 55 |
optdepr = None; |
| 56 |
optkey = ["Nonrecursive";"Elimination";"Schemes"]; |
| 57 |
optread = (fun () -> !bifinite_elim_flag) ; |
| 58 |
optwrite = (fun b -> bifinite_elim_flag := b) } |
| 59 |
|
| 60 |
let case_flag = ref false |
| 61 |
let () = |
| 62 |
declare_bool_option |
| 63 |
{ optstage = Summary.Stage.Interp; |
| 64 |
optdepr = None; |
| 65 |
optkey = ["Case";"Analysis";"Schemes"]; |
| 66 |
optread = (fun () -> !case_flag) ; |
| 67 |
optwrite = (fun b -> case_flag := b) } |
| 68 |
|
| 69 |
let eq_flag = ref false |
| 70 |
let () = |
| 71 |
declare_bool_option |
| 72 |
{ optstage = Summary.Stage.Interp; |
| 73 |
optdepr = None; |
| 74 |
optkey = ["Boolean";"Equality";"Schemes"]; |
| 75 |
optread = (fun () -> !eq_flag) ; |
| 76 |
optwrite = (fun b -> eq_flag := b) } |
| 77 |
|
| 78 |
let is_eq_flag () = !eq_flag |
| 79 |
|
| 80 |
let eq_dec_flag = ref false |
| 81 |
let () = |
| 82 |
declare_bool_option |
| 83 |
{ optstage = Summary.Stage.Interp; |
| 84 |
optdepr = None; |
| 85 |
optkey = ["Decidable";"Equality";"Schemes"]; |
| 86 |
optread = (fun () -> !eq_dec_flag) ; |
| 87 |
optwrite = (fun b -> eq_dec_flag := b) } |
| 88 |
|
| 89 |
let rewriting_flag = ref false |
| 90 |
let () = |
| 91 |
declare_bool_option |
| 92 |
{ optstage = Summary.Stage.Interp; |
| 93 |
optdepr = None; |
| 94 |
optkey = ["Rewriting";"Schemes"]; |
| 95 |
optread = (fun () -> !rewriting_flag) ; |
| 96 |
optwrite = (fun b -> rewriting_flag := b) } |
| 97 |
|
| 98 |
|
| 99 |
let define ~poly ?loc name sigma c types = |
| 100 |
let poly = |
| 101 |
PolyFlags.of_univ_poly poly |
| 102 |
in |
| 103 |
let info = Declare.Info.make ~poly () in |
| 104 |
let cinfo = Declare.CInfo.make ~name ~typ:types () in |
| 105 |
let fth_ref = Declare.declare_definition ~info:info ~cinfo:cinfo ~opaque:false ~body:c sigma in |
| 106 |
fth_ref |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
let declare_beq_scheme_gen ?locmap names kn = |
| 111 |
ignore (define_mutual_scheme ?locmap beq_scheme_kind names kn) |
| 112 |
|
| 113 |
let debug = CDebug.create ~name:"indschemes" () |
| 114 |
|
| 115 |
let alarm what internal msg = |
| 116 |
match internal with |
| 117 |
| UserAutomaticRequest -> |
| 118 |
debug Pp.(fun () -> |
| 119 |
hov 0 msg ++ fnl () ++ what ++ str " not defined."); |
| 120 |
None |
| 121 |
| UserIndividualRequest -> Some msg |
| 122 |
|
| 123 |
let try_declare_scheme ?locmap what f internal names kn = |
| 124 |
try f ?locmap names kn |
| 125 |
with e when CErrors.noncritical e -> |
| 126 |
let e = Exninfo.capture e in |
| 127 |
let rec extract_exn = function Logic_monad.TacticFailure e -> extract_exn e | e -> e in |
| 128 |
let msg = match extract_exn (fst e) with |
| 129 |
| ParameterWithoutEquality cst -> |
| 130 |
alarm what internal |
| 131 |
(str "Boolean equality not found for parameter " ++ Printer.pr_global cst ++ |
| 132 |
str".") |
| 133 |
| InductiveWithProduct -> |
| 134 |
alarm what internal |
| 135 |
(str "Unable to decide equality of functional arguments.") |
| 136 |
| InductiveWithSort -> |
| 137 |
alarm what internal |
| 138 |
(str "Unable to decide equality of type arguments.") |
| 139 |
| NonSingletonProp ind -> |
| 140 |
alarm what internal |
| 141 |
(str "Cannot extract computational content from proposition " ++ |
| 142 |
quote (Printer.pr_inductive (Global.env()) ind) ++ str ".") |
| 143 |
| EqNotFound ind' -> |
| 144 |
alarm what internal |
| 145 |
(str "Boolean equality on " ++ |
| 146 |
quote (Printer.pr_inductive (Global.env()) ind') ++ |
| 147 |
strbrk " is missing.") |
| 148 |
| UndefinedCst s -> |
| 149 |
alarm what internal |
| 150 |
(strbrk "Required constant " ++ str s ++ str " undefined.") |
| 151 |
| DeclareUniv.AlreadyDeclared (kind, id) as exn -> |
| 152 |
let msg = CErrors.print exn in |
| 153 |
alarm what internal msg |
| 154 |
| DecidabilityMutualNotSupported -> |
| 155 |
alarm what internal |
| 156 |
(str "Decidability lemma for mutual inductive types not supported.") |
| 157 |
| EqUnknown s -> |
| 158 |
alarm what internal |
| 159 |
(str "Found unsupported " ++ str s ++ str " while building Boolean equality.") |
| 160 |
| NoDecidabilityCoInductive -> |
| 161 |
alarm what internal |
| 162 |
(str "Scheme Equality is only for inductive types.") |
| 163 |
| DecidabilityIndicesNotSupported -> |
| 164 |
alarm what internal |
| 165 |
(str "Inductive types with indices not supported.") |
| 166 |
| ConstructorWithNonParametricInductiveType ind -> |
| 167 |
alarm what internal |
| 168 |
(strbrk "Unsupported constructor with an argument whose type is a non-parametric inductive type." ++ |
| 169 |
strbrk " Type " ++ quote (Printer.pr_inductive (Global.env()) ind) ++ |
| 170 |
str " is applied to an argument which is not a variable.") |
| 171 |
| InternalDependencies -> |
| 172 |
alarm what internal |
| 173 |
(strbrk "Inductive types with internal dependencies in constructors not supported.") |
| 174 |
| e -> |
| 175 |
alarm what internal |
| 176 |
(str "Unexpected error during scheme creation: " ++ CErrors.print e) |
| 177 |
in |
| 178 |
match msg with |
| 179 |
| None -> () |
| 180 |
| Some msg -> Exninfo.iraise (CErrors.UserError msg, snd e) |
| 181 |
|
| 182 |
let beq_scheme_msg mind = |
| 183 |
let mib = Global.lookup_mind mind in |
| 184 |
|
| 185 |
str "Boolean equality on " ++ |
| 186 |
pr_enum (fun ind -> quote (Printer.pr_inductive (Global.env()) ind)) |
| 187 |
(List.init (Array.length mib.mind_packets) (fun i -> (mind,i))) |
| 188 |
|
| 189 |
let declare_beq_scheme_with ?locmap l kn = |
| 190 |
try_declare_scheme (beq_scheme_msg kn) declare_beq_scheme_gen UserIndividualRequest l kn |
| 191 |
|
| 192 |
let try_declare_beq_scheme ?locmap kn = |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
try_declare_scheme (beq_scheme_msg kn) declare_beq_scheme_gen UserAutomaticRequest [] kn |
| 197 |
|
| 198 |
let declare_beq_scheme ?locmap mi = declare_beq_scheme_with ?locmap [] mi |
| 199 |
|
| 200 |
|
| 201 |
let declare_one_case_analysis_scheme ?loc ind = |
| 202 |
let (mib, mip) as specif = Global.lookup_inductive ind in |
| 203 |
let kind = Elimschemes.pseudo_sort_quality_for_elim ind mip in |
| 204 |
let dep, suff = |
| 205 |
if Sorts.Quality.is_qprop kind then case_nodep, Some "case" |
| 206 |
else if not (Inductiveops.has_dependent_elim specif) then |
| 207 |
case_nodep, None |
| 208 |
else case_dep, Some "case" in |
| 209 |
let id = match suff with |
| 210 |
| None -> None |
| 211 |
| Some suff -> |
| 212 |
|
| 213 |
Some Names.(Id.of_string (Id.to_string mip.mind_typename ^ "_" ^ suff)) |
| 214 |
in |
| 215 |
let kelim = Inductiveops.elim_sort (mib,mip) in |
| 216 |
if Inductive.raw_eliminates_to kelim Sorts.Quality.qtype then |
| 217 |
define_individual_scheme ?loc dep id ind |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
let declare_one_induction_scheme ?loc ind = |
| 222 |
let (mib,mip) as specif = Global.lookup_inductive ind in |
| 223 |
let kind = Elimschemes.pseudo_sort_quality_for_elim ind mip in |
| 224 |
let from_prop = Sorts.Quality.is_qprop kind in |
| 225 |
let depelim = Inductiveops.has_dependent_elim specif in |
| 226 |
let kelim mip = Inductiveops.constant_sorts_below |
| 227 |
@@ Inductiveops.elim_sort (mib,mip) in |
| 228 |
let kelim = |
| 229 |
List.fold_right (fun x acc -> |
| 230 |
List.intersect UnivGen.QualityOrSet.equal acc x) |
| 231 |
(List.map kelim (Array.to_list mib.mind_packets)) |
| 232 |
[UnivGen.QualityOrSet.qtype; UnivGen.QualityOrSet.prop; UnivGen.QualityOrSet.set; UnivGen.QualityOrSet.sprop] in |
| 233 |
let kelim = |
| 234 |
if Global.sprop_allowed () |
| 235 |
then kelim |
| 236 |
else List.filter (fun s -> not (UnivGen.QualityOrSet.is_sprop s)) kelim |
| 237 |
in |
| 238 |
let elims = |
| 239 |
List.filter (fun (sort,_) -> List.mem_f UnivGen.QualityOrSet.equal sort kelim) |
| 240 |
[(UnivGen.QualityOrSet.qtype, "rect"); |
| 241 |
(UnivGen.QualityOrSet.prop, "ind"); |
| 242 |
(UnivGen.QualityOrSet.set, "rec"); |
| 243 |
(UnivGen.QualityOrSet.sprop, "sind")] |
| 244 |
in |
| 245 |
let elims = List.map (fun (to_kind,dflt_suff) -> |
| 246 |
if from_prop then elim_scheme ~dep:false ~to_kind, Some dflt_suff |
| 247 |
else if depelim then elim_scheme ~dep:true ~to_kind, Some dflt_suff |
| 248 |
else elim_scheme ~dep:false ~to_kind, None) |
| 249 |
elims |
| 250 |
in |
| 251 |
List.iter (fun (kind, suff) -> |
| 252 |
let id = match suff with |
| 253 |
| None -> None |
| 254 |
| Some suff -> |
| 255 |
|
| 256 |
Some Names.(Id.of_string (Id.to_string mip.mind_typename ^ "_" ^ suff)) |
| 257 |
in |
| 258 |
define_individual_scheme ?loc kind id ind) |
| 259 |
elims |
| 260 |
|
| 261 |
let declare_induction_schemes ?(locmap=Locmap.default None) kn = |
| 262 |
let mib = Global.lookup_mind kn in |
| 263 |
if mib.mind_finite <> Declarations.CoFinite then begin |
| 264 |
for i = 0 to Array.length mib.mind_packets - 1 do |
| 265 |
let loc = Ind_tables.Locmap.lookup ~locmap (kn,i) in |
| 266 |
declare_one_induction_scheme (kn,i) ?loc; |
| 267 |
done; |
| 268 |
end |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
let declare_eq_decidability_gen ?locmap names kn = |
| 273 |
let mib = Global.lookup_mind kn in |
| 274 |
if mib.mind_finite <> Declarations.CoFinite then |
| 275 |
define_mutual_scheme ?locmap eq_dec_scheme_kind names kn |
| 276 |
|
| 277 |
let eq_dec_scheme_msg ind = |
| 278 |
str "Decidable equality on " ++ quote (Printer.pr_inductive (Global.env()) ind) |
| 279 |
|
| 280 |
let declare_eq_decidability_scheme_with ?locmap l kn = |
| 281 |
try_declare_scheme ?locmap (eq_dec_scheme_msg (kn,0)) |
| 282 |
declare_eq_decidability_gen UserIndividualRequest l kn |
| 283 |
|
| 284 |
let try_declare_eq_decidability ?locmap kn = |
| 285 |
try_declare_scheme ?locmap (eq_dec_scheme_msg (kn,0)) |
| 286 |
declare_eq_decidability_gen UserAutomaticRequest [] kn |
| 287 |
|
| 288 |
let declare_eq_decidability ?locmap mi = declare_eq_decidability_scheme_with ?locmap [] mi |
| 289 |
|
| 290 |
let ignore_error f x = |
| 291 |
try f x with e when CErrors.noncritical e -> () |
| 292 |
|
| 293 |
let declare_rewriting_schemes ?loc ind = |
| 294 |
if Hipattern.is_inductive_equality (Global.env ()) ind then begin |
| 295 |
|
| 296 |
ignore_error (define_individual_scheme ?loc sym_scheme_kind None) ind; |
| 297 |
define_individual_scheme ?loc rew_r2l_scheme_kind None ind; |
| 298 |
define_individual_scheme ?loc rew_r2l_dep_scheme_kind None ind; |
| 299 |
define_individual_scheme ?loc rew_r2l_forward_dep_scheme_kind None ind; |
| 300 |
|
| 301 |
|
| 302 |
ignore_error (define_individual_scheme rew_l2r_scheme_kind None) ind; |
| 303 |
ignore_error |
| 304 |
(define_individual_scheme ?loc sym_involutive_scheme_kind None) ind; |
| 305 |
ignore_error |
| 306 |
(define_individual_scheme ?loc rew_l2r_dep_scheme_kind None) ind; |
| 307 |
ignore_error |
| 308 |
(define_individual_scheme ?loc rew_l2r_forward_dep_scheme_kind None) ind |
| 309 |
end |
| 310 |
|
| 311 |
let warn_cannot_build_congruence = |
| 312 |
CWarnings.create ~name:"cannot-build-congruence" ~category:CWarnings.CoreCategories.automation |
| 313 |
(fun () -> |
| 314 |
strbrk "Cannot build congruence scheme because eq is not found") |
| 315 |
|
| 316 |
let declare_congr_scheme ?loc ind = |
| 317 |
let env = Global.env () in |
| 318 |
if Hipattern.is_inductive_equality env ind then begin |
| 319 |
match Rocqlib.lib_ref_opt "core.eq.type" with |
| 320 |
| Some _ -> define_individual_scheme ?loc congr_scheme_kind None ind |
| 321 |
| None -> warn_cannot_build_congruence () |
| 322 |
end |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
let sch_isdep = function |
| 328 |
| SchemeInduction | SchemeElimination -> true |
| 329 |
| SchemeMinimality | SchemeCase -> false |
| 330 |
|
| 331 |
let sch_isrec = function |
| 332 |
| SchemeInduction | SchemeMinimality -> true |
| 333 |
| SchemeElimination | SchemeCase -> false |
| 334 |
|
| 335 |
|
| 336 |
let scheme_suffix_gen {sch_type; sch_sort} sort = |
| 337 |
let open Quality in |
| 338 |
|
| 339 |
let ind_suffix = match sch_isrec sch_type, sch_sort with |
| 340 |
| true , Qual (QConstant QSProp | QConstant QProp) -> "_ind" |
| 341 |
| true , _ -> "_rec" |
| 342 |
| false , _ -> "_case" in |
| 343 |
|
| 344 |
let aux_suffix = match sch_sort with |
| 345 |
| Qual (QConstant QSProp) -> "s" |
| 346 |
| Qual (QConstant QType) -> "t" |
| 347 |
| _ -> "" in |
| 348 |
|
| 349 |
let dep_suffix = match sch_isdep sch_type , sort with |
| 350 |
| true , QConstant QProp -> "_dep" |
| 351 |
| false , QConstant QType |
| 352 |
| false , QConstant QSProp -> "_nodep" |
| 353 |
| _ , _ -> "" in |
| 354 |
ind_suffix ^ aux_suffix ^ dep_suffix |
| 355 |
|
| 356 |
let smart_ind qid = |
| 357 |
let ind = Smartlocate.smart_global_inductive qid in |
| 358 |
if Dumpglob.dump() then Dumpglob.add_glob ?loc:qid.loc (IndRef ind); |
| 359 |
ind |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
let name_and_process_scheme env = function |
| 365 |
| (Some id, {sch_type; sch_qualid; sch_sort}) -> |
| 366 |
(id, sch_isdep sch_type, smart_ind sch_qualid, sch_sort) |
| 367 |
| (None, ({sch_type; sch_qualid; sch_sort} as sch)) -> |
| 368 |
|
| 369 |
let ind = smart_ind sch_qualid in |
| 370 |
let sort_of_ind = |
| 371 |
Elimschemes.pseudo_sort_quality_for_elim ind |
| 372 |
(snd (Inductive.lookup_mind_specif env ind)) |
| 373 |
in |
| 374 |
let suffix = scheme_suffix_gen sch sort_of_ind in |
| 375 |
let newid = Nameops.add_suffix (Nametab.basename_of_global (Names.GlobRef.IndRef ind)) suffix in |
| 376 |
let newref = CAst.make newid in |
| 377 |
(newref, sch_isdep sch_type, ind, sch_sort) |
| 378 |
|
| 379 |
let do_mutual_induction_scheme ~register ?(force_mutual=false) env ?(isrec=true) l = |
| 380 |
let sigma = Evd.from_env env in |
| 381 |
let _,_,ind,_ = match l with | x::_ -> x | [] -> assert false in |
| 382 |
let sigma, (ind, inst) = Evd.fresh_inductive_instance env sigma ~rigid:UnivRigid ind in |
| 383 |
let sigma, lrecspec = |
| 384 |
List.fold_left_map (fun sigma (_,dep,ind,sort) -> |
| 385 |
let sigma, sort = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma sort in |
| 386 |
(sigma, (ind,dep,sort))) |
| 387 |
sigma |
| 388 |
l |
| 389 |
in |
| 390 |
let sigma, listdecl = |
| 391 |
if isrec then Indrec.build_mutual_induction_scheme env sigma ~force_mutual lrecspec inst |
| 392 |
else |
| 393 |
List.fold_left_map (fun sigma (ind,dep,sort) -> |
| 394 |
let sigma, c, _ = Indrec.build_case_analysis_scheme env sigma (ind, inst) dep sort in |
| 395 |
sigma, c) |
| 396 |
sigma lrecspec |
| 397 |
in |
| 398 |
let poly = |
| 399 |
|
| 400 |
|
| 401 |
let _,_,ind,_ = List.hd l in |
| 402 |
Global.is_polymorphic (Names.GlobRef.IndRef ind) |
| 403 |
in |
| 404 |
let is_mutual = isrec && List.length listdecl > 1 in |
| 405 |
let declare decl ({CAst.v=fi; loc},dep,ind, sort) = |
| 406 |
let decltype = Retyping.get_type_of env sigma decl in |
| 407 |
let cst = define ?loc ~poly fi sigma decl (Some decltype) in |
| 408 |
let kind = |
| 409 |
let open Elimschemes in |
| 410 |
let open UnivGen.QualityOrSet in |
| 411 |
if not register then None |
| 412 |
else if is_mutual then None |
| 413 |
else if isrec then Some (elim_scheme ~dep ~to_kind:sort) |
| 414 |
else match sort with |
| 415 |
| Qual (QConstant QType) -> Some (if dep then case_dep else case_nodep) |
| 416 |
| Qual (QConstant QProp) -> Some (if dep then casep_dep else casep_nodep) |
| 417 |
| Set | Qual (QConstant QSProp | QVar _) -> |
| 418 |
|
| 419 |
None |
| 420 |
in |
| 421 |
match kind with |
| 422 |
| None -> () |
| 423 |
| Some kind -> |
| 424 |
|
| 425 |
DeclareScheme.declare_scheme SuperGlobal (Ind_tables.scheme_kind_name kind) (ind, cst) |
| 426 |
in |
| 427 |
let () = List.iter2 declare listdecl l in |
| 428 |
let lrecnames = List.map (fun ({CAst.v},_,_,_) -> v) l in |
| 429 |
Declare.fixpoint_message None lrecnames |
| 430 |
|
| 431 |
let do_scheme ~register env l = |
| 432 |
let isrec = match l with |
| 433 |
| [_, sch] -> sch_isrec sch.sch_type |
| 434 |
| _ -> |
| 435 |
if List.for_all (fun (_,sch) -> sch_isrec sch.sch_type) l |
| 436 |
then true |
| 437 |
else CErrors.user_err Pp.(str "Mutually defined schemes should be recursive.") |
| 438 |
in |
| 439 |
let lnamedepindsort = List.map (name_and_process_scheme env) l in |
| 440 |
do_mutual_induction_scheme ~register env ~isrec lnamedepindsort |
| 441 |
|
| 442 |
let do_scheme_equality ?locmap sch id = |
| 443 |
let mind,_ as ind = smart_ind id in |
| 444 |
match sch with |
| 445 |
| SchemeBooleanEquality | SchemeEquality -> |
| 446 |
declare_beq_scheme ?locmap mind; |
| 447 |
if sch = SchemeEquality then declare_eq_decidability ?locmap mind |
| 448 |
| SchemeRewriting -> |
| 449 |
let loc = Option.bind locmap (fun locmap -> Locmap.lookup ~locmap ind) in |
| 450 |
declare_rewriting_schemes ?loc ind |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
let list_split_rev_at index l = |
| 457 |
let rec aux i acc = function |
| 458 |
hd :: tl when Int.equal i index -> acc, tl |
| 459 |
| hd :: tl -> aux (succ i) (hd :: acc) tl |
| 460 |
| [] -> failwith "List.split_when: Invalid argument" |
| 461 |
in aux 0 [] l |
| 462 |
|
| 463 |
let fold_left' f = function |
| 464 |
[] -> invalid_arg "fold_left'" |
| 465 |
| hd :: tl -> List.fold_left f hd tl |
| 466 |
|
| 467 |
let mk_rocq_and sigma = Evd.fresh_global (Global.env ()) sigma (Rocqlib.lib_ref "core.and.type") |
| 468 |
let mk_rocq_conj sigma = Evd.fresh_global (Global.env ()) sigma (Rocqlib.lib_ref "core.and.conj") |
| 469 |
|
| 470 |
let mk_rocq_prod sigma = Evd.fresh_global (Global.env ()) sigma (Rocqlib.lib_ref "core.prod.type") |
| 471 |
let mk_rocq_pair sigma = Evd.fresh_global (Global.env ()) sigma (Rocqlib.lib_ref "core.prod.intro") |
| 472 |
|
| 473 |
let build_combined_scheme env schemes = |
| 474 |
let sigma = Evd.from_env env in |
| 475 |
let sigma, defs = List.fold_left_map (fun sigma cst -> |
| 476 |
let sigma, c = Evd.fresh_constant_instance env sigma cst in |
| 477 |
let c = on_snd (EConstr.EInstance.kind sigma) c in |
| 478 |
sigma, (c, Typeops.type_of_constant_in env c)) sigma schemes in |
| 479 |
let find_inductive ty = |
| 480 |
let (ctx, arity) = decompose_prod ty in |
| 481 |
let (_, last) = List.hd ctx in |
| 482 |
match Constr.kind last with |
| 483 |
| Constr.App (ind, args) -> |
| 484 |
let ind = Constr.destInd ind in |
| 485 |
let (_,spec) = Inductive.lookup_mind_specif env (fst ind) in |
| 486 |
ctx, ind, spec.mind_nrealargs |
| 487 |
| _ -> ctx, Constr.destInd last, 0 |
| 488 |
in |
| 489 |
let (c, t) = List.hd defs in |
| 490 |
let ctx, ind, nargs = find_inductive t in |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
let inprop = |
| 495 |
let inprop (_,t) = |
| 496 |
UnivGen.QualityOrSet.is_prop |
| 497 |
(Retyping.get_sort_quality_of env sigma (EConstr.of_constr t)) |
| 498 |
in |
| 499 |
List.for_all inprop defs |
| 500 |
in |
| 501 |
let mk_and, mk_conj = |
| 502 |
if inprop |
| 503 |
then (mk_rocq_and, mk_rocq_conj) |
| 504 |
else (mk_rocq_prod, mk_rocq_pair) |
| 505 |
in |
| 506 |
|
| 507 |
let prods = Termops.nb_prod sigma (EConstr.of_constr t) - (nargs + 1) in |
| 508 |
let sigma, rocqand = mk_and sigma in |
| 509 |
let sigma, rocqconj = mk_conj sigma in |
| 510 |
let relargs = Termops.rel_vect 0 prods in |
| 511 |
let concls = List.rev_map |
| 512 |
(fun (cst, t) -> |
| 513 |
Constr.mkApp(Constr.mkConstU cst, relargs), |
| 514 |
snd (decompose_prod_n prods t)) defs in |
| 515 |
let concl_bod, concl_typ = |
| 516 |
fold_left' |
| 517 |
(fun (accb, acct) (cst, x) -> |
| 518 |
Constr.mkApp (EConstr.to_constr sigma rocqconj, [| x; acct; cst; accb |]), |
| 519 |
Constr.mkApp (EConstr.to_constr sigma rocqand, [| x; acct |])) concls |
| 520 |
in |
| 521 |
let ctx, _ = |
| 522 |
list_split_rev_at prods |
| 523 |
(List.rev_map (fun (x, y) -> Context.Rel.Declaration.LocalAssum (x, y)) ctx) in |
| 524 |
let typ = EConstr.of_constr @@ List.fold_left (fun d c -> Term.mkProd_wo_LetIn c d) concl_typ ctx in |
| 525 |
let body = EConstr.of_constr @@ it_mkLambda_or_LetIn concl_bod ctx in |
| 526 |
let sigma = Typing.check env sigma body typ in |
| 527 |
(sigma, body, typ) |
| 528 |
|
| 529 |
let do_combined_scheme name csts = |
| 530 |
let open CAst in |
| 531 |
let sigma,body,typ = build_combined_scheme (Global.env ()) csts in |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
let poly = Global.is_polymorphic (Names.GlobRef.ConstRef (List.hd csts)) in |
| 539 |
ignore (define ~poly ?loc:name.loc name.v sigma body (Some typ)); |
| 540 |
Declare.fixpoint_message None [name.v] |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
let do_scheme_all_predicate ?all_depth ~declare_mind kn mib strpos sAll keyAll = |
| 546 |
|
| 547 |
let env = Global.env () in |
| 548 |
let sigma = Evd.from_env env in |
| 549 |
let sigma, (_, u) = Evd.fresh_inductive_instance ~rigid:UState.univ_rigid env sigma (kn,0) in |
| 550 |
let (uctx, mentry) = AllScheme.generate_all_predicate env sigma kn u mib strpos sAll in |
| 551 |
|
| 552 |
let poly_flag = PolyFlags.make ~univ_poly:true ~collapse_sort_variables:true ~cumulative:true in |
| 553 |
let univs = UState.univ_entry ~poly:poly_flag uctx in |
| 554 |
let kn_nested = declare_mind ?all_depth mentry univs in |
| 555 |
|
| 556 |
let () = Array.iteri (fun i _ -> DeclareScheme.declare_scheme |
| 557 |
SuperGlobal keyAll ((kn,i), GlobRef.IndRef (kn_nested,i)) |
| 558 |
) mib.mind_packets in |
| 559 |
kn_nested |
| 560 |
|
| 561 |
let do_scheme_all_theorem kn mib kn_nested focus strpos sAllThm keyAllThm = |
| 562 |
|
| 563 |
let env = Global.env () in |
| 564 |
let sigma = Evd.from_env env in |
| 565 |
let sigma, (_, u) = Evd.fresh_inductive_instance ~sort_rigid:true ~rigid:UState.univ_rigid env sigma (kn,focus) in |
| 566 |
let (sigma, thm) = AllScheme.generate_all_theorem env sigma kn kn_nested focus u mib strpos in |
| 567 |
|
| 568 |
let uctx = Evd.ustate sigma in |
| 569 |
let uctx = UState.collapse_above_prop_sort_variables ~to_prop:true uctx in |
| 570 |
let uctx = UState.normalize_variables uctx in |
| 571 |
let uctx = UState.minimize uctx in |
| 572 |
let sigma = Evd.set_universe_context sigma uctx in |
| 573 |
let thm = UState.nf_universes uctx (EConstr.to_constr sigma thm) in |
| 574 |
let uctx = UState.restrict uctx (Vars.universes_of_constr thm) in |
| 575 |
let sigma = Evd.set_universe_context sigma uctx in |
| 576 |
|
| 577 |
let poly_flag = PolyFlags.make ~univ_poly:true ~collapse_sort_variables:true ~cumulative:true in |
| 578 |
let info = Declare.Info.make ~poly:poly_flag () in |
| 579 |
let fth_name = Nameops.add_suffix mib.mind_packets.(focus).mind_typename sAllThm in |
| 580 |
let cinfo = Declare.CInfo.make ~name:fth_name ~typ:(None : (Evd.econstr option)) () in |
| 581 |
let fth_ref = Declare.declare_definition ~info:info ~cinfo:cinfo ~opaque:false ~body:(EConstr.of_constr thm) sigma in |
| 582 |
|
| 583 |
let () = DeclareScheme.declare_scheme SuperGlobal keyAllThm ((kn,focus), fth_ref) in |
| 584 |
() |
| 585 |
|
| 586 |
let do_all_forall ?(user_call_scheme=false) ?all_depth ~declare_mind kn strpos = |
| 587 |
let env = Global.env () in |
| 588 |
let mib = Environ.lookup_mind kn env in |
| 589 |
let isPrimRecord = Array.exists (fun ind -> match ind.mind_record with PrimRecord _ -> true | _ -> false) mib.mind_packets in |
| 590 |
if not isPrimRecord then begin |
| 591 |
let (strpos, (sAll, sAllThm), (keyAll, keyAllThm)) = |
| 592 |
AllScheme.compute_positive_uparams_and_suffix env kn mib strpos in |
| 593 |
if List.exists (fun b -> b) strpos then |
| 594 |
let kn_nested = do_scheme_all_predicate ?all_depth ~declare_mind kn mib strpos sAll keyAll in |
| 595 |
Array.iteri (fun focus _ -> do_scheme_all_theorem kn mib kn_nested focus strpos sAllThm keyAllThm) mib.mind_packets |
| 596 |
end |
| 597 |
else |
| 598 |
if user_call_scheme then |
| 599 |
CErrors.user_err Pp.(str "Not implemented for primitive records.") |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
let map_inductive_block ?(locmap=Locmap.default None) f kn n = |
| 605 |
for i=0 to n-1 do |
| 606 |
let loc = Ind_tables.Locmap.lookup ~locmap (kn,i) in |
| 607 |
f ?loc (kn,i) |
| 608 |
done |
| 609 |
|
| 610 |
type declare_mind_function = ?all_depth:int -> |
| 611 |
Entries.mutual_inductive_entry -> |
| 612 |
UState.named_universes_entry -> |
| 613 |
MutInd.t |
| 614 |
|
| 615 |
|
| 616 |
let { Goptions.get = default_all_depth } = |
| 617 |
Goptions.declare_int_option_and_ref ~key:["Depth";"Scheme";"All"] ~value:0 () |
| 618 |
|
| 619 |
let default_all_depth kn mib = |
| 620 |
let mib = Global.lookup_mind kn in |
| 621 |
if Inductiveops.mis_is_nested kn mib |
| 622 |
then default_all_depth () -1 |
| 623 |
else default_all_depth () |
| 624 |
|
| 625 |
let declare_default_schemes ?locmap ?all_depth ~(declare_mind:declare_mind_function) kn = |
| 626 |
let mib = Global.lookup_mind kn in |
| 627 |
let all_depth = Option.default (default_all_depth kn mib) all_depth in |
| 628 |
let n = Array.length mib.mind_packets in |
| 629 |
if !elim_flag && (mib.mind_finite <> Declarations.BiFinite || !bifinite_elim_flag) |
| 630 |
&& mib.mind_typing_flags.check_positive then |
| 631 |
declare_induction_schemes kn ?locmap; |
| 632 |
if all_depth > 0 && mib.mind_finite <> CoFinite then |
| 633 |
do_all_forall ~all_depth:(all_depth-1) ~declare_mind:declare_mind kn None; |
| 634 |
if !case_flag then map_inductive_block ?locmap declare_one_case_analysis_scheme kn n; |
| 635 |
if is_eq_flag() then try_declare_beq_scheme kn ?locmap; |
| 636 |
if !eq_dec_flag then try_declare_eq_decidability kn ?locmap; |
| 637 |
if !rewriting_flag then map_inductive_block ?locmap declare_congr_scheme kn n; |
| 638 |
if !rewriting_flag then map_inductive_block ?locmap declare_rewriting_schemes kn n |
| 639 |
|
| 640 |
module Internal = struct |
| 641 |
let do_scheme_all ~user_call_scheme ~declare_mind id strpos = |
| 642 |
let kn,_ = smart_ind id in |
| 643 |
do_all_forall ~user_call_scheme ~declare_mind kn strpos |
| 644 |
end |