
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/

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

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
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Cale Gibbard
-
Joel Reymont