3
\$\begingroup\$

I have had an assignment to construct a movie knowledgebase and ten specified queries in Prolog. This is my first ever Prolog program, so I would appreciate any pointers toward how I could improve.

We were instructed to not define any additional predicates than the three specified in Movie, and print_query_true and print_query_false in Main. We have not covered many standard/library predicates beyond writeln and nl.

Main

%main.pl

:- [movie].

print_query_true(Q) :-
    forall(Q, writeln(true:Q)).

print_query_false(Q) :-
    forall(\+ Q, (writeln(false:Q), nl)).

main :- 
    writeln('1. Did Leonardo DiCaprio act in Babel?'),
    nl,
    print_query_true(acted_in(leonardo_dicaprio,babel)),
    print_query_false(acted_in(leonardo_dicaprio,babel)),
    nl,
    writeln('2. Who was the director of District 9?'),
    nl,
    print_query_true(directed(X,district_9)),
    nl,
    writeln('3. Did someone act in both Click and The Aviator?'),
    nl,
    print_query_true((acted_in(X, click), acted_in(X,the_aviator))),
    print_query_false((acted_in(X, click), acted_in(X,the_aviator))),
    nl,
    writeln('4. Was there a movie released in 2010 that did not star Jennifer '
        + 'Aniston?'),
    nl,
    print_query_true(
        (released(X, 2010), acted_in(Y,X), \+ Y = jennifer_aniston)),
    print_query_false(
        (released(X, 2010), acted_in(Y,X), \+ Y = jennifer_aniston)),
    nl,
    writeln('5. Who directed movies released in 2009?'),
    nl,
    print_query_true((directed(X,Y), released(Y,2009))),
    print_query_false((directed(X,Y), released(Y,2009))),
    nl,
    writeln('6. Has anyone directed more than one movie?'),
    nl,
    print_query_true((directed(X,Y), directed(X,Z), \+ Y=Z)),
    print_query_false((directed(X,Y), directed(X,Z), \+ Y=Z)),
    nl,
    writeln('7. Does any movie have more than one director?'),
    nl,
    print_query_true((directed(X,Y), directed(Z,Y), \+ X=Z)),
    print_query_false((directed(X,Y), directed(Z,Y), \+ X=Z)),
    nl,
    writeln('8. Has anyone acted in more than one movie in 2008?'),
    nl,
    print_query_true(
        (acted_in(X,Y),
        acted_in(X,Z),
        released(Y,2008),
        released(Z,2008),
        \+ Y=Z)),
    print_query_false(
        (acted_in(X,Y),
        acted_in(X,Z),
        released(Y,2008),
        released(Z,2008),
        \+ Y=Z)),
    nl,
    writeln('9. Has anyone acted in more than two movies in the same year?'),
    nl,
    print_query_true(
        (acted_in(A,B),
        acted_in(A,C),
        acted_in(A,D),
        released(B,E),
        released(C,E),
        released(D,E),
        \+ B=C,
        \+ C=D,
        \+ B=D)),
    print_query_false(
        (acted_in(A,B),
        acted_in(A,C),
        acted_in(A,D),
        released(B,E),
        released(C,E),
        released(D,E),
        \+ B=C,
        \+ C=D,
        \+ B=D)),
    nl,
    writeln('10. Who has worked with the same director in different years?'),
    nl,
    print_query_true(
        (acted_in(A,B),
        directed(C,B),
        acted_in(A,D),
        directed(C,D),
        released(B,E),
        released(D,F),
        \+ E=F)),
    print_query_false(
        (acted_in(A,B),
        directed(C,B),
        acted_in(A,D),
        directed(C,D),
        released(B,E),
        released(D,F),
        \+ E=F)),
    nl,
    halt.

Movie

%acted_in(person,movie).
acted_in(brad_pitt,babel).
acted_in(cate_blanchette,babel).
acted_in(kate_beckinsdale,click).
acted_in(kate_beckinsdale,the_aviator).
acted_in(jennifer_aniston,the_bounty_hunter).
acted_in(leonardo_dicaprio,inception).
acted_in(leonardo_dicaprio,shutter_island).
acted_in(kristen_stewart,twilight).
acted_in(kristen_stewart,jumper).
acted_in(kristen_stewart,what_just_happened).
acted_in(christian_bale,the_dark_knight).
acted_in(christian_bale,batman_begins).

%directed(person,movie).
directed(alejandro_inarritu,babel).
directed(neill_blomkamp,district_9).
directed(brandon_camp,love_happens).
directed(quentin_tarantino,inglorious_basterds).
directed(quentin_tarantino,pulp_fiction).
directed(quentin_tarantino,four_rooms).
directed(allison_anders,four_rooms).
directed(alexandre_rockwell,four_rooms).
directed(robert_rodriguez,four_rooms).
directed(christopher_nolan,the_dark_knight).
directed(christopher_nolan,batman_begins).

%released(movie,year).
released(babel,2006).
released(inception,2010).
released(the_bounty_hunter,2010).
released(love_happens,2009).
released(inglorious_basterds,2009).
released(shutter_island,2010).
released(twilight,2008).
released(jumper,2008).
released(what_just_happened,2008).
released(the_dark_knight,2008).
released(batman_begins,2005).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.