forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional.fe
More file actions
40 lines (35 loc) · 1.21 KB
/
Copy pathfunctional.fe
File metadata and controls
40 lines (35 loc) · 1.21 KB
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
/// Core functional abstractions built around higher-kinded types.
///
/// This module defines:
/// - `Fn<T, U>`: a trait representing callable values.
/// - `Functor`: type constructors that can be mapped over.
/// - `Applicative`: type constructors that support `pure` and `<*>`.
/// - `Monad`: type constructors that support `bind`/`flat_map`.
///
/// These traits are designed for types of kind `* -> *`, such as `Option`
/// or partially applied types like `Result<E>`.
/// A callable value from `T` to `U`.
pub trait Fn<T, U> {
fn call(self, _ value: own T) -> U
}
/// A type constructor that can be mapped over.
pub trait Functor
where Self: * -> *
{
fn map<T, U, F: Fn<T, U>>(self: own Self<T>, _ func: F) -> Self<U>
}
/// A type constructor that supports `pure` and function application.
pub trait Applicative: Functor
where Self: * -> *
{
fn pure<T>(_ value: own T) -> Self<T>
fn ap<T, U, F: Fn<T, U>>(self: own Self<F>, _ value: own Self<T>) -> Self<U>
}
/// A monad in the sense of functional programming.
///
/// `bind` is also known as `flat_map` in some ecosystems.
pub trait Monad: Applicative
where Self: * -> *
{
fn bind<T, U, F: Fn<T, Self<U>>>(self: own Self<T>, _ func: F) -> Self<U>
}