typelevel / typelevel/cats

Monad Transformer typeclass

Open
#4,727 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

I saw this: https://github.com/typelevel/cats/pull/4724 and wondered if we have a monad transformer typeclass similar to: https://hackage.haskell.org/package/transformers-0.6.1.2/docs/Control-Monad-Trans-Class.html

I don't see one. I can imagine something like:

trait MonadTrans[T[_[_], _]] {
  def liftK[M[_]: Monad]: FunctionK[M, [A] =>> T[M, A]]
  def lift[M[_]: Monad, A](ma: M[A]): T[M, A]

  implicit def transMonad[M[_]: Monad]: Monad[[A] =>> T[M, A]]
}

And possibly even:

trait MonadTrans[T[_[_], _]] {
  def liftK[M[_]: Monad]: FunctionK[M, [A] =>> T[M, A]]
  def lift[M[_]: Monad, A](ma: M[A]): T[M, A]
  
  implicit def transMonad[M[_]: Monad]: Monad[[A] =>> T[M, A]]
  
  type RunT[_]
  def run[M[_]: Monad, A](fa: T[M, A]): M[RunT[A]]
  def liftRun[M[_]: Monad, A](run: M[RunT[A]]): T[M, A]
}

since I think all the transformers seem to have some notion of a "run" version. The laws would be:

MonadTrans[T].lift(fa) == MonadTrans[T].liftK[M](fa)

MonadTrans[T].lift(Monad[M].pure(a)) == MonadTrans[T].transMonad[M].pure(a)

MonadTrans[T].lift(ma.flatMap(f)) == MonadTrans[T].lift(ma).flatMap(a => MonadTrans[T].lift(f(a)))

MonadTrans[T].liftRun(MonadTrans[T].run(fa)) == fa

The argument against this, I think, would be that it's not clear what useful functions you would write against MonadTrans except perhaps for having a clear way to lift: MonadTrans[OptionT].lift(fa)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed MonadTrans signatures and laws in this issue, then compare them with Cats pull request 4724 and Haskell's transformers MonadTrans documentation. No repository files or tests are named; done would require an agreed design and a clear implementation and test scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.