Algebra Module¶
Algebraic abstractions for functional programming.
This module provides the core algebraic type classes that form the foundation of functional programming in Katharos. These abstractions follow mathematical laws and enable composable, predictable code.
- The hierarchy of abstractions (from least to most powerful):
Semigroup: Types with an associative binary operationMonoid: Semigroups with an identity elementFunctor: Types that can be mapped overApplicative: Functors with function applicationMonad: Applicatives with sequencing capabilities
Each abstraction builds upon the previous one, adding more structure and capabilities while maintaining the laws of the simpler abstractions.
Semigroup¶
- class katharos.algebra.Semigroup[source]¶
-
Abstract base class for semigroups.
A semigroup is a set equipped with an associative binary operation. The binary operation is represented by the
@operator.
Monoid¶
Functor¶
Applicative¶
- class katharos.algebra.Applicative[source]¶
-
Abstract base class for applicative functors.
An applicative functor extends
Functorwith the ability to apply functions wrapped in a context to values wrapped in a context. It sits betweenFunctorandMonadin the abstraction hierarchy.Use
pure()to lift a plain value into the applicative context, and the**operator (orap()) to apply a wrapped function to a wrapped value.Note
Instances must satisfy the applicative laws (where
Appis the concrete applicative type andid/composeare the identity and composition functions):Identity:
v ** App.pure(id) == vComposition:
w ** (v ** (u ** App.pure(compose))) == (w ** v) ** uHomomorphism:
App.pure(x) ** App.pure(f) == App.pure(f(x))Interchange:
App.pure(y) ** u == u ** App.pure(lambda f: f(y))
- abstractmethod classmethod pure(x)[source]¶
Lift a value into the applicative context.
- Parameters:
x (
TypeVar(T)) – The value to wrap in an applicative.- Return type:
Applicative[TypeVar(App),TypeVar(T)]- Returns:
An applicative containing the given value.
- abstractmethod ap(wrapped_funcs)[source]¶
Apply wrapped functions to this applicative’s value.
- Parameters:
wrapped_funcs (
Applicative[TypeVar(App),Callable[[TypeVar(A)],TypeVar(B)]]) – An applicative containing functions from A to B.- Return type:
Applicative[TypeVar(App),TypeVar(B)]- Returns:
An applicative containing the result of applying the function.
- __pow__(wrapped_funcs)[source]¶
Infix operator for applicative application (
**).- Parameters:
wrapped_funcs (
Applicative[TypeVar(App),Callable[[TypeVar(A)],TypeVar(B)]]) – An applicative containing functions from A to B.- Return type:
Applicative[TypeVar(App),TypeVar(B)]- Returns:
An applicative containing the result of applying the function.
Monad¶
- class katharos.algebra.Monad[source]¶
Bases:
Applicative,ABC,GenericAbstract base class for monads.
A monad extends
Applicativewith thebindoperation (>>=in Haskell, exposed as|), which allows sequencing computations that produce monadic values.Use
ret()to lift a plain value, and|(orbind()) to chain computations. Use>>(orthen()) to sequence actions while discarding the first result.Note
Instances must satisfy the monad laws:
Left identity:
ret(a).bind(f) == f(a)Right identity:
m.bind(ret) == mAssociativity:
m.bind(f).bind(g) == m.bind(lambda x: f(x).bind(g))
Examples
Using
bind(|operator):m = SomeMonad.ret(5) result = m | (lambda x: SomeMonad.ret(x * 2))
Sequencing with
then(>>operator):m1 = SomeMonad.ret(1) m2 = SomeMonad.ret(2) result = m1 >> m2 # returns m2, discarding m1's value