This page generated at Mon May 18 19:43:40 PDT 1998.
Note.
element(Name).
element(Name, Symbol).
element(Name, Symbol, Number).I also define some properties of the atomic numbers: Is the element metallic or non-metallic, and what group and period in the periodic table is assigned to the element. There also three utility commands that print information about an element: period(Atomic_number), group(Atomic_Number), and print_element(Name_of_an_element).
The data and program are in [ chem.plg ] save/download a copy of this file. Load it into Prolog and try a query:
print_element(boron).To see how this works list the rolog program:
listing(print_element).Now try the following simple searches/queries of our knowledge base:
element(boron, Symbol, Number).
element(Name, 'F', Number).
element(Name, Symbol, 14).
element(barium, 'Ba', 56).
element(hydrogen, 'H', 24).
element(hydrogen, 'H', Nbr, Rad, Class,Normally,Melt, Boil).
element(Name, _, _, radioactive, _,_,_, _).The last of these above has several answers... tap ';' to get each radioactive element in turn.
Problem: I just typed it in and haven't checked it for errors.
Write simple Prolog queries that will detect some of the possible errors that I might have made:
% month(Number, Long, Short, Days)
month(1, 'January', jan, 31).
...
month(12, 'December', dec, 31).However, the month of February has a problem.....
year(1998).(by using assert(year(1998)).).
Then
Year(N)will set N to the year for us and we can use it to find out whether February has 28 or 29 days.
% month(Number, Long, Short, Days)
month(1, 'January', jan, 31).
month(2, 'February', feb, 28):-
year(N),
( N mod 4 =:=0, N mod 100 =\=0
; N mod 400 =:= 0
).
month(2, 'February', feb, 29).
...
month(12, 'December', dec, 31).We can mix and match simple facts and more complex clause in a set of definitions.
Prepare a file "months.plg" based on these hints and test it out. I think I got the formula for leap year right... but check it!
(added later.... And also check for other stupid errors....:-)
Asking our user to type:
assert(year(1998)).is not friendly.... so we might add to the "months.plg" the following definition (which is less than perfect!):
set_year:-write('Input the year ending with a period: '), read(Year), assert(year(Year)).
Compile and run this and test it.... (use 'listing.' to see the data base).
Here is the catch: if you set_year twice, prolog learns that there are two years (try it!).
So we need to be able to cancel out all pre-existing year(N) records before we save the new one. The "abolish/2" predicate does this:
set_year:-write('Input the year ending with a period: '), read(Year),
abolish(year,1), assert(year(Year)).
Replace the previous set_year command by the above and test it out.
Finally.... you can also define set_year(N) as a non-interactive version. Here the user supplies the year directly:
set_year(1998).for example. Complete the following:
set_year(N):-You could then rewrite the interactive one:
set_year:-write('Input the year ending with a period: '), read(Year),
set_year(N).
Prolog will let you have both forms in one program and they are called set_year/0 and set_year/1 in any documentation.
Add the two versions of set_year (set_year/0 and set_year/1) to your months.plg file and test the result.
If you like you can take this much further... setting today's date, calculating tomorrow's date,...
Write a file 'cones.plg' that has a Prolog predicate that defines all possible scoop(Flavor). Test it with:
scoop(Top).(and don't forget to tap ';' to ask for each cone in turn.)
Then try two scoops of ice-cream.
scoop(Top),scoop(Bottom).(and don't forget to tap ';' to ask for each cone in turn.) Also try the following simple Prolog loop....
scoop(Top),write(Top),nl,fail.
Notice how we put fail at the end of the loop, because it forces Prolog to go back and find the next alternative, and in the above, only scoop provides a choice point.
We can use the same technique with two scoops... to generate all combinations:
scoop(Top),scoop(Bottom),write(Top+Bottom),nl,fail.Also notice that we can use + in the output.... because Prolog will write X+Y without evaluating it!
Edit your 'cones.plg' file so that it defines a predicate cone(Top, Middle, Bottom) and test it:
cone(Top,Middle,Bottom),
write(Top+Middle+Bottom),nl,fail.
Try the following which generates a database of all the possible cones, ready for further computation:
cone(A,B,C), assert( a_cone(A,B,C) ).
listing(a_cone).
Prolog has some high-level commands for handling list, sets, and "bags". (A bag can have elements that occur several times). The bagof(Term, Condition, Bag) predicate is built into our Prolog and assembles a list in Bag of all Terms that fit the Condition. Load your 'cones.plg' into Prolog and try the following:
bagof(X, scoop(X), Scoops).
bagof(X+Y, (scoop(X), scoop(Y)), Scoops).and, using the length(List, Length) predicate:
bagof(A+B+C, cone(A,B,C), Cones), length(Cones, Number_cones).Thank you Tracy for correcting the above!
cone(A,B,C), count, fail.were we need the right definition for count. The fail forces Prolog to backtrack and find another cone. 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:i 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: Initial 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).
Modify the program to count the cones must have three different flavors.
How about 4 scoops?
Then express the following simple example as a single Prolog query and then see what the answers are.
D1, D2, D2 are decimal digits,
and
X is the digit decimal number with digits D1,D2,D3,
and
X is also equal to D1^3 + D2^3 +D3^3.Note.
The three digit number X is 100*D1+10*D2+D3.
Finally find all 4 digit numbers whose digits, when cubed add up to them selves.
For all natural numbers (integers>0) X and Y:
div(X,Y) is true if Y divided by X leaves no remainder.Hint. apropos(division).
For all natural numbers (integers>0) X and Y:
nodivisors(X,Y) is true if and only if
either
X is less than 2
or.As-is X is greater than equal to 2
and
X does not divide Y,
and
for X1 is X-1, nodivisors(X1,Y).
For any natural number N,
prime(N) is true if and only if N has no divisors between 2 and N-1.
How about defining 'composite(N)' in terms of 'prime(N)'.
go.Then time bsearch (see help(time)), and then see if you can improve it.
The challenge of the puzzle becomes extracting the properties of the attributes from the clues you've been given. If you miss a "given" then this algorithm may generate a solution that is not a solution of the problem.
Load the bad solution and investigate why and how it fails, using trace to help. What are the problems?
Later I produced [ lotto93.plg ] that seems to have the problem licked by using a smarter idea and a way to express a 'for loop' in Prolog.
qsort(Given_list, Variable_for_sorted_list).Exercise - modify either of the above so that instead of sorting numbers into increasing order they can sort a list of any terms into any order. The call would become:
qsort(Given, Goal, Order).where Order would be the name of a predicate with two arguments, that is true precisely when the two arguments are in the right order, so
qsort(Given, Goal, <).should work.
More advanced exercise....
qsort(Given, Goal, lambda([Var1,Var2], Expression)).
help(sort).
help(setof).or trying out [ setof.plg ]
. . . . . . . . . ( end of section More sample Programs) <<Contents | Index>>
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 ]
. . . . . . . . . ( end of section CS320 Third Prolog Laboratory -- Writing Prolog "Programs") <<Contents | Index>>