|
1 | 1 | # Algebra Primitives |
2 | 2 |
|
3 | | -Swift Embedded compatible. |
| 3 | + |
| 4 | + |
| 5 | +Witness value types for the algebraic tower — magma, semigroup, monoid, group, ring, field, module, semilattice, lattice — that carry a structure's operations as stored closures, with the type name asserting the mathematical laws. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Quick Start |
| 10 | + |
| 11 | +`Algebra` types are *witnesses*: each one bundles the operations of an algebraic structure (its identity, its binary operation, its inverse) into a value, and the type name records the laws those operations are required to satisfy. Associativity, commutativity, idempotency, and the rest are documented invariants the constructing code must guarantee — they are not re-checked at every call. |
| 12 | + |
| 13 | +A bounded join-semilattice is the algebraic core of a state-based CRDT: any associative, commutative, *idempotent* merge with an identity converges. The witness makes that structure explicit and reusable. |
| 14 | + |
| 15 | +```swift |
| 16 | +import Algebra_Primitives |
| 17 | + |
| 18 | +// A grow-only counter merges by taking the per-replica maximum. |
| 19 | +// `max` is associative, commutative, and idempotent — a bounded semilattice with bottom 0. |
| 20 | +let merge = Algebra.Semilattice<Int>.maximum(bottom: 0) |
| 21 | + |
| 22 | +let replicaA = 5 |
| 23 | +let replicaB = 3 |
| 24 | +merge.combining(replicaA, replicaB) // 5 (least upper bound) |
| 25 | +merge.combining(replicaA, replicaA) // 5 (idempotent — re-merging is a no-op) |
| 26 | + |
| 27 | +// The merge induces a partial order: a ≤ b iff a ∨ b == b. |
| 28 | +merge.leq(3, 5) // true |
| 29 | +``` |
| 30 | + |
| 31 | +Stack two semilattices and you get a bounded lattice, with join, meet, and the order they share: |
| 32 | + |
| 33 | +```swift |
| 34 | +import Algebra_Primitives |
| 35 | + |
| 36 | +// Any Comparable chain is a distributive bounded lattice: join = max, meet = min. |
| 37 | +let lattice = Algebra.Lattice<Int>.minMax(bottom: .min, top: .max) |
| 38 | + |
| 39 | +lattice.join(3, 7) // 7 (∨ = least upper bound) |
| 40 | +lattice.meet(3, 7) // 3 (∧ = greatest lower bound) |
| 41 | +lattice.leq(3, 7) // true |
| 42 | +``` |
| 43 | + |
| 44 | +The same vocabulary scales up to rings and fields, where convenience accessors (`zero`, `one`, `adding`, `multiplying`, `reciprocal`) read off the underlying monoids and groups. The `Algebra.Law` harnesses verify a witness's invariants over a finite sample of elements, returning an `Algebra.Law.Violation?` that is `nil` when the law holds — pure functions with no traps, ready to drop into a test. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Installation |
| 49 | + |
| 50 | +```swift |
| 51 | +dependencies: [ |
| 52 | + .package(url: "https://github.com/swift-primitives/swift-algebra-primitives.git", branch: "main") |
| 53 | +] |
| 54 | +``` |
| 55 | + |
| 56 | +```swift |
| 57 | +.target( |
| 58 | + name: "App", |
| 59 | + dependencies: [ |
| 60 | + .product(name: "Algebra Primitives", package: "swift-algebra-primitives"), |
| 61 | + ] |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | +Import `Algebra_Primitives` for the whole tower, or depend on a single rung (e.g. `Algebra Semilattice Primitives`) to pull in only what you use. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Architecture |
| 70 | + |
| 71 | +The package is split along the algebraic tower so each rung is an independent product depending only on the rungs below it. The `Algebra` namespace is shared by all of them. |
| 72 | + |
| 73 | +| Product | Target | Purpose | |
| 74 | +|---------|--------|---------| |
| 75 | +| `Algebra Primitive` | `Sources/Algebra Primitive/` | The empty `Algebra` namespace enum that every structure extends. | |
| 76 | +| `Algebra Magma Primitives` | `Sources/Algebra Magma Primitives/` | `Algebra.Magma` — a set with one binary operation, no laws. | |
| 77 | +| `Algebra Semigroup Primitives` | `Sources/Algebra Semigroup Primitives/` | `Algebra.Semigroup` — associative magma. | |
| 78 | +| `Algebra Monoid Primitives` | `Sources/Algebra Monoid Primitives/` | `Algebra.Monoid` and `Algebra.Monoid.Commutative` — semigroup with identity. | |
| 79 | +| `Algebra Semiring Primitives` | `Sources/Algebra Semiring Primitives/` | `Algebra.Semiring` and its commutative variant — additive and multiplicative monoids with distributivity. | |
| 80 | +| `Algebra Semilattice Primitives` | `Sources/Algebra Semilattice Primitives/` | `Algebra.Semilattice` — idempotent commutative monoid (the CRDT merge). | |
| 81 | +| `Algebra Lattice Primitives` | `Sources/Algebra Lattice Primitives/` | `Algebra.Lattice` — join and meet semilattices with absorption and bounds. | |
| 82 | +| `Algebra Group Primitives` | `Sources/Algebra Group Primitives/` | `Algebra.Group` and `Algebra.Group.Abelian` — monoid with inverses. | |
| 83 | +| `Algebra Ring Primitives` | `Sources/Algebra Ring Primitives/` | `Algebra.Ring` and its commutative variant — additive abelian group with multiplicative monoid. | |
| 84 | +| `Algebra Field Primitives` | `Sources/Algebra Field Primitives/` | `Algebra.Field` and `Algebra.Field.Unit` — ring with a partial reciprocal. | |
| 85 | +| `Algebra Module Primitives` | `Sources/Algebra Module Primitives/` | `Algebra.Module` and `Algebra.VectorSpace` — scalars acting on a vector group. | |
| 86 | +| `Algebra Law Primitives` | `Sources/Algebra Law Primitives/` | `Algebra.Law` verification harnesses (associativity, commutativity, identity, inverse, distributivity, annihilation, reciprocal, action, compatibility) returning `Algebra.Law.Violation?`. | |
| 87 | +| `Algebra Primitives` | `Sources/Algebra Primitives/` | Umbrella re-exporting every rung above. | |
| 88 | +| `Algebra Primitives Test Support` | `Tests/Support/` | Re-exports the umbrella for test consumers. | |
| 89 | + |
| 90 | +Foundation-free. |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## Platform Support |
| 95 | + |
| 96 | +| Platform | Status | |
| 97 | +|----------|--------| |
| 98 | +| macOS 26 | Full support | |
| 99 | +| Linux | Full support | |
| 100 | +| Windows | Full support | |
| 101 | +| iOS / tvOS / watchOS / visionOS | Supported | |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Community |
| 106 | + |
| 107 | +<!-- BEGIN: discussion --> |
| 108 | +<!-- Discussion thread created at publication. --> |
| 109 | +<!-- END: discussion --> |
| 110 | + |
| 111 | +## License |
| 112 | + |
| 113 | +Apache 2.0. See [LICENSE.md](LICENSE.md). |
0 commit comments