ANN: Dogelog Player for Java 

It has been already 2 years since we started a new Prolog system. This new Prolog systems is 100% written in Prolog, including the I/O and the runtime compiler. We managed to adress the JavaScript and the Python target.

![doge_java.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/416192/660baa00-53a8-4848-8508-02fec62cd6fd.png)

We now completed a new target, namely Java. This target is different since in contrast to JavaScript and Python, the Java programming language is a typed programming language with type variables and primitive types.

## JavaScript Realization
Using JavaScript for our first take allowed rapid prototyping. Since it is type less, only the data has a type but variables need not to be typed, it is enough to introduce them via the let construct, we had a steady progress.

When implementing a Prolog system, the first thing that goes realized is the unify routine. The below shows the unify routine of Dogelog Player for JavaScript. Its a tail recursive solution using the native stack:
```JavaScript
export function unify(first, second) {
    for (; ;) {
        first = deref(first);
        second = deref(second);
        if (is_variable(first)) {
            if (is_variable(second) && first === second)
                return true;
            bind(second, first);
            return true;
        }
        if (is_variable(second)) {
            bind(first, second);
            return true;
        }
        if (!is_compound(first))
            return (first === second);
        if (!is_compound(second))
            return false;
        if (first.args.length !== second.args.length)
            return false;
        if (first.functor !== second.functor)
            return false;
        first = first.args;
        second = second.args;
        let i = 0;
        for (; i < first.length - 1; i++) {
            if (!unify(first[i], second[i]))
                return false;
        }
        first = first[i];
        second = second[i];
    }
}
```
Since the Python programming language and the JavaScript programming language have many things in common, we could also make a Dogelog Player version for Python. And since both programming languages allow coroutines, they became also part of Dogelog Player. 

The JavaScript version is also capable to run inside a browser, and there are some extra libraries that allow accessing and modifying the DOM, as well as libraries to deal with events.

## Java Realization
To create the Java realization we took the JavaScript version and added types. Unfortunately this is a quite tedious work, since one has to also add type casts here and then. The unify routine now reads:
```Java
    public static boolean unify(Object first, Object second) {
        for (; ; ) {
            first = deref(first);
            second = deref(second);
            if (is_variable(first)) {
                if (is_variable(second) && first == second)
                    return true;
                bind(second, (Variable) first);
                return true;
            }
            if (is_variable(second)) {
                bind(first, (Variable) second);
                return true;
            }
            if (!is_compound(first))
                return Objects.equals(first,second);
            if (!is_compound(second))
                return false;
            Object[] t1 = ((Compound) first).args;
            Object[] t2 = ((Compound) second).args;
            if (t1.length != t2.length)
                return false;
            if (!((Compound) first).functor.equals(((Compound) second).functor))
                return false;
            int i = 0;
            for (; i < t1.length - 1; i++) {
                if (!unify(t1[i], t2[i]))
                    return false;
            }
            first = t1[i];
            second = t2[i];
        }
    }
```
It was tempting to use the formerly Jekejeke Prolog code base, we refrained from doing that, and re-implemented Dogelog Player from A-Z close to the JavaScript source. There is one exception, we share with formerly Jekejeke Prolog implementations of custom Array, Map and Set, and the Arithmetic Logical Unit (ALU).

The current implementation doesn't have coroutines but we might experiment with the Lightweight Threads of Java in this respect. Also native libraries are not yet implemented, but based on recent development in formerly Jekejeke Prolog to provide ensure_loaded/1 with foreign/1 this shouldn't be a problem.

## Benchmark Results
The benchmark results show that we can indeed replace formerly Jekejeke Prolog by the new Dogelog Player for Java. Dogelog Player for Java is practically as fast as formerly Jekejeke Prolog for JDK 21:

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/416192/c402b6ab-63d0-e740-4501-114b95ac61cd.png)

Formerly Jekejeke Prolog for JDK 21 takes 7188 ms, Dogelog Player for Java does it in 7794 ms. And Dogelog Player is still in its fancy concerning every Prolog system runtime and compiler optimization.

What we did not yet do, we did not yet run the compilance suite that comes with Dogelog Player. But the above example already makes use of the 100% Prolog written I/O and runtime compiler without crashing!

## Future Work
With Dogelog Player for Java we have gained a Prolog system for the Java platform that features a tracing garbage collector that can deal with non-logical builtins such as change_arg/3. This is a different model from formerly Jekejeke Prolog which uses reference counting and pointer pairs.

But there is still a lot of work ahead, which we will possibly perform for all targets JavaScript, Python and Java in parallel. What comes to mind is multi-argument indexing, functional interfaces inlining and worker based multi-threading. This will easily fill the next 5-10 years.

## Conclusions
The benchmark results show that we can indeed replace formerly Jekejeke Prolog by the new Dogelog Player for Java. But there is still a lot of work ahead, which we will possibly perform for all targets JavaScript, Python and Java in parallel.

**Source Code von Dogelog Player for Java**
https://www.jekejeke.ch/srctab/doclet/docs/09_dog/common/playerj/drawer/package.jsp

**Binary Download von Dogelog Player for Java**
https://www.xlog.ch/izytab/doclet/docs/23_products/10_jekrun.html