| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
|
| 14 |
module ArgT = |
| 15 |
struct |
| 16 |
module DYN = Dyn.Make () |
| 17 |
module Map = DYN.Map |
| 18 |
type ('a, 'b, 'c) tag = ('a * 'b * 'c) DYN.tag |
| 19 |
type any = Any : ('a, 'b, 'c) tag -> any |
| 20 |
let eq = DYN.eq |
| 21 |
let repr = DYN.repr |
| 22 |
let create = DYN.create |
| 23 |
let dump = DYN.dump |
| 24 |
let name s = match DYN.name s with |
| 25 |
| None -> None |
| 26 |
| Some (DYN.Any t) -> |
| 27 |
Some (Any (Obj.magic t)) |
| 28 |
end |
| 29 |
|
| 30 |
type (_, _, _) genarg_type = |
| 31 |
| ExtraArg : ('a, 'b, 'c) ArgT.tag -> ('a, 'b, 'c) genarg_type |
| 32 |
| ListArg : ('a, 'b, 'c) genarg_type -> ('a list, 'b list, 'c list) genarg_type |
| 33 |
| OptArg : ('a, 'b, 'c) genarg_type -> ('a option, 'b option, 'c option) genarg_type |
| 34 |
| PairArg : ('a1, 'b1, 'c1) genarg_type * ('a2, 'b2, 'c2) genarg_type -> |
| 35 |
('a1 * 'a2, 'b1 * 'b2, 'c1 * 'c2) genarg_type |
| 36 |
|
| 37 |
type argument_type = ArgumentType : ('a, 'b, 'c) genarg_type -> argument_type |
| 38 |
|
| 39 |
let rec genarg_type_eq : type a1 a2 b1 b2 c1 c2. |
| 40 |
(a1, b1, c1) genarg_type -> (a2, b2, c2) genarg_type -> |
| 41 |
(a1 * b1 * c1, a2 * b2 * c2) CSig.eq option = |
| 42 |
fun t1 t2 -> match t1, t2 with |
| 43 |
| ExtraArg t1, ExtraArg t2 -> ArgT.eq t1 t2 |
| 44 |
| ListArg t1, ListArg t2 -> |
| 45 |
begin match genarg_type_eq t1 t2 with |
| 46 |
| None -> None |
| 47 |
| Some Refl -> Some Refl |
| 48 |
end |
| 49 |
| OptArg t1, OptArg t2 -> |
| 50 |
begin match genarg_type_eq t1 t2 with |
| 51 |
| None -> None |
| 52 |
| Some Refl -> Some Refl |
| 53 |
end |
| 54 |
| PairArg (t1, u1), PairArg (t2, u2) -> |
| 55 |
begin match genarg_type_eq t1 t2 with |
| 56 |
| None -> None |
| 57 |
| Some Refl -> |
| 58 |
match genarg_type_eq u1 u2 with |
| 59 |
| None -> None |
| 60 |
| Some Refl -> Some Refl |
| 61 |
end |
| 62 |
| _ -> None |
| 63 |
|
| 64 |
let rec pr_genarg_type : type a b c. (a, b, c) genarg_type -> Pp.t = function |
| 65 |
| ListArg t -> pr_genarg_type t ++ spc () ++ str "list" |
| 66 |
| OptArg t -> pr_genarg_type t ++ spc () ++ str "opt" |
| 67 |
| PairArg (t1, t2) -> |
| 68 |
str "("++ pr_genarg_type t1 ++ spc () ++ |
| 69 |
str "*" ++ spc () ++ pr_genarg_type t2 ++ str ")" |
| 70 |
| ExtraArg s -> str (ArgT.repr s) |
| 71 |
|
| 72 |
let argument_type_eq arg1 arg2 = match arg1, arg2 with |
| 73 |
| ArgumentType t1, ArgumentType t2 -> |
| 74 |
match genarg_type_eq t1 t2 with |
| 75 |
| None -> false |
| 76 |
| Some Refl -> true |
| 77 |
|
| 78 |
let pr_argument_type (ArgumentType t) = pr_genarg_type t |
| 79 |
|
| 80 |
type 'a uniform_genarg_type = ('a, 'a, 'a) genarg_type |
| 81 |
|
| 82 |
|
| 83 |
type 'a vernac_genarg_type = ('a, Util.Empty.t, Util.Empty.t) genarg_type |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
type rlevel = [ `rlevel ] |
| 88 |
type glevel = [ `glevel ] |
| 89 |
type tlevel = [ `tlevel ] |
| 90 |
|
| 91 |
type (_, _) abstract_argument_type = |
| 92 |
| Rawwit : ('a, 'b, 'c) genarg_type -> ('a, rlevel) abstract_argument_type |
| 93 |
| Glbwit : ('a, 'b, 'c) genarg_type -> ('b, glevel) abstract_argument_type |
| 94 |
| Topwit : ('a, 'b, 'c) genarg_type -> ('c, tlevel) abstract_argument_type |
| 95 |
|
| 96 |
type 'l generic_argument = GenArg : ('a, 'l) abstract_argument_type * 'a -> 'l generic_argument |
| 97 |
|
| 98 |
type raw_generic_argument = rlevel generic_argument |
| 99 |
type glob_generic_argument = glevel generic_argument |
| 100 |
type typed_generic_argument = tlevel generic_argument |
| 101 |
|
| 102 |
let rawwit t = Rawwit t |
| 103 |
let glbwit t = Glbwit t |
| 104 |
let topwit t = Topwit t |
| 105 |
|
| 106 |
let wit_list t = ListArg t |
| 107 |
|
| 108 |
let wit_opt t = OptArg t |
| 109 |
|
| 110 |
let wit_pair t1 t2 = PairArg (t1, t2) |
| 111 |
|
| 112 |
let in_gen t o = GenArg (t, o) |
| 113 |
|
| 114 |
let abstract_argument_type_eq : |
| 115 |
type a b l. (a, l) abstract_argument_type -> (b, l) abstract_argument_type -> (a, b) CSig.eq option = |
| 116 |
fun t1 t2 -> match t1, t2 with |
| 117 |
| Rawwit t1, Rawwit t2 -> |
| 118 |
begin match genarg_type_eq t1 t2 with |
| 119 |
| None -> None |
| 120 |
| Some Refl -> Some Refl |
| 121 |
end |
| 122 |
| Glbwit t1, Glbwit t2 -> |
| 123 |
begin match genarg_type_eq t1 t2 with |
| 124 |
| None -> None |
| 125 |
| Some Refl -> Some Refl |
| 126 |
end |
| 127 |
| Topwit t1, Topwit t2 -> |
| 128 |
begin match genarg_type_eq t1 t2 with |
| 129 |
| None -> None |
| 130 |
| Some Refl -> Some Refl |
| 131 |
end |
| 132 |
|
| 133 |
let out_gen (type a) (type l) (t : (a, l) abstract_argument_type) (o : l generic_argument) : a = |
| 134 |
let GenArg (t', v) = o in |
| 135 |
match abstract_argument_type_eq t t' with |
| 136 |
| None -> failwith "out_gen" |
| 137 |
| Some Refl -> v |
| 138 |
|
| 139 |
let has_type (GenArg (t, v)) u = match abstract_argument_type_eq t u with |
| 140 |
| None -> false |
| 141 |
| Some _ -> true |
| 142 |
|
| 143 |
let unquote : type l. (_, l) abstract_argument_type -> _ = function |
| 144 |
| Rawwit t -> ArgumentType t |
| 145 |
| Glbwit t -> ArgumentType t |
| 146 |
| Topwit t -> ArgumentType t |
| 147 |
|
| 148 |
let genarg_tag (GenArg (t, _)) = unquote t |
| 149 |
|
| 150 |
type 'a raw_abstract_argument_type = ('a,rlevel) abstract_argument_type |
| 151 |
type 'a glob_abstract_argument_type = ('a,glevel) abstract_argument_type |
| 152 |
type 'a typed_abstract_argument_type = ('a,tlevel) abstract_argument_type |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
module type Param = sig type ('raw, 'glb, 'top) t end |
| 157 |
module ArgMap(M : Param) = |
| 158 |
struct |
| 159 |
type _ pack = Pack : ('raw, 'glb, 'top) M.t -> ('raw * 'glb * 'top) pack |
| 160 |
include ArgT.Map(struct type 'a t = 'a pack end) |
| 161 |
end |
| 162 |
|
| 163 |
let create_arg name = |
| 164 |
match ArgT.name name with |
| 165 |
| None -> ExtraArg (ArgT.create name) |
| 166 |
| Some _ -> |
| 167 |
CErrors.anomaly (str "generic argument already declared: " ++ str name ++ str ".") |
| 168 |
|
| 169 |
let make0 = create_arg |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
module type GenObj = |
| 174 |
sig |
| 175 |
type ('raw, 'glb, 'top) obj |
| 176 |
val name : string |
| 177 |
val default : ('raw, 'glb, 'top) ArgT.tag -> ('raw, 'glb, 'top) obj option |
| 178 |
end |
| 179 |
|
| 180 |
let get_arg_tag = function |
| 181 |
| ExtraArg s -> s |
| 182 |
| _ -> assert false |
| 183 |
|
| 184 |
module Register (M : GenObj) = |
| 185 |
struct |
| 186 |
module GenMap = ArgMap(struct type ('r, 'g, 't) t = ('r, 'g, 't) M.obj end) |
| 187 |
|
| 188 |
let arg0_map = ref GenMap.empty |
| 189 |
|
| 190 |
let register0 arg f = |
| 191 |
let s = get_arg_tag arg in |
| 192 |
if GenMap.mem s !arg0_map then |
| 193 |
let msg = str M.name ++ str " function already registered: " ++ str (ArgT.repr s) ++ str "." in |
| 194 |
CErrors.anomaly msg |
| 195 |
else |
| 196 |
arg0_map := GenMap.add s (GenMap.Pack f) !arg0_map |
| 197 |
|
| 198 |
let get_obj0 name = |
| 199 |
try |
| 200 |
let GenMap.Pack obj = GenMap.find name !arg0_map in obj |
| 201 |
with Not_found -> |
| 202 |
match M.default name with |
| 203 |
| None -> |
| 204 |
CErrors.anomaly (str M.name ++ str " function not found: " ++ str (ArgT.repr name) ++ str ".") |
| 205 |
| Some obj -> obj |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
let obj t = get_obj0 @@ get_arg_tag t |
| 210 |
|
| 211 |
let mem t = |
| 212 |
let t = get_arg_tag t in |
| 213 |
GenMap.mem t !arg0_map |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
let fold_keys f acc = GenMap.fold (fun (Any (tag,Pack _)) acc -> f (ArgT.Any tag) acc) !arg0_map acc |
| 218 |
|
| 219 |
end |