caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: "François Pottier" <francois.pottier@inria.fr>
Cc: KC Sivaramakrishnan <sk826@cam.ac.uk>, caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] [ANN] New release of visitors
Date: Thu, 9 Mar 2017 14:17:19 -0500	[thread overview]
Message-ID: <CAPFanBESvRPRPsa9hvLopwZADw3icBMgzuwdwnL_BZ++cB9xLw@mail.gmail.com> (raw)
In-Reply-To: <58C19FF4.1030200@inria.fr>

This use of the monoid structure is very nice. I believe that you
could cut through the intermediate tree structure as follows. Is there
any downside?

type 'a delayed_tree = 'a cascade -> 'a cascade

let delayed_tree_to_cascade k = k nil
let delayed_tree_to_iterator (dt : 'a delayed_tree) : 'a iterator =
  cascade_to_iterator (delayed_tree_to_cascade dt)

type 'a delay = 'a

class ['self] delayed_tree_monoid = object (_ : 'self)
  method zero =
    fun k -> k

  method plus s1 s2 =
    fun k -> s1 (s2 k)

  method visit_delay
    : 'env 'a .
      ('env -> 'a -> 'b delayed_tree) ->
      'env -> 'a delay -> 'b delayed_tree
    = fun visit_ env x k ->
        (fun () -> visit_ env x k ())
end

let yield _env x = fun k () -> Cons (x, k)

On Thu, Mar 9, 2017 at 1:33 PM, François Pottier
<francois.pottier@inria.fr> wrote:
>
> Dear KC,
>
> On 09/03/2017 12:53, KC Sivaramakrishnan wrote:
>>
>> Thanks for the wonderful library and the excellent documentation. I have
>> a feature request. Could you provide a python-style generator for
>> traversing the data structure on demand? For a binary tree:
>>
>>      type 'a t = Leaf | Node of 'a t * 'a * 'a t
>>
>> I envision a `mk_gen` function:
>>
>>      val mk_gen : 'a t -> (unit -> 'a option)
>>      (** [mk_gen t] returns a generator function [f], which when invoked
>>          performs 1-step of the traversal and returns [Some v], where [v]
>>          is the node value. [f] returns [None] when the traversal is done.
>> *)
>
>
> Thanks for this excellent question, which I had not thought about.
>
> A quick answer might be that, if you are using OCaml + effect handlers,
> it should be fairly easy for you to perform control inversion and turn
> a producer-controlled traversal (as performed by an [iter] visitor) into
> a consumer-controlled traversal.
>
> That said, I was able to come up with a solution in pure OCaml. It is
> somewhat
> unexpected and (I think) quite nice, so I am posting it (with comments) on
> the
> list (see attached file). If there is demand, most of this code could be
> made
> part of the VisitorsRuntime library.
>
> Best regards,
>
>
> --
> François Pottier
> francois.pottier@inria.fr
> http://gallium.inria.fr/~fpottier/
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2017-03-09 19:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-09 10:29 François Pottier
     [not found] ` <CACVN0Vvw_oxj9bpEUJe0QBo5ZNC7pVE2b5LJQ+e7hSx-HnMa7A@mail.gmail.com>
2017-03-09 18:33   ` François Pottier
2017-03-09 19:17     ` Gabriel Scherer [this message]
2017-03-09 20:22       ` François Pottier
2017-03-14 17:28         ` 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=CAPFanBESvRPRPsa9hvLopwZADw3icBMgzuwdwnL_BZ++cB9xLw@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=francois.pottier@inria.fr \
    --cc=sk826@cam.ac.uk \
    /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).