/usr/lib/swi-prolog/library/arithmetic.pl is in swi-prolog-nox 7.6.4+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2011-2015, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(arithmetic,
[ arithmetic_function/1, % +Name/Arity
arithmetic_expression_value/2 % :Expression, -Value
]).
:- use_module(library(error)).
:- use_module(library(lists)).
:- set_prolog_flag(generate_debug_info, false).
/** <module> Extensible arithmetic
This module provides a portable partial replacement of SWI-Prolog's
user-defined arithmetic (evaluable) functions. It defines the
compatibility directive arithmetic_function/1 and support for both
runtime and compile-time evaluation of expressions that are a mixture
between Prolog predicates used as functions and built-in evaluable
terms.
*/
:- meta_predicate
arithmetic_function(:),
arithmetic_expression_value(:, -).
:- multifile
evaluable/2. % Term, Module
%! arithmetic_function(:NameArity) is det.
%
% Declare a predicate as an arithmetic function.
%
% @deprecated This function provides a partial work around for
% pure Prolog user-defined arithmetic functions that has been
% dropped in SWI-Prolog 5.11.23. Notably, it only deals with
% expression know at compile time.
arithmetic_function(Term) :-
throw(error(context_error(nodirective, arithmetic_function(Term)), _)).
arith_decl_clauses(NameArity,
[(:- public(PI)),
arithmetic:evaluable(Term, Q)
]) :-
prolog_load_context(module, M),
strip_module(M:NameArity, Q, Spec),
( Q == M
-> PI = Name/ImplArity
; PI = Q:Name/ImplArity
),
( Spec = Name/Arity
-> functor(Term, Name, Arity),
ImplArity is Arity+1
; type_error(predicate_indicator, Term)
).
%! eval_clause(+Term, -Clause) is det.
%
% Clause is a clause for evaluating the arithmetic expression
% Term.
eval_clause(Term, (eval(Gen, M, Result) :- Body)) :-
functor(Term, Name, Arity),
functor(Gen, Name, Arity),
Gen =.. [_|Args],
eval_args(Args, PlainArgs, M, Goals, [Result is NewTerm]),
NewTerm =.. [Name|PlainArgs],
list_conj(Goals, Body).
eval_args([], [], _, Goals, Goals).
eval_args([E0|T0], [A0|T], M, [eval(E0, M, A0)|GT], RT) :-
eval_args(T0, T, M, GT, RT).
list_conj([One], One) :- !.
list_conj([H|T0], (H,T)) :-
list_conj(T0, T).
eval_clause(Clause) :-
current_arithmetic_function(Term),
eval_clause(Term, Clause).
term_expansion(eval('$builtin', _, _), Clauses) :-
findall(Clause, eval_clause(Clause), Clauses).
%! arithmetic_expression_value(:Expression, -Result) is det.
%
% True when Result unifies with the arithmetic result of
% evaluating Expression.
arithmetic_expression_value(M:Expression, Result) :-
eval(Expression, M, Result).
eval(Number, _, Result) :-
number(Number),
!,
Result = Number.
eval(Term, M, Result) :-
evaluable(Term, M2),
visible(M, M2),
!,
call(M2:Term, Result).
eval('$builtin', _, _).
visible(M, M) :- !.
visible(M, Super) :-
import_module(M, Parent),
visible(Parent, Super).
/*******************************
* COMPILE-TIME *
*******************************/
math_goal_expansion(A is Expr, Goal) :-
expand_function(Expr, Native, Pre),
tidy((Pre, A is Native), Goal).
math_goal_expansion(ExprA =:= ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA =:= NativeB), Goal).
math_goal_expansion(ExprA =\= ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA =\= NativeB), Goal).
math_goal_expansion(ExprA > ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA > NativeB), Goal).
math_goal_expansion(ExprA < ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA < NativeB), Goal).
math_goal_expansion(ExprA >= ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA >= NativeB), Goal).
math_goal_expansion(ExprA =< ExprB, Goal) :-
expand_function(ExprA, NativeA, PreA),
expand_function(ExprB, NativeB, PreB),
tidy((PreA, PreB, NativeA =< NativeB), Goal).
expand_function(Expression, NativeExpression, Goal) :-
do_expand_function(Expression, NativeExpression, Goal0),
tidy(Goal0, Goal).
do_expand_function(X, X, true) :-
evaluable(X),
!.
do_expand_function(Function, Result, ArgCode) :-
current_arithmetic_function(Function),
!,
Function =.. [Name|Args],
expand_function_arguments(Args, ArgResults, ArgCode),
Result =.. [Name|ArgResults].
do_expand_function(Function, Result, (ArgCode, Pred)) :-
prolog_load_context(module, M),
evaluable(Function, M2),
visible(M, M2),
!,
Function =.. [Name|Args],
expand_predicate_arguments(Args, ArgResults, ArgCode),
append(ArgResults, [Result], PredArgs),
Pred =.. [Name|PredArgs].
do_expand_function(Function, _, _) :-
type_error(evaluable, Function).
expand_function_arguments([], [], true).
expand_function_arguments([H0|T0], [H|T], (A,B)) :-
do_expand_function(H0, H, A),
expand_function_arguments(T0, T, B).
expand_predicate_arguments([], [], true).
expand_predicate_arguments([H0|T0], [H|T], (A,B)) :-
do_expand_function(H0, H1, A0),
( callable(H1),
current_arithmetic_function(H1)
-> A = (A0, H is H1)
; A = A0,
H = H1
),
expand_predicate_arguments(T0, T, B).
%! evaluable(F) is semidet.
%
% True if F and all its subterms are evaluable terms or variables.
evaluable(F) :-
var(F),
!.
evaluable(F) :-
number(F),
!.
evaluable([_Code]) :- !.
evaluable(Func) :- % Funtional notation.
functor(Func, ., 2),
!.
evaluable(F) :-
string(F),
!,
string_length(F, 1).
evaluable(F) :-
current_arithmetic_function(F),
( compound(F)
-> forall(arg(_,F,A), evaluable(A))
; true
).
%! tidy(+GoalIn, -GoalOut)
%
% Cleanup the output from expand_function/3.
tidy(A, A) :-
var(A),
!.
tidy(((A,B),C), R) :-
!,
tidy((A,B,C), R).
tidy((true,A), R) :-
!,
tidy(A, R).
tidy((A,true), R) :-
!,
tidy(A, R).
tidy((A, X is Y), R) :-
var(X), var(Y),
!,
tidy(A, R),
X = Y.
tidy((A,B), (TA,TB)) :-
!,
tidy(A, TA),
tidy(B, TB).
tidy(A, A).
/*******************************
* EXPANSION HOOK *
*******************************/
:- multifile
system:term_expansion/2,
system:goal_expansion/2.
system:term_expansion((:- arithmetic_function(Term)), Clauses) :-
arith_decl_clauses(Term, Clauses).
system:goal_expansion(Math, MathGoal) :-
math_goal_expansion(Math, MathGoal).
|