-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathMonad.hs
More file actions
235 lines (208 loc) · 5.96 KB
/
Monad.hs
File metadata and controls
235 lines (208 loc) · 5.96 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{-# LANGUAGE RebindableSyntax #-}
-- |
-- Module : Data.Array.Accelerate.Control.Monad
-- Copyright : [2018..2020] The Accelerate Team
-- License : BSD3
--
-- Maintainer : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability : experimental
-- Portability : non-portable (GHC extensions)
--
-- A monad sequences actions over a parametrised type.
--
-- This is essentially the same as the standard Haskell 'Control.Monad' class,
-- lifted to Accelerate 'Exp' terms.
--
-- @since 1.4.0.0
--
module Data.Array.Accelerate.Control.Monad (
-- * Monad class
Monad(..),
-- * Functions
-- ** Basic functions
(=<<), (>>),
(>=>), (<=<),
join,
-- ** Conditional execution of monadic expressions
when, unless,
-- ** Monadic lifting operations
liftM, liftM2, liftM3, liftM4, liftM5,
) where
import Data.Array.Accelerate.Data.Functor
import Data.Array.Accelerate.Language
import Data.Array.Accelerate.Sugar.Elt
import Data.Array.Accelerate.Smart
import Prelude ( Bool, flip, id )
-- | The 'Monad' class is used for scalar types which can be sequenced.
-- Instances of 'Monad' should satisfy the following laws:
--
-- [Left identity] @'return' a '>>=' k = k a@
-- [Right identity] @m '>>=' 'return' = m@
-- [Associativity] @m '>>=' (\\x -> k x '>>=' h) = (m '>>=' k) '>>=' h@
--
-- Furthermore, the 'Monad' and 'Functor' operations should relate as follows:
-- * @'fmap' f xs = xs '>>=' 'return' . f@
--
class Functor m => Monad m where
-- | Sequentially compose two actions, passing any value produced
-- by the first as an argument to the second.
--
-- \'@as '>>=' bs@\' can be understood as the @do@ expression
--
-- @
-- do a <- as
-- bs a
-- @
--
infixl 1 >>=
(>>=) :: (Elt a, Elt b, Elt (m a), Elt (m b))
=> Exp (m a)
-> (Exp a -> Exp (m b))
-> Exp (m b)
-- | Inject a value into the monadic type
--
return :: (Elt a, Elt (m a)) => Exp a -> Exp (m a)
-- | Same as '>>=', but with the arguments interchanged
--
infixr 1 =<<
(=<<) :: (Monad m, Elt a, Elt b, Elt (m a), Elt (m b))
=> (Exp a -> Exp (m b))
-> Exp (m a)
-> Exp (m b)
(=<<) = flip (>>=)
-- | Sequentially compose two actions, discarding any value produced by the
-- first, like sequencing operators (such as the semicolon) in imperative
-- languages.
--
-- \'@as '>>' bs@\' can be understood as the @do@ expression
--
-- @
-- do as
-- bs
-- @
--
infixl 1 >>
(>>) :: (Monad m, Elt a, Elt b, Elt (m a), Elt (m b))
=> Exp (m a)
-> Exp (m b)
-> Exp (m b)
m >> k = m >>= \_ -> k
-- | Left-to-right composition of Kleisli arrows.
--
-- \'@(bs '>=>' cs) a@\' can be understood as the @do@ expression
--
-- @
-- do b <- bs a
-- cs b
-- @
--
infixr 1 >=>
(>=>) :: (Monad m, Elt a, Elt b, Elt c, Elt (m b), Elt (m c))
=> (Exp a -> Exp (m b))
-> (Exp b -> Exp (m c))
-> (Exp a -> Exp (m c))
f >=> g = \x -> f x >>= g
-- | Right-to-left composition of Kleisli arrows. @('>=>')@, with the arguments
-- flipped.
--
-- Note how this operator resembles function composition @('.')@:
--
-- > (.) :: (b -> c) -> (a -> b) -> a -> c
-- > (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
--
infixr 1 <=<
(<=<) :: (Monad m, Elt a, Elt b, Elt c, Elt (m b), Elt (m c))
=> (Exp b -> Exp (m c))
-> (Exp a -> Exp (m b))
-> (Exp a -> Exp (m c))
(<=<) = flip (>=>)
-- | The 'join' function is the conventional monad join operator. It
-- is used to remove one level of monadic structure, projecting its
-- bound argument into the outer level.
--
-- \'@'join' bss@\' can be understood as the @do@ expression
--
-- @
-- do bs <- bss
-- bs
-- @
--
join :: (Monad m, Elt a, Elt (m a), Elt (m (m a))) => Exp (m (m a)) -> Exp (m a)
join = (>>= id)
-- | Conditional execution of a monadic expression
--
when :: (Monad m, Elt (m ())) => Exp Bool -> Exp (m ()) -> Exp (m ())
when p s = cond p s (return (constant ()))
-- | The reverse of 'when'
--
unless :: (Monad m, Elt (m ())) => Exp Bool -> Exp (m ()) -> Exp (m ())
unless p s = cond p (return (constant ())) s
-- | Promote a function to a monad
--
liftM :: (Monad m, Elt a, Elt b, Elt (m a), Elt (m b)) => (Exp a -> Exp b) -> Exp (m a) -> Exp (m b)
liftM f m1 = do
x1 <- m1
return (f x1)
-- | Promote a function to a monad, scanning the monadic arguments from
-- left to right.
--
liftM2 :: (Monad m, Elt a, Elt b, Elt c, Elt (m a), Elt (m b), Elt (m c))
=> (Exp a -> Exp b -> Exp c)
-> Exp (m a)
-> Exp (m b)
-> Exp (m c)
liftM2 f m1 m2 = do
x1 <- m1
x2 <- m2
return (f x1 x2)
-- | Promote a function to a monad, scanning the monadic arguments from
-- left to right (cf. 'liftM2')
--
liftM3 :: (Monad m, Elt a, Elt b, Elt c, Elt d, Elt (m a), Elt (m b), Elt (m c), Elt (m d))
=> (Exp a -> Exp b -> Exp c -> Exp d)
-> Exp (m a)
-> Exp (m b)
-> Exp (m c)
-> Exp (m d)
liftM3 f m1 m2 m3 = do
x1 <- m1
x2 <- m2
x3 <- m3
return (f x1 x2 x3)
-- | Promote a function to a monad, scanning the monadic arguments from
-- left to right (cf. 'liftM2')
--
liftM4 :: (Monad m, Elt a, Elt b, Elt c, Elt d, Elt e, Elt (m a), Elt (m b), Elt (m c), Elt (m d), Elt (m e))
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e)
-> Exp (m a)
-> Exp (m b)
-> Exp (m c)
-> Exp (m d)
-> Exp (m e)
liftM4 f m1 m2 m3 m4 = do
x1 <- m1
x2 <- m2
x3 <- m3
x4 <- m4
return (f x1 x2 x3 x4)
-- | Promote a function to a monad, scanning the monadic arguments from
-- left to right (cf. 'liftM2')
--
liftM5 :: ( Monad m
, Elt a, Elt b, Elt c, Elt d, Elt e, Elt f
, Elt (m a), Elt (m b), Elt (m c), Elt (m d), Elt (m e), Elt (m f)
)
=> (Exp a -> Exp b -> Exp c -> Exp d -> Exp e -> Exp f)
-> Exp (m a)
-> Exp (m b)
-> Exp (m c)
-> Exp (m d)
-> Exp (m e)
-> Exp (m f)
liftM5 f m1 m2 m3 m4 m5 = do
x1 <- m1
x2 <- m2
x3 <- m3
x4 <- m4
x5 <- m5
return (f x1 x2 x3 x4 x5)