From: Andre Nathan <andre@digirati.com.br>
To: Gerd Stolpmann <info@gerd-stolpmann.de>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] GADTs and Menhir
Date: Wed, 01 Apr 2015 15:12:20 -0300 [thread overview]
Message-ID: <551C3504.6050209@digirati.com.br> (raw)
In-Reply-To: <1427894198.16182.68.camel@e130.lan.sumadev.de>
[-- Attachment #1: Type: text/plain, Size: 1415 bytes --]
Hi Gerd
On 04/01/2015 10:16 AM, Gerd Stolpmann wrote:
> You need Any if you want to put several t values into a container, e.g.
>
> Does not work: [ Int 34; Bool true ]
> Does work: [ Any(Int 34); Any(Bool true) ]
>
> This nice thing with GADTs is that you can "undo" this change of
> perspective by matching against the cases:
But is there any way to, say, write some sort of lookup function for
values in such containers?
Eg. given an eval function, this doesn't work:
let rec find k l =
match l with
| [] -> raise Not_found
| (k', v)::rest ->
if k = k' then v
else find k rest
let foo () =
let dict = ["int", Any (Int 1); "bool", Any (Bool true)] in
let Any x = find "int" dict in
eval x
^^^^^^
Error: This expression has type a#0 but an expression was expected of
type a#0
The type constructor a#0 would escape its scope
I don't understand why `eval` can be written but it's result cannot be
returned from another function.
I tried introducing a type variable but it still doesn't work
let foo (type a) () : a =
let dict = ["x", Any (Int 1); "y", Any (Bool true)] in
let Any (x : a t) = find "x" dict in
eval x
^^^^^^^^^
Error: This pattern matches values of type a t
but a pattern was expected which matches values of type a#0 t
Type a is not compatible with type a#0
Thanks,
Andre
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]
prev parent reply other threads:[~2015-04-01 18:12 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
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 [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=551C3504.6050209@digirati.com.br \
--to=andre@digirati.com.br \
--cc=caml-list@inria.fr \
--cc=info@gerd-stolpmann.de \
/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