Source file elimschemes.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
open Constr
open Indrec
open Declarations
open Ind_tables
open UnivGen
let build_induction_scheme_in_type env dep sort ind =
let sigma = Evd.from_env env in
let sigma, pind = Evd.fresh_inductive_instance ~rigid:UState.univ_rigid env sigma ind in
let pind = Util.on_snd EConstr.EInstance.make pind in
let sigma, sort = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma sort in
let sigma, c = build_induction_scheme env sigma pind dep sort in
EConstr.to_constr sigma c, Evd.ustate sigma
let change_sort_arity sort =
let rec drec a = match kind a with
| Cast (c,_,_) -> drec c
| Prod (n,t,c) -> let s, c' = drec c in s, mkProd (n, t, c')
| LetIn (n,b,t,c) -> let s, c' = drec c in s, mkLetIn (n,b,t,c')
| Sort s -> s, mkSort sort
| _ -> assert false
in
drec
(** [weaken_sort_scheme env sigma s n c t] derives by subtyping from [c:t]
whose conclusion is quantified on [Type i] at position [n] of [t] a
scheme quantified on sort [s]. [s] is declared less or equal to [i]. *)
let weaken_sort_scheme env evd sort npars term ty =
let open Context.Rel.Declaration in
let evdref = ref evd in
let rec drec ctx np elim =
match kind elim with
| Prod (n,t,c) ->
let ctx = LocalAssum (n, t) :: ctx in
if Int.equal np 0 then
let osort, t' = change_sort_arity (EConstr.ESorts.kind !evdref sort) t in
evdref := Evd.set_leq_sort !evdref sort (EConstr.ESorts.make osort);
mkProd (n, t', c),
mkLambda (n, t', mkApp(term, Context.Rel.instance mkRel 0 ctx))
else
let c',term' = drec ctx (np-1) c in
mkProd (n, t, c'), mkLambda (n, t, term')
| LetIn (n,b,t,c) ->
let ctx = LocalDef (n, b, t) :: ctx in
let c',term' = drec ctx np c in
mkLetIn (n,b,t,c'), mkLetIn (n,b,t,term')
| _ -> CErrors.anomaly ~label:"weaken_sort_scheme" (Pp.str "wrong elimination type.")
in
let ty, term = drec [] npars ty in
!evdref, ty, term
let optimize_non_type_induction_scheme kind dep sort env _handle ind =
match lookup_scheme kind ind with
| Some cte ->
let sigma = Evd.from_env env in
let sigma, cte = Evd.fresh_constant_instance env sigma cte in
let c = mkConstU cte in
let t = Typeops.type_of_constant_in env cte in
let (mib,mip) = Inductive.lookup_mind_specif env ind in
let npars =
if (Inductiveops.mis_is_recursive_subset env [ind] (Rtree.Kind.make mip.mind_recargs))
then
mib.mind_nparams_rec
else
mib.mind_nparams in
let sigma, sort = Evd.fresh_sort_in_quality sigma sort in
let sigma, t', c' = weaken_sort_scheme env sigma sort npars c t in
let sigma = Evd.minimize_universes sigma in
(Evarutil.nf_evars_universes sigma c', Evd.ustate sigma)
| None ->
build_induction_scheme_in_type env dep sort ind
let rect_dep =
declare_individual_scheme_object "rect_dep"
(fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.qtype x)
let rec_dep =
declare_individual_scheme_object "rec_dep"
(optimize_non_type_induction_scheme rect_dep true QualityOrSet.set)
let ind_dep =
declare_individual_scheme_object "ind_dep"
(optimize_non_type_induction_scheme rec_dep true QualityOrSet.prop)
let sind_dep =
declare_individual_scheme_object "sind_dep"
(fun env _ x -> build_induction_scheme_in_type env true QualityOrSet.sprop x)
let rect_nodep =
declare_individual_scheme_object "rect_nodep"
(fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.qtype x)
let rec_nodep =
declare_individual_scheme_object "rec_nodep"
(optimize_non_type_induction_scheme rect_nodep false QualityOrSet.set)
let ind_nodep =
declare_individual_scheme_object "ind_nodep"
(optimize_non_type_induction_scheme rec_nodep false QualityOrSet.prop)
let sind_nodep =
declare_individual_scheme_object "sind_nodep"
(fun env _ x -> build_induction_scheme_in_type env false QualityOrSet.sprop x)
let elim_scheme ~dep ~to_kind =
let open QualityOrSet in
match to_kind with
| Qual q ->
begin
match q with
| QConstant QSProp when dep -> sind_dep
| QConstant QProp when dep -> ind_dep
| (QConstant QType | QVar _) when dep -> rect_dep
| QConstant QSProp -> sind_nodep
| QConstant QProp -> ind_nodep
| QConstant QType | QVar _ -> rect_nodep
end
| Set -> if dep then rec_dep else rec_nodep
let build_case_analysis_scheme_in_type env dep sort ind =
let sigma = Evd.from_env env in
let (sigma, indu) = Evd.fresh_inductive_instance env sigma ind in
let indu = Util.on_snd EConstr.EInstance.make indu in
let sigma, sort = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma sort in
let (sigma, c) = build_case_analysis_scheme env sigma indu dep sort in
let (c, _) = Indrec.eval_case_analysis c in
EConstr.Unsafe.to_constr c, Evd.ustate sigma
let case_dep =
declare_individual_scheme_object "case_dep"
(fun env _ x -> build_case_analysis_scheme_in_type env true QualityOrSet.qtype x)
let case_nodep =
declare_individual_scheme_object "case_nodep"
(fun env _ x -> build_case_analysis_scheme_in_type env false QualityOrSet.qtype x)
let casep_dep =
declare_individual_scheme_object "casep_dep"
(fun env _ x -> build_case_analysis_scheme_in_type env true QualityOrSet.prop x)
let casep_nodep =
declare_individual_scheme_object "casep_nodep"
(fun env _ x -> build_case_analysis_scheme_in_type env false QualityOrSet.prop x)