Polymorphic Jam: Type Checking Rules and Examples, Exams of Programming Languages

The type checking rules for polymorphic jam, a functional language, along with examples of untyped jam expressions that are not typable in polymorphic jam or typed jam, and an extension of polymorphic jam. It also includes problems for students to practice type checking and cps conversion.

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-zus
koofers-user-zus 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Comp 311: Sample Exam II
November 22, 2005
Name:
Id #:
Instructions
1. The examination is closed book. The type checking rules for (Implic-
itly) Polymorphic Jam are given on the first three pages of the exam
as a reference.
2. Fill in the information above and the pledge below.
3. There are 7 problems on the exam worth a total of 110 points.
4. You have four hours to complete the exam. You must take the exam
during a continuous four hour block plus an optional 10 minute break.
Do not discuss the contents of the exam with anyone other than the
instructor and teaching assistants between now and the due date for
the exam.
Pledge:
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Polymorphic Jam: Type Checking Rules and Examples and more Exams Programming Languages in PDF only on Docsity!

Comp 311: Sample Exam II

November 22, 2005

Name:

Id #:

Instructions

  1. The examination is closed book. The type checking rules for (Implic- itly) Polymorphic Jam are given on the first three pages of the exam as a reference.
  2. Fill in the information above and the pledge below.
  3. There are 7 problems on the exam worth a total of 110 points.
  4. You have four hours to complete the exam. You must take the exam during a continuous four hour block plus an optional 10 minute break. Do not discuss the contents of the exam with anyone other than the instructor and teaching assistants between now and the due date for the exam.

Pledge:

Synopsis of Implicitly Polymorphic Jam

The syntax of (Implicitly) Polymorphic Jam is a restriction of the syntax of untyped Jam. Every legal Polymorphic Jam program is also a legal untyped Jam Program. But the converse is false, because there may not be a valid typing for a given untyped Jam program.

Abstract Syntax

The following grammar describes the abstract syntax of Polymorphic Jam. Each clause in the grammar corresponds directly to a node in the abstract syntax tree. The let construction has been limited to a single binding for the sake of notational simplicity. It is straightforward to generalize the rule to multiple bindings (with mutual recursion). Note that let is recursive.

M ::= M (M · · · M ) | P (M · · · M ) | if M then M else M | let x := M in M | V V ::= map x · · · x to M | x | n | true | false | null n ::= 1 | 2 |... P ::= cons | first | rest | null? | cons? | + | - | / | * | = | < | <= | <- | + | - | ~ | ref |! x ::= variable names

In the preceding grammar, unary and binary operators are treated ex- actly like primitive functions. Monomorphic types in the language are defined by τ , below. Polymor- phic types are defined by σ. The → corresponds to a function type, whose inputs are to the left of the arrow and whose output is to the right of the arrow.

σ ::= ∀α 1 · · · αn. τ τ ::= int | bool | unit | τ 1 × · · · × τn → τ | α | list τ | ref τ α ::= type variable names

Type Checking Rules

In the following rules, the notation Γ[x 1 : τ 1 ,... , xn : τn] means the Γ ∪ {x 1 : τ 1 ,... , xn : τn}.

Γ true : bool Γ false : bool Γ ` n : int

To open a polymorphic type

∀α 1 ,... , αn. τ,

substitute the chosen type terms τ 1 ,... , τn for the quantified type variables α 1 ,... , αn:

OPEN(∀α 1 ,... , αn. τ, τ 1 ,... , τn) = τ[α 1 :=τ 1 ,...,αn:=τn]

which creates a monomorphic type from a polymorphic type. For example,

OPEN(∀α. α → α, τ ) = τ → τ

Types of Primitives

The following table gives types for all of the primitive functions and op- erators and the polymorphic constant null. Programs are type checked starting with a primitive type environment consisting of this table.

null ∀α. list α cons ∀α. α × list α → list α first ∀α. list α → α rest ∀α. list α → list α cons? ∀α. list α → bool null? ∀α. list α → bool = ∀α. α × α → bool

  • int × int → int
  • int × int → int
  • int × int → int / int × int → int

< int × int → bool <= int × int → bool

(unary) - int → int (unary) + int → int (unary) ˜ bool → bool <- ∀α. ref α × α → unit ref ∀α. α → ref α ! ∀α. ref α → α

Typed Jam

The Typed Jam language used in Assignment 5 (absent the explicit type information embedded in program text) can be formalized as a subset of Polymorphic Jam. For the purposes of this test, Typed Jam is simply Poly- morphic Jam less the letpoly inference rule which prevents it from inferring polymorphic types for program-defined functions.

Problem 1. [15 points]

(i) [5 points] Give a simple example of an untyped Jam expression that is not typable in Polymorphic Jam, yet does not generate a run-time error when executed. Briefly but convincingly explain why.

(ii) [5 points] Give a simple example of an untyped Jam expression that is not typable in Typed Jam, but is typable in Polymorphic Jam. Briefly but convincingly explain why.

(iii) [5 points] Assume that we extend Polymorphic Jam by dropping the “value restriction” on the right hand side of bindings in letpoly rule. Give a simple example of a program that is typable in extended Polymorphic Jam but generates a run-time type error (misinterpreting one type of data as another) when it is executed.

(ii) [15 points] Is the same program

let foldr := map f,e,l to if null?(l) then e else f(first(l), foldr(f, e, rest(l))); in foldr(cons, null, cons(foldr(map x,y to x+y, 0, cons(1,null)), null))

typable in Polymorphic Jam? Justify your answer in same way as in part (i).

Problem 3. [25 points] Convert the following untyped Jam program to CPS. Use the identity function as your top level continuation and do not CPS either nested lets or applications of primitive operations (primitive functions or operators). Note that let is recursive.

let foldr := map f,e,l to if null?(l) then e else f(first(l), foldr(f, e, rest(l))); in foldr(map x,y to x+y, 0, cons(1,null))

Your CPS translation simply has to put all calls on program defined func- tions in tail position.

Problem 5. [10 points] Assume that the heap array (of 32-bit machine words) shown below contains two kinds of records: INT records and CONS records. An INT record represents a 32 bit integer i; it consists of a tag word containing the value 1 followed by a word containing the 32-bit integer i. A CONS record represents a pair of data values which are either references to INT records, references to CONS records, or the null reference. It consists of a tag word containing the value 2 followed by two words containing references. References to heap objects are simply their locations (offsets in words from the base) in the heap. The null reference is represented by the value -1. Given a root set consisting of location 26, circle the locations of all the live objects in the heap consisting of the following 34 words of memory.

Location Contents 0 2 1 3 2 - 3 2 4 9 5 6 6 2 7 13 8 3 9 1 10 1 11 1 12 2 13 2 14 9 15 - 16 1 17 1 18 2 19 6 20 13 21 1 22 25 23 2 24 6 25 0 26 2 27 32 28 18 29 2 30 0 31 23 32 1 33 50

Problem 7. [10 points] In problem 5, you marked the live nodes in the heap. Assume that you have recorded this information in a separate bit-map table (only 34 bits long for this tiny heap). Perform the the “sweep” step for a “mark-and-sweep” collector that does not move data and links the free blocks in a free-list where the first word in each free block is the size of the block in words minus 1 and the second word is the address of the next free block. The mininum size for a block in the free list is two words. Coalesce adjacent free blocks and use the dummy pointer value -1 to terminate the list. Set the variable free to point the first node in the free list. You do not have to show the bit-map table since it simply records the information given in your answer to problem 5. Note: if a node is freed and it is isolated (no free node is adjacent), then the header already contains the correct value for the free-list!

Location Old Contents New Contents (if changed) 0 2 1 3 2 - 3 2 4 9 5 6 6 2 7 13 8 3 9 1 10 1 11 1 12 2 13 2 14 9 15 - 16 1 17 1 18 2 19 6 20 13 21 1 22 25 23 2 24 6 25 0 26 2 27 32 28 18 29 2 30 0 31 23 32 1 33 50