| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An explanation about how mathematical hierarchies are laid out. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --allow-unsolved-metas #-} |
| 8 |
|
| 9 |
module README.Design.Hierarchies where |
| 10 |
|
| 11 |
open import Data.Sum.Base using (_⊎_) |
| 12 |
open import Level using (Level; _⊔_; suc) |
| 13 |
open import Relation.Binary.Core using (_Preserves₂_⟶_⟶_) |
| 14 |
|
| 15 |
private |
| 16 |
variable |
| 17 |
a b ℓ : Level |
| 18 |
A : Set a |
| 19 |
|
| 20 |
------------------------------------------------------------------------ |
| 21 |
-- Introduction |
| 22 |
------------------------------------------------------------------------ |
| 23 |
|
| 24 |
-- One of the key design decisions facing the library is how to handle |
| 25 |
-- mathematical hierarchies, e.g. |
| 26 |
-- ∙ Binary relations: preorder → partial order → total order |
| 27 |
-- ↘ equivalence |
| 28 |
-- ∙ Algebraic structures: magma → semigroup → monoid → group |
| 29 |
-- ↘ band → semilattice |
| 30 |
-- |
| 31 |
-- Some of the hierarchies in the library are: |
| 32 |
-- ∙ Algebra |
| 33 |
-- ∙ Function |
| 34 |
-- ∙ Relation.Binary |
| 35 |
-- ∙ Relation.Binary.Indexed |
| 36 |
-- |
| 37 |
-- A given hierarchy `X` is always split into 4 separate folders: |
| 38 |
-- ∙ X.Core |
| 39 |
-- ∙ X.Definitions |
| 40 |
-- ∙ X.Structures |
| 41 |
-- ∙ X.Bundles |
| 42 |
-- all four of which are publicly re-exported by `X` itself. |
| 43 |
-- |
| 44 |
-- Additionally a hierarchy `X` may contain additional files |
| 45 |
-- ∙ X.Bundles.Raw |
| 46 |
-- ∙ X.Consequences |
| 47 |
-- ∙ X.Constructs |
| 48 |
-- ∙ X.Properties |
| 49 |
-- ∙ X.Morphisms |
| 50 |
-- |
| 51 |
-- Descriptions of these modules are now described below using the |
| 52 |
-- running example of the `Relation.Binary` and `Algebra` hierarchies. |
| 53 |
|
| 54 |
-- Note that we redefine everything here for illustrative purposes, |
| 55 |
-- and that the definitions given below may be slightly simpler |
| 56 |
-- than the real definitions in order to focus on the points being |
| 57 |
-- discussed. |
| 58 |
|
| 59 |
|
| 60 |
------------------------------------------------------------------------ |
| 61 |
-- Main hierarchy modules |
| 62 |
------------------------------------------------------------------------ |
| 63 |
|
| 64 |
------------------------------------------------------------------------ |
| 65 |
-- X.Core |
| 66 |
|
| 67 |
-- The Core module contains the basic units of the hierarchy. |
| 68 |
|
| 69 |
-- For example, in the case of binary relations these are homogeneous and |
| 70 |
-- heterogeneous binary relations: |
| 71 |
|
| 72 |
REL : Set a → Set b → (ℓ : Level) → Set (a ⊔ b ⊔ suc ℓ) |
| 73 |
REL A B ℓ = A → B → Set ℓ |
| 74 |
|
| 75 |
Rel : Set a → (ℓ : Level) → Set (a ⊔ suc ℓ) |
| 76 |
Rel A ℓ = A → A → Set ℓ |
| 77 |
|
| 78 |
-- and in Algebra these are unary and binary operators, e.g. |
| 79 |
|
| 80 |
Op₁ : Set a → Set a |
| 81 |
Op₁ A = A → A |
| 82 |
|
| 83 |
Op₂ : Set a → Set a |
| 84 |
Op₂ A = A → A → A |
| 85 |
|
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- X.Definitions |
| 89 |
|
| 90 |
-- The Definitions module defines the various properties that the |
| 91 |
-- basic units of the hierarchy may have. |
| 92 |
|
| 93 |
-- Examples in Relation.Binary include reflexivity, transitivity, etc. |
| 94 |
|
| 95 |
Reflexive : Rel A ℓ → Set _ |
| 96 |
Reflexive _∼_ = ∀ {x} → x ∼ x |
| 97 |
|
| 98 |
Symmetric : Rel A ℓ → Set _ |
| 99 |
Symmetric _∼_ = ∀ {x y} → x ∼ y → y ∼ x |
| 100 |
|
| 101 |
Transitive : Rel A ℓ → Set _ |
| 102 |
Transitive _∼_ = ∀ {x y z} → x ∼ y → y ∼ z → x ∼ z |
| 103 |
|
| 104 |
Total : Rel A ℓ → Set _ |
| 105 |
Total _∼_ = ∀ x y → x ∼ y ⊎ y ∼ x |
| 106 |
|
| 107 |
-- Examples in Algebra include associativity, commutativity. |
| 108 |
-- Note that all definitions for Algebra are based on some notion of |
| 109 |
-- underlying equality. |
| 110 |
|
| 111 |
Associative : Rel A ℓ → Op₂ A → Set _ |
| 112 |
Associative _≈_ _∙_ = ∀ x y z → ((x ∙ y) ∙ z) ≈ (x ∙ (y ∙ z)) |
| 113 |
|
| 114 |
Commutative : Rel A ℓ → Op₂ A → Set _ |
| 115 |
Commutative _≈_ _∙_ = ∀ x y → (x ∙ y) ≈ (y ∙ x) |
| 116 |
|
| 117 |
LeftIdentity : Rel A ℓ → A → Op₂ A → Set _ |
| 118 |
LeftIdentity _≈_ e _∙_ = ∀ x → (e ∙ x) ≈ x |
| 119 |
|
| 120 |
RightIdentity : Rel A ℓ → A → Op₂ A → Set _ |
| 121 |
RightIdentity _≈_ e _∙_ = ∀ x → (x ∙ e) ≈ x |
| 122 |
|
| 123 |
-- Note that the types in `Definitions` modules are not meant to express |
| 124 |
-- the full concept on their own. For example the `Associative` type does |
| 125 |
-- not require the underlying relation to be an equivalence relation. |
| 126 |
-- Instead they are designed to aid modular reuse of the core concepts. |
| 127 |
-- The complete concepts are captured in various structures/bundles |
| 128 |
-- where the definitions are correctly used in context. |
| 129 |
|
| 130 |
|
| 131 |
------------------------------------------------------------------------ |
| 132 |
-- X.Structures |
| 133 |
|
| 134 |
-- When an abstract hierarchy of some sort (for instance semigroup → |
| 135 |
-- monoid → group) is included in the library, the basic approach is to |
| 136 |
-- specify the properties of every concept in terms of a record |
| 137 |
-- containing just properties, parameterised on the underlying |
| 138 |
-- sets, relations and operations. For example: |
| 139 |
|
| 140 |
record IsEquivalence {A : Set a} |
| 141 |
(_≈_ : Rel A ℓ) |
| 142 |
: Set (a ⊔ ℓ) |
| 143 |
where |
| 144 |
field |
| 145 |
refl : Reflexive _≈_ |
| 146 |
sym : Symmetric _≈_ |
| 147 |
trans : Transitive _≈_ |
| 148 |
|
| 149 |
-- More specific concepts are then specified in terms of simpler ones: |
| 150 |
|
| 151 |
record IsMagma {A : Set a} (≈ : Rel A ℓ) (∙ : Op₂ A) : Set (a ⊔ ℓ) where |
| 152 |
field |
| 153 |
isEquivalence : IsEquivalence ≈ |
| 154 |
∙-cong : ∙ Preserves₂ ≈ ⟶ ≈ ⟶ ≈ |
| 155 |
|
| 156 |
record IsSemigroup {A : Set a} (≈ : Rel A ℓ) (∙ : Op₂ A) : Set (a ⊔ ℓ) where |
| 157 |
field |
| 158 |
isMagma : IsMagma ≈ ∙ |
| 159 |
associative : Associative ≈ ∙ |
| 160 |
|
| 161 |
open IsMagma isMagma public |
| 162 |
|
| 163 |
-- Note here that `open IsMagma isMagma public` ensures that the |
| 164 |
-- fields of the `isMagma` record can be accessed directly; this |
| 165 |
-- technique enables the user of an `IsSemigroup` record to use underlying |
| 166 |
-- records without having to manually open an entire record hierarchy. |
| 167 |
-- |
| 168 |
-- This is not always possible, though. Consider the following definition |
| 169 |
-- of preorders: |
| 170 |
|
| 171 |
record IsPreorder {A : Set a} |
| 172 |
(_≈_ : Rel A ℓ) -- The underlying equality. |
| 173 |
(_∼_ : Rel A ℓ) -- The relation. |
| 174 |
: Set (a ⊔ ℓ) where |
| 175 |
field |
| 176 |
isEquivalence : IsEquivalence _≈_ |
| 177 |
refl : Reflexive _∼_ |
| 178 |
trans : Transitive _∼_ |
| 179 |
|
| 180 |
module Eq = IsEquivalence isEquivalence |
| 181 |
|
| 182 |
-- The IsEquivalence field in IsPreorder is not opened publicly because |
| 183 |
-- the `refl` and `trans` fields would clash with those in the |
| 184 |
-- `IsPreorder` record. Instead we provide an internal module and the |
| 185 |
-- equality fields can be accessed via `Eq.refl` and `Eq.trans`. |
| 186 |
|
| 187 |
|
| 188 |
------------------------------------------------------------------------ |
| 189 |
-- X.Bundles |
| 190 |
|
| 191 |
-- Although structures are useful for describing the properties of a |
| 192 |
-- given set of operations/relations, sometimes you don't require the |
| 193 |
-- properties to hold for a given set of objects but only that such a |
| 194 |
-- set of objects exists. In this case bundles are what you're after. |
| 195 |
|
| 196 |
-- Each structure has a corresponding bundle that include the structure |
| 197 |
-- along with the corresponding sets, relations and operations as |
| 198 |
-- fields. |
| 199 |
|
| 200 |
record Setoid c ℓ : Set (suc (c ⊔ ℓ)) where |
| 201 |
field |
| 202 |
Carrier : Set c |
| 203 |
_≈_ : Rel Carrier ℓ |
| 204 |
isEquivalence : IsEquivalence _≈_ |
| 205 |
|
| 206 |
open IsEquivalence isEquivalence public |
| 207 |
|
| 208 |
-- The contents of the structure is always re-exported publicly, |
| 209 |
-- providing access to its fields. |
| 210 |
|
| 211 |
record Magma c ℓ : Set (suc (c ⊔ ℓ)) where |
| 212 |
infixl 7 _∙_ |
| 213 |
infix 4 _≈_ |
| 214 |
field |
| 215 |
Carrier : Set c |
| 216 |
_≈_ : Rel Carrier ℓ |
| 217 |
_∙_ : Op₂ Carrier |
| 218 |
isMagma : IsMagma _≈_ _∙_ |
| 219 |
|
| 220 |
open IsMagma isMagma public |
| 221 |
|
| 222 |
|
| 223 |
record Semigroup : Set (suc (a ⊔ ℓ)) where |
| 224 |
infixl 7 _∙_ |
| 225 |
infix 4 _≈_ |
| 226 |
field |
| 227 |
Carrier : Set a |
| 228 |
_≈_ : Rel Carrier ℓ |
| 229 |
_∙_ : Op₂ Carrier |
| 230 |
isSemigroup : IsSemigroup _≈_ _∙_ |
| 231 |
|
| 232 |
open IsSemigroup isSemigroup public |
| 233 |
|
| 234 |
magma : Magma a ℓ |
| 235 |
magma = record { isMagma = isMagma } |
| 236 |
|
| 237 |
-- Note that the `Semigroup` record does not include a (primitive; |
| 238 |
-- definitional) `Magma` field, by contrast with the `IsSemigroup` |
| 239 |
-- structure which *does* include an `isMagma` field as primitive. |
| 240 |
-- Instead, the `Semigroup` record includes an additional declaration |
| 241 |
-- (a 'manifest field' of the `record`) defining a `Magma` bundle, in |
| 242 |
-- terms of that exported `isMagma` field. In this way, 'inheritance' |
| 243 |
-- is *automatic* for the `IsX` sub*structures* of a given bundle, |
| 244 |
-- while supporting the *optional* export of inherited sub*bundles*. |
| 245 |
|
| 246 |
-- The above setup may seem a bit complicated, but it has been arrived |
| 247 |
-- at after a lot of thought and is designed to both make the hierarchies |
| 248 |
-- easy to work with whilst also providing enough flexibility for the |
| 249 |
-- different applications of their concepts. |
| 250 |
|
| 251 |
-- NOTE: bundles for the function hierarchy are designed a little |
| 252 |
-- differently, as a function with an unknown domain and codomain is |
| 253 |
-- of little use. |
| 254 |
|
| 255 |
------------------------- |
| 256 |
-- Bundle re-exporting -- |
| 257 |
------------------------- |
| 258 |
|
| 259 |
-- In general ensuring that bundles re-export everything in their |
| 260 |
-- sub-bundles can get a little tricky. |
| 261 |
|
| 262 |
-- Imagine we have the following general scenario where bundle A is a |
| 263 |
-- direct refinement of bundle C (i.e. the record `IsA` has an `IsC` field) |
| 264 |
-- but is also morally a refinement of bundles B and D. |
| 265 |
|
| 266 |
-- Structures Bundles |
| 267 |
-- ========== ======= |
| 268 |
-- IsA A |
| 269 |
-- / || \ / || \ |
| 270 |
-- IsB IsC IsD B C D |
| 271 |
|
| 272 |
-- The procedure for re-exports in the bundles is as follows: |
| 273 |
|
| 274 |
-- 1. `open IsA isA public using (IsC, M)` where `M` is everything |
| 275 |
-- exported by `IsA` that is not exported by `IsC`. |
| 276 |
|
| 277 |
-- 2. Construct `c : C` via the `isC` obtained in step 1. |
| 278 |
|
| 279 |
-- 3. `open C c public hiding (N)` where `N` is the list of fields |
| 280 |
-- shared by both `A` and `C`. |
| 281 |
|
| 282 |
-- 4. Construct `b : B` via the `isB` obtained in step 1. |
| 283 |
|
| 284 |
-- 5. `open B b public using (O)` where `O` is everything exported |
| 285 |
-- by `B` but not exported by `IsA`. |
| 286 |
|
| 287 |
-- 6. Construct `d : D` via the `isC` obtained in step 1. |
| 288 |
|
| 289 |
-- 7. `open D d public using (P)` where `P` is everything exported |
| 290 |
-- by `D` but not exported by `IsA`. |
| 291 |
|
| 292 |
------------------------------------------------------------------------ |
| 293 |
-- Other hierarchy modules |
| 294 |
------------------------------------------------------------------------ |
| 295 |
|
| 296 |
------------------------------------------------------------------------ |
| 297 |
-- X.Bundles.Raw |
| 298 |
|
| 299 |
-- Sometimes it is useful to have the bundles without any accompanying |
| 300 |
-- laws. These correspond more or less to what the definitions would |
| 301 |
-- be in non-dependently typed languages like Haskell. |
| 302 |
|
| 303 |
-- Each bundle therefore has a corresponding raw bundle that only |
| 304 |
-- includes the operations, but not the laws. |
| 305 |
|
| 306 |
record RawMagma c ℓ : Set (suc (c ⊔ ℓ)) where |
| 307 |
infixl 7 _∙_ |
| 308 |
infix 4 _≈_ |
| 309 |
field |
| 310 |
Carrier : Set c |
| 311 |
_≈_ : Rel Carrier ℓ |
| 312 |
_∙_ : Op₂ Carrier |
| 313 |
|
| 314 |
record RawMonoid c ℓ : Set (suc (c ⊔ ℓ)) where |
| 315 |
infixl 7 _∙_ |
| 316 |
infix 4 _≈_ |
| 317 |
field |
| 318 |
Carrier : Set c |
| 319 |
_≈_ : Rel Carrier ℓ |
| 320 |
_∙_ : Op₂ Carrier |
| 321 |
ε : Carrier |
| 322 |
|
| 323 |
------------------------------------------------------------------------ |
| 324 |
-- X.Consequences |
| 325 |
|
| 326 |
-- The "consequences" modules contains proofs for how the different |
| 327 |
-- types in the `Definitions` module relate to each other. For example: |
| 328 |
-- that any total relation is reflexive or that commutativity allows |
| 329 |
-- one to translate between left and right identities. |
| 330 |
|
| 331 |
total⇒refl : ∀ {_∼_ : Rel A ℓ} → Total _∼_ → Reflexive _∼_ |
| 332 |
total⇒refl = {!!} |
| 333 |
|
| 334 |
idˡ∧comm⇒idʳ : ∀ {_≈_ : Rel A ℓ} {e _∙_} → Commutative _≈_ _∙_ → |
| 335 |
LeftIdentity _≈_ e _∙_ → RightIdentity _≈_ e _∙_ |
| 336 |
idˡ∧comm⇒idʳ = {!!} |
| 337 |
|
| 338 |
------------------------------------------------------------------------ |
| 339 |
-- X.Construct |
| 340 |
|
| 341 |
-- The "construct" folder contains various generic ways of constructing |
| 342 |
-- new instances of the hierarchy. For example, |
| 343 |
|
| 344 |
import Relation.Binary.Construct.Intersection |
| 345 |
|
| 346 |
-- takes in two relations and forms the new relation that says two |
| 347 |
-- elements are only related if they are related via both of the |
| 348 |
-- original relations. |
| 349 |
|
| 350 |
-- These files are layed out in four parts, mimicking the main modules |
| 351 |
-- of the hierarchy itself. First they define the new relation, then |
| 352 |
-- subsequently the definitions, then structures and finally |
| 353 |
-- bundles can be translated across to it. |
| 354 |
|
| 355 |
------------------------------------------------------------------------ |
| 356 |
-- X.Morphisms |
| 357 |
|
| 358 |
-- The `Morphisms` folder is a sub-hierarchy containing relationships |
| 359 |
-- such as homomorphisms, monomorphisms and isomorphisms between the |
| 360 |
-- structures and bundles in the hierarchy. |
| 361 |
|
| 362 |
------------------------------------------------------------------------ |
| 363 |
-- X.Properties |
| 364 |
|
| 365 |
-- The `Properties` folder contains additional proofs about the theory |
| 366 |
-- of each bundle. They are usually designed so that a bundle's |
| 367 |
-- `Properties` file re-exports the contents of the `Properties` files |
| 368 |
-- above it in the hierarchy. For example |
| 369 |
-- `Algebra.Properties.AbelianGroup` re-exports the contents of |
| 370 |
-- `Algebra.Properties.Group`. |