remark-agda/test/Simple.lagda.md

50 lines
874 B
Markdown
Raw Normal View History

2024-09-13 00:43:27 +00:00
# Commutativity of addition
This document shows how to prove commutativity of addition on natural numbers.
<details>
<summary>Imports</summary>
2024-09-12 23:38:07 +00:00
```
module Simple where
open import Agda.Primitive
open import Relation.Binary.PropositionalEquality.Core
variable
2024-09-13 00:43:27 +00:00
l : Level
```
2024-09-12 23:38:07 +00:00
2024-09-13 00:43:27 +00:00
</details>
Declare our data structures:
```
2024-09-12 23:38:07 +00:00
data : Set where
2024-09-13 00:43:27 +00:00
zero :
suc :
```
2024-09-12 23:38:07 +00:00
2024-09-13 00:43:27 +00:00
Define addition:
```
2024-09-12 23:38:07 +00:00
_+_ :
zero + b = b
suc a + b = suc (a + b)
2024-09-13 00:43:27 +00:00
```
Prove commutativity:
2024-09-12 23:38:07 +00:00
2024-09-13 00:43:27 +00:00
```
2024-09-12 23:38:07 +00:00
+-comm : (m n : ) → m + n ≡ n + m
+-comm zero n = lemma n where
2024-09-13 00:43:27 +00:00
lemma : (n : ) → n ≡ n + zero
lemma zero = refl
lemma (suc n) = cong suc (lemma n)
2024-09-12 23:38:07 +00:00
+-comm (suc m) n = trans (cong suc (+-comm m n)) (sym (lemma n m)) where
2024-09-13 00:43:27 +00:00
lemma : (m n : ) → m + suc n ≡ suc (m + n)
lemma zero n = refl
lemma (suc m) n = cong suc (lemma m n)
```
That's it!