Type Checking Textbook: Types and Programming Languages Benjamin Pierce.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Type checking © Marcelo d’Amorim 2010.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Foundations of Programming Languages: Introduction to Lambda Calculus
Type-Safe Programming in C George Necula EECS Department University of California, Berkeley.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS5205: Foundation in Programming Languages Lecture 1 : Overview
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
(Advanced) topics in Programming Languages Instructor: Mooly Sagiv TA: Shachar Itzhaky Spring 2012 Inspired.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Subtyping Chapter 15 Benjamin Pierce Types and Programming Languages.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Software Testing and Quality Assurance Motivation and Review of Software Verification & Validation 1.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Explicit Polymorphism.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Announcements Exam 2 graded Rainbow grades: HW1-5, Exam 1-2, Quiz 1-6 Part 1: Problem 1, Problem 2, will go over 2 Part 2: Problem 1, Problem 2 Part.
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Lambda Calculus CSE 340 – Principles of Programming Languages
Types for Programs and Proofs
More on Lambda Calculus
Ik-Soon Kim December 18, 2010 Embedded Software Platform Team
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
Presentation transcript:

Type Checking Textbook: Types and Programming Languages Benjamin Pierce

Plan Motivation (Chapter 1) Untyped Arithmetic Expressions (Chapter 3) Typed Arithmetic Expressions (Chapter 8) Untyped Lambda Calculus (Chapter 5) Typed Lambda Calculus (Chapter 9) Extensions

Challenges of Program Verification Specification of software behavior Reasoning about logical formulas The annotation burden The dynamic nature of software Automatically infer safe invariants from the code Concentrate on very simple properties with lightweight annotations

June 4, 1996 The European Ariane5 rocket explodes 40s into its maiden flight due to a software bug.

1997 Mars Rover Loses Contact 1999 Mars Climate Orbiter is Lost 1999 Mars polar Lander is lost 2004 Mar rover freezes

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

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 then 5 else Usually limited to simple properties –Prevents runtime errors –Enforce modularity –Protects user-defined abstractions –Allows tractable analysis Properties beyond scope (usually) –Array out of bound –Division by zero –Non null reference

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

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

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

Statically vs. Dynamically Checked Languages Statically Checked Dynamically Checked SafeML, Haskel, Java Lisp, Scheme, Perl UnsafeC, C++

Eiffel, 1989 Cook, W.R. (1989) - A Proposal for Making Eiffel Type-Safe, in Proceedings of ECOOP'89. S. Cook (ed.), pp Cambridge University Press. Betrand Meyer, on unsoundness of Eiffel: “Eiffel users universally report that they almost never run into such problems in real software development.”

Ten years later: Java

Efficiency Compilers can used types to optimize computations Pointer scope (Titanium) 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

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 izzero t zero test if false then 0 else 11 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 izzero t zero test succ truetype error if 0 then 0 else 0type error

SOS for Booleans t ::= terms true constant true false constant false if t then t else t conditional v ::= values true false 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 if t 1 then t 1 else t 2  if t’ 1 then t 1 else t 2 (E-IF) t 1  t’ 1

SOS for Numbers t ::= terms 0 constant zero succ t successor pred t predecessor iszero t zero test v ::= values true true value false false value nv numeric values nv ::= numeric values 0 zero value succ nv successor value t 1  t’ 1 succ t 1  succ t’ 1 (E-SUCC) pred 0  0 (E-PREDZERO) pred (succ nv 1 )  nv 1 (E-PREDSUCC) iszero 0  true (E-ISZEROZERO) iszero (succ nv 1 )  false (E-ISZEROSUCC) t 1  t’ 1 pred t 1  pred t’ 1 (E-PRED) t 1  t’ 1 iszero t 1  iszero t’ 1 (E-ISZERO)

Typed Arithmetic Expressions Chapter 8

Stuck Computations The goal of the type system is to ensure at compile-time that no stuck ever occurs at runtime 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

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

Type Rules for Numbers T ::= types Nat 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 ISZERO

The Typing Relation Formally 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

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

Safety 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 Progress: If t is well defined then either t is a value or for some t’: t  t’ Preservation: if t : T and t  t’ then t’ : T nv ::= numeric values 0 zero value succ nv successor value

Untyped Lambda Calculus Chapter 5

Untyped Lambda Calculus t ::= terms x variable  x. t abstraction t t application  ( x. x) ( x. x) ( x. x) Syntactic Conventions Applications associates to left The body of abstraction extends as far as possible

Scope An occurrence of x is free in a term t if it is not in the body on an abstraction x. t –otherwise it is bound – x is a binder FV: t  P(Var) is the set free variables of t –FV(x) = {x} –FV(  x. t) = FV(t) – {x} –FV (t 1 t 2 ) = FV(t 1 )  FV(t 2 ) Terms w/o free variables are combinators –Example: x. x

Operational Semantics  ( x. t 12 ) t 2  [x  t 2 ] t 12 (  -reduction) [x  s]x =s [x  s]y=y if y  x [x  s ] ( y. t 1 ) = y. [x  s ] t 1 if y  x and y  FV(s) [x  s ] (t 1 t 2 ) = ([x  s ] t 1 ) ([x  s ] t 2 ) ( x. x) y  y ( x. x ( x. x) ) (u r)  u r ( x. x) ( x ( y. x y)) (y z)  ( x ( w. x w)) (y z)  w. y z w

Operational Semantics  ( x. t 12 ) t 2  [x  t 2 ] t 12 (  -reduction) [x  s]x =s [x  s]y=y if y  x [x  s ] ( y. t 1 ) = y. [x  s ] t 1 if y  x and y  FV(s) [x  s ] (t 1 t 2 ) = ([x  s ] t 1 ) ([x  s ] t 2 ) Evaluation orders: Full beta reduction Normal order Call by name Call by value ) x. x)(( x. x)( z. ( x. x) z))

Programming in the Lambda Calculus Turing complete Multiple arguments – (x, y). s = x. y.s Church Booleans –tru = t. f. t –fls = t. f. f –test = l. m. n. l m n –and = b. c. b c fls Pairs –pair = f. b. s. b f s –fst = p. p tru –snd = p. p fls Church Numerals –c 0 = f. z. z –c 1 = f. z. s z –c 2 = f. z. s (s z) –c 3 = f. z. s (s (s z)) Divergence –omega= ( x. x x) ( x. x x)

Summary Lambda Calculus Powerful Useful to illustrate ideas But can be counterintuitive Usually extended with useful syntactic sugars

Simple Typed Lambda Calculus Chapter 9

Simple Typed Lambda Calculus t ::= terms x variable  x: T. t abstraction t t application T::= types  T  T types of functions

SOS for Simple Typed Lambda Calculus t ::= terms x variable  x: T. t abstraction t t application v::= values  x: T. t abstraction values T::= types  T  T types of functions t 1  t 2 t 1  t’ 1 t 1 t 2  t’ 1 t 2 (E-APP1) t 2  t’ 2 t 1 t 2  t 1 t’ 2 (E-APP2)  ( x: T 11. t 12 ) t 2  [x  t 2 ] t 12 (E-APPABS)

t ::= terms x variable  x: T. t abstraction T::= types  T  T types of functions  ::= context   empty context , x : T term variable binding   t : T x : T    x : T (T-VAR) , x : T 1  t 2 : T 2   x : T 1. t 2 : T 2 : T 1  T 2 (T-ABS)   t 1 : T 11  T 12   t 1 t 2 : T 12 (T-APP)   t 2 : T 11 t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T Type Rules

t ::= terms x variable  x: T. t abstraction t t application true constant true false constant false if t then t else t conditional T::= types Bool Boolean type  T  T types of functions  ::= context   empty context , x : T term variable binding   t : T x : T    x : T (T-VAR) , x : T 1  t 2 : T 2   x : T 1. t 2 : T 2 : T 1  T 2 (T-ABS)   t 1 : T 11  T 12   t 1 t 2 : T 12 (T-APP)   t 2 : T 11 t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T   true : Bool (T-TRUE)   false : Bool (T-FALSE)   t 1 : Bool t 2 : T t 3 : T   if t 1 then t 2 else t 3 : T (T-IF)

Examples ) x:Bool. x ) true if true then ) x:Bool. x) else ) x:Bool. x) if true then ) x:Bool. x) else ) x:Bool. y:Bool. x)

The Typing Relation Formally the typing relation is the smallest ternary relation on contexts, terms and types –in terms of inclusion A term t is typable in a given context  (well typed) if there exists some type T such that   t : T

Inversion of the typing relation   x : R  x: R    x : T 1. t2 : R  R = T 1  R 2 for some R 2 with   t 2 : R 2   t 1 t 2 : R  there exists T 11 such that   t 1 : T 11  R and   t 2 : T 11   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

Uniqueness of Types Each term t has at most one type in any given context –If t is typable then its type is unique There is a unique type derivation tree for t

Safety Canonical Forms: –If v is a value of type Boolean then v =true or v=false –If v is a value of type T 1  T 2 then v = x. T 1. t 2 Progress: If t is closed and well defined (  t : T for some T) then either t is a value of for some t’: t  t’ Permutation: if   t : T and  is a permutation of  then   t : T Weakening: if   t : T and x  dom(  ) then , x : S  t : T Preservation of types under substitution: –If , x : S  t : T and   s : S then   [x  s] t : T Preservation: if   t : T and t  t’ then   t’ : T

Implicit vs. Explicit Types Do we have to spell out the type of every argument? Implicit type systems allow the programmers to omit the types as long as the resulting type is unique –Reduces the annotation burden A type inference algorithm infers a unique type or issues an error

Other Issues The Curry-Howard Correspondence Erasure of typability Curry-Stlyle vs. Church style of languages definition

Extensions Subtyping (Chapters 15-19) –Most general type Recursive Types (Chapters 20, 21) –NatList = Polymorphism (Chapters 22-28) – length:list   int –Append: list     list  Higher-order systems (Chapters 29-32)

Summary Type systems provide a useful for enforcing certain safety properties –Can be combined with runtime systems and program analysis Interacts with the programmer A lot of interesting theory