caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Module type troubles
@ 2006-07-18  4:31 Chris King
       [not found] ` <3C1C2DED-FF8B-4C6C-ABD2-DDFEFF808967@gaillourdet.net>
  0 siblings, 1 reply; 2+ messages in thread
From: Chris King @ 2006-07-18  4:31 UTC (permalink / raw)
  To: O'Caml Mailing List

(Sorry for the long explaination that follows, it's the most concise
example I can come up with.)

Say I have two polymorphic types with associated constructors:

type 'a foo = { foo: 'a }
let make_foo a = { foo = a }

type 'a bar = { bar: 'a }
let make_bar a = { bar = a }

Using the "combine" function, I am able to define constructors for
compound types:

let combine make_a make_b (a, b) =
    make_a a, make_b b

type 'c foobar = 'a foo * 'b bar constraint 'c = 'a * 'b
let make_foobar c: 'c foobar =
    combine make_foo make_bar c

type 'c foobarfoo = 'a foobar * 'b foo constraint 'c = 'a * 'b
let make_foobarfoo c: 'c foobarfoo =
    combine make_foobar make_foo c


Now I would like to do the same thing with modules:

module Foo = struct
    type 'a t = { foo: 'a }
    let make a = { foo = a }
end

module Bar = struct
    type 'a t = { bar: 'a }
    let make a = { bar = a }
end

module type Combinable = sig
    type 'a t
    val make: 'a -> 'a t
end

module Combine(A: Combinable)(B: Combinable) = struct
    type 'c t = 'a A.t * 'b B.t constraint 'c = 'a * 'b
    let make (a, b) = A.make a, B.make b
end

module FooBar = Combine(Foo)(Bar)
module FooBarFoo = Combine(FooBar)(Foo)

Everything works fine until the last line.  The compiler complains:

Type declarations do not match:
  type 'a t = 'b Foo.t * 'c Bar.t constraint 'a = 'b * 'c
is not included in
  type 'a t

I realize that this is because type 'a FooBar.t cannot exist without
its constraint.  Is there a way to acheive the module structure I
want, save using Obj?

Thanks in advance,
Chris King


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

* Re: [Caml-list] Module type troubles
       [not found] ` <3C1C2DED-FF8B-4C6C-ABD2-DDFEFF808967@gaillourdet.net>
@ 2006-07-18 13:24   ` Chris King
  0 siblings, 0 replies; 2+ messages in thread
From: Chris King @ 2006-07-18 13:24 UTC (permalink / raw)
  To: Jean-Marie Gaillourdet; +Cc: O'Caml Mailing List

On 7/18/06, Jean-Marie Gaillourdet <jm@gaillourdet.net> wrote:
> As far as I understand ML modules, the only way to achieve that is to
> capture your type variables with abstract types in separate modules,
> i.e. Foo and Bar have to get their type parameters as modules.

I was hoping to keep the functions polymorphic but it seems like
functors like you say are the best route... they'll probably end up
making my code cleaner anyway :P

Thanks,
Chris King


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

end of thread, other threads:[~2006-07-18 13:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-18  4:31 Module type troubles Chris King
     [not found] ` <3C1C2DED-FF8B-4C6C-ABD2-DDFEFF808967@gaillourdet.net>
2006-07-18 13:24   ` [Caml-list] " Chris King

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).