Programming Languages (CSC-302 99S)

Exam 3: Logic Programming, Syntax, and Types

This page may be found online at

Distributed: Friday, April 23, 1999
Due: Start of class, Friday, April 30, 1999
No extensions!


There are four questions on this exam. Each has equal point value, although each may take a different amount of time for you to complete.

If you are confused by any question, do not hesitate to ask me for clarification. I will reserve time at the start of classes next week for questions. In addition, you can speak to me individually. I may not be available at the time you are working on the exam. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445). Email is also a reasonable way to contact me.

This examination is open book, open notes, open mind, open computer, open Web. Feel free to use any and all resources available to you except for other people and answer keys. That is, you should not consult other students, other faculty, or anyone else. (If someone outside of Grinnell has already created a Web page that answers your question and is not an answer key, that's okay; however, you can't ask someone to create such a page.) In addition, while you can look at the pages I've created for this and other courses, you should not refer any pages that are clearly answer keys to homework assignments or exams.

As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

This is a take-home examination. It is likely to take you about four hours. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date. No late exams will be accepted. I may make a solution key available soon after the exam.

Because different students will be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say ``This is definitely the hardest exam I have ever taken.'' You may also summarize these policies.

Answer all of your questions electronically. That is, you must write all of your answers on the computer and print them out. Please put your answers in the same order that the problems appear in.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by highlighting your answer and including a clear set of work that you used to derive the answer.


1. Simplified Integers

As we saw in our discussion of types, we decided that it is possible to define integers with three basic operations:

In this notation, 3 would be written succ(succ(succ(zero))) and -2 would be written pred(pred(zero)).

In working with this notation, it may help to have techniques that allow you to convert from this form to ``standard'' integers and back again.

 * toInt(Num,Int)
 *   Converts a number in the pred/succ form to an integer.
 *   Does not like backtracking.
toInt(succ(Num), I) :- toInt(Num,Part), I is Part + 1.
toInt(pred(Num), I) :- toInt(Num,Part), I is Part - 1.

 * fromInt(Int,Num).
 *   Converts an integer to a number in pred/succ form.
 *   Can be used to generate numbers in the succ/pred form.
 *   Does not like backtracking.
fromInt(I, pred(Num)) :- I < 0, J is I+1, fromInt(J, Num).
fromInt(I, succ(Num)) :- I > 0, J is I-1, fromInt(J, Num).

For example,

?- toInt(pred(pred(zero)), X).
?- toInt(succ(succ(succ(zero))), X).
?- toInt(succ(pred(zero)), X).

Note that these are intended as generative predicates, but that they're not guaranteed to work successfully if you backtrack through them.

In each of the following, you are asked to write a predicate that manipulates values written using the pred/succ notation. Make sure that you include a few examples that show how your predicates work (or fail to work).

A. An equals predicate

Write an appropriate equals(X,Y) that holds if X and Y are equal, even if they don't have the same form. That is, pred(pred(succ(zero))) should be the same as pred(zero).

You may not use fromInt or toInt

B. Canonical Form

We might say that one of these integers is in ``canonical form'' if does not have both pred and succ. Write a predicate, canonical(X,C) that holds if C is the canonical form of X. Your predicate should also be able to generate the canonical form of any number.

You may not use fromInt or toInt

C. Addition

Write an appropriate add(Operand1,Operand2,Result) predicate that holds if Operand1 and Operand2 sum to Result.

You may not use fromInt or toInt

2. Some Parenthetical Grammars

Note that you only have two do two of B, C, and D. If you do all three correctly, you will receive some modicum of extra credit.

In many languages, such as Scheme, it is important to have balanced parentheses. That is, every opening paren has exactly one closing paren, every closing paren has exactly one opening paren, and the opening paren in each pair precedes the closing paren.

One might note that

One might write this in grammar form as

S ::=           // empty string; no parens
   |  '(' S ')' // parentheses around correct string
   |  S S       // two correct strings

A. Ambiguity

Unfortunately, this grammar is ambiguous. Find a string which has at least two parse trees and draw those trees.

Then rewrite this grammar unambiguously. Indicate which strategy you used to select among parse trees for strings with multiple parse trees.

B. Closing Multiple Parens

In some variants of LISP (real or imagined), a square right bracket closes ``the appropriate number'' of left parens, but must close at least one. For example, ((] is legal because the square bracket can close both left parens. Similarly, ((]) is legal, because the right bracket can close one left paren.

Write a grammar for this language.

C. Closing All Open Parens

In other variants of LISP, a square right bracket closes all open parens. In such a language, ((]) is illegal, because the right bracket closes both left parens, leaving nothing for the final right paren to close.

Write a grammar for this language.

D. Balanced Brackets

In yet other variants of LISP, you can use both parens and brackets. A right bracket closes all open parens up to the corresponding left bracket. For example, ([((]) is legal in this language.

Write a grammar for this language.

E. Comparing Grammars

From the perspective of grammar designer, which of the versions of the square bracket grammar was easier to write?

3. Assigning Records

Suppose we have been asked to design a language in which we would like to include a record type (in effect, an ordered product type) and assignment between record variables. To begin such a design, we need to consider some of the relationships between records.

We begin by considering three related types --- ab, abc, and abd --- defined as

  ab = record
    a: real;
    b: int;
  abc = record
    a: real;
    b: int;
    c: int;
  abd = record
    a: real;
    b: int;
    d: int;
  cab = record
    c: int;
    a: real;
    b: int;

We also define three variables using these types

  alpha: ab;
  beta: abc;
  gamma: abd;
  delta: cab;

A. Kinds of Assignment

Give a short argument both for and against each of the following assignments:

The following two are optional and may be done for extra credit.

B. Checking Assignment

Suppose that our language does not include recursive types (but records can have records as arguments). Write a type-checking routine for a language that accepts only the assignment v1=v2 (where v1 is of type T1 and v2 is of type T2) if T2 contains all of the fields of T1. (Note that T2 can contain additional fields.) This rule allows alpha=beta, alpha=delta, and beta=delta.

Write your routine in a reasonable pseudocode. You may assume a reasonable set of operations for getting information about types and fields.

4. Polymorphism

Surprisingly, polymorphism is a term that many computer scientists seem to use in different ways.

A. Some Definitions

Find three definitions of polymorphism other than those in our book and course web. At least one definition must come from a book, rather than from the Web. Write down the definitions.

B. Similarities

How are the definitions similar?

C. Differences

How are the definitions diffedifferent?

D. Synthesis

Write a clear definition of polymorphism, based on the definitions you have found (as well as those in the book and course web).


Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at

Source text last modified Mon Apr 26 12:53:04 1999.

This page generated on Mon Apr 26 12:59:29 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at