1module Control.Applicative.OrphansSpec (main, spec) where 2 3import Test.Hspec 4import Control.Applicative 5import Data.Orphans () 6import Data.Monoid 7import Prelude 8 9-- simplest one to use 10newtype Identity a = Identity { runIdentity :: a } 11 12instance Functor Identity where 13 fmap f = Identity . f . runIdentity 14 15instance Applicative Identity where 16 pure = Identity 17 Identity f <*> x = f <$> x 18 19instance Monad Identity where 20 return = Identity 21 m >>= k = k (runIdentity m) 22 23main :: IO () 24main = hspec spec 25 26spec :: Spec 27spec = do 28 describe "Monoid (Const a b)" $ do 29 it "mempty returns an empty const" $ 30 getConst (mempty :: (Const String Int)) `shouldBe` "" 31 it "mappends const part" $ 32 getConst ((Const "aaa" :: Const String Int) `mappend` (Const "bbb" :: Const String Int)) 33 `shouldBe` "aaabbb" 34 35 describe "Monad (WrappedMonad m)" $ 36 it "allows to use a Monad interface in a WrappedMonad" $ 37 (runIdentity . unwrapMonad 38 $ (WrapMonad (return 1 :: Identity Int)) 39 >> (WrapMonad (return 2 :: Identity Int))) 40 `shouldBe` (2::Int) 41