| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Dependent product combinators for propositional equality |
| 5 |
-- preserving functions |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module Data.Product.Function.Dependent.Propositional where |
| 11 |
|
| 12 |
open import Data.Product.Base as Product using (Σ; map; proj₂; _,_) |
| 13 |
open import Data.Product.Properties using (Σ-≡,≡→≡; Σ-≡,≡↔≡; Σ-≡,≡←≡) |
| 14 |
open import Level using (Level; 0ℓ) |
| 15 |
open import Function.Related.Propositional |
| 16 |
using (_∼[_]_; module EquationalReasoning; K-reflexive; |
| 17 |
implication; reverseImplication; equivalence; injection; |
| 18 |
reverseInjection; leftInverse; surjection; bijection) |
| 19 |
open import Function.Base using (_$_; _∘_; _∘′_) |
| 20 |
open import Function.Properties.Inverse using (↔⇒↠; ↔⇒⟶; ↔⇒⟵; ↔-sym; ↔⇒↩; refl) |
| 21 |
open import Function.Properties.RightInverse using (↩⇒↪; ↪⇒↩) |
| 22 |
open import Function.Properties.Inverse.HalfAdjointEquivalence |
| 23 |
using (↔⇒≃; _≃_; ≃⇒↔) |
| 24 |
open import Function.Consequences.Propositional |
| 25 |
using (inverseʳ⇒injective; strictlySurjective⇒surjective) |
| 26 |
open import Function.Definitions using (Inverseˡ; Inverseʳ; Injective; StrictlySurjective) |
| 27 |
open import Function.Bundles |
| 28 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 29 |
open import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 30 |
using (module ≡-Reasoning) |
| 31 |
open import Function.Construct.Symmetry using (↩-sym; ↪-sym) |
| 32 |
|
| 33 |
private |
| 34 |
variable |
| 35 |
i a b c d : Level |
| 36 |
I J : Set i |
| 37 |
A B : I → Set a |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Functions |
| 41 |
|
| 42 |
module _ where |
| 43 |
open Func |
| 44 |
|
| 45 |
Σ-⟶ : (I⟶J : I ⟶ J) → |
| 46 |
(∀ {i} → A i ⟶ B (to I⟶J i)) → |
| 47 |
Σ I A ⟶ Σ J B |
| 48 |
Σ-⟶ I⟶J A⟶B = mk⟶ $ Product.map (to I⟶J) (to A⟶B) |
| 49 |
|
| 50 |
------------------------------------------------------------------------ |
| 51 |
-- Equivalences |
| 52 |
|
| 53 |
module _ where |
| 54 |
open Surjection |
| 55 |
|
| 56 |
Σ-⇔ : (I↠J : I ↠ J) → |
| 57 |
(∀ {i} → A i ⇔ B (to I↠J i)) → |
| 58 |
Σ I A ⇔ Σ J B |
| 59 |
Σ-⇔ {B = B} I↠J A⇔B = mk⇔ |
| 60 |
(map (to I↠J) (Equivalence.to A⇔B)) |
| 61 |
(map (to⁻ I↠J) (Equivalence.from A⇔B ∘ ≡.subst B (≡.sym (proj₂ (surjective I↠J _) ≡.refl)))) |
| 62 |
|
| 63 |
-- See also Data.Product.Relation.Binary.Pointwise.Dependent.WithK.↣. |
| 64 |
|
| 65 |
------------------------------------------------------------------------ |
| 66 |
-- Injections |
| 67 |
|
| 68 |
module _ where |
| 69 |
|
| 70 |
Σ-↣ : (I↔J : I ↔ J) → |
| 71 |
(∀ {i} → A i ↣ B (Inverse.to I↔J i)) → |
| 72 |
Σ I A ↣ Σ J B |
| 73 |
Σ-↣ {I = I} {J = J} {A = A} {B = B} I↔J A↣B = mk↣ to-injective |
| 74 |
where |
| 75 |
open ≡.≡-Reasoning |
| 76 |
|
| 77 |
I≃J = ↔⇒≃ I↔J |
| 78 |
|
| 79 |
subst-application′ : |
| 80 |
let open _≃_ I≃J in |
| 81 |
{x₁ x₂ : I} {y : A (from (to x₁))} |
| 82 |
(g : ∀ x → A (from (to x)) → B (to x)) |
| 83 |
(eq : to x₁ ≡ to x₂) → |
| 84 |
≡.subst B eq (g x₁ y) ≡ g x₂ (≡.subst A (≡.cong from eq) y) |
| 85 |
subst-application′ {x₁} {x₂} {y} g eq = |
| 86 |
≡.subst B eq (g x₁ y) ≡⟨ ≡.cong (≡.subst B eq) (≡.sym (g′-lemma _ _)) ⟩ |
| 87 |
≡.subst B eq (g′ (to x₁) y) ≡⟨ ≡.subst-application A g′ eq ⟩ |
| 88 |
g′ (to x₂) (≡.subst A (≡.cong from eq) y) ≡⟨ g′-lemma _ _ ⟩ |
| 89 |
g x₂ (≡.subst A (≡.cong from eq) y) ∎ |
| 90 |
where |
| 91 |
open _≃_ I≃J |
| 92 |
|
| 93 |
g′ : ∀ x → A (from x) → B x |
| 94 |
g′ x = |
| 95 |
≡.subst B (right-inverse-of x) ∘ |
| 96 |
g (from x) ∘ |
| 97 |
≡.subst A (≡.sym (≡.cong from (right-inverse-of x))) |
| 98 |
|
| 99 |
g′-lemma : ∀ x y → g′ (to x) y ≡ g x y |
| 100 |
g′-lemma x y = |
| 101 |
≡.subst B (right-inverse-of (to x)) |
| 102 |
(g (from (to x)) $ |
| 103 |
≡.subst A (≡.sym (≡.cong from (right-inverse-of (to x)))) y) ≡⟨ ≡.cong (λ p → ≡.subst B p (g (from (to x)) |
| 104 |
(≡.subst A (≡.sym (≡.cong from p)) y))) |
| 105 |
(≡.sym (left-right x)) ⟩ |
| 106 |
≡.subst B (≡.cong to (left-inverse-of x)) |
| 107 |
(g (from (to x)) $ |
| 108 |
≡.subst A |
| 109 |
(≡.sym (≡.cong from (≡.cong to (left-inverse-of x)))) |
| 110 |
y) ≡⟨ lemma _ ⟩ |
| 111 |
|
| 112 |
g x y ∎ |
| 113 |
where |
| 114 |
lemma : ∀ {x′} eq {y : A (from (to x′))} → |
| 115 |
≡.subst B (≡.cong to eq) |
| 116 |
(g (from (to x)) |
| 117 |
(≡.subst A (≡.sym (≡.cong from (≡.cong to eq))) y)) ≡ |
| 118 |
g x′ y |
| 119 |
lemma ≡.refl = ≡.refl |
| 120 |
|
| 121 |
open Injection |
| 122 |
|
| 123 |
to′ : Σ I A → Σ J B |
| 124 |
to′ = Product.map (_≃_.to I≃J) (to A↣B) |
| 125 |
|
| 126 |
to-injective : Injective _≡_ _≡_ to′ |
| 127 |
to-injective {(x₁ , x₂)} {(y₁ , y₂)} = |
| 128 |
|
| 129 |
Σ-≡,≡→≡ ∘′ |
| 130 |
|
| 131 |
map (_≃_.injective I≃J) (λ {eq₁} eq₂ → injective A↣B ( |
| 132 |
to A↣B (≡.subst A (_≃_.injective I≃J eq₁) x₂) ≡⟨⟩ |
| 133 |
|
| 134 |
(let eq = |
| 135 |
≡.trans (≡.sym (_≃_.left-inverse-of I≃J x₁)) |
| 136 |
(≡.trans (≡.cong (_≃_.from I≃J) eq₁) |
| 137 |
(≡.trans (_≃_.left-inverse-of I≃J y₁) |
| 138 |
≡.refl)) in |
| 139 |
to A↣B (≡.subst A eq x₂)) ≡⟨ ≡.cong (λ p → to A↣B |
| 140 |
(≡.subst A |
| 141 |
(≡.trans (≡.sym (_≃_.left-inverse-of I≃J _)) |
| 142 |
(≡.trans (≡.cong (_≃_.from I≃J) eq₁) p)) |
| 143 |
x₂)) |
| 144 |
(≡.trans-reflʳ _) ⟩ |
| 145 |
|
| 146 |
(let eq = ≡.trans (≡.sym (_≃_.left-inverse-of I≃J x₁)) |
| 147 |
(≡.trans (≡.cong (_≃_.from I≃J) eq₁) |
| 148 |
(_≃_.left-inverse-of I≃J y₁)) in |
| 149 |
to A↣B (≡.subst A eq x₂)) ≡⟨ ≡.cong (to A↣B) |
| 150 |
(≡.sym (≡.subst-subst (≡.sym (_≃_.left-inverse-of I≃J _)))) ⟩ |
| 151 |
|
| 152 |
to A↣B ((≡.subst A (≡.trans (≡.cong (_≃_.from I≃J) eq₁) |
| 153 |
(_≃_.left-inverse-of I≃J y₁)) $ |
| 154 |
≡.subst A (≡.sym (_≃_.left-inverse-of I≃J x₁)) x₂)) ≡⟨ ≡.cong (to A↣B) |
| 155 |
(≡.sym (≡.subst-subst (≡.cong (_≃_.from I≃J) eq₁))) ⟩ |
| 156 |
to A↣B ( |
| 157 |
(≡.subst A (_≃_.left-inverse-of I≃J y₁) $ |
| 158 |
≡.subst A (≡.cong (_≃_.from I≃J) eq₁) $ |
| 159 |
≡.subst A (≡.sym (_≃_.left-inverse-of I≃J x₁)) x₂)) ≡⟨ ≡.sym (subst-application′ |
| 160 |
(λ x y → to A↣B |
| 161 |
(≡.subst A (_≃_.left-inverse-of I≃J x) y)) |
| 162 |
eq₁) ⟩ |
| 163 |
≡.subst B eq₁ (to A↣B $ |
| 164 |
(≡.subst A (_≃_.left-inverse-of I≃J x₁) $ |
| 165 |
≡.subst A (≡.sym (_≃_.left-inverse-of I≃J x₁)) x₂)) ≡⟨ ≡.cong (≡.subst B eq₁ ∘ to A↣B) |
| 166 |
(≡.subst-subst (≡.sym (_≃_.left-inverse-of I≃J _))) ⟩ |
| 167 |
|
| 168 |
(let eq = ≡.trans (≡.sym (_≃_.left-inverse-of I≃J x₁)) |
| 169 |
(_≃_.left-inverse-of I≃J x₁) in |
| 170 |
≡.subst B eq₁ (to A↣B (≡.subst A eq x₂))) ≡⟨ ≡.cong (λ p → ≡.subst B eq₁ (to A↣B (≡.subst A p x₂))) |
| 171 |
(≡.trans-symˡ (_≃_.left-inverse-of I≃J _)) ⟩ |
| 172 |
≡.subst B eq₁ (to A↣B (≡.subst A ≡.refl x₂)) ≡⟨⟩ |
| 173 |
|
| 174 |
≡.subst B eq₁ (to A↣B x₂) ≡⟨ eq₂ ⟩ |
| 175 |
|
| 176 |
to A↣B y₂ ∎ |
| 177 |
|
| 178 |
)) ∘ |
| 179 |
|
| 180 |
Σ-≡,≡←≡ |
| 181 |
|
| 182 |
------------------------------------------------------------------------ |
| 183 |
-- Surjections |
| 184 |
|
| 185 |
module _ where |
| 186 |
open Surjection |
| 187 |
|
| 188 |
Σ-↠ : (I↠J : I ↠ J) → |
| 189 |
(∀ {x} → A x ↠ B (to I↠J x)) → |
| 190 |
Σ I A ↠ Σ J B |
| 191 |
Σ-↠ {I = I} {J = J} {A = A} {B = B} I↠J A↠B = |
| 192 |
mk↠ₛ strictlySurjective′ |
| 193 |
where |
| 194 |
to′ : Σ I A → Σ J B |
| 195 |
to′ = map (to I↠J) (to A↠B) |
| 196 |
|
| 197 |
backcast : ∀ {i} → B i → B (to I↠J (to⁻ I↠J i)) |
| 198 |
backcast = ≡.subst B (≡.sym (to∘to⁻ I↠J _)) |
| 199 |
|
| 200 |
to⁻′ : Σ J B → Σ I A |
| 201 |
to⁻′ = map (to⁻ I↠J) (Surjection.to⁻ A↠B ∘ backcast) |
| 202 |
|
| 203 |
strictlySurjective′ : StrictlySurjective _≡_ to′ |
| 204 |
strictlySurjective′ (x , y) = to⁻′ (x , y) , Σ-≡,≡→≡ |
| 205 |
( to∘to⁻ I↠J x |
| 206 |
, (≡.subst B (to∘to⁻ I↠J x) (to A↠B (to⁻ A↠B (backcast y))) ≡⟨ ≡.cong (≡.subst B _) (to∘to⁻ A↠B _) ⟩ |
| 207 |
≡.subst B (to∘to⁻ I↠J x) (backcast y) ≡⟨ ≡.subst-subst-sym (to∘to⁻ I↠J x) ⟩ |
| 208 |
y ∎) |
| 209 |
) where open ≡.≡-Reasoning |
| 210 |
|
| 211 |
|
| 212 |
------------------------------------------------------------------------ |
| 213 |
-- Left inverses |
| 214 |
|
| 215 |
module _ where |
| 216 |
open LeftInverse |
| 217 |
|
| 218 |
Σ-↩ : (I↩J : I ↩ J) → |
| 219 |
(∀ {i} → A i ↩ B (to I↩J i)) → |
| 220 |
Σ I A ↩ Σ J B |
| 221 |
Σ-↩ {I = I} {J = J} {A = A} {B = B} I↩J A↩B = mk↩ {to = to′ } {from = from′} inv |
| 222 |
where |
| 223 |
to′ : Σ I A → Σ J B |
| 224 |
to′ = map (to I↩J) (to A↩B) |
| 225 |
|
| 226 |
backcast : ∀ {j} → B j → B (to I↩J (from I↩J j)) |
| 227 |
backcast = ≡.subst B (≡.sym (inverseˡ I↩J ≡.refl)) |
| 228 |
|
| 229 |
from′ : Σ J B → Σ I A |
| 230 |
from′ = map (from I↩J) (from A↩B ∘ backcast) |
| 231 |
|
| 232 |
inv : Inverseˡ _≡_ _≡_ to′ from′ |
| 233 |
inv {j , b} ≡.refl = Σ-≡,≡→≡ (strictlyInverseˡ I↩J j , ( |
| 234 |
begin |
| 235 |
≡.subst B (inverseˡ I↩J ≡.refl) (to A↩B (from A↩B (backcast b))) ≡⟨ ≡.cong (≡.subst B _) (inverseˡ A↩B ≡.refl) ⟩ |
| 236 |
≡.subst B (inverseˡ I↩J ≡.refl) (backcast b) ≡⟨ ≡.subst-subst-sym (inverseˡ I↩J _) ⟩ |
| 237 |
b ∎)) where open ≡.≡-Reasoning |
| 238 |
|
| 239 |
------------------------------------------------------------------------ |
| 240 |
-- Right inverses |
| 241 |
|
| 242 |
module _ where |
| 243 |
open RightInverse |
| 244 |
|
| 245 |
-- the dual to Σ-↩, taking advantage of the proof above by threading |
| 246 |
-- relevant symmetry proofs through it. |
| 247 |
Σ-↪ : (I↪J : I ↪ J) → (∀ {j} → A (from I↪J j) ↪ B j) → Σ I A ↪ Σ J B |
| 248 |
Σ-↪ I↪J A↪B = ↩-sym (Σ-↩ (↪-sym I↪J) (↪-sym A↪B)) |
| 249 |
|
| 250 |
------------------------------------------------------------------------ |
| 251 |
-- Inverses |
| 252 |
|
| 253 |
module _ where |
| 254 |
open Inverse |
| 255 |
|
| 256 |
Σ-↔ : (I↔J : I ↔ J) → |
| 257 |
(∀ {x} → A x ↔ B (to I↔J x)) → |
| 258 |
Σ I A ↔ Σ J B |
| 259 |
Σ-↔ {I = I} {J = J} {A = A} {B = B} I↔J A↔B = mk↔ₛ′ |
| 260 |
(Surjection.to surjection′) |
| 261 |
(Surjection.to⁻ surjection′) |
| 262 |
(Surjection.to∘to⁻ surjection′) |
| 263 |
left-inverse-of |
| 264 |
where |
| 265 |
open ≡.≡-Reasoning |
| 266 |
|
| 267 |
I≃J = ↔⇒≃ I↔J |
| 268 |
|
| 269 |
surjection′ : Σ I A ↠ Σ J B |
| 270 |
surjection′ = Σ-↠ (↔⇒↠ (≃⇒↔ I≃J)) (↔⇒↠ A↔B) |
| 271 |
|
| 272 |
left-inverse-of : ∀ p → Surjection.to⁻ surjection′ (Surjection.to surjection′ p) ≡ p |
| 273 |
left-inverse-of (x , y) = to Σ-≡,≡↔≡ |
| 274 |
( _≃_.left-inverse-of I≃J x |
| 275 |
, (≡.subst A (_≃_.left-inverse-of I≃J x) |
| 276 |
(from A↔B |
| 277 |
(≡.subst B (≡.sym (_≃_.right-inverse-of I≃J |
| 278 |
(_≃_.to I≃J x))) |
| 279 |
(to A↔B y))) ≡⟨ ≡.subst-application B (λ _ → from A↔B) _ ⟩ |
| 280 |
|
| 281 |
from A↔B |
| 282 |
(≡.subst B (≡.cong (_≃_.to I≃J) |
| 283 |
(_≃_.left-inverse-of I≃J x)) |
| 284 |
(≡.subst B (≡.sym (_≃_.right-inverse-of I≃J |
| 285 |
(_≃_.to I≃J x))) |
| 286 |
(to A↔B y))) ≡⟨ ≡.cong (λ eq → from A↔B (≡.subst B eq |
| 287 |
(≡.subst B (≡.sym (_≃_.right-inverse-of I≃J _)) _))) |
| 288 |
(_≃_.left-right I≃J _) ⟩ |
| 289 |
from A↔B |
| 290 |
(≡.subst B (_≃_.right-inverse-of I≃J |
| 291 |
(_≃_.to I≃J x)) |
| 292 |
(≡.subst B (≡.sym (_≃_.right-inverse-of I≃J |
| 293 |
(_≃_.to I≃J x))) |
| 294 |
(to A↔B y))) ≡⟨ ≡.cong (from A↔B) |
| 295 |
(≡.subst-subst-sym (_≃_.right-inverse-of I≃J _)) ⟩ |
| 296 |
|
| 297 |
from A↔B (to A↔B y) ≡⟨ Inverse.strictlyInverseʳ A↔B _ ⟩ |
| 298 |
|
| 299 |
y ∎) |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
private module _ where |
| 304 |
open Inverse |
| 305 |
|
| 306 |
swap-coercions : ∀ {k} (B : J → Set b) |
| 307 |
(I↔J : _↔_ I J) → |
| 308 |
(∀ {x} → A x ∼[ k ] B (to I↔J x)) → |
| 309 |
∀ {x} → A (from I↔J x) ∼[ k ] B x |
| 310 |
swap-coercions {A = A} B I↔J eq {x} = |
| 311 |
A (from I↔J x) ∼⟨ eq ⟩ |
| 312 |
B (to I↔J (from I↔J x)) ↔⟨ K-reflexive (≡.cong B $ strictlyInverseˡ I↔J x) ⟩ |
| 313 |
B x ∎ |
| 314 |
where open EquationalReasoning |
| 315 |
|
| 316 |
|
| 317 |
cong : ∀ {k} (I↔J : I ↔ J) → |
| 318 |
(∀ {x} → A x ∼[ k ] B (Inverse.to I↔J x)) → |
| 319 |
Σ I A ∼[ k ] Σ J B |
| 320 |
cong {k = implication} I↔J A⟶B = Σ-⟶ (↔⇒⟶ I↔J) A⟶B |
| 321 |
cong {B = B} {k = reverseImplication} I↔J A⟵B = Σ-⟶ (↔⇒⟵ I↔J) (swap-coercions {k = reverseImplication} B I↔J A⟵B) |
| 322 |
cong {k = equivalence} I↔J A⇔B = Σ-⇔ (↔⇒↠ I↔J) A⇔B |
| 323 |
cong {k = injection} I↔J A↣B = Σ-↣ I↔J A↣B |
| 324 |
cong {B = B} {k = reverseInjection} I↔J A↢B = Σ-↣ (↔-sym I↔J) (swap-coercions {k = reverseInjection} B I↔J A↢B) |
| 325 |
cong {B = B} {k = leftInverse} I↔J A↩B = ↩⇒↪ (Σ-↩ (↔⇒↩ (↔-sym I↔J)) (↪⇒↩ (swap-coercions {k = leftInverse} B I↔J A↩B))) |
| 326 |
cong {k = surjection} I↔J A↠B = Σ-↠ (↔⇒↠ I↔J) A↠B |
| 327 |
cong {k = bijection} I↔J A↔B = Σ-↔ I↔J A↔B |
| 328 |
|
| 329 |
congˡ : ∀ {k} → (∀ {x} → A x ∼[ k ] B x) → Σ I A ∼[ k ] Σ I B |
| 330 |
congˡ = cong (refl _) |