Debian Codegraph @

src/Relation/Binary/HeterogeneousEquality/Quotients.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Quotients for Heterogeneous equality
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K --safe #-}
8
9 module Relation.Binary.HeterogeneousEquality.Quotients where
10
11 open import Function.Base using (_$_; const; _∘_)
12 open import Level hiding (lift)
13 open import Relation.Binary.Bundles using (Setoid)
14 open import Relation.Binary.HeterogeneousEquality
15 open ≅-Reasoning
16
17 record Quotient {c ℓ} (S : Setoid c ℓ) : Set (suc (c ⊔ ℓ)) where
18 open Setoid S renaming (Carrier to A)
19 field
20 Q : Set c
21 abs : A → Q
22
23 compat : (B : Q → Set c) (f : ∀ a → B (abs a)) → Set (c ⊔ ℓ)
24 compat _ f = {a a′ : A} → a ≈ a′ → f a ≅ f a′
25
26 field
27 compat-abs : compat _ abs
28 lift : (B : Q → Set c) (f : ∀ a → B (abs a)) → compat B f → ∀ q → B q
29 lift-conv : {B : Q → Set c} (f : ∀ a → B (abs a)) (p : compat B f) →
30 ∀ a → lift B f p (abs a) ≅ f a
31
32 Quotients : ∀ c ℓ → Set (suc (c ⊔ ℓ))
33 Quotients c ℓ = (S : Setoid c ℓ) → Quotient S
34
35 module Properties {c ℓ} {S : Setoid c ℓ} (Qu : Quotient S) where
36
37 open Setoid S renaming (Carrier to A) hiding (refl; sym; trans)
38 open Quotient Qu
39
40 module _ {B B′ : Q → Set c} {f : ∀ a → B (abs a)} {p : compat B f} where
41
42 lift-unique : {g : ∀ q → B′ q} → (∀ a → g (abs a) ≅ f a) →
43 ∀ x → lift B f p x ≅ g x
44 lift-unique {g} ext = lift _ liftf≅g liftf≅g-ext where
45
46 liftf≅g : ∀ a → lift B f p (abs a) ≅ g (abs a)
47 liftf≅g x = begin
48 lift _ f p (abs x) ≅⟨ lift-conv f p x ⟩
49 f x ≅⟨ sym (ext x) ⟩
50 g (abs x) ∎
51
52 liftf≅g-ext : ∀ {a a′} → a ≈ a′ → liftf≅g a ≅ liftf≅g a′
53 liftf≅g-ext eq = ≅-heterogeneous-irrelevantˡ _ _
54 $ cong (lift B f p) (compat-abs eq)
55
56 lift-ext : {g : ∀ a → B′ (abs a)} {p′ : compat B′ g} → (∀ x → f x ≅ g x) →
57 ∀ x → lift B f p x ≅ lift B′ g p′ x
58 lift-ext {g} {p′} h = lift-unique $ λ a → begin
59 lift B′ g p′ (abs a) ≅⟨ lift-conv g p′ a ⟩
60 g a ≅⟨ sym (h a) ⟩
61 f a ∎
62
63 lift-conv-abs : ∀ a → lift (const Q) abs compat-abs a ≅ a
64 lift-conv-abs = lift-unique (λ _ → refl)
65
66 lift-fold : {B : Q → Set c} (f : ∀ q → B q) →
67 ∀ a → lift B (f ∘ abs) (cong f ∘ compat-abs) a ≅ f a
68 lift-fold f = lift-unique (λ _ → refl)
69
70 abs-epimorphism : {B : Q → Set c} {f g : ∀ q → B q} →
71 (∀ x → f (abs x) ≅ g (abs x)) → ∀ q → f q ≅ g q
72 abs-epimorphism {B} {f} {g} p q = begin
73 f q ≅⟨ sym (lift-fold f q) ⟩
74 lift B (f ∘ abs) (cong f ∘ compat-abs) q ≅⟨ lift-ext p q ⟩
75 lift B (g ∘ abs) (cong g ∘ compat-abs) q ≅⟨ lift-fold g q ⟩
76 g q ∎
77
78 ------------------------------------------------------------------------
79 -- Properties provable with extensionality
80
81 module _ (ext : ∀ {a b} {A : Set a} {B₁ B₂ : A → Set b} {f₁ : ∀ a → B₁ a}
82 {f₂ : ∀ a → B₂ a} → (∀ a → f₁ a ≅ f₂ a) → f₁ ≅ f₂) where
83
84 module Properties₂
85 {c ℓ} {S₁ S₂ : Setoid c ℓ} (Qu₁ : Quotient S₁) (Qu₂ : Quotient S₂)
86 where
87
88 module S₁ = Setoid S₁
89 module S₂ = Setoid S₂
90 module Qu₁ = Quotient Qu₁
91 module Qu₂ = Quotient Qu₂
92
93 module _ {B : _ → _ → Set c} (f : ∀ s₁ s₂ → B (Qu₁.abs s₁) (Qu₂.abs s₂)) where
94
95 compat₂ : Set _
96 compat₂ = ∀ {a b a′ b′} → a S₁.≈ a′ → b S₂.≈ b′ → f a b ≅ f a′ b′
97
98 lift₂ : compat₂ → ∀ q q′ → B q q′
99 lift₂ p = Qu₁.lift _ g (ext ∘ g-ext) module Lift₂ where
100
101 g : ∀ a q → B (Qu₁.abs a) q
102 g a = Qu₂.lift (B (Qu₁.abs a)) (f a) (p S₁.refl)
103
104 g-ext : ∀ {a a′} → a S₁.≈ a′ → ∀ q → g a q ≅ g a′ q
105 g-ext a≈a′ = Properties.lift-ext Qu₂ (λ _ → p a≈a′ S₂.refl)
106
107 lift₂-conv : (p : compat₂) → ∀ a a′ → lift₂ p (Qu₁.abs a) (Qu₂.abs a′) ≅ f a a′
108 lift₂-conv p a a′ = begin
109 lift₂ p (Qu₁.abs a) (Qu₂.abs a′)
110 ≅⟨ cong (_$ (Qu₂.abs a′)) (Qu₁.lift-conv (Lift₂.g p) (ext ∘ Lift₂.g-ext p) a) ⟩
111 Lift₂.g p a (Qu₂.abs a′)
112 ≡⟨⟩
113 Qu₂.lift (B (Qu₁.abs a)) (f a) (p S₁.refl) (Qu₂.abs a′)
114 ≅⟨ Qu₂.lift-conv (f a) (p S₁.refl) a′ ⟩
115 f a a′
116 ∎
117
118 module Properties₃ {c ℓ} {S₁ S₂ S₃ : Setoid c ℓ}
119 (Qu₁ : Quotient S₁) (Qu₂ : Quotient S₂) (Qu₃ : Quotient S₃)
120 where
121
122 module S₁ = Setoid S₁
123 module S₂ = Setoid S₂
124 module S₃ = Setoid S₃
125 module Qu₁ = Quotient Qu₁
126 module Qu₂ = Quotient Qu₂
127 module Qu₃ = Quotient Qu₃
128
129 module _ {B : _ → _ → _ → Set c}
130 (f : ∀ s₁ s₂ s₃ → B (Qu₁.abs s₁) (Qu₂.abs s₂) (Qu₃.abs s₃)) where
131
132 compat₃ : Set _
133 compat₃ = ∀ {a b c a′ b′ c′} → a S₁.≈ a′ → b S₂.≈ b′ → c S₃.≈ c′ →
134 f a b c ≅ f a′ b′ c′
135
136 lift₃ : compat₃ → ∀ q₁ q₂ q₃ → B q₁ q₂ q₃
137 lift₃ p = Qu₁.lift _ h (ext ∘ h-ext) module Lift₃ where
138
139 g : ∀ a b q → B (Qu₁.abs a) (Qu₂.abs b) q
140 g a b = Qu₃.lift (B (Qu₁.abs a) (Qu₂.abs b)) (f a b) (p S₁.refl S₂.refl)
141
142 g-ext : ∀ {a a′ b b′} → a S₁.≈ a′ → b S₂.≈ b′ → ∀ c → g a b c ≅ g a′ b′ c
143 g-ext a≈a′ b≈b′ = Properties.lift-ext Qu₃ (λ _ → p a≈a′ b≈b′ S₃.refl)
144
145 h : ∀ a q₂ q₃ → B (Qu₁.abs a) q₂ q₃
146 h a = Qu₂.lift (λ b → ∀ q → B (Qu₁.abs a) b q) (g a) (ext ∘ g-ext S₁.refl)
147
148 h-ext : ∀ {a a′} → a S₁.≈ a′ → ∀ b → h a b ≅ h a′ b
149 h-ext a≈a′ = Properties.lift-ext Qu₂ $ λ _ → ext (g-ext a≈a′ S₂.refl)