caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Julien Signoles <Julien.Signoles@lri.fr>
To: Keiko Nakata <keiko@kurims.kyoto-u.ac.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] a module with multiple signatures
Date: Thu, 2 Jun 2005 09:53:48 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.58.0506020945100.20156@pc8-102> (raw)
In-Reply-To: <20050602.161836.68553253.keiko@kurims.kyoto-u.ac.jp>


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


  reply	other threads:[~2005-06-02  7:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-02  7:18 Keiko Nakata
2005-06-02  7:53 ` Julien Signoles [this message]
2005-06-02 11:15   ` [Caml-list] " Keiko Nakata
2005-06-02 12:08     ` Julien Signoles
2005-06-02  8:04 ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0506020945100.20156@pc8-102 \
    --to=julien.signoles@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=keiko@kurims.kyoto-u.ac.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).