UP | HOME

Bullshit Monad

At the end of the day one sees just another instance of the same common pattern - Upanishads turned into a obscurity, Buddhism ruined by sects, philosophy degraded into a contest of systems of abstract bullshitting, science became a cult.

Yes, this is somehow a locus at the core (boy, I hate this word!) of programming, but nevertheless.

The level of bullshitting about Monads is way off charts. This is why so many pages of “commentaries” and gradual clarifications are needed - do reduce bullshit to What actually Is.

There is literally 2/3 of fundamental Computer Science entangled in this “locus”.

The locus (of programming) consist of:

Interfaces

A minimal interface is a single typed lambda. (We do not talk about empty interfaces).

We do currying to get a multi-argument lambdas and we partially apply curried lambdas.

This is how to make DSLs out of lambdas.

Modules

A minimal module is one that exports a single such typed lambda. This is rather orthogonal concept for structuring the code, but it can be taken to a higher level with the concept of a Functor and applying one module to another (to get a specialization or compose “traits”).

Just an ADT

A minimal ADT is a non-leaking abstract type, defined only in terms of exported interface (a set of operations) and informally stated relations or “laws” among them.

Mathematicians defined the Set abstraction in this way, without considering anything else but operations, relations and “laws”. This is, of course, a perfect ADT 200 years before any computers. Barbara Liskov just have noticed this first.

Abstract Data Types (not machine types) are the basic building blocks. Everything builds upon this required principle.

A “class” for Types (ADTs)

This unfortunate, crappy OO terminology confuses everyone. A type-class defines a shape of a Trait via required set interfaces (and informally stated “laws” and invariants).

A Monad is naturally defined as a type-class and to implement it is become an instance of it.

And also an instance of a…

This is the famous Duck Typing (walks like a duck and quacks like a duck). This literally means that an ADT implements another set of interfaces (and the required “laws” and invariants)

Universal patterns

The abstract concept itself is too general, so Monads have specializations, and which particular Monad is another question.

It turns out that this general concept captures and generalizes nicely the fundamental notion of an abstraction barrier, which in turn, is as fundamental as a cell membrane (a partition).

Passing a context along

A context could be passed explicitly as a parameter (partially applied or not) or implicitly as an implicit parameter (in languages which support implicits).

  • to pass around the “State of the whole World” (an IO Monad)
  • to pass a partially consumed input Stream (a Reader Monad)
  • to pass along a partially filled String (a Writer Monad)
  • etc.

The fundamental property of referential transparency holds because no reference to a context is leaked out. This implies that there at most one reference to a context.

There is, however, a standard idiom (in a function programming world) of passing the context within >>=, so it is “hidden” completely behind an abstraction barrier, and there is zero public references to the context and the user of an exported interfaces cannot even “see” that these is one.

This trick alone justifies the use of a Monad type-class, and this idiom is so universal, that the IO Monad can be though of (modeled) as passing of the state of the world behind the abstraction barrier, and the referential transparency property holds.

Any instance of an IO Monad, lets say, is just a value in a pure-functional world, while all the updating (which is described declaratively) would happen eventually behind the abstraction barrier (inside >>==) and will actually be done at runtime.

Nesting of pure functions

  • Nesting is how one defines (or establishes) an explicit order of evaluation.

Abstraction barrier

Author: <schiptsov@gmail.com>

Email: lngnmn2@yahoo.com

Created: 2023-08-08 Tue 18:42

Emacs 29.1.50 (Org mode 9.7-pre)