-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNaturalsLecture.agda
76 lines (55 loc) · 1.69 KB
/
NaturalsLecture.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module NaturalsLecture where
{-
January 13, 2020, Part 1
-}
data Nat : Set where
zero : Nat
suc : Nat → Nat
_ : Nat
_ = zero
one : Nat
one = suc zero
two : Nat
two = suc (suc zero)
add : Nat → Nat → Nat
add zero n = n
add (suc m) n = suc (add m n)
three = add two one
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
_ : zero ≡ zero
_ = refl
_ : three ≡ suc (suc (suc zero))
_ = refl
open import Data.Nat
gauss : ℕ → ℕ
gauss zero = 0
gauss (suc n) = suc n + gauss n
open import Data.Nat.Properties
open Relation.Binary.PropositionalEquality.≡-Reasoning using (begin_; _≡⟨_⟩_; _∎)
open import Relation.Binary.PropositionalEquality using (sym; cong; cong₂)
_ : (x : ℕ) → (y : ℕ) → x + y + x ≡ 2 * x + y
_ = λ x y →
begin
(x + y) + x ≡⟨ +-comm (x + y) x ⟩
x + (x + y) ≡⟨ sym (+-assoc x x y) ⟩
(x + x) + y ≡⟨ cong₂ _+_ {u = y} (cong₂ _+_ {x = x } refl (sym (+-identityʳ x))) refl ⟩
(x + (x + zero)) + y ≡⟨ refl ⟩
2 * x + y
∎
{-
Alternative version using cong and a λ instead of cong₂.
Thanks Jacob and Kuang-Chen!
-}
_ : (x : ℕ) → (y : ℕ) → x + y + x ≡ 2 * x + y
_ = λ x y →
begin
(x + y) + x ≡⟨ +-comm (x + y) x ⟩
x + (x + y) ≡⟨ sym (+-assoc x x y) ⟩
(x + x) + y ≡⟨ cong (λ □ → (x + □) + y) (sym (+-identityʳ x)) ⟩
(x + (x + zero)) + y ≡⟨ refl ⟩
2 * x + y
∎
open import Data.Nat.Solver using (module +-*-Solver)
open +-*-Solver
_ : ∀ (x y : ℕ) → x + y + x ≡ 2 * x + y
_ = solve 2 (λ x y → x :+ y :+ x := con 2 :* x :+ y) refl