Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 381299a

Browse files
committedMay 23, 2024·
More list properties about catMaybes/mapMaybe
- Follow-up to PR #2361 - Ported from my [formal-prelude](https://github.com/omelkonian/formal-prelude/tree/c10fe94876a7378088f2e4cf68d9b18858dcc256)
1 parent c5255d0 commit 381299a

File tree

5 files changed

+225
-78
lines changed

5 files changed

+225
-78
lines changed
 

‎CHANGELOG.md

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

344344
* In `Data.List.Properties`:
345345
```agda
346-
length-catMaybes : length (catMaybes xs) ≤ length xs
347-
applyUpTo-∷ʳ : applyUpTo f n ∷ʳ f n ≡ applyUpTo f (suc n)
348-
applyDownFrom-∷ʳ : applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡ applyDownFrom f (suc n)
349-
upTo-∷ʳ : upTo n ∷ʳ n ≡ upTo (suc n)
350-
downFrom-∷ʳ : applyDownFrom suc n ∷ʳ 0 ≡ downFrom (suc n)
351-
reverse-selfInverse : SelfInverse {A = List A} _≡_ reverse
352-
reverse-applyUpTo : reverse (applyUpTo f n) ≡ applyDownFrom f n
353-
reverse-upTo : reverse (upTo n) ≡ downFrom n
354-
reverse-applyDownFrom : reverse (applyDownFrom f n) ≡ applyUpTo f n
355-
reverse-downFrom : reverse (downFrom n) ≡ upTo n
356-
mapMaybe-map : mapMaybe f ∘ map g ≗ mapMaybe (f ∘ g)
357-
map-mapMaybe : map g ∘ mapMaybe f ≗ mapMaybe (Maybe.map g ∘ f)
358-
align-map : align (map f xs) (map g ys) ≡ map (map f g) (align xs ys)
359-
zip-map : zip (map f xs) (map g ys) ≡ map (map f g) (zip xs ys)
360-
unzipWith-map : unzipWith f ∘ map g ≗ unzipWith (f ∘ g)
361-
map-unzipWith : map (map g) (map h) ∘ unzipWith f ≗ unzipWith (map g h ∘ f)
362-
unzip-map : unzip ∘ map (map f g) ≗ map (map f) (map g) ∘ unzip
363-
splitAt-map : splitAt n ∘ map f ≗ map (map f) (map f) ∘ splitAt n
364-
uncons-map : uncons ∘ map f ≗ map (map f (map f)) ∘ uncons
365-
last-map : last ∘ map f ≗ map f ∘ last
366-
tail-map : tail ∘ map f ≗ map (map f) ∘ tail
367-
mapMaybe-cong : f ≗ g → mapMaybe f ≗ mapMaybe g
368-
zipWith-cong : (∀ a b → f a b ≡ g a b) → ∀ as → zipWith f as ≗ zipWith g as
369-
unzipWith-cong : f ≗ g → unzipWith f ≗ unzipWith g
370-
foldl-cong : (∀ x y → f x y ≡ g x y) → ∀ x → foldl f x ≗ foldl g x
371-
alignWith-flip : alignWith f xs ys ≡ alignWith (f ∘ swap) ys xs
372-
alignWith-comm : f ∘ swap ≗ f → alignWith f xs ys ≡ alignWith f ys xs
373-
align-flip : align xs ys ≡ map swap (align ys xs)
374-
zip-flip : zip xs ys ≡ map swap (zip ys xs)
375-
unzipWith-swap : unzipWith (swap ∘ f) ≗ swap ∘ unzipWith f
376-
unzip-swap : unzip ∘ map swap ≗ swap ∘ unzip
377-
take-take : take n (take m xs) ≡ take (n ⊓ m) xs
378-
take-drop : take n (drop m xs) ≡ drop m (take (m + n) xs)
379-
zip-unzip : uncurry′ zip ∘ unzip ≗ id
380-
unzipWith-zipWith : f ∘ uncurry′ g ≗ id → length xs ≡ length ys → unzipWith f (zipWith g xs ys) ≡ (xs , ys)
381-
unzip-zip : length xs ≡ length ys → unzip (zip xs ys) ≡ (xs , ys)
382-
mapMaybe-++ : mapMaybe f (xs ++ ys) ≡ mapMaybe f xs ++ mapMaybe f ys
383-
unzipWith-++ : unzipWith f (xs ++ ys) ≡ zip _++_ _++_ (unzipWith f xs) (unzipWith f ys)
384-
catMaybes-concatMap : catMaybes ≗ concatMap fromMaybe
385-
catMaybes-++ : catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
386-
map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
346+
length-catMaybes : length (catMaybes xs) ≤ length xs
347+
applyUpTo-∷ʳ : applyUpTo f n ∷ʳ f n ≡ applyUpTo f (suc n)
348+
applyDownFrom-∷ʳ : applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡ applyDownFrom f (suc n)
349+
upTo-∷ʳ : upTo n ∷ʳ n ≡ upTo (suc n)
350+
downFrom-∷ʳ : applyDownFrom suc n ∷ʳ 0 ≡ downFrom (suc n)
351+
reverse-selfInverse : SelfInverse {A = List A} _≡_ reverse
352+
reverse-applyUpTo : reverse (applyUpTo f n) ≡ applyDownFrom f n
353+
reverse-upTo : reverse (upTo n) ≡ downFrom n
354+
reverse-applyDownFrom : reverse (applyDownFrom f n) ≡ applyUpTo f n
355+
reverse-downFrom : reverse (downFrom n) ≡ upTo n
356+
mapMaybe-map : mapMaybe f ∘ map g ≗ mapMaybe (f ∘ g)
357+
map-mapMaybe : map g ∘ mapMaybe f ≗ mapMaybe (Maybe.map g ∘ f)
358+
align-map : align (map f xs) (map g ys) ≡ map (map f g) (align xs ys)
359+
zip-map : zip (map f xs) (map g ys) ≡ map (map f g) (zip xs ys)
360+
unzipWith-map : unzipWith f ∘ map g ≗ unzipWith (f ∘ g)
361+
map-unzipWith : map (map g) (map h) ∘ unzipWith f ≗ unzipWith (map g h ∘ f)
362+
unzip-map : unzip ∘ map (map f g) ≗ map (map f) (map g) ∘ unzip
363+
splitAt-map : splitAt n ∘ map f ≗ map (map f) (map f) ∘ splitAt n
364+
uncons-map : uncons ∘ map f ≗ map (map f (map f)) ∘ uncons
365+
last-map : last ∘ map f ≗ map f ∘ last
366+
tail-map : tail ∘ map f ≗ map (map f) ∘ tail
367+
mapMaybe-cong : f ≗ g → mapMaybe f ≗ mapMaybe g
368+
zipWith-cong : (∀ a b → f a b ≡ g a b) → ∀ as → zipWith f as ≗ zipWith g as
369+
unzipWith-cong : f ≗ g → unzipWith f ≗ unzipWith g
370+
foldl-cong : (∀ x y → f x y ≡ g x y) → ∀ x → foldl f x ≗ foldl g x
371+
alignWith-flip : alignWith f xs ys ≡ alignWith (f ∘ swap) ys xs
372+
alignWith-comm : f ∘ swap ≗ f → alignWith f xs ys ≡ alignWith f ys xs
373+
align-flip : align xs ys ≡ map swap (align ys xs)
374+
zip-flip : zip xs ys ≡ map swap (zip ys xs)
375+
unzipWith-swap : unzipWith (swap ∘ f) ≗ swap ∘ unzipWith f
376+
unzip-swap : unzip ∘ map swap ≗ swap ∘ unzip
377+
take-take : take n (take m xs) ≡ take (n ⊓ m) xs
378+
take-drop : take n (drop m xs) ≡ drop m (take (m + n) xs)
379+
zip-unzip : uncurry′ zip ∘ unzip ≗ id
380+
unzipWith-zipWith : f ∘ uncurry′ g ≗ id →
381+
length xs ≡ length ys →
382+
unzipWith f (zipWith g xs ys) ≡ (xs , ys)
383+
unzip-zip : length xs ≡ length ys → unzip (zip xs ys) ≡ (xs , ys)
384+
mapMaybe-++ : mapMaybe f (xs ++ ys) ≡ mapMaybe f xs ++ mapMaybe f ys
385+
unzipWith-++ : unzipWith f (xs ++ ys) ≡
386+
zip _++_ _++_ (unzipWith f xs) (unzipWith f ys)
387+
catMaybes-concatMap : catMaybes ≗ concatMap fromMaybe
388+
catMaybes-++ : catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
389+
map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
390+
Any-catMaybes⁺ : Any (M.Any P) xs → Any P (catMaybes xs)
391+
mapMaybeIsInj₁∘mapInj₁ : mapMaybe isInj₁ (map inj₁ xs) ≡ xs
392+
mapMaybeIsInj₁∘mapInj₂ : mapMaybe isInj₁ (map inj₂ xs) ≡ []
393+
mapMaybeIsInj₂∘mapInj₂ : mapMaybe isInj₂ (map inj₂ xs) ≡ xs
394+
mapMaybeIsInj₂∘mapInj₁ : mapMaybe isInj₂ (map inj₁ xs) ≡ []
387395
```
388396

389397
* In `Data.List.Relation.Binary.Sublist.Setoid.Properties`:
@@ -496,7 +504,15 @@ Additions to existing modules
496504

497505
* Added new proofs to `Data.List.Relation.Binary.Permutation.Propositional.Properties`:
498506
```agda
499-
product-↭ : product Preserves _↭_ ⟶ _≡_
507+
product-↭ : product Preserves _↭_ ⟶ _≡_
508+
catMaybes-↭ : xs ↭ ys → catMaybes xs ↭ catMaybes ys
509+
mapMaybe-↭ : xs ↭ ys → mapMaybe f xs ↭ mapMaybe f ys
510+
```
511+
512+
* Added new proofs to `Data.List.Relation.Binary.Permutation.Setoid.Properties.Maybe`:
513+
```agda
514+
catMaybes-↭ : xs ↭ ys → catMaybes xs ↭ catMaybes ys
515+
mapMaybe-↭ : xs ↭ ys → mapMaybe f xs ↭ mapMaybe f ys
500516
```
501517

502518
* Added new functions in `Data.String.Base`:

‎src/Data/List/Properties.agda

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

51-
5252
open ≡-Reasoning
5353

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

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

@@ -741,12 +715,44 @@ map-concatMap f g xs = begin
741715
742716

743717
------------------------------------------------------------------------
744-
-- mapMaybe
718+
-- catMaybes
745719

746-
module _ {f g : A Maybe B} where
720+
catMaybes-concatMap : catMaybes {A = A} ≗ concatMap fromMaybe
721+
catMaybes-concatMap [] = refl
722+
catMaybes-concatMap (mx ∷ xs) with ih catMaybes-concatMap xs | mx
723+
... | just x = cong (x ∷_) ih
724+
... | nothing = ih
747725

748-
mapMaybe-cong : f ≗ g mapMaybe f ≗ mapMaybe g
749-
mapMaybe-cong f≗g = cong catMaybes ∘ map-cong f≗g
726+
length-catMaybes : xs length (catMaybes {A = A} xs) ≤ length xs
727+
length-catMaybes [] = ≤-refl
728+
length-catMaybes (mx ∷ xs) with ih length-catMaybes xs | mx
729+
... | just _ = s≤s ih
730+
... | nothing = m≤n⇒m≤1+n ih
731+
732+
catMaybes-++ : (xs ys : List (Maybe A))
733+
catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
734+
catMaybes-++ [] _ = refl
735+
catMaybes-++ (mx ∷ xs) ys with ih catMaybes-++ xs ys | mx
736+
... | just x = cong (x ∷_) ih
737+
... | nothing = ih
738+
739+
map-catMaybes : (f : A B) map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
740+
map-catMaybes _ [] = refl
741+
map-catMaybes f (mx ∷ xs) with ih map-catMaybes f xs | mx
742+
... | just x = cong (f x ∷_) ih
743+
... | nothing = ih
744+
745+
Any-catMaybes⁺ : {P : Pred A ℓ} {xs : List (Maybe A)}
746+
Any (MAny P) xs Any P (catMaybes xs)
747+
Any-catMaybes⁺ {xs = nothing ∷ xs} (there x∈) = Any-catMaybes⁺ x∈
748+
Any-catMaybes⁺ {xs = just x ∷ xs} (here (just px)) = here px
749+
Any-catMaybes⁺ {xs = just x ∷ xs} (there x∈) = there $ Any-catMaybes⁺ x∈
750+
751+
------------------------------------------------------------------------
752+
-- mapMaybe
753+
754+
mapMaybe-cong : {f g : A Maybe B} f ≗ g mapMaybe f ≗ mapMaybe g
755+
mapMaybe-cong f≗g = cong catMaybes ∘ map-cong f≗g
750756

751757
mapMaybe-just : (xs : List A) mapMaybe just xs ≡ xs
752758
mapMaybe-just [] = refl
@@ -792,6 +798,36 @@ module _ (g : B → C) (f : A → Maybe B) where
792798
mapMaybe (Maybe.map g) (map f xs) ≡⟨ mapMaybe-map _ f xs ⟩
793799
mapMaybe (Maybe.map g ∘ f) xs ∎
794800

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

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

+24-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,24 @@ 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 mx xs↭) with ih catMaybes-↭ xs↭ | mx
386+
... | nothing = ih
387+
... | just x = prep x ih
388+
catMaybes-↭ (swap mx my xs↭) with ih catMaybes-↭ xs↭ | mx | my
389+
... | nothing | nothing = ih
390+
... | nothing | just y = prep y ih
391+
... | just x | nothing = prep x ih
392+
... | just x | just y = swap x y ih
393+
394+
------------------------------------------------------------------------
395+
-- mapMaybe
396+
397+
mapMaybe-↭ : (f : A Maybe B) xs ↭ ys mapMaybe f xs ↭ mapMaybe f ys
398+
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,72 @@
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 p xs↭) with ih catMaybes-↭ xs↭ | p
51+
... | nothing = ih
52+
... | just x~y = prep x~y ih
53+
catMaybes-↭ (swap p q xs↭) with ih catMaybes-↭ xs↭ | p | q
54+
... | nothing | nothing = ih
55+
... | nothing | just y = prep y ih
56+
... | just x | nothing = prep x ih
57+
... | just x | just y = swap x y ih
58+
59+
------------------------------------------------------------------------
60+
-- mapMaybe
61+
62+
module _ (sᴬ : Setoid a ℓ) (sᴮ : Setoid b ℓ′) where
63+
open Setoid sᴬ using () renaming (_≈_ to _≈ᴬ_)
64+
open Permutation sᴬ using () renaming (_↭_ to _↭ᴬ_)
65+
open Permutation sᴮ using () renaming (_↭_ to _↭ᴮ_)
66+
67+
private sᴹᴮ = setoidᴹ sᴮ
68+
open Setoid sᴹᴮ using () renaming (_≈_ to _≈ᴹᴮ_)
69+
70+
mapMaybe-↭ : {f} f Preserves _≈ᴬ_ ⟶ _≈ᴹᴮ_
71+
{xs ys} xs ↭ᴬ ys mapMaybe f xs ↭ᴮ mapMaybe f ys
72+
mapMaybe-↭ pres = catMaybes-↭ sᴮ ∘ map⁺ sᴬ sᴹᴮ pres

0 commit comments

Comments
 (0)
Please sign in to comment.