Skip to content

Instantly share code, notes, and snippets.

@cblp
Last active February 21, 2020 06:48
Show Gist options
  • Save cblp/40dbc904b8ee689aa1f3f7b771370329 to your computer and use it in GitHub Desktop.
Save cblp/40dbc904b8ee689aa1f3f7b771370329 to your computer and use it in GitHub Desktop.
Transparent type annotations in Haskell
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
import Meta
data R = R
{ bravo :: Bool
, charlie :: Char // "Charlie docs"
}
$(mkMeta 'charlie)
-- meta_charlie = "LitT (StrTyLit \"Charlie docs\")"
main :: IO ()
main = putStrLn meta_charlie
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
module Meta where
import Control.Monad
import Language.Haskell.TH
type a // b = a
mkMeta name = do
VarI _ typ _ <- reify name
let AppT _record (AppT (AppT (ConT con) _field) ann) = typ
unless (con == ''(//)) $
fail $ show con
let metaName = mkName $ "meta_" <> nameBase name
[d| $(varP metaName) = $(stringE $ show ann) |]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment