-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifts.hs
546 lines (492 loc) · 15 KB
/
Lifts.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
{-| Runners and distributive laws for Proxy and FreeT.
-}
{-# LANGUAGE CPP, Rank2Types #-}
module Lifts (
-- * ErrorT
errorP,
runErrorP,
distErrorP,
catchError,
liftCatchError,
errorF,
runErrorF,
distErrorF,
-- * MaybeT
maybeP,
runMaybeP,
distMaybeP,
maybeF,
runMaybeF,
distMaybeF,
-- * ReaderT
readerP,
runReaderP,
distReaderP,
readerF,
runReaderF,
distReaderF,
-- * StateT
stateP,
runStateP,
evalStateP,
execStateP,
distStateP,
stateF,
runStateF,
evalStateF,
execStateF,
distStateF,
-- * WriterT
-- $writert
writerP,
runWriterP,
execWriterP,
distWriterP,
writerF,
runWriterF,
execWriterF,
distWriterF,
-- * RWST
rwsP,
runRWSP,
evalRWSP,
execRWSP,
distRWSP
{-
rwsF,
runRWSF,
evalRWSF,
execRWSF,
distRWSF,
-}
) where
import Control.Monad.Trans.Class (lift)
import qualified Control.Monad.Trans.Error as E
import qualified Control.Monad.Trans.Maybe as M
import qualified Control.Monad.Trans.Reader as R
import qualified Control.Monad.Trans.State.Strict as S
import qualified Control.Monad.Trans.Writer.Strict as W
import qualified Control.Monad.Trans.RWS.Strict as RWS
import Data.Monoid (Monoid(mempty, mappend))
import Pipes.Internal
import qualified Control.Monad.Trans.Free as F
-- | Based on unsafeHoist, but for 'F.FreeT'
hoistF
:: (Monad m, Functor f)
=> (forall x . m x -> n x) -> F.FreeT f m r -> F.FreeT f n r
hoistF nat = go
where
go m = F.FreeT (nat (do
m' <- F.runFreeT m
return (case m' of
F.Pure r -> F.Pure r
F.Free f -> F.Free (fmap go f))
))
-- | Wrap the base monad in 'E.ErrorT'
errorP
:: (Monad m, E.Error e)
=> Proxy a' a b' b m (Either e r)
-> Proxy a' a b' b (E.ErrorT e m) r
errorP p = do
x <- unsafeHoist lift p
lift $ E.ErrorT (return x)
{-# INLINABLE errorP #-}
-- | Run 'E.ErrorT' in the base monad
runErrorP
:: (Monad m)
=> Proxy a' a b' b (E.ErrorT e m) r -> Proxy a' a b' b m (Either e r)
runErrorP = go
where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
Pure r -> Pure (Right r)
M m -> M (do
x <- E.runErrorT m
return (case x of
Left e -> Pure (Left e)
Right p' -> go p' ) )
{-# INLINABLE runErrorP #-}
-- | Pull 'E.ErrorT' outside of Proxy. This is a monad morphism.
distErrorP
:: (Monad m)
=> Proxy a' a b' b (E.ErrorT e m) r
-> E.ErrorT e (Proxy a' a b' b m) r
distErrorP p = E.ErrorT (runErrorP p)
{-# INLINABLE distErrorP #-}
-- | Wrap the base monad in 'E.ErrorT'
errorF
:: (Monad m, Functor f, E.Error e)
=> F.FreeT f m (Either e r)
-> F.FreeT f (E.ErrorT e m) r
errorF p = do
x <- hoistF lift p
lift $ E.ErrorT (return x)
{-# INLINABLE errorF #-}
-- | Run 'E.ErrorT' in the base monad
runErrorF
:: (Monad m, Functor f)
=> F.FreeT f (E.ErrorT e m) r -> F.FreeT f m (Either e r)
runErrorF = go
where
go m = F.FreeT (do
x <- E.runErrorT (F.runFreeT m)
return (case x of
Left e -> F.Pure (Left e)
Right (F.Pure r) -> F.Pure (Right r)
Right (F.Free f) -> F.Free (fmap go f) ) )
{-# INLINABLE runErrorF #-}
-- | Pull 'E.ErrorT' outside of 'F.FreeT'. This is a monad morphism.
distErrorF
:: (Monad m, Functor f)
=> F.FreeT f (E.ErrorT e m) r -> E.ErrorT e (F.FreeT f m) r
distErrorF m = E.ErrorT (runErrorF m)
{-# INLINABLE distErrorF #-}
-- | Catch an error in the base monad
catchError
:: (Monad m)
=> Proxy a' a b' b (E.ErrorT e m) r
-- ^
-> (e -> Proxy a' a b' b (E.ErrorT f m) r)
-- ^
-> Proxy a' a b' b (E.ErrorT f m) r
catchError p0 f = go p0
where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
Pure r -> Pure r
M m -> M (E.ErrorT (do
x <- E.runErrorT m
return (Right (case x of
Left e -> f e
Right p' -> go p' )) ))
{-# INLINABLE catchError #-}
-- | Wrap the base monad in 'M.MaybeT'
maybeP
:: (Monad m)
=> Proxy a' a b' b m (Maybe r) -> Proxy a' a b' b (M.MaybeT m) r
maybeP p = do
x <- unsafeHoist lift p
lift $ M.MaybeT (return x)
{-# INLINABLE maybeP #-}
-- | Run 'M.MaybeT' in the base monad
runMaybeP
:: (Monad m)
=> Proxy a' a b' b (M.MaybeT m) r -> Proxy a' a b' b m (Maybe r)
runMaybeP = go
where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
Pure r -> Pure (Just r)
M m -> M (do
x <- M.runMaybeT m
return (case x of
Nothing -> Pure Nothing
Just p' -> go p' ) )
{-# INLINABLE runMaybeP #-}
-- | Pull 'M.MaybeT' outside of Proxy. This is a monad morphism.
distMaybeP
:: (Monad m)
=> Proxy a' a b' b (M.MaybeT m) r
-> M.MaybeT (Proxy a' a b' b m) r
distMaybeP p = M.MaybeT (runMaybeP p)
{-# INLINABLE distMaybeP #-}
-- | Wrap the base monad in 'M.MaybeT'
maybeF
:: (Monad m, Functor f)
=> F.FreeT f m (Maybe r) -> F.FreeT f (M.MaybeT m) r
maybeF m = do
x <- hoistF lift m
lift $ M.MaybeT (return x)
{-# INLINABLE maybeF #-}
-- | Run 'M.MaybeT' in the base monad
runMaybeF
:: (Monad m, Functor f)
=> F.FreeT f (M.MaybeT m) r -> F.FreeT f m (Maybe r)
runMaybeF = go
where
go m = F.FreeT (do
x <- M.runMaybeT (F.runFreeT m)
return (case x of
Nothing -> F.Pure Nothing
Just (F.Pure r) -> F.Pure (Just r)
Just (F.Free f) -> F.Free (fmap go f) ) )
{-# INLINABLE runMaybeF #-}
-- | Pull 'E.ErrorT' outside of 'F.FreeT'. This is a monad morphism.
distMaybeF
:: (Monad m, Functor f)
=> F.FreeT f (M.MaybeT m) r -> M.MaybeT (F.FreeT f m) r
distMaybeF m = M.MaybeT (runMaybeF m)
{-# INLINABLE distMaybeF #-}
-- | Wrap the base monad in 'R.ReaderT'
readerP
:: (Monad m)
=> (i -> Proxy a' a b' b m r) -> Proxy a' a b' b (R.ReaderT i m) r
readerP k = do
i <- lift R.ask
unsafeHoist lift (k i)
{-# INLINABLE readerP #-}
-- | Run 'R.ReaderT' in the base monad
runReaderP
:: (Monad m)
=> i -> Proxy a' a b' b (R.ReaderT i m) r -> Proxy a' a b' b m r
runReaderP i = go
where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
Pure r -> Pure r
M m -> M (do
p' <- R.runReaderT m i
return (go p') )
{-# INLINABLE runReaderP #-}
-- | Pull 'R.ReaderT' outside of Proxy. This is a monad morphism.
distReaderP
:: (Monad m)
=> Proxy a' a b' b (R.ReaderT i m) r
-> R.ReaderT i (Proxy a' a b' b m) r
distReaderP p = R.ReaderT (flip runReaderP p)
{-# INLINABLE distReaderP #-}
-- | Wrap the base monad in 'R.ReaderT'
readerF
:: (Monad m, Functor f)
=> (i -> F.FreeT f m r) -> F.FreeT f (R.ReaderT i m) r
readerF k = do
i <- lift R.ask
hoistF lift (k i)
{-# INLINABLE readerF #-}
-- | Run 'R.ReaderT' in the base monad
runReaderF
:: (Monad m, Functor f)
=> i -> F.FreeT f (R.ReaderT i m) r -> F.FreeT f m r
runReaderF i = go
where
go m = F.FreeT (do
x <- R.runReaderT (F.runFreeT m) i
return (case x of
F.Pure r -> F.Pure r
F.Free f -> F.Free (fmap go f) ) )
{-# INLINABLE runReaderF #-}
-- | Pull 'R.ReaderT' outside of 'F.FreeT'. This is a monad morphism.
distReaderF
:: (Monad m, Functor f)
=> F.FreeT f (R.ReaderT i m) r -> R.ReaderT i (F.FreeT f m) r
distReaderF m = R.ReaderT (flip runReaderF m)
{-# INLINABLE distReaderF #-}
-- | Wrap the base monad in 'S.StateT'
stateP
:: (Monad m)
=> (s -> Proxy a' a b' b m (r, s)) -> Proxy a' a b' b (S.StateT s m) r
stateP k = do
s <- lift S.get
(r, s') <- unsafeHoist lift (k s)
lift (S.put s')
return r
{-# INLINABLE stateP #-}
-- | Run 'S.StateT' in the base monad
runStateP
:: (Monad m)
=> s -> Proxy a' a b' b (S.StateT s m) r -> Proxy a' a b' b m (r, s)
runStateP = go
where
go s p = case p of
Request a' fa -> Request a' (\a -> go s (fa a ))
Respond b fb' -> Respond b (\b' -> go s (fb' b'))
Pure r -> Pure (r, s)
M m -> M (do
(p', s') <- S.runStateT m s
return (go s' p') )
{-# INLINABLE runStateP #-}
-- | Evaluate 'S.StateT' in the base monad
evalStateP
:: (Monad m) => s -> Proxy a' a b' b (S.StateT s m) r -> Proxy a' a b' b m r
evalStateP s = fmap fst . runStateP s
{-# INLINABLE evalStateP #-}
-- | Execute 'S.StateT' in the base monad
execStateP
:: (Monad m) => s -> Proxy a' a b' b (S.StateT s m) r -> Proxy a' a b' b m s
execStateP s = fmap snd . runStateP s
{-# INLINABLE execStateP #-}
-- | Pull 'S.StateT' outside of Proxy. This is a monad morphism.
distStateP
:: (Monad m)
=> Proxy a' a b' b (S.StateT s m) r
-> S.StateT s (Proxy a' a b' b m) r
distStateP p = S.StateT (flip runStateP p)
{-# INLINABLE distStateP #-}
-- | Wrap the base monad in 'S.StateT'
stateF
:: (Monad m, Functor f)
=> (s -> F.FreeT f m (r, s)) -> F.FreeT f (S.StateT s m) r
stateF k = do
s <- lift S.get
(r, s') <- hoistF lift (k s)
lift (S.put s')
return r
{-# INLINABLE stateF #-}
-- | Run 'S.StateT' in the base monad
runStateF
:: (Monad m, Functor f)
=> s -> F.FreeT f (S.StateT s m) r -> F.FreeT f m (r, s)
runStateF = go
where
go s m = F.FreeT (do
(x, s') <- S.runStateT (F.runFreeT m) s
return (case x of
F.Pure r -> F.Pure (r, s')
F.Free f -> F.Free (fmap (go s') f) ) )
{-# INLINABLE runStateF #-}
-- | Evaluate 'S.StateT' in the base monad
evalStateF
:: (Monad m, Functor f) => s -> F.FreeT f (S.StateT s m) r -> F.FreeT f m r
evalStateF s = fmap fst . runStateF s
{-# INLINABLE evalStateF #-}
-- | Execute 'S.StateT' in the base monad
execStateF
:: (Monad m, Functor f) => s -> F.FreeT f (S.StateT s m) r -> F.FreeT f m s
execStateF s = fmap snd . runStateF s
{-# INLINABLE execStateF #-}
-- | Pull 'S.StateT' outside of 'F.FreeT'. This is a monad morphism.
distStateF
:: (Monad m, Functor f)
=> F.FreeT f (S.StateT s m) r -> S.StateT s (F.FreeT f m) r
distStateF m = S.StateT (flip runStateF m)
{-# INLINABLE distStateF #-}
{- $writert
Note that 'runWriterP' and 'execWriterP' will keep the accumulator in
weak-head-normal form so that folds run in constant space when possible.
This means that until @transformers@ adds a truly strict 'W.WriterT', you
should consider unwrapping 'W.WriterT' first using 'runWriterP' or
'execWriterP' before 'Pipes.run'ning your 'Proxy'. You will get better
performance this way and eliminate space leaks if your accumulator doesn't
have any lazy fields.
-}
-- | Wrap the base monad in 'W.WriterT'
writerP
:: (Monad m, Monoid w)
=> Proxy a' a b' b m (r, w) -> Proxy a' a b' b (W.WriterT w m) r
writerP p = do
(r, w) <- unsafeHoist lift p
lift $ W.tell w
return r
{-# INLINABLE writerP #-}
-- | Run 'W.WriterT' in the base monad
runWriterP
:: (Monad m, Monoid w)
=> Proxy a' a b' b (W.WriterT w m) r -> Proxy a' a b' b m (r, w)
runWriterP = go mempty
where
go w p = case p of
Request a' fa -> Request a' (\a -> go w (fa a ))
Respond b fb' -> Respond b (\b' -> go w (fb' b'))
Pure r -> Pure (r, w)
M m -> M (do
(p', w') <- W.runWriterT m
let wt = mappend w w'
wt `seq` return (go wt p') )
{-# INLINABLE runWriterP #-}
-- | Execute 'W.WriterT' in the base monad
execWriterP
:: (Monad m, Monoid w)
=> Proxy a' a b' b (W.WriterT w m) r -> Proxy a' a b' b m w
execWriterP = fmap snd . runWriterP
{-# INLINABLE execWriterP #-}
-- | Pull 'W.WriterT' outside of Proxy. This is a monad morphism.
distWriterP
:: (Monad m, Monoid w)
=> Proxy a' a b' b (W.WriterT w m) r
-> W.WriterT w (Proxy a' a b' b m) r
distWriterP p = W.WriterT (runWriterP p)
{-# INLINABLE distWriterP #-}
-- | Wrap the base monad in 'W.WriterT'
writerF
:: (Monad m, Functor f, Monoid w)
=> F.FreeT f m (r, w) -> F.FreeT f (W.WriterT w m) r
writerF m = do
(r, w) <- hoistF lift m
lift $ W.tell w
return r
{-# INLINABLE writerF #-}
-- | Run 'W.WriterT' in the base monad
runWriterF
:: (Monad m, Functor f, Monoid w)
=> F.FreeT f (W.WriterT w m) r -> F.FreeT f m (r, w)
runWriterF = go mempty
where
go w m = F.FreeT (do
(x, w') <- W.runWriterT (F.runFreeT m)
let wt = mappend w w'
wt `seq` return (case x of
F.Pure r -> F.Pure (r, wt)
F.Free f -> F.Free (fmap (go wt) f) ) )
{-# INLINABLE runWriterF #-}
-- | Execute 'W.WriterT' in the base monad
execWriterF
:: (Monad m, Functor f, Monoid w)
=> F.FreeT f (W.WriterT w m) r -> F.FreeT f m w
execWriterF = fmap snd . runWriterF
{-# INLINABLE execWriterF #-}
-- | Pull 'W.WriterT' outside of 'F.FreeT'. This is a monad morphism.
distWriterF
:: (Monad m, Functor f, Monoid w)
=> F.FreeT f (W.WriterT w m) r -> W.WriterT w (F.FreeT f m) r
distWriterF m = W.WriterT (runWriterF m)
{-# INLINABLE distWriterF #-}
-- | Wrap the base monad in 'RWS.RWST'
rwsP
:: (Monad m, Monoid w)
=> (i -> s -> Proxy a' a b' b m (r, s, w))
-> Proxy a' a b' b (RWS.RWST i w s m) r
rwsP k = do
i <- lift RWS.ask
s <- lift RWS.get
(r, s', w) <- unsafeHoist lift (k i s)
lift $ do
RWS.put s'
RWS.tell w
return r
{-# INLINABLE rwsP #-}
-- | Run 'RWST' in the base monad
runRWSP :: (Monad m, Monoid w)
=> i
-> s
-> Proxy a' a b' b (RWS.RWST i w s m) r
-> Proxy a' a b' b m (r, s, w)
runRWSP i = go mempty
where
go w s p = case p of
Request a' fa -> Request a' (\a -> go w s (fa a ))
Respond b fb' -> Respond b (\b' -> go w s (fb' b'))
Pure r -> Pure (r, s, w)
M m -> M (do
(p', s', w') <- RWS.runRWST m i s
let wt = mappend w w'
wt `seq` return (go w' s' p') )
{-# INLINABLE runRWSP #-}
-- | Evaluate 'RWST' in the base monad
evalRWSP :: (Monad m, Monoid w)
=> i
-> s
-> Proxy a' a b' b (RWS.RWST i w s m) r
-> Proxy a' a b' b m (r, w)
evalRWSP i s = fmap go . runRWSP i s
where go (r, _, w) = (r, w)
{-# INLINABLE evalRWSP #-}
-- | Execute 'RWST' in the base monad
execRWSP :: (Monad m, Monoid w)
=> i
-> s
-> Proxy a' a b' b (RWS.RWST i w s m) r
-> Proxy a' a b' b m (s, w)
execRWSP i s = fmap go . runRWSP i s
where go (_, s', w) = (s', w)
{-# INLINABLE execRWSP #-}
-- | Pull 'RWST' outside of Proxy. This is a monad morphism.
distRWSP
:: (Monad m, Monoid w)
=> Proxy a' a b' b (RWS.RWST i w s m) r
-> RWS.RWST i w s (Proxy a' a b' b m) r
distRWSP p = RWS.RWST (\i s -> runRWSP i s p)
{-# INLINABLE distRWSP #-}