Debian Codegraph @

plugins/funind/glob_termops.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 Constr
12 open Glob_term
13 open CErrors
14 open Util
15 open Names
16
17 (*
18 Some basic functions to rebuild glob_constr
19 In each of them the location is Loc.ghost
20 *)
21 let mkGRef ref = DAst.make @@ GRef (ref, None)
22 let mkGVar id = DAst.make @@ GVar id
23 let mkGApp (rt, rtl) = DAst.make @@ GApp (rt, rtl)
24 let mkGLambda (n, t, b) = DAst.make @@ GLambda (n, None, Explicit, t, b)
25 let mkGProd (n, t, b) = DAst.make @@ GProd (n, None, Explicit, t, b)
26 let mkGLetIn (n, b, t, c) = DAst.make @@ GLetIn (n, None, b, t, c)
27 let mkGCases (rto, l, brl) = DAst.make @@ GCases (RegularStyle, rto, l, brl)
28
29 let mkGHole () =
30 DAst.make
31 @@ GHole (GBinderType Anonymous)
32
33 (*
34 Some basic functions to decompose glob_constrs
35 These are analogous to the ones constrs
36 *)
37 let glob_decompose_app =
38 let rec decompose_rapp acc rt =
39 (* msgnl (str "glob_decompose_app on : "++ Printer.pr_glob_constr rt); *)
40 match DAst.get rt with
41 | GApp (rt, rtl) ->
42 decompose_rapp (List.fold_left (fun y x -> x :: y) acc rtl) rt
43 | _ -> (rt, List.rev acc)
44 in
45 decompose_rapp []
46
47 (* [glob_make_eq t1 t2] build the glob_constr corresponding to [t2 = t1] *)
48 let glob_make_eq ?(typ = mkGHole ()) t1 t2 =
49 mkGApp (mkGRef (Rocqlib.lib_ref "core.eq.type"), [typ; t2; t1])
50
51 (* [glob_make_neq t1 t2] build the glob_constr corresponding to [t1 <> t2] *)
52 let glob_make_neq t1 t2 =
53 mkGApp (mkGRef (Rocqlib.lib_ref "core.not.type"), [glob_make_eq t1 t2])
54
55 let remove_name_from_mapping mapping na =
56 match na with Anonymous -> mapping | Name id -> Id.Map.remove id mapping
57
58 let change_vars =
59 let rec change_vars mapping rt =
60 DAst.map_with_loc
61 (fun ?loc -> function GRef _ as x -> x
62 | GVar id ->
63 let new_id = try Id.Map.find id mapping with Not_found -> id in
64 GVar new_id | GEvar _ as x -> x | GPatVar _ as x -> x
65 | GApp (rt', rtl) ->
66 GApp (change_vars mapping rt', List.map (change_vars mapping) rtl)
67 | GProj (f, rtl, rt) ->
68 GProj (f, List.map (change_vars mapping) rtl, change_vars mapping rt)
69 | GLambda (name, r, k, t, b) ->
70 GLambda
71 ( name
72 , r, k
73 , change_vars mapping t
74 , change_vars (remove_name_from_mapping mapping name) b )
75 | GProd (name, r, k, t, b) ->
76 GProd
77 ( name
78 , r, k
79 , change_vars mapping t
80 , change_vars (remove_name_from_mapping mapping name) b )
81 | GLetIn (name, r, def, typ, b) ->
82 GLetIn
83 ( name
84 , r
85 , change_vars mapping def
86 , Option.map (change_vars mapping) typ
87 , change_vars (remove_name_from_mapping mapping name) b )
88 | GLetTuple (nal, (na, rto), b, e) ->
89 let new_mapping =
90 List.fold_left remove_name_from_mapping mapping nal
91 in
92 GLetTuple
93 ( nal
94 , (na, Option.map (change_vars mapping) rto)
95 , change_vars mapping b
96 , change_vars new_mapping e )
97 | GCases (sty, infos, el, brl) ->
98 GCases
99 ( sty
100 , infos
101 , List.map (fun (e, x) -> (change_vars mapping e, x)) el
102 , List.map (change_vars_br mapping) brl )
103 | GIf (b, (na, e_option), lhs, rhs) ->
104 GIf
105 ( change_vars mapping b
106 , (na, Option.map (change_vars mapping) e_option)
107 , change_vars mapping lhs
108 , change_vars mapping rhs )
109 | GRec _ -> user_err ?loc Pp.(str "Local (co)fixes are not supported")
110 | GFloat _ as x -> x
111 | GCast (b, k, c) ->
112 GCast (change_vars mapping b, k, change_vars mapping c)
113 | GArray (u, t, def, ty) ->
114 GArray
115 ( u
116 , Array.map (change_vars mapping) t
117 , change_vars mapping def
118 , change_vars mapping ty )
119 | GSort _ | GHole _ | GGenarg _ | GInt _ | GString _ as x -> x)
120 rt
121 and change_vars_br mapping ({CAst.loc; v = idl, patl, res} as br) =
122 let new_mapping = List.fold_right Id.Map.remove idl mapping in
123 if Id.Map.is_empty new_mapping then br
124 else CAst.make ?loc (idl, patl, change_vars new_mapping res)
125 in
126 change_vars
127
128 let rec alpha_pat excluded pat =
129 let loc = pat.CAst.loc in
130 match DAst.get pat with
131 | PatVar Anonymous ->
132 let new_id = Indfun_common.fresh_id excluded "_x" in
133 (DAst.make ?loc @@ PatVar (Name new_id), new_id :: excluded, Id.Map.empty)
134 | PatVar (Name id) ->
135 if Id.List.mem id excluded then
136 let new_id = Namegen.next_ident_away id (Id.Set.of_list excluded) in
137 ( DAst.make ?loc @@ PatVar (Name new_id)
138 , new_id :: excluded
139 , Id.Map.add id new_id Id.Map.empty )
140 else (pat, excluded, Id.Map.empty)
141 | PatCstr (constr, patl, na) ->
142 let new_na, new_excluded, map =
143 match na with
144 | Name id when Id.List.mem id excluded ->
145 let new_id = Namegen.next_ident_away id (Id.Set.of_list excluded) in
146 (Name new_id, new_id :: excluded, Id.Map.add id new_id Id.Map.empty)
147 | _ -> (na, excluded, Id.Map.empty)
148 in
149 let new_patl, new_excluded, new_map =
150 List.fold_left
151 (fun (patl, excluded, map) pat ->
152 let new_pat, new_excluded, new_map = alpha_pat excluded pat in
153 (new_pat :: patl, new_excluded, Id.Map.fold Id.Map.add new_map map))
154 ([], new_excluded, map) patl
155 in
156 ( DAst.make ?loc @@ PatCstr (constr, List.rev new_patl, new_na)
157 , new_excluded
158 , new_map )
159
160 let alpha_patl excluded patl =
161 let patl, new_excluded, map =
162 List.fold_left
163 (fun (patl, excluded, map) pat ->
164 let new_pat, new_excluded, new_map = alpha_pat excluded pat in
165 (new_pat :: patl, new_excluded, Id.Map.fold Id.Map.add new_map map))
166 ([], excluded, Id.Map.empty)
167 patl
168 in
169 (List.rev patl, new_excluded, map)
170
171 let raw_get_pattern_id pat acc =
172 let rec get_pattern_id pat =
173 match DAst.get pat with
174 | PatVar Anonymous -> assert false
175 | PatVar (Name id) -> [id]
176 | PatCstr (constr, patternl, _) ->
177 List.fold_right
178 (fun pat idl ->
179 let idl' = get_pattern_id pat in
180 idl' @ idl)
181 patternl []
182 in
183 get_pattern_id pat @ acc
184
185 let get_pattern_id pat = raw_get_pattern_id pat []
186
187 let rec alpha_rt excluded rt =
188 let loc = rt.CAst.loc in
189 let new_rt =
190 DAst.make ?loc
191 @@
192 match DAst.get rt with
193 | (GRef _ | GVar _ | GEvar _ | GPatVar _) as rt -> rt
194 | GLambda (Anonymous, r, k, t, b) ->
195 let new_id =
196 Namegen.next_ident_away (Id.of_string "_x") (Id.Set.of_list excluded)
197 in
198 let new_excluded = new_id :: excluded in
199 let new_t = alpha_rt new_excluded t in
200 let new_b = alpha_rt new_excluded b in
201 GLambda (Name new_id, r, k, new_t, new_b)
202 | GProd (Anonymous, r, k, t, b) ->
203 let new_t = alpha_rt excluded t in
204 let new_b = alpha_rt excluded b in
205 GProd (Anonymous, r, k, new_t, new_b)
206 | GLetIn (Anonymous, r, b, t, c) ->
207 let new_b = alpha_rt excluded b in
208 let new_t = Option.map (alpha_rt excluded) t in
209 let new_c = alpha_rt excluded c in
210 GLetIn (Anonymous, r, new_b, new_t, new_c)
211 | GLambda (Name id, r, k, t, b) ->
212 let new_id = Namegen.next_ident_away id (Id.Set.of_list excluded) in
213 let t, b =
214 if Id.equal new_id id then (t, b)
215 else
216 let replace = change_vars (Id.Map.add id new_id Id.Map.empty) in
217 (t, replace b)
218 in
219 let new_excluded = new_id :: excluded in
220 let new_t = alpha_rt new_excluded t in
221 let new_b = alpha_rt new_excluded b in
222 GLambda (Name new_id, r, k, new_t, new_b)
223 | GProd (Name id, r, k, t, b) ->
224 let new_id = Namegen.next_ident_away id (Id.Set.of_list excluded) in
225 let new_excluded = new_id :: excluded in
226 let t, b =
227 if Id.equal new_id id then (t, b)
228 else
229 let replace = change_vars (Id.Map.add id new_id Id.Map.empty) in
230 (t, replace b)
231 in
232 let new_t = alpha_rt new_excluded t in
233 let new_b = alpha_rt new_excluded b in
234 GProd (Name new_id, r, k, new_t, new_b)
235 | GLetIn (Name id, r, b, t, c) ->
236 let new_id = Namegen.next_ident_away id (Id.Set.of_list excluded) in
237 let c =
238 if Id.equal new_id id then c
239 else change_vars (Id.Map.add id new_id Id.Map.empty) c
240 in
241 let new_excluded = new_id :: excluded in
242 let new_b = alpha_rt new_excluded b in
243 let new_t = Option.map (alpha_rt new_excluded) t in
244 let new_c = alpha_rt new_excluded c in
245 GLetIn (Name new_id, r, new_b, new_t, new_c)
246 | GLetTuple (nal, (na, rto), t, b) ->
247 let rev_new_nal, new_excluded, mapping =
248 List.fold_left
249 (fun (nal, excluded, mapping) na ->
250 match na with
251 | Anonymous -> (na :: nal, excluded, mapping)
252 | Name id ->
253 let new_id =
254 Namegen.next_ident_away id (Id.Set.of_list excluded)
255 in
256 if Id.equal new_id id then (na :: nal, id :: excluded, mapping)
257 else
258 ( Name new_id :: nal
259 , id :: excluded
260 , Id.Map.add id new_id mapping ))
261 ([], excluded, Id.Map.empty)
262 nal
263 in
264 let new_nal = List.rev rev_new_nal in
265 let new_rto, new_t, new_b =
266 if Id.Map.is_empty mapping then (rto, t, b)
267 else
268 let replace = change_vars mapping in
269 (Option.map replace rto, t, replace b)
270 in
271 let new_t = alpha_rt new_excluded new_t in
272 let new_b = alpha_rt new_excluded new_b in
273 let new_rto = Option.map (alpha_rt new_excluded) new_rto in
274 GLetTuple (new_nal, (na, new_rto), new_t, new_b)
275 | GCases (sty, infos, el, brl) ->
276 let new_el =
277 List.map (function rt, i -> (alpha_rt excluded rt, i)) el
278 in
279 GCases (sty, infos, new_el, List.map (alpha_br excluded) brl)
280 | GIf (b, (na, e_o), lhs, rhs) ->
281 GIf
282 ( alpha_rt excluded b
283 , (na, Option.map (alpha_rt excluded) e_o)
284 , alpha_rt excluded lhs
285 , alpha_rt excluded rhs )
286 | GRec _ -> user_err Pp.(str "Not handled GRec")
287 | (GSort _ | GInt _ | GFloat _ | GString _ | GHole _ | GGenarg _) as rt -> rt
288 | GCast (b, k, c) ->
289 GCast (alpha_rt excluded b, k, alpha_rt excluded c)
290 | GApp (f, args) ->
291 GApp (alpha_rt excluded f, List.map (alpha_rt excluded) args)
292 | GProj (f, args, c) ->
293 GProj (f, List.map (alpha_rt excluded) args, alpha_rt excluded c)
294 | GArray (u, t, def, ty) ->
295 GArray
296 ( u
297 , Array.map (alpha_rt excluded) t
298 , alpha_rt excluded def
299 , alpha_rt excluded ty )
300 in
301 new_rt
302
303 and alpha_br excluded {CAst.loc; v = ids, patl, res} =
304 let new_patl, new_excluded, mapping = alpha_patl excluded patl in
305 let new_ids = List.fold_right raw_get_pattern_id new_patl [] in
306 let new_excluded = new_ids @ excluded in
307 let renamed_res = change_vars mapping res in
308 let new_res = alpha_rt new_excluded renamed_res in
309 CAst.make ?loc (new_ids, new_patl, new_res)
310
311 (*
312 [is_free_in id rt] checks if [id] is a free variable in [rt]
313 *)
314 let is_free_in id =
315 let rec is_free_in x =
316 DAst.with_loc_val
317 (fun ?loc -> function GRef _ -> false | GVar id' -> Id.compare id' id == 0
318 | GEvar _ -> false | GPatVar _ -> false
319 | GApp (rt, rtl) | GProj (_, rtl, rt) -> List.exists is_free_in (rt :: rtl)
320 | GLambda (n, _, _, t, b) | GProd (n, _, _, t, b) ->
321 let check_in_b =
322 match n with Name id' -> not (Id.equal id' id) | _ -> true
323 in
324 is_free_in t || (check_in_b && is_free_in b)
325 | GLetIn (n, _, b, t, c) ->
326 let check_in_c =
327 match n with Name id' -> not (Id.equal id' id) | _ -> true
328 in
329 is_free_in b
330 || Option.cata is_free_in true t
331 || (check_in_c && is_free_in c)
332 | GCases (_, _, el, brl) ->
333 List.exists (fun (e, _) -> is_free_in e) el
334 || List.exists is_free_in_br brl
335 | GLetTuple (nal, _, b, t) ->
336 let check_in_nal =
337 not
338 (List.exists
339 (function Name id' -> Id.equal id' id | _ -> false)
340 nal)
341 in
342 is_free_in t || (check_in_nal && is_free_in b)
343 | GIf (cond, _, br1, br2) ->
344 is_free_in cond || is_free_in br1 || is_free_in br2
345 | GRec _ -> user_err Pp.(str "Not handled GRec") | GSort _ -> false
346 | GHole _ -> false
347 | GGenarg _ -> false (* XXX isn't this incorrect? *)
348 | GCast (b, _, t) ->
349 is_free_in b || is_free_in t
350 | GInt _ | GFloat _ | GString _ -> false
351 | GArray (_u, t, def, ty) ->
352 Array.exists is_free_in t || is_free_in def || is_free_in ty)
353 x
354 and is_free_in_br {CAst.v = ids, _, rt} =
355 (not (Id.List.mem id ids)) && is_free_in rt
356 in
357 is_free_in
358
359 let rec pattern_to_term pt =
360 DAst.with_val
361 (function
362 | PatVar Anonymous -> assert false
363 | PatVar (Name id) -> mkGVar id
364 | PatCstr (constr, patternl, _) ->
365 let cst_narg =
366 Inductiveops.constructor_nallargs (Global.env ()) constr
367 in
368 let implicit_args =
369 Array.to_list
370 (Array.init (cst_narg - List.length patternl) (fun _ -> mkGHole ()))
371 in
372 let patl_as_term = List.map pattern_to_term patternl in
373 mkGApp
374 (mkGRef (GlobRef.ConstructRef constr), implicit_args @ patl_as_term))
375 pt
376
377 let replace_var_by_term x_id term =
378 let rec replace_var_by_pattern x =
379 DAst.map
380 (function
381 | GVar id when Id.compare id x_id == 0 -> DAst.get term
382 | (GRef _ | GVar _ | GEvar _ | GPatVar _) as rt -> rt
383 | GApp (rt', rtl) ->
384 GApp (replace_var_by_pattern rt', List.map replace_var_by_pattern rtl)
385 | GProj (f, rtl, rt) ->
386 GProj (f, List.map replace_var_by_pattern rtl, replace_var_by_pattern rt)
387 | GLambda (Name id, _, _, _, _) as rt when Id.compare id x_id == 0 -> rt
388 | GLambda (name, r, k, t, b) ->
389 GLambda (name, r, k, replace_var_by_pattern t, replace_var_by_pattern b)
390 | GProd (Name id, _, _, _, _) as rt when Id.compare id x_id == 0 -> rt
391 | GProd (name, r, k, t, b) ->
392 GProd (name, r, k, replace_var_by_pattern t, replace_var_by_pattern b)
393 | GLetIn (Name id, _, _, _, _) as rt when Id.compare id x_id == 0 -> rt
394 | GLetIn (name, r, def, typ, b) ->
395 GLetIn
396 ( name
397 , r
398 , replace_var_by_pattern def
399 , Option.map replace_var_by_pattern typ
400 , replace_var_by_pattern b )
401 | GLetTuple (nal, _, _, _) as rt
402 when List.exists
403 (function Name id -> Id.equal id x_id | _ -> false)
404 nal ->
405 rt
406 | GLetTuple (nal, (na, rto), def, b) ->
407 GLetTuple
408 ( nal
409 , (na, Option.map replace_var_by_pattern rto)
410 , replace_var_by_pattern def
411 , replace_var_by_pattern b )
412 | GCases (sty, infos, el, brl) ->
413 GCases
414 ( sty
415 , infos
416 , List.map (fun (e, x) -> (replace_var_by_pattern e, x)) el
417 , List.map replace_var_by_pattern_br brl )
418 | GIf (b, (na, e_option), lhs, rhs) ->
419 GIf
420 ( replace_var_by_pattern b
421 , (na, Option.map replace_var_by_pattern e_option)
422 , replace_var_by_pattern lhs
423 , replace_var_by_pattern rhs )
424 | GRec _ -> CErrors.user_err (Pp.str "Not handled GRec")
425 | (GSort _ | GHole _ | GGenarg _) as rt -> rt (* is this correct for GGenarg? *)
426 | GInt _ as rt -> rt
427 | GFloat _ as rt -> rt
428 | GString _ as rt -> rt
429 | GArray (u, t, def, ty) ->
430 GArray
431 ( u
432 , Array.map replace_var_by_pattern t
433 , replace_var_by_pattern def
434 , replace_var_by_pattern ty )
435 | GCast (b, k, c) ->
436 GCast (replace_var_by_pattern b, k, replace_var_by_pattern c))
437 x
438 and replace_var_by_pattern_br ({CAst.loc; v = idl, patl, res} as br) =
439 if List.exists (fun id -> Id.compare id x_id == 0) idl then br
440 else CAst.make ?loc (idl, patl, replace_var_by_pattern res)
441 in
442 replace_var_by_pattern
443
444 (* checking unifiability of patterns *)
445 exception NotUnifiable
446
447 let rec are_unifiable_aux env = function
448 | [] -> ()
449 | (l, r) :: eqs -> (
450 match (DAst.get l, DAst.get r) with
451 | PatVar _, _ | _, PatVar _ -> are_unifiable_aux env eqs
452 | PatCstr (constructor1, cpl1, _), PatCstr (constructor2, cpl2, _) ->
453 if not (Environ.QConstruct.equal env constructor2 constructor1) then
454 raise NotUnifiable
455 else
456 let eqs' =
457 try List.combine cpl1 cpl2 @ eqs
458 with Invalid_argument _ -> anomaly (Pp.str "are_unifiable_aux.")
459 in
460 are_unifiable_aux env eqs' )
461
462 let are_unifiable env pat1 pat2 =
463 try
464 are_unifiable_aux env [(pat1, pat2)];
465 true
466 with NotUnifiable -> false
467
468 let rec eq_cases_pattern_aux env = function
469 | [] -> ()
470 | (l, r) :: eqs -> (
471 match (DAst.get l, DAst.get r) with
472 | PatVar _, PatVar _ -> eq_cases_pattern_aux env eqs
473 | PatCstr (constructor1, cpl1, _), PatCstr (constructor2, cpl2, _) ->
474 if not (Environ.QConstruct.equal env constructor2 constructor1) then
475 raise NotUnifiable
476 else
477 let eqs' =
478 try List.combine cpl1 cpl2 @ eqs
479 with Invalid_argument _ -> anomaly (Pp.str "eq_cases_pattern_aux.")
480 in
481 eq_cases_pattern_aux env eqs'
482 | _ -> raise NotUnifiable )
483
484 let eq_cases_pattern env pat1 pat2 =
485 try
486 eq_cases_pattern_aux env [(pat1, pat2)];
487 true
488 with NotUnifiable -> false
489
490 let ids_of_pat =
491 let rec ids_of_pat ids =
492 DAst.with_val (function
493 | PatVar Anonymous -> ids
494 | PatVar (Name id) -> Id.Set.add id ids
495 | PatCstr (_, patl, _) -> List.fold_left ids_of_pat ids patl)
496 in
497 ids_of_pat Id.Set.empty
498
499 let expand_as =
500 let rec add_as map rt =
501 match DAst.get rt with
502 | PatVar _ -> map
503 | PatCstr (_, patl, Name id) ->
504 Id.Map.add id (pattern_to_term rt) (List.fold_left add_as map patl)
505 | PatCstr (_, patl, _) -> List.fold_left add_as map patl
506 in
507 let rec expand_as map =
508 DAst.map (function
509 | (GRef _ | GEvar _ | GPatVar _ | GSort _ | GHole _ | GGenarg _ | GInt _
510 | GFloat _ | GString _ ) as rt -> rt
511 | GVar id as rt -> (
512 try DAst.get (Id.Map.find id map) with Not_found -> rt )
513 | GApp (f, args) -> GApp (expand_as map f, List.map (expand_as map) args)
514 | GProj (f, args, c) -> GProj (f, List.map (expand_as map) args, expand_as map c)
515 | GLambda (na, r, k, t, b) ->
516 GLambda (na, r, k, expand_as map t, expand_as map b)
517 | GProd (na, r, k, t, b) -> GProd (na, r, k, expand_as map t, expand_as map b)
518 | GLetIn (na, r, v, typ, b) ->
519 GLetIn
520 (na, r, expand_as map v, Option.map (expand_as map) typ, expand_as map b)
521 | GLetTuple (nal, (na, po), v, b) ->
522 GLetTuple
523 ( nal
524 , (na, Option.map (expand_as map) po)
525 , expand_as map v
526 , expand_as map b )
527 | GIf (e, (na, po), br1, br2) ->
528 GIf
529 ( expand_as map e
530 , (na, Option.map (expand_as map) po)
531 , expand_as map br1
532 , expand_as map br2 )
533 | GRec _ -> user_err Pp.(str "Not handled GRec")
534 | GCast (b, k, c) ->
535 GCast (expand_as map b, k, expand_as map c)
536 | GCases (sty, po, el, brl) ->
537 GCases
538 ( sty
539 , Option.map (expand_as map) po
540 , List.map (fun (rt, t) -> (expand_as map rt, t)) el
541 , List.map (expand_as_br map) brl )
542 | GArray (u, t, def, ty) ->
543 GArray
544 (u, Array.map (expand_as map) t, expand_as map def, expand_as map ty))
545 and expand_as_br map {CAst.loc; v = idl, cpl, rt} =
546 CAst.make ?loc (idl, cpl, expand_as (List.fold_left add_as map cpl) rt)
547 in
548 expand_as Id.Map.empty
549
550 (* [resolve_and_replace_implicits] solves implicits of its argument and replaces them by their solution *)
551
552 let resolve_and_replace_implicits exptyp env sigma rt =
553 let open Evd in
554 let open Evar_kinds in
555 (* we first (pseudo) understand [rt] and get back the computed evar_map *)
556 (* FIXME : JF (30/03/2017) I'm not completely sure to have split understand as needed.
557 If someone knows how to prevent solved existantial removal in understand, please do not hesitate to change the computation of [ctx] here *)
558 let implicit_holes = ref [] in
559 let binder_holes = ref [] in
560 let ctx =
561 let open Pretyping in
562 (* Intercept the pretyper for holes and record the generated evar *)
563 let register_evar kind loc evk = match kind with
564 | GImplicitArg (grk, pk, bk) -> implicit_holes := ((loc, grk, pk, bk), evk) :: !implicit_holes
565 | GBinderType na -> binder_holes := ((loc, na), evk) :: !binder_holes
566 | _ -> ()
567 in
568 let pretype_hole self kind ?loc ~flags tycon env sigma =
569 let sigma, j = default_pretyper.pretype_hole self kind ?loc ~flags tycon env sigma in
570 (* The value is guaranteed to be an undefined evar at this point *)
571 let evk, _ = EConstr.destEvar sigma j.uj_val in
572 let () = register_evar kind loc evk in
573 sigma, j
574 in
575 let pretype_type self c ?loc ~flags valcon env sigma =
576 let sigma, j = default_pretyper.pretype_type self c ?loc ~flags valcon env sigma in
577 let () = match DAst.get c, EConstr.kind sigma j.utj_val with
578 | GHole (kind), Evar (evk, _) -> register_evar kind c.CAst.loc evk
579 | _ -> ()
580 in
581 sigma, j
582 in
583 let flags = Pretyping.all_and_fail_flags in
584 let pretype_flags = {
585 program_mode = false;
586 use_coercions = true;
587 poly = PolyFlags.default;
588 resolve_tc = true;
589 undeclared_evars_rr = false;
590 unconstrained_sorts = false;
591 } in
592 let hypnaming = Evarutil.VarSet.variables (Global.env ()) in
593 let genv = GlobEnv.make ~hypnaming env sigma Glob_ops.empty_lvar in
594 let pretyper = { default_pretyper with pretype_hole; pretype_type } in
595 let sigma', _ = eval_pretyper pretyper ~flags:pretype_flags (Some exptyp) genv sigma rt in
596 solve_remaining_evars flags env ~initial:sigma sigma'
597 in
598 let ctx = Evd.minimize_universes ctx in
599 let f c =
600 EConstr.of_constr
601 (Evarutil.nf_evars_universes ctx (EConstr.Unsafe.to_constr c))
602 in
603 let expand_hole evopt default = match evopt with
604 | None -> default
605 | Some evk ->
606 (* we found the evar corresponding to this hole *)
607 let EvarInfo evi = Evd.find ctx evk in
608 match Evd.evar_body evi with
609 | Evar_defined c ->
610 (* we just have to lift the solution in glob_term *)
611 (* XXX should we use full printing flags? *)
612 let flags = PrintingFlags.Detype.current() in
613 Detyping.detype Detyping.Now ~flags env ctx (f c)
614 | Evar_empty ->
615 (* the hole was not solved : we do nothing *)
616 default
617 in
618 (* then we map [rt] to replace the implicit holes by their values *)
619 let rec change rt =
620 match DAst.get rt with
621 | GHole (GImplicitArg (grk, pk, bk)) ->
622 let eq (loc1, gr1, p1, b1) (loc2, gr2, p2, b2) =
623 Environ.QGlobRef.equal env gr1 gr2 && p1 = p2 && b1 == (b2 : bool) && loc1 = (loc2 : Loc.t option)
624 in
625 let evopt = List.assoc_f_opt eq (rt.CAst.loc, grk, pk, bk) !implicit_holes in
626 expand_hole evopt rt
627 | GHole (GBinderType na) ->
628 let eq (loc1, na1) (loc2, na2) = Name.equal na1 na2 && loc1 = (loc2 : Loc.t option) in
629 let evopt = List.assoc_f_opt eq (rt.CAst.loc, na) !binder_holes in
630 expand_hole evopt rt
631 | _ -> Glob_ops.map_glob_constr change rt
632 in
633 change rt