Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The heart is the <code>bits</code> function:</p> <pre><code>-- | Memoize an ordered type with a bits instance. bits :: (Ord a, Bits a) =&gt; Memo a bits f = IntTrie.apply (fmap f IntTrie.identity) </code></pre> <p>It is the only function (except the trivial <code>unit :: Memo ()</code>) which can give you a <code>Memo a</code> value. It uses the same idea as in this <a href="http://www.haskell.org/haskellwiki/Memoization" rel="noreferrer">page</a> about Haskell memoization. Section 2 shows the simplest memoization strategy using a list and section 3 does the same using a binary tree of naturals similar to the <code>IntTree</code> used in memocombinators.</p> <p>The basic idea is to use a construction like <code>(map fib [0 ..] !!)</code> or in the memocombinators case - <code>IntTrie.apply (fmap f IntTrie.identity)</code>. The thing to notice here is the correspondance between <code>IntTie.apply</code> and <code>!!</code> and also between <code>IntTrie.identity</code> and <code>[0..]</code>.</p> <p>The next step is memoizing functions with other types of arguments. This is done with the <code>wrap</code> function which uses an isomorphism between types <code>a</code> and <code>b</code> to construct a <code>Memo b</code> from a <code>Memo a</code>. For example:</p> <pre><code>Memo.integral f =&gt; wrap fromInteger toInteger bits f =&gt; bits (f . fromInteger) . toInteger =&gt; IntTrie.apply (fmap (f . fromInteger) IntTrie.identity) . toInteger ~&gt; (semantically equivalent) (map (f . fromInteger) [0..] !!) . toInteger </code></pre> <p>The rest of the source code deals with types like List, Maybe, Either and memoizing multiple arguments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload