@@ -24,13 +24,14 @@ open import Data.List.Membership.Propositional using (_∈_)
2424open import Data.List.Relation.Unary.All using (All; []; _∷_)
2525open import Data.List.Relation.Unary.Any using (Any; here; there)
2626open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe)
27+ open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny)
2728open import Data.Nat.Base
2829open import Data.Nat.Divisibility using (_∣_; divides; ∣n⇒∣m*n)
2930open import Data.Nat.Properties
3031open import Data.Product.Base as Product
3132 using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>)
3233import 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 ₂)
3435open import Data.These.Base as These using (These; this; that; these)
3536open import Data.Fin.Properties using (toℕ-cast)
3637open import Function.Base using (id; _∘_; _∘′_; _∋_; _-⟨_∣; ∣_⟩-_; _$_; const; flip)
@@ -48,6 +49,7 @@ open import Relation.Unary using (Pred; Decidable; ∁)
4849open import Relation.Unary.Properties using (∁?)
4950import Data.Nat.GeneralisedArithmetic as ℕ
5051
52+ private variable ℓ : Level
5153
5254open ≡-Reasoning
5355
@@ -83,71 +85,6 @@ private
8385≡-dec _≟_ [] (y ∷ ys) = no λ ()
8486≡-dec _≟_ (x ∷ xs) (y ∷ ys) = ∷-dec (x ≟ y) (≡-dec _≟_ xs ys)
8587
86- ------------------------------------------------------------------------
87- -- map
88-
89- map-id : map id ≗ id {A = List A}
90- map-id [] = refl
91- map-id (x ∷ xs) = cong (x ∷_) (map-id xs)
92-
93- map-id-local : ∀ {f : A → A} {xs} → All (λ x → f x ≡ x) xs → map f xs ≡ xs
94- map-id-local [] = refl
95- map-id-local (fx≡x ∷ pxs) = cong₂ _∷_ fx≡x (map-id-local pxs)
96-
97- map-++ : ∀ (f : A → B) xs ys →
98- map f (xs ++ ys) ≡ map f xs ++ map f ys
99- map-++ f [] ys = refl
100- map-++ f (x ∷ xs) ys = cong (f x ∷_) (map-++ f xs ys)
101-
102- map-cong : ∀ {f g : A → B} → f ≗ g → map f ≗ map g
103- map-cong f≗g [] = refl
104- map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (map-cong f≗g xs)
105-
106- map-cong-local : ∀ {f g : A → B} {xs} →
107- All (λ x → f x ≡ g x) xs → map f xs ≡ map g xs
108- map-cong-local [] = refl
109- map-cong-local (fx≡gx ∷ fxs≡gxs) = cong₂ _∷_ fx≡gx (map-cong-local fxs≡gxs)
110-
111- length-map : ∀ (f : A → B) xs → length (map f xs) ≡ length xs
112- length-map f [] = refl
113- length-map f (x ∷ xs) = cong suc (length-map f xs)
114-
115- map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
116- map-∘ [] = refl
117- map-∘ (x ∷ xs) = cong (_ ∷_) (map-∘ xs)
118-
119- map-injective : ∀ {f : A → B} → Injective _≡_ _≡_ f → Injective _≡_ _≡_ (map f)
120- map-injective finj {[]} {[]} eq = refl
121- map-injective finj {x ∷ xs} {y ∷ ys} eq =
122- let fx≡fy , fxs≡fys = ∷-injective eq in
123- cong₂ _∷_ (finj fx≡fy) (map-injective finj fxs≡fys)
124-
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-
15188------------------------------------------------------------------------
15289-- _++_
15390
@@ -263,6 +200,46 @@ module _ (A : Set a) where
263200 ; ε-homo = refl
264201 }
265202
203+
204+ ------------------------------------------------------------------------
205+ -- map
206+
207+ map-id : map id ≗ id {A = List A}
208+ map-id [] = refl
209+ map-id (x ∷ xs) = cong (x ∷_) (map-id xs)
210+
211+ map-id-local : ∀ {f : A → A} {xs} → All (λ x → f x ≡ x) xs → map f xs ≡ xs
212+ map-id-local [] = refl
213+ map-id-local (fx≡x ∷ pxs) = cong₂ _∷_ fx≡x (map-id-local pxs)
214+
215+ map-++ : ∀ (f : A → B) xs ys →
216+ map f (xs ++ ys) ≡ map f xs ++ map f ys
217+ map-++ f [] ys = refl
218+ map-++ f (x ∷ xs) ys = cong (f x ∷_) (map-++ f xs ys)
219+
220+ map-cong : ∀ {f g : A → B} → f ≗ g → map f ≗ map g
221+ map-cong f≗g [] = refl
222+ map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (map-cong f≗g xs)
223+
224+ map-cong-local : ∀ {f g : A → B} {xs} →
225+ All (λ x → f x ≡ g x) xs → map f xs ≡ map g xs
226+ map-cong-local [] = refl
227+ map-cong-local (fx≡gx ∷ fxs≡gxs) = cong₂ _∷_ fx≡gx (map-cong-local fxs≡gxs)
228+
229+ length-map : ∀ (f : A → B) xs → length (map f xs) ≡ length xs
230+ length-map f [] = refl
231+ length-map f (x ∷ xs) = cong suc (length-map f xs)
232+
233+ map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
234+ map-∘ [] = refl
235+ map-∘ (x ∷ xs) = cong (_ ∷_) (map-∘ xs)
236+
237+ map-injective : ∀ {f : A → B} → Injective _≡_ _≡_ f → Injective _≡_ _≡_ (map f)
238+ map-injective finj {[]} {[]} eq = refl
239+ map-injective finj {x ∷ xs} {y ∷ ys} eq =
240+ let fx≡fy , fxs≡fys = ∷-injective eq in
241+ cong₂ _∷_ (finj fx≡fy) (map-injective finj fxs≡fys)
242+
266243------------------------------------------------------------------------
267244-- cartesianProductWith
268245
@@ -740,6 +717,39 @@ map-concatMap f g xs = begin
740717 concatMap (map f ∘′ g) xs
741718 ∎
742719
720+ ------------------------------------------------------------------------
721+ -- catMaybes
722+
723+ catMaybes-concatMap : catMaybes {A = A} ≗ concatMap fromMaybe
724+ catMaybes-concatMap [] = refl
725+ catMaybes-concatMap (just x ∷ xs) = cong (x ∷_) (catMaybes-concatMap xs)
726+ catMaybes-concatMap (nothing ∷ xs) = catMaybes-concatMap xs
727+
728+ length-catMaybes : ∀ xs → length (catMaybes {A = A} xs) ≤ length xs
729+ length-catMaybes [] = ≤-refl
730+ length-catMaybes (just x ∷ xs) = s≤s (length-catMaybes xs)
731+ length-catMaybes (nothing ∷ xs) = m≤n⇒m≤1+n (length-catMaybes xs)
732+
733+ catMaybes-++ : (xs ys : List (Maybe A)) →
734+ catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys
735+ catMaybes-++ [] ys = refl
736+ catMaybes-++ (just x ∷ xs) ys = cong (x ∷_) (catMaybes-++ xs ys)
737+ catMaybes-++ (nothing ∷ xs) ys = catMaybes-++ xs ys
738+
739+ module _ (f : A → B) where
740+
741+ map-catMaybes : map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f)
742+ map-catMaybes [] = refl
743+ map-catMaybes (just x ∷ xs) = cong (f x ∷_) (map-catMaybes xs)
744+ map-catMaybes (nothing ∷ xs) = map-catMaybes xs
745+
746+ Any-catMaybes⁺ : ∀ {P : Pred A ℓ} {xs : List (Maybe A)}
747+ → Any (MAny P) xs → Any P (catMaybes xs)
748+ Any-catMaybes⁺ {xs = nothing ∷ xs} (there x∈) = Any-catMaybes⁺ x∈
749+ Any-catMaybes⁺ {xs = just x ∷ xs} = λ where
750+ (here (just px)) → here px
751+ (there x∈) → there $ Any-catMaybes⁺ x∈
752+
743753------------------------------------------------------------------------
744754-- mapMaybe
745755
@@ -792,6 +802,26 @@ module _ (g : B → C) (f : A → Maybe B) where
792802 mapMaybe (Maybe.map g) (map f xs) ≡⟨ mapMaybe-map _ f xs ⟩
793803 mapMaybe (Maybe.map g ∘ f) xs ∎
794804
805+ mapMaybeIsInj₁∘mapInj₁ : (xs : List A) → mapMaybe (isInj₁ {B = B}) (map inj₁ xs) ≡ xs
806+ mapMaybeIsInj₁∘mapInj₁ = λ where
807+ [] → refl
808+ (x ∷ xs) → cong (x ∷_) (mapMaybeIsInj₁∘mapInj₁ xs)
809+
810+ mapMaybeIsInj₁∘mapInj₂ : (xs : List B) → mapMaybe (isInj₁ {A = A}) (map inj₂ xs) ≡ []
811+ mapMaybeIsInj₁∘mapInj₂ = λ where
812+ [] → refl
813+ (x ∷ xs) → mapMaybeIsInj₁∘mapInj₂ xs
814+
815+ mapMaybeIsInj₂∘mapInj₂ : (xs : List B) → mapMaybe (isInj₂ {A = A}) (map inj₂ xs) ≡ xs
816+ mapMaybeIsInj₂∘mapInj₂ = λ where
817+ [] → refl
818+ (x ∷ xs) → cong (x ∷_) (mapMaybeIsInj₂∘mapInj₂ xs)
819+
820+ mapMaybeIsInj₂∘mapInj₁ : (xs : List A) → mapMaybe (isInj₂ {B = B}) (map inj₁ xs) ≡ []
821+ mapMaybeIsInj₂∘mapInj₁ = λ where
822+ [] → refl
823+ (x ∷ xs) → mapMaybeIsInj₂∘mapInj₁ xs
824+
795825------------------------------------------------------------------------
796826-- sum
797827
0 commit comments