Oriel — AI & technology worth noticing, every day.

Monads in Lean

Web page · https://eli.thegreenplace.net/2026/monads-in-lean

First seen by Oriel on .

AI summary

The post teaches Lean 4 monads by progressively extending an expression evaluator to handle failures, logging, mutable state, and IO while hiding sequencing bookkeeping behind bind and do notation.

  • Lean’s Monad type class provides pure and bind; the >>= operator and do notation make chained computations concise while automatically propagating effects such as Option failures.
  • A generic evalM works with any Monad and a supplied division function, allowing the same evaluator to return Option or Except String results.
  • LoggedOption combines optional results with accumulated string logs, illustrating a writer monad whose bind implementation appends logs while propagating failure.
  • EvalM models state as Vars → Option (α × Vars), enabling variable storage and lookup while threading updated state through expression evaluation.
  • OptionT composes Option failure with IO logging, while LawfulMonad separately requires proofs of left identity, right identity, and associativity because Monad itself does not enforce them.

#lean 4 #monads #functional programming #type systems

Generated by gpt-5.6-sol. Check the original for details.

Metric trends

Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained

Points

Score reported by the source.

  • Hacker News: 13, latest observation ; +9 over observed period since
  • Lobsters: 13, latest observation ; +12 over observed period since

Comments

Comment count reported by the source.

  • Hacker News: 1, latest observation ; +1 over observed period since
  • Lobsters: 5, latest observation ; +5 over observed period since

Aliases

SourceExternal IDURL
Hacker News50450321https://eli.thegreenplace.net/2026/monads-in-lean/ Read discussion
Lobstersquicnyhttps://eli.thegreenplace.net/2026/monads-in-lean/ Read discussion