module Data.Derive.Is(makeIs) where
import Language.Haskell
import Data.Derive.Internal.Derivation
makeIs :: Derivation
makeIs = derivationCustom "Is" $ \(_,d) -> Right $ concatMap (makeIsCtor d) $ dataDeclCtors d
makeIsCtor :: DataDecl -> CtorDecl -> [Decl ()]
makeIsCtor d c = if not $ isIdent $ ctorDeclName c then [] else
[TypeSig () [name nam] (TyFun () (dataDeclType d) (tyCon "Bool"))
,FunBind () $ match : [defMatch | length (dataDeclCtors d) > 1]]
where
nam = "is" ++ ctorDeclName c
match = Match () (name nam) [PParen () $ PRec () (qname $ ctorDeclName c) []] (UnGuardedRhs () $ con "True") Nothing
defMatch = Match () (name nam) [PWildCard ()] (UnGuardedRhs () $ con "False") Nothing