]>
>You can provide combinators to build the type information:
Wow, that's pretty cool. I'm not that familiar with abstract types and modules in caml (or any functional language), so this was kind of an eye opener. I think I understand how they work a bit more now, thanks. I do have one thing I can't figure out. Here's my test module:
module C:
sig
type 'a t
val int_t : int t
val float_t : float t
(* Begins with @ so it's right-associative :-) *)
val (@->) : 'a t -> 'b t -> ('a -> 'b) t
val get_function : string -> 'a t -> 'a
val conv : 'a t -> int list
end =
struct
type 'a t = T of int | Tl of int list
let int_t = T 1
let float_t = T 2
let (@->) a b = match a,b with
(T a,T b) -> Tl [a;b]
| (T a,Tl b) -> Tl (a :: b)
| (Tl a,Tl b) -> Tl (a @ b)
| (Tl a,T b) -> Tl (a @ [b])
let get_function s l = Obj.magic s (* just get it to compile *)
let conv (Tl a) = a
end
The way I've done 'a t is as a variant int list. This works, but my @-> flattens nested lists because of the pattern match, so while the type system differentiates between
# (float_t @-> int_t @-> float_t @-> float_t @-> int_t);;
- : (float -> int -> float -> float -> int) C.t = <abstr>
and (note the nested parens):
# (float_t @-> (int_t @-> float_t @-> float_t) @-> int_t);;
- : (float -> (int -> float -> float) -> int) C.t = <abstr>
my internal representation doesn't:
# conv (float_t @-> int_t @-> float_t @-> float_t @-> int_t);;
- : int list = [2; 1; 2; 2; 1]
# conv (float_t @-> (int_t @-> float_t @-> float_t) @-> int_t);;
- : int list = [2; 1; 2; 2; 1]
I thought I could fix this by saying type 'a t = T of int | Tl of 'a t list, but that seems to constrain (@->) to be 'a t -> 'a t -> 'a t and I lose the cruicial ('a -> 'b) t combinator (not sure if I'm using that word correctly) and I get a type error. Any idea how I could get it to nest?
Chris
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr