From: Jacques Garrigue <jacques.garrigue@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Calling a single function on every member of a GADT?
Date: Wed, 8 Jan 2020 10:43:31 +0100 [thread overview]
Message-ID: <0096d024-b800-375b-938e-0ce53cddad87@gmail.com> (raw)
In-Reply-To: <41592816-f82e-43a4-b67f-02e69623fe23@www.fastmail.com>
Actually, this is a rare case where using a polymorphic method may be
handy too:
let rec fold : type a r. r -> <v : 'b. r -> 'b term -> r> -> a term -> r =
fun i f -> function
| Int _ as t -> f#v i t
| Add -> f#v i Add
| App (x, y) as t -> f#v (fold (fold i f x) f y) t
let v =
object method v : type a. _ -> a Gadt.term -> _ =
fun x -> function
| Int n -> x+n
| Add -> x+1
| App _ -> x+2
end
let r = Gadt.fold 0 v (App (App (Add, Int 3), Int 5))
The point being that to match on a Gadt you will anyway need to use the
(type a) construct to allow refinement.
Jacques
On 08/01/2020 07:54, rixed@happyleptic.org wrote:
> Hello and thank you for the answer.
>
> On Tue, Jan 7, 2020, at 21:21, Ivan Gotovchits wrote:
>> It is the limitation of the let-bound polymorphism. (...)
>> In your case, I would define a visitor type, e.g.,
>> type 'r visitor = {visit : 'a. 'a term -> 'r -> 'r}
> Oh I see. I've used this trick to force a function to be polymorphic, but I failed to see that this was the problem because to me `f` is not any more polymorphic when the `term` is a GADT than when it's not.
>
> So there is no lighter syntax to specify that `f` should accept any member of a GADT than the syntax to specify that `f` should accept any type at all?
>
> I wonder, is this just a limitation of the OCaml parser or is there some deep reason for these work-around (like is the case, from my understanding, for the value restriction)?
>
> Sorry for asking such simple questions but I've build a mental model of how OCaml type checking works mostly from trials and errors, which used to work great until some versions ago :)
next prev parent reply other threads:[~2020-01-08 9:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-07 19:24 rixed
2020-01-07 20:21 ` Ivan Gotovchits
2020-01-08 6:54 ` rixed
2020-01-08 9:43 ` Jacques Garrigue [this message]
2020-01-08 20:32 ` Ivan Gotovchits
2020-01-10 9:49 ` Malcolm Matalka
2020-01-10 19:52 ` Ivan Gotovchits
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=0096d024-b800-375b-938e-0ce53cddad87@gmail.com \
--to=jacques.garrigue@gmail.com \
--cc=caml-list@inria.fr \
/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