| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Choosing between elements based on the result of applying a function |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Algebra |
| 10 |
|
| 11 |
module Algebra.Construct.LiftedChoice where |
| 12 |
|
| 13 |
open import Algebra.Consequences.Base using (sel⇒idem) |
| 14 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]; [_,_]′) |
| 15 |
open import Data.Product.Base using (_×_; _,_) |
| 16 |
open import Function.Base using (const; _$_) |
| 17 |
open import Level using (Level; _⊔_) |
| 18 |
open import Relation.Binary.Core using (Rel; _⇒_; _Preserves_⟶_) |
| 19 |
open import Relation.Nullary using (¬_; yes; no) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 21 |
open import Relation.Binary.Structures using (IsEquivalence) |
| 22 |
open import Relation.Unary using (Pred) |
| 23 |
|
| 24 |
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning |
| 25 |
|
| 26 |
private |
| 27 |
variable |
| 28 |
a b p ℓ : Level |
| 29 |
A : Set a |
| 30 |
B : Set b |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Definition |
| 34 |
|
| 35 |
module _ (_≈_ : Rel B ℓ) (_•_ : Op₂ B) where |
| 36 |
|
| 37 |
Lift : Selective _≈_ _•_ → (A → B) → Op₂ A |
| 38 |
Lift ∙-sel f x y = [ const x , const y ]′ $ ∙-sel (f x) (f y) |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Algebraic properties |
| 42 |
|
| 43 |
module _ {_≈_ : Rel B ℓ} {_∙_ : Op₂ B} |
| 44 |
(∙-isSelectiveMagma : IsSelectiveMagma _≈_ _∙_) where |
| 45 |
|
| 46 |
private module M = IsSelectiveMagma ∙-isSelectiveMagma |
| 47 |
open M hiding (sel; isMagma) |
| 48 |
open ≈-Reasoning setoid |
| 49 |
|
| 50 |
module _ (f : A → B) where |
| 51 |
|
| 52 |
private |
| 53 |
_◦_ = Lift _≈_ _∙_ M.sel f |
| 54 |
|
| 55 |
sel-≡ : Selective _≡_ _◦_ |
| 56 |
sel-≡ x y with M.sel (f x) (f y) |
| 57 |
... | inj₁ _ = inj₁ ≡.refl |
| 58 |
... | inj₂ _ = inj₂ ≡.refl |
| 59 |
|
| 60 |
distrib : ∀ x y → ((f x) ∙ (f y)) ≈ f (x ◦ y) |
| 61 |
distrib x y with M.sel (f x) (f y) |
| 62 |
... | inj₁ fx∙fy≈fx = fx∙fy≈fx |
| 63 |
... | inj₂ fx∙fy≈fy = fx∙fy≈fy |
| 64 |
|
| 65 |
module _ (f : A → B) {_≈′_ : Rel A ℓ} |
| 66 |
(≈-reflexive : _≡_ ⇒ _≈′_) where |
| 67 |
|
| 68 |
private |
| 69 |
_◦_ = Lift _≈_ _∙_ M.sel f |
| 70 |
|
| 71 |
sel : Selective _≈′_ _◦_ |
| 72 |
sel x y = Sum.map ≈-reflexive ≈-reflexive (sel-≡ f x y) |
| 73 |
|
| 74 |
idem : Idempotent _≈′_ _◦_ |
| 75 |
idem = sel⇒idem _≈′_ sel |
| 76 |
|
| 77 |
module _ {f : A → B} {_≈′_ : Rel A ℓ} |
| 78 |
(f-injective : ∀ {x y} → f x ≈ f y → x ≈′ y) |
| 79 |
where |
| 80 |
|
| 81 |
private |
| 82 |
_◦_ = Lift _≈_ _∙_ M.sel f |
| 83 |
|
| 84 |
cong : f Preserves _≈′_ ⟶ _≈_ → Congruent₂ _≈′_ _◦_ |
| 85 |
cong f-cong {x} {y} {u} {v} x≈y u≈v |
| 86 |
with M.sel (f x) (f u) | M.sel (f y) (f v) |
| 87 |
... | inj₁ fx∙fu≈fx | inj₁ fy∙fv≈fy = x≈y |
| 88 |
... | inj₂ fx∙fu≈fu | inj₂ fy∙fv≈fv = u≈v |
| 89 |
... | inj₁ fx∙fu≈fx | inj₂ fy∙fv≈fv = f-injective (begin |
| 90 |
f x ≈⟨ sym fx∙fu≈fx ⟩ |
| 91 |
f x ∙ f u ≈⟨ ∙-cong (f-cong x≈y) (f-cong u≈v) ⟩ |
| 92 |
f y ∙ f v ≈⟨ fy∙fv≈fv ⟩ |
| 93 |
f v ∎) |
| 94 |
... | inj₂ fx∙fu≈fu | inj₁ fy∙fv≈fy = f-injective (begin |
| 95 |
f u ≈⟨ sym fx∙fu≈fu ⟩ |
| 96 |
f x ∙ f u ≈⟨ ∙-cong (f-cong x≈y) (f-cong u≈v) ⟩ |
| 97 |
f y ∙ f v ≈⟨ fy∙fv≈fy ⟩ |
| 98 |
f y ∎) |
| 99 |
|
| 100 |
assoc : Associative _≈_ _∙_ → Associative _≈′_ _◦_ |
| 101 |
assoc ∙-assoc x y z = f-injective (begin |
| 102 |
f ((x ◦ y) ◦ z) ≈⟨ distrib f (x ◦ y) z ⟨ |
| 103 |
f (x ◦ y) ∙ f z ≈⟨ ∙-congʳ (distrib f x y) ⟨ |
| 104 |
(f x ∙ f y) ∙ f z ≈⟨ ∙-assoc (f x) (f y) (f z) ⟩ |
| 105 |
f x ∙ (f y ∙ f z) ≈⟨ ∙-congˡ (distrib f y z) ⟩ |
| 106 |
f x ∙ f (y ◦ z) ≈⟨ distrib f x (y ◦ z) ⟩ |
| 107 |
f (x ◦ (y ◦ z)) ∎) |
| 108 |
|
| 109 |
comm : Commutative _≈_ _∙_ → Commutative _≈′_ _◦_ |
| 110 |
comm ∙-comm x y = f-injective (begin |
| 111 |
f (x ◦ y) ≈⟨ distrib f x y ⟨ |
| 112 |
f x ∙ f y ≈⟨ ∙-comm (f x) (f y) ⟩ |
| 113 |
f y ∙ f x ≈⟨ distrib f y x ⟩ |
| 114 |
f (y ◦ x) ∎) |
| 115 |
|
| 116 |
------------------------------------------------------------------------ |
| 117 |
-- Algebraic structures |
| 118 |
|
| 119 |
module _ {_≈′_ : Rel A ℓ} {f : A → B} |
| 120 |
(f-injective : ∀ {x y} → f x ≈ f y → x ≈′ y) |
| 121 |
(f-cong : f Preserves _≈′_ ⟶ _≈_) |
| 122 |
(≈′-isEquivalence : IsEquivalence _≈′_) |
| 123 |
where |
| 124 |
|
| 125 |
private |
| 126 |
module E = IsEquivalence ≈′-isEquivalence |
| 127 |
_◦_ = Lift _≈_ _∙_ M.sel f |
| 128 |
|
| 129 |
isMagma : IsMagma _≈′_ _◦_ |
| 130 |
isMagma = record |
| 131 |
{ isEquivalence = ≈′-isEquivalence |
| 132 |
; ∙-cong = cong (λ {x} → f-injective {x}) f-cong |
| 133 |
} |
| 134 |
|
| 135 |
isSemigroup : Associative _≈_ _∙_ → IsSemigroup _≈′_ _◦_ |
| 136 |
isSemigroup ∙-assoc = record |
| 137 |
{ isMagma = isMagma |
| 138 |
; assoc = assoc (λ {x} → f-injective {x}) ∙-assoc |
| 139 |
} |
| 140 |
|
| 141 |
isBand : Associative _≈_ _∙_ → IsBand _≈′_ _◦_ |
| 142 |
isBand ∙-assoc = record |
| 143 |
{ isSemigroup = isSemigroup ∙-assoc |
| 144 |
; idem = idem f E.reflexive |
| 145 |
} |
| 146 |
|
| 147 |
isSelectiveMagma : IsSelectiveMagma _≈′_ _◦_ |
| 148 |
isSelectiveMagma = record |
| 149 |
{ isMagma = isMagma |
| 150 |
; sel = sel f E.reflexive |
| 151 |
} |
| 152 |
|
| 153 |
------------------------------------------------------------------------ |
| 154 |
-- Other properties |
| 155 |
|
| 156 |
module _ {P : Pred A p} (f : A → B) where |
| 157 |
|
| 158 |
private |
| 159 |
_◦_ = Lift _≈_ _∙_ M.sel f |
| 160 |
|
| 161 |
preservesᵒ : (∀ {x y} → P x → (f x ∙ f y) ≈ f y → P y) → |
| 162 |
(∀ {x y} → P y → (f x ∙ f y) ≈ f x → P x) → |
| 163 |
∀ x y → P x ⊎ P y → P (x ◦ y) |
| 164 |
preservesᵒ left right x y (inj₁ px) with M.sel (f x) (f y) |
| 165 |
... | inj₁ _ = px |
| 166 |
... | inj₂ fx∙fy≈fx = left px fx∙fy≈fx |
| 167 |
preservesᵒ left right x y (inj₂ py) with M.sel (f x) (f y) |
| 168 |
... | inj₁ fx∙fy≈fy = right py fx∙fy≈fy |
| 169 |
... | inj₂ _ = py |
| 170 |
|
| 171 |
preservesʳ : (∀ {x y} → P y → (f x ∙ f y) ≈ f x → P x) → |
| 172 |
∀ x {y} → P y → P (x ◦ y) |
| 173 |
preservesʳ right x {y} Py with M.sel (f x) (f y) |
| 174 |
... | inj₁ fx∙fy≈fx = right Py fx∙fy≈fx |
| 175 |
... | inj₂ fx∙fy≈fy = Py |
| 176 |
|
| 177 |
preservesᵇ : ∀ {x y} → P x → P y → P (x ◦ y) |
| 178 |
preservesᵇ {x} {y} Px Py with M.sel (f x) (f y) |
| 179 |
... | inj₁ _ = Px |
| 180 |
... | inj₂ _ = Py |
| 181 |
|
| 182 |
forcesᵇ : (∀ {x y} → P x → (f x ∙ f y) ≈ f x → P y) → |
| 183 |
(∀ {x y} → P y → (f x ∙ f y) ≈ f y → P x) → |
| 184 |
∀ x y → P (x ◦ y) → P x × P y |
| 185 |
forcesᵇ presˡ presʳ x y P[x∙y] with M.sel (f x) (f y) |
| 186 |
... | inj₁ fx∙fy≈fx = P[x∙y] , presˡ P[x∙y] fx∙fy≈fx |
| 187 |
... | inj₂ fx∙fy≈fy = presʳ P[x∙y] fx∙fy≈fy , P[x∙y] |