1
votes

Question

Please help confirm or correct the understandings of what Monad is and its traits.

As Data Type

In my understanding, a Monad is:

  1. a container which can accommodate any type T and
  2. provides a bind interface that allows its client to apply a flat-map function and
  3. projects its content into another Monad of any type T'.

There need to be a return or unit interface to create a Monad of type T.

unit:= T -> M[T]

enter image description here

In Scala, List() or Set() are the examples of return interface, and any Scala sequence types (Array, List, Map, String) are Monad which provide flatMap interface which is bind.

Are these correct?

As Design Pattern

Software engineering provides ways to manage complexity or to structure software, such as Structured Programming without goto, UNIX pipe to pipeline transformation, Object Oriented to encapsulate data & control access, etc.

Is Monad a design pattern providing a way to structure a computation as a chain?

In other systems

UNIX commands

I suppose UNIX commands e.g. cat, grep are functions that can be chained but it does not mean they are Monad, and they are not Monad because they do not have return/unit nor they are not data type. Or is it still regarded e.g. IO Monad as in Monadic i/o and UNIX shell programming?

Python

I believe there is no bind or Scala flatMap equivalent in Python out of the box. Can I say Python does not have Monad feature out of the box?

References

1
no (to your graphic), bind applies f (A -> M[B]) to A value(s) "in" the M[A] value and produces one combined M[B] value. if we just map that f we end up with M[M[B]], but monadic bind "splices" those M[B]s inside the one combined M[B]: twice_each [x,y,z] = [x,x] ++ [y,y] ++ [z,z] or twice_each xs: for x in xs: for a in [x,x]: yield a.Will Ness

1 Answers

0
votes
  1. Yes, you're right about those interface things. However, it is noteworthy that in abstraction, a monad should have two adjunctive methods which can be composed to chain the computations. Note that flatMap is simply the composition of such methods - flat and map. map can be used to define a computation of type M[A] -> M[M[B]] and flat which is used to define M[M[B]] -> M[B].

  2. Yes, in Scala they're a means to chain computations.

  3. The shell script commands may fulfil the purpose of the monads (in the considered analogies) but still can't be regarded as monads (by me at least) as they don't necessarily comply with point 1.

  4. Yes, the monads are NOT supported out-of-the-box in Python. One has to rely on the nested loops only.