CS 291 Assignment #8

Due Saturday, April 18th (by 11:59pm)
Not accepted after April 21st

The Assignment

  1. Define a predicate numnames that describes the relationship between integers less than 1000 and their names:
    ?- numnames([five],X).
    X = 5 ;
    false.
    ?- numnames([thirteen],X).
    X = 13 ;
    false.
    ?- numnames([thirty],X).
    X = 30 ;
    false.
    ?- numnames([thirty, seven], X).
    X = 37 ;
    false.
    ?- numnames([two, hundred], X).
    X = 200 ;
    false.
    ?- numnames([nine, hundred, and, fifty, one], X).
    X = 951.
    ?- numnames(X, 375).
    X = [three, hundred, and, seventy, five] ;
    false.
    ?- numnames([seven], 7).
    true ;
    false.
    
    You will want to describe some facts exhaustively (e.g. the mapping between single digit names and values), while adding rules that describe how to build more complex number names out of simpler pieces. Hint: The first six tests above each illustrate a separate category of numbers that you will need to consider. Also, think about breaking this up into several relations when specifying your solution.
  2. In class we defined the predicate connected, that determined whether a pair of nodes in a graph were connected via one or more edges. The graph described by the edge facts was originally finite and acyclic. Below, I've added one more edge to the graph to produce a cycle (from f back to a). Thus there are now an infinite number of ways to demonstrate that two nodes are connected. Define a predicate path_connects that keeps track of the nodes visited while establishing a path between two nodes. Your solution should not refer to connected — it wouldn't do you any good anyway.

    Given:

    edge(a,b).
    edge(b,c).
    edge(b,d).
    edge(c,e).
    edge(d,e).
    edge(e,f).
    edge(g,h).
    edge(f,a).  % This new edge makes the graph cyclic
    
    Your predicate should behave as follows:
    ?- path_connects(a,c,P).
    P = [a, b, c] ;
    P = [a, b, c, e, f, a, b, c] ;
    P = [a, b, c, e, f, a, b, c, e|...] ;
    P = [a, b, c, e, f, a, b, c, e|...] ;
    P = [a, b, c, e, f, a, b, c, e|...] .
    
    ?- path_connects(a,d,[a,b,d]).
    true ;
    false.
    
    ?- path_connects(a,g,P).
    ERROR: Out of local stack
       Exception: (697,494) path_connects(c, g, _G2092467) ?
    
    (When the last query above throws the error, press 'a' to abort the execution.)
  3. Draw the search tree for the query con(a,d) given the program shown below. (The edges define a finite, acyclic graph.) You must show all nodes in the tree up to a depth of five. (That is, four more levels of nodes after the root query.) If you've got access to a tree-drawing package of some sort, feel free to generate and submit an electronic version of the tree. Otherwise, you can draw the tree on good old-fashioned paper and submit it in person.

    edge(a,b).
    edge(a,c).
    edge(c,d).
    edge(c,e).
    
    con(X,Y) :- edge(X,Y).
    con(X,Y) :- con(X,Z), con(Z,Y).
    

Submitting:

Submit your file(s) as attachments to me at brichards@ups.edu. If you plan to submit the last problem on paper, please plan ahead so you don't have to submit it late!


Brad Richards, 2009