From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: philippe.veber@googlemail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Closed variants, type constraints and module signature
Date: Sat, 15 May 2010 00:49:40 +0900 (JST) [thread overview]
Message-ID: <20100515.004940.200940107.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <AANLkTikEddrAoZnT7aPdb86f0KJAnrRRx7pBpa_4l_aM@mail.gmail.com>
From: Philippe Veber <philippe.veber@googlemail.com>
> I'd like to define a type with a variable that is constrained to accept only
> polymorphic variant types included in a given set of tags. That is how I
> believed one should do :
>
> Objective Caml version 3.11.2
>
> # type 'a t = 'a constraint 'a = [< `a | `b ];;
> type 'a t = 'a constraint 'a = [< `a | `b ]
>
> But I stumbled upon the following problem, when trying to use this
> definition
>
>
> # module type S = sig
> val v : 'a t
> end;;
> module type S = sig val v : [< `a | `b ] t end
>
> # module I : S = struct
> let v = `a
> end;;
>
> Error: Signature mismatch:
> Modules do not match: sig val v : [> `a ] end is not included in S
> Values do not match:
> val v : [> `a ]
> is not included in
> val v : [< `a | `b ] t
>
> Does anyone know why the definition of module I is rejected ? And if this is
> the intended behavior, why does the following work ?
>
> # let v : 'a t = `a
> ;;
> val v : [< `a | `b > `a ] t = `a
But it doesn't really work!
More precisely, the type [< `a | `b > `a ] t is an instance of 'a t,
not 'a t itself, an a module interface should give a type at most as
general as the implementation.
In your case, you should simply write
type t = [`a | `b]
since you don't know what v may be.
Jacques Garrigue
next prev parent reply other threads:[~2010-05-14 15:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-14 15:17 Philippe Veber
2010-05-14 15:49 ` Jacques Garrigue [this message]
2010-05-14 21:33 ` [Caml-list] " Philippe Veber
2010-05-15 0:54 ` Jacques Garrigue
2010-05-17 11:56 ` Philippe Veber
2010-05-17 14:31 ` Jacques Garrigue
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=20100515.004940.200940107.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=philippe.veber@googlemail.com \
/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