Is an IS type constructor a monad or a monad?

People usually say that type IS is a monad.

In some functional languages ​​and libraries (for example, Scala / Scalaz) you have a constructor of type List or Option, and you can define a Monad implementation that is separate from the original type. Thus, in principle, there is nothing that would prohibit you in the type system from creating separate Monad instances for the same type constructor.


  • Is it possible for a type constructor to have multiple monads?
  • If so, can you provide any meaningful example of this? any "artificial"?
  • what about monoids, applications ...?
+5
source share
2 answers

You can find it all in math.

  • A monad is a triple (T, return, bind) such that (...). When bind and return can be inferred from the context, we simply refer to the monad as T

  • A monoid is a triple (M, e, β€’) such that (...). (...) we simply refer to the monoid as M

  • A topological space is a pair (S, T) such that (...). We simply refer to a topological space as S

  • The ring is a tuple (V, 0, +, 1, Γ—) ...

So, for a given type constructor T may be several different definitions of return and bind that make the monad. To avoid having to refer to the triple each time, we can give T different names to disambiguate in a way that matches the newtype construct in Haskell. For example: [] vs ZipList , State s vs ReaderT s (Writer s) .


PS There is something artificial in that the monad or monoid is triple, especially considering that there are different presentations: we could also say that the monad is triple (T, fmap, join) or that the monoid is a pair (M, β€’) , with an identification element hidden in an additional condition (because it is uniquely defined β€’ ). The ontology of mathematical structures is a more philosophical issue that goes beyond SO (and also beyond my experience). But a more reasonable way to reformulate such definitions may be to say that "the monad (defined | characterized) by the triple (T, return, bind) ".

+14
source

Since you're asking about using the language, Google says the phrase β€œhas a monad” doesn't seem to be used normally the way you ask. Most real cases are present in sentences such as "The Haskell community has a problem with the monad ." However, several cases of vaguely similar use exist in the wild, such as "the only thing that makes it" monadic "is that it has a copy of Monad ." That is, the monad is often used as a synonym for monadic, changing some other noun to create a phrase (monad problem, instance of Monad ), which is sometimes used as an object of the verb.

As for coding: in Haskell, a type can declare one instance from Monad , one from Monoid and so on. When a certain type can have many such instances, for example, as numbers are monoids when adding, multiplying, maximum, minimum, and many other operations, Haskell defines separate types, such as Sum Int , a Monoid instance on Int where operation + and Product Int . a Monoid , where the operation * .

I have not comprehensively checked tens of thousands of hits, although, therefore, it is very possible that there are better examples of what you are asking for.

The snippet that I usually saw for him is the one I just used: type - category under .

+2
source

Source: https://habr.com/ru/post/1274677/


All Articles