Debian Codegraph @

vernac/retrieveObl.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * INRIA, CNRS and contributors - Copyright 1999-2018 *)
4 (* <O___,, * (see CREDITS file for the list of authors) *)
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 Names
12
13 (**
14 - Get types of existentials ;
15 - Flatten dependency tree (prefix order) ;
16 - Replace existentials by de Bruijn indices in term, applied to the right arguments ;
17 - Apply term prefixed by quantification on "existentials".
18 *)
19
20 let check_evars env evm =
21 Evar.Map.iter
22 (fun key evi ->
23 if Evd.is_obligation_evar evm key then ()
24 else
25 let loc, k = Evd.evar_source evi in
26 Pretype_errors.error_unsolvable_implicit ?loc env evm key None)
27 (Evd.undefined_map evm)
28
29 type obligation_info =
30 ( Names.Id.t
31 * Constr.types
32 * Evar_kinds.t Loc.located
33 * (bool * Evar_kinds.obligation_definition_status)
34 * Int.Set.t
35 * unit Proofview.tactic option )
36 array
37
38 type oblinfo =
39 { ev_name : int * Id.t
40 ; ev_hyps : EConstr.named_context
41 ; ev_status : bool * Evar_kinds.obligation_definition_status
42 ; ev_chop : int option
43 ; ev_src : Evar_kinds.t Loc.located
44 ; ev_typ : Constr.types
45 ; ev_tac : unit Proofview.tactic option
46 ; ev_deps : Int.Set.t }
47
48 (** Substitute evar references in t using de Bruijn indices,
49 where n binders were passed through. *)
50
51 let succfix (depth, fixrels) = (succ depth, List.map succ fixrels)
52
53 let subst_evar_constr evm evs n idf t =
54 let seen = ref Int.Set.empty in
55 let transparent = ref Id.Set.empty in
56 let evar_info id = CList.assoc_f Evar.equal id evs in
57 let rec substrec (depth, fixrels) c =
58 match EConstr.kind evm c with
59 | Constr.Evar (k, args) ->
60 let {ev_name = id, idstr; ev_hyps = hyps; ev_chop = chop} =
61 try evar_info k
62 with Not_found ->
63 CErrors.anomaly ~label:"eterm"
64 Pp.(
65 str "existential variable "
66 ++ int (Evar.repr k)
67 ++ str " not found.")
68 in
69 seen := Int.Set.add id !seen;
70 (* Evar arguments are created in inverse order,
71 and we must not apply to defined ones (i.e. LetIn's)
72 *)
73 let args =
74 let args = Evd.expand_existential evm (k, args) in
75 let n = match chop with None -> 0 | Some c -> c in
76 let l, r = CList.chop n (List.rev args) in
77 List.rev r
78 in
79 let args =
80 let rec aux hyps args acc =
81 let open Context.Named.Declaration in
82 match (hyps, args) with
83 | LocalAssum _ :: tlh, c :: tla ->
84 aux tlh tla (substrec (depth, fixrels) c :: acc)
85 | LocalDef _ :: tlh, _ :: tla -> aux tlh tla acc
86 | [], [] -> acc
87 | _, _ -> acc
88 (*failwith "subst_evars: invalid argument"*)
89 in
90 aux hyps args []
91 in
92 if
93 List.exists
94 (fun x ->
95 match EConstr.kind evm x with
96 | Constr.Rel n -> Int.List.mem n fixrels
97 | _ -> false)
98 args
99 then transparent := Id.Set.add idstr !transparent;
100 EConstr.mkApp (idf idstr, Array.of_list args)
101 | Constr.Fix _ ->
102 EConstr.map_with_binders evm succfix substrec (depth, 1 :: fixrels) c
103 | _ -> EConstr.map_with_binders evm succfix substrec (depth, fixrels) c
104 in
105 let t' = substrec (0, []) t in
106 (EConstr.to_constr evm t', !seen, !transparent)
107
108 (** Substitute variable references in t using de Bruijn indices,
109 where n binders were passed through. *)
110 let subst_vars acc n t =
111 let var_index id = Util.List.index Id.equal id acc in
112 let rec substrec depth c =
113 match Constr.kind c with
114 | Constr.Var v -> (
115 try Constr.mkRel (depth + var_index v) with Not_found -> c )
116 | _ -> Constr.map_with_binders succ substrec depth c
117 in
118 substrec 0 t
119
120 (** Rewrite type of an evar ([ H1 : t1, ... Hn : tn |- concl ])
121 to a product : forall H1 : t1, ..., forall Hn : tn, concl.
122 Changes evars and hypothesis references to variable references.
123 *)
124 let etype_of_evar evm evs hyps concl =
125 let open Context.Named.Declaration in
126 let rec aux acc n = function
127 | decl :: tl -> (
128 let t', s, trans =
129 subst_evar_constr evm evs n EConstr.mkVar
130 (Context.Named.Declaration.get_type decl)
131 in
132 let t'' = subst_vars acc 0 t' in
133 let rest, s', trans' =
134 aux (Context.Named.Declaration.get_id decl :: acc) (succ n) tl
135 in
136 let s' = Int.Set.union s s' in
137 let trans' = Id.Set.union trans trans' in
138 match decl with
139 | LocalDef (id, c, _) ->
140 let c', s'', trans'' = subst_evar_constr evm evs n EConstr.mkVar c in
141 let c' = subst_vars acc 0 c' in
142 ( Term.mkNamedProd_or_LetIn (LocalDef (EConstr.Unsafe.to_binder_annot id, c', t'')) rest
143 , Int.Set.union s'' s'
144 , Id.Set.union trans'' trans' )
145 | LocalAssum (id, _) ->
146 (Term.mkNamedProd_or_LetIn (LocalAssum (EConstr.Unsafe.to_binder_annot id, t'')) rest, s', trans') )
147 | [] ->
148 let t', s, trans = subst_evar_constr evm evs n EConstr.mkVar concl in
149 (subst_vars acc 0 t', s, trans)
150 in
151 aux [] 0 (List.rev hyps)
152
153 let trunc_named_context n ctx =
154 let len = List.length ctx in
155 CList.firstn (len - n) ctx
156
157 let rec chop_product n t =
158 let pop t = Vars.lift (-1) t in
159 if Int.equal n 0 then Some t
160 else
161 match Constr.kind t with
162 | Constr.Prod (_, _, b) ->
163 if Vars.noccurn 1 b then chop_product (pred n) (pop b) else None
164 | _ -> None
165
166 let evar_dependencies evm oev =
167 let one_step deps =
168 Evar.Set.fold
169 (fun ev s ->
170 let evi = Evd.find_undefined evm ev in
171 let deps' = Evd.evars_of_filtered_evar_info evm evi in
172 if Evar.Set.mem oev deps' then
173 invalid_arg
174 ( "Ill-formed evar map: cycle detected for evar "
175 ^ Pp.string_of_ppcmds @@ Evar.print oev )
176 else Evar.Set.union deps' s)
177 deps deps
178 in
179 let rec aux deps =
180 let deps' = one_step deps in
181 if Evar.Set.equal deps deps' then deps else aux deps'
182 in
183 aux (Evar.Set.singleton oev)
184
185 let move_after ((id, ev, deps) as obl) l =
186 let rec aux restdeps = function
187 | ((id', _, _) as obl') :: tl ->
188 let restdeps' = Evar.Set.remove id' restdeps in
189 if Evar.Set.is_empty restdeps' then obl' :: obl :: tl
190 else obl' :: aux restdeps' tl
191 | [] -> [obl]
192 in
193 aux (Evar.Set.remove id deps) l
194
195 let sort_dependencies evl =
196 let rec aux l found list =
197 match l with
198 | ((id, ev, deps) as obl) :: tl ->
199 let found' = Evar.Set.union found (Evar.Set.singleton id) in
200 if Evar.Set.subset deps found' then aux tl found' (obl :: list)
201 else aux (move_after obl tl) found list
202 | [] -> List.rev list
203 in
204 aux evl Evar.Set.empty []
205
206 type obligation_name_lifter =
207 (Names.Id.t -> EConstr.t) -> EConstr.t -> Constr.t
208
209 let retrieve_obligations env name evm fs ?deps ?status t ty =
210 (* 'Serialize' the evars *)
211 let nc = Environ.named_context env in
212 let nc_len = Context.Named.length nc in
213 let evm = Evarutil.nf_evar_map_undefined evm in
214 let evl = Evd.undefined_map evm in
215 let evl = match deps with
216 | None -> evl
217 | Some deps -> Evar.Map.filter (fun ev _ -> Evar.Set.mem ev deps) evl
218 in
219 let evl = Evar.Map.bindings evl in
220 let evl = List.map (fun (id, ev) -> (id, ev, evar_dependencies evm id)) evl in
221 let sevl = sort_dependencies evl in
222 let evl = List.map (fun (id, ev, _) -> (id, ev)) sevl in
223 let evn =
224 let i = ref (-1) in
225 List.rev_map
226 (fun (id, ev) ->
227 incr i;
228 ( id
229 , ( !i
230 , Id.of_string
231 (Id.to_string name ^ "_obligation_" ^ string_of_int (succ !i)) )
232 , ev ))
233 evl
234 in
235 let evts =
236 (* Remove existential variables in types and build the corresponding products *)
237 List.fold_right
238 (fun (id, (n, nstr), ev) evs ->
239 let hyps = Evd.evar_filtered_context ev in
240 let hyps = trunc_named_context nc_len hyps in
241 let evtyp, deps, transp = etype_of_evar evm evs hyps (Evd.evar_concl ev) in
242 let evtyp, hyps, chop =
243 match chop_product fs evtyp with
244 | Some t -> (t, trunc_named_context fs hyps, fs)
245 | None -> (evtyp, hyps, 0)
246 in
247 let loc, k = Evd.evar_source (Evd.find_undefined evm id) in
248 let status =
249 match k with
250 | Evar_kinds.QuestionMark {Evar_kinds.qm_obligation = o} -> o
251 | _ -> (
252 match status with
253 | Some o -> o
254 | None ->
255 Evar_kinds.Define (not (Program.get_proofs_transparency ())) )
256 in
257 let force_status, status, chop =
258 match status with
259 | Evar_kinds.Define true as stat ->
260 if not (Int.equal chop fs) then (true, Evar_kinds.Define false, None)
261 else (false, stat, Some chop)
262 | s -> (false, s, None)
263 in
264 let info =
265 { ev_name = (n, nstr)
266 ; ev_hyps = hyps
267 ; ev_status = (force_status, status)
268 ; ev_chop = chop
269 ; ev_src = (loc, k)
270 ; ev_typ = evtyp
271 ; ev_deps = deps
272 ; ev_tac = None }
273 in
274 (id, info) :: evs)
275 evn []
276 in
277 let t', _, transparent =
278 (* Substitute evar refs in the term by variables *)
279 subst_evar_constr evm evts 0 EConstr.mkVar t
280 in
281 let ty, _, _ = subst_evar_constr evm evts 0 EConstr.mkVar ty in
282 let evars =
283 List.map
284 (fun (ev, info) ->
285 let { ev_name = _, name
286 ; ev_status = force_status, status
287 ; ev_src = src
288 ; ev_typ = typ
289 ; ev_deps = deps
290 ; ev_tac = tac } =
291 info
292 in
293 let force_status, status =
294 match status with
295 | Evar_kinds.Define true when Id.Set.mem name transparent ->
296 (true, Evar_kinds.Define false)
297 | _ -> (force_status, status)
298 in
299 (name, typ, src, (force_status, status), deps, tac))
300 evts
301 in
302 let evnames = List.map (fun (ev, info) -> (ev, snd info.ev_name)) evts in
303 let evmap f c = Util.pi1 (subst_evar_constr evm evts 0 f c) in
304 (Array.of_list (List.rev evars), (evnames, evmap), t', ty)