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
use std::iter::{IntoIterator, Iterator, FlatMap};
use std::collections::{LinkedList, VecDeque};
pub trait Bind: IntoIterator {
fn bind<U, F>(self, f: F) -> FlatMap<Self::IntoIter, U, F>
where
F: Fn(Self::Item) -> U,
U: IntoIterator,
Self: Sized {
self.into_iter().flat_map( f)
}
}
impl<R> Bind for R where R: IntoIterator {}
pub trait Monad: Bind {
fn pure(x: Self::Item) -> Self;
}
impl<T> Monad for Option<T> {
fn pure(x: T) -> Self {
Some(x)
}
}
impl<T,E> Monad for Result<T,E>{
fn pure(x: T) -> Self {
Ok(x)
}
}
impl<T> Monad for Vec<T>{
fn pure(x: T) -> Self {
let mut v = Self::new();
v.push(x);
v
}
}
impl<T> Monad for LinkedList<T>{
fn pure(x: T) -> Self {
let mut v = Self::new();
v.push_front(x);
v
}
}
impl<T> Monad for VecDeque<T>{
fn pure(x: T) -> Self {
let mut v = Self::new();
v.push_front(x);
v
}
}
pub trait MZero: Monad {
fn mzero() -> Self;
}
impl<T> MZero for Option<T> {
fn mzero() -> Self {None}
}
impl<T> MZero for Vec<T> {
fn mzero() -> Self {Self::new()}
}
impl<T> MZero for LinkedList<T> {
fn mzero() -> Self {Self::new()}
}
impl<T> MZero for VecDeque<T> {
fn mzero() -> Self {Self::new()}
}
pub trait MPlus: MZero {
fn mplus(&mut self, _: &mut Self) ;
}
impl<T> MPlus for Vec<T> {
fn mplus(&mut self, other: &mut Self) {
self.append( other);
}
}
impl<T> MPlus for LinkedList<T> {
fn mplus(&mut self, other: &mut Self) {
self.append( other);
}
}
impl<T> MPlus for VecDeque<T> {
fn mplus(&mut self, other: &mut Self) {
self.append( other);
}
}
#[macro_export]
macro_rules! mdo {
(pure $e:expr ) => [Option::pure($e)];
(let $v:ident = $e:expr ; $($rest:tt)*) => [Option::pure($e).bind( move |$v| { mdo!($($rest)*)} )];
(guard $boolean:expr ; $($rest:tt)*) => [(if $boolean {Some(())} else {None}).bind( move |_| { mdo!($($rest)*)} )];
(_ <- $monad:expr ; $($rest:tt)* ) => [($monad).bind( move |_| { mdo!($($rest)*)} )];
(&$v:ident <- $monad:expr ; $($rest:tt)* ) => [($monad).bind( move |&$v| { mdo!($($rest)*)} )];
($v:ident <- pure $e:expr ; $($rest:tt)* ) => [Option::pure($e).bind( move |$v| { mdo!($($rest)*)} )];
($v:ident <- $monad:expr ; $($rest:tt)* ) => [($monad).bind( move |$v| { mdo!($($rest)*)} )];
($monad:expr ) => [$monad];
}
#[cfg(test)]
mod tests {
use crate::monad::{Bind, Monad};
use quickcheck::quickcheck;
quickcheck!{
fn prop_monad_comprehension_vs_iteration( xs: Vec<i32>) -> bool {
let ys = mdo!{
&v <- &xs;
guard v < 4;
pure v * 2
}.collect::<Vec<i32>>();
let zs = (&xs).into_iter().filter(|&v| v < &4).map(|v| v*2).collect::<Vec<i32>>();
ys == zs
}
}
}