/* SYMBOLIC DIFFERENTIATION EXAMPLE */ DOMAINS REAL32 = REAL include "EXPR1.PRO" include "DIFF1.PRO" PREDICATES run diff integ readexp(EXP); readexp(EXP,string,integer) check(EXP,TOKL,EXP); writeexp(EXP); /* String Editor */ editstr(string,string,integer,integer,integer). editstr(char,string,string,integer,integer,integer). editstr(symbol,char,string,string,integer,integer,integer). repeat GOAL run. CLAUSES run:- makewindow(1,71,7,"",0,0,25,80), makewindow(1,71,7,"",1,14,9,52), write(" S Y M B O L I C I N T E G R A T I O N "),nl, write(" ********************************************"),nl, field_attr(0,3,44,112), write(" An expression may include: "),nl, write(" addition, subtraction, multiplication, division \n"), write(" exponents, natual logarithms, cos, sin and tan.\n\n"), write(" e.g. x+x or x*(2+y)^2 or ln(1+1/(1-x))"), makewindow(3,7,7,"",10,3,14,73), clearwindow,diff. diff :- readexp(EXP), symb_diff(EXP,"x",EXP1), write("\n Differentiated expression:\n "), writeexp(EXP1), write("\n\n Reduced expression:\n "), reduce(EXP1,EXP2), writeexp(EXP2), write("\n\n Hit any key to continue..."),readchar(_), fail. diff :- diff. integ :- readexp(EXP), symb_integ(EXP,"x",EXP1), write("\n Integrated expression:\n "), writeexp(EXP1), write("\n\n Reduced expression:\n "), reduce(EXP1,EXP2), writeexp(EXP2), write("\n\n Hit any key to continue..."),readchar(_), fail. integ :- integ. repeat. repeat:- repeat. /* CLAUSES FOR READING OF AN EXPRESSION */ readexp(EXP) :- clearwindow, cursor(11,1), write(" for help to quit"), cursor(1,1), write("Write an expression: "),!, readexp(EXP,"",0). readexp(num(0)):-exit(0). readexp(EXP1,STR,Pos) :- cursor(Ypos,Xpos), editstr(STR,STR1,Xpos,Ypos,Pos), cursor(2,0), write(" "), cursor(2,22), tokl(STR1,TOKL), s_exp(TOKL,OL,EXP), !, check(EXP,OL,EXP1). check(EXP,[],EXP):- cursor(2,22), write(" \n"), write(" \n"), write(" \n"), write(" \n"), write(" \n"), cursor(2,0),!. check(EXP,Rest,NewExp):- strexp(EXP,Str1), lstcat(Rest,Str2), str_len(Str1,LenStr1), ErrPos = 22 + LenStr1, cursor(2,ErrPos), write("^ syntax error"), cursor(1,22), concat(Str1,Str2,Str3), !, readexp(NewExp,Str3,LenStr1). /* CLAUSE FOR WRITING OF AN EXPRESSION */ writeexp(EXP) :- strexp(EXP,STR), write(STR). /* CLAUSES TO EDIT A STRING */ editstr(InString, OutString, Xpos, Ypos, Cpos) :- cursor(Ypos,Xpos), write(InString," "), NXPos = Xpos+Cpos, cursor(Ypos,NXpos), readchar(Ch), !, editstr(Ch, InString, OutString, Xpos, Ypos, Cpos). % Return -- Accept the current string. editstr('\13',InString,InString,Xpos,Ypos,_) :- cursor(Ypos,Xpos), write(InString), nl. % ESC -- Terminate the program. editstr('\27',_,"",_,_,_) :- exit. % HELP -- Display the help message % taken from checkhelp. editstr('?',InString,OutString,Xpos,Ypos,Cpos) :- makewindow(4,23,7,"",10,3,14,73), file_str("diff.hlp",I), display(I), removewindow, !, editstr(InString,OutString,Xpos,Ypos,Cpos). % ^S -- Move Cursor left editstr('\19',InString,OutString,Xpos,Ypos,Cpos) :- Cpos > 0, NewCpos = Cpos - 1, !, editstr(InString,OutString,Xpos,Ypos,NewCpos). editstr('\19',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr(InString,OutString,Xpos,Ypos,Cpos). % ^D -- Move Cursor left editstr('\4',InString,OutString,Xpos,Ypos,Cpos) :- str_len(InString,InStrLen), Cpos < InStrLen, NewCpos = Cpos + 1, !, editstr(InString,OutString,Xpos,Ypos,NewCpos). editstr('\4',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr(InString,OutString,Xpos,Ypos,Cpos). % ^H -- Backspace, Delete the previous character. editstr('\8',InString,OutString,Xpos,Ypos,1) :- frontchar(InString,_,NewString), !, editstr(NewString,OutString,Xpos,Ypos,0). editstr('\8',InString,OutString,Xpos,Ypos,Cpos) :- Cpos > 1, NewCpos = Cpos - 1, !, editstr('\21',InString,OutString,Xpos,Ypos,NewCpos). % Delete Char. editstr('\8',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr(InString,OutString,Xpos,Ypos,Cpos). % ^U -- Delete the current character. editstr('\21',InString,OutString,Xpos,Ypos,Cpos) :- frontstr(Cpos,InString,HeadString,TailStringP), frontchar(TailStringP,_,TailString), concat(HeadString,TailString,NewString), !, editstr(NewString,OutString,Xpos,Ypos,Cpos). editstr('\21',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr(InString,OutString,Xpos,Ypos,Cpos). % Insert a Character. editstr(Ch,InString,OutString,Xpos,Ypos,Cpos) :- makewindow(_,_,_,_,_,_,_,Width), str_len(InString,InStrLen), InStrLen < Width - 3 - Xpos, Ch >= ' ', Ch <= '~', frontstr(Cpos,InString,HeadString,TailStringP), frontchar(TailString,Ch,TailStringP), concat(HeadString,TailString,NewString), NewCpos = Cpos + 1, !, editstr(NewString,OutString,Xpos,Ypos,NewCpos). % Extended Key Hit. editstr('\0',InString,OutString,Xpos,Ypos,Cpos) :- readchar(Key), !, editstr(extended,Key,InString,OutString,Xpos,Ypos,Cpos). % Ignore All other keys pressed. editstr(_,InString,OutString,Xpos,Ypos,Cpos) :- editstr(InString,OutString,Xpos,Ypos,Cpos). % Process extended keys. % Del Key, map to the ^U key. editstr(extended,'\83',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr('\21',InString,OutString,Xpos,Ypos,Cpos). % Left Arrow, map to the ^S key. editstr(extended,'\75',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr('\19',InString,OutString,Xpos,Ypos,Cpos). % Right Arrow, map to the ^D key. editstr(extended,'\77',InString,OutString,Xpos,Ypos,Cpos) :- !, editstr('\4',InString,OutString,Xpos,Ypos,Cpos). % End. editstr(extended,'\79',InString,OutString,Xpos,Ypos,_) :- str_len(InString,InStrLen), !, editstr(InString,OutString,Xpos,Ypos,InStrLen). % Home. editstr(extended,'\71',InString,OutString,Xpos,Ypos,_) :- !, editstr(InString,OutString,Xpos,Ypos,0). % Ignore the rest. editstr(extended,_,InString,OutString,Xpos,Ypos,Cpos) :- !, editstr(InString,OutString,Xpos,Ypos,Cpos).