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
162
163
164
165
//! # MonadPlus
//!
//! The `MonadPlus` module provides a trait definition for monads that support
//! choice operations, extending the basic Monad trait. It's similar to Alternative
//! in Haskell, but specifically for monads.
//!
//! MonadPlus adds two key operations:
//! - `mzero`: A monad that represents failure or an empty computation
//! - `mplus`: An operation to combine two monads of the same type
//!
//! # Laws
//!
//! For a valid MonadPlus implementation, the following laws should hold:
//!
//! 1. Identity: `mzero().mplus(&x) == x` and `x.mplus(&mzero()) == x`
//! - Zero is a neutral element for mplus
//!
//! 2. Associativity: `a.mplus(&b).mplus(&c) == a.mplus(&b.mplus(&c))`
//! - The mplus operation is associative
//!
//! 3. Left Zero: `mzero().bind(f) == mzero()`
//! - Zero is annihilator for bind
//!
//! 4. Right Zero: `x.bind(|_| mzero()) == mzero()`
//! - Zero is annihilator for bind in the other direction
//!
//! 5. Left Distribution: `a.mplus(&b).bind(f) == a.bind(f).mplus(&b.bind(f))`
//! - Bind distributes over mplus
//!
//! # Examples
//!
//! ```rust
//! use rustica::traits::monad_plus::MonadPlus;
//! use rustica::traits::monad::Monad;
//!
//! // Option is a MonadPlus where None is mzero and mplus takes the first Some or None
//! let opt1: Option<i32> = Some(42);
//! let opt2: Option<i32> = None;
//! let opt3: Option<i32> = Some(7);
//!
//! // Using mplus directly
//! assert_eq!(Option::<i32>::mzero::<i32>(), None);
//! assert_eq!(opt1.mplus(&opt2), Some(42));
//! assert_eq!(opt2.mplus(&opt3), Some(7));
//! assert_eq!(opt2.mplus(&opt2), None);
//!
//! // MonadPlus with bind (>>=)
//! let result = opt1.bind(|x| {
//! if *x > 40 {
//! Option::<i32>::mzero::<i32>()
//! } else {
//! Some(*x * 2)
//! }
//! });
//! assert_eq!(result, None);
//! ```
use Debug;
use crateMonad;
/// A trait for monads that can represent a choice between multiple values.
///
/// MonadPlus extends the basic Monad trait with operations for alternative
/// computations, providing a "zero" element (mzero) and a way to combine
/// alternatives (mplus).
///
/// # Type Parameters
/// * None explicit, but the implementing type must be a Monad
///
/// # Laws
/// For a valid MonadPlus implementation, the following laws must hold:
///
/// 1. Identity: `mzero().mplus(&x) == x` and `x.mplus(&mzero()) == x`
/// - Zero is a neutral element for mplus
///
/// 2. Associativity: `a.mplus(&b).mplus(&c) == a.mplus(&b.mplus(&c))`
/// - The mplus operation is associative
///
/// 3. Left Zero: `mzero().bind(f) == mzero()`
/// - Zero is annihilator for bind
///
/// 4. Right Zero: `x.bind(|_| mzero()) == mzero()`
/// - Zero is annihilator for bind in the other direction
///
/// 5. Left Distribution: `a.mplus(&b).bind(f) == a.bind(f).mplus(&b.bind(f))`
/// - Bind distributes over mplus