/* YAP Prolog / SWI Prolog compatibility layer This implements many essential predicates found in SWI Prolog and many other Prolog implementations that aren't available in YAP Prolog. Generally, these predicates behave as expected; in exceptional circumstances (i.e. errors/argument instantiation which throw an exception),these predicates may silently fail. Eliminate your bugs using SWI's excellent development facilities. Time-complexity and optimization issues may be differ between SWI/GNU/YAP. In general, SWI will be much faster at things like flag/3, as we simply use the assert/retract mechanism. 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_yap.pl,v 1.3 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. */ :- use_module(library(system)). :- use_module(library(lists)). :- use_module(library(charsio)). flag(Sym, Old, NewExp) :- ((bb_get(Sym, Old), !) ; Old=0), New is NewExp, bb_put(Sym, New). %simple gensym % gensym(+Base, -Unique) gensym(Base, Atom) :- atom_concat('gs_', Base, Key), flag(Key, N, N+1), number_atom(N, NA), atom_concat(Base, NA, Atom). sformat(String, Templ, Args) :- format_to_chars(Templ, Args, String). % SWI-Strings are represented as codes-lists in yAP 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). %time time(Goal) :- statistics(runtime,CPU), Goal, statistics(runtime,CPU2), CPUT is CPU2-CPU, write('time: '), write(CPUT), write('ms.\n'). % internal database recordz(K,V) :- recordz(K,V,_). recorded(K,V) :- recorded(K,V,_). % For all possible Bindings in Cond, Action can be proven. forall(Cond, Action) :- Cond, (Action -> fail ; (!, fail)). forall(_,_). % 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). % 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 true; assert_static(delete_file(_) :- write('file deletion not implemented in YAP compatibility layer.\n'))). thread_local(_). /* Authors: David Reitter Copyright (C): 2003 Media Lab Europe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */