Debian Codegraph @

tactics/elimschemes.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Hugo Herbelin from contents related to inductive schemes
12 initially developed by Christine Paulin (induction schemes), Vincent
13 Siles (decidable equality and boolean equality) and Matthieu Sozeau
14 (combined scheme) in file command.ml, Sep 2009 *)
15
16 (* This file builds schemes related to case analysis and recursion schemes *)
17
18 open Names
19 open Indrec
20 open Declarations
21 open Ind_tables
22 open UnivGen
23
24 let prop_but_default_dependent_elim =
25 Summary.ref ~name:"PROP-BUT-DEFAULT-DEPENDENT-ELIM" Indset_env.empty
26
27 let inPropButDefaultDepElim : inductive -> Libobject.obj =
28 Libobject.declare_object @@
29 Libobject.superglobal_object "prop_but_default_dependent_elim"
30 ~cache:(fun i ->
31 prop_but_default_dependent_elim := Indset_env.add i !prop_but_default_dependent_elim)
32 ~subst:(Some (fun (subst,i) -> Mod_subst.subst_ind subst i))
33 ~discharge:(fun i -> Some i)
34
35 let declare_prop_but_default_dependent_elim i =
36 Lib.add_leaf (inPropButDefaultDepElim i)
37
38 let is_prop_but_default_dependent_elim i = Indset_env.mem i !prop_but_default_dependent_elim
39
40 let pseudo_sort_quality_for_elim ind mip =
41 let s = mip.mind_sort in
42 if Sorts.is_prop s && is_prop_but_default_dependent_elim ind
43 then Sorts.Quality.qtype
44 else Sorts.quality s
45
46 let default_case_analysis_dependence env ind =
47 let _, mip as specif = Inductive.lookup_mind_specif env ind in
48 Inductiveops.has_dependent_elim specif
49 && (not (Sorts.is_prop mip.mind_sort) || is_prop_but_default_dependent_elim ind)
50
51
52 (* **************************************************** *)
53 (* Induction/recursion schemes *)
54 (* **************************************************** *)
55
56 let build_induction_scheme_in_type env dep sort ind =
57 let sigma = Evd.from_env env in
58 let sigma, pind = Evd.fresh_inductive_instance ~rigid:UState.univ_rigid env sigma ind in
59 let sigma, sort = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma sort in
60 let sigma, c = build_induction_scheme env sigma pind dep sort in
61 EConstr.to_constr sigma c, Evd.ustate sigma
62
63 let rect_dep =
64 declare_individual_scheme_object "rect_dep"
65 (fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.qtype x)
66
67 let rec_dep =
68 declare_individual_scheme_object "rec_dep"
69 (fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.set x)
70
71 let ind_dep =
72 declare_individual_scheme_object "ind_dep"
73 (fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.prop x)
74
75 let sind_dep =
76 declare_individual_scheme_object "sind_dep"
77 (fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.sprop x)
78
79 let rect_nodep =
80 declare_individual_scheme_object "rect_nodep"
81 (fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.qtype x)
82
83 let rec_nodep =
84 declare_individual_scheme_object "rec_nodep"
85 (fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.set x)
86
87 let ind_nodep =
88 declare_individual_scheme_object "ind_nodep"
89 (fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.prop x)
90
91 let sind_nodep =
92 declare_individual_scheme_object "sind_nodep"
93 (fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.sprop x)
94
95 let elim_scheme ~dep ~to_kind =
96 let open QualityOrSet in
97 match to_kind with
98 | Qual q ->
99 begin
100 match q with
101 | QConstant QSProp when dep -> sind_dep
102 | QConstant QProp when dep -> ind_dep
103 | (QConstant QType | QVar _) when dep -> rect_dep
104 | QConstant QSProp -> sind_nodep
105 | QConstant QProp -> ind_nodep
106 | QConstant QType | QVar _ -> rect_nodep
107 end
108 | Set -> if dep then rec_dep else rec_nodep
109
110 let elimination_suffix =
111 let open UnivGen.QualityOrSet in
112 let open Sorts.Quality in
113 function
114 | Qual (QConstant QSProp) -> "_sind"
115 | Qual (QConstant QProp) -> "_ind"
116 | Qual (QConstant QType) | Qual (QVar _) -> "_rect"
117 | Set -> "_rec"
118
119 let make_elimination_ident id s = Nameops.add_suffix id (elimination_suffix s)
120
121 (* Look up function for the default elimination constant *)
122
123 let lookup_eliminator_by_name env ind_sp s =
124 let open Names in
125 let open Environ in
126 let kn,i = ind_sp in
127 let mpu = KerName.modpath @@ MutInd.user kn in
128 let mpc = KerName.modpath @@ MutInd.canonical kn in
129 let ind_id = (lookup_mind kn env).mind_packets.(i).mind_typename in
130 let id = make_elimination_ident ind_id s in
131 let knu = KerName.make mpu id in
132 let knc = KerName.make mpc id in
133 (* Try first to get an eliminator defined in the same section as the *)
134 (* inductive type *)
135 let cst = Constant.make knu knc in
136 if mem_constant cst env then GlobRef.ConstRef cst
137 else
138 (* Then try to get a user-defined eliminator in some other places *)
139 (* using short name (e.g. for "eq_rec") *)
140 try Nametab.locate (Libnames.qualid_of_ident id)
141 with Not_found ->
142 CErrors.user_err
143 Pp.(strbrk "Cannot find the elimination combinator " ++
144 Id.print id ++ strbrk ", the elimination of the inductive definition " ++
145 Nametab.pr_global_env Id.Set.empty (GlobRef.IndRef ind_sp) ++
146 strbrk " on sort " ++ UnivGen.QualityOrSet.pr Sorts.QVar.raw_pr s ++
147 strbrk " is probably not allowed.")
148
149 let deprecated_lookup_by_name =
150 CWarnings.create ~name:"deprecated-lookup-elim-by-name" ~category:Deprecation.Version.v9_2
151 Pp.(fun (env,ind,to_kind,r) ->
152 let pp_scheme () s = str (scheme_kind_name s) in
153 fmt "Found unregistered eliminator %t for %t by name.@ \
154 Use \"Register Scheme\" with it instead@ \
155 (\"as %a\" if dependent or \"as %a\" if non dependent)."
156 (fun () -> Termops.pr_global_env env r)
157 (fun () -> Termops.pr_global_env env (IndRef ind))
158 pp_scheme (elim_scheme ~dep:true ~to_kind)
159 pp_scheme (elim_scheme ~dep:false ~to_kind))
160
161 let lookup_eliminator_by_name env ind s =
162 let r = lookup_eliminator_by_name env ind s in
163 deprecated_lookup_by_name (env,ind,s,r);
164 r
165
166 let lookup_eliminator env ind s =
167 let nodep_scheme_first =
168 (* compat, add an option to control this and remove someday *)
169 let _, mip = Inductive.lookup_mind_specif env ind in
170 Sorts.is_prop mip.mind_sort && not (is_prop_but_default_dependent_elim ind)
171 in
172 let schemes =
173 List.map (fun dep -> elim_scheme ~dep ~to_kind:s)
174 (if nodep_scheme_first then [false;true] else [true;false])
175 in
176 match List.find_map (fun scheme -> lookup_scheme scheme ind) schemes with
177 | Some c -> c
178 | None ->
179 (* XXX also lookup_scheme at less precise sort? eg if s=set try to_kind:qtype *)
180 lookup_eliminator_by_name env ind s
181
182
183 (* **************************************************** *)
184 (* Case Analysis *)
185 (* **************************************************** *)
186
187 let build_case_analysis_scheme_in_type env dep sort ind =
188 let sigma = Evd.from_env env in
189 let (sigma, indu) = Evd.fresh_inductive_instance env sigma ind in
190 let sigma, sort = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma sort in
191 let (sigma, c, _) = build_case_analysis_scheme env sigma indu dep sort in
192 EConstr.Unsafe.to_constr c, Evd.ustate sigma
193
194 let case_dep =
195 declare_individual_scheme_object "case_dep"
196 (fun env _ x -> build_case_analysis_scheme_in_type env true QualityOrSet.qtype x)
197
198 let case_nodep =
199 declare_individual_scheme_object "case_nodep"
200 (fun env _ x -> build_case_analysis_scheme_in_type env false QualityOrSet.qtype x)
201
202 let casep_dep =
203 declare_individual_scheme_object "casep_dep"
204 (fun env _ x -> build_case_analysis_scheme_in_type env true QualityOrSet.prop x)
205
206 let casep_nodep =
207 declare_individual_scheme_object "casep_nodep"
208 (fun env _ x -> build_case_analysis_scheme_in_type env false QualityOrSet.prop x)