Skip to content

Commit 61903c8

Browse files
[ add ] filter-∩, filter-map and filter-swap for Data.List.Properties (#2942)
* [ add ] `filter-∩` and `filter-swap` for `Data.List.Properties` * [ add ] `filter-map` to `Data.List.Properties` * also introduced minor readibility rewrites for `filter-∩` and `filter-swap` * [ add ] CHANGELOG entry for `filter-map`, `filter-∩` and `filter-swap` * [ meta-fix ] adjusted changelog formatting * [ refactor ] naming convention of arbitrary lists Co-authored-by: jamesmckinna <[email protected]> --------- Co-authored-by: jamesmckinna <[email protected]>
1 parent 3771bf4 commit 61903c8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ Additions to existing modules
259259
map : P ⊆ Q → All P xs → All Q xs
260260
```
261261

262+
* In `Data.List.Properties`:
263+
```
264+
filter-map : filter P? ∘ map f ≗ map f ∘ filter (P? ∘ f)
265+
filter-∩ : filter (P? ∩? Q?) ≗ filter P? ∘ filter Q?
266+
filter-swap : filter P? ∘ filter Q? ≗ filter Q? ∘ filter P?
267+
```
268+
262269
* In `Data.Nat.Divisiblity`:
263270
```agad
264271
m∣n⇒m^o∣n^o : ∀ o → m ∣ n → m ^ o ∣ n ^ o

src/Data/List/Properties.agda

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny)
2828
open import Data.Nat.Base
2929
open import Data.Nat.Properties
3030
open import Data.Product.Base as Product
31-
using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>)
31+
using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; swap; <_,_>)
3232
import Data.Product.Relation.Unary.All as Product using (All)
3333
open import Data.Sum using (_⊎_; inj₁; inj₂; isInj₁; isInj₂)
3434
open import Data.These.Base as These using (These; this; that; these)
@@ -47,7 +47,7 @@ open import Relation.Nullary.Negation.Core using (contradiction; ¬_)
4747
open import Relation.Nullary.Decidable as Decidable
4848
using (isYes; map′; ⌊_⌋; ¬?; _×-dec_; dec-true; dec-false)
4949
open import Relation.Unary using (Pred; Decidable; ∁; _≐_)
50-
open import Relation.Unary.Properties using (∁?)
50+
open import Relation.Unary.Properties using (∁?; _∩?_)
5151
import Data.Nat.GeneralisedArithmetic as ℕ
5252

5353
open ≡-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+
12611267
module _ {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

Comments
 (0)