% 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. % Can not evaluate an uninstantiated variable. X es Y:-var(Y),!, fail. % If it is an atomic constant then copy it. X es X:-atomic(X),!. % X es +Y:-!,X es Y. function(+X,X). % X es -Y:- !,MY es Y, X is -MY. function(-X,Y):- Y is -Y. % 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 vectors of vectors! % They 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 ; X = FY). % The last alternative leaves functors (undeclared functions) unevaluated. % while evaluating any arguments that can be evaluated. :- write(''), nl. :- write('X es Y loaded. Define functions like this: function(f(...), nl,e).'). doit:-prompt(_, 'Expression=? '), repeat, read(Term), Val es Term, prin(' Value='), write(Val), nl. :- write('Input 'doit?'... end each expression with a period.'), nl. examples('function(sq(X), X*X).'). examples('function(factorial(0), 1).'). examples('function(factorial(N), N*factorial(N-1)).'). function:-print('Define a function by inputting 'function(Form, Value).''), print('or 'function(Form, Expression):-ConditionsEtc.'').