Skip to content

Commit 7ff9c14

Browse files
committed
Merge branch 'master' of github.com:mukeshsoni/haskell-programming-book
2 parents f9a9483 + ade4b64 commit 7ff9c14

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Data.Monoid
2+
import Test.QuickCheck (quickCheck, Arbitrary, arbitrary)
3+
import MonoidLaws
4+
5+
data Trivial = Trivial deriving (Eq, Show)
6+
7+
instance Monoid Trivial where
8+
mempty = Trivial
9+
mappend _ _ = Trivial
10+
11+
instance Arbitrary Trivial where
12+
arbitrary = return Trivial
13+
14+
main = do
15+
quickCheck (monoidAssoc :: (Trivial -> Trivial -> Trivial -> Bool))
16+
quickCheck (monoidLeftIdentity :: (Trivial -> Bool))
17+
quickCheck (monoidRightIdentity :: (Trivial -> Bool))
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Data.Monoid
2+
import Test.QuickCheck (quickCheck, Arbitrary, arbitrary)
3+
import MonoidLaws
4+
5+
data Identity a = Identity a deriving (Eq, Show)
6+
7+
instance (Monoid a) => Monoid (Identity a) where
8+
mempty = Identity mempty
9+
mappend (Identity a) (Identity b) = Identity (a <> b)
10+
11+
instance (Arbitrary a) => Arbitrary (Identity a) where
12+
arbitrary = fmap Identity arbitrary
13+
14+
main = do
15+
quickCheck (monoidAssoc :: (Identity String -> Identity String -> Identity String -> Bool))
16+
quickCheck (monoidLeftIdentity :: (Identity String -> Bool))
17+
quickCheck (monoidRightIdentity :: (Identity String -> Bool))

chapter15/exercises/monoid/3_two.hs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Data.Monoid
2+
import Test.QuickCheck (quickCheck, Arbitrary, arbitrary)
3+
import MonoidLaws
4+
5+
data Two a b = Two a b deriving (Eq, Show)
6+
7+
instance (Monoid a, Monoid b) => Monoid (Two a b) where
8+
mempty = Two mempty mempty
9+
mappend (Two a b) (Two x y) = Two (a <> x) (b <> y)
10+
11+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Two a b) where
12+
arbitrary = do
13+
a <- arbitrary
14+
b <- arbitrary
15+
return (Two a b)
16+
17+
main = do
18+
quickCheck (monoidAssoc :: (Two String String -> Two String String -> Two String String -> Bool))
19+
quickCheck (monoidLeftIdentity :: (Two String String -> Bool))
20+
quickCheck (monoidRightIdentity :: (Two String String -> Bool))
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Data.Monoid
2+
import Test.QuickCheck (quickCheck, Arbitrary, arbitrary)
3+
import MonoidLaws
4+
5+
newtype BoolConj = BoolConj Bool deriving (Eq, Show)
6+
7+
instance Monoid BoolConj where
8+
mempty = BoolConj True
9+
mappend (BoolConj False) _ = BoolConj False
10+
mappend _ (BoolConj False) = BoolConj False
11+
mappend _ _ = BoolConj True
12+
13+
instance Arbitrary BoolConj where
14+
arbitrary = fmap BoolConj arbitrary
15+
16+
main = do
17+
quickCheck (monoidAssoc :: (BoolConj -> BoolConj -> BoolConj -> Bool))
18+
quickCheck (monoidLeftIdentity :: (BoolConj -> Bool))
19+
quickCheck (monoidRightIdentity :: (BoolConj -> Bool))
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module MonoidLaws where
2+
3+
import Data.Monoid
4+
import Control.Monad
5+
import Test.QuickCheck
6+
7+
monoidAssoc :: (Eq a, Monoid a) => a -> a -> a -> Bool
8+
monoidAssoc a b c = a <> (b <> c) == (a <> b) <> c
9+
10+
monoidLeftIdentity :: (Eq a, Monoid a) => a -> Bool
11+
monoidLeftIdentity a = mempty <> a == a
12+
13+
monoidRightIdentity :: (Eq a, Monoid a) => a -> Bool
14+
monoidRightIdentity a = a <> mempty == a

0 commit comments

Comments
 (0)