Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0adbbe8
introduced alternative definitions; revised one usage of previous one
jamesmckinna Apr 22, 2023
b62ef81
deprecated field name (NB tricky)
jamesmckinna Apr 22, 2023
8993f59
installed new definition
jamesmckinna Apr 22, 2023
7f09808
removed old definition; tidied up
jamesmckinna Apr 22, 2023
221ac92
`CHANGELOG` plus portable definition of the `proof` projection
jamesmckinna Apr 22, 2023
5935db4
`CHANGELOG`
jamesmckinna Apr 22, 2023
829ddd5
more explicit deprecation warning
jamesmckinna Apr 22, 2023
e9c7170
typo in `CHANGELOG`
jamesmckinna Apr 23, 2023
824dce6
exchnaged repeated recursive for a nested irrefutbale `with` pattern
jamesmckinna Apr 23, 2023
dbfb5a7
Merge branch 'issue1919' of https://github.com/jamesmckinna/agda-stdl…
jamesmckinna Apr 23, 2023
61ef3f0
Revert "Merge branch 'issue1919' of https://github.com/jamesmckinna/a…
jamesmckinna Apr 23, 2023
693270e
Revert "exchnaged repeated recursive for a nested irrefutbale `with` …
jamesmckinna Apr 23, 2023
f120c1e
tweaks
jamesmckinna Apr 25, 2023
887e652
The free `Magma` on a `Set`, resp. `Setoid`
jamesmckinna Apr 27, 2023
60d912b
proof of uniquness of the `eval` homomorphism
jamesmckinna Apr 27, 2023
c994ceb
functoriality of `map`
jamesmckinna Apr 28, 2023
8da3c1c
tidying up
jamesmckinna Apr 28, 2023
9458ae2
notational tidying up
jamesmckinna Apr 28, 2023
7d63a62
fix-whitespace
jamesmckinna Apr 28, 2023
4c4e152
tweak notation
jamesmckinna Apr 28, 2023
4492833
tweak notation a bit more; add comments
jamesmckinna Apr 28, 2023
86ba471
moved `Set` construction up
jamesmckinna Apr 28, 2023
5d9bb28
added `algᴹ-isMagmaHomomorphism`
jamesmckinna Apr 28, 2023
8dd84a1
Merge branch 'free-algebras' of https://github.com/jamesmckinna/agda-…
jamesmckinna Apr 28, 2023
b91ef55
radical refactoring; added `map` for lifting `FreeMonad` from `Setoid…
jamesmckinna Apr 29, 2023
0e04513
cosmetic
jamesmckinna Apr 29, 2023
3de4bd8
typo
jamesmckinna Apr 29, 2023
bfc920b
more typos
jamesmckinna Apr 29, 2023
1a8074d
corollary to uniqueness: naturality of `alg`
jamesmckinna Apr 30, 2023
eef3d3f
cosmetic renamings; more serious refactoring required
jamesmckinna Apr 30, 2023
d5f2a80
added `MagmaHomomorphism` bundle; another major refactoring
jamesmckinna May 1, 2023
0186232
added functoriality of `FreeMagmaFunctor`
jamesmckinna May 1, 2023
7a18f08
refactored functoriality of `FreeMagmaFunctor`
jamesmckinna May 1, 2023
6bbc2af
tidied up leftovers of functoriality of `FreeMagmaFunctor`
jamesmckinna May 1, 2023
8302fc6
Merge branch 'free-algebras' of https://github.com/jamesmckinna/agda-…
jamesmckinna May 1, 2023
90434b7
refactored dependencies; `Uniqueness` and its `Corollary` no longer d…
jamesmckinna May 1, 2023
0388629
reverted bogus `merge`; retained refactored dependencies
jamesmckinna May 1, 2023
bb3c155
Revert "tidied up leftovers of functoriality of `FreeMagmaFunctor`"
jamesmckinna May 1, 2023
6835c67
final reversion of commits
jamesmckinna May 1, 2023
e0a44dd
Revert "tweaks"
jamesmckinna May 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,12 @@ New modules
Algebra.Construct.Flip.Op
```

* Algebraic structures obtained as the free thing (for their signature):
```
Algebra.Bundles.Free
Algebra.Bundles.Free.Magma
```

* Morphisms between module-like algebraic structures:
```
Algebra.Module.Morphism.Construct.Composition
Expand Down
11 changes: 11 additions & 0 deletions src/Algebra/Bundles/Free.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Definitions of 'pre-free' and 'free' bundles
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

module Algebra.Bundles.Free where

open import Algebra.Bundles.Free.Magma public
262 changes: 262 additions & 0 deletions src/Algebra/Bundles/Free/Magma.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Definitions of 'pre-free' and 'free' magma
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

module Algebra.Bundles.Free.Magma where

open import Algebra.Core
import Algebra.Definitions as Defs using (Congruent₂)
import Algebra.Structures as Strs using (IsMagma)
open import Algebra.Morphism.Structures using (IsMagmaHomomorphism)
open import Algebra.Bundles using (Magma)
open import Algebra.Bundles.Raw using (RawMagma)
open import Function.Base using (id; _∘_)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.Morphism using (IsRelHomomorphism)
open import Level using (Level; _⊔_)
open import Relation.Nullary.Negation.Core using (¬_)
open import Relation.Binary
using (Setoid; IsEquivalence; Reflexive; Symmetric; Transitive)
open import Relation.Binary.PropositionalEquality
using (_≡_; cong₂) renaming (refl to ≡-refl; isEquivalence to ≡-isEquivalence)
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning


------------------------------------------------------------------------
-- 'pre'-free algebra

infixl 7 _∙_

data PreFreeMagma {a} (A : Set a) : Set a where

var : A PreFreeMagma A
_∙_ : Op₂ (PreFreeMagma A)

module _ {a b} {A : Set a} {B : Set b} where

map : (A B) PreFreeMagma A PreFreeMagma B
map f (var a) = var (f a)
map f (s ∙ t) = (map f s) ∙ (map f t)

module _ {a b c} {A : Set a} {B : Set b} {C : Set c} where

map-id : (t : PreFreeMagma A) map id t ≡ t
map-id (var a) = ≡-refl
map-id (s ∙ t) = cong₂ _∙_ (map-id s) (map-id t)

map-∘ : (g : A B) (f : B C) t map (f ∘ g) t ≡ (map f ∘ map g) t
map-∘ g f (var a) = ≡-refl
map-∘ g f (s ∙ t) = cong₂ _∙_ (map-∘ g f s) (map-∘ g f t)

------------------------------------------------------------------------
-- Functor, RawMonad instance: TODO

------------------------------------------------------------------------
-- parametrised 'equational' theory over the 'pre'-free algebra

module PreFreeTheory {c ℓ} {A : Set c} (R : Rel A ℓ) where

data PreFreeMagmaTheory : Rel (PreFreeMagma A) (c ⊔ ℓ)

open Defs PreFreeMagmaTheory

data PreFreeMagmaTheory where

var : {a b} (R a b) PreFreeMagmaTheory (var a) (var b)
_∙_ : Congruent₂ _∙_

PreFreeTheorySyntax : {c ℓ} {A : Set c} (R : Rel A ℓ) Rel (PreFreeMagma A) (c ⊔ ℓ)
PreFreeTheorySyntax R = PreFreeMagmaTheory where open PreFreeTheory R

syntax PreFreeTheorySyntax R m n = m ≈[ R ] n
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! (meant for several lines above)

Copy link
Collaborator Author

@jamesmckinna jamesmckinna Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But still, just as the functor should perhaps better be called Syntax, this should have a better name. But the mutual declare/import definitions/define trick is part of what makes Agda beautiful to work with



------------------------------------------------------------------------
-- Free algebra on a Set
{-
in the propositional case, we can immediately define the following
but how best to organise this under the Algebra.Bundles.Free hierarchy?
e.g. should we distinguish Free.Magma.Setoid from Free.Magma.Propositional?
-}

module FreeMagma {c} (A : Set c) where

private Carrier = PreFreeMagma A

_≈_ : Rel Carrier c
m ≈ n = m ≈[ _≡_ ] n

open PreFreeTheory {A = A} _≡_

≈⇒≡ : {m n} m ≈ n m ≡ n
≈⇒≡ (var ≡-refl) = ≡-refl
≈⇒≡ (eq₁ ∙ eq₂) = cong₂ _∙_ (≈⇒≡ eq₁) (≈⇒≡ eq₂)

refl : Reflexive _≈_
refl {var _} = var ≡-refl
refl {_ ∙ _} = refl ∙ refl

≡⇒≈ : {m n} m ≡ n m ≈ n
≡⇒≈ ≡-refl = refl

rawFreeMagma : RawMagma c c
rawFreeMagma = record { Carrier = Carrier ; _≈_ = _≡_ ; _∙_ = _∙_ }

open Strs {A = Carrier} _≡_

isMagma : IsMagma _∙_
isMagma = record { isEquivalence = ≡-isEquivalence ; ∙-cong = cong₂ _∙_ }

freeMagma : Magma c c
freeMagma = record { RawMagma rawFreeMagma ; isMagma = isMagma }

------------------------------------------------------------------------
-- Extending to a Setoid

module PreservesEquivalence {c ℓ} {A : Set c} (R : Rel A ℓ) where

open PreFreeTheory R

_≈R_ = λ m n m ≈[ R ] n

refl : Reflexive R Reflexive _≈R_
refl r {var _} = var r
refl r {_ ∙ _} = (refl r) ∙ (refl r)

sym : Symmetric R Symmetric _≈R_
sym s (var r) = var (s r)
sym s (r₁ ∙ r₂) = sym s r₁ ∙ sym s r₂

trans : Transitive R Transitive _≈R_
trans t (var r) (var s) = var (t r s)
trans t (r₁ ∙ r₂) (s₁ ∙ s₂) = trans t r₁ s₁ ∙ trans t r₂ s₂

preservesEquivalence : IsEquivalence R IsEquivalence _≈R_
preservesEquivalence isEq = record { refl = refl r ; sym = sym s ; trans = trans t }
where open IsEquivalence isEq renaming (refl to r; sym to s; trans to t)

------------------------------------------------------------------------
-- Free algebra on a Setoid

module FreeMagmaOn {c ℓ} (S : Setoid c ℓ) where

open Setoid S renaming (Carrier to A; isEquivalence to isEq)

open PreFreeTheory _≈_ public

open PreservesEquivalence _≈_

open Strs _≈R_

isMagma : IsMagma _∙_
isMagma = record { isEquivalence = preservesEquivalence isEq ; ∙-cong = _∙_ }

freeMagma : Magma c (c ⊔ ℓ)
freeMagma = record { Carrier = PreFreeMagma A; _≈_ = _≈R_ ; _∙_ = _∙_ ; isMagma = isMagma }

------------------------------------------------------------------------
-- Eval, as the unique fold ⟦_⟧_ over PreFreeMagma A

module Eval {a ℓa m ℓm} (𝓐 : Setoid a ℓa) (𝓜 : Magma m ℓm) where

open Setoid 𝓐 renaming (Carrier to A)

open Magma 𝓜 renaming (Carrier to M; _∙_ to _∙ᴹ_)

⟦_⟧_ : PreFreeMagma A (A M) M
⟦ var a ⟧ η = η a
⟦ s ∙ t ⟧ η = ⟦ s ⟧ η ∙ᴹ ⟦ t ⟧ η

------------------------------------------------------------------------
-- Any Magma *is* an algebra for the PreFreeMagma Functor

module Alg {m ℓm} (𝓜 : Magma m ℓm) where

open Magma 𝓜 renaming (setoid to setoidᴹ; Carrier to M)

open Eval setoidᴹ 𝓜

algᴹ : PreFreeMagma M M
algᴹ t = ⟦ t ⟧ id

------------------------------------------------------------------------
-- Properties of ⟦_⟧_

module Properties {a ℓa m ℓm} (𝓐 : Setoid a ℓa) (𝓜 : Magma m ℓm) where

open Setoid 𝓐 renaming (Carrier to A; _≈_ to _≈ᴬ_)

open Magma 𝓜
renaming (Carrier to M; _≈_ to _≈ᴹ_; _∙_ to _∙ᴹ_
; setoid to setoidᴹ; rawMagma to rawMagmaᴹ; refl to reflᴹ
; isMagma to isMagmaᴹ)

open Eval 𝓐 𝓜

open Alg 𝓜

open FreeMagmaOn 𝓐

open Magma freeMagma renaming (rawMagma to rawMagmaᴬ; Carrier to FA)

module _: A M} (hom-η : IsRelHomomorphism _≈ᴬ_ _≈ᴹ_ η) where

⟦_⟧ᴹ : FA M
⟦_⟧ᴹ = ⟦_⟧ η

open Strs _≈ᴹ_
open IsMagma isMagmaᴹ renaming (∙-cong to congᴹ)
open IsRelHomomorphism hom-η renaming (cong to cong-η)

cong : {s t} s ≈ t ⟦ s ⟧ᴹ ≈ᴹ ⟦ t ⟧ᴹ
cong (var r) = cong-η r
cong (s ∙ t) = congᴹ (cong s) (cong t)

isRelHomomorphism : IsRelHomomorphism _≈_ _≈ᴹ_ ⟦_⟧ᴹ
isRelHomomorphism = record { cong = cong }

isMagmaHomomorphism : IsMagmaHomomorphism rawMagmaᴬ rawMagmaᴹ ⟦_⟧ᴹ
isMagmaHomomorphism = record { isRelHomomorphism = isRelHomomorphism
; homo = λ _ _ reflᴹ
}

unfold-⟦_⟧ᴹ : t ⟦ t ⟧ᴹ ≈ᴹ algᴹ (map η t)
unfold-⟦ var a ⟧ᴹ = reflᴹ
unfold-⟦ s ∙ t ⟧ᴹ = congᴹ unfold-⟦ s ⟧ᴹ unfold-⟦ t ⟧ᴹ

module _ {h : FA M} (isHom : IsMagmaHomomorphism rawMagmaᴬ rawMagmaᴹ h)
(h∘var≈ᴹη : a h (var a) ≈ᴹ η a) where

open IsMagmaHomomorphism isHom

open ≈-Reasoning setoidᴹ

isUnique⟦_⟧ᴹ : t h t ≈ᴹ ⟦ t ⟧ᴹ
isUnique⟦ var a ⟧ᴹ = h∘var≈ᴹη a
isUnique⟦ s ∙ t ⟧ᴹ = begin
h (s PreFreeMagma.∙ t) ≈⟨ homo s t ⟩
(h s) ∙ᴹ (h t) ≈⟨ congᴹ isUnique⟦ s ⟧ᴹ isUnique⟦ t ⟧ᴹ ⟩
⟦ s ⟧ᴹ ∙ᴹ (⟦ t ⟧ᴹ) ∎

-- immediate corollary

module _ {m ℓm} (𝓜 : Magma m ℓm) where

open Magma 𝓜 renaming (setoid to setoidᴹ; rawMagma to rawMagmaᴹ)

open Alg 𝓜

open Magma (FreeMagmaOn.freeMagma setoidᴹ)

open Properties setoidᴹ 𝓜

algᴹ-isMagmaHomomorphism : IsMagmaHomomorphism rawMagma rawMagmaᴹ algᴹ
algᴹ-isMagmaHomomorphism = isMagmaHomomorphism (record { cong = id })

------------------------------------------------------------------------
-- Monad instance: TODO