(Advanced) topics in Programming Languages

Similar documents
Seminar in Programming Languages

Programming Languages Fall 2014

CIS 500 Software Foundations Fall October 2

Costly software bugs that could have been averted with type checking

Pierce Ch. 3, 8, 11, 15. Type Systems

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Types and Type Inference

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Types and Type Inference

Formal Systems and their Applications

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Simply-Typed Lambda Calculus

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

More Lambda Calculus and Intro to Type Systems

Lesson 4 Typed Arithmetic Typed Lambda Calculus

More Lambda Calculus and Intro to Type Systems

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Programming Languages

Types, Type Inference and Unification

CS558 Programming Languages

COS 320. Compiling Techniques

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

More Lambda Calculus and Intro to Type Systems

Com S 541. Programming Languages I

CIS 500 Software Foundations Midterm I

Concepts of Programming Languages

Lambda Calculus. Concepts in Programming Languages Recitation 6:

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Programming Languages Third Edition

Recursive Types and Subtyping

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Formal Semantics of Programming Languages

CSCE 314 Programming Languages. Type System

Programming Languages 2nd edition Tucker and Noonan"

Fundamental Concepts. Chapter 1

CS 242. Fundamentals. Reading: See last slide

Lambda Calculi With Polymorphism

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Formal Semantics of Programming Languages

CSCI-GA Scripting Languages

Programming Languages Fall 2013

MIDTERM EXAMINATION - CS130 - Spring 2005

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Lambda Calculus.

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CS558 Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

Programming Language Concepts

CMSC 330: Organization of Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Untyped Lambda Calculus

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Programmiersprachen (Programming Languages)

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Type Systems, Type Inference, and Polymorphism

Lecture 9: Typed Lambda Calculus

Programming Languages Lecture 14: Sum, Product, Recursive Types

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Some instance messages and methods

Concepts of programming languages

Programming with Math and Logic

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Types and Programming Languages. Lecture 5. Extensions of simple types

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Principles of Program Analysis. Lecture 1 Harry Xu Spring 2013

Last class. CS Principles of Programming Languages. Introduction. Outline

What Is Computer Science? The Scientific Study of Computation. Expressing or Describing

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Weeks 6&7: Procedures and Parameter Passing

CSE 505 Graduate PL. Fall 2013

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Programming Languages Lecture 15: Recursive Types & Subtyping

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

CS 314 Principles of Programming Languages

Program Analysis: Lecture 02 Page 1 of 32

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CMSC 330: Organization of Programming Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Functional Languages and Higher-Order Functions

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lambda Calculi With Polymorphism

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Programming Languages

CS 3360 Design and Implementation of Programming Languages. Exam 1

Concepts of program design Exam January 31, 13:30 16:30

Transcription:

Spring 2012 (Advanced) topics in Programming Languages Instructor: Mooly Sagiv TA: Shachar Itzhaky http://www.cs.tau.ac.il/~msagiv/courses/apl12.html Inspired by John Mitchell CS 242

Compilation course Prerequisites

Course Grade 20% Class notes 30% Assignments 50% Home or (easier) class exam

Class Notes Prepared by two students First draft completed within one week Consumes a lot of time Use LaTeX template provided in the homepage Read supplementary material Correct course notes Bonus for interesting corrections Add many more examples and elaborations

Course Themes Programming Language Concepts A language is a conceptual universe (Perlis) Framework for problem-solving Useful concepts and programming methods Understand the languages you use, by comparison Appreciate history, diversity of ideas in programming Be prepared for new programming methods, paradigms, tools Critical thought Identify properties of language, not syntax or sales pitch Language and implementation Every convenience has its cost Recognize the cost of presenting an abstract view of machine Understand trade-offs in programming language design

Language goals and trade-offs Architect Programmer Testing Programming Language Diagnostic Tools Compiler, Runtime environment

Instructor s Background First programming language Pascal Soon switched to C (unix) Efficient low level programming was the key Small programs did amazing things Led an industrial project was written in common lisp Semi-automatically port low level OS code between 16 and 32 bit architectures The programming setting has dramatically changed: Object oriented Garbage collection Huge programs Performance depends on many issues Productivity is sometimes more importance than performance Software reuse is a key

Other Lessons Learned Futuristic ideas may be useful problem-solving methods now, and may be part of languages you use in the future Examples Recursion Object orientation Garbage collection High level concurrency support Higher order functions Pattern matching

More examples of practical use of futuristic ideas Function passing: pass functions in C by building your own closures, as in STL function objects Blocks are a nonstandard extension added by Apple to C that uses a lambda expression like syntax to create closures Continuations: used in web languages for workflow processing Monads: programming technique from functional programming Concurrency: atomicity instead of locking Decorators in Python to dynamically change the behavior of a function

What s new in programming languages Commercial trend over past 5+ years Increasing use of type-safe languages: Java, C#, Scripting languages, other languages for web applications Teaching trends Java replaced C as most common intro language Less emphasis on how data, control represented in machine Research and development trends Modularity Java, C++: standardization of new module features Program analysis Automated error detection, programming env, compilation Isolation and security Sandboxing, language-based security, Web 2.0 Increasing client-side functionality, mashup isolation problems

What s worth studying? Dominant languages and paradigms Leading languages for general systems programming Explosion of programming technologies for the web Important implementation ideas Performance challenges Concurrency Design tradeoffs Concepts that research community is exploring for new programming languages and tools Formal methods in practice Grammars Semantics Domain theory

Suggested Reading J. Mitchell. Concepts in Programming Languages B. Pierce. Types and Programming Languages J. Mitchell. Foundations for Programming Languages C. A. R. Hoare. An axiomatic basis for computer programming. Communications of the ACM, 12(10):576-580 and 583, October 1969 Peter J. Landin. The next 700 programming languages

Related Courses Compilers Programming languages Semantics of programming languages Program analysis

Tentative Schedule 6/3 introduction 13/3 javascript 20/3 Haskel 27/3 No class 3/4 Exception and continuation 17/4, 24/4, 2/5 Type Systems 8/5 Dependent types 15/5 IO Monads 22/5, 29/5, 5/6 Concurrency 12/6, 19/6 Domain Specific Languages 22/6 Summary class

Type Checking Benjamin Pierce. Types and Programming Languages

August 2005 As a Malaysia Airlines jetliner cruised from Perth, Australia, to Kuala Lumpur, Malaysia, one evening last August, it suddenly took on a mind of its own and zoomed 3,000 feet upward. The captain disconnected the autopilot and pointed the Boeing 777's nose down to avoid stalling, but was jerked into a steep dive. He throttled back sharply on both engines, trying to slow the plane. Instead, the jet raced into another climb. The crew eventually regained control and manually flew their 177 passengers safely back to Australia. Investigators quickly discovered the reason for the plane's roller-coaster ride 38,000 feet above the Indian Ocean. A defective software program had provided incorrect data about the aircraft's speed and acceleration, confusing flight computers. August 2005

Error Detection Early error detection Logical errors Interface errors Dimension analysis Effectiveness also depends on the programmer Can be used for code maintenance

Type Systems A tractable syntactic method for proving absence of certain program behaviors by classifying phrases according to the kinds they compute Examples Whenever f is called, its argument must be integer The arguments of f are not aliased The types of dimensions must match

What is a type A denotation of set of values Int Bool A set of legal operations

Static Type Checking Performed at compile-time Conservative (sound but incomplete) if <complex test> then 5 else <type error> Usually limited to simple properties Prevents runtime errors Enforce modularity Protects user-defined abstractions Allows tractable analysis But worst case complexity can be high Properties beyond scope (usually) Array out of bound Division by zero Non null reference

Abstraction Types define interface between different software components Enforces disciplined programming Ease software integration Other abstractions exist

Documentation Types are useful for reading programs Can be used by language tools

Language Safety A safe programming language protects its own abstraction Can be achieved by type safety Type safety for Java was formally proven

Statically vs. Dynamically Checked Languages Statically Checked Dynamically Checked Safe ML, Haskel, Java, C# Lisp, Scheme, Perl, Python Unsafe C, C++

Efficiency Compilers can use types to optimize computations Pointer scope Region inference

Language Design Design the programming language with the type system But types incur some notational overhead Implicit vs. explicit types The annotation overhead Designing libraries is challenging Generics/Polymorphism help

Untyped Arithmetic Expressions Chapter 3

Untyped Arithmetic Expressions t ::= terms true constant true false constant false if t then t else t conditional 0 constant zero succ t successor pred t predecessor iszero t zero test if false then 0 else 1 1 iszero (pred (succ 0)) true

Untyped Arithmetic Expressions t ::= terms true constant true false constant false if t then t else t conditional 0 constant zero succ t successor pred t predecessor iszero t zero test succ true if 0 then 0 else 0 type error type error

Structural Operational Semantics (SOS) The mathematical meaning of programs A high level definition of interpreter Allow inductively proving program properties A binary relation on terms t t One step of executing t may yield the value t Inductive definitions of Axioms Inference rules The meaning of a program is a set of trees The actual interpreter can be automatically derived

SOS rules for Untyped Arithmetic Expressions if true then t 1 else t 2 t 1 (E-IFTRUE) if false then t 1 else t 2 t 2 (E-IFFALSE) t 1 t 1 (E-IF) if t 1 then t 2 else t 3 if t 1 then t 2 else t 3 t 1 t 1 succ t 1 succ t 1 t 1 t 1 pred t 1 pred t 1 pred 0 0 (E-PREDZERO) pred (succ t) t (E-PREDSUCC) (E-SUCC) (E-PRED) t 1 t 1 iszero t 1 iszero t 1 (E-ISZERO) iszero 0 true (E-ISZEROZERO) iszero succ t false (E-ISZERONZERO)

Examples if false then 0 else 1 iszero (pred (succ 0)) succ true if 0 then 0 else 0 if iszero (succ true) then 0 else 1

Properties of the semantics Determinism t 1 t 2 t 1 t 3 t 2 = t 3 Reflexive transitive closure t * t if either t = t or there exists t 0, t 1,, t n such that t=t 0, t = t n and for every 0 i < n: t i t i+1 Semantic meaning : Terms Nat Bool t = t if t Nat Bool t * t

Typed Arithmetic Expressions Chapter 8

Well Typed Programs A set of type rules conservatively define well typed programs The typing relation is the smallest binary relation between terms and types in terms of inclusion A term t is typable (well typed) if there exists some type T such that t : T The type checking problem is to determine for a given term t and type T if t : T The type inference problem is to infer for a given term t a type T such that t : T

Type Safety Stuck terms: Undefined Semantics t : t t The goal of the type system is to ensure at compiletime that no stuck ever occurs at runtime Type Safety (soundness) Progress: A well-typed term t never gets stuck Either it has value or there exists t such that t t Preservation: (subject reduction) If well type term takes a step in evaluation, then the resulting term is also well typed

Typed Arithmetic Expressions t ::= true false if t then t else t terms constant true constant false conditional 0 constant zero v ::= true false nv succ t pred t iszero t values true value false value numeric value successor predecessor zero test nv ::= numeric values 0 zero value succ nv successor value

Type Rules for Booleans T ::= Bool types type of Boolean t : T true : Bool (T-TRUE) false : Bool (T-FALSE) t 1 : Bool t 2 : T t 3 : T (T-IF) if t 1 then t 2 else t 3 : T

Type Rules for Numbers T ::= Nat types type of Natural numbers t : T 0 : Nat (T-ZERO) t 1 : Nat succ(t 1 ) : Nat T-SUCC t 1 : Nat pred(t 1 ) : Nat T-PRED t 1 : Nat iszero(t 1 ) : Bool T-ISZERO

Type Rules for Arithmetic Expressions true : Bool (T-TRUE) false : Bool (T-FALSE) t 1 : Bool t 2 : T t 3 : T (T-IF) if t 1 then t 2 else t 3 : T 0 : Nat (T-ZERO) t 1 : Nat succ(t 1 ) : Nat T-SUCC t 1 : Nat pred(t 1 ) : Nat T-PRED t 1 : Nat iszero(t 1 ) : Bool T-ISZERO

Examples if false then 0 else 1 if iszero 0 then 0 else 1 iszero (pred (succ 0)) succ true if 0 then 0 else 0 if iszero (succ true) then 0 else 1

LEMMA: Inversion of the typing relation true : R R = Bool false : R R = Bool if t 1 then t 2 else t 3 : R t 1 : Bool, t 2 : R, t 3 : R 0 : R R = Nat succ t 1 : R R = Nat and t 1 : Nat pred t 1 : R R = Nat and t 1 : Nat iszero t1 : R R = Bool and t 1 : Nat

Uniqueness of Types Each term t has at most one type If t is typable then its type is unique There is a unique type derivation tree for t Does not hold for general languages Need a partial order on types Unique most general type

Type Safety LEMMA 8.3.1: Canonical Forms: If v is a value of type Boolean then v =true or v=false If v is a value of type Nat then v belongs to nv nv ::= numeric values 0 zero value succ nv successor value Progress : If t is well typed then either t is a value or for some t : t t Preservation: if t : T and t t then t : T

Language Restrictions so far Simple expression language Fixed number of types No loops/recursion No variables/states No memory allocation

Extensions Untyped lambda calculus (Chapter 5) Simple Typed Lambda Calculus (Chapter 9) Subtyping (Chapters 15-19) Most general type Recursive Types (Chapters 20, 21) NatList = <Nil: Unit, cons: {Nat, NatList}> Polymorphism (Chapters 22-28) length:list int Append: list list Higher-order systems (Chapters 29-32)

Summary Type Systems Type systems provide a useful mechanism for conservatively enforcing certain safety properties Can be combined with runtime systems and static program analysis Interacts with the programmer A lot of interesting theory Another alternative is static program analysis Infer abstractions of values at every program point

Other Course Topics Dependent Types Monads Continuations Concurrency Domain specific languages