Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Generators/iterators and lazy evaluation?
Date: Thu, 05 Apr 2007 12:49:39 +1000	[thread overview]
Message-ID: <1175741379.6481.24.camel@rosella.wigram> (raw)
In-Reply-To: <20070405071652.bcb9be46.mle+ocaml@mega-nerd.com>

On Thu, 2007-04-05 at 07:16 +1000, Erik de Castro Lopo wrote:

> One ocaml solution to this is an class/object:
> 
>     class pow2gen =
>         object
>             val mutable current = 0
>     
>             method next () =
>                 current <-
>                     if current == 0 then 1
>                     else current * 2 ;
>                 current
>         end

Yep, and one solution would be for an Ocaml programmer to modify
Felix so it can generate Ocaml instead of C++.

Felix mechanically translates threaded code into event driven
code, that is, it control inverts procedures so that I/O and
calls become returns which remember their return point.

The general structure of a translated procedure ( in Ocaml notation ):

	mutable pc : int
	mutable parent: ptype
	method resume() = match pc with
	| 1 -> ...
	| 2 -> ...
	..
	| 6 -> ... pc <- 7; new thing (self)         (* call thing *)
	| 7 -> ... let tmp = parent in parent <- NULL; tmp (* return *)
	| 8 -> ... pc <- 9; self                  (* yield *)



which is used by 

	while(tr != NULL) tr <- tr#resume()


I call this procedural continuation passing, Reynolds calls it
the method of resumptions. You will note the object here
is a stack frame on the heap, and they're linked together
into a spaghetti stack by parent pointers.

To make this work for functions you add a pointer saying
where to put the result, and translate the function
to a procedure accepting the additional pointer.

This requires unravelling applications to SSA (single 
assignment) form so an application:

	x := f a

is modelled by

	f (x,a)
	

There are more details, the above is only a sketch: you can
have a look at the precise model used by Felix (which is written
in Ocaml), noting Felix generators are simply functions translated
to procedures and stored in variables so when they're resume()d
they just keep going (whereas ordinary procedures invoked via
variables are clones of a prototype to ensure they start in
a fresh state).

The thing to note is that this is a two step model:

A. Translate Python to control inverted Ocaml code
B. Compile and execute the generate Ocaml code

and hence produces high performance native code. The problem
is that Python is dynamically typed .. so most of the performance
will be lost doing type dispatches.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  parent reply	other threads:[~2007-04-05  2:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-04 16:33 Raj B
2007-04-04 20:10 ` [Caml-list] " Mathias Kende
2007-04-04 20:46 ` Alain Frisch
2007-04-04 21:16   ` Erik de Castro Lopo
2007-04-04 21:37     ` Erik de Castro Lopo
2007-04-04 22:55     ` Bill Wood
2007-04-05  2:49     ` skaller [this message]
2007-04-05 16:41     ` Richard Jones
2007-04-04 23:53 ` Jon Harrop
2007-04-05  3:13   ` Erik de Castro Lopo
2007-04-05  5:58   ` Alain Frisch
2007-04-05 20:35     ` Jon Harrop

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=1175741379.6481.24.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=mle+ocaml@mega-nerd.com \
    /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