[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Keiko Nakata <keiko@k...> |
| Subject: | private rows question |
Hello.
Why the following functor G is not typable?
I thought that g could have a type something like int -> [< `A | `B ].
module G(X:sig type t = private [< `A ] val f : int -> t end) = struct
let g x = if x = 0 then `B else X.f x
end
This question comes from a wish to reuse my program codes
using private rows in the following way:
(In my actual program, each branches of the function g of F
performs much more verbose task. Thus, writing duplicate codes
is a bit painful.)
module F(X:sig
type t = private [< `A | `B | `C]
val f : [`III of string] -> t end) =
struct
let rec g binds = function
[] -> []
| `Var name :: tl -> (List.assoc name binds) :: (g binds tl)
| (`I name) :: tl -> `A :: (g ((name, `A) :: binds) tl)
| (`II name) :: tl -> `B :: (g ((name, `B) :: binds ) tl)
| (`III _ as iii) :: tl ->
let iii' = X.f iii in
iii' :: (g ((name, iii') :: binds) tl)
end
module ABC = struct
type t = [`A | `B | `C]
let f = function `III name -> `C
end
module AB = struct
type t = [`A | `B]
let f = function `III name -> `A
end
module FABC = F(ABC)
module FAB = F(AB)
let abc : [`I | `II | `III] list -> [`A | `B | `C] list = FABC.g
let ab : [`I | `II | `III] list -> [`A | `B ] list = FBC.g
I thought that the reuse might be possible
by making the function g take the function f as an argument, as in follows:
let rec g f binds = function
[] -> []
| `Var name :: tl -> (List.assoc name binds) :: (g f binds tl)
| (`I name) :: tl -> `A :: (g f ((name, `A) :: binds) tl)
| (`II name) :: tl -> `B :: (g f ((name, `B) :: binds ) tl)
| (`III name as iii) :: tl ->
let iii' = f iii in
iii' :: (g f ((name, iii') :: binds) tl)
let f = function `III name -> `C
let abc binds ls : [ `A | `B | `C] = g f binds ls
However, this attempt also failed...
(Anyway, I do not find the addition of f to g's arguments pleasant,
since this could increase the number of g's formal parameters
unnecessarily when I define more complicated functions.)
Best regards,
Keiko NAKATA