[Haskell-cafe] Memoisation + unsafePerformIO

Tony Morris tmorris at tmorris.net
Sun Jul 8 22:11:17 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello everyone,
I am trying to write a generalisation of memoisation for a couple of
backtracking algorithms that I wrote (which don't specify a memoisation
technique), by keeping a local destructive update using unsafePerformIO
and IORef - neither of which I have used before and I am struggling to
figure out. I am going from the document:
http://haskell.org/hawiki/GlobalMutableState

I am not sure if:
a) this is even possible
b) if a) holds, if this is a good thing

Here is some code to help demonstrate what I intend:

{-# OPTIONS_GHC -fglasgow-exts #-}
import Data.Ix

- -- implementations must guarantee that
- -- memo f k is equivalent to f k
- -- but may otherwise use local destructive updates
class Memo k v where
  memo :: (k -> v) -> k -> v

instance (Ix k) => Memo k v where
  memo f k = error("todo: array")

instance (Ord k) => Memo k v where
  memo f k = error("todo: binary tree")

- --
Tony Morris
http://tmorris.net/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGkZlFmnpgrYe6r60RArT8AJ4iFVyzmUN1pdxpMBokZpj48CiqRgCfReIe
2yZ55LMcFs24TJOVeCV4tbo=
=IR5s
-----END PGP SIGNATURE-----


More information about the Haskell-Cafe mailing list