mtl-uplift: Lift substacks of monad transformer stacks

[ control, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

Lift substacks of monad transformer stacks.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1
Dependencies base (>=4.12 && <5), mtl (>=2.2) [details]
License MIT
Copyright 2020 Samuel Schlesinger
Author Samuel Schlesinger
Maintainer sgschlesinger@gmail.com
Category Control
Home page https://github.com/SamuelSchlesinger/mtl-uplift
Bug tracker https://github.com/SamuelSchlesinger/mtl-uplift/issues
Source repo head: git clone https://github.com/samuelschlesinger/mtl-uplift
Uploaded by sgschlesinger at 2022-01-21T08:54:33Z
Distributions
Downloads 388 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-01-21 [all 1 reports]

Readme for mtl-uplift-0.1.0.1

[back to package description]

mtl-uplift

Hackage

Uplift entire substacks of monad transformer stacks, boilerplate free!

Many industry Haskell programmers are all too familiar with code like:

lift . lift . lift . lift . lift . lift . lift . lift . lift . lift . lift . lift . lift $ do
  blah

This may or may not be an exaggeration. Now, we can just write the much less visually annoying:

uplift @TopOfSubstackT $ do
  blah

This relieves the programmer from having to do any bookkeeping about what level of their greater stack their substack lives at, but just to know which monad tops it. Beyond that, if we transform our stack, with the old code we would potentially have to modify the number of calls to lift, but now, as long as we still want to uplift a substack with the same top, we don't have to. Isn't that uplifting?

Here is a more complete example from the tests:

{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BlockArguments #-}
module Main where

import Control.Monad.Trans.Uplift

import Control.Monad.State
import Control.Monad.Writer
import Control.Monad.Reader

type Stack = StateT Bool (WriterT [String] (ReaderT Char IO))

runStack :: Stack a -> IO (a, [String])
runStack stack = runReaderT (runWriterT (evalStateT stack True)) 'X'

main :: IO ()
main = do
  (a, xs) <- runStack do
    uplift @(WriterT [String]) do
      tell ["One", "Two"]
      c <- uplift @(ReaderT Char) ask
      if c == 'X' then tell ["One, Two, Three, Four"]
      else pure ()
  guard (xs == ["One", "Two", "One, Two, Three, Four"])