/* GNU Prolog compatibility layer This implements many essential predicates found in SWI Prolog and many other Prolog implementations that aren't available in GNU Prolog. Generally, these predicates behave as expected; in exceptional circumstances (i.e. errors/argument instantiation which throw an exception),these predicates may silently fail. In an implementation that is ready to be compiled (and that's what's GNU Prolog good at), these bugs should have been eliminated. Time-complexity and optimization issues may be differ between SWI/GNU. In general, SWI will be much faster at things like flag/3, as we simply use the assert/retract mechanism. Additionally, you need to include lists.pl from the SWI distribution. Some predicates cannot be used in their original form: Use eval_arith/2 instead of is/2 to enable custom functions in GNU/YAP. Author: David Reitter, reitter at mle.media.mit.edu -- stealing and copying stuff from various sources, in particular from Jan Wielemaker's SWI Prolog. Version: $Id: compat_gnu.pl,v 1.5 2003/11/02 20:00:21 dr Exp $ (C) 2003 Media Lab Europe Ltd. Licensed under the GNU public license. See end for licensing information. */ not(Goal) :- \+ Goal. % is_list taken from the SWI manual is_list(X) :- var(X), !, fail. is_list([]). is_list([_|T]) :- is_list(T). succ(I,J) :- nonvar(I), I>=0, J is I+1. succ(I,J) :- nonvar(J), I is J-1, I>=0. sformat(String, Templ, Args) :- open_output_atom_stream(Stream), format(Stream, Templ, Args), close_output_atom_stream(Stream, String). % SWI-Strings are represented as codes-lists in GNU string_to_atom(Atom, Atom) :- atom(Atom), !. string_to_atom(String, Atom) :- atom_codes(Atom, String). string(S) :- is_codes(S). is_codes(X) :- var(X), !, fail. is_codes([]). is_codes([N|T]) :- integer(N), N>0, N<256, is_codes(T). % evaluation :- dynamic(register_arithmetic/1). arithmetic_function(Functor/Arity) :- %print(register_arithmetic(Functor/Arity)),nl, retractall(register_arithmetic(Functor/Arity)), assertz(register_arithmetic(Functor/Arity)). eval_arith(Term, Result) :- Term =.. [F|Args], eval_arith2(Args, Args2), ((length(Args2,L), register_arithmetic(F/L), append(Args2, [Result], Args3), ATermN =.. [F|Args3], ATermN, !) ; (ATerm =.. [F|Args2], Result is ATerm % try internal eval routine )), !. eval_arith2([], []). eval_arith2([Term|Rest], [TermResult|RestResult]) :- eval_arith(Term, TermResult), eval_arith2(Rest, RestResult). % rewrite atom into lower case downcase_atom(Atom, AtomLower) :- atom_chars(Atom, Chars), downcase_codes(Chars, CharsLower), atom_chars(AtomLower, CharsLower). downcase_codes([],[]). downcase_codes([F|R], [FL|RL]) :- lower_upper(FL,F), downcase_codes(R,RL). % For all possible Bindings in Cond, Action can be proven. forall(Cond, Action) :- Cond, (Action -> fail ; (!, fail)). forall(_,_). flag(Sym, Old, NewExp) :- g_read(Sym, Old), nonvar(NewExp) -> New is NewExp, % evaluate g_assign(Sym, New) ; g_read(Sym, New). % between(Min, Max, X) % X is a value between Min and Max % usually used to generate all possible values [Min..Max] between(Min, Max, _) :- (var(Min);var(Max)), write('ERROR: between/3: Arguments are not sufficiently instantiated'). between(Min, Max, X) :- nonvar(X), X >= Min, X =< Max. between(Min, Max, Min) :- nonvar(Min), nonvar(Max), Min =< Max. between(Min, Max, X) :- nonvar(Min), nonvar(Max), Min