caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andre Nathan <andre@digirati.com.br>
To: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] GADTs and Menhir
Date: Wed, 01 Apr 2015 09:16:30 -0300	[thread overview]
Message-ID: <551BE19E.3010302@digirati.com.br> (raw)
In-Reply-To: <551B067C.2030006@digirati.com.br>

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

On 03/31/2015 05:41 PM, Andre Nathan wrote:
> Can you give me an example of what this "GADT of type representations"
> would look like? I couldn't understand the Haskell example...

I found a reference to an email from Jeremy Yallop to the list from 2013
[1], and managed to get it working with the following solution:

(* foo.ml *)
open Printf

type 'a t =
  | Int : int -> int t
  | Bool : bool -> bool t

type ast =
  [ `Int of int
  | `Bool of bool
  ]

type any =
  | Any : 'a t -> any

let typed = function
  | `Int i -> Any (Int i)
  | `Bool b -> Any (Bool b)

let print : type a. a t -> unit = function
  | Int i -> printf "%d\n" i
  | Bool b -> printf "%b\n" b

The parser now returns a `Foo.ast`:

value:
  | i = INTEGER { `Int i }
  | b = BOOL    { `Bool b }
  ;

and with that I can print the parsed value with

  let ast = Parser.start Lexer.token lexbuf in
  let Foo.Any t = Foo.typed ast in
  Foo.print t

I'm happy that it works, but the `any` type is a bit of a mistery to me.
In Jeremy's email it's explained as

"An existential to hide the type index of a well-typed AST, making it
possible to write functions that return constructed ASTs whose type is
not statically known."

Does anyone have a reference to literature that explains this technique
(I'm guessing that would be Pierce's book)? The OCaml manual briefly
shows an example with a `dyn` type, but not much is said about it.

Thanks,
Andre

[1] https://sympa.inria.fr/sympa/arc/caml-list/2013-01/msg00013.html


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

  reply	other threads:[~2015-04-01 12:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-31 18:16 Andre Nathan
2015-03-31 19:45 ` Francois Pottier
2015-03-31 20:41   ` Andre Nathan
2015-04-01 12:16     ` Andre Nathan [this message]
2015-04-01 12:27       ` Jeremy Yallop
2015-04-01 12:43         ` Andre Nathan
2015-04-01 13:16       ` Gerd Stolpmann
2015-04-01 18:12         ` Andre Nathan

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=551BE19E.3010302@digirati.com.br \
    --to=andre@digirati.com.br \
    --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;
as well as URLs for NNTP newsgroup(s).