karpal-core
Core algebraic structures for Rust: HKT encoding, a full functor hierarchy
(Functor through Monad), Semigroup, Monoid, adjunctions, ends/coends,
dinatural transformations, and do_!/ado_! notation macros.
What's inside
HKT encoding
GAT-based Higher-Kinded Type encoding with zero dependencies:
use ;
// HKT — OptionF::Of<T> = Option<T>, ResultF<E>::Of<T> = Result<T, E>, VecF::Of<T> = Vec<T>
// HKT2 — ResultBF::P<A, B> = Result<B, A>, TupleF::P<A, B> = (A, B)
Functor hierarchy
| Trait | Supertrait | Instances |
|---|---|---|
| Functor | HKT | OptionF, ResultF, VecF |
| Apply | Functor | OptionF, ResultF, VecF |
| Applicative | Apply | OptionF, ResultF, VecF |
| Chain | Apply | OptionF, ResultF, VecF |
| Monad | Applicative + Chain | blanket impl |
| Alt | Functor | OptionF, ResultF, VecF |
| Plus | Alt | OptionF, VecF |
| Alternative | Applicative + Plus | blanket impl |
| Foldable | HKT | OptionF, ResultF, VecF |
| Traversable | Functor + Foldable | OptionF, ResultF, VecF |
| FunctorFilter | Functor | OptionF, VecF |
| Selective | Applicative | OptionF |
| Contravariant | HKT | PredicateF |
| Bifunctor | HKT2 | ResultBF, TupleF |
| NaturalTransformation | HKT | OptionToVec, VecHeadToOption |
| Adjunction | HKT | IdentityAdj, CurryAdj |
| ContravariantAdjunction | HKT | ContAdj |
| DinaturalTransformation | HKT2 | DinaturalId |
| End | HKT2 | trait with generic run<A> |
| Coend | HKT2 | struct with elim |
| ComposeF | HKT | Functor composition |
Chain and do_! — sequential, dependent computations
When each step depends on the result of the previous one, do_! replaces
nested .and_then() chains with flat, readable notation:
use ;
use OptionF;
// A multi-step lookup where each step can fail
let items = vec!;
let aliases = vec!;
assert_eq!;
assert_eq!; // first step fails → short-circuits
Applicative and ado_! — independent computations combined
Unlike do_!, ado_! makes it explicit that the computations don't
depend on each other. All bindings are evaluated independently, then
combined in the yield:
use ;
use OptionF;
// All three lookups are independent — if any fails, the whole thing fails
let env = vec!;
assert_eq!;
let env = vec!; // PORT missing
assert_eq!;
Traversable — batch operations that fail fast
"Run this fallible operation on every element; if any one fails, the
whole batch fails." traverse does this without manual loops or
early-return boilerplate:
use Traversable;
use ;
let ids = vec!;
let parsed: = ;
assert_eq!;
// One invalid entry poisons the whole batch
let ids = vec!;
let parsed: = ;
assert_eq!;
Alt — fallback chains
Try multiple strategies in order, taking the first success:
use Alt;
use OptionF;
assert_eq!;
assert_eq!;
assert_eq!;
Foldable — generic aggregation with Monoid
Summarize any foldable structure using any Monoid. The fold knows
nothing about the container or the summary type — it just needs
combine and empty:
use ;
use VecF;
let events = vec!;
let hist: Histogram = fold_map;
let clicks = hist.buckets.iter.find.unwrap.1;
let views = hist.buckets.iter.find.unwrap.1;
assert_eq!;
assert_eq!;
Bifunctor — map both sides of a Result or tuple
use Bifunctor;
use ;
// Map the error to a structured type and the success to a display string
let result: = Err;
let mapped = bimap;
assert_eq!;
// Transform both halves of a key-value pair
let entry = ;
let display = bimap;
assert_eq!;
FunctorFilter — map and filter in one pass
use FunctorFilter;
use VecF;
// Parse valid entries, silently skip malformed ones
let raw = vec!;
let valid: = filter_map;
assert_eq!;
Semigroup / Monoid
use ;
// Combine anything associative
assert_eq!;
assert_eq!;
// Monoid adds an identity element
assert_eq!;
// Tuple instances combine component-wise
assert_eq!;
Instances: all numeric types (additive), String, Vec<T>, Option<T: Semigroup>, (A, B).
Newtype wrappers
Select alternative Semigroup/Monoid instances for numeric types:
use ;
use VecF;
// Product uses multiplication instead of addition
let product = fold_map;
assert_eq!;
// Min/Max for ordered types
assert_eq!;
assert_eq!;
// First/Last pick the first/last Some value
assert_eq!;
assert_eq!;
Features
| Feature | Default | Description |
|---|---|---|
std |
yes | Enables Vec, String, PredicateF instances |
alloc |
no | Same instances via alloc (for no_std) |
License
MIT OR Apache-2.0