Debian Codegraph @

plugins/ltac2/tac2quote.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Pp
12 open Names
13 open Util
14 open CAst
15 open Tac2dyn
16 open Tac2expr
17 open Tac2qexpr
18
19 (** Generic arguments *)
20
21 let wit_pattern = Arg.create "pattern"
22 let wit_reference = Arg.create "reference"
23 let wit_module = Arg.create "module"
24 let wit_ident = Arg.create "ident"
25 let wit_constr = Arg.create "constr"
26 let wit_open_constr = Arg.create "open_constr"
27 let wit_preterm = Arg.create "preterm"
28
29 (** Syntactic quoting of expressions. *)
30
31 module Refs = struct
32 let prefix_gen n =
33 MPfile (DirPath.make (List.map Id.of_string [n; "Ltac2"]))
34
35 let control_prefix = prefix_gen "Control"
36 let pattern_prefix = prefix_gen "Pattern"
37 let array_prefix = prefix_gen "Array"
38 let constr_prefix = prefix_gen "Constr"
39 let format_prefix = MPdot (prefix_gen "Message", Id.of_string "Format")
40
41 let kername prefix n = KerName.make prefix (Id.of_string_soft n)
42 let std_core n = kername Tac2env.std_prefix n
43 let rocq_core n = kername Tac2env.rocq_prefix n
44 let control_core n = kername control_prefix n
45 let pattern_core n = kername pattern_prefix n
46
47 let t_unit = rocq_core "unit"
48 let v_unit = Tac2ffi.of_unit ()
49
50 let t_int = rocq_core "int"
51 let t_string = rocq_core "string"
52 let t_array = rocq_core "array"
53 let t_list = rocq_core "list"
54 let t_constr = rocq_core "constr"
55 let t_preterm = rocq_core "preterm"
56 let t_pattern = rocq_core "pattern"
57 let t_ident = rocq_core "ident"
58 let t_option = rocq_core "option"
59 let t_exn = rocq_core "exn"
60 let t_reference = std_core "reference"
61
62 let t_module = kername (prefix_gen "Module") "t"
63
64 let c_nil = rocq_core "[]"
65 let c_cons = rocq_core "::"
66
67 let c_none = rocq_core "None"
68 let c_some = rocq_core "Some"
69
70 let t_bool = rocq_core "bool"
71 let c_true = rocq_core "true"
72 let c_false = rocq_core "false"
73
74 end
75 open Refs
76
77 (** Quoters *)
78
79 let in_constr mods n =
80 let mp = List.fold_left (fun mp mod_ -> MPdot (mp, Id.of_string mod_))
81 constr_prefix
82 mods
83 in
84 kername mp n
85
86 let global_ref ?loc kn =
87 CAst.make ?loc @@ CTacRef (AbsKn (TacConstant kn))
88
89 let constructor ?loc kn args =
90 let cst = CAst.make ?loc @@ CTacCst (AbsKn (Other kn)) in
91 if List.is_empty args then cst
92 else CAst.make ?loc @@ CTacApp (cst, args)
93
94 let std_constructor ?loc name args =
95 constructor ?loc (std_core name) args
96
97 let std_proj ?loc name =
98 AbsKn (std_core name)
99
100 let thunk e =
101 let t_unit = rocq_core "unit" in
102 let loc = e.loc in
103 let ty = CAst.make?loc @@ CTypRef (AbsKn (Other t_unit), []) in
104 let pat = CAst.make ?loc @@ CPatVar (Anonymous) in
105 let pat = CAst.make ?loc @@ CPatCnv (pat, ty) in
106 CAst.make ?loc @@ CTacFun ([pat], e)
107
108 let of_pair f g {loc;v=(e1, e2)} =
109 CAst.make ?loc @@ CTacApp (CAst.make ?loc @@ CTacCst (AbsKn (Tuple 2)), [f e1; g e2])
110
111 let of_tuple ?loc el = match el with
112 | [] ->
113 CAst.make ?loc @@ CTacCst (AbsKn (Tuple 0))
114 | [e] -> e
115 | el ->
116 let len = List.length el in
117 CAst.make ?loc @@ CTacApp (CAst.make ?loc @@ CTacCst (AbsKn (Tuple len)), el)
118
119 let of_string {loc;v=n} =
120 CAst.make ?loc @@ CTacAtm (AtmStr n)
121
122 let of_int {loc;v=n} =
123 CAst.make ?loc @@ CTacAtm (AtmInt n)
124
125 let of_option ?loc f opt = match opt with
126 | None -> constructor ?loc (rocq_core "None") []
127 | Some e -> constructor ?loc (rocq_core "Some") [f e]
128
129 let inj_wit ?loc wit x =
130 CAst.make ?loc @@ CTacExt (wit, x)
131
132 let of_variable {loc;v=id} =
133 let qid = Libnames.qualid_of_ident ?loc id in
134 if Tac2env.is_constructor qid then
135 CErrors.user_err ?loc (str "Invalid identifier" ++ spc () ++ Id.print id ++
136 spc () ++ str "classifying as an Ltac2 constructor")
137 else CAst.make ?loc @@ CTacRef (RelId qid)
138
139 let of_anti f = function
140 | QExpr x -> f x
141 | QAnti id -> of_variable id
142
143 let of_ident {loc;v=id} = inj_wit ?loc wit_ident id
144
145 let quote_constr ?(delimiters=[]) c =
146 let loc = Constrexpr_ops.constr_loc c in
147 List.fold_left (fun c d ->
148 CAst.make ?loc @@ Constrexpr.(CDelimiters(DelimUnboundedScope, Id.to_string d, c)))
149 c
150 delimiters
151
152 let of_constr ?delimiters c =
153 let loc = Constrexpr_ops.constr_loc c in
154 let c = quote_constr ?delimiters c in
155 inj_wit ?loc wit_constr c
156
157 let of_open_constr ?delimiters c =
158 let loc = Constrexpr_ops.constr_loc c in
159 let c = quote_constr ?delimiters c in
160 inj_wit ?loc wit_open_constr c
161
162 let of_preterm ?delimiters c =
163 let loc = Constrexpr_ops.constr_loc c in
164 let c = quote_constr ?delimiters c in
165 inj_wit ?loc wit_preterm c
166
167 let of_open_constr_expected_istype c =
168 let {loc} as c = quote_constr c in
169 let mk e = CAst.make ?loc e in
170 let c =
171 let sc = Notation.current_type_scope_names () in
172 let delim = List.filter_map (fun sc -> Notation.scope_delimiters (Notation.find_scope sc)) sc in
173 (* Not sure if this puts the delimiters in the correct order, usually we have just "type" *)
174 List.fold_left (fun c delim ->
175 mk @@ Constrexpr.CDelimiters (DelimOnlyTmpScope, delim, c))
176 c
177 delim
178 in
179 let c = inj_wit ?loc wit_preterm c in
180 let gref n = global_ref ?loc n in
181 mk @@ CTacApp
182 (gref @@ in_constr ["Pretype"] "pretype",
183 [mk @@ CTacApp (gref @@ in_constr ["Pretype";"Flags"] "open_constr_flags_with_tc_kn",
184 [of_tuple ?loc []]);
185 gref @@ in_constr ["Pretype"] "expected_istype";
186 c])
187
188 let of_bool ?loc b =
189 let c = if b then rocq_core "true" else rocq_core "false" in
190 constructor ?loc c []
191
192 let rec of_list ?loc f = function
193 | [] -> constructor (rocq_core "[]") []
194 | e :: l ->
195 constructor ?loc (rocq_core "::") [f e; of_list ?loc f l]
196
197 let array_literal ?loc a =
198 if CList.is_empty a then global_ref ?loc (kername array_prefix "empty")
199 else
200 let of_list_kn = global_ref ?loc (kername array_prefix "of_list") in
201 CAst.make ?loc @@ CTacApp (of_list_kn, [of_list ?loc (fun x -> x) a])
202
203 let of_qhyp {loc;v=h} = match h with
204 | QAnonHyp n -> std_constructor ?loc "AnonHyp" [of_int n]
205 | QNamedHyp id -> std_constructor ?loc "NamedHyp" [of_ident id]
206
207 let of_bindings {loc;v=b} = match b with
208 | QNoBindings ->
209 std_constructor ?loc "NoBindings" []
210 | QImplicitBindings tl ->
211 std_constructor ?loc "ImplicitBindings" [of_list ?loc of_open_constr tl]
212 | QExplicitBindings tl ->
213 let map e = of_pair (fun q -> of_anti of_qhyp q) of_open_constr e in
214 std_constructor ?loc "ExplicitBindings" [of_list ?loc map tl]
215
216 let of_constr_with_bindings c = of_pair of_open_constr of_bindings c
217
218 let rec of_intro_pattern {loc;v=pat} = match pat with
219 | QIntroForthcoming b ->
220 std_constructor ?loc "IntroForthcoming" [of_bool b]
221 | QIntroNaming iname ->
222 std_constructor ?loc "IntroNaming" [of_intro_pattern_naming iname]
223 | QIntroAction iact ->
224 std_constructor ?loc "IntroAction" [of_intro_pattern_action iact]
225
226 and of_intro_pattern_naming {loc;v=pat} = match pat with
227 | QIntroIdentifier id ->
228 std_constructor ?loc "IntroIdentifier" [of_anti of_ident id]
229 | QIntroFresh id ->
230 std_constructor ?loc "IntroFresh" [of_anti of_ident id]
231 | QIntroAnonymous ->
232 std_constructor ?loc "IntroAnonymous" []
233
234 and of_intro_pattern_action {loc;v=pat} = match pat with
235 | QIntroWildcard ->
236 std_constructor ?loc "IntroWildcard" []
237 | QIntroOrAndPattern pat ->
238 std_constructor ?loc "IntroOrAndPattern" [of_or_and_intro_pattern pat]
239 | QIntroInjection il ->
240 std_constructor ?loc "IntroInjection" [of_intro_patterns il]
241 | QIntroApplyOn (c, i) ->
242 std_constructor ?loc "IntroApplyOn" [thunk @@ of_open_constr c; of_intro_pattern i]
243 | QIntroRewrite b ->
244 std_constructor ?loc "IntroRewrite" [of_bool ?loc b]
245
246 and of_or_and_intro_pattern {loc;v=pat} = match pat with
247 | QIntroOrPattern ill ->
248 std_constructor ?loc "IntroOrPattern" [of_list ?loc of_intro_patterns ill]
249 | QIntroAndPattern il ->
250 std_constructor ?loc "IntroAndPattern" [of_intro_patterns il]
251
252 and of_intro_patterns {loc;v=l} =
253 of_list ?loc of_intro_pattern l
254
255 let of_hyp_location_flag ?loc = function
256 | Locus.InHyp -> std_constructor ?loc "InHyp" []
257 | Locus.InHypTypeOnly -> std_constructor ?loc "InHypTypeOnly" []
258 | Locus.InHypValueOnly -> std_constructor ?loc "InHypValueOnly" []
259
260 let of_occurrences {loc;v=occ} = match occ with
261 | QAllOccurrences -> std_constructor ?loc "AllOccurrences" []
262 | QAllOccurrencesBut occs ->
263 let map occ = of_anti of_int occ in
264 let occs = of_list ?loc map occs in
265 std_constructor ?loc "AllOccurrencesBut" [occs]
266 | QNoOccurrences -> std_constructor ?loc "NoOccurrences" []
267 | QOnlyOccurrences occs ->
268 let map occ = of_anti of_int occ in
269 let occs = of_list ?loc map occs in
270 std_constructor ?loc "OnlyOccurrences" [occs]
271
272 let of_hyp_location ?loc ((occs, id), flag) =
273 of_tuple ?loc [
274 of_anti of_ident id;
275 of_occurrences occs;
276 of_hyp_location_flag ?loc flag;
277 ]
278
279 let of_clause {loc;v=cl} =
280 let hyps = of_option ?loc (fun l -> of_list ?loc of_hyp_location l) cl.q_onhyps in
281 let concl = of_occurrences cl.q_concl_occs in
282 CAst.make ?loc @@ CTacRec (None, [
283 std_proj "on_hyps", hyps;
284 std_proj "on_concl", concl;
285 ])
286
287 let of_destruction_arg {loc;v=arg} = match arg with
288 | QElimOnConstr c ->
289 let arg = thunk (of_constr_with_bindings c) in
290 std_constructor ?loc "ElimOnConstr" [arg]
291 | QElimOnIdent id -> std_constructor ?loc "ElimOnIdent" [of_ident id]
292 | QElimOnAnonHyp n -> std_constructor ?loc "ElimOnAnonHyp" [of_int n]
293
294 let of_induction_clause {loc;v=cl} =
295 let arg = of_destruction_arg cl.indcl_arg in
296 let eqn = of_option ?loc of_intro_pattern_naming cl.indcl_eqn in
297 let as_ = of_option ?loc of_or_and_intro_pattern cl.indcl_as in
298 let in_ = of_option ?loc of_clause cl.indcl_in in
299 CAst.make ?loc @@ CTacRec (None, [
300 std_proj "indcl_arg", arg;
301 std_proj "indcl_eqn", eqn;
302 std_proj "indcl_as", as_;
303 std_proj "indcl_in", in_;
304 ])
305
306 let check_pattern_id ?loc id =
307 if Tac2env.is_constructor (Libnames.qualid_of_ident id) then
308 CErrors.user_err ?loc (str "Invalid pattern binding name " ++ Id.print id)
309
310 let pattern_vars pat =
311 let rec aux () accu pat = match pat.CAst.v with
312 | Constrexpr.CPatVar id
313 | Constrexpr.CEvar ({CAst.v=id}, []) ->
314 let loc = pat.CAst.loc in
315 let () = check_pattern_id ?loc id in
316 Id.Map.add id loc accu
317 | _ ->
318 Constrexpr_ops.fold_constr_expr_with_binders (fun _ () -> ()) aux () accu pat
319 in
320 aux () Id.Map.empty pat
321
322 let abstract_vars loc ?typ vars tac =
323 let get_name na = match na.CAst.v with
324 | Name id -> Some (CAst.make ?loc:na.CAst.loc id)
325 | Anonymous -> None
326 in
327 let def = List.find_map get_name vars in
328 let na, tac = match def with
329 | None -> (Anonymous, tac)
330 | Some id0 ->
331 (* Trick: in order not to shadow a variable nor to choose an arbitrary
332 name, we reuse one which is going to be shadowed by the matched
333 variables anyways. *)
334 let build_bindings (n, accu) { CAst.loc; CAst.v = na } = match na with
335 | Anonymous -> (n + 1, accu)
336 | Name _ ->
337 let get = global_ref ?loc (kername array_prefix "get") in
338 let args = [of_variable id0; of_int CAst.(make ?loc n)] in
339 let e = CAst.make ?loc @@ CTacApp (get, args) in
340 let accu = (CAst.make ?loc @@ CPatVar na, e) :: accu in
341 (n + 1, accu)
342 in
343 let (_, bnd) = List.fold_left build_bindings (0, []) vars in
344 let tac = CAst.make ?loc @@ CTacLet (false, bnd, tac) in
345 (Name id0.CAst.v, tac)
346 in
347 let pat = CAst.make ?loc @@ CPatVar na in
348 let pat = match typ with
349 | None -> pat
350 | Some typ ->
351 let t_array = rocq_core "array" in
352 let typ = CAst.make ?loc @@ CTypRef (AbsKn (Other t_array), [typ]) in
353 CAst.make ?loc @@ CPatCnv (pat, typ)
354 in
355 CAst.make ?loc @@ CTacFun ([pat], tac)
356
357 let of_pattern p =
358 inj_wit ?loc:p.CAst.loc wit_pattern p
359
360 let of_conversion {loc;v=c} = match c with
361 | QConvert c ->
362 let pat = of_option ?loc of_pattern None in
363 let c = CAst.make ?loc @@ CTacFun ([CAst.make ?loc @@ CPatVar Anonymous], of_constr c) in
364 of_tuple ?loc [pat; c]
365 | QConvertWith (pat, c) ->
366 let vars = pattern_vars pat in
367 let pat = of_option ?loc of_pattern (Some pat) in
368 let c = of_constr c in
369 (* Order is critical here *)
370 let vars = List.map (fun (id, loc) -> CAst.make ?loc (Name id)) (Id.Map.bindings vars) in
371 let c = abstract_vars loc vars c in
372 of_tuple [pat; c]
373
374 let of_repeat {loc;v=r} = match r with
375 | QPrecisely n -> std_constructor ?loc "Precisely" [of_int n]
376 | QUpTo n -> std_constructor ?loc "UpTo" [of_int n]
377 | QRepeatStar -> std_constructor ?loc "RepeatStar" []
378 | QRepeatPlus -> std_constructor ?loc "RepeatPlus" []
379
380 let of_orient {loc;v=b} =
381 let helper b =
382 if b then std_constructor ?loc "LTR" []
383 else std_constructor ?loc "RTL" []
384 in
385 of_option ?loc helper b
386
387 let of_rewriting {loc;v=rew} =
388 let orient = of_orient rew.rew_orient in
389 let repeat = of_repeat rew.rew_repeat in
390 let equatn = thunk (of_constr_with_bindings rew.rew_equatn) in
391 CAst.make ?loc @@ CTacRec (None, [
392 std_proj "rew_orient", orient;
393 std_proj "rew_repeat", repeat;
394 std_proj "rew_equatn", equatn;
395 ])
396
397 let of_hyp ?loc id =
398 let hyp = global_ref ?loc (control_core "hyp") in
399 CAst.make ?loc @@ CTacApp (hyp, [of_ident id])
400
401 let of_exact_hyp ?loc id =
402 let refine = global_ref ?loc (control_core "refine") in
403 CAst.make ?loc @@ CTacApp (refine, [thunk (of_hyp ?loc id)])
404
405 let of_exact_var ?loc id =
406 let refine = global_ref ?loc (control_core "refine") in
407 CAst.make ?loc @@ CTacApp (refine, [thunk (of_variable id)])
408
409 let of_dispatch tacs =
410 let loc = tacs.loc in
411 let default = function
412 | Some e -> thunk e
413 | None -> thunk (CAst.make ?loc @@ CTacCst (AbsKn (Tuple 0)))
414 in
415 let map e = of_pair default (fun l -> of_list ?loc default l) (CAst.make ?loc e) in
416 of_pair (fun l -> of_list ?loc default l) (fun r -> of_option ?loc map r) tacs
417
418 let make_red_flag l =
419 let open Genredexpr in
420 let rec add_flag red = function
421 | [] -> red
422 | {v=flag} :: lf ->
423 let red = match flag with
424 | QHead -> { red with rStrength = Head }
425 | QBeta -> { red with rBeta = true }
426 | QMatch -> { red with rMatch = true }
427 | QFix -> { red with rFix = true }
428 | QCofix -> { red with rCofix = true }
429 | QZeta -> { red with rZeta = true }
430 | QConst {loc;v=l} ->
431 if red.rDelta then
432 CErrors.user_err ?loc Pp.(str
433 "Cannot set both constants to unfold and constants not to unfold");
434 { red with rConst = red.rConst @ l }
435 | QDeltaBut {loc;v=l} ->
436 if red.rConst <> [] && not red.rDelta then
437 CErrors.user_err ?loc Pp.(str
438 "Cannot set both constants to unfold and constants not to unfold");
439 { red with rConst = red.rConst @ l; rDelta = true }
440 | QIota ->
441 { red with rMatch = true; rFix = true; rCofix = true }
442 in
443 add_flag red lf
444 in
445 add_flag
446 {rBeta = false; rMatch = false; rFix = false; rCofix = false;
447 rZeta = false; rDelta = false; rConst = []; rStrength = Norm; }
448 l
449
450 let of_reference r =
451 let of_ref ref =
452 inj_wit ?loc:ref.loc wit_reference ref
453 in
454 of_anti of_ref r
455
456 let of_module qid = inj_wit ?loc:qid.loc wit_module qid
457
458 let of_strength ?loc s =
459 let s = let open Genredexpr in match s with
460 | Norm -> std_core "Norm"
461 | Head -> std_core "Head"
462 in
463 constructor ?loc s []
464
465 let of_strategy_flag {loc;v=flag} =
466 let open Genredexpr in
467 let flag = make_red_flag flag in
468 CAst.make ?loc @@ CTacRec (None, [
469 std_proj "rStrength", of_strength ?loc flag.rStrength;
470 std_proj "rBeta", of_bool ?loc flag.rBeta;
471 std_proj "rMatch", of_bool ?loc flag.rMatch;
472 std_proj "rFix", of_bool ?loc flag.rFix;
473 std_proj "rCofix", of_bool ?loc flag.rCofix;
474 std_proj "rZeta", of_bool ?loc flag.rZeta;
475 std_proj "rDelta", of_bool ?loc flag.rDelta;
476 std_proj "rConst", of_list ?loc of_reference flag.rConst;
477 ])
478
479 let of_hintdb {loc;v=hdb} = match hdb with
480 | QHintAll -> of_option ?loc (fun l -> of_list (fun id -> of_anti of_ident id) l) None
481 | QHintDbs ids -> of_option ?loc (fun l -> of_list (fun id -> of_anti of_ident id) l) (Some ids)
482
483 let extract_name ?loc oid = match oid with
484 | None -> Anonymous
485 | Some id ->
486 let () = check_pattern_id ?loc id in
487 Name id
488
489 (** For every branch in the matching, generate a corresponding term of type
490 [(match_kind * pattern * (context -> constr array -> 'a))]
491 where the function binds the names from the pattern to the contents of the
492 constr array. *)
493 let of_constr_matching {loc;v=m} =
494 let map {loc;v=({loc=ploc;v=pat}, tac)} =
495 let (knd, pat, na) = match pat with
496 | QConstrMatchPattern pat ->
497 let knd = constructor ?loc (pattern_core "MatchPattern") [] in
498 (knd, pat, Anonymous)
499 | QConstrMatchContext (id, pat) ->
500 let na = extract_name ?loc id in
501 let knd = constructor ?loc (pattern_core "MatchContext") [] in
502 (knd, pat, na)
503 in
504 let vars = pattern_vars pat in
505 (* Order of elements is crucial here! *)
506 let vars = Id.Map.bindings vars in
507 let vars = List.map (fun (id, loc) -> CAst.make ?loc (Name id)) vars in
508 (* Annotate the bound array variable with constr type *)
509 let typ =
510 let t_constr = rocq_core "constr" in
511 CAst.make ?loc @@ CTypRef (AbsKn (Other t_constr), [])
512 in
513 let e = abstract_vars loc ~typ vars tac in
514 let e = CAst.make ?loc @@ CTacFun ([CAst.make ?loc @@ CPatVar na], e) in
515 let pat = inj_wit ?loc:ploc wit_pattern pat in
516 of_tuple [knd; pat; e]
517 in
518 let e = of_list ?loc map m in
519 e
520
521 (** From the patterns and the body of the branch, generate:
522 - a goal pattern: (constr_match list * constr_match)
523 - a branch function (ident array -> context array -> constr array -> context -> 'a)
524 *)
525 let of_goal_matching {loc;v=gm} =
526 let mk_pat {loc;v=p} = match p with
527 | QConstrMatchPattern pat ->
528 let knd = constructor ?loc (pattern_core "MatchPattern") [] in
529 (CAst.make ?loc Anonymous, pat, knd)
530 | QConstrMatchContext (id, pat) ->
531 let na = extract_name ?loc id in
532 let knd = constructor ?loc (pattern_core "MatchContext") [] in
533 (CAst.make ?loc na, pat, knd)
534 in
535 let mk_gpat {loc;v=p} =
536 let concl_pat = p.q_goal_match_concl in
537 let hyps_pats = p.q_goal_match_hyps in
538 let (concl_ctx, concl_pat, concl_knd) = mk_pat concl_pat in
539 let vars = pattern_vars concl_pat in
540 let map accu (na, bod, pat) = match bod with
541 | None ->
542 let (ctx, pat, knd) = mk_pat pat in
543 let vars = pattern_vars pat in
544 (Id.Map.fold Id.Map.add vars accu, (na, None, (ctx, knd, pat)))
545 | Some bod ->
546 let bctx, bpat, bknd = mk_pat bod in
547 let (ctx, pat, knd) = mk_pat pat in
548 let bvars = pattern_vars bpat in
549 let vars = pattern_vars pat in
550 let accu = Id.Map.fold Id.Map.add bvars accu in
551 let accu = Id.Map.fold Id.Map.add vars accu in
552 (accu, (na, Some ((bctx, bknd, bpat)), (ctx, knd, pat)))
553 in
554 let (vars, hyps_pats) = List.fold_left_map map vars hyps_pats in
555 let concl = of_tuple [concl_knd; of_pattern concl_pat] in
556 let of_hyp_pat (_, bpat, (_, knd, pat)) =
557 let bpat = Option.map (fun (_, a, b) -> (a, b)) bpat in
558 let of_mpat (a, b) = of_tuple [a; of_pattern b] in
559 of_tuple [of_option of_mpat bpat; of_mpat (knd, pat)]
560 in
561 let r = of_tuple [
562 of_list ?loc of_hyp_pat hyps_pats;
563 concl]
564 in
565 let hyps = List.map (fun (na, _, _) -> na) hyps_pats in
566 let hbctx = CList.filter_map (fun (_, b, _) -> Option.map pi1 b) hyps_pats in
567 let hctx = CList.map (fun (_, _, (na, _, _)) -> na) hyps_pats in
568 (* Order of elements is crucial here! *)
569 let vars = Id.Map.bindings vars in
570 let subst = List.map (fun (id, loc) -> CAst.make ?loc (Name id)) vars in
571 (r, hyps, hbctx, hctx, subst, concl_ctx)
572 in
573 let map {loc;v=(pat, tac)} =
574 let (pat, hyps, hbctx, hctx, subst, cctx) = mk_gpat pat in
575 let tac = CAst.make ?loc @@ CTacFun ([CAst.make ?loc @@ CPatVar cctx.CAst.v], tac) in
576 let tac = abstract_vars loc subst tac in
577 let tac = abstract_vars loc hctx tac in
578 let tac = abstract_vars loc hbctx tac in
579 let tac = abstract_vars loc hyps tac in
580 of_tuple ?loc [pat; tac]
581 in
582 let e = of_list ?loc map gm in
583 let tykn = pattern_core "goal_matching" in
584 let ty = CAst.make ?loc (CTypRef (AbsKn (Other tykn),[CAst.make ?loc (CTypVar Anonymous)])) in
585 CAst.make ?loc (CTacCnv (e, ty))
586
587 let of_move_location {loc;v=mv} = match mv with
588 | QMoveAfter id -> std_constructor ?loc "MoveAfter" [of_anti of_ident id]
589 | QMoveBefore id -> std_constructor ?loc "MoveBefore" [of_anti of_ident id]
590 | QMoveFirst -> std_constructor ?loc "MoveFirst" []
591 | QMoveLast -> std_constructor ?loc "MoveLast" []
592
593 let of_pose p =
594 of_pair (fun id -> of_option (fun id -> of_anti of_ident id) id) of_open_constr p
595
596 let of_assertion {loc;v=ast} = match ast with
597 | QAssertType (ipat, c, tac) ->
598 let ipat = of_option of_intro_pattern ipat in
599 let c = of_open_constr_expected_istype c in
600 let tac = of_option thunk tac in
601 std_constructor ?loc "AssertType" [ipat; c; tac]
602 | QAssertValue (id, c) ->
603 let id = of_anti of_ident id in
604 let c = of_constr c in
605 std_constructor ?loc "AssertValue" [id; c]
606
607 open Tac2types
608
609 exception InvalidFormat
610
611 let parse_format (s : string) : format list =
612 let len = String.length s in
613 let buf = Buffer.create len in
614 let rec parse i accu =
615 if len <= i then accu
616 else match s.[i] with
617 | '%' -> parse_argument (i + 1) accu
618 | _ ->
619 let i' = parse_literal i in
620 if Int.equal i i' then parse i' accu
621 else
622 let lit = Buffer.contents buf in
623 let () = Buffer.clear buf in
624 parse i' (FmtLiteral lit :: accu)
625 and parse_literal i =
626 if len <= i then i
627 else match s.[i] with
628 | '%' -> i
629 | c ->
630 let () = Buffer.add_char buf c in
631 parse_literal (i + 1)
632 and parse_argument i accu =
633 if len <= i then raise InvalidFormat
634 else match s.[i] with
635 | '%' -> parse (i + 1) (FmtLiteral "%" :: accu)
636 | 's' -> parse (i + 1) (FmtString :: accu)
637 | 'i' -> parse (i + 1) (FmtInt :: accu)
638 | 'I' -> parse (i + 1) (FmtIdent :: accu)
639 | 'm' -> parse (i + 1) (FmtMessage :: accu)
640 | 't' -> parse (i + 1) (FmtConstr :: accu)
641 | 'a' -> parse (i + 1) (FmtAlpha :: accu)
642 | 'A' -> parse (i + 1) (FmtAlpha0 :: accu)
643 | _ -> raise InvalidFormat
644 in
645 parse 0 []
646
647 let of_format accu = function
648 | FmtString ->
649 CAst.make @@ CTacApp (global_ref (kername format_prefix "string"), [accu])
650 | FmtInt ->
651 CAst.make @@ CTacApp (global_ref (kername format_prefix "int"), [accu])
652 | FmtConstr ->
653 CAst.make @@ CTacApp (global_ref (kername format_prefix "constr"), [accu])
654 | FmtIdent ->
655 CAst.make @@ CTacApp (global_ref (kername format_prefix "ident"), [accu])
656 | FmtLiteral lit ->
657 let s = of_string (CAst.make lit) in
658 CAst.make @@ CTacApp (global_ref (kername format_prefix "literal"), [s; accu])
659 | FmtAlpha ->
660 CAst.make @@ CTacApp (global_ref (kername format_prefix "alpha"), [accu])
661 | FmtAlpha0 ->
662 CAst.make @@ CTacApp (global_ref (kername format_prefix "alpha0"), [accu])
663 | FmtMessage ->
664 CAst.make @@ CTacApp (global_ref (kername format_prefix "message"), [accu])
665
666 let of_format { v = fmt; loc } =
667 let fmt =
668 try parse_format fmt
669 with InvalidFormat ->
670 CErrors.user_err ?loc (str "Invalid format")
671 in
672 let stop = global_ref (kername format_prefix "stop") in
673 List.fold_left of_format stop fmt