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
//! This module contains Purescript-inspired effects monads for rust
//!
//! Here, an effect is defined an evaluatable function.
#![no_std]
#![feature(fn_traits, unboxed_closures)]

#[cfg(test)]
#[macro_use]
extern crate std;

#[macro_export]
macro_rules! effect_map {
    ( $e:expr ) => {
        move || $e
    };
    ( $b:block ) => {
        move || $b
    };
}

/// Helper enum for acting as a resolve function.
///
/// Ideally, we would use a closure instead of this type, but this type exists
/// as a workaround alternative to avoid using boxed closures
pub enum ResolveFn<T> {
    Const(T),
}

impl<T, Args> FnOnce<Args> for ResolveFn<T> {
    type Output = T;

    #[inline(always)]
    extern "rust-call" fn call_once(self, _: Args) -> Self::Output {
        use ResolveFn::Const;

        match self {
            Const(v) => v,
        }
    }
}

impl<T> From<T> for ResolveFn<T> {
    fn from(v: T) -> Self {
        use ResolveFn::Const;
        Const(v)
    }
}

/// Monad trait for effect functions
pub trait EffectMonad<A>: Sized {
    /// Sequentially composes two effect functions, passing
    /// the output of the first to the input of the second
    fn bind<B, Eb, F>(self, f: F) -> BoundEffect<Self, F>
        where Eb: FnOnce() -> B,
              F: FnOnce(A) -> Eb;

    /// Sequentially composes the two effects, while ignoring the return values
    /// of the effects. Similar to the `>>` function in Haskell, but without
    /// returning the value of the second Monad.
    ///
    /// Shorthand for
    /// ```rust
    /// effectMonad.bind(|_| someOtherEffectMonad);
    /// ```
    #[inline(always)]
    fn bind_ignore_contents<B, Eb>(self, eb: Eb) -> BoundEffect<Self, ResolveFn<Eb>>
        where Eb: FnOnce() -> B,
    {
        self.bind(eb.into())
    }
}

impl<T, A> EffectMonad<A> for T
    where T: FnOnce() -> A,
{
    #[inline(always)]
    fn bind<B, Eb, F>(self, f: F) -> BoundEffect<Self, F>
        where Eb: FnOnce() -> B,
              F: FnOnce(A) -> Eb,
    {
        bind_effects(self, f)
    }
}

/// A struct representing two bound effects. Ideally, we would be able to a
/// closure here, but that's not possible without returning a boxed version of
/// the closure, which we don't want to do.
pub struct BoundEffect<Ea, F> {
    ea: Ea,
    f: F,
}

impl<A, B, Ea, Eb, F> FnOnce<()> for BoundEffect<Ea, F>
    where Ea: FnOnce() -> A,
          Eb: FnOnce() -> B,
          F: FnOnce(A) -> Eb,
{
    type Output = B;
    extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
        // This is for readability... hopefully it'll be optimized out
        let a_result = (self.ea)();
        (self.f)(a_result)()
    }
}

fn bind_effects<A, B, Ea, Eb, F>(first: Ea, f: F) -> BoundEffect<Ea, F>
    where Ea: FnOnce() -> A,
          Eb: FnOnce() -> B,
          F: FnOnce(A) -> Eb,
{
    BoundEffect {
        ea: first,
        f: f,
    }
}

#[cfg(test)]
mod public_test {
    use super::*;

    #[test]
    fn effect_monad_bind_performs() {
        let mut x: isize = 0;
        let px = &mut x as *mut isize;
        (|| unsafe {
            *px += 2;
        }).bind_ignore_contents(|| unsafe {
            *px -= 1;
        })();
        assert_eq!(x, 1);
    }

    #[test]
    fn effect_monad_bind_performs_sequentially() {
        let mut x: isize = 3;
        let px = &mut x as *mut isize;
        (|| unsafe {
            *px *= 2;
        }).bind_ignore_contents(|| unsafe {
            *px -= 1;
        })();
        assert_eq!(x, 5);
    }

    #[test]
    fn effect_monad_bind_binds() {
        let mut x: isize = 0;
        let px = &mut x as *mut isize;
        (|| unsafe {
            *px *= 2;
            42
        }).bind(|a: isize| {
            move || unsafe {
                *px = a
            }
        })();
        assert_eq!(x, 42);
    }

    #[test]
    fn println_can_be_mapped_as_effect() {
        effect_map!(println!("hello")).bind_ignore_contents(effect_map!(println!("goodbye")))();
    }

    #[test]
    fn effect_map_performs_effect() {
        let mut x: isize = 0;
        {
            let px = &mut x;
            effect_map!(*px += 1)();
        }
        assert_eq!(x, 1);
    }

    #[test]
    fn effect_can_implicitly_borrow() {
        let mut x = 1;
        {
            (|| {
                x += 5;
            })();
        }
        assert_eq!(x, 6);
    }

    #[test]
    fn effect_map_compiles_block() {
        let mut x: isize =  0;
        {
            let px = &mut x;
            effect_map!({
                *px = 42;
            })()
        }
        assert_eq!(x, 42);
    }

    #[test]
    fn effect_monad_bind_safely_chains_state() {
        let mut x: isize = 0;
        {
            let px = &mut x;
            (effect_map!({
                *px = 6;
                px
            })).bind(|px| effect_map!(*px += 1))();
        }
        assert_eq!(x, 7);
    }
}

// It's OK for the code in the following tests to be "unsafe" becuase we know
// that only one of the closures will ever be executing at once.
//
// The REAL way to implement this pattern would be with a state monad rather
// than an effect monad

#[test]
fn bind_effect_binds() {
    let mut x: isize = 0;
    let px = &mut x as *mut isize;
    bind_effects(|| unsafe {
        *px *= 2;
        42
    }, |a: isize| {
        move || unsafe {
            *px = a
        }
    })();
    assert_eq!(x, 42);
}