| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Proofview.Notations |
| 14 |
|
| 15 |
type ('a, _) arity0 = |
| 16 |
| OneAty : ('a, 'a -> 'a Proofview.tactic) arity0 |
| 17 |
| AddAty : ('a, 'b) arity0 -> ('a, 'a -> 'b) arity0 |
| 18 |
|
| 19 |
type tag = int |
| 20 |
|
| 21 |
type valexpr = |
| 22 |
| ValInt of int |
| 23 |
|
| 24 |
| ValBlk of tag * valexpr array |
| 25 |
|
| 26 |
| ValStr of Bytes.t |
| 27 |
|
| 28 |
| ValCls of closure |
| 29 |
|
| 30 |
| ValOpn of KerName.t * valexpr array |
| 31 |
|
| 32 |
| ValExt : 'a Tac2dyn.Val.tag * 'a -> valexpr |
| 33 |
|
| 34 |
|
| 35 |
and closure = MLTactic : (valexpr, 'v) arity0 * Tac2expr.frame option * 'v -> closure |
| 36 |
|
| 37 |
let arity_one = OneAty |
| 38 |
let arity_suc a = AddAty a |
| 39 |
|
| 40 |
type 'a arity = (valexpr, 'a) arity0 |
| 41 |
|
| 42 |
let mk_closure arity f = MLTactic (arity, None, f) |
| 43 |
|
| 44 |
let mk_closure_val arity f = ValCls (mk_closure arity f) |
| 45 |
|
| 46 |
module Valexpr = |
| 47 |
struct |
| 48 |
|
| 49 |
type t = valexpr |
| 50 |
|
| 51 |
let is_int = function |
| 52 |
| ValInt _ -> true |
| 53 |
| ValBlk _ | ValStr _ | ValCls _ | ValOpn _ | ValExt _ -> false |
| 54 |
|
| 55 |
let tag v = match v with |
| 56 |
| ValBlk (n, _) -> n |
| 57 |
| ValInt _ | ValStr _ | ValCls _ | ValOpn _ | ValExt _ -> |
| 58 |
CErrors.anomaly (Pp.str "Unexpected value shape") |
| 59 |
|
| 60 |
let field v n = match v with |
| 61 |
| ValBlk (_, v) -> v.(n) |
| 62 |
| ValInt _ | ValStr _ | ValCls _ | ValOpn _ | ValExt _ -> |
| 63 |
CErrors.anomaly (Pp.str "Unexpected value shape") |
| 64 |
|
| 65 |
let set_field v n w = match v with |
| 66 |
| ValBlk (_, v) -> v.(n) <- w |
| 67 |
| ValInt _ | ValStr _ | ValCls _ | ValOpn _ | ValExt _ -> |
| 68 |
CErrors.anomaly (Pp.str "Unexpected value shape") |
| 69 |
|
| 70 |
let make_block tag v = ValBlk (tag, v) |
| 71 |
let make_int n = ValInt n |
| 72 |
|
| 73 |
end |
| 74 |
|
| 75 |
let to_closure = function |
| 76 |
| ValCls cls -> cls |
| 77 |
| ValExt _ | ValInt _ | ValBlk _ | ValStr _ | ValOpn _ -> assert false |
| 78 |
|
| 79 |
let wrap fr tac = match fr with |
| 80 |
| None -> tac |
| 81 |
| Some fr -> Tac2bt.with_frame fr tac |
| 82 |
|
| 83 |
let rec apply : type a. a arity -> _ -> a -> valexpr list -> valexpr Proofview.tactic = |
| 84 |
fun arity fr f args -> match args, arity with |
| 85 |
| [], arity -> Proofview.tclUNIT (ValCls (MLTactic (arity, fr, f))) |
| 86 |
|
| 87 |
| [a0], OneAty -> wrap fr (f a0) |
| 88 |
| [a0; a1], AddAty OneAty -> wrap fr (f a0 a1) |
| 89 |
| [a0; a1; a2], AddAty (AddAty OneAty) -> wrap fr (f a0 a1 a2) |
| 90 |
| [a0; a1; a2; a3], AddAty (AddAty (AddAty OneAty)) -> wrap fr (f a0 a1 a2 a3) |
| 91 |
|
| 92 |
| a :: args, OneAty -> |
| 93 |
wrap fr (f a) >>= fun f -> |
| 94 |
let MLTactic (arity, fr, f) = to_closure f in |
| 95 |
apply arity fr f args |
| 96 |
| a :: args, AddAty arity -> |
| 97 |
apply arity fr (f a) args |
| 98 |
|
| 99 |
let apply (MLTactic (arity, wrap, f)) args = apply arity wrap f args |
| 100 |
|
| 101 |
let apply_val v args = apply (to_closure v) args |
| 102 |
|
| 103 |
type n_closure = |
| 104 |
| NClosure : 'a arity * (valexpr list -> 'a) -> n_closure |
| 105 |
|
| 106 |
let rec abstract n f = |
| 107 |
if Int.equal n 1 then NClosure (OneAty, fun accu v -> f (List.rev (v :: accu))) |
| 108 |
else |
| 109 |
let NClosure (arity, fe) = abstract (n - 1) f in |
| 110 |
NClosure (AddAty arity, fun accu v -> fe (v :: accu)) |
| 111 |
|
| 112 |
let abstract n f = |
| 113 |
match n with |
| 114 |
| 1 -> MLTactic (OneAty, None, fun a -> f [a]) |
| 115 |
| 2 -> MLTactic (AddAty OneAty, None, fun a b -> f [a;b]) |
| 116 |
| 3 -> MLTactic (AddAty (AddAty OneAty), None, fun a b c -> f [a;b;c]) |
| 117 |
| 4 -> MLTactic (AddAty (AddAty (AddAty OneAty)), None, fun a b c d -> f [a;b;c;d]) |
| 118 |
| _ -> |
| 119 |
let () = assert (n > 0) in |
| 120 |
let NClosure (arity, f) = abstract n f in |
| 121 |
MLTactic (arity, None, f []) |
| 122 |
|
| 123 |
let annotate_closure fr (MLTactic (arity, fr0, f)) = |
| 124 |
assert (Option.is_empty fr0); |
| 125 |
MLTactic (arity, Some fr, f) |
| 126 |
|
| 127 |
let rec purify_closure : type a. a arity -> (unit -> a) -> a = fun arity f -> |
| 128 |
match arity with |
| 129 |
| OneAty -> (fun x -> Proofview.tclUNIT () >>= fun () -> f () x) |
| 130 |
| AddAty a -> (fun x -> purify_closure a (fun () -> f () x)) |
| 131 |
|
| 132 |
let purify_closure ar f = purify_closure ar (fun () -> f) |