| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Basic properties of ℕᵇ |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Nat.Binary.Properties where |
| 10 |
|
| 11 |
open import Algebra.Bundles |
| 12 |
using (Magma; Monoid; CommutativeMonoid; Semigroup; CommutativeSemigroup |
| 13 |
; Semiring; CommutativeSemiring) |
| 14 |
open import Algebra.Morphism.Structures |
| 15 |
import Algebra.Morphism.MonoidMonomorphism as MonoidMonomorphism |
| 16 |
open import Algebra.Consequences.Propositional using (comm∧distrˡ⇒distrʳ) |
| 17 |
open import Data.Bool.Base using (if_then_else_; Bool; true; false) |
| 18 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 19 |
open import Data.Nat.Binary.Base |
| 20 |
open import Data.Nat.Base as ℕ using (ℕ; z≤n; s≤s; s<s⁻¹) |
| 21 |
open import Data.Nat.DivMod using (_%_; _/_; m/n≤m; +-distrib-/-∣ˡ) |
| 22 |
open import Data.Nat.Divisibility using (∣-refl) |
| 23 |
import Data.Nat.Properties as ℕ |
| 24 |
open import Data.Nat.Solver using (module +-*-Solver) |
| 25 |
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂; ∃) |
| 26 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂) |
| 27 |
open import Function.Base using (_∘_; _$_; id) |
| 28 |
open import Function.Definitions using (Injective; Surjective; |
| 29 |
Inverseˡ; Inverseʳ; Inverseᵇ) |
| 30 |
open import Function.Consequences.Propositional |
| 31 |
using (strictlySurjective⇒surjective; strictlyInverseˡ⇒inverseˡ |
| 32 |
; strictlyInverseʳ⇒inverseʳ) |
| 33 |
open import Level using (0ℓ) |
| 34 |
open import Relation.Binary |
| 35 |
open import Relation.Binary.Consequences using (trans∧irr⇒asym; tri⇒dec<) |
| 36 |
open import Relation.Binary.Morphism |
| 37 |
using (IsRelHomomorphism; IsOrderHomomorphism; IsOrderMonomorphism) |
| 38 |
import Relation.Binary.Morphism.OrderMonomorphism as OrderMonomorphism |
| 39 |
open import Relation.Binary.PropositionalEquality.Algebra |
| 40 |
using (magma; isMagma) |
| 41 |
open import Relation.Binary.PropositionalEquality.Core |
| 42 |
using (_≡_; _≢_; refl; cong; cong₂; sym; _≗_; trans; ≢-sym; subst₂; |
| 43 |
subst; resp₂) |
| 44 |
open import Relation.Binary.PropositionalEquality.Properties |
| 45 |
using (isDecEquivalence; setoid; decSetoid; module ≡-Reasoning; |
| 46 |
isEquivalence) |
| 47 |
open import Relation.Nullary using (¬_; yes; no) |
| 48 |
import Relation.Nullary.Decidable as Dec |
| 49 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 50 |
|
| 51 |
open import Algebra.Definitions {A = ℕᵇ} _≡_ |
| 52 |
open import Algebra.Structures {A = ℕᵇ} _≡_ |
| 53 |
import Algebra.Properties.CommutativeSemigroup as CommSemigProp |
| 54 |
import Algebra.Properties.CommutativeSemigroup ℕ.+-commutativeSemigroup |
| 55 |
as ℕ-+-semigroupProperties |
| 56 |
import Relation.Binary.Construct.StrictToNonStrict _≡_ _<_ |
| 57 |
as StrictToNonStrict |
| 58 |
open +-*-Solver |
| 59 |
|
| 60 |
private |
| 61 |
variable |
| 62 |
x : ℕᵇ |
| 63 |
|
| 64 |
infix 4 _<?_ _≟_ _≤?_ |
| 65 |
|
| 66 |
------------------------------------------------------------------------ |
| 67 |
-- Properties of _≡_ |
| 68 |
------------------------------------------------------------------------ |
| 69 |
|
| 70 |
2[1+x]≢0 : 2[1+ x ] ≢ 0ᵇ |
| 71 |
2[1+x]≢0 () |
| 72 |
|
| 73 |
1+[2x]≢0 : 1+[2 x ] ≢ 0ᵇ |
| 74 |
1+[2x]≢0 () |
| 75 |
|
| 76 |
2[1+_]-injective : Injective _≡_ _≡_ 2[1+_] |
| 77 |
2[1+_]-injective refl = refl |
| 78 |
|
| 79 |
1+[2_]-injective : Injective _≡_ _≡_ 1+[2_] |
| 80 |
1+[2_]-injective refl = refl |
| 81 |
|
| 82 |
_≟_ : DecidableEquality ℕᵇ |
| 83 |
zero ≟ zero = yes refl |
| 84 |
zero ≟ 2[1+ _ ] = no λ() |
| 85 |
zero ≟ 1+[2 _ ] = no λ() |
| 86 |
2[1+ _ ] ≟ zero = no λ() |
| 87 |
2[1+ x ] ≟ 2[1+ y ] = Dec.map′ (cong 2[1+_]) 2[1+_]-injective (x ≟ y) |
| 88 |
2[1+ _ ] ≟ 1+[2 _ ] = no λ() |
| 89 |
1+[2 _ ] ≟ zero = no λ() |
| 90 |
1+[2 _ ] ≟ 2[1+ _ ] = no λ() |
| 91 |
1+[2 x ] ≟ 1+[2 y ] = Dec.map′ (cong 1+[2_]) 1+[2_]-injective (x ≟ y) |
| 92 |
|
| 93 |
≡-isDecEquivalence : IsDecEquivalence {A = ℕᵇ} _≡_ |
| 94 |
≡-isDecEquivalence = isDecEquivalence _≟_ |
| 95 |
|
| 96 |
≡-setoid : Setoid 0ℓ 0ℓ |
| 97 |
≡-setoid = setoid ℕᵇ |
| 98 |
|
| 99 |
≡-decSetoid : DecSetoid 0ℓ 0ℓ |
| 100 |
≡-decSetoid = decSetoid _≟_ |
| 101 |
|
| 102 |
------------------------------------------------------------------------ |
| 103 |
-- Properties of toℕ & fromℕ |
| 104 |
------------------------------------------------------------------------ |
| 105 |
|
| 106 |
toℕ-double : ∀ x → toℕ (double x) ≡ 2 ℕ.* (toℕ x) |
| 107 |
toℕ-double zero = refl |
| 108 |
toℕ-double 1+[2 x ] = cong ((2 ℕ.*_) ∘ ℕ.suc) (toℕ-double x) |
| 109 |
toℕ-double 2[1+ x ] = cong (2 ℕ.*_) (sym (ℕ.*-distribˡ-+ 2 1 (toℕ x))) |
| 110 |
|
| 111 |
toℕ-suc : ∀ x → toℕ (suc x) ≡ ℕ.suc (toℕ x) |
| 112 |
toℕ-suc zero = refl |
| 113 |
toℕ-suc 2[1+ x ] = cong (ℕ.suc ∘ (2 ℕ.*_)) (toℕ-suc x) |
| 114 |
toℕ-suc 1+[2 x ] = ℕ.*-distribˡ-+ 2 1 (toℕ x) |
| 115 |
|
| 116 |
toℕ-pred : ∀ x → toℕ (pred x) ≡ ℕ.pred (toℕ x) |
| 117 |
toℕ-pred zero = refl |
| 118 |
toℕ-pred 2[1+ x ] = cong ℕ.pred $ sym $ ℕ.*-distribˡ-+ 2 1 (toℕ x) |
| 119 |
toℕ-pred 1+[2 x ] = toℕ-double x |
| 120 |
|
| 121 |
toℕ-fromℕ' : toℕ ∘ fromℕ' ≗ id |
| 122 |
toℕ-fromℕ' 0 = refl |
| 123 |
toℕ-fromℕ' (ℕ.suc n) = begin |
| 124 |
toℕ (fromℕ' (ℕ.suc n)) ≡⟨⟩ |
| 125 |
toℕ (suc (fromℕ' n)) ≡⟨ toℕ-suc (fromℕ' n) ⟩ |
| 126 |
ℕ.suc (toℕ (fromℕ' n)) ≡⟨ cong ℕ.suc (toℕ-fromℕ' n) ⟩ |
| 127 |
ℕ.suc n ∎ |
| 128 |
where open ≡-Reasoning |
| 129 |
|
| 130 |
fromℕ≡fromℕ' : fromℕ ≗ fromℕ' |
| 131 |
fromℕ≡fromℕ' n = fromℕ-helper≡fromℕ' n n ℕ.≤-refl |
| 132 |
where |
| 133 |
split : ℕᵇ → Maybe Bool × ℕᵇ |
| 134 |
split zero = nothing , zero |
| 135 |
split 2[1+ n ] = just false , n |
| 136 |
split 1+[2 n ] = just true , n |
| 137 |
|
| 138 |
head = proj₁ ∘ split |
| 139 |
tail = proj₂ ∘ split |
| 140 |
|
| 141 |
split-injective : Injective _≡_ _≡_ split |
| 142 |
split-injective {zero} {zero} refl = refl |
| 143 |
split-injective {2[1+ _ ]} {2[1+ _ ]} refl = refl |
| 144 |
split-injective {1+[2 _ ]} {1+[2 _ ]} refl = refl |
| 145 |
|
| 146 |
split-if : ∀ x xs → split (if x then 1+[2 xs ] else 2[1+ xs ]) ≡ (just x , xs) |
| 147 |
split-if false xs = refl |
| 148 |
split-if true xs = refl |
| 149 |
|
| 150 |
head-suc : ∀ n → head (suc (suc (suc n))) ≡ head (suc n) |
| 151 |
head-suc zero = refl |
| 152 |
head-suc 2[1+ n ] = refl |
| 153 |
head-suc 1+[2 n ] = refl |
| 154 |
|
| 155 |
tail-suc : ∀ n → suc (tail (suc n)) ≡ tail (suc (suc (suc n))) |
| 156 |
tail-suc zero = refl |
| 157 |
tail-suc 2[1+ n ] = refl |
| 158 |
tail-suc 1+[2 n ] = refl |
| 159 |
|
| 160 |
head-homo : ∀ n → head (suc (fromℕ' n)) ≡ just (n % 2 ℕ.≡ᵇ 0) |
| 161 |
head-homo ℕ.zero = refl |
| 162 |
head-homo (ℕ.suc ℕ.zero) = refl |
| 163 |
head-homo (ℕ.suc (ℕ.suc n)) = trans (head-suc (fromℕ' n)) (head-homo n) |
| 164 |
|
| 165 |
open ≡-Reasoning |
| 166 |
|
| 167 |
tail-homo : ∀ n → tail (suc (fromℕ' n)) ≡ fromℕ' (n / 2) |
| 168 |
tail-homo ℕ.zero = refl |
| 169 |
tail-homo (ℕ.suc ℕ.zero) = refl |
| 170 |
tail-homo (ℕ.suc (ℕ.suc n)) = begin |
| 171 |
tail (suc (fromℕ' (ℕ.suc (ℕ.suc n)))) ≡⟨ tail-suc (fromℕ' n) ⟨ |
| 172 |
suc (tail (suc (fromℕ' n))) ≡⟨ cong suc (tail-homo n) ⟩ |
| 173 |
fromℕ' (ℕ.suc (n / 2)) ≡⟨ cong fromℕ' (+-distrib-/-∣ˡ {2} n ∣-refl) ⟨ |
| 174 |
fromℕ' (ℕ.suc (ℕ.suc n) / 2) ∎ |
| 175 |
|
| 176 |
fromℕ-helper≡fromℕ' : ∀ n w → n ℕ.≤ w → fromℕ.helper n n w ≡ fromℕ' n |
| 177 |
fromℕ-helper≡fromℕ' ℕ.zero w p = refl |
| 178 |
fromℕ-helper≡fromℕ' (ℕ.suc n) (ℕ.suc w) (s≤s n≤w) = |
| 179 |
split-injective (begin |
| 180 |
split (fromℕ.helper n (ℕ.suc n) (ℕ.suc w)) ≡⟨ split-if _ _ ⟩ |
| 181 |
just (n % 2 ℕ.≡ᵇ 0) , fromℕ.helper n (n / 2) w ≡⟨ cong (_ ,_) rec-n/2 ⟩ |
| 182 |
just (n % 2 ℕ.≡ᵇ 0) , fromℕ' (n / 2) ≡⟨ cong₂ _,_ (head-homo n) (tail-homo n) ⟨ |
| 183 |
head (fromℕ' (ℕ.suc n)) , tail (fromℕ' (ℕ.suc n)) ≡⟨⟩ |
| 184 |
split (fromℕ' (ℕ.suc n)) ∎) |
| 185 |
where rec-n/2 = fromℕ-helper≡fromℕ' (n / 2) w (ℕ.≤-trans (m/n≤m n 2) n≤w) |
| 186 |
|
| 187 |
toℕ-fromℕ : toℕ ∘ fromℕ ≗ id |
| 188 |
toℕ-fromℕ n rewrite fromℕ≡fromℕ' n = toℕ-fromℕ' n |
| 189 |
|
| 190 |
toℕ-injective : Injective _≡_ _≡_ toℕ |
| 191 |
toℕ-injective {zero} {zero} _ = refl |
| 192 |
toℕ-injective {2[1+ x ]} {2[1+ y ]} 2[1+xN]≡2[1+yN] = cong 2[1+_] x≡y |
| 193 |
where |
| 194 |
1+xN≡1+yN = ℕ.*-cancelˡ-≡ (ℕ.suc _) (ℕ.suc _) 2 2[1+xN]≡2[1+yN] |
| 195 |
xN≡yN = cong ℕ.pred 1+xN≡1+yN |
| 196 |
x≡y = toℕ-injective xN≡yN |
| 197 |
|
| 198 |
toℕ-injective {2[1+ x ]} {1+[2 y ]} 2[1+xN]≡1+2yN = |
| 199 |
contradiction 2[1+xN]≡1+2yN (ℕ.even≢odd (ℕ.suc (toℕ x)) (toℕ y)) |
| 200 |
|
| 201 |
toℕ-injective {1+[2 x ]} {2[1+ y ]} 1+2xN≡2[1+yN] = |
| 202 |
contradiction (sym 1+2xN≡2[1+yN]) (ℕ.even≢odd (ℕ.suc (toℕ y)) (toℕ x)) |
| 203 |
|
| 204 |
toℕ-injective {1+[2 x ]} {1+[2 y ]} 1+2xN≡1+2yN = cong 1+[2_] x≡y |
| 205 |
where |
| 206 |
2xN≡2yN = cong ℕ.pred 1+2xN≡1+2yN |
| 207 |
xN≡yN = ℕ.*-cancelˡ-≡ _ _ 2 2xN≡2yN |
| 208 |
x≡y = toℕ-injective xN≡yN |
| 209 |
|
| 210 |
toℕ-surjective : Surjective _≡_ _≡_ toℕ |
| 211 |
toℕ-surjective = strictlySurjective⇒surjective (λ n → fromℕ n , toℕ-fromℕ n) |
| 212 |
|
| 213 |
toℕ-isRelHomomorphism : IsRelHomomorphism _≡_ _≡_ toℕ |
| 214 |
toℕ-isRelHomomorphism = record |
| 215 |
{ cong = cong toℕ |
| 216 |
} |
| 217 |
|
| 218 |
fromℕ-injective : Injective _≡_ _≡_ fromℕ |
| 219 |
fromℕ-injective {x} {y} f[x]≡f[y] = begin |
| 220 |
x ≡⟨ sym (toℕ-fromℕ x) ⟩ |
| 221 |
toℕ (fromℕ x) ≡⟨ cong toℕ f[x]≡f[y] ⟩ |
| 222 |
toℕ (fromℕ y) ≡⟨ toℕ-fromℕ y ⟩ |
| 223 |
y ∎ |
| 224 |
where open ≡-Reasoning |
| 225 |
|
| 226 |
fromℕ-toℕ : fromℕ ∘ toℕ ≗ id |
| 227 |
fromℕ-toℕ = toℕ-injective ∘ toℕ-fromℕ ∘ toℕ |
| 228 |
|
| 229 |
toℕ-inverseˡ : Inverseˡ _≡_ _≡_ toℕ fromℕ |
| 230 |
toℕ-inverseˡ = strictlyInverseˡ⇒inverseˡ {f⁻¹ = fromℕ} toℕ toℕ-fromℕ |
| 231 |
|
| 232 |
toℕ-inverseʳ : Inverseʳ _≡_ _≡_ toℕ fromℕ |
| 233 |
toℕ-inverseʳ = strictlyInverseʳ⇒inverseʳ toℕ fromℕ-toℕ |
| 234 |
|
| 235 |
toℕ-inverseᵇ : Inverseᵇ _≡_ _≡_ toℕ fromℕ |
| 236 |
toℕ-inverseᵇ = toℕ-inverseˡ , toℕ-inverseʳ |
| 237 |
|
| 238 |
fromℕ-pred : ∀ n → fromℕ (ℕ.pred n) ≡ pred (fromℕ n) |
| 239 |
fromℕ-pred n = begin |
| 240 |
fromℕ (ℕ.pred n) ≡⟨ cong (fromℕ ∘ ℕ.pred) (sym (toℕ-fromℕ n)) ⟩ |
| 241 |
fromℕ (ℕ.pred (toℕ y)) ≡⟨ cong fromℕ (sym (toℕ-pred y)) ⟩ |
| 242 |
fromℕ (toℕ (pred y)) ≡⟨ fromℕ-toℕ (pred y) ⟩ |
| 243 |
pred y ≡⟨ refl ⟩ |
| 244 |
pred (fromℕ n) ∎ |
| 245 |
where open ≡-Reasoning; y = fromℕ n |
| 246 |
|
| 247 |
x≡0⇒toℕ[x]≡0 : x ≡ zero → toℕ x ≡ 0 |
| 248 |
x≡0⇒toℕ[x]≡0 {zero} _ = refl |
| 249 |
|
| 250 |
toℕ[x]≡0⇒x≡0 : toℕ x ≡ 0 → x ≡ zero |
| 251 |
toℕ[x]≡0⇒x≡0 {zero} _ = refl |
| 252 |
|
| 253 |
------------------------------------------------------------------------ |
| 254 |
-- Properties of _<_ |
| 255 |
------------------------------------------------------------------------ |
| 256 |
-- Basic properties |
| 257 |
|
| 258 |
x≮0 : x ≮ zero |
| 259 |
x≮0 () |
| 260 |
|
| 261 |
x≢0⇒x>0 : x ≢ zero → x > zero |
| 262 |
x≢0⇒x>0 {zero} 0≢0 = contradiction refl 0≢0 |
| 263 |
x≢0⇒x>0 {2[1+ _ ]} _ = 0<even |
| 264 |
x≢0⇒x>0 {1+[2 _ ]} _ = 0<odd |
| 265 |
|
| 266 |
1+[2x]<2[1+x] : ∀ x → 1+[2 x ] < 2[1+ x ] |
| 267 |
1+[2x]<2[1+x] x = odd<even (inj₂ refl) |
| 268 |
|
| 269 |
<⇒≢ : _<_ ⇒ _≢_ |
| 270 |
<⇒≢ (even<even x<x) refl = <⇒≢ x<x refl |
| 271 |
<⇒≢ (odd<odd x<x) refl = <⇒≢ x<x refl |
| 272 |
|
| 273 |
>⇒≢ : _>_ ⇒ _≢_ |
| 274 |
>⇒≢ y<x = ≢-sym (<⇒≢ y<x) |
| 275 |
|
| 276 |
≡⇒≮ : _≡_ ⇒ _≮_ |
| 277 |
≡⇒≮ x≡y x<y = <⇒≢ x<y x≡y |
| 278 |
|
| 279 |
≡⇒≯ : _≡_ ⇒ _≯_ |
| 280 |
≡⇒≯ x≡y x>y = >⇒≢ x>y x≡y |
| 281 |
|
| 282 |
<⇒≯ : _<_ ⇒ _≯_ |
| 283 |
<⇒≯ (even<even x<y) (even<even y<x) = <⇒≯ x<y y<x |
| 284 |
<⇒≯ (even<odd x<y) (odd<even (inj₁ y<x)) = <⇒≯ x<y y<x |
| 285 |
<⇒≯ (even<odd x<y) (odd<even (inj₂ refl)) = <⇒≢ x<y refl |
| 286 |
<⇒≯ (odd<even (inj₁ x<y)) (even<odd y<x) = <⇒≯ x<y y<x |
| 287 |
<⇒≯ (odd<even (inj₂ refl)) (even<odd y<x) = <⇒≢ y<x refl |
| 288 |
<⇒≯ (odd<odd x<y) (odd<odd y<x) = <⇒≯ x<y y<x |
| 289 |
|
| 290 |
>⇒≮ : _>_ ⇒ _≮_ |
| 291 |
>⇒≮ y<x = <⇒≯ y<x |
| 292 |
|
| 293 |
<⇒≤ : _<_ ⇒ _≤_ |
| 294 |
<⇒≤ = inj₁ |
| 295 |
|
| 296 |
------------------------------------------------------------------------ |
| 297 |
-- Properties of _<_ and toℕ & fromℕ. |
| 298 |
|
| 299 |
toℕ-mono-< : toℕ Preserves _<_ ⟶ ℕ._<_ |
| 300 |
toℕ-mono-< {zero} {2[1+ _ ]} _ = ℕ.0<1+n |
| 301 |
toℕ-mono-< {zero} {1+[2 _ ]} _ = ℕ.0<1+n |
| 302 |
toℕ-mono-< {2[1+ x ]} {2[1+ y ]} (even<even x<y) = begin |
| 303 |
ℕ.suc (2 ℕ.* (ℕ.suc xN)) ≤⟨ ℕ.+-monoʳ-≤ 1 (ℕ.*-monoʳ-≤ 2 xN<yN) ⟩ |
| 304 |
ℕ.suc (2 ℕ.* yN) ≤⟨ ℕ.n≤1+n _ ⟩ |
| 305 |
2 ℕ.+ (2 ℕ.* yN) ≡⟨ sym (ℕ.*-distribˡ-+ 2 1 yN) ⟩ |
| 306 |
2 ℕ.* (ℕ.suc yN) ∎ |
| 307 |
where open ℕ.≤-Reasoning; xN = toℕ x; yN = toℕ y; xN<yN = toℕ-mono-< x<y |
| 308 |
toℕ-mono-< {2[1+ x ]} {1+[2 y ]} (even<odd x<y) = |
| 309 |
ℕ.+-monoʳ-≤ 1 (ℕ.*-monoʳ-≤ 2 (toℕ-mono-< x<y)) |
| 310 |
toℕ-mono-< {1+[2 x ]} {2[1+ y ]} (odd<even (inj₁ x<y)) = begin |
| 311 |
ℕ.suc (ℕ.suc (2 ℕ.* xN)) ≡⟨⟩ |
| 312 |
2 ℕ.+ (2 ℕ.* xN) ≡⟨ sym (ℕ.*-distribˡ-+ 2 1 xN) ⟩ |
| 313 |
2 ℕ.* (ℕ.suc xN) ≤⟨ ℕ.*-monoʳ-≤ 2 xN<yN ⟩ |
| 314 |
2 ℕ.* yN ≤⟨ ℕ.*-monoʳ-≤ 2 (ℕ.n≤1+n _) ⟩ |
| 315 |
2 ℕ.* (ℕ.suc yN) ∎ |
| 316 |
where open ℕ.≤-Reasoning; xN = toℕ x; yN = toℕ y; xN<yN = toℕ-mono-< x<y |
| 317 |
toℕ-mono-< {1+[2 x ]} {2[1+ .x ]} (odd<even (inj₂ refl)) = |
| 318 |
ℕ.≤-reflexive (sym (ℕ.*-distribˡ-+ 2 1 (toℕ x))) |
| 319 |
toℕ-mono-< {1+[2 x ]} {1+[2 y ]} (odd<odd x<y) = ℕ.+-monoʳ-< 1 (ℕ.*-monoʳ-< 2 xN<yN) |
| 320 |
where xN = toℕ x; yN = toℕ y; xN<yN = toℕ-mono-< x<y |
| 321 |
|
| 322 |
toℕ-cancel-< : ∀ {x y} → toℕ x ℕ.< toℕ y → x < y |
| 323 |
toℕ-cancel-< {zero} {2[1+ y ]} x<y = 0<even |
| 324 |
toℕ-cancel-< {zero} {1+[2 y ]} x<y = 0<odd |
| 325 |
toℕ-cancel-< {2[1+ x ]} {2[1+ y ]} x<y = |
| 326 |
even<even (toℕ-cancel-< (s<s⁻¹ (ℕ.*-cancelˡ-< 2 _ _ x<y))) |
| 327 |
toℕ-cancel-< {2[1+ x ]} {1+[2 y ]} x<y |
| 328 |
rewrite ℕ.*-distribˡ-+ 2 1 (toℕ x) = |
| 329 |
even<odd (toℕ-cancel-< (ℕ.*-cancelˡ-< 2 _ _ (ℕ.≤-trans (s≤s (ℕ.n≤1+n _)) (s<s⁻¹ x<y)))) |
| 330 |
toℕ-cancel-< {1+[2 x ]} {2[1+ y ]} x<y with toℕ x ℕ.≟ toℕ y |
| 331 |
... | yes x≡y = odd<even (inj₂ (toℕ-injective x≡y)) |
| 332 |
... | no x≢y |
| 333 |
rewrite ℕ.+-suc (toℕ y) (toℕ y ℕ.+ 0) = |
| 334 |
odd<even (inj₁ (toℕ-cancel-< (ℕ.≤∧≢⇒< (ℕ.*-cancelˡ-≤ 2 (ℕ.+-cancelˡ-≤ 2 _ _ x<y)) x≢y))) |
| 335 |
toℕ-cancel-< {1+[2 x ]} {1+[2 y ]} x<y = |
| 336 |
odd<odd (toℕ-cancel-< (ℕ.*-cancelˡ-< 2 _ _ (s<s⁻¹ x<y))) |
| 337 |
|
| 338 |
fromℕ-cancel-< : ∀ {x y} → fromℕ x < fromℕ y → x ℕ.< y |
| 339 |
fromℕ-cancel-< = subst₂ ℕ._<_ (toℕ-fromℕ _) (toℕ-fromℕ _) ∘ toℕ-mono-< |
| 340 |
|
| 341 |
fromℕ-mono-< : fromℕ Preserves ℕ._<_ ⟶ _<_ |
| 342 |
fromℕ-mono-< = toℕ-cancel-< ∘ subst₂ ℕ._<_ (sym (toℕ-fromℕ _)) (sym (toℕ-fromℕ _)) |
| 343 |
|
| 344 |
toℕ-isHomomorphism-< : IsOrderHomomorphism _≡_ _≡_ _<_ ℕ._<_ toℕ |
| 345 |
toℕ-isHomomorphism-< = record |
| 346 |
{ cong = cong toℕ |
| 347 |
; mono = toℕ-mono-< |
| 348 |
} |
| 349 |
|
| 350 |
toℕ-isMonomorphism-< : IsOrderMonomorphism _≡_ _≡_ _<_ ℕ._<_ toℕ |
| 351 |
toℕ-isMonomorphism-< = record |
| 352 |
{ isOrderHomomorphism = toℕ-isHomomorphism-< |
| 353 |
; injective = toℕ-injective |
| 354 |
; cancel = toℕ-cancel-< |
| 355 |
} |
| 356 |
|
| 357 |
------------------------------------------------------------------------ |
| 358 |
-- Relational properties of _<_ |
| 359 |
|
| 360 |
<-irrefl : Irreflexive _≡_ _<_ |
| 361 |
<-irrefl refl (even<even x<x) = <-irrefl refl x<x |
| 362 |
<-irrefl refl (odd<odd x<x) = <-irrefl refl x<x |
| 363 |
|
| 364 |
<-trans : Transitive _<_ |
| 365 |
<-trans {zero} {_} {2[1+ _ ]} _ _ = 0<even |
| 366 |
<-trans {zero} {_} {1+[2 _ ]} _ _ = 0<odd |
| 367 |
<-trans (even<even x<y) (even<even y<z) = even<even (<-trans x<y y<z) |
| 368 |
<-trans (even<even x<y) (even<odd y<z) = even<odd (<-trans x<y y<z) |
| 369 |
<-trans (even<odd x<y) (odd<even (inj₁ y<z)) = even<even (<-trans x<y y<z) |
| 370 |
<-trans (even<odd x<y) (odd<even (inj₂ refl)) = even<even x<y |
| 371 |
<-trans (even<odd x<y) (odd<odd y<z) = even<odd (<-trans x<y y<z) |
| 372 |
<-trans (odd<even (inj₁ x<y)) (even<even y<z) = odd<even (inj₁ (<-trans x<y y<z)) |
| 373 |
<-trans (odd<even (inj₂ refl)) (even<even x<z) = odd<even (inj₁ x<z) |
| 374 |
<-trans (odd<even (inj₁ x<y)) (even<odd y<z) = odd<odd (<-trans x<y y<z) |
| 375 |
<-trans (odd<even (inj₂ refl)) (even<odd x<z) = odd<odd x<z |
| 376 |
<-trans (odd<odd x<y) (odd<even (inj₁ y<z)) = odd<even (inj₁ (<-trans x<y y<z)) |
| 377 |
<-trans (odd<odd x<y) (odd<even (inj₂ refl)) = odd<even (inj₁ x<y) |
| 378 |
<-trans (odd<odd x<y) (odd<odd y<z) = odd<odd (<-trans x<y y<z) |
| 379 |
|
| 380 |
<-asym : Asymmetric _<_ |
| 381 |
<-asym {x} {y} = trans∧irr⇒asym refl <-trans <-irrefl {x} {y} |
| 382 |
|
| 383 |
-- Should not be implemented via the morphism `toℕ` in order to |
| 384 |
-- preserve O(log n) time requirement. |
| 385 |
<-cmp : Trichotomous _≡_ _<_ |
| 386 |
<-cmp zero zero = tri≈ x≮0 refl x≮0 |
| 387 |
<-cmp zero 2[1+ _ ] = tri< 0<even (λ()) x≮0 |
| 388 |
<-cmp zero 1+[2 _ ] = tri< 0<odd (λ()) x≮0 |
| 389 |
<-cmp 2[1+ _ ] zero = tri> (λ()) (λ()) 0<even |
| 390 |
<-cmp 2[1+ x ] 2[1+ y ] with <-cmp x y |
| 391 |
... | tri< x<y _ _ = tri< lt (<⇒≢ lt) (<⇒≯ lt) where lt = even<even x<y |
| 392 |
... | tri≈ _ refl _ = tri≈ (<-irrefl refl) refl (<-irrefl refl) |
| 393 |
... | tri> _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = even<even x>y |
| 394 |
<-cmp 2[1+ x ] 1+[2 y ] with <-cmp x y |
| 395 |
... | tri< x<y _ _ = tri< lt (<⇒≢ lt) (<⇒≯ lt) where lt = even<odd x<y |
| 396 |
... | tri≈ _ refl _ = tri> (>⇒≮ gt) (>⇒≢ gt) gt |
| 397 |
where |
| 398 |
gt = subst (_< 2[1+ x ]) refl (1+[2x]<2[1+x] x) |
| 399 |
... | tri> _ _ y<x = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = odd<even (inj₁ y<x) |
| 400 |
<-cmp 1+[2 _ ] zero = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = 0<odd |
| 401 |
<-cmp 1+[2 x ] 2[1+ y ] with <-cmp x y |
| 402 |
... | tri< x<y _ _ = tri< lt (<⇒≢ lt) (<⇒≯ lt) where lt = odd<even (inj₁ x<y) |
| 403 |
... | tri≈ _ x≡y _ = tri< lt (<⇒≢ lt) (<⇒≯ lt) where lt = odd<even (inj₂ x≡y) |
| 404 |
... | tri> _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = even<odd x>y |
| 405 |
<-cmp 1+[2 x ] 1+[2 y ] with <-cmp x y |
| 406 |
... | tri< x<y _ _ = tri< lt (<⇒≢ lt) (<⇒≯ lt) where lt = odd<odd x<y |
| 407 |
... | tri≈ _ refl _ = tri≈ (≡⇒≮ refl) refl (≡⇒≯ refl) |
| 408 |
... | tri> _ _ x>y = tri> (>⇒≮ gt) (>⇒≢ gt) gt where gt = odd<odd x>y |
| 409 |
|
| 410 |
_<?_ : Decidable _<_ |
| 411 |
_<?_ = tri⇒dec< <-cmp |
| 412 |
|
| 413 |
------------------------------------------------------------------------ |
| 414 |
-- Structures for _<_ |
| 415 |
|
| 416 |
<-isStrictPartialOrder : IsStrictPartialOrder _≡_ _<_ |
| 417 |
<-isStrictPartialOrder = record |
| 418 |
{ isEquivalence = isEquivalence |
| 419 |
; irrefl = <-irrefl |
| 420 |
; trans = <-trans |
| 421 |
; <-resp-≈ = resp₂ _<_ |
| 422 |
} |
| 423 |
|
| 424 |
<-isStrictTotalOrder : IsStrictTotalOrder _≡_ _<_ |
| 425 |
<-isStrictTotalOrder = record |
| 426 |
{ isStrictPartialOrder = <-isStrictPartialOrder |
| 427 |
; compare = <-cmp |
| 428 |
} |
| 429 |
|
| 430 |
------------------------------------------------------------------------ |
| 431 |
-- Bundles for _<_ |
| 432 |
|
| 433 |
<-strictPartialOrder : StrictPartialOrder _ _ _ |
| 434 |
<-strictPartialOrder = record |
| 435 |
{ isStrictPartialOrder = <-isStrictPartialOrder |
| 436 |
} |
| 437 |
|
| 438 |
<-strictTotalOrder : StrictTotalOrder _ _ _ |
| 439 |
<-strictTotalOrder = record |
| 440 |
{ isStrictTotalOrder = <-isStrictTotalOrder |
| 441 |
} |
| 442 |
|
| 443 |
------------------------------------------------------------------------ |
| 444 |
-- Other properties of _<_ |
| 445 |
|
| 446 |
x<2[1+x] : ∀ x → x < 2[1+ x ] |
| 447 |
x<1+[2x] : ∀ x → x < 1+[2 x ] |
| 448 |
|
| 449 |
x<2[1+x] zero = 0<even |
| 450 |
x<2[1+x] 2[1+ x ] = even<even (x<2[1+x] x) |
| 451 |
x<2[1+x] 1+[2 x ] = odd<even (inj₁ (x<1+[2x] x)) |
| 452 |
|
| 453 |
x<1+[2x] zero = 0<odd |
| 454 |
x<1+[2x] 2[1+ x ] = even<odd (x<2[1+x] x) |
| 455 |
x<1+[2x] 1+[2 x ] = odd<odd (x<1+[2x] x) |
| 456 |
|
| 457 |
------------------------------------------------------------------------ |
| 458 |
-- Properties of _≤_ |
| 459 |
------------------------------------------------------------------------ |
| 460 |
-- Basic properties |
| 461 |
|
| 462 |
<⇒≱ : _<_ ⇒ _≱_ |
| 463 |
<⇒≱ x<y (inj₁ y<x) = contradiction y<x (<⇒≯ x<y) |
| 464 |
<⇒≱ x<y (inj₂ y≡x) = contradiction (sym y≡x) (<⇒≢ x<y) |
| 465 |
|
| 466 |
≤⇒≯ : _≤_ ⇒ _≯_ |
| 467 |
≤⇒≯ x≤y x>y = <⇒≱ x>y x≤y |
| 468 |
|
| 469 |
≮⇒≥ : _≮_ ⇒ _≥_ |
| 470 |
≮⇒≥ {x} {y} x≮y with <-cmp x y |
| 471 |
... | tri< lt _ _ = contradiction lt x≮y |
| 472 |
... | tri≈ _ eq _ = inj₂ (sym eq) |
| 473 |
... | tri> _ _ y<x = <⇒≤ y<x |
| 474 |
|
| 475 |
≰⇒> : _≰_ ⇒ _>_ |
| 476 |
≰⇒> {x} {y} x≰y with <-cmp x y |
| 477 |
... | tri< lt _ _ = contradiction (<⇒≤ lt) x≰y |
| 478 |
... | tri≈ _ eq _ = contradiction (inj₂ eq) x≰y |
| 479 |
... | tri> _ _ x>y = x>y |
| 480 |
|
| 481 |
≤∧≢⇒< : ∀ {x y} → x ≤ y → x ≢ y → x < y |
| 482 |
≤∧≢⇒< (inj₁ x<y) _ = x<y |
| 483 |
≤∧≢⇒< (inj₂ x≡y) x≢y = contradiction x≡y x≢y |
| 484 |
|
| 485 |
0≤x : ∀ x → zero ≤ x |
| 486 |
0≤x zero = inj₂ refl |
| 487 |
0≤x 2[1+ _ ] = inj₁ 0<even |
| 488 |
0≤x 1+[2 x ] = inj₁ 0<odd |
| 489 |
|
| 490 |
x≤0⇒x≡0 : x ≤ zero → x ≡ zero |
| 491 |
x≤0⇒x≡0 (inj₂ x≡0) = x≡0 |
| 492 |
|
| 493 |
------------------------------------------------------------------------ |
| 494 |
-- Properties of _<_ and toℕ & fromℕ. |
| 495 |
|
| 496 |
fromℕ-mono-≤ : fromℕ Preserves ℕ._≤_ ⟶ _≤_ |
| 497 |
fromℕ-mono-≤ m≤n with ℕ.m≤n⇒m<n∨m≡n m≤n |
| 498 |
... | inj₁ m<n = inj₁ (fromℕ-mono-< m<n) |
| 499 |
... | inj₂ m≡n = inj₂ (cong fromℕ m≡n) |
| 500 |
|
| 501 |
toℕ-mono-≤ : toℕ Preserves _≤_ ⟶ ℕ._≤_ |
| 502 |
toℕ-mono-≤ (inj₁ x<y) = ℕ.<⇒≤ (toℕ-mono-< x<y) |
| 503 |
toℕ-mono-≤ (inj₂ refl) = ℕ.≤-reflexive refl |
| 504 |
|
| 505 |
toℕ-cancel-≤ : ∀ {x y} → toℕ x ℕ.≤ toℕ y → x ≤ y |
| 506 |
toℕ-cancel-≤ = subst₂ _≤_ (fromℕ-toℕ _) (fromℕ-toℕ _) ∘ fromℕ-mono-≤ |
| 507 |
|
| 508 |
fromℕ-cancel-≤ : ∀ {x y} → fromℕ x ≤ fromℕ y → x ℕ.≤ y |
| 509 |
fromℕ-cancel-≤ = subst₂ ℕ._≤_ (toℕ-fromℕ _) (toℕ-fromℕ _) ∘ toℕ-mono-≤ |
| 510 |
|
| 511 |
toℕ-isHomomorphism-≤ : IsOrderHomomorphism _≡_ _≡_ _≤_ ℕ._≤_ toℕ |
| 512 |
toℕ-isHomomorphism-≤ = record |
| 513 |
{ cong = cong toℕ |
| 514 |
; mono = toℕ-mono-≤ |
| 515 |
} |
| 516 |
|
| 517 |
toℕ-isMonomorphism-≤ : IsOrderMonomorphism _≡_ _≡_ _≤_ ℕ._≤_ toℕ |
| 518 |
toℕ-isMonomorphism-≤ = record |
| 519 |
{ isOrderHomomorphism = toℕ-isHomomorphism-≤ |
| 520 |
; injective = toℕ-injective |
| 521 |
; cancel = toℕ-cancel-≤ |
| 522 |
} |
| 523 |
|
| 524 |
------------------------------------------------------------------------ |
| 525 |
-- Relational properties of _≤_ |
| 526 |
|
| 527 |
≤-refl : Reflexive _≤_ |
| 528 |
≤-refl = inj₂ refl |
| 529 |
|
| 530 |
≤-reflexive : _≡_ ⇒ _≤_ |
| 531 |
≤-reflexive {x} {_} refl = ≤-refl {x} |
| 532 |
|
| 533 |
≤-trans : Transitive _≤_ |
| 534 |
≤-trans = StrictToNonStrict.trans isEquivalence (resp₂ _<_) <-trans |
| 535 |
|
| 536 |
<-≤-trans : ∀ {x y z} → x < y → y ≤ z → x < z |
| 537 |
<-≤-trans x<y (inj₁ y<z) = <-trans x<y y<z |
| 538 |
<-≤-trans x<y (inj₂ refl) = x<y |
| 539 |
|
| 540 |
≤-<-trans : ∀ {x y z} → x ≤ y → y < z → x < z |
| 541 |
≤-<-trans (inj₁ x<y) y<z = <-trans x<y y<z |
| 542 |
≤-<-trans (inj₂ refl) y<z = y<z |
| 543 |
|
| 544 |
≤-antisym : Antisymmetric _≡_ _≤_ |
| 545 |
≤-antisym = StrictToNonStrict.antisym isEquivalence <-trans <-irrefl |
| 546 |
|
| 547 |
≤-total : Total _≤_ |
| 548 |
≤-total x y with <-cmp x y |
| 549 |
... | tri< x<y _ _ = inj₁ (<⇒≤ x<y) |
| 550 |
... | tri≈ _ x≡y _ = inj₁ (≤-reflexive x≡y) |
| 551 |
... | tri> _ _ y<x = inj₂ (<⇒≤ y<x) |
| 552 |
|
| 553 |
-- Should not be implemented via the morphism `toℕ` in order to |
| 554 |
-- preserve O(log n) time requirement. |
| 555 |
_≤?_ : Decidable _≤_ |
| 556 |
x ≤? y with <-cmp x y |
| 557 |
... | tri< x<y _ _ = yes (<⇒≤ x<y) |
| 558 |
... | tri≈ _ x≡y _ = yes (≤-reflexive x≡y) |
| 559 |
... | tri> _ _ y<x = no (<⇒≱ y<x) |
| 560 |
|
| 561 |
|
| 562 |
------------------------------------------------------------------------ |
| 563 |
-- Structures |
| 564 |
|
| 565 |
≤-isPreorder : IsPreorder _≡_ _≤_ |
| 566 |
≤-isPreorder = record |
| 567 |
{ isEquivalence = isEquivalence |
| 568 |
; reflexive = ≤-reflexive |
| 569 |
; trans = ≤-trans |
| 570 |
} |
| 571 |
|
| 572 |
≤-isPartialOrder : IsPartialOrder _≡_ _≤_ |
| 573 |
≤-isPartialOrder = record |
| 574 |
{ isPreorder = ≤-isPreorder |
| 575 |
; antisym = ≤-antisym |
| 576 |
} |
| 577 |
|
| 578 |
≤-isTotalOrder : IsTotalOrder _≡_ _≤_ |
| 579 |
≤-isTotalOrder = record |
| 580 |
{ isPartialOrder = ≤-isPartialOrder |
| 581 |
; total = ≤-total |
| 582 |
} |
| 583 |
|
| 584 |
≤-isDecTotalOrder : IsDecTotalOrder _≡_ _≤_ |
| 585 |
≤-isDecTotalOrder = record |
| 586 |
{ isTotalOrder = ≤-isTotalOrder |
| 587 |
; _≟_ = _≟_ |
| 588 |
; _≤?_ = _≤?_ |
| 589 |
} |
| 590 |
|
| 591 |
------------------------------------------------------------------------ |
| 592 |
-- Bundles |
| 593 |
|
| 594 |
≤-preorder : Preorder 0ℓ 0ℓ 0ℓ |
| 595 |
≤-preorder = record |
| 596 |
{ isPreorder = ≤-isPreorder |
| 597 |
} |
| 598 |
|
| 599 |
≤-partialOrder : Poset 0ℓ 0ℓ 0ℓ |
| 600 |
≤-partialOrder = record |
| 601 |
{ isPartialOrder = ≤-isPartialOrder |
| 602 |
} |
| 603 |
|
| 604 |
≤-totalOrder : TotalOrder 0ℓ 0ℓ 0ℓ |
| 605 |
≤-totalOrder = record |
| 606 |
{ isTotalOrder = ≤-isTotalOrder |
| 607 |
} |
| 608 |
|
| 609 |
≤-decTotalOrder : DecTotalOrder 0ℓ 0ℓ 0ℓ |
| 610 |
≤-decTotalOrder = record |
| 611 |
{ isDecTotalOrder = ≤-isDecTotalOrder |
| 612 |
} |
| 613 |
|
| 614 |
------------------------------------------------------------------------ |
| 615 |
-- Equational reasoning for _≤_ and _<_ |
| 616 |
|
| 617 |
module ≤-Reasoning where |
| 618 |
|
| 619 |
open import Relation.Binary.Reasoning.Base.Triple |
| 620 |
≤-isPreorder |
| 621 |
<-asym |
| 622 |
<-trans |
| 623 |
(resp₂ _<_) |
| 624 |
<⇒≤ |
| 625 |
<-≤-trans |
| 626 |
≤-<-trans |
| 627 |
public |
| 628 |
hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨) |
| 629 |
|
| 630 |
------------------------------------------------------------------------ |
| 631 |
-- Properties of _<ℕ_ |
| 632 |
------------------------------------------------------------------------ |
| 633 |
|
| 634 |
<⇒<ℕ : ∀ {x y} → x < y → x <ℕ y |
| 635 |
<⇒<ℕ x<y = toℕ-mono-< x<y |
| 636 |
|
| 637 |
<ℕ⇒< : ∀ {x y} → x <ℕ y → x < y |
| 638 |
<ℕ⇒< {x} {y} t[x]<t[y] = begin-strict |
| 639 |
x ≡⟨ sym (fromℕ-toℕ x) ⟩ |
| 640 |
fromℕ (toℕ x) <⟨ fromℕ-mono-< t[x]<t[y] ⟩ |
| 641 |
fromℕ (toℕ y) ≡⟨ fromℕ-toℕ y ⟩ |
| 642 |
y ∎ |
| 643 |
where open ≤-Reasoning |
| 644 |
|
| 645 |
------------------------------------------------------------------------ |
| 646 |
-- Properties of _+_ |
| 647 |
|
| 648 |
-- toℕ/fromℕ are homomorphisms for _+_ |
| 649 |
|
| 650 |
toℕ-homo-+ : ∀ x y → toℕ (x + y) ≡ toℕ x ℕ.+ toℕ y |
| 651 |
toℕ-homo-+ zero _ = refl |
| 652 |
toℕ-homo-+ 2[1+ x ] zero = cong ℕ.suc (sym (ℕ.+-identityʳ _)) |
| 653 |
toℕ-homo-+ 1+[2 x ] zero = cong ℕ.suc (sym (ℕ.+-identityʳ _)) |
| 654 |
toℕ-homo-+ 2[1+ x ] 2[1+ y ] = begin |
| 655 |
toℕ (2[1+ x ] + 2[1+ y ]) ≡⟨⟩ |
| 656 |
toℕ 2[1+ (suc (x + y)) ] ≡⟨⟩ |
| 657 |
2 ℕ.* (1 ℕ.+ (toℕ (suc (x + y)))) ≡⟨ cong ((2 ℕ.*_) ∘ ℕ.suc) (toℕ-suc (x + y)) ⟩ |
| 658 |
2 ℕ.* (2 ℕ.+ toℕ (x + y)) ≡⟨ cong ((2 ℕ.*_) ∘ (2 ℕ.+_)) (toℕ-homo-+ x y) ⟩ |
| 659 |
2 ℕ.* (2 ℕ.+ (toℕ x ℕ.+ toℕ y)) ≡⟨ solve 2 (λ m n → con 2 :* (con 2 :+ (m :+ n)) := |
| 660 |
con 2 :* (con 1 :+ m) :+ con 2 :* (con 1 :+ n)) |
| 661 |
refl (toℕ x) (toℕ y) ⟩ |
| 662 |
toℕ 2[1+ x ] ℕ.+ toℕ 2[1+ y ] ∎ |
| 663 |
where open ≡-Reasoning |
| 664 |
|
| 665 |
toℕ-homo-+ 2[1+ x ] 1+[2 y ] = begin |
| 666 |
toℕ (2[1+ x ] + 1+[2 y ]) ≡⟨⟩ |
| 667 |
toℕ (suc 2[1+ (x + y) ]) ≡⟨ toℕ-suc 2[1+ (x + y) ] ⟩ |
| 668 |
ℕ.suc (toℕ 2[1+ (x + y) ]) ≡⟨⟩ |
| 669 |
ℕ.suc (2 ℕ.* (ℕ.suc (toℕ (x + y)))) ≡⟨ cong (λ v → ℕ.suc (2 ℕ.* ℕ.suc v)) (toℕ-homo-+ x y) ⟩ |
| 670 |
ℕ.suc (2 ℕ.* (ℕ.suc (m ℕ.+ n))) ≡⟨ solve 2 (λ m n → con 1 :+ (con 2 :* (con 1 :+ (m :+ n))) := |
| 671 |
con 2 :* (con 1 :+ m) :+ (con 1 :+ (con 2 :* n))) |
| 672 |
refl m n ⟩ |
| 673 |
(2 ℕ.* ℕ.suc m) ℕ.+ (ℕ.suc (2 ℕ.* n)) ≡⟨⟩ |
| 674 |
toℕ 2[1+ x ] ℕ.+ toℕ 1+[2 y ] ∎ |
| 675 |
where open ≡-Reasoning; m = toℕ x; n = toℕ y |
| 676 |
|
| 677 |
toℕ-homo-+ 1+[2 x ] 2[1+ y ] = begin |
| 678 |
toℕ (1+[2 x ] + 2[1+ y ]) ≡⟨⟩ |
| 679 |
toℕ (suc 2[1+ (x + y) ]) ≡⟨ toℕ-suc 2[1+ (x + y) ] ⟩ |
| 680 |
ℕ.suc (toℕ 2[1+ (x + y) ]) ≡⟨⟩ |
| 681 |
ℕ.suc (2 ℕ.* (ℕ.suc (toℕ (x + y)))) ≡⟨ cong (ℕ.suc ∘ (2 ℕ.*_) ∘ ℕ.suc) (toℕ-homo-+ x y) ⟩ |
| 682 |
ℕ.suc (2 ℕ.* (ℕ.suc (m ℕ.+ n))) ≡⟨ solve 2 (λ m n → con 1 :+ (con 2 :* (con 1 :+ (m :+ n))) := |
| 683 |
(con 1 :+ (con 2 :* m)) :+ (con 2 :* (con 1 :+ n))) |
| 684 |
refl m n ⟩ |
| 685 |
(ℕ.suc (2 ℕ.* m)) ℕ.+ (2 ℕ.* (ℕ.suc n)) ≡⟨⟩ |
| 686 |
toℕ 1+[2 x ] ℕ.+ toℕ 2[1+ y ] ∎ |
| 687 |
where open ≡-Reasoning; m = toℕ x; n = toℕ y |
| 688 |
|
| 689 |
toℕ-homo-+ 1+[2 x ] 1+[2 y ] = begin |
| 690 |
toℕ (1+[2 x ] + 1+[2 y ]) ≡⟨⟩ |
| 691 |
toℕ (suc 1+[2 (x + y) ]) ≡⟨ toℕ-suc 1+[2 (x + y) ] ⟩ |
| 692 |
ℕ.suc (toℕ 1+[2 (x + y) ]) ≡⟨⟩ |
| 693 |
ℕ.suc (ℕ.suc (2 ℕ.* (toℕ (x + y)))) ≡⟨ cong (ℕ.suc ∘ ℕ.suc ∘ (2 ℕ.*_)) (toℕ-homo-+ x y) ⟩ |
| 694 |
ℕ.suc (ℕ.suc (2 ℕ.* (m ℕ.+ n))) ≡⟨ solve 2 (λ m n → con 1 :+ (con 1 :+ (con 2 :* (m :+ n))) := |
| 695 |
(con 1 :+ (con 2 :* m)) :+ (con 1 :+ (con 2 :* n))) |
| 696 |
refl m n ⟩ |
| 697 |
(ℕ.suc (2 ℕ.* m)) ℕ.+ (ℕ.suc (2 ℕ.* n)) ≡⟨⟩ |
| 698 |
toℕ 1+[2 x ] ℕ.+ toℕ 1+[2 y ] ∎ |
| 699 |
where open ≡-Reasoning; m = toℕ x; n = toℕ y |
| 700 |
|
| 701 |
toℕ-isMagmaHomomorphism-+ : IsMagmaHomomorphism +-rawMagma ℕ.+-rawMagma toℕ |
| 702 |
toℕ-isMagmaHomomorphism-+ = record |
| 703 |
{ isRelHomomorphism = toℕ-isRelHomomorphism |
| 704 |
; homo = toℕ-homo-+ |
| 705 |
} |
| 706 |
|
| 707 |
toℕ-isMonoidHomomorphism-+ : IsMonoidHomomorphism +-0-rawMonoid ℕ.+-0-rawMonoid toℕ |
| 708 |
toℕ-isMonoidHomomorphism-+ = record |
| 709 |
{ isMagmaHomomorphism = toℕ-isMagmaHomomorphism-+ |
| 710 |
; ε-homo = refl |
| 711 |
} |
| 712 |
|
| 713 |
toℕ-isMonoidMonomorphism-+ : IsMonoidMonomorphism +-0-rawMonoid ℕ.+-0-rawMonoid toℕ |
| 714 |
toℕ-isMonoidMonomorphism-+ = record |
| 715 |
{ isMonoidHomomorphism = toℕ-isMonoidHomomorphism-+ |
| 716 |
; injective = toℕ-injective |
| 717 |
} |
| 718 |
|
| 719 |
suc≗1+ : suc ≗ 1ᵇ +_ |
| 720 |
suc≗1+ zero = refl |
| 721 |
suc≗1+ 2[1+ _ ] = refl |
| 722 |
suc≗1+ 1+[2 _ ] = refl |
| 723 |
|
| 724 |
suc-+ : ∀ x y → suc x + y ≡ suc (x + y) |
| 725 |
suc-+ zero y = sym (suc≗1+ y) |
| 726 |
suc-+ 2[1+ x ] zero = refl |
| 727 |
suc-+ 1+[2 x ] zero = refl |
| 728 |
suc-+ 2[1+ x ] 2[1+ y ] = cong (suc ∘ 2[1+_]) (suc-+ x y) |
| 729 |
suc-+ 2[1+ x ] 1+[2 y ] = cong (suc ∘ 1+[2_]) (suc-+ x y) |
| 730 |
suc-+ 1+[2 x ] 2[1+ y ] = refl |
| 731 |
suc-+ 1+[2 x ] 1+[2 y ] = refl |
| 732 |
|
| 733 |
1+≗suc : (1ᵇ +_) ≗ suc |
| 734 |
1+≗suc = suc-+ zero |
| 735 |
|
| 736 |
fromℕ'-homo-+ : ∀ m n → fromℕ' (m ℕ.+ n) ≡ fromℕ' m + fromℕ' n |
| 737 |
fromℕ'-homo-+ 0 _ = refl |
| 738 |
fromℕ'-homo-+ (ℕ.suc m) n = begin |
| 739 |
fromℕ' ((ℕ.suc m) ℕ.+ n) ≡⟨⟩ |
| 740 |
suc (fromℕ' (m ℕ.+ n)) ≡⟨ cong suc (fromℕ'-homo-+ m n) ⟩ |
| 741 |
suc (a + b) ≡⟨ sym (suc-+ a b) ⟩ |
| 742 |
(suc a) + b ≡⟨⟩ |
| 743 |
(fromℕ' (ℕ.suc m)) + (fromℕ' n) ∎ |
| 744 |
where open ≡-Reasoning; a = fromℕ' m; b = fromℕ' n |
| 745 |
|
| 746 |
fromℕ-homo-+ : ∀ m n → fromℕ (m ℕ.+ n) ≡ fromℕ m + fromℕ n |
| 747 |
fromℕ-homo-+ m n rewrite fromℕ≡fromℕ' (m ℕ.+ n) | fromℕ≡fromℕ' m | fromℕ≡fromℕ' n = |
| 748 |
fromℕ'-homo-+ m n |
| 749 |
|
| 750 |
------------------------------------------------------------------------ |
| 751 |
-- Algebraic properties of _+_ |
| 752 |
|
| 753 |
-- Mostly proved by using the isomorphism between `ℕ` and `ℕᵇ` provided |
| 754 |
-- by `toℕ`/`fromℕ`. |
| 755 |
|
| 756 |
private |
| 757 |
module +-Monomorphism = MonoidMonomorphism toℕ-isMonoidMonomorphism-+ |
| 758 |
|
| 759 |
+-assoc : Associative _+_ |
| 760 |
+-assoc = +-Monomorphism.assoc ℕ.+-isMagma ℕ.+-assoc |
| 761 |
|
| 762 |
+-comm : Commutative _+_ |
| 763 |
+-comm = +-Monomorphism.comm ℕ.+-isMagma ℕ.+-comm |
| 764 |
|
| 765 |
+-identityˡ : LeftIdentity zero _+_ |
| 766 |
+-identityˡ _ = refl |
| 767 |
|
| 768 |
+-identityʳ : RightIdentity zero _+_ |
| 769 |
+-identityʳ = +-Monomorphism.identityʳ ℕ.+-isMagma ℕ.+-identityʳ |
| 770 |
|
| 771 |
+-identity : Identity zero _+_ |
| 772 |
+-identity = +-identityˡ , +-identityʳ |
| 773 |
|
| 774 |
+-cancelˡ-≡ : LeftCancellative _+_ |
| 775 |
+-cancelˡ-≡ = +-Monomorphism.cancelˡ ℕ.+-isMagma ℕ.+-cancelˡ-≡ |
| 776 |
|
| 777 |
+-cancelʳ-≡ : RightCancellative _+_ |
| 778 |
+-cancelʳ-≡ = +-Monomorphism.cancelʳ ℕ.+-isMagma ℕ.+-cancelʳ-≡ |
| 779 |
|
| 780 |
------------------------------------------------------------------------ |
| 781 |
-- Structures for _+_ |
| 782 |
|
| 783 |
+-isMagma : IsMagma _+_ |
| 784 |
+-isMagma = isMagma _+_ |
| 785 |
|
| 786 |
+-isSemigroup : IsSemigroup _+_ |
| 787 |
+-isSemigroup = +-Monomorphism.isSemigroup ℕ.+-isSemigroup |
| 788 |
|
| 789 |
+-isCommutativeSemigroup : IsCommutativeSemigroup _+_ |
| 790 |
+-isCommutativeSemigroup = record |
| 791 |
{ isSemigroup = +-isSemigroup |
| 792 |
; comm = +-comm |
| 793 |
} |
| 794 |
|
| 795 |
+-0-isMonoid : IsMonoid _+_ 0ᵇ |
| 796 |
+-0-isMonoid = +-Monomorphism.isMonoid ℕ.+-0-isMonoid |
| 797 |
|
| 798 |
+-0-isCommutativeMonoid : IsCommutativeMonoid _+_ 0ᵇ |
| 799 |
+-0-isCommutativeMonoid = +-Monomorphism.isCommutativeMonoid ℕ.+-0-isCommutativeMonoid |
| 800 |
|
| 801 |
------------------------------------------------------------------------ |
| 802 |
-- Bundles for _+_ |
| 803 |
|
| 804 |
+-magma : Magma 0ℓ 0ℓ |
| 805 |
+-magma = magma _+_ |
| 806 |
|
| 807 |
+-semigroup : Semigroup 0ℓ 0ℓ |
| 808 |
+-semigroup = record |
| 809 |
{ isSemigroup = +-isSemigroup |
| 810 |
} |
| 811 |
|
| 812 |
+-commutativeSemigroup : CommutativeSemigroup 0ℓ 0ℓ |
| 813 |
+-commutativeSemigroup = record |
| 814 |
{ isCommutativeSemigroup = +-isCommutativeSemigroup |
| 815 |
} |
| 816 |
|
| 817 |
+-0-monoid : Monoid 0ℓ 0ℓ |
| 818 |
+-0-monoid = record |
| 819 |
{ ε = zero |
| 820 |
; isMonoid = +-0-isMonoid |
| 821 |
} |
| 822 |
|
| 823 |
+-0-commutativeMonoid : CommutativeMonoid 0ℓ 0ℓ |
| 824 |
+-0-commutativeMonoid = record |
| 825 |
{ isCommutativeMonoid = +-0-isCommutativeMonoid |
| 826 |
} |
| 827 |
|
| 828 |
module Bin+CSemigroup = CommSemigProp +-commutativeSemigroup |
| 829 |
|
| 830 |
------------------------------------------------------------------------ |
| 831 |
-- Properties of _+_ and _≤_ |
| 832 |
|
| 833 |
+-mono-≤ : _+_ Preserves₂ _≤_ ⟶ _≤_ ⟶ _≤_ |
| 834 |
+-mono-≤ {x} {x′} {y} {y′} x≤x′ y≤y′ = begin |
| 835 |
x + y ≡⟨ sym $ cong₂ _+_ (fromℕ-toℕ x) (fromℕ-toℕ y) ⟩ |
| 836 |
fromℕ m + fromℕ n ≡⟨ sym (fromℕ-homo-+ m n) ⟩ |
| 837 |
fromℕ (m ℕ.+ n) ≤⟨ fromℕ-mono-≤ (ℕ.+-mono-≤ m≤m′ n≤n′) ⟩ |
| 838 |
fromℕ (m′ ℕ.+ n′) ≡⟨ fromℕ-homo-+ m′ n′ ⟩ |
| 839 |
fromℕ m′ + fromℕ n′ ≡⟨ cong₂ _+_ (fromℕ-toℕ x′) (fromℕ-toℕ y′) ⟩ |
| 840 |
x′ + y′ ∎ |
| 841 |
where |
| 842 |
open ≤-Reasoning |
| 843 |
m = toℕ x; m′ = toℕ x′ |
| 844 |
n = toℕ y; n′ = toℕ y′ |
| 845 |
m≤m′ = toℕ-mono-≤ x≤x′; n≤n′ = toℕ-mono-≤ y≤y′ |
| 846 |
|
| 847 |
+-monoˡ-≤ : ∀ x → (_+ x) Preserves _≤_ ⟶ _≤_ |
| 848 |
+-monoˡ-≤ x y≤z = +-mono-≤ y≤z (≤-refl {x}) |
| 849 |
|
| 850 |
+-monoʳ-≤ : ∀ x → (x +_) Preserves _≤_ ⟶ _≤_ |
| 851 |
+-monoʳ-≤ x y≤z = +-mono-≤ (≤-refl {x}) y≤z |
| 852 |
|
| 853 |
+-mono-<-≤ : _+_ Preserves₂ _<_ ⟶ _≤_ ⟶ _<_ |
| 854 |
+-mono-<-≤ {x} {x′} {y} {y′} x<x′ y≤y′ = begin-strict |
| 855 |
x + y ≡⟨ sym $ cong₂ _+_ (fromℕ-toℕ x) (fromℕ-toℕ y) ⟩ |
| 856 |
fromℕ m + fromℕ n ≡⟨ sym (fromℕ-homo-+ m n) ⟩ |
| 857 |
fromℕ (m ℕ.+ n) <⟨ fromℕ-mono-< (ℕ.+-mono-<-≤ m<m′ n≤n′) ⟩ |
| 858 |
fromℕ (m′ ℕ.+ n′) ≡⟨ fromℕ-homo-+ m′ n′ ⟩ |
| 859 |
fromℕ m′ + fromℕ n′ ≡⟨ cong₂ _+_ (fromℕ-toℕ x′) (fromℕ-toℕ y′) ⟩ |
| 860 |
x′ + y′ ∎ |
| 861 |
where |
| 862 |
open ≤-Reasoning |
| 863 |
m = toℕ x; n = toℕ y |
| 864 |
m′ = toℕ x′; n′ = toℕ y′ |
| 865 |
m<m′ = toℕ-mono-< x<x′; n≤n′ = toℕ-mono-≤ y≤y′ |
| 866 |
|
| 867 |
+-mono-≤-< : _+_ Preserves₂ _≤_ ⟶ _<_ ⟶ _<_ |
| 868 |
+-mono-≤-< {x} {x′} {y} {y′} x≤x′ y<y′ = subst₂ _<_ (+-comm y x) (+-comm y′ x′) y+x<y′+x′ |
| 869 |
where |
| 870 |
y+x<y′+x′ = +-mono-<-≤ y<y′ x≤x′ |
| 871 |
|
| 872 |
+-monoˡ-< : ∀ x → (_+ x) Preserves _<_ ⟶ _<_ |
| 873 |
+-monoˡ-< x y<z = +-mono-<-≤ y<z (≤-refl {x}) |
| 874 |
|
| 875 |
+-monoʳ-< : ∀ x → (x +_) Preserves _<_ ⟶ _<_ |
| 876 |
+-monoʳ-< x y<z = +-mono-≤-< (≤-refl {x}) y<z |
| 877 |
|
| 878 |
x≤y+x : ∀ x y → x ≤ y + x |
| 879 |
x≤y+x x y = begin |
| 880 |
x ≡⟨ sym (+-identityˡ x) ⟩ |
| 881 |
0ᵇ + x ≤⟨ +-monoˡ-≤ x (0≤x y) ⟩ |
| 882 |
y + x ∎ |
| 883 |
where open ≤-Reasoning |
| 884 |
|
| 885 |
x≤x+y : ∀ x y → x ≤ x + y |
| 886 |
x≤x+y x y = begin |
| 887 |
x ≤⟨ x≤y+x x y ⟩ |
| 888 |
y + x ≡⟨ +-comm y x ⟩ |
| 889 |
x + y ∎ |
| 890 |
where open ≤-Reasoning |
| 891 |
|
| 892 |
x<x+y : ∀ x {y} → y > 0ᵇ → x < x + y |
| 893 |
x<x+y x {y} y>0 = begin-strict |
| 894 |
x ≡⟨ sym (fromℕ-toℕ x) ⟩ |
| 895 |
fromℕ (toℕ x) <⟨ fromℕ-mono-< (ℕ.m<m+n (toℕ x) (toℕ-mono-< y>0)) ⟩ |
| 896 |
fromℕ (toℕ x ℕ.+ toℕ y) ≡⟨ fromℕ-homo-+ (toℕ x) (toℕ y) ⟩ |
| 897 |
fromℕ (toℕ x) + fromℕ (toℕ y) ≡⟨ cong₂ _+_ (fromℕ-toℕ x) (fromℕ-toℕ y) ⟩ |
| 898 |
x + y ∎ |
| 899 |
where open ≤-Reasoning |
| 900 |
|
| 901 |
x<x+1 : ∀ x → x < x + 1ᵇ |
| 902 |
x<x+1 x = x<x+y x 0<odd |
| 903 |
|
| 904 |
x<1+x : ∀ x → x < 1ᵇ + x |
| 905 |
x<1+x x rewrite +-comm 1ᵇ x = x<x+1 x |
| 906 |
|
| 907 |
x<1⇒x≡0 : x < 1ᵇ → x ≡ zero |
| 908 |
x<1⇒x≡0 0<odd = refl |
| 909 |
|
| 910 |
------------------------------------------------------------------------ |
| 911 |
-- Other properties |
| 912 |
|
| 913 |
x≢0⇒x+y≢0 : ∀ {x} (y : ℕᵇ) → x ≢ zero → x + y ≢ zero |
| 914 |
x≢0⇒x+y≢0 {2[1+ _ ]} zero _ = λ() |
| 915 |
x≢0⇒x+y≢0 {zero} _ 0≢0 = contradiction refl 0≢0 |
| 916 |
|
| 917 |
------------------------------------------------------------------------ |
| 918 |
-- Properties of _*_ |
| 919 |
|
| 920 |
-- toℕ/fromℕ are homomorphisms for _*_ |
| 921 |
|
| 922 |
private 2*ₙ2*ₙ = (2 ℕ.*_) ∘ (2 ℕ.*_) |
| 923 |
|
| 924 |
toℕ-homo-* : ∀ x y → toℕ (x * y) ≡ toℕ x ℕ.* toℕ y |
| 925 |
toℕ-homo-* x y = aux x y (size x ℕ.+ size y) ℕ.≤-refl |
| 926 |
where |
| 927 |
aux : (x y : ℕᵇ) → (cnt : ℕ) → (size x ℕ.+ size y ℕ.≤ cnt) → toℕ (x * y) ≡ toℕ x ℕ.* toℕ y |
| 928 |
aux zero _ _ _ = refl |
| 929 |
aux 2[1+ x ] zero _ _ = sym (ℕ.*-zeroʳ (toℕ x ℕ.+ (ℕ.suc (toℕ x ℕ.+ 0)))) |
| 930 |
aux 1+[2 x ] zero _ _ = sym (ℕ.*-zeroʳ (toℕ x ℕ.+ (toℕ x ℕ.+ 0))) |
| 931 |
aux 2[1+ x ] 2[1+ y ] (ℕ.suc cnt) (s≤s |x|+1+|y|≤cnt) = begin |
| 932 |
toℕ (2[1+ x ] * 2[1+ y ]) ≡⟨⟩ |
| 933 |
toℕ (double 2[1+ (x + (y + xy)) ]) ≡⟨ toℕ-double 2[1+ (x + (y + xy)) ] ⟩ |
| 934 |
2 ℕ.* (toℕ 2[1+ (x + (y + xy)) ]) ≡⟨⟩ |
| 935 |
2*ₙ2*ₙ (ℕ.suc (toℕ (x + (y + xy)))) ≡⟨ cong (2*ₙ2*ₙ ∘ ℕ.suc) (toℕ-homo-+ x (y + xy)) ⟩ |
| 936 |
2*ₙ2*ₙ (ℕ.suc (m ℕ.+ (toℕ (y + xy)))) ≡⟨ cong (2*ₙ2*ₙ ∘ ℕ.suc ∘ (m ℕ.+_)) (toℕ-homo-+ y xy) ⟩ |
| 937 |
2*ₙ2*ₙ (ℕ.suc (m ℕ.+ (n ℕ.+ toℕ xy))) ≡⟨ cong (2*ₙ2*ₙ ∘ ℕ.suc ∘ (m ℕ.+_) ∘ (n ℕ.+_)) |
| 938 |
(aux x y cnt |x|+|y|≤cnt) ⟩ |
| 939 |
2*ₙ2*ₙ (ℕ.suc (m ℕ.+ (n ℕ.+ (m ℕ.* n)))) ≡⟨ solve 2 (λ m n → con 2 :* (con 2 :* (con 1 :+ (m :+ (n :+ m :* n)))) := |
| 940 |
(con 2 :* (con 1 :+ m)) :* (con 2 :* (con 1 :+ n))) |
| 941 |
refl m n ⟩ |
| 942 |
(2 ℕ.* (1 ℕ.+ m)) ℕ.* (2 ℕ.* (1 ℕ.+ n)) ≡⟨⟩ |
| 943 |
toℕ 2[1+ x ] ℕ.* toℕ 2[1+ y ] ∎ |
| 944 |
where |
| 945 |
open ≡-Reasoning; m = toℕ x; n = toℕ y; xy = x * y |
| 946 |
|
| 947 |
|x|+|y|≤cnt = ℕ.≤-trans (ℕ.+-monoʳ-≤ (size x) (ℕ.n≤1+n (size y))) |x|+1+|y|≤cnt |
| 948 |
|
| 949 |
aux 2[1+ x ] 1+[2 y ] (ℕ.suc cnt) (s≤s |x|+1+|y|≤cnt) = begin |
| 950 |
toℕ (2[1+ x ] * 1+[2 y ]) ≡⟨⟩ |
| 951 |
toℕ (2[1+ (x + y * 2[1+ x ]) ]) ≡⟨⟩ |
| 952 |
2 ℕ.* (ℕ.suc (toℕ (x + y * 2[1+ x ]))) ≡⟨ cong ((2 ℕ.*_) ∘ ℕ.suc) (toℕ-homo-+ x _) ⟩ |
| 953 |
2 ℕ.* (ℕ.suc (m ℕ.+ (toℕ (y * 2[1+ x ])))) ≡⟨ cong ((2 ℕ.*_) ∘ ℕ.suc ∘ (m ℕ.+_)) |
| 954 |
(aux y 2[1+ x ] cnt |y|+1+|x|≤cnt) ⟩ |
| 955 |
2 ℕ.* (1+m ℕ.+ (n ℕ.* (toℕ 2[1+ x ]))) ≡⟨⟩ |
| 956 |
2 ℕ.* (1+m ℕ.+ (n ℕ.* 2[1+m])) ≡⟨ solve 2 (λ m n → |
| 957 |
con 2 :* ((con 1 :+ m) :+ (n :* (con 2 :* (con 1 :+ m)))) := |
| 958 |
(con 2 :* (con 1 :+ m)) :* (con 1 :+ con 2 :* n)) |
| 959 |
refl m n ⟩ |
| 960 |
2[1+m] ℕ.* (ℕ.suc (2 ℕ.* n)) ≡⟨⟩ |
| 961 |
toℕ 2[1+ x ] ℕ.* toℕ 1+[2 y ] ∎ |
| 962 |
where |
| 963 |
open ≡-Reasoning; m = toℕ x; n = toℕ y; 1+m = ℕ.suc m; 2[1+m] = 2 ℕ.* (ℕ.suc m) |
| 964 |
|
| 965 |
eq : size x ℕ.+ (ℕ.suc (size y)) ≡ size y ℕ.+ (ℕ.suc (size x)) |
| 966 |
eq = ℕ-+-semigroupProperties.x∙yz≈z∙yx (size x) 1 _ |
| 967 |
|
| 968 |
|y|+1+|x|≤cnt = subst (ℕ._≤ cnt) eq |x|+1+|y|≤cnt |
| 969 |
|
| 970 |
aux 1+[2 x ] 2[1+ y ] (ℕ.suc cnt) (s≤s |x|+1+|y|≤cnt) = begin |
| 971 |
toℕ (1+[2 x ] * 2[1+ y ]) ≡⟨⟩ |
| 972 |
toℕ 2[1+ (y + x * 2[1+ y ]) ] ≡⟨⟩ |
| 973 |
2 ℕ.* (ℕ.suc (toℕ (y + x * 2[1+ y ]))) ≡⟨ cong ((2 ℕ.*_) ∘ ℕ.suc) |
| 974 |
(toℕ-homo-+ y (x * 2[1+ y ])) ⟩ |
| 975 |
2 ℕ.* (ℕ.suc (n ℕ.+ (toℕ (x * 2[1+ y ])))) ≡⟨ cong ((2 ℕ.*_) ∘ ℕ.suc ∘ (n ℕ.+_)) |
| 976 |
(aux x 2[1+ y ] cnt |x|+1+|y|≤cnt) ⟩ |
| 977 |
2 ℕ.* (1+n ℕ.+ (m ℕ.* toℕ 2[1+ y ])) ≡⟨⟩ |
| 978 |
2 ℕ.* (1+n ℕ.+ (m ℕ.* 2[1+n])) ≡⟨ solve 2 (λ m n → |
| 979 |
con 2 :* ((con 1 :+ n) :+ (m :* (con 2 :* (con 1 :+ n)))) := |
| 980 |
(con 1 :+ (con 2 :* m)) :* (con 2 :* (con 1 :+ n))) |
| 981 |
refl m n ⟩ |
| 982 |
(ℕ.suc 2m) ℕ.* 2[1+n] ≡⟨⟩ |
| 983 |
toℕ 1+[2 x ] ℕ.* toℕ 2[1+ y ] ∎ |
| 984 |
where |
| 985 |
open ≡-Reasoning |
| 986 |
m = toℕ x; n = toℕ y; 1+n = ℕ.suc n |
| 987 |
2m = 2 ℕ.* m; 2[1+n] = 2 ℕ.* (ℕ.suc n) |
| 988 |
|
| 989 |
aux 1+[2 x ] 1+[2 y ] (ℕ.suc cnt) (s≤s |x|+1+|y|≤cnt) = begin |
| 990 |
toℕ (1+[2 x ] * 1+[2 y ]) ≡⟨⟩ |
| 991 |
toℕ 1+[2 (x + y * 1+2x) ] ≡⟨⟩ |
| 992 |
ℕ.suc (2 ℕ.* (toℕ (x + y * 1+2x))) ≡⟨ cong (ℕ.suc ∘ (2 ℕ.*_)) (toℕ-homo-+ x (y * 1+2x)) ⟩ |
| 993 |
ℕ.suc (2 ℕ.* (m ℕ.+ (toℕ (y * 1+2x)))) ≡⟨ cong (ℕ.suc ∘ (2 ℕ.*_) ∘ (m ℕ.+_)) |
| 994 |
(aux y 1+2x cnt |y|+1+|x|≤cnt) ⟩ |
| 995 |
ℕ.suc (2 ℕ.* (m ℕ.+ (n ℕ.* [1+2x]′))) ≡⟨ cong ℕ.suc $ ℕ.*-distribˡ-+ 2 m (n ℕ.* [1+2x]′) ⟩ |
| 996 |
ℕ.suc (2m ℕ.+ (2 ℕ.* (n ℕ.* [1+2x]′))) ≡⟨ cong (ℕ.suc ∘ (2m ℕ.+_)) (sym (ℕ.*-assoc 2 n _)) ⟩ |
| 997 |
(ℕ.suc 2m) ℕ.+ 2n ℕ.* [1+2x]′ ≡⟨⟩ |
| 998 |
[1+2x]′ ℕ.+ 2n ℕ.* [1+2x]′ ≡⟨ cong (ℕ._+ (2n ℕ.* [1+2x]′)) $ |
| 999 |
sym (ℕ.*-identityˡ [1+2x]′) ⟩ |
| 1000 |
1 ℕ.* [1+2x]′ ℕ.+ 2n ℕ.* [1+2x]′ ≡⟨ sym (ℕ.*-distribʳ-+ [1+2x]′ 1 2n) ⟩ |
| 1001 |
(ℕ.suc 2n) ℕ.* [1+2x]′ ≡⟨ ℕ.*-comm (ℕ.suc 2n) [1+2x]′ ⟩ |
| 1002 |
toℕ 1+[2 x ] ℕ.* toℕ 1+[2 y ] ∎ |
| 1003 |
where |
| 1004 |
open ≡-Reasoning |
| 1005 |
m = toℕ x; n = toℕ y; 2m = 2 ℕ.* m; 2n = 2 ℕ.* n |
| 1006 |
1+2x = 1+[2 x ]; [1+2x]′ = toℕ 1+2x |
| 1007 |
|
| 1008 |
eq : size x ℕ.+ (ℕ.suc (size y)) ≡ size y ℕ.+ (ℕ.suc (size x)) |
| 1009 |
eq = ℕ-+-semigroupProperties.x∙yz≈z∙yx (size x) 1 _ |
| 1010 |
|
| 1011 |
|y|+1+|x|≤cnt = subst (ℕ._≤ cnt) eq |x|+1+|y|≤cnt |
| 1012 |
|
| 1013 |
|
| 1014 |
toℕ-isMagmaHomomorphism-* : IsMagmaHomomorphism *-rawMagma ℕ.*-rawMagma toℕ |
| 1015 |
toℕ-isMagmaHomomorphism-* = record |
| 1016 |
{ isRelHomomorphism = toℕ-isRelHomomorphism |
| 1017 |
; homo = toℕ-homo-* |
| 1018 |
} |
| 1019 |
|
| 1020 |
toℕ-isMonoidHomomorphism-* : IsMonoidHomomorphism *-1-rawMonoid ℕ.*-1-rawMonoid toℕ |
| 1021 |
toℕ-isMonoidHomomorphism-* = record |
| 1022 |
{ isMagmaHomomorphism = toℕ-isMagmaHomomorphism-* |
| 1023 |
; ε-homo = refl |
| 1024 |
} |
| 1025 |
|
| 1026 |
toℕ-isMonoidMonomorphism-* : IsMonoidMonomorphism *-1-rawMonoid ℕ.*-1-rawMonoid toℕ |
| 1027 |
toℕ-isMonoidMonomorphism-* = record |
| 1028 |
{ isMonoidHomomorphism = toℕ-isMonoidHomomorphism-* |
| 1029 |
; injective = toℕ-injective |
| 1030 |
} |
| 1031 |
|
| 1032 |
fromℕ-homo-* : ∀ m n → fromℕ (m ℕ.* n) ≡ fromℕ m * fromℕ n |
| 1033 |
fromℕ-homo-* m n = begin |
| 1034 |
fromℕ (m ℕ.* n) ≡⟨ cong fromℕ (cong₂ ℕ._*_ m≡aN n≡bN) ⟩ |
| 1035 |
fromℕ (toℕ a ℕ.* toℕ b) ≡⟨ cong fromℕ (sym (toℕ-homo-* a b)) ⟩ |
| 1036 |
fromℕ (toℕ (a * b)) ≡⟨ fromℕ-toℕ (a * b) ⟩ |
| 1037 |
a * b ∎ |
| 1038 |
where |
| 1039 |
open ≡-Reasoning |
| 1040 |
a = fromℕ m; b = fromℕ n |
| 1041 |
m≡aN = sym (toℕ-fromℕ m); n≡bN = sym (toℕ-fromℕ n) |
| 1042 |
|
| 1043 |
private |
| 1044 |
module *-Monomorphism = MonoidMonomorphism toℕ-isMonoidMonomorphism-* |
| 1045 |
|
| 1046 |
------------------------------------------------------------------------ |
| 1047 |
-- Algebraic properties of _*_ |
| 1048 |
|
| 1049 |
-- Mostly proved by using the isomorphism between `ℕ` and `ℕᵇ` provided |
| 1050 |
-- by `toℕ`/`fromℕ`. |
| 1051 |
|
| 1052 |
*-assoc : Associative _*_ |
| 1053 |
*-assoc = *-Monomorphism.assoc ℕ.*-isMagma ℕ.*-assoc |
| 1054 |
|
| 1055 |
*-comm : Commutative _*_ |
| 1056 |
*-comm = *-Monomorphism.comm ℕ.*-isMagma ℕ.*-comm |
| 1057 |
|
| 1058 |
*-identityˡ : LeftIdentity 1ᵇ _*_ |
| 1059 |
*-identityˡ = *-Monomorphism.identityˡ ℕ.*-isMagma ℕ.*-identityˡ |
| 1060 |
|
| 1061 |
*-identityʳ : RightIdentity 1ᵇ _*_ |
| 1062 |
*-identityʳ x = trans (*-comm x 1ᵇ) (*-identityˡ x) |
| 1063 |
|
| 1064 |
*-identity : Identity 1ᵇ _*_ |
| 1065 |
*-identity = (*-identityˡ , *-identityʳ) |
| 1066 |
|
| 1067 |
*-zeroˡ : LeftZero zero _*_ |
| 1068 |
*-zeroˡ _ = refl |
| 1069 |
|
| 1070 |
*-zeroʳ : RightZero zero _*_ |
| 1071 |
*-zeroʳ zero = refl |
| 1072 |
*-zeroʳ 2[1+ _ ] = refl |
| 1073 |
*-zeroʳ 1+[2 _ ] = refl |
| 1074 |
|
| 1075 |
*-zero : Zero zero _*_ |
| 1076 |
*-zero = *-zeroˡ , *-zeroʳ |
| 1077 |
|
| 1078 |
*-distribˡ-+ : _*_ DistributesOverˡ _+_ |
| 1079 |
*-distribˡ-+ a b c = begin |
| 1080 |
a * (b + c) ≡⟨ sym (fromℕ-toℕ (a * (b + c))) ⟩ |
| 1081 |
fromℕ (toℕ (a * (b + c))) ≡⟨ cong fromℕ (toℕ-homo-* a (b + c)) ⟩ |
| 1082 |
fromℕ (k ℕ.* (toℕ (b + c))) ≡⟨ cong (fromℕ ∘ (k ℕ.*_)) (toℕ-homo-+ b c) ⟩ |
| 1083 |
fromℕ (k ℕ.* (m ℕ.+ n)) ≡⟨ cong fromℕ (ℕ.*-distribˡ-+ k m n) ⟩ |
| 1084 |
fromℕ (k ℕ.* m ℕ.+ k ℕ.* n) ≡⟨ cong fromℕ $ sym $ |
| 1085 |
cong₂ ℕ._+_ (toℕ-homo-* a b) (toℕ-homo-* a c) ⟩ |
| 1086 |
fromℕ (toℕ (a * b) ℕ.+ toℕ (a * c)) ≡⟨ cong fromℕ (sym (toℕ-homo-+ (a * b) (a * c))) ⟩ |
| 1087 |
fromℕ (toℕ (a * b + a * c)) ≡⟨ fromℕ-toℕ (a * b + a * c) ⟩ |
| 1088 |
a * b + a * c ∎ |
| 1089 |
where open ≡-Reasoning; k = toℕ a; m = toℕ b; n = toℕ c |
| 1090 |
|
| 1091 |
*-distribʳ-+ : _*_ DistributesOverʳ _+_ |
| 1092 |
*-distribʳ-+ = comm∧distrˡ⇒distrʳ *-comm *-distribˡ-+ |
| 1093 |
|
| 1094 |
*-distrib-+ : _*_ DistributesOver _+_ |
| 1095 |
*-distrib-+ = *-distribˡ-+ , *-distribʳ-+ |
| 1096 |
|
| 1097 |
------------------------------------------------------------------------ |
| 1098 |
-- Structures |
| 1099 |
|
| 1100 |
*-isMagma : IsMagma _*_ |
| 1101 |
*-isMagma = isMagma _*_ |
| 1102 |
|
| 1103 |
*-isSemigroup : IsSemigroup _*_ |
| 1104 |
*-isSemigroup = *-Monomorphism.isSemigroup ℕ.*-isSemigroup |
| 1105 |
|
| 1106 |
*-1-isMonoid : IsMonoid _*_ 1ᵇ |
| 1107 |
*-1-isMonoid = *-Monomorphism.isMonoid ℕ.*-1-isMonoid |
| 1108 |
|
| 1109 |
*-1-isCommutativeMonoid : IsCommutativeMonoid _*_ 1ᵇ |
| 1110 |
*-1-isCommutativeMonoid = *-Monomorphism.isCommutativeMonoid ℕ.*-1-isCommutativeMonoid |
| 1111 |
|
| 1112 |
+-*-isSemiringWithoutAnnihilatingZero : IsSemiringWithoutAnnihilatingZero _+_ _*_ zero 1ᵇ |
| 1113 |
+-*-isSemiringWithoutAnnihilatingZero = record |
| 1114 |
{ +-isCommutativeMonoid = +-0-isCommutativeMonoid |
| 1115 |
; *-cong = cong₂ _*_ |
| 1116 |
; *-assoc = *-assoc |
| 1117 |
; *-identity = *-identity |
| 1118 |
; distrib = *-distrib-+ |
| 1119 |
} |
| 1120 |
|
| 1121 |
+-*-isSemiring : IsSemiring _+_ _*_ zero 1ᵇ |
| 1122 |
+-*-isSemiring = record |
| 1123 |
{ isSemiringWithoutAnnihilatingZero = +-*-isSemiringWithoutAnnihilatingZero |
| 1124 |
; zero = *-zero |
| 1125 |
} |
| 1126 |
|
| 1127 |
+-*-isCommutativeSemiring : IsCommutativeSemiring _+_ _*_ zero 1ᵇ |
| 1128 |
+-*-isCommutativeSemiring = record |
| 1129 |
{ isSemiring = +-*-isSemiring |
| 1130 |
; *-comm = *-comm |
| 1131 |
} |
| 1132 |
|
| 1133 |
------------------------------------------------------------------------ |
| 1134 |
-- Bundles |
| 1135 |
|
| 1136 |
*-magma : Magma 0ℓ 0ℓ |
| 1137 |
*-magma = record |
| 1138 |
{ isMagma = *-isMagma |
| 1139 |
} |
| 1140 |
|
| 1141 |
*-semigroup : Semigroup 0ℓ 0ℓ |
| 1142 |
*-semigroup = record |
| 1143 |
{ isSemigroup = *-isSemigroup |
| 1144 |
} |
| 1145 |
|
| 1146 |
*-1-monoid : Monoid 0ℓ 0ℓ |
| 1147 |
*-1-monoid = record |
| 1148 |
{ isMonoid = *-1-isMonoid |
| 1149 |
} |
| 1150 |
|
| 1151 |
*-1-commutativeMonoid : CommutativeMonoid 0ℓ 0ℓ |
| 1152 |
*-1-commutativeMonoid = record |
| 1153 |
{ isCommutativeMonoid = *-1-isCommutativeMonoid |
| 1154 |
} |
| 1155 |
|
| 1156 |
+-*-semiring : Semiring 0ℓ 0ℓ |
| 1157 |
+-*-semiring = record |
| 1158 |
{ isSemiring = +-*-isSemiring |
| 1159 |
} |
| 1160 |
|
| 1161 |
+-*-commutativeSemiring : CommutativeSemiring 0ℓ 0ℓ |
| 1162 |
+-*-commutativeSemiring = record |
| 1163 |
{ isCommutativeSemiring = +-*-isCommutativeSemiring |
| 1164 |
} |
| 1165 |
|
| 1166 |
------------------------------------------------------------------------ |
| 1167 |
-- Properties of _*_ and _≤_ & _<_ |
| 1168 |
|
| 1169 |
*-mono-≤ : _*_ Preserves₂ _≤_ ⟶ _≤_ ⟶ _≤_ |
| 1170 |
*-mono-≤ {x} {u} {y} {v} x≤u y≤v = toℕ-cancel-≤ (begin |
| 1171 |
toℕ (x * y) ≡⟨ toℕ-homo-* x y ⟩ |
| 1172 |
toℕ x ℕ.* toℕ y ≤⟨ ℕ.*-mono-≤ (toℕ-mono-≤ x≤u) (toℕ-mono-≤ y≤v) ⟩ |
| 1173 |
toℕ u ℕ.* toℕ v ≡⟨ sym (toℕ-homo-* u v) ⟩ |
| 1174 |
toℕ (u * v) ∎) |
| 1175 |
where open ℕ.≤-Reasoning |
| 1176 |
|
| 1177 |
*-monoʳ-≤ : ∀ x → (x *_) Preserves _≤_ ⟶ _≤_ |
| 1178 |
*-monoʳ-≤ x y≤y′ = *-mono-≤ (≤-refl {x}) y≤y′ |
| 1179 |
|
| 1180 |
*-monoˡ-≤ : ∀ x → (_* x) Preserves _≤_ ⟶ _≤_ |
| 1181 |
*-monoˡ-≤ x y≤y′ = *-mono-≤ y≤y′ (≤-refl {x}) |
| 1182 |
|
| 1183 |
*-mono-< : _*_ Preserves₂ _<_ ⟶ _<_ ⟶ _<_ |
| 1184 |
*-mono-< {x} {u} {y} {v} x<u y<v = toℕ-cancel-< (begin-strict |
| 1185 |
toℕ (x * y) ≡⟨ toℕ-homo-* x y ⟩ |
| 1186 |
toℕ x ℕ.* toℕ y <⟨ ℕ.*-mono-< (toℕ-mono-< x<u) (toℕ-mono-< y<v) ⟩ |
| 1187 |
toℕ u ℕ.* toℕ v ≡⟨ sym (toℕ-homo-* u v) ⟩ |
| 1188 |
toℕ (u * v) ∎) |
| 1189 |
where open ℕ.≤-Reasoning |
| 1190 |
|
| 1191 |
*-monoʳ-< : ∀ x → ((1ᵇ + x) *_) Preserves _<_ ⟶ _<_ |
| 1192 |
*-monoʳ-< x {y} {z} y<z = begin-strict |
| 1193 |
(1ᵇ + x) * y ≡⟨ *-distribʳ-+ y 1ᵇ x ⟩ |
| 1194 |
1ᵇ * y + x * y ≡⟨ cong (_+ x * y) (*-identityˡ y) ⟩ |
| 1195 |
y + x * y <⟨ +-mono-<-≤ y<z (*-monoʳ-≤ x (<⇒≤ y<z)) ⟩ |
| 1196 |
z + x * z ≡⟨ cong (_+ x * z) (sym (*-identityˡ z)) ⟩ |
| 1197 |
1ᵇ * z + x * z ≡⟨ sym (*-distribʳ-+ z 1ᵇ x) ⟩ |
| 1198 |
(1ᵇ + x) * z ∎ |
| 1199 |
where open ≤-Reasoning |
| 1200 |
|
| 1201 |
*-monoˡ-< : ∀ x → (_* (1ᵇ + x)) Preserves _<_ ⟶ _<_ |
| 1202 |
*-monoˡ-< x {y} {z} y<z = begin-strict |
| 1203 |
y * (1ᵇ + x) ≡⟨ *-comm y (1ᵇ + x) ⟩ |
| 1204 |
(1ᵇ + x) * y <⟨ *-monoʳ-< x y<z ⟩ |
| 1205 |
(1ᵇ + x) * z ≡⟨ *-comm (1ᵇ + x) z ⟩ |
| 1206 |
z * (1ᵇ + x) ∎ |
| 1207 |
where open ≤-Reasoning |
| 1208 |
|
| 1209 |
------------------------------------------------------------------------ |
| 1210 |
-- Other properties of _*_ |
| 1211 |
|
| 1212 |
x*y≡0⇒x≡0∨y≡0 : ∀ x {y} → x * y ≡ zero → x ≡ zero ⊎ y ≡ zero |
| 1213 |
x*y≡0⇒x≡0∨y≡0 zero {_} _ = inj₁ refl |
| 1214 |
x*y≡0⇒x≡0∨y≡0 _ {zero} _ = inj₂ refl |
| 1215 |
|
| 1216 |
x≢0∧y≢0⇒x*y≢0 : ∀ {x y} → x ≢ zero → y ≢ zero → x * y ≢ zero |
| 1217 |
x≢0∧y≢0⇒x*y≢0 {x} {_} x≢0 y≢0 xy≡0 with x*y≡0⇒x≡0∨y≡0 x xy≡0 |
| 1218 |
... | inj₁ x≡0 = x≢0 x≡0 |
| 1219 |
... | inj₂ y≡0 = y≢0 y≡0 |
| 1220 |
|
| 1221 |
2*x≡x+x : ∀ x → 2ᵇ * x ≡ x + x |
| 1222 |
2*x≡x+x x = begin |
| 1223 |
2ᵇ * x ≡⟨⟩ |
| 1224 |
(1ᵇ + 1ᵇ) * x ≡⟨ *-distribʳ-+ x 1ᵇ 1ᵇ ⟩ |
| 1225 |
1ᵇ * x + 1ᵇ * x ≡⟨ cong₂ _+_ (*-identityˡ x) (*-identityˡ x) ⟩ |
| 1226 |
x + x ∎ |
| 1227 |
where open ≡-Reasoning |
| 1228 |
|
| 1229 |
1+-* : ∀ x y → (1ᵇ + x) * y ≡ y + x * y |
| 1230 |
1+-* x y = begin |
| 1231 |
(1ᵇ + x) * y ≡⟨ *-distribʳ-+ y 1ᵇ x ⟩ |
| 1232 |
1ᵇ * y + x * y ≡⟨ cong (_+ x * y) (*-identityˡ y) ⟩ |
| 1233 |
y + x * y ∎ |
| 1234 |
where open ≡-Reasoning |
| 1235 |
|
| 1236 |
*-1+ : ∀ x y → y * (1ᵇ + x) ≡ y + y * x |
| 1237 |
*-1+ x y = begin |
| 1238 |
y * (1ᵇ + x) ≡⟨ *-distribˡ-+ y 1ᵇ x ⟩ |
| 1239 |
y * 1ᵇ + y * x ≡⟨ cong (_+ y * x) (*-identityʳ y) ⟩ |
| 1240 |
y + y * x ∎ |
| 1241 |
where open ≡-Reasoning |
| 1242 |
|
| 1243 |
------------------------------------------------------------------------ |
| 1244 |
-- Properties of double |
| 1245 |
------------------------------------------------------------------------ |
| 1246 |
|
| 1247 |
double[x]≡0⇒x≡0 : double x ≡ zero → x ≡ zero |
| 1248 |
double[x]≡0⇒x≡0 {zero} _ = refl |
| 1249 |
|
| 1250 |
x≡0⇒double[x]≡0 : x ≡ 0ᵇ → double x ≡ 0ᵇ |
| 1251 |
x≡0⇒double[x]≡0 = cong double |
| 1252 |
|
| 1253 |
x≢0⇒double[x]≢0 : x ≢ zero → double x ≢ zero |
| 1254 |
x≢0⇒double[x]≢0 x≢0 = x≢0 ∘ double[x]≡0⇒x≡0 |
| 1255 |
|
| 1256 |
double≢1 : double x ≢ 1ᵇ |
| 1257 |
double≢1 {zero} () |
| 1258 |
|
| 1259 |
double≗2* : double ≗ 2ᵇ *_ |
| 1260 |
double≗2* x = toℕ-injective $ begin |
| 1261 |
toℕ (double x) ≡⟨ toℕ-double x ⟩ |
| 1262 |
2 ℕ.* (toℕ x) ≡⟨ sym (toℕ-homo-* 2ᵇ x) ⟩ |
| 1263 |
toℕ (2ᵇ * x) ∎ |
| 1264 |
where open ≡-Reasoning |
| 1265 |
|
| 1266 |
double-*-assoc : ∀ x y → (double x) * y ≡ double (x * y) |
| 1267 |
double-*-assoc x y = begin |
| 1268 |
(double x) * y ≡⟨ cong (_* y) (double≗2* x) ⟩ |
| 1269 |
(2ᵇ * x) * y ≡⟨ *-assoc 2ᵇ x y ⟩ |
| 1270 |
2ᵇ * (x * y) ≡⟨ sym (double≗2* (x * y)) ⟩ |
| 1271 |
double (x * y) ∎ |
| 1272 |
where open ≡-Reasoning |
| 1273 |
|
| 1274 |
double[x]≡x+x : ∀ x → double x ≡ x + x |
| 1275 |
double[x]≡x+x x = trans (double≗2* x) (2*x≡x+x x) |
| 1276 |
|
| 1277 |
double-distrib-+ : ∀ x y → double (x + y) ≡ double x + double y |
| 1278 |
double-distrib-+ x y = begin |
| 1279 |
double (x + y) ≡⟨ double≗2* (x + y) ⟩ |
| 1280 |
2ᵇ * (x + y) ≡⟨ *-distribˡ-+ 2ᵇ x y ⟩ |
| 1281 |
(2ᵇ * x) + (2ᵇ * y) ≡⟨ sym (cong₂ _+_ (double≗2* x) (double≗2* y)) ⟩ |
| 1282 |
double x + double y ∎ |
| 1283 |
where open ≡-Reasoning |
| 1284 |
|
| 1285 |
double-mono-≤ : double Preserves _≤_ ⟶ _≤_ |
| 1286 |
double-mono-≤ {x} {y} x≤y = begin |
| 1287 |
double x ≡⟨ double≗2* x ⟩ |
| 1288 |
2ᵇ * x ≤⟨ *-monoʳ-≤ 2ᵇ x≤y ⟩ |
| 1289 |
2ᵇ * y ≡⟨ sym (double≗2* y) ⟩ |
| 1290 |
double y ∎ |
| 1291 |
where open ≤-Reasoning |
| 1292 |
|
| 1293 |
double-mono-< : double Preserves _<_ ⟶ _<_ |
| 1294 |
double-mono-< {x} {y} x<y = begin-strict |
| 1295 |
double x ≡⟨ double≗2* x ⟩ |
| 1296 |
2ᵇ * x <⟨ *-monoʳ-< 1ᵇ x<y ⟩ |
| 1297 |
2ᵇ * y ≡⟨ sym (double≗2* y) ⟩ |
| 1298 |
double y ∎ |
| 1299 |
where open ≤-Reasoning |
| 1300 |
|
| 1301 |
double-cancel-≤ : ∀ {x y} → double x ≤ double y → x ≤ y |
| 1302 |
double-cancel-≤ {x} {y} 2x≤2y with <-cmp x y |
| 1303 |
... | tri< x<y _ _ = <⇒≤ x<y |
| 1304 |
... | tri≈ _ x≡y _ = ≤-reflexive x≡y |
| 1305 |
... | tri> _ _ x>y = contradiction 2x≤2y (<⇒≱ (double-mono-< x>y)) |
| 1306 |
|
| 1307 |
double-cancel-< : ∀ {x y} → double x < double y → x < y |
| 1308 |
double-cancel-< {x} {y} 2x<2y with <-cmp x y |
| 1309 |
... | tri< x<y _ _ = x<y |
| 1310 |
... | tri≈ _ refl _ = contradiction 2x<2y (<-irrefl refl) |
| 1311 |
... | tri> _ _ x>y = contradiction (double-mono-< x>y) (<⇒≯ 2x<2y) |
| 1312 |
|
| 1313 |
x<double[x] : ∀ x → x ≢ zero → x < double x |
| 1314 |
x<double[x] x x≢0 = begin-strict |
| 1315 |
x <⟨ x<x+y x (x≢0⇒x>0 x≢0) ⟩ |
| 1316 |
x + x ≡⟨ sym (double[x]≡x+x x) ⟩ |
| 1317 |
double x ∎ |
| 1318 |
where open ≤-Reasoning |
| 1319 |
|
| 1320 |
x≤double[x] : ∀ x → x ≤ double x |
| 1321 |
x≤double[x] x = begin |
| 1322 |
x ≤⟨ x≤x+y x x ⟩ |
| 1323 |
x + x ≡⟨ sym (double[x]≡x+x x) ⟩ |
| 1324 |
double x ∎ |
| 1325 |
where open ≤-Reasoning |
| 1326 |
|
| 1327 |
double-suc : ∀ x → double (suc x) ≡ 2ᵇ + double x |
| 1328 |
double-suc x = begin |
| 1329 |
double (suc x) ≡⟨ cong double (suc≗1+ x) ⟩ |
| 1330 |
double (1ᵇ + x) ≡⟨ double-distrib-+ 1ᵇ x ⟩ |
| 1331 |
2ᵇ + double x ∎ |
| 1332 |
where open ≡-Reasoning |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
------------------------------------------------------------------------ |
| 1337 |
-- Properties of suc |
| 1338 |
------------------------------------------------------------------------ |
| 1339 |
|
| 1340 |
suc≢0 : suc x ≢ zero |
| 1341 |
suc≢0 {zero} () |
| 1342 |
suc≢0 {2[1+ _ ]} () |
| 1343 |
suc≢0 {1+[2 _ ]} () |
| 1344 |
|
| 1345 |
suc-injective : Injective _≡_ _≡_ suc |
| 1346 |
suc-injective {zero} {zero} p = refl |
| 1347 |
suc-injective {zero} {2[1+ y ]} p = contradiction 1+[2 p ]-injective (suc≢0 ∘ sym) |
| 1348 |
suc-injective {2[1+ x ]} {zero} p = contradiction 1+[2 p ]-injective suc≢0 |
| 1349 |
suc-injective {2[1+ x ]} {2[1+ y ]} p = cong 2[1+_] (suc-injective 1+[2 p ]-injective) |
| 1350 |
suc-injective {1+[2 x ]} {1+[2 y ]} refl = refl |
| 1351 |
|
| 1352 |
2[1+_]-double-suc : 2[1+_] ≗ double ∘ suc |
| 1353 |
2[1+_]-double-suc zero = refl |
| 1354 |
2[1+_]-double-suc 2[1+ x ] = cong 2[1+_] (2[1+_]-double-suc x) |
| 1355 |
2[1+_]-double-suc 1+[2 x ] = refl |
| 1356 |
|
| 1357 |
1+[2_]-suc-double : 1+[2_] ≗ suc ∘ double |
| 1358 |
1+[2_]-suc-double zero = refl |
| 1359 |
1+[2_]-suc-double 2[1+ x ] = refl |
| 1360 |
1+[2_]-suc-double 1+[2 x ] = begin |
| 1361 |
1+[2 1+[2 x ] ] ≡⟨ cong 1+[2_] (1+[2_]-suc-double x) ⟩ |
| 1362 |
1+[2 (suc 2x) ] ≡⟨⟩ |
| 1363 |
suc 2[1+ 2x ] ≡⟨ cong suc (2[1+_]-double-suc 2x) ⟩ |
| 1364 |
suc (double (suc 2x)) ≡⟨ cong (suc ∘ double) (sym (1+[2_]-suc-double x)) ⟩ |
| 1365 |
suc (double 1+[2 x ]) ∎ |
| 1366 |
where open ≡-Reasoning; 2x = double x |
| 1367 |
|
| 1368 |
x+suc[y]≡suc[x]+y : ∀ x y → x + suc y ≡ suc x + y |
| 1369 |
x+suc[y]≡suc[x]+y x y = begin |
| 1370 |
x + suc y ≡⟨ +-comm x _ ⟩ |
| 1371 |
suc y + x ≡⟨ suc-+ y x ⟩ |
| 1372 |
suc (y + x) ≡⟨ cong suc (+-comm y x) ⟩ |
| 1373 |
suc (x + y) ≡⟨ sym (suc-+ x y) ⟩ |
| 1374 |
suc x + y ∎ |
| 1375 |
where open ≡-Reasoning |
| 1376 |
|
| 1377 |
0<suc : ∀ x → zero < suc x |
| 1378 |
0<suc x = x≢0⇒x>0 (suc≢0 {x}) |
| 1379 |
|
| 1380 |
x<suc[x] : ∀ x → x < suc x |
| 1381 |
x<suc[x] x = begin-strict |
| 1382 |
x <⟨ x<1+x x ⟩ |
| 1383 |
1ᵇ + x ≡⟨ sym (suc≗1+ x) ⟩ |
| 1384 |
suc x ∎ |
| 1385 |
where open ≤-Reasoning |
| 1386 |
|
| 1387 |
x≤suc[x] : ∀ x → x ≤ suc x |
| 1388 |
x≤suc[x] x = <⇒≤ (x<suc[x] x) |
| 1389 |
|
| 1390 |
x≢suc[x] : ∀ x → x ≢ suc x |
| 1391 |
x≢suc[x] x = <⇒≢ (x<suc[x] x) |
| 1392 |
|
| 1393 |
suc-mono-≤ : suc Preserves _≤_ ⟶ _≤_ |
| 1394 |
suc-mono-≤ {x} {y} x≤y = begin |
| 1395 |
suc x ≡⟨ suc≗1+ x ⟩ |
| 1396 |
1ᵇ + x ≤⟨ +-monoʳ-≤ 1ᵇ x≤y ⟩ |
| 1397 |
1ᵇ + y ≡⟨ sym (suc≗1+ y) ⟩ |
| 1398 |
suc y ∎ |
| 1399 |
where open ≤-Reasoning |
| 1400 |
|
| 1401 |
suc[x]≤y⇒x<y : ∀ {x y} → suc x ≤ y → x < y |
| 1402 |
suc[x]≤y⇒x<y {x} (inj₁ sx<y) = <-trans (x<suc[x] x) sx<y |
| 1403 |
suc[x]≤y⇒x<y {x} (inj₂ refl) = x<suc[x] x |
| 1404 |
|
| 1405 |
x<y⇒suc[x]≤y : ∀ {x y} → x < y → suc x ≤ y |
| 1406 |
x<y⇒suc[x]≤y {x} {y} x<y = begin |
| 1407 |
suc x ≡⟨ sym (fromℕ-toℕ (suc x)) ⟩ |
| 1408 |
fromℕ (toℕ (suc x)) ≡⟨ cong fromℕ (toℕ-suc x) ⟩ |
| 1409 |
fromℕ (ℕ.suc (toℕ x)) ≤⟨ fromℕ-mono-≤ (toℕ-mono-< x<y) ⟩ |
| 1410 |
fromℕ (toℕ y) ≡⟨ fromℕ-toℕ y ⟩ |
| 1411 |
y ∎ |
| 1412 |
where open ≤-Reasoning |
| 1413 |
|
| 1414 |
suc-* : ∀ x y → suc x * y ≡ y + x * y |
| 1415 |
suc-* x y = begin |
| 1416 |
suc x * y ≡⟨ cong (_* y) (suc≗1+ x) ⟩ |
| 1417 |
(1ᵇ + x) * y ≡⟨ 1+-* x y ⟩ |
| 1418 |
y + x * y ∎ |
| 1419 |
where open ≡-Reasoning |
| 1420 |
|
| 1421 |
*-suc : ∀ x y → x * suc y ≡ x + x * y |
| 1422 |
*-suc x y = begin |
| 1423 |
x * suc y ≡⟨ cong (x *_) (suc≗1+ y) ⟩ |
| 1424 |
x * (1ᵇ + y) ≡⟨ *-1+ y x ⟩ |
| 1425 |
x + x * y ∎ |
| 1426 |
where open ≡-Reasoning |
| 1427 |
|
| 1428 |
x≤suc[y]*x : ∀ x y → x ≤ (suc y) * x |
| 1429 |
x≤suc[y]*x x y = begin |
| 1430 |
x ≤⟨ x≤x+y x (y * x) ⟩ |
| 1431 |
x + y * x ≡⟨ sym (suc-* y x) ⟩ |
| 1432 |
(suc y) * x ∎ |
| 1433 |
where open ≤-Reasoning |
| 1434 |
|
| 1435 |
suc[x]≤double[x] : ∀ x → x ≢ zero → suc x ≤ double x |
| 1436 |
suc[x]≤double[x] x = x<y⇒suc[x]≤y {x} {double x} ∘ x<double[x] x |
| 1437 |
|
| 1438 |
suc[x]<2[1+x] : ∀ x → suc x < 2[1+ x ] |
| 1439 |
suc[x]<2[1+x] x = begin-strict |
| 1440 |
suc x <⟨ x<double[x] (suc x) suc≢0 ⟩ |
| 1441 |
double (suc x) ≡⟨ sym (2[1+_]-double-suc x) ⟩ |
| 1442 |
2[1+ x ] ∎ |
| 1443 |
where open ≤-Reasoning |
| 1444 |
|
| 1445 |
double[x]<1+[2x] : ∀ x → double x < 1+[2 x ] |
| 1446 |
double[x]<1+[2x] x = begin-strict |
| 1447 |
double x <⟨ x<suc[x] (double x) ⟩ |
| 1448 |
suc (double x) ≡⟨ sym (1+[2_]-suc-double x) ⟩ |
| 1449 |
1+[2 x ] ∎ |
| 1450 |
where open ≤-Reasoning |
| 1451 |
|
| 1452 |
------------------------------------------------------------------------ |
| 1453 |
-- Properties of pred |
| 1454 |
------------------------------------------------------------------------ |
| 1455 |
|
| 1456 |
pred-suc : pred ∘ suc ≗ id |
| 1457 |
pred-suc zero = refl |
| 1458 |
pred-suc 2[1+ x ] = sym (2[1+_]-double-suc x) |
| 1459 |
pred-suc 1+[2 x ] = refl |
| 1460 |
|
| 1461 |
suc-pred : x ≢ zero → suc (pred x) ≡ x |
| 1462 |
suc-pred {zero} 0≢0 = contradiction refl 0≢0 |
| 1463 |
suc-pred {2[1+ _ ]} _ = refl |
| 1464 |
suc-pred {1+[2 x ]} _ = sym (1+[2_]-suc-double x) |
| 1465 |
|
| 1466 |
pred-mono-≤ : pred Preserves _≤_ ⟶ _≤_ |
| 1467 |
pred-mono-≤ {x} {y} x≤y = begin |
| 1468 |
pred x ≡⟨ cong pred (sym (fromℕ-toℕ x)) ⟩ |
| 1469 |
pred (fromℕ m) ≡⟨ sym (fromℕ-pred m) ⟩ |
| 1470 |
fromℕ (ℕ.pred m) ≤⟨ fromℕ-mono-≤ (ℕ.pred-mono-≤ (toℕ-mono-≤ x≤y)) ⟩ |
| 1471 |
fromℕ (ℕ.pred n) ≡⟨ fromℕ-pred n ⟩ |
| 1472 |
pred (fromℕ n) ≡⟨ cong pred (fromℕ-toℕ y) ⟩ |
| 1473 |
pred y ∎ |
| 1474 |
where |
| 1475 |
open ≤-Reasoning; m = toℕ x; n = toℕ y |
| 1476 |
|
| 1477 |
pred[x]<x : x ≢ zero → pred x < x |
| 1478 |
pred[x]<x {x} x≢0 = begin-strict |
| 1479 |
pred x <⟨ x<suc[x] (pred x) ⟩ |
| 1480 |
suc (pred x) ≡⟨ suc-pred x≢0 ⟩ |
| 1481 |
x ∎ |
| 1482 |
where open ≤-Reasoning |
| 1483 |
|
| 1484 |
pred[x]+y≡x+pred[y] : ∀ {x y} → x ≢ 0ᵇ → y ≢ 0ᵇ → (pred x) + y ≡ x + pred y |
| 1485 |
pred[x]+y≡x+pred[y] {x} {y} x≢0 y≢0 = begin |
| 1486 |
px + y ≡⟨ cong (px +_) (sym (suc-pred y≢0)) ⟩ |
| 1487 |
px + suc py ≡⟨ cong (px +_) (suc≗1+ py) ⟩ |
| 1488 |
px + (1ᵇ + py) ≡⟨ Bin+CSemigroup.x∙yz≈yx∙z px 1ᵇ py ⟩ |
| 1489 |
(1ᵇ + px) + py ≡⟨ cong (_+ py) (sym (suc≗1+ px)) ⟩ |
| 1490 |
(suc px) + py ≡⟨ cong (_+ py) (suc-pred x≢0) ⟩ |
| 1491 |
x + py ∎ |
| 1492 |
where open ≡-Reasoning; px = pred x; py = pred y |
| 1493 |
|
| 1494 |
|
| 1495 |
------------------------------------------------------------------------ |
| 1496 |
-- Properties of size |
| 1497 |
------------------------------------------------------------------------ |
| 1498 |
|
| 1499 |
|x|≡0⇒x≡0 : size x ≡ 0 → x ≡ 0ᵇ |
| 1500 |
|x|≡0⇒x≡0 {zero} refl = refl |
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
------------------------------------------------------------------------ |
| 1505 |
-- DEPRECATED NAMES |
| 1506 |
------------------------------------------------------------------------ |
| 1507 |
-- Please use the new names as continuing support for the old names is |
| 1508 |
-- not guaranteed. |
| 1509 |
|
| 1510 |
-- Version 1.4 |
| 1511 |
|
| 1512 |
*-+-isSemiringWithoutAnnihilatingZero = +-*-isSemiringWithoutAnnihilatingZero |
| 1513 |
{-# WARNING_ON_USAGE *-+-isSemiringWithoutAnnihilatingZero |
| 1514 |
"Warning: *-+-isSemiringWithoutAnnihilatingZero was deprecated in v1.4. |
| 1515 |
Please use +-*-isSemiringWithoutAnnihilatingZero instead." |
| 1516 |
#-} |
| 1517 |
*-+-isSemiring = +-*-isSemiring |
| 1518 |
{-# WARNING_ON_USAGE *-+-isSemiring |
| 1519 |
"Warning: *-+-isSemiring was deprecated in v1.4. |
| 1520 |
Please use +-*-isSemiring instead." |
| 1521 |
#-} |
| 1522 |
*-+-isCommutativeSemiring = +-*-isCommutativeSemiring |
| 1523 |
{-# WARNING_ON_USAGE *-+-isCommutativeSemiring |
| 1524 |
"Warning: *-+-isCommutativeSemiring was deprecated in v1.4. |
| 1525 |
Please use +-*-isCommutativeSemiring instead." |
| 1526 |
#-} |
| 1527 |
*-+-semiring = +-*-semiring |
| 1528 |
{-# WARNING_ON_USAGE *-+-semiring |
| 1529 |
"Warning: *-+-semiring was deprecated in v1.4. |
| 1530 |
Please use +-*-semiring instead." |
| 1531 |
#-} |
| 1532 |
*-+-commutativeSemiring = +-*-commutativeSemiring |
| 1533 |
{-# WARNING_ON_USAGE *-+-commutativeSemiring |
| 1534 |
"Warning: *-+-commutativeSemiring was deprecated in v1.4. |
| 1535 |
Please use +-*-commutativeSemiring instead." |
| 1536 |
#-} |
| 1537 |
|
| 1538 |
-- Version 2.0 |
| 1539 |
|
| 1540 |
{- issue1858/issue1755: raw bundles have moved to `Data.X.Base` -} |
| 1541 |
open Data.Nat.Binary.Base public |
| 1542 |
using (+-rawMagma; +-0-rawMonoid; *-rawMagma; *-1-rawMonoid) |