Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created August 31, 2011 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tarrasch/1182960 to your computer and use it in GitHub Desktop.
Save Tarrasch/1182960 to your computer and use it in GitHub Desktop.
Standard undeterministic list monad with quickcheck properties for the 3 monad laws
{-# LANGUAGE FlexibleInstances #-}
import Test.QuickCheck
newtype List a = List { unList :: [a] }
deriving (Show, Read, Eq, Ord)
data ListBind a = ListBind (a -> List a) String
instance Show (ListBind a) where
show (ListBind _ name) = name
instance Arbitrary a => Arbitrary (List a) where
arbitrary = fmap List arbitrary
instance Arbitrary (ListBind Int) where
arbitrary = elements $ zipWith ListBind [f1, f2, f2] ["f1","f2","f3"]
where f1 x = List $ replicate (min 13 x) x
f2 x = List [x+5]
f3 x = f2 x
instance Monad List where
return a = List $ return a
(List xs) >>= f = List $ concat $ map (unList . f) xs
prop_right_identity :: List Int -> Bool
prop_right_identity xs = (xs >>= return) == xs
prop_left_identity :: Int -> ListBind Int -> Bool
prop_left_identity x (ListBind f _)= (return x >>= f) == f x
prop_associativity :: List Int -> ListBind Int -> ListBind Int -> Bool
prop_associativity m (ListBind f _) (ListBind g _) =
(m >>= (\x -> f x >>= g)) == ((m >>= f) >>= g)
myList :: List Int
myList = do
f <- List [(+1), (*3)]
x <- List [1..3]
return (f x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment