caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Max Skaller <skaller@maxtal.com.au>
To: Pierre Weis <Pierre.Weis@inria.fr>
Cc: Hongwei Xi <hwxi@ececs.uc.edu>, caml-list@inria.fr
Subject: Re: reference initialization
Date: Mon, 15 May 2000 00:37:48 +1000	[thread overview]
Message-ID: <391EBA3B.26824324@maxtal.com.au> (raw)
In-Reply-To: <200005121707.TAA26883@pauillac.inria.fr>

Pierre Weis wrote:
 
> You should consider that there is a general initialisation function in
> the Array module, named Array.init, that allocates for you a fresh
> array then fill it with values obtained by calling an arbitrary
> supplied function:
> 
> # Array.init;;
> - : int -> f:(int -> 'a) -> 'a array = <fun>
> 
> Using it, you don't need to bother with any dummy initialization value:
> 
> let combine_arrays a b =
>   let alen = Array.length a in
>   let blen = Array.length b in
>   let init i = if i < alen then a.(i) else b.(i) in
>   Array.init (alen + blen) init
> 
> This code ensures the ``always initialized strategy'' of ML, and seems
> to me elegant and clear (but note that it uses higher-order
> functionality). Have I missed something ?

I think so: the init function is a callback; code which
'naturally' initialises an array by storing into it needs
to be 'control inverted': in general this is very hard,
if not impossible, to do by hand. The general case requires
the client to write an 'interpreter' for the initialisation
algorithm, maintaining state in the callback's closure.

If we consider a simple initialisation like:

	for i=0 to n-1 do x.(i) <- i done

the control inverse is

	fun i -> i

which is simple. However, for more complex algorithms,
such as a sort, this seems non-trivial. For example,
the naive functional solution to fibonacci:

	let rec fib i = match i with 
		| 0 | 1 -> 1 
		| _ -> fib (i-1) + fib (i-2)

works, but is unacceptably inefficient. The actual control
inverse of the usual procedural array initialisation algorithm,
which keeps track of the last two fibonacci numbers, requires
a wrapper function to create the storage in a closure:

	let fib_creator () =
		let a = ref 1 and b = ref 1 in
		let fib i = (* ignore the i *)
			let result = !a + !b in
			b := !a; a := result;
			result
		in fib

Here, fib_creator is a lazy data structure, and fib an iterator
over it. A 'purer' solution could be constructed
using streams I think.

I have written a tool which performs control inversion mechanically,
although in a different context (procedural code 'reading' messages
is control inverted to be event driven). There is some sort of
deep duality theorem here.

But I think the effect is: code which is easy to write procedurally
must be converted to use continuation passing, and that isn't
a trivial task.

[On the other hand, it is usually easy to write a dispatcher for
callback driven code, provided it is the control inverse of
a single thread of control]

So I think you are missing something: iter provides a reasonable
solution for only 'half' of those problems where the callback
driven style is 'natural'. In ocaml, the only way I'm aware of
to encode continuations 'naturally' requires using (hardware
or bytecode) threads.

[BTW: if we had this duality theorem, we would know how to
mix functional and stateful code in the same language seamlessly:
it would seem Charity offers some hints here]

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



  parent reply	other threads:[~2000-05-14 21:20 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-03  1:27 When functional languages can be accepted by industry? Dennis (Gang) Chen
2000-04-06 16:51 ` Jean-Christophe Filliatre
2000-04-07  5:27   ` Dennis (Gang) Chen
     [not found]     ` <14574.1721.508470.790475@cylinder.csl.sri.com>
2000-04-11  0:24       ` Dennis (Gang) Chen
2000-04-11 17:58         ` Pierre Weis
2000-04-12  1:45           ` Dennis (Gang) Chen
2000-04-12 17:27             ` Daniel de Rauglaudre
2000-04-13 15:40               ` John Max Skaller
2000-04-14 19:16                 ` John Max Skaller
2000-04-12 18:06             ` David Brown
2000-04-13  1:23               ` Dennis (Gang) Chen
2000-04-13 14:36                 ` Pierre Weis
2000-04-13  6:53             ` Jean-Christophe Filliatre
2000-04-13 12:20               ` Frank Atanassow
2000-04-13 17:28                 ` John Max Skaller
2000-04-13 12:28               ` Steve Stevenson
2000-04-13 13:38               ` jean-marc alliot
2000-04-13 16:00                 ` William Chesters
2000-04-13 14:29               ` T. Kurt Bond
2000-04-13 17:23                 ` Julian Assange
2000-04-16 16:33                   ` John Max Skaller
2000-04-17 15:06                   ` Markus Mottl
2000-04-17 19:55                     ` John Prevost
2000-04-24  2:36                       ` Chris Tilt
2000-04-14  9:19                 ` The beginning of a library for Formal algebra and numerical Analysis Christophe Raffalli
2000-04-14  9:32                 ` Caml wish list Christophe Raffalli
2000-04-19 11:40                   ` thierry BRAVIER
2000-04-19 13:45                     ` William Chesters
2000-04-19 20:45                       ` Christophe Raffalli
2000-04-25 18:16                       ` Pierre Weis
2000-05-10  4:50                         ` reference initialization Hongwei Xi
2000-05-11 13:58                           ` Pierre Weis
2000-05-11 18:59                             ` Hongwei Xi
2000-05-12 17:07                               ` Pierre Weis
2000-05-12 19:59                                 ` Hongwei Xi
2000-05-15  6:58                                   ` Max Skaller
2000-05-15 17:56                                     ` Hongwei Xi
2000-05-14 14:37                                 ` John Max Skaller [this message]
2000-05-13  7:07                               ` Daniel de Rauglaudre
2000-05-13  7:09                               ` Daniel de Rauglaudre
2000-05-11 16:02                           ` John Prevost
2000-04-13 16:59               ` When functional languages can be accepted by industry? John Max Skaller
2000-04-15 22:29                 ` William Chesters
2000-04-16 22:24                 ` Nickolay Semyonov
2000-04-18  6:52                   ` Max Skaller
2000-04-17 12:51                 ` jean-marc alliot
2000-04-17 17:49                   ` John Max Skaller
2000-04-17 22:34                     ` Brian Rogoff
2000-04-19 15:31                       ` John Max Skaller
2000-04-19 18:30                       ` Michael Hicks
2000-04-20 16:40                       ` Markus Mottl
2000-04-20 17:58                         ` Brian Rogoff
2000-04-20 18:52                           ` Markus Mottl
2000-04-21 20:44                             ` Michael Hohn
2000-04-21 19:22                           ` John Max Skaller
2000-04-21 19:09                         ` John Max Skaller
2000-04-21 19:45                           ` Markus Mottl
2000-04-21 19:56                           ` Brian Rogoff
2000-04-21 19:18                         ` John Max Skaller
2000-04-18 10:53                     ` Sven LUTHER
2000-04-19 15:57                       ` John Max Skaller
2000-04-13  7:05             ` Pierre Weis
2000-04-13 17:04               ` Julian Assange
2000-04-07 15:44 ` John Max Skaller
2000-05-11 13:48 reference initialization Dave Berry
2000-05-11 14:28 Stephanie Weirich
2000-05-12 20:38 ` Hongwei Xi
2000-05-15  8:49   ` Xavier Leroy
2000-05-15 17:47     ` Hongwei Xi
2000-05-15 21:33       ` Pierre Weis
2000-05-16  2:53         ` Hongwei Xi
2000-05-18 16:16           ` Pierre Weis
2000-05-19  6:54             ` Max Skaller
2000-05-22 15:28               ` Pierre Weis
2000-05-22 22:29                 ` Max Skaller
2000-05-15 22:20       ` Dave Mason
2000-05-15  9:36   ` Eijiro Sumii
2000-05-20 20:13 Simon Peyton-Jones
2000-05-22 16:10 ` Pierre Weis

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=391EBA3B.26824324@maxtal.com.au \
    --to=skaller@maxtal.com.au \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=hwxi@ececs.uc.edu \
    /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).