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

Similar documents
Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus.

Programming Languages Fall 2014

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

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Formal Systems and their Applications

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

Programming Languages

Subsumption. Principle of safe substitution

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

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

The Substitution Model

Untyped Lambda Calculus

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

Untyped Lambda Calculus

Lecture 9: Typed Lambda Calculus

The Substitution Model. Nate Foster Spring 2018

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Mutable References. Chapter 1

CS 314 Principles of Programming Languages

A Typed Lambda Calculus for Input Sanitation

CS558 Programming Languages

CSCI-GA Scripting Languages

Operational Semantics of Cool

Lecture 15 CIS 341: COMPILERS

(Advanced) topics in Programming Languages

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CIS 500 Software Foundations Fall October 2

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

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

Costly software bugs that could have been averted with type checking

CMSC330. Objects, Functional Programming, and lambda calculus

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

Concepts of programming languages

CMSC 330: Organization of Programming Languages

Functional Programming. Pure Functional Languages

The Untyped Lambda Calculus

Operational Semantics. One-Slide Summary. Lecture Outline

Denotational Semantics. Domain Theory

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

CSE 505 Graduate PL. Fall 2013

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

COS 320. Compiling Techniques

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Let Arguments Go First

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

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CSE 505: Concepts of Programming Languages

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Simply-Typed Lambda Calculus

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

Chapter 22: Type Reconstruction (Type Inference)

Type systems (An introduction)

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

Typed Lambda Calculus and Exception Handling

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Principles of Programming Languages

Principles of Programming Languages

CIS 500 Software Foundations Midterm I

Programming Languages Lecture 14: Sum, Product, Recursive Types

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Type Systems Winter Semester 2006

Functional Programming. Big Picture. Design of Programming Languages

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Type Checking and Type Inference

Arbitrary-rank polymorphism in (GHC) Haskell

Testing, Debugging, and Verification

Principles of Programming Languages

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

UNIT 3

Simon Peyton Jones Microsoft Research. August 2012

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

CMSC 330: Organization of Programming Languages. Operational Semantics

The Untyped Lambda Calculus

The Untyped Lambda Calculus

CIS 500 Software Foundations. Final Exam. May 3, Answer key

CS558 Programming Languages

Handout 10: Imperative programs and the Lambda Calculus

CSCE 314 Programming Languages. Type System

Chapter 5: The Untyped Lambda Calculus

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Chapter 1. Fundamentals of Higher Order Programming

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

Verifying Safety Property of Lustre Programs: Temporal Induction

Homework 3 COSE212, Fall 2018

CS558 Programming Languages

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Recursive Types and Subtyping

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

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

Transcription:

Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages

t ::= x x. t t t Call-by-value small step perational Semantics terms variable v ::= values abstraction x. t abstraction values application ( x. t 12 ) v 2 [x v 2 ] t 12 (E-AppAbs) t 1 t 1 (E-APPL1) t 1 t 2 t 1 t 2 t 2 t 2 (E-APPL2) v 1 t 2 v 1 t 2

t ::= x x. t t t Call-by-value big-step Operational Semantics terms variable v ::= values abstraction x. t abstraction values application other values x. t x. t (V-Value) t 1 x. t 3 t 2 v 1 [x v 1 ] t 3 v 2 t 1 t 2 v 2 (V-App)

A Pseudocode for call-by value interpreter Lambda eval(lambda t) { switch(t) { case t= x. t1: // A value return t case t = t1 t2: Lambda temp = eval(t1); assert temp = x. t3; Lambda v1 = eval(t2) ; // v1 must be a value return eval([x v1] t3) ; default: assert false; } }

Consistency of Function Application Prevent runtime errors during evaluation Reject inconsistent terms What does x x mean? Cannot be always enforced if <tricky computation> then true else ( x. x)

A Naïve Attempt Add function type Type rule x. t : x. x : If true then ( x. x) else ( x. y y) : Too Coarse

Simple Types T ::= Bool T T types type of Booleans type of functions T 1 T 2 T 3 = T 1 ( T 2 T 3 )

Explicit vs. Implicit Types How to define the type of abstractions? Explicit: defined by the programmer t ::= x x: T. t t t Type terms variable abstraction application Implicit: Inferred by analyzing the body The type checking problem: Determine if typed term is well typed The type inference problem: Determine if there exists a type for (an untyped) term which makes it well typed

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

Typing Function Declarations x : T 1 t 2 : T 2 ( x : T 1. t 2 ): T 1 T 2 (T-ABS) A typing context maps free variables into types, x : T 1 t 2 : T 2 ( x : T 1. t 2 ): T 1 T 2 (T-ABS)

Typing Free Variables x : T x : T (T-VAR)

Typing Function Applications t 1 : T 11 T 12 t 2 : T 11 (T-APP) t 1 t 2 : T 12

Typing Conditionals t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-IF) if true then ( x: Bool. x) else ( y: Bool. not y)

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

Type Rules t ::= x x: T. t terms variable abstraction t : T x : T x : T (T-VAR), x : T 1 t 2 : T 2 x : T 1. t 2 : T 1 T 2 (T-ABS) T::= T T types types of functions t 1 : T 11 T 12 t 1 t 2 : T 12 t 2 : T 11 (T-APP) ::= context empty context, x : T term variable binding

t ::= x x: T. t t t true false if t then t else t terms variable abstraction application constant true constant false conditional 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 2 : T 11 t 1 t 2 : T 12 (T-APP) T::= types Bool Boolean type T T types of functions ::= context empty context, x : T term variable binding 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)

Simple Example t : T ) x:bool. x ) true x:bool {x : Bool} (T-VAR) x : T x : T, x : T 1 t 2 : T 2 (T-VAR) x : Bool x : Bool (T-ABS) x : Bool. x : Bool : Bool Bool 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 2 : T 11 (T-APP) ) x:bool. x ) true: Bool true : Bool (T-TRUE) (T-APP) true : Bool (T-TRUE) false : Bool (T-FALSE)

Another Example t : T if true then ) x:bool. x) else ) x:bool. x) 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 2 : T 11 (T-APP) 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)

Another Example t : T if true then ) x:bool. x) else ( x:bool. y:bool. x) 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 2 : T 11 (T-APP) 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)

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 Interesting on closed terms (empty contexts)

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

Top-Down Type Checking Type compute_type(tenv:, Lambda t) { switch (t) { case x : return x; case x : T. t : return compute_type( [x T], t); case t1 t2: T = compute_type(, t1) ; assert T == T11 T12; T =compute_type(t2); assert T == T11; return T12

Type Safety Well typed programs cannot go wrong If t is well typed then either t is a value or there exists an evaluation step t t [Progress] If t is well typed and there exists an evaluation step t t then t is also well typed [Preservation]

Expressive Power How expressive is typed lambda calculus Can we code divergence? How about loops?

Summary Constructive rules for preventing runtime errors in a Turing complete programming language Efficient type checking Unique types Type safety But limits programming

Summary: Lambda Calculus Powerful The ultimate assembly language Useful to illustrate ideas But can be counterintuitive Usually extended with useful syntactic sugars Other calculi exist pi-calculus object calculus mobile ambients