2014-01-06 06:53:03 +00:00
|
|
|
import macros
|
|
|
|
using Nat
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-05 16:52:46 +00:00
|
|
|
-- In this example, we prove two simple theorems about even/odd numbers.
|
|
|
|
-- First, we define the predicates even and odd.
|
2014-01-06 06:53:03 +00:00
|
|
|
definition even (a : Nat) := ∃ b, a = 2*b
|
|
|
|
definition odd (a : Nat) := ∃ b, a = 2*b + 1
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-05 16:52:46 +00:00
|
|
|
-- Prove that the sum of two even numbers is even.
|
|
|
|
--
|
|
|
|
-- Notes: we use the macro
|
2014-01-06 03:10:21 +00:00
|
|
|
-- obtain [bindings] ',' 'from' [expr]_1 ',' [expr]_2
|
2014-01-05 16:52:46 +00:00
|
|
|
--
|
|
|
|
-- It is syntax sugar for existential elimination.
|
|
|
|
-- It expands to
|
|
|
|
--
|
2014-01-09 16:33:52 +00:00
|
|
|
-- exists_elim [expr]_1 (fun [binding], [expr]_2)
|
2014-01-05 16:52:46 +00:00
|
|
|
--
|
|
|
|
-- It is defined in the file macros.lua.
|
|
|
|
--
|
|
|
|
-- We also use the calculational proof style.
|
|
|
|
-- See doc/lean/calc.md for more information.
|
|
|
|
--
|
2014-01-06 03:10:21 +00:00
|
|
|
-- We use the first two obtain-expressions to extract the
|
2014-01-05 16:52:46 +00:00
|
|
|
-- witnesses w1 and w2 s.t. a = 2*w1 and b = 2*w2.
|
|
|
|
-- We can do that because H1 and H2 are evidence/proof for the
|
|
|
|
-- fact that 'a' and 'b' are even.
|
|
|
|
--
|
|
|
|
-- We use a calculational proof 'calc' expression to derive
|
|
|
|
-- the witness w1+w2 for the fact that a+b is also even.
|
|
|
|
-- That is, we provide a derivation showing that a+b = 2*(w1 + w2)
|
2014-01-05 20:05:08 +00:00
|
|
|
theorem EvenPlusEven {a b : Nat} (H1 : even a) (H2 : even b) : even (a + b)
|
2014-01-06 03:10:21 +00:00
|
|
|
:= obtain (w1 : Nat) (Hw1 : a = 2*w1), from H1,
|
|
|
|
obtain (w2 : Nat) (Hw2 : b = 2*w2), from H2,
|
2014-01-09 16:33:52 +00:00
|
|
|
exists_intro (w1 + w2)
|
2014-01-06 03:10:21 +00:00
|
|
|
(calc a + b = 2*w1 + b : { Hw1 }
|
|
|
|
... = 2*w1 + 2*w2 : { Hw2 }
|
2014-01-07 21:44:53 +00:00
|
|
|
... = 2*(w1 + w2) : symm (distributer 2 w1 w2))
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-09 16:33:52 +00:00
|
|
|
-- In the following example, we omit the arguments for add_assoc, refl and distribute.
|
2014-01-05 16:52:46 +00:00
|
|
|
-- Lean can infer them automatically.
|
|
|
|
--
|
2014-01-06 19:48:27 +00:00
|
|
|
-- refl is the reflexivity proof. (refl a) is a proof that two
|
2014-01-05 16:52:46 +00:00
|
|
|
-- definitionally equal terms are indeed equal.
|
2014-01-05 20:05:08 +00:00
|
|
|
-- "definitionally equal" means that they have the same normal form.
|
2014-01-05 16:52:46 +00:00
|
|
|
-- We can also view it as "Proof by computation".
|
|
|
|
-- The normal form of (1+1), and 2*1 is 2.
|
|
|
|
--
|
|
|
|
-- Another remark: '2*w + 1 + 1' is not definitionally equal to '2*w + 2*1'.
|
|
|
|
-- The gotcha is that '2*w + 1 + 1' is actually '(2*w + 1) + 1' since +
|
|
|
|
-- is left associative. Moreover, Lean normalizer does not use
|
|
|
|
-- any theorems such as + associativity.
|
2014-01-05 20:05:08 +00:00
|
|
|
theorem OddPlusOne {a : Nat} (H : odd a) : even (a + 1)
|
2014-01-06 03:10:21 +00:00
|
|
|
:= obtain (w : Nat) (Hw : a = 2*w + 1), from H,
|
2014-01-09 16:33:52 +00:00
|
|
|
exists_intro (w + 1)
|
2014-01-06 03:10:21 +00:00
|
|
|
(calc a + 1 = 2*w + 1 + 1 : { Hw }
|
2014-01-20 23:38:00 +00:00
|
|
|
... = 2*w + (1 + 1) : add_assoc _ _ _
|
2014-01-06 03:10:21 +00:00
|
|
|
... = 2*w + 2*1 : refl _
|
2014-01-07 21:44:53 +00:00
|
|
|
... = 2*(w + 1) : symm (distributer _ _ _))
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-05 16:52:46 +00:00
|
|
|
-- The following command displays the proof object produced by Lean after
|
|
|
|
-- expanding macros, and infering implicit/missing arguments.
|
2014-01-06 06:53:03 +00:00
|
|
|
print environment 2
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-05 16:52:46 +00:00
|
|
|
-- By default, Lean does not display implicit arguments.
|
|
|
|
-- The following command will force it to display them.
|
2014-01-09 16:33:52 +00:00
|
|
|
set_option pp::implicit true
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-06 06:53:03 +00:00
|
|
|
print environment 2
|
2014-01-04 02:45:10 +00:00
|
|
|
|
2014-01-05 16:52:46 +00:00
|
|
|
-- As an exercise, prove that the sum of two odd numbers is even,
|
|
|
|
-- and other similar theorems.
|