• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
tutorial1.doc
 

tutorial1.doc

on

  • 610 views

 

Statistics

Views

Total Views
610
Views on SlideShare
610
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    tutorial1.doc tutorial1.doc Presentation Transcript

    • The University of Birmingham - School of Computer Science 02630 - Software Workshop Prolog Tutorial Sheet 1 Your registration number : Your course : Due date : 12.00 hours, Friday 5th October 2007 This tutorial sheet complements the material covered in Lecture 1. You are expected to attempt all parts. You will sometimes need to read some other material (eg WWW notes or books) to be able to complete this sheet. References to the WWW notes are given in this sheet; references to books are given in the outline of the module. This tutorial consists of three parts, each of one or more questions. Part 1 presents you with questions about the basic material in the lecture. Part 2 consists of material designed to apply your knowledge, perhaps in a creative way. The final part prepares for material presented in the next lecture. The marking scheme is as follows: each part attracts equal marks. For each section, there will be two marks given: one will be given if you are judged to have made a reasonable attempt; the other for getting the part correct. Where you are asked to write a program or run a query, only solutions produced using SICStus Prolog running on the School’s servers will be accepted. Late submissions are not accepted.
    • If any part of your answer is found to be copied/plagiarised, you will lose all your marks for this sheet and the School’s discipline procedures will be applied. You may need to hand in this sheet and any other sheets, for instance, program listings. Each sheet should have your Student ID number and the phrase “SW Prolog 2007/08 assgt 1” clearly written at the top. All sheets should be stapled together ensuring the staple or staples will not injure anyone handling your work. An assessed work box is provided near the School’s Reception. You must staple a School assessed work cover sheet to your work. Solutions will be returned only with marked work. Solutions are neither available on the WWW nor in the School Library. To be able to complete this work satisfactorily, you will need to be familiar with some material in the WWW notes on Prolog. The home page of the notes is: http://www.cs.bham.ac.uk/~pjh/prolog_course/sem242.html. You will need to be familiar with the following sub-sections: 1 Setting up SICStus Prolog, using SICStus Prolog, editors and public-domain Prologs for home use. http://www.cs.bham.ac.uk/~pjh/prolog_course/using_sicstus/using_sicstus_prolog.html 2 Module 1 - Some unification, some data types and some built-in predicates http://www.cs.bham.ac.uk/~pjh/prolog_course/md1/md1.html 2 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007
    • SW Prolog 2007/08 assgt 1 ID number: Part 1 1 Clauses, facts and rules This is a Prolog program. return_to_uni(Boxes_of_Luggage, Cost_of_Journey, Type_of_Vehicle, Vehicle_Name) :- vehicle(Vehicle_Name, Type_of_Vehicle, Cost_of_Journey, Passengers), luggage_space(Type_of_Vehicle, Passengers, Luggage_Space_in_Boxes), Luggage_Space_in_Boxes >= Boxes_of_Luggage. ideal_car_capacity(10). passenger_equals_boxes(2). vehicle(my_car, car, 30, 0 ). vehicle(parents_car, car, 0, 2 ). vehicle(sisters_car, car, 27, 1 ). vehicle(maiden, train, 47, public_transport). vehicle(centroid, train, 15, public_transport). vehicle(national_slow_coach, coach, 10, public_transport). luggage_space(train, _Irrelevant, 3). luggage_space(coach, _Irrelevant, 2). luggage_space(car, Passengers, Space) :- ideal_car_capacity(Total_Capacity), passenger_equals_boxes(Passengers_v_Boxes), Loss_of_Space_to_Passengers is Passengers * Passengers_v_Boxes, Space is Total_Capacity - Loss_of_Space_to_Passengers. 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007 3
    • Label the program† to show all the: (a) clauses (b) facts (c) rules. 2 Procedures In Prolog, a procedure is a group of one or more clauses that have the same “name” (eg passenger_equals_boxes) and the same number of arguments. In the code given for Question 1, how many procedure(s) is/are there: (a) 1 procedure (b) 2 procedures (c) 5 procedures (d) 6 procedures (e) 12 procedures (f) 19 procedures. † If you want to try this program out for yourself (which is a good idea), copy and paste the program into a file and load it into the Prolog compiler. (See the WWW page: http://www.cs.bham.ac.uk/~pjh/prolog_course/using_sicstus/loading_programs.html.) Then you can enter queries such as: ?- return_to_uni(1, Cost_in_Pounds, Vehicle_Type, Which_Vehicle). You can experiment by changing the value of the first argument (eg from 1 to 10) to see how it changes the results you get. 4 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007
    • SW Prolog 2007/08 assgt 1 ID number: Before you attempt this question, you should read: http://www.cs.bham.ac.uk/~pjh/prolog_course/md1/md1_data_types.html 3 Variables and atoms Using the program given in Question 1, list: (a) four variables Hint: you can use the Prolog interpreter to help you - providing you have read 1 about var/1 and nonvar/1 in the WWW notes or in a textbook. 2 3 4 (b) four atomics 1 Hint: you can use the Prolog interpreter to help you - providing you have read about atomic/1 in the WWW notes or in a textbook. 2 3 4 (c) four atoms (not in your answer to part b) 1 Hint: you can use the Prolog interpreter to help you - providing you have read 2 about atom/1 in the WWW notes or in a textbook. 3 4 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007 5
    • 4 Variables, atomics, atoms and numbers Complete the following table to show which of the following are atomics, atoms, numbers and variables. The first row has been completed for you. Term Variable Atomic Atom Number cimabue no yes yes no Painter six '2/3' 7 Atom variable Part 2 5 Unification of simple terms Complete the following table to show which pairs of terms will unify. Again, one row has been completed for you. Syracuse android 7.0 7 Sheep Syracuse yes yes yes yes yes Part 1: android completed: 7.0 correct: 7 Sheep You can find out if two terms unify in Prolog by typing them into the Prolog interpreter. Suppose you wanted to test terms called Fred1 and Fred2: type Fred1 = Fred2. More information is in: http://www.cs.bham.ac.uk/~pjh/prolog_course/md1/md1_matching.html 6 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007
    • SW Prolog 2007/08 assgt 1 ID number: 6 Querying facts This is a Prolog program. All the clauses in this program are facts: auction_price(van_gogh, cabbages, 17.50). You can copy and paste this into auction_price(van_gogh, view_of_st_denis, 7.00). an editor, then load it into Prolog auction_price(simone_martini, st_john, 1.50). to run. See the WWW page: auction_price(constable, salisbury_cathedral, 27.50). http://www.cs.bham.ac.uk/~pjh/p auction_price(turner, salisbury_cathedral, 0.75). auction_price(turner, view_of_ludlow_castle, 2.50). rolog_course/using_sicstus/loadi ng_programs.html 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007 7
    • What solutions does Prolog find for the following queries? (a) | ?- auction_price(Painter, Picture_Name, Price_Paid). Painter = Picture_Name = Price_Paid = Painter = Picture_Name = Price_Paid = Painter = Picture_Name = Price_Paid = Painter = Picture_Name = Price_Paid = Painter = Picture_Name = Price_Paid = Painter = Picture_Name = Price_Paid = (b) | ?- auction_price(turner, Picture_Name, Price_Paid). Picture_Name = Price_Paid = Picture_Name = Price_Paid = 8 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007
    • SW Prolog 2007/08 assgt 1 ID number: 7 More on querying facts Using the program above, what solution or solutions does Prolog find to the following queries? (a) | ?- auction_price(turner, salisbury_cathedral, Price_Paid). (b) | ?- auction_price(Painter1, salisbury_cathedral, Value1), auction_price(Painter2, salisbury_cathedral, Value2), Value is Value1 + Value2. Part 2: completed: correct: 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007 9
    • Part 3 8 Writing a program Here are four queries and the variable bindings they produce. There are five facts and no rules in this program. | ?- link(a, b). yes | ?- link(b, X). X = e ? ; no | ?- link(c, Y). Y = b ? ; Y = e ? ; no | ?- link(a, X), link(X, C). C = e, X = b ? ; C = e, X = b ? ; no List all the five facts in the original program. Part 3: completed: correct: 10 06-02630 – Software Workshop Prolog -Tutorial 1 - © P J Hancox 2007