Recursion and Induction on (Indexed) Inductive Types #
We have been using match
expressions to define functions on inductive types. In this file, we will see how to define functions using recursion and induction principles. These principles are automatically generated by Lean when an inductive type is defined, with functions like Nat.rec
automatically introduced.
The function that allows us to define (dependent) functions by recursion and prove results by induction on the natural numbers is Nat.rec
. It has the following type:
The motive
should really be motif, i.e., a template. To define functions to say Nat
, the motive is the constant function with value Nat
. We will first focus on this case. The motive is constant for recursive definitions of (non dependent) functions.
What the above says is that to define a function f
recusively, the data we need is:
- The value of
f
at0
. - The function
f
atn + 1
givenn
and the valuef n
off
atn
.
Equations
- stringFromNat = sorry
Instances For
When a function f
is defined using Nat.rec
, some definitional equalities are automatically generated. For example, the following is true by definition:
We can write the definitional equalities in a general form.
We see next recursive definitions of dependent functions. The motive is not constant in this case.
Concretely, we define:
Note that ×
and →
are right-associative. So, ℕ × ℕ × Unit
is the same as ℕ × (ℕ × Unit)
.
The recursive definition of tuple'
satisfies the following definitional equalities.