caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Trying to define a functor combining polymorphic variants
@ 2014-05-01 20:58 Berke Durak
  2014-05-01 23:55 ` Leo White
  2014-05-02  6:14 ` Jacques Garrigue
  0 siblings, 2 replies; 5+ messages in thread
From: Berke Durak @ 2014-05-01 20:58 UTC (permalink / raw)
  To: caml-list

Hello all,

I have been using the following kind of construct:

  module A = struct
    type message = [`Alpha]
    let string_of_message = function `Alpha -> "alpha"
  end

  module B = struct
    type message = [`Beta]
    let string_of_message = function `Beta -> "beta"
  end

  module AB = struct
    type message = [ A.message | B.message ]
    let string_of_message = function
    | #A.message as msg -> A.string_of_message msg
    | #B.message as msg -> B.string_of_message msg
  end

So I naturally wanted to write a functor that does what the module AB does:

  module type S = sig
    type message
    val string_of_message : message -> string
  end

  module PROD(A : S)(B : S) = struct
    type message = [ A.message | B.message ]
    let string_of_message = function
    | #A.t as msg -> A.string_of_message msg
    | #B.t as msg -> B.string_of_message msg
  end

But we (me + people on #ocaml: mrvn, drup, ggole, whitequark...) couldn't find a
way to specify, in the signature S, that message is a polymorphic variant so
that [ A.message | B.message ] is legal.

We tried things like:

  module type S = sig type 'a t = ([> ] as 'a) end

  module PROD(X : S)(Y : S) = struct
    type ('a,'b) t = [ 'a X.t | 'b Y.t ]
  end

but all we get is:

  Error: The type [>  ] A.message is not a polymorphic variant type

Any suggestions?
-- 
Berke Durak

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-05-03  1:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-01 20:58 [Caml-list] Trying to define a functor combining polymorphic variants Berke Durak
2014-05-01 23:55 ` Leo White
2014-05-02  6:14 ` Jacques Garrigue
2014-05-02 15:50   ` Berke Durak
2014-05-03  1:43     ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).