Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Ivan Gotovchits <ivg@ieee.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] GADT and locally ADT
Date: Tue, 4 Jun 2013 07:47:06 +0200	[thread overview]
Message-ID: <CAPFanBEau9FFNJAVF+9RyhfnbuLi+smcW3Hc6bk2BMxM3ysnnA@mail.gmail.com> (raw)
In-Reply-To: <87ehciseni.fsf@golf.niidar.ru>

If the function knows (or infer) that the argument type is (ro), then
there is only one case to handle. But there is also a function that is
polymorphic over the second argument, and it must be callable with
both (ro) and (rw) values : this function works "for any access mode".
This is what the annotated function

>      let f (type t)  (inp: ('a,t) access)  = match inp with
>        | ReadWrite (ch) -> ()
>        | Read (ch) -> ();;

does.

Now, when you write:

>     let f1 = function
>       | ReadWrite (ch) -> ();;

the inference will deduce from the pattern that the expected type is
(rw). This is the most general type if you assume that the user only
writes exhaustive patterns. But because of this specialization,
extending this pattern-matching with a (ro) case will not type-check
without an annotation.

On Tue, Jun 4, 2013 at 6:06 AM, Ivan Gotovchits <ivg@ieee.org> wrote:
>
> 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?"...
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2013-06-04  5:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-04  4:06 Ivan Gotovchits
2013-06-04  5:47 ` Gabriel Scherer [this message]
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=CAPFanBEau9FFNJAVF+9RyhfnbuLi+smcW3Hc6bk2BMxM3ysnnA@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    /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