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.