Assignment 9 for CSCI 431

Running Prolog at UNCA

Some of the problems below can be worked by running the C-Prolog interpreter on Ivy. Here are a few guidelines for using the Prolog interpreter.

Rules Exercises

  1. Indicate whether the following are syntactically correct Rules.
    
    a :-  b, c, d:- e f.
    
    happy(X):- a , b.
    
    happy(X):- hasmoney(X) & has_friends(X).
    
    fun(fish):- blue(betty), bike(yamaha).
    
  2. Given the database below, indicate whether you think a particular query will succeed or fail.
    
    likes(john,mary).
    likes(john,trains).
    likes(peter,fast_cars).       
    
    likes(Person1,Person2):-
    hobby(Person1,Hobby), hobby(Person2,Hobby).
    hobby(john,trainspotting). hobby(tim,sailing). hobby(helen,trainspotting). hobby(simon,sailing). ?- likes(john,trains). ?- likes(helen,john). ?- likes(tim,helen). ?- likes(john,helen).

Search Exercises

  1. Consider the goal ?- fun(What) using the database below.
    
    fun(X) :-              /* fun if .... */                       
            red(X),        /* red */                    
            car(X).            /* and a car */             
    fun(X) :-              /* or its fun if its.... */    
            blue(X),           /* blue */                   
            bike(X).           /* and a bike */       
    
    /* database of red  & blue items */
    red(cricket_ball).        blue(cheese).       
    red(my_hat).              blue(raleigh).        
    red(car_27).              blue(honda).        
                                                   
    /* database of cars  and bikes*/             
    car(peugot).              bike(yamaha).        
    car(rover).               bike(raleigh).     
                              bike(honda).         
    

    Below you will see pairs of goals. These may either be output of a goal e.g. red(my_hat) or calls to another goal e.g. car(my_hat). Indicate which one of the following goal pairs you would expect to see first, for the above query.

    1. fun(X)
      red(X)
    2. blue(cheese)
      red(car_27)
    3. bike(honda)
      bike(yamaha)
    4. blue(cheese)
      bike(raleigh)
    5. bike(X)
      car(X)

  2. Consider the following program.
    
    a(X):-
         b(X), c(X), d(X).
    a(X):-
         c(X), d(X).
    a(X):-
         d(X).
    
    b(1).                             
    b(a).                            
    b(2).
    b(3).
    d(10).
    d(11).
    c(3).
    c(4).
    

    Given the query ?- a(X). Indicate the successive variable bindings that the variable X gets when the above query is run. Separate bindings by a comma (ie. 1,3,5.....)

Lists Exercise