Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Variant parameterized method?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Variant parameterized method?
From: Brian Hurt <>

> What I want to do is write a class interface like:
> class virtual ['a] foo :
> object
> 	method virtual doit : 'a
> 	method map : 'b. ('a -> 'b) -> 'b foo
> end
> The map function returns a new foo (not necessarily a new member of 
> whatever derived from foo class the function is actually being called on) 
> parameterized on the variant type.

This precise type is not admissible in the ocaml type system.
In ocaml recursive types must be regular: only 'a foo may occur in the
expansion of 'a foo.

This problem is discussed in an answer to PR#1730 in the caml bug
This can be solved by introducing an explicit wrapper.

I give two versions of the code: with a polymorphic reference and with
a recursive modules. They are strictly equivalent: one is not safer
than the other (at least currently).

(* polymorphic reference *)
type 'a c0 = C of < map : 'b. ('a -> 'b) -> 'b c0 >

type m = {mutable new_c : 'a. 'a -> 'a c0}
let m = {new_c = fun _ -> failwith "new_c"}

class ['a] c (x : 'a) = object
  method map : 'b. ('a -> 'b) -> 'b c0 = fun f -> m.new_c (f x)
let () = m.new_c <- fun x -> C (new c x)

(* recursive module *)
module rec M : sig
  class ['a] c : 'a -> object
    method map : ('a -> 'b) -> 'b M.c_t
  type 'a c_t = C of 'a c
end = struct
  class ['a] c (x : 'a) = object (_ : _ #M.c)
    method map = fun f -> M.C (new M.c (f x))
  type 'a c_t = C of 'a c

Not that the fact the result of #map is a ['b c_t] rather than a ['b
c] is not a practical problem: you can just write
  let C x' = x#map f
If you prefer a [(x#map f).c] notation, use a one-field record rather
than a one constructor sum.

If you think (as I do) that all these examples are just too
complicated in practice, there is a simpler way to go:
only define a fold method in your class, and define map itself out of
the class.

Jacques Garrigue

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: