More Lambda Calculus and Intro to Type Systems

Similar documents
More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems

Simply-Typed Lambda Calculus

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Simply-Typed Lambda Calculus

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

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

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

CIS 500 Software Foundations Fall September 25

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

Concepts of programming languages

Type Systems Winter Semester 2006

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

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

CSE 505: Concepts of Programming Languages

Lecture 5: The Untyped λ-calculus

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Com S 541. Programming Languages I

Programming Languages

CMSC 330: Organization of Programming Languages

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.

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

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

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

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

CIS 500 Software Foundations Midterm I

Chapter 5: The Untyped Lambda Calculus

Introduction to Lambda Calculus. Lecture 7 CS /08/09

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

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

CMSC 330: Organization of Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Fundamentals and lambda calculus

Costly software bugs that could have been averted with type checking

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

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

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

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

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

G Programming Languages - Fall 2012

Formal Systems and their Applications

The Untyped Lambda Calculus

dynamically typed dynamically scoped

Introduction to the λ-calculus

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Programming Languages Lecture 15: Recursive Types & Subtyping

Semantic Analysis and Type Checking

Functional Programming

CSE-321 Programming Languages 2010 Midterm

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Functional Programming and Haskell

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lecture 5: The Halting Problem. Michael Beeson

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages. Lambda Calculus

Software II: Principles of Programming Languages. Why Expressions?

Programming Languages Lecture 14: Sum, Product, Recursive Types

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CMSC330. Objects, Functional Programming, and lambda calculus

CS 242. Fundamentals. Reading: See last slide

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

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

CMSC 330: Organization of Programming Languages

Graphical Untyped Lambda Calculus Interactive Interpreter

Functional Languages and Higher-Order Functions

COS 320. Compiling Techniques

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

Programming Language Concepts: Lecture 19

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CSCI.4430/6969 Programming Languages Lecture Notes

Lecture Notes on Data Representation

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

Elixir, functional programming and the Lambda calculus.

Mutable References. Chapter 1

CMSC 330: Organization of Programming Languages

Programming Systems in Artificial Intelligence Functional Programming

Chapter 11 :: Functional Languages

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

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

CMSC 330: Organization of Programming Languages. Operational Semantics

CS558 Programming Languages

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Transcription:

More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic Safety, Judgments, Derivations, Soundness #1 #2 Lambda Review Functional Programming λ-calculus is a calculus of functions e := x λx. e e 1 e 2 Several evaluation strategies exist based on β-reduction (λx.e) e β [e /x] e The λ-calculus is a prototypical functional language with: no side effects several evaluation strategies lots of functions nothing but functions (pure λ-calculus does not have any other data type) How does this simple calculus relate to real programming languages? #3 How can we program with functions? How can we program with only functions? #4 Programming With Functions Variables in Functional Languages Functional programming style is a programming style that relies on lots of functions A typical functional paradigm is using functions as arguments or results of other functions Called higher-order programming Some impure functional languages permit sideeffects (e.g., Lisp, Scheme, ML, Python) references (pointers), in-place update, arrays, exceptions Others (and by others we mean Haskell ) use monads to model state updates We can introduce new variables: let x = e 1 in e 2 x is bound by let x is statically scoped in (a subset of) e 2 This is pretty much like (λx. e 2 ) e 1 In a functional language, variables are never updated they are just names for expressions or values e.g., x is a name for the value denoted by e 1 in e 2 This models the meaning of let in math (proofs) #5 #6 1

Referential Transparency In pure functional programs, we can reason equationally, by substitution Called referential transparency let x = e 1 in e 2 === [e 1 /x]e 2 In an imperative language a side-effect in e 1 might invalidate the above equation The behavior of a function in a pure functional language depends only on the actual arguments Just like a function in math This makes it easier to understand and to reason about functional programs #7 How Tough Is Lambda? How complex (a la CS theory) is it to determine if: e 1 β* e and e 2 β* e #8 Expressiveness of λ-calculus Encoding Booleans in λ-calculus The λ-calculus is a minimal system but can express data types (integers, booleans, lists, trees, etc.) branching recursion This is enough to encode Turing machines We say the lambda calculus is Turing-complete Corollary: e = β e is undecidable Still, how do we encode all these constructs using only functions? Idea: encode the behavior of values and not their structure What can we do with a boolean? we can make a binary choice (= if statement) A boolean is a function that, given two choices, selects one of them: true = def λx. λy. x false = def λx. λy. y if E 1 then E 2 else E 3 = def E 1 E 2 E 3 Example: if true then u else v is (λx. λy. x) u v β (λy. u) v β u #9 #10 Encoding Pairs in λ-calculus What can we do with a pair? we can access one of its elements (=.field access ) A pair is a function that, given a boolean, returns the first or second element mkpair x y = def λb. b x y fst p = def p true snd p = def p false fst (mkpair x y) β (mkpair x y) true β true x y β x Encoding Numbers in λ Calculus What can we do with a natural number? What do you, the viewers at home, think? #11 #12 2

Encoding Numbers λ-calculus What can we do with a natural number? we can iterate a number of times over some function (= for loop ) A natural number is a function that given an operation f and a starting value s, applies f a number of times to s: 0 = def λf. λs. s 1 = def λf. λs. f s 2 = def λf. λs. f (f s) Very similar to List.fold_left and friends These are numerals in a unary representation Called Church numerals #13 Test Time! How would you encode the successor function (succ x = x+1)? How would you encode more general addition? Recall: 4 = def λf. λs. f f f (f s) #14 Computing with Natural Numbers Computation Example The successor function succ n = def λf. λs. f (n f s) or succ n = def λf. λs. n f (f s) Addition add n 1 n 2 = def n 1 succ n 2 Multiplication mult n 1 n 2 = def n 1 (add n 2 ) 0 Testing equality with 0 iszero n = def n (λb. false) true Subtraction Is not instructive, but makes a fun exercise #15 What is the result of the application add 0? (λn 1. λn 2. n 1 succ n 2 ) 0 β λn 2. 0 succ n 2 = λn 2. (λf. λs. s) succ n 2 β λn 2. n 2 = λx. x By computing with functions we can express some optimizations But we need to reduce under the lambda Thus this never happens in practice #16 Toward Recursion Given a predicate P, encode the function find such that find P n is the smallest natural number which is larger than n and satisfies P Claim: with find we can encode all recursion Intuitively, why is this true? Encoding Recursion Given a predicate P encode the function find such that find P n is the smallest natural number which is larger than n and satisfies P find satisfies the equation find p n = if p n then n else find p (succ n) Define F = λf.λp.λn.(p n) n (f p (succ n)) We need a fixed point of F find = F find or find p n = F find p n #17 #18 3

The Fixed-Point Combinator Y Let Y = λf. (λy.f(y y)) (λx. F(x x)) This is called the fixed-point combinator Verify that Y F is a fixed point of F Y F β (λy.f (y y)) (λx. F (x x)) β F (Y F) Thus Y F = β F (Y F) Given any function in λ-calculus we can compute its fixed-point (w00t! why do we not win here?) Thus we can define find as the fixed-point of the function F from the previous slide Essence of recursion is the self-application y y Expressiveness of Lambda Calculus Encodings are fun Yes! Yes they are! But programming in pure λ-calculus is painful So we will add constants (0, 1, 2,, true, false, if-then-else, etc.) Next we will add types #19 #20 Still Going! Types One minute break Stretch! #21 A program variable can assume a range of values during the execution of a program An upper bound of such a range is called a type of the variable A variable of type bool is supposed to assume only boolean values If x has type bool then the boolean expression not(x) has a sensible meaning during every run of the program #22 Typed and Untyped Languages The Purpose Of Types Untyped languages Do not restrict the range of values for a given variable Operations might be applied to inappropriate arguments. The behavior in such cases might be unspecified The pure λ-calculus is an extreme case of an untyped language (however, its behavior is completely specified) (Statically) Typed languages Variables are assigned (non-trivial) types A type system keeps track of types Types might or might not appear in the program itself Languages can be explicitly typed or implicitly typed #23 The foremost purpose of types is to prevent certain types of run-time execution errors Traditional trapped execution errors Cause the computation to stop immediately And are thus well-specified behavior Usually enforced by hardware e.g., Division by zero, floating point op with a NaN e.g., Dereferencing the address 0 (on most systems) Untrapped execution errors Behavior is unspecified (depends on the state of the machine = this is very bad!) e.g., accessing past the end of an array e.g., jumping to an address in the data segment #24 4

Execution Errors A program is deemed safe if it does not cause untrapped errors Languages in which all programs are safe are safe languages For a given language we can designate a set of forbidden errors A superset of the untrapped errors, usually including some trapped errors as well e.g., null pointer dereference Modern Type System Powers: prevent race conditions (e.g., Flanagan TLDI 05) prevent insecure information flow (e.g., Li POPL 05) prevent resource leaks (e.g., Vault, Weimer) help with generic programming, probabilistic languages, are often combined with dynamic analyses (e.g., CCured) #25 Preventing Forbidden Errors - Static Checking Forbidden errors can be caught by a combination of static and run-time checking Static checking Detects errors early, before testing Types provide the necessary static information for static checking e.g., ML, Modula-3, Java Detecting certain errors statically is undecidable in most languages #26 Preventing Forbidden Errors - Dynamic Checking Safe Languages Required when static checking is undecidable e.g., array-bounds checking Run-time encodings of types are still used (e.g. Lisp) Should be limited since it delays the manifestation of errors Can be done in hardware (e.g. null-pointer) #27 There are typed languages that are not safe ( weakly typed languages ) All safe languages use types (statica or dynamic) Typed Untyped Safe Static ML, Java, Ada, C#, Haskell,... Dynamic Lisp, Scheme, Ruby, Perl, Smalltalk, PHP, Python, λ-calculus Unsafe C, C++, Pascal,...? Assembly We focus on statically typed languages #28 Why Typed Languages? Homework Development Type checking catches early many mistakes Reduced debugging time Typed signatures are a powerful basis for design Typed signatures enable separate compilation Maintenance Types act as checked specifications Types can enforce abstraction Execution Static checking reduces the need for dynamic checking Safe languages are easier to analyze statically the compiler can generate better code Read Cardelli article Spread it over the break Read great works of literature Homework 5 Due Today Don t ruin your Spring Break by having it hanging over you No Class Next Week (Spring Break!) Next Lecture: Tue Mar 14 #29 #30 5