about summary refs log tree commit diff
path: root/scratch/haskell-programming-from-first-principles/monad.hs
blob: 2f80b457b125467e51a6ca259895d06b026ea9db (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module MonadScratch where

import Data.Function ((&))
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Control.Applicative (liftA2)
import qualified Control.Monad as Monad

--------------------------------------------------------------------------------

bind :: Monad m => (a -> m b) -> m a -> m b
bind f x = Monad.join $ fmap f x

--------------------------------------------------------------------------------

fTrigger :: Functor f => f (Int, String, [Int])
fTrigger = undefined

aTrigger :: Applicative a => a (Int, String, [Int])
aTrigger = undefined

mTrigger :: Monad m => m (Int, String, [Int])
mTrigger = undefined

--------------------------------------------------------------------------------

data Sum a b
  = Fst a
  | Snd b
  deriving (Eq, Show)

instance (Eq a, Eq b) => EqProp (Sum a b) where
  (=-=) = eq

instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where
  arbitrary = frequency [ (1, Fst <$> arbitrary)
                        , (1, Snd <$> arbitrary)
                        ]

instance Functor (Sum a) where
  fmap f (Fst x) = Fst x
  fmap f (Snd x) = Snd (f x)

instance Applicative (Sum a) where
  pure x = Snd x
  (Snd f) <*> (Snd x) = Snd (f x)
  (Snd f) <*> (Fst x) = Fst x
  (Fst x) <*> _ = Fst x

instance Monad (Sum a) where
  (Fst x) >>= _ = Fst x
  (Snd x) >>= f = f x

--------------------------------------------------------------------------------

data Nope a = NopeDotJpg deriving (Eq, Show)

instance Arbitrary (Nope a) where
  arbitrary = pure NopeDotJpg

instance EqProp (Nope a) where
  (=-=) = eq

instance Functor Nope where
  fmap f _ = NopeDotJpg

instance Applicative Nope where
  pure _ = NopeDotJpg
  _ <*> _ = NopeDotJpg

instance Monad Nope where
  NopeDotJpg >>= f = NopeDotJpg

--------------------------------------------------------------------------------

data BahEither b a
  = PLeft a
  | PRight b
  deriving (Eq, Show)

instance (Arbitrary b, Arbitrary a) => Arbitrary (BahEither b a) where
  arbitrary = frequency [ (1, PLeft <$> arbitrary)
                        , (1, PRight <$> arbitrary)
                        ]

instance (Eq a, Eq b) => EqProp (BahEither a b) where
  (=-=) = eq

instance Functor (BahEither b) where
  fmap f (PLeft x) = PLeft (f x)
  fmap _ (PRight x) = PRight x

instance Applicative (BahEither b) where
  pure = PLeft
  (PRight x) <*> _ = PRight x
  (PLeft f) <*> (PLeft x) = PLeft (f x)
  _ <*> (PRight x) = PRight x

instance Monad (BahEither b) where
  (PRight x) >>= _ = PRight x
  (PLeft x) >>= f = f x

--------------------------------------------------------------------------------

newtype Identity a = Identity a
  deriving (Eq, Ord, Show)

instance Functor Identity where
  fmap f (Identity x) = Identity (f x)

instance Applicative Identity where
  pure = Identity
  (Identity f) <*> (Identity x) = Identity (f x)

instance Monad Identity where
  (Identity x) >>= f = f x

--------------------------------------------------------------------------------

data List a
  = Nil
  | Cons a (List a)
  deriving (Eq, Show)

instance Arbitrary a => Arbitrary (List a) where
  arbitrary = frequency [ (1, pure Nil)
                        , (1, Cons <$> arbitrary <*> arbitrary)
                        ]

instance Eq a => EqProp (List a) where
  (=-=) = eq

fromList :: [a] -> List a
fromList [] = Nil
fromList (x:xs) = Cons x (fromList xs)

instance Semigroup (List a) where
  Nil <> xs = xs
  xs <> Nil = xs
  (Cons x xs) <> ys =
    Cons x (xs <> ys)

instance Functor List where
  fmap f Nil = Nil
  fmap f (Cons x xs) = Cons (f x) (fmap f xs)

instance Applicative List where
  pure x = Cons x Nil
  Nil <*> _ = Nil
  _ <*> Nil = Nil
  (Cons f fs) <*> xs =
    (f <$> xs) <> (fs <*> xs)

instance Monad List where
  Nil >>= _ = Nil
  (Cons x xs) >>= f = (f x) <> (xs >>= f)

--------------------------------------------------------------------------------

j :: Monad m => m (m a) -> m a
j = Monad.join

l1 :: Monad m => (a -> b) -> m a -> m b
l1 = Monad.liftM

l2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
l2 = Monad.liftM2

a :: Monad m => m a -> m (a -> b) -> m b
a = flip (<*>)

meh :: Monad m => [a] -> (a -> m b) -> m [b]
meh xs f = flipType $ f <$> xs

flipType :: Monad m => [m a] -> m [a]
flipType [] = pure mempty
flipType (m:ms) =
  m >>= (\x -> (x:) <$> flipType ms)