From: Philippe Veber <philippe.veber@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: Jeff Meister <nanaki@gmail.com>,
Tiphaine Turpin <Tiphaine.Turpin@free.fr>,
Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] [GADT noob question] Building a GADT from an untyped representation
Date: Tue, 8 Jan 2013 12:00:57 +0100 [thread overview]
Message-ID: <CAOOOohQi9XpOmmwDMJgoOCrDV4ONN4kEYkzUG8Dh+NfoUYG+-Q@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=HnM9szid1o5hkRTjvS79_8OJh+TeJ77V2A+r76Ordg4Q@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2610 bytes --]
Now it looks super nice!
Thanks a lot Jeremy, your explanations were really helpful!
ph.
2013/1/5 Jeremy Yallop <yallop@gmail.com>
> On 5 January 2013 13:40, Philippe Veber <philippe.veber@gmail.com> wrote:
> > I still have a question though: what is the exact meaning of the _
> > character in the polymorphic type "< rn : 'a. int * 'a expr -> _>"
> > and how is it useful/necessary for your example to run?
>
> It's analogous to '_' in patterns: a kind of anonymous type variable
> that you can use to avoid giving a name to a type. (As with "_" in
> patterns, there"s no connection between occurrences of "_", so "_ *
> int -> _" means "'a * int -> 'b", not "'a * int -> 'a", for example.)
>
> It's not doing anything special here: you could equally give a name to
> the type without changing the meaning of the code.
>
> > Could your example be written without a record/object type using
> > polymorphic type annotations for functions?
>
> I don't believe it's possible to make function arguments polymorphic
> using annotations. However, the code can be significantly simplified
> to remove that bit of polymorphism altogether. As written it mixes
> two techniques for hiding the type index in the expression GADT during
> parsing: CPS (in the inner 'parse' function) and an existential type
> (in the return type of 'parse_expr'). In fact, either of these
> approaches in isolation is sufficient. Here's a more direct
> implementation using only the existential:
>
> (* Well-typed parser. Incomplete -- it doesn't handle fst and snd -- and a
> bit careless about error-checking. Perhaps sufficient to give a
> flavour.
> *)
> let parse_expr : string -> any_expr =
> let rec parse s pos =
> match s.[pos] with
> | 'T' -> pos + 1, Expr True
> | 'F' -> pos + 1, Expr False
> | '!' -> let pos', Expr e = parse s (pos + 1) in
> (* Check that 'e' has boolean type before we can parse it to
> Not. This is more than just good practice: without the
> type-checking step the parser won't compile. *)
> begin match type_of e with
> | TBool -> pos', Expr (Not e)
> | t -> typing_failure (pos + 1) pos' s TBool t
> end
> | '(' -> let pos, Expr l = parse s (pos + 1) in
> if s.[pos] <> ',' then parsing_failure pos ',' s else
> let pos, Expr r = parse s (pos + 1) in
> if s.[pos] <> ')' then parsing_failure pos ')' s else
> pos + 1, Expr (Pair (l, r))
> in
> fun s -> snd (parse s 0)
>
[-- Attachment #2: Type: text/html, Size: 3512 bytes --]
prev parent reply other threads:[~2013-01-08 11:01 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
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 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=CAOOOohQi9XpOmmwDMJgoOCrDV4ONN4kEYkzUG8Dh+NfoUYG+-Q@mail.gmail.com \
--to=philippe.veber@gmail.com \
--cc=Tiphaine.Turpin@free.fr \
--cc=caml-list@inria.fr \
--cc=nanaki@gmail.com \
--cc=yallop@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).