Debian Codegraph @

plugins/ltac2_ltac1/tac2core_ltac1.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 Pp
13 open Names
14 open Genarg
15 open Ltac2_plugin
16 open Tac2val
17 open Tac2ffi
18 open Tac2env
19 open Tac2expr
20 open Proofview.Notations
21 open Tac2externals
22
23 let ltac2_ltac1_plugin = "rocq-runtime.plugins.ltac2_ltac1"
24
25 let pname ?(plugin=ltac2_ltac1_plugin) s = { mltac_plugin = plugin; mltac_tactic = s }
26
27 let define ?plugin s = define (pname ?plugin s)
28
29 let return x = Proofview.tclUNIT x
30
31 (** Ltac1 in Ltac2 *)
32
33 let ltac1 = Tac2quote_ltac1.ltac1
34
35 let () =
36 define "ltac1_ref" (list ident @-> ret ltac1) @@ fun ids ->
37 let open Ltac_plugin in
38 let r =
39 match ids with
40 | [] -> raise Not_found
41 | _ :: _ as ids ->
42 let (id, path) = List.sep_last ids in
43 let path = DirPath.make (List.rev path) in
44 let fp = Libnames.make_path path id in
45 if Tacenv.exists_tactic fp then
46 List.hd (Tacenv.locate_extended_all_tactic (Libnames.qualid_of_path fp))
47 else raise Not_found
48 in
49 Tacinterp.Value.of_closure (Tacinterp.default_ist ()) (Tacenv.interp_ltac r)
50
51 let () =
52 define "ltac1_run" (ltac1 @-> tac unit) @@ fun v ->
53 let open Ltac_plugin in
54 Tacinterp.tactic_of_value (Tacinterp.default_ist ()) v
55
56 let () =
57 define "ltac1_apply" (ltac1 @-> list ltac1 @-> closure @-> tac unit) @@ fun f args k ->
58 let open Ltac_plugin in
59 let open Tacexpr in
60 let open Locus in
61 let k ret =
62 Proofview.tclIGNORE (Tac2val.apply k [Tac2ffi.repr_of ltac1 ret])
63 in
64 let fold arg (i, vars, lfun) =
65 let id = Id.of_string ("x" ^ string_of_int i) in
66 let x = Reference (ArgVar CAst.(make id)) in
67 (succ i, x :: vars, Id.Map.add id arg lfun)
68 in
69 let (_, args, lfun) = List.fold_right fold args (0, [], Id.Map.empty) in
70 let lfun = Id.Map.add (Id.of_string "F") f lfun in
71 let ist = { (Tacinterp.default_ist ()) with Tacinterp.lfun = lfun; } in
72 let tac = CAst.make @@ TacArg (TacCall (CAst.make (ArgVar CAst.(make @@ Id.of_string "F"),args))) in
73 Tacinterp.val_interp ist tac k
74
75 let () =
76 define "ltac1_of_int" (int @-> ret ltac1)
77 Ltac_plugin.Tacinterp.Value.of_int
78
79 let () =
80 define "ltac1_to_int" (ltac1 @-> ret (option int))
81 Ltac_plugin.Tacinterp.Value.to_int
82
83 let () =
84 define "ltac1_of_constr" (constr @-> ret ltac1)
85 Ltac_plugin.Tacinterp.Value.of_constr
86
87 let () =
88 define "ltac1_to_constr" (ltac1 @-> ret (option constr))
89 Ltac_plugin.Tacinterp.Value.to_constr
90
91 let () =
92 define "ltac1_of_preterm" (preterm @-> ret ltac1)
93 Ltac_plugin.Taccoerce.Value.of_uconstr
94
95 let () =
96 define "ltac1_to_preterm" (ltac1 @-> ret (option preterm))
97 Ltac_plugin.Taccoerce.Value.to_uconstr
98
99 let () =
100 define "ltac1_of_ident" (ident @-> ret ltac1)
101 Ltac_plugin.Taccoerce.Value.of_ident
102
103 let () =
104 define "ltac1_to_ident" (ltac1 @-> ret (option ident))
105 Ltac_plugin.Taccoerce.Value.to_ident
106
107 let () =
108 define "ltac1_of_list" (list ltac1 @-> ret ltac1) @@ fun l ->
109 Geninterp.Val.(inject (Base typ_list) l)
110
111 let () =
112 define "ltac1_to_list" (ltac1 @-> ret (option (list ltac1)))
113 Ltac_plugin.Tacinterp.Value.to_list
114
115 let () =
116 define "ltac1_tag_name" (ltac1 @-> ret string) @@ fun (Dyn (tag,_)) ->
117 Geninterp.Val.repr tag
118
119 let gtypref kn = GTypRef (Other kn, [])
120
121 open Tac2quote.Refs
122
123 let core_prefix path n = KerName.make path (Id.of_string_soft n)
124 let ltac1_core n = core_prefix Tac2env.ltac1_prefix n
125 let t_ltac1 = ltac1_core "t"
126 let ltac1_lambda = ltac1_core "lambda"
127
128 let () =
129 let intern ist (ids, tac) =
130 let map { CAst.v = id } = id in
131 let ids = List.map map ids in
132 (* Prevent inner calls to Ltac2 values *)
133 let extra = Tac2intern.drop_ltac2_env ist.Genintern.extra in
134 let ltacvars = List.fold_right Id.Set.add ids ist.Genintern.ltacvars in
135 let ist = { ist with Genintern.extra; ltacvars } in
136 let _, tac = Genintern.intern Ltac_plugin.Tacarg.wit_tactic ist tac in
137 let fold ty _ = GTypArrow (gtypref t_ltac1, ty) in
138 let ty = List.fold_left fold (gtypref t_unit) ids in
139 GlbVal (ids, tac), ty
140 in
141 let interp _ (ids, tac) =
142 let clos args =
143 let add lfun id v =
144 let v = Tac2ffi.repr_to ltac1 v in
145 Id.Map.add id v lfun
146 in
147 let lfun = List.fold_left2 add Id.Map.empty ids args in
148 let ist = { env_ist = Id.Map.empty } in
149 let lfun = Tac2interp.set_env ist lfun in
150 let ist = Ltac_plugin.Tacinterp.default_ist () in
151 let ist = { ist with Geninterp.lfun = lfun } in
152 let tac = (Ltac_plugin.Tacinterp.eval_tactic_ist ist tac : unit Proofview.tactic) in
153 tac >>= fun () ->
154 return v_unit
155 in
156 let len = List.length ids in
157 if Int.equal len 0 then
158 clos []
159 else
160 return (Tac2ffi.of_closure (Tac2val.abstract len clos))
161 in
162 let subst s (ids, tac) = (ids, Gensubst.substitute Ltac_plugin.Tacarg.wit_tactic s tac) in
163 let print env sigma (ids, tac) =
164 let ids =
165 if List.is_empty ids then mt ()
166 else pr_sequence Id.print ids ++ spc () ++ str "|-" ++ spc ()
167 in
168 str "ltac1:(" ++ ids ++ Ltac_plugin.Pptactic.pr_glob_tactic env tac ++ str ")"
169 in
170 let raw_print env sigma (ids, tac) =
171 let ids =
172 if List.is_empty ids then mt ()
173 else pr_sequence (fun id -> Id.print id.CAst.v) ids ++ spc () ++ str "|-" ++ spc ()
174 in
175 str "ltac1:(" ++ ids ++ Ltac_plugin.Pptactic.pr_raw_tactic env sigma tac ++ str ")"
176 in
177 let obj = {
178 ml_intern = intern;
179 ml_subst = subst;
180 ml_interp = interp;
181 ml_print = print;
182 ml_raw_print = raw_print;
183 } in
184 define_ml_object Tac2quote_ltac1.wit_ltac1 obj
185
186 let () =
187 let open Ltac_plugin in
188 let intern ist (ids, tac) =
189 let map { CAst.v = id } = id in
190 let ids = List.map map ids in
191 (* Prevent inner calls to Ltac2 values *)
192 let extra = Tac2intern.drop_ltac2_env ist.Genintern.extra in
193 let ltacvars = List.fold_right Id.Set.add ids ist.Genintern.ltacvars in
194 let ist = { ist with Genintern.extra; ltacvars } in
195 let _, tac = Genintern.intern Ltac_plugin.Tacarg.wit_tactic ist tac in
196 let fold ty _ = GTypArrow (gtypref t_ltac1, ty) in
197 let ty = List.fold_left fold (gtypref t_ltac1) ids in
198 GlbVal (ids, tac), ty
199 in
200 let interp _ (ids, tac) =
201 let clos args =
202 let add lfun id v =
203 let v = Tac2ffi.repr_to ltac1 v in
204 Id.Map.add id v lfun
205 in
206 let lfun = List.fold_left2 add Id.Map.empty ids args in
207 let ist = { env_ist = Id.Map.empty } in
208 let lfun = Tac2interp.set_env ist lfun in
209 let ist = Ltac_plugin.Tacinterp.default_ist () in
210 let ist = { ist with Geninterp.lfun = lfun } in
211 return (Tac2ffi.repr_of ltac1 (Tacinterp.Value.of_closure ist tac))
212 in
213 let len = List.length ids in
214 if Int.equal len 0 then
215 clos []
216 else
217 return (Tac2ffi.of_closure (Tac2val.abstract len clos))
218 in
219 let subst s (ids, tac) = (ids, Gensubst.substitute Tacarg.wit_tactic s tac) in
220 let print env sigma (ids, tac) =
221 let ids =
222 if List.is_empty ids then mt ()
223 else pr_sequence Id.print ids ++ str " |- "
224 in
225 str "ltac1val:(" ++ ids++ Ltac_plugin.Pptactic.pr_glob_tactic env tac ++ str ")"
226 in
227 let raw_print env sigma (ids, tac) =
228 let ids =
229 if List.is_empty ids then mt ()
230 else pr_sequence (fun id -> Id.print id.CAst.v) ids ++ spc () ++ str "|-" ++ spc ()
231 in
232 str "ltac1val:(" ++ ids ++ Ltac_plugin.Pptactic.pr_raw_tactic env sigma tac ++ str ")"
233 in
234 let obj = {
235 ml_intern = intern;
236 ml_subst = subst;
237 ml_interp = interp;
238 ml_print = print;
239 ml_raw_print = raw_print;
240 } in
241 define_ml_object Tac2quote_ltac1.wit_ltac1val obj
242
243 (** Ltac2 in Ltac1 *)
244
245 (** Embedding Ltac2 closures of type [Ltac1.t -> Ltac1.t] inside Ltac1. There is
246 no relevant data because arguments are passed by conventional names. *)
247 let wit_ltac2_val : (Util.Empty.t, unit, Util.Empty.t) genarg_type =
248 Genarg.make0 "ltac2:Ltac1.lambda"
249
250 (** Ltac2 quotations in Ltac1 code *)
251 let wit_ltac2in1 : (Id.t CAst.t list * raw_tacexpr, Id.t list * glb_tacexpr, Util.Empty.t) genarg_type
252 = Genarg.make0 "ltac2in1"
253
254 (** Ltac2 quotations in Ltac1 returning Ltac1 values.
255 When ids are bound interning turns them into Ltac1.lambda. *)
256 let wit_ltac2in1_val : (Id.t CAst.t list * raw_tacexpr, glb_tacexpr, Util.Empty.t) genarg_type
257 = Genarg.make0 "ltac2in1val"
258
259 let pr_ltac2in1_ids ids =
260 if List.is_empty ids then mt ()
261 else hov 0 (pr_sequence Id.print ids ++ str " |- ")
262
263 let () =
264 let pr_raw (ids, e) = Genprint.PrinterBasic (fun _env _sigma ->
265 let ids = List.map (fun v -> v.CAst.v) ids in
266 pr_ltac2in1_ids ids ++ Tac2print.pr_rawexpr_gen E5 ~avoid:(Id.Set.of_list ids) e)
267 in
268 let pr_glb (ids, e) =
269 Genprint.PrinterBasic Pp.(fun _env _sigma ->
270 pr_ltac2in1_ids ids ++ Tac2print.pr_glbexpr ~avoid:(Id.Set.of_list ids) e)
271 in
272 Genprint.register_noval_print0 wit_ltac2in1 pr_raw pr_glb
273
274 let () =
275 let pr_raw (ids, e) = Genprint.PrinterBasic (fun _env _sigma ->
276 let ids = List.map (fun v -> v.CAst.v) ids in
277 pr_ltac2in1_ids ids ++ Tac2print.pr_rawexpr_gen E5 ~avoid:(Id.Set.of_list ids) e)
278 in
279 let pr_glb e =
280 Genprint.PrinterBasic (fun _env _sigma ->
281 Tac2print.pr_glbexpr ~avoid:Id.Set.empty e)
282 in
283 Genprint.register_noval_print0 wit_ltac2in1_val pr_raw pr_glb
284
285 let () =
286 let open Tac2typing_env in
287 let intern ist (ids, tac) =
288 let t_ltac1 = monomorphic (GTypRef (Other t_ltac1, [])) in
289 let bnd = List.map (fun id -> Name id.CAst.v, t_ltac1) ids in
290 let tac = Tac2intern.genintern_warn_not_unit ist bnd tac in
291 (ist, (List.map (fun id -> id.CAst.v) ids, tac))
292 in
293 Genintern.register_intern0 wit_ltac2in1 intern
294
295 let () =
296 let add_lambda id tac =
297 let pat = CAst.make ?loc:id.CAst.loc (CPatVar (Name id.v)) in
298 let loc = tac.CAst.loc in
299 let mk v = CAst.make ?loc v in
300 let lam = mk @@ CTacFun ([pat], tac) in
301 mk @@ CTacApp (mk @@ CTacRef (AbsKn (TacConstant ltac1_lambda)), [lam])
302 in
303 let intern ist (bnd,tac) =
304 let tac = List.fold_right add_lambda bnd tac in
305 let tac = Tac2intern.genintern ist [] (GTypRef (Other t_ltac1, [])) tac in
306 ist, tac
307 in
308 Genintern.register_intern0 wit_ltac2in1_val intern
309
310 let () = Gensubst.register_subst0 wit_ltac2in1 (fun s (ids, e) -> ids, Tac2intern.subst_expr s e)
311 let () = Gensubst.register_subst0 wit_ltac2in1_val Tac2intern.subst_expr
312
313 let () =
314 let create name wit =
315 let e = Tac2entries.Pltac.tac2expr_in_env in
316 let inject (loc, v) = Ltac_plugin.Tacexpr.TacGeneric (Some name, in_gen (rawwit wit) v) in
317 Ltac_plugin.Tacentries.create_ltac_quotation ~plugin:ltac2_ltac1_plugin name inject (e, None)
318 in
319 let () = create "ltac2" wit_ltac2in1 in
320 let () = create "ltac2val" wit_ltac2in1_val in
321 ()
322
323 (* Ltac1 runtime representation of Ltac2 closures. *)
324 let typ_ltac2 : valexpr Geninterp.Val.typ =
325 Geninterp.Val.create "ltac2:ltac2_eval"
326
327 let () = Genprint.register_val_print0 typ_ltac2 (fun v ->
328 TopPrinterBasic (fun () -> Pp.str "<ltac2 closure>"))
329
330 let cast_typ (type a) (tag : a Geninterp.Val.typ) (v : Geninterp.Val.t) : a =
331 let Geninterp.Val.Dyn (tag', v) = v in
332 match Geninterp.Val.eq tag tag' with
333 | None -> assert false
334 | Some Refl -> v
335
336 let () =
337 let open Ltac_plugin in
338 (* This is a hack similar to Tacentries.ml_val_tactic_extend *)
339 let intern_fun _ e = Empty.abort e in
340 let subst_fun s v = v in
341 let () = Genintern.register_intern0 wit_ltac2_val intern_fun in
342 let () = Gensubst.register_subst0 wit_ltac2_val subst_fun in
343 (* These are bound names and not relevant *)
344 let tac_id = Id.of_string "F" in
345 let arg_id = Id.of_string "X" in
346 let interp_fun ist () =
347 let tac = cast_typ typ_ltac2 @@ Id.Map.get tac_id ist.Tacinterp.lfun in
348 let arg = Id.Map.get arg_id ist.Tacinterp.lfun in
349 let tac = Tac2ffi.to_closure tac in
350 Tac2val.apply tac [Tac2ffi.repr_of ltac1 arg] >>= fun ans ->
351 let ans = Tac2ffi.repr_to ltac1 ans in
352 Ftactic.return ans
353 in
354 let () = Geninterp.register_interp0 wit_ltac2_val interp_fun in
355 define "ltac1_lambda" (valexpr @-> ret ltac1) @@ fun f ->
356 let body = Tacexpr.TacGeneric (Some ltac2_ltac1_plugin, in_gen (glbwit wit_ltac2_val) ()) in
357 let clos = CAst.make (Tacexpr.TacFun ([Name arg_id], CAst.make (Tacexpr.TacArg body))) in
358 let f = Geninterp.Val.inject (Geninterp.Val.Base typ_ltac2) f in
359 let lfun = Id.Map.singleton tac_id f in
360 let ist = { (Tacinterp.default_ist ()) with Tacinterp.lfun } in
361 Tacinterp.Value.of_closure ist clos
362
363 let ltac2_eval =
364 let open Ltac_plugin in
365 let ml_name = {
366 Tacexpr.mltac_plugin = ltac2_ltac1_plugin;
367 mltac_tactic = "ltac2_eval";
368 } in
369 let eval_fun args ist = match args with
370 | [] -> assert false
371 | tac :: args ->
372 (* By convention the first argument is the tactic being applied, the rest
373 being the arguments it should be fed with *)
374 let tac = cast_typ typ_ltac2 tac in
375 let tac = Tac2ffi.to_closure tac in
376 let args = List.map (fun arg -> Tac2ffi.repr_of ltac1 arg) args in
377 Proofview.tclIGNORE (Tac2val.apply tac args)
378 in
379 let () = Tacenv.register_ml_tactic ml_name [|eval_fun|] in
380 { Tacexpr.mltac_name = ml_name; mltac_index = 0 }
381
382 let () =
383 let open Ltac_plugin in
384 let open Tacinterp in
385 let interp ist (ids, tac) = match ids with
386 | [] ->
387 (* Evaluate the Ltac2 quotation eagerly *)
388 let idtac = Value.of_closure { ist with lfun = Id.Map.empty }
389 (CAst.make (Tacexpr.TacId [])) in
390 let ist = { env_ist = Id.Map.empty } in
391 Tac2interp.interp ist tac >>= fun v ->
392 let v = idtac in
393 Ftactic.return v
394 | _ :: _ ->
395 (* Return a closure [@f := {blob} |- fun ids => ltac2_eval(f, ids) ] *)
396 (* This name cannot clash with Ltac2 variables which are all lowercase *)
397 let self_id = Id.of_string "F" in
398 let nas = List.map (fun id -> Name id) ids in
399 let mk_arg id = Tacexpr.Reference (Locus.ArgVar (CAst.make id)) in
400 let args = List.map mk_arg ids in
401 let clos = CAst.make (Tacexpr.TacFun
402 (nas, CAst.make (Tacexpr.TacML (ltac2_eval, mk_arg self_id :: args)))) in
403 let self = GTacFun (List.map (fun id -> Name id) ids, tac) in
404 let self = Tac2interp.interp_value { env_ist = Id.Map.empty } self in
405 let self = Geninterp.Val.inject (Geninterp.Val.Base typ_ltac2) self in
406 let ist = { ist with lfun = Id.Map.singleton self_id self } in
407 Ftactic.return (Value.of_closure ist clos)
408 in
409 Geninterp.register_interp0 wit_ltac2in1 interp
410
411 let () =
412 let interp ist tac =
413 let ist = { env_ist = Id.Map.empty } in
414 Tac2interp.interp ist tac >>= fun v ->
415 let v = repr_to ltac1 v in
416 Ftactic.return v
417 in
418 Geninterp.register_interp0 wit_ltac2in1_val interp