Avigad.Chapter8 #
Induction and Recursion
Exercise 1 #
Open a namespace Hidden
to avoid naming conflicts, and use the equation
compiler to define addition, multiplication, and exponentiation on the natural
numbers. Then use the equation compiler to derive some of their basic
properties.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Exercise 2 #
Similarly, use the equation compiler to define some basic operations on lists
(like the reverse function) and prove theorems about lists by induction (such as
the fact that reverse (reverse xs) = xs
for any list xs
).
Equations
- Avigad.Chapter8.ex2.reverse [] = []
- Avigad.Chapter8.ex2.reverse (head :: tail) = Avigad.Chapter8.ex2.reverse tail ++ [head]
Instances For
Exercise 3 #
Define your own function to carry out course-of-value recursion on the natural
numbers. Similarly, see if you can figure out how to define WellFounded.fix
on
your own.
A copied implementation of Nat.brecOn
with the motive
properly supplied.
Notice the noncomputable
tag; the code generator does not support the recOn
recursor yet, for reasons I haven't fully understood yet. This thread touches on
the topic:
https://leanprover-community.github.io/archive/stream/270676-lean4/topic/code.20generator.20does.20not.20support.20recursor.html
Equations
- One or more equations did not get rendered due to their size.
Instances For
Exercise 4 #
Following the examples in Section Dependent Pattern Matching, define a function that will append two vectors. This is tricky; you will have to define an auxiliary function.
- nil: {α : Type u} → Avigad.Chapter8.ex4.Vector α 0
- cons: {α : Type u} → α → {n : Nat} → Avigad.Chapter8.ex4.Vector α n → Avigad.Chapter8.ex4.Vector α (n + 1)
Instances For
As long we flip the indices in our type signature in the resulting summation, there is no need for an auxiliary function.
Equations
- Avigad.Chapter8.ex4.Vector.append Avigad.Chapter8.ex4.Vector.nil x = x
- Avigad.Chapter8.ex4.Vector.append (Avigad.Chapter8.ex4.Vector.cons a as) x = Avigad.Chapter8.ex4.Vector.cons a (Avigad.Chapter8.ex4.Vector.append as x)
Instances For
Exercise 5 #
Consider the following type of arithmetic expressions.
- const: Nat → Avigad.Chapter8.ex5.Expr
- var: Nat → Avigad.Chapter8.ex5.Expr
- plus: Avigad.Chapter8.ex5.Expr → Avigad.Chapter8.ex5.Expr → Avigad.Chapter8.ex5.Expr
- times: Avigad.Chapter8.ex5.Expr → Avigad.Chapter8.ex5.Expr → Avigad.Chapter8.ex5.Expr
Instances For
Equations
- Avigad.Chapter8.ex5.instReprExpr = { reprPrec := Avigad.Chapter8.ex5.reprExpr✝ }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Avigad.Chapter8.ex5.eval v (Avigad.Chapter8.ex5.Expr.const a) = a
- Avigad.Chapter8.ex5.eval v (Avigad.Chapter8.ex5.Expr.var a) = v a
- Avigad.Chapter8.ex5.eval v (Avigad.Chapter8.ex5.Expr.plus a a_1) = Avigad.Chapter8.ex5.eval v a + Avigad.Chapter8.ex5.eval v a_1
- Avigad.Chapter8.ex5.eval v (Avigad.Chapter8.ex5.Expr.times a a_1) = Avigad.Chapter8.ex5.eval v a * Avigad.Chapter8.ex5.eval v a_1
Instances For
Equations
- Avigad.Chapter8.ex5.sampleVal x = match x with | 0 => 5 | 1 => 6 | x => 0
Instances For
Constant Fusion #
Implement "constant fusion," a procedure that simplifies subterms like 5 + 7 to
12. Using the auxiliary function
simpConst, define a function "fuse": to simplify a plus or a times, first simplify the arguments recursively, and then apply
simpConst` to try to simplify the result.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Avigad.Chapter8.ex5.fuse (Avigad.Chapter8.ex5.Expr.plus e₁ e₂) = Avigad.Chapter8.ex5.simpConst (Avigad.Chapter8.ex5.Expr.plus (Avigad.Chapter8.ex5.fuse e₁) (Avigad.Chapter8.ex5.fuse e₂))
- Avigad.Chapter8.ex5.fuse (Avigad.Chapter8.ex5.Expr.times e₁ e₂) = Avigad.Chapter8.ex5.simpConst (Avigad.Chapter8.ex5.Expr.times (Avigad.Chapter8.ex5.fuse e₁) (Avigad.Chapter8.ex5.fuse e₂))
- Avigad.Chapter8.ex5.fuse x = x