[Haskell-cafe] Vertigo / GHC / Memo

Peter Verswyvelen bf3 at telenet.be
Thu Nov 15 14:49:28 EST 2007

Hi Haskell Lovers,

I’m trying to compile Vertigo (http://conal.net/Vertigo), which seems to be
a module from which I can really learn a lot of stuff.
However, it seems to use a Memo module, which does not seem to be part of
GHC (anymore?) As far as I understand, it uses a memo function to make sure
that when performing symbolic differentiation, it does not get stuck in an
endless loop.

As I realize no generic memoization function can be made, I guess it got
removed at some point?

Can anyone give me a hint how to find a suitable Memo module for Vertigo? 

As a sidenote, I must say the memoization section on Haskell.org is not
really helpful for a newbie like me... 


PS: I adapted the following code from SOE, but I'm not sure it does the job
correctly (it will certainly be slow...)

{-# OPTIONS_GHC -fglasgow-exts #-}

module Memo(memo) where

import Data.IORef
import System.IO.Unsafe

memo :: Eq a => (a->b) -> (a->b)
memo f = unsafePerformIO $ do
   cache <- newIORef []
   return $ \x -> 
     unsafePerformIO $ do
       vals <- readIORef cache
       case x `inCache` vals of
         Nothing -> do 
           let y = f x
           modifyIORef cache ((x,y):)
           return y
         Just y  -> do 
           return y

inCache :: Eq a => a -> [(a,b)] -> Maybe b
x `inCache` [] = Nothing
x `inCache` ((x',y'):xys) =
   if x == x' 
   then Just y' 
   else x `inCache` xys

More information about the Haskell-Cafe mailing list