Debian Codegraph @

pretyping/glob_ops.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 Util
12 open CAst
13 open Names
14 open Nameops
15 open Glob_term
16 open Evar_kinds
17
18 (* Untyped intermediate terms, after ASTs and before constr. *)
19
20 let cases_pattern_loc c = c.CAst.loc
21
22 let alias_of_pat pat = DAst.with_val (function
23 | PatVar name -> name
24 | PatCstr(_,_,name) -> name
25 ) pat
26
27 let set_pat_alias id = DAst.map (function
28 | PatVar Anonymous -> PatVar (Name id)
29 | PatCstr (cstr,patl,Anonymous) -> PatCstr (cstr,patl,Name id)
30 | pat -> assert false)
31
32 let cases_predicate_names tml =
33 List.flatten (List.map (function
34 | (tm,(na,None)) -> [na]
35 | (tm,(na,Some {v=(_,nal)})) -> na::nal) tml)
36
37 let mkGApp ?loc p l = DAst.make ?loc @@
38 match DAst.get p with
39 | GApp (f,l') -> GApp (f,l'@l)
40 | _ -> GApp (p,l)
41
42 let map_glob_decl_left_to_right f (na,r,k,obd,ty) =
43 let comp1 = Option.map f obd in
44 let comp2 = f ty in
45 (na,r,k,comp1,comp2)
46
47 let glob_qvar_eq g1 g2 = match g1, g2 with
48 | GLocalQVar na1, GLocalQVar na2 -> CAst.eq Name.equal na1 na2
49 | GQVar q1, GQVar q2 -> Sorts.QVar.equal q1 q2
50 | GRawQVar q1, GRawQVar q2 -> Sorts.QVar.equal q1 q2
51 | (GLocalQVar _ | GQVar _ | GRawQVar _), _ -> false
52
53 let glob_quality_eq g1 g2 = match g1, g2 with
54 | GQConstant q1, GQConstant q2 -> Sorts.Quality.Constants.equal q1 q2
55 | GQualVar q1, GQualVar q2 -> glob_qvar_eq q1 q2
56 | (GQConstant _ | GQualVar _), _ -> false
57
58 let glob_sort_name_eq g1 g2 = match g1, g2 with
59 | GSProp, GSProp
60 | GProp, GProp
61 | GSet, GSet -> true
62 | GUniv u1, GUniv u2 -> Univ.Level.equal u1 u2
63 | GLocalUniv u1, GLocalUniv u2 -> lident_eq u1 u2
64 | GRawUniv u1, GRawUniv u2 -> Univ.Level.equal u1 u2
65 | (GSProp|GProp|GSet|GUniv _|GLocalUniv _|GRawUniv _), _ -> false
66
67 exception ComplexSort
68
69 let glob_Type_sort = None, UAnonymous {rigid=UnivRigid}
70 let glob_SProp_sort = None, UNamed [GSProp, 0]
71 let glob_Prop_sort = None, UNamed [GProp, 0]
72 let glob_Set_sort = None, UNamed [GSet, 0]
73
74 let map_glob_sort_gen f = function
75 | UNamed l -> UNamed (f l)
76 | UAnonymous _ as x -> x
77
78 let glob_sort_gen_eq f u1 u2 =
79 match u1, u2 with
80 | UAnonymous {rigid=r1}, UAnonymous {rigid=r2} -> r1 = r2
81 | UNamed l1, UNamed l2 -> f l1 l2
82 | (UNamed _ | UAnonymous _), _ -> false
83
84 let glob_sort_eq (q1, l1) (q2, l2) =
85 Option.equal glob_qvar_eq q1 q2 &&
86 glob_sort_gen_eq
87 (List.equal (fun (x,m) (y,n) ->
88 glob_sort_name_eq x y
89 && Int.equal m n))
90 l1 l2
91
92 let glob_sort_quality s =
93 if glob_sort_eq s glob_Type_sort then UnivGen.QualityOrSet.qtype
94 else match s with
95 | None, UNamed [s, 0] ->
96 begin match s with
97 | GSProp -> UnivGen.QualityOrSet.sprop
98 | GProp -> UnivGen.QualityOrSet.prop
99 | GSet -> UnivGen.QualityOrSet.set
100 | GUniv _ | GLocalUniv _ | GRawUniv _ -> raise ComplexSort
101 end
102 | _ -> raise ComplexSort
103
104 let fresh_glob_sort_in_quality sigma s =
105 Evd.fresh_sort_in_quality sigma @@ glob_sort_quality s
106
107 let glob_level_eq u1 u2 =
108 glob_sort_gen_eq glob_sort_name_eq u1 u2
109
110 let instance_eq (q1,u1) (q2,u2) =
111 List.equal glob_quality_eq q1 q2
112 && List.equal glob_level_eq u1 u2
113
114 let binding_kind_eq bk1 bk2 = match bk1, bk2 with
115 | Explicit, Explicit -> true
116 | NonMaxImplicit, NonMaxImplicit -> true
117 | MaxImplicit, MaxImplicit -> true
118 | (Explicit | NonMaxImplicit | MaxImplicit), _ -> false
119
120 let glob_relevance_eq a b = match a, b with
121 | GRelevant, GRelevant | GIrrelevant, GIrrelevant -> true
122 | GRelevanceVar q1, GRelevanceVar q2 -> glob_qvar_eq q1 q2
123 | (GRelevant | GIrrelevant | GRelevanceVar _), _ -> false
124
125 let relevance_info_eq = Option.equal glob_relevance_eq
126
127 let case_style_eq s1 s2 = let open Constr in match s1, s2 with
128 | LetStyle, LetStyle -> true
129 | IfStyle, IfStyle -> true
130 | LetPatternStyle, LetPatternStyle -> true
131 | MatchStyle, MatchStyle -> true
132 | RegularStyle, RegularStyle -> true
133 | (LetStyle | IfStyle | LetPatternStyle | MatchStyle | RegularStyle), _ -> false
134
135 let rec mk_cases_pattern_eq g p1 p2 = match DAst.get p1, DAst.get p2 with
136 | PatVar na1, PatVar na2 -> g na1 na2
137 | PatCstr (c1, pl1, na1), PatCstr (c2, pl2, na2) ->
138 Construct.CanOrd.equal c1 c2 && List.equal (mk_cases_pattern_eq g) pl1 pl2 &&
139 Name.equal na1 na2
140 | (PatVar _ | PatCstr _), _ -> false
141
142 let cast_kind_eq t1 t2 = let open Constr in match t1, t2 with
143 | DEFAULTcast, DEFAULTcast
144 | VMcast, VMcast
145 | NATIVEcast, NATIVEcast -> true
146 | (DEFAULTcast | VMcast | NATIVEcast), _ -> false
147
148 let matching_var_kind_eq k1 k2 = match k1, k2 with
149 | FirstOrderPatVar ido1, FirstOrderPatVar ido2 -> Id.equal ido1 ido2
150 | SecondOrderPatVar id1, SecondOrderPatVar id2 -> Id.equal id1 id2
151 | (FirstOrderPatVar _ | SecondOrderPatVar _), _ -> false
152
153 let tomatch_tuple_eq f (c1, p1) (c2, p2) =
154 let eqp {CAst.v=(i1, na1)} {CAst.v=(i2, na2)} =
155 Ind.CanOrd.equal i1 i2 && List.equal Name.equal na1 na2
156 in
157 let eq_pred (n1, o1) (n2, o2) = Name.equal n1 n2 && Option.equal eqp o1 o2 in
158 f c1 c2 && eq_pred p1 p2
159
160 and cases_clause_eq f g {CAst.v=(id1, p1, c1)} {CAst.v=(id2, p2, c2)} =
161 (* In principle, id1 and id2 canonically derive from p1 and p2 *)
162 List.equal (mk_cases_pattern_eq g) p1 p2 && f c1 c2
163
164 let glob_decl_eq f (na1, r1, bk1, c1, t1) (na2, r2, bk2, c2, t2) =
165 Name.equal na1 na2 && relevance_info_eq r1 r2 && binding_kind_eq bk1 bk2 &&
166 Option.equal f c1 c2 && f t1 t2
167
168 let fix_kind_eq k1 k2 = match k1, k2 with
169 | GFix (a1, i1), GFix (a2, i2) ->
170 Int.equal i1 i2 && Array.equal (Option.equal Int.equal) a1 a2
171 | GCoFix i1, GCoFix i2 -> Int.equal i1 i2
172 | (GFix _ | GCoFix _), _ -> false
173
174 let evar_instance_eq f (x1,c1) (x2,c2) =
175 Id.equal x1.CAst.v x2.CAst.v && f c1 c2
176
177 let mk_glob_constr_eq f g c1 c2 = match DAst.get c1, DAst.get c2 with
178 | GRef (gr1, u1), GRef (gr2, u2) ->
179 GlobRef.CanOrd.equal gr1 gr2 &&
180 Option.equal instance_eq u1 u2
181 | GVar id1, GVar id2 -> Id.equal id1 id2
182 | GEvar (id1, arg1), GEvar (id2, arg2) ->
183 Id.equal id1.CAst.v id2.CAst.v && List.equal (evar_instance_eq f) arg1 arg2
184 | GPatVar k1, GPatVar k2 -> matching_var_kind_eq k1 k2
185 | GApp (f1, arg1), GApp (f2, arg2) ->
186 f f1 f2 && List.equal f arg1 arg2
187 | GLambda (na1, r1, bk1, t1, c1), GLambda (na2, r2, bk2, t2, c2) ->
188 g na1 na2 (Some t1) (Some t2) && relevance_info_eq r1 r2
189 && binding_kind_eq bk1 bk2 && f t1 t2 && f c1 c2
190 | GProd (na1, r1, bk1, t1, c1), GProd (na2, r2, bk2, t2, c2) ->
191 g na1 na2 (Some t1) (Some t2) && relevance_info_eq r1 r2
192 && binding_kind_eq bk1 bk2 && f t1 t2 && f c1 c2
193 | GLetIn (na1, r1, b1, t1, c1), GLetIn (na2, r2, b2, t2, c2) ->
194 g na1 na2 t1 t2 && relevance_info_eq r1 r2
195 && f b1 b2 && Option.equal f t1 t2 && f c1 c2
196 | GCases (st1, c1, tp1, cl1), GCases (st2, c2, tp2, cl2) ->
197 case_style_eq st1 st2 && Option.equal f c1 c2 &&
198 List.equal (tomatch_tuple_eq f) tp1 tp2 &&
199 List.equal (cases_clause_eq f (fun na1 na2 -> g na1 na2 None None)) cl1 cl2
200 | GLetTuple (na1, (n1, p1), c1, t1), GLetTuple (na2, (n2, p2), c2, t2) ->
201 List.equal (fun na1 na2 -> g na1 na2 None None) na1 na2 && g n1 n2 None None &&
202 Option.equal f p1 p2 && f c1 c2 && f t1 t2
203 | GIf (m1, (pat1, p1), c1, t1), GIf (m2, (pat2, p2), c2, t2) ->
204 f m1 m2 && g pat1 pat2 None None &&
205 Option.equal f p1 p2 && f c1 c2 && f t1 t2
206 | GRec (kn1, id1, decl1, t1, c1), GRec (kn2, id2, decl2, t2, c2) ->
207 fix_kind_eq kn1 kn2 && Array.equal Id.equal id1 id2 &&
208 Array.equal (fun l1 l2 -> List.equal (glob_decl_eq f) l1 l2) decl1 decl2 &&
209 Array.equal f c1 c2 && Array.equal f t1 t2
210 | GSort s1, GSort s2 -> glob_sort_eq s1 s2
211 | GHole (_kn1), GHole (_kn2) ->
212 true (* this deserves a FIXME *)
213 | GGenarg gn1, GGenarg gn2 ->
214 gn1 == gn2 (* Only thing sensible *)
215 | GCast (c1, k1, t1), GCast (c2, k2, t2) ->
216 f c1 c2 && Option.equal cast_kind_eq k1 k2 && f t1 t2
217 | GProj ((cst1, u1), args1, c1), GProj ((cst2, u2), args2, c2) ->
218 GlobRef.(CanOrd.equal (ConstRef cst1) (ConstRef cst2)) &&
219 Option.equal instance_eq u1 u2 &&
220 List.equal f args1 args2 && f c1 c2
221 | GInt i1, GInt i2 -> Uint63.equal i1 i2
222 | GFloat f1, GFloat f2 -> Float64.equal f1 f2
223 | GString s1, GString s2 -> Pstring.equal s1 s2
224 | GArray (u1, t1, def1, ty1), GArray (u2, t2, def2, ty2) ->
225 Array.equal f t1 t2 && f def1 def2 && f ty1 ty2 &&
226 Option.equal instance_eq u1 u2
227 | (GRef _ | GVar _ | GEvar _ | GPatVar _ | GApp _ | GLambda _ | GProd _ | GLetIn _ |
228 GCases _ | GLetTuple _ | GIf _ | GRec _ | GSort _ | GHole _ | GGenarg _ | GCast _ | GProj _ |
229 GInt _ | GFloat _ | GString _ | GArray _), _ -> false
230
231 let rec glob_constr_eq c = mk_glob_constr_eq glob_constr_eq (fun na1 na2 _ _ -> Name.equal na1 na2) c
232
233 let cases_pattern_eq c = mk_cases_pattern_eq Name.equal c
234
235 let rec map_case_pattern_binders f = DAst.map (function
236 | PatVar na as x ->
237 let r = f na in
238 if r == na then x
239 else PatVar r
240 | PatCstr (c,ps,na) as x ->
241 let rna = f na in
242 let rps =
243 CList.Smart.map (fun p -> map_case_pattern_binders f p) ps
244 in
245 if rna == na && rps == ps then x
246 else PatCstr(c,rps,rna)
247 )
248
249 let map_glob_constr_left_to_right_with_names f g = DAst.map (function
250 | GApp (g,args) ->
251 let comp1 = f g in
252 let comp2 = Util.List.map_left f args in
253 GApp (comp1,comp2)
254 | GLambda (na,r,bk,ty,c) ->
255 let comp1 = f ty in
256 let comp2 = f c in
257 GLambda (g na,r,bk,comp1,comp2)
258 | GProd (na,r,bk,ty,c) ->
259 let comp1 = f ty in
260 let comp2 = f c in
261 GProd (g na,r,bk,comp1,comp2)
262 | GLetIn (na,r,b,t,c) ->
263 let comp1 = f b in
264 let compt = Option.map f t in
265 let comp2 = f c in
266 GLetIn (g na,r,comp1,compt,comp2)
267 | GCases (sty,rtntypopt,tml,pl) ->
268 let comp1 = Option.map f rtntypopt in
269 let comp2 = Util.List.map_left (fun (tm,(x,indxl)) -> (f tm,(g x,Option.map (CAst.map (fun (ind,xl) -> (ind,List.map g xl))) indxl))) tml in
270 let comp3 = Util.List.map_left (CAst.map (fun (idl,p,c) -> (List.map (fun id -> Name.get_id (g (Name id))) idl,List.map (map_case_pattern_binders g) p,f c))) pl in
271 GCases (sty,comp1,comp2,comp3)
272 | GLetTuple (nal,(na,po),b,c) ->
273 let comp1 = Option.map f po in
274 let comp2 = f b in
275 let comp3 = f c in
276 GLetTuple (List.map g nal,(g na,comp1),comp2,comp3)
277 | GIf (c,(na,po),b1,b2) ->
278 let comp1 = Option.map f po in
279 let comp2 = f b1 in
280 let comp3 = f b2 in
281 GIf (f c,(g na,comp1),comp2,comp3)
282 | GRec (fk,idl,bl,tyl,bv) ->
283 let comp1 = Array.map (Util.List.map_left (map_glob_decl_left_to_right f)) bl in
284 let comp2 = Array.map f tyl in
285 let comp3 = Array.map f bv in
286 let g id = Name.get_id (g (Name id)) in
287 GRec (fk,Array.map g idl,comp1,comp2,comp3)
288 | GCast (c,k,t) ->
289 let c = f c in
290 let t = f t in
291 GCast (c,k,t)
292 | GProj (p,args,c) ->
293 let comp1 = Util.List.map_left f args in
294 let comp2 = f c in
295 GProj (p,comp1,comp2)
296 | GArray (u,t,def,ty) ->
297 let comp1 = Array.map_left f t in
298 let comp2 = f def in
299 let comp3 = f ty in
300 GArray (u,comp1,comp2,comp3)
301 | (GVar _ | GSort _ | GHole _ | GGenarg _ | GRef _ | GEvar _ | GPatVar _ | GInt _ | GFloat _ | GString _) as x -> x
302 )
303
304 let map_glob_constr_left_to_right f = map_glob_constr_left_to_right_with_names f (fun na -> na)
305
306 let map_glob_constr = map_glob_constr_left_to_right
307
308 let fold_return_type f acc (na,tyopt) = Option.fold_left f acc tyopt
309
310 let fold_glob_constr f acc = DAst.with_val (function
311 | GVar _ -> acc
312 | GApp (c,args) -> List.fold_left f (f acc c) args
313 | GLambda (_,_,_,b,c) | GProd (_,_,_,b,c) ->
314 f (f acc b) c
315 | GLetIn (_,_,b,t,c) ->
316 f (Option.fold_left f (f acc b) t) c
317 | GCases (_,rtntypopt,tml,pl) ->
318 let fold_pattern acc {CAst.v=(idl,p,c)} = f acc c in
319 List.fold_left fold_pattern
320 (List.fold_left f (Option.fold_left f acc rtntypopt) (List.map fst tml))
321 pl
322 | GLetTuple (_,rtntyp,b,c) ->
323 f (f (fold_return_type f acc rtntyp) b) c
324 | GIf (c,rtntyp,b1,b2) ->
325 f (f (f (fold_return_type f acc rtntyp) c) b1) b2
326 | GRec (_,_,bl,tyl,bv) ->
327 let acc = Array.fold_left
328 (List.fold_left (fun acc (_,_,_,bbd,bty) ->
329 f (Option.fold_left f acc bbd) bty)) acc bl in
330 Array.fold_left f (Array.fold_left f acc tyl) bv
331 | GCast (c,k,t) ->
332 let acc = f acc t in
333 f acc c
334 | GProj (p,args,c) ->
335 f (List.fold_left f acc args) c
336 | GArray (_u,t,def,ty) -> f (f (Array.fold_left f acc t) def) ty
337 | (GSort _ | GHole _ | GGenarg _ | GRef _ | GEvar _ | GPatVar _ | GInt _ | GFloat _ | GString _) -> acc
338 )
339 let fold_return_type_with_binders f g v acc (na,tyopt) =
340 (* eta expansion is important if g has effects, eg bound_glob_vars below, see #11959 *)
341 Option.fold_left (fun acc -> f (Name.fold_right g na v) acc) acc tyopt
342
343 let fold_glob_constr_with_binders g f v acc = DAst.(with_val (function
344 | GVar _ -> acc
345 | GApp (c,args) -> List.fold_left (f v) (f v acc c) args
346 | GLambda (na,_,_,b,c) | GProd (na,_,_,b,c) ->
347 f (Name.fold_right g na v) (f v acc b) c
348 | GLetIn (na,_,b,t,c) ->
349 f (Name.fold_right g na v) (Option.fold_left (f v) (f v acc b) t) c
350 | GCases (_,rtntypopt,tml,pl) ->
351 let fold_pattern acc {v=(idl,p,c)} = f (List.fold_right g idl v) acc c in
352 let fold_tomatch (v',acc) (tm,(na,onal)) =
353 ((if rtntypopt = None then v' else
354 Option.fold_left (fun v'' {v=(_,nal)} -> List.fold_right (Name.fold_right g) nal v'')
355 (Name.fold_right g na v') onal),
356 f v acc tm) in
357 let (v',acc) = List.fold_left fold_tomatch (v,acc) tml in
358 let acc = Option.fold_left (f v') acc rtntypopt in
359 List.fold_left fold_pattern acc pl
360 | GLetTuple (nal,rtntyp,b,c) ->
361 f (List.fold_right (Name.fold_right g) nal v)
362 (f v (fold_return_type_with_binders f g v acc rtntyp) b) c
363 | GIf (c,rtntyp,b1,b2) ->
364 f v (f v (f v (fold_return_type_with_binders f g v acc rtntyp) c) b1) b2
365 | GRec (_,idl,bll,tyl,bv) ->
366 let v' = Array.fold_right g idl v in
367 let f' i acc fid =
368 let v,acc =
369 List.fold_left
370 (fun (v,acc) (na,_,_,bbd,bty) ->
371 (Name.fold_right g na v, f v (Option.fold_left (f v) acc bbd) bty))
372 (v,acc)
373 bll.(i) in
374 f v' (f v acc tyl.(i)) (bv.(i)) in
375 Array.fold_left_i f' acc idl
376 | GCast (c,k,t) ->
377 let acc = f v acc t in
378 f v acc c
379 | GProj (p,args,c) ->
380 f v (List.fold_left (f v) acc args) c
381 | GArray (_u, t, def, ty) -> f v (f v (Array.fold_left (f v) acc t) def) ty
382 | (GSort _ | GHole _ | GGenarg _ | GRef _ | GEvar _ | GPatVar _ | GInt _ | GFloat _ | GString _) -> acc))
383
384 let iter_glob_constr f = fold_glob_constr (fun () -> f) ()
385
386 let occur_glob_constr id =
387 let rec occur barred acc c = match DAst.get c with
388 | GVar id' -> Id.equal id id'
389 | _ ->
390 (* [g] looks if [id] appears in a binding position, in which
391 case, we don't have to look in the corresponding subterm *)
392 let g id' barred = barred || Id.equal id id' in
393 let f barred acc c = acc || not barred && occur false acc c in
394 fold_glob_constr_with_binders g f barred acc c in
395 occur false false
396
397 let free_glob_vars =
398 let rec vars bound vs c = match DAst.get c with
399 | GVar id' -> if Id.Set.mem id' bound then vs else Id.Set.add id' vs
400 | _ -> fold_glob_constr_with_binders Id.Set.add vars bound vs c in
401 fun rt ->
402 let vs = vars Id.Set.empty Id.Set.empty rt in
403 vs
404
405 let glob_visible_short_qualid c =
406 let rec aux acc c = match DAst.get c with
407 | GRef (c,_) ->
408 let qualid = Nametab.shortest_qualid_of_global ~force_short:true Id.Set.empty c in
409 let dir,id = Libnames.repr_qualid qualid in
410 if DirPath.is_empty dir then Id.Set.add id acc else acc
411 | _ ->
412 fold_glob_constr aux acc c
413 in aux Id.Set.empty c
414
415 let warn_variable_collision =
416 let open Pp in
417 CWarnings.create ~name:"variable-collision" ~category:CWarnings.CoreCategories.ltac
418 (fun name ->
419 strbrk "Collision between bound variables of name " ++ Id.print name)
420
421 let add_and_check_ident id set =
422 if Id.Set.mem id set then warn_variable_collision id;
423 Id.Set.add id set
424
425 let bound_glob_vars =
426 let rec vars bound =
427 fold_glob_constr_with_binders
428 (fun id () -> bound := add_and_check_ident id !bound)
429 (fun () () -> vars bound)
430 () ()
431 in
432 fun rt ->
433 let bound = ref Id.Set.empty in
434 vars bound rt;
435 !bound
436
437 (** Mapping of names in binders *)
438
439 (* spiwack: I used a smart-style kind of mapping here, because the
440 operation will be the identity almost all of the time (with any
441 term outside of Ltac to begin with). But to be honest, there would
442 probably be no significant penalty in doing reallocation as
443 pattern-matching expressions are usually rather small. *)
444
445 let map_inpattern_binders f ({loc;v=(id,nal)} as x) =
446 let r = CList.Smart.map f nal in
447 if r == nal then x
448 else CAst.make ?loc (id,r)
449
450 let map_tomatch_binders f ((c,(na,inp)) as x) : tomatch_tuple =
451 let r = Option.Smart.map (fun p -> map_inpattern_binders f p) inp in
452 if r == inp then x
453 else c,(f na, r)
454
455 let map_cases_branch_binders f ({CAst.loc;v=(il,cll,rhs)} as x) : cases_clause =
456 (* spiwack: not sure if I must do something with the list of idents.
457 It is intended to be a superset of the free variable of the
458 right-hand side, if I understand correctly. But I'm not sure when
459 or how they are used. *)
460 let r = List.Smart.map (fun cl -> map_case_pattern_binders f cl) cll in
461 if r == cll then x
462 else CAst.make ?loc (il,r,rhs)
463
464 let map_pattern_binders f tomatch branches =
465 CList.Smart.map (fun tm -> map_tomatch_binders f tm) tomatch,
466 CList.Smart.map (fun br -> map_cases_branch_binders f br) branches
467
468 (** /mapping of names in binders *)
469
470 let map_tomatch f (c,pp) : tomatch_tuple = f c , pp
471
472 let map_cases_branch f =
473 CAst.map (fun (il,cll,rhs) -> (il , cll , f rhs))
474
475 let map_pattern f tomatch branches =
476 List.map (fun tm -> map_tomatch f tm) tomatch,
477 List.map (fun br -> map_cases_branch f br) branches
478
479 let loc_of_glob_constr c = c.CAst.loc
480
481 (**********************************************************************)
482 (* Alpha-renaming *)
483
484 exception UnsoundRenaming
485
486 let collide_id l id = List.exists (fun (id',id'') -> Id.equal id id' || Id.equal id id'') l
487 let test_id l id = if collide_id l id then raise UnsoundRenaming
488 let test_na l na = Name.iter (test_id l) na
489
490 let update_subst na l =
491 let in_range id l = List.exists (fun (_,id') -> Id.equal id id') l in
492 let l' = Name.fold_right Id.List.remove_assoc na l in
493 Name.fold_right
494 (fun id _ ->
495 if in_range id l' then
496 let id' = Namegen.next_ident_away_from id (fun id' -> in_range id' l') in
497 Name id', (id,id')::l
498 else na,l)
499 na (na,l)
500
501 let rename_var l id =
502 try
503 let id' = Id.List.assoc id l in
504 (* Check that no other earlier binding hides the one found *)
505 let _,(id'',_) = List.extract_first (fun (_,id) -> Id.equal id id') l in
506 if Id.equal id id'' then id' else raise UnsoundRenaming
507 with Not_found ->
508 if List.exists (fun (_,id') -> Id.equal id id') l then raise UnsoundRenaming
509 else id
510
511 let force c = DAst.make ?loc:c.CAst.loc (DAst.get c)
512
513 let rec rename_glob_vars l c = force @@ DAst.map_with_loc (fun ?loc -> function
514 | GVar id as r ->
515 let id' = rename_var l id in
516 if id == id' then r else GVar id'
517 | GRef (GlobRef.VarRef id,_) as r ->
518 if List.exists (fun (_,id') -> Id.equal id id') l then raise UnsoundRenaming
519 else r
520 | GProd (na,r,bk,t,c) ->
521 let na',l' = update_subst na l in
522 GProd (na',r,bk,rename_glob_vars l t,rename_glob_vars l' c)
523 | GLambda (na,r,bk,t,c) ->
524 let na',l' = update_subst na l in
525 GLambda (na',r,bk,rename_glob_vars l t,rename_glob_vars l' c)
526 | GLetIn (na,r,b,t,c) ->
527 let na',l' = update_subst na l in
528 GLetIn (na',r,rename_glob_vars l b,Option.map (rename_glob_vars l) t,rename_glob_vars l' c)
529 (* Lazy strategy: we fail if a collision with renaming occurs, rather than renaming further *)
530 | GCases (ci,po,tomatchl,cls) ->
531 let test_pred_pat (na,ino) =
532 test_na l na; Option.iter (fun {v=(_,nal)} -> List.iter (test_na l) nal) ino in
533 let test_clause idl = List.iter (test_id l) idl in
534 let po = Option.map (rename_glob_vars l) po in
535 let tomatchl = Util.List.map_left (fun (tm,x) -> test_pred_pat x; (rename_glob_vars l tm,x)) tomatchl in
536 let cls = Util.List.map_left (CAst.map (fun (idl,p,c) -> test_clause idl; (idl,p,rename_glob_vars l c))) cls in
537 GCases (ci,po,tomatchl,cls)
538 | GLetTuple (nal,(na,po),c,b) ->
539 List.iter (test_na l) (na::nal);
540 GLetTuple (nal,(na,Option.map (rename_glob_vars l) po),
541 rename_glob_vars l c,rename_glob_vars l b)
542 | GIf (c,(na,po),b1,b2) ->
543 test_na l na;
544 GIf (rename_glob_vars l c,(na,Option.map (rename_glob_vars l) po),
545 rename_glob_vars l b1,rename_glob_vars l b2)
546 | GRec (k,idl,decls,bs,ts) ->
547 Array.iter (test_id l) idl;
548 GRec (k,idl,
549 Array.map (List.map (fun (na,r,k,bbd,bty) ->
550 test_na l na;
551 (na,r,k,Option.map (rename_glob_vars l) bbd,rename_glob_vars l bty))) decls,
552 Array.map (rename_glob_vars l) bs,
553 Array.map (rename_glob_vars l) ts)
554 | _ -> DAst.get (map_glob_constr (rename_glob_vars l) c)
555 ) c
556
557 (**********************************************************************)
558 (* Conversion from glob_constr to cases pattern, if possible *)
559
560 let is_gvar id c = match DAst.get c with
561 | GVar id' -> Id.equal id id'
562 | _ -> false
563
564 let rec cases_pattern_of_glob_constr env na c =
565 (* Forcing evaluation to ensure that the possible raising of
566 Not_found is not delayed *)
567 let c = DAst.force c in
568 DAst.map (function
569 | GVar id ->
570 begin match na with
571 | Name _ ->
572 (* Unable to manage the presence of both an alias and a variable *)
573 raise Not_found
574 | Anonymous -> PatVar (Name id)
575 end
576 | GHole _ -> PatVar na
577 | GGenarg _ -> PatVar na (* XXX does this really make sense? *)
578 | GRef (GlobRef.VarRef id,_) -> PatVar (Name id)
579 | GRef (GlobRef.ConstructRef cstr,_) -> PatCstr (cstr,[],na)
580 | GApp (c, l) ->
581 begin match DAst.get c with
582 | GRef (GlobRef.ConstructRef cstr,_) ->
583 let nparams = Inductiveops.inductive_nparams env (fst cstr) in
584 let _,l = List.chop nparams l in
585 PatCstr (cstr,List.map (cases_pattern_of_glob_constr env Anonymous) l,na)
586 | _ -> raise Not_found
587 end
588 | GLetIn (Name id as na',_,b,None,e) when is_gvar id e && na = Anonymous ->
589 (* A canonical encoding of aliases *)
590 DAst.get (cases_pattern_of_glob_constr env na' b)
591 | _ -> raise Not_found
592 ) c
593
594 open Declarations
595 open Context
596
597 (* Keep only patterns which are not bound to a local definitions *)
598 let drop_local_defs params decls args =
599 let decls = List.skipn (Rel.length params) (List.rev decls) in
600 let rec aux decls args =
601 match decls, args with
602 | [], [] -> []
603 | Rel.Declaration.LocalDef _ :: decls, pat :: args ->
604 begin
605 match DAst.get pat with
606 | PatVar Anonymous -> aux decls args
607 | _ -> raise Not_found (* The pattern is used, one cannot drop it *)
608 end
609 | Rel.Declaration.LocalAssum _ :: decls, a :: args -> a :: aux decls args
610 | _ -> assert false in
611 aux decls args
612
613 let add_patterns_for_params_remove_local_defs env (ind,j) l =
614 let (mib,mip) = Inductive.lookup_mind_specif env ind in
615 let nparams = mib.Declarations.mind_nparams in
616 let l =
617 if mip.mind_consnrealdecls.(j-1) = mip.mind_consnrealargs.(j-1) then
618 (* Optimisation *) l
619 else
620 let (ctx, _) = mip.mind_nf_lc.(j - 1) in
621 drop_local_defs mib.mind_params_ctxt ctx l in
622 Util.List.addn nparams (DAst.make @@ PatVar Anonymous) l
623
624 let add_alias ?loc na c =
625 match na with
626 | Anonymous -> c
627 | Name id -> GLetIn (na,None,DAst.make ?loc c,None,DAst.make ?loc (GVar id))
628
629 (* Turn a closed cases pattern into a glob_constr *)
630 let rec glob_constr_of_cases_pattern_aux env isclosed x = DAst.map_with_loc (fun ?loc -> function
631 | PatCstr (cstr,[],na) -> add_alias ?loc na (GRef (GlobRef.ConstructRef cstr,None))
632 | PatCstr (cstr,l,na) ->
633 let ref = DAst.make ?loc @@ GRef (GlobRef.ConstructRef cstr,None) in
634 let l = add_patterns_for_params_remove_local_defs env cstr l in
635 add_alias ?loc na (GApp (ref, List.map (glob_constr_of_cases_pattern_aux env isclosed) l))
636 | PatVar (Name id) when not isclosed ->
637 GVar id
638 | PatVar Anonymous when not isclosed ->
639 GHole (GQuestionMark {
640 Evar_kinds.default_question_mark with Evar_kinds.qm_obligation=Define false;
641 })
642 | _ -> raise Not_found
643 ) x
644
645 let glob_constr_of_closed_cases_pattern env p = match DAst.get p with
646 | PatCstr (cstr,l,na) ->
647 let loc = p.CAst.loc in
648 na,glob_constr_of_cases_pattern_aux env true (DAst.make ?loc @@ PatCstr (cstr,l,Anonymous))
649 | _ ->
650 raise Not_found
651
652 let glob_constr_of_cases_pattern env p = glob_constr_of_cases_pattern_aux env false p
653
654 let kind_of_glob_kind = function
655 | GImplicitArg (gr, p, b) -> ImplicitArg (gr, p, b)
656 | GBinderType na -> BinderType na
657 | GNamedHole (fresh, id) -> NamedHole id
658 | GQuestionMark qm -> QuestionMark qm
659 | GCasesType -> CasesType false
660 | GInternalHole -> InternalHole
661 | GImpossibleCase -> ImpossibleCase
662
663 let naming_of_glob_kind = function
664 | GNamedHole (true, id) -> Namegen.IntroFresh id
665 | GNamedHole (false, id) -> Namegen.IntroIdentifier id
666 | _ -> Namegen.IntroAnonymous
667
668 (* This has to be in some file... *)
669
670 open Ltac_pretype
671
672 let empty_lvar : ltac_var_map = {
673 ltac_constrs = Id.Map.empty;
674 ltac_uconstrs = Id.Map.empty;
675 ltac_idents = Id.Map.empty;
676 ltac_genargs = Id.Map.empty;
677 }