# Agda学习笔记1

## 快捷键

• C-c C-l : 加载，把问号转换成goal
• C-c C-f/C-b : 在goal之间切换
• C-c C-, : goal&context
• C-c C-. : goal&context&type
• C-c C-r : refine 有时可以自动填充
• C-c C-c spilt:
• 直接回车：补上变量
• 输入变量：把这个变量解释成所有定义
• C-c C-a : 自动填充（一般用不上）

## Natural Number

### 自然数集合

data $$ℕ$$ : Set where

zero : $$ℕ$$

suc : $$ℕ \rightarrow ℕ$$

### operations

1. $$\_+\_ : ℕ → ℕ → ℕ$$

zero + n = n

suc m + n = suc (m + n)

2. $$\_*\_ : ℕ → ℕ → ℕ$$

zero * n = zero

suc m * n = n + (m * n)

3. $$pred : ℕ → ℕ$$

pred 0 = 0

pred (suc n) = n

### cong

cong f : 把 f 添加到左右两边

+0 : ∀ (y : ℕ) -> y ≡ y + zero
+0 zero = refl
+0 (suc y) = cong suc (+0 y)


### 加法结合律

+assoc : ∀ (x y z : ℕ) → x + (y + z) ≡ (x + y) + z
+assoc zero y z = refl
+assoc (suc x) y z rewrite +assoc x y z = refl


### 加法交换律

+suc : ∀ (x y : ℕ) → suc x + y ≡ x + suc y
+suc zero y = refl
+suc (suc x) y rewrite +suc x y = refl

+comm : ∀ (x y : ℕ) → x + y ≡ y + x
+comm zero y = +0 y
+comm (suc x) y rewrite +comm x y = +suc y x


+comm : ∀ (x y : ℕ) → x + y ≡ y + x
+comm zero y = +0 y
+comm (suc x) y rewrite +comm x y | +suc y x = refl


rewrite加竖线就是从左到右替换

### 乘法分配律

*distribr : ∀ (x y z : ℕ) → (x + y) * z ≡ x * z + y * z
*distribr zero y z = refl
*distribr (suc x) y z rewrite *distribr x y z | +assoc z (x * z) (y * z) = refl


### 比较大小

_<_ : ℕ → ℕ → 𝔹
0 < 0 = ff
0 < (suc y) = tt
(suc x) < (suc y) = x < y
(suc x) < 0 = ff
_=ℕ_ : ℕ → ℕ → 𝔹
0 =ℕ 0 = tt
suc x =ℕ suc y = x =ℕ y
_ =ℕ _ = ff
_≤_ : ℕ → ℕ → 𝔹
x ≤ y = (x < y) || x =ℕ y
_>_ : ℕ → ℕ → 𝔹
a > b = b < a
_≥_ : ℕ → ℕ → 𝔹
a ≥ b = b ≤ a


### 衍生的一些证明

<-0 : ∀ (x : ℕ) → x < 0 ≡ false
<-0 0 = refl
<-0 (suc y) = refl

𝔹-contra : false ≡ true → ∀{ℓ} {P : Set ℓ} → P
𝔹-contra ()

<-trans : ∀ {x y z : ℕ} → x < y ≡ true → y < z ≡ true → x < z ≡ true
<-trans {x} {0} p1 p2 rewrite <-0 x = 𝔹-contra p1
<-trans {0} {suc y} {0} p1 ()
<-trans {0} {suc y} {suc z} p1 p2 = refl
<-trans {suc x} {suc y} {0} p1 ()
<-trans {suc x} {suc y} {suc z} p1 p2 = <-trans {x} {y} {z} p1 p2


=ℕ-refl : ∀ (x : ℕ) → (x =ℕ x) ≡ tt
=ℕ-refl 0 = refl
=ℕ-refl (suc x) = =ℕ-refl x

=ℕ-from-≡ : ∀ {x y : ℕ} → x ≡ y → x =ℕ y ≡ tt
=ℕ-from-≡ {x} refl = =ℕ-refl x


### begin-qed

+0 : ∀ (y : ℕ) -> y ≡ y + zero
+0 zero = refl
+0 (suc y) =
begin
suc y
≡⟨ cong suc (+0 y) ⟩
suc (y + zero)
≡⟨⟩
suc y + zero
∎


<>里的是依据，某些根据定义的依据可以不用写(如zero+x=x)

## 作业题

### 乘法交换律

+assoc' : ∀ (x y z : ℕ) → (x + y) + z ≡ x + (y + z)
+assoc' x y z rewrite +assoc x y z = refl

*0 : ∀ (x : ℕ) → zero ≡ x * zero
*0 zero = refl
*0 (suc x) = *0 x

*suc : (x y : ℕ) → x + x * y ≡ x * suc y
*suc zero y = refl
*suc (suc x) y rewrite +assoc x y (x * y) | +comm x y | +assoc' y x (x * y) | *suc x y = refl

*-comm : (x y : ℕ) → x * y ≡ y * x
*-comm zero zero = refl
*-comm zero (suc y) = *-comm zero y
*-comm (suc x) y rewrite *-comm x y = *suc y x


### 乘法结合律

*-assoc : (x y z : ℕ) → (x * y) * z ≡ x * (y * z)
*-assoc zero y z = refl
*-assoc (suc x) y z rewrite *distribr y (x * y) z | *-assoc x y z = refl


### 一些比较大小

n≮n : (n : ℕ) → n < n ≡ false
n≮n zero = refl
n≮n (suc x) = n≮n x

-- problem 2.2
<-antisym : (x y : ℕ) → x < y ≡ true → y < x ≡ false
<-antisym zero (suc y) p1 = refl
<-antisym (suc x) (suc y) = <-antisym x y

-- problem 2.3
<-trichotomy : (x y : ℕ) → x < y ∨ x =ℕ y ∨ y < x ≡ true
<-trichotomy zero zero = refl
<-trichotomy zero (suc y) = refl
<-trichotomy (suc x) zero = refl
<-trichotomy (suc x) (suc y) = <-trichotomy x y

posted @ 2022-11-04 22:39  lcyfrog  阅读(174)  评论(0编辑  收藏  举报