Monoid.icl 1.87 KB
Newer Older
1
implementation module Data.Monoid
2

3 4 5
from StdOverloaded import class + (..), class * (..), class zero (..), class one (..)
from StdBool import &&, ||
from StdFunc import o, id
6
from Data.Maybe import :: Maybe(..)
7
from StdList import ++
8

9
instance Semigroup () where
10 11
  mappend _ _  = ()

12 13 14 15 16
instance Monoid () where
  mempty = ()

instance Semigroup (Dual a) | Semigroup a where
  mappend (Dual x) (Dual y) = Dual (mappend y x)
17 18 19

instance Monoid (Dual a) | Monoid a where
  mempty = Dual mempty
20

21
instance Semigroup (Endo .a) where
22
  mappend (Endo f) (Endo g) = Endo (f o g)
23

24
instance Monoid (Endo .a) where
25
  mempty = Endo id
26 27 28

instance Semigroup All where
  mappend (All x) (All y) = All (x && y)
29 30 31

instance Monoid All where
  mempty = All True
32 33 34

instance Semigroup Any where
  mappend (Any x) (Any y) = Any (x || y)
35 36 37

instance Monoid Any where
  mempty = Any False
38 39 40

instance Semigroup (Sum a) | + a & zero a where
  mappend (Sum x) (Sum y) = Sum (x + y)
41 42 43

instance Monoid (Sum a) | + a & zero a where
  mempty = Sum zero
44 45 46

instance Semigroup (Product a) | * a & one a where
  mappend (Product x) (Product y) = Product (x * y)
47 48 49 50

instance Monoid (Product a) | * a & one a where
  mempty = Product one

51
instance Semigroup (First a) where
52 53 54
  mappend r=:(First (Just _)) _ = r
  mappend (First Nothing) r = r

55 56 57 58
instance Monoid (First a) where
  mempty = First Nothing

instance Semigroup (Last a) where
59 60 61
  mappend _ r=:(Last (Just _)) = r
  mappend r (Last Nothing) = r

62 63 64
instance Monoid (Last a) where
  mempty = Last Nothing

65
getDual :: !(Dual .a) -> .a
66 67
getDual (Dual x) = x

68
appEndo :: !(Endo .a) -> (.a -> .a)
69 70
appEndo (Endo f) = f

71
getAll :: !All -> Bool
72 73
getAll (All b) = b

74
getAny :: !Any -> Bool
75 76
getAny (Any b) = b

77
getSum :: !(Sum a) -> a
78 79
getSum (Sum x) = x

80
getProduct :: !(Product a) -> a
81 82
getProduct (Product x) = x

83
getFirst :: !(First a) -> Maybe a
84 85
getFirst (First x) = x

86
getLast :: !(Last a) -> Maybe a
87
getLast (Last x) = x