Admin User, created Apr 26. 2025
###
# Modern Albufeira Prolog Interpreter
#
# 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.
#
# 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.
#
# 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.
#
# 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.
#
# Trademarks
# Jekejeke is a registered trademark of XLOG Technologies AG.
##
import functools
from nova.core import (make_check, exec_build, compare_term, set_to_list,
list_objects, objects_list, equal_term, check_callable,
Compound, is_compound, add, make_error, Cache, check_atom,
exec_unify, deref, check_nonvar, copy_term)
#######################################################################
# sort/2 and keysort/2 #
#######################################################################
###
# sort(L, R): [TC2 8.4.3]
# The predicate succeeds in R with the sorted list L.
##
def test_sort(args):
alpha = deref(exec_build(args[0]))
res = list_objects(alpha)
res.sort(key=functools.cmp_to_key(compare_term))
count = objects_dedup(res)
return exec_unify(args[1], objects_list(res, 0, count))
def objects_dedup(res):
j = 0
i = 0
while i < len(res):
alpha = res[i]
i += 1
while i < len(res) and equal_term(alpha, res[i]):
i += 1
res[j] = alpha
j += 1
return j
###
# keysort(L, R): [TC2 8.4.4]
# The predicate succeeds in R with the key sorted list L.
##
def test_keysort(args):
alpha = deref(exec_build(args[0]))
res = list_objects(alpha)
objects_pairs(res)
res.sort(key=functools.cmp_to_key(lambda first, second:
compare_term(get_key(first), get_key(second))))
return exec_unify(args[1], objects_list(res, 0, len(res)))
def objects_pairs(res):
i = 0
while i < len(res):
alpha = res[i]
if (is_compound(alpha) and
"-" == alpha.functor and
len(alpha.args) == 2):
pass
else:
check_nonvar(alpha)
alpha = copy_term(alpha)
raise make_error(Compound("type_error", ["pair", alpha]))
i += 1
def get_key(peek):
return 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.
##
def test_ir_call_site(args):
alpha = deref(exec_build(args[0]))
check_callable(alpha)
if is_compound(alpha):
functor = alpha.functor
check_atom(functor)
oldargs = alpha.args
res = Compound(Cache(functor), oldargs)
else:
check_atom(alpha)
res = Cache(alpha)
return exec_unify(args[1], res)
#######################################################################
# Iso Lib Init #
#######################################################################
def main():
add("sort", 2, make_check(test_sort))
add("keysort", 2, make_check(test_keysort))
add("ir_call_site", 2, make_check(test_ir_call_site))