Skip to content
LeanLearn
Level 3~35 minLean 4

Your own data types

Define structures and inductive types, compute on them by pattern matching, and prove facts about every value by cases and structural induction.

You will be able to
  • Define a structure and an inductive type in Lean.
  • Write functions by pattern matching on constructors.
  • Prove a property of every value by cases (finite types) or structural induction (recursive types).

Recommended first: Mathematical induction: proving infinitely many cases, Equality and rewriting. You can start anyway. Nothing is locked.

EncounterActivity 1 of 8

Encounter

Lean's numbers, lists and even And and Or are not built in: they are inductive types, defined by listing the ways to build a value (the constructors). You can define your own.

  • A structure bundles fields: a point has an x and a y.
  • An enumeration lists finitely many values: the days of the week.
  • A recursive type builds values from smaller ones: a tree is a leaf, or a node with two smaller trees.

Whatever you define, Lean gives you two powers for free: pattern matching to compute, and cases/induction to prove.