Expand description
The deep_causality_haft crate provides foundational traits and utilities for
implementing Higher-Kinded Types (HKTs) and functional programming patterns
(Functor, Applicative, Monad, Foldable) in Rust.
This crate is a core component of the deep_causality project, enabling
the construction of flexible and robust type-encoded effect systems.
§Core Concepts
- Higher-Kinded Types (HKTs): Abstractions over type constructors (e.g.,
Option<T>,Vec<T>). This allows writing generic code that works across different container types. - Functor: Defines the
fmapoperation for mapping a function over a type constructor. - Applicative: Extends
Functorwithpure(to lift values) andapply(to apply functions within a context). - Monad: Provides the
bindoperation for sequencing computations that produce effectful values. - Foldable: Defines the
foldoperation for reducing a data structure to a single value. - Type-Encoded Effect Systems: A mechanism to explicitly track and manage side-effects (like errors, logging, counters) using Rust’s type system, ensuring compile-time verification.
§Modules
core: Core HKT definitions and machinery.algebra: Algebraic traits (Functor, Monad, etc.).effect_system: Type-encoded effect system traits.extensions: Concrete HKT witness implementations for standard Rust types.utils_tests: Internal utilities and test-specific effect types.
§Usage
This crate is primarily intended for internal use within the deep_causality project
to build its core abstractions. However, the traits and concepts can be generally applied
to other Rust projects requiring advanced functional programming patterns and effect management.
Re-exports§
pub use crate::iso::NaturalIso;pub use crate::iso::NaturalIso2;pub use crate::iso::NaturalIso3;pub use crate::iso::NaturalIso4;pub use crate::iso::NaturalIso5;
Modules§
- iso
- Tier 3 isomorphism traits — natural isomorphisms between HKT witnesses.
- utils_
tests - This module provides utility types and implementations primarily used for testing
and demonstrating the type-encoded effect system within
deep_causality_haft.
Structs§
- Arrow
Builder - A fluent builder over the
Arrowalgebra that hides the combinator types. - Arrow
Term - The typed façade over
ArrowCore: a free-arrow term whoseIn/Outobjects are tracked in the type so that only sound wiring composes. - BTree
MapWitness BTreeMapWitness<K>is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theBTreeMap<K, V>type constructor, where the key typeKis fixed.- BoxWitness
BoxWitnessis a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theBox<T>type constructor. It allowsBoxto be used with generic functional programming traits likeFunctor,Applicative,Foldable, andMonad.- Choice
- The coproduct sum
+++: route each summand to its own arrow —Either<A, C> → Either<B, D>fromF: A → BandG: C → D. - Cofree
- The cofree comonad on a functor
F:head :< f (Cofree f a). - Cofree
Witness - The
HKTwitness for the cofree comonad over the functorF(dual ofFreeWitness). - Compose
- Sequential composition
f >>> g: the arrow that runsf, thengon its output. - Fanin
- Fanin
|||: the coproduct elimination — both branches converge on one output type,Either<A, C> → BfromF: A → BandG: C → B. This is the universal map of the coproduct (haft.either.coproduct_universal) as an arrow. - Fanout
- Fanout
&&&: feeds one input to two arrows —A → (B, C)fromf: A → Bandg: A → C. Requires the input to beClone(it is duplicated to both arrows). - First
- Strength on the first component: lifts
F: A → Bto(A, C) → (B, C), passing the second component through unchanged. - FnMorphism
- The canonical function-pointer carrier for
Morphism: an arrowA → Bis a plainfn(A) -> B. - Free
Witness - The
HKTwitness for the free monad over the operation functorF. - Fun
- The category of functions
Fun:Hom<B> = B, identity is|a| a, composition isg ∘ f. - Hash
MapWitness HashMapWitness<K>is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theHashMap<K, V>type constructor, where the key typeKis fixed.- Id
- The identity arrow
A → A— the unit of composition. - IoAnd
Then - The monadic bind of the IO monad: run the inner action, feed its output to
f, then run the actionfreturns. Short-circuits on the first error (the second action does not run). - IoFail
- The zero of the IO monad: an action that fails with
errorand performs no side effect. - IoMap
- The functor map of the IO monad: run the inner action, then apply
fto its successful output. - IoMap
Err - Transforms the error channel of an IO action, leaving the success path untouched.
- IoPure
- The unit of the IO monad: an action that yields
valuewith no side effect. - Kleisli
- The Kleisli category of a monad
M(Mac Lane §VI.5; Moggi, Notions of Computation and Monads, 1991):Hom<B> = M::Type<B>, identity ispure, and composition isbind(compose(f, g)(a) = bind(f(a), g), the Kleisli arrow>=>). - Left
left: liftF: A → BtoEither<A, C> → Either<B, C>, acting on the left summand and passing aRightvalue through unchanged.- Lift
- Lifts a plain function
F: Fn(A) -> Binto anArrowA → B. - Linked
List Witness LinkedListWitnessis a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theLinkedList<T>type constructor. It allowsLinkedListto be used with generic functional programming traits likeFunctor,Applicative,Foldable, andMonad.- NoConstraint
- The universal constraint — every type satisfies it.
- Option
ToVec - The natural transformation
Option ⇒ Vec:None ↦ [],Some a ↦ [a]. - Option
Witness OptionWitnessis a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theOption<T>type constructor. It allowsOptionto be used with generic functional programming traits likeFunctor,Applicative,Foldable, andMonad.- Placeholder
- A zero-sized type used purely as a marker or placeholder when implementing Higher-Kinded Type (HKT) traits for concrete types.
- Result
Unbound Witness ResultUnboundWitnessis a zero-sized type that acts as a witness for theResult<A, B>type constructor where both parameters are unbound.- Result
Witness ResultWitness<E>is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theResult<T, E>type constructor, where the error typeEis fixed.- Right
right: liftF: A → BtoEither<C, A> → Either<C, B>, acting on the right summand and passing aLeftvalue through unchanged.- Second
- Strength on the second component: lifts
F: A → Bto(C, A) → (C, B), passing the first component through unchanged. - Split
- The monoidal product
***: runs two arrows in parallel on a pair —(A, C) → (B, D)fromf: A → Bandg: C → D. - SymMonoidal
- The generators of the cartesian symmetric-monoidal PROP:
copy/discard(the copy comonoid),merge/unit(the merge monoid), andswap(the symmetry). A zero-sized namespace — the generators are ad-hoc polymorphic (over any value, or anyMonoid), so they are associated functions rather than trait methods. - Tuple2
Witness Tuple2Witnessacts as a witness for the(A, B)type constructor.- Tuple3
Witness Tuple3Witnessacts as a witness for the(A, B, C)type constructor.- VecDeque
Witness VecDequeWitnessis a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theVecDeque<T>type constructor. It allowsVecDequeto be used with generic functional programming traits likeFunctorandFoldable.- VecWitness
VecWitnessis a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for theVec<T>type constructor. It allowsVecto be used with generic functional programming traits likeFunctor,Applicative,Foldable, andMonad.
Enums§
- Arrow
Core - The erased core: the free arrow over a generator set
G, stored as first-order data. - Arrow
Val - The value universe interpreted terms flow through: a binary tree with payload leaves.
- Either
- A value of one of two types,
Left(L)orRight(R). - Free
- The free monad on a functor
F:Pure a | Suspend (f (Free f a)).
Traits§
- Adjunction
- The
Adjunctiontrait defines a pair of adjoint functorsL(Left) andR(Right) with an optional runtimeContext. - Alias
Adjunction - Alias trait for
Adjunctionproviding more intuitive method names. - Alias
CoMonad - Alias trait for
CoMonadproviding more intuitive method names. - Alias
Foldable - Alias trait for
Foldableproviding more intuitive method names. - Alias
Functor - Alias trait for
Functorproviding more intuitive method names. - Alias
Monad - Alias trait for
Monadproviding more intuitive method names. - Alias
Profunctor - Alias trait for
Profunctorproviding more intuitive method names. - Applicative
- The
Applicativetrait extendsFunctorandPureby providing theapplyoperation to apply a function wrapped in a context to a value wrapped in a context. - Arrow
- A value-level arrow
In → Out: a runnable, composable transformation. - Bifunctor
- The
Bifunctortrait allows mapping over both arguments of a type constructorF<A, B>. - Category
- A category: an identity morphism on every object and associative composition of compatible morphisms (Mac Lane, Categories for the Working Mathematician, §I.1).
- Clone
Functor - Witness capability: an
HKTfunctor whoseType<T>can be cloned whenever the payloadT: Clone. - CoMonad
- The
CoMonadtrait represents a comonadic context, which is the dual of aMonad. - Cybernetic
Loop - The
CyberneticLooptrait models a complete feedback control system involving 5 distinct components. - Debug
Functor - Witness capability: an
HKTfunctor whoseType<T>can beDebug-formatted whenever the payloadT: Debug. - Effect3
- Effect3: The Bridge Trait for Arity 3 Type Constructors.
- Effect4
- Effect4: The Bridge Trait for Arity 4 Type Constructors.
- Effect5
- Effect5: The Bridge Trait for Arity 5 Type Constructors.
- Effect3
Unbound - Effect3Unbound: Parametric Effect Trait for Arity 3.
- Effect4
Unbound - Effect4Unbound: Parametric Effect Trait for Arity 4.
- Effect5
Unbound - Effect5Unbound: Parametric Effect Trait for Arity 5.
- Endo
Arrow - The value-level realization of the
Endomorphismmonoid, as a type extension: iteration of an endo-arrow (anArrowwhose input and output type coincide). - Endomorphism
- The type-preserving fragment of
Morphism: arrowsT → T. - EqFunctor
- Witness capability: an
HKTfunctor whoseType<T>can be compared structurally whenever the payloadT: PartialEq. - Foldable
- The
Foldabletrait abstracts over data structures that can be reduced to a single summary value. - Functor
- The
Functortrait abstracts over types that can be mapped over. - HKT
- Trait for a Higher-Kinded Type (HKT) with one type parameter (arity 1).
- HKT2
- Trait for a Higher-Kinded Type (HKT) with two type parameters (arity 2).
- HKT3
- Trait for a Higher-Kinded Type (HKT) with three type parameters (arity 3).
- HKT4
- Trait for a Higher-Kinded Type (HKT) with four type parameters (arity 4).
- HKT5
- Trait for a Higher-Kinded Type (HKT) with five type parameters (arity 5).
- HKT2
Unbound - Trait for a Higher-Kinded Type (HKT) with two unbound generic parameters (Arity 2).
- HKT3
Unbound - Trait for a Higher-Kinded Type (HKT) with three generic parameters (Arity 3).
- HKT4
Unbound - Trait for a Higher-Kinded Type (HKT) with four generic parameters (Arity 4).
- HKT5
Unbound - Trait for a Higher-Kinded Type (HKT) with five generic parameters (Arity 5).
- HKT6
Unbound - Trait for a Higher-Kinded Type (HKT) with six generic parameters (Arity 6).
- IoAction
- A deferred description of an input/output computation.
- LogAdd
Entry - LogAppend
- Trait for types that can append log entries from another instance of themselves.
- LogEffect
- LogSize
- Monad
- The
Monadtrait extendsFunctorandPureby providing thebindoperation for sequencing computations that produce effectful values. - Monad
Effect3 - Monadic logic for an Arity 3 type after it has been partially applied via
Effect3. - Monad
Effect4 - Monadic logic for an Arity 4 type after it has been partially applied via
Effect4. - Monad
Effect5 - Monadic logic for an Arity 5 type after it has been partially applied via
Effect5. - Monad
Effect3 Unbound - Monadic logic for Parametric Effects (Arity 3 Unbound).
- Monad
Effect4 Unbound - Monadic logic for Parametric Effects (Arity 4 Unbound).
- Monad
Effect5 Unbound - Monadic logic for Parametric Effects (Arity 5 Unbound).
- Monoidal
Merge - The
MonoidalMergetrait models the “fusion” or “interaction” of two contexts to produce a third. - Morphism
- The explicit typed-arrow base: a family of arrows
P::Type<A, B>(thinkA → B) with an identity arrow and the ability to apply an arrow to an input. - Natural
Transformation - A natural transformation
F ⇒ Gbetween twoHKTfunctors. - Parametric
Monad - The
ParametricMonad(or Indexed Monad) trait allows for monadic computations where the type of the underlying state can change at each step. - Profunctor
- The
Profunctortrait represents a type constructor that is contravariant in its first argument and covariant in its second argument. - Pure
- The
Puretrait provides the ability to lift a value into a context. - Riemann
Map - The
RiemannMaptrait models high-arity geometric interactions, specifically the Riemann Curvature Tensor and Scattering Matrices. - Satisfies
- Marker trait indicating that type
Tsatisfies constraintC. - Traversable
- The
Traversabletrait abstracts over data structures that can be “traversed” or “sequenced” in a way that preserves effects. It combines the capabilities ofFunctor(mapping over elements) andFoldable(reducing to a single value).