Skip to main content

khive_fold/
lib.rs

1//! khive-fold: Cognitive primitives — Fold, Anchor, Objective, Selector.
2//!
3//! Four cognitive primitives that form the "paper-folding" operation:
4//!
5//! - **Fold**: `entries → derived state` (deterministic reduce)
6//! - **Anchor**: causal graph traversal (provenance chains)
7//! - **Objective**: score candidates and select best
8//! - **Selector**: budget-constrained pack (many → subset)
9//!
10//! Plus deterministic ordering primitives, composition combinators,
11//! and common strategies (Recency, Relevance, Weighted, etc.).
12//!
13//! # Quick Start
14//!
15//! ```
16//! use khive_fold::{fold_fn, Fold, FoldContext};
17//!
18//! let counter = fold_fn(
19//!     |_ctx| 0usize,
20//!     |count, _entry: &i32, _ctx| count + 1,
21//! );
22//!
23//! let entries = [1, 2, 3, 4, 5];
24//! let result = counter.derive(entries.iter(), &FoldContext::new());
25//! assert_eq!(result.state, 5);
26//! ```
27
28// ── Core fold ───────────────────────────────────────────────────────────
29
30mod compose;
31mod context;
32mod error;
33mod fold;
34mod result;
35
36pub use compose::{filter, map, DualFold, FilterFold, MapFold, SequentialFold};
37pub use context::{FoldContext, SharedJson};
38pub use error::{FoldError, FoldResult, FoldResult as FoldResultType};
39pub use fold::{
40    fold_fn, AnyFold, BoxedFold, CommonFold, CommonFoldState, CountFold, FilterCountFold, FnFold,
41    Fold, FoldFailure, SumI64Fold, TryFold,
42};
43pub use result::FoldOutcome;
44
45// ── Anchor primitive ────────────────────────────────────────────────────
46
47pub mod anchor;
48
49pub use anchor::{Anchor, AnchorGraph, AnchorRef, BfsAnchor};
50
51// ── Selector primitive ──────────────────────────────────────────────────
52
53pub mod selector;
54
55pub use selector::{GreedySelector, Selector, SelectorInput, SelectorOutput, SelectorWeights};
56
57// ── Objective primitive ─────────────────────────────────────────────────
58
59pub mod objective;
60pub mod ordering;
61
62pub use khive_score::{cmp_asc_then_id, cmp_desc_then_id, DeterministicScore};
63pub use objective::builtin::{
64    FirstMatchObjective, HasImportance, HasTimestamp, ImportanceObjective, MaxScoreObjective,
65    RecencyObjective, RelevanceObjective, ThresholdObjective,
66};
67pub use objective::compose::{
68    ConsensusObjective, NegateObjective, PriorityObjective, ScaleObjective, UnionObjective,
69    WeightedObjective,
70};
71pub use objective::error::{ObjectiveError, ObjectiveResult};
72pub use objective::{objective_fn, DeterministicObjective, Objective, ObjectiveContext, Selection};
73pub use ordering::{
74    canonical_f32, canonical_f64, cmp_asc_score_then_id, cmp_desc_score_then_id, HasId, QuantKey,
75    Ranked, ScoredEntry,
76};