{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module ALMA.Base.Equivalence.DiscreteEq where
open import Agda.Primitive using (_⊔_; Level)
open import Agda.Builtin.Equality using (_≡_; refl)
open import Agda.Builtin.Sigma using (Σ; _,_)
open import Data.Product.Base using (_×_)
open import Relation.Binary.Structures using (IsEquivalence; IsPreorder)
DiscreteEq : {a d : Level} {X : Set a} → (layer : X → X → Set d)
→ X → X → Set d
DiscreteEq layer x y = layer x y
module RelationAlgebra
{a d : Level} {X : Set a} where
_⊆_ : {d₁ d₂ : Level}
→ (X → X → Set d₁) → (X → X → Set d₂) → Set (a ⊔ d₁ ⊔ d₂)
R ⊆ S = ∀ {x y} → R x y → S x y
Id : X → X → Set a
Id x y = x ≡ y
infixr 19 _∘_
_∘_ : {d₁ d₂ : Level}
→ (X → X → Set d₂) → (X → X → Set d₁) → X → X → Set (a ⊔ d₁ ⊔ d₂)
(R ∘ S) x z = Σ X (λ y → S x y × R y z)
infix 25 _⁻¹
_⁻¹ : {d₁ : Level} → (X → X → Set d₁) → X → X → Set d₁
R ⁻¹ = λ x y → R y x
∘-assoc : (R S T : X → X → Set d)
→ ((R ∘ S) ∘ T) ⊆ (R ∘ (S ∘ T))
∘-assoc R S T (y , Txy , (z , Syz , Rzw)) = z , ((y , Txy , Syz) , Rzw)
∘-assoc' : (R S T : X → X → Set d)
→ (R ∘ (S ∘ T)) ⊆ ((R ∘ S) ∘ T)
∘-assoc' R S T (z , ((y , Txy , Syz) , Rzw)) = y , Txy , (z , Syz , Rzw)
Id-left : (R : X → X → Set d) → (Id ∘ R) ⊆ R
Id-left R (y , Rxy , refl) = Rxy
Id-right : (R : X → X → Set d) → (R ∘ Id) ⊆ R
Id-right R (y , refl , Ryz) = Ryz
⁻¹-invol : (R : X → X → Set d) → ((R ⁻¹) ⁻¹) ⊆ R
⁻¹-invol R p = p
⁻¹-invol' : (R : X → X → Set d) → R ⊆ ((R ⁻¹) ⁻¹)
⁻¹-invol' R p = p
⁻¹-∘ : (R S : X → X → Set d)
→ ((R ∘ S) ⁻¹) ⊆ (S ⁻¹ ∘ R ⁻¹)
⁻¹-∘ R S (y , Swy , Ryx) = y , Ryx , Swy
⁻¹-∘' : (R S : X → X → Set d)
→ (S ⁻¹ ∘ R ⁻¹) ⊆ ((R ∘ S) ⁻¹)
⁻¹-∘' R S (y , Ryx , Swy) = y , Swy , Ryx
Symmetric : (R : X → X → Set d) → Set (a ⊔ d)
Symmetric R = R ⊆ R ⁻¹
Transitive : (R : X → X → Set d) → Set (a ⊔ d)
Transitive R = (R ∘ R) ⊆ R
Reflexive : (R : X → X → Set d) → Set (a ⊔ d)
Reflexive R = Id ⊆ R
reflexive-symmetric-transitive→equivalence
: (R : X → X → Set d)
→ Reflexive R → Symmetric R → Transitive R
→ IsEquivalence R
reflexive-symmetric-transitive→equivalence R refl-R sym-R trans-R = record
{ refl = λ {x} → refl-R refl
; sym = λ {x} {y} → sym-R
; trans = λ {x} {y} {z} p q → trans-R (y , p , q)
}
module GaloisConnection
{a b d e : Level} {X : Set a} {Y : Set b}
(_≤_ : X → X → Set d) (_⊑_ : Y → Y → Set e) where
record Galois (f : X → Y) (g : Y → X) : Set (a ⊔ b ⊔ d ⊔ e) where
field
adj : ∀ {x y} → f x ⊑ y → x ≤ g y
coadj : ∀ {x y} → x ≤ g y → f x ⊑ y
open Galois public
module ClosureProperties
(≤-refl : ∀ x → x ≤ x)
(≤-trans : ∀ {x y z} → x ≤ y → y ≤ z → x ≤ z)
(⊑-refl : ∀ y → y ⊑ y)
(⊑-trans : ∀ {x y z} → x ⊑ y → y ⊑ z → x ⊑ z)
(f : X → Y) (g : Y → X)
(gal : Galois f g) where
g-mono : ∀ {y y'} → y ⊑ y' → g y ≤ g y'
g-mono {y} {y'} y⊑y' =
Galois.adj gal
(⊑-trans (Galois.coadj gal (≤-refl (g y))) y⊑y')
f-mono : ∀ {x x'} → x ≤ x' → f x ⊑ f x'
f-mono {x} {x'} x≤x' =
Galois.coadj gal
(≤-trans x≤x' (Galois.adj gal (⊑-refl (f x'))))
c : X → X
c = λ x → g (f x)
c-extensive : ∀ x → x ≤ c x
c-extensive x = Galois.adj gal (⊑-refl (f x))
c-idempotent : ∀ x → c (c x) ≤ c x
c-idempotent x = g-mono (Galois.coadj gal (≤-refl (c x)))
c-mono : ∀ {x x'} → x ≤ x' → c x ≤ c x'
c-mono {x} {x'} x≤x' = g-mono (f-mono x≤x')
module Symmetrisation
{a d : Level} {X : Set a}
(_≤_ : X → X → Set d) where
Sym : X → X → Set d
Sym x y = (x ≤ y) × (y ≤ x)
sym-refl : (∀ x → x ≤ x) → ∀ x → Sym x x
sym-refl refl-≤ x = refl-≤ x , refl-≤ x
sym-sym : ∀ {x y} → Sym x y → Sym y x
sym-sym (p , q) = q , p
sym-trans : (∀ {x y z} → x ≤ y → y ≤ z → x ≤ z)
→ ∀ {x y z} → Sym x y → Sym y z → Sym x z
sym-trans trans-≤ (p₁ , p₂) (q₁ , q₂) =
trans-≤ p₁ q₁ , trans-≤ q₂ p₂
sym-isEquivalence : IsPreorder _≡_ _≤_ → IsEquivalence Sym
sym-isEquivalence pre = record
{ refl = λ {x} → sym-refl (λ y → IsPreorder.refl pre) x
; sym = sym-sym
; trans = sym-trans (IsPreorder.trans pre)
}
sym-universal
: (R : X → X → Set d)
→ (∀ {x y} → R x y → R y x)
→ (∀ {x y} → R x y → x ≤ y)
→ ∀ {x y} → R x y → Sym x y
sym-universal R sym-R R⊆≤ p = R⊆≤ p , R⊆≤ (sym-R p)
module Fixpoint
{a d : Level} {X : Set a}
(_≤_ : X → X → Set d)
(c : X → X) where
record IsClosureOperator : Set (a ⊔ d) where
field
extensive : ∀ x → x ≤ c x
monotone : ∀ {x y} → x ≤ y → c x ≤ c y
idempotent : ∀ x → c (c x) ≤ c x
open IsClosureOperator public
IsFixed : X → Set d
IsFixed x = c x ≤ x
fixed-stable : IsClosureOperator → ∀ x → IsFixed (c x)
fixed-stable co x = IsClosureOperator.idempotent co x
fixed-closed
: IsClosureOperator → ∀ x
→ IsFixed x → Symmetrisation.Sym _≤_ (c x) x
fixed-closed co x fixed = fixed , IsClosureOperator.extensive co x
FixedRel : X → X → Set d
FixedRel x y = IsFixed x × IsFixed y
module GaloisFixpointBridge
{a b d e : Level} {X : Set a} {Y : Set b}
(_≤_ : X → X → Set d) (_⊑_ : Y → Y → Set e)
(≤-refl : ∀ x → x ≤ x)
(≤-trans : ∀ {x y z} → x ≤ y → y ≤ z → x ≤ z)
(⊑-refl : ∀ y → y ⊑ y)
(⊑-trans : ∀ {x y z} → x ⊑ y → y ⊑ z → x ⊑ z)
(f : X → Y) (g : Y → X)
(gal : GaloisConnection.Galois _≤_ _⊑_ f g) where
open GaloisConnection _≤_ _⊑_
module CP = ClosureProperties ≤-refl ≤-trans ⊑-refl ⊑-trans f g gal
module FP = Fixpoint _≤_ CP.c
galois-c-is-closure : FP.IsClosureOperator
galois-c-is-closure = record
{ extensive = CP.c-extensive
; monotone = CP.c-mono
; idempotent = CP.c-idempotent
}