Debian Codegraph @

interp/constrextern.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 (*i*)
12 open CErrors
13 open Util
14 open Names
15 open Nameops
16 open Termops
17 open Libnames
18 open Impargs
19 open CAst
20 open Notation
21 open Constrexpr
22 open Constrexpr_ops
23 open Notation_ops
24 open Glob_term
25 open Glob_ops
26 open Pattern
27 open Detyping
28 open Structures
29 open Notationextern
30
31 module ExternFlags = PrintingFlags.Extern
32 module NamedDecl = Context.Named.Declaration
33 (*i*)
34
35 (* Translation from glob_constr to front constr *)
36
37 (**********************************************************************)
38
39 let hole = CAst.make @@ CHole (None)
40
41 let is_reserved_type ~flags na t =
42 flags.ExternFlags.use_implicit_types &&
43 match na with
44 | Anonymous -> false
45 | Name id ->
46 try
47 let pat = Reserve.find_reserved_type id in
48 let _ : _ * _ * _ * _ =
49 match_notation_constr ~print_parentheses:true ~factorize_eqns:flags.factorize_eqns
50 t ~vars:Id.Set.empty ([],pat)
51 in
52 true
53 with Not_found | No_match -> false
54
55 (**********************************************************************)
56 (* Various externalisation functions *)
57
58 let insert_delimiters e = function
59 | None -> e
60 | Some sc -> CAst.make @@ CDelimiters (DelimUnboundedScope,sc,e)
61
62 let insert_pat_delimiters ?loc p = function
63 | None -> p
64 | Some sc -> CAst.make ?loc @@ CPatDelimiters (DelimUnboundedScope,sc,p)
65
66 let insert_pat_alias ?loc p = function
67 | Anonymous -> p
68 | Name _ as na -> CAst.make ?loc @@ CPatAlias (p,(CAst.make ?loc na))
69
70 let rec insert_entry_coercion ?loc l c = match l with
71 | [] -> c
72 | (inscope,ntn)::l -> CAst.make ?loc @@ CNotation (Some inscope,ntn,([insert_entry_coercion ?loc l c],[],[],[]))
73
74 let rec insert_pat_coercion ?loc l c = match l with
75 | [] -> c
76 | (inscope,ntn)::l -> CAst.make ?loc @@ CPatNotation (Some inscope,ntn,([insert_pat_coercion ?loc l c],[],[]),[])
77
78 (**********************************************************************)
79 (* conversion of references *)
80
81 let extern_evar n l = CEvar (n,l)
82
83 (** We allow customization of the global_reference printer.
84 For instance, in the debugger the tables of global references
85 may be inaccurate *)
86
87 let rec dirpath_of_modpath = function
88 | MPfile dp -> dp
89 | MPbound mbid -> let (_,id,_) = MBId.repr mbid in DirPath.make [id]
90 | MPdot (t, l) -> Libnames.add_dirpath_suffix (dirpath_of_modpath t) l
91
92 let qualid_of_global = function
93 | GlobRef.VarRef id -> Libnames.qualid_of_ident id
94 (* We rely on the tacite invariant that the label of a constant is used to build its internal name *)
95 | GlobRef.ConstRef cst -> Libnames.make_qualid (dirpath_of_modpath (Constant.modpath cst)) (Constant.label cst)
96 (* We rely on the tacite invariant that an inductive block inherits the name of its first type *)
97 | GlobRef.IndRef (ind,0) -> Libnames.make_qualid (dirpath_of_modpath (MutInd.modpath ind)) (MutInd.label ind)
98 (* These are hacks *)
99 | GlobRef.IndRef (ind,n) -> Libnames.make_qualid (dirpath_of_modpath (MutInd.modpath ind)) (Id.of_string_soft ("<inductive:" ^ Id.to_string (MutInd.label ind) ^ ":" ^ string_of_int n ^ ">"))
100 | GlobRef.ConstructRef ((ind,0),p) -> Libnames.make_qualid (dirpath_of_modpath (MutInd.modpath ind)) (Id.of_string_soft ("<constructor:" ^ Id.to_string (MutInd.label ind) ^ ":" ^ string_of_int (p+1) ^ ">"))
101 | GlobRef.ConstructRef ((ind,n),p) -> Libnames.make_qualid (dirpath_of_modpath (MutInd.modpath ind)) (Id.of_string_soft ("<constructor:" ^ Id.to_string (MutInd.label ind) ^ ":" ^ string_of_int n ^ ":" ^ string_of_int (p+1) ^ ">"))
102
103 let default_extern_reference ?loc vars r =
104 try Nametab.shortest_qualid_of_global ?loc vars r
105 with Not_found -> qualid_of_global r
106
107 let my_extern_reference = ref default_extern_reference
108
109 let set_extern_reference f = my_extern_reference := f
110 let get_extern_reference () = !my_extern_reference
111
112 let extern_reference ?loc vars l = !my_extern_reference vars l
113
114 (**********************************************************************)
115 (* utilities *)
116
117 let rec fill_arg_scopes args subscopes (_,scopes as all) =
118 match args, subscopes with
119 | [], _ -> []
120 | a :: args, scopt :: subscopes ->
121 (a, ((constr_some_level,None), (scopt, scopes))) :: fill_arg_scopes args subscopes all
122 | a :: args, [] ->
123 (a, ((constr_some_level,None), ([], scopes))) :: fill_arg_scopes args [] all
124
125 let overlap_right_left {notation_entry = entry} lev_after ((typs,_):Notation_term.interpretation) =
126 List.exists (fun (_id,(({notation_subentry = entry'; notation_relative_level = lev; notation_position = side},_),_,_)) ->
127 match side with
128 | Some Right when notation_entry_eq entry entry' -> may_capture_cont_after lev_after lev
129 | _ -> false) typs
130
131 let update_with_subscope ~flags from_entry (entry,(scopt,scl)) lev_after closed scopes =
132 let {notation_subentry = entry; notation_relative_level = lev; notation_position = side} = entry in
133 let lev = if flags.ExternFlags.parentheses && side <> None then LevelLe 0 (* min level *) else lev in
134 let lev_after =
135 match side with
136 | Some Left -> Some from_entry.notation_level
137 | Some Right -> if closed then None else lev_after
138 | None -> None in
139 let subentry' = {notation_subentry = entry; notation_relative_level = lev; notation_position = side} in
140 ((subentry',lev_after),(scopt,scl@scopes))
141
142 let find_entry_coercion_with_application ?non_included custom entry is_empty_extra_args =
143 if is_empty_extra_args then
144 (* We need a direct coercion from custom to entry *)
145 match availability_of_entry_coercion ?non_included custom entry with
146 | None -> raise No_match
147 | Some coercion -> coercion, []
148 else
149 (* We need a coercion from custom to constr, then from constr to entry *)
150 match availability_of_entry_coercion custom constr_lowest_level with
151 | None -> raise No_match
152 | Some appcoercion ->
153 match availability_of_entry_coercion constr_some_level entry with
154 | None -> raise No_match
155 | Some coercion -> coercion, appcoercion
156
157 (**********************************************************************)
158 (* mapping patterns to cases_pattern_expr *)
159
160 let add_patt_for_params ind l =
161 if !Flags.in_debugger then l else
162 Util.List.addn (Inductiveops.inductive_nparamdecls (Global.env()) ind) (CAst.make @@ CPatAtom None) l
163
164 let add_cpatt_for_params ind l =
165 if !Flags.in_debugger then l else
166 Util.List.addn (Inductiveops.inductive_nparamdecls (Global.env()) ind) (DAst.make @@ PatVar Anonymous) l
167
168 let drop_implicits_in_patt ~flags cst nb_expl ?(tags=[]) args =
169 let impl_st = implicits_of_global cst in
170 let impl_data = extract_impargs_data impl_st in
171 let rec impls_fit l = function
172 | [], t, _ -> Some (List.rev_append l t)
173 | _, [], _ -> None
174 | t, hh :: tt, true :: tags -> impls_fit (hh :: l) (t, tt, tags)
175 | h::t, { CAst.v = CPatAtom None }::tt, ((false :: tags) | ([] as tags)) when is_status_implicit h -> impls_fit l (t,tt,tags)
176 | h::_, _, _ when is_status_implicit h -> None
177 | _::t, hh::tt, ((false :: tags) | ([] as tags)) -> impls_fit (hh::l) (t,tt,tags)
178 in
179 let try_impls_fit (imps,args,tags) =
180 if not !Constrintern.parsing_explicit &&
181 flags.ExternFlags.implicits &&
182 List.exists is_status_implicit imps
183 (* Note: !print_implicits_explicit_args=true not supported for patterns *)
184 then None
185 else impls_fit [] (imps,args,tags)
186 in
187 let rec select = function
188 | [] -> None
189 | (_,imps)::imps_list ->
190 match try_impls_fit (imps,args, tags) with
191 | None -> select imps_list
192 | x -> x
193 in
194 if Int.equal nb_expl 0 then select impl_data
195 else
196 let imps = List.skipn_at_best nb_expl (select_stronger_impargs impl_st) in
197 try_impls_fit (imps,args, tags)
198
199 let destPrim = function { CAst.v = CPrim t } -> Some t | _ -> None
200 let destPatPrim = function { CAst.v = CPatPrim t } -> Some t | _ -> None
201
202 let make_notation_gen loc ntn mknot mkprim destprim l bl =
203 match snd ntn,List.map destprim l with
204 (* Special case to avoid writing "- 3" for e.g. (Z.opp 3) *)
205 | "- _", [Some (Number p)] when not (NumTok.Signed.is_zero p) ->
206 assert (bl=[]);
207 mknot (loc,ntn,([mknot (loc,(InConstrEntry,"( _ )"),l,[])]),[])
208 | _ ->
209 match decompose_notation_key ntn, l with
210 | (InConstrEntry,[Terminal x]), [] ->
211 begin match String.unquote_coq_string x with
212 | Some s -> mkprim (loc, String s)
213 | None ->
214 match NumTok.Unsigned.parse_string x with
215 | Some n -> mkprim (loc, Number (NumTok.SPlus,n))
216 | None -> mknot (loc,ntn,l,bl) end
217 | (InConstrEntry,[Terminal "-"; Terminal x]), [] ->
218 begin match NumTok.Unsigned.parse_string x with
219 | Some n -> mkprim (loc, Number (NumTok.SMinus,n))
220 | None -> mknot (loc,ntn,l,bl) end
221 | _ -> mknot (loc,ntn,l,bl)
222
223 let make_notation loc (inscope,ntn) (terms,termlists,binders,binderlists as subst) =
224 if not (List.is_empty termlists) || not (List.is_empty binderlists) then
225 CAst.make ?loc @@ CNotation (Some inscope,ntn,subst)
226 else
227 make_notation_gen loc ntn
228 (fun (loc,ntn,l,bl) -> CAst.make ?loc @@ CNotation (Some inscope,ntn,(l,[],bl,[])))
229 (fun (loc,p) -> CAst.make ?loc @@ CPrim p)
230 destPrim terms binders
231
232 let make_pat_notation ?loc (inscope,ntn) (terms,termlists,binders as subst) =
233 if not (List.is_empty termlists && List.is_empty binders) then
234 (CAst.make ?loc @@ CPatNotation (Some inscope,ntn,subst,[]))
235 else
236 make_notation_gen loc ntn
237 (fun (loc,ntn,l,_) -> CAst.make ?loc @@ CPatNotation (Some inscope,ntn,(l,[],[]),[]))
238 (fun (loc,p) -> CAst.make ?loc @@ CPatPrim p)
239 destPatPrim terms []
240
241 let apply_pat_notation (CAst.{v;loc} as c) args =
242 if List.is_empty args then c else
243 match v with
244 | CPatNotation (sc,ntn,subst,[]) -> CAst.make ?loc @@ CPatNotation (sc,ntn,subst,args)
245 | CPatPrim _ -> raise No_match (* TODO: add support for applied primitive token, see also Constrexpr_ops.mkAppPattern *)
246 | CPatDelimiters _ -> raise No_match (* TODO: add support for applied delimited patterns *)
247 | _ -> assert false
248
249 let pattern_printable_in_both_syntax ~flags (ind,_ as c) =
250 let impl_st = extract_impargs_data (implicits_of_global (GlobRef.ConstructRef c)) in
251 let nb_params = Inductiveops.inductive_nparams (Global.env()) ind in
252 List.exists (fun (_,impls) ->
253 (List.length impls >= nb_params) &&
254 let params,args = Util.List.chop nb_params impls in
255 not flags.ExternFlags.implicits &&
256 (List.for_all is_status_implicit params)&&(List.for_all (fun x -> not (is_status_implicit x)) args)
257 ) impl_st
258
259 let extern_record_pattern ~flags cstrsp args =
260 if not (ExternFlags.Records.use_record_syntax flags.ExternFlags.records (fst cstrsp))
261 then None
262 else
263 try
264 let projs = Structure.find_projections (Global.env ()) (fst cstrsp) in
265 let rec ip projs args acc =
266 match projs, args with
267 | [], [] -> acc
268 | proj :: q, pat :: tail ->
269 let acc =
270 match proj, pat with
271 | _, { CAst.v = CPatAtom None } ->
272 (* we don't want to have 'x := _' in our patterns *)
273 acc
274 | Some c, _ ->
275 let loc = pat.CAst.loc in
276 (extern_reference ?loc Id.Set.empty (GlobRef.ConstRef c), pat) :: acc
277 | _ -> raise No_match in
278 ip q tail acc
279 | _ -> assert false
280 in
281 Some (List.rev (ip projs args []))
282 with
283 Not_found | No_match | Exit -> None
284
285 (* Better to use extern_glob_constr composed with injection/retraction ?? *)
286 let rec extern_cases_pattern_in_scope ~flags ((custom,(lev_after:int option)),scopes as allscopes) vars pat =
287 try
288 if !Flags.in_debugger || flags.ExternFlags.raw_literals then raise No_match;
289 let (na,p,key) = uninterp_prim_token_cases_pattern ~print_float:flags.float pat scopes in
290 match availability_of_entry_coercion custom constr_lowest_level with
291 | None -> raise No_match
292 | Some coercion ->
293 let loc = cases_pattern_loc pat in
294 insert_pat_coercion ?loc coercion
295 (insert_pat_alias ?loc (insert_pat_delimiters ?loc (CAst.make ?loc @@ CPatPrim p) key) na)
296 with No_match ->
297 try
298 if !Flags.in_debugger || not flags.ExternFlags.notations then raise No_match;
299 extern_notation_pattern ~flags allscopes vars pat
300 (uninterp_cases_pattern_notations (Global.env ()) pat)
301 with No_match ->
302 let loc = pat.CAst.loc in
303 match DAst.get pat with
304 | PatVar (Name id) when entry_has_global custom || entry_has_ident custom ->
305 CAst.make ?loc (CPatAtom (Some (qualid_of_ident ?loc id)))
306 | pat ->
307 match availability_of_entry_coercion custom constr_lowest_level with
308 | None -> raise No_match
309 | Some coercion ->
310 let allscopes = ((constr_some_level,None),scopes) in
311 let pat = match pat with
312 | PatVar (Name id) -> CAst.make ?loc (CPatAtom (Some (qualid_of_ident ?loc id)))
313 | PatVar (Anonymous) -> CAst.make ?loc (CPatAtom None)
314 | PatCstr(cstrsp,args,na) ->
315 let args = List.map (extern_cases_pattern_in_scope ~flags allscopes vars) args in
316 let p =
317 match extern_record_pattern ~flags cstrsp args with
318 | Some l -> CPatRecord l
319 | None ->
320 let c = extern_reference vars (GlobRef.ConstructRef cstrsp) in
321 if Constrintern.get_asymmetric_patterns () then
322 if pattern_printable_in_both_syntax ~flags cstrsp
323 then CPatCstr (c, None, args)
324 else CPatCstr (c, Some (add_patt_for_params (fst cstrsp) args), [])
325 else
326 let full_args = add_patt_for_params (fst cstrsp) args in
327 let tags = try Inductiveops.constructor_alltags (Global.env()) cstrsp
328 with _ when !Flags.in_debugger -> []
329 in
330 match drop_implicits_in_patt ~flags (GlobRef.ConstructRef cstrsp) 0 ~tags full_args with
331 | Some true_args -> CPatCstr (c, None, true_args)
332 | None -> CPatCstr (c, Some full_args, [])
333 in
334 insert_pat_alias ?loc (CAst.make ?loc p) na
335 in
336 insert_pat_coercion coercion pat
337
338 and apply_notation_to_pattern ?loc ~flags gr ((terms,termlists,binders),(no_implicit,nb_to_drop,more_args))
339 ((custom, lev_after), (tmp_scope, scopes) as allscopes) vars pat rule =
340 let lev_after = if List.is_empty more_args then lev_after else Some Notation.app_level in
341 let extra_args =
342 let subscopes = find_arguments_scope (Global.env ()) gr in
343 let more_args_scopes = try List.skipn nb_to_drop subscopes with Failure _ -> [] in
344 let more_args = fill_arg_scopes more_args more_args_scopes (snd allscopes) in
345 let more_args = List.map (fun (c,allscopes) -> extern_cases_pattern_in_scope ~flags allscopes vars c) more_args in
346 if Constrintern.get_asymmetric_patterns () || not (List.is_empty termlists) then more_args
347 else if no_implicit then more_args else
348 match drop_implicits_in_patt ~flags gr nb_to_drop more_args with
349 | Some true_args -> true_args
350 | None -> raise No_match in
351 match rule with
352 | NotationRule (_,ntn as specific_ntn) ->
353 begin
354 let entry =
355 let entry = fst (Notation.level_of_notation ntn) in
356 if overlap_right_left entry lev_after pat then {entry with notation_level = max_int} else entry in
357 let coercion, appcoercion = find_entry_coercion_with_application custom entry (List.is_empty extra_args) in
358 let closed = not (List.is_empty coercion) in
359 match availability_of_notation specific_ntn (tmp_scope,scopes) with
360 (* Uninterpretation is not allowed in current context *)
361 | None -> raise No_match
362 (* Uninterpretation is allowed in current context *)
363 | Some (scopt,key) ->
364 let scopes' = Option.List.cons scopt scopes in
365 let l =
366 List.map (fun (c,subscope) ->
367 let scopes = update_with_subscope ~flags entry subscope lev_after closed scopes' in
368 extern_cases_pattern_in_scope ~flags scopes vars c)
369 terms in
370 let ll =
371 List.map (fun (c,subscope) ->
372 let scopes = update_with_subscope ~flags entry subscope lev_after closed scopes' in
373 List.map (extern_cases_pattern_in_scope ~flags scopes vars) c)
374 termlists in
375 let bl =
376 List.map (fun (c,subscope) ->
377 let scopes = update_with_subscope ~flags entry subscope lev_after closed scopes' in
378 (extern_cases_pattern_in_scope ~flags scopes vars c, Explicit))
379 binders
380 in
381 insert_pat_coercion appcoercion
382 (insert_pat_delimiters ?loc
383 (apply_pat_notation
384 (insert_pat_coercion coercion
385 (make_pat_notation ?loc specific_ntn (l,ll,bl)))
386 extra_args)
387 key)
388 end
389 | AbbrevRule kn ->
390 match availability_of_entry_coercion custom constr_lowest_level with
391 | None -> raise No_match
392 | Some coercion ->
393 let qid = Nametab.shortest_qualid_of_abbreviation ?loc vars kn in
394 let l1 =
395 List.rev_map (fun (c,(subentry,(scopt,scl))) ->
396 extern_cases_pattern_in_scope ~flags ((subentry,lev_after),(scopt,scl@scopes)) vars c)
397 terms in
398 assert (List.is_empty termlists);
399 assert (List.is_empty binders);
400 insert_pat_coercion ?loc coercion (CAst.make ?loc @@ CPatCstr (qid,None,List.rev_append l1 extra_args))
401
402 and extern_notation_pattern ~flags allscopes vars t = function
403 | [] -> raise No_match
404 | { not_rule = keyrule; not_patt = pat; not_status = n } :: rules ->
405 try
406 if is_printing_inactive_rule keyrule pat then raise No_match;
407 let loc = t.loc in
408 match DAst.get t with
409 | PatCstr (cstr,args,na) ->
410 let t = if na = Anonymous then t else DAst.make ?loc (PatCstr (cstr,args,Anonymous)) in
411 let p = apply_notation_to_pattern ~flags ?loc (GlobRef.ConstructRef cstr)
412 (match_notation_constr_cases_pattern t pat) allscopes vars pat keyrule in
413 insert_pat_alias ?loc p na
414 | PatVar Anonymous -> CAst.make ?loc @@ CPatAtom None
415 | PatVar (Name id) -> CAst.make ?loc @@ CPatAtom (Some (qualid_of_ident ?loc id))
416 with
417 No_match -> extern_notation_pattern ~flags allscopes vars t rules
418
419 let rec extern_notation_ind_pattern ~flags allscopes vars ind args = function
420 | [] -> raise No_match
421 | { not_rule = keyrule; not_patt = pat; not_status = n } :: rules ->
422 try
423 if is_printing_inactive_rule keyrule pat then raise No_match;
424 apply_notation_to_pattern ~flags (GlobRef.IndRef ind)
425 (match_notation_constr_ind_pattern ind args pat) allscopes vars pat keyrule
426 with
427 No_match -> extern_notation_ind_pattern ~flags allscopes vars ind args rules
428
429 let extern_ind_pattern_in_scope ~flags (custom,scopes as allscopes) vars ind args =
430 (* pboutill: There are letins in pat which is incompatible with notations and
431 not explicit application. *)
432 if !Flags.in_debugger then
433 let c = extern_reference vars (GlobRef.IndRef ind) in
434 let args = List.map (extern_cases_pattern_in_scope ~flags allscopes vars) args in
435 CAst.make @@ CPatCstr (c, Some args, [])
436 else
437 try
438 if not flags.ExternFlags.notations || Inductiveops.inductive_has_local_defs (Global.env()) ind
439 then raise No_match;
440 extern_notation_ind_pattern ~flags allscopes vars ind args
441 (uninterp_ind_pattern_notations (Global.env ()) ind)
442 with No_match ->
443 let c = extern_reference vars (GlobRef.IndRef ind) in
444 let args = List.map (extern_cases_pattern_in_scope ~flags allscopes vars) args in
445 let tags = Inductiveops.inductive_alltags (Global.env()) ind in
446 match drop_implicits_in_patt ~flags (GlobRef.IndRef ind) 0 ~tags args with
447 | Some true_args -> CAst.make @@ CPatCstr (c, None, true_args)
448 | None -> CAst.make @@ CPatCstr (c, Some args, [])
449
450 let extern_cases_pattern ~flags vars p =
451 extern_cases_pattern_in_scope ~flags ((constr_some_level,None),([],[])) vars p
452
453 (**********************************************************************)
454 (* Externalising applications *)
455
456 let occur_name na aty =
457 match na with
458 | Name id -> occur_var_constr_expr id aty
459 | Anonymous -> false
460
461 let is_gvar id c = match DAst.get c with
462 | GVar id' -> Id.equal id id'
463 | _ -> false
464
465 let is_projection ~flags nargs r =
466 if not !Flags.in_debugger && flags.ExternFlags.projections then
467 try
468 match r with
469 | GlobRef.ConstRef c ->
470 let n = Structure.projection_nparams (Global.env ()) c + 1 in
471 if n <= nargs then Some n
472 else None
473 | _ -> None
474 with Not_found -> None
475 else None
476
477 let is_hole = function CHole _ | CEvar _ -> true | _ -> false
478
479 let isCRef_no_univ = function
480 | CRef (_,None) -> true
481 | _ -> false
482
483 let is_significant_implicit a =
484 not (is_hole (a.CAst.v))
485
486 let is_needed_for_correct_partial_application tail imp =
487 List.is_empty tail && not (maximal_insertion_of imp)
488
489 exception Expl
490
491 (* Take a list of arguments starting at position [q] and their implicit status *)
492 (* Decide for each implicit argument if it skipped or made explicit *)
493 (* If the removal of implicit arguments is not possible, raise [Expl] *)
494 (* [inctx] tells if the term is in a context which will enforce the external type *)
495 (* [n] is the total number of arguments block to which the [args] belong *)
496 let adjust_implicit_arguments ~flags inctx n args impl =
497 let rec exprec = function
498 | a::args, imp::impl when is_status_implicit imp ->
499 let tail = exprec (args,impl) in
500 let visible =
501 (flags.ExternFlags.implicits && flags.ExternFlags.implicits_explicit_args) ||
502 (is_needed_for_correct_partial_application tail imp) ||
503 (flags.ExternFlags.implicits_defensive &&
504 (not (is_inferable_implicit inctx n imp) || !Flags.beautify) &&
505 is_significant_implicit (Lazy.force a))
506 in
507 if visible then
508 (Lazy.force a,Some (make @@ explicitation imp)) :: tail
509 else
510 tail
511 | a::args, _::impl -> (Lazy.force a,None) :: exprec (args,impl)
512 | args, [] -> List.map (fun a -> (Lazy.force a,None)) args (*In case of polymorphism*)
513 | [], (imp :: _) when is_status_implicit imp && maximal_insertion_of imp ->
514 (* The non-explicit application cannot be parsed back with the same type *)
515 raise Expl
516 | [], _ -> []
517 in exprec (args,impl)
518
519 let extern_projection ~flags inctx f nexpectedparams args impl =
520 let (args1,args2) = List.chop (nexpectedparams + 1) args in
521 let nextraargs = List.length args2 in
522 let (impl1,impl2) = impargs_for_proj ~nexpectedparams ~nextraargs impl in
523 let n = nexpectedparams + 1 + nextraargs in
524 let args1 = adjust_implicit_arguments ~flags inctx n args1 impl1 in
525 let args2 = adjust_implicit_arguments ~flags inctx n args2 impl2 in
526 let (c1,expl), args1 = List.sep_last args1 in
527 assert (expl = None);
528 let c = CProj (false,f,args1,c1) in
529 if args2 = [] then c else CApp (CAst.make c, args2)
530
531 let is_start_implicit = function
532 | imp :: _ -> is_status_implicit imp && maximal_insertion_of imp
533 | [] -> false
534
535 let extern_record ~flags ref args =
536 try
537 let cstrsp = match ref with GlobRef.ConstructRef c -> c | _ -> raise Not_found in
538 if not (ExternFlags.Records.use_record_syntax flags.ExternFlags.records (fst cstrsp))
539 then None
540 else
541 let struc = Structure.find (Global.env ()) (fst cstrsp) in
542 let projs = struc.Structure.projections in
543 let rec cut args n =
544 if Int.equal n 0 then args
545 else
546 match args with
547 | [] -> raise No_match
548 | _ :: t -> cut t (n - 1) in
549 let args = cut args struc.Structure.nparams in
550 let rec ip projs args acc =
551 match projs with
552 | [] -> acc
553 | { Structure.proj_body = None } :: _ -> raise No_match
554 | { Structure.proj_body = Some c; proj_true = false } :: q ->
555 (* we don't want to print locals *)
556 ip q args acc
557 | { Structure.proj_body = Some c; proj_true = true } :: q ->
558 match args with
559 | [] -> raise No_match
560 (* we give up since the constructor is not complete *)
561 | arg :: tail ->
562 let arg = Lazy.force arg in
563 let loc = arg.CAst.loc in
564 let ref = extern_reference ?loc Id.Set.empty (GlobRef.ConstRef c) in
565 ip q tail ((ref, arg) :: acc)
566 in
567 Some (List.rev (ip projs args []))
568 with
569 | Not_found | No_match | Exit -> None
570
571 let extern_global impl f us =
572 if not !Constrintern.parsing_explicit && is_start_implicit impl
573 then
574 CAppExpl ((f, us), [])
575 else
576 CRef (f,us)
577
578 (* Implicit args indexes are in ascending order *)
579 (* inctx is useful only if there is a last argument to be deduced from ctxt *)
580 let extern_applied_ref ~flags inctx impl (cf,f) us args =
581 try
582 if not !Constrintern.parsing_explicit &&
583 flags.ExternFlags.implicits &&
584 not flags.ExternFlags.implicits_explicit_args &&
585 List.exists is_status_implicit impl
586 then raise Expl;
587 let impl = if !Constrintern.parsing_explicit then [] else impl in
588 let n = List.length args in
589 let ref = CRef (f,us) in
590 let r = CAst.make ref in
591 let ip = is_projection ~flags n cf in
592 match ip with
593 | Some i ->
594 (* [t.(f args1) args2] projection-style notation *)
595 extern_projection ~flags inctx (f,us) (i-1) args impl
596 | None ->
597 let args = adjust_implicit_arguments ~flags inctx n args impl in
598 if args = [] then ref else CApp (r, args)
599 with Expl ->
600 (* A [@f args] node *)
601 let args = List.map Lazy.force args in
602 match is_projection ~flags (List.length args) cf with
603 | Some n ->
604 let args = List.map (fun c -> (c,None)) args in
605 let args1, args2 = List.chop n args in
606 let (c1,_), args1 = List.sep_last args1 in
607 let c = CProj (true, (f,us), args1, c1) in
608 if args2 = [] then c else CApp (CAst.make c, args2)
609 | None ->
610 CAppExpl ((f,us), args)
611
612 type application_style =
613 | UseCApp of (Constrexpr.constr_expr * Constrexpr.explicitation CAst.t option) list
614 | UseCAppExpl of constr_expr Lazy.t list
615
616 let is_empty_extra_args = function
617 | UseCApp extra_args -> List.is_empty extra_args
618 | UseCAppExpl extra_args -> List.is_empty extra_args
619
620 let extern_applied_abbreviation (cf,f) abbrevargs = function
621 | UseCApp extraargs ->
622 let abbrevargs = List.map (fun a -> (a,None)) abbrevargs in
623 let args = abbrevargs @ extraargs in
624 if args = [] then cf else CApp (CAst.make cf, args)
625 | UseCAppExpl extraargs ->
626 let args = abbrevargs @ List.map Lazy.force extraargs in
627 CAppExpl ((f,None), args)
628
629 let mkFlattenedCApp (head,args) =
630 match head.CAst.v with
631 | CApp (g,args') ->
632 (* may happen with notations for a prefix of an n-ary application *)
633 (* or after removal of a coercion to funclass *)
634 CApp (g,args'@args)
635 | h ->
636 if List.is_empty args then h else CApp (head, args)
637
638 let extern_applied_notation f = function
639 | UseCApp args -> mkFlattenedCApp (f,args)
640 | UseCAppExpl _ -> raise No_match (* No @f for notations *)
641
642 let extern_args extern env args =
643 let map (arg, argscopes) = lazy (extern argscopes env arg) in
644 List.map map args
645
646 let match_coercion_app c = match DAst.get c with
647 | GApp (r, args) ->
648 begin match DAst.get r with
649 | GRef (r,_) -> Some (c.CAst.loc, r, args)
650 | _ -> None
651 end
652 | _ -> None
653
654 let remove_one_coercion ~flags inctx c =
655 try match match_coercion_app c with
656 | Some (loc,r,args) when not flags.ExternFlags.coercions ->
657 let nargs = List.length args in
658 (match Coercionops.hide_coercion r with
659 | Some nparams when
660 let inctx = inctx || (* coercion to funclass implying being in context *) nparams+1 < nargs in
661 nparams < nargs && inctx ->
662 (* We skip the coercion *)
663 let l = List.skipn nparams args in
664 let (a,l) = match l with a::l -> (a,l) | [] -> assert false in
665 (* Don't flatten App's in case of funclass so that
666 (atomic) notations on [a] work; should be compatible
667 since printer does not care whether App's are
668 collapsed or not and notations with an implicit
669 coercion using funclass either would have already
670 been confused with ordinary application or would have need
671 a surrounding context and the coercion to funclass would
672 have been made explicit to match *)
673 let a' = if List.is_empty l then a else DAst.make ?loc @@ GApp (a,l) in
674 let inctx = inctx || not (List.is_empty l) in
675 Some (nparams+1, inctx, a')
676 | _ -> None)
677 | _ -> None
678 with Not_found ->
679 None
680
681 let rec flatten_application c = match DAst.get c with
682 | GApp (f, l) ->
683 begin match DAst.get f with
684 | GApp(a,l') ->
685 let loc = c.CAst.loc in
686 flatten_application (DAst.make ?loc @@ GApp (a,l'@l))
687 | _ -> c
688 end
689 | a -> c
690
691 let same_binder_type ty nal c =
692 match nal, DAst.get c with
693 | _::_, (GProd (_,_,_,ty',_) | GLambda (_,_,_,ty',_)) -> glob_constr_eq ty ty'
694 | [], _ -> true
695 | _ -> assert false
696
697 (**********************************************************************)
698 (* mapping glob_constr to numerals (in presence of coercions, choose the *)
699 (* one with no delimiter if possible) *)
700
701 let extern_possible_prim_token ~flags ((custom,_),scopes) r =
702 if flags.ExternFlags.raw_literals then raise No_match;
703 let (n,key) = uninterp_prim_token ~print_float:flags.float r scopes in
704 match availability_of_entry_coercion custom constr_lowest_level with
705 | None -> raise No_match
706 | Some coercion ->
707 insert_entry_coercion coercion (insert_delimiters (CAst.make ?loc:(loc_of_glob_constr r) @@ CPrim n) key)
708
709 let filter_enough_applied nargs l =
710 (* This is to ensure that notations for coercions are used only when
711 the coercion is fully applied; not explicitly done yet, but we
712 could also expect that the notation is exactly talking about the
713 coercion *)
714 match nargs with
715 | None -> l
716 | Some nargs ->
717 List.filter (fun rule ->
718 match rule.not_status with
719 | AppBoundedNotation n -> n >= nargs
720 | AppUnboundedNotation | NotAppNotation -> false) l
721
722 (* Helper function for safe and optimal printing of primitive tokens *)
723 (* such as those for Int63 *)
724 let extern_prim_token_delimiter_if_required n key_n scope_n scopes =
725 match availability_of_prim_token n scope_n scopes with
726 | Some None -> CPrim n
727 | None -> CDelimiters(DelimUnboundedScope, key_n, CAst.make (CPrim n))
728 | Some (Some key) -> CDelimiters(DelimUnboundedScope, key, CAst.make (CPrim n))
729
730 (**********************************************************************)
731 (* mapping decl *)
732
733 let extended_glob_local_binder_of_decl loc = function
734 | (p,r,bk,None,t) -> GLocalAssum (p,r,bk,t)
735 | (p,r,bk,Some x, t) ->
736 assert (bk = Explicit);
737 match DAst.get t with
738 | GHole (GNamedHole _) -> GLocalDef (p,r,x,Some t)
739 | GHole _ -> GLocalDef (p,r,x,None)
740 | _ -> GLocalDef (p,r,x,Some t)
741
742 let extended_glob_local_binder_of_decl ?loc u = DAst.make ?loc (extended_glob_local_binder_of_decl loc u)
743
744 (**********************************************************************)
745 (* mapping special floats *)
746
747 (* this helper function is copied from notation.ml as it's not exported *)
748 let qualid_of_ref n =
749 n |> Rocqlib.lib_ref |> Nametab.shortest_qualid_of_global Id.Set.empty
750
751 let q_infinity () = qualid_of_ref "num.float.infinity"
752 let q_neg_infinity () = qualid_of_ref "num.float.neg_infinity"
753 let q_nan () = qualid_of_ref "num.float.nan"
754
755 let extern_float f scopes =
756 if Float64.is_nan f then CRef(q_nan (), None)
757 else if Float64.is_infinity f then CRef(q_infinity (), None)
758 else if Float64.is_neg_infinity f then CRef(q_neg_infinity (), None)
759 else
760 let n = NumTok.Signed.of_string (Float64.to_hex_string f) in
761 extern_prim_token_delimiter_if_required (Number n)
762 "float" "float_scope" scopes
763
764 (**********************************************************************)
765 (* mapping glob_constr to constr_expr *)
766
767 type extern_env = {
768 vars : Id.Set.t;
769 uvars : UnivNames.universe_binders;
770 flags : ExternFlags.t;
771 }
772 let extern_env ~flags env sigma = {
773 vars = vars_of_env env;
774 uvars = Evd.universe_binders sigma;
775 flags;
776 }
777 let empty_extern_env ~flags = {
778 vars = Id.Set.empty;
779 uvars = UnivNames.empty_binders;
780 flags;
781 }
782
783 let on_eenv_vars f eenv = { eenv with vars = f eenv.vars }
784
785 let extern_glob_sort_name uvars = function
786 | GSProp -> CSProp
787 | GProp -> CProp
788 | GSet -> CSet
789 | GLocalUniv u -> CType (qualid_of_lident u)
790 | GRawUniv u -> CRawType u
791 | GUniv u -> begin match UnivNames.qualid_of_level uvars u with
792 | Some qid -> CType qid
793 | None -> CRawType u
794 end
795
796 let extern_glob_qvar uvars = function
797 | GLocalQVar {v=Anonymous;loc} -> CQAnon loc
798 | GLocalQVar {v=Name id; loc} -> CQVar (qualid_of_ident ?loc id)
799 | GRawQVar q -> CRawQVar q
800 | GQVar q -> begin match UnivNames.qualid_of_quality uvars q with
801 | Some qid -> CQVar qid
802 | None -> CRawQVar q
803 end
804
805 let extern_relevance uvars = function
806 | GRelevant -> CRelevant
807 | GIrrelevant -> CIrrelevant
808 | GRelevanceVar q -> CRelevanceVar (extern_glob_qvar uvars q)
809
810 let extern_relevance_info uvars = Option.map (extern_relevance uvars)
811
812 let extern_glob_quality uvars = function
813 | GQConstant q -> CQConstant q
814 | GQualVar q -> CQualVar (extern_glob_qvar uvars q)
815
816 let extern_glob_sort uvars (q, l) =
817 Option.map (extern_glob_qvar uvars) q,
818 map_glob_sort_gen (List.map (on_fst (extern_glob_sort_name uvars))) l
819
820 let extern_instance uvars = function
821 | Some (ql,ul) ->
822 let ql = List.map (extern_glob_quality uvars) ql in
823 let ul = List.map (map_glob_sort_gen (extern_glob_sort_name uvars)) ul in
824 Some (ql,ul)
825 | None -> None
826
827 let extern_ref {vars; uvars} ref us =
828 extern_global (select_stronger_impargs (implicits_of_global ref))
829 (extern_reference vars ref) (extern_instance uvars us)
830
831 let extern_var ?loc id = CRef (qualid_of_ident ?loc id,None)
832
833 let add_vname eenv na = { eenv with vars = add_vname eenv.vars na }
834
835 let rec insert_impargs impargs r = match impargs with
836 | [] -> r
837 | bk :: rest ->
838 match DAst.get r with
839 | GProd (na,rinfo,_,t,c) ->
840 DAst.make ?loc:r.loc (GProd (na, rinfo, bk, t, insert_impargs rest c))
841 | GLetIn (na,rinfo,b,t,c) ->
842 DAst.make ?loc:r.loc (GLetIn (na, rinfo, b, t, insert_impargs impargs c))
843 | _ -> r
844
845 (* Max printing depth is handled by a combination of this and Format's max box depth.
846
847 Strictly speaking we will print an ellipsis for everything which
848 has bigger extern depth and everything which has bigger Pp box
849 depth than the limit, but in practice extern depth is always
850 smaller than box depth.
851
852 As such extern depth acts as an optimization to avoid work on
853 subterms which will anyway be ellipsis'd out. *)
854 type depth = Unlimited | Until of { current : int; max : int }
855
856 let max_depth = ref None
857
858 let set_max_depth d = max_depth := d
859
860 let init_depth flags = match flags.ExternFlags.depth with
861 | None -> Unlimited
862 | Some max -> Until { current = 0; max }
863
864 let succ_depth = function
865 | Unlimited -> Some Unlimited
866 | Until { current; max } ->
867 let current = succ current in
868 if Int.equal current max then None
869 else Some (Until { current; max })
870
871 let ellipsis = Constrexpr.CRef (Libnames.qualid_of_ident (Id.of_string_soft "..."), None)
872
873 let rec extern depth0 inctx scopes (eenv:extern_env) r =
874 let flags = eenv.flags in
875 match succ_depth depth0 with
876 | None -> CAst.make ?loc:r.CAst.loc ellipsis
877 | Some depth ->
878 match remove_one_coercion ~flags inctx (flatten_application r) with
879 | Some (nargs,inctx,r') ->
880 (try extern_notations depth inctx scopes eenv (Some nargs) r
881 with No_match -> extern depth0 inctx scopes eenv r')
882 | None ->
883
884 let r' = match DAst.get r with
885 | GInt i when Rocqlib.has_ref "num.int63.wrap_int" ->
886 let wrap = Rocqlib.lib_ref "num.int63.wrap_int" in
887 DAst.make (GApp (DAst.make (GRef (wrap, None)), [r]))
888 | GFloat f when Rocqlib.has_ref "num.float.wrap_float" ->
889 let wrap = Rocqlib.lib_ref "num.float.wrap_float" in
890 DAst.make (GApp (DAst.make (GRef (wrap, None)), [r]))
891 | _ -> r in
892
893 try extern_notations depth inctx scopes eenv None r'
894 with No_match ->
895
896 let loc = r.CAst.loc in
897 match DAst.get r with
898 | GRef (ref,us) when entry_has_global (fst (fst scopes)) -> CAst.make ?loc (extern_ref eenv ref us)
899
900 | GVar id when entry_has_global (fst (fst scopes)) || entry_has_ident (fst (fst scopes)) ->
901 CAst.make ?loc (extern_var ?loc id)
902
903 | c ->
904
905 match availability_of_entry_coercion (fst (fst scopes)) constr_lowest_level with
906 | None -> raise No_match
907 | Some coercion ->
908
909 let scopes = ((constr_some_level, None), snd scopes) in
910 let c = match c with
911
912 (* The remaining cases are only for the constr entry *)
913
914 | GRef (ref,us) -> extern_ref eenv ref us
915
916 | GVar id -> extern_var ?loc id
917
918 | GEvar (n,l) ->
919 extern_evar n (List.map (on_snd (extern depth false scopes eenv)) l)
920
921 | GPatVar kind ->
922 (match kind with
923 | Evar_kinds.SecondOrderPatVar n -> CPatVar n
924 | Evar_kinds.FirstOrderPatVar n -> CEvar (CAst.make n,[]))
925
926 | GApp (f,args) ->
927 (match DAst.get f with
928 | GRef (ref,us) ->
929 let subscopes = find_arguments_scope (Global.env ()) ref in
930 let args = fill_arg_scopes args subscopes (snd scopes) in
931 let args = extern_args (extern depth true) eenv args in
932 (* Try a "{|...|}" record notation *)
933 (match extern_record ~flags ref args with
934 | Some l -> CRecord l
935 | None ->
936 (* Otherwise... *)
937 extern_applied_ref ~flags inctx
938 (select_stronger_impargs (implicits_of_global ref))
939 (ref,extern_reference ?loc eenv.vars ref) (extern_instance eenv.uvars us) args)
940 | GProj (f,params,c) ->
941 extern_applied_proj depth inctx scopes eenv f params c args
942 | _ ->
943 let args = List.map (fun c -> (sub_extern depth true scopes eenv c,None)) args in
944 let head = sub_extern depth false scopes eenv f in
945 mkFlattenedCApp (head,args))
946
947 | GProj (f,params,c) ->
948 extern_applied_proj depth inctx scopes eenv f params c []
949
950 | GLetIn (na,_,b,t,c) ->
951 CLetIn (make ?loc na,
952 sub_extern depth (Option.has_some t) scopes eenv b,
953 Option.map (extern_typ depth scopes eenv) t,
954 extern depth inctx scopes (add_vname eenv na) c)
955
956 | GProd (na,r,bk,t,c) ->
957 factorize_prod depth scopes eenv na r bk t c
958
959 | GLambda (na,r,bk,t,c) ->
960 factorize_lambda depth inctx scopes eenv na r bk t c
961
962 | GCases (sty,rtntypopt,tml,eqns) ->
963 let vars' =
964 List.fold_right (Name.fold_right Id.Set.add)
965 (cases_predicate_names tml) eenv.vars in
966 let eenv' = { eenv with vars = vars' } in
967 let rtntypopt' = Option.map (extern_typ depth scopes eenv') rtntypopt in
968 let tml = List.map (fun (tm,(na,x)) ->
969 let na' = match na, DAst.get tm with
970 | Anonymous, GVar id ->
971 begin match rtntypopt with
972 | None -> None
973 | Some ntn ->
974 if occur_glob_constr id ntn then
975 Some (CAst.make Anonymous)
976 else None
977 end
978 | Anonymous, _ -> None
979 | Name id, GVar id' when Id.equal id id' -> None
980 | Name _, _ -> Some (CAst.make na) in
981 (sub_extern depth false scopes eenv tm,
982 na',
983 Option.map (fun {CAst.loc;v=(ind,nal)} ->
984 let args = List.map (fun x -> DAst.make @@ PatVar x) nal in
985 let fullargs = add_cpatt_for_params ind args in
986 extern_ind_pattern_in_scope ~flags scopes eenv.vars ind fullargs
987 ) x))
988 tml
989 in
990 let eqns =
991 List.map (extern_eqn depth (inctx || rtntypopt <> None) scopes eenv)
992 (factorize_eqns ~flags:eenv.flags.factorize_eqns eqns)
993 in
994 CCases (sty,rtntypopt',tml,eqns)
995
996 | GLetTuple (nal,(na,typopt),tm,b) ->
997 let inctx = inctx || typopt <> None in
998 CLetTuple (List.map CAst.make nal,
999 (Option.map (fun _ -> (make na)) typopt,
1000 Option.map (extern_typ depth scopes (add_vname eenv na)) typopt),
1001 sub_extern depth false scopes eenv tm,
1002 extern depth inctx scopes (List.fold_left add_vname eenv nal) b)
1003
1004 | GIf (c,(na,typopt),b1,b2) ->
1005 let inctx = inctx || typopt <> None in
1006 CIf (sub_extern depth false scopes eenv c,
1007 (Option.map (fun _ -> (CAst.make na)) typopt,
1008 Option.map (extern_typ depth scopes (add_vname eenv na)) typopt),
1009 sub_extern depth inctx scopes eenv b1, sub_extern depth inctx scopes eenv b2)
1010
1011 | GRec (fk,idv,blv,tyv,bv) ->
1012 let eenv' = on_eenv_vars (Array.fold_right Id.Set.add idv) eenv in
1013 (match fk with
1014 | GFix (nv,n) ->
1015 let listdecl =
1016 Array.mapi (fun i fi ->
1017 let (bl,ty,def) = blv.(i), tyv.(i), bv.(i) in
1018 let bl = List.map (extended_glob_local_binder_of_decl ?loc) bl in
1019 let (assums,ids,bl) = extern_local_binder depth scopes eenv bl in
1020 let eenv0 = on_eenv_vars (List.fold_right (Name.fold_right Id.Set.add) ids) eenv in
1021 let eenv1 = on_eenv_vars (List.fold_right (Name.fold_right Id.Set.add) ids) eenv' in
1022 let n =
1023 match nv.(i) with
1024 | None -> None
1025 | Some x -> Some (CAst.make @@ CStructRec (CAst.make @@ Name.get_id (List.nth assums x)))
1026 in
1027 ((CAst.make fi), None, n, bl, extern_typ depth scopes eenv0 ty,
1028 sub_extern depth true scopes eenv1 def)) idv
1029 in
1030 CFix (CAst.(make ?loc idv.(n)), Array.to_list listdecl)
1031 | GCoFix n ->
1032 let listdecl =
1033 Array.mapi (fun i fi ->
1034 let bl = List.map (extended_glob_local_binder_of_decl ?loc) blv.(i) in
1035 let (_,ids,bl) = extern_local_binder depth scopes eenv bl in
1036 let eenv0 = on_eenv_vars (List.fold_right (Name.fold_right Id.Set.add) ids) eenv in
1037 let eenv1 = on_eenv_vars (List.fold_right (Name.fold_right Id.Set.add) ids) eenv' in
1038 ((CAst.make fi),None,bl,extern_typ depth scopes eenv0 tyv.(i),
1039 sub_extern depth true scopes eenv1 bv.(i))) idv
1040 in
1041 CCoFix (CAst.(make ?loc idv.(n)),Array.to_list listdecl))
1042
1043 | GSort s -> CSort (extern_glob_sort eenv.uvars s)
1044
1045 | GHole e -> CHole (Some e)
1046
1047 | GGenarg arg -> CGenargGlob arg
1048
1049 | GCast (c, k, c') ->
1050 let scl = Notation.compute_glob_type_scope c' in
1051 let c' = extern_typ depth scopes eenv c' in
1052 let c = extern depth true (fst scopes,(scl, snd (snd scopes))) eenv c in
1053 CCast (c, k, c')
1054
1055 | GInt i ->
1056 extern_prim_token_delimiter_if_required
1057 (Number NumTok.(Signed.of_bigint CHex (Z.of_int64 (Uint63.to_int64 i))))
1058 "uint63" "uint63_scope" (snd scopes)
1059
1060 | GFloat f -> extern_float f (snd scopes)
1061
1062 | GString s ->
1063 extern_prim_token_delimiter_if_required
1064 (String (Pstring.to_string s))
1065 "pstring" "pstring_scope" (snd scopes)
1066
1067 | GArray(u,t,def,ty) ->
1068 CArray(
1069 extern_instance eenv.uvars u,
1070 Array.map (extern depth inctx scopes eenv) t,
1071 extern depth inctx scopes eenv def,
1072 extern_typ depth scopes eenv ty)
1073
1074 in insert_entry_coercion coercion (CAst.make ?loc c)
1075
1076 and extern_typ depth (subentry,(_,scopes)) =
1077 extern depth true (subentry,(Notation.current_type_scope_names (),scopes))
1078
1079 and sub_extern depth inctx (subentry,(_,scopes)) = extern depth inctx (subentry,([],scopes))
1080
1081 and factorize_prod depth scopes eenv na r bk t c =
1082 let implicit_type = is_reserved_type ~flags:eenv.flags na t in
1083 let r = extern_relevance_info eenv.uvars r in
1084 let aty = extern_typ depth scopes eenv t in
1085 let eenv = add_vname eenv na in
1086 let store, get = set_temporary_memory () in
1087 match na, DAst.get c with
1088 | Name id, GCases (Constr.LetPatternStyle, None, [(e,(Anonymous,None))],(_::_ as eqns))
1089 when is_gvar id e && List.length (store (factorize_eqns ~flags:eenv.flags.factorize_eqns eqns)) = 1 ->
1090 (match get () with
1091 | [{CAst.v=(ids,disj_of_patl,b)}] ->
1092 let disjpat = List.map (function [pat] -> pat | _ -> assert false) disj_of_patl in
1093 let disjpat = if occur_glob_constr id b then List.map (set_pat_alias id) disjpat else disjpat in
1094 let b = extern_typ depth scopes eenv b in
1095 let p = mkCPatOr (List.map (extern_cases_pattern_in_scope ~flags:eenv.flags scopes eenv.vars) disjpat) in
1096 let binder = CLocalPattern p in
1097 (match b.v with
1098 | CProdN (bl,b) -> CProdN (binder::bl,b)
1099 | _ -> CProdN ([binder],b))
1100 | _ -> assert false)
1101 | _, _ ->
1102 let c' = extern_typ depth scopes eenv c in
1103 match na, c'.v with
1104 | Name id, CProdN (CLocalAssum(nal,r',Default bk',ty)::bl,b)
1105 when relevance_info_expr_eq r r'
1106 && binding_kind_eq bk bk'
1107 && not (occur_var_constr_expr id ty) (* avoid na in ty escapes scope *)
1108 && (constr_expr_eq aty ty || (constr_expr_eq ty hole && same_binder_type t nal c)) ->
1109 let ty = if implicit_type then ty else aty in
1110 CProdN (CLocalAssum(make na::nal,r,Default bk,ty)::bl,b)
1111 | _, CProdN (bl,b) ->
1112 let ty = if implicit_type then hole else aty in
1113 CProdN (CLocalAssum([make na],r,Default bk,ty)::bl,b)
1114 | _, _ ->
1115 let ty = if implicit_type then hole else aty in
1116 CProdN ([CLocalAssum([make na],r,Default bk,ty)],c')
1117
1118 and factorize_lambda depth inctx scopes eenv na r bk t c =
1119 let implicit_type = is_reserved_type ~flags:eenv.flags na t in
1120 let r = extern_relevance_info eenv.uvars r in
1121 let aty = extern_typ depth scopes eenv t in
1122 let eenv = add_vname eenv na in
1123 let store, get = set_temporary_memory () in
1124 match na, DAst.get c with
1125 | Name id, GCases (Constr.LetPatternStyle, None, [(e,(Anonymous,None))],(_::_ as eqns))
1126 when is_gvar id e && List.length (store (factorize_eqns ~flags:eenv.flags.factorize_eqns eqns)) = 1 ->
1127 (match get () with
1128 | [{CAst.v=(ids,disj_of_patl,b)}] ->
1129 let disjpat = List.map (function [pat] -> pat | _ -> assert false) disj_of_patl in
1130 let disjpat = if occur_glob_constr id b then List.map (set_pat_alias id) disjpat else disjpat in
1131 let b = sub_extern depth inctx scopes eenv b in
1132 let p = mkCPatOr (List.map (extern_cases_pattern_in_scope ~flags:eenv.flags scopes eenv.vars) disjpat) in
1133 let binder = CLocalPattern p in
1134 (match b.v with
1135 | CLambdaN (bl,b) -> CLambdaN (binder::bl,b)
1136 | _ -> CLambdaN ([binder],b))
1137 | _ -> assert false)
1138 | _, _ ->
1139 let c' = sub_extern depth inctx scopes eenv c in
1140 match c'.v with
1141 | CLambdaN (CLocalAssum(nal,r',Default bk',ty)::bl,b)
1142 when relevance_info_expr_eq r r'
1143 && binding_kind_eq bk bk'
1144 && not (occur_name na ty) (* avoid na in ty escapes scope *)
1145 && (constr_expr_eq aty ty || (constr_expr_eq ty hole && same_binder_type t nal c)) ->
1146 let aty = if implicit_type then ty else aty in
1147 CLambdaN (CLocalAssum(make na::nal,r,Default bk,aty)::bl,b)
1148 | CLambdaN (bl,b) ->
1149 let ty = if implicit_type then hole else aty in
1150 CLambdaN (CLocalAssum([make na],r,Default bk,ty)::bl,b)
1151 | _ ->
1152 let ty = if implicit_type then hole else aty in
1153 CLambdaN ([CLocalAssum([make na],r,Default bk,ty)],c')
1154
1155 and extern_local_binder depth scopes eenv = function
1156 [] -> ([],[],[])
1157 | b :: l ->
1158 match DAst.get b with
1159 | GLocalDef (na,r,bd,ty) ->
1160 let (assums,ids,l) =
1161 extern_local_binder depth scopes (on_eenv_vars (Name.fold_right Id.Set.add na) eenv) l in
1162 (assums,na::ids,
1163 CLocalDef(CAst.make na, extern_relevance_info eenv.uvars r, extern depth false scopes eenv bd,
1164 Option.map (extern_typ depth scopes eenv) ty) :: l)
1165
1166 | GLocalAssum (na,r,bk,ty) ->
1167 let implicit_type = is_reserved_type ~flags:eenv.flags na ty in
1168 let ty = extern_typ depth scopes eenv ty in
1169 (match extern_local_binder depth scopes (on_eenv_vars (Name.fold_right Id.Set.add na) eenv) l with
1170 | (assums,ids,CLocalAssum(nal,r',k,ty')::l)
1171 when (constr_expr_eq ty ty' || implicit_type && constr_expr_eq ty' hole) &&
1172 binder_kind_eq k (Default bk) &&
1173 match na with Name id -> not (occur_var_constr_expr id ty')
1174 | _ -> true ->
1175 (na::assums,na::ids,
1176 CLocalAssum(CAst.make na::nal,r',k,ty')::l)
1177 | (assums,ids,l) ->
1178 let ty = if implicit_type then hole else ty in
1179 (na::assums,na::ids,
1180 CLocalAssum([CAst.make na],extern_relevance_info eenv.uvars r,Default bk,ty) :: l))
1181
1182 | GLocalPattern ((p,_),_,bk,_) ->
1183 let p = mkCPatOr (List.map (extern_cases_pattern ~flags:eenv.flags eenv.vars) p) in
1184 let (assums,ids,l) = extern_local_binder depth scopes eenv l in
1185 (assums,ids, CLocalPattern p :: l)
1186
1187 and extern_eqn depth inctx scopes eenv {CAst.loc;v=(ids,pll,c)} =
1188 let pll = List.map (List.map (extern_cases_pattern_in_scope ~flags:eenv.flags scopes eenv.vars)) pll in
1189 make ?loc (pll,extern depth inctx scopes eenv c)
1190
1191 and extern_notations depth inctx scopes eenv nargs t =
1192 try extern_possible_prim_token ~flags:eenv.flags scopes t
1193 with No_match ->
1194 if not eenv.flags.notations then raise No_match;
1195 let t = flatten_application t in
1196 extern_notation depth inctx scopes eenv t (filter_enough_applied nargs (uninterp_notations (Global.env ()) t))
1197
1198 and extern_notation depth inctx ((custom,(lev_after: int option)),scopes as allscopes) eenv t rules =
1199 match rules with
1200 | [] -> raise No_match
1201 | { not_rule = keyrule; not_patt = pat; not_status = n } :: rules ->
1202 let loc = Glob_ops.loc_of_glob_constr t in
1203 try
1204 if is_printing_inactive_rule keyrule pat then raise No_match;
1205 let f,args =
1206 match DAst.get t with
1207 | GApp (f,args) -> f,args
1208 | _ -> t,[] in
1209 let nallargs = List.length args in
1210 let argsscopes,argsimpls =
1211 match DAst.get f with
1212 | GRef (ref,_) ->
1213 let subscopes = find_arguments_scope (Global.env ()) ref in
1214 let impls = select_stronger_impargs (implicits_of_global ref) in
1215 subscopes, impls
1216 | _ ->
1217 [], [] in
1218 (* Adjust to the number of arguments expected by the notation *)
1219 let (t,args,argsscopes,argsimpls) = match n with
1220 | AppBoundedNotation n when nallargs >= n ->
1221 let args1, args2 = List.chop n args in
1222 let args2scopes = try List.skipn n argsscopes with Failure _ -> [] in
1223 let args2impls =
1224 if n = 0 then
1225 (* Note: NApp(NRef f,[]), hence n=0, encodes @f and
1226 conventionally deactivates implicit arguments *)
1227 []
1228 else try List.skipn n argsimpls with Failure _ -> [] in
1229 DAst.make @@ GApp (f,args1), args2, args2scopes, args2impls
1230 | AppUnboundedNotation -> t, [], [], []
1231 | NotAppNotation ->
1232 begin match DAst.get f with
1233 | GRef (ref,us) -> f, args, argsscopes, argsimpls
1234 | _ -> t, [], [], []
1235 end
1236 | AppBoundedNotation _ -> raise No_match in
1237 (* Try matching ... *)
1238 let terms,termlists,binders,binderlists =
1239 match_notation_constr
1240 ~print_parentheses:eenv.flags.parentheses
1241 ~factorize_eqns:eenv.flags.factorize_eqns
1242 t ~vars:eenv.vars pat
1243 in
1244 let lev_after = if List.is_empty args then lev_after else Some Notation.app_level in
1245 (* Try externing extra args... *)
1246 let extra_args =
1247 let args = fill_arg_scopes args argsscopes (snd allscopes) in
1248 let args = extern_args (extern depth true) eenv args in
1249 try UseCApp (adjust_implicit_arguments ~flags:eenv.flags inctx nallargs args argsimpls) with Expl -> UseCAppExpl args in
1250 (* Try availability of interpretation ... *)
1251 match keyrule with
1252 | NotationRule (_,ntn as specific_ntn) ->
1253 let entry = fst (Notation.level_of_notation ntn) in
1254 let non_included = overlap_right_left entry lev_after pat in
1255 let coercion, appcoercion = find_entry_coercion_with_application ~non_included custom entry (is_empty_extra_args extra_args) in
1256 (match availability_of_notation specific_ntn scopes with
1257 (* Uninterpretation is not allowed in current context *)
1258 | None -> raise No_match
1259 (* Uninterpretation is allowed in current context *)
1260 | Some (scopt,key) ->
1261 let closed = not (List.is_empty coercion) in
1262 let scopes' = Option.List.cons scopt (snd scopes) in
1263 let l =
1264 List.map (fun ((vars,c),subscope) ->
1265 let scopes = update_with_subscope ~flags:eenv.flags entry subscope lev_after closed scopes' in
1266 extern depth (* assuming no overloading: *) true scopes { eenv with vars} c)
1267 terms
1268 in
1269 let ll =
1270 List.map (fun ((vars,l),subscope) ->
1271 let scopes = update_with_subscope ~flags:eenv.flags entry subscope lev_after closed scopes' in
1272 List.map (extern depth true scopes { eenv with vars }) l)
1273 termlists
1274 in
1275 let bl =
1276 List.map (fun ((vars,bl),subscope) ->
1277 let scopes = update_with_subscope ~flags:eenv.flags entry subscope lev_after closed scopes' in
1278 (mkCPatOr
1279 (List.map
1280 (extern_cases_pattern_in_scope ~flags:eenv.flags scopes vars) bl)),
1281 Explicit)
1282 binders
1283 in
1284 let bll =
1285 List.map (fun ((vars,bl),subscope) ->
1286 let scopes = update_with_subscope ~flags:eenv.flags entry subscope lev_after closed scopes' in
1287 pi3 (extern_local_binder depth scopes { eenv with vars } bl))
1288 binderlists
1289 in
1290 let c = make_notation loc specific_ntn (l,ll,bl,bll) in
1291 let c = insert_entry_coercion coercion (insert_delimiters c key) in
1292 insert_entry_coercion appcoercion (CAst.make ?loc @@ extern_applied_notation c extra_args))
1293 | AbbrevRule kn ->
1294 let l =
1295 List.map (fun ((vars,c),(subentry,(scopt,scl))) ->
1296 extern depth true ((subentry,lev_after),(scopt,scl@snd scopes)) { eenv with vars } c)
1297 terms
1298 in
1299 let cf = Nametab.shortest_qualid_of_abbreviation ?loc eenv.vars kn in
1300 let a = CRef (cf,None) in
1301 let c = CAst.make ?loc @@ extern_applied_abbreviation (a,cf) l extra_args in
1302 if isCRef_no_univ c.CAst.v && entry_has_global custom then c
1303 else match availability_of_entry_coercion custom constr_lowest_level with
1304 | None -> raise No_match
1305 | Some coercion -> insert_entry_coercion coercion c
1306 with
1307 No_match -> extern_notation depth inctx allscopes eenv t rules
1308
1309 and extern_applied_proj depth inctx scopes eenv (cst,us) params c extraargs =
1310 let ref = GlobRef.ConstRef cst in
1311 let subscopes = find_arguments_scope (Global.env ()) ref in
1312 let nparams = List.length params in
1313 let args = params @ c :: extraargs in
1314 let args = fill_arg_scopes args subscopes (snd scopes) in
1315 let args = extern_args (extern depth true) eenv args in
1316 let imps = select_stronger_impargs (implicits_of_global ref) in
1317 let f = extern_reference eenv.vars ref in
1318 let us = extern_instance eenv.uvars us in
1319 extern_projection ~flags:eenv.flags inctx (f,us) nparams args imps
1320
1321 let extern inctx scopes eenv c : constr_expr = extern (init_depth eenv.flags) inctx scopes eenv c
1322
1323 let extern_glob_constr eenv c =
1324 extern false ((constr_some_level,None),([],[])) eenv c
1325
1326 let extern_glob_type ?impargs eenv c =
1327 let c = Option.fold_right insert_impargs impargs c in
1328 extern_typ (init_depth eenv.flags) ((constr_some_level,None),([],[])) eenv c
1329
1330 (******************************************************************)
1331 (* Main translation function from constr -> constr_expr *)
1332
1333 let extern_constr ?(inctx=false) ?scope ~(flags:PrintingFlags.t) env sigma t =
1334 let r = Detyping.detype Detyping.Later ~flags:flags.detype env sigma t in
1335 let eenv = extern_env ~flags:flags.extern env sigma in
1336 let scope = Option.cata (fun x -> [x]) [] scope in
1337 extern inctx ((constr_some_level,None),(scope,[])) eenv r
1338
1339 let extern_constr_in_scope ?inctx scope ~flags env sigma t =
1340 extern_constr ?inctx ~scope ~flags env sigma t
1341
1342 let make_avoid goal_concl_style env =
1343 if goal_concl_style then (Namegen.Generator.fresh, Fresh.of_set @@ vars_of_env env)
1344 (* TODO: this is linear in the size of the environment, maybe we can do better *)
1345 else (Namegen.Generator.fresh, Nameops.Fresh.empty)
1346
1347 let extern_type ?(goal_concl_style=false) ~(flags:PrintingFlags.t) env sigma ?impargs t =
1348 (* "goal_concl_style" means do alpha-conversion using the "goal" convention *)
1349 (* i.e.: avoid using the names of goal/section/rel variables and the short *)
1350 (* names of global definitions of current module when computing names for *)
1351 (* bound variables. *)
1352 (* Not "goal_concl_style" means do alpha-conversion avoiding only *)
1353 (* those goal/section/rel variables that occurs in the subterm under *)
1354 (* consideration; see namegen.ml for further details *)
1355 let avoid = make_avoid goal_concl_style env in
1356 let r = Detyping.detype Detyping.Later ~isgoal:goal_concl_style ~avoid ~flags:flags.detype env sigma t in
1357 extern_glob_type ?impargs (extern_env ~flags:flags.extern env sigma) r
1358
1359 let extern_sort ~universes ~qualities sigma s =
1360 extern_glob_sort (Evd.universe_binders sigma)
1361 (detype_sort ~universes ~qualities sigma s)
1362
1363 let extern_closed_glob ?(goal_concl_style=false) ?(inctx=false) ?scope ~(flags:PrintingFlags.t) env sigma t =
1364 let avoid = make_avoid goal_concl_style env in
1365 let r =
1366 Detyping.detype_closed_glob ~isgoal:goal_concl_style ~avoid ~flags:flags.detype env sigma t
1367 in
1368 let eenv = extern_env env sigma ~flags:flags.extern in
1369 let scope = Option.cata (fun x -> [x]) [] scope in
1370 extern inctx ((constr_some_level,None),(scope,[])) eenv r
1371
1372 (******************************************************************)
1373 (* Main translation function from pattern -> constr_expr *)
1374
1375 (* thunk for value restriction *)
1376 let any_any_branch () =
1377 (* | _ => _ *)
1378 CAst.make ([],[DAst.make @@ PatVar Anonymous], DAst.make @@ GHole (GInternalHole))
1379
1380 let genset = Namegen.Generator.idset
1381
1382 let next_name_away na (gen, avoid) =
1383 let (id, avoid) = Namegen.Generator.next_name_away gen na avoid in
1384 (id, (gen, avoid))
1385
1386 let compute_displayed_let_name_in env sigma flags (gen, avoid) na =
1387 let open Namegen in
1388 let na, avoid = compute_displayed_let_name_in gen env sigma flags avoid na in
1389 na, (gen, avoid)
1390
1391 let compute_displayed_name_in_pattern env sigma (gen, avoid) na c =
1392 let open Namegen in
1393 let na, avoid = compute_displayed_name_in_gen gen (fun _ -> Patternops.noccurn_pattern) env sigma avoid na c in
1394 na, (gen, avoid)
1395
1396 let glob_of_pat_under_context glob_of_pat avoid env sigma (nas, pat) =
1397 let fold (avoid, env, nas, epat) na =
1398 let na, avoid = compute_displayed_name_in_pattern (Global.env ()) sigma avoid na epat in
1399 let env = Termops.add_name na env in
1400 let epat = match epat with PLambda (_, _, p) -> p | _ -> assert false in
1401 (avoid, env, na :: nas, epat)
1402 in
1403 let epat = Array.fold_right (fun na p -> PLambda (na, PMeta None, p)) nas pat in
1404 let (avoid', env', nas, _) = Array.fold_left fold (avoid, env, [], epat) nas in
1405 let pat = glob_of_pat avoid' env' sigma pat in
1406 (Array.rev_of_list nas, pat)
1407
1408 let rec glob_of_pat
1409 : 'a 'g 's. ('a -> 'g glob_constr_r) -> 's Namegen.Generator.input -> _ -> _ -> 'a constr_pattern_r -> 'g glob_constr_g
1410 = fun (type a g s) (of_extra:a -> g glob_constr_r) (avoid : s Namegen.Generator.t * s) env sigma (pat: a constr_pattern_r) ->
1411 let open Sorts.Quality in
1412 DAst.make @@ match pat with
1413 | PRef ref -> GRef (ref,None)
1414 | PVar id -> GVar id
1415 | PEvar (evk,l) ->
1416 let filter (id, pat) = match pat with PVar id' -> Id.equal id id' | _ -> true in
1417 let EvarInfo evi = Evd.find sigma evk in
1418 let hyps = Evd.evar_filtered_context evi in
1419 let map decl pat = NamedDecl.get_id decl, pat in
1420 let l = List.filter filter @@ List.map2 map hyps l in
1421 let id = match Evd.evar_ident evk sigma with
1422 | None -> "__"
1423 | Some id -> Libnames.string_of_path id
1424 in
1425 let id = Id.of_string_soft id in
1426 GEvar (CAst.make id,List.map (fun (id,c) -> (CAst.make id, glob_of_pat of_extra avoid env sigma c)) l)
1427 | PRel n ->
1428 let id = try match lookup_name_of_rel n env with
1429 | Name id -> id
1430 | Anonymous ->
1431 anomaly ~label:"glob_constr_of_pattern" (Pp.str "index to an anonymous variable.")
1432 with Not_found -> Id.of_string ("_UNBOUND_REL_"^(string_of_int n)) in
1433 GVar id
1434 | PMeta None -> GHole (GInternalHole)
1435 | PMeta (Some n) -> GPatVar (Evar_kinds.FirstOrderPatVar n)
1436 | PExtra g -> of_extra g
1437 | PProj (p,c) -> GApp (DAst.make @@ GRef (GlobRef.ConstRef (Projection.constant p),None),
1438 [glob_of_pat of_extra avoid env sigma c])
1439 | PApp (f,args) ->
1440 GApp (glob_of_pat of_extra avoid env sigma f,Array.map_to_list (glob_of_pat of_extra avoid env sigma) args)
1441 | PSoApp (n,args) ->
1442 GApp (DAst.make @@ GPatVar (Evar_kinds.SecondOrderPatVar n),
1443 List.map (glob_of_pat of_extra avoid env sigma) args)
1444 | PProd (na,t,c) ->
1445 let na',avoid' = compute_displayed_name_in_pattern (Global.env ()) sigma avoid na c in
1446 let env' = Termops.add_name na' env in
1447 GProd (na',None,Explicit,glob_of_pat of_extra avoid env sigma t,glob_of_pat of_extra avoid' env' sigma c)
1448 | PLetIn (na,b,t,c) ->
1449 let na',avoid' = compute_displayed_let_name_in (Global.env ()) sigma Namegen.RenamingForGoal avoid na in
1450 let env' = Termops.add_name na' env in
1451 GLetIn (na',None,glob_of_pat of_extra avoid env sigma b, Option.map (glob_of_pat of_extra avoid env sigma) t,
1452 glob_of_pat of_extra avoid' env' sigma c)
1453 | PLambda (na,t,c) ->
1454 let na',avoid' = compute_displayed_name_in_pattern (Global.env ()) sigma avoid na c in
1455 let env' = Termops.add_name na' env in
1456 GLambda (na',None,Explicit,glob_of_pat of_extra avoid env sigma t, glob_of_pat of_extra avoid' env' sigma c)
1457 | PIf (c,b1,b2) ->
1458 GIf (glob_of_pat of_extra avoid env sigma c, (Anonymous,None),
1459 glob_of_pat of_extra avoid env sigma b1, glob_of_pat of_extra avoid env sigma b2)
1460 | PCase ({cip_style=Constr.LetStyle},None,tm,[(0,n,b)]) ->
1461 let n, b = glob_of_pat_under_context (glob_of_pat of_extra) avoid env sigma (n, b) in
1462 let nal = Array.to_list n in
1463 GLetTuple (nal,(Anonymous,None),glob_of_pat of_extra avoid env sigma tm,b)
1464 | PCase (info,p,tm,bl) ->
1465 let mat = match bl, info.cip_ind with
1466 | [], _ -> []
1467 | _, Some ind ->
1468 let map (i, n, c) =
1469 let n, c = glob_of_pat_under_context (glob_of_pat of_extra) avoid env sigma (n, c) in
1470 let nal = Array.to_list n in
1471 let mkPatVar na = DAst.make @@ PatVar na in
1472 let p = DAst.make @@ PatCstr ((ind,i+1),List.map mkPatVar nal,Anonymous) in
1473 let ids = List.map_filter Nameops.Name.to_option nal in
1474 CAst.make @@ (ids,[p],c)
1475 in
1476 List.map map bl
1477 | _, None -> anomaly (Pp.str "PCase with some branches but unknown inductive.")
1478 in
1479 let mat = if info.cip_extensible then mat @ [any_any_branch ()] else mat
1480 in
1481 let indnames,rtn = match p, info.cip_ind with
1482 | None, _ -> (Anonymous,None),None
1483 | Some p, Some ind ->
1484 let nas, p = glob_of_pat_under_context (glob_of_pat of_extra) avoid env sigma p in
1485 let nas = Array.rev_to_list nas in
1486 ((List.hd nas, Some (CAst.make (ind, List.tl nas))), Some p)
1487 | _ -> anomaly (Pp.str "PCase with non-trivial predicate but unknown inductive.")
1488 in
1489 GCases (Constr.RegularStyle,rtn,[glob_of_pat of_extra avoid env sigma tm,indnames],mat)
1490 | PFix ((ln,i),(lna,tl,bl)) ->
1491 let def_avoid, def_env, lfi =
1492 Array.fold_left
1493 (fun (avoid, env, l) na ->
1494 let id, avoid = next_name_away na avoid in
1495 (avoid, Name id :: env, id::l))
1496 (avoid, env, []) lna in
1497 let n = Array.length tl in
1498 let v = Array.map3
1499 (fun c t i -> Detyping.share_pattern_names (glob_of_pat of_extra) (i+1) [] def_avoid def_env sigma c (Patternops.lift_pattern n t))
1500 bl tl ln in
1501 GRec(GFix (Array.map (fun i -> Some i) ln,i),Array.of_list (List.rev lfi),
1502 Array.map (fun (bl,_,_) -> bl) v,
1503 Array.map (fun (_,_,ty) -> ty) v,
1504 Array.map (fun (_,bd,_) -> bd) v)
1505 | PCoFix (ln,(lna,tl,bl)) ->
1506 let def_avoid, def_env, lfi =
1507 Array.fold_left
1508 (fun (avoid, env, l) na ->
1509 let id, avoid = next_name_away na avoid in
1510 (avoid, Name id :: env, id::l))
1511 (avoid, env, []) lna in
1512 let ntys = Array.length tl in
1513 let v = Array.map2
1514 (fun c t -> share_pattern_names (glob_of_pat of_extra) 0 [] def_avoid def_env sigma c (Patternops.lift_pattern ntys t))
1515 bl tl in
1516 GRec(GCoFix ln,Array.of_list (List.rev lfi),
1517 Array.map (fun (bl,_,_) -> bl) v,
1518 Array.map (fun (_,_,ty) -> ty) v,
1519 Array.map (fun (_,bd,_) -> bd) v)
1520 | PSort (Qual (QConstant QSProp)) -> GSort Glob_ops.glob_SProp_sort
1521 | PSort (Qual (QConstant QProp)) -> GSort Glob_ops.glob_Prop_sort
1522 | PSort (Qual (QConstant QType | QVar _)) -> GSort Glob_ops.glob_Type_sort
1523 | PSort Set -> GSort Glob_ops.glob_Set_sort
1524 | PInt i -> GInt i
1525 | PFloat f -> GFloat f
1526 | PString s -> GString s
1527 | PArray(t,def,ty) ->
1528 let glob_of = glob_of_pat of_extra avoid env sigma in
1529 GArray (None, Array.map glob_of t, glob_of def, glob_of ty)
1530
1531 let extern_constr_pattern_gen of_extra ~flags env sigma pat =
1532 extern true ((constr_some_level,None),([],[]))
1533 (* XXX no vars? *)
1534 { vars = Id.Set.empty; uvars = Evd.universe_binders sigma; flags }
1535 (glob_of_pat of_extra (genset, Id.Set.empty) env sigma pat)
1536
1537 let extern_constr_pattern ~flags env sigma pat =
1538 let of_extra e = Util.Empty.abort e in
1539 extern_constr_pattern_gen of_extra ~flags env sigma pat
1540
1541 let extern_uninstantiated_pattern ~flags env sigma pat =
1542 let of_extra g = GGenarg g in
1543 extern_constr_pattern_gen of_extra ~flags env sigma pat
1544
1545 let extern_rel_context ~(flags:PrintingFlags.t) env sigma sign =
1546 let a = detype_rel_context Detyping.Later ~flags:flags.detype ([],env) sigma sign in
1547 let eenv = extern_env env sigma ~flags:flags.extern in
1548 let a = List.map (extended_glob_local_binder_of_decl) a in
1549 pi3 (extern_local_binder (init_depth eenv.flags) ((constr_some_level,None),([],[])) eenv a)