1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* SPDX-License-Identifier: MIT
* Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
*/
//! The value-level `Arrow` algebra: a strong category for wiring operators together.
//!
//! Where [`Morphism`](crate::Morphism) is the *witness-level* interface (identity +
//! application, but no composition under the no-`dyn` policy), this module provides the
//! *value-level* algebra where composition is **total**: every combinator returns a new
//! concrete `Arrow`-implementing type, so composites compose.
//!
//! - **Category:** [`Id`], [`Lift`] (lift a function), [`Compose`] (`f >>> g`).
//! - **Strength (the monoidal product):** [`First`], [`Second`], [`Split`] (`***`),
//! [`Fanout`] (`&&&`) — the operation the causal monad's `bind` cannot express.
//! - **Builder:** [`arrow`] + [`ArrowBuilder`] hide the combinator types behind a fluent
//! chain (the textual form of a wiring diagram).
//!
//! # Intellectual lineage
//!
//! This design is the convergence of several lineages put together. In chronological order:
//!
//! - **1972 — Defunctionalization.** John C. Reynolds, *"Definitional Interpreters for
//! Higher-Order Programming Languages,"* Proc. ACM Annual Conference (1972; reprinted in
//! Higher-Order and Symbolic Computation 11(4), 1998). Represent higher-order / closure
//! values as first-order data so they can be named and composed without indirection —
//! why each combinator here is a concrete generic struct rather than a `Box<dyn Fn>`.
//! - **1986 — Typestate.** Robert E. Strom & Shaula Yemini, *"Typestate: A Programming
//! Concept for Enhancing Software Reliability,"* IEEE Transactions on Software
//! Engineering SE-12(1). Encode an object's state in its type — what lets
//! [`ArrowBuilder`] thread the growing arrow type through `Self`, hidden from the caller.
//! - **1991 — String diagrams.** André Joyal & Ross Street, *"The Geometry of Tensor
//! Calculus, I,"* Advances in Mathematics 88(1). The graphical calculus of monoidal
//! categories: a fluent chain is the textual form of a wiring diagram.
//! - **1997 — Freyd / premonoidal categories.** John Power & Edmund Robinson,
//! *"Premonoidal categories and notions of computation,"* Mathematical Structures in
//! Computer Science 7(5). The categorical home of arrows; *strength* is the name for
//! [`First`] / [`Split`].
//! - **2000 — Arrows.** John Hughes, *"Generalising Monads to Arrows,"* Science of
//! Computer Programming 37(1–3). The Arrow abstraction and its combinators — `arr`,
//! `>>>`, `first`, `***`, `&&&` — which appear here as [`Lift`], [`Compose`], [`First`] /
//! [`Second`], [`Split`], [`Fanout`].
//! - **2001 — Arrow notation.** Ross Paterson, *"A New Notation for Arrows,"* ICFP 2001.
//! The `proc` surface syntax over arrow combinators — the ancestor of the
//! builder-as-syntax idea ([`arrow`] / [`ArrowBuilder`]).
//! - **2009 — Finally tagless.** Jacques Carette, Oleg Kiselyov & Chung-chieh Shan,
//! *"Finally Tagless, Partially Evaluated,"* Journal of Functional Programming 19(5)
//! (earlier APLAS 2007). Encode a typed DSL's terms as trait methods so each term is a
//! value whose type is built up — no GADT/AST — exactly the combinators-as-generic-
//! structs encoding used here. (Related: Oliveira & Cook, *"Extensibility for the
//! Masses: Practical Extensibility with Object Algebras,"* ECOOP 2012.)
//! - **Rust `std::iter::Iterator` (the canonical embodiment).** `Map<I, F>`,
//! `Filter<I, P>`, `Zip<A, B>` are generic adapter structs, and `xs.map(f).filter(p)`
//! builds a monomorphized nested type the caller never names. That is precisely the
//! "builder hides the combinator types" property — realized here for the *full* Arrow
//! (sequential `>>>` **and** the monoidal product `***` / `&&&`), not just `map`/`filter`.
//! `Future` combinators and `tower::Service` + `Layer` are the same encoding.
//!
//! The synthesis specific to this crate: apply the `Iterator`-adapter encoding to the
//! *complete* strong category and use the fluent builder as the camouflage layer for the
//! Causal Arrow generalization — a well-typed fluent chain *is* a string diagram. See
//! `openspec/notes/arrow/causal-arrow-generalization.md` §8 and
//! `openspec/notes/arrow/causal-process-builder.md`.
pub use EndoArrow;
pub use ;
pub use Compose;
pub use Fanout;
pub use First;
pub use Id;
pub use Lift;
pub use Second;
pub use Split;
/// A value-level arrow `In → Out`: a runnable, composable transformation.
///
/// # Category Theory
///
/// `Arrow` is a **strong category** (Hughes' Arrow): `Id`/`Compose` give the category,
/// the `first`/`split` combinators give the monoidal **strength**, and `fanout` gives the
/// diagonal. Unlike the witness-level [`Morphism`](crate::Morphism) — which cannot host
/// composition, because composing two closures yields an unnameable type and `Box<dyn Fn>`
/// is forbidden — every combinator here returns a *new concrete type*, so **composition is
/// total** and everything is monomorphized (zero-cost, no `dyn`, no macros).
///
/// The combinator methods are provided; an implementor supplies only `In`, `Out`, and
/// `run`. `run` takes `&self`, so an arrow is reusable.