inline-java
inline-java copied to clipboard
Streamline callbacks to haskell with inline-java
Many libraries in java call back into user code to perform its functions. One notable example is GUI libraries. We have another example in jvm-streaming. https://github.com/tweag/inline-java/blob/master/jvm-streaming/src/main/haskell/Language/Java/Streaming.hs#L92
iterator <-
[java| new Iterator() {
private boolean end = false;
private Object lookahead;
@Override
public boolean hasNext() { return !end; }
@Override
public Object next() {
if (hasNext()) {
final Object temp = lookahead;
lookahead = hsNext();
return temp;
} else
throw new java.util.NoSuchElementException();
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
private native void hsFinalize(long tblPtr);
private native Object hsNext();
@Override
public void finalize() { hsFinalize($tblPtr); }
} |]
The iterator is passed to a function that will retrieve values from Haskell via the native methods of our anonymous Iterator class.
Various housekeeping tasks are necessary to link Haskell functions with those native methods. This issue is about designing and implementing a mechanism that would allow to automate the housekeeping. Suppose we implemented somehow a new quasiquoter like:
iterator <-
[new| Iterator() {
private boolean end = false;
private Object lookahead;
@Override
public boolean hasNext() { return !end; }
@Override
public Object next() {
if (hasNext()) {
final Object temp = lookahead;
lookahead = hsNext();
return temp;
} else
throw new java.util.NoSuchElementException();
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
private native Object $$hsNext();
@Override
public void finalize() { hsFinalize(); }
} |]
Which is capable of identifying hsNext, link it with the corresponding Haskell function and automatically synthesize hsFinalize.
A major challenge with this approach is to produce the FFI wrappers of the haskell functions. Template Haskell can produce FFI wrappers with addTopDecls, but the type information necessary to do so would be available only after type-checking.
Well, another approach could be to have some template haskell code
generateFunctionalInterface :: [TH.Type] -> TH.Type -> Q [TH.Decl]
generateFunctionalInterface argumentTypes returnType = ...
that produces the necessary FFI declarations and a function like
newFunctionalInterface :: String -> (ArgType1 -> ... -> ArgTypeN -> ReturnType) -> IO (J ty)
newFunctionalInterface methodName haskellCallback = ...
where J ty is a reference to an instance of some functional interface.
Compared to the first proposal, the user is telling the compiler which types to use, and the compiler would produce some error if the types are wrong.
There is some code here which is reusable to setup different types of callbacks: https://github.com/tweag/inline-java/blob/master/benchmarks/wizzardo-http/src/main/haskell/Language/Java/Function.hs