-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compose.hs
594 lines (527 loc) · 19.8 KB
/
Compose.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{-# LANGUAGE CPP #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Trans.Compose where
import Control.Monad.Base
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Control
import Control.Monad.Trans.Control.Identity
import Control.Monad.Trans.Elevator
import Data.Kind
#if defined(VERSION_exceptions)
import Control.Monad.Catch qualified as Exceptions
import Control.Monad.Catch.Pure qualified as Exceptions.T
#endif
#if defined(VERSION_mtl)
import Control.Monad.Accum qualified as Mtl
import Control.Monad.Accum.OrphanInstances qualified as Mtl ()
import Control.Monad.Cont.Class qualified as Mtl
import Control.Monad.Error.Class qualified as Mtl
import Control.Monad.Reader.Class qualified as Mtl
import Control.Monad.RWS.Class qualified as Mtl (MonadRWS)
import Control.Monad.Select qualified as Mtl
import Control.Monad.Select.OrphanInstances qualified as Mtl ()
import Control.Monad.State.Class qualified as Mtl
import Control.Monad.Trans.Accum qualified as Mtl.T
import Control.Monad.Trans.Cont qualified as Mtl.T
import Control.Monad.Trans.Except qualified as Mtl.T
import Control.Monad.Trans.RWS.CPS qualified as Mtl.CPST
import Control.Monad.Trans.RWS.Lazy qualified as Mtl.LT
import Control.Monad.Trans.RWS.Strict qualified as Mtl.ST
import Control.Monad.Trans.Reader qualified as Mtl.T
import Control.Monad.Trans.Select qualified as Mtl.T
import Control.Monad.Trans.State.Lazy qualified as Mtl.LT
import Control.Monad.Trans.State.Strict qualified as Mtl.ST
import Control.Monad.Trans.Writer.CPS qualified as Mtl.CPST
import Control.Monad.Trans.Writer.Lazy qualified as Mtl.LT
import Control.Monad.Trans.Writer.Strict qualified as Mtl.ST
import Control.Monad.Writer.Class qualified as Mtl
import Data.Functor.Identity qualified as Mtl
#endif
#if defined(VERSION_primitive)
import Control.Monad.Primitive qualified as Primitive
#endif
#if defined(VERSION_random)
import Data.Functor.Const qualified as Random
import System.Random.Stateful qualified as Random
#endif
#if defined(VERSION_resourcet)
import Control.Monad.Trans.Resource qualified as ResourceT
#endif
#if defined(VERSION_unliftio_core)
import Control.Monad.IO.Unlift qualified as UnliftIO
#endif
-- * 'ComposeT'
--
-- $composet
--
-- 'ComposeT' can be used in monad transformer stacks to derive instances.
--
-- This also allows the usage of these instances, while in the middle of the transformer stack.
-- This proves particularly useful, when writing a runner for a transformer stack.
-- | A newtype wrapper for two stacked monad transformers.
--
-- Access instances of the intermediate monad @(t2 m)@, whenever @t1@ implements 'MonadTrans' \/
-- 'MonadTransControl' \/ 'MonadTransControlIdentity'.
--
-- __Type level arguments:__
--
-- [@t1 :: ('Type' -> 'Type') -> 'Type' -> 'Type'@] outer monad transformer
-- [@t2 :: ('Type' -> 'Type') -> 'Type' -> 'Type'@] inner monad transformer
-- [@m :: 'Type' -> 'Type'@] monad
-- [@a :: 'Type'@] value
type ComposeT :: ((Type -> Type) -> Type -> Type) -- @t1@
-> ((Type -> Type) -> Type -> Type) -- @t2@
-> (Type -> Type) -- @m@
-> Type -- @a@
-> Type
newtype ComposeT t1 t2 m a = ComposeT { deComposeT :: t1 (t2 m) a }
deriving newtype (Applicative, Functor, Monad)
instance (MonadTrans t1, MonadTrans t2) => MonadTrans (ComposeT t1 t2) where
lift = ComposeT . lift . lift
instance (MonadTransControl t1, MonadTransControl t2) => MonadTransControl (ComposeT t1 t2) where
type StT (ComposeT t1 t2) a = StT t2 (StT t1 a)
liftWith f = defaultLiftWith2 ComposeT deComposeT $ \ x -> f x
restoreT = defaultRestoreT2 ComposeT
instance (MonadTransControlIdentity t1, MonadTransControlIdentity t2) => MonadTransControlIdentity (ComposeT t1 t2) where
liftWithIdentity inner = ComposeT $ liftWithIdentity $ \ runId1 ->
liftWithIdentity $ \ runId2 -> inner $ runId2 . runId1 . deComposeT
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( MonadIO m
, MonadTrans (ComposeT t1 t2)
) => MonadIO (ComposeT t1 t2 m)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( MonadBase b m
, MonadTrans (ComposeT t1 t2)
) => MonadBase b (ComposeT t1 t2 m)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( MonadBaseControl b m
, MonadTransControl (ComposeT t1 t2)
) => MonadBaseControl b (ComposeT t1 t2 m)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( MonadBaseControlIdentity b m
, MonadTransControlIdentity (ComposeT t1 t2)
) => MonadBaseControlIdentity b (ComposeT t1 t2 m)
#if defined(VERSION_exceptions)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Exceptions.MonadThrow (t2 m)
, MonadTrans t1
) => Exceptions.MonadThrow (ComposeT t1 t2 m)
-- | Set by 'Exceptions.T.CatchT'.
deriving via Exceptions.T.CatchT (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Exceptions.MonadThrow (ComposeT Exceptions.T.CatchT t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Exceptions.MonadCatch (t2 m)
, MonadTransControl t1
) => Exceptions.MonadCatch (ComposeT t1 t2 m)
-- | Set by 'Exceptions.T.CatchT'.
deriving via Exceptions.T.CatchT (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Exceptions.MonadCatch (ComposeT Exceptions.T.CatchT t2 m)
#endif
#if defined(VERSION_mtl)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadAccum w (t2 m)
, MonadTrans t1
) => Mtl.MonadAccum w (ComposeT t1 t2 m)
-- | Set by 'Mtl.T.AccumT'.
deriving via Mtl.T.AccumT w (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadAccum w (ComposeT (Mtl.T.AccumT w) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadCont (t2 m)
, MonadTransControl t1
) => Mtl.MonadCont (ComposeT t1 t2 m)
-- | Set by 'Mtl.T.ContT'.
deriving via Mtl.T.ContT r (t2 (m :: Type -> Type))
instance Mtl.MonadCont (ComposeT (Mtl.T.ContT r) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadError e (t2 m)
, MonadTransControl t1
) => Mtl.MonadError e (ComposeT t1 t2 m)
-- | Set by 'Mtl.T.ExceptT'.
deriving via Mtl.T.ExceptT e (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Mtl.MonadError e (ComposeT (Mtl.T.ExceptT e) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadReader r (t2 m)
, MonadTransControl t1
) => Mtl.MonadReader r (ComposeT t1 t2 m)
-- | Set by 'Mtl.T.ReaderT'.
deriving via Mtl.T.ReaderT r (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Mtl.MonadReader r (ComposeT (Mtl.T.ReaderT r) t2 m)
-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadReader r (ComposeT (Mtl.CPST.RWST r w s) t2 m)
-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadReader r (ComposeT (Mtl.LT.RWST r w s) t2 m)
-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadReader r (ComposeT (Mtl.ST.RWST r w s) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadRWS r w s (t2 m)
, MonadTransControl t1
) => Mtl.MonadRWS r w s (ComposeT t1 t2 m)
-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadRWS r w s (ComposeT (Mtl.CPST.RWST r w s) t2 m)
-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadRWS r w s (ComposeT (Mtl.LT.RWST r w s) t2 m)
-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadRWS r w s (ComposeT (Mtl.ST.RWST r w s) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadSelect r (t2 m)
, MonadTrans t1
) => Mtl.MonadSelect r (ComposeT t1 t2 m)
-- | Set by 'Mtl.T.SelectT'.
deriving via Mtl.T.SelectT r (t2 (m :: Type -> Type))
instance
( MonadBaseControlIdentity Mtl.Identity (t2 m)
) => Mtl.MonadSelect r (ComposeT (Mtl.T.SelectT r) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadState s (t2 m)
, MonadTrans t1
) => Mtl.MonadState s (ComposeT t1 t2 m)
-- | Set by 'Mtl.LT.StateT'.
deriving via Mtl.LT.StateT s (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Mtl.MonadState s (ComposeT (Mtl.LT.StateT s) t2 m)
-- | Set by 'Mtl.ST.StateT'.
deriving via Mtl.ST.StateT s (t2 (m :: Type -> Type))
instance
( Monad (t2 m)
) => Mtl.MonadState s (ComposeT (Mtl.ST.StateT s) t2 m)
-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadState s (ComposeT (Mtl.CPST.RWST r w s) t2 m)
-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadState s (ComposeT (Mtl.LT.RWST r w s) t2 m)
-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadState s (ComposeT (Mtl.ST.RWST r w s) t2 m)
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( Mtl.MonadWriter w (t2 m)
, MonadTransControl t1
) => Mtl.MonadWriter w (ComposeT t1 t2 m)
-- | Set by 'Mtl.CPST.WriterT'.
deriving via Mtl.CPST.WriterT w (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.CPST.WriterT w) t2 m)
-- | Set by 'Mtl.LT.WriterT'.
deriving via Mtl.LT.WriterT w (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.LT.WriterT w) t2 m)
-- | Set by 'Mtl.ST.WriterT'.
deriving via Mtl.ST.WriterT w (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.ST.WriterT w) t2 m)
-- | Set by 'Mtl.CPST.RWST'.
deriving via Mtl.CPST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.CPST.RWST r w s) t2 m)
-- | Set by 'Mtl.LT.RWST'.
deriving via Mtl.LT.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.LT.RWST r w s) t2 m)
-- | Set by 'Mtl.ST.RWST'.
deriving via Mtl.ST.RWST r w s (t2 (m :: Type -> Type))
instance
( Monoid w
, Monad (t2 m)
) => Mtl.MonadWriter w (ComposeT (Mtl.ST.RWST r w s) t2 m)
#endif
#if defined(VERSION_primitive)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( Primitive.PrimMonad m
, MonadTrans (ComposeT t1 t2)
) =>
Primitive.PrimMonad (ComposeT t1 t2 m)
#endif
#if defined(VERSION_random)
-- TODO: `MonadIO (t2 m)` should not be required for this instance
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
instance {-# OVERLAPPABLE #-} (Random.StatefulGen g (t2 m), MonadTrans t1, MonadIO (t2 m)) => Random.StatefulGen (Random.Const g (ComposeT t1 t2)) (ComposeT t1 t2 m) where
uniformWord32R word32 = ComposeT . lift . Random.uniformWord32R word32 . Random.getConst
uniformWord64R word64 = ComposeT . lift . Random.uniformWord64R word64 . Random.getConst
uniformWord8 = ComposeT . lift . Random.uniformWord8 . Random.getConst
uniformWord16 = ComposeT . lift . Random.uniformWord16 . Random.getConst
uniformWord32 = ComposeT . lift . Random.uniformWord32 . Random.getConst
uniformWord64 = ComposeT . lift . Random.uniformWord64 . Random.getConst
uniformShortByteString n = ComposeT . lift . Random.uniformShortByteString n . Random.getConst
-- TODO: `MonadIO (t2 m)` should not be required for this instance
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
instance {-# OVERLAPPABLE #-} (Random.FrozenGen f (t2 m), MonadTrans t1, MonadIO (t2 m)) => Random.FrozenGen (Random.Const f (ComposeT t1 t2)) (ComposeT t1 t2 m) where
type MutableGen (Random.Const f (ComposeT t1 t2)) (ComposeT t1 t2 m) = Random.Const (Random.MutableGen f (t2 m)) (ComposeT t1 t2)
freezeGen = ComposeT . lift . fmap Random.Const . Random.freezeGen . Random.getConst
thawGen = ComposeT . lift . fmap Random.Const . Random.thawGen . Random.getConst
-- TODO: `MonadIO (t2 m)` should not be required for this instance
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
instance {-# OVERLAPPABLE #-} (Random.RandomGenM g r (t2 m), MonadTrans t1, MonadIO (t2 m)) => Random.RandomGenM (Random.Const g (ComposeT t1 t2)) r (ComposeT t1 t2 m) where
applyRandomGenM f = ComposeT . lift . Random.applyRandomGenM f . Random.getConst
#endif
#if defined(VERSION_resourcet)
-- TODO: `MonadIO m` and `MonadTrans t2` should not be required for this instance
-- | /OVERLAPPABLE/.
-- Elevated to @(t2 m)@.
deriving via Elevator t1 (t2 (m :: Type -> Type))
instance {-# OVERLAPPABLE #-}
( ResourceT.MonadResource (t2 m)
, MonadTrans t1
, MonadIO m
, MonadTrans t2
) => ResourceT.MonadResource (ComposeT t1 t2 m)
-- TODO: `MonadIO m` and `MonadTrans t2` should not be required for this instance
-- | Set by 'ResourceT.ResourceT'.
deriving via ResourceT.ResourceT (t2 (m :: Type -> Type))
instance
( MonadIO (t2 m)
, MonadIO m
, MonadTrans t2
) => ResourceT.MonadResource (ComposeT ResourceT.ResourceT t2 m)
#endif
#if defined(VERSION_unliftio_core)
-- | Elevated to @m@.
deriving via Elevator (ComposeT t1 t2) m
instance
( UnliftIO.MonadUnliftIO m
, MonadTransControlIdentity (ComposeT t1 t2)
) =>
UnliftIO.MonadUnliftIO (ComposeT t1 t2 m)
#endif
-- ** Run 'ComposeT'
--
-- $runComposet
--
-- You have to run the composed monad transformers to get back into the base monad at some point.
-- | Run two stacked monad transformers.
--
-- This function takes the two individual monad transformer runners as arguments.
runComposeT :: (forall a. t1 (t2 m) a -> t2 m (StT t1 a)) -- ^ run @t1@
-> (forall a. t2 m a -> m (StT t2 a)) -- ^ run @t2@
-> (forall a. ComposeT t1 t2 m a -> m (StT t2 (StT t1 a)))
runComposeT runT1 runT2 = runT2 . runT1 . deComposeT
-- | Equivalent to 'runComposeT', but discards the monadic state 'StT'.
-- This is a simple approach when your monad transformer stack doesn't carry monadic state.
--
-- @
-- 'StT' ('ComposeT' t1 t2) a ~ a
-- @
--
-- This can be used to improve error messages when modifying a monad transformer stack.
runComposeT' :: (t1 (t2 m) a -> t2 m a) -- ^ run @t1@
-> (t2 m a -> m a) -- ^ run @t2@
-> (ComposeT t1 t2 m a -> m a)
runComposeT' runT1 runT2 = runT2 . runT1 . deComposeT
-- * Examples
-- ** Example 1: Create a new type class
--
-- $example1
--
-- When creating a new type class that supports 'ComposeT', you want to add recursive instances for
-- 'ComposeT'.
--
-- @
-- class 'Monad' m => MonadCustom m where
-- simpleMethod :: a -> m a
-- complicatedMethod :: (a -> m a) -> m a
-- @
--
-- You can easily derive those instances, after implementing an instance for 'Elevator'.
-- This is explained in "Control.Monad.Trans.Elevator".
--
-- Then it's possible to derive the recursive instance.
-- This is an /OVERLAPPABLE/ instance, because we want to be able to add new "base-case" instances
-- through transformers in a stack.
--
-- @
-- deriving via 'Elevator' t1 (t2 (m :: * -> *))
-- instance {-\# OVERLAPPABLE \#-}
-- ( 'Monad' (t1 (t2 m))
-- , 'MonadTransControl' t1
-- , MonadCustom (t2 m)
-- ) => MonadCustom ('ComposeT' t1 t2 m)
-- @
-- ** Example 2: Add an instance
--
-- $example2
--
-- Add a type class instance for a new monad transformer, when there already is a recursive instance
-- for 'ComposeT'.
--
-- @
-- newtype CustomT m a = CustomT { unCustomT :: t'Control.Monad.Trans.Identity.IdentityT' m a }
-- deriving newtype ('Functor', 'Applicative', 'Monad')
-- deriving newtype ('MonadTrans', 'MonadTransControl', 'MonadTransControlIdentity')
-- @
--
-- First we need the regular instance.
-- The method implementations are 'undefined' here, because they would only distract from
-- 'ComposeT'.
--
-- @
-- instance 'Monad' m => MonadCustom (CustomT m) where
-- simpleMethod = 'undefined'
-- complicatedMethod = 'undefined'
-- @
--
-- To add a "base-case" instance, that takes priority over the recursive instance,
-- /FlexibleInstances/ are required.
--
-- @
-- deriving via CustomT (t2 (m :: * -> *))
-- instance 'Monad' (t2 m) => MonadCustom ('ComposeT' CustomT t2 m)
-- @
-- ** Example 3: Build a transformer stack
--
-- $example3
--
-- Create a monad transformer stack and wrap it using a newtype.
--
-- @
-- type AppStackT = t'Control.Monad.Trans.Compose.Transparent.TransparentT' t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.Reader.ReaderT' 'Bool' t'Control.Monad.Trans.Compose.Infix..|>' CustomT t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.Reader.ReaderT' 'Char' t'Control.Monad.Trans.Compose.Infix..|>' 'Control.Monad.Trans.State.Lazy.StateT' 'Int'
-- newtype AppT m a = AppT { unAppT :: AppStackT m a }
-- deriving newtype ('Functor', 'Applicative', 'Monad')
-- @
--
-- Using t'Control.Monad.Trans.Compose.Infix..|>' we can write @AppStackT@ in the order of initialization.
-- We are adding t'Control.Monad.Trans.Compose.Transparent.TransparentT' to the bottom of the stack,
-- so that all the other transformer instances actually end up in the stack.
-- Now we can simply derive just the instances, that we want.
--
-- @
-- deriving newtype ('MonadTrans', 'MonadTransControl')
-- deriving newtype ('Control.Monad.State.Class.MonadState' 'Int')
-- deriving newtype MonadCustom
-- @
--
-- We can even access instances, that would have been shadowed in a regular transformer stack.
--
-- @
-- deriving newtype ('Control.Monad.Reader.Class.MonadReader' 'Bool')
-- @
-- ** Example 4: Run a transformer stack
--
-- $example4
--
-- This is the part, that actually contains your application logic.
-- Because of the setup with 'ComposeT', we won't have to worry about 'lift'ing during the
-- initialization.
-- With 'Control.Monad.Trans.Compose.Infix...>' we can use the order of initialization again.
--
-- @
-- runAppT :: AppT m a -> m ('StT' AppT a)
-- runAppT appTma =
-- 'Control.Monad.Trans.Compose.Transparent.runTransparentT'
-- 'Control.Monad.Trans.Compose.Infix../>' (\\ tma -> 'Control.Monad.Trans.Reader.runReaderT' tma 'True')
-- 'Control.Monad.Trans.Compose.Infix../>' runCustomT
-- 'Control.Monad.Trans.Compose.Infix../>' runReaderT'
-- 'Control.Monad.Trans.Compose.Infix../>' runStateT'
-- $ unAppT appTma
-- where
-- runReaderT' :: 'Control.Monad.Reader.Class.MonadReader' 'Bool' m => 'Control.Monad.Trans.Reader.ReaderT' 'Char' m a -> m a
-- runReaderT' tma = do
-- bool <- 'Control.Monad.Reader.Class.ask'
-- let char = if bool then \'Y\' else \'N\'
-- 'Control.Monad.Trans.Reader.runReaderT' tma char
--
-- runStateT' :: 'Control.Monad.Reader.Class.MonadReader' 'Char' m => 'Control.Monad.Trans.State.Lazy.StateT' 'Int' m a -> m (a, 'Int')
-- runStateT' tma = do
-- char <- 'Control.Monad.Reader.Class.ask'
-- let num = 'fromEnum' char
-- 'Control.Monad.Trans.State.Lazy.runStateT' tma num
-- @