Debian Codegraph @

pretyping/evardefine.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 open Pp
12 open Names
13 open Constr
14 open Context
15 open Termops
16 open EConstr
17 open Vars
18 open Namegen
19 open Evd
20 open Evarutil
21 open Evar_kinds
22 open Pretype_errors
23
24 module RelDecl = Context.Rel.Declaration
25
26 let env_nf_evar sigma env =
27 let nf_evar c = nf_evar sigma c in
28 process_rel_context
29 (fun d e -> push_rel (RelDecl.map_constr nf_evar d) e) env
30
31 let env_nf_betaiotaevar sigma env =
32 process_rel_context
33 (fun d env ->
34 push_rel (RelDecl.map_constr (fun c -> Reductionops.nf_betaiota env sigma c) d) env) env
35
36 (****************************************)
37 (* Operations on value/type constraints *)
38 (****************************************)
39
40 type type_constraint = EConstr.types option
41
42 type val_constraint = EConstr.constr option
43
44 (* Old comment...
45 * Basically, we have the following kind of constraints (in increasing
46 * strength order):
47 * (false,(None,None)) -> no constraint at all
48 * (true,(None,None)) -> we must build a judgement which _TYPE is a kind
49 * (_,(None,Some ty)) -> we must build a judgement which _TYPE is ty
50 * (_,(Some v,_)) -> we must build a judgement which _VAL is v
51 * Maybe a concrete datatype would be easier to understand.
52 * We differentiate (true,(None,None)) from (_,(None,Some Type))
53 * because otherwise Case(s) would be misled, as in
54 * (n:nat) Case n of bool [_]nat end would infer the predicate Type instead
55 * of Set.
56 *)
57
58 (* The empty type constraint *)
59 let empty_tycon = None
60
61 (* Builds a type constraint *)
62 let mk_tycon ty = Some ty
63
64 (* Constrains the value of a type *)
65 let empty_valcon = None
66
67 (* Builds a value constraint *)
68 let mk_valcon c = Some c
69
70 let idx = Namegen.default_dependent_ident
71
72 (* Refining an evar to a product *)
73
74 let define_pure_evar_as_product env evd na evk =
75 let open Context.Named.Declaration in
76 let evi = Evd.find_undefined evd evk in
77 let evenv = evar_env env evi in
78 let id = match na with Some id -> id | None -> idx in
79 let id = next_ident_away id (Environ.ids_of_named_context_val (Evd.evar_hyps evi)) in
80 let concl = Reductionops.whd_all evenv evd (Evd.evar_concl evi) in
81 let s = destSort evd concl in
82 let evksrc = evar_source evi in
83 let src = subterm_source evk ~where:Domain evksrc in
84 let evd1,(dom,u1) =
85 new_type_evar evenv evd univ_flexible_alg ~src ~filter:(evar_filter evi)
86 in
87 let rdom = ESorts.relevance_of_sort u1 in
88 let evd2,rng =
89 let newenv = push_named (LocalAssum (make_annot id rdom, dom)) evenv in
90 let src = subterm_source evk ~where:Codomain evksrc in
91 let filter = Filter.extend 1 (evar_filter evi) in
92 if Environ.is_impredicative_sort env (ESorts.kind evd1 s) then
93 (* Impredicative product, conclusion must fall in [Prop]. *)
94 new_evar newenv evd1 concl ~src ~filter
95 else
96 let status = univ_flexible_alg in
97 let evd3, (rng, srng) =
98 new_type_evar newenv evd1 status ~src ~filter
99 in
100 let prods = Typeops.sort_of_product env (ESorts.kind evd3 u1) (ESorts.kind evd3 srng) in
101 let evd3 = Evd.set_leq_sort evd3 (ESorts.make prods) s in
102 evd3, rng
103 in
104 let prod = mkProd (make_annot (Name id) rdom, dom, subst_var evd2 id rng) in
105 let evd3 = Evd.define evk prod evd2 in
106 evd3,prod
107
108 (* Refine an applied evar to a product and returns its instantiation *)
109
110 let define_evar_as_product env evd ?name (evk,args) =
111 let evd,prod = define_pure_evar_as_product env evd name evk in
112 (* Quick way to compute the instantiation of evk with args *)
113 let na,dom,rng = destProd evd prod in
114 let evdom = mkEvar (fst (destEvar evd dom), args) in
115 let evrngargs = SList.cons (mkRel 1) (SList.Skip.map (lift 1) args) in
116 let evrng = mkEvar (fst (destEvar evd rng), evrngargs) in
117 evd, mkProd (na, evdom, evrng)
118
119 (* Refine an evar with an abstraction
120
121 I.e., solve x1..xq |- ?e:T(x1..xq) with e:=λy:A.?e'[x1..xq,y] where:
122 - either T(x1..xq) = πy:A(x1..xq).B(x1..xq,y)
123 or T(x1..xq) = ?d[x1..xq] and we define ?d := πy:?A.?B
124 with x1..xq |- ?A:Type and x1..xq,y |- ?B:Type
125 - x1..xq,y:A |- ?e':B
126 *)
127
128 let define_pure_evar_as_lambda env evd name evk =
129 let open Context.Named.Declaration in
130 let evi = Evd.find_undefined evd evk in
131 let evenv = evar_env env evi in
132 let typ = Reductionops.whd_all evenv evd (evar_concl evi) in
133 let evd1,(na,dom,rng) = match EConstr.kind evd typ with
134 | Prod (na,dom,rng) -> (evd,(na,dom,rng))
135 | Evar ev' -> let evd,typ = define_evar_as_product env evd ?name ev' in evd,destProd evd typ
136 | _ -> error_not_product env evd typ in
137 let avoid = Environ.ids_of_named_context_val (Evd.evar_hyps evi) in
138 let id =
139 map_annot (fun na -> next_name_away_with_default_using_types evenv evd "x" na avoid
140 (Reductionops.whd_evar evd dom)) na
141 in
142 let newenv = push_named (LocalAssum (id, dom)) evenv in
143 let filter = Filter.extend 1 (evar_filter evi) in
144 let src = subterm_source evk ~where:Body (evar_source evi) in
145 let abstract_arguments = Abstraction.abstract_last (Evd.evar_abstract_arguments evi) in
146 let evty = subst1 (mkVar id.binder_name) rng in
147 let typeclass_candidate = Typeclasses.is_maybe_class_type newenv evd1 evty in
148 let evd2,body = new_evar ~typeclass_candidate newenv evd1 ~src evty ~filter ~abstract_arguments in
149 let lam = mkLambda (map_annot Name.mk_name id, dom, subst_var evd2 id.binder_name body) in
150 Evd.define evk lam evd2, lam
151
152 let define_evar_as_lambda env evd ?name (evk,args) =
153 let evd,lam = define_pure_evar_as_lambda env evd name evk in
154 (* Quick way to compute the instantiation of evk with args *)
155 let na,dom,body = destLambda evd lam in
156 let evbodyargs = SList.cons (mkRel 1) (SList.Skip.map (lift 1) args) in
157 let evbody = mkEvar (fst (destEvar evd body), evbodyargs) in
158 evd, mkLambda (na, dom, evbody)
159
160 let rec evar_absorb_arguments env evd (evk,args as ev) = function
161 | [] -> evd,ev
162 | a::l ->
163 (* TODO: optimize and avoid introducing intermediate evars *)
164 let evd,lam = define_pure_evar_as_lambda env evd None evk in
165 let _,_,body = destLambda evd lam in
166 let evk = fst (destEvar evd body) in
167 evar_absorb_arguments env evd (evk, SList.cons a args) l
168
169 (* Refining an evar to a sort *)
170
171 let define_evar_as_sort env evd (ev,args) =
172 let evd, s = new_sort_variable univ_rigid evd in
173 let evi = Evd.find_undefined evd ev in
174 let concl = Reductionops.whd_all (evar_env env evi) evd (Evd.evar_concl evi) in
175 let sort = destSort evd concl in
176 let evd' = Evd.define ev (mkSort s) evd in
177 Evd.set_leq_sort evd' (ESorts.super evd' s) sort, s
178
179 (* Unify with unknown array *)
180
181 let rec presplit env sigma c =
182 let c = Reductionops.whd_all env sigma c in
183 match EConstr.kind sigma c with
184 | App (h,args) when isEvar sigma h ->
185 let sigma, lam = define_evar_as_lambda env sigma (destEvar sigma h) in
186 (* XXX could be just whd_all -> no recursion? *)
187 presplit env sigma (mkApp (lam, args))
188 | _ -> sigma, c
189
190 let define_pure_evar_as_array env sigma evk =
191 let evi = Evd.find_undefined sigma evk in
192 let evenv = evar_env env evi in
193 let evksrc = evar_source evi in
194 let src = subterm_source evk ~where:Domain evksrc in
195 let sigma, u = new_univ_level_variable univ_flexible sigma in
196 let s' = ESorts.make @@ Sorts.sort_of_univ @@ Univ.Universe.make u in
197 let sigma, ty = new_evar evenv sigma ~typeclass_candidate:false ~src ~filter:(evar_filter evi) (mkSort s') in
198 let concl = Reductionops.whd_all evenv sigma (Evd.evar_concl evi) in
199 let s = destSort sigma concl in
200 (* array@{u} ty : Type@{u} <= Type@{s} *)
201 let sigma = Evd.set_leq_sort sigma s' s in
202 let ar = Typeops.type_of_array env (UVars.Instance.of_array ([||],[|u|])) in
203 let sigma = Evd.define evk (mkApp (EConstr.of_constr ar, [| ty |])) sigma in
204 sigma
205
206 let is_array_const env sigma c =
207 match EConstr.kind sigma c with
208 | Const (cst,_) ->
209 (match (Environ.retroknowledge env).Retroknowledge.retro_array with
210 | None -> false
211 | Some cst' -> Environ.QConstant.equal env cst cst')
212 | _ -> false
213
214 let split_as_array env sigma0 = function
215 | None -> sigma0, None
216 | Some c ->
217 let sigma, c = presplit env sigma0 c in
218 match EConstr.kind sigma c with
219 | App (h,[|ty|]) when is_array_const env sigma h -> sigma, Some ty
220 | Evar ev ->
221 let sigma = define_pure_evar_as_array env sigma (fst ev) in
222 let ty = match EConstr.kind sigma c with
223 | App (_,[|ty|]) -> ty
224 | _ -> assert false
225 in
226 sigma, Some ty
227 | _ -> sigma0, None
228
229 let valcon_of_tycon x = x
230 let lift_tycon n = Option.map (lift n)
231
232 let pr_tycon env sigma = function
233 None -> str "None"
234 | Some t -> Termops.Internal.print_constr_env env sigma t