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
use crate::boolean::Boolean;
use std::marker::PhantomData;

// functor

/// Represents an applicable unit that takes input and produces output.
pub trait Functor<Input> {
    type Output;
}

pub type ApplyFunctor<Func, Input> = <Func as Functor<Input>>::Output;

// predicate functor

/// A [Functor] that outputs [Boolean].
pub trait Predicate<Input>
where
    Self: Functor<Input>,
    Self::Output: Boolean,
{
}

/// Composes two functors from `Rhs` to `Lhs`.
pub struct Compose<Lhs, Rhs> {
    _phantom: PhantomData<(Lhs, Rhs)>,
}

impl<Input, Lhs, Rhs> Functor<Input> for Compose<Lhs, Rhs>
where
    Lhs: Functor<ApplyFunctor<Rhs, Input>>,
    Rhs: Functor<Input>,
{
    type Output = ApplyFunctor<Lhs, ApplyFunctor<Rhs, Input>>;
}

/// An identity [Functor].
pub struct IdentityFunctor {}

impl<Input> Functor<Input> for IdentityFunctor {
    type Output = Input;
}

/// A [Functor] that applies `Func` to `(Lhs, input)` type.
pub struct LeftComposeFunctor<Lhs, Func> {
    _phantom: PhantomData<(Lhs, Func)>,
}

impl<Lhs, Rhs, Func> Functor<Rhs> for LeftComposeFunctor<Lhs, Func>
where
    Func: Functor<(Lhs, Rhs)>,
{
    type Output = ApplyFunctor<Func, (Lhs, Rhs)>;
}

/// A [Functor] that applies `Func` to `(input, Rhs)` type.
pub struct RightComposeFunctor<Rhs, Func> {
    _phantom: PhantomData<(Rhs, Func)>,
}

impl<Lhs, Rhs, Func> Functor<Lhs> for RightComposeFunctor<Rhs, Func>
where
    Func: Functor<(Lhs, Rhs)>,
{
    type Output = ApplyFunctor<Func, (Lhs, Rhs)>;
}