-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoidF.hs
51 lines (36 loc) · 1.25 KB
/
MonoidF.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
{-# LANGUAGE FlexibleInstances #-}
{-
MonoidF is a type modifier for interpreting applicative functors
of monoids as monoids that combine their contained values
using (liftA2 mappend).
MonoidZip is a type modifier for interpreting traversable applicative
functors of monoids as monoids that combine their contained values
using (zipWithT mappend).
-}
module MonoidF where
import Control.Applicative
import ZipT
newtype MonoidF a = MonoidF a
deriving (Eq, Show)
getMonoidF (MonoidF x) = x
instance Functor MonoidF where
fmap f (MonoidF x) = MonoidF (f x)
instance Applicative MonoidF where
pure = MonoidF
MonoidF f <*> MonoidF x = MonoidF (f x)
-- Monoid instance
instance (Applicative f, Monoid a) => Monoid (MonoidF (f a)) where
mempty = MonoidF (pure mempty)
mappend = liftA2 (liftA2 mappend)
newtype MonoidZip a = MonoidZip a
deriving (Eq, Show)
getMonoidZip (MonoidZip x) = x
instance Functor MonoidZip where
fmap f (MonoidZip x) = MonoidZip (f x)
instance Applicative MonoidZip where
pure = MonoidZip
MonoidZip f <*> MonoidZip x = MonoidZip (f x)
-- Monoid instance
instance (Applicative f, Traversable f, Monoid a) => Monoid (MonoidZip (f a)) where
mempty = MonoidZip (pure mempty)
mappend = liftA2 (zipWithT mappend)