Skip to content

Instantly share code, notes, and snippets.

@cblp
Created August 7, 2018 13:30
Show Gist options
  • Save cblp/c76c7c04d17b99e867c285712e95e003 to your computer and use it in GitHub Desktop.
Save cblp/c76c7c04d17b99e867c285712e95e003 to your computer and use it in GitHub Desktop.
{-# 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)
]
{-# 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