
Hi all, I have below duplicate code, but i don't know how to use TH instance code. ------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x instance Variable Int where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x instance Variable (Maybe Char) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x instance Variable (Maybe Int) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x instance Variable ProcessID where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x instance Variable GWindowId where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x ------------------------------> duplicate code end <------------------------------ Any TH expert help? Thanks, -- Andy

What you're looking for is something like:
deriveVariable _t = [d|
instance Variable $t where
toVariant = toVariant . show
fromVariant x = fmap (\v -> read v :: $t) $ fromVariant x|]
deriveVariable (conT ''PageType)
deriveVariable (conT ''Int)
deriveVariable (conT ''Maybe `appT` conT ''Char)
...
On Tue, Jun 22, 2010 at 11:24 AM, Andy Stewart
Hi all,
I have below duplicate code, but i don't know how to use TH instance code.
------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
instance Variable Int where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x
instance Variable (Maybe Char) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x
instance Variable (Maybe Int) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x
instance Variable ProcessID where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x
instance Variable GWindowId where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x ------------------------------> duplicate code end <------------------------------
Any TH expert help?
Thanks,
-- Andy
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

v-- I had accidentally elided the _'s before the t's in the quasiquotation before. What you're looking for is something like: deriveVariable _t = [d| instance Variable $_t where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: $_t) $ fromVariant x|] deriveVariable (conT ''PageType) deriveVariable (conT ''Int) deriveVariable (conT ''Maybe `appT` conT ''Char) ...
On Tue, Jun 22, 2010 at 11:24 AM, Andy Stewart
wrote: Hi all,
I have below duplicate code, but i don't know how to use TH instance code.
------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
instance Variable Int where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x
instance Variable (Maybe Char) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x
instance Variable (Maybe Int) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x
instance Variable ProcessID where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x
instance Variable GWindowId where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x ------------------------------> duplicate code end <------------------------------
Any TH expert help?
Thanks,
-- Andy
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

I have below duplicate code, but i don't know how to use TH instance code.
------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
If this isn't an exercise to learn TH, you might also want to try scoped type variables (7.8.7) to connect the 'read v' annotation to the instance head: http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/other-type-extension... Claus

Hi Edward,
Thank you very much, your code works perfect!
-- Andy
Edward Kmett
v-- I had accidentally elided the _'s before the t's in the quasiquotation before.
What you're looking for is something like:
deriveVariable _t = [d| instance Variable $_t where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: $_t) $ fromVariant x|]
deriveVariable (conT ''PageType) deriveVariable (conT ''Int) deriveVariable (conT ''Maybe `appT` conT ''Char) ...
On Tue, Jun 22, 2010 at 11:24 AM, Andy Stewart
wrote: Hi all,
I have below duplicate code, but i don't know how to use TH instance code.
------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
instance Variable Int where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x
instance Variable (Maybe Char) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x
instance Variable (Maybe Int) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x
instance Variable ProcessID where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x
instance Variable GWindowId where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x ------------------------------> duplicate code end <------------------------------
Any TH expert help?
Thanks,
-- Andy
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Jun 22, 2010 at 6:24 PM, Andy Stewart
Hi all,
I have below duplicate code, but i don't know how to use TH instance code.
------------------------------> duplicate code start <------------------------------ instance Variable PageType where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
instance Variable Int where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x
instance Variable (Maybe Char) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x
instance Variable (Maybe Int) where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x
instance Variable ProcessID where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x
instance Variable GWindowId where toVariant = toVariant . show fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x ------------------------------> duplicate code end <------------------------------
Any TH expert help?
Thanks,
-- Andy
mkInstance :: Name -> Q Dec mkInstance n = do tv <- [|toVariant . show|] fv <- [|fmap read . fromVariant|] return $ InstanceD [] (ConT ''Variable `AppT` ConT n) [ FunD (mkName "toVariant") [Clause [] [NormalB tv] []] , FunD (mkName "fromVariant") [Clause [] [NormalB fv] []] ] mkInstances = mapM mkInstance mkInstances [''PageType, ''Int, ..] Haven't tried it, but it should be pretty close. Michael
participants (4)
-
Andy Stewart
-
Claus Reinke
-
Edward Kmett
-
Michael Snoyman