Deriving a base monad using a monad transformer and the identity monad

jules

I read (for example here and here) that all of the base monads (Mabye, Error, ...) are derived from their corresponding monad transformers (MaybeT, ErrorT, ...) using the identity monad Identity. An example would be:

type Maybe a = MaybeT Identity a

But that of course doesn't result in a constructor for Maybe a. And in the sources MaybeT is defined as newtype MaybeT m a = MaybeT (m (Maybe a)).

Am I missing something important? How could one derive a base monad using the corresponding monad transformer and the identitiy monad resulting in a concrete
constructor that can be matched against?

chi

Different approaches are being used here.

Sometimes, the base monad Foo is defined in terms of its transformer as FooT Identity. For example, State from transformer package, as Daniel Wagner points out.

Other times, the base monad Foo is defined independently. In these cases, it usually happens that Foo and FooT Indentity are distinct types, which however are isomorphic. This means that you can convert between both types without losing any information.

I guess that since Maybe is defined in the Haskell report as a Prelude type, we can not easily redefine it as the isomorphic MaybeT Identity. Indeed, since it's common to destruct/eliminate values in Maybe a by pattern matching against Nothing and Just _, we can't use another definition. If we had user-definable patterns we could use pattern Just x = Module.Just (Identity x), but we have not these (for now).

Instead, other monads such as State are not in the Prelude, nor in the Haskell report. They are also not typically destructed via pattern matching by whomever is importing Control.Monad.State. In such cases, it felt less harmful to move to the StateT Identity variant.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is there a valid array monad transformer?

From Java

How is Kleisli a monad Transformer?

From Dev

Stacking ResourceT monad transformer

From Dev

WriterT monad transformer

From Dev

Monad Transformer stacks in Scala

From Dev

Why is Identity monad useful?

From Dev

Unwrapping the STT monad in a transformer stack?

From Dev

Monad Transformer stacks with MaybeT and RandT

From Dev

Working with monad transformer RWST in Haskell

From Dev

Running the base monad of a pipe

From Dev

"No instance for MonadRandom" when using weightedSample in a monad transformer stack

From Dev

"No instance for MonadRandom" when using weightedSample in a monad transformer stack

From Dev

Use list monad inside monad transformer type classes?

From Dev

Tidying up Monads - turning application of a monad transformer into newtype monad

From Dev

Writing the Identity monad in terms of Free

From Dev

Using into() inside a monad pipeline

From Dev

Using the ST monad

From Dev

A monad of tuples (monad, state)?

From Dev

A monad of tuples (monad, state)?

From Dev

Adding a monad transformer to the Yesod Handler stack

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

Converting a nested type into a monad transformer stack

From Dev

Scotty monad transformer for per-handler Reader

From Dev

Is there a library or typeclass for getting the transformer version of a monad?

From Dev

How to implement the `List` monad transformer in Scala?

From Dev

Java 8 Generic of Generic for Monad Transformer

From Dev

How to refactor program that uses state monad transformer?

From Dev

MonadFix instance for interpreter monad transformer generated by FreeT?

From Dev

Defining bind for home-made monad transformer

Related Related

  1. 1

    Is there a valid array monad transformer?

  2. 2

    How is Kleisli a monad Transformer?

  3. 3

    Stacking ResourceT monad transformer

  4. 4

    WriterT monad transformer

  5. 5

    Monad Transformer stacks in Scala

  6. 6

    Why is Identity monad useful?

  7. 7

    Unwrapping the STT monad in a transformer stack?

  8. 8

    Monad Transformer stacks with MaybeT and RandT

  9. 9

    Working with monad transformer RWST in Haskell

  10. 10

    Running the base monad of a pipe

  11. 11

    "No instance for MonadRandom" when using weightedSample in a monad transformer stack

  12. 12

    "No instance for MonadRandom" when using weightedSample in a monad transformer stack

  13. 13

    Use list monad inside monad transformer type classes?

  14. 14

    Tidying up Monads - turning application of a monad transformer into newtype monad

  15. 15

    Writing the Identity monad in terms of Free

  16. 16

    Using into() inside a monad pipeline

  17. 17

    Using the ST monad

  18. 18

    A monad of tuples (monad, state)?

  19. 19

    A monad of tuples (monad, state)?

  20. 20

    Adding a monad transformer to the Yesod Handler stack

  21. 21

    How to get ReaderT to work with another monad transformer?

  22. 22

    Converting a nested type into a monad transformer stack

  23. 23

    Scotty monad transformer for per-handler Reader

  24. 24

    Is there a library or typeclass for getting the transformer version of a monad?

  25. 25

    How to implement the `List` monad transformer in Scala?

  26. 26

    Java 8 Generic of Generic for Monad Transformer

  27. 27

    How to refactor program that uses state monad transformer?

  28. 28

    MonadFix instance for interpreter monad transformer generated by FreeT?

  29. 29

    Defining bind for home-made monad transformer

HotTag

Archive