caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Lukasz Stafiniak <lukstafi@gmail.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Polymorphic recursion and GADTs
Date: Wed, 11 Dec 2013 09:43:48 +1100	[thread overview]
Message-ID: <83E00BC5-3599-4702-873E-0FD6863050CC@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAJMfKEW9TVbuBWwwA+6zwKS+dr0kmOW_fWDD+Z=PcT0+m7ADqw@mail.gmail.com>

On 2013/12/11 01:24, Lukasz Stafiniak wrote:

> I have another challenge for you. Without the redundant annotations (i.e. with only an annotation on let-rec) it type-checks nicely. I am providing more annotations as an option.
> >>>
> type _ term =
>   | Lit : integer -> integer term
>   | Plus : integer term * integer term -> integer term
>   | IsZero : integer term -> boolean term
>   | If : (*∀'a.*)boolean term * 'a term * 'a term -> 'a term
>   | Pair : (*∀'a, 'b.*)'a term * 'b term -> (('a * 'b)) term
>   | Fst : (*∀'a, 'b.*)(('a * 'b)) term -> 'a term
>   | Snd : (*∀'a, 'b.*)(('a * 'b)) term -> 'b term
> and integer
> and boolean
>   
> external plus : (integer -> integer -> integer) = "plus"
> external is_zero : (integer -> boolean) = "is_zero"
> external if_then : (boolean -> 'a -> 'a -> 'a) = "if_then"
> let rec eval : type a . (a term -> a) =
>   ((function Lit i -> i | IsZero x -> is_zero (eval x)
>     | Plus (x, y) -> plus (eval x) (eval y)
>     | If (b, t, e) -> if_then (eval b) (eval t) (eval e)
>     | Pair (x, y) -> (eval x, eval y)
>     | Fst p -> let ((x, y): (a * 't47)) = eval p in x
>     | Snd p -> let ((x, y): ('t59 * a)) = eval p in y): a term -> a)
> <<<
> We get at "eval p" to the right of "let ((x, y): (a * 't47))":
> Error: This expression has type a * b#0
>        but an expression was expected of type a * b#0
>        The type constructor b#0 would escape its scope

As always, Gabriel's answer is correct, but I have two remarks.
First, concerning your first example where you needed annotations on the local function,
you can actually avoid that by using pattern matching rather than function application:

let rec walk : type a b . (a place -> b place -> (a, b) nearby) =
  fun x goal ->
  match compare x goal with
  | Same -> Here (x, goal)
  | NotSame ->
      let Ex1 ((y, to_y)) = wander x in Transitive (to_y, walk y goal)

Now, for the above example, of course you don't need to annotate the "function" construct,
but if you want to name the rigid variables 't47 and 't59, you can do that through eta-expansion:

let rec eval : type a . (a term -> a) =
  function Lit i -> i | IsZero x -> is_zero (eval x)
    | Plus (x, y) -> plus (eval x) (eval y)
    | If (b, t, e) -> if_then (eval b) (eval t) (eval e)
    | Pair (x, y) -> (eval x, eval y)
    | Fst p -> (fun (type b) p -> let ((x, y): (a * b)) = eval p in x) p
    | Snd p -> (fun (type b) p -> let ((x, y): (b * a)) = eval p in y) p

Not very clean, so we need to do something about it.

	Jacques

  parent reply	other threads:[~2013-12-10 22:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-09 17:16 Lukasz Stafiniak
2013-12-09 17:24 ` Lukasz Stafiniak
2013-12-09 21:34   ` Lukasz Stafiniak
2013-12-10 13:20     ` Lukasz Stafiniak
2013-12-10 13:21       ` Lukasz Stafiniak
2013-12-10 14:24         ` Lukasz Stafiniak
2013-12-10 19:07           ` Gabriel Scherer
2013-12-10 22:43           ` Jacques Garrigue [this message]
2013-12-10 22:51             ` Lukasz Stafiniak
2013-12-10 22:55               ` Lukasz Stafiniak
2013-12-09 17:25 ` Gabriel Scherer

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=83E00BC5-3599-4702-873E-0FD6863050CC@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=lukstafi@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).