Higher-Kinded Types (Haft)
Relevant source files
The following files were used as context for generating this wiki page:
- website/web/src/content/blog/en/announcement-haft-hkt.md
- openspec/changes/archive/spec-kit-old/008-hkt-uncertain-specs/plan.md
- openspec/changes/archive/spec-kit-old/008-hkt-uncertain-specs/spec.md
- openspec/changes/archive/2026-05-15-rebuild-website-astro/specs/documentation-site/spec.md
- openspec/changes/archive/2026-05-15-rebuild-website-astro/design.md
- AGENTS.md
Introduction
Higher-Order Abstract Functional Traits (HAFT) is a specialized framework within the DeepCausality project designed to implement Higher-Kinded Types (HKT) and algebraic abstractions in Rust. While Rust natively lacks HKT support, HAFT enables abstraction over type constructors (containers) rather than just concrete types. This allows the framework to define generic algorithms that operate uniformly across different contexts, such as Option<T>, Vec<T>, or more complex causal structures like Uncertain<T>.
HAFT serves as the "mathematical engine room" for the project, providing the rigorous functional programming foundations required for context-aware causal reasoning. It enables the creation of a Causal Monad and Type-Encoded Effect Systems, which allow the library to chain causal steps while remaining agnostic to underlying storage, logging, or error-handling details.
Sources: website/web/src/content/blog/en/announcement-haft-hkt.md:1-25, AGENTS.md:150-155
Core Architecture: The Witness Pattern
Since Rust does not support syntax like F<T> where F is a variable type constructor, HAFT utilizes the Witness Pattern (or Family Pattern) in combination with Generic Associated Types (GATs). This architecture separates the "shape" of a container from its contained data.
HKT and Witness Mechanism
A "Witness" is a zero-sized struct that represents a type constructor. For example, OptionWitness represents the concept of an Option without being a specific Option<i32>. The HKT trait defines the mapping between this witness and the actual type constructor.
The following table describes the primary components of the HKT mapping system:
| Component | Description |
|---|---|
HKT Trait | The base trait that defines a GAT Type<T> to map a witness to a constructor. |
| Witness Struct | A zero-sized type (e.g., OptionWitness, UncertainWitness) used as a proxy for a type constructor. |
| GAT (Generic Associated Type) | The Rust feature used to express type Type<T> within a trait implementation. |
Sources: website/web/src/content/blog/en/announcement-haft-hkt.md:40-65, openspec/changes/archive/spec-kit-old/008-hkt-uncertain-specs/spec.md:65-80
Algebraic Abstractions
HAFT provides standard functional programming traits adapted for the HKT witness system. These allow for composable transformations across different data contexts.
Functors and Monads
- Functor: Defines the
fmapoperation, allowing functions to be applied to values inside an HKT context while preserving that context. - Monad: Extends the Functor capability with
bind(orflat_map), enabling the chaining of operations where each step returns a new context. This is essential for sequencing dependent causal computations.
Handling Multi-Parameter Complexity (Arity)
Types with multiple parameters, such as Result<T, E>, present a challenge for standard Monad traits that expect a single type parameter. HAFT solves this through Arity Traits (HKT2, HKT3, etc.), which allow for "partial application" at the type level by fixing certain parameters (like the Error type E) while leaving others open.
Sources: website/web/src/content/blog/en/announcement-haft-hkt.md:68-115
Integration with Uncertain Types
A significant application of HAFT is within the deep_causality_uncertain crate. To make Uncertain<T> (probabilistic computations) composable, it must implement HKT traits. However, because Uncertain<T> requires its inner type to implement ProbabilisticType, it cannot always fulfill the unconstrained generic requirements of standard HKT traits.
Implementation Strategy for Uncertain HKT
To bridge the gap, the project implements "HKT-like" traits that mirror HAFT signatures but include necessary type bounds.
- UncertainWitness: A zero-sized type stored in
src/extensions/to representUncertain<T>. - ConstTree Integration:
Uncertain<T>usesConstTree<UncertainNodeContent<T>>fromdeep_causality_astas its underlying container to facilitate pure transformations duringfmapandbindoperations. - Monadic Subtypes: This integration allows
Uncertain<T>andMaybeUncertain<T>to function as monadic subtypes within the broader Propagating Effect system.
Sources: openspec/changes/archive/spec-kit-old/008-hkt-uncertain-specs/plan.md:30-75, openspec/changes/archive/spec-kit-old/008-hkt-uncertain-specs/spec.md:50-60
Type-Encoded Effect Systems
HAFT enables advanced Type-Encoded Effect Systems, where a computation encapsulates more than just an output value. In the DeepCausality framework, an effect can encompass values, errors, logs, state history, and global configuration.
Effect5 and HKT5
The Effect5 trait maps these five distinct requirements to an HKT5 witness. This allows the core engine to perform reasoning using a Causal Monad that remains pure and testable, regardless of whether it is interacting with a simple debug log or a complex telemetry system.
| Effect Layer | Description |
|---|---|
| Value | The primary result of the computation. |
| Error | The failure modes of the process. |
| Logs | Audit trails for causal tracing. |
| State | Markovian history of the system. |
| Context | Global configuration or environmental state. |
Sources: website/web/src/content/blog/en/announcement-haft-hkt.md:120-145
Summary
Higher-Kinded Types (Haft) provide the abstract mathematical framework necessary for DeepCausality to treat various computational contexts—probabilistic, fallible, or stateful—as uniform, composable units. By utilizing the Witness Pattern and GATs, HAFT overcomes Rust's native limitations, enabling a robust monadic inference engine and type-encoded effect systems that ensure causal reasoning remains deterministic and context-aware.