[clean-list] Transform to CPS

Erik Zuurbier EZuurbier@Abz.nl
Tue, 4 Feb 2003 16:20:59 +0100


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C2CC61.055CD860
Content-Type: text/plain

Hi cleaners

Does anybody of you know of a systematic way to transform a function (and
its context) to a version using continuations - assuming that performance
improves? And is there a way to systematically identify opportunities for
such transformations?

For instance, I know how to write a function that lists all items of a tree:

:: Tree a = Nil | Node (Tree a) a (Tree a)

tree2list :: (Tree a) -> [a]
tree2list Nil = []
tree2list (Node left a right) = (tree2list left) ++ [a] ++ (tree2list right)

and then how to speed this up using a continuation:

tree2list` :: (Tree a) [a] -> [a]
tree2list` Nil cont = cont
tree2list` (Node left a right) cont = tree2list` left [a:tree2list` right
cont]

to be called with an empty list as its second parameter.

But identifying and carrying out this transformation to me looks more like
black magic than a craft.

Regards Erik Zuurbier

------_=_NextPart_001_01C2CC61.055CD860
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">



Transform to CPS



Hi cleaners

Does anybody of you know of a = systematic way to transform a function (and its context) to a version = using continuations - assuming that performance improves? And is there = a way to systematically identify opportunities for such = transformations?

For instance, I know how to write a = function that lists all items of a tree:

:: Tree a =3D Nil | Node (Tree a) a = (Tree a)

tree2list :: (Tree a) -> [a]
tree2list Nil =3D []
tree2list (Node left a right) =3D = (tree2list left) ++ [a] ++ (tree2list right)

and then how to speed this up using a = continuation:

tree2list` :: (Tree a) [a] -> = [a]
tree2list` Nil cont =3D cont
tree2list` (Node left a right) cont = =3D tree2list` left [a:tree2list` right cont]

to be called with an empty list as its = second parameter.

But identifying and carrying out this = transformation to me looks more like black magic than a craft.

Regards Erik Zuurbier

------_=_NextPart_001_01C2CC61.055CD860--