German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Concepts of Programming Languages, Spring 2014 Practice Assignment1 Discussion: 22.02.2014-27.02.2014 Exercise 1-1 Movies The supplied file movies.pl includes a database of movies specified by 4 different predicates: movie(m,y). % movie M was produced in year Y director(m,d). % movie M was directed by director D actor(m,a,r). % actor A played role R in movie M actress(m,a,r). % actress A played role R in movie M a) Implement the queries that answer the following questions: 1. What is the production year of the movie American Beauty??- movie(american_beauty,y).?- movie(n,y),n=american_beauty. 2. Find a movie that was produced in the year 2000.?- movie(n,2000).?- movie(m,y),y=2000. 3. Find a movie that was produced before the year 2000.?- movie(n,y),y<2000. 4. Find the name and year of a movie.?- movie(n,y). 5. Find a director who has directed a movie in which the actress Scarlett Johansson appeared.?- actress(m,scarlett_johansson,_), director(m,d).?- actress(m,a,_), director(m,d),a=scarlett_johansson. 1
6. Find an actor who has also directed a movie.?- actor(_,a,_),director(_,a). 7. Find an actor who has appeared in more than one movie.?- actor(m1,a,_),actor(m2,a,_),m1\=m2. 8. Find an actor or actress who has also directed a movie (You may use disjunction).?- director(_,a),(actor(_,a,_);actress(_,a,_)). Note: It is necessary to add parentheses around the disjunction in this example. Without them, Prolog would give a higher precedence to the logical-and and that will not correspond to our intent. b) Implement the following Prolog predicates: 1. same_year_as(m1,m2) succeeds if movie M1 was produced in the same year as movie M2. same_year_as(m1,m2):- movie(m1,y), movie(m2,y), M1\=M2. 2. cast_member(a,m) succeeds if person A was an actor or actress in movie M (write 2 rules). cast_member(a,m):- actor(m,a,_). cast_member(a,m):- actress(m,a,_). 3. cast_member2(a,m) succeeds if person A was an actor or actress in movie M (write a single rule). cast_member2(a,m):- actor(m,a,_); actress(m,a,_). 4. directed_by(x,y) succeeds if person X has been in a movie directed by person Y. directed_by(x,y):- director(m,y), cast_member(x,m). 5. released_since(m,y) succeeds if movie M was produced during or after year Y. released_since(m,y):- movie(m,year), Year>=Y. 6. newer(m1,m2) succeeds if movie M1 was produced after movie M2. newer(m1,m2):- movie(m1,y1), movie(m2,y2), Y1>Y2. 2
7. released_between(m,y1,y2) succeeds if movie M was produced between year Y1 and year Y2 inclusively. released_between(m,y1,y2):- movie(m,year), Year>=Y1. Year=<Y2. c) What is the difference between the three queries below??- actor(m1,d,_),actor(m2,d,_).?- actor(m1,d,_),actor(m2,d,_),m1\=m2.?- actor(m1,d,_),actor(m2,d,_),m1@<m2. The first query produces all possible combinations of results, including the case where M1 is the same as M2; whereas in the second query, the problem is solved by adding the constraint that M1 cannot be the same as M2. However, the second query still has redundancies as it displays the results of two distinct movies twice, in the second time M1 is bound to the second movie and M2 is bound to the first one. The third query allows the display of two different movies only if M1 lexicographically precedes M2, and hence solves the problem of the second query. 3
Exercise 1-2 Family We are going to add some new predicates to the family database family.pl. The database is specified by 3 different predicates: parent(x,y) % person X is the parent of person Y. male(x) % person X is a male. husband(x,y) % person X is the husband of person Y. Your task is to implement the following Prolog predicates: a) female(a): A is a female person. female(x) :- \+ male(x). b) grandfather(a,b): A is the grandfather of B. grandfather(x,y) :- father(x,p), parent(p,y). c) grandmother(a,b): A is a grandmother of B. grandmother(x,y) :- parent(x,p), \+male(x). d) brother(a,b): A is the brother of B. brother(x,y) :- parent(p,x), male(x), e) uncle(a,b): A is the uncle of B. uncle(x,y) :- brother(x,p), parent(p,y). f) sister(a,b): A is the sister of B. sister(x,y) :- parent(p,x), female(x), 4
g) has_son(a): The person A has a son. has_son(x) :- parent(x,y), male(y). h) married(a,b): A and B are married to each other. married(x,y) :- husband(x,y). married(x,y) :- husband(y,x). i) siblings(a,b): A and B are siblings (both parents in common) siblings(x,y) :- parent(z,x), parent(z,y), male(z), parent(m,x), parent(m,y), female(m), j) cousins(a,b): A and B are cousins cousins(x,y) :- parent(a,x), parent(b,y), siblings(a,b), k) no_children(a): A has no children no_children(x) :- \+ parent(x,_). no_children(x) :- married(x,y), \+parent(y,_). l) brother_in_law(a,b): A is the brother-in-law of B. brother_in_law(x,y) :- brother(x,p), married(p,y). brother_in_law(x,y) :- husband(x,w), sister(w,y). Hint: Insert new facts about your own parents and other relatives to see if the predicates work as expected! 5