Version française
Home     About     Download     Resources     Contact us    
Browse thread
Stupid question re:modules
[ 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@m...>
Subject: Re: [Caml-list] Stupid question re:modules
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