程序代写代做代考 prolog Prolog Tutorial 1

Prolog Tutorial 1

To do this tutorial start with the trade.txt file that is provided on Cate and add clauses to it as specified

below. You can use whichever text editor you like. Remember to save and reconsult the file

after each change you make!

1. Do the Trade Program Exercises from the notes.

2. Change the fact needs(_, computers) to a rule that specifies every country needs computers.

3. Add that Britain, Japan, Germany, France are countries. You can do this in (at least) two different
ways. Can you see how?

In the rest of this exercise you do not need to use the concept (and predicate) country. So, for

example if you want to query which country produces oil, for simplicity, just type the query

produces(X, oil) rather than, for example, country(X), produces(X, oil).

4. Add facts to capture the following information:

 Britain and Japan need coal.

 Japan needs diamonds.

5. Using a predicate nat_res such that nat_res(C,I) expresses that country C has item
I as a natural resource, add facts to express the information below:

Country Has natural resource

France Coal

Britain Coal

South Africa Diamonds

6. Using a predicate ce such that ce(C,I) expresses that it is cost effective to mine item
I in country C, add facts to express the information below:

Country Can cost effectively mine

France Coal

South Africa Diamonds

7. Formulate and run the following queries, ensuring the answers you get are correct.

(a) Which countries need an item which they have as a natural resource?

Answer: britain, coal

(b) Is there an iron producing country that can cost effectively mine an item needed

by Japan?

Answer: france, coal

(c) Which countries need both oil and coal?

Answer: britain, japan

(d) Which countries need an item that is cost effectively mined in South Africa?

Answer: japan, diamonds

8. Using the following predicates and the ones you have already defined, add Prolog rules
to express the information given in (a)–(d) below.

mines(C,I) Country C mines item I

exports(C1,I,C2) C1 exports item I to C2

in_competition(C1,C2,I) C1 and C2 are in competition with respect to item I

lower_price(I) the price of item I is lowered

(a) Any country mines its natural resources if it can do so cost effectively.

(b) Any country exports an item to another if the first country produces or mines it

and the second country needs it.

(c) Any two (different) countries that export the same item are in competition with one another

with respect to that item. (Use X \=Y to mean X and Y are different.)

(d) If there are two countries in competition with one another with respect to an item

the price of that item will be lowered.

9. Formulate and run the following queries, ensuring the answers you get are correct.

(a) Who countries mine what?

Answer: france, coal

south_africa, diamonds

(b) Who exports at least two different items, and what are they?

Answer: france, iron, coal The answer may be repeated. That is ok.

(c) Are there any two different countries in competition with respect to oil?

Answer: oman, iraq

The answer may be repeated many times. That is ok. If you put “, !.” after the last

conjunct of the query, in this example, the repetitions will disappear – more on this in

later lectures.

(d) Are there any two countries in competition with respect to an item they both

export to Britain and Japan?

Answer: oman, iraq, oil The answer may be repeated many times. That is ok.

(e) Does Germany export anything to a country which needs something that is

exported by a country which mines diamonds?

Answer: Germany exports cars to Japan that needs diamonds that are mined by South

Africa.

(f) Which item needed by at least two different countries will have its price lowered?

Answer: oil !!