diff --git a/Everything/Guardedness.agda b/Everything/Guardedness.agda index e750407..8c742f8 100644 --- a/Everything/Guardedness.agda +++ b/Everything/Guardedness.agda @@ -12,4 +12,6 @@ open import Fields.CauchyCompletion.Archimedean open import Sets.Cardinality.Infinite.Examples +open import ProjectEuler.Problem2 + module Everything.Guardedness where diff --git a/Lists/Lists.agda b/Lists/Lists.agda index 13bb81b..6d4440d 100644 --- a/Lists/Lists.agda +++ b/Lists/Lists.agda @@ -77,3 +77,9 @@ containsDecidable decide (x :: l) needle | inr x!=n | inr notIn = inr t t : ((x ≡ needle) || contains l needle) → False t (inl x) = x!=n x t (inr x) = notIn x + +filter' : {a b : _} {A : Set a} {pred : A → Set b} → (dec : (a : A) → pred a || (pred a → False)) → List A → List A +filter' dec [] = [] +filter' dec (x :: l) with dec x +... | inl _ = x :: filter' dec l +... | inr _ = filter' dec l diff --git a/LogicalFormulae.agda b/LogicalFormulae.agda index 3ad833f..0acefc9 100644 --- a/LogicalFormulae.agda +++ b/LogicalFormulae.agda @@ -60,6 +60,9 @@ liftEquality f .f x .x refl refl = refl applyEquality : {a : _} {b : _} {A : Set a} {B : Set b} (f : A → B) → {x y : A} → (x ≡ y) → ((f x) ≡ (f y)) applyEquality {A} {B} f {x} {.x} refl = refl +applyEquality2 : {a b c : _} {A : Set a} {B : Set b} {C : Set c} (f : A → B → C) → {x y : A} → (x ≡ y) → {m n : B} → (m ≡ n) → f x m ≡ f y n +applyEquality2 f x=y m=n rewrite x=y | m=n = refl + identityOfIndiscernablesLeft : {m n o : _} {A : Set m} {B : Set n} {a : A} {b : B} {c : A} → (prop : A → B → Set o) → (prop a b) → (a ≡ c) → (prop c b) identityOfIndiscernablesLeft {a = a} {b} {.a} prop pAB refl = pAB diff --git a/Numbers/BinaryNaturals/Addition.agda b/Numbers/BinaryNaturals/Addition.agda index 052b61f..a0a7c84 100644 --- a/Numbers/BinaryNaturals/Addition.agda +++ b/Numbers/BinaryNaturals/Addition.agda @@ -32,28 +32,29 @@ _+B_ : BinNat → BinNat → BinNat +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) +private + +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 @@ -198,3 +199,9 @@ _+B_ : BinNat → BinNat → BinNat where ans2 : zero :: incr (as +Binherit bs) ≡ canonical (zero :: incr (as +B bs)) ans2 rewrite +BIsInherited' as bs | equalityCommutative (incrPreservesCanonical' (as +B bs)) | canonicalAscends' {zero} (incr (as +B bs)) (incrNonzero (as +B bs)) = refl + ++BIsHom : (a b : BinNat) → binNatToN (a +B b) ≡ (binNatToN a) +N (binNatToN b) ++BIsHom a b = transitivity (equalityCommutative (binNatToNIsCanonical (a +B b))) (transitivity (equalityCommutative (applyEquality binNatToN (+BIsInherited' a b))) (nToN _)) + +sumCanonical : (a b : BinNat) → canonical a ≡ a → canonical b ≡ b → canonical (a +B b) ≡ a +B b +sumCanonical a b a=a b=b = transitivity (equalityCommutative (+BIsInherited' a b)) (+BIsInherited a b (equalityCommutative a=a) (equalityCommutative b=b)) diff --git a/Numbers/BinaryNaturals/Order.agda b/Numbers/BinaryNaturals/Order.agda index d9b4365..517a221 100644 --- a/Numbers/BinaryNaturals/Order.agda +++ b/Numbers/BinaryNaturals/Order.agda @@ -6,6 +6,7 @@ open import Numbers.Naturals.Order open import Numbers.Naturals.Order.Lemmas open import Numbers.Naturals.Semiring open import Numbers.BinaryNaturals.Definition +open import Orders.Partial.Definition open import Orders.Total.Definition open import Semirings.Definition @@ -33,38 +34,38 @@ a 0 rewrite binNatToNZero' b b=0 = naughtE b>0 - chopFirstBit : (m n : BinNat) {b : Bit} (s : Compare) → go Set a where [] : Vec X zero _,-_ : {n : ℕ} -> X -> Vec X n -> Vec X (succ n) +infixr 10 _,-_ + vecLen : {a : _} {X : Set a} {n : ℕ} → Vec X n → ℕ vecLen {a} {X} {n} v = n