/usr/lib/swi-prolog/library/arithmetic.pl is in swi-prolog-nox 7.2.3+dfsg-6.
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 | /*  Part of SWI-Prolog
    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@cs.vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2011-2014, VU University Amsterdam
    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 General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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.
*/
:- 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 runtime.
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).
 |