Computer Science 3675
Section 001
Fall 2006
Solutions to practice questions for quiz 5

  1. True or false.

    1. Using a cut can reduce the time used by a program. True

    2. Using a cut can reduce the amount of memory used by a program. True

    3. Using a cut in a predicate definition usually restricts the modes in which a predicate can run. True

    4. It is rare to find cuts in logic programs. False

  2. Explain negation as failure. What is its goal, and how does it work?

    Negation as failure is intended to make it possible to express that a given atom is false. The mechanism is to try to prove the atom true. If it is possible to prove atom A true, then the negation of A is false. But if the proof of A failse, then the negation of A must be true.

  3. In a logic programming style, write axioms for computing predicate samelength(X,Y), which is true just when X and Y are lists that have the same length.

    Here is it written in Prolog.

        samelength([],[]).
        samelength([_|A],[_|B]) :- samelength(A,B).
      

  4. In a logic programming style, write axioms for computing predicate allsame(X), which is true just when all members of list X are the same. For example, allsame([5,5,5]) is true, as is allsame([a,a]), but allsame([2,4,4]) is false. Note that allsame([]) is true, and allsame([b]) is true.

    Here it is written in Prolog.

        allsame([]).
        allsame([_]).
        allsame([H,H|R]) :- allsame([H|R]).
      

  5. Two individuals are cousins if they have a grandparent in common, but they are not the same person, and are not siblings. Using predicate grandparent(X, Y), meaning that X is a grandparent of Y, and predicate sibling(X, Y) meaning that X and Y are siblings, write a definition of cousin(X, Y) using Prolog notation, meaning that X and Y are cousins.

    In a typical Prolog dialect, the definition would be as follows.

      cousin(X,Y) :- grandparent(Z,X), 
                     grandparent(Z,Y), 
                     not(X = Y), 
                     not(sibling(X,Y)).
    
    ISO Prolog uses unary operator \+ instead of not, so the definition is as follows in ISO Prolog. <
      cousin(X,Y) :- grandparent(Z,X), 
                     grandparent(Z,Y), 
                     \+ X = Y, 
                     \+ sibling(X,Y).
    

  6. Show the logic programming search tree for goal (member(X,[3,4,5]), member(X,[4])), up to the point where a success is found. The definition of the member predicate is as follows, written in Prolog syntax.

          member(M, [M|X]).
          member(M, [X|T]) :- member(M, T).
      

    The graphics aren't pretty, but here what the tree looks like.

                 member(X,[3,4,5]), member(X,[4])
                /                               \
                | X = 3                      member(X,[4,5]), member(X,[4])
                |                           /                              \
            member(3,[4])                   | X = 4
                         \                  |
                   member(3,[])             |
                                            |
                                       member(4,[4])
                                      /             \
                                   success