------------------------------------------------------------------------ -- The Agda standard library -- -- Basic properties of ℕᵇ ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Nat.Binary.Properties where open import Algebra.Bundles using (Magma; Monoid; CommutativeMonoid; Semigroup; CommutativeSemigroup ; Semiring; CommutativeSemiring) open import Algebra.Morphism.Structures import Algebra.Morphism.MonoidMonomorphism as MonoidMonomorphism open import Algebra.Consequences.Propositional using (comm∧distrˡ⇒distrʳ) open import Data.Bool.Base using (if_then_else_; Bool; true; false) open import Data.Maybe.Base using (Maybe; just; nothing) open import Data.Nat.Binary.Base open import Data.Nat.Base as ℕ using (ℕ; z≤n; s≤s; s0 : x ≢ zero → x > zero x≢0⇒x>0 {zero} 0≢0 = contradiction refl 0≢0 x≢0⇒x>0 {2[1+ _ ]} _ = 00 {1+[2 _ ]} _ = 0⇒≢ : _>_ ⇒ _≢_ >⇒≢ yy = >⇒≢ x>y x≡y <⇒≯ : _<_ ⇒ _≯_ <⇒≯ (even⇒≮ : _>_ ⇒ _≮_ >⇒≮ y (λ()) (λ()) 0 _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = eveny <-cmp 2[1+ x ] 1+[2 y ] with <-cmp x y ... | tri< x (>⇒≮ gt) (>⇒≢ gt) gt where gt = subst (_< 2[1+ x ]) refl (1+[2x]<2[1+x] x) ... | tri> _ _ y (>⇒≮ gt) (>⇒≢ gt) gt where gt = odd (>⇒≮ gt) (>⇒≢ gt) gt where gt = 0 _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = eveny <-cmp 1+[2 x ] 1+[2 y ] with <-cmp x y ... | tri< x _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = oddy _y = <⇒≱ x>y x≤y ≮⇒≥ : _≮_ ⇒ _≥_ ≮⇒≥ {x} {y} x≮y with <-cmp x y ... | tri< lt _ _ = contradiction lt x≮y ... | tri≈ _ eq _ = inj₂ (sym eq) ... | tri> _ _ y : _≰_ ⇒ _>_ ≰⇒> {x} {y} x≰y with <-cmp x y ... | tri< lt _ _ = contradiction (<⇒≤ lt) x≰y ... | tri≈ _ eq _ = contradiction (inj₂ eq) x≰y ... | tri> _ _ x>y = x>y ≤∧≢⇒< : ∀ {x y} → x ≤ y → x ≢ y → x < y ≤∧≢⇒< (inj₁ x _ _ y _ _ y 0ᵇ → x < x + y x0 = begin-strict x ≡⟨ sym (fromℕ-toℕ x) ⟩ fromℕ (toℕ x) <⟨ fromℕ-mono-< (ℕ.m0)) ⟩ fromℕ (toℕ x ℕ.+ toℕ y) ≡⟨ fromℕ-homo-+ (toℕ x) (toℕ y) ⟩ fromℕ (toℕ x) + fromℕ (toℕ y) ≡⟨ cong₂ _+_ (fromℕ-toℕ x) (fromℕ-toℕ y) ⟩ x + y ∎ where open ≤-Reasoning x _ _ x>y = contradiction 2x≤2y (<⇒≱ (double-mono-< x>y)) double-cancel-< : ∀ {x y} → double x < double y → x < y double-cancel-< {x} {y} 2x<2y with <-cmp x y ... | tri< x _ _ x>y = contradiction (double-mono-< x>y) (<⇒≯ 2x<2y) x0 x≢0) ⟩ x + x ≡⟨ sym (double[x]≡x+x x) ⟩ double x ∎ where open ≤-Reasoning x≤double[x] : ∀ x → x ≤ double x x≤double[x] x = begin x ≤⟨ x≤x+y x x ⟩ x + x ≡⟨ sym (double[x]≡x+x x) ⟩ double x ∎ where open ≤-Reasoning double-suc : ∀ x → double (suc x) ≡ 2ᵇ + double x double-suc x = begin double (suc x) ≡⟨ cong double (suc≗1+ x) ⟩ double (1ᵇ + x) ≡⟨ double-distrib-+ 1ᵇ x ⟩ 2ᵇ + double x ∎ where open ≡-Reasoning ------------------------------------------------------------------------ -- Properties of suc ------------------------------------------------------------------------ suc≢0 : suc x ≢ zero suc≢0 {zero} () suc≢0 {2[1+ _ ]} () suc≢0 {1+[2 _ ]} () suc-injective : Injective _≡_ _≡_ suc suc-injective {zero} {zero} p = refl suc-injective {zero} {2[1+ y ]} p = contradiction 1+[2 p ]-injective (suc≢0 ∘ sym) suc-injective {2[1+ x ]} {zero} p = contradiction 1+[2 p ]-injective suc≢0 suc-injective {2[1+ x ]} {2[1+ y ]} p = cong 2[1+_] (suc-injective 1+[2 p ]-injective) suc-injective {1+[2 x ]} {1+[2 y ]} refl = refl 2[1+_]-double-suc : 2[1+_] ≗ double ∘ suc 2[1+_]-double-suc zero = refl 2[1+_]-double-suc 2[1+ x ] = cong 2[1+_] (2[1+_]-double-suc x) 2[1+_]-double-suc 1+[2 x ] = refl 1+[2_]-suc-double : 1+[2_] ≗ suc ∘ double 1+[2_]-suc-double zero = refl 1+[2_]-suc-double 2[1+ x ] = refl 1+[2_]-suc-double 1+[2 x ] = begin 1+[2 1+[2 x ] ] ≡⟨ cong 1+[2_] (1+[2_]-suc-double x) ⟩ 1+[2 (suc 2x) ] ≡⟨⟩ suc 2[1+ 2x ] ≡⟨ cong suc (2[1+_]-double-suc 2x) ⟩ suc (double (suc 2x)) ≡⟨ cong (suc ∘ double) (sym (1+[2_]-suc-double x)) ⟩ suc (double 1+[2 x ]) ∎ where open ≡-Reasoning; 2x = double x x+suc[y]≡suc[x]+y : ∀ x y → x + suc y ≡ suc x + y x+suc[y]≡suc[x]+y x y = begin x + suc y ≡⟨ +-comm x _ ⟩ suc y + x ≡⟨ suc-+ y x ⟩ suc (y + x) ≡⟨ cong suc (+-comm y x) ⟩ suc (x + y) ≡⟨ sym (suc-+ x y) ⟩ suc x + y ∎ where open ≡-Reasoning 00 (suc≢0 {x}) x