1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
module Data.Derive.Is(makeIs) where
{-
test :: Sample
isFirst :: Sample a -> Bool
isFirst (First{}) = True ; isFirst _ = False
isSecond :: Sample a -> Bool
isSecond (Second{}) = True ; isSecond _ = False
isThird :: Sample a -> Bool
isThird (Third{}) = True ; isThird _ = False
-}
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 =
[TypeSig sl [name nam] (dataDeclType d `TyFun` tyCon "Bool")
,FunBind $ match : [defMatch | length (dataDeclCtors d) > 1]]
where
nam = "is" ++ ctorDeclName c
match = Match sl (name nam) [PParen $ PRec (qname $ ctorDeclName c) []] Nothing (UnGuardedRhs $ con "True") (BDecls [])
defMatch = Match sl (name nam) [PWildCard] Nothing (UnGuardedRhs $ con "False") (BDecls [])
|