{-# OPTIONS --cubical-compatible --safe #-}
module Data.Nat.Base where
open import Algebra.Bundles.Raw using (RawMagma; RawMonoid; RawNearSemiring; RawSemiring)
open import Algebra.Definitions.RawMagma using (_∣ˡ_; _,_)
open import Data.Bool.Base using (Bool; true; false; T; not)
open import Data.Parity.Base using (Parity; 0ℙ; 1ℙ)
open import Level using (0ℓ)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; _≢_; refl; cong)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
open import Relation.Unary using (Pred)
open import Agda.Builtin.Nat public
using (zero; suc) renaming (Nat to ℕ)
pattern 2+ n = suc (suc n)
open import Agda.Builtin.Nat public
using () renaming (_==_ to _≡ᵇ_)
open import Agda.Builtin.Nat public
using () renaming (_<_ to _<ᵇ_)
infix 4 _≤ᵇ_
_≤ᵇ_ : (m n : ℕ) → Bool
zero ≤ᵇ n = true
suc m ≤ᵇ n = m <ᵇ n
infix 4 _≤_ _<_ _≥_ _>_ _≰_ _≮_ _≱_ _≯_
data _≤_ : Rel ℕ 0ℓ where
z≤n : ∀ {n} → zero ≤ n
s≤s : ∀ {m n} (m≤n : m ≤ n) → suc m ≤ suc n
_<_ : Rel ℕ 0ℓ
m < n = suc m ≤ n
pattern z<s {n} = s≤s (z≤n {n})
pattern s<s {m} {n} m<n = s≤s {m} {n} m<n
pattern sz<ss {n} = s<s (z<s {n})
s≤s⁻¹ : ∀ {m n} → suc m ≤ suc n → m ≤ n
s≤s⁻¹ (s≤s m≤n) = m≤n
s<s⁻¹ : ∀ {m n} → suc m < suc n → m < n
s<s⁻¹ (s<s m<n) = m<n
_≥_ : Rel ℕ 0ℓ
m ≥ n = n ≤ m
_>_ : Rel ℕ 0ℓ
m > n = n < m
_≰_ : Rel ℕ 0ℓ
a ≰ b = ¬ a ≤ b
_≮_ : Rel ℕ 0ℓ
a ≮ b = ¬ a < b
_≱_ : Rel ℕ 0ℓ
a ≱ b = ¬ a ≥ b
_≯_ : Rel ℕ 0ℓ
a ≯ b = ¬ a > b
record NonZero (n : ℕ) : Set where
field
nonZero : T (not (n ≡ᵇ 0))
instance
nonZero : ∀ {n} → NonZero (suc n)
nonZero = _
≢-nonZero : ∀ {n} → n ≢ 0 → NonZero n
≢-nonZero {zero} 0≢0 = contradiction refl 0≢0
≢-nonZero {suc n} n≢0 = _
>-nonZero : ∀ {n} → n > 0 → NonZero n
>-nonZero z<s = _
≢-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n ≢ 0
≢-nonZero⁻¹ (suc n) ()
>-nonZero⁻¹ : ∀ n → .{{NonZero n}} → n > 0
>-nonZero⁻¹ (suc n) = z<s
record NonTrivial (n : ℕ) : Set where
field
nonTrivial : T (1 <ᵇ n)
instance
nonTrivial : ∀ {n} → NonTrivial (2+ n)
nonTrivial = _
n>1⇒nonTrivial : ∀ {n} → n > 1 → NonTrivial n
n>1⇒nonTrivial sz<ss = _
nonTrivial⇒nonZero : ∀ n → .{{NonTrivial n}} → NonZero n
nonTrivial⇒nonZero (2+ _) = _
nonTrivial⇒n>1 : ∀ n → .{{NonTrivial n}} → n > 1
nonTrivial⇒n>1 (2+ _) = sz<ss
nonTrivial⇒≢1 : ∀ {n} → .{{NonTrivial n}} → n ≢ 1
nonTrivial⇒≢1 {{()}} refl
open import Agda.Builtin.Nat public
using (_+_; _*_) renaming (_-_ to _∸_)
+-rawMagma : RawMagma 0ℓ 0ℓ
+-rawMagma = record
{ _≈_ = _≡_
; _∙_ = _+_
}
+-0-rawMonoid : RawMonoid 0ℓ 0ℓ
+-0-rawMonoid = record
{ _≈_ = _≡_
; _∙_ = _+_
; ε = 0
}
*-rawMagma : RawMagma 0ℓ 0ℓ
*-rawMagma = record
{ _≈_ = _≡_
; _∙_ = _*_
}
*-1-rawMonoid : RawMonoid 0ℓ 0ℓ
*-1-rawMonoid = record
{ _≈_ = _≡_
; _∙_ = _*_
; ε = 1
}
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ
+-*-rawNearSemiring = record
{ Carrier = _
; _≈_ = _≡_
; _+_ = _+_
; _*_ = _*_
; 0# = 0
}
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ
+-*-rawSemiring = record
{ Carrier = _
; _≈_ = _≡_
; _+_ = _+_
; _*_ = _*_
; 0# = 0
; 1# = 1
}
open import Agda.Builtin.Nat
using (div-helper; mod-helper)
pred : ℕ → ℕ
pred n = n ∸ 1
infix 8 _!
infixl 7 _⊓_ _⊓′_ _/_ _%_
infixl 6 _+⋎_ _⊔_ _⊔′_
_+⋎_ : ℕ → ℕ → ℕ
zero +⋎ n = n
suc m +⋎ n = suc (n +⋎ m)
_⊔_ : ℕ → ℕ → ℕ
zero ⊔ n = n
suc m ⊔ zero = suc m
suc m ⊔ suc n = suc (m ⊔ n)
_⊔′_ : ℕ → ℕ → ℕ
m ⊔′ n with m <ᵇ n
... | false = m
... | true = n
_⊓_ : ℕ → ℕ → ℕ
zero ⊓ n = zero
suc m ⊓ zero = zero
suc m ⊓ suc n = suc (m ⊓ n)
_⊓′_ : ℕ → ℕ → ℕ
m ⊓′ n with m <ᵇ n
... | false = n
... | true = m
parity : ℕ → Parity
parity 0 = 0ℙ
parity 1 = 1ℙ
parity (suc (suc n)) = parity n
⌊_/2⌋ : ℕ → ℕ
⌊ 0 /2⌋ = 0
⌊ 1 /2⌋ = 0
⌊ suc (suc n) /2⌋ = suc ⌊ n /2⌋
⌈_/2⌉ : ℕ → ℕ
⌈ n /2⌉ = ⌊ suc n /2⌋
infixr 8 _^_
_^_ : ℕ → ℕ → ℕ
x ^ zero = 1
x ^ suc n = x * x ^ n
∣_-_∣ : ℕ → ℕ → ℕ
∣ zero - y ∣ = y
∣ x - zero ∣ = x
∣ suc x - suc y ∣ = ∣ x - y ∣
∣_-_∣′ : ℕ → ℕ → ℕ
∣ x - y ∣′ with x <ᵇ y
... | false = x ∸ y
... | true = y ∸ x
_/_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ
m / (suc n) = div-helper 0 n m n
_%_ : (dividend divisor : ℕ) .{{_ : NonZero divisor}} → ℕ
m % (suc n) = mod-helper 0 n m n
_! : ℕ → ℕ
zero ! = 1
suc n ! = suc n * n !
infix 4 _≤′_ _<′_ _≥′_ _>′_
data _≤′_ (m : ℕ) : ℕ → Set where
≤′-refl : m ≤′ m
≤′-step : ∀ {n} (m≤′n : m ≤′ n) → m ≤′ suc n
_<′_ : Rel ℕ 0ℓ
m <′ n = suc m ≤′ n
pattern <′-base = ≤′-refl
pattern <′-step {n} m<′n = ≤′-step {n} m<′n
_≥′_ : Rel ℕ 0ℓ
m ≥′ n = n ≤′ m
_>′_ : Rel ℕ 0ℓ
m >′ n = n <′ m
infix 4 _≤″_ _<″_ _≥″_ _>″_
_≤″_ : (m n : ℕ) → Set
_≤″_ = _∣ˡ_ +-rawMagma
_<″_ : Rel ℕ 0ℓ
m <″ n = suc m ≤″ n
_≥″_ : Rel ℕ 0ℓ
m ≥″ n = n ≤″ m
_>″_ : Rel ℕ 0ℓ
m >″ n = n <″ m
s<″s⁻¹ : ∀ {m n} → suc m <″ suc n → m <″ n
s<″s⁻¹ (k , eq) = k , cong pred eq
data _≤‴_ : ℕ → ℕ → Set where
≤‴-refl : ∀{m} → m ≤‴ m
≤‴-step : ∀{m n} → suc m ≤‴ n → m ≤‴ n
infix 4 _≤‴_ _<‴_ _≥‴_ _>‴_
_<‴_ : Rel ℕ 0ℓ
m <‴ n = suc m ≤‴ n
_≥‴_ : Rel ℕ 0ℓ
m ≥‴ n = n ≤‴ m
_>‴_ : Rel ℕ 0ℓ
m >‴ n = n <‴ m
data Ordering : Rel ℕ 0ℓ where
less : ∀ m k → Ordering m (suc (m + k))
equal : ∀ m → Ordering m m
greater : ∀ m k → Ordering (suc (m + k)) m
compare : ∀ m n → Ordering m n
compare zero zero = equal zero
compare (suc m) zero = greater zero m
compare zero (suc n) = less zero n
compare (suc m) (suc n) with compare m n
... | less m k = less (suc m) k
... | equal m = equal (suc m)
... | greater n k = greater (suc n) k
pattern less-than-or-equal {k} eq = k , eq
{-# WARNING_ON_USAGE less-than-or-equal
"Warning: less-than-or-equal was deprecated in v2.1. Please match directly on proofs of ≤″ using constructor Algebra.Definitions.RawMagma._∣ˡ_._,_ instead. "
#-}
pattern ≤″-offset k = k , refl
{-# WARNING_ON_USAGE ≤″-offset
"Warning: ≤″-offset was deprecated in v2.1. Please match directly on proofs of ≤″ using pattern (_, refl) from Algebra.Definitions.RawMagma._∣ˡ_ instead. "
#-}
pattern <″-offset k = k , refl
{-# WARNING_ON_USAGE <″-offset
"Warning: <″-offset was deprecated in v2.1. Please match directly on proofs of ≤″ using pattern (_, refl) from Algebra.Definitions.RawMagma._∣ˡ_ instead. "
#-}
s≤″s⁻¹ : ∀ {m n} → suc m ≤″ suc n → m ≤″ n
s≤″s⁻¹ (k , eq) = k , cong pred eq
{-# WARNING_ON_USAGE s≤″s⁻¹
"Warning: s≤″s⁻¹ was deprecated in v2.1. Please match directly on proofs of ≤″ using constructor Algebra.Definitions.RawMagma._∣ˡ_._,_ instead. "
#-}