caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] GADT and locally ADT
Date: Tue, 04 Jun 2013 15:50:45 +0400	[thread overview]
Message-ID: <8761xurt62.fsf@golf.niidar.ru> (raw)
In-Reply-To: <CAPFanBE=Zs_tzNqaJ_2YMjoeH+ycc=O6SF2MAbW6y-kMbgB5aA@mail.gmail.com> (Gabriel Scherer's message of "Tue, 4 Jun 2013 11:23:01 +0200")

Gabriel Scherer <gabriel.scherer@gmail.com> writes:

> In expressions, type variables 'a 'b do not enforce polymorphism, they
> are merely name for types that will be inferred.
>   let f : 'a -> 'a = fun x -> x + 1
> is accepted. So your second example with ('a, 'b) does not enforce polymorphism.
>
> This is not in general a problem as if the inferred type is indeed a
> type variable, it will be generalized by the inference algorithm and
> therefore "turned into polymorphism" after type inference. It is
> problematic however for polymorphic recursive functions (this form of
> polymorphism would have to be "guessed" before the recursive
> definition is type-checked, and therefore requires an annotation), and
> for GADTs.
>
> There are two *distinct* notions used to enforce polymorphism:
> - polymorphic type variables as in
>     val f : 'a -> 'a
>   or
>     let f : 'a . 'a -> 'a = ...
>   (useful for polymorphic recursion)
> - local abstract types
>    let f (type t) : t -> t = ...
>    fun (type t) -> ...
>   (useful for GADTs)
>
> Local abstract types are documented in
> http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc80 . They
> are not type variable (they cannot be unified with anything, hence
> enforce polymorphism), but type "constructors" syntactically (like
> 'list' in ('a list), but without a parameter). We say that GADTs
> introduce equations and refine types; they can only refine local
> abstract types, not type variables. The refinement of GADT types is
> similar to what happen when you traverse a module abstraction, outside
> you know the abstract type M.t, and inside you know (type M.t = int).
>
> Due to this "implementation detail" of GADTs, the following code (that
> enforces polymorphism) would still fail to type-check:
>
>     let f : 'b . ('a, 'b) access -> unit = function
>       | ReadWrite ch -> ()
>       | Read ch -> ()
>
> while the following work
>
>     let f (type t) : ('a, t) access -> unit = function
>       | ReadWrite ch -> ()
>       | Read ch -> ()
>
> Finally, the syntax
>
>    let f : type t . ('a, t) acc -> unit = function ...
>
> is a merge of both ('a 'b . ...) and ((type t) ...): it provides a
> local type constructor, and enables polymorphic recursion. This is
> what you should use when you write recursive functions using GADTs.
>

Thanks for a very informative answer! I hope that I do understand this,
though practice will show.

Thanks again!


-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

      reply	other threads:[~2013-06-04 11:50 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
2013-06-04  7:23   ` Ivan Gotovchits
2013-06-04  9:23     ` Gabriel Scherer
2013-06-04 11:50       ` Ivan Gotovchits [this message]

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=8761xurt62.fsf@golf.niidar.ru \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.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;
as well as URLs for NNTP newsgroup(s).