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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Cooperative extension of a local context-dependent computation to a global computation.
//!
//! [`Extend`] is the dual of [`Semimonad`](crate::classes::Semimonad): where `bind` sequences
//! computations by extracting a value and feeding it to a function that produces a new context,
//! `extend` takes a function that consumes a whole context and lifts it to operate within
//! the context. In categorical terms, `extend` is co-Kleisli extension.
//!
//! This module is a port of PureScript's
//! [`Control.Extend`](https://pursuit.purescript.org/packages/purescript-control/docs/Control.Extend).
#[fp_macros::document_module]
mod inner {
use {
crate::{
classes::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for types that support co-Kleisli extension.
///
/// `Extend` is the dual of [`Semimonad`](crate::classes::Semimonad). Where
/// `bind : (A -> F<B>) -> F<A> -> F<B>` feeds a single extracted value into a
/// function that produces a new context, `extend : (F<A> -> B) -> F<A> -> F<B>`
/// feeds an entire context into a function and re-wraps the result.
///
/// `class Functor w <= Extend w`
///
/// # Laws
///
/// **Associativity:** composing two extensions is the same as extending with
/// a pre-composed function.
///
/// For any `f: F<B> -> C` and `g: F<A> -> B`:
///
/// ```text
/// extend(f, extend(g, w)) == extend(|w| f(extend(g, w)), w)
/// ```
///
/// This is dual to the associativity law for `bind`.
///
/// # Note on `LazyBrand`
///
/// `LazyBrand` cannot implement `Extend` because `Extend: Functor` and
/// `LazyBrand` cannot implement `Functor` (its `evaluate` returns `&A`,
/// not owned `A`). PureScript's `Lazy` has `Extend`/`Comonad` because GC
/// provides owned values from `force`. In this library, `ThunkBrand` fills
/// the `Functor + Extend + Comonad` role for lazy types, while `LazyBrand`
/// provides memoization via [`RefFunctor`](crate::classes::RefFunctor).
#[document_examples]
///
/// Associativity law for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// let w = vec![1, 2, 3];
/// let f = |v: Vec<i32>| v.iter().sum::<i32>();
/// let g = |v: Vec<i32>| v.len() as i32;
///
/// // extend(f, extend(g, w)) == extend(|w| f(extend(g, w)), w)
/// let lhs = extend::<VecBrand, _, _>(f, extend::<VecBrand, _, _>(g, w.clone()));
/// let rhs = extend::<VecBrand, _, _>(|w| f(extend::<VecBrand, _, _>(g, w)), w);
/// assert_eq!(lhs, rhs);
/// ```
pub trait Extend: Functor {
/// Extends a local context-dependent computation to a global computation.
///
/// Given a function that consumes an `F<A>` and produces a `B`, and a
/// value of type `F<A>`, produces an `F<B>` by applying the function in
/// a context-sensitive way.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value(s) inside the comonadic context.",
"The result type of the extension function."
)]
///
#[document_parameters(
"The function that consumes a whole context and produces a value.",
"The comonadic context to extend over."
)]
///
#[document_returns(
"A new comonadic context containing the results of applying the function."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::*,
/// types::*,
/// };
///
/// let result = IdentityBrand::extend(|id: Identity<i32>| id.0 * 2, Identity(5));
/// assert_eq!(result, Identity(10));
/// ```
fn extend<'a, A: 'a + Clone, B: 'a>(
f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>);
/// Duplicates a comonadic context, wrapping it inside another layer of the same context.
///
/// `duplicate(wa)` is equivalent to `extend(identity, wa)`. It is the dual of
/// [`join`](crate::functions::join) for monads.
///
/// Produces `F<F<A>>` from `F<A>`, embedding the original context as the inner value.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value(s) inside the comonadic context."
)]
///
#[document_parameters("The comonadic context to duplicate.")]
///
#[document_returns("A doubly-wrapped comonadic context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::*,
/// types::*,
/// };
///
/// let result = IdentityBrand::duplicate(Identity(5));
/// assert_eq!(result, Identity(Identity(5)));
/// ```
fn duplicate<'a, A: 'a + Clone>(
wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
where
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): 'a, {
Self::extend(|w| w, wa)
}
/// Extends with the arguments flipped.
///
/// A version of [`extend`](Extend::extend) where the comonadic context comes
/// first, followed by the extension function.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value(s) inside the comonadic context.",
"The result type of the extension function."
)]
///
#[document_parameters(
"The comonadic context to extend over.",
"The function that consumes a whole context and produces a value."
)]
///
#[document_returns(
"A new comonadic context containing the results of applying the function."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::*,
/// types::*,
/// };
///
/// let result = IdentityBrand::extend_flipped(Identity(5), |id| id.0 * 3);
/// assert_eq!(result, Identity(15));
/// ```
fn extend_flipped<'a, A: 'a + Clone, B: 'a>(
wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
Self::extend(f, wa)
}
/// Forwards co-Kleisli composition.
///
/// Composes two co-Kleisli functions left-to-right: first applies `f` via
/// [`extend`](Extend::extend), then applies `g` to the result. This is the
/// dual of [`compose_kleisli`](crate::functions::compose_kleisli).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value(s) inside the comonadic context.",
"The result type of the first co-Kleisli function.",
"The result type of the second co-Kleisli function."
)]
///
#[document_parameters(
"The first co-Kleisli function.",
"The second co-Kleisli function.",
"The comonadic context to operate on."
)]
///
#[document_returns(
"The result of composing both co-Kleisli functions and applying them to the context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::*,
/// types::*,
/// };
///
/// let f = |id: Identity<i32>| id.0 + 1;
/// let g = |id: Identity<i32>| id.0 * 10;
/// let result = IdentityBrand::compose_co_kleisli(f, g, Identity(5));
/// assert_eq!(result, 60);
/// ```
fn compose_co_kleisli<'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
g: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)) -> C + 'a,
wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> C {
g(Self::extend(f, wa))
}
/// Backwards co-Kleisli composition.
///
/// Composes two co-Kleisli functions right-to-left: first applies `g` via
/// [`extend`](Extend::extend), then applies `f` to the result. This is the
/// dual of [`compose_kleisli_flipped`](crate::functions::compose_kleisli_flipped).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value(s) inside the comonadic context.",
"The result type of the second co-Kleisli function (applied first).",
"The result type of the first co-Kleisli function (applied second)."
)]
///
#[document_parameters(
"The second co-Kleisli function (applied after `g`).",
"The first co-Kleisli function (applied first to the context).",
"The comonadic context to operate on."
)]
///
#[document_returns(
"The result of composing both co-Kleisli functions and applying them to the context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::*,
/// types::*,
/// };
///
/// let f = |id: Identity<i32>| id.0 * 10;
/// let g = |id: Identity<i32>| id.0 + 1;
/// let result = IdentityBrand::compose_co_kleisli_flipped(f, g, Identity(5));
/// assert_eq!(result, 60);
/// ```
fn compose_co_kleisli_flipped<'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)) -> C + 'a,
g: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> C {
f(Self::extend(g, wa))
}
}
/// Extends a local context-dependent computation to a global computation.
///
/// Free function version that dispatches to [the type class' associated function][`Extend::extend`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the comonadic context.",
"The type of the value(s) inside the comonadic context.",
"The result type of the extension function."
)]
///
#[document_parameters(
"The function that consumes a whole context and produces a value.",
"The comonadic context to extend over."
)]
///
#[document_returns("A new comonadic context containing the results of applying the function.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// types::*,
/// };
///
/// // extend on Identity: apply f to the whole container
/// let w = Identity(10);
/// let result = extend::<IdentityBrand, _, _>(|id| id.0 * 2, w);
/// assert_eq!(result, Identity(20));
///
/// // extend on Vec: apply f to each suffix
/// let v = vec![1, 2, 3];
/// let sums = extend::<VecBrand, _, _>(|s: Vec<i32>| s.iter().sum::<i32>(), v);
/// assert_eq!(sums, vec![6, 5, 3]);
/// ```
pub fn extend<'a, Brand: Extend, A: 'a + Clone, B: 'a>(
f: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
wa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
Brand::extend(f, wa)
}
/// Duplicates a comonadic context, wrapping it inside another layer of the same context.
///
/// `duplicate(wa)` is equivalent to `extend(identity, wa)`. It is the dual of
/// [`join`](crate::functions::join) for monads.
///
/// Produces `F<F<A>>` from `F<A>`, embedding the original context as the inner value.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the comonadic context.",
"The type of the value(s) inside the comonadic context."
)]
///
#[document_parameters("The comonadic context to duplicate.")]
///
#[document_returns("A doubly-wrapped comonadic context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// // duplicate on Vec produces all suffixes
/// let v = vec![1, 2, 3];
/// let d = duplicate::<VecBrand, _>(v);
/// assert_eq!(d, vec![vec![1, 2, 3], vec![2, 3], vec![3]]);
/// ```
pub fn duplicate<'a, Brand: Extend, A: 'a + Clone>(
wa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
where
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): 'a, {
Brand::duplicate(wa)
}
/// Forwards co-Kleisli composition.
///
/// Composes two co-Kleisli functions left-to-right: first applies `f` via
/// [`extend()`](crate::classes::extend::extend), then applies `g` to the result. This is the dual of
/// [`compose_kleisli`](crate::functions::compose_kleisli).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the comonadic context.",
"The type of the value(s) inside the comonadic context.",
"The result type of the first co-Kleisli function.",
"The result type of the second co-Kleisli function."
)]
///
#[document_parameters(
"The first co-Kleisli function.",
"The second co-Kleisli function.",
"The comonadic context to operate on."
)]
///
#[document_returns(
"The result of composing both co-Kleisli functions and applying them to the context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// types::*,
/// };
///
/// let f = |id: Identity<i32>| id.0 + 1;
/// let g = |id: Identity<i32>| id.0 * 10;
/// let w = Identity(5);
/// // compose_co_kleisli(f, g, w): extend f, then apply g
/// let result = compose_co_kleisli::<IdentityBrand, _, _, _>(f, g, w);
/// assert_eq!(result, 60);
/// ```
pub fn compose_co_kleisli<'a, Brand: Extend, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
g: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)) -> C + 'a,
wa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> C {
Brand::compose_co_kleisli(f, g, wa)
}
/// Backwards co-Kleisli composition.
///
/// Composes two co-Kleisli functions right-to-left: first applies `g` via
/// [`extend()`](crate::classes::extend::extend), then applies `f` to the result. This is the dual of
/// [`compose_kleisli_flipped`](crate::functions::compose_kleisli_flipped).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the comonadic context.",
"The type of the value(s) inside the comonadic context.",
"The result type of the second co-Kleisli function (applied first).",
"The result type of the first co-Kleisli function (applied second)."
)]
///
#[document_parameters(
"The second co-Kleisli function (applied after `g`).",
"The first co-Kleisli function (applied first to the context).",
"The comonadic context to operate on."
)]
///
#[document_returns(
"The result of composing both co-Kleisli functions and applying them to the context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// types::*,
/// };
///
/// let f = |id: Identity<i32>| id.0 * 10;
/// let g = |id: Identity<i32>| id.0 + 1;
/// let w = Identity(5);
/// // compose_co_kleisli_flipped(f, g, w): extend g, then apply f
/// let result = compose_co_kleisli_flipped::<IdentityBrand, _, _, _>(f, g, w);
/// assert_eq!(result, 60);
/// ```
pub fn compose_co_kleisli_flipped<'a, Brand: Extend, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)) -> C + 'a,
g: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
wa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> C {
Brand::compose_co_kleisli_flipped(f, g, wa)
}
/// Extends with the arguments flipped.
///
/// A version of [`extend()`](crate::classes::extend::extend) where the comonadic context comes first, followed
/// by the extension function. Useful for pipelines where the value is known
/// before the function.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the comonadic context.",
"The type of the value(s) inside the comonadic context.",
"The result type of the extension function."
)]
///
#[document_parameters(
"The comonadic context to extend over.",
"The function that consumes a whole context and produces a value."
)]
///
#[document_returns("A new comonadic context containing the results of applying the function.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// types::*,
/// };
///
/// let w = Identity(5);
/// let result = extend_flipped::<IdentityBrand, _, _>(w, |id| id.0 * 3);
/// assert_eq!(result, Identity(15));
/// ```
pub fn extend_flipped<'a, Brand: Extend, A: 'a + Clone, B: 'a>(
wa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
f: impl Fn(Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
Brand::extend_flipped(wa, f)
}
}
pub use inner::*;