Docsity

Homework - Programming Languages - Spring 2009 | CS 56500, Study notes of Programming Languages

Partial preview of the text

Download Homework - Programming Languages - Spring 2009 | CS 56500 and more Study notes Programming Languages in PDF only on Docsity!

CS 565:

Programming Languages

Spring 2009

M,W,F 1:30 -2:

Lawson 1106

Administrivia

 Who am I?

 Course Web Page:

 (^) http://www.cs.purdue.edu/homes/suresh/565-Spring2009/

 Office Hours:

 Tu, Th: 2 -- 3 PM

 By appointment

 Main text:

 Types and Programming Languages,

B. Pierce, MIT Press

Prerequistes

 (^) Programming experience/maturity

 exposure to various language constructs
 Java, ML, Lisp, Prolog, C
 Undergraduate compilers and/or PL class
 CS 352 and/or CS 456 or equivalent

 (^) Mathematical maturity

 familiarity with first-order logic, set theory, graph
theory, induction

 (^) Most important:

 Intellectual curiosity and creativity

Resources

 (^) Web page for text:  (^) http://www.cis.upenn.edu/~bcpierce/tapl  (^) Supplementary material:  (^) http://www2.imm.dtu.dk/~riis/PPA/ppasup2004.html  (^) Web page for ML implementations  (^) http://caml.inria.fr (Caml)  (^) http://www.smlnj.org (SML/NJ)  (^) http://www.mlton.org (MLton)  (^) Proceedings of conferences  (^) POPL, PLDI, ICFP, ...

Motivation (cont)

 (^) Prove specific facts about programs

 Verify correctness

 (^) Important in mission-critical systems

 Safety or isolation properties

 Need an unambiguous vocabulary

 (^) Understand specific language features

 Better language design

 Guide improvements in implementations

Goals

 A more sophisticated appreciation of programs,

their structure, and the field as a whole

 Viewing programs as rich, formal, mathematical

objects, not mere syntax

 Define and prove rigorous claims about a program’s

meaning and behavior

 Develop sound intuitions to better judge language

properties

 Develop tools to be better programmers,

software designers and computer scientists

Topics

 (^) Part I (Foundations): Semantic formalisms, λ- calculus, abstract intepretation, introduction to types  (^) Part II (Design): Simply-typed λ-calculus, records, references, subtyping, object-based programming  (^) Part III (Features): Polymorphism, abstract data types, advanced topics (e.g., concurrency, linearity, ...)  (^) Part IV (Tools): Mechanized meta-theory and proof assistants

Semantics

 Three classical approaches:

 Operational

 (^) Define programs in terms of rules that apply to a specific virtual machine  (^) Useful for implementing a compiler or interpreter

 Denotational

 (^) Meaning in terms of functions from syntax (program text) to domains (values)  (^) Useful for describing the behavior of programs

 Axiomatic

 (^) Logical rules for reasoning about programs  (^) Useful for proving program correctness

Case studies

 (^) Lisp 1.  (^) Based on λ-calculus  (^) Key aspect of the calculus is notion of substitution of free variables:  (^) function f (args) = .... x ....  (^) Suppose x is not included in args. Where should the binding for x be constructed?  (^) At the point where f is defined (lexical scoping)  (^) At the points where f is applied (dynamic scoping)  (^) Lisp 1.5 (and later dialects) chose dynamic scoping, even though it is widely agreed today that lexical scoping is the more sensible choice.  (^) When do these distinctions arise? Why are the differences important?  (^) Lack of formal semantics to explore the ramifications of design choice

Case studies

 ML

 Interaction of types with references
 Polymorphism: code that works uniformly on all various
types of data
 length: α list -> int
 hd: α list -> α
 tl: α list -> α list
 Type inference
 Assign the most general type to variables based on the
contexts in which they occur

Case studies: Java Array Types

(^) One of Java’s design mistakes is the subtyping rule for arrays ‣ (^) Given Object types T and T' , the array subtyping rule is

T <: T' ⇒ T[] <: T'[]
if T is a subtype of T' then T[] is a subtype of T'[]
‣ Example: String <: Object thus
String[] <: Object[]

17

Case Studies: Java Array Types

 (^) Static typing guarantees that if we have declaration T v; then the following holds at all times v instanceof T  (^) This is good because it guarantees that type errors do not occur at run-time.  (^) Arrays break static type safety

19

Case Studies: Java Array Types

Object[] appThreads = new Thread[10]; //OK Thread[] <: Object[] appThreads[0] = "badaboom"; //OK String <: Object runFirstThread((Thread[]) appThreads); //appThreads instanceof Thread[] void runFirstThread( Thread[] o ) { o[0].run(); //OK o[0] is declared to be a Thread The problem is the subtyping rule

20

Case Studies: Java Array Types

Why have that rule? To write sort(Object[]) once rather than have to rewrite it for each new class!

 but subtype polymorphism is a poor substitute for
parametric polymorphism

The right solution is genericity…

sort(<A implements Comparable>)