File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
chapter15/exercises/semigroup Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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)
You can’t perform that action at this time.
0 commit comments