[Haskell-cafe] Template Haskell parameterizing on type

Joel Reymont joelr1 at gmail.com
Thu Nov 10 07:31:51 EST 2005


Anyone?

On Nov 9, 2005, at 4:49 AM, Joel Reymont wrote:

> Folks,
>
> How can I create a generic pickler that's parameterized on type and  
> constructor?
>
> I define types like this:
>
> newtype AvgPot = AvgPot Word64 deriving (Show, Typeable)
> newtype NumberOfPlayers = NumberOfPlayers Word16 deriving (Show,  
> Typeable)
>
> and then have a lot of boiler-plate code like this, a pickler for  
> each type:
>
> puAvgPot :: PU AvgPot
> puAvgPot = wrap (\a -> AvgPot a, \(AvgPot b) -> b) endian
>
> where
>
> data PU a = PU { appP :: (a, [Word8]) -> [Word8],
>                  appU :: [Word8] -> (a, [Word8]) }
>
> wrap :: (a -> b, b -> a) -> PU a -> PU b
>
> What I would like to have is a generic pickler that's parameterized  
> on constructor and type. These can and will be the same, though. I  
> want something like this, how can it be done in Template Haskell or  
> otherwise?
>
> pickler :: PU T
> pickler Con pa = wrap (\a -> Con a, \(Con b) -> b) pa

--
http://wagerlabs.com/







More information about the Haskell-Cafe mailing list