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
/*
* SPDX-License-Identifier: MIT
* Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
*/
use crate::;
/// The `Traversable` trait abstracts over data structures that can be "traversed"
/// or "sequenced" in a way that preserves effects. It combines the capabilities
/// of `Functor` (mapping over elements) and `Foldable` (reducing to a single value).
///
/// The core operation of `Traversable` is `sequence`, which takes a structure
/// containing monadic/applicative values (`F<M<A>>`) and "flips" it inside out
/// to produce a monadic/applicative value containing the structure (`M<F<A>>`).
///
/// # Intuition & Analogy
///
/// Imagine you have a list of optional values (`Vec<Option<i32>>`). If any of the
/// options in the list are `None`, you might want the whole operation to fail and
/// just yield `None`. Otherwise, you want `Some` list of integers (`Option<Vec<i32>>`).
/// `sequence` achieves exactly this.
///
/// - `Vec<Option<A>>` -> `Option<Vec<A>>`
/// - `Vec<Result<A, E>>` -> `Result<Vec<A>, E>`
/// - `Option<Result<A, E>>` -> `Result<Option<A>, E>`
///
/// `Traversable` allows you to abstract over these kinds of transformations. It's
/// particularly useful for collecting errors, propagating "empty" states, or
/// accumulating results across collections of effectful computations.
///
/// # Laws (Informal)
///
/// `Traversable` laws are typically expressed in terms of `sequence` and `traverse`
/// (which can be defined in terms of `sequence` and `fmap`, or vice-versa). References:
/// McBride & Paterson, JFP 18(1) 2008 §3; Jaskelioff & Rypacek, MSFP 2012.
///
/// 1. **Naturality**: for every applicative morphism `phi: M -> N` (preserving `pure` and
/// `apply`), `phi(t.sequence::<M>()) == t.map(phi).sequence::<N>()`
/// (post-processing the result equals pre-processing each element).
/// 2. **Identity**: `t.map(Identity::new).sequence::<Identity>() == Identity::new(t)`
/// (sequencing at the *Identity applicative* is the identity — note: the weaker phrasing
/// `t.sequence == t.map(id).sequence` is vacuously true by the Functor law and
/// constrains nothing).
/// 3. **Composition**: `t.map(compose).sequence == t.sequence.sequence`
/// (Sequencing over a composite structure is equivalent to composing the sequenced results).
///
/// Laws are stated for pure functions; a stateful `FnMut` closure voids them.
/// Laws 1–2 are machine-checked in `lean/DeepCausalityFormal/Haft/Traversable.lean`.
///
/// # Type Parameters
///
/// * `F`: A Higher-Kinded Type (HKT) witness that represents the type constructor
/// of the traversable structure (e.g., `VecWitness`, `OptionWitness`).
/// This `F` must also be a `Functor` and `Foldable`.