Debian Codegraph @

plugins/ltac2/tac2core.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 Tac2val
15 open Tac2ffi
16 open Tac2extffi
17 open Tac2expr
18 open Proofview.Notations
19
20 let ltac2_plugin = "rocq-runtime.plugins.ltac2"
21
22 let constr_flags =
23 let open Pretyping in
24 {
25 use_coercions = true;
26 use_typeclasses = Pretyping.UseTC;
27 solve_unification_constraints = true;
28 fail_evar = true;
29 expand_evars = true;
30 program_mode = false;
31 poly = PolyFlags.default;
32 undeclared_evars_rr = false;
33 unconstrained_sorts = false;
34 }
35
36 let open_constr_no_classes_flags =
37 let open Pretyping in
38 {
39 use_coercions = true;
40 use_typeclasses = Pretyping.NoUseTC;
41 solve_unification_constraints = true;
42 fail_evar = false;
43 expand_evars = false;
44 program_mode = false;
45 poly = PolyFlags.default;
46 undeclared_evars_rr = false;
47 unconstrained_sorts = false;
48 }
49
50 let preterm_flags =
51 let open Pretyping in
52 {
53 use_coercions = true;
54 use_typeclasses = Pretyping.NoUseTC;
55 solve_unification_constraints = true;
56 fail_evar = false;
57 expand_evars = false;
58 program_mode = false;
59 poly = PolyFlags.default;
60 undeclared_evars_rr = false;
61 unconstrained_sorts = false;
62 }
63
64 (** Standard values *)
65
66 open Tac2quote.Refs
67
68 let v_blk = Valexpr.make_block
69
70 let of_relevance = function
71 | Sorts.Relevant -> ValInt 0
72 | Sorts.Irrelevant -> ValInt 1
73 | Sorts.RelevanceVar q -> ValBlk (0, [|of_qvar q|])
74
75 let to_relevance = function
76 | ValInt 0 -> Sorts.Relevant
77 | ValInt 1 -> Sorts.Irrelevant
78 | ValBlk (0, [|qvar|]) ->
79 let qvar = to_qvar qvar in
80 Sorts.RelevanceVar qvar
81 | _ -> assert false
82
83 (* XXX ltac2 exposes relevance internals so breaks ERelevance abstraction
84 ltac2 Constr.Binder.relevance probably needs to be made an abstract type *)
85 let relevance = make_repr of_relevance to_relevance
86
87 let of_rec_declaration (nas, ts, cs) =
88 let binders = Array.map2 (fun na t -> (na, t)) nas ts in
89 (Tac2ffi.of_array of_binder binders,
90 Tac2ffi.of_array Tac2ffi.of_constr cs)
91
92 let to_rec_declaration (nas, cs) =
93 let nas = Tac2ffi.to_array to_binder nas in
94 (Array.map fst nas,
95 Array.map snd nas,
96 Tac2ffi.to_array Tac2ffi.to_constr cs)
97
98 let of_case_invert = let open Constr in function
99 | NoInvert -> ValInt 0
100 | CaseInvert {indices} ->
101 v_blk 0 [|of_array of_constr indices|]
102
103 let to_case_invert = let open Constr in function
104 | ValInt 0 -> NoInvert
105 | ValBlk (0, [|indices|]) ->
106 let indices = to_array to_constr indices in
107 CaseInvert {indices}
108 | _ -> CErrors.anomaly Pp.(str "unexpected value shape")
109
110 let of_result f = function
111 | Inl c -> v_blk 0 [|f c|]
112 | Inr e -> v_blk 1 [|Tac2ffi.of_exn e|]
113
114 (** Helper functions *)
115
116 let thaw f : _ Proofview.tactic = f ()
117
118 let fatal_flag : unit Exninfo.t = Exninfo.make "fatal_flag"
119
120 let has_fatal_flag info = match Exninfo.get info fatal_flag with
121 | None -> false
122 | Some () -> true
123
124 let set_bt info =
125 if !Tac2bt.print_ltac2_backtrace then
126 Tac2bt.get_backtrace >>= fun bt ->
127 Proofview.tclUNIT (Exninfo.add info Tac2bt.backtrace bt)
128 else Proofview.tclUNIT info
129
130 let throw ?(info = Exninfo.null) e =
131 set_bt info >>= fun info ->
132 let info = Exninfo.add info fatal_flag () in
133 Proofview.tclLIFT (Proofview.NonLogical.raise (e, info))
134
135 let fail ?(info = Exninfo.null) e =
136 set_bt info >>= fun info ->
137 Proofview.tclZERO ~info e
138
139 let return x = Proofview.tclUNIT x
140 let pname ?(plugin=ltac2_plugin) s = { mltac_plugin = plugin; mltac_tactic = s }
141
142 let catchable_exception = function
143 | Logic_monad.Exception _ -> false
144 | e -> CErrors.noncritical e
145
146 (* Adds ltac2 backtrace
147 With [passthrough:false], acts like [Proofview.wrap_exceptions] + Ltac2 backtrace handling
148 *)
149 let wrap_exceptions ?(passthrough=false) f =
150 try f ()
151 with e ->
152 let e, info = Exninfo.capture e in
153 set_bt info >>= fun info ->
154 if not passthrough && catchable_exception e
155 then begin if has_fatal_flag info
156 then Proofview.tclLIFT (Proofview.NonLogical.raise (e, info))
157 else Proofview.tclZERO ~info e
158 end
159 else Exninfo.iraise (e, info)
160
161 let assert_focussed =
162 Proofview.Goal.goals >>= fun gls ->
163 match gls with
164 | [_] -> Proofview.tclUNIT ()
165 | [] | _ :: _ :: _ -> throw Tac2ffi.err_notfocussed
166
167 let pf_apply ?(catch_exceptions=false) f =
168 let f env sigma = wrap_exceptions ~passthrough:(not catch_exceptions) (fun () -> f env sigma) in
169 Proofview.Goal.goals >>= function
170 | [] ->
171 Proofview.tclENV >>= fun env ->
172 Proofview.tclEVARMAP >>= fun sigma ->
173 f env sigma
174 | [gl] ->
175 gl >>= fun gl ->
176 f (Proofview.Goal.env gl) (Proofview.Goal.sigma gl)
177 | _ :: _ :: _ ->
178 throw Tac2ffi.err_notfocussed
179
180 open Tac2externals
181
182 let define ?plugin s = define (pname ?plugin s)
183
184 (** Printing *)
185
186 let () = define "print" (pp @-> ret unit) Feedback.msg_notice
187
188 let () = define "message_empty" (ret pp) (Pp.mt ())
189
190 let () = define "message_of_int" (int @-> ret pp) Pp.int
191
192 let () = define "message_of_string" (string @-> ret pp) Pp.str
193
194 let () = define "message_to_string" (pp @-> ret string) Pp.string_of_ppcmds
195
196 let () =
197 define "message_of_constr" (constr @-> tac pp) @@ fun c ->
198 pf_apply @@ fun env sigma -> return (Printer.pr_econstr_env env sigma c)
199
200 let () =
201 define "message_of_lconstr" (constr @-> tac pp) @@ fun c ->
202 pf_apply @@ fun env sigma -> return (Printer.pr_leconstr_env env sigma c)
203
204 let () =
205 define "message_of_preterm" (preterm @-> tac pp) @@ fun c ->
206 pf_apply @@ fun env sigma -> return (Printer.pr_closed_glob_env env sigma c)
207
208 let () =
209 define "message_of_lpreterm" (preterm @-> tac pp) @@ fun c ->
210 pf_apply @@ fun env sigma -> return (Printer.pr_closed_lglob_env env sigma c)
211
212 let () = define "message_of_ident" (ident @-> ret pp) Id.print
213
214 let () = define "constant_print" (constant @-> ret pp) @@ fun c ->
215 Nametab.pr_global_env Id.Set.empty (ConstRef c)
216
217 let () = define "projection_print" (projection @-> ret pp) @@ fun p ->
218 Nametab.pr_global_env Id.Set.empty (ConstRef (Projection.constant p))
219
220 let () = define "ind_print" (inductive @-> ret pp) @@ fun ind ->
221 Nametab.pr_global_env Id.Set.empty (IndRef ind)
222
223 let () = define "constructor_print" (constructor @-> ret pp) @@ fun ctor ->
224 Nametab.pr_global_env Id.Set.empty (ConstructRef ctor)
225
226 let () =
227 define "message_of_exn" (valexpr @-> eret pp) @@ fun v env sigma ->
228 Tac2print.pr_valexpr env sigma v (GTypRef (Other t_exn, []))
229
230 let () = define "message_concat" (pp @-> pp @-> ret pp) Pp.app
231
232 let () = define "message_force_new_line" (ret pp) (Pp.fnl ())
233
234 let () = define "message_break" (int @-> int @-> ret pp) (fun i j -> Pp.brk (i,j))
235
236 let () = define "message_space" (ret pp) (Pp.spc())
237
238 let () = define "message_hbox" (pp @-> ret pp) Pp.h
239
240 let () = define "message_vbox" (int @-> pp @-> ret pp) Pp.v
241
242 let () = define "message_hvbox" (int @-> pp @-> ret pp) Pp.hv
243
244 let () = define "message_hovbox" (int @-> pp @-> ret pp) Pp.hov
245
246 let () = define "format_stop" (ret format) []
247
248 let () =
249 define "format_string" (format @-> ret format) @@ fun s ->
250 FmtString :: s
251
252 let () =
253 define "format_int" (format @-> ret format) @@ fun s ->
254 FmtInt :: s
255
256 let () =
257 define "format_constr" (format @-> ret format) @@ fun s ->
258 FmtConstr :: s
259
260 let () =
261 define "format_ident" (format @-> ret format) @@ fun s ->
262 FmtIdent :: s
263
264 let () =
265 define "format_literal" (string @-> format @-> ret format) @@ fun lit s ->
266 FmtLiteral lit :: s
267
268 let () =
269 define "format_alpha" (format @-> ret format) @@ fun s ->
270 FmtAlpha :: s
271
272 let () =
273 define "format_alpha0" (format @-> ret format) @@ fun s ->
274 FmtAlpha0 :: s
275
276 let () =
277 define "format_message" (format @-> ret format) @@ fun s ->
278 FmtMessage :: s
279
280 let arity_of_format fmt =
281 let open Tac2types in
282 let fold accu = function
283 | FmtLiteral _ -> accu
284 | FmtString | FmtInt | FmtConstr | FmtIdent | FmtMessage -> 1 + accu
285 | FmtAlpha | FmtAlpha0 -> 2 + accu
286 in
287 List.fold_left fold 0 fmt
288
289 let () =
290 define "format_kfprintf" (closure @-> format @-> tac valexpr) @@ fun k fmt ->
291 let open Tac2types in
292 let pop1 l = match l with [] -> assert false | x :: l -> (x, l) in
293 let pop2 l = match l with [] | [_] -> assert false | x :: y :: l -> (x, y, l) in
294 let arity = arity_of_format fmt in
295 let rec eval accu args fmt = match fmt with
296 | [] -> apply k [of_pp accu]
297 | tag :: fmt ->
298 match tag with
299 | FmtLiteral s ->
300 eval (Pp.app accu (Pp.str s)) args fmt
301 | FmtString ->
302 let (s, args) = pop1 args in
303 let pp = Pp.str (to_string s) in
304 eval (Pp.app accu pp) args fmt
305 | FmtInt ->
306 let (i, args) = pop1 args in
307 let pp = Pp.int (to_int i) in
308 eval (Pp.app accu pp) args fmt
309 | FmtConstr ->
310 let (c, args) = pop1 args in
311 let c = to_constr c in
312 pf_apply begin fun env sigma ->
313 let pp = Printer.pr_econstr_env env sigma c in
314 eval (Pp.app accu pp) args fmt
315 end
316 | FmtIdent ->
317 let (i, args) = pop1 args in
318 let pp = Id.print (to_ident i) in
319 eval (Pp.app accu pp) args fmt
320 | FmtMessage ->
321 let (m, args) = pop1 args in
322 let m = to_pp m in
323 eval (Pp.app accu m) args fmt
324 | FmtAlpha ->
325 let (f, x, args) = pop2 args in
326 Tac2val.apply_val f [of_unit (); x] >>= fun pp ->
327 eval (Pp.app accu (to_pp pp)) args fmt
328 | FmtAlpha0 ->
329 let (f, x, args) = pop2 args in
330 Tac2val.apply_val f [x] >>= fun pp ->
331 eval (Pp.app accu (to_pp pp)) args fmt
332 in
333 let eval v = eval (Pp.mt ()) v fmt in
334 if Int.equal arity 0 then eval []
335 else return (Tac2ffi.of_closure (Tac2val.abstract arity eval))
336
337 let () =
338 define "format_ikfprintf" (closure @-> valexpr @-> format @-> tac valexpr) @@ fun k v fmt ->
339 let arity = arity_of_format fmt in
340 let eval _args = apply k [v] in
341 if Int.equal arity 0 then eval []
342 else return (Tac2ffi.of_closure (Tac2val.abstract arity eval))
343
344 (** Array *)
345
346 let () = define "array_empty" (ret valexpr) (v_blk 0 [||])
347
348 let () =
349 define "array_make" (int @-> valexpr @-> tac valexpr) @@ fun n x ->
350 try return (v_blk 0 (Array.make n x)) with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
351
352 let () =
353 define "array_length" (block @-> ret int) @@ fun (_, v) -> Array.length v
354
355 let () =
356 define "array_set" (block @-> int @-> valexpr @-> tac unit) @@ fun (_, v) n x ->
357 try Array.set v n x; return () with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
358
359 let () =
360 define "array_get" (block @-> int @-> tac valexpr) @@ fun (_, v) n ->
361 try return (Array.get v n) with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
362
363 let () =
364 define "array_blit"
365 (block @-> int @-> block @-> int @-> int @-> tac unit)
366 @@ fun (_, v0) s0 (_, v1) s1 l ->
367 try Array.blit v0 s0 v1 s1 l; return () with Invalid_argument _ ->
368 throw Tac2ffi.err_outofbounds
369
370 let () =
371 define "array_fill" (block @-> int @-> int @-> valexpr @-> tac unit) @@ fun (_, d) s l v ->
372 try Array.fill d s l v; return () with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
373
374 let () =
375 define "array_concat" (list block @-> ret valexpr) @@ fun l ->
376 v_blk 0 (Array.concat (List.map snd l))
377
378 (** Ident *)
379
380 let () = define "ident_equal" (ident @-> ident @-> ret bool) Id.equal
381
382 let () = define "ident_to_string" (ident @-> ret string) Id.to_string
383
384 let () =
385 define "ident_of_string" (string @-> ret (option ident)) @@ fun s ->
386 try Some (Id.of_string s) with e when CErrors.noncritical e -> None
387
388 (** Int *)
389
390 let () = define "int_equal" (int @-> int @-> ret bool) (==)
391
392 let () = define "int_neg" (int @-> ret int) (~-)
393 let () = define "int_abs" (int @-> ret int) abs
394
395 let () = define "int_compare" (int @-> int @-> ret int) Int.compare
396 let () = define "int_add" (int @-> int @-> ret int) (+)
397 let () = define "int_sub" (int @-> int @-> ret int) (-)
398 let () = define "int_mul" (int @-> int @-> ret int) ( * )
399
400 let () = define "int_div" (int @-> int @-> tac int) @@ fun m n ->
401 if n == 0 then throw Tac2ffi.err_division_by_zero else return (m / n)
402 let () = define "int_mod" (int @-> int @-> tac int) @@ fun m n ->
403 if n == 0 then throw Tac2ffi.err_division_by_zero else return (m mod n)
404
405 let () = define "int_asr" (int @-> int @-> ret int) (asr)
406 let () = define "int_lsl" (int @-> int @-> ret int) (lsl)
407 let () = define "int_lsr" (int @-> int @-> ret int) (lsr)
408 let () = define "int_land" (int @-> int @-> ret int) (land)
409 let () = define "int_lor" (int @-> int @-> ret int) (lor)
410 let () = define "int_lxor" (int @-> int @-> ret int) (lxor)
411 let () = define "int_lnot" (int @-> ret int) lnot
412
413 (** Char *)
414
415 let () = define "char_of_int" (int @-> tac char) @@ fun i ->
416 try return (Char.chr i)
417 with Invalid_argument _ as e ->
418 let e, info = Exninfo.capture e in
419 throw ~info e
420
421 let () = define "char_to_int" (char @-> ret int) Char.code
422
423 (** String *)
424
425 let () =
426 define "string_make" (int @-> char @-> tac bytes) @@ fun n c ->
427 try return (Bytes.make n c) with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
428
429 let () = define "string_length" (bytes @-> ret int) Bytes.length
430
431 let () =
432 define "string_set" (bytes @-> int @-> char @-> tac unit) @@ fun s n c ->
433 try Bytes.set s n c; return () with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
434
435 let () =
436 define "string_get" (bytes @-> int @-> tac char) @@ fun s n ->
437 try return (Bytes.get s n) with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
438
439 let () = define "string_concat" (bytes @-> list bytes @-> ret bytes) Bytes.concat
440
441 let () =
442 define "string_app" (bytes @-> bytes @-> ret bytes) @@ fun a b ->
443 Bytes.concat Bytes.empty [a; b]
444
445 let () =
446 define "string_sub" (bytes @-> int @-> int @-> tac bytes) @@ fun s off len ->
447 try return (Bytes.sub s off len) with Invalid_argument _ -> throw Tac2ffi.err_outofbounds
448
449 let () = define "string_equal" (bytes @-> bytes @-> ret bool) Bytes.equal
450
451 let () = define "string_compare" (bytes @-> bytes @-> ret int) Bytes.compare
452
453 (** Pstring *)
454
455 let () =
456 define "pstring_max_length" (ret uint63) Pstring.max_length;
457 define "pstring_to_string" (pstring @-> ret string) Pstring.to_string;
458 define "pstring_of_string" (string @-> ret (option pstring)) Pstring.of_string;
459 define "pstring_make" (uint63 @-> uint63 @-> ret pstring) Pstring.make;
460 define "pstring_length" (pstring @-> ret uint63) Pstring.length;
461 define "pstring_get" (pstring @-> uint63 @-> ret uint63) Pstring.get;
462 define "pstring_sub" (pstring @-> uint63 @-> uint63 @-> ret pstring) Pstring.sub;
463 define "pstring_cat" (pstring @-> pstring @-> ret pstring) Pstring.cat;
464 define "pstring_equal" (pstring @-> pstring @-> ret bool) Pstring.equal;
465 define "pstring_compare" (pstring @-> pstring @-> ret int) Pstring.compare
466
467 (** Terms *)
468
469 (** constr -> constr *)
470 let () =
471 define "constr_type" (constr @-> tac valexpr) @@ fun c ->
472 let get_type env sigma =
473 let (sigma, t) = Typing.type_of env sigma c in
474 let t = Tac2ffi.of_constr t in
475 Proofview.Unsafe.tclEVARS sigma <*> Proofview.tclUNIT t
476 in
477 pf_apply ~catch_exceptions:true get_type
478
479 (** constr -> constr *)
480 let () =
481 define "constr_equal" (constr @-> constr @-> tac bool) @@ fun c1 c2 ->
482 Proofview.tclEVARMAP >>= fun sigma -> return (EConstr.eq_constr sigma c1 c2)
483
484 let () =
485 define "constr_kind" (constr @-> eret valexpr) @@ fun c env sigma ->
486 let open Constr in
487 match EConstr.kind sigma c with
488 | Rel n ->
489 v_blk 0 [|Tac2ffi.of_int n|]
490 | Var id ->
491 v_blk 1 [|Tac2ffi.of_ident id|]
492 | Meta n ->
493 v_blk 2 [|Tac2ffi.of_int n|]
494 | Evar (evk, args) ->
495 let args = Evd.expand_existential sigma (evk, args) in
496 v_blk 3 [|
497 Tac2ffi.of_evar evk;
498 Tac2ffi.of_array Tac2ffi.of_constr (Array.of_list args);
499 |]
500 | Sort s ->
501 v_blk 4 [|Tac2ffi.of_sort s|]
502 | Cast (c, k, t) ->
503 v_blk 5 [|
504 Tac2ffi.of_constr c;
505 Tac2ffi.of_cast k;
506 Tac2ffi.of_constr t;
507 |]
508 | Prod (na, t, u) ->
509 v_blk 6 [|
510 of_binder (na, t);
511 Tac2ffi.of_constr u;
512 |]
513 | Lambda (na, t, c) ->
514 v_blk 7 [|
515 of_binder (na, t);
516 Tac2ffi.of_constr c;
517 |]
518 | LetIn (na, b, t, c) ->
519 v_blk 8 [|
520 of_binder (na, t);
521 Tac2ffi.of_constr b;
522 Tac2ffi.of_constr c;
523 |]
524 | App (c, cl) ->
525 v_blk 9 [|
526 Tac2ffi.of_constr c;
527 Tac2ffi.of_array Tac2ffi.of_constr cl;
528 |]
529 | Const (cst, u) ->
530 v_blk 10 [|
531 Tac2ffi.of_constant cst;
532 Tac2ffi.of_instance u;
533 |]
534 | Ind (ind, u) ->
535 v_blk 11 [|
536 Tac2ffi.of_inductive ind;
537 Tac2ffi.of_instance u;
538 |]
539 | Construct (cstr, u) ->
540 v_blk 12 [|
541 Tac2ffi.of_constructor cstr;
542 Tac2ffi.of_instance u;
543 |]
544 | Case (ci, u, pms, c, iv, t, bl) ->
545 (* FIXME: also change representation Ltac2-side? *)
546 let (ci, c, iv, t, bl) = EConstr.expand_case env sigma (ci, u, pms, c, iv, t, bl) in
547 let c = on_snd (EConstr.ERelevance.kind sigma) c in
548 v_blk 13 [|
549 Tac2ffi.of_case ci;
550 Tac2ffi.(of_pair of_constr of_relevance c);
551 of_case_invert iv;
552 Tac2ffi.of_constr t;
553 Tac2ffi.of_array Tac2ffi.of_constr bl;
554 |]
555 | Fix ((recs, i), def) ->
556 let (nas, cs) = of_rec_declaration def in
557 v_blk 14 [|
558 Tac2ffi.of_array Tac2ffi.of_int recs;
559 Tac2ffi.of_int i;
560 nas;
561 cs;
562 |]
563 | CoFix (i, def) ->
564 let (nas, cs) = of_rec_declaration def in
565 v_blk 15 [|
566 Tac2ffi.of_int i;
567 nas;
568 cs;
569 |]
570 | Proj (p, r, c) ->
571 v_blk 16 [|
572 Tac2ffi.of_projection p;
573 of_relevance (EConstr.ERelevance.kind sigma r);
574 Tac2ffi.of_constr c;
575 |]
576 | Int n ->
577 v_blk 17 [|Tac2ffi.of_uint63 n|]
578 | Float f ->
579 v_blk 18 [|Tac2ffi.of_float f|]
580 | String s ->
581 v_blk 19 [|Tac2ffi.of_pstring s|]
582 | Array(u,t,def,ty) ->
583 v_blk 20 [|
584 of_instance u;
585 Tac2ffi.of_array Tac2ffi.of_constr t;
586 Tac2ffi.of_constr def;
587 Tac2ffi.of_constr ty;
588 |]
589
590 let () =
591 define "constr_make" (valexpr @-> eret constr) @@ fun knd env sigma ->
592 match Tac2ffi.to_block knd with
593 | (0, [|n|]) ->
594 let n = Tac2ffi.to_int n in
595 EConstr.mkRel n
596 | (1, [|id|]) ->
597 let id = Tac2ffi.to_ident id in
598 EConstr.mkVar id
599 | (2, [|n|]) ->
600 let n = Tac2ffi.to_int n in
601 EConstr.mkMeta n
602 | (3, [|evk; args|]) ->
603 let evk = to_evar evk in
604 let args = Tac2ffi.to_array Tac2ffi.to_constr args in
605 EConstr.mkLEvar sigma (evk, Array.to_list args)
606 | (4, [|s|]) ->
607 let s = Tac2ffi.to_sort s in
608 EConstr.mkSort s
609 | (5, [|c; k; t|]) ->
610 let c = Tac2ffi.to_constr c in
611 let k = Tac2ffi.to_cast k in
612 let t = Tac2ffi.to_constr t in
613 EConstr.mkCast (c, k, t)
614 | (6, [|na; u|]) ->
615 let (na, t) = to_binder na in
616 let u = Tac2ffi.to_constr u in
617 EConstr.mkProd (na, t, u)
618 | (7, [|na; c|]) ->
619 let (na, t) = to_binder na in
620 let u = Tac2ffi.to_constr c in
621 EConstr.mkLambda (na, t, u)
622 | (8, [|na; b; c|]) ->
623 let (na, t) = to_binder na in
624 let b = Tac2ffi.to_constr b in
625 let c = Tac2ffi.to_constr c in
626 EConstr.mkLetIn (na, b, t, c)
627 | (9, [|c; cl|]) ->
628 let c = Tac2ffi.to_constr c in
629 let cl = Tac2ffi.to_array Tac2ffi.to_constr cl in
630 EConstr.mkApp (c, cl)
631 | (10, [|cst; u|]) ->
632 let cst = Tac2ffi.to_constant cst in
633 let u = to_instance u in
634 EConstr.mkConstU (cst, u)
635 | (11, [|ind; u|]) ->
636 let ind = Tac2ffi.to_inductive ind in
637 let u = to_instance u in
638 EConstr.mkIndU (ind, u)
639 | (12, [|cstr; u|]) ->
640 let cstr = Tac2ffi.to_constructor cstr in
641 let u = to_instance u in
642 EConstr.mkConstructU (cstr, u)
643 | (13, [|ci; c; iv; t; bl|]) ->
644 let ci = Tac2ffi.to_case ci in
645 let c = Tac2ffi.(to_pair to_constr to_relevance c) in
646 let c = on_snd EConstr.ERelevance.make c in
647 let iv = to_case_invert iv in
648 let t = Tac2ffi.to_constr t in
649 let bl = Tac2ffi.to_array Tac2ffi.to_constr bl in
650 EConstr.mkCase (EConstr.contract_case env sigma (ci, c, iv, t, bl))
651 | (14, [|recs; i; nas; cs|]) ->
652 let recs = Tac2ffi.to_array Tac2ffi.to_int recs in
653 let i = Tac2ffi.to_int i in
654 let def = to_rec_declaration (nas, cs) in
655 EConstr.mkFix ((recs, i), def)
656 | (15, [|i; nas; cs|]) ->
657 let i = Tac2ffi.to_int i in
658 let def = to_rec_declaration (nas, cs) in
659 EConstr.mkCoFix (i, def)
660 | (16, [|p; r; c|]) ->
661 let p = Tac2ffi.to_projection p in
662 let r = to_relevance r in
663 let c = Tac2ffi.to_constr c in
664 EConstr.mkProj (p, EConstr.ERelevance.make r, c)
665 | (17, [|n|]) ->
666 let n = Tac2ffi.to_uint63 n in
667 EConstr.mkInt n
668 | (18, [|f|]) ->
669 let f = Tac2ffi.to_float f in
670 EConstr.mkFloat f
671 | (19, [|s|]) ->
672 let s = Tac2ffi.to_pstring s in
673 EConstr.mkString s
674 | (20, [|u;t;def;ty|]) ->
675 let t = Tac2ffi.to_array Tac2ffi.to_constr t in
676 let def = Tac2ffi.to_constr def in
677 let ty = Tac2ffi.to_constr ty in
678 let u = to_instance u in
679 EConstr.mkArray(u,t,def,ty)
680 | _ -> assert false
681
682 let () =
683 define "constr_check" (constr @-> tac valexpr) @@ fun c ->
684 pf_apply @@ fun env sigma ->
685 try
686 let (sigma, _) = Typing.type_of env sigma c in
687 Proofview.Unsafe.tclEVARS sigma >>= fun () ->
688 return (of_result Tac2ffi.of_constr (Inl c))
689 with e when CErrors.noncritical e ->
690 let e = Exninfo.capture e in
691 return (of_result Tac2ffi.of_constr (Inr e))
692
693 let () =
694 define "constr_liftn" (int @-> int @-> constr @-> ret constr)
695 EConstr.Vars.liftn
696
697 let () =
698 define "constr_substnl" (list constr @-> int @-> constr @-> ret constr)
699 EConstr.Vars.substnl
700
701 let () =
702 define "constr_closenl" (list ident @-> int @-> constr @-> tac constr)
703 @@ fun ids k c ->
704 Proofview.tclEVARMAP >>= fun sigma ->
705 return (EConstr.Vars.substn_vars sigma k ids c)
706
707 let () =
708 define "constr_closedn" (int @-> constr @-> tac bool) @@ fun n c ->
709 Proofview.tclEVARMAP >>= fun sigma ->
710 return (EConstr.Vars.closedn sigma n c)
711
712 let () =
713 define "constr_noccur_between" (int @-> int @-> constr @-> tac bool) @@ fun n m c ->
714 Proofview.tclEVARMAP >>= fun sigma ->
715 return (EConstr.Vars.noccur_between sigma n m c)
716
717 let () =
718 define "constr_case" (inductive @-> tac valexpr) @@ fun ind ->
719 Proofview.tclENV >>= fun env ->
720 try
721 let ans = Inductiveops.make_case_info env ind Constr.RegularStyle in
722 return (Tac2ffi.of_case ans)
723 with e when CErrors.noncritical e ->
724 throw Tac2ffi.err_notfound
725
726 let () =
727 define "case_to_inductive" (case @-> ret inductive) @@ fun case ->
728 case.ci_ind
729
730 let () = define "constr_cast_default" (ret valexpr) (of_cast DEFAULTcast)
731 let () = define "constr_cast_vm" (ret valexpr) (of_cast VMcast)
732 let () = define "constr_cast_native" (ret valexpr) (of_cast NATIVEcast)
733
734 let () =
735 define "constr_in_context" (ident @-> constr @-> thunk unit @-> tac constr) @@ fun id t c ->
736 Proofview.Goal.goals >>= function
737 | [gl] ->
738 gl >>= fun gl ->
739 let env = Proofview.Goal.env gl in
740 let sigma = Proofview.Goal.sigma gl in
741 let has_var =
742 try
743 let _ = Environ.lookup_named id env in
744 true
745 with Not_found -> false
746 in
747 if has_var then
748 Tacticals.tclZEROMSG (str "Variable already exists")
749 else
750 let open Context.Named.Declaration in
751 let sigma, t_rel =
752 let t_ty = Retyping.get_type_of env sigma t in
753 (* If the user passed eg ['_] for the type we force it to indeed be a type *)
754 let sigma, j = Typing.type_judgment env sigma {uj_val=t; uj_type=t_ty} in
755 sigma, EConstr.ESorts.relevance_of_sort j.utj_type
756 in
757 let nenv = EConstr.push_named (LocalAssum (Context.make_annot id t_rel, t)) env in
758 let (sigma, (evt, s)) = Evarutil.new_type_evar nenv sigma Evd.univ_flexible in
759 let relevance = EConstr.ESorts.relevance_of_sort s in
760 let (sigma, evk) = Evarutil.new_pure_evar (Environ.named_context_val nenv) sigma ~relevance evt in
761 Proofview.Unsafe.tclEVARS sigma >>= fun () ->
762 Proofview.Unsafe.tclSETGOALS [Proofview.with_empty_state evk] >>= fun () ->
763 thaw c >>= fun _ ->
764 Proofview.Unsafe.tclSETGOALS [Proofview.goal_with_state (Proofview.Goal.goal gl) (Proofview.Goal.state gl)] >>= fun () ->
765 let args = EConstr.identity_subst_val (Environ.named_context_val env) in
766 let args = SList.cons (EConstr.mkRel 1) args in
767 let ans = EConstr.mkEvar (evk, args) in
768 return (EConstr.mkLambda (Context.make_annot (Name id) t_rel, t, ans))
769 | _ ->
770 throw Tac2ffi.err_notfocussed
771
772 (** preterm -> constr *)
773
774 let () = define "constr_flags" (ret pretype_flags) constr_flags
775
776 let () =
777 define "pretype_flags_set_use_coercions"
778 (bool @-> pretype_flags @-> ret pretype_flags) @@ fun b flags ->
779 { flags with use_coercions = b }
780
781 let () =
782 define "pretype_flags_set_use_typeclasses"
783 (bool @-> pretype_flags @-> ret pretype_flags) @@ fun b flags ->
784 { flags with use_typeclasses = if b then UseTC else NoUseTC }
785
786 let () =
787 define "pretype_flags_set_allow_evars"
788 (bool @-> pretype_flags @-> ret pretype_flags) @@ fun b flags ->
789 { flags with fail_evar = not b }
790
791 let () =
792 define "pretype_flags_set_nf_evars"
793 (bool @-> pretype_flags @-> ret pretype_flags) @@ fun b flags ->
794 { flags with expand_evars = b }
795
796 let () = define "expected_istype" (ret expected_type) IsType
797
798 let () = define "expected_oftype" (constr @-> ret expected_type) @@ fun c ->
799 OfType c
800
801 let () = define "expected_without_type_constraint" (ret expected_type)
802 WithoutTypeConstraint
803
804 let () =
805 define "constr_pretype" (pretype_flags @-> expected_type @-> preterm @-> tac constr) @@ fun flags expected_type c ->
806 let pretype env sigma =
807 let sigma, t = Pretyping.understand_uconstr ~flags ~expected_type env sigma c in
808 Proofview.Unsafe.tclEVARS sigma <*> Proofview.tclUNIT t
809 in
810 pf_apply ~catch_exceptions:true pretype
811
812 let () =
813 define "constr_binder_make" (option ident @-> constr @-> tac binder) @@ fun na ty ->
814 pf_apply @@ fun env sigma ->
815 match Retyping.relevance_of_type env sigma ty with
816 | rel ->
817 let na = match na with None -> Anonymous | Some id -> Name id in
818 return (Context.make_annot na rel, ty)
819 | exception (Retyping.RetypeError _ as e) ->
820 let e, info = Exninfo.capture e in
821 fail ~info (CErrors.UserError Pp.(str "Not a type."))
822
823 let () =
824 define "constr_binder_unsafe_make"
825 (option ident @-> relevance @-> constr @-> ret binder)
826 @@ fun na rel ty ->
827 let na = match na with None -> Anonymous | Some id -> Name id in
828 Context.make_annot na (EConstr.ERelevance.make rel), ty
829
830 let () =
831 define "constr_binder_name" (binder @-> ret (option ident)) @@ fun (bnd, _) ->
832 match bnd.Context.binder_name with Anonymous -> None | Name id -> Some id
833
834 let () =
835 define "constr_binder_type" (binder @-> ret constr) @@ fun (_, ty) -> ty
836
837 let () =
838 define "constr_binder_relevance" (binder @-> ret relevance) @@ fun (na, _) ->
839 EConstr.Unsafe.to_relevance na.binder_relevance
840
841 let () =
842 define "constr_relevance_equal" (relevance @-> relevance @-> eret bool) @@ fun r1 r2 _ sigma ->
843 EConstr.ERelevance.(equal sigma (make r1) (make r2))
844
845 let () = define "constr_relevance_relevant" (ret relevance) Sorts.Relevant
846
847 let () = define "constr_relevance_irrelevant" (ret relevance) Sorts.Irrelevant
848
849 let () =
850 define "constr_has_evar" (constr @-> tac bool) @@ fun c ->
851 Proofview.tclEVARMAP >>= fun sigma ->
852 return (Evarutil.has_undefined_evars sigma c)
853
854 (** Uint63 *)
855
856 let () = define "uint63_compare" (uint63 @-> uint63 @-> ret int) Uint63.compare
857
858 let () = define "uint63_of_int" (int @-> ret uint63) Uint63.of_int
859
860 let () = define "uint63_print" (uint63 @-> ret pp) @@ fun i ->
861 Pp.str (Uint63.to_string i)
862
863 (** Extra equalities *)
864
865 let () = define "evar_equal" (evar @-> evar @-> ret bool) Evar.equal
866 let () = define "float_equal" (float @-> float @-> ret bool) Float64.equal
867 let () = define "uint63_equal" (uint63 @-> uint63 @-> ret bool) Uint63.equal
868 let () = define "meta_equal" (int @-> int @-> ret bool) Int.equal
869 let () = define "constr_cast_equal" (cast @-> cast @-> ret bool) Glob_ops.cast_kind_eq
870
871 let () =
872 define "constant_equal"
873 (constant @-> constant @-> ret bool)
874 Constant.UserOrd.equal
875 let () =
876 define "constr_case_equal" (case @-> case @-> ret bool) @@ fun x y ->
877 Ind.UserOrd.equal x.ci_ind y.ci_ind
878 let () =
879 define "constructor_equal" (constructor @-> constructor @-> ret bool) Construct.UserOrd.equal
880 let () =
881 define "projection_equal" (projection @-> projection @-> ret bool) Projection.UserOrd.equal
882
883 (** Patterns *)
884
885 let () =
886 define "pattern_empty_context" (ret matching_context)
887 Constr_matching.empty_context
888
889 let () =
890 define "pattern_matches" (pattern @-> constr @-> tac valexpr) @@ fun pat c ->
891 pf_apply @@ fun env sigma ->
892 let ans =
893 try Some (Constr_matching.matches env sigma pat c)
894 with Constr_matching.PatternMatchingFailure -> None
895 in
896 begin match ans with
897 | None -> fail Tac2ffi.err_matchfailure
898 | Some ans ->
899 let ans = Id.Map.bindings ans in
900 let of_pair (id, c) = Tac2ffi.of_tuple [| Tac2ffi.of_ident id; Tac2ffi.of_constr c |] in
901 return (Tac2ffi.of_list of_pair ans)
902 end
903
904 let () =
905 define "pattern_matches_subterm" (pattern @-> constr @-> tac (pair matching_context (list (pair ident constr)))) @@ fun pat c ->
906 let open Constr_matching in
907 let rec of_ans s = match IStream.peek s with
908 | IStream.Nil -> fail Tac2ffi.err_matchfailure
909 | IStream.Cons ({ m_sub = (_, sub); m_ctx }, s) ->
910 let ans = Id.Map.bindings sub in
911 Proofview.tclOR (return (m_ctx, ans)) (fun _ -> of_ans s)
912 in
913 pf_apply @@ fun env sigma ->
914 let ans = Constr_matching.match_subterm env sigma (Id.Set.empty,pat) c in
915 of_ans ans
916
917 let () =
918 define "pattern_matches_vect" (pattern @-> constr @-> tac valexpr) @@ fun pat c ->
919 pf_apply @@ fun env sigma ->
920 let ans =
921 try Some (Constr_matching.matches env sigma pat c)
922 with Constr_matching.PatternMatchingFailure -> None
923 in
924 match ans with
925 | None -> fail Tac2ffi.err_matchfailure
926 | Some ans ->
927 let ans = Id.Map.bindings ans in
928 let ans = Array.map_of_list snd ans in
929 return (Tac2ffi.of_array Tac2ffi.of_constr ans)
930
931 let () =
932 define "pattern_matches_subterm_vect" (pattern @-> constr @-> tac (pair matching_context (array constr))) @@ fun pat c ->
933 let open Constr_matching in
934 let rec of_ans s = match IStream.peek s with
935 | IStream.Nil -> fail Tac2ffi.err_matchfailure
936 | IStream.Cons ({ m_sub = (_, sub); m_ctx }, s) ->
937 let ans = Id.Map.bindings sub in
938 let ans = Array.map_of_list snd ans in
939 Proofview.tclOR (return (m_ctx,ans)) (fun _ -> of_ans s)
940 in
941 pf_apply @@ fun env sigma ->
942 let ans = Constr_matching.match_subterm env sigma (Id.Set.empty,pat) c in
943 of_ans ans
944
945 let match_pattern = map_repr
946 (fun (b,pat) -> if b then Tac2match.MatchPattern pat else Tac2match.MatchContext pat)
947 (function Tac2match.MatchPattern pat -> (true, pat) | MatchContext pat -> (false, pat))
948 (pair bool pattern)
949
950 let () =
951 define "pattern_matches_goal"
952 (bool @-> list (pair (option match_pattern) match_pattern) @-> match_pattern @-> tac valexpr)
953 @@ fun rev hp cp ->
954 assert_focussed >>= fun () ->
955 Proofview.Goal.enter_one @@ fun gl ->
956 let env = Proofview.Goal.env gl in
957 let sigma = Proofview.Goal.sigma gl in
958 let concl = Proofview.Goal.concl gl in
959 Tac2match.match_goal env sigma concl ~rev (hp, cp) >>= fun (hyps, ctx, subst) ->
960 let empty_context = Constr_matching.empty_context in
961 let of_ctxopt ctx = Tac2ffi.of_matching_context (Option.default empty_context ctx) in
962 let hids = Tac2ffi.of_array Tac2ffi.of_ident (Array.map_of_list pi1 hyps) in
963 let hbctx = Tac2ffi.of_array of_ctxopt
964 (Array.of_list (CList.filter_map (fun (_,bctx,_) -> bctx) hyps))
965 in
966 let hctx = Tac2ffi.of_array of_ctxopt (Array.map_of_list pi3 hyps) in
967 let subs = Tac2ffi.of_array Tac2ffi.of_constr (Array.map_of_list snd (Id.Map.bindings subst)) in
968 let cctx = of_ctxopt ctx in
969 let ans = Tac2ffi.of_tuple [| hids; hbctx; hctx; subs; cctx |] in
970 Proofview.tclUNIT ans
971
972 let () =
973 define "pattern_instantiate"
974 (matching_context @-> constr @-> ret constr)
975 Constr_matching.instantiate_context
976
977 (** Error *)
978
979 let () =
980 define "throw" (exn @-> tac valexpr) @@ fun (e, info) -> throw ~info e
981
982 let () =
983 define "throw_bt" (exn @-> exninfo @-> tac valexpr) @@ fun (e,_) info ->
984 Proofview.tclLIFT (Proofview.NonLogical.raise (e, info))
985
986 let () =
987 define "clear_err_info" (err @-> ret err) @@ fun (e,_) -> (e, Exninfo.null)
988
989 let () = define "current_exninfo" (unit @-> tac exninfo) @@ fun () ->
990 return () >>= fun () ->
991 set_bt (Exninfo.reify())
992
993 let () = define "message_of_exninfo" (exninfo @-> ret pp) CErrors.print_extra
994
995 let () = define "print_err" (err @-> ret pp) @@ fun (e,_) -> CErrors.print e
996
997 (** Control *)
998
999 (** exn -> 'a *)
1000 let () =
1001 define "zero" (exn @-> tac valexpr) @@ fun (e, info) -> fail ~info e
1002
1003 let () =
1004 define "zero_bt" (exn @-> exninfo @-> tac valexpr) @@ fun (e,_) info ->
1005 Proofview.tclZERO ~info e
1006
1007 (** (unit -> 'a) -> (exn -> 'a) -> 'a *)
1008 let () =
1009 define "plus" (thunk valexpr @-> fun1 exn valexpr @-> tac valexpr) @@ fun x k ->
1010 Proofview.tclOR (thaw x) k
1011
1012 let () =
1013 define "plus_bt" (thunk valexpr @-> fun2 exn exninfo valexpr @-> tac valexpr) @@ fun run handle ->
1014 Proofview.tclOR (thaw run) (fun e -> handle e (snd e))
1015
1016 (** (unit -> 'a) -> 'a *)
1017 let () =
1018 define "once" (thunk valexpr @-> tac valexpr) @@ fun f ->
1019 Proofview.tclONCE (thaw f)
1020
1021 (** (unit -> 'a) -> ('a * ('exn -> 'a)) result *)
1022 let () =
1023 define "case" (thunk valexpr @-> tac (result (pair valexpr (fun1 exn valexpr)))) @@ fun f ->
1024 Proofview.tclCASE (thaw f) >>= begin function
1025 | Proofview.Next (x, k) ->
1026 let k (e,info) = set_bt info >>= fun info -> k (e,info) in
1027 return (Ok (x, k))
1028 | Proofview.Fail e -> return (Error e)
1029 end
1030
1031 let () =
1032 define "numgoals" (unit @-> tac int) @@ fun () ->
1033 Proofview.numgoals
1034
1035 (** (unit -> unit) list -> unit *)
1036 let () =
1037 define "dispatch" (list (thunk unit) @-> tac unit) @@ fun l ->
1038 let l = List.map (fun f -> thaw f) l in
1039 Proofview.tclDISPATCH l
1040
1041 (** (unit -> unit) list -> (unit -> unit) -> (unit -> unit) list -> unit *)
1042 let () =
1043 define "extend" (list (thunk unit) @-> thunk unit @-> list (thunk unit) @-> tac unit) @@ fun lft tac rgt ->
1044 let lft = List.map (fun f -> thaw f) lft in
1045 let tac = thaw tac in
1046 let rgt = List.map (fun f -> thaw f) rgt in
1047 Proofview.tclEXTEND lft tac rgt
1048
1049 (** (unit -> unit) -> unit *)
1050 let () =
1051 define "enter" (thunk unit @-> tac unit) @@ fun f ->
1052 let f = Proofview.tclIGNORE (thaw f) in
1053 Proofview.tclINDEPENDENT f
1054
1055 (** int -> int -> (unit -> 'a) -> 'a *)
1056 let () =
1057 define "focus" (int @-> int @-> thunk valexpr @-> tac valexpr) @@ fun i j tac ->
1058 Proofview.tclFOCUS i j (thaw tac)
1059
1060 (** int -> unit **)
1061 let () =
1062 define "cycle" (int @-> tac unit) @@ fun i ->
1063 Proofview.cycle i
1064
1065 (** unit -> unit *)
1066 let () = define "shelve" (unit @-> tac unit) @@ fun _ -> Proofview.shelve
1067
1068 (** unit -> unit *)
1069 let () =
1070 define "shelve_unifiable" (unit @-> tac unit) @@ fun _ ->
1071 Proofview.shelve_unifiable
1072
1073 let () =
1074 define "new_goal" (evar @-> tac unit) @@ fun ev ->
1075 Proofview.tclEVARMAP >>= fun sigma ->
1076 if Evd.mem sigma ev then
1077 let sigma = Evd.remove_future_goal sigma ev in
1078 let sigma = Evd.unshelve sigma [ev] in
1079 Proofview.Unsafe.tclEVARS sigma <*>
1080 Proofview.Unsafe.tclNEWGOALS [Proofview.with_empty_state ev] <*>
1081 Proofview.tclUNIT ()
1082 else throw Tac2ffi.err_notfound
1083
1084 let () =
1085 define "unshelve" (thunk valexpr @-> tac valexpr) @@ fun t ->
1086 Proofview.with_shelf (thaw t) >>= fun (gls,v) ->
1087 let gls = List.map Proofview.with_empty_state gls in
1088 Proofview.Unsafe.tclGETGOALS >>= fun ogls ->
1089 Proofview.Unsafe.tclSETGOALS (gls @ ogls) >>= fun () ->
1090 return v
1091
1092 (** unit -> constr *)
1093 let () =
1094 define "goal" (unit @-> tac constr) @@ fun _ ->
1095 assert_focussed >>= fun () ->
1096 Proofview.Goal.enter_one @@ fun gl ->
1097 let sigma = Proofview.Goal.sigma gl in
1098 let concl = Proofview.Goal.concl gl in
1099 return (Reductionops.nf_evar sigma concl)
1100
1101 (** ident -> constr *)
1102 let () =
1103 define "hyp" (ident @-> tac constr) @@ fun id ->
1104 pf_apply @@ fun env _ ->
1105 let mem = try ignore (Environ.lookup_named id env); true with Not_found -> false in
1106 if mem then return (EConstr.mkVar id)
1107 else Tacticals.tclZEROMSG
1108 (str "Hypothesis " ++ quote (Id.print id) ++ str " not found") (* FIXME: Do something more sensible *)
1109
1110 let () =
1111 define "hyp_value" (ident @-> tac (option constr)) @@ fun id ->
1112 pf_apply @@ fun env _ ->
1113 match EConstr.lookup_named id env with
1114 | d -> return (Context.Named.Declaration.get_value d)
1115 | exception Not_found ->
1116 Tacticals.tclZEROMSG
1117 (str "Hypothesis " ++ quote (Id.print id) ++ str " not found") (* FIXME: Do something more sensible *)
1118
1119 let () =
1120 define "hyps" (unit @-> tac valexpr) @@ fun _ ->
1121 pf_apply @@ fun env _ ->
1122 let open Context in
1123 let open Named.Declaration in
1124 let hyps = List.rev (Environ.named_context env) in
1125 let map = function
1126 | LocalAssum (id, t) ->
1127 let t = EConstr.of_constr t in
1128 Tac2ffi.of_tuple [|
1129 Tac2ffi.of_ident id.binder_name;
1130 Tac2ffi.of_option Tac2ffi.of_constr None;
1131 Tac2ffi.of_constr t;
1132 |]
1133 | LocalDef (id, c, t) ->
1134 let c = EConstr.of_constr c in
1135 let t = EConstr.of_constr t in
1136 Tac2ffi.of_tuple [|
1137 Tac2ffi.of_ident id.binder_name;
1138 Tac2ffi.of_option Tac2ffi.of_constr (Some c);
1139 Tac2ffi.of_constr t;
1140 |]
1141 in
1142 return (Tac2ffi.of_list map hyps)
1143
1144 (** (unit -> constr) -> unit *)
1145 let () =
1146 define "refine" (thunk constr @-> tac unit) @@ fun c ->
1147 let c = thaw c >>= fun c -> Proofview.tclUNIT ((), c, None) in
1148 Proofview.Goal.enter @@ fun gl ->
1149 Refine.generic_refine ~typecheck:true c gl
1150
1151 let () = define "solve_constraints" (unit @-> tac unit) @@ fun () -> Refine.solve_constraints
1152
1153 let () =
1154 define "with_holes" (thunk valexpr @-> fun1 valexpr valexpr @-> tac valexpr) @@ fun x f ->
1155 Tacticals.tclRUNWITHHOLES false (thaw x) f
1156
1157 let () =
1158 define "progress" (thunk valexpr @-> tac valexpr) @@ fun f ->
1159 Proofview.tclPROGRESS (thaw f)
1160
1161 let () =
1162 define "abstract" (option ident @-> thunk unit @-> tac unit) @@ fun id f ->
1163 Abstract.tclABSTRACT id (thaw f)
1164
1165 let () =
1166 define "time" (option string @-> thunk valexpr @-> tac valexpr) @@ fun s f ->
1167 Proofview.tclTIME s (thaw f)
1168
1169 let () =
1170 define "timeout" (int @-> thunk valexpr @-> tac valexpr) @@ fun i f ->
1171 Proofview.tclTIMEOUT i (thaw f)
1172
1173 let () =
1174 define "timeoutf" (float @-> thunk valexpr @-> tac valexpr) @@ fun f64 f ->
1175 Proofview.tclTIMEOUTF (Float64.to_float f64) (thaw f)
1176
1177 let () =
1178 define "check_interrupt" (unit @-> tac unit) @@ fun _ ->
1179 Proofview.tclCHECKINTERRUPT
1180
1181 (** Fresh *)
1182
1183 let () = define "fresh_free_empty" (ret free) Nameops.Fresh.empty
1184
1185 let () = define "fresh_free_add" (ident @-> free @-> ret free) Nameops.Fresh.add
1186
1187 let () =
1188 define "fresh_free_union" (free @-> free @-> ret free) Nameops.Fresh.union
1189
1190 let () =
1191 define "fresh_free_of_ids" (list ident @-> ret free) @@ fun ids ->
1192 List.fold_right Nameops.Fresh.add ids Nameops.Fresh.empty
1193
1194 let () =
1195 define "fresh_free_of_constr" (constr @-> tac free) @@ fun c ->
1196 Proofview.tclEVARMAP >>= fun sigma ->
1197 let rec fold accu c =
1198 match EConstr.kind sigma c with
1199 | Constr.Var id -> Nameops.Fresh.add id accu
1200 | _ -> EConstr.fold sigma fold accu c
1201 in
1202 return (fold Nameops.Fresh.empty c)
1203
1204 (* for backwards compat reasons the ocaml and ltac2 APIs
1205 exchange the meaning of "fresh" and "next" *)
1206 let () =
1207 define "fresh_next" (free @-> ident @-> ret (pair ident free)) @@ fun avoid id ->
1208 let id = Namegen.mangle_id id in
1209 Nameops.Fresh.fresh id avoid
1210
1211 let () =
1212 define "fresh_fresh" (free @-> ident @-> ret ident) @@ fun avoid id ->
1213 let id = Namegen.mangle_id id in
1214 Nameops.Fresh.next id avoid
1215
1216 (** Env *)
1217
1218 let () =
1219 define "env_get" (list ident @-> ret (option reference)) @@ fun ids ->
1220 match ids with
1221 | [] -> None
1222 | _ :: _ as ids ->
1223 let (id, path) = List.sep_last ids in
1224 let path = DirPath.make (List.rev path) in
1225 let fp = Libnames.make_path path id in
1226 try Some (Nametab.global_of_path fp) with Not_found -> None
1227
1228 let () =
1229 define "env_expand" (list ident @-> ret (list reference)) @@ fun ids ->
1230 match ids with
1231 | [] -> []
1232 | _ :: _ as ids ->
1233 let (id, path) = List.sep_last ids in
1234 let path = DirPath.make (List.rev path) in
1235 let qid = Libnames.make_qualid path id in
1236 Nametab.locate_all qid
1237
1238 let () =
1239 define "env_path" (reference @-> tac (list ident)) @@ fun r ->
1240 match Nametab.path_of_global r with
1241 | fp ->
1242 let (path, id) = Libnames.repr_path fp in
1243 let path = DirPath.repr path in
1244 return (List.rev_append path [id])
1245 | exception Not_found ->
1246 throw Tac2ffi.err_notfound
1247
1248 let () =
1249 define "env_instantiate" (reference @-> tac constr) @@ fun r ->
1250 Proofview.tclENV >>= fun env ->
1251 Proofview.tclEVARMAP >>= fun sigma ->
1252 let (sigma, c) = Evd.fresh_global env sigma r in
1253 Proofview.Unsafe.tclEVARS sigma >>= fun () ->
1254 return c
1255
1256 (** Ind *)
1257
1258 let () =
1259 define "ind_equal" (inductive @-> inductive @-> ret bool) Ind.UserOrd.equal
1260
1261 let () =
1262 define "ind_data"
1263 (inductive @-> tac ind_data)
1264 @@ fun ind ->
1265 Proofview.tclENV >>= fun env ->
1266 if Environ.mem_mind (fst ind) env then
1267 return (ind, Environ.lookup_mind (fst ind) env)
1268 else
1269 throw Tac2ffi.err_notfound
1270
1271 let () = define "ind_repr" (ind_data @-> ret inductive) fst
1272 let () = define "ind_index" (inductive @-> ret int) snd
1273
1274 let () =
1275 define "ind_nblocks" (ind_data @-> ret int) @@ fun (_, mib) ->
1276 Array.length mib.Declarations.mind_packets
1277
1278 let () =
1279 define "ind_nconstructors" (ind_data @-> ret int) @@ fun ((_, n), mib) ->
1280 Array.length Declarations.(mib.mind_packets.(n).mind_consnames)
1281
1282 let () =
1283 define "ind_get_block"
1284 (ind_data @-> int @-> tac ind_data)
1285 @@ fun (ind, mib) n ->
1286 if 0 <= n && n < Array.length mib.Declarations.mind_packets then
1287 return ((fst ind, n), mib)
1288 else throw Tac2ffi.err_notfound
1289
1290 let () =
1291 define "ind_get_constructor"
1292 (ind_data @-> int @-> tac constructor)
1293 @@ fun ((mind, n), mib) i ->
1294 let open Declarations in
1295 let ncons = Array.length mib.mind_packets.(n).mind_consnames in
1296 if 0 <= i && i < ncons then
1297 (* WARNING: In the ML API constructors are indexed from 1 for historical
1298 reasons, but Ltac2 uses 0-indexing instead. *)
1299 return ((mind, n), i + 1)
1300 else throw Tac2ffi.err_notfound
1301
1302 let () =
1303 define "ind_get_nparams"
1304 (ind_data @-> ret int) @@ fun (_, mib) ->
1305 mib.Declarations.mind_nparams
1306
1307 let () =
1308 define "ind_get_nparams_rec"
1309 (ind_data @-> ret int) @@ fun (_, mib) ->
1310 mib.Declarations.mind_nparams_rec
1311
1312 let () =
1313 define "constructor_inductive"
1314 (constructor @-> ret inductive)
1315 @@ fun (ind, _) -> ind
1316
1317 let () =
1318 define "constructor_index"
1319 (constructor @-> ret int)
1320 @@ fun (_, i) ->
1321 (* WARNING: ML constructors are 1-indexed but Ltac2 constructors are 0-indexed *)
1322 i-1
1323
1324 let () =
1325 define "constructor_nargs"
1326 (ind_data @-> ret (array int)) @@ fun ((_,i),mib) ->
1327 let open Declarations in
1328 mib.mind_packets.(i).mind_consnrealargs
1329
1330 let () =
1331 define "constructor_ndecls"
1332 (ind_data @-> ret (array int)) @@ fun ((_,i),mib) ->
1333 let open Declarations in
1334 mib.mind_packets.(i).mind_consnrealdecls
1335
1336 let () =
1337 define "ind_get_projections" (ind_data @-> ret (option (array projection)))
1338 @@ fun (ind,mib) ->
1339 Declareops.inductive_make_projections ind mib
1340 |> Option.map fst
1341 |> Option.map (Array.map (fun (p,_) -> Projection.make p false))
1342
1343 (** Proj *)
1344
1345 let () =
1346 define "projection_ind" (projection @-> ret inductive) Projection.inductive
1347
1348 let () =
1349 define "projection_index" (projection @-> ret int) Projection.arg
1350
1351 let () =
1352 define "projection_unfolded" (projection @-> ret bool) Projection.unfolded
1353
1354 let () =
1355 define "projection_set_unfolded" (projection @-> bool @-> ret projection) @@ fun p b ->
1356 Projection.make (Projection.repr p) b
1357
1358 let () =
1359 define "projection_of_constant" (constant @-> ret (option projection)) @@ fun c ->
1360 Structures.PrimitiveProjections.find_opt c |> Option.map (fun p -> Projection.make p false)
1361
1362 let () =
1363 define "projection_to_constant" (projection @-> ret (option constant)) @@ fun p ->
1364 Some (Projection.constant p)
1365
1366 let () = define "module_equal" (modpath @-> modpath @-> ret bool) @@ fun a b ->
1367 ModPath.equal a b
1368
1369 let () = define "module_to_message" (modpath @-> ret pp) @@ fun m ->
1370 (* XXX use ModPath.print instead? (nametab is ambiguous since there's no single nametab)
1371 or expose ModPath.print as a separate external? *)
1372 try Nametab.Modules.pr m
1373 with Not_found ->
1374 try Nametab.ModTypes.pr m
1375 with Not_found ->
1376 try Nametab.OpenMods.pr (DirOpenModule m)
1377 with Not_found ->
1378 try Nametab.OpenMods.pr (DirOpenModtype m)
1379 with Not_found ->
1380 CErrors.anomaly Pp.(str "Unknown module or modtype " ++ ModPath.print m)
1381
1382 let is_openmod m =
1383 ModPath.subpath m (Global.current_modpath())
1384
1385 (* Find info about open module [m] in [senv_l] describing the open
1386 modules of some safe env with current module [senv_m].
1387 Returns [None] if [m] is the library, [Some v] if [m] is some inner open module. *)
1388 let rec find_openmod m senv_m senv_l =
1389 let open ModPath in
1390 match senv_m, senv_l with
1391 | MPbound _, _ -> assert false
1392 | MPfile _, [] -> assert (ModPath.equal m senv_m); None
1393 | MPfile _, _ :: _ -> assert false
1394 | MPdot (m0, _), is_modtype :: rest ->
1395 if ModPath.equal m senv_m then Some is_modtype
1396 else find_openmod m m0 rest
1397 | MPdot _, [] -> assert false
1398
1399 (* Assuming [m] is currently open, tell whether it is modtype. *)
1400 let open_module_is_modtype m =
1401 let senv = Global.safe_env() in
1402 match find_openmod m (Safe_typing.current_modpath senv) (Safe_typing.module_is_modtype senv) with
1403 | None -> false
1404 | Some b -> b
1405
1406 let open_module_is_functor m =
1407 let senv = Global.safe_env() in
1408 match find_openmod m (Safe_typing.current_modpath senv) (Safe_typing.module_num_parameters senv) with
1409 | None -> false
1410 | Some nparams -> not (Int.equal nparams 0)
1411
1412 let () = define "module_is_modtype" (modpath @-> eret bool) @@ fun m env _ ->
1413 if is_openmod m then open_module_is_modtype m
1414 else
1415 try ignore (Environ.lookup_modtype m env); true
1416 with Not_found -> false
1417
1418 let () = define "module_is_functor" (modpath @-> eret bool) @@ fun m env _ ->
1419 if is_openmod m then open_module_is_functor m
1420 else
1421 let modbody_is_functor m = match Mod_declarations.mod_type m with
1422 | NoFunctor _ -> false
1423 | MoreFunctor _ -> true
1424 in
1425 match Environ.lookup_module m env with
1426 | m -> modbody_is_functor m
1427 | exception Not_found -> match Environ.lookup_modtype m env with
1428 | m -> modbody_is_functor m
1429 | exception Not_found -> assert false
1430
1431 let () = define "module_is_bound_module" (modpath @-> ret bool) @@ function
1432 | MPbound _ -> true
1433 | MPfile _ | MPdot _ -> false
1434
1435 let () = define "module_is_library" (modpath @-> ret bool) @@ function
1436 | MPfile _ -> true
1437 | MPbound _ | MPdot _ -> false
1438
1439 let () = define "module_is_open" (modpath @-> ret bool) is_openmod
1440
1441 let () = define "module_parent_module" (modpath @-> ret (option modpath)) @@ function
1442 | MPdot (m, _) -> Some m
1443 | MPbound _ | MPfile _ -> None
1444
1445 let () = define "module_of_reference" (reference @-> tac modpath) @@ function
1446 | VarRef _ -> throw (Invalid_argument "module_of_reference")
1447 | ConstRef c -> return (Constant.modpath c)
1448 | IndRef (mind,_) | ConstructRef ((mind,_),_) -> return (MutInd.modpath mind)
1449
1450 let () = define "current_module" (unit @-> ret modpath) @@ fun () ->
1451 Global.current_modpath()
1452
1453 let () = define "module_loaded_libraries" (unit @-> ret (list modpath)) @@ fun () ->
1454 List.map (fun dp -> MPfile dp) (Library.loaded_libraries())
1455
1456 let module_field_handler = triple (fun1 modpath valexpr) (fun1 reference valexpr) (fun1 unit valexpr)
1457
1458 let () = define "module_field_handle" (module_field @-> module_field_handler @-> tac valexpr) @@ fun f handler ->
1459 let (handle_submodule, handle_reference, handle_rewrule) = handler in
1460 match f with
1461 | Ref x -> handle_reference x
1462 | Submodule x -> handle_submodule x
1463 | Rewrule -> handle_rewrule ()
1464
1465 let openmod_revstruct m senv =
1466 let rec close senv modtype =
1467 let curm = Safe_typing.current_modpath senv in
1468 if ModPath.equal m curm then senv
1469 else
1470 let l = match curm with
1471 | MPdot (_, l) -> l
1472 | _ -> assert false
1473 in
1474 match modtype with
1475 | [] -> assert false
1476 | false :: modtype ->
1477 (* None: type constraint of submodule doesn't matter since we
1478 will anyway only return "Submodule M" and not look at its
1479 contents *)
1480 close (snd @@ Safe_typing.end_module l None senv) modtype
1481 | true :: modtype -> close (snd @@ Safe_typing.end_modtype l senv) modtype
1482 in
1483 let modtype = Safe_typing.module_is_modtype senv in
1484 let senv = close senv modtype in
1485 Safe_typing.structure_body_of_safe_env senv
1486
1487 let () = define "module_contents" (modpath @-> ret (option (list module_field))) @@ fun m ->
1488 let body =
1489 if is_openmod m then
1490 (* XXX not sure what this does with side effects *)
1491 Some (List.rev (openmod_revstruct m (Global.safe_env())))
1492 else
1493 match Environ.lookup_module m (Global.env()) with
1494 | exception Not_found -> (* modtype *) None
1495 | body -> match Mod_declarations.mod_type body with
1496 | MoreFunctor _ -> (* functor *) None
1497 | NoFunctor body -> Some body
1498 in
1499 let to_field (lab, f) : ModField.t = match (f:_ Declarations.structure_field_body) with
1500 | SFBconst _ ->
1501 let kn = KerName.make m lab in
1502 Ref (ConstRef (Global.constant_of_delta_kn kn))
1503 | SFBmind _ ->
1504 let kn = KerName.make m lab in
1505 Ref (IndRef ((Global.mind_of_delta_kn kn, 0)))
1506 | SFBrules _ -> Rewrule
1507 | SFBmodule _ -> Submodule (MPdot (m, lab))
1508 | SFBmodtype _ -> Submodule (MPdot (m, lab))
1509 in
1510 Option.map (List.map to_field) body
1511
1512 module MapTagDyn = Dyn.Make()
1513
1514 type ('a,'set,'map) map_tag = ('a * 'set * 'map) MapTagDyn.tag
1515
1516 type any_map_tag = Any : _ map_tag -> any_map_tag
1517 type tagged_set = TaggedSet : (_,'set,_) map_tag * 'set -> tagged_set
1518 type tagged_map = TaggedMap : (_,_,'map) map_tag * 'map -> tagged_map
1519
1520 let map_tag_ext : any_map_tag Tac2dyn.Val.tag = Tac2dyn.Val.create "fmap_tag"
1521 let map_tag_repr = Tac2ffi.repr_ext map_tag_ext
1522
1523 let set_ext : tagged_set Tac2dyn.Val.tag = Tac2dyn.Val.create "fset"
1524 let set_repr = Tac2ffi.repr_ext set_ext
1525 let tag_set tag s = Tac2ffi.repr_of set_repr (TaggedSet (tag,s))
1526
1527 let map_ext : tagged_map Tac2dyn.Val.tag = Tac2dyn.Val.create "fmap"
1528 let map_repr = Tac2ffi.repr_ext map_ext
1529 let tag_map tag m = Tac2ffi.repr_of map_repr (TaggedMap (tag,m))
1530
1531 module type MapType = sig
1532 (* to have less boilerplate we use S.elt rather than declaring a toplevel type t *)
1533 module S : CSig.USetS
1534 module M : CMap.UExtS with type key = S.elt and module Set := S
1535 type valmap
1536 val valmap_eq : (valmap, valexpr M.t) Util.eq
1537 val repr : S.elt Tac2ffi.repr
1538 end
1539
1540 module MapTypeV = struct
1541 type _ t = Map : (module MapType with type S.elt = 't and type S.t = 'set and type valmap = 'map)
1542 -> ('t * 'set * 'map) t
1543 end
1544
1545 module MapMap = MapTagDyn.Map(MapTypeV)
1546
1547 let maps = ref MapMap.empty
1548
1549 let register_map ?(plugin=ltac2_plugin) ~tag_name x =
1550 let tag = MapTagDyn.create (plugin^":"^tag_name) in
1551 let () = maps := MapMap.add tag (Map x) !maps in
1552 let () = define ~plugin tag_name (ret map_tag_repr) (Any tag) in
1553 tag
1554
1555 let get_map (type t s m) (tag:(t,s,m) map_tag)
1556 : (module MapType with type S.elt = t and type S.t = s and type valmap = m) =
1557 let Map v = MapMap.find tag !maps in
1558 v
1559
1560 let map_tag_eq (type a b c a' b' c') (t1:(a,b,c) map_tag) (t2:(a',b',c') map_tag)
1561 : (a*b*c,a'*b'*c') Util.eq option
1562 = MapTagDyn.eq t1 t2
1563
1564 let assert_map_tag_eq t1 t2 = match map_tag_eq t1 t2 with
1565 | Some v -> v
1566 | None -> assert false
1567
1568 let ident_map_tag : _ map_tag = register_map ~tag_name:"fmap_ident_tag" (module struct
1569 module S = Id.Set
1570 module M = Id.Map
1571 let repr = Tac2ffi.ident
1572 type valmap = valexpr M.t
1573 let valmap_eq = Refl
1574 end)
1575
1576 let int_map_tag : _ map_tag = register_map ~tag_name:"fmap_int_tag" (module struct
1577 module S = Int.Set
1578 module M = Int.Map
1579 let repr = Tac2ffi.int
1580 type valmap = valexpr M.t
1581 let valmap_eq = Refl
1582 end)
1583
1584 let string_map_tag : _ map_tag = register_map ~tag_name:"fmap_string_tag" (module struct
1585 module S = String.Set
1586 module M = String.Map
1587 let repr = Tac2ffi.string
1588 type valmap = valexpr M.t
1589 let valmap_eq = Refl
1590 end)
1591
1592 let inductive_map_tag : _ map_tag = register_map ~tag_name:"fmap_inductive_tag" (module struct
1593 module S = Indset_env
1594 module M = Indmap_env
1595 let repr = inductive
1596 type valmap = valexpr M.t
1597 let valmap_eq = Refl
1598 end)
1599
1600 let constructor_map_tag : _ map_tag = register_map ~tag_name:"fmap_constructor_tag" (module struct
1601 module S = Constrset_env
1602 module M = Constrmap_env
1603 let repr = Tac2ffi.constructor
1604 type valmap = valexpr M.t
1605 let valmap_eq = Refl
1606 end)
1607
1608 let constant_map_tag : _ map_tag = register_map ~tag_name:"fmap_constant_tag" (module struct
1609 module S = Cset_env
1610 module M = Cmap_env
1611 let repr = Tac2ffi.constant
1612 type valmap = valexpr M.t
1613 let valmap_eq = Refl
1614 end)
1615
1616 let () =
1617 define "fset_empty" (map_tag_repr @-> ret valexpr) @@ fun (Any tag) ->
1618 let (module V) = get_map tag in
1619 tag_set tag V.S.empty
1620
1621 let () =
1622 define "fset_is_empty" (set_repr @-> ret bool) @@ fun (TaggedSet (tag,s)) ->
1623 let (module V) = get_map tag in
1624 V.S.is_empty s
1625
1626 let () =
1627 define "fset_mem" (valexpr @-> set_repr @-> ret bool) @@ fun x (TaggedSet (tag,s)) ->
1628 let (module V) = get_map tag in
1629 V.S.mem (repr_to V.repr x) s
1630
1631 let () =
1632 define "fset_add" (valexpr @-> set_repr @-> ret valexpr) @@ fun x (TaggedSet (tag,s)) ->
1633 let (module V) = get_map tag in
1634 tag_set tag (V.S.add (repr_to V.repr x) s)
1635
1636 let () =
1637 define "fset_remove" (valexpr @-> set_repr @-> ret valexpr) @@ fun x (TaggedSet (tag,s)) ->
1638 let (module V) = get_map tag in
1639 tag_set tag (V.S.remove (repr_to V.repr x) s)
1640
1641 let () =
1642 define "fset_union" (set_repr @-> set_repr @-> ret valexpr)
1643 @@ fun (TaggedSet (tag,s1)) (TaggedSet (tag',s2)) ->
1644 let Refl = assert_map_tag_eq tag tag' in
1645 let (module V) = get_map tag in
1646 tag_set tag (V.S.union s1 s2)
1647
1648 let () =
1649 define "fset_inter" (set_repr @-> set_repr @-> ret valexpr)
1650 @@ fun (TaggedSet (tag,s1)) (TaggedSet (tag',s2)) ->
1651 let Refl = assert_map_tag_eq tag tag' in
1652 let (module V) = get_map tag in
1653 tag_set tag (V.S.inter s1 s2)
1654
1655 let () =
1656 define "fset_diff" (set_repr @-> set_repr @-> ret valexpr)
1657 @@ fun (TaggedSet (tag,s1)) (TaggedSet (tag',s2)) ->
1658 let Refl = assert_map_tag_eq tag tag' in
1659 let (module V) = get_map tag in
1660 tag_set tag (V.S.diff s1 s2)
1661
1662 let () =
1663 define "fset_equal" (set_repr @-> set_repr @-> ret bool)
1664 @@ fun (TaggedSet (tag,s1)) (TaggedSet (tag',s2)) ->
1665 let Refl = assert_map_tag_eq tag tag' in
1666 let (module V) = get_map tag in
1667 V.S.equal s1 s2
1668
1669 let () =
1670 define "fset_subset" (set_repr @-> set_repr @-> ret bool)
1671 @@ fun (TaggedSet (tag,s1)) (TaggedSet (tag',s2)) ->
1672 let Refl = assert_map_tag_eq tag tag' in
1673 let (module V) = get_map tag in
1674 V.S.subset s1 s2
1675
1676 let () =
1677 define "fset_cardinal" (set_repr @-> ret int) @@ fun (TaggedSet (tag,s)) ->
1678 let (module V) = get_map tag in
1679 V.S.cardinal s
1680
1681 let () =
1682 define "fset_elements" (set_repr @-> ret valexpr) @@ fun (TaggedSet (tag,s)) ->
1683 let (module V) = get_map tag in
1684 Tac2ffi.of_list (repr_of V.repr) (V.S.elements s)
1685
1686 let () =
1687 define "fmap_empty" (map_tag_repr @-> ret valexpr) @@ fun (Any (tag)) ->
1688 let (module V) = get_map tag in
1689 let Refl = V.valmap_eq in
1690 tag_map tag V.M.empty
1691
1692 let () =
1693 define "fmap_is_empty" (map_repr @-> ret bool) @@ fun (TaggedMap (tag,m)) ->
1694 let (module V) = get_map tag in
1695 let Refl = V.valmap_eq in
1696 V.M.is_empty m
1697
1698 let () =
1699 define "fmap_mem" (valexpr @-> map_repr @-> ret bool) @@ fun x (TaggedMap (tag,m)) ->
1700 let (module V) = get_map tag in
1701 let Refl = V.valmap_eq in
1702 V.M.mem (repr_to V.repr x) m
1703
1704 let () =
1705 define "fmap_add" (valexpr @-> valexpr @-> map_repr @-> ret valexpr)
1706 @@ fun x v (TaggedMap (tag,m)) ->
1707 let (module V) = get_map tag in
1708 let Refl = V.valmap_eq in
1709 tag_map tag (V.M.add (repr_to V.repr x) v m)
1710
1711 let () =
1712 define "fmap_remove" (valexpr @-> map_repr @-> ret valexpr)
1713 @@ fun x (TaggedMap (tag,m)) ->
1714 let (module V) = get_map tag in
1715 let Refl = V.valmap_eq in
1716 tag_map tag (V.M.remove (repr_to V.repr x) m)
1717
1718 let () =
1719 define "fmap_find_opt" (valexpr @-> map_repr @-> ret (option valexpr))
1720 @@ fun x (TaggedMap (tag,m)) ->
1721 let (module V) = get_map tag in
1722 let Refl = V.valmap_eq in
1723 V.M.find_opt (repr_to V.repr x) m
1724
1725 let () =
1726 define "fmap_mapi" (closure @-> map_repr @-> tac valexpr)
1727 @@ fun f (TaggedMap (tag,m)) ->
1728 let (module V) = get_map tag in
1729 let Refl = V.valmap_eq in
1730 let module Monadic = V.M.Monad(Proofview.Monad) in
1731 Monadic.mapi (fun k v -> apply f [repr_of V.repr k;v]) m >>= fun m ->
1732 return (tag_map tag m)
1733
1734 let () =
1735 define "fmap_fold" (closure @-> map_repr @-> valexpr @-> tac valexpr)
1736 @@ fun f (TaggedMap (tag,m)) acc ->
1737 let (module V) = get_map tag in
1738 let Refl = V.valmap_eq in
1739 let module Monadic = V.M.Monad(Proofview.Monad) in
1740 Monadic.fold (fun k v acc -> apply f [repr_of V.repr k;v;acc]) m acc
1741
1742 let () =
1743 define "fmap_cardinal" (map_repr @-> ret int) @@ fun (TaggedMap (tag,m)) ->
1744 let (module V) = get_map tag in
1745 let Refl = V.valmap_eq in
1746 V.M.cardinal m
1747
1748 let () =
1749 define "fmap_bindings" (map_repr @-> ret valexpr) @@ fun (TaggedMap (tag,m)) ->
1750 let (module V) = get_map tag in
1751 let Refl = V.valmap_eq in
1752 Tac2ffi.(of_list (of_pair (repr_of V.repr) identity) (V.M.bindings m))
1753
1754 let () =
1755 define "fmap_domain" (map_repr @-> ret valexpr) @@ fun (TaggedMap (tag,m)) ->
1756 let (module V) = get_map tag in
1757 let Refl = V.valmap_eq in
1758 tag_set tag (V.M.domain m)