Sei sulla pagina 1di 12

SISTEME EXPERT

Tema Prolog

FACULTATEA DE AUTOMATICA SI CALCULATOARE


NEGOIE ANDREEA
3.04.2019
1.1 Which of the following sequences of characters are atoms, which are variables, and which
are neither?

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

ATOMS: 1,3,5,6,8

VARIABLES: 2,4,9

NEITHER: 7,10

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)

functor:love / variable: Vincent/ atom:Mia

loves/2

2. ’loves(Vincent,mia)’ -atom
3. Butch(boxer) -nothing
4. boxer(Butch)

functor:boxer
variable: Butcher

boxer/1

5. and(big(burger),kahuna(burger))

and, big,burger- atoms

big(burger), kahuna(burger), and(big(burger),kahuna(burger))- complex terms

and/2, big/1,kahuna/1

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

and,big,kahuna-atoms

7. big(X), kahuna(X), and(big(X),kahuna(X)) -complex terms

and/2,big/1,kahuna/1

8. _and(big(X),kahuna(X)) - nothing
9. (Butch kills Vincent) -nothing
10. kills(Butch Vincent) -nothing
11. kills(Butch,Vincent -nothing

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
3 facts / 4 rules/ all clauses/each contains a predicat: woman, man, person, loves, father, son,
daughter

Head:-

person(X)

Goals:-

man(X)
woman(X)

Head:-

loves(X,Y)

Goals:-

father(X,Y)

Head:-

father(Y,Z)

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) :- footmassage(X,Mia)
5. Mia loves everyone who is a good dancer.

loves(Mia,X) :- dacer(X,good)

6. Jules eats anything that is nutritious or tasty.

eats(Jules,X):- nutricious(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). ron,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.

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)

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

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

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, _,_11,_,_21,_,_31,_),

word(V2, _,_12,_,_22,_,_32,_),

word(V3, _,_13,_,_23,_,_33,_),

word(H1, _,_11,_,_12,_,_13,_),

word(H2, _,_21,_,_22,_,_23,_),

word(H3, _,_31,_,_32,_,_33,_).

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

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.

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.

Run Prolog Now!

directlyIn(katarina, olga).

directlyIn(olga, natasha).

directlyIn(natasha, irina).

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

in(X, Y) :-

directlyIn(X, Z),

in(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.

travelFromTo(X, Y) :-

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

directTrain(X, Z),

travelFromTo(Z, Y).

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

greater_than(succ(_), 0).

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

greater_than(X, Y).

Potrebbero piacerti anche