-
-
Save cblp/c76c7c04d17b99e867c285712e95e003 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module A where | |
import Language.Haskell.TH | |
makeType :: String -> DecsQ | |
makeType t = do | |
let name = mkName t | |
pure [DataD [] name [] Nothing [NormalC name []] []] | |
useType :: String -> String -> DecsQ | |
useType t f = do | |
let tname = mkName t | |
fname = mkName f | |
sequence | |
[ sigD fname [t| $(conT tname) -> IO () |] | |
, funD fname [clause [wildP] (normalB [| print $(stringE t) |]) []] | |
] | |
makeAndUse :: String -> DecsQ | |
makeAndUse t = | |
concat <$> sequenceA | |
[ makeType t | |
, useType t ("show" ++ t) | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
import A | |
-- makeType "Hello" | |
-- useType "Hello" "showHello" | |
makeAndUse "Hello" | |
main :: IO () | |
main = showHello Hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment