| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Dependent product combinators for setoid equality preserving |
| 5 |
-- functions. |
| 6 |
-- |
| 7 |
-- NOTE: the first component of the equality is propositional equality. |
| 8 |
------------------------------------------------------------------------ |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Data.Product.Function.Dependent.Setoid where |
| 13 |
|
| 14 |
open import Data.Product.Base using (map; _,_; proj₁; proj₂) |
| 15 |
open import Data.Product.Relation.Binary.Pointwise.Dependent as Σ |
| 16 |
open import Level using (Level) |
| 17 |
open import Function |
| 18 |
open import Function.Consequences.Setoid |
| 19 |
open import Function.Properties.Injection using (mkInjection) |
| 20 |
open import Function.Properties.Surjection using (mkSurjection; ↠⇒⇔) |
| 21 |
open import Function.Properties.Equivalence using (mkEquivalence; ⇔⇒⟶; ⇔⇒⟵) |
| 22 |
open import Function.Properties.RightInverse using (mkRightInverse) |
| 23 |
import Function.Construct.Symmetry as Sym |
| 24 |
open import Relation.Binary.Core using (_=[_]⇒_) |
| 25 |
open import Relation.Binary.Bundles as B |
| 26 |
open import Relation.Binary.Indexed.Heterogeneous |
| 27 |
using (IndexedSetoid) |
| 28 |
open import Relation.Binary.Indexed.Heterogeneous.Construct.At |
| 29 |
using (_atₛ_) |
| 30 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 31 |
import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 32 |
|
| 33 |
private |
| 34 |
variable |
| 35 |
i a b ℓ₁ ℓ₂ : Level |
| 36 |
I J : Set i |
| 37 |
A B : IndexedSetoid I a ℓ₁ |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Properties related to "relatedness" |
| 41 |
------------------------------------------------------------------------ |
| 42 |
|
| 43 |
private module _ (A : IndexedSetoid I a ℓ₁) where |
| 44 |
open IndexedSetoid A |
| 45 |
|
| 46 |
cast : ∀ {i j} → j ≡ i → Carrier i → Carrier j |
| 47 |
cast j≡i = ≡.subst Carrier (≡.sym $ j≡i) |
| 48 |
|
| 49 |
cast-cong : ∀ {i j} {x y : Carrier i} |
| 50 |
(j≡i : j ≡ i) → |
| 51 |
x ≈ y → |
| 52 |
cast j≡i x ≈ cast j≡i y |
| 53 |
cast-cong ≡.refl p = p |
| 54 |
|
| 55 |
cast-eq : ∀ {i j x} (eq : i ≡ j) → cast eq x ≈ x |
| 56 |
cast-eq ≡.refl = IndexedSetoid.refl A |
| 57 |
|
| 58 |
private |
| 59 |
_×ₛ_ : (I : Set i) → IndexedSetoid I a ℓ₁ → Setoid _ _ |
| 60 |
I ×ₛ A = Σ.setoid (≡.setoid I) A |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Functions |
| 64 |
|
| 65 |
module _ where |
| 66 |
open Func |
| 67 |
open Setoid |
| 68 |
|
| 69 |
function : |
| 70 |
(f : I ⟶ J) → |
| 71 |
(∀ {i} → Func (A atₛ i) (B atₛ (to f i))) → |
| 72 |
Func (I ×ₛ A) (J ×ₛ B) |
| 73 |
function {I = I} {J = J} {A = A} {B = B} I⟶J A⟶B = record |
| 74 |
{ to = to′ |
| 75 |
; cong = cong′ |
| 76 |
} |
| 77 |
where |
| 78 |
to′ = map (to I⟶J) (to A⟶B) |
| 79 |
|
| 80 |
cong′ : Congruent (_≈_ (I ×ₛ A)) (_≈_ (J ×ₛ B)) to′ |
| 81 |
cong′ (≡.refl , ∼) = (≡.refl , cong A⟶B ∼) |
| 82 |
|
| 83 |
------------------------------------------------------------------------ |
| 84 |
-- Equivalences |
| 85 |
|
| 86 |
module _ where |
| 87 |
open Equivalence |
| 88 |
|
| 89 |
equivalence : |
| 90 |
(I⇔J : I ⇔ J) → |
| 91 |
(∀ {i} → Func (A atₛ i) (B atₛ (to I⇔J i))) → |
| 92 |
(∀ {j} → Func (B atₛ j) (A atₛ (from I⇔J j))) → |
| 93 |
Equivalence (I ×ₛ A) (J ×ₛ B) |
| 94 |
equivalence I⇔J A⟶B B⟶A = mkEquivalence |
| 95 |
(function (⇔⇒⟶ I⇔J) A⟶B) |
| 96 |
(function (⇔⇒⟵ I⇔J) B⟶A) |
| 97 |
|
| 98 |
equivalence-↪ : |
| 99 |
(I↪J : I ↪ J) → |
| 100 |
(∀ {i} → Equivalence (A atₛ (RightInverse.from I↪J i)) (B atₛ i)) → |
| 101 |
Equivalence (I ×ₛ A) (J ×ₛ B) |
| 102 |
equivalence-↪ {A = A} {B = B} I↪J A⇔B = |
| 103 |
equivalence (RightInverse.equivalence I↪J) A→B (fromFunction A⇔B) |
| 104 |
where |
| 105 |
A→B : ∀ {i} → Func (A atₛ i) (B atₛ (RightInverse.to I↪J i)) |
| 106 |
A→B = record |
| 107 |
{ to = to A⇔B ∘ cast A (RightInverse.strictlyInverseʳ I↪J _) |
| 108 |
; cong = to-cong A⇔B ∘ cast-cong A (RightInverse.strictlyInverseʳ I↪J _) |
| 109 |
} |
| 110 |
|
| 111 |
equivalence-↠ : |
| 112 |
(I↠J : I ↠ J) → |
| 113 |
(∀ {x} → Equivalence (A atₛ x) (B atₛ (Surjection.to I↠J x))) → |
| 114 |
Equivalence (I ×ₛ A) (J ×ₛ B) |
| 115 |
equivalence-↠ {A = A} {B = B} I↠J A⇔B = |
| 116 |
equivalence (↠⇒⇔ I↠J) B-to B-from |
| 117 |
where |
| 118 |
B-to : ∀ {x} → Func (A atₛ x) (B atₛ (Surjection.to I↠J x)) |
| 119 |
B-to = toFunction A⇔B |
| 120 |
|
| 121 |
B-from : ∀ {y} → Func (B atₛ y) (A atₛ (Surjection.to⁻ I↠J y)) |
| 122 |
B-from = record |
| 123 |
{ to = from A⇔B ∘ cast B (Surjection.to∘to⁻ I↠J _) |
| 124 |
; cong = from-cong A⇔B ∘ cast-cong B (Surjection.to∘to⁻ I↠J _) |
| 125 |
} |
| 126 |
|
| 127 |
------------------------------------------------------------------------ |
| 128 |
-- Injections |
| 129 |
|
| 130 |
module _ where |
| 131 |
open Injection hiding (function) |
| 132 |
open IndexedSetoid |
| 133 |
|
| 134 |
injection : |
| 135 |
(I↣J : I ↣ J) → |
| 136 |
(∀ {i} → Injection (A atₛ i) (B atₛ (Injection.to I↣J i))) → |
| 137 |
Injection (I ×ₛ A) (J ×ₛ B) |
| 138 |
injection {I = I} {J = J} {A = A} {B = B} I↣J A↣B = mkInjection func inj |
| 139 |
where |
| 140 |
func : Func (I ×ₛ A) (J ×ₛ B) |
| 141 |
func = function (Injection.function I↣J) (Injection.function A↣B) |
| 142 |
|
| 143 |
inj : Injective (Func.Eq₁._≈_ func) (Func.Eq₂._≈_ func) (Func.to func) |
| 144 |
inj (to[i]≡to[j] , y) = |
| 145 |
injective I↣J to[i]≡to[j] , |
| 146 |
lemma (injective I↣J to[i]≡to[j]) y |
| 147 |
where |
| 148 |
lemma : |
| 149 |
∀ {i j} {x : Carrier A i} {y : Carrier A j} → |
| 150 |
i ≡ j → |
| 151 |
(_≈_ B (to A↣B x) (to A↣B y)) → |
| 152 |
_≈_ A x y |
| 153 |
lemma ≡.refl = Injection.injective A↣B |
| 154 |
|
| 155 |
------------------------------------------------------------------------ |
| 156 |
-- Surjections |
| 157 |
|
| 158 |
module _ where |
| 159 |
open Surjection hiding (function) |
| 160 |
open Setoid |
| 161 |
|
| 162 |
surjection : |
| 163 |
(I↠J : I ↠ J) → |
| 164 |
(∀ {x} → Surjection (A atₛ x) (B atₛ (to I↠J x))) → |
| 165 |
Surjection (I ×ₛ A) (J ×ₛ B) |
| 166 |
surjection {I = I} {J = J} {A = A} {B = B} I↠J A↠B = |
| 167 |
mkSurjection func surj |
| 168 |
where |
| 169 |
func : Func (I ×ₛ A) (J ×ₛ B) |
| 170 |
func = function (Surjection.function I↠J) (Surjection.function A↠B) |
| 171 |
|
| 172 |
to⁻′ : Carrier (J ×ₛ B) → Carrier (I ×ₛ A) |
| 173 |
to⁻′ (j , y) = to⁻ I↠J j , to⁻ A↠B (cast B (Surjection.to∘to⁻ I↠J _) y) |
| 174 |
|
| 175 |
strictlySurj : StrictlySurjective (Func.Eq₂._≈_ func) (Func.to func) |
| 176 |
strictlySurj (j , y) = to⁻′ (j , y) , |
| 177 |
to∘to⁻ I↠J j , IndexedSetoid.trans B (to∘to⁻ A↠B _) (cast-eq B (to∘to⁻ I↠J j)) |
| 178 |
|
| 179 |
surj : Surjective (Func.Eq₁._≈_ func) (Func.Eq₂._≈_ func) (Func.to func) |
| 180 |
surj = strictlySurjective⇒surjective (I ×ₛ A) (J ×ₛ B) (Func.cong func) strictlySurj |
| 181 |
|
| 182 |
------------------------------------------------------------------------ |
| 183 |
-- RightInverse |
| 184 |
|
| 185 |
module _ where |
| 186 |
open RightInverse |
| 187 |
open Setoid |
| 188 |
|
| 189 |
rightInverse : |
| 190 |
(I↪J : I ↪ J) → |
| 191 |
(∀ {j} → RightInverse (A atₛ (from I↪J j)) (B atₛ j)) → |
| 192 |
RightInverse (I ×ₛ A) (J ×ₛ B) |
| 193 |
rightInverse {I = I} {J = J} {A = A} {B = B} I↪J A↪B = |
| 194 |
mkRightInverse equiv invʳ |
| 195 |
where |
| 196 |
equiv : Equivalence (I ×ₛ A) (J ×ₛ B) |
| 197 |
equiv = equivalence-↪ I↪J (RightInverse.equivalence A↪B) |
| 198 |
|
| 199 |
strictlyInvʳ : StrictlyInverseʳ (_≈_ (I ×ₛ A)) (Equivalence.to equiv) (Equivalence.from equiv) |
| 200 |
strictlyInvʳ (i , x) = strictlyInverseʳ I↪J i , IndexedSetoid.trans A (strictlyInverseʳ A↪B _) (cast-eq A (strictlyInverseʳ I↪J i)) |
| 201 |
|
| 202 |
invʳ : Inverseʳ (_≈_ (I ×ₛ A)) (_≈_ (J ×ₛ B)) (Equivalence.to equiv) (Equivalence.from equiv) |
| 203 |
invʳ = strictlyInverseʳ⇒inverseʳ (I ×ₛ A) (J ×ₛ B) (Equivalence.from-cong equiv) strictlyInvʳ |
| 204 |
|
| 205 |
------------------------------------------------------------------------ |
| 206 |
-- LeftInverse |
| 207 |
|
| 208 |
module _ where |
| 209 |
open LeftInverse |
| 210 |
open Setoid |
| 211 |
|
| 212 |
leftInverse : |
| 213 |
(I↩J : I ↩ J) → |
| 214 |
(∀ {i} → LeftInverse (A atₛ i) (B atₛ (to I↩J i))) → |
| 215 |
LeftInverse (I ×ₛ A) (J ×ₛ B) |
| 216 |
leftInverse {I = I} {J = J} {A = A} {B = B} I↩J A↩B = |
| 217 |
Sym.leftInverse (rightInverse (Sym.rightInverse I↩J) (Sym.rightInverse A↩B)) |
| 218 |
|
| 219 |
------------------------------------------------------------------------ |
| 220 |
-- Inverses |
| 221 |
|
| 222 |
module _ where |
| 223 |
open Inverse hiding (inverse) |
| 224 |
open Setoid |
| 225 |
|
| 226 |
inverse : (I↔J : I ↔ J) → |
| 227 |
(∀ {i} → Inverse (A atₛ i) (B atₛ (to I↔J i))) → |
| 228 |
Inverse (I ×ₛ A) (J ×ₛ B) |
| 229 |
inverse {I = I} {J = J} {A = A} {B = B} I↔J A↔B = record |
| 230 |
{ to = to′ |
| 231 |
; from = from′ |
| 232 |
; to-cong = to′-cong |
| 233 |
; from-cong = from′-cong |
| 234 |
; inverse = invˡ , invʳ |
| 235 |
} |
| 236 |
where |
| 237 |
to′ : Carrier (I ×ₛ A) → Carrier (J ×ₛ B) |
| 238 |
to′ (i , x) = to I↔J i , to A↔B x |
| 239 |
|
| 240 |
to′-cong : Congruent (_≈_ (I ×ₛ A)) (_≈_ (J ×ₛ B)) to′ |
| 241 |
to′-cong (≡.refl , x≈y) = to-cong I↔J ≡.refl , to-cong A↔B x≈y |
| 242 |
|
| 243 |
from′ : Carrier (J ×ₛ B) → Carrier (I ×ₛ A) |
| 244 |
from′ (j , y) = from I↔J j , from A↔B (cast B (strictlyInverseˡ I↔J _) y) |
| 245 |
|
| 246 |
from′-cong : Congruent (_≈_ (J ×ₛ B)) (_≈_ (I ×ₛ A)) from′ |
| 247 |
from′-cong (≡.refl , x≈y) = from-cong I↔J ≡.refl , from-cong A↔B (cast-cong B (strictlyInverseˡ I↔J _) x≈y) |
| 248 |
|
| 249 |
strictlyInvˡ : StrictlyInverseˡ (_≈_ (J ×ₛ B)) to′ from′ |
| 250 |
strictlyInvˡ (i , x) = strictlyInverseˡ I↔J i , |
| 251 |
IndexedSetoid.trans B (strictlyInverseˡ A↔B _) |
| 252 |
(cast-eq B (strictlyInverseˡ I↔J i)) |
| 253 |
|
| 254 |
invˡ : Inverseˡ (_≈_ (I ×ₛ A)) (_≈_ (J ×ₛ B)) to′ from′ |
| 255 |
invˡ = strictlyInverseˡ⇒inverseˡ (I ×ₛ A) (J ×ₛ B) to′-cong strictlyInvˡ |
| 256 |
|
| 257 |
lem : ∀ {i j} → i ≡ j → ∀ {x : IndexedSetoid.Carrier B (to I↔J i)} {y : IndexedSetoid.Carrier B (to I↔J j)} → |
| 258 |
IndexedSetoid._≈_ B x y → |
| 259 |
IndexedSetoid._≈_ A (from A↔B x) (from A↔B y) |
| 260 |
lem ≡.refl x≈y = from-cong A↔B x≈y |
| 261 |
|
| 262 |
strictlyInvʳ : StrictlyInverseʳ (_≈_ (I ×ₛ A)) to′ from′ |
| 263 |
strictlyInvʳ (i , x) = strictlyInverseʳ I↔J i , |
| 264 |
IndexedSetoid.trans A (lem (strictlyInverseʳ I↔J _) (cast-eq B (strictlyInverseˡ I↔J _))) (strictlyInverseʳ A↔B _) |
| 265 |
|
| 266 |
invʳ : Inverseʳ (_≈_ (I ×ₛ A)) (_≈_ (J ×ₛ B)) to′ from′ |
| 267 |
invʳ = strictlyInverseʳ⇒inverseʳ (I ×ₛ A) (J ×ₛ B) from′-cong strictlyInvʳ |
| 268 |
|
| 269 |
|
| 270 |
------------------------------------------------------------------------ |
| 271 |
-- DEPRECATED NAMES |
| 272 |
------------------------------------------------------------------------ |
| 273 |
-- Please use the new names as continuing support for the old names is |
| 274 |
-- not guaranteed. |
| 275 |
|
| 276 |
-- Version 2.3 |
| 277 |
|
| 278 |
left-inverse = rightInverse |
| 279 |
{-# WARNING_ON_USAGE left-inverse |
| 280 |
"Warning: left-inverse was deprecated in v2.3. |
| 281 |
Please use rightInverse or leftInverse instead." |
| 282 |
#-} |