caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [GADT noob question] Building a GADT from an untyped representation
@ 2013-01-04 13:32 Philippe Veber
  2013-01-04 14:54 ` Yury Sulsky
  0 siblings, 1 reply; 10+ messages in thread
From: Philippe Veber @ 2013-01-04 13:32 UTC (permalink / raw)
  To: caml users

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

Dear list,

Suppose I define a GADT for expressions:

type _ expr =
| Int : int -> int expr
| Float : float -> float expr

Now I want to write a parser, that will build an ['a expr] from a string.
Without thinking much, I tried the following:

let parse_expr : type s. string -> s expr = fun x ->
  try Int (int_of_string x)
  with _ ->
    Float (float_of_string x)
;;

Which fails with the following error message:

Error: This expression has type int expr but an expression was expected of
type s expr

That makes sense, since [s] is a locally abstract type. I tried a couple of
variants and finally realised that I could not even write the type of
[parse_expr]: it should be [string -> 'a expr] for some ['a], but I'm not
sure that really means something.

So to put it simple, how does one construct a GADT value from a string ?

Cheers,
  ph.

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-01-08 11:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-04 13:32 [Caml-list] [GADT noob question] Building a GADT from an untyped representation Philippe Veber
2013-01-04 14:54 ` Yury Sulsky
2013-01-04 15:05   ` Philippe Veber
2013-01-04 16:22     ` Tiphaine Turpin
2013-01-04 17:25       ` Philippe Veber
2013-01-04 22:00         ` Jeff Meister
2013-01-05  0:27           ` Jeremy Yallop
2013-01-05 13:40             ` Philippe Veber
2013-01-05 16:26               ` Jeremy Yallop
2013-01-08 11:00                 ` Philippe Veber

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).