caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Stupid question re:modules
@ 2007-08-24  0:32 Brian Hurt
  2007-08-24  2:56 ` [Caml-list] " Jacques Garrigue
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Brian Hurt @ 2007-08-24  0:32 UTC (permalink / raw)
  To: caml-list


I should just know this.  So let's say I have two module types defined:

module type Foo = sig
 	type 'a t
 	val foo : 'a -> 'a t
end;;

module type Bar = sig
 	type 'a t
 	val bar : 'a -> 'a t
end;;

Now, I want to define a module that is both a Foo and a Bar without 
cutting and pasting the module definitions around.  I've been trying to 
do:

module Baz : sig
 	type 'a baz
 	include Foo with type 'a t = 'a baz
 	include Bar with type 'a t = 'a baz
end;;

but this blows up on the Bar line (multiple definitions of 'a t).

There is a solution to this, I'm just being stupid and forgetting what it 
is.  Hints would be appreciated.

Brian



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

* Re: [Caml-list] Stupid question re:modules
  2007-08-24  0:32 Stupid question Brian Hurt
@ 2007-08-24  2:56 ` Jacques Garrigue
  2007-08-24  4:31 ` Julien Moutinho
  2007-08-24 10:16 ` Vincent Aravantinos
  2 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2007-08-24  2:56 UTC (permalink / raw)
  To: bhurt; +Cc: caml-list

From: Brian Hurt <bhurt@spnz.org>
> I should just know this.  So let's say I have two module types defined:
> 
> module type Foo = sig
>  	type 'a t
>  	val foo : 'a -> 'a t
> end;;
> 
> module type Bar = sig
>  	type 'a t
>  	val bar : 'a -> 'a t
> end;;
> 
> Now, I want to define a module that is both a Foo and a Bar without 
> cutting and pasting the module definitions around.  I've been trying to 
> do:
> 
> module Baz : sig
>  	type 'a baz
>  	include Foo with type 'a t = 'a baz
>  	include Bar with type 'a t = 'a baz
> end;;
> 
> but this blows up on the Bar line (multiple definitions of 'a t).
> 
> There is a solution to this, I'm just being stupid and forgetting what it 
> is.  Hints would be appreciated.

Unfortunately, there is no solution to this, as you cannot remove a
declaration from a signature, and include cannot overwrite existing
type definitions. There was a paper in ICFP'2005 on how to solve this,
but I'm not aware of any plan to follow it.

If you're ready to write longer signatures, there is a workaround.

module type T = sig type 'a t end

module FooS(X:T) = struct
  module type S = sig
    val foo : 'a -> 'a X.t
  end
end

module BarS(X:T) = struct
  module type S = sig
    val bar : 'a -> 'a X.t
  end
end

module Baz : sig
  module T : sig type 'a t end
  include FooS(T)
  include BarS(T)
end

The basic idea if you want to be able to construct signatures
modularly is to always keep types and function declarations
separated.

Jacques Garrigue


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

* Re: [Caml-list] Stupid question re:modules
  2007-08-24  0:32 Stupid question Brian Hurt
  2007-08-24  2:56 ` [Caml-list] " Jacques Garrigue
@ 2007-08-24  4:31 ` Julien Moutinho
  2007-08-24 10:16 ` Vincent Aravantinos
  2 siblings, 0 replies; 5+ messages in thread
From: Julien Moutinho @ 2007-08-24  4:31 UTC (permalink / raw)
  To: Brian Hurt; +Cc: caml-list

On Thu, Aug 23, 2007 at 08:32:22PM -0400, Brian Hurt wrote:
>
> I should just know this.  So let's say I have two module types defined:
>
> module type Foo = sig
> 	type 'a t
> 	val foo : 'a -> 'a t
> end;;
>
> module type Bar = sig
> 	type 'a t
> 	val bar : 'a -> 'a t
> end;;
>
> Now, I want to define a module that is both a Foo and a Bar without cutting 
> and pasting the module definitions around.  I've been trying to do:
>
> module Baz : sig
> 	type 'a baz
> 	include Foo with type 'a t = 'a baz
> 	include Bar with type 'a t = 'a baz
> end;;
>
> but this blows up on the Bar line (multiple definitions of 'a t).
>
> There is a solution to this, I'm just being stupid and forgetting what it 
> is.  Hints would be appreciated.
>
> Brian

I might haven't got exactly what you want,
but, with control over Foo and Bar,
and starting from the structure,
I would have written :

module type Foo =
  functor (T: T) ->
  sig val foo : 'a -> 'a T.t end

module type Bar =
  functor (T: T) ->
  sig val bar : 'a -> 'a T.t end

module Baz =
  functor (T: T) ->
  functor (Foo: Foo) ->
  functor (Bar: Bar) ->
  struct
	include T
	type 'a baz = 'a t
	module Foo = Foo(T)
	module Bar = Bar(T)
	include Foo
	include Bar
  end

Then I would have generated the signature automatically with -i.

But if you really want a short .mli without redundancy,
I see no solution, even for the above functorized code.
Because :
  1/ unable to override or delete elements from a signature
  2/ unable to get the signature of a structure
     from a functor in a signature.

I mean, given this :

module type T = sig end
module type F =
  functor (T: T) ->
  sig end

This signature does not work :

module B :
  functor (T: T) ->
  functor (F: F) ->
  sig module M : F(T) end

nor this :

module B :
  functor (T: T) ->
  functor (M: F(T)) ->
  sig end

nor this :

module B :
  functor (T: T) ->
  functor (F: F) ->
  sig include F(T) end

nor this :

module B :
  functor (T: T) ->
  functor (F: F) ->
  sig open F(T) end

Despite the fact that the structured version of
the first two signatures work.
And that I do not understand why they all do not work,
since it's a kind of substitution, isn't it?

Anyway, hope you'll find a way to _avoid_ your problem.


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

* Re: [Caml-list] Stupid question re:modules
  2007-08-24  0:32 Stupid question Brian Hurt
  2007-08-24  2:56 ` [Caml-list] " Jacques Garrigue
  2007-08-24  4:31 ` Julien Moutinho
@ 2007-08-24 10:16 ` Vincent Aravantinos
  2007-09-23  9:37   ` David Teller
  2 siblings, 1 reply; 5+ messages in thread
From: Vincent Aravantinos @ 2007-08-24 10:16 UTC (permalink / raw)
  To: Brian Hurt; +Cc: caml-list


Le 24 août 07 à 02:32, Brian Hurt a écrit :
>
> I should just know this.  So let's say I have two module types  
> defined:
>
> module type Foo = sig
> 	type 'a t
> 	val foo : 'a -> 'a t
> end;;
>
> module type Bar = sig
> 	type 'a t
> 	val bar : 'a -> 'a t
> end;;
>
> Now, I want to define a module that is both a Foo and a Bar without  
> cutting and pasting the module definitions around.  I've been  
> trying to do:
>
> module Baz : sig
> 	type 'a baz
> 	include Foo with type 'a t = 'a baz
> 	include Bar with type 'a t = 'a baz
> end;;
>
> but this blows up on the Bar line (multiple definitions of 'a t).
>
> There is a solution to this, I'm just being stupid and forgetting  
> what it is.  Hints would be appreciated.
>
> Brian

See also :
  http://caml.inria.fr/pub/ml-archives/caml-list/2006/12/ 
c7461312202053f2213a9bb33206fcb8.en.html

BTW, I thought there was some kind of research about inheritance and  
modules (heard about "mixin modules"), is there any plan to implement  
this one day in ocaml ?

The lack of real inheritance with modules is a major restriction IMHO...




--
Vincent Aravantinos
PhD Student - LIG - CAPP Team


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

* Re: [Caml-list] Stupid question re:modules
  2007-08-24 10:16 ` Vincent Aravantinos
@ 2007-09-23  9:37   ` David Teller
  0 siblings, 0 replies; 5+ messages in thread
From: David Teller @ 2007-09-23  9:37 UTC (permalink / raw)
  To: Vincent Aravantinos; +Cc: Brian Hurt, caml-list

Ask Tom Hirschowitz. I'm not sure it's still his research interest, unfortunately.

Cheers,
 David

On Fri, 2007-08-24 at 12:16 +0200, Vincent Aravantinos wrote:

> BTW, I thought there was some kind of research about inheritance and  
> modules (heard about "mixin modules"), is there any plan to implement  
> this one day in ocaml ?
> 
> The lack of real inheritance with modules is a major restriction IMHO...


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

end of thread, other threads:[~2007-09-23  9:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-24  0:32 Stupid question Brian Hurt
2007-08-24  2:56 ` [Caml-list] " Jacques Garrigue
2007-08-24  4:31 ` Julien Moutinho
2007-08-24 10:16 ` Vincent Aravantinos
2007-09-23  9:37   ` David Teller

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