Debian Codegraph @

plugins/ltac2/tac2ffi.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 Tac2dyn
13 open Tac2val
14
15 type 'a repr = {
16 r_of : 'a -> valexpr;
17 r_to : valexpr -> 'a;
18 }
19
20 let repr_of r x = r.r_of x
21 let repr_to r x = r.r_to x
22
23 let make_repr r_of r_to = { r_of; r_to; }
24
25 let map_repr f g r = {
26 r_of = (fun x -> r.r_of (g x));
27 r_to = (fun x -> f (r.r_to x));
28 }
29
30 type ind_data = (Names.Ind.t * Declarations.mutual_inductive_body)
31 type binder = (Names.Name.t EConstr.binder_annot * EConstr.types)
32
33 module ModField = struct
34 type t =
35 | Ref of Names.GlobRef.t
36 | Submodule of Names.ModPath.t
37 | Rewrule
38 end
39
40 (** Dynamic tags *)
41
42 let val_exn = Val.create "exn"
43 let val_exninfo = Val.create "exninfo"
44 let val_constr = Val.create "constr"
45 let val_ident = Val.create "ident"
46 let val_pattern = Val.create "pattern"
47 let val_preterm = Val.create "preterm"
48 let val_matching_context = Val.create "matching_context"
49 let val_pp = Val.create "pp"
50 let val_evar = Val.create "evar"
51 let val_sort = Val.create "sort"
52 let val_cast = Val.create "cast"
53 let val_inductive = Val.create "inductive"
54 let val_constant = Val.create "constant"
55 let val_constructor = Val.create "constructor"
56 let val_projection = Val.create "projection"
57 let val_qvar = Val.create "qvar"
58 let val_case = Val.create "case"
59 let val_binder = Val.create "binder"
60 let val_instance = Val.create "instance"
61 let val_free = Val.create "free"
62 let val_uint63 = Val.create "uint63"
63 let val_float = Val.create "float"
64 let val_pstring = Val.create "pstring"
65 let val_ind_data : (Names.Ind.t * Declarations.mutual_inductive_body) Val.tag = Val.create "ind_data"
66 let val_transparent_state : TransparentState.t Val.tag = Val.create "transparent_state"
67 let val_pretype_flags = Val.create "pretype_flags"
68 let val_expected_type = Val.create "expected_type"
69 let val_reduction = Val.create "reduction"
70 let val_rewstrategy = Val.create "rewstrategy"
71 let val_modpath = Val.create "modpath"
72 let val_module_field = Val.create "module_field"
73
74 let extract_val (type a) (type b) (tag : a Val.tag) (tag' : b Val.tag) (v : b) : a =
75 match Val.eq tag tag' with
76 | None -> assert false
77 | Some Refl -> v
78
79 (** Exception *)
80
81 exception LtacError of Names.KerName.t * valexpr array
82
83 (** Conversion functions *)
84
85 let valexpr = {
86 r_of = (fun obj -> obj);
87 r_to = (fun obj -> obj);
88 }
89
90 let of_unit () = ValInt 0
91
92 let to_unit = function
93 | ValInt 0 -> ()
94 | _ -> assert false
95
96 let unit = {
97 r_of = of_unit;
98 r_to = to_unit;
99 }
100
101 let of_int n = ValInt n
102 let to_int = function
103 | ValInt n -> n
104 | _ -> assert false
105
106 let int = {
107 r_of = of_int;
108 r_to = to_int;
109 }
110
111 let of_bool b = if b then ValInt 0 else ValInt 1
112
113 let to_bool = function
114 | ValInt 0 -> true
115 | ValInt 1 -> false
116 | _ -> assert false
117
118 let bool = {
119 r_of = of_bool;
120 r_to = to_bool;
121 }
122
123 let of_char n = ValInt (Char.code n)
124 let to_char = function
125 | ValInt n -> Char.chr n
126 | _ -> assert false
127
128 let char = {
129 r_of = of_char;
130 r_to = to_char;
131 }
132
133 let of_bytes s = ValStr s
134 let to_bytes = function
135 | ValStr s -> s
136 | _ -> assert false
137
138 let bytes = {
139 r_of = of_bytes;
140 r_to = to_bytes;
141 }
142
143 let of_string s = of_bytes (Bytes.of_string s)
144 let to_string s = Bytes.to_string (to_bytes s)
145 let string = {
146 r_of = of_string;
147 r_to = to_string;
148 }
149
150 let rec of_list f = function
151 | [] -> ValInt 0
152 | x :: l -> ValBlk (0, [| f x; of_list f l |])
153
154 let rec to_list f = function
155 | ValInt 0 -> []
156 | ValBlk (0, [|v; vl|]) -> f v :: to_list f vl
157 | _ -> assert false
158
159 let list r = {
160 r_of = (fun l -> of_list r.r_of l);
161 r_to = (fun l -> to_list r.r_to l);
162 }
163
164 let of_closure cls = ValCls cls
165
166 let to_closure = Tac2val.to_closure
167
168 let closure = {
169 r_of = of_closure;
170 r_to = to_closure;
171 }
172
173 type ('a, 'b) fun1 = 'a -> 'b Proofview.tactic
174
175 let of_fun1 to_arg of_res f =
176 of_closure (mk_closure arity_one (fun x ->
177 Proofview.Monad.map of_res @@
178 f (to_arg x)))
179
180 let to_fun1 of_arg to_res f x =
181 Proofview.Monad.map to_res @@
182 apply (to_closure f) [of_arg x]
183
184 let fun1 arg res = {
185 r_of = of_fun1 arg.r_to res.r_of;
186 r_to = to_fun1 arg.r_of res.r_to;
187 }
188
189 let thunk r = fun1 unit r
190
191 type ('a, 'b, 'c) fun2 = 'a -> 'b -> 'c Proofview.tactic
192
193 let of_fun2 to_arg1 to_arg2 of_res f =
194 of_closure (mk_closure (arity_suc arity_one) (fun x y ->
195 Proofview.Monad.map of_res @@
196 f (to_arg1 x) (to_arg2 y)))
197
198 let to_fun2 of_arg1 of_arg2 to_res f x y =
199 Proofview.Monad.map to_res @@
200 apply (to_closure f) [of_arg1 x; of_arg2 y]
201
202 let fun2 arg1 arg2 res = {
203 r_of = of_fun2 arg1.r_to arg2.r_to res.r_of;
204 r_to = to_fun2 arg1.r_of arg2.r_of res.r_to;
205 }
206
207 let of_ext tag c =
208 ValExt (tag, c)
209
210 let to_ext tag = function
211 | ValExt (tag', e) -> extract_val tag tag' e
212 | _ -> assert false
213
214 let repr_ext tag = {
215 r_of = (fun e -> of_ext tag e);
216 r_to = (fun e -> to_ext tag e);
217 }
218
219 let of_constr c = of_ext val_constr c
220 let to_constr c = to_ext val_constr c
221 let constr = repr_ext val_constr
222
223 let of_matching_context c = of_ext val_matching_context c
224 let to_matching_context c = to_ext val_matching_context c
225 let matching_context = repr_ext val_matching_context
226
227 let of_preterm c = of_ext val_preterm c
228 let to_preterm c = to_ext val_preterm c
229 let preterm = repr_ext val_preterm
230
231 let of_cast c = of_ext val_cast c
232 let to_cast c = to_ext val_cast c
233 let cast = repr_ext val_cast
234
235 let of_ident c = of_ext val_ident c
236 let to_ident c = to_ext val_ident c
237 let ident = repr_ext val_ident
238
239 let of_pattern c = of_ext val_pattern c
240 let to_pattern c = to_ext val_pattern c
241 let pattern = repr_ext val_pattern
242
243 let of_evar ev = of_ext val_evar ev
244 let to_evar ev = to_ext val_evar ev
245 let evar = repr_ext val_evar
246
247 let of_sort ev = of_ext val_sort ev
248 let to_sort ev = to_ext val_sort ev
249 let sort = repr_ext val_sort
250
251 let of_reduction ev = of_ext val_reduction ev
252 let to_reduction ev = to_ext val_reduction ev
253 let reduction = repr_ext val_reduction
254
255 let of_rewstrategy ev = of_ext val_rewstrategy ev
256 let to_rewstrategy ev = to_ext val_rewstrategy ev
257 let rewstrategy = repr_ext val_rewstrategy
258
259 let rocq_core n = Names.(KerName.make Tac2env.rocq_prefix (Id.of_string_soft n))
260
261 let internal_err = rocq_core "Internal"
262
263 let err_notfocussed =
264 LtacError (rocq_core "Not_focussed", [||])
265
266 let err_outofbounds =
267 LtacError (rocq_core "Out_of_bounds", [||])
268
269 let err_notfound =
270 LtacError (rocq_core "Not_found", [||])
271
272 let err_matchfailure =
273 LtacError (rocq_core "Match_failure", [||])
274
275 let err_division_by_zero =
276 LtacError (rocq_core "Division_by_zero", [||])
277
278 let of_exninfo = of_ext val_exninfo
279 let to_exninfo = to_ext val_exninfo
280
281 let exninfo = {
282 r_of = of_exninfo;
283 r_to = to_exninfo;
284 }
285
286 (* Invariant: no [LtacError] should be put into a value with tag [val_exn]. *)
287 let of_err e = of_ext val_exn e
288 let to_err e = to_ext val_exn e
289 let err = repr_ext val_exn
290
291 (** FIXME: handle backtrace in Ltac2 exceptions *)
292 let of_exn c = match fst c with
293 | LtacError (kn, c) -> ValOpn (kn, c)
294 | _ -> ValOpn (internal_err, [|of_err c|])
295
296 let to_exn c = match c with
297 | ValOpn (kn, c) ->
298 if Names.KerName.equal kn internal_err then
299 to_err c.(0)
300 else
301 (LtacError (kn, c), Exninfo.null)
302 | _ -> assert false
303
304 let exn = {
305 r_of = of_exn;
306 r_to = to_exn;
307 }
308
309 let of_result of_ok = function
310 | Ok v -> ValBlk (0, [|of_ok v|])
311 | Error e -> ValBlk (1, [|of_exn e|])
312
313 let to_result to_ok = function
314 | ValBlk (0, [|v|]) -> Ok (to_ok v)
315 | ValBlk (1, [|e|]) -> Error (to_exn e)
316 | _ -> assert false
317
318 let result ok = {
319 r_of = of_result ok.r_of;
320 r_to = to_result ok.r_to;
321 }
322
323 let of_option f = function
324 | None -> ValInt 0
325 | Some c -> ValBlk (0, [|f c|])
326
327 let to_option f = function
328 | ValInt 0 -> None
329 | ValBlk (0, [|c|]) -> Some (f c)
330 | _ -> assert false
331
332 let option r = {
333 r_of = (fun l -> of_option r.r_of l);
334 r_to = (fun l -> to_option r.r_to l);
335 }
336
337 let of_pp c = of_ext val_pp c
338 let to_pp c = to_ext val_pp c
339 let pp = repr_ext val_pp
340
341 let of_tuple cl = ValBlk (0, cl)
342 let to_tuple = function
343 | ValBlk (0, cl) -> cl
344 | _ -> assert false
345
346 let of_pair f g (x, y) = ValBlk (0, [|f x; g y|])
347 let to_pair f g = function
348 | ValBlk (0, [|x; y|]) -> (f x, g y)
349 | _ -> assert false
350 let pair r0 r1 = {
351 r_of = (fun p -> of_pair r0.r_of r1.r_of p);
352 r_to = (fun p -> to_pair r0.r_to r1.r_to p);
353 }
354
355 let of_triple f g h (x, y, z) = ValBlk (0, [|f x; g y; h z|])
356 let to_triple f g h = function
357 | ValBlk (0, [|x; y; z|]) -> (f x, g y, h z)
358 | _ -> assert false
359 let triple r0 r1 r2 = {
360 r_of = (fun p -> of_triple r0.r_of r1.r_of r2.r_of p);
361 r_to = (fun p -> to_triple r0.r_to r1.r_to r2.r_to p);
362 }
363
364 let of_array f vl = ValBlk (0, Array.map f vl)
365 let to_array f = function
366 | ValBlk (0, vl) -> Array.map f vl
367 | _ -> assert false
368 let array r = {
369 r_of = (fun l -> of_array r.r_of l);
370 r_to = (fun l -> to_array r.r_to l);
371 }
372
373 let of_block (n, args) = ValBlk (n, args)
374 let to_block = function
375 | ValBlk (n, args) -> (n, args)
376 | _ -> assert false
377
378 let block = {
379 r_of = of_block;
380 r_to = to_block;
381 }
382
383 let of_open (kn, args) = ValOpn (kn, args)
384
385 let to_open = function
386 | ValOpn (kn, args) -> (kn, args)
387 | _ -> assert false
388
389 let open_ = {
390 r_of = of_open;
391 r_to = to_open;
392 }
393
394 let of_free n = of_ext val_free n
395 let to_free x = to_ext val_free x
396
397 let free = {
398 r_of = of_free;
399 r_to = to_free;
400 }
401
402 let of_uint63 n = of_ext val_uint63 n
403 let to_uint63 x = to_ext val_uint63 x
404
405 let uint63 = {
406 r_of = of_uint63;
407 r_to = to_uint63;
408 }
409
410 let of_float f = of_ext val_float f
411 let to_float x = to_ext val_float x
412
413 let float = {
414 r_of = of_float;
415 r_to = to_float;
416 }
417
418 let of_pstring s = of_ext val_pstring s
419 let to_pstring x = to_ext val_pstring x
420
421 let pstring = {
422 r_of = of_pstring;
423 r_to = to_pstring;
424 }
425
426 let of_transparent_state c = of_ext val_transparent_state c
427 let to_transparent_state c = to_ext val_transparent_state c
428 let transparent_state = repr_ext val_transparent_state
429
430 let of_pretype_flags c = of_ext val_pretype_flags c
431 let to_pretype_flags c = to_ext val_pretype_flags c
432 let pretype_flags = repr_ext val_pretype_flags
433
434 let of_expected_type c = of_ext val_expected_type c
435 let to_expected_type c = to_ext val_expected_type c
436 let expected_type = repr_ext val_expected_type
437
438 let of_ind_data c = of_ext val_ind_data c
439 let to_ind_data c = to_ext val_ind_data c
440 let ind_data = repr_ext val_ind_data
441
442 let of_inductive c = of_ext val_inductive c
443 let to_inductive c = to_ext val_inductive c
444 let inductive = repr_ext val_inductive
445
446 let of_constant c = of_ext val_constant c
447 let to_constant c = to_ext val_constant c
448 let constant = repr_ext val_constant
449
450 let of_constructor c = of_ext val_constructor c
451 let to_constructor c = to_ext val_constructor c
452 let constructor = repr_ext val_constructor
453
454 let of_projection c = of_ext val_projection c
455 let to_projection c = to_ext val_projection c
456 let projection = repr_ext val_projection
457
458 let of_qvar c = of_ext val_qvar c
459 let to_qvar c = to_ext val_qvar c
460 let qvar = repr_ext val_qvar
461
462 let of_case c = of_ext val_case c
463 let to_case c = to_ext val_case c
464 let case = repr_ext val_case
465
466 let of_binder c = of_ext val_binder c
467 let to_binder c = to_ext val_binder c
468 let binder = repr_ext val_binder
469
470 let of_instance c = of_ext val_instance c
471 let to_instance c = to_ext val_instance c
472 let instance = repr_ext val_instance
473
474 let of_modpath c = of_ext val_modpath c
475 let to_modpath c = to_ext val_modpath c
476 let modpath = repr_ext val_modpath
477
478 let of_module_field c = of_ext val_module_field c
479 let to_module_field c = to_ext val_module_field c
480 let module_field = repr_ext val_module_field
481
482
483 let of_reference = let open Names.GlobRef in function
484 | VarRef id -> ValBlk (0, [| of_ident id |])
485 | ConstRef cst -> ValBlk (1, [| of_constant cst |])
486 | IndRef ind -> ValBlk (2, [| of_inductive ind |])
487 | ConstructRef cstr -> ValBlk (3, [| of_constructor cstr |])
488
489 let to_reference = let open Names.GlobRef in function
490 | ValBlk (0, [| id |]) -> VarRef (to_ident id)
491 | ValBlk (1, [| cst |]) -> ConstRef (to_constant cst)
492 | ValBlk (2, [| ind |]) -> IndRef (to_inductive ind)
493 | ValBlk (3, [| cstr |]) -> ConstructRef (to_constructor cstr)
494 | _ -> assert false
495
496 let reference = {
497 r_of = of_reference;
498 r_to = to_reference;
499 }