| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Subtraction on Bin and some of its properties. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Nat.Binary.Subtraction where |
| 10 |
|
| 11 |
open import Algebra.Core using (Op₂) |
| 12 |
open import Algebra.Bundles using (Magma) |
| 13 |
open import Algebra.Consequences.Propositional using (comm∧distrˡ⇒distrʳ) |
| 14 |
open import Algebra.Morphism.Consequences using (homomorphic₂-inv) |
| 15 |
open import Data.Bool.Base using (true; false; if_then_else_) |
| 16 |
open import Data.Nat as ℕ using (ℕ) |
| 17 |
open import Data.Nat.Binary.Base using (ℕᵇ; 0ᵇ; 2[1+_]; 1+[2_]; double; |
| 18 |
pred; toℕ; fromℕ; even<odd; odd<even; _≥_; _>_; _≤_; _<_; _+_; zero; suc; 1ᵇ; |
| 19 |
_*_) |
| 20 |
open import Data.Nat.Binary.Properties |
| 21 |
import Data.Nat.Properties as ℕ |
| 22 |
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂; ∃) |
| 23 |
open import Data.Sum.Base using (inj₁; inj₂) |
| 24 |
open import Data.Vec.Base using ([]; _∷_) |
| 25 |
open import Function.Base using (_∘_; _$_) |
| 26 |
open import Level using (0ℓ) |
| 27 |
open import Relation.Binary |
| 28 |
using (Tri; tri<; tri≈; tri>; _Preserves_⟶_; _Preserves₂_⟶_⟶_) |
| 29 |
open import Relation.Binary.PropositionalEquality.Algebra using (magma) |
| 30 |
open import Relation.Binary.PropositionalEquality.Core |
| 31 |
using (_≡_; refl; cong; cong₂; sym; trans; subst; _≢_) |
| 32 |
open import Relation.Binary.PropositionalEquality.Properties |
| 33 |
using (module ≡-Reasoning) |
| 34 |
open import Relation.Nullary using (Dec; yes; no; does) |
| 35 |
open import Relation.Nullary.Negation using (contradiction) |
| 36 |
|
| 37 |
open import Algebra.Definitions {A = ℕᵇ} _≡_ |
| 38 |
open import Algebra.Properties.CommutativeSemigroup +-commutativeSemigroup |
| 39 |
using (xy∙z≈y∙xz; x∙yz≈y∙xz) |
| 40 |
open import Algebra.Solver.CommutativeMonoid +-0-commutativeMonoid |
| 41 |
|
| 42 |
private |
| 43 |
variable |
| 44 |
x y : ℕᵇ |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Definition |
| 48 |
|
| 49 |
infixl 6 _∸_ |
| 50 |
|
| 51 |
_∸_ : Op₂ ℕᵇ |
| 52 |
zero ∸ _ = 0ᵇ |
| 53 |
x ∸ zero = x |
| 54 |
2[1+ x ] ∸ 2[1+ y ] = double (x ∸ y) |
| 55 |
1+[2 x ] ∸ 1+[2 y ] = double (x ∸ y) |
| 56 |
2[1+ x ] ∸ 1+[2 y ] = if does (x <? y) then 0ᵇ else 1+[2 (x ∸ y) ] |
| 57 |
1+[2 x ] ∸ 2[1+ y ] = if does (x ≤? y) then 0ᵇ else pred (double (x ∸ y)) |
| 58 |
|
| 59 |
------------------------------------------------------------------------ |
| 60 |
-- Properties of _∸_ and _≡_ |
| 61 |
|
| 62 |
∸-magma : Magma 0ℓ 0ℓ |
| 63 |
∸-magma = magma _∸_ |
| 64 |
|
| 65 |
x∸0≡x : ∀ x → x ∸ 0ᵇ ≡ x |
| 66 |
x∸0≡x zero = refl |
| 67 |
x∸0≡x 2[1+ _ ] = refl |
| 68 |
x∸0≡x 1+[2 _ ] = refl |
| 69 |
|
| 70 |
x∸x≡0 : ∀ x → x ∸ x ≡ 0ᵇ |
| 71 |
x∸x≡0 zero = refl |
| 72 |
x∸x≡0 2[1+ x ] = x≡0⇒double[x]≡0 (x∸x≡0 x) |
| 73 |
x∸x≡0 1+[2 x ] = x≡0⇒double[x]≡0 (x∸x≡0 x) |
| 74 |
|
| 75 |
toℕ-homo-∸ : ∀ x y → toℕ (x ∸ y) ≡ (toℕ x) ℕ.∸ (toℕ y) |
| 76 |
toℕ-homo-∸ zero zero = refl |
| 77 |
toℕ-homo-∸ zero 2[1+ y ] = refl |
| 78 |
toℕ-homo-∸ zero 1+[2 y ] = refl |
| 79 |
toℕ-homo-∸ 2[1+ x ] zero = refl |
| 80 |
toℕ-homo-∸ 2[1+ x ] 2[1+ y ] = begin |
| 81 |
toℕ (double (x ∸ y)) ≡⟨ toℕ-double (x ∸ y) ⟩ |
| 82 |
2 ℕ.* toℕ (x ∸ y) ≡⟨ cong (2 ℕ.*_) (toℕ-homo-∸ x y) ⟩ |
| 83 |
2 ℕ.* (toℕ x ℕ.∸ toℕ y) ≡⟨ ℕ.*-distribˡ-∸ 2 (ℕ.suc (toℕ x)) (ℕ.suc (toℕ y)) ⟩ |
| 84 |
toℕ 2[1+ x ] ℕ.∸ toℕ 2[1+ y ] ∎ |
| 85 |
where open ≡-Reasoning |
| 86 |
toℕ-homo-∸ 2[1+ x ] 1+[2 y ] with x <? y |
| 87 |
... | yes x<y = sym (ℕ.m≤n⇒m∸n≡0 (toℕ-mono-≤ (inj₁ (even<odd x<y)))) |
| 88 |
... | no x≮y = begin |
| 89 |
ℕ.suc (2 ℕ.* toℕ (x ∸ y)) ≡⟨ cong (ℕ.suc ∘ (2 ℕ.*_)) (toℕ-homo-∸ x y) ⟩ |
| 90 |
ℕ.suc (2 ℕ.* (toℕ x ℕ.∸ toℕ y)) ≡⟨ cong ℕ.suc (ℕ.*-distribˡ-∸ 2 (toℕ x) (toℕ y)) ⟩ |
| 91 |
ℕ.suc (2 ℕ.* toℕ x ℕ.∸ 2 ℕ.* toℕ y) ≡⟨ sym (ℕ.+-∸-assoc 1 (ℕ.*-monoʳ-≤ 2 (toℕ-mono-≤ (≮⇒≥ x≮y)))) ⟩ |
| 92 |
ℕ.suc (2 ℕ.* toℕ x) ℕ.∸ 2 ℕ.* toℕ y ≡⟨ sym (cong (ℕ._∸ 2 ℕ.* toℕ y) (ℕ.+-suc (toℕ x) (1 ℕ.* toℕ x))) ⟩ |
| 93 |
2 ℕ.* (ℕ.suc (toℕ x)) ℕ.∸ ℕ.suc (2 ℕ.* toℕ y) ∎ |
| 94 |
where open ≡-Reasoning |
| 95 |
toℕ-homo-∸ 1+[2 x ] zero = refl |
| 96 |
toℕ-homo-∸ 1+[2 x ] 2[1+ y ] with x ≤? y |
| 97 |
... | yes x≤y = sym (ℕ.m≤n⇒m∸n≡0 (toℕ-mono-≤ (inj₁ (odd<even x≤y)))) |
| 98 |
... | no _ = begin |
| 99 |
toℕ (pred (double (x ∸ y))) ≡⟨ toℕ-pred (double (x ∸ y)) ⟩ |
| 100 |
ℕ.pred (toℕ (double (x ∸ y))) ≡⟨ cong ℕ.pred (toℕ-double (x ∸ y)) ⟩ |
| 101 |
ℕ.pred (2 ℕ.* toℕ (x ∸ y)) ≡⟨ cong (ℕ.pred ∘ (2 ℕ.*_)) (toℕ-homo-∸ x y) ⟩ |
| 102 |
ℕ.pred (2 ℕ.* (toℕ x ℕ.∸ toℕ y)) ≡⟨ cong ℕ.pred (ℕ.*-distribˡ-∸ 2 (toℕ x) (toℕ y)) ⟩ |
| 103 |
ℕ.pred (2 ℕ.* toℕ x ℕ.∸ 2 ℕ.* toℕ y) ≡⟨ ℕ.pred[m∸n]≡m∸[1+n] (2 ℕ.* toℕ x) (2 ℕ.* toℕ y) ⟩ |
| 104 |
2 ℕ.* toℕ x ℕ.∸ ℕ.suc (2 ℕ.* toℕ y) ≡⟨ sym (cong (2 ℕ.* toℕ x ℕ.∸_) (ℕ.+-suc (toℕ y) (1 ℕ.* toℕ y))) ⟩ |
| 105 |
ℕ.suc (2 ℕ.* toℕ x) ℕ.∸ 2 ℕ.* (ℕ.suc (toℕ y)) ∎ |
| 106 |
where open ≡-Reasoning |
| 107 |
toℕ-homo-∸ 1+[2 x ] 1+[2 y ] = begin |
| 108 |
toℕ (double (x ∸ y)) ≡⟨ toℕ-double (x ∸ y) ⟩ |
| 109 |
2 ℕ.* toℕ (x ∸ y) ≡⟨ cong (2 ℕ.*_) (toℕ-homo-∸ x y) ⟩ |
| 110 |
2 ℕ.* (toℕ x ℕ.∸ toℕ y) ≡⟨ ℕ.*-distribˡ-∸ 2 (toℕ x) (toℕ y) ⟩ |
| 111 |
2 ℕ.* toℕ x ℕ.∸ 2 ℕ.* toℕ y ∎ |
| 112 |
where open ≡-Reasoning |
| 113 |
|
| 114 |
fromℕ-homo-∸ : ∀ m n → fromℕ (m ℕ.∸ n) ≡ (fromℕ m) ∸ (fromℕ n) |
| 115 |
fromℕ-homo-∸ = homomorphic₂-inv ∸-magma ℕ.∸-magma |
| 116 |
(cong fromℕ) toℕ-inverseᵇ toℕ-homo-∸ |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- Properties of _∸_ and _≤_/_<_ |
| 120 |
|
| 121 |
even∸odd-for≥ : x ≥ y → 2[1+ x ] ∸ 1+[2 y ] ≡ 1+[2 (x ∸ y) ] |
| 122 |
even∸odd-for≥ {x} {y} x≥y with x <? y |
| 123 |
... | no _ = refl |
| 124 |
... | yes x<y = contradiction x≥y (<⇒≱ x<y) |
| 125 |
|
| 126 |
odd∸even-for> : x > y → 1+[2 x ] ∸ 2[1+ y ] ≡ pred (double (x ∸ y)) |
| 127 |
odd∸even-for> {x} {y} x>y with x ≤? y |
| 128 |
... | no _ = refl |
| 129 |
... | yes x≤y = contradiction x>y (≤⇒≯ x≤y) |
| 130 |
|
| 131 |
x≤y⇒x∸y≡0 : x ≤ y → x ∸ y ≡ 0ᵇ |
| 132 |
x≤y⇒x∸y≡0 {x} {y} = toℕ-injective ∘ trans (toℕ-homo-∸ x y) ∘ ℕ.m≤n⇒m∸n≡0 ∘ toℕ-mono-≤ |
| 133 |
|
| 134 |
x∸y≡0⇒x≤y : x ∸ y ≡ 0ᵇ → x ≤ y |
| 135 |
x∸y≡0⇒x≤y {x} {y} = toℕ-cancel-≤ ∘ ℕ.m∸n≡0⇒m≤n ∘ trans (sym (toℕ-homo-∸ x y)) ∘ cong toℕ |
| 136 |
|
| 137 |
x<y⇒y∸x>0 : x < y → y ∸ x > 0ᵇ |
| 138 |
x<y⇒y∸x>0 {x} {y} = toℕ-cancel-< ∘ subst (ℕ._> 0) (sym (toℕ-homo-∸ y x)) ∘ ℕ.m<n⇒0<n∸m ∘ toℕ-mono-< |
| 139 |
|
| 140 |
------------------------------------------------------------------------ |
| 141 |
-- Properties of _∸_ and _+_ |
| 142 |
|
| 143 |
[x∸y]+y≡x : x ≥ y → (x ∸ y) + y ≡ x |
| 144 |
[x∸y]+y≡x {x} {y} x≥y = toℕ-injective (begin |
| 145 |
toℕ (x ∸ y + y) ≡⟨ toℕ-homo-+ (x ∸ y) y ⟩ |
| 146 |
toℕ (x ∸ y) ℕ.+ toℕ y ≡⟨ cong (ℕ._+ toℕ y) (toℕ-homo-∸ x y) ⟩ |
| 147 |
(toℕ x ℕ.∸ toℕ y) ℕ.+ toℕ y ≡⟨ ℕ.m∸n+n≡m (toℕ-mono-≤ x≥y) ⟩ |
| 148 |
toℕ x ∎) |
| 149 |
where open ≡-Reasoning |
| 150 |
|
| 151 |
x+y∸y≡x : ∀ x y → (x + y) ∸ y ≡ x |
| 152 |
x+y∸y≡x x y = +-cancelʳ-≡ _ _ x ([x∸y]+y≡x (x≤y+x y x)) |
| 153 |
|
| 154 |
[x+y]∸x≡y : ∀ x y → (x + y) ∸ x ≡ y |
| 155 |
[x+y]∸x≡y x y = trans (cong (_∸ x) (+-comm x y)) (x+y∸y≡x y x) |
| 156 |
|
| 157 |
x+[y∸x]≡y : x ≤ y → x + (y ∸ x) ≡ y |
| 158 |
x+[y∸x]≡y {x} {y} x≤y = begin-equality |
| 159 |
x + (y ∸ x) ≡⟨ +-comm x _ ⟩ |
| 160 |
(y ∸ x) + x ≡⟨ [x∸y]+y≡x x≤y ⟩ |
| 161 |
y ∎ |
| 162 |
where open ≤-Reasoning |
| 163 |
|
| 164 |
∸-+-assoc : ∀ x y z → (x ∸ y) ∸ z ≡ x ∸ (y + z) |
| 165 |
∸-+-assoc x y z = toℕ-injective $ begin |
| 166 |
toℕ ((x ∸ y) ∸ z) ≡⟨ toℕ-homo-∸ (x ∸ y) z ⟩ |
| 167 |
toℕ (x ∸ y) ℕ.∸ n ≡⟨ cong (ℕ._∸ n) (toℕ-homo-∸ x y) ⟩ |
| 168 |
(k ℕ.∸ m) ℕ.∸ n ≡⟨ ℕ.∸-+-assoc k m n ⟩ |
| 169 |
k ℕ.∸ (m ℕ.+ n) ≡⟨ cong (k ℕ.∸_) (sym (toℕ-homo-+ y z)) ⟩ |
| 170 |
k ℕ.∸ (toℕ (y + z)) ≡⟨ sym (toℕ-homo-∸ x (y + z)) ⟩ |
| 171 |
toℕ (x ∸ (y + z)) ∎ |
| 172 |
where open ≡-Reasoning; k = toℕ x; m = toℕ y; n = toℕ z |
| 173 |
|
| 174 |
+-∸-assoc : ∀ x {y z} → z ≤ y → (x + y) ∸ z ≡ x + (y ∸ z) |
| 175 |
+-∸-assoc x {y} {z} z≤y = toℕ-injective $ begin |
| 176 |
toℕ ((x + y) ∸ z) ≡⟨ toℕ-homo-∸ (x + y) z ⟩ |
| 177 |
(toℕ (x + y)) ℕ.∸ n ≡⟨ cong (ℕ._∸ n) (toℕ-homo-+ x y) ⟩ |
| 178 |
(k ℕ.+ m) ℕ.∸ n ≡⟨ ℕ.+-∸-assoc k n≤m ⟩ |
| 179 |
k ℕ.+ (m ℕ.∸ n) ≡⟨ cong (k ℕ.+_) (sym (toℕ-homo-∸ y z)) ⟩ |
| 180 |
k ℕ.+ toℕ (y ∸ z) ≡⟨ sym (toℕ-homo-+ x (y ∸ z)) ⟩ |
| 181 |
toℕ (x + (y ∸ z)) ∎ |
| 182 |
where |
| 183 |
open ≡-Reasoning; k = toℕ x; m = toℕ y; n = toℕ z; n≤m = toℕ-mono-≤ z≤y |
| 184 |
|
| 185 |
x+y∸x+z≡y∸z : ∀ x y z → (x + y) ∸ (x + z) ≡ y ∸ z |
| 186 |
x+y∸x+z≡y∸z x y z = begin-equality |
| 187 |
(x + y) ∸ (x + z) ≡⟨ sym (∸-+-assoc (x + y) x z) ⟩ |
| 188 |
((x + y) ∸ x) ∸ z ≡⟨ cong (_∸ z) ([x+y]∸x≡y x y) ⟩ |
| 189 |
y ∸ z ∎ |
| 190 |
where open ≤-Reasoning |
| 191 |
|
| 192 |
suc[x]∸suc[y] : ∀ x y → suc x ∸ suc y ≡ x ∸ y |
| 193 |
suc[x]∸suc[y] x y = begin-equality |
| 194 |
suc x ∸ suc y ≡⟨ cong₂ _∸_ (suc≗1+ x) (suc≗1+ y) ⟩ |
| 195 |
(1ᵇ + x) ∸ (1ᵇ + y) ≡⟨ x+y∸x+z≡y∸z 1ᵇ x y ⟩ |
| 196 |
x ∸ y ∎ |
| 197 |
where open ≤-Reasoning |
| 198 |
|
| 199 |
∸-mono-≤ : _∸_ Preserves₂ _≤_ ⟶ _≥_ ⟶ _≤_ |
| 200 |
∸-mono-≤ {x} {y} {u} {v} x≤y v≤u = toℕ-cancel-≤ (begin |
| 201 |
toℕ (x ∸ u) ≡⟨ toℕ-homo-∸ x u ⟩ |
| 202 |
toℕ x ℕ.∸ toℕ u ≤⟨ ℕ.∸-mono (toℕ-mono-≤ x≤y) (toℕ-mono-≤ v≤u) ⟩ |
| 203 |
toℕ y ℕ.∸ toℕ v ≡⟨ sym (toℕ-homo-∸ y v) ⟩ |
| 204 |
toℕ (y ∸ v) ∎) |
| 205 |
where open ℕ.≤-Reasoning |
| 206 |
|
| 207 |
∸-monoˡ-≤ : (x : ℕᵇ) → (_∸ x) Preserves _≤_ ⟶ _≤_ |
| 208 |
∸-monoˡ-≤ x y≤z = ∸-mono-≤ y≤z (≤-refl {x}) |
| 209 |
|
| 210 |
∸-monoʳ-≤ : (x : ℕᵇ) → (x ∸_) Preserves _≥_ ⟶ _≤_ |
| 211 |
∸-monoʳ-≤ x y≥z = ∸-mono-≤ (≤-refl {x}) y≥z |
| 212 |
|
| 213 |
x∸y≤x : ∀ x y → x ∸ y ≤ x |
| 214 |
x∸y≤x x y = begin |
| 215 |
x ∸ y ≤⟨ ∸-monoʳ-≤ x (0≤x y) ⟩ |
| 216 |
x ∸ 0ᵇ ≡⟨ x∸0≡x x ⟩ |
| 217 |
x ∎ |
| 218 |
where open ≤-Reasoning |
| 219 |
|
| 220 |
x∸y<x : {x y : ℕᵇ} → x ≢ 0ᵇ → y ≢ 0ᵇ → x ∸ y < x |
| 221 |
x∸y<x {x} {y} x≢0 y≢0 = begin-strict |
| 222 |
x ∸ y ≡⟨ cong₂ _∸_ (sym (suc-pred x≢0)) (sym (suc-pred y≢0)) ⟩ |
| 223 |
suc px ∸ suc py ≡⟨ suc[x]∸suc[y] px py ⟩ |
| 224 |
px ∸ py ≤⟨ x∸y≤x px py ⟩ |
| 225 |
px <⟨ pred[x]<x x≢0 ⟩ |
| 226 |
x ∎ |
| 227 |
where open ≤-Reasoning; px = pred x; py = pred y |
| 228 |
|
| 229 |
------------------------------------------------------------------------ |
| 230 |
-- Properties of _∸_ and _*_ |
| 231 |
|
| 232 |
*-distribˡ-∸ : _*_ DistributesOverˡ _∸_ |
| 233 |
*-distribˡ-∸ x y z = toℕ-injective $ begin |
| 234 |
toℕ (x * (y ∸ z)) ≡⟨ toℕ-homo-* x (y ∸ z) ⟩ |
| 235 |
k ℕ.* (toℕ (y ∸ z)) ≡⟨ cong (k ℕ.*_) (toℕ-homo-∸ y z) ⟩ |
| 236 |
k ℕ.* (m ℕ.∸ n) ≡⟨ ℕ.*-distribˡ-∸ k m n ⟩ |
| 237 |
(k ℕ.* m) ℕ.∸ (k ℕ.* n) ≡⟨ cong₂ ℕ._∸_ (sym (toℕ-homo-* x y)) (sym (toℕ-homo-* x z)) ⟩ |
| 238 |
toℕ (x * y) ℕ.∸ toℕ (x * z) ≡⟨ sym (toℕ-homo-∸ (x * y) (x * z)) ⟩ |
| 239 |
toℕ ((x * y) ∸ (x * z)) ∎ |
| 240 |
where open ≡-Reasoning; k = toℕ x; m = toℕ y; n = toℕ z |
| 241 |
|
| 242 |
*-distribʳ-∸ : _*_ DistributesOverʳ _∸_ |
| 243 |
*-distribʳ-∸ = comm∧distrˡ⇒distrʳ *-comm *-distribˡ-∸ |
| 244 |
|
| 245 |
*-distrib-∸ : _*_ DistributesOver _∸_ |
| 246 |
*-distrib-∸ = *-distribˡ-∸ , *-distribʳ-∸ |