[Haskell-cafe] Parameterizing on type

Cale Gibbard cgibbard at gmail.com
Thu Nov 10 11:45:00 EST 2005


I came up with the following, but perhaps someone more familiar with
TH could clean it up.

pickler :: String -> Q Exp
pickler con = do
    x <- newName "x"
    [| \pu -> wrap ($(conE con'), $(lamE [conP con' [varP x]] (varE x))) pu |]
        where con' = mkName con

-- in a separate module:
puAvgPot = $(pickler "AvgPot") endian

 - Cale

On 08/11/05, Joel Reymont <joelr1 at gmail.com> 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
>
>         Thanks, Joel
>
> --
> http://wagerlabs.com/
>
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list