Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Kaspar Rohrer <kaspar.rohrer@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] Problem with GADTs and escaping types
Date: Sun, 4 Jan 2015 15:13:26 +0100	[thread overview]
Message-ID: <DE4261CE-1489-4456-9A89-B52CF9BFA6FA@gmail.com> (raw)

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

Hi all

I was playing around with GADTs over the last few days and encountered the following problem (reduced to a minimal case here): 

> type t =
>   | App : {k:'t . 'a->'t->'t; s:'a} -> t
> 
> let munge : ('a->'t->'t as 'k) -> 'k = fun k -> fun s r -> k s r
> 
> let transform = function
>   | App {k;s} -> App {k=munge k;s}

Note that I am using inlined records, which are only available in the unreleased 4.03 version. But I get the same error when using a separate record in OCaml 4.01, i.e.:

> type t = App : 'a app_t -> t
> and 'a app_t = { k : 't. 'a->'t->'t; s:'a }

OCaml will choke on the transform function with the following error:

> Error: This field value has type a#17 -> 'b -> 'b which is less general than
>          't. 'a -> 't -> 't

If I add type annotations like so:

> let transform_annotated = function
>   | App {k;s} -> App {k=(munge k : 'a->'t->'t);s=(s:'a)}

OCaml will instead give me an error about escaping types:

> Error: This expression has type a#19 -> 'a -> 'a
>        but an expression was expected of type a#19 -> 't -> 't
>        The type constructor a#19 would escape its scope

I can work around this whole problem by inlining the munge function:

> let transform_inline = function
>   | App {k;s} -> let k' s r = k s r in App {k=k';s}

This works but it is not ideal for my case, as it leads to somewhat unwieldy match expressions.

Can somebody explain to me why the first case does not work? Especially when considering that the types should be equal (substituting the identity function for munge produces the the same error).

Thank you in advance

	Kaspar M. Rohrer


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

             reply	other threads:[~2015-01-04 14:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-04 14:13 Kaspar Rohrer [this message]
2015-01-04 17:12 ` Leo White

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=DE4261CE-1489-4456-9A89-B52CF9BFA6FA@gmail.com \
    --to=kaspar.rohrer@gmail.com \
    --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