IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
back
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

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.

  1. 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!).

    1. change the makewindow definition to discover what the parameters do
    2. change the prompts, add extra ones that prompt and respond
    3. choose new cursor positions


  2. 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:

    1. the phone number of Fred
    2. all phone number-person combinations.


  3. 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)

  4. 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):

  1. all green cars
  2. all red cars that are datsuns
  3. all cars whose mileage is less than 12000
  4. all cars that cost less than $4000
  5. all cars that have more than 100000 mileage yet cost more than 10000
  6. all red cars that are more than 6 years old
  7. all cars that are less than 2 years old yet have more than 1000 miles
  8. all cars that are volvos
  9. all cars that are blue that are not hondas

 

  1. 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:

    1. list all pairs of 9 year olds possible
    2. 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:

    1. list all male 8 year old pairs possible
    2. list all mixed gender 9 year olds possible


  2. 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:

    1. From the data in the database draw a family tree of the Carrington Family

    2. Write external goals that answer yes or no to the following statements.
      1. Blake is Adam's parent.
      2. Danny has Blake as a parent.
      3. Sammy Jo is a male
      4. Alexis is a parent of Falon
      5. Crystal is a female
      6. Blake is the male parent of Sammy-Jo.
      7. Danny is the male child of Blake.
      8. Crystal is the mother of Falon.
      9. Geoff is the father of Danny.
      10. Amanda is Christina's daughter.


    3. Write external goals that will extract the required information in the following examples.
      1. Who are Falon's parents?
      2. What are the names of all the males?
      3. List the names of Blake's children.
      4. Who is Amanda's mother?
      5. Who are Blake's sons.
      6. Who is Danny's grandmother?


    4. 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

wonko@wonko.info
©Copyright t 1992..2017+. Edition 25.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.