#readwise
# Monad Laws

## Metadata
- Author: [[Mark Seemann]]
- Full Title: Monad Laws
- URL: https://blog.ploeh.dk/2022/04/11/monad-laws/
## Highlights
- When looked at from the right angle, these are intuitive and reasonable rules: [identity](https://en.wikipedia.org/wiki/Identity_element) and [associativity](https://en.wikipedia.org/wiki/Associative_property).
That sounds almost like a [monoid](https://blog.ploeh.dk/2017/10/06/monoids), but the rules are more relaxed. For monoids, we require a [binary operation](https://en.wikipedia.org/wiki/Binary_operation). That's not the case for functors and monads. Instead of binary operations, for monads we're going to use [Kleisli composition](https://blog.ploeh.dk/2022/04/04/kleisli-composition). ([View Highlight](https://read.readwise.io/read/01gwrhvmaz34z165ww0cpgdjdj))
- In short, the **monad laws comprise three rules**:
• **Left identity**
• **Right identity**
• **Associativity** ([View Highlight](https://read.readwise.io/read/01gwrhzpff1j81nyksan700x4v))
- A left identity is an element that, when applied to the left of an operator, doesn't change the other element. When used with the [fish operator](https://blog.ploeh.dk/2022/04/04/kleisli-composition) it looks like this:
`id >=> h ≡ h`
**For monads, `id` turns out to be `return`**, so more specifically:
`return >=> h ≡ h` ([View Highlight](https://read.readwise.io/read/01gwrj30cm57tz7egwbxzhgqk4))
- Right identity starts out similar to left identity. Using the fish operator, the Haskell wiki describes it as:
`f >=> return ≡ f` ([View Highlight](https://read.readwise.io/read/01gwrj4w6d2xwc6rctw5rg4k19))
- `return` is the identity element for monadic composition. It doesn't really 'do' anything in itself. ([View Highlight](https://read.readwise.io/read/01gwrj590ss1jz77w406xxbh3v))
- **The third monad law is the associativity law. Keep in mind that an operator like `+` is associative if it's true that
`(x + y) + z = x + (y + z)`
Instead of `+`, the operator in question is the fish operator**, and since values are functions, we typically call them `f`, `g`, and `h` rather than `x`, `y`, and `z`:
`(f >=> g) >=> h ≡ f >=> (g >=> h)` ([View Highlight](https://read.readwise.io/read/01gwrj73x265thpbteef8bhgb1))