------------------------------------------------------------------------ -- The Agda standard library -- -- Subtraction on Bin and some of its properties. ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Nat.Binary.Subtraction where open import Algebra.Core using (Op₂) open import Algebra.Bundles using (Magma) open import Algebra.Consequences.Propositional using (comm∧distrˡ⇒distrʳ) open import Algebra.Morphism.Consequences using (homomorphic₂-inv) open import Data.Bool.Base using (true; false; if_then_else_) open import Data.Nat as ℕ using (ℕ) open import Data.Nat.Binary.Base using (ℕᵇ; 0ᵇ; 2[1+_]; 1+[2_]; double; pred; toℕ; fromℕ; even_; _≤_; _<_; _+_; zero; suc; 1ᵇ; _*_) open import Data.Nat.Binary.Properties import Data.Nat.Properties as ℕ open import Data.Product.Base using (_×_; _,_; proj₁; proj₂; ∃) open import Data.Sum.Base using (inj₁; inj₂) open import Data.Vec.Base using ([]; _∷_) open import Function.Base using (_∘_; _$_) open import Level using (0ℓ) open import Relation.Binary using (Tri; tri<; tri≈; tri>; _Preserves_⟶_; _Preserves₂_⟶_⟶_) open import Relation.Binary.PropositionalEquality.Algebra using (magma) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong; cong₂; sym; trans; subst; _≢_) open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning) open import Relation.Nullary using (Dec; yes; no; does) open import Relation.Nullary.Negation using (contradiction) open import Algebra.Definitions {A = ℕᵇ} _≡_ open import Algebra.Properties.CommutativeSemigroup +-commutativeSemigroup using (xy∙z≈y∙xz; x∙yz≈y∙xz) open import Algebra.Solver.CommutativeMonoid +-0-commutativeMonoid private variable x y : ℕᵇ ------------------------------------------------------------------------ -- Definition infixl 6 _∸_ _∸_ : Op₂ ℕᵇ zero ∸ _ = 0ᵇ x ∸ zero = x 2[1+ x ] ∸ 2[1+ y ] = double (x ∸ y) 1+[2 x ] ∸ 1+[2 y ] = double (x ∸ y) 2[1+ x ] ∸ 1+[2 y ] = if does (x : x > y → 1+[2 x ] ∸ 2[1+ y ] ≡ pred (double (x ∸ y)) odd∸even-for> {x} {y} x>y with x ≤? y ... | no _ = refl ... | yes x≤y = contradiction x>y (≤⇒≯ x≤y) x≤y⇒x∸y≡0 : x ≤ y → x ∸ y ≡ 0ᵇ x≤y⇒x∸y≡0 {x} {y} = toℕ-injective ∘ trans (toℕ-homo-∸ x y) ∘ ℕ.m≤n⇒m∸n≡0 ∘ toℕ-mono-≤ x∸y≡0⇒x≤y : x ∸ y ≡ 0ᵇ → x ≤ y x∸y≡0⇒x≤y {x} {y} = toℕ-cancel-≤ ∘ ℕ.m∸n≡0⇒m≤n ∘ trans (sym (toℕ-homo-∸ x y)) ∘ cong toℕ x0 : x < y → y ∸ x > 0ᵇ x0 {x} {y} = toℕ-cancel-< ∘ subst (ℕ._> 0) (sym (toℕ-homo-∸ y x)) ∘ ℕ.m