From: Ivan Gotovchits <ivg@ieee.org>
To: caml-list@inria.fr
Subject: [Caml-list] GADT and locally ADT
Date: Tue, 04 Jun 2013 08:06:41 +0400 [thread overview]
Message-ID: <87ehciseni.fsf@golf.niidar.ru> (raw)
I'm trying to get a grasp of GADT with a simple example, in which I use subj as a
phantom type. With the following, purely synthetical types:
type ro
type rw
type ('a,_) access =
| Read : 'a -> ('a,ro) access
| ReadWrite : 'a -> ('a,rw) access
next I define function
let f1 = function
| ReadWrite (ch) -> ();;
val f : ('a, rw) access -> unit = <fun>
That's ok, I understand, by intuition, that the argument of the function
shouldn't have both types at once: ('a,rw) access and ('a,ro) access. So
the matching is exhaustive. In accordance with it, compiler forbids the
following function:
let f inp = match inp with
| ReadWrite (ch) -> ()
| Read (ch) -> ();;
Characters 57-66:
| Read (ch) -> ();;
^^^^^^^^^
Error: This pattern matches values of type ('a, ro) access
but a pattern was expected which matches values of type
('a, rw) access
But, if I add a type annotation I can successfully delude the compiler
and it typechecks what he has recently considered illegal:
let f (type t) (inp: ('a,t) access) = match inp with
| ReadWrite (ch) -> ()
| Read (ch) -> ();;
val f : ('a, 'b) access -> unit = <fun>
Are there any logical explanation to this?
Thank in advance!
--
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
...."Have you mooed today?"...
next reply other threads:[~2013-06-04 4:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-04 4:06 Ivan Gotovchits [this message]
2013-06-04 5:47 ` Gabriel Scherer
2013-06-04 7:23 ` Ivan Gotovchits
2013-06-04 9:23 ` Gabriel Scherer
2013-06-04 11:50 ` Ivan Gotovchits
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=87ehciseni.fsf@golf.niidar.ru \
--to=ivg@ieee.org \
--cc=caml-list@inria.fr \
/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).