home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
proglang
/
pie2.arj
/
PIEDOC.DOC
< prev
next >
Wrap
Text File
|
2000-01-01
|
37KB
|
957 lines
PIE is a public domain Prolog Interpreter. You are encouraged to share
this program with others. To start PIE go to the subdirectory where PIE is
installed and issue the command "PIE" followed by <ENTER>. PIE will load and
you will be facing the "Goal:" prompt. PIE will execute whatever valid
command you give it. I recommend that you first load and execute the
demonstration programs that are included with PIE. These programs are as
follows:
piedemo.pro To load "piedemo.pro" issue the command
"consult("piedemo.pro")" followed by <ENTER>. You can
execute "piedemo.pro" by issuing the command "start"
followed by <ENTER>. The program "piedemo.pro" will
demonstrate some of PIE's graphics capability.
chem.pro To load "chem.pro" issue the command "consult("chem.pro")"
followed by <ENTER>. You can execute the program
"chem.pro" by issuing the command "start(1) followed by
<ENTER>. The program "chem.pro" defines an organic
molecule and analyzes it by looking for functional groups.
eliza.pro To load "eliza.pro" issue the command
"consult("eliza.pro")" followed by <ENTER>. Execute
"eliza.pro" by issuing the command "converse" followed by
<ENTER>. The program "eliza.pro" mimics a psychologist and
allows the user to interact with it. Don't ever call it a
jerk though. It will take offense to that insult.
turtle.pro To load "turtle.pro" issue the command
"consult("turtle.pro")" followed by <ENTER>. To execute
"turtle.pro" issue the command "start followed by <ENTER>.
The program "turtle.pro" will mimic an etch-a-scetch.
Press 'l' to go left, press 'r' to go right, press 'u' to
go up, press 'd' to go down or press '*' to quit.
object.pro To load "object.pro" issue the command
"consult("object.pro")" followed by <ENTER>. To execute
"object.pro" issue the command "start" followed by <ENTER>.
The program "object.pro" is a whimsical demonstration of
object oriented programming in Prolog.
Other useful commands when you are just starting with PIE include the
following:
retract(_),fail To clear a program out of memory. Issue this command to
clear the memory before consulting a new program.
edit("<filename>") This command loads a program into the full screen editor so
that you can modify it. If you exit the editor with the
F10 key PIE will consult the edited program so that it can
be run.
trace This command should be issued before running a program if
you want to trace the execution of the program step by
step. For example the command "trace,start" will execute
the "start" command and allow you to trace it's execution.
The trace command does not work well with graphics programs
since it is a text oriented feature.
system This command allows you to shell out to DOS and execute DOS
commands. Type "exit" followed by <ENTER> to return to
PIE.
halt The "halt command exits PIE and returns you to DOS.
Introduction to Prolog
While I will provide some instuction in Prolog programming this
documentation is not intended to be a complete guide on the subject. For that
I recommend the book "Prolog Programming in Depth" by Michael A. Covington,
Donald Nute and Andre Vellino.
To program your first Prolog program type the command:
edit("first.pro")
and press <ENTER>. This command will put you in the programming text editor.
Now type the following lines:
hello:-
write("Hello World"),
nl,
readchar(_).
and press the F10 key to return to the goal prompt. Be very careful since one
little mistake can cause the program to crash. If you entered the program
correctly the interpreter will say:
true
1 Solution
To run this program type "hello" and press <ENTER>. The program will execute
and print the words "Hello World" on the screen and wait for you to press a
key.
Now type the command "edit("first.pro")" and press <ENTER>. Now change
your program to the following:
hello:-
write("Hello World"),
nl,
readchar(_),
introduce.
introduce:-
write("What is your name?"),
readln(Name),
write("Hello ",Name),
readchar(_).
- 2 -
and press the F10 key to return to the goal prompt. Notice that the routine
named "introduce" is called the same way as the built-in commands (called
predicates) are. In effect programming in Prolog is building new predicates
and calling them from other predicates. The readln predicate uses the
variable "Name". Note that all variable names must begin with a capital
letter. Also make sure that the periods and commas are in their correct
locations. Run your program by issuing the command "hello" and pressing
<ENTER>. The program will do the following:
- write "Hello World"
- wait for you to press the space bar
- ask for your name
- wait for you to enter the name and press <ENTER>
- write "Hello" to you
To trace the execution of the program you can issue the command
"trace,hello" and press <ENTER>. The interpreter will allow you to single
step though the program one step at a time. Press the <SPACE> bar to execute
each command. When you are back to the goal prompt issue the command
"edit("first.pro")" and press <ENTER>. Now change your program to:
hello:-
write("Hello World"),
nl,
readchar(_),
introduce.
introduce:-
write("What is your name?"),
readln(Name),
write("Hello ",Name),
readchar(_),
makelist(Name,[],List),
write(List),
readchar(_).
makelist("",List,List):-!.
makelist(Name,Accumulator,List):-
frontchar(Name,Char,Rest),
makelist(Rest,[Char|Accumulator],List).
and press the F10 key. This program will take your name and separate it into
a list of characters after it has gone through the greeting phase of the
previous program. The predicate makelist is different than the introduce and
hello predicates in two ways. First, it uses parameters passed from the
introduce predicate. The parameters are as follows:
Name The name variable is bound to the name you typed in.
- 3 -
[] The empty brackets signify that this list is an empty list.
List This will be the variable that will contain the list of
characters in your name. At this point of the program it
has not been bound to a value and is therefore considered a
free variable.
The second difference is that it consists two clauses. Predicates can have
any number of different clauses. The first clause:
makelist("",List,List):-!.
signals that your name has been completely separated into a list of charaters
with the first parameter being empty quote marks. The second parameter
represented by the variable List contains the list of characters that are in
your name in reverse order. The third parameter contains the List variable
which is bound to the list characters found in your name. By placing a bound
variable in the position of the third parameter that parameter becomes bound
and that list will be returned to the calling predicate. The '!' tells the
interpreter that it is not to try to execute the makelist predicate again at
this time.
If the makelist predicate has not removed all of the characters then the
first clause of the makelist predicate will fail. Failure in Prolog is not a
serious occurance and Prolog will simply go on to process another clause. The
second clause of the makelist predicate:
makelist(Name,Accumulator,List):-
frontchar(Name,Char,Rest),
makelist(Rest,[Char|Accumulator],List).
will remove the first character from your name and add that character to the
list of characters it is adding to the Accumulator variable. Notice the
notation used in the last second parameter of the last line of the clause to
represent a list. The list is divided into two parts. The first part is the
head which consists of the first item of the list. The second part consists
of the rest of the list. Lists are used in Prolog instead of arrays.
The last line of the second clause of the makelist predicate calls itself
again. This ability for a predicate to call itself is called recursion. In
the recursive call the first parameter is the Rest variable which is bound to
your name minus the first character. The second parameter is the list of
characters that have been removed from your name. The third parameter has not
yet been bound to a list yet. This will take place whenever the name has had
all of it's characters removed and added to the list being accumulated in the
second parameter.
To run this program issue the command "hello" and press <ENTER>. The
program will do the following:
- write "Hello World"
- wait for you to press the space bar
- ask for your name
- wait for you to enter the name and press <ENTER>
- 4 -
- write "Hello" to you
- write a list with the characters in your name in reverse order
You can learn much about Prolog by using the trace command to single step
through this program.
This ends my short tutorial on Prolog. Again, I can't over estimate the
importance of obtaining a good book on Prolog programming techniques.
Language Reference
! - The cut prevents backtracking or in other words, does not
allow Prolog to try different solutions. A cut is
especially helpful in increasing the efficiency of
programs. For instance, a cut immediately before a tail
recursive call will invoke tail recursion optimization
which will greatly increase the efficiency of the program
execution.
arg(N,TERM,ARGn) This command provides a way to extract parts (called
arguments) from a term. For example the call:
arg(3,[1,2,3,4,5,6],Arg)
will extract the number '3' from the list and return it as
the value of Arg.
assert(RULE) - The assert command will place a copy of RULE at the end
of Prolog's dynamic database. This command is the same as
the assertz(RULE) command.
asserta(RULE) - The asserta command will place a copy of RULE at the
beginning of Prolog's dynamic database.
assertz(RULE) - The assertz command will place a copy of RULE at the end
of Prolog's dynamic database. This command is the same as
the assert(RULE) command.
call(GOAL) - The call(GOAL) predicate will execute the term specified
in the variable in GOAL. The call(GOAL) predicate will
also accept a string which it will try to parse into an
valid Prolog goal and execute it. For example, both
call("edit")
and
call(edit)
will call the edit predicate.
char_int(CHAR,INT) - The char_int(CHAR,INT) predicate will convert a character
to an integer that is equal to the ASCII value for that
integer or convert an integer to the ASCII character that
is represented by that integer.
char_real(CHAR,REAL)
- The char_real is the same as the char_int predicate
except that it handles real numbers instead of just
- 5 -
integers. For most uses the char_real predicate is
preferred.
clause(HEAD,BODY) - return clauses from the database
clearwindow - The clearwindow predicate clears the current window and
sets the cursor to the upper left corner of that window.
colorsetup(Integer)
- The colorsetup command will allow the user to
interactively change the color of the current window if
Integer is equal to '0' or the frame around the window if
Integer is equal to '1'.
concat(STRING,STRING,STRING)
- The concat predicate is used for combining and dividing
strings. For example:
concat("Hello ","World",A)
A will equal the string "Hello World".
concat("Hello ",A,"Hello World")
A will equal the string "World"
concat(A,"World","Hello World")
A will equal the string "Hello "
consult(FILENAME) - The consult predicate will read in a file and add the
clauses to Prolog's dynamic database. This is the standard
way to initially load a program.
consult_str(STRING)
- The consult_str(STRING) predicate will parse the contents
of the variable string and add the clauses to Prolog's
dynamic database.
cursor(Row,Col) - The cursor predicate allows the programmer to specify
where on a given window the cursor will be located or it
can be used to return the location of the cursor.
cutbacktrack(BTOP) - dynamic cut to designated backtrack point
date(YEAR,MONTH,DAY)
- The date(YEAR,MONTH,DAY) predicate returns the system
date.
debug - debug will cause the tokens generated by the scanner to
be displayed if there is a error in parsing.
dir(Path,Filemask,Filename)
- The dir(Path,Filemask, Filename) will call a directory
function in the current window. The user can move the
cursor to scroll through the list of files and select one
that will be returned in the Filename variable. For
example:
dir("c:\pie","*.pro",Filename)
- 6 -
will display a list of file names in the "c:\pie"
subdirectory with the file extension of "*.pro".
display(TERM*) - outputs functors in prefix notation
edit(File) - The edit(File) predicate will allow the user to edit a
program file specified by the File variable. When the
editor is exited the file is consulted into Prolog's
dynamic database.
edit(pred/arity) - edit selected predicate
edit_file(FILE) The edit_file(FILE) predicate allows the user to edit a
text file whose name is specified in the FILE variable.
The file is not consulted after the editor is exited.
edit - The edit command allows the user to edit the current
clauses in the dynamic database. When the editor is edited
the clauses are consulted into Prolog's dynamic database.
eof - If the eof predicate is true then the file pointer is at
the end of the current seeing file. Otherwise it fails.
existwindow(WindowNo)
- The existwindow(WindoNo) predicate tests to see if the
window whose number is specified in the WindoNo variable
exists.
fail - The fail predicate always fails and forces Prolog to
backtrack.
file_str(FILENAME,STRING)
- The file_string(FILENAME,STRING) predicate loads the
contents of the file whose name is specified in the
FILENAME variable into the STRING variable.
frontchar(STRING,CHAR,REST)
- The frontchar(STRING,TOKEN,REST) predicate removes (or
adds) the first character of a string. This predicate is
used to break strings into their component characters.
frontstr(REAL,STRING,FIRST,END)
- The frontstr(REAL,STRING,FIRST,END) predicate will remove
the first string with a length defined by the REAL variable
from the original string.
fronttoken(STRING,TOKEN,REST)
- The fronttoken(STRING,TOKEN,REST) predicate is used to
break strings into tokens. A token can either be a number,
word or a symbol. A common use for the fronttoken
predicate is to remove a token from a string. For example
in the call:
fronttoken("Hello there.",Token,Rest)
- 7 -
the variable Token will equal "Hello" and the variable rest
will equal " there.".
functor(TERM,FUNCTOR,ARITY)
- returns the functor and arity of a term or build a new
functor.
getbacktrack(BTOP) - gets backtrack point for dynamic cut.
help - The help predicate displays the help file in the current
window. Press the F10 or <ESCAPE> key to exit.
integer(TERM) - tests the trerm to see if it is an integer.
REAL is EXPRESSION - The is predicate evaluates arithmetic expressions. The
arithmetic expression must be on the right side and the
answer must be on the left. The full range of math
operators is listed below:
+ - plus
- - minus
* - multiplication
/ - division
mod - modulo
abs - absolute value
cos - cosine (units are in radians)
sin - sine (units are in radians)
tan - tangent (units are in radians)
arctan - arctangent (units are in radians)
exp - returns e raised to a power
ln - natural logarithm
log - logarithm
sqrt - square root
round - round
trunc - truncate
list(pred) - list all clauses for pred.
list(pred/arity) - list selected arity.
- 8 -
list - list all clauses.
makewindow(WinNo,ScrAtt,FAtt,FString,Row,Col,Height,Width)
- The
makewindow(Winno,ScrAtt,FAtt,FString,Row,Height,Width)
predicate will create a window with the following
attributes:
Winno - The window number. The programmer should
start numbering windows from 4 since numbers
1 - 3 are used for the system windows.
ScrAtt - Defines the screen color inside of the
window.
FAtt - Defines the color of the frame around the
window.
FString - This is the string that is displayed at the
top of the window. This string is
automatically centered.
Row - Specifies the row where the top of the
window starts relative to the whole screen.
Col - Specifies the column where the left of the
window starts relative to the whole screen.
nl - The nl predicate outputs a carriage return and linefeed
to the current output device whether that is the screen or
the telling file.
nonvar(TERM) - tests to see if the term is bound.
not(GOAL) - The not(GOAL) predicate reverses the success of the goal
specified in the GOAL variable. For example the call:
not(true)
is the same as the fail predicate.
notrace - turns off tracing.
op(PRIORITY,ASSOC,OP)
- returns operators, or changes operator precedence.
random(REAL) - The random(REAL) predicate invokes the random number
generator to generate a real number between or equal to
zero and less than one.
randominit(INTEGER)
- The randominit(INTEGER) reinitializes the random number
generator. The integer specified in the INTEGER variable
is a seed number. If randominit is not called before using
the random predicate then Prolog will use 1 as the seed
number for the random series.
- 9 -
read(TERM) - The read(TERM) predicate will read a term from the
current input device into the TERM variable. Normally the
current input device is the keyboard but can directed to a
file with the see predicate.
readchar(CHAR) - The readchar(CHAR) reads a character from the current
input device into the CHAR variable.
readln(LINE) - The readln(LINE) reads a string from the current input
device until a carriage return is encountered. The string
is read into the LINE variable.
real(TERM) - tests to see if the term is a real.
reconsult(FILENAME)
- The reconsult(FILENAME) retracts all of the current terms
in the Prolog database and consults the contents of the
file specified in the FILENAME variable.
removewindow - The removewindow predicate removes the current window.
Don't remove the system windows or PIE may act strangely.
repeat - The repeat predicate is always true and provides an
infinite number of potential solutions.
repeat(GOAL) - The repeat(GOAL) predicate is always true unless the goal
specified in the GOAL variable fails. This predicate will
provide multiple solutions until some condition is met.
For example:
repeat(not(eof))
will generate new solutions until the end of the seeing
file is reached.
resizewindow - The resizewindow predicate provides the user with a way
to interactively resize the current window. Press the F10
key to exit this function.
retract(TERM) - The retract(TERM) predicate allows the programmer or user
to remove clauses from Prolog's dynamic database. This
facility provides a program a means to remove part of
itself while it is executing and reclaim the memory used by
the removed clauses.
save(FILENAME) - The save(FILENAME) will save the clauses in Prolog's
dynamic database into a file specified by the FILENAME
variable.
scr_char(ROW,COL,CHAR)
- The scr_char(ROW,COL,CHAR) will print a character
specified by the CHAR variable at the location on the
screen specified by the ROW and COL variable.
- 10 -
searchstring(STRING,SEARCHSTRING,LOCATION)
- The searchstring(STRING,SEARCHSTRING,LOCATION) will
locate a string specified by the SEARCHSTRING variable in
the string specified by the STRING variable. The starting
location of the string specified by the SEARCHSTRING
variable is returned in the LOCATION variable.
see(FILENAME) - The see(FILENAME) predicate opens the file whose name is
specified by the FILENAME variable and redirects the input
to this file.
seeing(FILENAME) - The seeing(FILENAME) predicate will return in the
FILENAME variable the name of the current file being used
for input.
seen - The seen predicate will close the file currently being
used for input and redirect the input to the keyboard.
shiftwindow(No) - The shiftwindow(No) predicate will change the current
window to the window specified by the No predicate. The
window shifted to will be automatically moved to the
foreground.
storage - The storage predicate will write to the current output
device a description of the system's use of the different
memory areas.
str_atom(STRING,ATOM)
- The str_atom(STRING,ATOM) predicate will convert between
a string specified in the STRING variable and an atom
specified in the ATOM variable.
str_int(STRING,INT)
- The str_int(STRING,INT) predicate converts between a
string specified in the STRING variable and an integer
specified in the INT variable.
str_len(STRING,LENGTH)
- The str_len(STRING,LENGTH) predicate will return the
length of a string specified by the STRING variable. It
will also return a string of blanks of a length specified
by the LENGTH predicate.
substring(STRING,START,LENGTH,SUBSTRING)
- The substring(STRING,START,LENGTH,SUBSTRING) predicate
will extract a substring returned in the SUBSTRING variable
from a string specified by the STRING variable starting at
a location specified in the START variable with a length
specified by the LENGTH variable. For example, the call:
substring("ABEKAT",5,2,SUBSTRING)
will return the string "AT" in the SUBSTRING variable.
system - The system command allows the user to access the
operating system from within a Prolog program.
- 11 -
system(Command,VideoReset,Error)
- The system(Command,VideoReset,Error) command provides a
method for a Prolog program to execute a DOS command from
within a Prolog program. The DOS command is specified as a
string in the Command variable. The VideoReset variable
will specify whether or not the screen will be reset when
the DOS command is executed. If the VideoReset variable
equals 0 the screen is not reset. The number 1 will cause
the screen to be reset. Any DOS errors that are returned
by the system are returned in the Error variable.
tell(FILENAME) - The tell(FILENAME) opens a file whose name is specified
by the FILENAME variable and redirect the programs output
to that file.
telling(FILENAME) - The telling(FILENAME) will return the name of the current
output file in the FILENAME variable.
TERM =.. LIST - univ; conversion between term and list
TERM == TERM - testing for true equality
TERM =< TERM - less than or equal
TERM = TERM - unify two terms
TERM < TERM - integer less than
TERM >= TERM - greater than or equal
TERM >< TERM - different evaluated values
TERM > TERM - integer greater than
TERM \== TERM - not true equality
TERM \= TERM - test whether unify
time(HOUR,MIN,SEC,HUNDREDS)
- The time(HOUR,MIN.SEC,HUNDREDS) predicate will set or
return the system time.
told - The told predicate closes the current output file and
redirects the programs output to the screen.
trace - The trace predicate turns on the Prolog's tracing
facility which allows the user to single step through the
programs execution with a display of the calls to the
predicates in the program execution.
true - The true predicate simply is true and always succeeds.
var(TERM) - tests to see if the TERM variable is free.
- 12 -
VAR - calls the predicate to which VAR is bound.
window_str(String) - The window_str(STRING) will write or read a string
specified or returned in the STRING variable.
write(TERM*) - The write(TERM*) is Prolog's standard way of writing to
the current output device. Any number of terms can be used
with the write predicate.
Math operators
=============================
+ - plus
- - minus
* - multiplication
/ - division
mod - modulo
abs - absolute value
cos - cosine (units are in radians)
sin - sine (units are in radians)
tan - tangent (units are in radians)
arctan - arctangent (units are in radians)
exp - returns e raised to a power
ln - natural logarithm
log - logarithm
sqrt - square root
round - round
trunc - truncate
Graphics System
The graphics are based on a virtual screen. The lower left hand corner
is 0,0 and the upper right hand is 32000,32000. It does not matter what
graphic adapter is used, the screen coordinates are the same. In addition to
the normal lines, dots, ellipses etc., there are also turtle graphics commands
and Bezier curves. The graphics predicates are as follows:
angle(Angle)-(i)(o)
- The angle(ANGLE) predicate allows the programmer to set
or read the current angle the "turtle" is facing. If the
"turtle" is facing to the right the angle will equal 0. If
the "turtle" is facing straight up the angle is 90.
beep - The beep predicate simply beeps the computer.
bezier(X1,Y1,CX1,CY1,CX2,CY2,X2,Y2)-(i,i,i,i,i,i,i,i)
-
The bezier(X1,Y1,CX1,CY1,CX2,CY2,X2,Y2) predicate draws a
Bezier curve with the end points X1,Y1 and X2,Y2 and with
the control points CX1,CY1 and CX2,CY2.
bkcolor(Color)-(i) - The bkcolor(Color) predicate sets the background color to
the color specified by the Color variable.
- 13 -
circle(X,Y,Radius)-(i,i,i)
- The circle(X,Y,Radius) draws a circle with a center at
the coordinates specified by the X and Y variables and with
a radius specified by the Radius variable.
closegraph - The closegraph predicate closes the graphics system.
dot(X,Y)-(i,i) - The dot(X,Y) predicate makes a dot at the location with
the coordinates specified by X and Y variables.
dot - makes a dot at the current location.
draw(GraphicsDriver,GraphMode)-(i,i)
- sets the computer to graphics mode using the driver
specified in GraphicsDriver and the graphics mode specified
by GraphMode.
draw(GraphMode)-(i)
- sets the computer to graphics mode specified by
GraphMode. The graphics driver is automatically called.
draw - sets the computer to graphics mode. The graphics driver
and mode are automatically determined.
drawpoly(Pointlist)-(i,i,...i,i)
- draws a polygon with the point pairs specified.
elipse(X,Y,StAngle,EdAngle,Xradius,Yradius)-(i,i,i,i,i,i)
- draws an ellipse centered at X,Y starting at StAngle and
ending at EdAngle with X and Y radii of Xradius and
Yradius.
ellipse(X,Y,Xradius,Yradius)-(i,i,i,i)
- draws an ellipse centered at X,Y with X and Y radii of
Xradius and Yradius.
fillstyle(Fillpattern,Fillcolor)-(i,i)(o,o)
- sets or returns the Fillpattern and Fillcolor.
floodfill(X,Y)-(i,i)
- floodfills an area with a pattern.
line(X1,Y1,X2,Y2)-(i,i,i,i)
- makes a line from X1,Y1 to X2,Y2.
lineto(X,Y)-(i,i) - makes a line from the current location to X,Y.
move(Steps)-(i) - moves the turtle a certain number of steps in the
direction specified by the current angle. Negative numbers
moves the turtle backwards.
outtext(Text)-(i) - writes Text to the screen at the current location and in
the current font and color
- 14 -
outtext(X,Y,Text)-(i,i,i)
- writes Text to the screen at X,Y in the current font and
color
pendown(Status)-(i)
- reports the marking status of the turtle. The number 0
means the pen is up and 1 means the pen is down.
pendown - causes the turtle to start making marks when it is moved
with the move predicate.
penpos(X,Y)-(i,i)(o,o)
- sets or returns current pen location.
penup - causes the turtle to stop making marks when it is moved.
rectangle(X1,Y1,X2,Y2)-(i,i,i,i)
- draws a rectangle with the upper left corner located at
X1,Y1 and the lower right corner located at X2,Y2.
settextstyle(Font,Direction,Charsize)-(i,i,i)
- sets the current font, Direction, and Charsize. The
fonts are specified as: 0 = default bitmapped, 1 = triplex,
2 = small, 3 = sans serif, 4 = gothic. Direction is
specified as: 0 = horizontal (default) and 1 = vertical.
Charsize is an integer that specifies a magnification
factor.
smooth(Setting)-(i)(o)
- sets or returns the smoothing value for the bezier
curves. A higher number increases the smoothness of the
curves but increases the time taken to draw the curves.
The default value is 5.
sound(Duration,Frequency)-(i,i)
- produces a sound with the specified Duration and
Frequency.
text - closes the graphics system and restores the text mode.
turn(Angle)-(i) - turns the turtle the degrees specified in Angle.
Positive angles turn the turtle left and negative angles
turn the turtle right.
- 15 -