Version française
Home     About     Download     Resources     Contact us    
Browse thread
a module with multiple signatures
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Signoles <Julien.Signoles@l...>
Subject: Re: [Caml-list] a module with multiple signatures

> module M = struct type s = S type t = T1 | T2 of s end
>
> module F1 = functor (X : sig type t end) -> struct type t = X.t end
>
> module F2 = functor (X : sig type s type t end) ->
>                  struct type s = X.s type t = X.t  end
>
> module M1 = F1(M)
>
> module M2 = F2(M)
>
> let f x = match x with  M1.T1 -> 1 | M1.T2 x -> 2;;
> (* This is not type checked *)
> (* I got the error "Unbound constructor M1.T1" *)
>
> (* This is type checked *)
> let g (x : M1.t) = match x with  M.T1 -> 1 | M.T2 x -> 2;;
>
> Why M1.T1 should not be bound?

Because only M.T1 exists, not M1.T1. You can see this on a simpler
example:

module X = struct type t = T end
module Y = struct type t = X.t end
let f x = match x with Y.T -> ()
(* type error : "Unbound constructor Y.T" *)
let f (x:Y.t) = match x with X.t -> ()
(* val f : Y.t -> unit *)

But, in ocaml, it possible to export the constructor T in Y by using the
following syntax:

module X = struct type t = T end
module Y = struct type t = X.t = T (* T is egal to X.t *) end
let f x = match x with Y.T -> ()
(* val f : Y.t -> unit *)

> Anyway,
> there may be a nicer way to give a module multiple signatures
> while avoiding duplicate type declarations as possible?

You can use signature constraints:

module M = struct type s = S type t = T1 | T2 end
module M1 : sig type t = M.t end = M
module M2 : sig type s = M.s type t = M.t end = M

Hope this helps,
Julien
-- 
mailto:Julien.Signoles@lri.fr ; http://www.lri.fr/~signoles
"In theory, practice and theory are the same,
but in practice they are different" (Larry McVoy)