@@ -28,7 +28,7 @@ open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny)
2828open import Data.Nat.Base
2929open import Data.Nat.Properties
3030open import Data.Product.Base as Product
31- using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>)
31+ using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; swap; <_,_>)
3232import Data.Product.Relation.Unary.All as Product using (All)
3333open import Data.Sum using (_⊎_; inj₁; inj₂; isInj₁; isInj₂)
3434open import Data.These.Base as These using (These; this; that; these)
@@ -47,7 +47,7 @@ open import Relation.Nullary.Negation.Core using (contradiction; ¬_)
4747open import Relation.Nullary.Decidable as Decidable
4848 using (isYes; map′; ⌊_⌋; ¬?; _×-dec_; dec-true; dec-false)
4949open import Relation.Unary using (Pred; Decidable; ∁; _≐_)
50- open import Relation.Unary.Properties using (∁?)
50+ open import Relation.Unary.Properties using (∁?; _∩?_ )
5151import Data.Nat.GeneralisedArithmetic as ℕ
5252
5353open ≡-Reasoning
@@ -1258,6 +1258,12 @@ module _ {P : Pred A p} (P? : Decidable P) where
12581258 ... | true = cong (x ∷_) ih
12591259 ... | false = ih
12601260
1261+ filter-map : (f : B → A) → filter P? ∘ map f ≗ map f ∘ filter (P? ∘ f)
1262+ filter-map f [] = refl
1263+ filter-map f (x ∷ xs) with ih ← filter-map f xs | does (P? (f x))
1264+ ... | true = cong (f x ∷_) ih
1265+ ... | false = ih
1266+
12611267module _ {P : Pred A p} {Q : Pred A q} (P? : Decidable P) (Q? : Decidable Q) where
12621268
12631269 filter-≐ : P ≐ Q → filter P? ≗ filter Q?
@@ -1266,6 +1272,25 @@ module _ {P : Pred A p} {Q : Pred A q} (P? : Decidable P) (Q? : Decidable Q) whe
12661272 ... | yes P[x] = trans (cong (x ∷_) (filter-≐ P≐Q xs)) (sym (filter-accept Q? (proj₁ P≐Q P[x])))
12671273 ... | no ¬P[x] = trans (filter-≐ P≐Q xs) (sym (filter-reject Q? (¬P[x] ∘ proj₂ P≐Q)))
12681274
1275+ filter-∩ : filter (P? ∩? Q?) ≗ filter P? ∘ filter Q?
1276+ filter-∩ [] = refl
1277+ filter-∩ (x ∷ xs) with ih ← filter-∩ xs | P? x | Q? x
1278+ ... | yes P[x] | yes _ = trans (cong (x ∷_) ih) (sym (filter-accept P? P[x]))
1279+ ... | no ¬P[x] | yes _ = trans ih (sym (filter-reject P? ¬P[x]))
1280+ ... | yes _ | no _ = ih
1281+ ... | no _ | no _ = ih
1282+
1283+
1284+ module _ {P : Pred A p} {Q : Pred A q} (P? : Decidable P) (Q? : Decidable Q) where
1285+
1286+ filter-swap : filter P? ∘ filter Q? ≗ filter Q? ∘ filter P?
1287+ filter-swap xs = begin
1288+ filter P? (filter Q? xs) ≡⟨ filter-∩ P? Q? xs ⟨
1289+ filter (P? ∩? Q?) xs ≡⟨ filter-≐ (P? ∩? Q?) (Q? ∩? P?) (swap , swap) xs ⟩
1290+ filter (Q? ∩? P?) xs ≡⟨ filter-∩ Q? P? xs ⟩
1291+ filter Q? (filter P? xs) ∎
1292+
1293+
12691294------------------------------------------------------------------------
12701295-- derun and deduplicate
12711296
0 commit comments