Debian Codegraph @

src/Data/Product/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of products
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Product.Properties where
10
11 open import Axiom.UniquenessOfIdentityProofs using (UIP; module Decidable⇒UIP)
12 open import Data.Product.Base using (_,_; Σ; _×_; map; swap; ∃; ∃₂)
13 open import Function.Base using (_∋_; _∘_; id)
14 open import Function.Bundles using (_↔_; mk↔ₛ′)
15 open import Level using (Level)
16 open import Relation.Binary.Definitions using (DecidableEquality)
17 open import Relation.Binary.PropositionalEquality.Core
18 using (_≡_; refl; _≗_; subst; cong; cong₂; cong′)
19 open import Relation.Nullary.Decidable as Dec using (Dec; yes; no)
20
21 private
22 variable
23 a b c d ℓ : Level
24 A : Set a
25 B : Set b
26 C : Set c
27 D : Set d
28
29 ------------------------------------------------------------------------
30 -- Equality (dependent)
31
32 module _ {B : A → Set b} where
33
34 ,-injectiveˡ : ∀ {a c} {b : B a} {d : B c} → (a , b) ≡ (c , d) → a ≡ c
35 ,-injectiveˡ refl = refl
36
37 ,-injectiveʳ-≡ : ∀ {a b} {c : B a} {d : B b} → UIP A → (a , c) ≡ (b , d) → (q : a ≡ b) → subst B q c ≡ d
38 ,-injectiveʳ-≡ {c = c} u refl q = cong (λ x → subst B x c) (u q refl)
39
40 ,-injectiveʳ-UIP : ∀ {a} {b c : B a} → UIP A → (Σ A B ∋ (a , b)) ≡ (a , c) → b ≡ c
41 ,-injectiveʳ-UIP u p = ,-injectiveʳ-≡ u p refl
42
43 ≡-dec : DecidableEquality A → (∀ {a} → DecidableEquality (B a)) →
44 DecidableEquality (Σ A B)
45 ≡-dec dec₁ dec₂ (a , x) (b , y) with dec₁ a b
46 ... | no [a≢b] = no ([a≢b] ∘ ,-injectiveˡ)
47 ... | yes refl = Dec.map′ (cong (a ,_)) (,-injectiveʳ-UIP (Decidable⇒UIP.≡-irrelevant dec₁)) (dec₂ x y)
48
49 ------------------------------------------------------------------------
50 -- Equality (non-dependent)
51
52 ,-injectiveʳ : ∀ {a c : A} {b d : B} → (a , b) ≡ (c , d) → b ≡ d
53 ,-injectiveʳ refl = refl
54
55 ,-injective : ∀ {a c : A} {b d : B} → (a , b) ≡ (c , d) → a ≡ c × b ≡ d
56 ,-injective refl = refl , refl
57
58 map-cong : ∀ {f g : A → C} {h i : B → D} → f ≗ g → h ≗ i → map f h ≗ map g i
59 map-cong f≗g h≗i (x , y) = cong₂ _,_ (f≗g x) (h≗i y)
60
61 -- The following properties are definitionally true (because of η)
62 -- but for symmetry with ⊎ it is convenient to define and name them.
63
64 swap-involutive : swap {A = A} {B = B} ∘ swap ≗ id
65 swap-involutive _ = refl
66
67 ------------------------------------------------------------------------
68 -- Equality between pairs can be expressed as a pair of equalities
69
70 module _ {A : Set a} {B : A → Set b} {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B} where
71 Σ-≡,≡→≡ : Σ (a₁ ≡ a₂) (λ p → subst B p b₁ ≡ b₂) → p₁ ≡ p₂
72 Σ-≡,≡→≡ (refl , refl) = refl
73
74 Σ-≡,≡←≡ : p₁ ≡ p₂ → Σ (a₁ ≡ a₂) (λ p → subst B p b₁ ≡ b₂)
75 Σ-≡,≡←≡ refl = refl , refl
76
77 private
78 left-inverse-of : (p : Σ (a₁ ≡ a₂) (λ x → subst B x b₁ ≡ b₂)) →
79 Σ-≡,≡←≡ (Σ-≡,≡→≡ p) ≡ p
80 left-inverse-of (refl , refl) = refl
81
82 right-inverse-of : (p : p₁ ≡ p₂) → Σ-≡,≡→≡ (Σ-≡,≡←≡ p) ≡ p
83 right-inverse-of refl = refl
84
85 Σ-≡,≡↔≡ : (∃ λ (p : a₁ ≡ a₂) → subst B p b₁ ≡ b₂) ↔ p₁ ≡ p₂
86 Σ-≡,≡↔≡ = mk↔ₛ′ Σ-≡,≡→≡ Σ-≡,≡←≡ right-inverse-of left-inverse-of
87
88 -- the non-dependent case. Proofs are exactly as above, and straightforward.
89 module _ {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : A × B} where
90 ×-≡,≡→≡ : (a₁ ≡ a₂ × b₁ ≡ b₂) → p₁ ≡ p₂
91 ×-≡,≡→≡ (refl , refl) = refl
92
93 ×-≡,≡←≡ : p₁ ≡ p₂ → (a₁ ≡ a₂ × b₁ ≡ b₂)
94 ×-≡,≡←≡ refl = refl , refl
95
96 ×-≡,≡↔≡ : (a₁ ≡ a₂ × b₁ ≡ b₂) ↔ p₁ ≡ p₂
97 ×-≡,≡↔≡ = mk↔ₛ′
98 ×-≡,≡→≡
99 ×-≡,≡←≡
100 (λ { refl → refl })
101 (λ { (refl , refl) → refl })
102
103 ------------------------------------------------------------------------
104 -- The order of ∃₂ can be swapped
105
106 ∃∃↔∃∃ : (R : A → B → Set ℓ) → (∃₂ λ x y → R x y) ↔ (∃₂ λ y x → R x y)
107 ∃∃↔∃∃ R = mk↔ₛ′ to from cong′ cong′
108 where
109 to : (∃₂ λ x y → R x y) → (∃₂ λ y x → R x y)
110 to (x , y , Rxy) = (y , x , Rxy)
111
112 from : (∃₂ λ y x → R x y) → (∃₂ λ x y → R x y)
113 from (y , x , Rxy) = (x , y , Rxy)