% experimental functional programming feature... Use 'es' for 'is'... % % as a side effect 'S es T' will unify S with the result of evaluating parts of T % % Declare functions and macros using 'function(f(X), Expr):-condition' :-op(700, xfx, es). :-op(700, xfx, are). % are is a special case for vectors: [] are []. [X|R] are [Y|S] :-!, X es Y, R are S. % The following are commented out until tested. % X es Y:-macro(Y,E), !, X=E. % X es Y:-macro(Y,E), !, X es E. % the next 7 definitions are intended to make sure that 'es' contains % 'is' as a special case. % Can not evaluate an uninstantiated variable. _ es Y:-var(Y),!, fail. % If it is an atomic constant then copy it. X es Y:-atomic(Y),function(Y,X),!. X es X:-atomic(X),!. X es +Y:-!,X es Y. X es -Y:- !,MY es Y, X is -MY. X es Y+Z:-!,VY es Y, VZ es Z, X is VY+VZ. X es Y*Z:-!,VY es Y, VZ es Z, X is VY*VZ. X es Y-Z:-!,VY es Y, VZ es Z, X is VY-VZ. X es Y/Z:-!,VY es Y, VZ es Z, X is VY/VZ. X es Y mod Z:-!,VY es Y, VZ es Z, X is VY mod VZ. % The next two define the value of a vector as a vector of values % and are needed to handle the general case X es []:-!,X=[]. [X|RX] es [Y|RY]:-!,X es Y, RX es RY. % This is the general case - Y = F(Args). X es Y :-Y=..[F|Args], ArgVals es Args, FY=..[F|ArgVals], function(FY,E),!,X es E. % The next alternative leaves functors (undeclared functions) unevaluated. % while evaluating any arguments that can be evaluated. % X es Y :-Y=..[F|Args], % write('Warning: '),write(F),write(' is an undefined function.'),nl, % ArgVals es Args, % X=..[F|ArgVals]. go:-prompt(_, 'Expression=? '), repeat, read(Term), Val es Term, write(' Value='), write(Val), nl. :-dynamic(function/2). function:-write('Define a function by: assert(function(Form, Value)).'),nl, write('or assert(function(Form, Expression):-ConditionsEtc).'),nl. function(sq(X), X*X).