From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA07575 for caml-redistribution@pauillac.inria.fr; Thu, 6 Apr 2000 15:46:08 +0200 (MET DST) Resent-Message-Id: <200004061346.PAA07575@pauillac.inria.fr> Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA06625 for ; Wed, 5 Apr 2000 18:04:08 +0200 (MET DST) Received: from localhost.localdomain ([61.8.10.18]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id SAA23073 for ; Wed, 5 Apr 2000 18:03:10 +0200 (MET DST) Received: from maxtal.com.au (IDENT:root@localhost [127.0.0.1]) by localhost.localdomain (8.9.3/8.8.7) with ESMTP id CAA20726 for ; Thu, 6 Apr 2000 02:02:32 +1000 Sender: root@localhost.localdomain Message-ID: <38EB6398.72F562F4@maxtal.com.au> Date: Thu, 06 Apr 2000 02:02:32 +1000 From: John Max Skaller X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 CC: caml-list@inria.fr Subject: Control inversion References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Resent-From: weis@pauillac.inria.fr Resent-Date: Thu, 6 Apr 2000 15:46:08 +0200 Resent-To: caml-redistribution@pauillac.inria.fr Given a 'functional/procedural' program which does blocking read operations, usually modelled by threading constructions, can we transform this program into one which operates entirely synchronously using callbacks from a message dispatcher (where the messages are the same data that the blocking read would have fetched? I call this process 'control inversion', because it turns master into slave, and vice versa: the read 'subroutine' called by the master algorithm, is turned inside out so that the code is executed by returning control, and then continuing via a callback (making the code a slave of the dispatcher). The requirements for control inversion are to save the state of the environment and point of control, and to restore the state, modified by the presence of the 'read' message, at the next point of control. With 'heavyweight' threads, function programs using the machine stack are control inverted by operating system level stack/register saving. With (ultra) lightweight threads, we cannot use the machine stack, except temporarily, so we must maintain a 'stack' manually, which is difficult to implement .. by hand. But it should not be so hard to add 'control inversion' to a bytecode machine. This has been done for Python (by Chritian Tismer, called Stackless Python). But it should also be possible for generated code. I have a clue that for Haskell, the notion of 'arrows' was invented to replace monads (these arrows correspond to arrows of categories) for just this purpose (the application here was thousands of concurrent interactive web sessions, where existing technology requires 'recording' the state between interactions in an extremely clubsy ad hoc (usually perlish) way. My application is different, but the requirements seem the same. I'm currently using ocaml to implement a simplistic language which provides 'automatic' control inversion (allowing programmers to write threaded programs instead of event driven ones). Unfortunately, the technique is to generate C++ code. I'd be very interested to hear more from those that know more about the inner workings of ocaml, whether there is a possible 'native' solution to this problem. I imagine this would require language constructions to specify threads (rather than just a library function call/calls). -- John (Max) Skaller, mailto:skaller@maxtal.com.au 10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850 checkout Vyper http://Vyper.sourceforge.net download Interscript http://Interscript.sourceforge.net