Last active
February 21, 2020 06:48
-
-
Save cblp/40dbc904b8ee689aa1f3f7b771370329 to your computer and use it in GitHub Desktop.
Transparent type annotations in Haskell
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 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 |
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 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