Skip to content

Commit 17c84f4

Browse files
authored
More list properties about catMaybes/mapMaybe (#2389)
* More list properties about `catMaybes/mapMaybe` - Follow-up to PR #2361 - Ported from my [formal-prelude](https://github.com/omelkonian/formal-prelude/tree/c10fe94876a7378088f2e4cf68d9b18858dcc256) * Revert irrefutable `with`s for inductive hypotheses.
1 parent b822650 commit 17c84f4

File tree

5 files changed

+217
-78
lines changed

5 files changed

+217
-78
lines changed

CHANGELOG.md

+58-42
Original file line numberDiff line numberDiff line change
@@ -414,47 +414,55 @@ Additions to existing modules
414414

415415
* In `Data.List.Properties`:
416416
```agda
417-
length-catMaybes : length (catMaybes xs) ≤ length xs
418-
applyUpTo-∷ʳ : applyUpTo f n ∷ʳ f n ≡ applyUpTo f (suc n)
419-
applyDownFrom-∷ʳ : applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡ applyDownFrom f (suc n)
420-
upTo-∷ʳ : upTo n ∷ʳ n ≡ upTo (suc n)
421-
downFrom-∷ʳ : applyDownFrom suc n ∷ʳ 0 ≡ downFrom (suc n)
422-
reverse-selfInverse : SelfInverse {A = List A} _≡_ reverse
423-
reverse-applyUpTo : reverse (applyUpTo f n) ≡ applyDownFrom f n
424-
reverse-upTo : reverse (upTo n) ≡ downFrom n
425-
reverse-applyDownFrom : reverse (applyDownFrom f n) ≡ applyUpTo f n
426-
reverse-downFrom : reverse (downFrom n) ≡ upTo n
427-
mapMaybe-map : mapMaybe f ∘ map g ≗ mapMaybe (f ∘ g)
428-
map-mapMaybe : map g ∘ mapMaybe f ≗ mapMaybe (Maybe.map g ∘ f)
429-
align-map : align (map f xs) (map g ys) ≡ map (map f g) (align xs ys)
430-
zip-map : zip (map f xs) (map g ys) ≡ map (map f g) (zip xs ys)
431-
unzipWith-map : unzipWith f ∘ map g ≗ unzipWith (f ∘ g)
432-
map-unzipWith : map (map g) (map h) ∘ unzipWith f ≗ unzipWith (map g h ∘ f)
433-
unzip-map : unzip ∘ map (map f g) ≗ map (map f) (map g) ∘ unzip
434-
splitAt-map : splitAt n ∘ map f ≗ map (map f) (map f) ∘ splitAt n
435-
uncons-map : uncons ∘ map f ≗ map (map f (map f)) ∘ uncons
436-
last-map : last ∘ map f ≗ map f ∘ last
437-
tail-map : tail ∘ map f ≗ map (map f) ∘ tail
438-
mapMaybe-cong : f ≗ g → mapMaybe f ≗ mapMaybe g
439-
zipWith-cong : (∀ a b → f a b ≡ g a b) → ∀ as → zipWith f as ≗ zipWith g as
440-
unzipWith-cong : f ≗ g → unzipWith f ≗ unzipWith g
441-
foldl-cong : (∀ x y → f x y ≡ g x y) → ∀ x → foldl f x ≗ foldl g x
442-
alignWith-flip : alignWith f xs ys ≡ alignWith (f ∘ swap) ys xs
443-
alignWith-comm : f ∘ swap ≗ f → alignWith f xs ys ≡ alignWith f ys xs
444-
align-flip : align xs ys ≡ map swap (align ys xs)
445-
zip-flip : zip xs ys ≡ map swap (zip ys xs)
446-
unzipWith-swap : unzipWith (swap ∘ f) ≗ swap ∘ unzipWith f
447-
unzip-swap : unzip ∘ map swap ≗ swap ∘ unzip
448-
take-take : take n (take m xs) ≡ take (n ⊓ m) xs
449-
take-drop : take n (drop m xs) ≡ drop m (take (m + n) xs)
450-
zip-unzip : uncurry′ zip ∘ unzip ≗ id
451-
unzipWith-zipWith : f ∘ uncurry′ g ≗ id → length xs ≡ length ys → unzipWith f (zipWith g xs ys) ≡ (xs , ys)
452-
unzip-zip : length xs ≡ length ys → unzip (zip xs ys) ≡ (xs , ys)
453-
mapMaybe-++ : mapMaybe f (xs ++ ys) ≡ mapMaybe f xs ++ mapMaybe f ys
454-
unzipWith-++ : unzipWith f (xs ++ ys) ≡ zip _++_ _++_ (unzipWith f xs) (unzipWith f ys)
455-
catMaybes-concatMap : catMaybes ≗ concatMap fromMaybe
456-
catMaybes-++ : catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
457-
map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
417+
length-catMaybes : length (catMaybes xs) ≤ length xs
418+
applyUpTo-∷ʳ : applyUpTo f n ∷ʳ f n ≡ applyUpTo f (suc n)
419+
applyDownFrom-∷ʳ : applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡ applyDownFrom f (suc n)
420+
upTo-∷ʳ : upTo n ∷ʳ n ≡ upTo (suc n)
421+
downFrom-∷ʳ : applyDownFrom suc n ∷ʳ 0 ≡ downFrom (suc n)
422+
reverse-selfInverse : SelfInverse {A = List A} _≡_ reverse
423+
reverse-applyUpTo : reverse (applyUpTo f n) ≡ applyDownFrom f n
424+
reverse-upTo : reverse (upTo n) ≡ downFrom n
425+
reverse-applyDownFrom : reverse (applyDownFrom f n) ≡ applyUpTo f n
426+
reverse-downFrom : reverse (downFrom n) ≡ upTo n
427+
mapMaybe-map : mapMaybe f ∘ map g ≗ mapMaybe (f ∘ g)
428+
map-mapMaybe : map g ∘ mapMaybe f ≗ mapMaybe (Maybe.map g ∘ f)
429+
align-map : align (map f xs) (map g ys) ≡ map (map f g) (align xs ys)
430+
zip-map : zip (map f xs) (map g ys) ≡ map (map f g) (zip xs ys)
431+
unzipWith-map : unzipWith f ∘ map g ≗ unzipWith (f ∘ g)
432+
map-unzipWith : map (map g) (map h) ∘ unzipWith f ≗ unzipWith (map g h ∘ f)
433+
unzip-map : unzip ∘ map (map f g) ≗ map (map f) (map g) ∘ unzip
434+
splitAt-map : splitAt n ∘ map f ≗ map (map f) (map f) ∘ splitAt n
435+
uncons-map : uncons ∘ map f ≗ map (map f (map f)) ∘ uncons
436+
last-map : last ∘ map f ≗ map f ∘ last
437+
tail-map : tail ∘ map f ≗ map (map f) ∘ tail
438+
mapMaybe-cong : f ≗ g → mapMaybe f ≗ mapMaybe g
439+
zipWith-cong : (∀ a b → f a b ≡ g a b) → ∀ as → zipWith f as ≗ zipWith g as
440+
unzipWith-cong : f ≗ g → unzipWith f ≗ unzipWith g
441+
foldl-cong : (∀ x y → f x y ≡ g x y) → ∀ x → foldl f x ≗ foldl g x
442+
alignWith-flip : alignWith f xs ys ≡ alignWith (f ∘ swap) ys xs
443+
alignWith-comm : f ∘ swap ≗ f → alignWith f xs ys ≡ alignWith f ys xs
444+
align-flip : align xs ys ≡ map swap (align ys xs)
445+
zip-flip : zip xs ys ≡ map swap (zip ys xs)
446+
unzipWith-swap : unzipWith (swap ∘ f) ≗ swap ∘ unzipWith f
447+
unzip-swap : unzip ∘ map swap ≗ swap ∘ unzip
448+
take-take : take n (take m xs) ≡ take (n ⊓ m) xs
449+
take-drop : take n (drop m xs) ≡ drop m (take (m + n) xs)
450+
zip-unzip : uncurry′ zip ∘ unzip ≗ id
451+
unzipWith-zipWith : f ∘ uncurry′ g ≗ id →
452+
length xs ≡ length ys →
453+
unzipWith f (zipWith g xs ys) ≡ (xs , ys)
454+
unzip-zip : length xs ≡ length ys → unzip (zip xs ys) ≡ (xs , ys)
455+
mapMaybe-++ : mapMaybe f (xs ++ ys) ≡ mapMaybe f xs ++ mapMaybe f ys
456+
unzipWith-++ : unzipWith f (xs ++ ys) ≡
457+
zip _++_ _++_ (unzipWith f xs) (unzipWith f ys)
458+
catMaybes-concatMap : catMaybes ≗ concatMap fromMaybe
459+
catMaybes-++ : catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
460+
map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
461+
Any-catMaybes⁺ : Any (M.Any P) xs → Any P (catMaybes xs)
462+
mapMaybeIsInj₁∘mapInj₁ : mapMaybe isInj₁ (map inj₁ xs) ≡ xs
463+
mapMaybeIsInj₁∘mapInj₂ : mapMaybe isInj₁ (map inj₂ xs) ≡ []
464+
mapMaybeIsInj₂∘mapInj₂ : mapMaybe isInj₂ (map inj₂ xs) ≡ xs
465+
mapMaybeIsInj₂∘mapInj₁ : mapMaybe isInj₂ (map inj₁ xs) ≡ []
458466
```
459467

460468
* In `Data.List.Relation.Binary.Sublist.Setoid.Properties`:
@@ -567,7 +575,15 @@ Additions to existing modules
567575

568576
* Added new proofs to `Data.List.Relation.Binary.Permutation.Propositional.Properties`:
569577
```agda
570-
product-↭ : product Preserves _↭_ ⟶ _≡_
578+
product-↭ : product Preserves _↭_ ⟶ _≡_
579+
catMaybes-↭ : xs ↭ ys → catMaybes xs ↭ catMaybes ys
580+
mapMaybe-↭ : xs ↭ ys → mapMaybe f xs ↭ mapMaybe f ys
581+
```
582+
583+
* Added new proofs to `Data.List.Relation.Binary.Permutation.Setoid.Properties.Maybe`:
584+
```agda
585+
catMaybes-↭ : xs ↭ ys → catMaybes xs ↭ catMaybes ys
586+
mapMaybe-↭ : xs ↭ ys → mapMaybe f xs ↭ mapMaybe f ys
571587
```
572588

573589
* In `Data.Rational.Properties`:

src/Data/List/Properties.agda

+66-34
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ open import Data.List.Base as List
2424
open import Data.List.Membership.Propositional using (_∈_)
2525
open import Data.List.Relation.Unary.All using (All; []; _∷_)
2626
open import Data.List.Relation.Unary.Any using (Any; here; there)
27-
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe)
27+
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
28+
open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny)
2829
open import Data.Nat.Base
2930
open import Data.Nat.Divisibility using (_∣_; divides; ∣n⇒∣m*n)
3031
open import Data.Nat.Properties
3132
open import Data.Product.Base as Product
3233
using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>)
3334
import Data.Product.Relation.Unary.All as Product using (All)
34-
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
35+
open import Data.Sum using (_⊎_; inj₁; inj₂; isInj₁; isInj₂)
3536
open import Data.These.Base as These using (These; this; that; these)
3637
open import Data.Fin.Properties using (toℕ-cast)
3738
open import Function.Base using (id; _∘_; _∘′_; _∋_; _-⟨_∣; ∣_⟩-_; _$_; const; flip)
@@ -49,12 +50,11 @@ open import Relation.Unary using (Pred; Decidable; ∁)
4950
open import Relation.Unary.Properties using (∁?)
5051
import Data.Nat.GeneralisedArithmetic as ℕ
5152

52-
5353
open ≡-Reasoning
5454

5555
private
5656
variable
57-
a b c d e p : Level
57+
a b c d e p : Level
5858
A : Set a
5959
B : Set b
6060
C : Set c
@@ -123,32 +123,6 @@ map-injective finj {x ∷ xs} {y ∷ ys} eq =
123123
let fx≡fy , fxs≡fys = ∷-injective eq in
124124
cong₂ _∷_ (finj fx≡fy) (map-injective finj fxs≡fys)
125125

126-
------------------------------------------------------------------------
127-
-- catMaybes
128-
129-
catMaybes-concatMap : catMaybes {A = A} ≗ concatMap fromMaybe
130-
catMaybes-concatMap [] = refl
131-
catMaybes-concatMap (just x ∷ xs) = cong (x ∷_) (catMaybes-concatMap xs)
132-
catMaybes-concatMap (nothing ∷ xs) = catMaybes-concatMap xs
133-
134-
length-catMaybes : xs length (catMaybes {A = A} xs) ≤ length xs
135-
length-catMaybes [] = ≤-refl
136-
length-catMaybes (just x ∷ xs) = s≤s (length-catMaybes xs)
137-
length-catMaybes (nothing ∷ xs) = m≤n⇒m≤1+n (length-catMaybes xs)
138-
139-
catMaybes-++ : (xs ys : List (Maybe A))
140-
catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
141-
catMaybes-++ [] ys = refl
142-
catMaybes-++ (just x ∷ xs) ys = cong (x ∷_) (catMaybes-++ xs ys)
143-
catMaybes-++ (nothing ∷ xs) ys = catMaybes-++ xs ys
144-
145-
module _ (f : A B) where
146-
147-
map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
148-
map-catMaybes [] = refl
149-
map-catMaybes (just x ∷ xs) = cong (f x ∷_) (map-catMaybes xs)
150-
map-catMaybes (nothing ∷ xs) = map-catMaybes xs
151-
152126
------------------------------------------------------------------------
153127
-- _++_
154128

@@ -742,12 +716,40 @@ map-concatMap f g xs = begin
742716
743717

744718
------------------------------------------------------------------------
745-
-- mapMaybe
719+
-- catMaybes
720+
721+
catMaybes-concatMap : catMaybes {A = A} ≗ concatMap fromMaybe
722+
catMaybes-concatMap [] = refl
723+
catMaybes-concatMap (just x ∷ xs) = cong (x ∷_) $ catMaybes-concatMap xs
724+
catMaybes-concatMap (nothing ∷ xs) = catMaybes-concatMap xs
725+
726+
length-catMaybes : xs length (catMaybes {A = A} xs) ≤ length xs
727+
length-catMaybes [] = ≤-refl
728+
length-catMaybes (just _ ∷ xs) = s≤s $ length-catMaybes xs
729+
length-catMaybes (nothing ∷ xs) = m≤n⇒m≤1+n $ length-catMaybes xs
746730

747-
module _ {f g : A Maybe B} where
731+
catMaybes-++ : (xs ys : List (Maybe A))
732+
catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
733+
catMaybes-++ [] _ = refl
734+
catMaybes-++ (just x ∷ xs) ys = cong (x ∷_) $ catMaybes-++ xs ys
735+
catMaybes-++ (nothing ∷ xs) ys = catMaybes-++ xs ys
748736

749-
mapMaybe-cong : f ≗ g mapMaybe f ≗ mapMaybe g
750-
mapMaybe-cong f≗g = cong catMaybes ∘ map-cong f≗g
737+
map-catMaybes : (f : A B) map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
738+
map-catMaybes _ [] = refl
739+
map-catMaybes f (just x ∷ xs) = cong (f x ∷_) $ map-catMaybes f xs
740+
map-catMaybes f (nothing ∷ xs) = map-catMaybes f xs
741+
742+
Any-catMaybes⁺ : {P : Pred A ℓ} {xs : List (Maybe A)}
743+
Any (MAny P) xs Any P (catMaybes xs)
744+
Any-catMaybes⁺ {xs = nothing ∷ xs} (there x∈) = Any-catMaybes⁺ x∈
745+
Any-catMaybes⁺ {xs = just x ∷ xs} (here (just px)) = here px
746+
Any-catMaybes⁺ {xs = just x ∷ xs} (there x∈) = there $ Any-catMaybes⁺ x∈
747+
748+
------------------------------------------------------------------------
749+
-- mapMaybe
750+
751+
mapMaybe-cong : {f g : A Maybe B} f ≗ g mapMaybe f ≗ mapMaybe g
752+
mapMaybe-cong f≗g = cong catMaybes ∘ map-cong f≗g
751753

752754
mapMaybe-just : (xs : List A) mapMaybe just xs ≡ xs
753755
mapMaybe-just [] = refl
@@ -793,6 +795,36 @@ module _ (g : B → C) (f : A → Maybe B) where
793795
mapMaybe (Maybe.map g) (map f xs) ≡⟨ mapMaybe-map _ f xs ⟩
794796
mapMaybe (Maybe.map g ∘ f) xs ∎
795797

798+
-- embedding-projection pairs
799+
module _ {proj : B Maybe A} {emb : A B} where
800+
mapMaybe-map-retract : proj ∘ emb ≗ just mapMaybe proj ∘ map emb ≗ id
801+
mapMaybe-map-retract retract xs = begin
802+
mapMaybe proj (map emb xs) ≡⟨ mapMaybe-map _ _ xs ⟩
803+
mapMaybe (proj ∘ emb) xs ≡⟨ mapMaybe-cong retract xs ⟩
804+
mapMaybe just xs ≡⟨ mapMaybe-just _ ⟩
805+
xs ∎
806+
807+
module _ {proj : C Maybe B} {emb : A C} where
808+
mapMaybe-map-none : proj ∘ emb ≗ const nothing mapMaybe proj ∘ map emb ≗ const []
809+
mapMaybe-map-none retract xs = begin
810+
mapMaybe proj (map emb xs) ≡⟨ mapMaybe-map _ _ xs ⟩
811+
mapMaybe (proj ∘ emb) xs ≡⟨ mapMaybe-cong retract xs ⟩
812+
mapMaybe (const nothing) xs ≡⟨ mapMaybe-nothing xs ⟩
813+
[] ∎
814+
815+
-- embedding-projection pairs on sums
816+
mapMaybeIsInj₁∘mapInj₁ : (xs : List A) mapMaybe (isInj₁ {B = B}) (map inj₁ xs) ≡ xs
817+
mapMaybeIsInj₁∘mapInj₁ = mapMaybe-map-retract λ _ refl
818+
819+
mapMaybeIsInj₁∘mapInj₂ : (xs : List B) mapMaybe (isInj₁ {A = A}) (map inj₂ xs) ≡ []
820+
mapMaybeIsInj₁∘mapInj₂ = mapMaybe-map-none λ _ refl
821+
822+
mapMaybeIsInj₂∘mapInj₂ : (xs : List B) mapMaybe (isInj₂ {A = A}) (map inj₂ xs) ≡ xs
823+
mapMaybeIsInj₂∘mapInj₂ = mapMaybe-map-retract λ _ refl
824+
825+
mapMaybeIsInj₂∘mapInj₁ : (xs : List A) mapMaybe (isInj₂ {B = B}) (map inj₁ xs) ≡ []
826+
mapMaybeIsInj₂∘mapInj₁ = mapMaybe-map-none λ _ refl
827+
796828
------------------------------------------------------------------------
797829
-- sum
798830

src/Data/List/Relation/Binary/Permutation/Propositional/Properties.agda

+22-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ open import Data.List.Membership.Propositional
2323
open import Data.List.Membership.Propositional.Properties
2424
import Data.List.Properties as Lₚ
2525
open import Data.Product.Base using (_,_; _×_; ∃; ∃₂)
26-
open import Function.Base using (_∘_; _⟨_⟩_)
26+
open import Data.Maybe.Base using (Maybe; just; nothing)
27+
open import Function.Base using (_∘_; _⟨_⟩_; _$_)
2728
open import Level using (Level)
2829
open import Relation.Unary using (Pred)
2930
open import Relation.Binary.Core using (Rel; _Preserves_⟶_; _Preserves₂_⟶_⟶_)
@@ -38,6 +39,7 @@ private
3839
a b p : Level
3940
A : Set a
4041
B : Set b
42+
xs ys : List A
4143

4244
------------------------------------------------------------------------
4345
-- Permutations of empty and singleton lists
@@ -373,3 +375,22 @@ product-↭ (swap {xs} {ys} x y r) = begin
373375
(y * x) * product ys ≡⟨ *-assoc y x (product ys) ⟩
374376
y * (x * product ys) ∎
375377
where open ≡-Reasoning
378+
379+
------------------------------------------------------------------------
380+
-- catMaybes
381+
382+
catMaybes-↭ : xs ↭ ys catMaybes xs ↭ catMaybes ys
383+
catMaybes-↭ refl = refl
384+
catMaybes-↭ (trans xs↭ ↭ys) = trans (catMaybes-↭ xs↭) (catMaybes-↭ ↭ys)
385+
catMaybes-↭ (prep nothing xs↭) = catMaybes-↭ xs↭
386+
catMaybes-↭ (prep (just x) xs↭) = prep x $ catMaybes-↭ xs↭
387+
catMaybes-↭ (swap nothing nothing xs↭) = catMaybes-↭ xs↭
388+
catMaybes-↭ (swap nothing (just y) xs↭) = prep y $ catMaybes-↭ xs↭
389+
catMaybes-↭ (swap (just x) nothing xs↭) = prep x $ catMaybes-↭ xs↭
390+
catMaybes-↭ (swap (just x) (just y) xs↭) = swap x y $ catMaybes-↭ xs↭
391+
392+
------------------------------------------------------------------------
393+
-- mapMaybe
394+
395+
mapMaybe-↭ : (f : A Maybe B) xs ↭ ys mapMaybe f xs ↭ mapMaybe f ys
396+
mapMaybe-↭ f = catMaybes-↭ ∘ map⁺ f

src/Data/List/Relation/Binary/Permutation/Setoid/Properties.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ steps-respʳ ys≋xs (trans ys↭ws ws↭zs) = cong (steps ys↭ws +_)
144144
------------------------------------------------------------------------
145145
-- map
146146

147-
module _ (T : Setoid b ℓ) where
147+
module _ {ℓ} (T : Setoid b ℓ) where
148148

149149
open Setoid T using () renaming (_≈_ to _≈′_)
150150
open Permutation T using () renaming (_↭_ to _↭′_)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of permutations using setoid equality (on Maybe elements)
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible --safe #-}
8+
9+
module Data.List.Relation.Binary.Permutation.Setoid.Properties.Maybe where
10+
11+
open import Relation.Binary.Core using (_Preserves_⟶_)
12+
open import Relation.Binary.Bundles using (Setoid)
13+
14+
open import Level using (Level)
15+
open import Function.Base using (_∘_; _$_)
16+
17+
open import Data.List.Base using (catMaybes; mapMaybe)
18+
open import Data.List.Relation.Binary.Pointwise using (Pointwise; []; _∷_)
19+
import Data.List.Relation.Binary.Permutation.Setoid as Permutation
20+
open import Data.List.Relation.Binary.Permutation.Setoid.Properties
21+
22+
open import Data.Maybe using (Maybe; nothing; just)
23+
open import Data.Maybe.Relation.Binary.Pointwise using (nothing; just)
24+
renaming (setoid to setoidᴹ)
25+
26+
private
27+
variable
28+
a b ℓ ℓ′ : Level
29+
30+
------------------------------------------------------------------------
31+
-- catMaybes
32+
33+
module _ (sᴬ : Setoid a ℓ) where
34+
open Setoid sᴬ using (_≈_)
35+
open Permutation sᴬ
36+
37+
private sᴹ = setoidᴹ sᴬ
38+
open Setoid sᴹ using () renaming (_≈_ to _≈ᴹ_)
39+
open Permutation sᴹ using () renaming (_↭_ to _↭ᴹ_)
40+
41+
catMaybes-↭ : {xs ys} xs ↭ᴹ ys catMaybes xs ↭ catMaybes ys
42+
catMaybes-↭ (refl p) = refl (pointwise p)
43+
where
44+
pointwise : {xs ys} Pointwise _≈ᴹ_ xs ys
45+
Pointwise _≈_ (catMaybes xs) (catMaybes ys)
46+
pointwise [] = []
47+
pointwise (just p ∷ ps) = p ∷ pointwise ps
48+
pointwise (nothing ∷ ps) = pointwise ps
49+
catMaybes-↭ (trans xs↭ ↭ys) = trans (catMaybes-↭ xs↭) (catMaybes-↭ ↭ys)
50+
catMaybes-↭ (prep nothing xs↭) = catMaybes-↭ xs↭
51+
catMaybes-↭ (prep (just x~y) xs↭) = prep x~y $ catMaybes-↭ xs↭
52+
catMaybes-↭ (swap nothing nothing xs↭) = catMaybes-↭ xs↭
53+
catMaybes-↭ (swap nothing (just y) xs↭) = prep y $ catMaybes-↭ xs↭
54+
catMaybes-↭ (swap (just x) nothing xs↭) = prep x $ catMaybes-↭ xs↭
55+
catMaybes-↭ (swap (just x) (just y) xs↭) = swap x y $ catMaybes-↭ xs↭
56+
57+
------------------------------------------------------------------------
58+
-- mapMaybe
59+
60+
module _ (sᴬ : Setoid a ℓ) (sᴮ : Setoid b ℓ′) where
61+
open Setoid sᴬ using () renaming (_≈_ to _≈ᴬ_)
62+
open Permutation sᴬ using () renaming (_↭_ to _↭ᴬ_)
63+
open Permutation sᴮ using () renaming (_↭_ to _↭ᴮ_)
64+
65+
private sᴹᴮ = setoidᴹ sᴮ
66+
open Setoid sᴹᴮ using () renaming (_≈_ to _≈ᴹᴮ_)
67+
68+
mapMaybe-↭ : {f} f Preserves _≈ᴬ_ ⟶ _≈ᴹᴮ_
69+
{xs ys} xs ↭ᴬ ys mapMaybe f xs ↭ᴮ mapMaybe f ys
70+
mapMaybe-↭ pres = catMaybes-↭ sᴮ ∘ map⁺ sᴬ sᴹᴮ pres

0 commit comments

Comments
 (0)