Anda di halaman 1dari 13

Tema 1 - Sisteme Expert

Roxana Vlad

1. Exercitii Chapter 1 Lean Prolog Now;

1.1 Exercitiul 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 - nici variable/atom
8. 'Jules' - atom
9. _Jules - variable
10. '_Jules' - atom

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 term

• Functor:loves

• Arity: 2

2. ’loves(Vincent,mia)’

• Atom

3. Butch(boxer)

• Nimic

4. boxer(Butch)

• Functor:boxer

• Variable: Butcher

• Arity: 1
5. and(big(burger),kahuna(burger))

• Complex term

• Functor: and

• Arity: 2

6. and(big(X),kahuna(X))

• Complex term

• Functor: and

• Arity : 2

7. big(X), kahuna(X), and(big(X),kahuna(X))

• Complex terms

• Functor : and

• Arity : 2

8. _and(big(X),kahuna(X))

• Nimic

9. (Butch kills Vincent)

• Nimic

10. kills(Butch Vincent)

• Nimic

11. kills(Butch,Vincent

• Nimic

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).-fact
woman(mia).-fact
man(jules).-fact
person(X):- man(X); woman(X).-rule
loves(X,Y):- father(X,Y).-rule
father(Y,Z):- man(Y), son(Z,Y).-rule
father(Y,Z):- man(Y), daughter(Z,Y).-rule

• FACTS : 3
• RULES : 4
• CLAUSES : 7 ( TOATE )
• PREDICATES : 7 ( WOMAN, MAN, PERSON, FATHER,SON, DAUGHTER, LOVES )

1. Head:

person(X)

1.Goals:
man(X)
woman(X)

2.Head:

loves(X,Y)

2. Goals:

father(X,Y)

3. Head:

father(Y,Z)

3. Goals:

man(Y)
son(Z,Y)
daughter(Z,Y)

1.4 Represent the following in Prolog:

1. Butch is a killer. => killer(Butch).

2. Mia and Marsellus are married. => married(Mia,Marsellus).

3. Zed is dead. => dead(Zed).

4. Marsellus kills everyone who gives Mia a footmassage. => kills(Marsellus,X) :-


givesFootMassage(X,Mia).
5. Mia loves everyone who is a good dancer. => loves(Mia,X) :- goodDacer(X).

6. Jules eats anything that is nutritious or tasty.

=> eats(Jules,X):- nutricious(X).

=> eats ( Jules,X ) :- tasty(X).

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

3. wizard(hermione). FALSE

4. witch(hermione). ERROR

5. wizard(harry). TRUE

6. wizard(Y). => Y=ron; Y=harry

7. witch(Y). ERROR

2. Program Prolog pentru determinarea celui mai mic multiplu comun a doua
numere

cmmmc(X,Y,C)

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

3. Exerci*ile din prezentarea: “P2 Unificare Prolog”

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 estepretios;
estePretios(aur).
d) maşina este un mercedes albastru cu capacitatea de cinci călători
masina(mercedes,albastru,Cinci_calatori)

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).
Vali ii este parinte lui X.
X ii ieste parinte lui Y.
Y ii este parinte lui Roco.

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.

copil(peter, 9).
copil(paul, 10).
copil(chris, 9).
copil(susan, 9).
joaca(X, Y) :- copil(X,Z),!,copil(Y,Z),X\=Y.
27 ?- joaca(C,J).
C = peter, J = chris ;
C = peter, J = susan.

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

barbat(gelu).
barbat(bogdan).
barbat(toma).
fumator(toma).
fumator(dan).
vegetarian(gelu).
partener(X) :- barbat(X), not(fumator(X)).
partener(X) :- barbat(X), vegetarian(X).

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”

este(X,literat):- poate(X, citi).

delfinii(nu_sunt_literati)

4. Exercitiile 2.3 si 2.4 Chapter 2 Lean Prolog Now 


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

W1 = a,

W2 = criminal,

W3 = eats,

W4 = a,

W5 = criminal ;

W1 = a,

W2 = criminal,

W3 = eats,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = criminal,

W3 = eats,

W4 = every,

W5 = criminal ;

W1 = a,

W2 = criminal,

W3 = eats,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = criminal,

W3 = likes,

W4 = a,

W5 = criminal ;

W1 = a,

W2 = criminal,

W3 = likes,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = criminal,

W3 = likes,

W4 = every,

W5 = criminal ;

W1 = a,

W2 = criminal,

W3 = likes,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = 'big kahuna burger',

W3 = eats,

W4 = a,

W5 = criminal ;

W1 = a,

W2 = 'big kahuna burger',

W3 = eats,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = 'big kahuna burger',

W3 = eats,

W4 = every,

W5 = criminal ;

W1 = a,

W2 = 'big kahuna burger',

W3 = eats,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = 'big kahuna burger',

W3 = likes,

W4 = a,

W5 = criminal ;

W1 = a,

W2 = 'big kahuna burger',

W3 = likes,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = a,

W2 = 'big kahuna burger',

W3 = likes,

W4 = every,

W5 = criminal ;

W1 = a,

W2 = 'big kahuna burger',

W3 = likes,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = criminal,

W3 = eats,

W4 = a,

W5 = criminal ;

W1 = every,

W2 = criminal,

W3 = eats,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = criminal,

W3 = eats,

W4 = every,

W5 = criminal ;

W1 = every,

W2 = criminal,

W3 = eats,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = criminal,

W3 = likes,

W4 = a,

W5 = criminal ;

W1 = every,

W2 = criminal,

W3 = likes,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = criminal,

W3 = likes,

W4 = every,

W5 = criminal ;

W1 = every,

W2 = criminal,

W3 = likes,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = 'big kahuna burger',

W3 = eats,

W4 = a,

W5 = criminal ;

W1 = every,

W2 = 'big kahuna burger',

W3 = eats,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = 'big kahuna burger',

W3 = eats,

W4 = every,

W5 = criminal ;

W1 = every,

W2 = 'big kahuna burger',

W3 = eats,

W4 = every,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = 'big kahuna burger',

W3 = likes,

W4 = a,

W5 = criminal ;

W1 = every,

W2 = 'big kahuna burger',

W3 = likes,

W4 = a,

W5 = 'big kahuna burger' ;

W1 = every,

W2 = 'big kahuna burger',

W3 = likes,

W4 = every,

W5 = criminal ;

W1 = every,

W2 = 'big kahuna burger',

W3 = likes,

W4 = every,

W5 = 'big kahuna burger’.

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

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. Exerci*ile 3.1, 3.2, 3.3, 3.4 Chapter 3 Lean Prolog Now 


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

1. X este descendentul lui Y daca X este fiul lui Y.

2. X este descendentul lui Y daca X este descendentul lui Z / Z este descendentul lui Y.

In cel de-al doilea caz conditia este incerta.

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.

Run Prolog Now!

directlyIn(olga, natasha).

in(X, Y) :-
directlyIn(X, Y).

in(X, Y) :-

directlyIn(X, Z),

in(Z, Y).

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.

travelFromTo(X, Y) :-

directTrain(X, Y).

travelFromTo(X, Y) :-

directTrain(X, Z),

travelFromTo(Z, Y).
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

greater_than(succ(_), 0).

greater_than(succ(X), succ(Y)) :-

greater_than(X, Y).

Anda mungkin juga menyukai