Skip to content

Commit f103d3b

Browse files
committed
chapter 15 - added exercise 11 for semigroups
1 parent 693c97c commit f103d3b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Test.QuickCheck
2+
import Data.Semigroup
3+
import SemigroupLaws
4+
5+
data Validation a b =
6+
Failure' a | Success' b
7+
deriving (Eq, Show)
8+
9+
instance Semigroup a =>
10+
Semigroup (Validation a b) where
11+
(Success' a) <> (Success' b) = Success' b
12+
(Failure' a) <> (Failure' b) = Failure' (a <> b)
13+
(Failure' a) <> (Success' b) = Failure' a
14+
(Success' a) <> (Failure' b) = Failure' b
15+
16+
instance (Arbitrary a, Arbitrary b) => Arbitrary (Validation a b) where
17+
arbitrary = do
18+
x <- arbitrary
19+
y <- arbitrary
20+
oneof [return (Success' x), return (Failure' y)]
21+
22+
type ValidationAssoc = Validation String String -> Validation String String -> Validation String String -> Bool
23+
type ValidationAssoc2 = Validation String Integer -> Validation String Integer -> Validation String Integer -> Bool
24+
25+
main = do
26+
quickCheck (assocLaw :: ValidationAssoc)
27+
quickCheck (assocLaw :: ValidationAssoc2)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- TODO - couldn't figure out this exercise
2+
import Test.QuickCheck
3+
import Data.Semigroup
4+
import SemigroupLaws
5+
6+
newtype Combine a b =
7+
Combine {unCombine :: a -> b}
8+
9+
instance Semigroup b => Semigroup (Combine a b) where
10+
(Combine f) <> (Combine g) = Combine (f <> g)

0 commit comments

Comments
 (0)