Assignment 6 for CSCI 431

PART I --- Prolog

Running Prolog:

Use the Java Prolog interpreter introduced in class or you can use this 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).
  • 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

    Part II --- Ch 7

    1. Consider the Pascal program below
      
      PROGRAM MAIN(INPUT, OUTPUT) ;
      
        PROCEDURE P(V: INTEGER) ;
        VAR
           X: INTEGER ;
      
          PROCEDURE Q(V: INTEGER) ;
          BEGIN
            X := V ;
            WRITELN("In Q -- X = ", X:2, ", V = ", V:2) ;
          END ;
      
          PROCEDURE R(V: INTEGER) ;
            VAR
              X: INTEGER ;
      
            PROCEDURE S(V: INTEGER) ;
            BEGIN
               X := V ;
               Q(V+1) ;
               WRITELN("In S -- X = ", X:2, ", V = ", V:2) ;
            END ;
        
          BEGIN
            X := V ;
            S(V+1) ;
            WRITELN("In R -- X = ", X:2, ", V = ", V:2) ;
          END ;
      
        BEGIN
          X := V ;
          R(V+1) ;
          WRITELN("In P -- X = ", X:2, ", V = ", V:2);
          IF (V < 45000) THEN
             P(48048) ;
        END ;
      
      BEGIN
         P(43680) ;
      END .
      
      
      1. Assuming static scoping, what will be printed?
      2. Assuming dynamic scoping, what will be printed?


    2. Consider the procedure BIGSUB in PL/1 syntax below. For each of the following parameter-passing methods, state the values in the array LIST in BIGSUB after the return from SUB:
      1. parameters passed by value
      2. parameters passed by reference
      3. parameters passed by name
      4. parameters passed by value-result
      
      procedure BIGSUB;
                 integer GLOBAL;
                 integer array LIST[1..2];
                 procedure SUB (PARAM);
                    integer PARAM;  (declaration of type for the parameter PARAM)
                    begin
                    PARAM := 3;
                    GLOBAL := GLOBAL +1;
                    PARAM := 5;
                    end (SUB);
                begin
                LIST[1] := 2;
                LIST[2] := 2;
                GLOBAL := 1;
                SUB(LIST[GLOBAL]);
                end (BIGSUB);
      

    3. Show the run-time stack with activation record instances, including static and dynamic links, when execution reaches position 1 in the following skeletal program.
      
      
         procedure Main;
            procedure A;
               procedure B;
                   begin { B }
                                      <============ 1
                   end; { B }
              procedure C;
                   begin { C }
                   
                   B;
                   
                   end; { C }
            begin { A }
            
            C;
            
            end; { A }
         begin { Main }
         
         A;
         
         end; { Main }