Martin Escardo 1st May 2020.
This is ported from the Midlands Graduate School 2019 lecture notes
https://www.cs.bham.ac.uk/~mhe/HoTT-UF.in-Agda-Lecture-Notes/HoTT-UF-Agda.html
https://github.com/martinescardo/HoTT-UF.Agda-Lecture-Notes
\begin{code}
{-# OPTIONS --safe --without-K #-}
module MGS.MLTT where
open import MLTT.Universes public
open import MLTT.Unit-Type renaming (๐ to ๐') public
๐ : ๐คโ ฬ
๐ = ๐' {๐คโ}
๐-induction : (A : ๐ โ ๐คโฬ ) โ A โ โ (x : ๐) โ A x
๐-induction A a โ = a
๐-recursion : (B : ๐คโฬ ) โ B โ (๐ โ B)
๐-recursion B b x = ๐-induction (ฮป _ โ B) b x
!๐' : (X : ๐คโฬ ) โ X โ ๐
!๐' X x = โ
!๐ : {X : ๐คโฬ } โ X โ ๐
!๐ x = โ
open import MLTT.Empty-Type renaming (๐ to ๐') public
๐ : ๐คโ ฬ
๐ = ๐' {๐คโ}
๐-induction : (A : ๐ โ ๐ค ฬ ) โ (x : ๐) โ A x
๐-induction A ()
๐-recursion : (A : ๐ค ฬ ) โ ๐ โ A
๐-recursion A a = ๐-induction (ฮป _ โ A) a
!๐ : (A : ๐ค ฬ ) โ ๐ โ A
!๐ = ๐-recursion
is-empty : ๐ค ฬ โ ๐ค ฬ
is-empty X = X โ ๐
ยฌ : ๐ค ฬ โ ๐ค ฬ
ยฌ X = X โ ๐
open import MLTT.Natural-Numbers-Type public
โ-induction : (A : โ โ ๐ค ฬ )
โ A 0
โ ((n : โ) โ A n โ A (succ n))
โ (n : โ) โ A n
โ-induction A aโ f = h
where
h : (n : โ) โ A n
h 0 = aโ
h (succ n) = f n (h n)
โ-recursion : (X : ๐ค ฬ )
โ X
โ (โ โ X โ X)
โ โ โ X
โ-recursion X = โ-induction (ฮป _ โ X)
โ-iteration : (X : ๐ค ฬ )
โ X
โ (X โ X)
โ โ โ X
โ-iteration X x f = โ-recursion X x (ฮป _ x โ f x)
module Arithmetic where
_+_ _ร_ : โ โ โ โ โ
x + 0 = x
x + succ y = succ (x + y)
x ร 0 = 0
x ร succ y = x + x ร y
infixl 20 _+_
infixl 21 _ร_
module Arithmetic' where
_+_ _ร_ : โ โ โ โ โ
x + y = h y
where
h : โ โ โ
h = โ-iteration โ x succ
x ร y = h y
where
h : โ โ โ
h = โ-iteration โ 0 (x +_)
infixl 20 _+_
infixl 21 _ร_
module โ-order where
_โค_ _โฅ_ : โ โ โ โ ๐คโ ฬ
0 โค y = ๐
succ x โค 0 = ๐
succ x โค succ y = x โค y
x โฅ y = y โค x
infix 10 _โค_
infix 10 _โฅ_
open import MLTT.Plus-Type renaming (_+_ to infixr 20 _+_) public
+-induction : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } (A : X + Y โ ๐ฆ ฬ )
โ ((x : X) โ A (inl x))
โ ((y : Y) โ A (inr y))
โ (z : X + Y) โ A z
+-induction A f g (inl x) = f x
+-induction A f g (inr y) = g y
+-recursion : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } {A : ๐ฆ ฬ } โ (X โ A) โ (Y โ A) โ X + Y โ A
+-recursion {๐ค} {๐ฅ} {๐ฆ} {X} {Y} {A} = +-induction (ฮป _ โ A)
๐ : ๐คโ ฬ
๐ = ๐ + ๐
pattern โ = inl โ
pattern โ = inr โ
๐-induction : (A : ๐ โ ๐ค ฬ ) โ A โ โ A โ โ (n : ๐) โ A n
๐-induction A aโ aโ โ = aโ
๐-induction A aโ aโ โ = aโ
๐-induction' : (A : ๐ โ ๐ค ฬ ) โ A โ โ A โ โ (n : ๐) โ A n
๐-induction' A aโ aโ = +-induction A
(๐-induction (ฮป (x : ๐) โ A (inl x)) aโ)
(๐-induction (ฮป (y : ๐) โ A (inr y)) aโ)
open import MLTT.Sigma-Type renaming (_,_ to infixr 50 _,_) public
prโ : {X : ๐ค ฬ } {Y : X โ ๐ฅ ฬ } โ ฮฃ Y โ X
prโ (x , y) = x
prโ : {X : ๐ค ฬ } {Y : X โ ๐ฅ ฬ } โ (z : ฮฃ Y) โ Y (prโ z)
prโ (x , y) = y
-ฮฃ : {๐ค ๐ฅ : Universe} (X : ๐ค ฬ ) (Y : X โ ๐ฅ ฬ ) โ ๐ค โ ๐ฅ ฬ
-ฮฃ X Y = ฮฃ Y
syntax -ฮฃ X (ฮป x โ y) = ฮฃ x ๊ X , y
ฮฃ-induction : {X : ๐ค ฬ } {Y : X โ ๐ฅ ฬ } {A : ฮฃ Y โ ๐ฆ ฬ }
โ ((x : X) (y : Y x) โ A (x , y))
โ ((x , y) : ฮฃ Y) โ A (x , y)
ฮฃ-induction g (x , y) = g x y
curry : {X : ๐ค ฬ } {Y : X โ ๐ฅ ฬ } {A : ฮฃ Y โ ๐ฆ ฬ }
โ (((x , y) : ฮฃ Y) โ A (x , y))
โ ((x : X) (y : Y x) โ A (x , y))
curry f x y = f (x , y)
_ร_ : ๐ค ฬ โ ๐ฅ ฬ โ ๐ค โ ๐ฅ ฬ
X ร Y = ฮฃ x ๊ X , Y
ฮ : {X : ๐ค ฬ } (A : X โ ๐ฅ ฬ ) โ ๐ค โ ๐ฅ ฬ
ฮ {๐ค} {๐ฅ} {X} A = (x : X) โ A x
-ฮ : {๐ค ๐ฅ : Universe} (X : ๐ค ฬ ) (Y : X โ ๐ฅ ฬ ) โ ๐ค โ ๐ฅ ฬ
-ฮ X Y = ฮ Y
syntax -ฮ A (ฮป x โ b) = ฮ x ๊ A , b
id : {X : ๐ค ฬ } โ X โ X
id x = x
๐๐ : (X : ๐ค ฬ ) โ X โ X
๐๐ X = id
_โ_ : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } {Z : Y โ ๐ฆ ฬ }
โ ((y : Y) โ Z y)
โ (f : X โ Y)
โ (x : X) โ Z (f x)
g โ f = ฮป x โ g (f x)
domain : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } โ (X โ Y) โ ๐ค ฬ
domain {๐ค} {๐ฅ} {X} {Y} f = X
codomain : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } โ (X โ Y) โ ๐ฅ ฬ
codomain {๐ค} {๐ฅ} {X} {Y} f = Y
type-of : {X : ๐ค ฬ } โ X โ ๐ค ฬ
type-of {๐ค} {X} x = X
open import MLTT.Identity-Type renaming (_๏ผ_ to infix 0 _๏ผ_ ; refl to ๐ปโฏ๐ฏ๐ต) public
pattern refl x = ๐ปโฏ๐ฏ๐ต {x = x}
Id : (X : ๐ค ฬ ) โ X โ X โ ๐ค ฬ
Id _ x y = x ๏ผ y
๐ : (X : ๐ค ฬ ) (A : (x y : X) โ x ๏ผ y โ ๐ฅ ฬ )
โ ((x : X) โ A x x (refl x))
โ (x y : X) (p : x ๏ผ y) โ A x y p
๐ X A f x x (refl x) = f x
โ : {X : ๐ค ฬ } (x : X) (B : (y : X) โ x ๏ผ y โ ๐ฅ ฬ )
โ B x (refl x)
โ (y : X) (p : x ๏ผ y) โ B y p
โ x B b x (refl x) = b
๐' : (X : ๐ค ฬ ) (A : (x y : X) โ x ๏ผ y โ ๐ฅ ฬ )
โ ((x : X) โ A x x (refl x))
โ (x y : X) (p : x ๏ผ y) โ A x y p
๐' X A f x = โ x (A x) (f x)
๐s-agreement : (X : ๐ค ฬ ) (A : (x y : X) โ x ๏ผ y โ ๐ฅ ฬ )
(f : (x : X) โ A x x (refl x)) (x y : X) (p : x ๏ผ y)
โ ๐ X A f x y p ๏ผ ๐' X A f x y p
๐s-agreement X A f x x (refl x) = refl (f x)
transport : {X : ๐ค ฬ } (A : X โ ๐ฅ ฬ ) {x y : X}
โ x ๏ผ y โ A x โ A y
transport A (refl x) = ๐๐ (A x)
transport๐ : {X : ๐ค ฬ } (A : X โ ๐ฅ ฬ ) {x y : X}
โ x ๏ผ y โ A x โ A y
transport๐ {๐ค} {๐ฅ} {X} A {x} {y} = ๐ X (ฮป x y _ โ A x โ A y) (ฮป x โ ๐๐ (A x)) x y
nondep-โ : {X : ๐ค ฬ } (x : X) (A : X โ ๐ฅ ฬ )
โ A x โ (y : X) โ x ๏ผ y โ A y
nondep-โ x A = โ x (ฮป y _ โ A y)
transportโ : {X : ๐ค ฬ } (A : X โ ๐ฅ ฬ ) {x y : X}
โ x ๏ผ y โ A x โ A y
transportโ A {x} {y} p a = nondep-โ x A a y p
transports-agreement : {X : ๐ค ฬ } (A : X โ ๐ฅ ฬ ) {x y : X} (p : x ๏ผ y)
โ (transportโ A p ๏ผ transport A p)
ร (transport๐ A p ๏ผ transport A p)
transports-agreement A (refl x) = refl (transport A (refl x)) ,
refl (transport A (refl x))
lhs : {X : ๐ค ฬ } {x y : X} โ x ๏ผ y โ X
lhs {๐ค} {X} {x} {y} p = x
rhs : {X : ๐ค ฬ } {x y : X} โ x ๏ผ y โ X
rhs {๐ค} {X} {x} {y} p = y
_โ_ : {X : ๐ค ฬ } {x y z : X} โ x ๏ผ y โ y ๏ผ z โ x ๏ผ z
p โ q = transport (lhs p ๏ผ_) q p
_๏ผโจ_โฉ_ : {X : ๐ค ฬ } (x : X) {y z : X} โ x ๏ผ y โ y ๏ผ z โ x ๏ผ z
x ๏ผโจ p โฉ q = p โ q
_โ : {X : ๐ค ฬ } (x : X) โ x ๏ผ x
x โ = refl x
_โปยน : {X : ๐ค ฬ } โ {x y : X} โ x ๏ผ y โ y ๏ผ x
p โปยน = transport (_๏ผ lhs p) p (refl (lhs p))
_โ'_ : {X : ๐ค ฬ } {x y z : X} โ x ๏ผ y โ y ๏ผ z โ x ๏ผ z
p โ' q = transport (_๏ผ rhs q) (p โปยน) q
โagreement : {X : ๐ค ฬ } {x y z : X} (p : x ๏ผ y) (q : y ๏ผ z)
โ p โ' q ๏ผ p โ q
โagreement (refl x) (refl x) = refl (refl x)
rdnel : {X : ๐ค ฬ } {x y : X} (p : x ๏ผ y)
โ p โ refl y ๏ผ p
rdnel p = refl p
rdner : {X : ๐ค ฬ } {y z : X} (q : y ๏ผ z)
โ refl y โ' q ๏ผ q
rdner q = refl q
ap : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } (f : X โ Y) {x x' : X} โ x ๏ผ x' โ f x ๏ผ f x'
ap f {x} {x'} p = transport (ฮป - โ f x ๏ผ f -) p (refl (f x))
_โผ_ : {X : ๐ค ฬ } {A : X โ ๐ฅ ฬ } โ ฮ A โ ฮ A โ ๐ค โ ๐ฅ ฬ
f โผ g = โ x โ f x ๏ผ g x
ยฌยฌ ยฌยฌยฌ : ๐ค ฬ โ ๐ค ฬ
ยฌยฌ A = ยฌ (ยฌ A)
ยฌยฌยฌ A = ยฌ (ยฌยฌ A)
dni : (A : ๐ค ฬ ) โ A โ ยฌยฌ A
dni A a u = u a
contrapositive : {A : ๐ค ฬ } {B : ๐ฅ ฬ } โ (A โ B) โ (ยฌ B โ ยฌ A)
contrapositive f v a = v (f a)
tno : (A : ๐ค ฬ ) โ ยฌยฌยฌ A โ ยฌ A
tno A = contrapositive (dni A)
_โ_ : ๐ค ฬ โ ๐ฅ ฬ โ ๐ค โ ๐ฅ ฬ
X โ Y = (X โ Y) ร (Y โ X)
lr-implication : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } โ (X โ Y) โ (X โ Y)
lr-implication = prโ
rl-implication : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } โ (X โ Y) โ (Y โ X)
rl-implication = prโ
absurdityยณ-is-absurdity : {A : ๐ค ฬ } โ ยฌยฌยฌ A โ ยฌ A
absurdityยณ-is-absurdity {๐ค} {A} = firstly , secondly
where
firstly : ยฌยฌยฌ A โ ยฌ A
firstly = contrapositive (dni A)
secondly : ยฌ A โ ยฌยฌยฌ A
secondly = dni (ยฌ A)
_โ _ : {X : ๐ค ฬ } โ X โ X โ ๐ค ฬ
x โ y = ยฌ (x ๏ผ y)
โ -sym : {X : ๐ค ฬ } {x y : X} โ x โ y โ y โ x
โ -sym {๐ค} {X} {x} {y} u = ฮป (p : y ๏ผ x) โ u (p โปยน)
IdโFun : {X Y : ๐ค ฬ } โ X ๏ผ Y โ X โ Y
IdโFun {๐ค} = transport (๐๐ (๐ค ฬ ))
IdโFun' : {X Y : ๐ค ฬ } โ X ๏ผ Y โ X โ Y
IdโFun' (refl X) = ๐๐ X
IdโFuns-agree : {X Y : ๐ค ฬ } (p : X ๏ผ Y)
โ IdโFun p ๏ผ IdโFun' p
IdโFuns-agree (refl X) = refl (๐๐ X)
๐-is-not-๐ : ๐ โ ๐
๐-is-not-๐ p = IdโFun p โ
โ-is-not-โ : โ โ โ
โ-is-not-โ p = ๐-is-not-๐ q
where
f : ๐ โ ๐คโ ฬ
f โ = ๐
f โ = ๐
q : ๐ ๏ผ ๐
q = ap f p
decidable : ๐ค ฬ โ ๐ค ฬ
decidable A = A + ยฌ A
has-decidable-equality : (X : ๐ค ฬ ) โ ๐ค ฬ
has-decidable-equality X = (x y : X) โ decidable (x ๏ผ y)
๐-has-decidable-equality : has-decidable-equality ๐
๐-has-decidable-equality โ โ = inl (refl โ)
๐-has-decidable-equality โ โ = inr (โ -sym โ-is-not-โ)
๐-has-decidable-equality โ โ = inr โ-is-not-โ
๐-has-decidable-equality โ โ = inl (refl โ)
not-zero-is-one : (n : ๐) โ n โ โ โ n ๏ผ โ
not-zero-is-one โ f = !๐ (โ ๏ผ โ) (f (refl โ))
not-zero-is-one โ f = refl โ
inl-inr-disjoint-images : {X : ๐ค ฬ } {Y : ๐ฅ ฬ } {x : X} {y : Y} โ inl x โ inr y
inl-inr-disjoint-images {๐ค} {๐ฅ} {X} {Y} p = ๐-is-not-๐ q
where
f : X + Y โ ๐คโ ฬ
f (inl x) = ๐
f (inr y) = ๐
q : ๐ ๏ผ ๐
q = ap f p
right-fails-gives-left-holds : {P : ๐ค ฬ } {Q : ๐ฅ ฬ } โ P + Q โ ยฌ Q โ P
right-fails-gives-left-holds (inl p) u = p
right-fails-gives-left-holds (inr q) u = !๐ _ (u q)
module twin-primes where
open Arithmetic renaming (_ร_ to _*_ ; _+_ to _โ_)
open โ-order
is-prime : โ โ ๐คโ ฬ
is-prime n = (n โฅ 2) ร ((x y : โ) โ x * y ๏ผ n โ (x ๏ผ 1) + (x ๏ผ n))
twin-prime-conjecture : ๐คโ ฬ
twin-prime-conjecture = (n : โ) โ ฮฃ p ๊ โ , (p โฅ n)
ร is-prime p
ร is-prime (p โ 2)
positive-not-zero : (x : โ) โ succ x โ 0
positive-not-zero x p = ๐-is-not-๐ (g p)
where
f : โ โ ๐คโ ฬ
f 0 = ๐
f (succ x) = ๐
g : succ x ๏ผ 0 โ ๐ ๏ผ ๐
g = ap f
pred : โ โ โ
pred 0 = 0
pred (succ n) = n
succ-lc : {x y : โ} โ succ x ๏ผ succ y โ x ๏ผ y
succ-lc = ap pred
โ-has-decidable-equality : has-decidable-equality โ
โ-has-decidable-equality 0 0 = inl (refl 0)
โ-has-decidable-equality 0 (succ y) = inr (โ -sym (positive-not-zero y))
โ-has-decidable-equality (succ x) 0 = inr (positive-not-zero x)
โ-has-decidable-equality (succ x) (succ y) = f (โ-has-decidable-equality x y)
where
f : decidable (x ๏ผ y) โ decidable (succ x ๏ผ succ y)
f (inl p) = inl (ap succ p)
f (inr k) = inr (ฮป (s : succ x ๏ผ succ y) โ k (succ-lc s))
module basic-arithmetic-and-order where
open โ-order public
open Arithmetic renaming (_+_ to _โ_) hiding (_ร_)
+-assoc : (x y z : โ) โ (x โ y) โ z ๏ผ x โ (y โ z)
+-assoc x y zero = (x โ y) โ 0 ๏ผโจ refl _ โฉ
x โ (y โ 0) โ
+-assoc x y (succ z) = (x โ y) โ succ z ๏ผโจ refl _ โฉ
succ ((x โ y) โ z) ๏ผโจ ap succ IH โฉ
succ (x โ (y โ z)) ๏ผโจ refl _ โฉ
x โ (y โ succ z) โ
where
IH : (x โ y) โ z ๏ผ x โ (y โ z)
IH = +-assoc x y z
+-assoc' : (x y z : โ) โ (x โ y) โ z ๏ผ x โ (y โ z)
+-assoc' x y zero = refl _
+-assoc' x y (succ z) = ap succ (+-assoc' x y z)
+-base-on-first : (x : โ) โ 0 โ x ๏ผ x
+-base-on-first 0 = refl 0
+-base-on-first (succ x) = 0 โ succ x ๏ผโจ refl _ โฉ
succ (0 โ x) ๏ผโจ ap succ IH โฉ
succ x โ
where
IH : 0 โ x ๏ผ x
IH = +-base-on-first x
+-step-on-first : (x y : โ) โ succ x โ y ๏ผ succ (x โ y)
+-step-on-first x zero = refl (succ x)
+-step-on-first x (succ y) = succ x โ succ y ๏ผโจ refl _ โฉ
succ (succ x โ y) ๏ผโจ ap succ IH โฉ
succ (x โ succ y) โ
where
IH : succ x โ y ๏ผ succ (x โ y)
IH = +-step-on-first x y
+-comm : (x y : โ) โ x โ y ๏ผ y โ x
+-comm 0 y = 0 โ y ๏ผโจ +-base-on-first y โฉ
y ๏ผโจ refl _ โฉ
y โ 0 โ
+-comm (succ x) y = succ x โ y ๏ผโจ +-step-on-first x y โฉ
succ(x โ y) ๏ผโจ ap succ IH โฉ
succ(y โ x) ๏ผโจ refl _ โฉ
y โ succ x โ
where
IH : x โ y ๏ผ y โ x
IH = +-comm x y
+-lc : (x y z : โ) โ x โ y ๏ผ x โ z โ y ๏ผ z
+-lc 0 y z p = y ๏ผโจ (+-base-on-first y)โปยน โฉ
0 โ y ๏ผโจ p โฉ
0 โ z ๏ผโจ +-base-on-first z โฉ
z โ
+-lc (succ x) y z p = IH
where
q = succ (x โ y) ๏ผโจ (+-step-on-first x y)โปยน โฉ
succ x โ y ๏ผโจ p โฉ
succ x โ z ๏ผโจ +-step-on-first x z โฉ
succ (x โ z) โ
IH : y ๏ผ z
IH = +-lc x y z (succ-lc q)
_โผ_ : โ โ โ โ ๐คโ ฬ
x โผ y = ฮฃ z ๊ โ , x โ z ๏ผ y
โค-gives-โผ : (x y : โ) โ x โค y โ x โผ y
โค-gives-โผ 0 0 l = 0 , refl 0
โค-gives-โผ 0 (succ y) l = succ y , +-base-on-first (succ y)
โค-gives-โผ (succ x) 0 l = !๐ (succ x โผ zero) l
โค-gives-โผ (succ x) (succ y) l = ฮณ
where
IH : x โผ y
IH = โค-gives-โผ x y l
z : โ
z = prโ IH
p : x โ z ๏ผ y
p = prโ IH
ฮณ : succ x โผ succ y
ฮณ = z , (succ x โ z ๏ผโจ +-step-on-first x z โฉ
succ (x โ z) ๏ผโจ ap succ p โฉ
succ y โ)
โผ-gives-โค : (x y : โ) โ x โผ y โ x โค y
โผ-gives-โค 0 0 (z , p) = โ
โผ-gives-โค 0 (succ y) (z , p) = โ
โผ-gives-โค (succ x) 0 (z , p) = positive-not-zero (x โ z) q
where
q = succ (x โ z) ๏ผโจ (+-step-on-first x z)โปยน โฉ
succ x โ z ๏ผโจ p โฉ
zero โ
โผ-gives-โค (succ x) (succ y) (z , p) = IH
where
q = succ (x โ z) ๏ผโจ (+-step-on-first x z)โปยน โฉ
succ x โ z ๏ผโจ p โฉ
succ y โ
IH : x โค y
IH = โผ-gives-โค x y (z , succ-lc q)
โค-refl : (n : โ) โ n โค n
โค-refl zero = โ
โค-refl (succ n) = โค-refl n
โค-trans : (l m n : โ) โ l โค m โ m โค n โ l โค n
โค-trans zero m n p q = โ
โค-trans (succ l) zero n p q = !๐ (succ l โค n) p
โค-trans (succ l) (succ m) zero p q = q
โค-trans (succ l) (succ m) (succ n) p q = โค-trans l m n p q
โค-anti : (m n : โ) โ m โค n โ n โค m โ m ๏ผ n
โค-anti zero zero p q = refl zero
โค-anti zero (succ n) p q = !๐ (zero ๏ผ succ n) q
โค-anti (succ m) zero p q = !๐ (succ m ๏ผ zero) p
โค-anti (succ m) (succ n) p q = ap succ (โค-anti m n p q)
โค-succ : (n : โ) โ n โค succ n
โค-succ zero = โ
โค-succ (succ n) = โค-succ n
zero-minimal : (n : โ) โ zero โค n
zero-minimal n = โ
unique-minimal : (n : โ) โ n โค zero โ n ๏ผ zero
unique-minimal zero p = refl zero
unique-minimal (succ n) p = !๐ (succ n ๏ผ zero) p
โค-split : (m n : โ) โ m โค succ n โ (m โค n) + (m ๏ผ succ n)
โค-split zero n l = inl l
โค-split (succ m) zero l = inr (ap succ (unique-minimal m l))
โค-split (succ m) (succ n) l = +-recursion inl (inr โ ap succ) (โค-split m n l)
_<_ : โ โ โ โ ๐คโ ฬ
x < y = succ x โค y
infix 10 _<_
not-<-gives-โฅ : (m n : โ) โ ยฌ (n < m) โ m โค n
not-<-gives-โฅ zero n u = zero-minimal n
not-<-gives-โฅ (succ m) zero = dni (zero < succ m) (zero-minimal m)
not-<-gives-โฅ (succ m) (succ n) = not-<-gives-โฅ m n
bounded-โ-next : (A : โ โ ๐ค ฬ ) (k : โ)
โ A k
โ ((n : โ) โ n < k โ A n)
โ (n : โ) โ n < succ k โ A n
bounded-โ-next A k a ฯ n l = +-recursion f g s
where
s : (n < k) + (succ n ๏ผ succ k)
s = โค-split (succ n) k l
f : n < k โ A n
f = ฯ n
g : succ n ๏ผ succ k โ A n
g p = transport A ((succ-lc p)โปยน) a
root : (โ โ โ) โ ๐คโ ฬ
root f = ฮฃ n ๊ โ , f n ๏ผ 0
_has-no-root<_ : (โ โ โ) โ โ โ ๐คโ ฬ
f has-no-root< k = (n : โ) โ n < k โ f n โ 0
is-minimal-root : (โ โ โ) โ โ โ ๐คโ ฬ
is-minimal-root f m = (f m ๏ผ 0) ร (f has-no-root< m)
at-most-one-minimal-root : (f : โ โ โ) (m n : โ)
โ is-minimal-root f m โ is-minimal-root f n โ m ๏ผ n
at-most-one-minimal-root f m n (p , ฯ) (q , ฯ) = c m n a b
where
a : ยฌ (m < n)
a u = ฯ m u p
b : ยฌ (n < m)
b v = ฯ n v q
c : (m n : โ) โ ยฌ (m < n) โ ยฌ (n < m) โ m ๏ผ n
c m n u v = โค-anti m n (not-<-gives-โฅ m n v) (not-<-gives-โฅ n m u)
minimal-root : (โ โ โ) โ ๐คโ ฬ
minimal-root f = ฮฃ m ๊ โ , is-minimal-root f m
minimal-root-is-root : โ f โ minimal-root f โ root f
minimal-root-is-root f (m , p , _) = m , p
bounded-โ-search : โ k f โ (minimal-root f) + (f has-no-root< k)
bounded-โ-search zero f = inr (ฮป n โ !๐ (f n โ 0))
bounded-โ-search (succ k) f = +-recursion ฯ ฮณ (bounded-โ-search k f)
where
A : โ โ (โ โ โ) โ ๐คโ ฬ
A k f = (minimal-root f) + (f has-no-root< k)
ฯ : minimal-root f โ A (succ k) f
ฯ = inl
ฮณ : f has-no-root< k โ A (succ k) f
ฮณ u = +-recursion ฮณโ ฮณโ (โ-has-decidable-equality (f k) 0)
where
ฮณโ : f k ๏ผ 0 โ A (succ k) f
ฮณโ p = inl (k , p , u)
ฮณโ : f k โ 0 โ A (succ k) f
ฮณโ v = inr (bounded-โ-next (ฮป n โ f n โ 0) k v u)
root-gives-minimal-root : โ f โ root f โ minimal-root f
root-gives-minimal-root f (n , p) = ฮณ
where
g : ยฌ (f has-no-root< (succ n))
g ฯ = ฯ n (โค-refl n) p
ฮณ : minimal-root f
ฮณ = right-fails-gives-left-holds (bounded-โ-search (succ n) f) g
infixr 30 _ร_
infix 0 _โผ_
infixl 70 _โ_
infix 0 Id
infix 10 _โ_
infixl 30 _โ_
infixr 0 _๏ผโจ_โฉ_
infix 1 _โ
infix 40 _โปยน
infixr -1 -ฮฃ
infixr -1 -ฮ
\end{code}