[Template-haskell] Newbie question - class dependencies

Ian Lynagh igloo at earth.li
Sun Jul 8 12:24:15 EDT 2007


On Sun, Jul 08, 2007 at 05:01:21PM +0100, Hugo Pacheco wrote:
> 
> module Main where
> 
> myprint :: a -> String
> myprint = show
> 
> main = do
>    putStr $ myprint 1
>    putStr $ myprint '2'
> 
> My ideia would be to stage the computation of *myprint* until it is called,
> where the *Show* instance can be satisfied.

Do you mean that you want to have

    myprint :: Integer -> String
    myprint = show

generated when GHC notices that myprint is called with an Integer type?
Is so, then no, TH, can't do that.

What you can do is to write a function

    mkMyPrint :: Type -> Q [Dec]

which you could then call like so:

    $( mkMyPrint ''Integer )

but you have to do so by hand.


Thanks
Ian



More information about the template-haskell mailing list