[Haskell-cafe] "Rewrite thunk action" rule?

Peter Todd pete at petertodd.org
Sun Dec 21 03:02:01 EST 2008

I have a program with this data structure:

data Element = Element {
    elementOrigin :: V,
    elementSubs :: [Element]

and this important bit of code that operates on it:

transform :: T -> Element -> Element
transform t e = Element {
                    elementOrigin = tmulv t (elementOrigin e),
                    elementSubs = map (transform t) (elementSubs e)
Where V is a vertex and T is a matrix transformation. The following is true:

transform a (transform b e) == transform (a * b) e

I have read about rewrite rules, such would efficiently rewrite the obvious
transformations. However, given that the program will be applying *many*
transformations to very deep and wide Element trees, and applying those
transformations at many levels, I don't see how the optimizer would be able to
handle all cases, for instance a thunk getting left over from perhaps
evaluation prior to some IO function.  

FWIW here's an example "tree-builder" I'm using to test with:

mkElems 0 _ = Element {
                elementOrigin = V 0 0,
                elementSubs = []
mkElems depth width = Element {
                elementOrigin = V 0 0,
                elementSubs = replicate width $ transform (rotation (pi / (fromIntegral width))) $ transform (translation $ V 1 1) $ mkElems (depth - 1) width

with depth = width = 5

If I could somehow arrange for the transform function to detect when it
is being applied to a unevaluated thunk, and then modify the thunk in
place, that would basically be the behavior I need. Any suggestions?

http://petertodd.org 'peter'[:-1]@petertodd.org 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20081221/5a1cfbc8/attachment.bin

More information about the Haskell-Cafe mailing list