module Data.Int.Order where

Ordering integersπŸ”—

The usual partial order on the integers relies on the observation that the number line looks like two copies of the natural numbers, smashed end-to-end at the number zero. This is precisely the definition of the order we use:

data _≀_ : Int β†’ Int β†’ Type where
  neg≀neg : βˆ€ {x y} β†’ y Nat.≀ x β†’ negsuc x ≀ negsuc y
  pos≀pos : βˆ€ {x y} β†’ x Nat.≀ y β†’ pos x    ≀ pos y
  neg≀pos : βˆ€ {x y}             β†’ negsuc x ≀ pos y

Note the key properties: the ordering between negative numbers is reversed, and every negative number is smaller than every positive number. This means that decomposes, as an order, into an ordinal sum

Basic propertiesπŸ”—

Proving that this is actually a partial order is a straightforward combination of case-bashing and appealing to the analogous properties for the ordering on natural numbers.

Β¬pos≀neg : βˆ€ {x y} β†’ pos x ≀ negsuc y β†’ βŠ₯
Β¬pos≀neg ()

≀-is-prop : βˆ€ {x y} β†’ is-prop (x ≀ y)
≀-is-prop (neg≀neg p) (neg≀neg q) = ap neg≀neg (Nat.≀-is-prop p q)
≀-is-prop (pos≀pos p) (pos≀pos q) = ap pos≀pos (Nat.≀-is-prop p q)
≀-is-prop neg≀pos neg≀pos = refl

≀-refl : βˆ€ {x} β†’ x ≀ x
≀-refl {x = pos x}    = pos≀pos Nat.≀-refl
≀-refl {x = negsuc x} = neg≀neg Nat.≀-refl

≀-refl' : βˆ€ {x y} β†’ x ≑ y β†’ x ≀ y
≀-refl' {pos x} {pos y} p = pos≀pos (Nat.≀-refl' (pos-injective p))
≀-refl' {pos x} {negsuc y} p = absurd (posβ‰ negsuc p)
≀-refl' {negsuc x} {pos y} p = absurd (negsucβ‰ pos p)
≀-refl' {negsuc x} {negsuc y} p = neg≀neg (Nat.≀-refl' (negsuc-injective (sym p)))

≀-trans : βˆ€ {x y z} β†’ x ≀ y β†’ y ≀ z β†’ x ≀ z
≀-trans (neg≀neg p) (neg≀neg q) = neg≀neg (Nat.≀-trans q p)
≀-trans (neg≀neg p) neg≀pos     = neg≀pos
≀-trans (pos≀pos p) (pos≀pos q) = pos≀pos (Nat.≀-trans p q)
≀-trans neg≀pos (pos≀pos x)     = neg≀pos

≀-antisym : βˆ€ {x y} β†’ x ≀ y β†’ y ≀ x β†’ x ≑ y
≀-antisym (neg≀neg p) (neg≀neg q) = ap negsuc (Nat.≀-antisym q p)
≀-antisym (pos≀pos p) (pos≀pos q) = ap pos (Nat.≀-antisym p q)

unpos≀pos : βˆ€ {x y} β†’ pos x ≀ pos y β†’ x Nat.≀ y
unpos≀pos (pos≀pos p) = p

unneg≀neg : βˆ€ {x y} β†’ negsuc x ≀ negsuc y β†’ y Nat.≀ x
unneg≀neg (neg≀neg p) = p

apos≀apos : βˆ€ {x y} β†’ x Nat.≀ y β†’ assign pos x ≀ assign pos y
apos≀apos {x} {y} p = ≀-trans (≀-refl' (assign-pos x)) (≀-trans (pos≀pos p) (≀-refl' (sym (assign-pos y))))

unapos≀apos : βˆ€ {x y} β†’ assign pos x ≀ assign pos y β†’ x Nat.≀ y
unapos≀apos {x} {y} p = unpos≀pos (≀-trans (≀-refl' (sym (assign-pos x))) (≀-trans p (≀-refl' (assign-pos y))))

TotalityπŸ”—

The ordering on the integers is decidable, and moreover it is a total order. We show weak totality: if then

≀-is-weakly-total : βˆ€ x y β†’ Β¬ (x ≀ y) β†’ y ≀ x
≀-is-weakly-total (pos    x) (pos    y) p = pos≀pos $
  Nat.≀-is-weakly-total x y (p ∘ pos≀pos)
≀-is-weakly-total (pos    x) (negsuc y) p = neg≀pos
≀-is-weakly-total (negsuc x) (pos    y) p = absurd (p neg≀pos)
≀-is-weakly-total (negsuc x) (negsuc y) p = neg≀neg $
  Nat.≀-is-weakly-total y x (p ∘ neg≀neg)

instance
  Dec-≀ : βˆ€ {x y} β†’ Dec (x ≀ y)
  Dec-≀ {pos x} {pos y} with holds? (x Nat.≀ y)
  ... | yes p = yes (pos≀pos p)
  ... | no Β¬p = no Ξ» { (pos≀pos p) β†’ Β¬p p }
  Dec-≀ {negsuc x} {negsuc y} with holds? (y Nat.≀ x)
  ... | yes p = yes (neg≀neg p)
  ... | no Β¬p = no Ξ» { (neg≀neg p) β†’ Β¬p p }
  Dec-≀ {pos x} {negsuc y} = no Β¬pos≀neg
  Dec-≀ {negsuc x} {pos y} = yes neg≀pos

Universal properties of maximum and minimumπŸ”—

This case bash shows that maxβ„€ and minβ„€ satisfy their universal properties.

  maxβ„€-≀l : (x y : Int) β†’ x ≀ maxβ„€ x y
  maxβ„€-≀l (pos x)    (pos y)    = pos≀pos (Nat.max-≀l x y)
  maxβ„€-≀l (pos _)    (negsuc _) = ≀-refl
  maxβ„€-≀l (negsuc _) (pos _)    = neg≀pos
  maxβ„€-≀l (negsuc x) (negsuc y) = neg≀neg (Nat.min-≀l x y)

  maxβ„€-≀r : (x y : Int) β†’ y ≀ maxβ„€ x y
  maxβ„€-≀r (pos x)    (pos y)    = pos≀pos (Nat.max-≀r x y)
  maxβ„€-≀r (pos _)    (negsuc _) = neg≀pos
  maxβ„€-≀r (negsuc _) (pos _)    = ≀-refl
  maxβ„€-≀r (negsuc x) (negsuc y) = neg≀neg (Nat.min-≀r x y)

  maxβ„€-univ : (x y z : Int) β†’ x ≀ z β†’ y ≀ z β†’ maxβ„€ x y ≀ z
  maxβ„€-univ _ _ _ (pos≀pos x≀z) (pos≀pos y≀z) = pos≀pos (Nat.max-univ _ _ _ x≀z y≀z)
  maxβ„€-univ _ _ _ (pos≀pos x≀z) neg≀pos       = pos≀pos x≀z
  maxβ„€-univ _ _ _ neg≀pos       (pos≀pos y≀z) = pos≀pos y≀z
  maxβ„€-univ _ _ _ neg≀pos       neg≀pos       = neg≀pos
  maxβ„€-univ _ _ _ (neg≀neg xβ‰₯z) (neg≀neg yβ‰₯z) = neg≀neg (Nat.min-univ _ _ _ xβ‰₯z yβ‰₯z)

  minβ„€-≀l : (x y : Int) β†’ minβ„€ x y ≀ x
  minβ„€-≀l (pos x)    (pos y)    = pos≀pos (Nat.min-≀l x y)
  minβ„€-≀l (pos _)    (negsuc _) = neg≀pos
  minβ„€-≀l (negsuc _) (pos _)    = ≀-refl
  minβ„€-≀l (negsuc x) (negsuc y) = neg≀neg (Nat.max-≀l x y)

  minβ„€-≀r : (x y : Int) β†’ minβ„€ x y ≀ y
  minβ„€-≀r (pos x)    (pos y)    = pos≀pos (Nat.min-≀r x y)
  minβ„€-≀r (pos _)    (negsuc _) = ≀-refl
  minβ„€-≀r (negsuc _) (pos _)    = neg≀pos
  minβ„€-≀r (negsuc x) (negsuc y) = neg≀neg (Nat.max-≀r x y)

  minβ„€-univ : (x y z : Int) β†’ z ≀ x β†’ z ≀ y β†’ z ≀ minβ„€ x y
  minβ„€-univ _ _ _ (pos≀pos xβ‰₯z) (pos≀pos yβ‰₯z) = pos≀pos (Nat.min-univ _ _ _ xβ‰₯z yβ‰₯z)
  minβ„€-univ _ _ _ neg≀pos       neg≀pos       = neg≀pos
  minβ„€-univ _ _ _ neg≀pos       (neg≀neg y≀z) = neg≀neg y≀z
  minβ„€-univ _ _ _ (neg≀neg x≀z) neg≀pos       = neg≀neg x≀z
  minβ„€-univ _ _ _ (neg≀neg x≀z) (neg≀neg y≀z) = neg≀neg (Nat.max-univ _ _ _ x≀z y≀z)

Compatibility with the structureπŸ”—

The last case bash in this module will establish that the ordering on integers is compatible with the successor, predecessor, and negation. Since addition is equivalent to iterated application of the successor and predecessor, we get as a corollary that addition also respects the order.

suc-≀ : βˆ€ x y β†’ x ≀ y β†’ sucβ„€ x ≀ sucβ„€ y
suc-≀ (pos x) (pos y) (pos≀pos p) = pos≀pos (Nat.s≀s p)
suc-≀ (negsuc zero) (pos y) p = pos≀pos Nat.0≀x
suc-≀ (negsuc zero) (negsuc zero) p = ≀-refl
suc-≀ (negsuc zero) (negsuc (suc y)) (neg≀neg ())
suc-≀ (negsuc (suc x)) (pos y) p = neg≀pos
suc-≀ (negsuc (suc x)) (negsuc zero) p = neg≀pos
suc-≀ (negsuc (suc x)) (negsuc (suc y)) (neg≀neg (Nat.s≀s p)) = neg≀neg p

pred-≀ : βˆ€ x y β†’ x ≀ y β†’ predβ„€ x ≀ predβ„€ y
pred-≀ posz posz p = ≀-refl
pred-≀ posz (possuc y) p = neg≀pos
pred-≀ (possuc x) posz (pos≀pos ())
pred-≀ (possuc x) (possuc y) (pos≀pos (Nat.s≀s p)) = pos≀pos p
pred-≀ (negsuc x) posz p = neg≀neg Nat.0≀x
pred-≀ (negsuc x) (possuc y) p = neg≀pos
pred-≀ (negsuc x) (negsuc y) (neg≀neg p) = neg≀neg (Nat.s≀s p)

rot℀≀l : βˆ€ k x y β†’ x ≀ y β†’ rotβ„€ k x ≀ rotβ„€ k y
rot℀≀l posz             x y p = p
rot℀≀l (possuc k)       x y p = suc-≀ _ _ (rot℀≀l (pos k) x y p)
rot℀≀l (negsuc zero)    x y p = pred-≀ _ _ p
rot℀≀l (negsuc (suc k)) x y p = pred-≀ _ _ (rot℀≀l (negsuc k) x y p)

abstract
  +β„€-preserves-≀l : βˆ€ k x y β†’ x ≀ y β†’ (k +β„€ x) ≀ (k +β„€ y)
  +β„€-preserves-≀l k x y p = transport
    (sym (apβ‚‚ _≀_ (rot-is-add k x) (rot-is-add k y)))
    (rot℀≀l k x y p)

  +β„€-preserves-≀r : βˆ€ k x y β†’ x ≀ y β†’ (x +β„€ k) ≀ (y +β„€ k)
  +β„€-preserves-≀r k x y p = transport
    (apβ‚‚ _≀_ (+β„€-commutative k x) (+β„€-commutative k y))
    (+β„€-preserves-≀l k x y p)

  negβ„€-anti : βˆ€ x y β†’ x ≀ y β†’ negβ„€ y ≀ negβ„€ x
  negβ„€-anti posz       posz       x≀y                     = x≀y
  negβ„€-anti posz       (possuc y) _                       = neg≀pos
  negβ„€-anti (possuc x) (possuc y) (pos≀pos (Nat.s≀s x≀y)) = neg≀neg x≀y
  negβ„€-anti (negsuc _) posz       _                       = pos≀pos Nat.0≀x
  negβ„€-anti (negsuc _) (possuc y) _                       = neg≀pos
  negβ„€-anti (negsuc x) (negsuc y) (neg≀neg x≀y)           = pos≀pos (Nat.s≀s x≀y)

  negβ„€-anti-full : βˆ€ x y β†’ negβ„€ y ≀ negβ„€ x β†’ x ≀ y
  negβ„€-anti-full posz       (pos y)    _                       = pos≀pos Nat.0≀x
  negβ„€-anti-full posz       (negsuc y) (pos≀pos ())
  negβ„€-anti-full (possuc x) (possuc y) (neg≀neg x≀y)           = pos≀pos (Nat.s≀s x≀y)
  negβ„€-anti-full (negsuc x) (pos y)    _                       = neg≀pos
  negβ„€-anti-full (negsuc x) (negsuc y) (pos≀pos (Nat.s≀s y≀x)) = neg≀neg y≀x

  *β„€-cancel-≀r : βˆ€ {x y z} ⦃ _ : Positive x ⦄ β†’ (y *β„€ x) ≀ (z *β„€ x) β†’ y ≀ z
  *β„€-cancel-≀r {possuc x} {y = pos y} {pos z} ⦃ pos _ ⦄ p = pos≀pos
    (Nat.*-cancel-≀r (suc x) (unapos≀apos p))
  *β„€-cancel-≀r {possuc x} {y = pos y} {negsuc z} ⦃ pos _ ⦄ p = absurd (Β¬pos≀neg (≀-trans (≀-refl' (sym (assign-pos (y * suc x)))) p))
  *β„€-cancel-≀r {possuc x} {y = negsuc y} {pos z} ⦃ pos _ ⦄ p = neg≀pos
  *β„€-cancel-≀r {possuc x} {y = negsuc y} {negsuc z} ⦃ pos _ ⦄ p = neg≀neg
    (Nat.*-cancel-≀r (suc x) (Nat.+-reflects-≀l (z * suc x) (y * suc x) x (unneg≀neg p)))

  *β„€-preserves-≀r : βˆ€ {x y} z ⦃ _ : Positive z ⦄ β†’ x ≀ y β†’ (x *β„€ z) ≀ (y *β„€ z)
  *β„€-preserves-≀r {pos x} {pos y} (possuc z) ⦃ pos z ⦄ p = apos≀apos {x * suc z} {y * suc z} (Nat.*-preserves-≀r x y (suc z) (unpos≀pos p))
  *β„€-preserves-≀r {negsuc x} {pos y} (possuc z) ⦃ pos z ⦄ p = ≀-trans neg≀pos (≀-refl' (sym (assign-pos (y * suc z))))
  *β„€-preserves-≀r {negsuc x} {negsuc y} (possuc z) ⦃ pos z ⦄ p = neg≀neg (Nat.+-preserves-≀l (y * suc z) (x * suc z) z (Nat.*-preserves-≀r y x (suc z) (unneg≀neg p)))

  *β„€-nonnegative : βˆ€ {x y} β†’ 0 ≀ x β†’ 0 ≀ y β†’ 0 ≀ (x *β„€ y)
  *β„€-nonnegative {pos x} {pos y} (pos≀pos p) (pos≀pos q) = ≀-trans (pos≀pos Nat.0≀x) (≀-refl' (sym (assign-pos (x * y))))

  positiveβ†’nonnegative : βˆ€ {x} β†’ Positive x β†’ 0 ≀ x
  positiveβ†’nonnegative (pos x) = pos≀pos Nat.0≀x

  -β„•-nonnegative : βˆ€ {x y} β†’ y Nat.≀ x β†’ 0 ≀ (x β„•- y)
  -β„•-nonnegative {x} {y} Nat.0≀x = pos≀pos Nat.0≀x
  -β„•-nonnegative {suc x} {suc y} (Nat.s≀s p) = -β„•-nonnegative p

  -β„€-nonnegative : βˆ€ {x y} β†’ 0 ≀ x β†’ 0 ≀ y β†’ y ≀ x β†’ 0 ≀ (x -β„€ y)
  -β„€-nonnegative {posz} {posz} (pos≀pos p) (pos≀pos q) (pos≀pos r) = pos≀pos Nat.0≀x
  -β„€-nonnegative {possuc x} {posz} (pos≀pos p) (pos≀pos q) (pos≀pos r) = pos≀pos Nat.0≀x
  -β„€-nonnegative {possuc x} {possuc y} (pos≀pos p) (pos≀pos q) (pos≀pos r) = -β„•-nonnegative (Nat.≀-peel r)

The strict orderπŸ”—

data _<_ : Int β†’ Int β†’ Type where
  pos<pos : βˆ€ {x y} β†’ x Nat.< y β†’ pos x < pos y
  neg<pos : βˆ€ {x y} β†’ negsuc x < pos y
  neg<neg : βˆ€ {x y} β†’ y Nat.< x β†’ negsuc x < negsuc y

instance
  H-Level-< : βˆ€ {x y n} β†’ H-Level (x < y) (suc n)
  H-Level-< = prop-instance Ξ» where
    (pos<pos x) (pos<pos y) β†’ ap pos<pos (Nat.≀-is-prop x y)
    neg<pos neg<pos β†’ refl
    (neg<neg x) (neg<neg y) β†’ ap neg<neg (Nat.≀-is-prop x y)

<-not-equal : βˆ€ {x y} β†’ x < y β†’ x β‰  y
<-not-equal (pos<pos p) q = Nat.<-not-equal p (pos-injective q)
<-not-equal neg<pos q = negsuc≠pos q
<-not-equal (neg<neg p) q = Nat.<-not-equal p (negsuc-injective (sym q))

<-irrefl : βˆ€ {x y} β†’ x ≑ y β†’ Β¬ (x < y)
<-irrefl p q = <-not-equal q p

<-weaken : βˆ€ {x y} β†’ x < y β†’ x ≀ y
<-weaken (pos<pos x) = pos≀pos (Nat.<-weaken x)
<-weaken neg<pos = neg≀pos
<-weaken (neg<neg x) = neg≀neg (Nat.<-weaken x)

<-asym : βˆ€ {x y} β†’ x < y β†’ Β¬ (y < x)
<-asym (pos<pos x) (pos<pos y) = Nat.<-asym x y
<-asym (neg<neg x) (neg<neg y) = Nat.<-asym x y

≀-strengthen : βˆ€ {x y} β†’ x ≀ y β†’ (x ≑ y) ⊎ (x < y)
≀-strengthen (neg≀neg x) with Nat.≀-strengthen x
... | inl x=y = inl (ap negsuc (sym x=y))
... | inr x<y = inr (neg<neg x<y)
≀-strengthen (pos≀pos x) with Nat.≀-strengthen x
... | inl x=y = inl (ap pos x=y)
... | inr x<y = inr (pos<pos x<y)
≀-strengthen neg≀pos = inr neg<pos

<-from-≀ : βˆ€ {x y} β†’ x ≀ y β†’ x β‰  y β†’ x < y
<-from-≀ x≀y xβ‰ y with ≀-strengthen x≀y
... | inl x=y = absurd (x≠y x=y)
... | inr p = p

<-dec : βˆ€ x y β†’ Dec (x < y)
<-dec (pos x) (pos y) with Nat.≀-dec (suc x) y
... | yes x<y = yes (pos<pos x<y)
... | no Β¬x<y = no Ξ» { (pos<pos x<y) β†’ Β¬x<y x<y }
<-dec (pos x) (negsuc y) = no Ξ» ()
<-dec (negsuc x) (pos y) = yes neg<pos
<-dec (negsuc x) (negsuc y) with Nat.≀-dec (suc y) x
... | yes y<x = yes (neg<neg y<x)
... | no Β¬y<x = no Ξ» { (neg<neg y<x) β†’ Β¬y<x y<x }

instance
  Dec-< : βˆ€ {x y} β†’ Dec (x < y)
  Dec-< {x} {y} = <-dec x y

≀-from-not-< : βˆ€ {x y} β†’ Β¬ x < y β†’ y ≀ x
≀-from-not-< {pos x} {pos y} Β¬x<y = pos≀pos (Nat.≀-from-not-< x y (Ξ» x<y β†’ Β¬x<y (pos<pos x<y)))
≀-from-not-< {pos x} {negsuc y} Β¬x<y = neg≀pos
≀-from-not-< {negsuc x} {pos y} Β¬x<y = absurd (Β¬x<y neg<pos)
≀-from-not-< {negsuc x} {negsuc y} Β¬x<y = neg≀neg (Nat.≀-from-not-< y x (Ξ» y<x β†’ Β¬x<y (neg<neg y<x)))

<-linear : βˆ€ {x y} β†’ Β¬ x < y β†’ Β¬ y < x β†’ x ≑ y
<-linear {x} {y} Β¬x<y Β¬y<x = ≀-antisym (≀-from-not-< Β¬y<x) (≀-from-not-< Β¬x<y)

<-split : βˆ€ x y β†’ (x < y) ⊎ (x ≑ y) ⊎ (y < x)
<-split x y with <-dec x y
... | yes x<y = inl x<y
... | no Β¬x<y with <-dec y x
... | yes y<x = inr (inr y<x)
... | no Β¬y<x = inr (inl (<-linear Β¬x<y Β¬y<x))

<-trans : βˆ€ {x y z} β†’ x < y β†’ y < z β†’ x < z
<-trans (pos<pos p) (pos<pos q) = pos<pos (Nat.<-trans _ _ _ p q)
<-trans neg<pos (pos<pos q) = neg<pos
<-trans (neg<neg p) neg<pos = neg<pos
<-trans (neg<neg p) (neg<neg q) = neg<neg (Nat.<-trans _ _ _ q p)

<-≀-trans : βˆ€ {x y z} β†’ x < y β†’ y ≀ z β†’ x < z
<-≀-trans p q with ≀-strengthen q
... | inl y=z = substβ‚‚ _<_ refl y=z p
... | inr y<z = <-trans p y<z

≀-<-trans : βˆ€ {x y z} β†’ x ≀ y β†’ y < z β†’ x < z
≀-<-trans p q with ≀-strengthen p
... | inl x=y = substβ‚‚ _<_ (sym x=y) refl q
... | inr x<y = <-trans x<y q

abstract
  nat-diff-<-possuc : βˆ€ k x β†’ (k β„•- x) < possuc k
  nat-diff-<-possuc zero zero = pos<pos (Nat.s≀s Nat.0≀x)
  nat-diff-<-possuc zero (suc x) = neg<pos
  nat-diff-<-possuc (suc k) zero = pos<pos Nat.≀-refl
  nat-diff-<-possuc (suc k) (suc x) = <-trans (nat-diff-<-possuc k x) (pos<pos Nat.≀-refl)

  nat-diff-<-pos : βˆ€ k x β†’ (k β„•- suc x) < pos k
  nat-diff-<-pos zero zero = neg<pos
  nat-diff-<-pos zero (suc x) = neg<pos
  nat-diff-<-pos (suc k) zero = pos<pos auto
  nat-diff-<-pos (suc k) (suc x) = nat-diff-<-possuc k (suc x)

  negsuc-<-nat-diff : βˆ€ k x β†’ negsuc k < (x β„•- k)
  negsuc-<-nat-diff zero zero = neg<pos
  negsuc-<-nat-diff zero (suc x) = neg<pos
  negsuc-<-nat-diff (suc k) zero = neg<neg auto
  negsuc-<-nat-diff (suc k) (suc x) = <-trans (neg<neg auto) (negsuc-<-nat-diff k x)

  negsuc-≀-nat-diff : βˆ€ k x β†’ negsuc k ≀ (x β„•- suc k)
  negsuc-≀-nat-diff zero zero = neg≀neg Nat.0≀x
  negsuc-≀-nat-diff zero (suc x) = neg≀pos
  negsuc-≀-nat-diff (suc k) zero = neg≀neg auto
  negsuc-≀-nat-diff (suc k) (suc x) = ≀-trans (neg≀neg Nat.≀-ascend) (negsuc-≀-nat-diff k x)

  nat-diff-preserves-<r : βˆ€ k {x y} β†’ y Nat.< x β†’ (k β„•- x) < (k β„•- y)
  nat-diff-preserves-<r zero {suc zero} {zero} (Nat.s≀s Nat.0≀x) = neg<pos
  nat-diff-preserves-<r zero {suc (suc x)} {zero} (Nat.s≀s p) = neg<pos
  nat-diff-preserves-<r zero {suc (suc x)} {suc y} (Nat.s≀s p) = neg<neg p
  nat-diff-preserves-<r (suc k) {suc x} {zero} (Nat.s≀s p) = nat-diff-<-possuc k x
  nat-diff-preserves-<r (suc k) {suc x} {suc y} (Nat.s≀s p) = nat-diff-preserves-<r k {x} {y} p

  nat-diff-preserves-<l : βˆ€ k {x y} β†’ x Nat.< y β†’ (x β„•- k) < (y β„•- k)
  nat-diff-preserves-<l zero {zero} {suc y} (Nat.s≀s p) = pos<pos (Nat.s≀s Nat.0≀x)
  nat-diff-preserves-<l zero {suc x} {suc y} (Nat.s≀s p) = pos<pos (Nat.s≀s p)
  nat-diff-preserves-<l (suc k) {zero} {suc y} (Nat.s≀s Nat.0≀x) = negsuc-<-nat-diff k y
  nat-diff-preserves-<l (suc k) {suc x} {suc y} (Nat.s≀s p) = nat-diff-preserves-<l k {x} {y} p

  +β„€-preserves-<r : βˆ€ x y z β†’ x < y β†’ (x +β„€ z) < (y +β„€ z)
  +β„€-preserves-<r x y (pos z) (pos<pos p) = pos<pos (Nat.+-preserves-<r _ _ z p)
  +β„€-preserves-<r (negsuc x) (pos y) (pos z) neg<pos =
    let
      rem₁ : z Nat.≀ y + z
      rem₁ = Nat.≀-trans (Nat.+-≀l z y) (Nat.≀-refl' (Nat.+-commutative z y))
    in <-≀-trans (nat-diff-<-pos z x) (pos≀pos rem₁)
  +β„€-preserves-<r x y (pos z) (neg<neg p) = nat-diff-preserves-<r z (Nat.s≀s p)
  +β„€-preserves-<r x y (negsuc z) (pos<pos p) = nat-diff-preserves-<l (suc z) p
  +β„€-preserves-<r (negsuc x) (pos y) (negsuc z) neg<pos =
    let
      rem₁ : suc z Nat.≀ suc (x + z)
      rem₁ = Nat.≀-trans (Nat.+-≀l (suc z) x) (Nat.≀-refl' (ap suc (Nat.+-commutative z x)))
    in <-≀-trans (neg<neg rem₁) (negsuc-≀-nat-diff z y)
  +β„€-preserves-<r x y (negsuc z) (neg<neg p) = neg<neg (Nat.s≀s (Nat.+-preserves-<r _ _ z p))

  +β„€-preserves-<l : βˆ€ x y z β†’ x < y β†’ (z +β„€ x) < (z +β„€ y)
  +β„€-preserves-<l x y z p = substβ‚‚ _<_ (+β„€-commutative x z) (+β„€-commutative y z) (+β„€-preserves-<r x y z p)

  +β„€-preserves-< : βˆ€ x y x' y' β†’ x < y β†’ x' < y' β†’ (x +β„€ x') < (y +β„€ y')
  +β„€-preserves-< x y x' y' p q = <-trans (+β„€-preserves-<r x y x' p) (+β„€-preserves-<l x' y' y q)

  *β„€-cancel-<r : βˆ€ {x y z} ⦃ _ : Positive x ⦄ β†’ (y *β„€ x) < (z *β„€ x) β†’ y < z
  *β„€-cancel-<r {x} {y} {z} yx<zx = <-from-≀
    (*β„€-cancel-≀r {x} {y} {z} (<-weaken yx<zx))
    Ξ» y=z β†’ <-irrefl (ap (_*β„€ x) y=z) yx<zx

  *β„€-cancel-<l : βˆ€ {x y z} ⦃ _ : Positive x ⦄ β†’ (x *β„€ y) < (x *β„€ z) β†’ y < z
  *β„€-cancel-<l {x} {y} {z} xy<xz = *β„€-cancel-<r {x} {y} {z} (substβ‚‚ _<_ (*β„€-commutative x y) (*β„€-commutative x z) xy<xz)

  *β„€-preserves-<r : βˆ€ {x y} z ⦃ _ : Positive z ⦄ β†’ x < y β†’ (x *β„€ z) < (y *β„€ z)
  *β„€-preserves-<r {x} {y} z x<y = <-from-≀
    (*β„€-preserves-≀r {x} {y} z (<-weaken x<y))
    λ xz=yz → <-irrefl (*℀-injectiver z x y (positive→nonzero auto) xz=yz) x<y

  negβ„€-anti-< : βˆ€ {x y} β†’ x < y β†’ negβ„€ y < negβ„€ x
  negβ„€-anti-< {posz} {pos y} (pos<pos (Nat.s≀s p)) = neg<pos
  negβ„€-anti-< {possuc x} {pos y} (pos<pos (Nat.s≀s p)) = neg<neg p
  negβ„€-anti-< {negsuc x} {posz} neg<pos = pos<pos (Nat.s≀s Nat.0≀x)
  negβ„€-anti-< {negsuc x} {possuc y} neg<pos = neg<pos
  negβ„€-anti-< {negsuc x} {negsuc y} (neg<neg p) = pos<pos (Nat.s≀s p)

  negβ„€-anti-full-< : βˆ€ {x y} β†’ negβ„€ x < negβ„€ y β†’ y < x
  negβ„€-anti-full-< {x} {y} p = substβ‚‚ _<_ (negβ„€-negβ„€ y) (negβ„€-negβ„€ x) (negβ„€-anti-< p)