annotate automaton-in-agda/src/mul.agda @ 413:ad086c3161d7 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 18 Jun 2024 14:05:44 +0900
parents b85402051cdb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module mul where
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Relation.Binary.PropositionalEquality
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 data ℕ : Set where
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 zero : ℕ
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 suc : ℕ → ℕ
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 lemmm00 : ℕ
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 lemmm00 = suc (suc zero)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 _+_ : ℕ → ℕ → ℕ
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
13 x + zero = x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
14 x + (suc y) = suc (x + y)
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 _*_ : ℕ → ℕ → ℕ
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
17 x * zero = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
18 x * (suc y) = x + (x * y)
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 lemmm01 : ℕ
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 lemmm01 = suc (suc zero) + suc (suc zero)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 +-comm : ∀ x y → (x + y) ≡ (y + x)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 +-comm zero zero = refl
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 +-comm zero (suc y) = cong suc (+-comm zero y)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 +-comm (suc x) zero = cong suc (+-comm x zero)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 +-comm (suc x) (suc y) = begin
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
28 suc (suc x + y) ≡⟨ cong suc (+-comm (suc x) y) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
29 suc (y + suc x) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
30 suc (suc (y + x)) ≡⟨ cong suc (cong suc (+-comm y x )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
31 suc (suc (x + y)) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
32 suc (x + suc y) ≡⟨ cong suc (+-comm x (suc y) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
33 suc (suc y + x) ∎ where open ≡-Reasoning
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 +-assoc : ∀ x y z → (x + (y + z)) ≡ ((x + y) + z)
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
36 +-assoc x y zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
37 +-assoc x y (suc z) = cong suc (+-assoc x y z)
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
39 *-distr-r : {x y z : ℕ } → (x * y) + (x * z) ≡ x * (y + z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
40 *-distr-r {x} {y} {zero} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
41 *-distr-r {x} {y} {suc z} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
42 (x * y) + (x + (x * z)) ≡⟨ +-assoc (x * y) x _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
43 ((x * y) + x) + (x * z) ≡⟨ cong (λ k → k + (x * z)) (+-comm (x * y) _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
44 (x + (x * y) ) + (x * z) ≡⟨ sym (+-assoc x (x * y) _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
45 (x + ((x * y) + (x * z))) ≡⟨ cong (λ k → x + k)(*-distr-r {x} {y} {z}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
46 x + (x * (y + z)) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
47
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
48 *-distr-l : {x y z : ℕ } → (x * z) + (y * z) ≡ (x + y) * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
49 *-distr-l {x} {y} {zero} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
50 *-distr-l {x} {y} {suc z} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
51 (x + (x * z)) + (y + (y * z)) ≡⟨ +-assoc (x + (x * z)) y (y * z) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
52 ((x + (x * z)) + y) + (y * z) ≡⟨ cong (λ k → k + (y * z)) (sym (+-assoc x (x * z) y)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
53 (x + ((x * z) + y)) + (y * z) ≡⟨ cong (λ k → (x + k) + (y * z) ) (+-comm _ y) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
54 (x + (y + (x * z) )) + (y * z) ≡⟨ cong (λ k → k + (y * z)) (+-assoc x y (x * z)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
55 ((x + y) + (x * z) ) + (y * z) ≡⟨ sym (+-assoc (x + y) (x * z) (y * z)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
56 (x + y) + ((x * z) + (y * z)) ≡⟨ cong (λ k → (x + y) + k) (*-distr-l {x} {y} {z}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
57 (x + y) + ((x + y) * z) ∎ where open ≡-Reasoning
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 *-comm : ∀ x y → (x * y) ≡ (y * x)
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 *-comm zero zero = refl
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
61 *-comm zero (suc y) = trans (+-comm zero (zero * y)) (*-comm zero y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
62 *-comm (suc x) zero = trans (*-comm x zero) (+-comm (zero * x ) zero )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
63 *-comm (suc x) (suc y) = lemma2 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
64 lemma3 : (y : ℕ) → y ≡ (suc zero * y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
65 lemma3 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
66 lemma3 (suc y) = trans (cong suc (lemma3 y)) (+-comm _ (suc zero))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
67 lemma2 : (suc x * suc y) ≡ (suc y * suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
68 lemma2 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
69 (suc x * suc y) ≡⟨ sym ( *-distr-l {x} {suc zero} {suc y}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
70 (x + (x * y)) + (suc zero + (suc zero * y)) ≡⟨ cong (λ k → (x + (x * y)) + k) (+-comm (suc zero) (suc zero * y)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
71 (x + (x * y)) + suc (suc zero * y) ≡⟨ cong (λ k → (x + (x * y)) + k ) (cong suc (sym (lemma3 y))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
72 (x + (x * y)) + suc y ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
73 (x * suc y) + suc y ≡⟨ +-comm _ (suc y) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
74 suc y + (x * suc y ) ≡⟨ cong (λ k → suc y + k) (*-comm x (suc y)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
75 suc y + (suc y * x) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
76 suc y * suc x ∎ where open ≡-Reasoning
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
78 *-assoc : ∀ x y z → (x * (y * z)) ≡ ((x * y) * z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
79 *-assoc x y zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
80 *-assoc x y (suc z) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
81 x * (y * suc z) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
82 x * (y + (y * z)) ≡⟨ sym ( *-distr-r {x} {y} {y * z}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
83 (x * y) + (x * (y * z)) ≡⟨ cong (λ k → (x * y) + k) (*-assoc x y z) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 412
diff changeset
84 (x * y) + ((x * y) * z) ∎ where open ≡-Reasoning
412
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
b85402051cdb add mul
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87