| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Sum combinators for setoid equality preserving functions |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Sum.Function.Setoid where |
| 10 |
|
| 11 |
open import Data.Product.Base as Product using (_,_) |
| 12 |
open import Data.Sum.Base as Sum |
| 13 |
open import Data.Sum.Relation.Binary.Pointwise as Pointwise |
| 14 |
open import Relation.Binary.Core using (Rel) |
| 15 |
open import Relation.Binary.Bundles using (Setoid) |
| 16 |
open import Function.Base using (id; _∘_) |
| 17 |
open import Function.Bundles |
| 18 |
using (Func; Equivalence; Injection; Surjection; Bijection; LeftInverse |
| 19 |
; RightInverse; Inverse) |
| 20 |
open import Function.Definitions |
| 21 |
using (Injective; Surjective; Bijective; StrictlySurjective) |
| 22 |
open import Level using (Level; _⊔_) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
a₁ a₂ b₁ b₂ c₁ c₂ d₁ d₂ : Level |
| 27 |
a ℓ : Level |
| 28 |
A B C D : Set a |
| 29 |
≈₁ ≈₂ ≈₃ ≈₄ : Rel A ℓ |
| 30 |
S T U V : Setoid a ℓ |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Combinators for equality preserving functions |
| 34 |
|
| 35 |
inj₁ₛ : Func S (S ⊎ₛ T) |
| 36 |
inj₁ₛ = record { to = inj₁ ; cong = inj₁ } |
| 37 |
|
| 38 |
inj₂ₛ : Func T (S ⊎ₛ T) |
| 39 |
inj₂ₛ = record { to = inj₂ ; cong = inj₂ } |
| 40 |
|
| 41 |
[_,_]ₛ : Func S U → Func T U → Func (S ⊎ₛ T) U |
| 42 |
[ f , g ]ₛ = record |
| 43 |
{ to = [ to f , to g ] |
| 44 |
; cong = λ where |
| 45 |
(inj₁ x∼₁y) → cong f x∼₁y |
| 46 |
(inj₂ x∼₂y) → cong g x∼₂y |
| 47 |
} where open Func |
| 48 |
|
| 49 |
swapₛ : Func (S ⊎ₛ T) (T ⊎ₛ S) |
| 50 |
swapₛ = [ inj₂ₛ , inj₁ₛ ]ₛ |
| 51 |
|
| 52 |
------------------------------------------------------------------------ |
| 53 |
-- Definitions |
| 54 |
|
| 55 |
⊎-injective : ∀ {f g} → |
| 56 |
Injective ≈₁ ≈₂ f → |
| 57 |
Injective ≈₃ ≈₄ g → |
| 58 |
Injective (Pointwise ≈₁ ≈₃) (Pointwise ≈₂ ≈₄) (Sum.map f g) |
| 59 |
⊎-injective f-inj g-inj {inj₁ x} {inj₁ y} (inj₁ x∼₁y) = inj₁ (f-inj x∼₁y) |
| 60 |
⊎-injective f-inj g-inj {inj₂ x} {inj₂ y} (inj₂ x∼₂y) = inj₂ (g-inj x∼₂y) |
| 61 |
|
| 62 |
⊎-strictlySurjective : ∀ {f : A → B} {g : C → D} → |
| 63 |
StrictlySurjective ≈₁ f → |
| 64 |
StrictlySurjective ≈₂ g → |
| 65 |
StrictlySurjective (Pointwise ≈₁ ≈₂) (Sum.map f g) |
| 66 |
⊎-strictlySurjective f-sur g-sur = |
| 67 |
[ Product.map inj₁ inj₁ ∘ f-sur |
| 68 |
, Product.map inj₂ inj₂ ∘ g-sur |
| 69 |
] |
| 70 |
|
| 71 |
⊎-surjective : ∀ {f : A → B} {g : C → D} → |
| 72 |
Surjective ≈₁ ≈₂ f → |
| 73 |
Surjective ≈₃ ≈₄ g → |
| 74 |
Surjective (Pointwise ≈₁ ≈₃) (Pointwise ≈₂ ≈₄) (Sum.map f g) |
| 75 |
⊎-surjective f-sur g-sur = |
| 76 |
[ Product.map inj₁ (λ { fwd (inj₁ x) → inj₁ (fwd x)}) ∘ f-sur |
| 77 |
, Product.map inj₂ (λ { fwd (inj₂ y) → inj₂ (fwd y)}) ∘ g-sur |
| 78 |
] |
| 79 |
|
| 80 |
|
| 81 |
infixr 1 _⊎-equivalence_ _⊎-injection_ _⊎-left-inverse_ |
| 82 |
|
| 83 |
------------------------------------------------------------------------ |
| 84 |
-- Function bundles |
| 85 |
|
| 86 |
_⊎-function_ : Func S T → Func U V → Func (S ⊎ₛ U) (T ⊎ₛ V) |
| 87 |
S→T ⊎-function U→V = record |
| 88 |
{ to = Sum.map (to S→T) (to U→V) |
| 89 |
; cong = Pointwise.map (cong S→T) (cong U→V) |
| 90 |
} where open Func |
| 91 |
|
| 92 |
_⊎-equivalence_ : Equivalence S T → Equivalence U V → |
| 93 |
Equivalence (S ⊎ₛ U) (T ⊎ₛ V) |
| 94 |
S⇔T ⊎-equivalence U⇔V = record |
| 95 |
{ to = Sum.map (to S⇔T) (to U⇔V) |
| 96 |
; from = Sum.map (from S⇔T) (from U⇔V) |
| 97 |
; to-cong = Pointwise.map (to-cong S⇔T) (to-cong U⇔V) |
| 98 |
; from-cong = Pointwise.map (from-cong S⇔T) (from-cong U⇔V) |
| 99 |
} where open Equivalence |
| 100 |
|
| 101 |
_⊎-injection_ : Injection S T → Injection U V → |
| 102 |
Injection (S ⊎ₛ U) (T ⊎ₛ V) |
| 103 |
S↣T ⊎-injection U↣V = record |
| 104 |
{ to = Sum.map (to S↣T) (to U↣V) |
| 105 |
; cong = Pointwise.map (cong S↣T) (cong U↣V) |
| 106 |
; injective = ⊎-injective (injective S↣T) (injective U↣V) |
| 107 |
} where open Injection |
| 108 |
|
| 109 |
infixr 1 _⊎-surjection_ _⊎-inverse_ |
| 110 |
_⊎-surjection_ : Surjection S T → Surjection U V → |
| 111 |
Surjection (S ⊎ₛ U) (T ⊎ₛ V) |
| 112 |
S↠T ⊎-surjection U↠V = record |
| 113 |
{ to = Sum.map (to S↠T) (to U↠V) |
| 114 |
; cong = Pointwise.map (cong S↠T) (cong U↠V) |
| 115 |
; surjective = ⊎-surjective (surjective S↠T) (surjective U↠V) |
| 116 |
} where open Surjection |
| 117 |
|
| 118 |
_⊎-bijection_ : Bijection S T → Bijection U V → |
| 119 |
Bijection (S ⊎ₛ U) (T ⊎ₛ V) |
| 120 |
S⤖T ⊎-bijection U⤖V = record |
| 121 |
{ to = Sum.map (to S⤖T) (to U⤖V) |
| 122 |
; cong = Pointwise.map (cong S⤖T) (cong U⤖V) |
| 123 |
; bijective = ⊎-injective (injective S⤖T) (injective U⤖V) , |
| 124 |
⊎-surjective (surjective S⤖T) (surjective U⤖V) |
| 125 |
} where open Bijection |
| 126 |
|
| 127 |
_⊎-leftInverse_ : LeftInverse S T → LeftInverse U V → |
| 128 |
LeftInverse (S ⊎ₛ U) (T ⊎ₛ V) |
| 129 |
S↩T ⊎-leftInverse U↩V = record |
| 130 |
{ to = Sum.map (to S↩T) (to U↩V) |
| 131 |
; from = Sum.map (from S↩T) (from U↩V) |
| 132 |
; to-cong = Pointwise.map (to-cong S↩T) (to-cong U↩V) |
| 133 |
; from-cong = Pointwise.map (from-cong S↩T) (from-cong U↩V) |
| 134 |
; inverseˡ = λ { {inj₁ _} {.(inj₁ _)} (inj₁ x) → inj₁ (inverseˡ S↩T x) |
| 135 |
; {inj₂ _} {.(inj₂ _)} (inj₂ x) → inj₂ (inverseˡ U↩V x)} |
| 136 |
} where open LeftInverse |
| 137 |
|
| 138 |
_⊎-rightInverse_ : RightInverse S T → RightInverse U V → |
| 139 |
RightInverse (S ⊎ₛ U) (T ⊎ₛ V) |
| 140 |
S↪T ⊎-rightInverse U↪V = record |
| 141 |
{ to = Sum.map (to S↪T) (to U↪V) |
| 142 |
; from = Sum.map (from S↪T) (from U↪V) |
| 143 |
; to-cong = Pointwise.map (to-cong S↪T) (to-cong U↪V) |
| 144 |
; from-cong = Pointwise.map (from-cong S↪T) (from-cong U↪V) |
| 145 |
; inverseʳ = λ { {inj₁ _} (inj₁ x) → inj₁ (inverseʳ S↪T x) |
| 146 |
; {inj₂ _} (inj₂ x) → inj₂ (inverseʳ U↪V x) |
| 147 |
} |
| 148 |
} where open RightInverse |
| 149 |
|
| 150 |
_⊎-inverse_ : Inverse S T → Inverse U V → |
| 151 |
Inverse (S ⊎ₛ U) (T ⊎ₛ V) |
| 152 |
S↔T ⊎-inverse U↔V = record |
| 153 |
{ to = Sum.map (to S↔T) (to U↔V) |
| 154 |
; from = Sum.map (from S↔T) (from U↔V) |
| 155 |
; to-cong = Pointwise.map (to-cong S↔T) (to-cong U↔V) |
| 156 |
; from-cong = Pointwise.map (from-cong S↔T) (from-cong U↔V) |
| 157 |
; inverse = (λ { {inj₁ _} (inj₁ x) → inj₁ (inverseˡ S↔T x) |
| 158 |
; {inj₂ _} (inj₂ x) → inj₂ (inverseˡ U↔V x)}) , |
| 159 |
λ { {inj₁ _} (inj₁ x) → inj₁ (inverseʳ S↔T x) |
| 160 |
; {inj₂ _} (inj₂ x) → inj₂ (inverseʳ U↔V x) |
| 161 |
} |
| 162 |
} where open Inverse |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
------------------------------------------------------------------------ |
| 167 |
-- DEPRECATED NAMES |
| 168 |
------------------------------------------------------------------------ |
| 169 |
-- Please use the new names as continuing support for the old names is |
| 170 |
-- not guaranteed. |
| 171 |
|
| 172 |
-- Version 2.0 |
| 173 |
|
| 174 |
_⊎-left-inverse_ = _⊎-leftInverse_ |
| 175 |
{-# WARNING_ON_USAGE _⊎-left-inverse_ |
| 176 |
"Warning: _⊎-left-inverse_ was deprecated in v2.0. |
| 177 |
Please use _⊎-leftInverse_ instead." |
| 178 |
#-} |