{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StandaloneDeriving #-}
module Language.JVM.TH
( deriveBase
, deriveBases
, deriveThese
, deriveBaseWithBinary
) where
import Language.Haskell.TH
import GHC.Generics
import Control.DeepSeq
import Data.Binary
import Language.JVM.Stage
deriveThese :: Name -> [Name] -> Q [Dec]
deriveThese name items =
return . concat $ do
x <- ConT <$> items
return
[ StandaloneDerivD Nothing [] (AppT x (AppT n (ConT ''High)))
, StandaloneDerivD Nothing [] (AppT x (AppT n (ConT ''Low)))
]
where n = ConT name
deriveBase :: Name -> Q [Dec]
deriveBase name =
concat <$> sequence
[ [d|deriving instance Show ($n Low)|]
, [d|deriving instance Eq ($n Low)|]
, [d|deriving instance Generic ($n Low)|]
, [d|deriving instance NFData ($n Low)|]
, [d|deriving instance Ord ($n Low)|]
, [d|deriving instance Show ($n High)|]
, [d|deriving instance Eq ($n High)|]
, [d|deriving instance Generic ($n High)|]
, [d|deriving instance NFData ($n High)|]
]
where n = conT name
deriveBases :: [Name] -> Q [Dec]
deriveBases names =
concat <$> mapM deriveBase names
deriveBaseWithBinary :: Name -> Q [Dec]
deriveBaseWithBinary name = do
b <- deriveBase name
m1 <- deriveBinary name
return (b ++ m1)
deriveBinary :: Name -> Q [Dec]
deriveBinary name =
[d|deriving instance Binary ($n Low)|]
where
n = conT name