-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnivAlgebra.agda
161 lines (125 loc) · 6.48 KB
/
UnivAlgebra.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{- Basic definitions of Heterogeneous Universal Algebra:
Signature, Algebra, Homomorphism, Congruence, Quotient, Subalgebra. -}
module UnivAlgebra where
open import Relation.Binary hiding (Total)
open import Level renaming (suc to lsuc ; zero to lzero)
open import Data.Nat renaming (_⊔_ to _⊔ₙ_)
open import Data.Product renaming (map to pmap)
open import Function as F
open import Function.Equality as FE renaming (_∘_ to _∘ₛ_) hiding (setoid)
open import Data.Bool renaming (_≟_ to _≟b_)
open import Data.List renaming (map to lmap)
open import Relation.Binary.PropositionalEquality as PE hiding ( Reveal_·_is_;[_];isEquivalence)
open import Relation.Unary hiding (_⊆_;_⇒_)
open import Data.Fin hiding (_+_)
import Relation.Binary.EqReasoning as EqR
open import HeterogeneousVec
pattern _↦_ ar s = (ar , s)
open Setoid
open import Setoids
{- Multisort Signature -}
record Signature : Set₁ where
field
sorts : Set
ops : (List sorts) × sorts → Set
Arity : Set
Arity = List sorts
Type : Set
Type = List sorts × sorts
open Signature
{- Algebra -}
record Algebra {ℓ₁ ℓ₂ : Level} (Σ : Signature) : Set (lsuc (ℓ₁ ⊔ ℓ₂)) where
constructor _∥_
field
_⟦_⟧ₛ : sorts Σ → Setoid ℓ₁ ℓ₂
_⟦_⟧ₒ : ∀ {ar s} → ops Σ (ar , s) →
_⟦_⟧ₛ ✳ ar ⟶ _⟦_⟧ₛ s
_⟦_⟧ₛ* : (ar : Arity Σ) → Set _
_⟦_⟧ₛ* ar = ∥ _⟦_⟧ₛ ✳ ar ∥
open Algebra
open import Relation.Binary.Product.Pointwise using (_×-setoid_)
{- Subalgebras -}
open SetoidPredicate
OpClosed : ∀ {ℓ₁ ℓ₂ ℓ₃ Σ} → (A : Algebra {ℓ₁} {ℓ₂} Σ) →
(P : (s : sorts Σ) → Pred (∥ A ⟦ s ⟧ₛ ∥) ℓ₃) → Set _
OpClosed {ℓ₃ = ℓ₃} {Σ = Σ} A P = ∀ {ar s} (f : ops Σ (ar , s)) →
(P ⇨v ⟨→⟩ P s) (A ⟦ f ⟧ₒ ⟨$⟩_)
-- Subalgebra condition: A subsetoid closed by operations.
record SubAlg {ℓ₃ ℓ₁ ℓ₂} {Σ} (A : Algebra {ℓ₁} {ℓ₂} Σ) :
Set (lsuc (ℓ₁ ⊔ ℓ₂ ⊔ ℓ₃)) where
field
pr : (s : sorts Σ) → SetoidPredicate {ℓ₃ = ℓ₃} (A ⟦ s ⟧ₛ)
opClosed : OpClosed {Σ = Σ} A (λ s x → predicate (pr s) x)
pcong : ∀ {ar} {v₁ v₂ : HVec (λ s → Carrier $ SubSetoid (A ⟦ s ⟧ₛ)
(predicate (pr s))) ar} →
_∼v_ {l₁ = ℓ₂} {R = λ { s (a₁ , _) (a₂ , _) →
Setoid._≈_ (A ⟦ s ⟧ₛ) a₁ a₂ } } v₁ v₂ →
_∼v_ {l₁ = ℓ₂} {R = λ s → Setoid._≈_ (A ⟦ s ⟧ₛ)}
(map (λ _ → proj₁) v₁ )
(map (λ _ → proj₁) v₂)
pcong {[]} {⟨⟩} ∼⟨⟩ = ∼⟨⟩
pcong {i ∷ is} (∼▹ x eq) = ∼▹ x (pcong eq)
-- A subsetoid closed by operations is an Algebra.
SubAlgebra : ∀ {Σ} {ℓ₁ ℓ₂ ℓ₃} {A : Algebra {ℓ₁} {ℓ₂} Σ} →
SubAlg {ℓ₃ = ℓ₃} A → Algebra Σ
SubAlgebra {Σ} {A = A} S = is ∥ if
where
open SubAlg S
is : sorts Σ → _
is s = SubSetoid (A ⟦ s ⟧ₛ) (predicate (pr s))
if : ∀ {ar} {s} → (f : ops Σ (ar , s)) → is ✳ ar ⟶ is s
if {ar} {s} f = record { _⟨$⟩_ = λ v → (A ⟦ f ⟧ₒ ⟨$⟩ map (λ _ → proj₁) v)
, opClosed f (⇨₂ v)
; cong = λ { {v₁} {v₂} eq → Π.cong (A ⟦ f ⟧ₒ) (pcong eq) }
}
{- Product algebra -}
module ProdAlg {ℓ₁ ℓ₂ ℓ₃ ℓ₄}
{Σ : Signature}
(A : Algebra {ℓ₁} {ℓ₂} Σ)
(B : Algebra {ℓ₃} {ℓ₄} Σ) where
std : (s : sorts Σ) → Setoid _ _
std s = (A ⟦ s ⟧ₛ) ×-setoid (B ⟦ s ⟧ₛ)
_≈*_ : {ar : Arity Σ} → _
_≈*_ {ar} = _≈_ (std ✳ ar)
≈₁ : ∀ {ar} {vs vs' : ∥ std ✳ ar ∥}
→ vs ≈* vs' → _≈_ (_⟦_⟧ₛ A ✳ ar) (map (λ _ → proj₁) vs) (map (λ _ → proj₁) vs')
≈₁ {[]} ∼⟨⟩ = ∼⟨⟩
≈₁ {i ∷ is} (∼▹ (eq , _) equ) = ∼▹ eq (≈₁ equ)
≈₂ : ∀ {ar} {vs vs' : ∥ std ✳ ar ∥}
→ vs ≈* vs' → _≈_ (_⟦_⟧ₛ B ✳ ar) (map (λ _ → proj₂) vs) (map (λ _ → proj₂) vs')
≈₂ {[]} ∼⟨⟩ = ∼⟨⟩
≈₂ {i ∷ is} (∼▹ (_ , eq) equ) = ∼▹ eq (≈₂ equ)
{- Product of algebras -}
_×-alg_ : Algebra {ℓ₃ ⊔ ℓ₁} {ℓ₄ ⊔ ℓ₂} Σ
_×-alg_ = record {
_⟦_⟧ₛ = λ s → (A ⟦ s ⟧ₛ) ×-setoid (B ⟦ s ⟧ₛ)
; _⟦_⟧ₒ = λ {ar} {s} f → record { _⟨$⟩_ = if f ; cong = cng f}
}
where if : ∀ {ar s} (f : ops Σ (ar , s)) → _ → _
if {ar} {s} f vs = A ⟦ f ⟧ₒ ⟨$⟩ map (λ _ → proj₁) vs
, B ⟦ f ⟧ₒ ⟨$⟩ map (λ _ → proj₂) vs
cng : ∀ {ar s} (f : ops Σ (ar , s)) → {vs vs' : ∥ std ✳ ar ∥}
→ vs ≈* vs' → _≈_ (std s) (if f vs) (if f vs')
cng {ar} f equ = (Π.cong (_⟦_⟧ₒ A f) (≈₁ equ)) ,
((Π.cong (_⟦_⟧ₒ B f) (≈₂ equ)))
{- Congruence -}
record Congruence {ℓ₃ ℓ₁ ℓ₂} {Σ : Signature}
(A : Algebra {ℓ₁} {ℓ₂} Σ) : Set (lsuc ℓ₃ ⊔ ℓ₂ ⊔ ℓ₁) where
field
rel : (s : sorts Σ) → Rel (Carrier (A ⟦ s ⟧ₛ)) ℓ₃
welldef : (s : sorts Σ) → WellDefRel (A ⟦ s ⟧ₛ) (rel s)
cequiv : (s : sorts Σ) → IsEquivalence (rel s)
csubst : ∀ {ar s} (f : ops Σ (ar , s)) → rel * =[ A ⟦ f ⟧ₒ ⟨$⟩_ ]⇒ rel s
open Congruence
_⊆_ : ∀ {ℓ₃ ℓ₁ ℓ₂} {Σ : Signature} {A : Algebra {ℓ₁} {ℓ₂} Σ} →
Congruence {ℓ₃} A → Congruence {ℓ₃} A → Set _
Φ ⊆ Ψ = ∀ s → (rel Φ s) ⇒ (rel Ψ s)
{- Quotient Algebra -}
_/_ : ∀ {ℓ₁ ℓ₂ ℓ₃} {Σ} → (A : Algebra {ℓ₁} {ℓ₂} Σ) → (C : Congruence {ℓ₃} A) →
Algebra {ℓ₁} {ℓ₃} Σ
A / C = (λ s → record { Carrier = Carrier (A ⟦ s ⟧ₛ)
; _≈_ = rel C s
; isEquivalence = cequiv C s })
∥
(λ {ar} {s} f → record { _⟨$⟩_ = λ v → A ⟦ f ⟧ₒ ⟨$⟩ v
; cong = csubst C f } )