Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Sisteme expert

Facultatea de Automatica si Calculatoare

Master: Sisteme Infrmatice in Medicina (SIM)

1
1. Exercitii Chapter 1 Lean Prolog Now

Exercise 1.1 Which of the following sequences of characters are atoms, which are variables, and which are neither?

1. vINCENT atom
2. Footmassage variable
3. variable23 atom
4. Variable2000 variable
5. big_kahuna_burger atom
6. ’big kahuna burger’ atom
7. big kahuna burger neither
8. ’Jules’ atom
9. _Jules variable
10. ’_Jules’ atom

Exercise 1.2 Which of the following sequences of characters are atoms, which are variables, which are complex terms, and
which are not terms at all? Give the functor and arity of each complex term.

1. loves(Vincent,mia) complex terms; functor: loves; arity: 2


2. ’loves(Vincent,mia)’ atom
3. Butch(boxer) not a term
4. boxer(Butch) complex terms; functor: boxer; arity: 1
5. and(big(burger),kahuna(burger)) complex terms; functor: and; arity: 2
6. and(big(X),kahuna(X)) complex terms; functor: and; arity: 2
7. _and(big(X),kahuna(X)) variable
8. (Butch kills Vincent) not a term
9. kills(Butch Vincent) not a term
10. kills(Butch,Vincent not a term

Exercise 1.3 How many facts, rules, clauses, and predicates are there in the following knowledge base? What are the heads
of the rules, and what are the goals they contain?

woman(vincent).

woman(mia).

man(jules).

person(X):- man(X); woman(X).

loves(X,Y):- father(X,Y).

father(Y,Z):- man(Y), son(Z,Y).

father(Y,Z):- man(Y), daughter(Z,Y).

 facts: 3 (woman(vincent). woman(mia). man(jules).)


 rules: 4.

2
o Head of the rule1: person(X) ; goal1: man(X); woman(X)
o Head of the rule2: loves(X,Y); goals2: father(X,Y)
o Head of the rule3: father(Y,Z); goals3: man(Y), son(Z,Y)
o Head of the rule4: father(Y,Z); goals4: man(Y), daughter(Z,Y)

 clauses: 7
 predicates: 7 (women, man, person, loves, father, son, daughter)

Exercise 1.4 Represent the following in Prolog:

1. Butch is a killer. killer(butch).


2. Mia and Marsellus are married. maried (mia, marsellus).
3. Zed is dead. dead(zed).
4. Marsellus kills everyone who gives Mia a footmassage. kill(marsellus, X ):- footmasage(X,mia).
5. Mia loves everyone who is a good dancer. loves(mia, X):- good_dancer(X).
6. Jules eats anything that is nutritious or tasty. eat_anything(jules, X):- nutritious(X); tasty(X).

Exercise 1.5 Suppose we are working with the following knowledge base:

wizard(ron).

hasWand(harry).

quidditchPlayer(harry).

wizard(X):- hasBroom(X), hasWand(X).

hasBroom(X):- quidditchPlayer(X).

How does Prolog respond to the following queries?

1. wizard(ron). true
2. witch(ron). procedure `witch(A)' does not exist
3. wizard(hermione). false
4. witch(hermione). procedure `witch(A)' does not exist
5. wizard(harry). true
6. wizard(Y). Y=ron

Y=harry

7. witch(Y). procedure `witch(A)' does not exist

3
2. Program Prolog pentru determinarea celui mai mic multiplu comun a doua numere.
modulo(X,Y,X):-X<Y.

modulo(X,Y,Z):-X>=Y,X1 is X-Y,modulo(X1,Y,Z).

cmmdc(X,0,X).

cmmdc(X,Y,C):-modulo(X,Y,Z),cmmdc(Y,Z,C).

cmmmc(X,Y,C):-cmmdc(X,Y,A),C is X*Y/A.

4
3. Exercitiile din prezentarea: “P2 Unificare Prolog”

PARTEA a-II-a: EXERCITII

1. Exprimati in Prolog urmatoarele fapte:

a) susan are un cal; are(susan,cal).

b) rex mănâncă carne; mananca(rex, carne).

c) aurul este pretios; aur(pretios).

d) maşina este un mercedes albastru cu capacitatea de cinci călători

masina(mercedes, albastru, capacitate_calatori(5)).

2. Traduceti în limbaj natural următoarea întrebare Prolog si precizati răspunsul posibil:

părinte(vali, X),

părinte(X, Y),

părinte(Y, roco).

Al cui parinte este Vali? (Raspuns: X)

si Al cui parinte este X (copilul lui Vali)? (Raspuns: Y)

si Al cui parinte este Y (copilul lui X)? (Raspuns: roco)

5
Adica: Gaseste pe cineva care este copilul lui Vali si este parintele parintelui lui Roco (X) si pe cineva care
este copilul copilului lui Vali si parintele lui Roco. Y este deci nepotul lui Vali.

Scris sub forma de intrebare: „,Cine este bunicul lui roco care este si copilul lui vali?”

„Cine este tatal lui roco care este si nepotul lui vali?”
Daca nu am definit in prealabil in prolog relatii din care putem deduce raspunsul la aceste intrebari
programu va genera un raspuns de tipul:
X = _5071
Y = _5071
yes

3. Doi copii pot juca un meci într-un turneu de tenis dacă au aceeaşi vârstă. Fie următorii copii şi vârstele lor:

copil(peter, 9).

copil(paul, 10).

copil(chris, 9).

copil(susan, 9).

Definiti un predicat din care rezulta toate perechile de copii care pot juca un meci într-un turneu de tenis.

pot_juca(Jucator1, Jucator2) :- copil(Jucator1, Varsta), copil(Jucator2,Varsta), Jucator1\= Jucator2.

4. Scrieti un program care să îi găseasca Anei un partener la bal. Ea doreşte sa mearga cu un bărbat care este
nefumător sau vegetarian. Pentru aceasta dispunem de o bază de date cu informatii despre câtiva bărbatii:

barbat(gelu).

barbat(bogdan).

barbat(toma).

fumator(toma).

fumator(dan).

vegetarian(gelu).

Raspuns:

sot(X):-barbat(X),not(fumator(X)).

sot(X):-barbat(X),vegetarian(X).

6
5. Se dau următoarele enunturi:

1) Oricine poate citi este literat.

2) Delfinii nu sunt literati.

3) Anumiti delfini sunt inteligenti.

Să se demonstreze în Prolog că: “Există fiinte inteligente care nu pot citi”.

literat(X):-citeste(X).

citeste(susan).

om(susan).

animal(delfin).

literat(X):-om(X).

inteligent(X):-animal(X),dresat(X).

dresat(delfin).

?-literat(susan).
true

?-literat(delfin).

false

?-inteligent(delfin).
true

Desi delfinul este o fiinta inteligenta, el nu poate citi.

4. Exercitiile 2.3 si 2.4 Chapter 2 Lean Prolog Now

Exercise 2.3 Here is a tiny lexicon (that is, information about individual words) and a mini grammar consisting of one
syntactic rule (which defines a sentence to be an entity consisting of five words in the following order: a determiner, a noun,
a verb, a determiner, a noun).

word(determiner,a).
word(determiner,every).
word(noun,criminal).

7
word(noun,'big kahuna burger').
word(verb,eats).
word(verb,likes).

sentence(Word1,Word2,Word3,Word4,Word5):-
word(determiner,Word1),
word(noun,Word2),
word(verb,Word3),
word(determiner,Word4),
word(noun,Word5).

What query do you have to pose in order to find out which sentences the grammar can generate? List all sentences that this
grammar can generate in the order that Prolog will generate them in.

sentence(W1, W2, W3, W4, W5).

Propozitii generate de Prolog, in ordinea generarii:

1 - a criminal eats a criminal


2 - a criminal eats a big kahuna burger
3 - a criminal eats every criminal
4 - a criminal eats every big kahuna burger
5 - a criminal likes a criminal
6 - a criminal likes a big kahuna burger
7 - a criminal likes every criminal
8 - a criminal likes every big kahuna burger
9 - a big kahuna burger eats a criminal

8
10 - a big kahuna burger eats a big kahuna burger
11 - a big kahuna burger eats every criminal
12 - a big kahuna burger eats every big kahuna burger
13 - a big kahuna burger likes a criminal
14 - a big kahuna burger likes a big kahuna burger
15 - a big kahuna burger likes every criminal
16 - a big kahuna burger likes every big kahuna burger
17 - every criminal eats a criminal
18 - every criminal eats a big kahuna burger
19 - every criminal eats every criminal
20 - every criminal eats every big kahuna burger
21 - every criminal likes a criminal
22 - every criminal likes a big kahuna burger
23 - every criminal likes every criminal
24 - every criminal likes every big kahuna burger
25 - every big kahuna burger eats a criminal
26 - every big kahuna burger eats a big kahuna burger
27 - every big kahuna burger eats every criminal
28 - every big kahuna burger eats every big kahuna burger
29 - every big kahuna burger likes a criminal
30 - every big kahuna burger likes a big kahuna burger
31 - every big kahuna burger likes every criminal
32 - every big kahuna burger likes every big kahuna burger

Exercise 2.4 Here are six Italian words:

astante , astoria , baratto , cobalto , pistola , statale .

They are to be arranged, crossword puzzle fashion, in the following grid:

The following knowledge base represents a lexicon containing these words:

word(astante, a,s,t,a,n,t,e).
word(astoria, a,s,t,o,r,i,a).
word(baratto, b,a,r,a,t,t,o).
word(cobalto, c,o,b,a,l,t,o).
word(pistola, p,i,s,t,o,l,a).
word(statale, s,t,a,t,a,l,e).

9
Write a predicate crossword/6 that tells us how to fill in the grid. The first three arguments should be the vertical words from
left to right, and the last three arguments the horizontal words from top to bottom.

crossword(V1,V2,V3,H1,H2,H3):-

word(V1,_,V1H1,_,V1H2,_,V1H3,_),

word(V2,_,V2H1,_,V2H2,_,V2H3,_),

word(V3,_,V3H1,_,V3H2,_,V3H3,_),

word(H1,_,V1H1,_,V2H1,_,V3H1,_),

word(H2,_,V1H2,_,V2H2,_,V3H2,_),

word(H3,_,V1H3,_,V2H3,_,V3H3,_).

5. Exercitiile 3.1, 3.2, 3.3, 3.4 Chapter 3 Lean Prolog Now


3.3 Exercises

Exercise 3.1 In the text, we discussed the predicate

descend(X,Y) :- child(X,Y).
descend(X,Y) :- child(X,Z),
descend(Z,Y).

Suppose we reformulated this predicate as follows:

descend(X,Y) :- child(X,Y).
descend(X,Y) :- descend(X,Z),
descend(Z,Y).

Would this be problematic?

Nu. Rezultatele obtinute sunt aceleasi in ambele cazuri, ordinea afisarii fiind identice.

10
Yes, as you would get infinite recursion as Prolog tries to match descend(X, Z) with a Z when using the
second rule whenever you happened to look at a query for which the knowledge base didn't have a
solution. Note that changing child(Z,Y) to descend(Z,Y) doesn't have any impact, other than to create
another potential source of infinite recursion, however this is usually not going to have much impact
considering the first instance of recursion in the body will cause a problem before it does.

Exercise 3.2 Do you know these wooden Russian dolls (Matryoshka dolls) where the smaller ones are contained in bigger
ones? Here is a schematic picture:

First, write a knowledge base using the predicate directlyIn/2 which encodes which
doll is directly contained in which other doll. Then, define a recursive predicate in/2 ,
that tells us which doll is (directly or indirectly) contained in which other dolls. For
example, the query in(katarina,natasha) should evaluate to true,
while in(olga, katarina) should fail.

11
child(katarina,olga).

child(olga,natasha).

child(natasha,irina).

descend(X,Y) :- child(X,Y).

descend(X,Y) :- child(X,Z),

descend(Z,Y).

Exercise 3.3 We have the following knowledge base:

directTrain(saarbruecken,dudweiler).
directTrain(forbach,saarbruecken).
directTrain(freyming,forbach).
directTrain(stAvold,freyming).
directTrain(fahlquemont,stAvold).
directTrain(metz,fahlquemont).
directTrain(nancy,metz).

That is, this knowledge base holds facts about towns it is possible to travel between by taking a direct train. But of course, we
can travel further by chaining together direct train journeys. Write a recursive predicate travelFromTo/2 that tells us when we
can travel by train between two towns. For example, when given the query

travelFromTo(nancy,saarbruecken).

it should reply yes.

12
travelFromTo(X,Y):- directTrain(X,Y).

travelFromTo(X,Y):- directTrain(Y,X).

travelFromTo(X,Y):-

directTrain(X,Z),

travelFromTo(Z,Y).

travelFromTo(X,Y):-

directTrain(Y,Z),

travelFromTo(Z,X).

Exercise 3.4 Define a predicate greater_than/2 that takes two numerals in the notation that we introduced in the text (that is,
0, succ(0), succ(succ(0)), and so on) as arguments and decides whether the first one is greater than the second one.
For example:

?- greater_than(succ(succ(succ(0))),succ(0)).
yes
?- greater_than(succ(succ(0)),succ(succ(succ(0)))).
no

Program prolog:

greater_than(succ(_), 0).
greater_than(succ(X), succ(Y)) :-
greater_than(X, Y).

13

You might also like