From: Philippe Veber <philippe.veber@gmail.com>
To: Yury Sulsky <yury.sulsky@gmail.com>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] [GADT noob question] Building a GADT from an untyped representation
Date: Fri, 4 Jan 2013 16:05:36 +0100 [thread overview]
Message-ID: <CAOOOohQLZu0oCpDC5KvUXY_SV_Ah+qEYv-mhTEEbChVcpBBg6w@mail.gmail.com> (raw)
In-Reply-To: <CAEDxudyF8tXkoLc5a8XoSOUfGvH01VKBAHOmLQ5XbZAPrGkkBw@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]
Hi Yury,
Thanks for your answer! It is true your fix is accepted by the compiler,
but this is not what I was looking for: in the end I'd like to have an 'a
expr such that 'a is the type of the value obtained when evaluating the
expression. With the polymorphic variants, I cannot write an eval function
of type 'a eval -> 'a, which is the main motivation behind using GADT in
that (canonical) case. Sorry for ommiting this point!
ph.
2013/1/4 Yury Sulsky <yury.sulsky@gmail.com>
> Hi Philippe,
>
> I think you can do this by using a polymorphic variant as the type
> variable:
>
> type _ expr =
> | Int : int -> [> `int ] expr
> | Float : float -> [> `float ] expr
>
> let parse_expr : string -> [ `int | `float ] expr = fun x ->
> try Int (int_of_string x)
> with _ ->
> Float (float_of_string x)
> ;;
>
> - Yury
>
>
>
> On Fri, Jan 4, 2013 at 8:32 AM, Philippe Veber <philippe.veber@gmail.com>wrote:
>
>> 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: 3575 bytes --]
next prev parent reply other threads:[~2013-01-04 15:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-04 13:32 Philippe Veber
2013-01-04 14:54 ` Yury Sulsky
2013-01-04 15:05 ` Philippe Veber [this message]
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
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=CAOOOohQLZu0oCpDC5KvUXY_SV_Ah+qEYv-mhTEEbChVcpBBg6w@mail.gmail.com \
--to=philippe.veber@gmail.com \
--cc=caml-list@inria.fr \
--cc=yury.sulsky@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).