Type Classes
Type classes let Lean infer behavior automatically. This premium lesson shows how to define classes, create instances, and build reusable abstractions.
❗
This is a premium lesson. It adds real-world type class patterns and proof automation workflows.
Defining a Class
lean
1class Summable (α : Type) where2 zero : α3 add : α → α → α45instance : Summable Nat where6 zero := 07 add := Nat.addUsing Instances
lean
1def sumPair [Summable α] (x y : α) : α :=2 Summable.add x y34#eval sumPair 3 4Key Takeaway
Type classes encode reusable behavior. They power Lean's numeric operators, ordering, and many proof automation tools.
Premium Track
Premium content includes deeper type class hierarchies and instance search strategies.
View Premium Options