| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Proofview |
| 12 |
open Tac2ffi |
| 13 |
|
| 14 |
|
| 15 |
type v = Tac2val.valexpr |
| 16 |
type 'r of_v = v -> 'r |
| 17 |
type 'r to_v = 'r -> v |
| 18 |
type 'a with_env = Environ.env -> Evd.evar_map -> 'a |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type (_, _, _) spec_aux = |
| 24 |
| T : 'r to_v -> (v tactic, 'r tactic, 'r) spec_aux |
| 25 |
| V : 'r to_v -> (v tactic, 'r, 'r) spec_aux |
| 26 |
| E : 'r to_v -> (v tactic, 'r with_env, 'r) spec_aux |
| 27 |
| G : 'r to_v -> (v tactic, Goal.t -> 'r, 'r) spec_aux |
| 28 |
| F : 'a of_v * ('t, 'f, 'r) spec_aux -> (v -> 't , 'a -> 'f, 'r) spec_aux |
| 29 |
|
| 30 |
type (_,_) spec = S : ('v,'f,'r) spec_aux -> ('v,'f) spec [@@unboxed] |
| 31 |
|
| 32 |
let tac : type r. r repr -> (v tactic, r tactic) spec = fun r -> |
| 33 |
S (T (repr_of r)) |
| 34 |
|
| 35 |
let tac': type r. r to_v -> (v tactic, r tactic) spec = fun tr -> |
| 36 |
S (T tr) |
| 37 |
|
| 38 |
let ret : type r. r repr -> (v tactic, r) spec = fun r -> |
| 39 |
S (V (repr_of r)) |
| 40 |
|
| 41 |
let ret': type r. r to_v -> (v tactic, r) spec = fun tr -> |
| 42 |
S (V tr) |
| 43 |
|
| 44 |
let eret : type r. r repr -> (v tactic, r with_env) spec = fun r -> |
| 45 |
S (E (repr_of r)) |
| 46 |
|
| 47 |
let eret': type r. r to_v -> (v tactic, r with_env) spec = fun tr -> |
| 48 |
S (E tr) |
| 49 |
|
| 50 |
let gret : type r. r repr -> (v tactic, Goal.t -> r) spec = fun r -> |
| 51 |
S (G (repr_of r)) |
| 52 |
|
| 53 |
let gret': type r. r to_v -> (v tactic, Goal.t -> r) spec = fun tr -> |
| 54 |
S (G tr) |
| 55 |
|
| 56 |
let (@->) : type a t f. a repr -> (t,f) spec -> (v -> t, a -> f) spec = |
| 57 |
fun r (S ar) -> S (F (repr_to r, ar)) |
| 58 |
|
| 59 |
let (@-->): type a t f. a of_v -> (t,f) spec -> (v -> t, a -> f) spec = |
| 60 |
fun of_v (S ar) -> S (F (of_v, ar)) |
| 61 |
|
| 62 |
let rec interp_spec : type v f. (v,f) spec -> f -> v = fun (S ar) f -> |
| 63 |
let open Proofview.Notations in |
| 64 |
match ar with |
| 65 |
| T tr -> f >>= fun v -> tclUNIT (tr v) |
| 66 |
| V tr -> tclUNIT (tr f) |
| 67 |
| E tr -> tclENV >>= fun e -> tclEVARMAP >>= fun s -> tclUNIT (tr (f e s)) |
| 68 |
| G tr -> Goal.enter_one @@ fun g -> tclUNIT (tr (f g)) |
| 69 |
| F (tr,ar) -> fun v -> interp_spec (S ar) (f (tr v)) |
| 70 |
|
| 71 |
let rec arity_of : type v f. (v,f) spec -> v Tac2val.arity = fun (S ar) -> |
| 72 |
match ar with |
| 73 |
| F (_, T _) -> Tac2val.arity_one |
| 74 |
| F (_, V _) -> Tac2val.arity_one |
| 75 |
| F (_, E _) -> Tac2val.arity_one |
| 76 |
| F (_, G _) -> Tac2val.arity_one |
| 77 |
| F (_, ar) -> Tac2val.arity_suc (arity_of (S ar)) |
| 78 |
| _ -> invalid_arg "Tac2def.arity_of: not a function spec" |
| 79 |
|
| 80 |
let define : type v f. _ -> (v,f) spec -> f -> unit = fun n ar v -> |
| 81 |
let v = |
| 82 |
match ar with |
| 83 |
| S (V tr) -> tr v |
| 84 |
| S (F _) -> |
| 85 |
let arity = arity_of ar in |
| 86 |
let v = interp_spec ar v in |
| 87 |
let v = Tac2val.purify_closure arity v in |
| 88 |
let cl = Tac2val.mk_closure arity v in |
| 89 |
Tac2ffi.of_closure cl |
| 90 |
| _ -> invalid_arg "Tac2def.define: not a pure value" |
| 91 |
in |
| 92 |
Tac2env.define_primitive n v |