[clean-list] Macro facility able to encode program transformation?

David C. Norris David_Norris at brown.edu
Sat Oct 6 01:17:39 MEST 2007


The following code demonstrates a program transformation that converts a 
concise implementation of convolution to a highly efficient one (as fast 
as GCC-compiled C).  This transformation, applicable generally to folds 
over comprehensions, looks like a good candidate for automation.  Is 
Clean's macro facility powerful enough to encode a transformation of 
this nature within a Clean program?  Or would automating this 
transformation require preprocessing the Clean program text?

Regards,
David

// A slow version of convolution
slowconvolve :: (ar e) (ar e) -> (ar e) | Array ar e & Arith e
slowconvolve a b = {let start = max 0 (i-m)
                        end   = min i n
                    in
// 
--------------------------------------------------------------------------------
                        foldl (+) zero [ a.[i-k] * b.[k] \\ k <- 
[start..end] ]
// 
--------------------------------------------------------------------------------
                    \\ i <- [0..(m+n)]}
where
    m = size a - 1
    n = size b - 1

// Transforming the indicated line of the above function as follows 
produces a
// highly efficient function with performance on par with GCC-compiled C:
convolve :: (ar e) (ar e) -> (ar e) | Array ar e & Arith e
convolve a b = {let start = max 0 (i-m)
                    end   = min i n
                in
// 
--------------------------------------------------------------------------------
                    let roll k kend = roll zero k kend
                        where
                            roll result k kend
                            | k > kend  = result
                            | otherwise = roll ((+) result (a.[i-k] * 
b.[k])) (k+1) kend
                    in roll start end
// 
--------------------------------------------------------------------------------
                \\ i <- [0..(m+n)]}
where
    m = size a - 1
    n = size b - 1



More information about the clean-list mailing list