{-# OPTIONS --warning=error --safe --without-K #-} open import LogicalFormulae open import Lists.Lists open import Numbers.Naturals.Semiring open import Numbers.Naturals.Naturals open import Numbers.Naturals.Order open import Numbers.BinaryNaturals.Definition open import Semirings.Definition open import Orders.Total.Definition module Numbers.BinaryNaturals.Addition where -- Define the monoid structure, and show that it's the same as ℕ's _+Binherit_ : BinNat → BinNat → BinNat a +Binherit b = NToBinNat (binNatToN a +N binNatToN b) _+B_ : BinNat → BinNat → BinNat [] +B b = b (x :: a) +B [] = x :: a (zero :: xs) +B (y :: ys) = y :: (xs +B ys) (one :: xs) +B (zero :: ys) = one :: (xs +B ys) (one :: xs) +B (one :: ys) = zero :: incr (xs +B ys) +BCommutative : (a b : BinNat) → a +B b ≡ b +B a +BCommutative [] [] = refl +BCommutative [] (x :: b) = refl +BCommutative (x :: a) [] = refl +BCommutative (zero :: as) (zero :: bs) rewrite +BCommutative as bs = refl +BCommutative (zero :: as) (one :: bs) rewrite +BCommutative as bs = refl +BCommutative (one :: as) (zero :: bs) rewrite +BCommutative as bs = refl +BCommutative (one :: as) (one :: bs) rewrite +BCommutative as bs = refl +BIsInherited[] : (b : BinNat) (prB : b ≡ canonical b) → [] +Binherit b ≡ [] +B b +BIsInherited[] [] prB = refl +BIsInherited[] (zero :: b) prB = t where refine : (b : BinNat) → zero :: b ≡ canonical (zero :: b) → b ≡ canonical b refine b pr with canonical b refine b pr | x :: bl = ::Inj pr t : NToBinNat (0 +N binNatToN (zero :: b)) ≡ zero :: b t with TotalOrder.totality ℕTotalOrder 0 (binNatToN b) t | inl (inl pos) = transitivity (doubleIsBitShift (binNatToN b) pos) (applyEquality (zero ::_) (transitivity (binToBin b) (equalityCommutative (refine b prB)))) t | inl (inr ()) ... | inr eq with binNatToNZero b (equalityCommutative eq) ... | u with canonical b t | inr eq | u | [] = exFalso (bad b prB) where bad : (c : BinNat) → zero :: c ≡ [] → False bad c () t | inr eq | () | x :: bl +BIsInherited[] (one :: b) prB = ans where ans : NToBinNat (binNatToN (one :: b)) ≡ one :: b ans = transitivity (binToBin (one :: b)) (equalityCommutative prB) -- Show that the monoid structure of ℕ is the same as that of BinNat +BIsInherited : (a b : BinNat) (prA : a ≡ canonical a) (prB : b ≡ canonical b) → a +Binherit b ≡ a +B b +BinheritLemma : (a : BinNat) (b : BinNat) (prA : a ≡ canonical a) (prB : b ≡ canonical b) → incr (NToBinNat ((binNatToN a +N binNatToN b) +N ((binNatToN a +N binNatToN b) +N zero))) ≡ one :: (a +B b) +BIsInherited' : (a b : BinNat) → a +Binherit b ≡ canonical (a +B b) +BinheritLemma a b prA prB with TotalOrder.totality ℕTotalOrder 0 (binNatToN a +N binNatToN b) +BinheritLemma a b prA prB | inl (inl x) rewrite doubleIsBitShift (binNatToN a +N binNatToN b) x = applyEquality (one ::_) (+BIsInherited a b prA prB) +BinheritLemma a b prA prB | inr x with sumZeroImpliesSummandsZero (equalityCommutative x) +BinheritLemma a b prA prB | inr x | fst ,, snd = ans2 where bad : b ≡ [] bad = transitivity prB (binNatToNZero b snd) bad2 : a ≡ [] bad2 = transitivity prA (binNatToNZero a fst) ans2 : incr (NToBinNat ((binNatToN a +N binNatToN b) +N ((binNatToN a +N binNatToN b) +N zero))) ≡ one :: (a +B b) ans2 rewrite bad | bad2 = refl +BIsInherited [] b _ prB = +BIsInherited[] b prB +BIsInherited (x :: a) [] prA _ = transitivity (applyEquality NToBinNat (Semiring.commutative ℕSemiring (binNatToN (x :: a)) 0)) (transitivity (binToBin (x :: a)) (equalityCommutative prA)) +BIsInherited (zero :: as) (zero :: b) prA prB with TotalOrder.totality ℕTotalOrder 0 (binNatToN as +N binNatToN b) ... | inl (inl 0<) rewrite Semiring.commutative ℕSemiring (binNatToN as) 0 | Semiring.commutative ℕSemiring (binNatToN b) 0 | Semiring.+Associative ℕSemiring (binNatToN as +N binNatToN as) (binNatToN b) (binNatToN b) | equalityCommutative (Semiring.+Associative ℕSemiring (binNatToN as) (binNatToN as) (binNatToN b)) | Semiring.commutative ℕSemiring (binNatToN as) (binNatToN b) | Semiring.+Associative ℕSemiring (binNatToN as) (binNatToN b) (binNatToN as) | equalityCommutative (Semiring.+Associative ℕSemiring (binNatToN as +N binNatToN b) (binNatToN as) (binNatToN b)) | Semiring.commutative ℕSemiring 0 ((binNatToN as +N binNatToN b) +N (binNatToN as +N binNatToN b)) | equalityCommutative (Semiring.+Associative ℕSemiring (binNatToN as +N binNatToN b) (binNatToN as +N binNatToN b) 0) = transitivity (doubleIsBitShift (binNatToN as +N binNatToN b) (identityOfIndiscernablesRight _