Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://nrktkt.github.io/tail/
simple tail call optimization and stack safety for Java
https://nrktkt.github.io/tail/
java tail-call-optimization trampolines
Last synced: about 1 month ago
JSON representation
simple tail call optimization and stack safety for Java
- Host: GitHub
- URL: https://nrktkt.github.io/tail/
- Owner: nrktkt
- License: unlicense
- Created: 2020-03-12T01:15:52.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-22T17:10:04.000Z (over 1 year ago)
- Last Synced: 2024-11-03T12:31:59.407Z (about 1 month ago)
- Topics: java, tail-call-optimization, trampolines
- Language: Java
- Homepage: https://nrktkt.github.io/tail/
- Size: 32.2 KB
- Stars: 25
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-java-zh - Tail - 使用尾调用优化启用无限递归。 (项目 / 发展)
- awesome-java - Tail - Enable infinite recursion using tail call optimization. (Projects / Development)
README
# com.github.kag0.tail
simple tail call optimization for Java
enables infinitely deep [tail recursive calls](https://en.wikipedia.org/wiki/Tail_call) without throwing a `StackOverflowError`
no transitive dependencies
## Install
[![](https://jitpack.io/v/nrktkt/tail.svg)](https://jitpack.io/#nrktkt/tail)add the jitpack repository
```xml...
jitpack.io
https://jitpack.io
...```
add the dependency
```xml...
com.github.nrktkt
tail
Tag
...```
## Use
```java
import com.github.kag0.tail.Tail;
import static com.github.kag0.tail.Tail.*;Tail infiniteLoop(int i) {
System.out.println("Loop " + i + ", stack still intact!");
return call(() -> infiniteLoop(i + 1));
}infiniteLoop(0).evaluate();
```### Example: tail optimizing factorial computation
#### un-optimized first version
let's start with a simple recursive method to compute the `n`th factorial.
this code will throw a `StackOverflowError` for large values of `n`.```java
long factorial(long n) {
if(n == 1) return 1;
else return n * factorial(n - 1);
}
```#### move the recursive call into the tail position
the tail position is just another way of saying
"the last thing you do before the `return`".```java
long factorial(long fact, long n) {
if(n.equals(1)) return fact;
return factorial(fact * n, n - 1);
}
```this may require a slight refactor,
usually to add an additional parameter to accumulate progress.#### wrap the return type in `Tail`
this will enforce that the recursive call is in the tail position.
```java
Tail factorial(long fact, long n)
```#### wrap base cases with `done`
```java
if(n.equals(0)) return done(fact);
```#### wrap recursive calls with `call`
```java
return call(() -> factorial(fact * n, n - 1));
```#### profit
call `.evaluate()` on the invocation of your method.
```java
factorial(1, Long.MAX_VALUE).evaluate();
```recursive methods no longer blow the stack.
note that if you skip the 'move the recursive call into the tail position'
step, the code will not compile because the method is not tail recursive
and therefore not stack safe. thanks to `Tail` that is covered by type safety.### making safe recursive calls outside the tail position
in addition to making tail recursion safe,
we can also use trampolining to enable recursive methods
that would otherwise be tricky to make tail recursive.to do this, just use `.flatMap` to chain two `call`s together.
for example```java
Tail ackermann(int m, int n) {
if(m == 0)
return done(n + 1);
if(m > 0 && n == 0)
return call(() -> ackermann(m - 1, 1));
if(m > 0 && n > 0)
return call(() -> ackermann(m, n - 1)).flatMap(nn -> ackermann(m - 1, nn));
throw new IllegalArgumentException();
}
```## [Benchmarks](benchmark.md)