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.
You can build your data base and predicates to model any world that you are interested in.
I'm looking for a small "proof of concept". Keep it simple! I do not require a perfect working Prolog program -- just a prototype that could be improved to something useful.
You need a source code file to link to your page. Develop this in your favorite editor with extension ".plg". You can compile and test it using 'swipl' or 'Q' on our lab systems.
% --------- 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
swipland 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.
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.
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.
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>>
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).
appendStudy it. Test it out. Trace it. Describe in English how 'append' works.
. . . . . . . . . ( end of section CS320 Prolog Examples) <<Contents | End>>
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://cse.csusb.edu/dick/cs320/prolog/SWI-prolog/ ] and the Gnu Prolog manual is at [ http://cse.csusb.edu/dick/cs320/prolog/gnu/ ] and describes how to use gprolog and all the predicates it defines.
End of line does not count.
X is a variable - with an unknown (as yet?) value.
x is the atom x
swipl
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.
. . . . . . . . . ( end of section CS320 Prolog Programming) <<Contents | End>>