caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ANN: Generators in OCaml
@ 2011-08-09  8:13 oleg
  0 siblings, 0 replies; only message in thread
From: oleg @ 2011-08-09  8:13 UTC (permalink / raw)
  To: caml-list


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.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-08-09  8:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-09  8:13 [Caml-list] ANN: Generators in OCaml oleg

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).