caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Christoph Höger" <christoph.hoeger@celeraone.com>
To: OCaml Mailing List <caml-list@inria.fr>, francois.pottier@inria.fr
Subject: [Caml-list] default fold for visitors
Date: Thu, 17 Aug 2017 13:57:11 +0200	[thread overview]
Message-ID: <CAOazmvtrHupfTTiocLc5pRDwr3xBE6psNEE3Y1rjGnP8aTbn-A@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1823 bytes --]

Dear all (especially Francois),

I am currently porting a DSL to OCaml and wanted to use the most modern ppx
approach for the typical boilerplate. One of the first things is the
implementation of the free variables with the help of ppx_visitors.
Naturally, the set of free variables forms a monoid, so I can comfortably
use the reduce variety. But that allocates quite a lot of temporary sets,
so I had a look into the fold variety.

If I understand the documentation correctly, this class requires to define
a build method for each variant of the datatype. I wonder if there is a way
to have a "default" function, namely the identity of the result value?

Consider, for example the simple lambda calculus:


type expr = Abs of (string[@opaque]) * expr | App of expr * expr | Var of
(string[@opaque]) [@@deriving visitors { variety = "fold" }]

In that case, you'd want to define

method build_Abs () x = StrSet.remove x
method build_Var () = StrSet.singleton

but also have to

method build_App () = StrSet.union

According to the manual, the visitor should be something like this:

method visit_App () l r fvs0 =
  let fvs1 = self#visit_expr () l fvs0 in
  let fvs2 = self#visit_expr () l fvs1 in
  self#build_App () fvs1 fvs2                (* Why is this /always/
necessary ? *)

I suppose this final step of building the results allows some flexibility,
but in my case I could as well just yield fvs2. I wonder if it would be
possible to have a default implementation like this:

method build_App () _ r = r

For now, I am perfectly fine with the reduce variety, but I suppose that
there are some use cases where you would actually need to fold, but only
some variants actually do something. Is this a reasonable idea or is there
some caveat in the design of ppx_visitors that makes it impossible?

thanks,

Christoph

[-- Attachment #2: Type: text/html, Size: 2000 bytes --]

             reply	other threads:[~2017-08-17 11:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-17 11:57 Christoph Höger [this message]
2017-08-17 12:48 ` Gabriel Scherer
2017-08-19  9:25 ` François Pottier

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=CAOazmvtrHupfTTiocLc5pRDwr3xBE6psNEE3Y1rjGnP8aTbn-A@mail.gmail.com \
    --to=christoph.hoeger@celeraone.com \
    --cc=caml-list@inria.fr \
    --cc=francois.pottier@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;
as well as URLs for NNTP newsgroup(s).