Skip to main content

Arrows, Monads and Kleisli — part 1

Marcin Rzeźnicki

Oct 28, 2015|15 min read
Arrows Monads and Kleisli part 1 cover

Arrows are a type class used in programming to describe computations in a pure and declarative fashion. First proposed by computer scientist John Hughes as a generalization of monads, arrows provide a referentially transparent way of expressing relationships between logical steps in a computation. Unlike monads, arrows don’t limit steps to having one and only one input.

Arrow a b c represents a process that takes as input something of type b and outputs something of type c.

A type constructor arr that takes functions from any type s to another t, and lifts those functions into an arrow A between the two types.

A piping method first that takes an arrow between two types and converts it into an arrow between tuples. The first elements in the tuples represent the portion of the input and output that is altered, while the second elements are a third type u describing an unaltered portion that bypasses the computation.

A composition operator >>> that can attach a second arrow to a first as long as the first function’s output and the second’s input have matching types.

A merging operator *** that can take two arrows, possibly with different input and output types, and fuse them into one arrow between two compound types.

Image Alt

Arrows Monads and Kleisli part 1 business logic flow

Do or do not, there is no try.

The return operation takes a value from a plain type and puts it into a monadic container using the constructor, creating a monadic value. The bind operation takes as its arguments a monadic value and a function from a plain type to a monadic value, and returns a new monadic value.

Arrows Monads and Kleisli part 1 error handling

Type lambdas are cool and all, but not a single line of the compiler was ever written with them in mind. They’re just not going to work right: the relevant code is not robust.