|
|
|
|
Artificial Intelligence
eXercise #3
Prolog Programming
Each exercise that follows requires that you use the given programs
either as they are, or in some modified form, to answer some questions.
It is important that you make notes of how you went about answering
the questions. It is through error and trial that you will learn most
in this exercise.
- Greeting
This program is designed to demonstrate a procedural aspect of PROLOG
- that is its ability to handle regular programming type things. You
will notice that the program has an internal goal, and that the clauses
section of the program contains one long task.
/*greeting*/
predicates
hello
goal
hello.
clauses
hello :-
makewindow(1,7,7,"Hello",4,56,10,22) and
nl and write("please type in your name : ") and
cursor(4,5) and
readln(Name) and nl and
write(" Hello ",Name," and I really mean that").
You will notice that the WINDOW appears in the DIALOGUE box, and is
named (in the definition). Other things to notice : variables are
objects with labels beginning with a capital letter, nl means NewLine,
there are conventional readln and write statements (writeln doesn't
exist!).
- change the makewindow definition to discover what the parameters
do
- change the prompts, add extra ones that prompt and respond
- choose new cursor positions
- Phone Book
This program is an extension of the greeting program. In it, we have
much the same prompting, linked to a collection of clauses that provide
a 'database'. It is PROLOGs aim to complete a goal if given one (if
possible) - after the user input, part of the goal searches the stored
clauses for instances that match. If there are any variables specified
in the goal, PROLOG tries to bind (attach, map, unify) the variable
with as many values as there are for it.
/*phone book*/
predicates
entry(symbol,symbol)
goal
write("please type in a name : ") and
readln(Name) and
entry(Name,PhoneNum) and
write("the PhNum is :",PhoneNum) and nl.
clauses
entry("Fred","07-3241697").
entry("Barney","02-1164287").
entry("Wilma","04-671234").
At the prompt, get prolog to display:
- the phone number of Fred
- all phone number-person combinations.
- Thesaurus
This program displays simple binding at the external goal. To find
similes for words, issue a goal with a variable as the second value
in the clause. Add some more clauses. See if you can add a section
of code that prompts the user for the word to search for, and displays
all similar words.
/*thesaurus*/
predicates
isSimilar(symbol,symbol)
clauses
isSimilar(big,huge).
isSimilar(big,large).
isSimilar(yes,affirmative).
isSimilar(no,negative).
Hint: use an internal goal similar to the goal in /*phone book*/ but
you will need also to append a fail operator (the words and fail)
prior to the full-stop. This forces PROLOG to backtrack and search
for other solutions, and keep searching (with the aim of getting to
the full-stop and hence completing its goal) until it does not find
any more matches (and hence fail = true ---> terminate)
- Car Buyers Guide
This program utilises your skill at composing external goals
/*car buyers guide*/
domains
brand, color = symbol
age = integer
mileage,price = real
predicates
isCar(brand,mileage,age,color,price)
clauses
isCar(datsun,30000,6,red,3000).
isCar(mercedes,10000,3,black,32000).
isCar(honda,130000,3,blue,6000).
isCar(datsun,75000,8,green,3000).
isCar(lancer,3000,1,red,12000).
isCar(holden_hq,300860,15,blue,500).
isCar(range_rover,5000,1,white,36000).
isCar(rolls_royce,1230000,10,grey,25000).
isCar(datsun,30000,6,red,2500).
isCar(magna,42000,4,silver,6500).
isCar(volvo,73000,4,red,21000).
isCar(vee_dub,130000,14,blue,13000).
isCar(model_t,430000,53,green,120000).
Find answers to the following questions (writing down your goals as
you go):
- all green cars
- all red cars that are datsuns
- all cars whose mileage is less than 12000
- all cars that cost less than $4000
- all cars that have more than 100000 mileage yet cost more than
10000
- all red cars that are more than 6 years old
- all cars that are less than 2 years old yet have more than 1000
miles
- all cars that are volvos
- all cars that are blue that are not hondas
- The Luck Of The Draw
/*the luck of the draw*/
domains
child = symbol
age = integer
predicates
pupil(child,age)
clauses
pupil(fred,9).
pupil(barney,8).
pupil(wilma,9).
pupil(pebbles,9).
pupil(bam_bam,8).
pupil(dino,9).
pupil(betty,8).
Again, your skill at writing external goals is to be tested, using
variable binding to form expressions that result in output that:
- list all pairs of 9 year olds possible
- list all pairs of mixed ages possible
Now mutate the program (slightly) to include gender of the pupil (this
will mean altering the predicate and the clauses), then try to execute
goals that:
- list all male 8 year old pairs possible
- list all mixed gender 9 year olds possible
- The Dynasty Database
/* The Dynasty Database */
/* Based on the Carrington Family from */
/* the TV series called Dynasty */
domains
person = symbol
predicates
is_parent_of(person,person)
is_female(person)
is_male(person)
clauses
is_parent_of(blake,stephen). is_parent_of(blake,adam).
is_parent_of(blake,falon). is_parent_of(blake,amanda).
is_parent_of(blake,christina). is_parent_of(alexis,stephen).
is_parent_of(alexis,adam). is_parent_of(alexis,falon).
is_parent_of(alexis,amanda). is_parent_of(crystal,christina).
is_parent_of(stephen,danny). is_parent_of(sammy_jo,danny).
is_parent_of(falon,little_blake).is_parent_of(geoff,little_blake).
is_female(alexis). is_female(crystal).
is_female(falon). is_female(amanda).
is_female(sammy_jo). is_female(christina).
is_male(blake). is_male(stephen).
is_male(adam). is_male(little_blake).
is_male(geoff). is_male(danny).
Download the Dynasty.txt file to your home
directory, rename it as dynasty.pro and then complete the following:
- From the data in the database draw a family tree of the Carrington
Family
- Write external goals that answer yes or no to the following
statements.
- Blake is Adam's parent.
- Danny has Blake as a parent.
- Sammy Jo is a male
- Alexis is a parent of Falon
- Crystal is a female
- Blake is the male parent of Sammy-Jo.
- Danny is the male child of Blake.
- Crystal is the mother of Falon.
- Geoff is the father of Danny.
- Amanda is Christina's daughter.
- Write external goals that will extract the required information
in the following examples.
- Who are Falon's parents?
- What are the names of all the males?
- List the names of Blake's children.
- Who is Amanda's mother?
- Who are Blake's sons.
- Who is Danny's grandmother?
- EXTRA FOR EXPERTS: Add clauses and corresponding predicates
to the database so that the following relationships are defined
(hint: once a rule is defined it is allowed to be called by another
rule).
TEST your clauses by issuing goals that use them, and check they
deliver the correct responses.
HINT: the clause: is_mother_of(Mum,Child) :- is_parent_of(Mum,Child)
and is_female(Mum).
You will also need to define a predicate for each new type of
clause. (eg. is_mother_of(person,person)
- is_father_of
- is_daughter_of
- is_grandparent_of
- is_grandfather_of
- is_granddaughter_of
- is_brother_of (full brother)
- is_cousin_of
|
|
|
|
|
|
|