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
open Sorts
open Constr
open Indrec
open Declarations
open Typeops
open Ind_tables
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_family ~rigid:UnivRigid sigma sort in
let sigma, c = build_induction_scheme env sigma pind dep sort in
EConstr.to_constr sigma c, Evd.evar_universe_context 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 := (if false then Evd.set_eq_sort else Evd.set_leq_sort) env !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 = 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 [ind] mip.mind_recargs)
then
mib.mind_nparams_rec
else
mib.mind_nparams in
let sigma, sort = Evd.fresh_sort_in_family 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.evar_universe_context 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 InType x)
let rec_dep =
declare_individual_scheme_object "rec_dep"
(optimize_non_type_induction_scheme rect_dep true InSet)
let ind_dep =
declare_individual_scheme_object "ind_dep"
(optimize_non_type_induction_scheme rec_dep true InProp)
let sind_dep =
declare_individual_scheme_object "sind_dep"
(fun env _ x -> build_induction_scheme_in_type env true InSProp x)
let rect_nodep =
declare_individual_scheme_object "rect_nodep"
(fun env _ x -> build_induction_scheme_in_type env false InType x)
let rec_nodep =
declare_individual_scheme_object "rec_nodep"
(optimize_non_type_induction_scheme rect_nodep false InSet)
let ind_nodep =
declare_individual_scheme_object "ind_nodep"
(optimize_non_type_induction_scheme rec_nodep false InProp)
let sind_nodep =
declare_individual_scheme_object "sind_nodep"
(fun env _ x -> build_induction_scheme_in_type env false InSProp x)
let elim_scheme ~dep ~to_kind =
match dep, to_kind with
| false, InSProp -> sind_nodep
| false, InProp -> ind_nodep
| false, InSet -> rec_nodep
| false, (InType | InQSort) -> rect_nodep
| true, InSProp -> sind_dep
| true, InProp -> ind_dep
| true, InSet -> rec_dep
| true, (InType | InQSort) -> rect_dep
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_family ~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.evar_universe_context sigma
let case_dep =
declare_individual_scheme_object "case_dep"
(fun env _ x -> build_case_analysis_scheme_in_type env true InType x)
let case_nodep =
declare_individual_scheme_object "case_nodep"
(fun env _ x -> build_case_analysis_scheme_in_type env false InType x)
let casep_dep =
declare_individual_scheme_object "casep_dep"
(fun env _ x -> build_case_analysis_scheme_in_type env true InProp x)
let casep_nodep =
declare_individual_scheme_object "casep_nodep"
(fun env _ x -> build_case_analysis_scheme_in_type env false InProp x)