Exercises AI lab Exercise 1 Which of the following sequences of characters are atoms, which are variables, and which are neither? 1. variable23 2. Variable2000 3. big_kahuna_burger 4. ’big kahuna burger’ 5. big kahuna burger 6. _Jules Answer 1: 1. variable23 is an atom: it starts with a lower case letter. 2. Variable2000 is a variable: it starts with a capital letter. 3. big_kahuna_burger is an atom: it starts with a lower case letter. 4. 'big kahuna burger' is an atom: it is between two quotes ('). 5. big kahuna burger is neither: variables can never contain blanks and atoms cannot either unless the atom starts ends with '. 6. _Jules is a variable: it starts with _. Exercise 2: Which of the following sequences of characters are atoms, which are variables, which are complex terms, and which are not terms at all? Give the functor and arity of each complex term. 1. loves(Vincent,mia) 2. ’loves(Vincent,mia)’ 3. and(big(X),kahuna(X)) 4. _and(big(X),kahuna(X)) 5. (Butch kills Vincent) Answer 2: 1. loves(Vincent,mia) is a complex term. Functor is loves with arity 2. 2. 'loves(Vincent,mia)' is an atom: it is enclosed between quotes. 1 3. and(big(X),kahuna(X)) is a complex term. Functor is and with arity 2. 4. _and(big(X),kahuna(X)) is not a term. It starts with an underscore and can therefore not be an atom or a complex term. It cannot be a variable either because variables are not supposed to contain parentheses or commas. 5. (Butch kills Vincent) is not a term. It contains parentheses and empty spaces and can therefore neither be an atom nor a variable. It doesn't have the right format for a complex term either; e.g., there is no functor. Exercise 3 How many facts, rules, clauses are there in the following knowledge base? What are the heads of the rules, and what are the goals they contain? woman(vincent). woman(mia). man(jules). person(X) :- man(X); woman(X). loves(X,Y) :- knows(Y,X). father(Y,Z) :- man(Y), son(Z,Y). father(Y,Z) :- man(Y), daughter(Z,Y). Answer 3: There are 3 facts and 4 rules in this knowledge base. This means there are 7 clauses. The heads of the rules are person(X), loves(X,Y), and father(Y,Z) (what's on the left hand side of the rules), the goals are man(X), woman(X), knows(Y,X), man(Y), son(Z,Y), and daughter(Z,Y) (everything on the right hand side of the rules). Exercise 4 Represent the following in Prolog: 1. Butch is a killer. 2. Mia and Marcellus are married. 3. Zed is dead. 4. Mia loves everyone who is a good dancer. 5. Jules eats anything that is nutritious. 2 Answer 4: 1. Butch is a killer: killer(butch). 2. Mia and Marsellus are married: married(mia, marsellus). 3. Zed is dead: dead(zed). 4. Mia loves everyone who is a good dancer: love(mia,X) :- good_dancer(X). 5. Jules eats anything that is nutritious eat(jules,X) :- nutritious(X). Exercise 5 Which of the following pairs of terms match? Where relevant, give the variable instantiations that lead to successful matching. 1. ’bread’ = bread 2. Bread = bread 3. bread = sausage 4. food(bread,X,beer) = food(Y,sausage,X) 5. food(bread,X,beer) = food(Y,kahuna_burger) Answer 5: 1. 'bread' = bread matches. 2. Bread = bread matches; the variable Bread gets instantiated with the atom bread. 3. bread = sausage doesn't match. 4. food(bread,X,beer) = food(Y,sausage,X) doesn't match; X cannot be instantiated with sausage as well as beer. 5. food(bread,X,beer) = food(Y,kahuna_burger) doesn't match; terms are of different arity. Exercise 6 How does Prolog respond to the following queries? 1. [a,b,c,d] = [a,[b,c,d]]. 2. [a,b,c,d] = [a|[b,c,d]]. 3. [a,b,c,d] = [a,b,[c,d]]. 4. [a,b,c,d] = [a,b,c,d,[]]. 3 Answer 6: 1. ?- [a,b,c,d] = [a,[b,c,d]]. No (The first list has four elements; the second only two.) 2. ?- [a,b,c,d] = [a|[b,c,d]]. Yes 3. ?- [a,b,c,d] = [a,b,[c,d]]. No 4. ?- [a,b,c,d] = [a,b,c,d,[]]. No Exercise 7 How does Prolog respond to the following queries? 1. X = 3*4. 2. 4 is X. 3. X = Y. 4. is(X,+(1,2)). 5. 3+2 = +(3,2). Answer 7: 1. X = 3*4. Prolog answers: X = 3*4. Variable X is instantiated with complex term 3*4. 2. 4 is X. Prolog answers: ERROR: Arguments are not sufficiently instantiated. 3. X = Y. Prolog answers: X = Y. 4. is(X,+(1,2)). Prolog answers: X = 3. 5. 3+2 = +(3,2). Prolog answers: yes. 3+2 and +(3,2) are two ways of writing the same term. 4