Keep your Prolog notes open beside your keyboard so you can look for help when things do not work as you expect. Also see the Hints at the end of this lab.
A Simple Predicate
In Prolog, a program is a set of definitions of predicates. Here
is start of a file that defines the relationship between the number
of a month and the name of the month (in lowercase letters). As in
all good programs the first line is a short comment.
% --------- month(Number, LongName). ---------
month(1, january).
month(2, february).
month(3, march).
...Carefully prepare a file called months.plg that has the above lines plus nine (9) more months (4,5,6,7,8,9,10,11,12). Check to make sure that there is a period at the end of each definition.
Start up prolog
pland ask prolog to compile your file:
consult('months.plg').
(don't forget that period!)
List the information about month:
listing(month).You can now use this data in many ways.
First, it can test to see if something is a particular month:
month(1,january).
month(2,january).
Second, it can test whether a number or atom is ANY month. Because _ matches any argument value at all:
month(1, _).
month(12,_).
month(13,_).
month(_,january).
month(_,janember).
Third, month can be used to generate the name given the number, as long as you give it a variable like X to put it in:
month(1, X).
month(2, X).
month(13,X).OR Vice versa you can generate the number from the name:
month(N, february).
month(N, march).OR, even be used to generate a whole series of months:
month(Number, Name).
(tap ';' after each pair)OR, a series of names, with no numbers:
month(_, Name).OR, a series of Numbers, with no Names:
month(N, _).
How about the summer months:
month(N,X), N>3, N<6.Or a pair of months:
(X=january; X=march), month(N,X).
Notice the leverage:
One set of data, many ways to use it.
Logic Puzzles
If you have a logic puzzle and can see how to modify
the examples in the last lab.... have a go at it.
Combinatorics
Can you extend the snow-cones example or perhaps modify it
to calculate combinations of pizza toppings?
A Prototype Student Data Base
retract( Query )looks up a matching piece of data in the usual way, but then deletes it from the data base. The predicate
assert( Fact )puts the query into the data base. Here are some examples of how this is done.
Again in modern Prolog systems we have to set up the dynamic predicates in advance:
dynamic(functor/arity).
Can you see how it records some information on 3 students?
Notice the clever use of Prolog operators to encode grades:
cs320=a: A grade of A in CS320
cs320+b: A grade of B+ in CS320
cs320-c: A grade of C- in CS320(This may not be a good idea... but it shows you that in Prolog you can make expressions mean anything you want).
Note: If we don't use a list of grades in the record
we have to have a separate set of facts linking students,
courses and grades. For more on data base design see CSci480.
Admissions and Records
Here is a very simple application that manipulates the student
records in students.plg:
[ ar.plg ]
It defines the following commands:
load_students.
list_students.
save_students.
admit(Student_number, Student_name).
dismiss(Student_number, Student_name).Notice it also needs you to have download [ students.plg ] first.
Run the ar.plg program, load the students, list them, add a new student with number 9999 and name 'Joe Coyote', list the result, and save the students. Quit Prolog and look at the result.
Study how the program is written. Notice the unfriendly user interface. Notice how simple the resulting code is. Hence a possible prototype to check out the algorithms and data structures, not a finished program.
Grades.
Look at the program to record new grades in student records:
[ grading.plg ]
Save this program. You will also need your students.plg and
ar.plg file. Start up the grading.plg program and use
consult('ar.plg').
load_students.to get the data online.
Add student with number 9999 and name 'Joe Coyote'. Hint. [ Admissions and Records ] above.
Give him the grade of a in cs125.
grade(9999, cs125=a).List the student records. Give Joe a grade of B+ in cs201 (cs201+b), and list the result.
Give Joe a grade of A- in CS320, and check the result...
If this works, study the code and then try some other experiments.
. . . . . . . . . ( end of section A Prototype Student Data Base) <<Contents | End>>
More on data bases
Also see
[ UML to Prolog ]
below.
If you have Time
cone(A,B,C), count, fail.The fail forces Prolog to backtrack and find another cone. We need the right definition for count. We want count to increase some hidden item of data each time is called, and we don't want it to undo anything when backtracking from the fail.
Prolog variables only have one value so we can not use them for counting (except recursively - one symbol but many variables!). We can't use a variable so we use the Prolog database: Initially we have no cones:
cones(0).To count a cone we:
count:-retract(cones(Old)), New is Old+1, assert(cones(New)).(Prolog uses the jargon word retract to mean `find the first matching rule and remove it from the data base`.)
Put together the definition of cone, scoop, cones, and count, in your cones.plg file and then test:
cone(A,B, C), count, fail.
cones(Number).
Prolog List Processing
See Sebesta Section 16.6.7 for examples of handling lists.
Ask Prolog for a listing of
appendStudy it. Test it out. Trace it. Describe in English how 'append' works.
Problems and programms From the Book
Sebesta has some example problems and programing exercises
at th end of the chapter. Some of these are in our SWI-Prolog
already.
For more on Prolog
Schnupp and Bernard have written an excellent introduction.
Their book is in the CSUSB library.
If you want to learn the techniques used for expert systems, simulations, and half a dozen other purposes... see T Van Le's text book.
I have a page of pointers to Prolog information: [ prolog.html ]
Also see a selection of messages from the SWI-Prolog mailing list [ mbox ]
The SWI manual (with some GIFs missing) is in [ http://www.csci.csusb.edu/dick/cs320/prolog/SWI-prolog/ ] and the Gnu Prolog manual is at [ http://www.csci.csusb.edu/dick/cs320/prolog/gnu/ ] and describes how to use gprolog and all the predicates it defines.
Hints
Study these hints if unexpected things happen...
End of line does not count.
X is a variable - with an unknown (as yet?) value.
x is the atom x
pl
consult('name_of_file').
EDIT=name_of_your_favorite_editor
VISUAL=$EDIT
EDITOR=$EDIT
export EDIT VISUAL EDITOR
The next time you login most UNIX programs will know what editor
you like to use.
UML to Prolog
Speaking of data bases.... You can often do a very quick translation
from a UML class diagram into a prototype Prolog data base. Each
box becomes a predicate. The chemistry example had a predicate
called element which would come from the UML class Element
with 8 public attributes: name:atom, symbol:string, etc. . The
operations would typically be written as rules. Operations
that change the state of the object will need to retract the
old attribute values and assert the new ones in place of
the old ones. Relationships
between classes can be code as: a predicate relating the two classes,
a list stored as an parameter of a predicate in a class, or a
single parameter identifying a unique corresponding object in
the other calls. Inheritance is harder but you can write
Prolog rules that attempt to find information in one predicate,
and if it fails goes else where. You can also write rules that
will search a series of predicates to find which one needs changing.
element(Name, _, Number1), element(Name, _, Number2), Number1=\=Number2.
. . . . . . . . . ( end of section CS320 Prolog Examples) <<Contents | End>>