#category-theory #monads #tagless #free-monads #monad-transformers

algar

Algebraic structures, higher-kinded types and other category theory bad ideas

7 unstable releases (3 breaking)

0.4.1 Mar 22, 2023
0.4.0 Mar 21, 2023
0.3.1 Jan 26, 2023
0.2.1 Jan 20, 2023
0.1.0 Jan 17, 2023

#835 in Rust patterns

44 downloads per month

MIT license

50KB
1K SLoC

Algar

Crates.io Build docs-badge

Algebric structures, higher-kinded types and other category theory bad ideas.

Yes, you'll have generalized functors, applicatives, monads, traversable, free monads and much more in Rust at your fingertips. But no, they're not as ergonomic and beautiful as in Haskell, mainly because of the lack of higher-kinded types in Rust type-system.

In the examples section you'll also find my take on solving the expression problem in Rust: we start from a basic (wrong) implementation and then work towards a solution by trying to implement coproduct of functors and final tagless encoding.

Why?

I wrote this library for two reasons: first, mainly as a playground for learning Category Theory and Rust, second to see if it was even possible to implement such general abstract nonsense in Rust.

Does category theory make you a better programmer ?

I think it does. Category theory centers around abstraction and composition and I will argue strongly that abstraction and composition are the essence of programming.

Abstraction

Abstraction is essentially the core of computer science and exceptionally important in everyday programming: learning this sort of mathematics allows you to unlock a higher level of abstraction.

Since Category theory is the most abstract branch of math, it's no surprise that it lends itself to great programming abstractions and then to extremely useful programming ideas. Haskell programmers have been tapping this resource for a long time, and the ideas are percolating into other languages.

Composition

All software development is composition. The act of breaking a complex problem down to smaller parts, and then composing those smaller solutions together to form structures and patterns, hence your application, well that's what programming is all about.

We’ve been composing things forever, long before some great engineer came up with the idea of a subroutine. Some time ago the principles of structured programming revolutionized programming because they made blocks of code composable. Then came object oriented programming, which is all about composing objects. Functional programming is not only about composing functions and algebraic data structures — it makes concurrency composable — something that’s virtually impossible with other programming paradigms. -- Bartosz Milewski

Interested in learning more?

I heavely recommend:

Walking through those resources probably won't change your code overnight. Some people call it general abstract nonsense for a reason. That said, it does provide a nice framework for thinking about these abstract ideas, and is a recommended pursuit for all that are curious.

Prior Art

This library draws heavy inspiration from mathematics and other Rust and Elixir libraries: let me mention them here.

The Witchcraft Elixir library is why I started this journey.

The Fantasy Land Spec is a spec for projects such as this one, but targeted at Javascript. It does not come with its own implementation, but provides a helpful chart of class hierarchies.

The Scala Cats library is well documented and exceptionally enlightening on some concepts of category theory.

Fp-core.rs and higher have been invaluable resources to help me to port category theory concepts in Rust.

Obviously the Haskell Prelude deserves mention. Haskell has inspired so many programmers to write clean, declarative, functional code based on principled abstractions.

No runtime deps