23
votes

There are similar questions here but they are attached to a particular programming language and I am looking for an answer on the conceptual level.

As I understand, Functors are essentially immutable containers that expose map() API which derives another functor. Which addition makes it possible to call a particular functor a monad?

As I understand, every monad is a functor but not every functor is a monad.

2
A functor takes a pure function (and a functorial value) whereas a monad takes a Kleisli arrow, i.e. a function that returns a monad (and a monadic value). Hence you can chain two monads and the second monad can depend on the result of the previous one. You cannot do this with functors.user6445533
Did you have a look at en.wikipedia.org/wiki/…? The important addition to the Functor API is flatMap (or bind or chain or however you want to call it). If you chose a particular language, it would be easier to explain, as the purely conceptual answer is category theory.Bergi
As @Bergi says the simple answer to your question (in the second paragraph) is the presence of a bind/flatMap/chain/whatever (the monadic function). Assuming your Functor/Monad obeys the monad laws, of course. But I don't think that is a very helpful conceptual answer. And, it is true that monads are functors because all it takes to transform a monad into a functor is a trivial application of the monadic function to create map/select/etc.melston
If your type constructor has no value-level inhabitants, you'll never be able to make a unit injection function despite be able to make an admittedly vacuous map function. Similar tricks can be played if the type parameter of the type constructor is not used at the term-level.Alec

2 Answers

11
votes

Let me explain my understanding without going into category theory:

Functors and monads both provide some tool to wrapped input, returning a wrapped output.

Functor = unit + map (i.e. the tool)

where,

unit = something which takes raw input and wraps it inside a small context.

map = the tool which takes a function as input, applies it to raw value in wrapper, and returns wrapped result.

Example: Let us define a function which doubles an integer

// doubleMe :: Int a -> Int b
const doubleMe = a => 2 * a;
Maybe(2).map(doubleMe)  // Maybe(4)

Monad = unit + flatMap (or bind or chain)

flatMap = the tool which flattens the map, as its name implies. It will be clear soon with the example below.

Example: Let us say we have a curried function which appends two strings only if both are not blank.

Let me define one as below:

append :: (string a,string b) -> Maybe(string c)  

Let's now see the problem with map (the tool that comes with Functor),

Maybe("a").map(append("b")) // Maybe(Maybe("ab"))  

How come there are two Maybes here?

Well, that's what map does; it applies the provided function to the wrapped value and wraps the result.

Let's break this into steps,

  1. Apply the mapped function to the wrapped value ; here the mapped function is append("b") and the wrapped value is "a", which results in Maybe("ab").

  2. Wrap the result, which returns Maybe(Maybe("ab")).

Now the value we are interested in is wrapped twice. Here comes flatMap to the rescue.

Maybe("a").flatMap(append("b")) // Maybe("ab")

Of course, functors and monads have to follow some other laws too, but I believe this is not in the scope of what is asked.

3
votes

(Note that this will be a simplified explanation for category theory concepts)

Functor

A Functor is a function from a set of values a to another set of values: a -> b. For a programming language this could be a function that goes from String -> Integer:

function fn(text: string) : integer

Composition

Composition is when you use the value of one function as input to the value of the next: fa(fb(x)). For example:

hash(lowercase(text))

Monads

A Monad allows to compose Functors that either are not composable otherwise, compose Functors by adding extra functionality in the composition, or both.

  • An example of the first is a Monad for a Functor String -> (String, Integer)

  • An example of the second is a Monad that counts the Number of functions called on a value

A Monad includes a Functor T that is responsible for the functionality you want plus two other functions:

  • input -> T(input)
  • T(T(input)) -> T(input)

The first function allows to transform your input values to a set of values that our Monad can compose. The second function allows for the composition.

So in conclusion, every Monad is not a Functor but uses a Functor to complete it's purpose.