Prolog For Solving Cousin, Male Cousin, Female Cousin and Second Cousin and I Am My Own Grandfather

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

Part A: Prolog for solving Cousin, Male Cousin, Female Cousin and Second Cousin
implement main open core constants className = "main". classVersion = "". class facts gender : (string Person, string Gender) . parent : (string Parent, string Child) . siblings : (string PersonOne, string PersonTwo) . clauses parent("Mw", "Hs") . parent("Mw" , "Js"). parent("Mw", "Sa"). parent("Ms", "An"). parent("Ms", "Hr"). parent("Jl", "Al"). parent("Jl", "Ns"). parent("Jl", "Md"). parent("Ar", "Jl"). %parent of parent - this parent has no siblings only cousins parent("Mz", "Ms"). %parent of parent parent("Mz", "Mw"). %parent of parent siblings("Ms", "Mw"). %parents are siblings (for cousins) siblings("Mz", "Ar"). %grand parents are siblings (for second cousins) gender("Hs", "Male"). gender("Js", "Male"). gender("Hr", "Male"). gender("Sa", "Female"). gender("An", "Female"). class predicates solveCousins : () procedure . cousins : (string PersonOne, string PersonTwo) nondeterm(o,o).

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

solveMaleCousins : () procedure . maleCousins : ( string PersonOne, string PersonTwo) nondeterm (o,o). solveFemaleCousins : () procedure . femaleCousins : ( string PersonOne, string PersonTwo) nondeterm (o,o). solveSecondCousins : () procedure . secondCousins : ( string PersonOne, string PersonTwo) nondeterm (o,o). clauses cousins (X, Y):parent(A, X) and parent(B, Y) and siblings(A, B). clauses solveCousins () :cousins(X, Y), console::write(X , " and ", Y , " are cousins.\n"), fail. solveCousins(). clauses maleCousins (X, Y) :parent( A, X ) and parent(B, Y) and gender(Y, "Male") and (siblings(A, B) or siblings(B , A)). clauses solveMaleCousins() :maleCousins(X, Y), console::write(Y, " is male cousin of ", X, ".\n"), fail. solveMaleCousins(). clauses femaleCousins (X, Y) :parent( A, X ) and parent(B, Y) and gender(Y, "Female") and (siblings(A, B) or siblings(B , A)).

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

clauses solveFemaleCousins() :femaleCousins(X, Y), console::write(Y, " is female cousin of ", X, ".\n"), fail. solveFemaleCousins(). clauses secondCousins (X, Y) :parent( A, X ) and parent(B, Y) and parent(M, A) and parent(N, B) and siblings(M, N). clauses solveSecondCousins() :secondCousins(X, Y), console::write(X , " and ", Y , " are second cousins.\n"), fail. solveSecondCousins(). clauses classInfo(className, classVersion). clauses run():console::init(), console::write("\n\n\n************COUSINS*********\n\n\n"), solveCousins(), console::write("\n\n\n************MALE COUSINS*********\n\n\n"), solveMaleCousins(), console::write("\n\n\n************FEMALE COUSINS*********\n\n\n"), solveFemaleCousins(), console::write("\n\n\n************SECOND COUSINS*********\n\n\n"), solveSecondCousins(), succeed(). end implement main goal mainExe::run(main::run).

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

Part B: Book Exercise


Q.3 Prove P Q is logically equivalent to (P v Q) (P ^ Q) based on definition P Q = (PQ) ^ (QP) a) By using truth table: P T T F F Q T F T F P  Q T F T T Q  P T T F T (PQ) ^ (QP) T F F T P v Q T T T F P ^ Q T F F F (P v Q)  (P ^ Q) T F F T

Since Truth values for both (PQ) ^ (QP) and (P v Q)  (P ^ Q) are logically equivalent, we can infer that P Q is logically equivalent to (P v Q) (P ^ Q). b) By series of substitutions by using identities on page 49: (PQ) ^ (QP) | (P ^ Q)  (P v Q) Let s take left hand side equation (PQ) ^ (QP) We know that (P v Q) | ( P  Q) We can infer from this rule that (P ^ Q) | (P  Q) Consider P = (PQ) and Q = (QP) Now by using identity (P ^ Q) | (P  Q) on (PQ) ^ (QP) we can write: (PQ) ^ (QP) | [(P Q)  (Q  P)] We also know that (P v Q) | ( P  Q) or (P  Q) | (P v Q) and (Q  P) | (Q v P) We can infer that: [(P Q)  (Q  P)] | [(P v Q)  (Q v P)] Or by cancelling on both sides: (P Q)  (Q  P) | (P v Q)  (Q v P) We also know from identities that P v Q | P ^ Q and Q v P | P v Q We can write: (P v Q)  (Q v P) | (P ^ Q)  (P v Q) Since Q is same on both sides of implication we can replace Q with Q So final equation will be: (P ^ Q)  (P v Q) which is same as right hand side equation. Hence we can infer that (PQ) ^ (QP) is logically equivalent to (P ^ Q)  (P v Q) which is logically equivalent to P Q.

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

Q.12 I am my own grand father Situation: parent("D", parent( W , parent("D", parent("I", parent("F", parent("F", parent("W", parent("I", married("I" married("F" "S2") S1 ) "I") "S1") "S2") "I") "D") "D") , "W") , "D")

Expressions defining basic family relationships: FatherInLaw(X,Y) Married(Y,Z) ^ Father(X,Z) BrotherInLaw(X,Y) Married(Y,Z) ^ Brother(X,Z) SonInLaw(X,Y) Married(X,Z) ^ Parent(Y,Z) GrandParent(X,Y) Parent(Z,Y) ^ Parent(X,Z) Prove that I am my own GrandFather 1. My wife W is D s parent a. Parent(W,D) 2. I am married to W that implies I am also parent of D b. Married(I,W) Parent(I, D) 3. My Father F is my (I s) parent c. Parent(F,I) 4. My Father F is married to my daughter D that implies that D is my parent d. Married(F,D) Parent(D I) 5. So my daughter is my parent and I am hers, as inferred from d and b e. Parent(D,I) ^ Parent(I,D) 6. Since a grandparent should be parent of subject s parent. Here if I am subject and my parent is D and I am D s parent, hence it can be inferred that I am my own grandparent. f. (Parent(D,I) ^ Parent(I,D)) (grandparent(I,I) ^ grandparent(D,D))

Artificial Intelligence Assignment 2

Haisum | 0812155 | BS-SE VII

23-08-2011

In a single expression: [{(parent(W,D) ^ married(I,W)) parent(I,D)} ^ {(parent(F,I) ^ married(F,D)) parent(D,I)}] [grandparent(I,I) ^ grandparent(D,D)]

You might also like