forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrampoline.java
More file actions
371 lines (330 loc) · 12.2 KB
/
Trampoline.java
File metadata and controls
371 lines (330 loc) · 12.2 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package fj.control;
import fj.Bottom;
import fj.F;
import fj.F2;
import fj.P1;
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 represent 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 static abstract class Normal<A> extends Trampoline<A> {
public abstract <R> R foldNormal(final F<A, R> pure, final F<P1<Trampoline<A>>, R> k);
public <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 `sub`, and Kleisli-composes the
// continuations.
public <B> Trampoline<B> bind(final F<A, Trampoline<B>> f) {
return codense(sub, new F<Object, Trampoline<B>>() {
public Trampoline<B> f(final Object o) {
return suspend(new P1<Trampoline<B>>() {
public Trampoline<B> _1() {
return 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(new F<P1<Trampoline<Object>>, P1<Trampoline<A>>>() {
public P1<Trampoline<A>> f(final P1<Trampoline<Object>> p) {
return p.map(new F<Trampoline<Object>, Trampoline<A>>() {
public Trampoline<A> f(final Trampoline<Object> ot) {
return ot.fold(new F<Normal<Object>, Trampoline<A>>() {
public Trampoline<A> f(final Normal<Object> o) {
return o.foldNormal(new F<Object, Trampoline<A>>() {
public Trampoline<A> f(final Object o) {
return cont.f(o);
}
}, new F<P1<Trampoline<Object>>, Trampoline<A>>() {
public Trampoline<A> f(final P1<Trampoline<Object>> t) {
return t._1().bind(cont);
}
}
);
}
}, new F<Codense<Object>, Trampoline<A>>() {
public Trampoline<A> f(final Codense<Object> c) {
return codense(c.sub, new F<Object, Trampoline<A>>() {
public Trampoline<A> f(final Object o) {
return c.cont.f(o).bind(cont);
}
});
}
}
);
}
});
}
}, new F<Object, P1<Trampoline<A>>>() {
public P1<Trampoline<A>> f(final Object o) {
return new P1<Trampoline<A>>() {
public Trampoline<A> _1() {
return 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")
protected static <A, B> Codense<B> codense(final Normal<A> a, final F<A, Trampoline<B>> k) {
return new Codense<B>((Normal<Object>) a, (F<Object, Trampoline<B>>) k);
}
/**
* @return The first-class version of `pure`.
*/
public static <A> F<A, Trampoline<A>> pure() {
return new F<A, Trampoline<A>>() {
public Trampoline<A> f(final A a) {
return pure(a);
}
};
}
/**
* 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>(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>(a);
}
/**
* @return The first-class version of `suspend`.
*/
public static <A> F<P1<Trampoline<A>>, Trampoline<A>> suspend_() {
return new F<P1<Trampoline<A>>, Trampoline<A>>() {
public Trampoline<A> f(final P1<Trampoline<A>> trampolineP1) {
return suspend(trampolineP1);
}
};
}
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 `bind`.
*/
public static <A, B> F<F<A, Trampoline<B>>, F<Trampoline<A>, Trampoline<B>>> bind_() {
return new F<F<A, Trampoline<B>>, F<Trampoline<A>, Trampoline<B>>>() {
public F<Trampoline<A>, Trampoline<B>> f(final F<A, Trampoline<B>> f) {
return new F<Trampoline<A>, Trampoline<B>>() {
public Trampoline<B> f(final Trampoline<A> a) {
return a.bind(f);
}
};
}
};
}
/**
* @return The first-class version of `map`.
*/
public static <A, B> F<F<A, B>, F<Trampoline<A>, Trampoline<B>>> map_() {
return new F<F<A, B>, F<Trampoline<A>, Trampoline<B>>>() {
public F<Trampoline<A>, Trampoline<B>> f(final F<A, B> f) {
return new F<Trampoline<A>, Trampoline<B>>() {
public Trampoline<B> f(final Trampoline<A> a) {
return a.map(f);
}
};
}
};
}
/**
* @return The first-class version of `resume`.
*/
public static <A> F<Trampoline<A>, Either<P1<Trampoline<A>>, A>> resume_() {
return new F<Trampoline<A>, Either<P1<Trampoline<A>>, A>>() {
public Either<P1<Trampoline<A>>, A> f(final Trampoline<A> aTrampoline) {
return aTrampoline.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 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(new F<F<A, B>, Trampoline<B>>() {
public Trampoline<B> f(final F<A, B> f) {
return map(f);
}
});
}
/**
* 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(new F2<Trampoline<A>, Trampoline<B>, Trampoline<C>>() {
public Trampoline<C> f(final Trampoline<A> as, final Trampoline<B> bs) {
return 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 <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()) {
return suspend(P1.bind(x, y, new F2<Trampoline<A>, Trampoline<B>, Trampoline<C>>() {
public Trampoline<C> f(final Trampoline<A> ta, final Trampoline<B> tb) {
return suspend(new P1<Trampoline<C>>() {
public Trampoline<C> _1() {
return ta.zipWith(tb, f);
}
});
}
}.curry()));
}
for (final B y : eb.right()) {
return suspend(x.map(new F<Trampoline<A>, Trampoline<C>>() {
public Trampoline<C> f(final Trampoline<A> ta) {
return ta.map(f.flip().f(y));
}
}));
}
}
for (final A x : ea.right()) {
for (final B y : eb.right()) {
return suspend(new P1<Trampoline<C>>() {
public Trampoline<C> _1() {
return 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.");
}
}