[Haskell-cafe] memorize function with number parameterized types in GHC

Bin Jin bjin1990 at gmail.com
Tue Nov 8 16:53:09 CET 2011


Hi,
Thanks for your reply!
I made some changes according to your suggest. Now I get rid of
argument p. Unfortunately, GHC is not smart enough to memorize
this true polymorphic constant.
Can you give some hints on what kind of specialize pragmas I should
use?

Regards,
Bin Jin


On Tue, Nov 8, 2011 at 2:35 PM, <oleg at okmij.org> wrote:

>
> Bin Jin wrote:
>
> > Here is a function that will be called everytime by `(*)` in `Num`
> typeclass
> > > montgKeys :: (PostiveN p, Integral a, Bits a) => p -> a
> >
> > as you can imagine, I always pass (undefined :: p) as parameter to
> > `montgKeys`, so if it's handled well, it should be memorized for
> > future usage. But tracing shows that both `p2num` and `montgKeys` are
> > evaluated every time being called.
>
> First of all, let us get rid of the argument p. Let's define
>
> > newtype W p a = W{unW:: a}
>
> then we can easily re-write montgKeys to give it the following signature:
>
> > > montgKeys :: (PostiveN p, Integral a, Bits a) => W p a
>
> You can use ScopedTypevariables to set the needed 'p' from the context.
>
> So, montgKeys becomes a polymorphic constant, quite like minBound.
> Now, the hope is that when the types p and a are determined, GHC could
> specialize montgKeys and turn it into a real constant. Perhaps some
> RULE or specialize pragmas may help...
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111108/307a496c/attachment.htm>


More information about the Haskell-Cafe mailing list