[<<][meta][>>][..]
Sat Feb 13 19:48:52 CET 2010

Flattening expressions into SSA

-- Convert list of terms to dictionary and list of varrefs.
type Dict a = [(Term a, Symbol)]
anf :: [Term a] -> Dict a -> (Dict a, [Term a])

anf = f [] where
    f rs [] d = (d, reverse rs)
    f rs (t:ts) d =
        case t of
          Op rator rands -> 
              let (d',rands') = f [] rands d   -- recurse down terms
                  sym = gensym d'              -- create variable
                  t'  = Op rator rands'        -- transform term
                  d'' = (t', sym) : d'         -- extend environment
              in f (Var sym : rs) ts d''
          _ -> f (t:rs) ts d
    
-- Generate a unique variable name
gensym = Symbol . ("r" ++) . show . length


-- This is just a dumb tree flattener.  It won't enable sharing for
-- copied expressions.  Now, how to extend this to enable CSE?



[Reply][About]
[<<][meta][>>][..]