Admin User, created Apr 26. 2025
package foreign;
import nova.Machine;
import nova.Store;
import nova.eval;
import nova.special;
import java.util.*;
/**
* Foreign functions for library(compat)
* <p/>
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
* <p/>
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
* <p/>
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
* <p/>
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
* <p/>
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
public final class isolib {
/******************************************************************/
/* sort/2 and keysort/2 */
/******************************************************************/
/**
* sort(L, R): [TC2 8.4.3]
* The predicate succeeds in R with the sorted list L.
*/
private static boolean test_sort(Object[] args) {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Object[] res = special.list_objects(alpha);
Arrays.sort(res, eval::compare_term);
int count = objects_dedup(res);
return Machine.exec_unify(args[1], special.objects_list(res, 0, count));
}
private static int objects_dedup(Object[] res) {
int j = 0;
int i = 0;
while (i < res.length) {
Object alpha = res[i++];
while (i < res.length && eval.equal_term(alpha, res[i]))
i++;
res[j++] = alpha;
}
return j;
}
/**
* keysort(L, R): [TC2 8.4.4]
* The predicate succeeds in R with the key sorted list L.
*/
private static boolean test_keysort(Object[] args) {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Object[] res = special.list_objects(alpha);
objects_pairs(res);
Arrays.sort(res, (first, second) -> eval.compare_term(get_key(first), get_key(second)));
return Machine.exec_unify(args[1], special.objects_list(res, 0, res.length));
}
private static void objects_pairs(Object[] res) {
for (int i = 0; i < res.length; i++) {
Object alpha = res[i];
if (Store.is_compound(alpha) &&
"-".equals(((Store.Compound) alpha).functor) &&
((Store.Compound) alpha).args.length == 2) {
/* */
} else {
Machine.check_nonvar(alpha);
alpha = new Machine.copy_term().copy_term2(alpha);
throw Machine.make_error(new Store.Compound("type_error",
new Object[]{"pair", alpha}));
}
}
}
private static Object get_key(Object peek) {
return ((Store.Compound) peek).args[0];
}
/******************************************************************/
/* ir_call_site/2 */
/******************************************************************/
/**
* ir_call_site(F, T):
* The predicate succeeds in T with a
* cachable version of the callable F.
*/
private static boolean test_ir_call_site(Object[] args) {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Machine.check_callable(alpha);
Object res;
if (Store.is_compound(alpha)) {
Object functor = ((Store.Compound) alpha).functor;
special.check_atom(functor);
Object[] oldargs = ((Store.Compound) alpha).args;
res = new Store.Compound(new Store.Cache((String) functor), oldargs);
} else {
special.check_atom(alpha);
res = new Store.Cache((String) alpha);
}
return Machine.exec_unify(args[1], res);
}
/******************************************************************/
/* Iso Lib Init */
/******************************************************************/
public static void main() {
Store.add("sort", 2, special.make_check(isolib::test_sort));
Store.add("keysort", 2, special.make_check(isolib::test_keysort));
Store.add("ir_call_site", 2, special.make_check(isolib::test_ir_call_site));
}
}