Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
CS314: Principles of programming languages
[go: Go Back, main page]

CS314: Principles of programming languages

[Computer languages history]




“Well, what do you know about that! These forty years now, I’ve been speaking in prose without knowing it!”

From plotting queries to designing circuits, programming languages represent the substrate of computation. This course aims to teach you to use, compare, and build languages, in three ways. First, you will practice functional programming in contrast to object-oriented and imperative programming, which you may be more familiar with. Second, you will build interpreters to explore fundamental concepts across languages, such as syntax, semantics, binding, types, state, and control. Finally, you will connect these ideas to other programming languages and paradigms, in spite of their superficial differences.

This Web page is for Sections 01 and 02 in Spring 2006. Section 10 is taught separately by Detlef Ronneburger with Matthew Casella.

Lectures: Mon/Wed 5:00–6:20pm, SERC 118
Section 01 recitation: Tue 3:35–4:30pm, SERC 216
Section 02 recitation: Tue 6:55–7:50pm, SERC 208

Professor: Chung-chieh Shan (ccshan at cs)
Office hours: Mon 12–2pm or email to make an appointment, CoRE 306, 732-445-2003

Teaching assistant: Sang Hoon Yeo (yeo at paul)
Office hours: Thu 3:30–5:30pm, Hill 486, 732-445-3908

Teaching assistant: Jerry Hom (jhom at remus)
Office hours: Fri 2:30–4:30pm, CoRE 334, 732-445-0050

Announcements

(5/1) Another extra-credit option on the last homework
In addition to Haskell, Prolog, Java, and CALL-BY-NEED, you can get extra credit on the 4/24 homework by solving n-queens in OCaml.
(4/26) Grades
If you wish, you can write all four extra-credit programs on the 4/24 homework. Each program is worth one problem (that is, 50% of a homework). See the “Software” section on this page for more information on Haskell and Prolog.
There will not be a final exam. Instead, the last 20% of your total grade will be your average homework grade or your average midterm grade, whichever is greater. (This calculation currently yields the same letter grades for everyone as simply computing the total grade out of 80%.)
(4/24) Some demo programs from class
To demonstrate how non-strict (in other words, call-by-need (in other words, lazy) or call-by-name) languages avoid an infinite loop, we used the following programs in class today. To demonstrate how non-strict languages delay side effects such as mutable state, we used the following programs in class today. These programs will run in IMPLICIT-REFS, CALL-BY-REFERENCE, CALL-BY-NAME, and CALL-BY-NEED—try them out to see how the behavior changes!
To demonstrate non-strict data structures in Haskell, we used the following programs in class today. Again, try them out to see how they work!
(4/24) Fixed link to inferred.scm
The link in the schedule to the INFERRED interpreter used to point at the CHECKED interpreter instead. Fixed! Thanks to Alanza Burke.
(4/17) Participation credit in recitations
Despite (or due to) the fact that attending recitation helps you learn, the recitation quizzes were intended to account for your help to others. People leaving right after the quiz indicated that this intention was not accomplished. Therefore, there is no more extra credit for quizzes. You will receive extra credit for your participation in recitations just as in the class newsgroup: thoughtfully and concretely initiate or continue a discussion about the readings, the lectures, the recitations, or the homework.
(4/12) Fix to bank.scm
Janet Weiss found a bug in bank.scm: in the second version of the code in the file, bank-s should call itself, not bank. Fixed. Thanks!
(4/5) What is a context?
The homework problem on observational equivalence only defines a context as “a program that is missing a part”. The missing part must be an expression according to the grammar, which you can find under the-grammar in the interpreter. For example, let x = □ in 0 is a context, but let □ = 3 in 0 is not. Because let □ = 3 in 0 is not a context, you can’t conclude that x and 1 are not observationally equivalent just because let x = 3 in 0 is a program but let 1 = 3 in 0 is not.
(4/5) ProfessorJ
Besides a Web server, DrScheme includes an interpreter for a Java-like language. Choose a language under “ProfessorJ” in Language | Choose Language.
(3/24) A tool to test whether your code is in tail form
There are three ways to test whether a program is in tail form. First, you can check if every call to a non-primitive function is a tail call. Second, you can try to translate the program into SLIM assembly language; in particular, translate each procedure call in Scheme into a jump in SLIM. Third, you can download and use an experimental tool I just wrote. This Scheme program defines a command tail-form, which you can wrap around another Scheme program. If the wrapped-around program is in tail form, the wrapper doesn’t do anything.
> (tail-form
    (define factorial
      (lambda (n)
        (compute n 1)))
    (define compute
      (lambda (n result)
        (if (zero? n)
          result
          (compute (- n 1) (* result n)))))
    (factorial 5))
120
In contrast, if the wrapped-around program is not in tail form—in other words, if the wrapped-around program makes a non-tail call to a non-primitive function—then the wrapper triggers a syntax error.
> (tail-form
    (define factorial
      (lambda (n)
        (if (zero? n)
          1
          (* n (factorial (- n 1))))))
    (factorial 5))
non-tail-call: bad syntax in: (non-tail-call (factorial (- n 1)))
The error message (on the last line) identifies the offending call. This tool is experimental—please let me know whether it is helpful and whether you encounter any surprises.
(3/21) Reading, printing, and searching a linked list in C
For those of you familiar with the C programming language, I translated print.slim to C. Studying this code can help you understand how to allocate and arrange memory for linked lists. I also translated find.slim to C with the GNU C Compiler’s computed-goto extension. Studying this code can help you understand how to allocate and arrange memory for procedures.
(3/19) Homework correction and clarification
In handout exercise 3 (both the Scheme part and the SLIM part), please assume that the list is not empty and the target does not occur at the beginning of the list. In this exercise, you should not allocate any new memory. More specifically, the Scheme code you write should not use cons, and the new SLIM code you write should not refer to the free register.
SLIM registers correspond to Scheme variables. SLIM labels correspond to Scheme procedures. To jump to a SLIM label and change the concents of SLIM registers corresponds to a procedure call in Scheme. The only SLIM action that corresponds to set!, set-car!, or set-cdr! in Scheme is changing the contents of memory locations. Thus, except for handout exercise 3, do not use set!, set-car!, or set-cdr! in Scheme.
(3/6) Reminders and hints for the 2/27 homework
Recall that EOPL exercise 3.14 says that the program
let x = 30
in let x = -(x,1)
       y = -(x,2)
   in -(x,y)
should evaluate to 1. Be sure to check that your solution to exercise 3.18 makes the corresponding program
let x = 30
in (proc (x,y) -(x,y)
    -(x,1)
    -(x,2))
also evaluate to 1.
EOPL exercise 3.18 is independent of the two (other) exercises described on the 2/27 handout. In other words, you don’t have to implement an interpreter for a programming language with both procedures that take an arbitrary number of arguments and a primitive procedure sub. In particular, the primitive procedure sub you put in the initial environment in the two handout exercises should take one argument at a time: the program ((sub 4) 3) should yield 1, whereas the program (sub 4 3) doesn’t have to yield any result.
Feel free to use arbno and separated-list in the-grammar, as documented in the 2/13 reading. Feel free also not to use these features—you don’t need them, and some students find it easier to figure out the abstract syntax returned by the generated parser if they don’t use these features.
Another hint on handout exercise 2: of the two variants you need to add to (define-datatype proc ...), one constructor takes no arguments and the other constructor takes one argument. Each argument you provide to the constructor of a data structure that represents a defunctionalized procedure corresponds to a free variable in that procedure. A free variable in an expression is a variable that is used, but not bound, in that expression.
(3/5) How to make your grammar LL(1)
If you get the following error message from SLLGEN as you work on EOPL exercise 3.14 or 3.18:
parser-generation: grammar not LL(1): shift conflict detected for class identifier in nonterminal
be sure that your abstract syntax tree for repetition is heavy on the right (that is, right-associative) rather than heavy on the left (that is, left-associative). This is a quirk of SLLGEN shared by all so-called look-ahead parser generators: they require the context-free grammar to be in a certain restricted form.
(2/23) Typos in EOPL Sections 3.2–5
In the inference rules at the top of page 63 and in the middle of page 70, two occurrences of v should be val. Near the bottom of page 76 and the top of page 77, the procedure procedure is described as taking a “vble” as its first argument, but it is not explained that “vble” means variable name (i.e., a symbol).
We haven’t covered LETREC (recursive procedures) in class, but there is an important typo in EOPL Section 3.5: in the numbered list on page 80, the variable x is bound twice! Professor Wand has issued a fix. (This problem is of the same kind as the mistake Aaron Borden caught me making in lecture today.)
(2/22) Interpreters updated
I changed the files with the code for the interpreters to better match what we did in lecture. The LET interpreter (which adds variables and “let”) is in let.scm; the PROC interpreter (which adds procedures but not recursive procedures) is in proc.scm. The essence of these interpreters has not changed. Compare these two interpreters to see exactly what it takes to add procedures to a language! In your feb15 homework, please feel free to start from any version of let.scm posted so far.
(2/20) EOPL exercise 2.27
It’s unclear which concrete syntax on page 49 you should use for unparse-lc-exp. Take your pick: the expression
(unparse-lc-exp (lambda-exp 'hi (var-exp 'there)))
should return either the string “proc hi => there” or the string “(lambda (hi) there)”. But do you see why the first grammar has an ambiguity problem?
(2/12) How to see data built using define-datatype
It is useful to see the data you are working on. Unfortunately, DrScheme by default hides data you define with define-datatype. For example, a diff-exp from class on 2/8 is printed as
#<struct:diff-exp>
no matter what its subexpressions are. To make your data visible, say
(require (only mzscheme print-struct))
(print-struct #t)
Now you can see inside a diff-exp without writing a function to convert it to a string:
#(struct:diff-exp #(struct:const-exp 4) #(struct:const-exp 3))
(2/1) Homework clarifications
In TLL ex 5.4, (occurN markers lat) should be the number of elements of lat that occur in markers, so duplicates in markers don’t matter but duplicates in lat do matter. For example, if lat1 is (basil) and lat2 is (basil basil), then (occurN lat1 lat2) is 2 and (occurN lat2 lat1) is 1.
In TLL ex 4.7, it is the job of whoever invokes dot-product, not you who implements dot-product, to make sure that vec1 and vec2 have the same length. The dot-product of two empty lists (that is, two zero-dimensional vectors) is zero, which should be convenient for your coding.
(1/17) How to submit homework
Please submit each homework assignment assigned before 2/15 as a single plain-text file containing a sequence of S-expressions. The file should be named after the start date of the assignment, like jan18.scm or feb01.scm. Don’t bother with zip, tar, gz, pdf, ps, doc, or the like. Use comments (which start with a semicolon and extend to the end of the line) to label your responses and for exercises that ask for not just code (such as CA exercise 1.7). A skeleton file illustrates the format.

Communication

You are responsible for checking this Web page for announcements.

You are also responsible for participating in the class newsgroup. Point your newsreader (such as KNode, Pan, Thunderbird, or tin) at the newsgroup ru.nb.dcs.class.314.ccshan on the server news-lcsr.rutgers.edu. When the news server asks you to authenticate yourself, you may need to put “@remus”, “@paul”, or “@aramis” at the end of your username. The news server expires posts every night, and can be unresponsive for about 10 minutes around 10–11pm—please plan accordingly.

When sending email to course staff or posting to the newsgroup, use a descriptive subject. Put “314” in the subject of emails to course staff, so that the message stands out and is easy to categorize. See “The do’s and don’ts of posting on Google Groups” for more information on newsgroup etiquette.

Grading

Participation (10%)
Attend classes; ask questions; offer answers. By midnight before each (non-exam) class, finish the reading assigned at the previous class and post a message to the class newsgroup. Your post should thoughtfully and concretely initiate or continue a discussion about the readings, the lectures, the recitations, or the homework.
Homework (50%)
There will be a couple of homework assignments every week, containing short-response questions and programming problems. (If you are not a CS student, you can get a departmental account to do the programming assignments.) Each homework is due in one week by midnight unless otherwise indicated. Each day late discounts the grade by 10%. Please submit your work online.
Exams (40%)
There will be two midterms (10% each) and a final (20%).

You are encouraged to discuss homework with others, but everything you finally submit must be your own work, and you must acknowledge your collaborators in your submission. When in doubt, ask. Please refer to the department’s academic integrity policy. Cheating cheapens everyone’s degree.

You can see your grades and our comments online at the FAS Gradebook. Check out the grading criterion on the Web. On the course newsgroup, you can see the answer key and test programs we used for grading, but note:

Please email the teaching staff if you have any questions or concerns about the grading.

Readings

The following textbooks are required. The following textbooks are suggested.

If you need a reference for Scheme, try “Revised5 report on the algorithmic language Scheme” (aka R5RS, the de-facto standard), R. Kent Dybvig’s The Scheme Programming Language, or Dorai Sitaram’s “Teach yourself Scheme in fixnum days”.

Software

Scheme

Download and play with DrScheme. We use version 301 in class, but any recent version should work.

There are two ways to run DrScheme in the campus computer labs. You can download and install DrScheme in a location where you have write permission (such as the Z drive) every time you sit down at a computer. Or you can run DrScheme remotely from your remus account: start an X server such as Exceed, then use ssh to connect with port forwarding to spanky.rutgers.edu and run /ug/users/ccshan/local/spanky/bin/drscheme .

For graphics and quilting as described in Concrete Abstractions, install fungraph.ss as a DrScheme teachpack (Language | Add Teachpack) and load quilting.scm (File | Open). We’ll be using Concrete Abstractions with DrScheme in class, but the book can also be used with other Scheme implementations.

To work with code and exercises from EOPL, choose “Essentials of Programming Languages (2nd ed.)” under “Teaching Languages” in Language | Choose Language in DrScheme. See the EOPL 2nd ed. Web site for support code for other Scheme implementations.

Haskell

The learning page on the Haskell Web site links to numerous Haskell tutorials and four implementations, all of which are compatible with Queens.hs. For what it’s worth, I use GHC for class demonstrations.

Prolog

SWI-Prolog is a nice and free Prolog implementation. It is what I use in class demonstrations.

Schedule

It’s tentative! As noted above, readings (and your twice-a-week post to the class newsgroup) are due by midnight before the next class unless otherwise noted, and homework assignments are due by midnight in one week unless otherwise noted. Readings enclosed in parentheses are optional.

Date Lecture Reading Homework
1/18 What are programming languages? Why learn their principles? Using DrScheme to calculate with numbers and functions. [Handout] Paul Graham, “Beating the averages”; TLS 1–2; CA 1 The Little Lisper exercises 1.4–6, 1.8–10, 2.1, 2.5 (but (nonlat? l3) is true), 2.6, 2.10; CA exercises 1.1, 1.3, 1.7, 1.14 [Tests]
1/23 Functional programming focuses on values, such as pictures, lists, and atoms. Interpreters vs compilers. From Scheme predicates to top-down versus bottom-up inductive specifications. [Handout] TLS 3; EOPL 1.1 TLL ex 3.1–6, 3.9, 3.10 (just explain what rember has in common); EOPL ex 1.1–4
1/25 Specifying an inductive set of data: top-down, such as using a Scheme predicate; bottom-up, such as using rules of inference; with a context-free grammar; with a finite-state automaton. [Handout] [Code] TLS 4–5; EOPL 1.2 See handout
1/30 Three ingredients of a grammar: discrimination, aggregation, recursion. Constructors and observers. Follow the grammar! [Handout] TLS 6; EOPL 1.3–4 See handout
2/1 A simple interpreter: concrete and abstract syntax; data and procedural abstraction; representation and implementation independence. [Handout] [Code] TLS 7–8 (until page 137); EOPL 2.1–2 TLL ex 7.6–9; EOPL ex 2.7–10
2/6 Programming without mutation. [Spreadsheet] Debugging (recursive) programs: Think, then try. Trace assuming recursive calls work. EOPL 2.3–5; (CA 5–8)
2/8 Representing recursive data (such as environments and expressions) by hand, functions, and writing a data-type definition. [Handout] [Code] EOPL 3.1–3; (CA 10) EOPL ex 2.11, 2.14–15, 2.18–19, 2.22–24 (see handout)
2/13 Environments and let. Between procedural and data-structure representation: defunctionalization and refunctionalization. [Handout] [Code] EOPL 2nd edition Appendix A (see handout) EOPL ex 2.26–27 (see handout)
2/15 Between abstract and concrete syntax: parsing and scanning. A simple interpreter with variables. [Handout] [Interpreter let.scm] EOPL 3.4–5 See handout for what to do and how to submit
2/20 Lexical scope vs dynamic scope. Adding procedures to the simple interpreter. [Handout]
2/22 (cont’d) [Interpreter proc.scm]
2/27 Midterm CA 11.1–3 [Handout]
3/1 Adding recursive procedures to the simple interpreter. [Interpreter letrec.scm] SLIM: a machine with a processor and memory. [Handout] CA 11.4
3/6 Finite-state programs in Scheme and SLIM. Traversing and creating data structures in memory. [Handout] [SLIM emulator slim.scm] [even.scm] [even.slim] [print.slim] Ken Thompson, “Reflections on trusting trust” See handout for what to do and how to submit
3/8 Creating and changing data structures in memory. Side effects in Scheme. [Handout] (undistributed in class—sorry) [factorial.slim] [factorial.scm] [member.slim] [member.scm] [immoral.scm] [print.c] TLS 8
(spring break)
3/20 Creating procedures in memory. [Handout] [find-c.scm] [find.slim] [find.c] See handout for what to do and how to submit
3/22 Continuations and continuation-passing style. [Handout] [find.scm] [every.scm] [every.slim] See handout for what to do and how to submit
3/27 Adding state to the simple interpreter. [Handout] [Interpreter explicit-refs.scm] (EOPL 4.1–3) See handout for what to do and how to submit (due 4/5)
3/28 Recitation: Continuation-passing style
3/29 Explicit and implicit mutable references. [Handout] (EOPL 4.5)
4/3 Parameter-passing variations: call by value vs by reference, name, and need. Reasoning about program equivalence. [Handout] [Interpreter implicit-refs.scm] [Interpreter call-by-reference.scm] Tim Berners-Lee et al., “The rule of least power”
4/4 Recitation: Observational equivalence
4/5 [Handout] Web application by program transformation. [bank.scm] [bank-web.scm] Functional programming in Java. [function.java] [environment.java] [environment-visitor.java] Joshua Bloch, Effective Java, items 5, 13, 20–22
4/10 Midterm [Handout] (due 4/19)
4/11 Recitation: State-passing transformation
4/12 Type checking. [Handout] [Interpreter checked.scm] EOPL 5.1–3
4/17 Type inference. [Handout] [Interpreter inferred.scm] [Haskell demo code] EOPL 5.4 See handout (due 4/26)
4/19 Guest lecture by Markus Mottl from Jane Street Capital: [Slides in Active-DVI format]; [binary-tree example tree.ml]; [machine-learning example titanic.names titanic.data using AIFAD and CFG libraries]
4/24 Laziness and streams. [Handout] [Interpreter call-by-name.scm] [Interpreter call-by-need.scm] [Queens.hs in Haskell] [queens.cbn in CALL-BY-NEED] See handout (due 5/3)
4/26 Logic programming. [queens.scm in Scheme] [Prolog demo code] [queens.pl in Prolog] (Patrick Blackburn, Johan Bos, and Kristina Striegnitz, Learn Prolog Now!)
5/1 The unusual effectiveness of logic in computer science. [Slides] (Philip Wadler, “Proofs are programs”)
$LastChangedDate: 2006-05-01 23:36:21 -0400 (Mon, 01 May 2006) $