Lean Explained with TypeScript: Writing Proofs as Types
Lean[1] is a programming language for proving mathematical propositions[2]. Its verifier checks automatically that a proof is correct, which makes proofs very strict but laborious to write. The author argues that Lean is also useful for regular software, for example to guarantee that certain error states are unreachable or that bank accounts can't spend money twice.
The author explains that the Lean verifier is essentially a type checker[3], where propositions are written as types and proofs as values. A proof is correct if its value has the matching type. Simple propositions can also be written in TypeScript: with an explicit type annotation, a wrong proof produces a type error. Because constants can't use type variables, proofs are wrapped in generic functions that are never meant to be called.
Logical operations map onto TypeScript types: false corresponds to never, true to a type with one dummy value, And to an object with left and right fields, and Or to a union of types. An implication is written as an arrow function, and negation is defined as P implies False. The author shows that modus ponens is simply function application.
Read the original gruhn.meDiscussion
Sources
-
gruhn.me
Lean explained with TypeScript
OriginalDiscussion · 5Discussion · 2Discussion · 1Discussion


