% ########### FROM LAST WEEK #################

/*********************************************

male(X).

female(X).

father(X,Y).

mother(X,Y).

parent(X,Y):-father(X,Y);mother(X,Y).

diff(X,Y):-not(X==Y).

*********************************************/

 

parent(X,Y):-father(X,Y);mother(X,Y).

 

diff(X,Y):-not(X==Y).

 

is_mother(X):-female(X),

              parent(X,_).

 

is_father(X):-male(X),

              parent(X,_).

 

is_son(X):-male(X),

           parent(_,X).

          

sister_of(X,Y):-female(Y),

                parent(_,Y),

                diff(X,Y).

               

grandpa_of(X,Y):-male(X),

                 father(X,Z),

                 parent(Z,Y).

                

sibling(X,Y):-parent(Z,X),

              parent(Z,Y),

              diff(X,Y).

 

%#######################################################################

%#######################################################################

%  QUESTION 1

%  "is there a person who's a father to children of both genders?"

%#######################################################################

%#######################################################################

/*============ ANSWER ======================

 answer: the person must be male and must have children,

         at least one male and one female.

============================================*/

 

%--------- Test Database for "father both gender"---------

% god have 2 children: adam and eve

% adam and eve both have 2 children: richard and sybilla

 

/*

male(god).

male(adam).

male(richard).

male(bolderic).

male(charles).

male(godfroy).

 

female(eve).

female(sybilla).

female(elisabeth).

 

father(god,adam).

father(god,eve).

 

father(adam,richard).

father(adam,sybilla).

 

mother(eve,richard).

mother(eve,sybilla).

 

% only god and adam have children of both sexes

 

%==================================

% bolderic and elisabeth have 2 children male:charles and godfroy

 

father(bolderic,charles).

father(bolderic,godfroy).

 

mother(elisabeth,charles).

mother(elisabeth,godfroy).

 

%==================================

 

% male father with 2 children of both sexes: god(adam and eve), adam (richard and sybilla)

% male father with children of same sexes: bolderic(charles and godfroy)

 

*/

 

fatherbothgender(X):-male(X),father(X,M),father(X,F),male(M),female(F).

 

%#######################################################################

%#######################################################################

%  QUESTION 2 : GREAT AUNT

%#######################################################################

%#######################################################################

 

%------ males and females ------

male(zeus).

male(caesar).

male(poseidon).

male(ramses).

male(achiles).

male(charlemagne).

male(apollo).

male(charles).

male(david).

male(odin).

male(paul).

 

female(themis).

female(amoun).

female(athena).

female(catherine).

female(cleopatra).

female(kristine).

female(priscilla).

female(diana).

female(victoria).

female(isabella).

female(freya).

 

%----------- 0th generation -----------

father(zeus,catherine).

mother(themis,catherine).

 

father(ramses,cleopatra).

mother(amoun,cleopatra).

 

father(achiles,charlemagne).

father(achiles,kristine).

father(achiles,caesar).

mother(athena,charlemagne).

mother(athena,kristine).

mother(athena,caesar).

 

%----------- 1st generation -----------

father(charlemagne,apollo).

father(charlemagne,isabella).

mother(catherine,apollo).

mother(catherine,isabella).

 

father(caesar,poseidon).

mother(cleopatra,poseidon).

 

father(odin,paul).

father(odin,victoria).

mother(freya,paul).

mother(freya,victoria).

 

%----------- 2nd generation -----------

father(apollo,charles).

father(apollo,diana).

mother(victoria,charles).

mother(victoria,diana).

 

father(paul,david).

father(paul,priscilla).

mother(isabella,david).

mother(isabella,priscilla).

 

%----------- 3rd generation -----------

%  charles ,diana, david ,priscilla

 

/*======================= ANSWER ================================

great aunt = (sister of a grandparent ) OR (the wife of a brother of a grandparent)

 

X is great aunt of Y ====>

(X is a sister of G, G is  a grandparent of Y)  OR

(X is the wife of a brother B, B is a grandparent of Y)

================================================================*/

 

grandparent(X,Y):-parent(X,S),parent(S,Y).

 

% assuming that husband or wife have common children C

 

husband(X,Y):-male(X),female(Y),father(X,C),mother(Y,C).

 

wife(X,Y):-female(X),male(Y),mother(X,C),father(Y,C).

 

% X is sister of Y

 

sister(X,Y):-female(X),parent(P,X),parent(P,Y),diff(X,Y).

 

brother(X,Y):-male(X),parent(P,X),parent(P,Y),diff(X,Y).

 

 

% cleopatra is wife of caesar brother of charlemagne

% kristine is sister of charlemagne

% charlemagne is granparent of 3rd generation

 

greataunt(X,Y):- sister(X,G),grandparent(G,Y).

greataunt(X,Y):- wife(X,B),brother(B,P),grandparent(P,Y).

 

%#######################################################################

%#######################################################################

%  QUESTION 3 : HALF-BROTHER AND FULL-BROTHER

%#######################################################################

%#######################################################################

 

% test database for full and half brother

%------- normal familly ---------

 

male(pascal).

male(delphi).

male(kylix).

 

female(ada).

 

father(pascal,delphi).

father(pascal,kylix).

 

mother(ada,delphi).

mother(ada,kylix).

 

% delphi and kylix are full brother

 

%-------  half famillies -------

%--- 2 mothers, common father---

 

male(atlas).

male(phobos).

male(deimos).

female(hypnos).

female(medusa).

father(atlas,phobos).

father(atlas,deimos).

mother(hypnos,phobos).

mother(medusa,deimos).

 

% phobos and deimos are half brother with same father atlas

 

%--- 2 fathers, common mother---

 

male(neptune).

male(uranus).

male(tiger).

male(lion).

female(aphroditus).

father(neptune,tiger).

father(uranus,lion).

mother(aphroditus,tiger).

mother(aphroditus,lion).

 

% lion and tiger are half brother with same mother aphroditus

 

% deimos and phobos have different mothers

 

/*------------------------ ANSWER ----------------------------------

FULL-BROTHER(X,Y)=(X and Y are males) AND (X and Y have some mother) AND (X and Y have some father)

 

HALF-BROTHER(X,Y)=(X and Y are males) AND

                    (

                       <(X and Y have same mother) AND (X and Y have different father) > OR

                       <(X and Y have same father) AND (X and Y have different mother) >

                    )

------------------------------------------------------------------*/                   

 

fullbrother(X,Y):-male(X),male(Y),father(F,X),father(F,Y),mother(M,X),mother(M,Y),diff(X,Y).

 

% same mother but different fathers

samemother(X,Y):-mother(M,X),mother(M,Y),father(F1,X),father(F2,Y),diff(F1,F2).

 

% same father but different mothers

 

samefather(X,Y):-father(F,X),father(F,Y),mother(M1,X),mother(M2,Y),diff(M1,M2).

 

halfbrother(X,Y):-male(X),male(Y),samemother(X,Y).

halfbrother(X,Y):-male(X),male(Y),samefather(X,Y).

 

%#######################################################################

%#######################################################################

%  QUESTION 4 : Testing some codes

%#######################################################################

%#######################################################################

 

/**************************

1 ?- bread=bread.

 

Yes

 

the 2 primitives are identical

**************************

2 ?- 'Bread'=bread.

 

No

 

unification fails: one is a string and the other is an atom,

bread is not a variable

 

**************************

3 ?- 'bread'=bread.

 

Yes

 

because they are both atoms in this case.

 

**************************

4 ?- Bread=bread.

 

Bread = bread ;

 

No

 

variable Bread is assigned atom bread

 

**************************

5 ?- bread=sausage.

 

No

 

atoms are not identical

 

**************************

6 ?- food(bread)=bread.

 

No

 

unification fails because difference in arity.

**************************

7 ?- food(bread)=X.

 

X = food(bread) ;

 

No

 

unification succeeds because X is a variable that is assigned the atom

food(bread)

**************************

8 ?- food(X)=food(bread).

 

X = bread ;

 

unification succeeds at the level of the X variable and is assigned bread.

 

**************************

9 ?- food(bread,X)=food(Y,sausage).

 

X = sausage

Y = bread ;

 

No

 

unification succeeds X and Y are variables that are assigned to sausage and bread

**************************

10 ?- food(bread,X,beer)=food(Y,sausage,X).

 

No

 

unification fails because X is ambiguous (beer ? sausage ?)

 

**************************

11 ?- food(bread,X,beer)=food(Y,kahuma_burger).

 

No

 

unification cannot succeeds because difference in structure

 

**************************

12 ?- meal(food(bread),drink(beer))=meal(X,Y).

 

X = food(bread)

Y = drink(beer) ;

 

No

 

unification succeeds with X and Y

 

**************************

13 ?- meal(food(bread),X)=meal(X,drink(beer)).

 

No

 

unification fails because X is ambiguous (food(bread) ?  drink(beer)? )

 

**************************/

 

 

%#######################################################################

%#######################################################################

%  QUESTION 5

%  ?- food(X)=X.

%#######################################################################

%#######################################################################

 

/******************************************

ANSWER:

 

?- food(X)=X.

 

X = food(**) ;

No

 

It will produce infinite unification, prolog attempts to unify X with

food(food(food(food(food(food(.............

 

Modern prolog system employ a mechanism called occurs-check.

Occurs check is a feature of unification implemented in modern prolog system.

X is a variable and food(X) is a structure, prolog attempts to bind X to

compound term food(X) but will fail because it will result in unecessary

cyclic structure which may cause unification to loop forever.

******************************************/

 

%#######################################################################

%#######################################################################

%  QUESTION 6

%  Testing some codes

%#######################################################################

%#######################################################################

 

/*************************************

?- X = 3*4.

 

3*4 is an expression with 3 and 4 as leaves and * as the root

X is binded with that structure

************************************

?- X is 3*4.

 

"is" force 3*4 to be evaluated and becomes 12 which is assigned to X

************************************

?- 4 is X.

 

ERROR: Arguments are not sufficiently instantiated

the "is" operator cannot evaluate X because it is

unassigned.

************************************

?- X = Y.

 

both variables are unassigned and unification only

succeeds on the pointer level.

************************************

?- 3 is 1+2.

 

"is" operator force 1+2 to be evaluated to 3

the 2 numbers are equal, so unification return yes.

************************************

?- 3 is +(1,2).

 

"is" operator force evaluation of prefix notation +(1,2)

and becomes 3, the 2 numbers are equal, so unification return yes.

************************************

?- 3 is X+2.

 

"is" evaluate X+2 but X is unasigned, unification will fail.

************************************

?- X = 1, Y is X+2.

 

First unification assign 1 to X and "is" evaluate X+2 which becomes 3

and Y is binded to 3.

************************************

?- X = 8, 3 is X+2.

 

X is assigned 8 and "is" evaluate X+2 which is 10

10 is not equal to 3, unification fails.

************************************

?- X is 1+2.

 

"is" evaluates 1+2 and unify X to 3.

************************************

?- 1+2 is 1+2.

 

"is" evaluates 1+2 and becomes 3 which is different from

structure 1+2, unification fails.

************************************

?- is(X,+(1,2)).

 

this is just a prefix notation wich is equivalent to

X is 1+2

"is" evaluates 1+2 and unifies with variable X.

************************************

?- 3+2 = +(3,2).

 

+(3,2) is prefix for 3+2 so unification succeeds.

************************************

?- *(7,5) = 7*5.

 

*(7,5) is prefix for 7*5 so unification succeeds.

************************************

?- *(7,+(3,2)) = 7*(3+2).

 

*(7,+(3,2)) is the prefix notation of 7*(3+2).

************************************

?- *(7,(3+2)) = 7*(3+2).

 

it express the same meaning.

************************************

?- *(7,(3+2)) = 7*(+(3,2)).

 

we can "mix" prefix and infix in any order to express the same meaning.

*************************************/

 

%#######################################################################

%#######################################################################

%  QUESTION 7

%  Tracing and backtracking

%#######################################################################

%#######################################################################

 

% use trace of full brother

 

trace.

 

by tracing prolog, the system is trying to satisfy the predicate

defined by fullbrother:

 

  fullbrother(X,Y):-male(X),male(Y),father(F,X),father(F,Y),mother(M,X),mother(M,Y),diff(X,Y).

 

with the database:

 

  male(pascal).

  male(delphi).

  male(kylix).

  female(ada).

  father(pascal,delphi).

  father(pascal,kylix).

  mother(ada,delphi).

  mother(ada,kylix).

 

[trace] 3 ?- fullbrother(X,delphi).

 

   Call: (7) fullbrother(_G495, delphi) ? creep  

   Call: (8) male(_G495) ? creep

   Exit: (8) male(pascal) ? creep  

 

------  pascal is a male

 

   Call: (8) male(delphi) ? creep

   Exit: (8) male(delphi) ? creep 

 

------ delphi is a male

 

   Call: (8) father(_L187, pascal) ? creep

   Fail: (8) father(_L187, pascal) ? creep

  

------ pascal has no father   

  

   Redo: (8) male(_G495) ? creep  

   Exit: (8) male(delphi) ? creep

 

------ redo is delphi a male

  

   Call: (8) male(delphi) ? creep

   Exit: (8) male(delphi) ? creep  

 

------ again delphi is a male

  

   Call: (8) father(_L187, delphi) ? creep

   Exit: (8) father(pascal, delphi) ? creep

 

------ find another father to to delphi,try pascal

  

   Call: (8) father(pascal, delphi) ? creep

   Exit: (8) father(pascal, delphi) ? creep  

 

------ confirmation pascal is father to delphi

  

   Call: (8) mother(_L188, delphi) ? creep

   Exit: (8) mother(ada, delphi) ? creep

 

------ find another mother than ada,try ada

  

   Call: (8) mother(ada, delphi) ? creep

   Exit: (8) mother(ada, delphi) ? creep  

 

------ confirmation ada is mother of delphi

  

   Call: (8) diff(delphi, delphi) ? creep  

^  Call: (9) not(delphi==delphi) ? creep

^  Fail: (9) not(delphi==delphi) ? creep

   Fail: (8) diff(delphi, delphi) ? creep  

 

------ redundancy check

  

   Redo: (8) mother(ada, delphi) ? creep

   Redo: (8) mother(_L188, delphi) ? creep  

   Redo: (8) father(pascal, delphi) ? creep

   Redo: (8) father(_L187, delphi) ? creep  

   Redo: (8) male(_G495) ? creep  

   Exit: (8) male(kylix) ? creep

 

------ test if ada is mother and pascal father of delphi

  

   Call: (8) male(delphi) ? creep

   Exit: (8) male(delphi) ? creep  

 

------ ok delphi is a male

  

   Call: (8) father(_L187, kylix) ? creep

   Exit: (8) father(pascal, kylix) ? creep

 

------    find if kylix has another father

 

   Call: (8) father(pascal, delphi) ? creep

   Exit: (8) father(pascal, delphi) ? creep  

 

------ delphi is son of pascal

  

   Call: (8) mother(_L188, kylix) ? creep

   Exit: (8) mother(ada, kylix) ? creep

 

------ try is ada has another son

  

   Call: (8) mother(ada, delphi) ? creep

   Exit: (8) mother(ada, delphi) ? creep  

 

------ delphi is son of ada

  

   Call: (8) diff(kylix, delphi) ? creep  

^  Call: (9) not(kylix==delphi) ? creep

^  Exit: (9) not(kylix==delphi) ? creep

   Exit: (8) diff(kylix, delphi) ? creep  

   Exit: (7) fullbrother(kylix, delphi) ? creep

 

------ here all predicates are satisfied, kylix is the full brother of delphi

 

X = kylix

 

%#######################################################################

%#######################################################################