2023-04-18 18:42:40 -04:00
|
|
|
|
load "misc.quox";
|
|
|
|
|
load "bool.quox";
|
|
|
|
|
load "either.quox";
|
|
|
|
|
|
|
|
|
|
namespace nat {
|
|
|
|
|
|
2023-07-22 15:26:20 -04:00
|
|
|
|
def dup! : (n : ℕ) → [ω. Sing ℕ n] =
|
2023-04-01 13:16:30 -04:00
|
|
|
|
λ n ⇒
|
2023-07-22 15:26:20 -04:00
|
|
|
|
case n return n' ⇒ [ω. Sing ℕ n'] of {
|
|
|
|
|
zero ⇒ [(zero, [δ _ ⇒ zero])];
|
|
|
|
|
succ n, 1.d ⇒
|
|
|
|
|
appω (Sing ℕ n) (Sing ℕ (succ n))
|
|
|
|
|
(sing.app ℕ ℕ n (λ n ⇒ succ n)) d
|
2023-04-01 13:16:30 -04:00
|
|
|
|
};
|
|
|
|
|
|
2023-07-22 15:26:20 -04:00
|
|
|
|
def dup : ℕ → [ω.ℕ] =
|
|
|
|
|
λ n ⇒ appω (Sing ℕ n) ℕ (sing.val ℕ n) (dup! n);
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def plus : ℕ → ℕ → ℕ =
|
2023-04-01 13:16:30 -04:00
|
|
|
|
λ m n ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return ℕ of {
|
2023-04-01 13:16:30 -04:00
|
|
|
|
zero ⇒ n;
|
|
|
|
|
succ _, 1.p ⇒ succ p
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def timesω : ℕ → ω.ℕ → ℕ =
|
2023-04-01 13:16:30 -04:00
|
|
|
|
λ m n ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return ℕ of {
|
2023-04-01 13:16:30 -04:00
|
|
|
|
zero ⇒ zero;
|
|
|
|
|
succ _, 1.t ⇒ plus n t
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def times : ℕ → ℕ → ℕ =
|
|
|
|
|
λ m n ⇒ case dup n return ℕ of { [n] ⇒ timesω m n };
|
2023-04-01 13:16:30 -04:00
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def pred : ℕ → ℕ = λ n ⇒ case n return ℕ of { zero ⇒ zero; succ n ⇒ n };
|
2023-04-01 13:16:30 -04:00
|
|
|
|
|
2023-04-18 18:42:40 -04:00
|
|
|
|
def pred-succ : ω.(n : ℕ) → pred (succ n) ≡ n : ℕ =
|
|
|
|
|
λ n ⇒ δ 𝑖 ⇒ n;
|
2023-04-17 15:44:16 -04:00
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 succ-inj : (m n : ℕ) → succ m ≡ succ n : ℕ → m ≡ n : ℕ =
|
2023-04-18 18:42:40 -04:00
|
|
|
|
λ m n eq ⇒ δ 𝑖 ⇒ pred (eq @𝑖);
|
|
|
|
|
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 IsSucc : ℕ → ★ =
|
|
|
|
|
λ n ⇒ case n return ★ of { zero ⇒ False; succ _ ⇒ True };
|
2023-04-18 18:42:40 -04:00
|
|
|
|
|
|
|
|
|
def isSucc? : ω.(n : ℕ) → Dec (IsSucc n) =
|
|
|
|
|
λ n ⇒
|
|
|
|
|
caseω n return n' ⇒ Dec (IsSucc n') of {
|
|
|
|
|
zero ⇒ No (IsSucc zero) (λ v ⇒ v);
|
|
|
|
|
succ n ⇒ Yes (IsSucc (succ n)) 'true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
def zero-not-succ : 0.(m : ℕ) → Not (zero ≡ succ m : ℕ) =
|
2023-05-16 12:14:42 -04:00
|
|
|
|
λ m eq ⇒ coe (𝑖 ⇒ IsSucc (eq @𝑖)) @1 @0 'true;
|
2023-04-18 18:42:40 -04:00
|
|
|
|
|
|
|
|
|
def succ-not-zero : 0.(m : ℕ) → Not (succ m ≡ zero : ℕ) =
|
2023-05-16 12:14:42 -04:00
|
|
|
|
λ m eq ⇒ coe (𝑖 ⇒ IsSucc (eq @𝑖)) 'true;
|
2023-04-18 18:42:40 -04:00
|
|
|
|
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 not-succ-self : (m : ℕ) → Not (m ≡ succ m : ℕ) =
|
2023-04-18 18:42:40 -04:00
|
|
|
|
λ m ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return m' ⇒ Not (m' ≡ succ m' : ℕ) of {
|
2023-04-18 18:42:40 -04:00
|
|
|
|
zero ⇒ zero-not-succ 0;
|
|
|
|
|
succ n, ω.ih ⇒ λ eq ⇒ ih (succ-inj n (succ n) eq)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def eq? : DecEq ℕ =
|
|
|
|
|
λ m ⇒
|
|
|
|
|
caseω m
|
|
|
|
|
return m' ⇒ ω.(n : ℕ) → Dec (m' ≡ n : ℕ)
|
|
|
|
|
of {
|
|
|
|
|
zero ⇒ λ n ⇒
|
|
|
|
|
caseω n return n' ⇒ Dec (zero ≡ n' : ℕ) of {
|
|
|
|
|
zero ⇒ Yes (zero ≡ zero : ℕ) (δ _ ⇒ zero);
|
|
|
|
|
succ n' ⇒ No (zero ≡ succ n' : ℕ) (λ eq ⇒ zero-not-succ n' eq)
|
|
|
|
|
};
|
|
|
|
|
succ m', ω.ih ⇒ λ n ⇒
|
|
|
|
|
caseω n return n' ⇒ Dec (succ m' ≡ n' : ℕ) of {
|
|
|
|
|
zero ⇒ No (succ m' ≡ zero : ℕ) (λ eq ⇒ succ-not-zero m' eq);
|
|
|
|
|
succ n' ⇒
|
|
|
|
|
dec.elim (m' ≡ n' : ℕ) (λ _ ⇒ Dec (succ m' ≡ succ n' : ℕ))
|
|
|
|
|
(λ y ⇒ Yes (succ m' ≡ succ n' : ℕ) (δ 𝑖 ⇒ succ (y @𝑖)))
|
|
|
|
|
(λ n ⇒ No (succ m' ≡ succ n' : ℕ) (λ eq ⇒ n (succ-inj m' n' eq)))
|
|
|
|
|
(ih n')
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-23 11:33:32 -04:00
|
|
|
|
def eqb : ω.ℕ → ω.ℕ → Bool = λ m n ⇒ dec.bool (m ≡ n : ℕ) (eq? m n);
|
2023-04-18 18:42:40 -04:00
|
|
|
|
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 plus-zero : (m : ℕ) → m ≡ plus m 0 : ℕ =
|
2023-04-18 18:42:40 -04:00
|
|
|
|
λ m ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return m' ⇒ m' ≡ plus m' 0 : ℕ of {
|
2023-04-18 18:42:40 -04:00
|
|
|
|
zero ⇒ δ _ ⇒ zero;
|
|
|
|
|
succ _, ω.ih ⇒ δ 𝑖 ⇒ succ (ih @𝑖)
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 plus-succ : (m n : ℕ) → succ (plus m n) ≡ plus m (succ n) : ℕ =
|
2023-04-18 18:42:40 -04:00
|
|
|
|
λ m n ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return m' ⇒ succ (plus m' n) ≡ plus m' (succ n) : ℕ of {
|
2023-04-18 18:42:40 -04:00
|
|
|
|
zero ⇒ δ _ ⇒ succ n;
|
|
|
|
|
succ _, ω.ih ⇒ δ 𝑖 ⇒ succ (ih @𝑖)
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-18 17:12:04 -04:00
|
|
|
|
def0 plus-comm : (m n : ℕ) → plus m n ≡ plus n m : ℕ =
|
2023-04-18 18:42:40 -04:00
|
|
|
|
λ m n ⇒
|
2023-07-18 17:12:04 -04:00
|
|
|
|
case m return m' ⇒ plus m' n ≡ plus n m' : ℕ of {
|
2023-04-18 18:42:40 -04:00
|
|
|
|
zero ⇒ plus-zero n;
|
|
|
|
|
succ m', ω.ih ⇒
|
|
|
|
|
trans ℕ (succ (plus m' n)) (succ (plus n m')) (plus n (succ m'))
|
|
|
|
|
(δ 𝑖 ⇒ succ (ih @𝑖))
|
|
|
|
|
(plus-succ n m')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|