Prolog "compat"

Admin User, created Mar 15. 2024
         
/**
* 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.
*/
/**
* forall(A, B): [N208 8.10.4]
* The predicate succeeds when there is no success of A
* such that B fails. Otherwise, the predicate fails.
*/
% forall(+Goal, +Goal)
forall(A, B) :- \+ (A, \+ B).
/************************************************************/
/* sort/2 and keysort/2 */
/************************************************************/
/**
* sort(L, R): [TC2 8.4.3]
* The predicate succeeds in R with the unstable sorted list L.
*/
% sort(+List, -List)
sort(L, R) :-
sys_check_list(L),
sys_quicksort(L, R, []).
% sys_check_list(+Term)
sys_check_list(V) :- var(V),
throw(error(instantiation_error,_)).
sys_check_list([]) :- !.
sys_check_list([_|L]) :- !,
sys_check_list(L).
sys_check_list(L) :-
throw(error(type_error(list,L),_)).
% sys_quicksort(+List, -List, +List)
sys_quicksort([]) --> [].
sys_quicksort([X|L]) -->
{sys_partition(L, X, L1, L2)},
sys_quicksort(L1),
[X],
sys_quicksort(L2).
% sys_partition(+List, +Term, -List, -List)
sys_partition([], _, [], []).
sys_partition([X|L], Y, R, L2) :-
X @< Y, !, R = [X|L1],
sys_partition(L, Y, L1, L2).
sys_partition([X|L], Y, L1, L2) :-
X == Y, !,
sys_partition(L, Y, L1, L2).
sys_partition([X|L], Y, L1, [X|L2]) :-
sys_partition(L, Y, L1, L2).
/**
* keysort(L, R): [TC2 8.4.4]
* The predicate succeeds in R with the stable key sorted list L.
*/
% keysort(+List, -List)
keysort(L, R) :-
sys_check_pairs(L),
sys_keyquicksort(L, R, []).
% sys_check_pairs(+Term)
sys_check_pairs(V) :- var(V),
throw(error(instantiation_error,_)).
sys_check_pairs([]) :- !.
sys_check_pairs([P|L]) :- !,
sys_check_pair(P),
sys_check_pairs(L).
sys_check_pairs(L) :-
throw(error(type_error(list,L),_)).
% sys_check_pair(+Term)
sys_check_pair(V) :- var(V),
throw(error(instantiation_error,_)).
sys_check_pair(_-_) :- !.
sys_check_pair(P) :-
throw(error(type_error(pair,P),_)).
% sys_keyquicksort(+List, -List, +List)
sys_keyquicksort([]) --> [].
sys_keyquicksort([X-P|L]) -->
{sys_keypartition(L, X, L1, L2, L3)},
sys_keyquicksort(L1),
[X-P], sys_keylist(L2),
sys_keyquicksort(L3).
% sys_keypartition(+List, +Term, -List, -List, -List)
sys_keypartition([], _, [], [], []).
sys_keypartition([X-P|L], Y, R, L2, L3) :-
X @< Y, !, R = [X-P|L1],
sys_keypartition(L, Y, L1, L2, L3).
sys_keypartition([X-P|L], Y, L1, R, L3) :-
X == Y, !, R = [X-P|L2],
sys_keypartition(L, Y, L1, L2, L3).
sys_keypartition([X-P|L], Y, L1, L2, [X-P|L3]) :-
sys_keypartition(L, Y, L1, L2, L3).
% sys_keylist(+List, -List, +List)
sys_keylist([]) --> [].
sys_keylist([X|L]) --> [X], sys_keylist(L).
/*******************************************************************/
/* Subsumption */
/*******************************************************************/
/**
* subsumes_term(X, Y): [ISO 8.2.4]
* The built-in succeeds if X subsumes Y without keeping the bindings.
*/
% subsumes_term(+Term, +Term)
subsumes_term(X, Y) :- sys_not_subsumes(X, Y), !, fail.
subsumes_term(_, _).
% sys_not_subsumes(+Term, +Term)
sys_not_subsumes(X, Y) :- subsumes(X, Y), !, fail.
sys_not_subsumes(_, _).
/*******************************************************************/
/* Foreign Predicates */
/*******************************************************************/
% numbervars(X, N, M):
% defined in foreign(fastlib)
% unify_with_occurs_check(S, T): [ISO 8.2.2]
% The built-in succeeds when the Prolog terms S and T unify
% with occurs check, otherwise the built-in fails.
% defined in foreign(fastlib)
% subsumes(X, Y): [N208 8.2.4]
% The built-in succeeds if X subsumes Y.
% defined in foreign(fastlib)
:- ensure_loaded(foreign(fastlib)).