Programming with Induction, Recursion, and Algebraic Laws

COMP 105 Assignment

Due Tuesday, September 11, 2018 at 11:59PM

This first assignment is divided into three parts:

NOTE: This assignment is due one minute before midnight. You may turn it in up to 24 hours after the due date, which will cost you one extension token. If you wish not to spend an extension token, then when midnight arrives submit whatever you have. We give partial credit.

ALERT: This assignment is significantly easier than a typical COMP 105 assignment. Its role is to get you acclimated and to help you start thinking systematically about writing code from scratch, especially using algebraic laws to write recursive functions. Later assignments get harder and more time-consuming, so don’t use this one to gauge the difficulty of the course.

Reading-Comprehension Questions (10%)

Before starting the programming problems, answer the following questions about the reading. Place your answers in a plain text file called cqs.impcore.txt. The best procedure is to download the questions and edit in your answers.

Please read pages 6–14 in Programming Languages: Build, Prove, and Compare.

  1. What is the value of the following Impcore expression?

    (if (> 3 5) 17 99)
  2. Which of the following best describes the syntactic structure of Impcore?

    1. An expression can contain a definition
    2. A definition can contain an expression
    3. Both of the above
    4. None of the above
  3. Does the following Impcore test pass? Please answer “yes” or “no.”

    (check-expect (+ 1 2 3) 6)

    Assuming x is bound to a global variable, does the following Impcore test pass? Again, please answer “yes” or “no.”

    (check-expect (set x 1) 1)

Next read section 1.2, which starts on page 15, about abstract syntax.

  1. After reading about abstract syntax, look at this picture of an abstract-syntax tree for a “calculator expression”:

    Picture of an abstract-syntax tree 

    Answer these questions:

    1. What concrete syntax could you write in C for this expression?
    2. What concrete syntax could you write in Impcore for this expression?

Read the handout on programming with proof systems and algebraic laws, at https://www.cs.tufts.edu/comp/105/handouts/natproofs.pdf.

  1. I show you a recursive function f that takes one argument, a natural number n. The structure of n, and therefore the recursion pattern of f, are based on the Peano proof system from the handout.

    1. What are the different ways n can be formed?
    2. When f is given n, what code do you expect f to use to determine how n was formed?
    3. For which values of n do you expect f to make a recursive call?
    4. When a recursive call is made, what value is passed as argument?

Read the expectations about contracts in the course coding guidelines.

  1. Suppose I write a contract for a power function that says, “this function multiplies x by itself n times.” According to our expectations, is this a good contract or a bad contract? Please answer “good” or “bad.”

  2. At the end of the handout on programming with proof systems and algebraic laws, you will find a section on “Complete process examples.” This section suggests that the factorial function—but not the power function—could be submitted without a contract.

    1. Why would it be OK to submit the factorial function without a contract? For an idea, look at the “Exemplary” column in the “Documentation” section of the general coding rubric.
    2. Why doesn’t the same argument apply to the power function? For an idea, check the programming handout.

Programming in Impcore (80%)

The problems below serve multiple purposes:

You can start these exercises immediately after the first lecture. You may write very efficient solutions—but do not feel compelled to do so. Just make sure that your recursive functions terminate!

Do not share your solutions with anyone. We encourage you to discuss ideas, but no other student may see your code.

Getting set up with the software

The interpreter you need to run Impcore code is provided as part of the course. To add course software to your execution path, run

use -q comp105

You may want to put this command in your .cshrc or your .profile. The -q option is needed to prevent use from spraying text onto standard output, which may interfere with with scp, ssh, git, and rsync.

The impcore interpreter is in /comp/105/bin; if you have run use as suggested above you should be able to run it just by typing

ledit impcore

The ledit command gives you the ability to retrieve and edit previous lines of input.

If your code and unit tests are in file solution.imp, you can load and run them by typing

impcore -q < solution.imp

Unit testing

The special “extended-definition forms” check-expect, check-assert, and check-error are part of every language in the book. For example, as described in section 1.1.1 of the book (page 6), they are part of the Impcore language. These forms serve both as unit tests and as documentation. Every function you write must be tested and documented using check-expect or check-assert, and possibly also check-error. The number of unit tests must be appropriate to the function’s contract and to the structure of its input, as described in the programming handout.

How do you know when to use check-expect and when to use check-assert? Like this: check-expect tells if two expressions evaluate to the same value, and check-assert checks to see if a single expression evaluates to truth or falsehood. To be confident you are getting things right,

Expectations for your software-design process

To write code from scratch, this course recommends a nine-step design process. In this homework, the process is primarily for practice—many students will be able to do the problems without the help that the process provides. But in a couple of weeks, you will need to have a good process, or you will find that the homeworks take too much time.

On this homework, here’s what we expect for each step of the process:

  1. All data are integers, and most are natural numbers. We expect you to be able to break natural numbers down into the forms described in the handout on programming with proof systems and algebraic laws. We expect you to express your understanding in a short comment following each applicable function. Those forms will appear on the left-hand sides of the algebraic laws you write for step 6

  2. We expect you to be able to choose example inputs, which we expect to see in your unit tests.

  3. We expect you to choose names only for any helper functions that you choose to write. Most problems, but not all, can be solved without helper functions. When there are helper functions, they will be scrutinized carefully, and their names will be judged according to the general coding rubric.

  4. We expect each function you submit to be preceded by a short contract that appears in a comment.

    • Contracts for the assigned functions don’t require much thought; language from the book (or this homework) is fine.

    • Contracts for helper functions are another story. For each helper function you define, choose a good name, and write a specific, accurate contract. Use the coding guidelines. Your contract must meet the expectations laid out in the Documentation section of the general coding rubric.

    The contracts for the helper functions will weigh more heavily toward your grade.

    Here’s a model contract:

    ;; (occurs-in? d n) returns 1 if and only if decimal digit `d`
    ;; occurs in the decimal representation of the positive integer
    ;; `n`; it returns 0 otherwise.
  5. We expect unit tests to be submitted with each function, and we expect them to follow the function and to be indented eight spaces. We expect only the basics: one or two unit tests for each form of input.1 Additional unit tests are acceptable, but they must be separated from the basic tests by a blank line.

    Here are two example unit tests:

    (check-assert (not (occurs-in? 7 123)))
    (check-assert      (occurs-in? 2 123))
  6. We expect you to write algebraic laws for any function that contains an if expression or a recursive call. Algebraic laws should appear between a function’s contract and its definition.

  7. We expect the body of each function to have at most one case per algebraic law.

  8. We expect the body of each function to be consistent with the function’s algebraic laws.

  9. We expect each function’s unit tests to be indented eight spaces, and to pass.

Help with the process: a solution template

To help you organize and present the results of your work, we provide a solution template. Copy the template into file solution.imp. The template provides placeholders for contracts (step 4), unit tests (step 5), algebraic laws (step 6), and function definitions (steps 7 and 8).

In the template, you will see that most functions are followed by a single unit test that uses check-error. The test is a placeholder. Remove the check-error and replace it with unit tests that use check-expect or check-assert, which you must write. (For most functions, you will need multiple unit tests: at least one per algebraic law.)

Indent all unit tests by 8 spaces.

A placeholder function definition has body (/ 1 0). Evaluating this code divides 1 by 0, which causes an error. The solution template should pass all unit tests, as reported by

impcore -q < solution.imp

The template does not include placeholders for helper functions. If you write any helper functions, supply each helper function with a contract, laws, and unit tests.

Quick help with Impcore

The concrete syntax of Impcore can be found on page 6 of Programming Languages: Build, Prove, and Compare. As a quick summary, the following code uses every possible syntactic form of expression. But on this assignment, you must not use the while and set forms, and the code you submit must not print.

(define even? (n) (= (mod n 2) 0))

(define 3n+1-sequence (n) ; from Collatz
  (begin
    (while (!= n 1)
       (begin
         (println n)
         (if (even? n)
             (set n (/ n 2))
             (set n (+ (* 3 n) 1)))))
    n))

The “initial basis” of a programming language is a fancy technical name for the names that are already defined. In Impcore, the initial basis comprises primitive and predefined functions, which have these names:

mod      or       println  /        
!=       and      =        *        
>=       printu   >        -        
<=       print    <        +        
not      

The problems you must solve

You will solve three groups of ordinary problems and two challenge problems. Do the problems in the order in which they appear below, not in book-numbering order.

Direct applications of the method in the handout

Each problem in the first group can be solved by direct application of the methods sketched in the handout: choose a proof system, write algebraic laws, design the code.

The problems are as follows:

Generalization of the method in the handout

Each problem in the second group can be solved by generalizing the methods in the handout.

Recursion that is not structural

A recursive computation that is driven by the structure of the data, as in the first two groups of problems, is called structural recursion. Not all recursions are structural.

Two challenge problems

Other expectations for your solutions

For this assignment, we expect you to apply the recommended design process and to deliver working functions with good names, clear contracts, algebraic laws, and unit tests. We also expect the following:

A photograph we can use to learn your name (10%)

I hope to learn the name of every student in the class. The teaching assistants and recitation leaders would also like to learn your name. But we need your help. Part of the assignment, for 10% of the grade on the assignment, is to submit a photograph that will help us learn to recognize you. I’ve consulted with a skilled portrait photographer to prepare guidelines for producing a good, easily recognizable photograph, even if all you have is a cellphone camera. You’ll submit the photo as photo.jpg.

What to submit and how to submit it

You’ll choose a directory for your assignment, in which you will create or copy these files:

As soon as you have the files listed above, run submit105-impcore to submit a preliminary version of your work. The submit script checks your work and runs provide on your behalf. Do submit early, then keep submitting until your work is complete; we grade only the last submission.

The submit script will ask you some questions:

You may also submit extra-tests.imp, which should contain only test code and unit tests (check-expect or check-error). You can run the tests using the Unix command

cat solution.imp extra-tests.imp | impcore -q

How your work will be evaluated

How your code will be evaluated

In this assignment, you learn how we expect your code to be structured and organized. Our expectations are presented on the coding-style page on the course web site. When we get your work, we will evaluate it in two ways:

The detailed criteria we will use to assess your code are found at http://www.cs.tufts.edu/comp/105/coding-rubric.html. Though things may be worded differently, most of these criteria are also applied in COMP 11, 15, and 40—they make explicit what we mean by “good programming practice.” But as you might imagine, there is a lot of information there—probably more than you can assimilate in one reading. The highlights are

How your photograph will be evaluated

If your photograph is clear, makes it easy to recognize you, and is not ridiculous in size, it will earn a grade of Very Good (the top grade). If you’re not sure how to take a photograph that makes you easy to recognize, consult the table below. Aim for an “Exemplary” photograph (the left column), be willing to settle for “Satisfactory” (the middle column), and avoid “Must Improve” (the right column).2

Exemplary Satisfactory Must improve
Composition

• Head and shoulders fill 2/3 to 3/4 of the frame.

• The shot is taken from a distance of 4 to 6 feet, and the camera is zoomed as needed to include just head and shoulders.

Or, the shot is taken from a distance of 4 to 6 feet, then cropped to include just head and shoulders.

• The subject is looking at the camera with a normal look on the face.

• Face fills the frame; shoulders not visible.

• The subject is not looking at the camera, but there is a normal look on the face.

• Photo down to waist; full-body photo.

• More than one person visible in photo.

• Eyes are closed.

• The camera is too close to the subject. (This will happen if you compose the shot by moving the camera toward the subject until the subject’s head and shoulders fill the “viewfinder”; you’ll get perspective distortion.)

• The subject is making a strange face (eye rolls, etc)

Lighting

• The subject is illuminated by two or more light sources, such that one side of the subject’s face is noticeably brighter than the other (about 2 to 1).

• The main sources of light are soft and diffuse: overcast sky, indirect daylight, daylight reflected off a wall or building, and so on.

• The subject’s face is lit evenly.

• The subject’s face is lit by ambient light, plus flash bounced off a ceiling or wall (possible only with a real camera)

• The background is significantly brighter than the subject.

• There is light behind the subject aimed at the camera.

• The sun is shining in the subject’s face.

• The subject is illuminated by a camera flash.

Focus

• The subject is in sharp focus, while the background is a little blurry (possibly only with a real camera or with very sophisticated software).

• The subject’s face is in sharp focus.

• Some part of the subject is in sharp focus, or something near the subject is in sharp focus. The subject’s face is not in sharp focus but is still easy to recognize.

• The subject’s face is out of focus.

File

• The uploaded image file is from 300KB to 1.2MB in size.

• Resolution of the uploaded file is high enough that there’s no pixelation.

• The uploaded image is at least as tall as it is wide (portrait orientation)

• The uploaded image file is at most 2MB in size.

• When shown at a few inches high, the uploaded image file is pixelated or has compression blur.

• The uploaded image file is more than 2MB in size.

• At its natural resolution, the uploaded image file shows pixels or compression artifacts.

• The uploaded image is wider than it is tall (landscape orientation)


  1. For ordinary results, we expect one test per form of input. For Boolean results, we expect one “true” test and one “false” test for each form of input, when possible.

  2. Yes, a student once sent me a photograph of two people.