Karpal

Higher-Kinded Types and algebraic structures for Rust

Type-Safe Abstractions

HKT encoding via GATs lets you write functions generic over Option, Result, Vec, and any container that supports mapping, sequencing, or folding.

Complete Hierarchy

Functor through Monad, Alt through Alternative, Foldable, Traversable, Comonad, and contravariant duals — all with property-based law verification.

Profunctor Optics

Lens, Prism, and composition powered by the profunctor hierarchy. Build reusable, first-class field accessors and pattern matchers.

Ergonomic Macros

do_! flattens nested .and_then() chains. ado_! combines independent computations. Both work with any Monad or Applicative.

Quick Example

Flatten nested error handling with do_!:

use karpal_std::prelude::*;

// Without do_! — rightward drift with every step
fn process(input: &str) -> Option<String> {
    parse_id(input).and_then(|id| {
        lookup_user(id).and_then(|user| {
            check_permissions(&user).and_then(|role| {
                Some(format!("{} logged in as {:?}", user.name, role))
            })
        })
    })
}

// With do_! — reads top-to-bottom
fn process(input: &str) -> Option<String> {
    do_! { OptionF;
        id = parse_id(input);
        user = lookup_user(id);
        role = check_permissions(&user);
        Some(format!("{} logged in as {:?}", user.name, role))
    }
}

Workspace

CrateDescription
karpal-coreHKT encoding, functor hierarchy, Semigroup, Monoid, macros
karpal-profunctorProfunctor, Strong, Choice, FnP
karpal-opticsProfunctor optics: Lens, Prism, composition
karpal-stdStandard prelude re-exports

karpal-core and karpal-profunctor are no_std compatible with optional std/alloc feature gates.