-
Notifications
You must be signed in to change notification settings - Fork 5
/
stm.h
248 lines (212 loc) · 5.34 KB
/
stm.h
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
#ifndef STM_H
#define STM_H
#include <tuple>
#include <iostream>
#include "context.h"
#include "stm_runtime.h"
#include "stm_free.h"
#include "stm_bind.h"
#include "tvar.h"
namespace stm
{
// TODO: optimal random time backoff
// TODO: notifications instead of time wait
template <typename A>
using STML = free::STML<A>;
template <typename A>
A atomically(Context& context, const STML<A>& stml)
{
return runSTM(context, stml);
}
template <typename A, typename B>
STML<B> bind(const STML<A> ma, const free::ArrowFunc<A, B>& f)
{
return free::bindFree(ma, f);
}
template <typename A>
STML<A> retry(const A&)
{
return free::retry<A>();
}
template <typename A>
STML<fp::Unit> retryF(const A&)
{
return free::retry<fp::Unit>();
}
template <typename A>
STML<A> pure(const A& a)
{
return free::pureF(a);
}
const STML<fp::Unit> mRetry = free::retry<fp::Unit>();
template <typename A, typename Ret>
STML<Ret> with(
const STML<A>& ma,
const std::function<Ret(A)>& f)
{
return bind<A, Ret>(ma, [=](const A& a)
{
return pure(f(a));
});
}
template <typename A>
STML<TVar<A>> newTVar(const A& val, const std::string& name = "")
{
return free::newTVar(val, name);
}
template <typename A>
TVar<A> newTVarIO(Context& context, const A& val, const std::string& name = "")
{
return atomically(context, free::newTVar<A>(val, name));
}
template <typename A>
STML<A> readTVar(const TVar<A>& tvar)
{
return free::readTVar(tvar);
}
const auto mReadTVar = [](const auto& tvar)
{
return free::readTVar(tvar);
};
template <typename A>
STML<fp::Unit> writeTVar(const TVar<A>& tvar, const A& val)
{
return free::writeTVar(tvar, val);
}
const auto writeTVarT = [](const auto& tvar)
{
return [&](const auto& val)
{
return free::writeTVar(tvar, val);
};
};
const auto writeTVarV = [](const auto& val)
{
return [&](const auto& tvar)
{
return free::writeTVar(tvar, val);
};
};
template <typename A, typename Ret>
STML<Ret> withTVar(
const TVar<A>& tVar,
const std::function<Ret(A)>& f)
{
return with(readTVar(tVar), f);
}
template <typename A>
const STML<fp::Unit>
modifyTVar(const TVar<A>& tvar, const std::function<A(A)>& f)
{
return bind<A, fp::Unit>(readTVar(tvar), [=](const A& val)
{
return writeTVar(tvar, f(val));
});
}
template <typename A>
STML<fp::Unit> modifyTVarT(const TVar<A>& tvar)
{
return [&](const auto& f)
{
auto m = free::readTVar(tvar);
return bind(m, [=](const auto& val)
{
return free::writeTVar(tvar, f(val));
});
};
}
template <typename A>
STML<fp::Unit> voided(const STML<A>& ma)
{
return bind<A, fp::Unit>(ma, [](const A&) { return pure(fp::unit); });
}
template <typename A, typename B, typename Ret>
STML<Ret> both(const STML<A>& ma,
const STML<B>& mb,
const std::function<Ret(A, B)>& f)
{
return bind<A, Ret>(ma, [=](const A& a){
return bind<B, Ret>(mb, [=](const B& b){
return pure(f(a, b));
});
});
}
template <typename A, typename B>
STML<fp::Unit> bothVoided(const STML<A>& ma,
const STML<B>& mb)
{
return both<A, B, fp::Unit>(ma, mb, [](const A&, const B&)
{
return fp::unit;
});
}
// TODO: replace by var args.
template <typename A, typename B, typename Ret>
STML<Ret> bothTVars(
const STML<TVar<A>>& ma,
const STML<TVar<B>>& mb,
const std::function<Ret(A, B)>& f)
{
return both<A, B>(bind<TVar<A>, A>(ma, mReadTVar),
bind<TVar<B>, B>(mb, mReadTVar),
f);
}
template <typename A, typename B>
STML<B> sequence(const STML<A> ma, const STML<B>& mb)
{
return both<A, B, B>(ma, mb, [](const A&, const B& b)
{
return b;
});
}
template <typename A, typename B>
STML<B> ifThenElse(const STML<A>& m,
const STML<B>& mOnTrue,
const STML<B>& mOnFalse,
const std::function<bool(A)>& condF)
{
return bind<A, B>(m, [=](const A& a) {
// std::cout << "conditional cond: " << condF(a) << std::endl;
return condF(a) ? mOnTrue : mOnFalse;
});
}
template <typename B>
STML<B> ifThenElse(const STML<bool>& m,
const STML<B>& mOnTrue,
const STML<B>& mOnFalse)
{
return ifThenElse<bool, B>(m, mOnTrue, mOnFalse, fp::id);
}
// Use these combinators with care. Prefer ifThenElse instead of both when and unless.
template <typename B>
STML<fp::Unit> when(const STML<bool>& ma, const STML<B>& mb)
{
return bind<bool, fp::Unit>(ma, [=](bool cond) {
// std::cout << "when cond: " << cond << std::endl;
return cond
? voided<B>(mb)
: pure(fp::unit);
});
}
template <typename B>
STML<fp::Unit> unless(const STML<bool>& ma, const STML<B>& mb)
{
return bind<bool, fp::Unit>(ma, [=](bool cond) {
// std::cout << "unless cond: " << cond << std::endl;
return cond
? pure(fp::unit)
: voided<B>(mb);
});
}
template <typename A>
STML<A> modifyTVarRet(const TVar<A>& tvar, const std::function<A(A)>& f)
{
auto m = readTVar(tvar);
return bind<A, A>(m, [=](const A& i)
{
auto newResult = f(i);
return sequence(writeTVar(tvar, newResult), pure(newResult));
});
}
} // namespace stm
#endif // STM_H