Skip to content

Commit 6d28643

Browse files
committed
chapter 15: added exercise 12 for semigroup on AccumulateRight. Which implements a new semigroup instance for Validtion type
1 parent f103d3b commit 6d28643

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

chapter15/exercises/semigroup/11_validation.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import Test.QuickCheck
2-
import Data.Semigroup
3-
import SemigroupLaws
1+
import Test.QuickCheck (arbitrary, Arbitrary, quickCheck, oneof)
2+
import Data.Semigroup (Semigroup, (<>))
3+
import SemigroupLaws
44

55
data Validation a b =
6-
Failure' a | Success' b
6+
Failure a | Success b
77
deriving (Eq, Show)
88

99
instance Semigroup a =>
1010
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
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
1515

1616
instance (Arbitrary a, Arbitrary b) => Arbitrary (Validation a b) where
1717
arbitrary = do
1818
x <- arbitrary
1919
y <- arbitrary
20-
oneof [return (Success' x), return (Failure' y)]
20+
oneof [return (Success x), return (Failure y)]
2121

2222
type ValidationAssoc = Validation String String -> Validation String String -> Validation String String -> Bool
2323
type ValidationAssoc2 = Validation String Integer -> Validation String Integer -> Validation String Integer -> Bool
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Test.QuickCheck (arbitrary, Arbitrary, quickCheck, oneof)
2+
import Data.Semigroup (Semigroup, (<>))
3+
import SemigroupLaws
4+
5+
data Validation a b =
6+
Failure a | Success b
7+
deriving (Eq, Show)
8+
9+
newtype AccumulateRight a b =
10+
AccumulateRight (Validation a b)
11+
deriving (Eq, Show)
12+
13+
instance Semigroup a =>
14+
Semigroup (AccumulateRight a b) where
15+
(AccumulateRight (Success _)) <> (AccumulateRight (Success b)) = AccumulateRight (Success b)
16+
(AccumulateRight (Failure _)) <> (AccumulateRight (Failure b)) = AccumulateRight (Failure b)
17+
(AccumulateRight (Failure a)) <> (AccumulateRight (Success _)) = AccumulateRight (Failure a)
18+
(AccumulateRight (Success _)) <> (AccumulateRight (Failure b)) = AccumulateRight (Failure b)
19+
20+
instance (Arbitrary a, Arbitrary b) => Arbitrary (AccumulateRight a b) where
21+
arbitrary = do
22+
x <- arbitrary
23+
y <- arbitrary
24+
oneof [return (AccumulateRight (Success x)), return (AccumulateRight (Failure y))]
25+
26+
type ValidationAssoc = AccumulateRight String String -> AccumulateRight String String -> AccumulateRight String String -> Bool
27+
type ValidationAssoc2 = AccumulateRight String Integer -> AccumulateRight String Integer -> AccumulateRight String Integer -> Bool
28+
29+
main = do
30+
quickCheck (assocLaw :: ValidationAssoc)
31+
quickCheck (assocLaw :: ValidationAssoc2)

0 commit comments

Comments
 (0)