concept monad in category system administration

appears as: monads, monad, A monad, monad, The monad
Functional and Reactive Domain Modeling

This is an excerpt from Manning's book Functional and Reactive Domain Modeling.

In functional programming, an effect adds some capabilities to a computation. And because we’re dealing with a statically typed language, these capabilities come in the form of more power from the type system. An effect is modeled usually in the form of a type constructor that constructs types with these additional capabilities. Say you have any type A and you’d like to add the capability of aggregation, so that you can treat a collection of A as a separate type. You do this by constructing a type List[A] (for which the corresponding type constructor is List), which adds the effect of aggregation on A. Similarly, you can have Option[A] that adds the capability of optionality for the type A. In the next section, you’ll learn how to use type constructors such as Try and Future to model the effects of exceptions and latency, respectively. In chapter 4 we’ll discuss more advanced effect handling using applicatives and monads.

What does a generic monad module look like in Scala? A monad implements different semantics than an applicative. But a monad is also an applicative, and that makes it a more powerful abstraction than an applicative. Apart from supporting all the functions that an applicative does, a monad also implements flatMap:

trait Monad[F[_]] extends Applicative[F] {
   def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
  //.. other Applicative functions
}

You’ve seen flatMap as a method in List, Option, and many other classes in the Scala standard library. But what does the algebra of a generic flatMap, as defined in the preceding Monad module, tell you? It looks similar to the map combinator that you saw in Functor. The only difference is the signature of the function, f, that a flatMap takes. In the case of map, the input function returns a value, but in flatMap it returns an instance of the type for the monad (for example, List or Option). When you apply the function f to the input ma of type F[A] in flatMap, you end up with F[F[B]]. And the flatMap joins the two, flattening them into one single effect, F[B]. Semantically, a flatMap is equivalent to a map followed by a flatten.[18]

18For a complete explanation of how to implement monads, see Functional Programming in Scala.