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
|
/* $Id: expandmath.pl,v 1.3 2002/02/01 15:04:53 jan Exp $
Part of XPCE --- The SWI-Prolog GUI toolkit
Author: Jan Wielemaker and Anjo Anjewierden
E-mail: jan@swi.psy.uva.nl
WWW: http://www.swi.psy.uva.nl/projects/xpce/
Copyright (C): 1985-2002, University of 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 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.
*/
:- module(expand_math, [expand_function/3]).
:- use_module(expandgoal).
:- require([ append/3
, genarg/3
]).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This module cooperates with library(expandgoal), expanding mathematical
expressions on systems that have limited math functions and that cannot
define new math funtions.
For each function f/n, it expects a predicate f/n+1, where the first n
arguments are in the same order as the function, and the last is used
for the return value. In ensures all arguments to the predicate are
evaluated if they contain expressions.
There are two configuration predicates:
built_in(?Function)
Says Function is a built-in mathematical function. It will not
be converted, but its arguments will if they contain non-builtin
functions. Numbers and variables are not converted either.
math_alias(+Function, -Alias)
Alows for alias definition. Some of these aliases are needed
to call the right function (** --> pow for example). Some are
used to expand mathematical constants (pi, e).
Bugs:
Only experssions appearing in the source-code as an argument to
one of the math predicates is/2, =:=/2, =\=/2, >/2, </2, >=/2,
=</2 are translated, and only if these are determined as a goal,
by the goal expansion package.
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - */
:- multifile
user:goal_expansion/2.
:- dynamic
user:goal_expansion/2.
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(_, _, _) :- % testing under SWI-Prolog
prolog_load_context(module, quintus), !,
fail.
expand_function(Expression, NativeExpression, Goal) :-
do_expand_function(Expression, NativeExpression, Goal0),
tidy(Goal0, Goal).
do_expand_function(X, X, true) :-
all_builtin(X), !.
do_expand_function(A0, X, Code) :-
math_alias(A0, A), !,
do_expand_function(A, X, Code).
do_expand_function(Function, Result, ArgCode) :-
built_in(Function), !,
Function =.. [Name|Args],
expand_function_arguments(Args, ArgResults, ArgCode),
Result =.. [Name|ArgResults].
do_expand_function(Function, Result, (ArgCode, Pred)) :-
Function =.. [Name|Args],
expand_predicate_arguments(Args, ArgResults, ArgCode),
append(ArgResults, [Result], PredArgs),
Pred =.. [Name|PredArgs].
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),
( nonvar(H1),
built_in(H1)
-> A = (A0, H is H1)
; A = A0,
H = H1
),
expand_predicate_arguments(T0, T, B).
built_in(_+_).
built_in(_-_).
built_in(_*_).
built_in(-_).
built_in(+_).
built_in(abs(_)).
built_in(_/_).
built_in(_//_).
built_in(div(_,_)).
built_in(mod(_,_)).
built_in(\(_)).
built_in(\(_,_)).
built_in([_]). % "a" ...
built_in(_/\_).
built_in(_\/_).
built_in(_>>_).
built_in(_<<_).
built_in(integer(_)).
built_in(float(_)).
built_in(max(_,_)).
built_in(min(_,_)).
all_builtin(F) :-
var(F), !.
all_builtin(F) :-
number(F), !.
all_builtin(F) :-
built_in(F),
\+ (genarg(_,F,A), \+ all_builtin(A)).
math_alias(pi, 3.14159265358979323846).
math_alias(e, 2.7182818284590452354).
math_alias(atan(Y,X), atan2(Y,X)).
math_alias(**(X,Y), pow(X,Y)).
math_alias(X^Y, pow(X,Y)).
math_alias(ceil(X), ceiling(X)).
math_alias(random(M), random(0, M)).
/*******************************
* TIDY *
*******************************/
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).
/*******************************
* THE HOOK *
*******************************/
user:goal_expansion(A,B) :-
expand_math:math_goal_expansion(A,B).
|