-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch15_Optional.hs
55 lines (40 loc) · 1.38 KB
/
Ch15_Optional.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Ch15_Optional where
import Data.Monoid
import Test.QuickCheck
data Optional a = Nada | Only a deriving (Eq, Show)
newtype First' a =
First' { getFirst' :: Optional a }
deriving (Eq, Show)
type FirstMappend =
First' String
-> First' String
-> First' String
-> Bool
type FstId = First' String -> Bool
instance Semigroup (First' a) where
(First' (Only x)) <> (First' (Only y)) = First' (Only x)
First' Nada <> foy = foy
fox <> First' Nada = fox
instance Monoid (First' a) where
mempty = First' Nada
instance Monoid a => Semigroup (Optional a) where
(Only x) <> (Only y) = Only $ x `mappend` y
Nada <> oy = oy
ox <> Nada = ox
-- only one Monoid definition for Optional a
instance Monoid a => Monoid (Optional a) where
mempty = Nada
firstMappend :: First' a -> First' a -> First' a
firstMappend = mappend
monoidAssoc x y z =
x `mappend` (y `mappend` z) == (x `mappend` y) `mappend` z
monoidLeftIdentity a = (mempty `mappend` a) == a
monoidRightIdentity a = (a `mappend` mempty) == a
instance Arbitrary a => Arbitrary (First' a) where
arbitrary = frequency [(1, return $ First' Nada)
,(1, First' . Only <$> arbitrary)]
main :: IO ()
main = do
quickCheck (monoidAssoc :: FirstMappend)
quickCheck (monoidLeftIdentity :: FstId)
quickCheck (monoidRightIdentity :: FstId)