Question 1:
Think of a family of three or four generations (it could be yours, some famous people, or a hypothetical family). Name all the members, and provide facts on the gender (such as: male(john).), and parent relation (such as: parent(john, sue).). Declare these facts in a Prolog program.
Now, define and add the following relations in Prolog: father, mother, brother, sibling, grandson, cousin, mother in law, and descendant. Any reasonable and common definition of those relations is accepted.
For each relation you have defined, ask Prolog a few queries which return “true” or “false”, and several queries with variables (and get all answers). Write down what Prolog returns (also include 1-2 screenshots).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Code:
male(jonathan).
male(straizo).
male(dio).
male(zeppeli).
male(jeorge).
male(gio).
male(joseph).
male(caesar).
female(erina).
female(elizabeth).
female(mariah).
female(roberta).
female(lisa).
female(susan).
female(suzy).
female(holly).
parent(jonathan, jeorge).
parent(erina, jeorge).
parent(straizo, lisa).
parent(elizabeth, lisa).
parent(dio, gio).
parent(mariah, gio).
parent(zeppeli, susan).
parent(roberta, susan).
parent(jeorge, joseph).
parent(lisa, joseph).
parent(gio, suzy).
parent(susan, suzy).
parent(jeorge, caesar).
parent(lisa, caesar).
parent(joseph, holly).
parent(suzy, holly).
married(jonathan, erina).
married(erina, jonathan).
married(straizo, elizabeth).
married(elizabeth, straizo).
married(dio, mariah).
married(mariah, dio).
married(zeppeli, roberta).
married(roberta, zeppeli).
married(jeorge, lisa).
married(lisa, jeorge).
married(gio, susan).
married(susan, gio).
married(joseph, suzy).
married(suzy, joseph).
father(F, C) :- male(F), parent(F, C).
mother(M, C) :- female(M), parent(M, C).
sibling(A,B):- mother(M,A),mother(M,B),father(F,A),father(F,B),A\=B.
brother(A, B):- male(A), sibling(A, B).
grandson(A, B):- male(A), parent(P,A), parent(B,P).
cousin(A, B):- parent(P,A), parent(Q,B), sibling(P,Q).
mother_in_law(M, A):- married(A, B), mother(M, B).
descendant(D, A):- parent(A, D).
descendant(D, A):- parent(P, D), descendant(P, A).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Examples:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Question 2:
In Feb of 2011, Jeopardy invited two top human champions and IBM's computer program Watson in a competition between the best of human and the computer in this well-known quiz show. On day 2, the Final Jeopardy is a question in the category: US cities. The clue is: Its largest airport is named for a World War II hero; its second largest, for a World War II battle. The two human champions answered it correctly (Chicago), but Watson answered "Toronto", and it only betted a small amount of money.
Write a Prolog program so when the question is asked, the correct answer will be returned. You need to include several US cities, airport names, WWII hero names, and so on. Load your code in Prolog, issue the query, and see if Program can return the correct answer (also include 1-2 screenshots). To make the Prolog programming easier, you could modify the clue to be "One of its airport is named for a World War II hero; the other airport, for a World War II battle".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Code:
city(chicago).
city(newyork).
city(losangeles).
city(boston).
city(toronto).
city(vancouver).
airport(ohare).
airport(midway).
airport(jfk).
airport(california).
airport(logan).
airport(pearson).
airport(city).
airport(vanc).
has_airport(chicago,ohare).
has_airport(chicago,midway).
has_airport(newyork,jfk).
has_airport(losangeles,california).
has_airport(boston,logan).
has_airport(toronto,pearson).
has_airport(toronto,city).
has_airport(vancouver,vanc).
hero(ohare).
hero(jfk).
hero(pearson).
hero(logan).
battle(midway).
battle(southern).
battle(northern).
battle(california).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Examples:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Question 3:
This question concerns lists of elements. Define the following relations:
a. last(X,Xs) is true if X is the last element in the list Xs. For example, last(c, [a, b, c]) would be true; or if you query last(X, [a, b, c]), it would return X=c.
b. adjacent(X,Y,Zs) is true if the elements X and Y are adjacent in the list Zs. For example, adjacent(d, f, [a, b, c, d, f, g]) is true.
c. substitute(X,Y,Xs,Ys) is true if the list Ys is the result of substituting Y for all occurrences of X in the list Xs. For example, substitute(a, c, [2, 3, a, b, a, c, 4], [2, 3, c, b, c, c, 4]) is true.
For each, run a few examples in Prolog. Show what Prolog returns (also include 1-2 screenshots).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Code:
last(X,[X]).
last(X,[Y|Xs]):- last(X,Xs).
adjacent(X,Y,[X,Y|Zs]).
adjacent(Y,X,[X,Y|Zs]).
adjacent(X,Y,[Z|Zs]):- adjacent(X,Y,Zs).
substitute(X,Y,[],[]).
substitute(X,Y,[X|Xs],[Y,Ys]):- substitute(X,Y,Xs,Ys).
substitute(X,Y,[Z|Xs],[Z|Ys]):- X\=Z, substitute(X,Y,Xs,Ys).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Examples:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Question 4:
Copy/paste the Tic-Tac-Toe Prolog example in the lecture notes into a file, and run it in Prolog. Play with it to see how strong it is.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Code:
:- dynamic x/1,o/1.
ordered_line(1,5,9).
ordered_line(3,5,7).
ordered_line(1,2,3).
ordered_line(4,5,6).
ordered_line(7,8,9).
ordered_line(1,4,7).
ordered_line(2,5,8).
ordered_line(3,6,9).
line(A,B,C) :- ordered_line(A,B,C).
line(A,B,C) :- ordered_line(A,C,B).
line(A,B,C) :- ordered_line(B,A,C).
line(A,B,C) :- ordered_line(B,C,A).
line(A,B,C) :- ordered_line(C,A,B).
line(A,B,C) :- ordered_line(C,B,A).
move(A) :- good(A), empty(A).
empty(A) :- \+ full(A).
full(A) :- x(A).
full(A) :- o(A).
good(A) :- win(A). % a cell where we win
good(A) :- block_win(A). % a cell where we block the opponent from a win
good(A) :- split(A). % a cell where we can make a split to win
good(A) :- block_split(A).% a cell where we block the opponent from a split
good(A) :- build(A). % choose a cell to get a line
win(A) :- x(B), x(C), line(A,B,C).
block_win(A) :- o(B), o(C), line(A,B,C).
split(A) :- x(B), x(C), \+ (B = C), line(A,B,D), line(A,C,E), empty(D), empty(E).
block_split(A) :- o(B), o(C), \+ (B = C), line(A,B,D), line(A,C,E), empty(D), empty(E).
build(A) :- x(B), line(A,B,C), empty(C).
good(5). % choose a cell in a good location
good(1).
good(3).
good(7).
good(9).
good(2).
good(4).
good(6).
good(8).
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Question 5:
There are 16 white boxes in the following chart, each of which should be filled with a letter, so that all the white boxes in the same row or column will form a word that is among [“dog”, “run”, “top”, “five”, “four”, “lost”, “mess”, “unit”, “baker”, “forum”, “green”, “super”, “prolog”, “vanish”, “wonder”, “yellow”].
Suppose we use L1~L16 to represent these unknown letters, just as shown in the chart. Please write a Prolog program to solve this problem. In your program, you should declare some facts, and then define a rule for solving this problem. Load your program in Prolog, and ask Prolog a query to get the solution. Write down what Prolog returns (also include 1-2 screenshots).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Code:
word(dog, d,o,g).
word(run, r,u,n).
word(top, t,o,p).
word(five, f,i,v,e).
word(four, f,o,u,r).
word(lost, l,o,s,t).
word(mess, m,e,s,s).
word(unit, u,n,i,t).
word(baker, b,a,k,e,r).
word(forum, f,o,r,u,m).
word(green, g,r,e,e,n).
word(super, s,u,p,e,r).
word(prolog, p,r,o,l,o,g).
word(vanish, v,a,n,i,s,h).
word(wonder, w,o,n,d,e,r).
word(yellow, y,e,l,l,o,w).
solution(V1,V2,V3,H1,H2):-
word(V1, L1,L6,L9,L15),
word(V2, L3,L7,L11),
word(V3, L5,L8,L13,L16),
word(H1, L1,L2,L3,L4,L5),
word(H2, L9,L10,L11,L12,L13,L14),
V1\=V2,
V2\=V3,
V3\=H1,
H1\=H2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Examples:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$