summaryrefslogtreecommitdiff
path: root/Haskell-book/15/optional/app/First.hs
blob: 929cd6b634ea9797b30ff0cb34cae28e0c17dead (plain)
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
module Main where

import Data.Monoid
import Optional
import Test.QuickCheck

newtype First' a =
    First' { getFirst' :: Optional a }
    deriving (Eq, Show)

instance Monoid (First' a) where
    mempty = First' Nada
    mappend (First' Nada) x = x
    mappend x _ = x

instance Arbitrary a => Arbitrary (First' a) where
    arbitrary = frequency [ (1, return $ First' Nada)
                          , (1, fmap (First' . Only) arbitrary) ]

firstMappend :: First' a
             -> First' a
             -> First' a
firstMappend = mappend

type FirstMappend =
    First' String
  -> First' String
  -> First' String
  -> Bool

type FstId =
    First' String -> Bool

monoidAssoc :: (Eq m, Monoid m) => m -> m -> m -> Bool
monoidAssoc a b c =
    (a <> (b <> c)) == ((a <> b) <> c)

monoidLeftIdentity :: (Eq m, Monoid m) => m -> Bool
monoidLeftIdentity a = (mempty <> a) == a

monoidRightIdentity :: (Eq m, Monoid m) => m -> Bool
monoidRightIdentity a = (a <> mempty) == a

main :: IO ()
main = do
    quickCheck (monoidAssoc :: FirstMappend)
    quickCheck (monoidLeftIdentity :: FstId)
    quickCheck (monoidRightIdentity :: FstId)