[add optimization that will find when top level functions call themselves recursively and turn it into explicit loops
John Meacham <john@repetae.net>**20100226033526
 Ignore-this: 1fb6ac6071490c3ea25b98201c1e3c09
] hunk ./src/Grin/Main.hs 24
-import qualified Grin.SSimplify
+import Grin.SSimplify
hunk ./src/Grin/Main.hs 37
+    x <- explicitRecurse x
hunk ./src/Grin/SSimplify.hs 1
-module Grin.SSimplify(simplify) where
+module Grin.SSimplify(simplify,explicitRecurse) where
hunk ./src/Grin/SSimplify.hs 10
+import Support.Tickle
hunk ./src/Grin/SSimplify.hs 401
+
+-- this finds top level functions that call themselves recursively and turns the recursive call into a
+-- local definition, allowing it to be compiled to a direct loop.
+
+explicitRecurse
+    :: Grin
+    -> IO Grin
+explicitRecurse grin =  mapGrinFuncsM f grin where
+    f name lam | name `Set.notMember` (freeVars lam) = return lam
+    f name (as :-> e) = do
+        let nname = toAtom $ "bR" ++ fromAtom name
+            g (App n rs t) | n == name = App nname rs t
+            g e = tickle g e
+        return $ as :-> grinLet [createFuncDef True nname (as :-> g e) ] (App nname as (getType e))
+