{"id":51462478,"url":"https://github.com/sgtswagrid/scalgebra","last_synced_at":"2026-07-06T07:00:50.016Z","repository":{"id":350881663,"uuid":"1208635375","full_name":"SgtSwagrid/scalgebra","owner":"SgtSwagrid","description":"🌀 Fine-grained algebraic type classes for Scala 3 library authors.","archived":false,"fork":false,"pushed_at":"2026-05-09T18:04:39.000Z","size":6717,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-09T20:06:19.284Z","etag":null,"topics":["abstract-algebra","algebra","group-theory","library","scala","scala3","type-classes"],"latest_commit_sha":null,"homepage":"https://alecdorrington.com/scalgebra/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SgtSwagrid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-12T14:53:43.000Z","updated_at":"2026-05-09T18:03:58.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/SgtSwagrid/scalgebra","commit_stats":null,"previous_names":["sgtswagrid/not-enough-structures","sgtswagrid/scalgebra"],"tags_count":8,"template":false,"template_full_name":"SgtSwagrid/scala-library-template","purl":"pkg:github/SgtSwagrid/scalgebra","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SgtSwagrid%2Fscalgebra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SgtSwagrid%2Fscalgebra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SgtSwagrid%2Fscalgebra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SgtSwagrid%2Fscalgebra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SgtSwagrid","download_url":"https://codeload.github.com/SgtSwagrid/scalgebra/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SgtSwagrid%2Fscalgebra/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35180933,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-06T02:00:07.184Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstract-algebra","algebra","group-theory","library","scala","scala3","type-classes"],"created_at":"2026-07-06T07:00:49.186Z","updated_at":"2026-07-06T07:00:50.011Z","avatar_url":"https://github.com/SgtSwagrid.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n  \u003ch1\u003e🌀 Scalgebra\u003c/h1\u003e\n  \u003cp\u003eFine-grained algebraic type classes for Scala 3 library authors. \u003c/p\u003e\n\n  \u003cspan\u003e\n    \u003ca href=\"https://github.com/SgtSwagrid/scalgebra/actions/workflows/build-integrity.yml\"\u003e\u003cimg src=\"https://github.com/SgtSwagrid/scalgebra/actions/workflows/build-integrity.yml/badge.svg\" alt=\"Build status\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://search.maven.org/artifact/com.alecdorrington/scalgebra_3\"\u003e\u003cimg src=\"https://img.shields.io/maven-central/v/com.alecdorrington/scalgebra_3.svg\" alt=\"Maven Central\" /\u003e\u003c/a\u003e\n    \u003ca href=\"https://alecdorrington.com/scalgebra\"\u003e\u003cimg src=\"https://img.shields.io/badge/docs-latest-blue.svg\" alt=\"Documentation\" /\u003e\u003c/a\u003e\n  \u003c/span\u003e\n\n\u003c/div\u003e\n\n## 💡 Overview\n\n\u003cbr/\u003e\n\n\u003e \"Mathematics is the art of giving the same name to different things.\" — Henri Poincaré.\n\n\u003cbr/\u003e\n\n**Scalgebra** provides a hierarchy of type classes for algebraic structures: semigroups, monoids, groups, rings, fields, and more, split cleanly along additive and multiplicative lines.\n\nIt is aimed at **library designers** rather than end users. If you are writing a generic algorithm and want to express the minimal algebraic requirements on your type parameters, rather than demanding an all-or-nothing `Numeric`, then this library gives you the tools to do so.\n\n### Example\n\n```scala 3\nimport com.alecdorrington.scalgebra.Ring\n\n// Only requires addition, negation, and multiplication (not division).\ndef dot[X : Ring](xs: Seq[X], ys: Seq[X]): X =\n  xs.zip(ys).map(_ * _).reduce(_ + _)\n```\n\nYour users can then call `dot` on any type for which a `Ring` is in scope,\nincluding all standard numeric types, without any additional imports on their end.\n\n## ⬇️ Installation\n\nAdd the following dependency to your `build.sbt`:\n\n```scala\nlibraryDependencies += \"com.alecdorrington\" %% \"scalgebra\" % \"0.4.2\"\n```\n\nRequires Scala 3.\n\n## 📖 Usage\n\n### Selecting a type class\n\nChoose the weakest type class that covers the operations your code actually uses.\nThe [type class reference](#-type-class-reference) below lists what each one requires/provides.\n\n```scala 3\n// Too strong: you don't need division or a zero.\ndef double[X : Field](x: X): X = x + x\n\n// Just right.\ndef double[X : AdditiveSemigroup](x: X): X = x + x\n```\n\n### In-built types\n\n`given` instances for many types from the standard library are provided out-of-the-box.\nThey are available automatically; no import is required at call sites,\nas they are placed in the companion objects of the corresponding type class hierarchies.\nIncluded are all numeric types, tuples of algebraic structures, many collections, functions, etc.\n\n## 📋 Type class reference\n\nThe complete zoo of type classes is shown below.\nEach trait can be found in the package `com.alecdorrington.scalgebra`.\n\n| Structure                 | Capabilities                    |\n|---------------------------|---------------------------------|\n| `AdditiveIdentity`        | `0`                             |\n| `AdditiveInverse`         | `-_`                            |\n| `Difference`              | `-`                             |\n| `AdditiveSemigroup`       | `+`                             |\n| `DifferenceSemigroup`     | `+`, `-`                        |\n| `AdditiveMonoid`          | `+`, `0`                        |\n| `DifferenceMonoid`        | `+`, `-`, `0`                   |\n| `AdditiveGroup`           | `+`, `-_`, `0`                  |\n| `MultiplicativeIdentity`  | `1`                             |\n| `MultiplicativeInverse`   | `^-1`                           |\n| `Euclidean`               | `/`                             |\n| `MultiplicativeSemigroup` | `*`                             |\n| `EuclideanSemigroup`      | `*`, `/`                        |\n| `MultiplicativeMonoid`    | `*`, `1`                        |\n| `EuclideanMonoid`         | `*`, `/`, `1`                   |\n| `MultiplicativeGroup`     | `*`, `^-1`, `1`                 |\n| `Semiring`                | `+`, `*`, `0`, `1`              |\n| `DifferenceSemiring`      | `+`, `-`, `*`, `0`, `1`         |\n| `Pseudoring`              | `+`, `-_`, `*`, `0`             |\n| `Ring`                    | `+`, `-_`, `*`, `0`, `1`        |\n| `EuclideanRing`           | `+`, `-_`, `*`, `/`, `0`, `1`   |\n| `Semifield`               | `+`, `*`, `^-1`, `0`, `1`       |\n| `DifferenceSemifield`     | `+`, `-`, `*`, `^-1`, `0`, `1`  |\n| `Field`                   | `+`, `-_`, `*`, `^-1`, `0`, `1` |\n\n### Ordered variants\n\nThe `ordered` subpackage provides `Ordered`-prefixed variants that combine each type class with `Ordering`.\nUse these when your algorithm needs both algebraic operations and comparisons under a single context bound.\n\n```scala 3\nimport com.alecdorrington.scalgebra.ordered.OrderedField\n\ndef clamp[X : OrderedField](x: X, lb: X, ub: X): X = x.clamp(lb, ub)\n```\n\nThe ordered variants go beyond merely combining their unordered counterpart with `Ordering`,\nthey also add operations that require both capabilities simultaneously, such as `abs`, `sign`, and `clamp`.\nNote that an `OrderedField[X]` instance must be provided explicitly and is not derived automatically from `Field[X]` and `Ordering[X]`.\n\n### Normed variants\n\nThe `normed` subpackage provides `Normed`-prefixed variants that combine each type class with `Normed[X, S]`,\nwhere `S` is the scalar type that the norm maps into.\nUse these when your algorithm needs both algebraic operations and a norm under a single context bound.\n\n```scala 3\nimport com.alecdorrington.scalgebra.normed.NormedField\n\ndef normalize[X : NormedField.Over[X]](x: X): X = x / x.length\n```\n\nThe `Over[S]` type alias on each companion (e.g. `NormedField.Over[Double]`) allows them to be used as\nsingle-parameter context bounds.\nNote that a `NormedField[X, S]` instance must be provided explicitly and is not derived automatically from `Field[X]` and `Normed[X, S]`.\n\n### Providing evidence\n\nTo make your own type work with these type classes, provide a `given` instance:\n\n```scala 3\nimport com.alecdorrington.scalgebra.AdditiveMonoid\n\ncase class Vec2(x: Double, y: Double)\n\ngiven AdditiveMonoid[Vec2] with\n  def add(a: Vec2, b: Vec2): Vec2 = Vec2(a.x + b.x, a.y + b.y)\n  def zero: Vec2 = Vec2(0, 0)\n```\n\nFor in-built types (`Int`, `Double`, etc.), evidence is already included.\n\n## 🔌 Connectors\n\n_Scalgebra_ provides, as separate dependencies, connectors to all major abstract algebra libraries in the Scala ecosystem.\nThese provide automatic conversion between the algebraic type classes found here and those from each of the other libraries, where equivalents exist.\nConversions are provided in both directions.\n\n### Usage\n\nEach connector is published under the name\n`scalgebra-connector-\u003clibrary-name\u003e`\nand can be installed using:\n```scala\nlibraryDependencies += \"com.alecdorrington\" %% \"scalgebra-connector-\u003clibrary-name\u003e\" % \"\u003cscalgebra-version\u003e\"\n```\nThe version of the connector always matches the version of the core _Scalgebra_ library.\n\nThe following import statement will then load all relevant conversions:\n```scala\nimport com.alecdorrington.scalgebra.connector.\u003clibraryname\u003e.\u003cLibraryName\u003eConversions.given\n```\n\nThis could even be used to convert between algebra systems from multiple foreign libraries,\nwith no intention to ever use the intermediates that exist here.\n\n### Supported projects\n\nConnectors currently exist for the following projects:\n\n- [Algebird](https://twitter.github.io/algebird/)\n- [Breeze](https://github.com/scalanlp/breeze)\n- [Cats Algebra](https://typelevel.org/cats/algebra.html)\n- [Scalaz](https://github.com/scalaz/scalaz)\n- [Spire](https://spire-math.org/)\n- [ZIO Prelude](https://zio.dev/zio-prelude/)\n\n## ⚖️ What sets this library apart?\n\n- **Scala 3 native.** Built from the ground up with `given`/`using`, new-style context bounds, and significant indentation. No legacy implicit machinery.\n- **Fine-grained hierarchy.** The type class ladder spans from `AdditiveSemigroup` all the way up through `Semiring`, `Ring`, `EuclideanRing`, `Semifield`, and `Field`, with many intermediate structures (e.g. `DifferenceMonoid`, `DifferenceSemifield`, `EuclideanMonoid`) that other libraries skip over. You can express precisely the capabilities you need.\n- **Additive/multiplicative split.** Addition and multiplication are tracked as distinct capabilities via separate inheritance hierarchies, mirroring standard mathematical convention and enabling constraints like `Semiring` that are inexpressible in libraries with a single abstract binary operation.\n- **First-class ordered variants.** `OrderedRing`, `OrderedField`, etc. are proper type classes, not just a convention for pairing a structure with `Ordering`. They expose additional operations (`abs`, `sign`, `clamp`) that require both capabilities simultaneously.\n- **Zero-import evidence.** Instances for in-built types (`Int`, `Double`, etc.) are propagated through the companion object hierarchy, so call sites need no extra imports.\n- **Minimal footprint.** No number types, no approximate data structures, no lattices. Just the structural layer.\n- **Compatibility.** Integrates seamlessly with the competition.\n\n## 👁️ See also\n\n- Check out [Abstract Nonsense](https://github.com/SgtSwagrid/abstract-nonsense) for some different abstractions with a more category-theoretic focus.\n- This library was made using [Scala Library Template](https://github.com/SgtSwagrid/scala-library-template).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgtswagrid%2Fscalgebra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgtswagrid%2Fscalgebra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgtswagrid%2Fscalgebra/lists"}