-
Notifications
You must be signed in to change notification settings - Fork 253
/
Trampoline.java
286 lines (243 loc) · 9.05 KB
/
Trampoline.java
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package fj.control;
import fj.*;
import fj.data.Either;
import static fj.Function.curry;
import static fj.data.Either.left;
import static fj.data.Either.right;
/**
* A Trampoline is a potentially branching computation that can be stepped through and executed in constant stack.
* It represents suspendable coroutines with subroutine calls, reified as a data structure.
*/
public abstract class Trampoline<A> {
// A Normal Trampoline is either done or suspended, and is allowed to be a subcomputation of a Codense.
// This is the pointed functor part of the Trampoline monad.
private abstract static class Normal<A> extends Trampoline<A> {
public abstract <R> R foldNormal(final F<A, R> pure, final F<P1<Trampoline<A>>, R> k);
public final <B> Trampoline<B> bind(final F<A, Trampoline<B>> f) {
return codense(this, f);
}
}
// A Codense Trampoline delimits a subcomputation and tracks its current continuation. Subcomputations are only
// allowed to be Normal, so all of the continuations accumulate on the right.
private static final class Codense<A> extends Trampoline<A> {
// The Normal subcomputation
private final Normal<Object> sub;
// The current continuation
private final F<Object, Trampoline<A>> cont;
private Codense(final Normal<Object> t, final F<Object, Trampoline<A>> k) {
sub = t;
cont = k;
}
public <R> R fold(final F<Normal<A>, R> n,
final F<Codense<A>, R> gs) {
return gs.f(this);
}
// The monadic bind constructs a new Codense whose subcomputation is still {@code sub}, and Kleisli-composes the
// continuations.
public <B> Trampoline<B> bind(final F<A, Trampoline<B>> f) {
return codense(sub, o -> suspend(() -> cont.f(o).bind(f)));
}
// The resumption of a Codense is the resumption of its subcomputation. If that computation is done, its result
// gets shifted into the continuation.
public Either<P1<Trampoline<A>>, A> resume() {
return left(sub.resume().either(p -> p.map(ot ->
ot.fold(
o -> o.foldNormal(cont, t -> t._1().bind(cont)),
c -> codense(c.sub, o -> c.cont.f(o).bind(cont))
)
), o -> P.lazy(() -> cont.f(o))));
}
}
// A suspended computation that can be resumed.
private static final class Suspend<A> extends Normal<A> {
private final P1<Trampoline<A>> suspension;
private Suspend(final P1<Trampoline<A>> s) {
suspension = s;
}
public <R> R foldNormal(final F<A, R> pure, final F<P1<Trampoline<A>>, R> k) {
return k.f(suspension);
}
public <R> R fold(final F<Normal<A>, R> n, final F<Codense<A>, R> gs) {
return n.f(this);
}
public Either<P1<Trampoline<A>>, A> resume() {
return left(suspension);
}
}
// A pure value at the leaf of a computation.
private static final class Pure<A> extends Normal<A> {
private final A value;
private Pure(final A a) {
value = a;
}
public <R> R foldNormal(final F<A, R> pure, final F<P1<Trampoline<A>>, R> k) {
return pure.f(value);
}
public <R> R fold(final F<Normal<A>, R> n, final F<Codense<A>, R> gs) {
return n.f(this);
}
public Either<P1<Trampoline<A>>, A> resume() {
return right(value);
}
}
@SuppressWarnings("unchecked")
private static <A, B> Codense<B> codense(final Normal<A> a, final F<A, Trampoline<B>> k) {
return new Codense<>((Normal<Object>) a, (F<Object, Trampoline<B>>) k);
}
/**
* @return The first-class version of {@code pure}.
*/
public static <A> F<A, Trampoline<A>> pure() {
return Trampoline::pure;
}
/**
* Constructs a pure computation that results in the given value.
*
* @param a The value of the result.
* @return A trampoline that results in the given value.
*/
public static <A> Trampoline<A> pure(final A a) {
return new Pure<>(a);
}
/**
* Suspends the given computation in a thunk.
*
* @param a A trampoline suspended in a thunk.
* @return A trampoline whose next step runs the given thunk.
*/
public static <A> Trampoline<A> suspend(final F0<Trampoline<A>> a) {
return new Suspend<>(P.lazy(a));
}
/**
* Suspends the given computation in a thunk.
*
* @param a A trampoline suspended in a thunk.
* @return A trampoline whose next step runs the given thunk.
*/
public static <A> Trampoline<A> suspend(final P1<Trampoline<A>> a) {
return new Suspend<>(a);
}
/**
* @return The first-class version of {@code suspend}.
*/
public static <A> F<P1<Trampoline<A>>, Trampoline<A>> suspend_() {
return Trampoline::suspend;
}
protected abstract <R> R fold(final F<Normal<A>, R> n, final F<Codense<A>, R> gs);
/**
* Binds the given continuation to the result of this trampoline.
*
* @param f A function that constructs a trampoline from the result of this trampoline.
* @return A new trampoline that runs this trampoline, then continues with the given function.
*/
public abstract <B> Trampoline<B> bind(final F<A, Trampoline<B>> f);
/**
* Maps the given function across the result of this trampoline.
*
* @param f A function that gets applied to the result of this trampoline.
* @return A new trampoline that runs this trampoline, then applies the given function to the result.
*/
public final <B> Trampoline<B> map(final F<A, B> f) {
return bind(Trampoline.<B>pure().o(f));
}
/**
* @return The first-class version of {@code bind}.
*/
public static <A, B> F<F<A, Trampoline<B>>, F<Trampoline<A>, Trampoline<B>>> bind_() {
return f -> a -> a.bind(f);
}
/**
* @return The first-class version of {@code map}.
*/
public static <A, B> F<F<A, B>, F<Trampoline<A>, Trampoline<B>>> map_() {
return f -> a -> a.map(f);
}
/**
* @return The first-class version of {@code resume}.
*/
public static <A> F<Trampoline<A>, Either<P1<Trampoline<A>>, A>> resume_() {
return Trampoline::resume;
}
/**
* Runs a single step of this computation.
*
* @return The next step of this compuation.
*/
public abstract Either<P1<Trampoline<A>>, A> resume();
/**
* Runs this computation all the way to the end, in constant stack.
*
* @return The end result of this computation.
*/
@SuppressWarnings("LoopStatementThatDoesntLoop")
public final A run() {
Trampoline<A> current = this;
while (true) {
final Either<P1<Trampoline<A>>, A> x = current.resume();
for (final P1<Trampoline<A>> t : x.left()) {
current = t._1();
}
for (final A a : x.right()) {
return a;
}
}
}
/**
* Performs function application within a Trampoline (applicative functor pattern).
*
* @param lf A Trampoline resulting in the function to apply.
* @return A new Trampoline after applying the given function through this Trampoline.
*/
public final <B> Trampoline<B> apply(final Trampoline<F<A, B>> lf) {
return lf.bind(this::map);
}
/**
* Binds the given function across the result of this Trampoline and the given Trampoline.
*
* @param lb A given Trampoline to bind the given function with.
* @param f The function to combine the results of this Trampoline and the given Trampoline.
* @return A new Trampoline combining the results of the two trampolines with the given function.
*/
public final <B, C> Trampoline<C> bind(final Trampoline<B> lb, final F<A, F<B, C>> f) {
return lb.apply(map(f));
}
/**
* Promotes the given function of arity-2 to a function on Trampolines.
*
* @param f The function to promote to a function on Trampolines.
* @return The given function, promoted to operate on Trampolines.
*/
public static <A, B, C> F<Trampoline<A>, F<Trampoline<B>, Trampoline<C>>> liftM2(final F<A, F<B, C>> f) {
return curry((as, bs) -> as.bind(bs, f));
}
/**
* Combines two trampolines so they run cooperatively. The results are combined with the given function.
*
* @param b Another trampoline to combine with this trampoline.
* @param f A function to combine the results of the two trampolines.
* @return A new trampoline that runs this trampoline and the given trampoline simultaneously.
*/
@SuppressWarnings("LoopStatementThatDoesntLoop")
public final <B, C> Trampoline<C> zipWith(final Trampoline<B> b, final F2<A, B, C> f) {
final Either<P1<Trampoline<A>>, A> ea = resume();
final Either<P1<Trampoline<B>>, B> eb = b.resume();
for (final P1<Trampoline<A>> x : ea.left()) {
for (final P1<Trampoline<B>> y : eb.left()) {
F<Trampoline<A>, F<Trampoline<B>, Trampoline<C>>> z = ta -> tb -> suspend(() -> ta.zipWith(tb, f));
return suspend(x.bind(y, z));
}
for (final B y : eb.right()) {
return suspend(x.map(ta -> ta.map(f.flip().f(y))));
}
}
for (final A x : ea.right()) {
for (final B y : eb.right()) {
return suspend(() -> pure(f.f(x, y)));
}
for (final P1<Trampoline<B>> y : eb.left()) {
return suspend(y.map(liftM2(f.curry()).f(pure(x))));
}
}
throw Bottom.error("Match error: Trampoline is neither done nor suspended.");
}
}