/* Weekly exercises for INF3110 Prolog I swipl -s "file" consult("file"). Exercise 1 ---------- Represent the following statements in Prolog: 1. Ivar studies Informatics 2. The population of Norway is 4,5 million 3. Norway is a rich country 4. 2 is a prime number 5. Someone wrote Hamlet 6. All humans are mortal 7. All rich people pay taxes 8. Ivar takes his umbrella if it rains 9. Firebrigade employees are men over six feet tall. 1. studies(ivar, informatics). 2. population(norway, 4500000). 3. rich(norway). 4. prime(2). 5. author(hamlet, someone). 6. mortal(X) :- human(X). 7. paytax(X) :- rich(X), human(X). 8. takes_umbrella(ivar) :- rains. 9. man(X) :- firebrigade(X). 9.1. taller_than_six_feet(X) :- firebrigade(X). Exercise 2 ---------- Given the following facts: */ biography(bashir,saddams_fortrolige,2004). biography(moen,jensStoltenberg,2002). biography(halvorsen,rett_fra_hjertet,2004). biography(isaksen, dronningen_vi_ikke_fikk,2003). biography(hasted,eminem,2003). biography(n?ss,munch,2004). biography(willoch,utfordringer,2004). /* Make a question where the answer is the author and title of biographies published in 2003. biography(Author, Title, 2003). */ /* Exercise 3 ---------- Define a relation "likes", so that ola likes both Italian and mild Indian food. These are Italian dishes: pizza, spaghetti. These are Indian dishes: tandoori, curry, tikkaMasala. Of these, curry and tikkaMasala are hot, while tandoori is mild. ----- */ italian(pizza). italian(spaghetti). indian(tandoori). indian(curry). indian(tikkaMasala). mild(tandoori). spicy(curry). spicy(tikkaMasala). /* likes(ola, X) :- mild(X), indian(X). likes(ola, X) :- italian(X). */ /* Exercise 4 ---------- Extend the above with nationality, so that Norwegians (as ola) like Italian food and mild Indian food, while Indians like both mild and hot Indian dishes, but not Italian food. */ nationality(ola, norwegian). nationality(eyvind, norwegian). nationality(volker, indian). likes(Person, Food) :- italian(Food), nationality(Person, norwegian). likes(Person, Food) :- indian(Food), mild(Food), nationality(Person, norwegian). likes(Person, Food) :- indian(Food), nationality(Person, indian). /* Exercise 5 ---------- Define the fact that each of sun, sirius and vega is a star. Define the fact "orbits", so that each of mercury, venus, earth, and mars orbits the sun, while moon orbits earth and deimos orbits mars. */ orbits(mercury, sun). orbits(venus, sun). orbits(earth, sun). orbits(mars, sun). orbits(moon, earth). orbits(deimos, mars). /* Given this, define the relation "planet(B)" as those Bs that are orbiting the sun. */ planet(X) :- orbits(X, sun). /* Define the relation "satellite(B)" giving those that orbits a planet. */ satellite(X) :- orbits(X,Y), planet(Y). /* Define the solar system "solar" to be either the sun, a planet or a satellite. */ solar(sun). solar(X) :- planet(X). solar(X) :- satellite(X). /* Exercise 6 ---------- The relation tel(county,person,number) expresses that a "person" (represented by name) in a given "county" has a certain telephone "number". 1. Make a question that ask for the telephone number of ole_brumm in hundre_meter_skogen 2. Make a question that gives all counties and all persons with the number 123 3. Make rules so that local(N1,N2) expresses that N1 and N2 are numbers within the same county 4. Make a question (with rules) to test that there is not two different persons (either in different counties or with different names) with the same number. tel(hundre_meter_skogen, ole_brumm, Nr). tel(X, Y, 123). local(N1, N2) :- tel(X, Y1, N1), tel(X, Y2, N2), N1 \== N2. */ same() :- tel(Y1, X, N), tel(Y2, X, N), Y1 \== Y2. same() :- tel(X, Y1, N), tel(X, Y2, N), Y1 \== Y2. /* Exercise 7 ---------- Santa has outsourced the reindeer traveling. Reindeer Line Inc. has decided not to travel from the North Pole to any major city, but has set up some pre-scheduled lines. Here are some of them. */ reindeerLine(northpole,oslo). reindeerLine(oslo,london). reindeerLine(oslo,copenhagen). reindeerLine(copehagen,berlin). reindeerLine(northpole,stockholm). reindeerLine(stockholm,moscow). /* a) As part of the deal, Santa has got a SWI-prolog and a two-hours introduction to Prolog. So, instead of calling Reindeer Line Inc. in order to find out if there is a connection between north pole and a given city, Santa just has to make a Prolog rule that tells if this is the case. Can you help him? */ connection(X, Y) :- reindeerLine(X,Y). connection(X, Y) :- reindeerLine(X, Z), connection(Z, Y). santa(X) :- connection(northpole, X). /* b) If Santa wants to know if there is just one stop between the north pole and a city, what kind of question should be asked? reindeerLine(northpole, X), reindeerLine(X, moscow). */ /* Exercise 8 ---------- Natural numbers may be defined as follows in Prolog: natural_number(0). natural_number(s(X)) :- natural_number(X). The first fact asserts that "0" is a natural number while the second one says that if X is a natural number, then the s(X) is also a natural number. a) Define an operation "plus(X,Y,Z)" where the first two arguments are two natural numbers to be added and the result is given in the third argument. */ plus(X, Y, Z) :- Z is X+Y. /* b) Define an operation "mult(X,Y,Z)" which makes the product of the first two arguments giving the result in the third. */ mult(X, Y, Z) :- Z is X*Y. /* weekly exercises for INF3110 week 47 (19.11.-23.11.2012) Exercise 1 ----------- Given the following facts in Prolog: */ p(anne, aase, aale, 1960). p(arne, aase, aale, 1962). p(beate, anne, lars, 1989). p(bjorn, lise, arne, 1990). /* 1. Give rules for the relation gc(X,Y) which expresses that X is a grandchild of Y. */ child(X,Y) :- p(X,Y,_,_). child(X,Y) :- p(X,_,Y,_). gc(X,Y) :- child(X,Z), child(Z,Y). /* 2. What will the result be of executing the queries: gc(beate, aale) and gc(beate, X). */ /* 3. Give rules for the relation cd(X,Y) which expresses that X and Y have common descendants. (i.e. children, grandchildren, great grand children etc.) */ desc(D,A) :- child(D,A). desc(D,A) :- child(D,X), desc(X,A). cd(X,Y) :- desc(D,X), desc(D,Y), X \== Y. /* 4. Give rules for the relation oc(X) which expresses that X is an only child (enebarn). */ sibling(X,Y) :- child(X,P), child(Y,P), X \== Y. oc(X) :- p(X,_,_,_), \+sibling(X,_). /* Exercise 2 ----------- a) IN211 exam 1992, problem 2e: 1. How would you express the following facts in Prolog: "eva is anne's boss", "eva is atle's boss" and "lars is eva's boss". */ boss(anne, eva). boss(atle, eva). boss(eva, lars). /* 2. We introduce the relation sup, such that sup(X,Y) means that X is superior to Y, i.e. X is the boss of Y or the boss of the boss of Y. Give prolog rules which defines the sup relation. */ sup(SUP, SUB) :- boss(SUB, SUP). sup(SUP, SUB) :- boss(SUB, X), sup(SUP, X). /* 3. A collection of "boss"-facts is said to be OK if no person is superior to himself and if no person have more than one boss. Show how you can test that your facts are OK. */ multBoss(X) :- boss(X, B1), boss(X, B2), B1 \== B2. ok(X) :- \+sup(X,X), \+multBoss(X). /* b) IN211 exam 1997, problem 3: We consider some form for data storage divided in two, a local storage and a remote storage. (As. e.g. with memory and disk in a computer). The content of the local store and the remote store is expressed by the relation ls and rs, respectively. E.g. by the following facts: */ ls(1, ole). ls(2, dole). ls(3, ole). rs(1, ole). rs(2, dole). rs(3, doffen). rs(5, dolly). /* The first argument of ls and rs are called indexes, the second argument is called values. - Look up Write rules which describe a relation "find" such that find(I,X) is true if either ls(I,X) or rs(I,X) is true. If there are several solutions, ls solutions should come before rs solutions. */ find(I,X) :- ls(I,X). find(I,X) :- rs(I,X). /* - Error Write rules which describe a relation "error" such that error(I) is true if there for some index I are different values in rs and ls. Index 3 is for example wrong in the facts given above. */ error(I) :- ls(I,X), rs(I,Y), X \== Y. /* - Multiple Write rules to describe a relation "multi" such that multi(X) is true if X occurs more than once in ls, or in rs, or if X occurs in both rs and ls, but with different indexes. */ multi(X) :- ls(I1,X), ls(I2,X), I1 \== I2. multi(X) :- rs(I1,X), rs(I2,X), I1 \== I2. multi(X) :- ls(I1,X), rs(I2,X), I1 \== I2. /* How many solutions will multi(X) give (including repeated solutions), for the facts given above? */ /* c) INF3110/4110 exam 2003 problem 6. (http://www.uio.no/studier/emner/matnat/ifi/INF3110/h06/gamle_eksamen/INF3110-4110-2003-eks-eng.pdf) */ /* Exercise 3 ----------- Write a Prolog rule addAtEnd(List1, Object, List2) which is true if List2 is List1 extended with Object at the end. */ addEnd([], Object, [Object]). addEnd([L1|R1], Object, [L1|R2]) :- addEnd(R1, Object, R2). /* Exercise 4 ---------- Write a Prolog rule to reverse a list. */ reverse([], []). reverse([X], [X]). reverse([X|Xs], Zs) :- reverse(Xs, Ws), addEnd(Ws,X,Zs).