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
/*
* 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).
///
/// 1. **Naturality**: `t.sequence.map(f) == t.map(m.map(f)).sequence`
/// (Mapping over the result is the same as mapping inside the monadic value then sequencing).
/// 2. **Identity**: `t.sequence == t.map(id).sequence`
/// (Sequencing a structure of identity monads is the structure itself).
/// 3. **Composition**: `t.map(compose).sequence == t.sequence.sequence`
/// (Sequencing over a composite structure is equivalent to composing the sequenced results).
///
/// # 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`.