Avigad.Chapter7 #
Inductive Types
Exercise 1 #
Try defining other operations on the natural numbers, such as multiplication,
the predecessor function (with pred 0 = 0
), truncated subtraction (with
n - m = 0
when m
is greater than or equal to n
), and exponentiation. Then
try proving some of their basic properties, building on the theorems we have
already proved.
Since many of these are already defined in Lean’s core library, you should work within a namespace named hide, or something like that, in order to avoid name clashes.
Instances For
Equations
Instances For
Equations
Equations
Instances For
Equations
- Avigad.Chapter7.ex1.Nat.pred n = match n with | Avigad.Chapter7.ex1.Nat.zero => Avigad.Chapter7.ex1.Nat.zero | Avigad.Chapter7.ex1.Nat.succ n => n
Instances For
Equations
Instances For
Equations
Instances For
Exercise 2 #
Define some operations on lists, like a length
function or the reverse
function. Prove some properties, such as the following:
a. length (s ++ t) = length s + length t
b. length (reverse t) = length t
c. reverse (reverse t) = t
Exercise 3 #
Define an inductive data type consisting of terms built up from the following constructors:
• const n
, a constant denoting the natural number n
• var n
, a variable, numbered n
• plus s t
, denoting the sum of s
and t
• times s t
, denoting the product of s
and t
Recursively define a function that evaluates any such term with respect to an assignment of values to the variables.
- const: Nat → Avigad.Chapter7.ex3.Foo
- var: Nat → Avigad.Chapter7.ex3.Foo
- plus: Avigad.Chapter7.ex3.Foo → Avigad.Chapter7.ex3.Foo → Avigad.Chapter7.ex3.Foo
- times: Avigad.Chapter7.ex3.Foo → Avigad.Chapter7.ex3.Foo → Avigad.Chapter7.ex3.Foo
Instances For
Equations
- Avigad.Chapter7.ex3.value_at x [] = default
- Avigad.Chapter7.ex3.value_at 0 x = List.head! x
- Avigad.Chapter7.ex3.value_at (Nat.succ i) x = Avigad.Chapter7.ex3.value_at i (List.tail! x)
Instances For
Equations
- Avigad.Chapter7.ex3.eval_foo (Avigad.Chapter7.ex3.Foo.const n) x = n
- Avigad.Chapter7.ex3.eval_foo (Avigad.Chapter7.ex3.Foo.var n) x = Avigad.Chapter7.ex3.value_at n x
- Avigad.Chapter7.ex3.eval_foo (Avigad.Chapter7.ex3.Foo.plus m n) x = Avigad.Chapter7.ex3.eval_foo m x + Avigad.Chapter7.ex3.eval_foo n x
- Avigad.Chapter7.ex3.eval_foo (Avigad.Chapter7.ex3.Foo.times m n) x = Avigad.Chapter7.ex3.eval_foo m x * Avigad.Chapter7.ex3.eval_foo n x