Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell caching results of a function

Tags:

I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could cache the results. Something like

let cachedFunction = createCache slowFunction in (cachedFunction 3.1) + (cachedFunction 4.2) + (cachedFunction 3.1) 

I was looking into Data.Array and although the array is lazy, I need to initialize it with a list of pairs (using listArray) - which is impractical . If the 'key' is e.g. the 'Double' type, I cannot initialize it at all, and even if I can theoretically assign an Integer to every possible input, I have several tens of thousands possible inputs and I only actually use a handful. I would need to initialize the array (or, preferably a hash table, as only a handful of resutls will be used) using a function instead of a list.

Update: I am reading the memoization articles and as far as I understand it the MemoTrie could work the way I want. Maybe. Could somebody try to produce the 'cachedFunction'? Prefereably for a slow function that takes 2 Double arguments? Or, alternatively, that takes one Int argument in a domain of ~ [0..1 billion] that wouldn't eat all memory?

like image 868
ondra Avatar asked Feb 07 '10 15:02

ondra


2 Answers

Well, there's Data.HashTable. Hash tables don't tend to play nicely with immutable data and referential transparency, though, so I don't think it sees a lot of use.

For a small number of values, stashing them in a search tree (such as Data.Map) would probably be fast enough. If you can put up with doing some mangling of your Doubles, a more robust solution would be to use a trie-like structure, such as Data.IntMap; these have lookup times proportional primarily to key length, and roughly constant in collection size. If Int is too limiting, you can dig around on Hackage to find trie libraries that are more flexible in the type of key used.

As for how to cache the results, I think what you want is usually called "memoization". If you want to compute and memoize results on demand, the gist of the technique is to define an indexed data structure containing all possible results, in such a way that when you ask for a specific result it forces only the computations needed to get the answer you want. Common examples usually involve indexing into a list, but the same principle should apply for any non-strict data structure. As a rule of thumb, non-function values (including infinite recursive data structures) will often be cached by the runtime, but not function results, so the trick is to wrap all of your computations inside a top-level definition that doesn't depend on any arguments.

Edit: MemoTrie example ahoy!

This is a quick and dirty proof of concept; better approaches may exist.

{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} import Data.MemoTrie import Data.Binary import Data.ByteString.Lazy hiding (map)  mangle :: Double -> [Int] mangle = map fromIntegral . unpack . encode  unmangle :: [Int] -> Double unmangle = decode . pack . map fromIntegral  instance HasTrie Double where     data Double :->: a = DoubleTrie ([Int] :->: a)     trie f = DoubleTrie $ trie $ f . unmangle     untrie (DoubleTrie t) = untrie t . mangle  slow x      | x < 1 = 1     | otherwise = slow (x / 2) + slow (x / 3)  memoSlow :: Double -> Integer memoSlow = memo slow 

Do note the GHC extensions used by the MemoTrie package; hopefully that isn't a problem. Load it up in GHCi and try calling slow vs. memoSlow with something like (10^6) or (10^7) to see it in action.

Generalizing this to functions taking multiple arguments or whatnot should be fairly straightforward. For further details on using MemoTrie, you might find this blog post by its author helpful.

like image 105
C. A. McCann Avatar answered Oct 26 '22 10:10

C. A. McCann


See memoization

like image 31
Jeff Foster Avatar answered Oct 26 '22 08:10

Jeff Foster