Debian Codegraph @

src/Relation/Binary/PropositionalEquality/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Propositional equality
5 --
6 -- This file contains some core properies of propositional equality
7 -- which are re-exported by Relation.Binary.PropositionalEquality. They
8 -- are ``equality rearrangement'' lemmas.
9 ------------------------------------------------------------------------
10
11 {-# OPTIONS --cubical-compatible --safe #-}
12
13 module Relation.Binary.PropositionalEquality.Properties where
14
15 open import Function.Base using (id; _∘_)
16 open import Level using (Level)
17 open import Relation.Binary.Bundles using (Setoid; DecSetoid; Preorder; Poset)
18 open import Relation.Binary.Structures
19 using (IsEquivalence; IsDecEquivalence; IsPreorder; IsPartialOrder)
20 open import Relation.Binary.Definitions using (DecidableEquality)
21 import Relation.Binary.Properties.Setoid as Setoid
22 using (≈-isPreorder; ≈-isPartialOrder; ≈-preorder; ≈-poset)
23 open import Relation.Binary.PropositionalEquality.Core
24 using (_≡_; cong; refl; subst; trans; sym; subst₂; cong₂)
25 open import Relation.Binary.Reasoning.Syntax
26 open import Relation.Unary using (Pred)
27
28
29
30 private
31 variable
32 a b c p : Level
33 A B C : Set a
34
35 ------------------------------------------------------------------------
36 -- Standard eliminator for the propositional equality type
37
38 J : {A : Set a} {x : A} (B : (y : A) → x ≡ y → Set b)
39 {y : A} (p : x ≡ y) → B x refl → B y p
40 J B refl b = b
41
42 ------------------------------------------------------------------------
43 -- Binary and/or dependent versions of standard operations on equality
44
45 dcong : ∀ {A : Set a} {B : A → Set b} (f : (x : A) → B x) {x y}
46 → (p : x ≡ y) → subst B p (f x) ≡ f y
47 dcong f refl = refl
48
49 dcong₂ : ∀ {A : Set a} {B : A → Set b} {C : Set c}
50 (f : (x : A) → B x → C) {x₁ x₂ y₁ y₂}
51 → (p : x₁ ≡ x₂) → subst B p y₁ ≡ y₂
52 → f x₁ y₁ ≡ f x₂ y₂
53 dcong₂ f refl refl = refl
54
55 dsubst₂ : ∀ {A : Set a} {B : A → Set b} (C : (x : A) → B x → Set c)
56 {x₁ x₂ y₁ y₂} (p : x₁ ≡ x₂) → subst B p y₁ ≡ y₂
57 → C x₁ y₁ → C x₂ y₂
58 dsubst₂ C refl refl c = c
59
60 ddcong₂ : ∀ {A : Set a} {B : A → Set b} {C : (x : A) → B x → Set c}
61 (f : (x : A) (y : B x) → C x y) {x₁ x₂ y₁ y₂}
62 (p : x₁ ≡ x₂) (q : subst B p y₁ ≡ y₂)
63 → dsubst₂ C p q (f x₁ y₁) ≡ f x₂ y₂
64 ddcong₂ f refl refl = refl
65
66 ------------------------------------------------------------------------
67 -- Various equality rearrangement lemmas
68
69 trans-reflʳ : ∀ {x y : A} (p : x ≡ y) → trans p refl ≡ p
70 trans-reflʳ refl = refl
71
72 trans-assoc : ∀ {x y z u : A} (p : x ≡ y) {q : y ≡ z} {r : z ≡ u} →
73 trans (trans p q) r ≡ trans p (trans q r)
74 trans-assoc refl = refl
75
76 trans-symˡ : ∀ {x y : A} (p : x ≡ y) → trans (sym p) p ≡ refl
77 trans-symˡ refl = refl
78
79 trans-symʳ : ∀ {x y : A} (p : x ≡ y) → trans p (sym p) ≡ refl
80 trans-symʳ refl = refl
81
82 trans-injectiveˡ : ∀ {x y z : A} {p₁ p₂ : x ≡ y} (q : y ≡ z) →
83 trans p₁ q ≡ trans p₂ q → p₁ ≡ p₂
84 trans-injectiveˡ refl = subst₂ _≡_ (trans-reflʳ _) (trans-reflʳ _)
85
86 trans-injectiveʳ : ∀ {x y z : A} (p : x ≡ y) {q₁ q₂ : y ≡ z} →
87 trans p q₁ ≡ trans p q₂ → q₁ ≡ q₂
88 trans-injectiveʳ refl eq = eq
89
90 cong-id : ∀ {x y : A} (p : x ≡ y) → cong id p ≡ p
91 cong-id refl = refl
92
93 cong-∘ : ∀ {x y : A} {f : B → C} {g : A → B} (p : x ≡ y) →
94 cong (f ∘ g) p ≡ cong f (cong g p)
95 cong-∘ refl = refl
96
97 sym-cong : ∀ {x y : A} {f : A → B} (p : x ≡ y) → sym (cong f p) ≡ cong f (sym p)
98 sym-cong refl = refl
99
100 trans-cong : ∀ {x y z : A} {f : A → B} (p : x ≡ y) {q : y ≡ z} →
101 trans (cong f p) (cong f q) ≡ cong f (trans p q)
102 trans-cong refl = refl
103
104 cong₂-reflˡ : ∀ {_∙_ : A → B → C} {x u v} → (p : u ≡ v) →
105 cong₂ _∙_ refl p ≡ cong (x ∙_) p
106 cong₂-reflˡ refl = refl
107
108 cong₂-reflʳ : ∀ {_∙_ : A → B → C} {x y u} → (p : x ≡ y) →
109 cong₂ _∙_ p refl ≡ cong (_∙ u) p
110 cong₂-reflʳ refl = refl
111
112 module _ {P : Pred A p} {x y : A} where
113
114 subst-injective : ∀ (x≡y : x ≡ y) {p q : P x} →
115 subst P x≡y p ≡ subst P x≡y q → p ≡ q
116 subst-injective refl p≡q = p≡q
117
118 subst-subst : ∀ {z} (x≡y : x ≡ y) {y≡z : y ≡ z} {p : P x} →
119 subst P y≡z (subst P x≡y p) ≡ subst P (trans x≡y y≡z) p
120 subst-subst refl = refl
121
122 subst-subst-sym : (x≡y : x ≡ y) {p : P y} →
123 subst P x≡y (subst P (sym x≡y) p) ≡ p
124 subst-subst-sym refl = refl
125
126 subst-sym-subst : (x≡y : x ≡ y) {p : P x} →
127 subst P (sym x≡y) (subst P x≡y p) ≡ p
128 subst-sym-subst refl = refl
129
130 subst-∘ : ∀ {x y : A} {P : Pred B p} {f : A → B}
131 (x≡y : x ≡ y) {p : P (f x)} →
132 subst (P ∘ f) x≡y p ≡ subst P (cong f x≡y) p
133 subst-∘ refl = refl
134
135 -- Lemma 2.3.11 in the HoTT book, and `transport_map` in the UniMath
136 -- library
137 subst-application′ : ∀ {a b₁ b₂} {A : Set a}
138 (B₁ : A → Set b₁) {B₂ : A → Set b₂}
139 {x₁ x₂ : A} {y : B₁ x₁}
140 (g : ∀ x → B₁ x → B₂ x) (eq : x₁ ≡ x₂) →
141 subst B₂ eq (g x₁ y) ≡ g x₂ (subst B₁ eq y)
142 subst-application′ _ _ refl = refl
143
144 subst-application : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂}
145 (B₁ : A₁ → Set b₁) {B₂ : A₂ → Set b₂}
146 {f : A₂ → A₁} {x₁ x₂ : A₂} {y : B₁ (f x₁)}
147 (g : ∀ x → B₁ (f x) → B₂ x) (eq : x₁ ≡ x₂) →
148 subst B₂ eq (g x₁ y) ≡ g x₂ (subst B₁ (cong f eq) y)
149 subst-application _ _ refl = refl
150
151 ------------------------------------------------------------------------
152 -- Structure of equality as a binary relation
153
154 isEquivalence : IsEquivalence {A = A} _≡_
155 isEquivalence = record
156 { refl = refl
157 ; sym = sym
158 ; trans = trans
159 }
160
161 isDecEquivalence : DecidableEquality A → IsDecEquivalence _≡_
162 isDecEquivalence _≟_ = record
163 { isEquivalence = isEquivalence
164 ; _≟_ = _≟_
165 }
166
167 setoid : Set a → Setoid _ _
168 setoid A = record
169 { Carrier = A
170 ; _≈_ = _≡_
171 ; isEquivalence = isEquivalence
172 }
173
174 decSetoid : DecidableEquality A → DecSetoid _ _
175 decSetoid _≟_ = record
176 { _≈_ = _≡_
177 ; isDecEquivalence = isDecEquivalence _≟_
178 }
179
180 ------------------------------------------------------------------------
181 -- Bundles for equality as a binary relation
182
183 isPreorder : IsPreorder {A = A} _≡_ _≡_
184 isPreorder = Setoid.≈-isPreorder (setoid _)
185
186 isPartialOrder : IsPartialOrder {A = A} _≡_ _≡_
187 isPartialOrder = Setoid.≈-isPartialOrder (setoid _)
188
189 preorder : Set a → Preorder _ _ _
190 preorder A = Setoid.≈-preorder (setoid A)
191
192 poset : Set a → Poset _ _ _
193 poset A = Setoid.≈-poset (setoid A)
194
195 ------------------------------------------------------------------------
196 -- Reasoning
197
198 -- This is a special instance of `Relation.Binary.Reasoning.Setoid`.
199 -- Rather than instantiating the latter with (setoid A), we reimplement
200 -- equation chains from scratch since then goals are printed much more
201 -- readably.
202 module ≡-Reasoning {a} {A : Set a} where
203
204 open begin-syntax {A = A} _≡_ id public
205 open ≡-syntax {A = A} _≡_ trans public
206 open end-syntax {A = A} _≡_ refl public