Add homework 6 solutions

This commit is contained in:
Danila Fedorin 2019-03-15 19:23:42 -07:00
parent e5acf76748
commit 000d77e81a

119
fedorind.pl Normal file
View File

@ -0,0 +1,119 @@
% Here are a bunch of facts describing the Simpson's family tree.
% Don't change them!
female(mona).
female(jackie).
female(marge).
female(patty).
female(selma).
female(lisa).
female(maggie).
female(ling).
male(abe).
male(clancy).
male(herb).
male(homer).
male(bart).
married_(abe,mona).
married_(clancy,jackie).
married_(homer,marge).
married(X,Y) :- married_(X,Y).
married(X,Y) :- married_(Y,X).
parent(abe,herb).
parent(abe,homer).
parent(mona,homer).
parent(clancy,marge).
parent(jackie,marge).
parent(clancy,patty).
parent(jackie,patty).
parent(clancy,selma).
parent(jackie,selma).
parent(homer,bart).
parent(marge,bart).
parent(homer,lisa).
parent(marge,lisa).
parent(homer,maggie).
parent(marge,maggie).
parent(selma,ling).
%%
% Part 1. Family relations
%%
% 1. Define a predicate `child/2` that inverts the parent relationship.
child(X, Y) :- parent(Y, X).
% 2. Define two predicates `isMother/1` and `isFather/1`.
isMother(X) :- female(X), parent(X, _).
isFather(X) :- male(X), parent(X, _).
% 3. Define a predicate `grandparent/2`.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
% 4. Define a predicate `sibling/2`. Siblings share at least one parent.
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
% 5. Define two predicates `brother/2` and `sister/2`.
brother(X, Y) :- sibling(X, Y), male(X).
sister(X, Y) :- sibling(X, Y), female(X).
% 6. Define a predicate `siblingInLaw/2`. A sibling-in-law is either married to
% a sibling or the sibling of a spouse.
siblingInLaw(X, Y) :- married(X, SB), sibling(SB, Y) ; married(Y, SP), sibling(X, SP).
% 7. Define two predicates `aunt/2` and `uncle/2`. Your definitions of these
% predicates should include aunts and uncles by marriage.
aunt(X, Y) :- child(Y, P), (siblingInLaw(X, P) ; sibling(X, P)), female(X).
uncle(X, Y) :- child(Y, P), (siblingInLaw(X, P) ; sibling(X, P)), male(X).
% 8. Define the predicate `cousin/2`.
cousin(X, Y) :- parent(P1, X), parent(P2, Y), siblingInLaw(P1, P2).
% 9. Define the predicate `ancestor/2`.
ancestor(X, Y) :- parent(X, Y) ; parent(P, Y), ancestor(X, P).
% Extra credit: Define the predicate `related/2`.
% TODO
related(X, X, _).
related(X, Y, V) :- not(member(Y, V)),
(child(Y, S), related(X, S, [Y|V]) ;
parent(Y, C), related(X, C, [Y|V]) ;
married(Y, S), related(X, S, [Y|V])).
related(X, Y) :- related(X, Y, []), X \= Y.
%%
% Part 2. Language implementation (see course web page)
%%
cmd(X) :- number(X).
cmd(X) :- string(X).
cmd(X) :- bool(X).
cmd(add).
cmd(lte).
cmd(if(X, Y)).
prog([X|T]) :- cmd(X), prog(T).
prog([]).
if(X, Y) :- prog(X), prog(Y).
bool(f).
bool(t).
cmd(X, OS, [X|OS]) :- prog(OS), (number(X) ; string(X) ; bool(X)).
cmd(add, [X, Y | OS], [SUM| OS]) :- number(X), number(Y), is(SUM, X + Y).
cmd(lte, [X, Y | OS], [t | OS]) :- number(X), number(Y), X =< Y.
cmd(lte, [X, Y | OS], [f | OS]) :- number(X), number(Y), X > Y.
cmd(if(P1, _), [t | OS], FS) :- prog(P1, OS, FS).
cmd(if(_, P2), [f | OS], FS) :- prog(P2, OS, FS).
prog([], S, S).
prog([C|OP], S, FS) :- cmd(C), prog(OP), cmd(C, S, TS), prog(OP, TS, FS).