From: oleg@okmij.org
To: caml-list@inria.fr
Subject: [Caml-list] ANN: Generators in OCaml
Date: Tue, 9 Aug 2011 01:13:12 -0700 (PDT) [thread overview]
Message-ID: <20110809081312.6C177170AF@Adric.ern.nps.edu> (raw)
The announced small library of generators in OCaml lets us write
generators like those in Python 2 -- as well as generators that go
beyond Python. The library is a simple overlay over the library
delimcc of delimited continuations in OCaml. The library delimcc is
the only dependency of the generator library.
The library offers three primitives: yield, msplit, and new_prompt.
Prompts, created by new_prompt, are like loop labels in Perl. The
yield expression delivers its value to any enumerator in scope, not
necessarily the innermost one. One might be surprised at the absence
of enumerators among the primitives. Enumerators are programmed-in;
the library does provide a few popular enumerators such as
for_loop. Users may write enumerators of their own, like for-loop-while
or for-loop-upto, etc. The sample code shows examples of custom
enumerators (including zipWith for parallel loops). The library also
provides a few generators, such as the analogue of Icon's find -- so
that we can write the examples from Icon's tutorial.
Generator expressions are _expressions_: they may return a value, in
addition to yielding intermediate results. A good example is the
traversal of a binary tree post-order yielding the running sum of
labels of tree branches. Here is the complete code.
type label = int
type tree = Leaf | Node of label * tree * tree;;
let pint = new_prompt ()
let rec post_order = function
| Leaf -> 0
| Node (label, left, right) ->
let sum_left = post_order left in
let sum_right = post_order right in
let sum = sum_left + sum_right + label in
yield pint sum; sum
for_loop pint (fun () -> Printf.printf "Final: %d\n" (post_order tree1))
(fun x -> Printf.printf "Got %d\n" x)
Our generators can run side-by-side, hence supporting not only nested
loops but also parallel loops.
The library is simple, consisting of generator.mli and generator.ml,
which can be found in
http://okmij.org/ftp/ftp/continuations/caml-gen/
The file test_gen.ml has several tests, including tests from Icon's
tutorial. The Makefile tells how to compile the tests as a stand-alone
executable (bytecode or native).
Please see
http://okmij.org/ftp/continuations/generators.html#ML
for explanations and derivations of generators. Incidentally, the
comments in generator.ml _derive_ the implementation.
reply other threads:[~2011-08-09 8:15 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20110809081312.6C177170AF@Adric.ern.nps.edu \
--to=oleg@okmij.org \
--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