module Control.Monad.Laws.Instances where
import Control.Monad.State
import Control.Monad.Laws
import Test.ClassLaws
import Test.ClassLaws.TestingDatatypes(MyList(..), (+++), snoc, foldrMyList)
instance FunctorLaws [] where
functorLaw1 xs = addSteps (defaultFunctorLaw1 xs)
(case xs of
[] -> nilCase
ys@(_:_) -> conCase ys)
where
nilCase =
[ fmap id []
,
[]
]
conCase (y:ys) =
[ fmap id (y:ys)
,
id y:fmap id ys
,
y:fmap id ys
,
y:ys
,
id (y:ys)
]
testFunctorList
= do quickLawCheck (undefined::FunctorLaw1 Char [])
quickFLawCheck (undefined::FunctorLaw2 Int Char Bool [])
instance FunctorLaws Maybe
testFunctorMaybe
= do quickLawCheck (undefined::FunctorLaw1 Char Maybe)
quickFLawCheck (undefined::FunctorLaw2 Int Char Bool Maybe)
instance FunctorLaws IO
instance Functor MyList where
fmap f Nil = Nil
fmap f (Cons x xs) = snoc (f x) (fmap f xs)
instance FunctorLaws MyList
where
functorLaw1 xs = addSteps (defaultFunctorLaw1 xs)
(case xs of
Nil -> nilCase
zs@(Cons y ys) -> conCase zs)
where
nilCase =
[ fmap id Nil
,
Nil
]
conCase (Cons y ys) =
[ fmap id (Cons y ys)
,
snoc (id y) (fmap id ys)
,
snoc y (fmap id ys)
,
snoc y ys
,
id (Cons y ys)
]
testFunctorMyList
= do quickLawCheck (undefined::FunctorLaw1 Int MyList)
quickFLawCheck (undefined::FunctorLaw2 Char Int Int MyList)
instance MonadLaws []
testMonadList
= do quickFLawCheck (undefined::MonadLaw1 Char Int [])
quickLawCheck (undefined::MonadLaw2 Int [])
quickFLawCheck (undefined::MonadLaw3 Int Bool Char [])
instance MonadLaws Maybe
testMonadMaybe
= do quickFLawCheck (undefined::MonadLaw1 Char Int Maybe)
quickLawCheck (undefined::MonadLaw2 Int Maybe)
quickFLawCheck (undefined::MonadLaw3 Int Bool Char Maybe)
instance FunctorMonadLaws MyList
testFunctorMonadMyList
= do quickFLawCheck (undefined:: FunctorMonadLaw Char Int MyList)
instance MonadLaws IO
instance MonadLaws (State s)
testMonadState
= do quickFLawCheck (undefined::MonadLaw1 Bool Int (State Bool))
quickFLawCheck (undefined::MonadLaw2 Int (State Bool))
quickFLawCheck (undefined::MonadLaw3 Int Bool Char (State Bool))
instance Monad MyList where
m >>= k = foldrMyList ((+++) . k) Nil m
m >> k = foldrMyList ((+++) . (\ _ -> k)) Nil m
return x = Cons x (Cons x Nil)
fail _ = Nil
instance MonadLaws MyList
testMonadMyList
= do quickFLawCheck (undefined::MonadLaw1 Char Int MyList)
quickLawCheck (undefined::MonadLaw2 Int MyList)
quickFLawCheck (undefined::MonadLaw3 Int Bool Char MyList)
instance FunctorMonadLaws []
testFunctorMonadList
= do quickFLawCheck (undefined::FunctorMonadLaw Char Int [])
instance FunctorMonadLaws Maybe
testFunctorMonadMaybe
= do quickFLawCheck (undefined::FunctorMonadLaw Char Int Maybe)
instance FunctorMonadLaws IO
main = do testMonadMaybe
testMonadState
testFunctorList
testFunctorMaybe
testFunctorMonadList
testFunctorMonadMaybe
expectedFailures = do
testMonadMyList
testFunctorMyList
testFunctorMonadMyList