caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gregory Malecha <gmalecha@gmail.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>,
	Mailing List OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] "Type constructor b would escape its scope"
Date: Mon, 28 Mar 2016 15:35:30 +0000	[thread overview]
Message-ID: <CAGraiH+6kLWB3bv1wtDgi9+pUYd-MZfSPDt8O-o97-CFm-fx2w@mail.gmail.com> (raw)
In-Reply-To: <CAPFanBG6sOSX7r5pavPzsMhTcB1fwSjq36huMXyMNHD5bT=yEA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3422 bytes --]

Thanks so much.

On Mon, Mar 28, 2016, 1:08 AM Gabriel Scherer <gabriel.scherer@gmail.com>
wrote:

> Locally abstract types scope over the definition body where they can
> be used as type names or to refine GADTs by pattern-matching.
>
> module type Ty = sig type t end
>
> (* we need the scoping of the locally abstract type in this example;
>    note that (type a) would work as well *)
> let mkTy : type a . unit -> (module Ty with type t = a) =
>   fun () ->
>     let module T = struct type t = a end in
>     (module T)
>
> > val mkTy : unit -> (module Ty with type t = 'a) = <fun>
>
> (* a type variable 'a only scopes over the type annotation *)
> let mkTy : 'a . unit -> (module Ty with type t = 'a) =
>   fun () ->
>     let module T = struct type t = 'a end in
>     (module T)
>
> >  let module T = struct type t = 'a end in
> >                                 ^^
> > Error: Unbound type parameter 'a
>
>
> type _ tag = Int : int tag | Prod : 'a tag * 'b tag -> ('a * 'b) tag
>
> (* we need the ability to be refined by GADT matching here *)
> let rec default : type a . a tag -> a =
>   function
>   | Int -> 0
>   | Prod (ta, tb) -> (default ta, default tb)
>
> > val default : 'a tag -> 'a = <fun>
>
> (* a type variable just unifies with the first case and fails on the
> second *)
> let rec default : 'a . 'a tag -> 'a =
>   function
>   | Int -> 0
>   | Prod (ta, tb) -> (default ta, default tb)
> >   ^^^^^^^^^^^^^
> > Error: This pattern matches values of type ($0 * $1) tag
> >        but a pattern was expected which matches values of type int tag
> >        Type $0 * $1 is not compatible with type int
>
> (* the difference by "(type a) ..." and "type a . ..." is that the latter
>    allows polymorphic recursion (just as "'a . ..."), so this example fails
>    with just (type a) *)
> let rec default (type a) : a tag -> a =
>   function
>   | Int -> 0
>   | Prod (ta, tb) -> (default ta, default tb)
> >                             ^^
> > Error: This expression has type $0 tag but an expression
> > was expected of type 'a
> > The type constructor $0 would escape its scope
>
>
> On Mon, Mar 28, 2016 at 6:20 AM, Gregory Malecha <gmalecha@gmail.com>
> wrote:
> > Thanks. I'm not sure I understand the difference between the two though.
> Is
> > there an example showing when they produce different results?
> >
> > On Sun, Mar 27, 2016 at 6:12 PM, Jacques Garrigue
> > <garrigue@math.nagoya-u.ac.jp> wrote:
> >>
> >> On 2016/03/28 03:52, Gregory Malecha wrote:
> >> >
> >> > Thanks, Leo --
> >> >
> >> > This is exactly what I needed to know. I thought that the (type a)
> >> > annotation was forcing the function to be polymorphic. For future
> reference,
> >> > is there any way to write the annotation (that forces the function to
> be
> >> > polymorphic) without having to write explicit ‘fun's?
> >>
> >> You should either write
> >>
> >>   let generic_search_stream : ‘b. int option -> (internal_result, ‘b)
> >> result_stream = …
> >>
> >> or
> >>
> >>   let generic_search_stream : type b. int option -> (internal_result, b)
> >> result_stream = …
> >>
> >> where the latter one both requires polymorphism and defines a locally
> >> abstract type,
> >> i.e. probably the behavior you expect with (type b).
> >>
> >> Jacques
> >>
> >
> >
> >
> > --
> > gregory malecha
>

[-- Attachment #2: Type: text/html, Size: 4393 bytes --]

  reply	other threads:[~2016-03-28 15:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-27  6:19 Gregory Malecha
2016-03-27  7:04 ` Leo White
2016-03-27 18:52   ` Gregory Malecha
2016-03-28  1:12     ` Jacques Garrigue
2016-03-28  4:20       ` Gregory Malecha
2016-03-28  8:07         ` Gabriel Scherer
2016-03-28 15:35           ` Gregory Malecha [this message]
2016-03-28 19:20           ` Arnaud Spiwack
2016-03-28 20:51             ` Leo White
2016-03-28 21:00             ` Gerd Stolpmann

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=CAGraiH+6kLWB3bv1wtDgi9+pUYd-MZfSPDt8O-o97-CFm-fx2w@mail.gmail.com \
    --to=gmalecha@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=garrigue@math.nagoya-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).