English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2007-08-24 (02:56)
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

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

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

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

Jacques Garrigue