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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Haskell-style functional programming for Rust.
//!
//! [`hdo!`] turns a sequence of binds into a chain of monadic operations over
//! [`Option`], [`Result`] and [`Vec`]. The README lists the combinators, type
//! classes and types that are planned.
//!
//! ```
//! use raskell::hdo;
//!
//! let result = hdo! {
//! x <- Some(10);
//! y <- Some(20);
//!
//! let sum = x + y;
//!
//! guard sum > 5;
//!
//! pure sum * 2
//! };
//!
//! assert_eq!(result, Some(60));
//! ```
//!
//! # Statements
//!
//! | Syntax | Meaning |
//! | --- | --- |
//! | `pattern <- expression;` | Bind. Short-circuits on `None` / `Err`, iterates over a `Vec`. |
//! | `pattern <-? expression;` | Bind a `Result`, converting the error through [`From`]. |
//! | `pattern <- expression throw error;` | Bind a `Result` whose pattern may fail to match. |
//! | `pattern: Type <- expression;` | Bind with the bound value's type spelled out. |
//! | `guard condition;` | Yield `None` (or drop the list element) unless `condition` holds. |
//! | `guard condition throw error;` | Yield `Err(error)` unless `condition` holds. |
//! | `let pattern[: Type] = expression;` | Plain `let`, no monad involved. |
//! | `expression;` | An action, sequenced like a bind whose value is discarded. |
//! | `pure expression` | Lifts a plain value into the block's monad. Must come last. |
//! | `expression` | A final expression without `;`, already monadic. Must come last. |
//!
//! # The block's result
//!
//! A block ends either in `pure value`, which lifts a plain value into the
//! block's monad, or in a final expression without a semicolon, which is
//! already an [`Option`], [`Result`] or [`Vec`] and is returned as it is:
//!
//! ```
//! use raskell::hdo;
//!
//! let lifted: Option<i32> = hdo! {
//! x <- Some(10);
//!
//! pure x + 1
//! };
//!
//! let monadic: Option<i32> = hdo! {
//! x <- Some(10i32);
//!
//! x.checked_add(1)
//! };
//!
//! assert_eq!(lifted, monadic);
//! ```
//!
//! The `;` is what tells the two apart: a last statement that keeps it is an
//! action, which leaves the block without a result and fails to compile.
//!
//! # Errors of different types
//!
//! Plain `<-` keeps the error type as it is. Use `<-?` to convert it through
//! [`From`], the way `?` does:
//!
//! ```
//! use raskell::hdo;
//!
//! #[derive(Debug, PartialEq)]
//! struct Inner;
//!
//! #[derive(Debug, PartialEq)]
//! struct Outer;
//!
//! impl From<Inner> for Outer {
//! fn from(_: Inner) -> Self {
//! Outer
//! }
//! }
//!
//! fn inner() -> Result<i32, Inner> {
//! Err(Inner)
//! }
//!
//! let result: Result<i32, Outer> = hdo! {
//! x <-? inner();
//!
//! pure x * 2
//! };
//!
//! assert_eq!(result, Err(Outer));
//! ```
//!
//! # Refutable patterns
//!
//! A refutable pattern filters an `Option` block:
//!
//! ```
//! use raskell::hdo;
//!
//! let result = hdo! {
//! Some(x) <- Some(Some(10));
//!
//! pure x * 2
//! };
//!
//! assert_eq!(result, Some(20));
//! ```
//!
//! In a `Result` block there is no obvious error to produce, so `throw` is
//! required:
//!
//! ```
//! use raskell::hdo;
//!
//! #[derive(Debug, PartialEq)]
//! enum Error {
//! Missing,
//! }
//!
//! let result: Result<i32, Error> = hdo! {
//! Some(x) <- Ok(None::<i32>) throw Error::Missing;
//!
//! pure x * 2
//! };
//!
//! assert_eq!(result, Err(Error::Missing));
//! ```
//!
//! # Lists
//!
//! A `Vec` bind is a list comprehension, and `guard` filters it:
//!
//! ```
//! use raskell::hdo;
//!
//! let pairs: Vec<(i32, i32)> = hdo! {
//! x <- vec![1, 2, 3];
//! y <- vec![10, 20];
//!
//! guard x + y > 12;
//!
//! pure (x, y)
//! };
//!
//! assert_eq!(pairs, vec![(1, 20), (2, 20), (3, 10), (3, 20)]);
//! ```
//!
//! # Type annotations
//!
//! Rust infers the bound value's type from the expression. When the expression
//! carries no type (`Err(..)` says nothing about the `Ok` side), annotate the
//! pattern:
//!
//! ```
//! use raskell::hdo;
//!
//! #[derive(Debug, PartialEq)]
//! enum Error {
//! Failed,
//! Missing,
//! }
//!
//! let result: Result<i32, Error> = hdo! {
//! Some(x): Option<i32> <- Err(Error::Failed) throw Error::Missing;
//!
//! pure x * 2
//! };
//!
//! assert_eq!(result, Err(Error::Failed));
//! ```
//!
//! # Actions
//!
//! A bare statement expression is sequenced, not discarded, so anything
//! monadic in statement position short-circuits the block:
//!
//! ```
//! use raskell::hdo;
//!
//! #[derive(Debug, PartialEq)]
//! struct Error;
//!
//! fn check() -> Result<(), Error> {
//! Err(Error)
//! }
//!
//! let result: Result<i32, Error> = hdo! {
//! check();
//!
//! pure 1
//! };
//!
//! assert_eq!(result, Err(Error));
//! ```
//!
//! Bind it to `_` with a plain `let` to opt out:
//!
//! ```
//! # use raskell::hdo;
//! let mut empty: Vec<i32> = Vec::new();
//!
//! let result = hdo! {
//! x <- Some(10);
//!
//! let _ = empty.pop();
//!
//! pure x
//! };
//!
//! assert_eq!(result, Some(10));
//! ```
//!
//! # Async
//!
//! Wrapping the statements in `async { .. }` expands the block into an
//! `async move` block,
//! so `.await` is allowed anywhere inside it: in bind expressions, `guard`
//! conditions and `pure` alike. The result is a plain future: `.await` it, or
//! spawn it.
//!
//! ```
//! use raskell::hdo;
//!
//! #[derive(Debug, PartialEq)]
//! enum ApiError {
//! Store,
//! TooSmall,
//! }
//!
//! #[derive(Debug, PartialEq)]
//! struct StoreError;
//!
//! impl From<StoreError> for ApiError {
//! fn from(_: StoreError) -> Self {
//! ApiError::Store
//! }
//! }
//!
//! async fn fetch(value: i32) -> Result<i32, ApiError> {
//! Ok(value)
//! }
//!
//! async fn from_store() -> Result<i32, StoreError> {
//! Err(StoreError)
//! }
//!
//! let block = hdo!(async {
//! x <- fetch(10).await;
//! y <-? from_store().await;
//!
//! guard x > 5 throw ApiError::TooSmall;
//!
//! pure x + y
//! });
//!
//! let result: Result<i32, ApiError> = pollster::block_on(block);
//!
//! assert_eq!(result, Err(ApiError::Store));
//! ```
//!
//! A final expression ends an `async` block the same way, `.await` included:
//!
//! ```
//! use raskell::hdo;
//!
//! async fn fetch(value: i32) -> Option<i32> {
//! Some(value)
//! }
//!
//! let block = hdo!(async {
//! x <- fetch(10).await;
//!
//! fetch(x * 2).await
//! });
//!
//! assert_eq!(pollster::block_on(block), Some(20));
//! ```
//!
//! Futures are not awaited implicitly: write the `.await` yourself. The block's
//! type has to be known, either from an annotation on the awaited value or from
//! the enclosing function's signature.
//!
//! # Limitations
//!
//! * An `async` block binds [`Option`] and [`Result`] only: a list bind cannot
//! short-circuit a future.
//! * Lists are bound as [`Vec`]; other iterators need `.collect()` first.
// Lets macro expansion use `::raskell` paths inside this crate as well.
extern crate self as raskell;
/// The common imports.
pub use hdo;