<p>Hi, <br>
Then how about p2num, how to memorize this function.</p>
<p>Also I think it&#39;s okay to memorize this kind of function. The type system ensure all calling of montgKeys have the same type, e.g., it&#39;s a pure function without parameter, it&#39;s safe to memorize it since it didn&#39;t occupy more memory than representing dynamic generated types.</p>

<div class="gmail_quote">On Nov 6, 2011 11:06 PM, &quot;Yucheng Zhang&quot; &lt;<a href="mailto:yczhang89@gmail.com">yczhang89@gmail.com</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Sun, Nov 6, 2011 at 9:35 PM, Bin Jin &lt;<a href="mailto:bjin1990@gmail.com">bjin1990@gmail.com</a>&gt; wrote:<br>
&gt; Hi,<br>
&gt; Since I actually didn&#39;t use the parameter in calculation, the return value<br>
&gt; only depends on the type<br>
&gt; of input, not the actually value. If it&#39;s impossible to cache the result, is<br>
&gt; there another way to<br>
&gt; memorize this &quot;function&quot; ?<br>
<br>
Sorry, I haven&#39;t considered about &#39;number parameterized<br>
type&#39; when I answered the question.<br>
<br>
However, you can still use a data structure like MemoTrie [1]<br>
to memorize the function. The memorization is trivial, since<br>
you can convert between the number-typed &#39;undefined&#39; and<br>
&#39;Integer&#39; with the functions &#39;p2num&#39; and &#39;num2p&#39; in your<br>
code. I&#39;ve not tested, but this is an example using MemoTrie:<br>
<br>
&gt; import Data.MemoTrie<br>
&gt;<br>
&gt; memoMontgKeys :: (PostiveN p, Integral a, Bits a) =&gt; p -&gt; a<br>
&gt; memoMontgKeys = memoMontgKeys&#39; . p2num<br>
&gt;<br>
&gt; memoMontgKeys&#39; :: (Integral a) =&gt; Integer -&gt; a<br>
&gt; memoMontgKeys&#39; = memo (montgKeys . num2p)<br>
<br>
On the other hand, I think GHC is not expected to do the<br>
memorization automatically. An arbitrary number can turn up<br>
as the argument type of &#39;montgKeys&#39;. This is similar to a<br>
function with an Integer argument, which GHC does not<br>
memorize now.<br>
<br>
<br>
[1] <a href="http://hackage.haskell.org/package/MemoTrie" target="_blank">http://hackage.haskell.org/package/MemoTrie</a><br>
<br>
<br>
<br>
Yucheng Zhang<br>
</blockquote></div>