caml-list - the Caml user's mailing list
 help / Atom feed
From: Jacques Garrigue <>
Subject: Re: [Caml-list] Calling a single function on every member of a GADT?
Date: Wed, 8 Jan 2020 10:43:31 +0100
Message-ID: <> (raw)
In-Reply-To: <>

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

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.


On 08/01/2020 07:54, 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 :)

  reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-07 19:25 rixed
2020-01-07 20:20 ` Ivan Gotovchits
2020-01-08  6:55   ` rixed
2020-01-08  9:44     ` Jacques Garrigue [this message]
2020-01-08 20:32     ` Ivan Gotovchits
2020-01-10  9:50       ` Malcolm Matalka
2020-01-10 19:51         ` Ivan Gotovchits

Reply instructions:

You may reply publically 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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror

AGPL code for this site: git clone public-inbox