| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of sums (disjoint unions) |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Sum.Properties where |
| 10 |
|
| 11 |
open import Level using (Level) |
| 12 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; swap; [_,_]; map; |
| 13 |
map₁; map₂; assocˡ; assocʳ) |
| 14 |
open import Function.Base using (_∋_; _∘_; id) |
| 15 |
open import Function.Bundles using (mk↔ₛ′; _↔_) |
| 16 |
open import Relation.Binary.Definitions using (DecidableEquality) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core |
| 18 |
using (_≡_; _≗_; refl; cong) |
| 19 |
open import Relation.Nullary.Decidable.Core using (yes; no; map′) |
| 20 |
|
| 21 |
private |
| 22 |
variable |
| 23 |
a b c d e f : Level |
| 24 |
A : Set a |
| 25 |
B : Set b |
| 26 |
C : Set c |
| 27 |
D : Set d |
| 28 |
E : Set e |
| 29 |
F : Set f |
| 30 |
|
| 31 |
inj₁-injective : ∀ {x y} → (A ⊎ B ∋ inj₁ x) ≡ inj₁ y → x ≡ y |
| 32 |
inj₁-injective refl = refl |
| 33 |
|
| 34 |
inj₂-injective : ∀ {x y} → (A ⊎ B ∋ inj₂ x) ≡ inj₂ y → x ≡ y |
| 35 |
inj₂-injective refl = refl |
| 36 |
|
| 37 |
module _ (dec₁ : DecidableEquality A) |
| 38 |
(dec₂ : DecidableEquality B) where |
| 39 |
|
| 40 |
≡-dec : DecidableEquality (A ⊎ B) |
| 41 |
≡-dec (inj₁ x) (inj₁ y) = map′ (cong inj₁) inj₁-injective (dec₁ x y) |
| 42 |
≡-dec (inj₁ x) (inj₂ y) = no λ() |
| 43 |
≡-dec (inj₂ x) (inj₁ y) = no λ() |
| 44 |
≡-dec (inj₂ x) (inj₂ y) = map′ (cong inj₂) inj₂-injective (dec₂ x y) |
| 45 |
|
| 46 |
swap-involutive : swap {A = A} {B = B} ∘ swap ≗ id |
| 47 |
swap-involutive = [ (λ _ → refl) , (λ _ → refl) ] |
| 48 |
|
| 49 |
swap-↔ : (A ⊎ B) ↔ (B ⊎ A) |
| 50 |
swap-↔ = mk↔ₛ′ swap swap swap-involutive swap-involutive |
| 51 |
|
| 52 |
map-id : map {A = A} {B = B} id id ≗ id |
| 53 |
map-id (inj₁ _) = refl |
| 54 |
map-id (inj₂ _) = refl |
| 55 |
|
| 56 |
[,]-∘ : (f : A → B) |
| 57 |
{g : C → A} {h : D → A} → |
| 58 |
f ∘ [ g , h ] ≗ [ f ∘ g , f ∘ h ] |
| 59 |
[,]-∘ _ (inj₁ _) = refl |
| 60 |
[,]-∘ _ (inj₂ _) = refl |
| 61 |
|
| 62 |
[,]-map : {f : A → B} {g : C → D} |
| 63 |
{f′ : B → E} {g′ : D → E} → |
| 64 |
[ f′ , g′ ] ∘ map f g ≗ [ f′ ∘ f , g′ ∘ g ] |
| 65 |
[,]-map (inj₁ _) = refl |
| 66 |
[,]-map (inj₂ _) = refl |
| 67 |
|
| 68 |
map-map : {f : A → B} {g : C → D} |
| 69 |
{f′ : B → E} {g′ : D → F} → |
| 70 |
map f′ g′ ∘ map f g ≗ map (f′ ∘ f) (g′ ∘ g) |
| 71 |
map-map (inj₁ _) = refl |
| 72 |
map-map (inj₂ _) = refl |
| 73 |
|
| 74 |
map₁₂-map₂₁ : {f : A → B} {g : C → D} → |
| 75 |
map₁ f ∘ map₂ g ≗ map₂ g ∘ map₁ f |
| 76 |
map₁₂-map₂₁ (inj₁ _) = refl |
| 77 |
map₁₂-map₂₁ (inj₂ _) = refl |
| 78 |
|
| 79 |
map-assocˡ : (f : A → C) (g : B → D) (h : C → F) → |
| 80 |
map (map f g) h ∘ assocˡ ≗ assocˡ ∘ map f (map g h) |
| 81 |
map-assocˡ _ _ _ (inj₁ x ) = refl |
| 82 |
map-assocˡ _ _ _ (inj₂ (inj₁ y)) = refl |
| 83 |
map-assocˡ _ _ _ (inj₂ (inj₂ z)) = refl |
| 84 |
|
| 85 |
map-assocʳ : (f : A → C) (g : B → D) (h : C → F) → |
| 86 |
map f (map g h) ∘ assocʳ ≗ assocʳ ∘ map (map f g) h |
| 87 |
map-assocʳ _ _ _ (inj₁ (inj₁ x)) = refl |
| 88 |
map-assocʳ _ _ _ (inj₁ (inj₂ y)) = refl |
| 89 |
map-assocʳ _ _ _ (inj₂ z ) = refl |
| 90 |
|
| 91 |
[,]-cong : {f f′ : A → B} {g g′ : C → B} → |
| 92 |
f ≗ f′ → g ≗ g′ → |
| 93 |
[ f , g ] ≗ [ f′ , g′ ] |
| 94 |
[,]-cong = [_,_] |
| 95 |
|
| 96 |
[-,]-cong : {f f′ : A → B} {g : C → B} → |
| 97 |
f ≗ f′ → |
| 98 |
[ f , g ] ≗ [ f′ , g ] |
| 99 |
[-,]-cong = [_, (λ _ → refl) ] |
| 100 |
|
| 101 |
[,-]-cong : {f : A → B} {g g′ : C → B} → |
| 102 |
g ≗ g′ → |
| 103 |
[ f , g ] ≗ [ f , g′ ] |
| 104 |
[,-]-cong = [ (λ _ → refl) ,_] |
| 105 |
|
| 106 |
map-cong : {f f′ : A → B} {g g′ : C → D} → |
| 107 |
f ≗ f′ → g ≗ g′ → |
| 108 |
map f g ≗ map f′ g′ |
| 109 |
map-cong f≗f′ g≗g′ (inj₁ x) = cong inj₁ (f≗f′ x) |
| 110 |
map-cong f≗f′ g≗g′ (inj₂ x) = cong inj₂ (g≗g′ x) |
| 111 |
|
| 112 |
map₁-cong : {f f′ : A → B} → |
| 113 |
f ≗ f′ → |
| 114 |
map₁ {B = C} f ≗ map₁ f′ |
| 115 |
map₁-cong f≗f′ = [-,]-cong ((cong inj₁) ∘ f≗f′) |
| 116 |
|
| 117 |
map₂-cong : {g g′ : C → D} → |
| 118 |
g ≗ g′ → |
| 119 |
map₂ {A = A} g ≗ map₂ g′ |
| 120 |
map₂-cong g≗g′ = [,-]-cong ((cong inj₂) ∘ g≗g′) |
| 121 |
|
| 122 |
------------------------------------------------------------------------ |
| 123 |
-- DEPRECATED NAMES |
| 124 |
------------------------------------------------------------------------ |
| 125 |
-- Please use the new names as continuing support for the old names is |
| 126 |
-- not guaranteed. |
| 127 |
|
| 128 |
-- Version 2.0 |
| 129 |
|
| 130 |
[,]-∘-distr = [,]-∘ |
| 131 |
{-# WARNING_ON_USAGE [,]-∘-distr |
| 132 |
"Warning: [,]-∘-distr was deprecated in v2.0. |
| 133 |
Please use [,]-∘ instead." |
| 134 |
#-} |
| 135 |
|
| 136 |
[,]-map-commute = [,]-map |
| 137 |
{-# WARNING_ON_USAGE [,]-map-commute |
| 138 |
"Warning: [,]-map-commute was deprecated in v2.0. |
| 139 |
Please use [,]-map instead." |
| 140 |
#-} |
| 141 |
|
| 142 |
map-commute = map-map |
| 143 |
{-# WARNING_ON_USAGE map-commute |
| 144 |
"Warning: map-commute was deprecated in v2.0. |
| 145 |
Please use map-map instead." |
| 146 |
#-} |
| 147 |
|
| 148 |
map₁₂-commute = map₁₂-map₂₁ |
| 149 |
{-# WARNING_ON_USAGE map₁₂-commute |
| 150 |
"Warning: map₁₂-commute was deprecated in v2.0. |
| 151 |
Please use map₁₂-map₂₁ instead." |
| 152 |
#-} |