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

Simply-Typed Lambda Calculus

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

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

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.

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

Com S 541. Programming Languages I

Lecture 5: The Untyped λ-calculus

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

CIS 500 Software Foundations Fall September 25

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

Functional Programming

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

CMSC 330: Organization of Programming Languages

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

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

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

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

CSE 505: Concepts of Programming Languages

G Programming Languages - Fall 2012

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

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

CS 242. Fundamentals. Reading: See last slide

Type Systems Winter Semester 2006

Functional Programming and Haskell

Semantic Analysis and Type Checking

Recursive Types and Subtyping

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

CMSC 330: Organization of Programming Languages

Lecture 5: The Halting Problem. Michael Beeson

dynamically typed dynamically scoped

Graphical Untyped Lambda Calculus Interactive Interpreter

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

Software II: Principles of Programming Languages. Why Expressions?

Programming Languages

COS 320. Compiling Techniques

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

Functional Programming. Big Picture. Design of Programming Languages

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Formal Systems and their Applications

The role of semantic analysis in a compiler

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

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.

Chapter 7. Expressions and Assignment Statements ISBN

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Recursive Types and Subtyping

Chapter 7. Expressions and Assignment Statements

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

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

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

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

Concepts of programming languages

CMSC330. Objects, Functional Programming, and lambda calculus

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

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

(Advanced) topics in Programming Languages

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

CS 11 Haskell track: lecture 1

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

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

Reasoning About Imperative Programs. COS 441 Slides 10

Programming Languages Lecture 14: Sum, Product, Recursive Types

Types and Type Inference

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Short Notes of CS201

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Costly software bugs that could have been averted with type checking

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

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

Programming Systems in Artificial Intelligence Functional Programming

CS201 - Introduction to Programming Glossary By

Functional Languages. Hwansoo Han

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

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

Mutable References. Chapter 1

Type Checking and Type Inference

Static Program Analysis Part 1 the TIP language

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

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

LECTURE 17. Expressions and Assignment

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

Programming Languages Lecture 15: Recursive Types & Subtyping

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

Introduction. chapter Functions

Type Checking and Type Equality

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

Typing Control. Chapter Conditionals

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Chapter 11 :: Functional Languages

Weeks 6&7: Procedures and Parameter Passing

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

Lambda Calculus.

CS558 Programming Languages

Transcription:

More Lambda Calculus and Intro to Type Systems #1

One Slide Summary The lambda calculus is a model of computation or a programming language that is as expressive as a Turing machine. The lambda calculus centers on function definition and function application. The meaning of function application is given by substitution (beta reduction). We can encode the booleans (and, or, not, if) and the numbers (zero, successor, add, multiply, equality, looping) via lambdas. #2

Plan Heavy Class Participation Thus, wake up! (not actually kidding) Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic Safety, Judgments, Derivations, Soundness #3

Lambda Review -calculus is a calculus of functions e := x x. e e1 e2 Several evaluation strategies exist based on -reduction ( x e) e! [e /x] e How does this simple calculus relate to real programming languages? #4

Functional Programming 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 can we program with functions? How can we program with only functions? #5

Programming With Functions Functional programming 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 #6

Variables in Functional Languages We can introduce new variables: let x = e1 in e2 x is bound by let x is statically scoped in (a subset of) e2 This is pretty much like ( x. e2) e1 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 e1 in e2 This models the meaning of let in math (proofs) #7

Referential Transparency In pure functional programs, we can reason equationally, by substitution Called referential transparency let x = e1 in e2 === [e1/x]e2 In an imperative language a side-effect in e1 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 #8

How Tough Is Lambda? Given e1 and e2, how complex (a la CS theory) is it to determine if: e1! * e and e2! * e #9

Expressiveness of -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: e1 = e2 is undecidable Still, how do we encode all these constructs using only functions? Idea: encode the behavior of values and not their structure #10

Encoding Booleans in -Calculus 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 E1 then E2 else E3 =def E1 E2 E3 Example: if true then u else v is ( x. y. x) u v! ( y. u) v! u #11

More Boolean Encodings Let s try to do boolean or together Recall: true =def x. y. x false =def x. y. y if E1 then E2 else E3 =def E1 E2 E3 We want or to take in two booleans and yield a result that is a boolean How can we do this? #12

A Trying Ordeal Recall: true =def x. y. x false =def x. y. y if E1 then E2 else E3 =def E1 E2 E3 Intution: or a b = if a then true else b Either of these will work: or =def a. b. a true b or =def a. b. x. y. a x (b x y) #13

Another Demand How to do and and not and a b = if a then b else false and =def a. b. a b false and =def a. b. x. y. a (b x y) y not a = if a then false else true not =def a. a false true not =def a. x. y. a y x #15

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 fst p =def snd p =def fst (mkpair x y)! true x y b. b x y p true p false! (mkpair x y) true! x #16

Encoding Numbers in Calculus What can we do with a natural number? What do you, the viewers at home, think? #17

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 #18

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) #19

Computing with Natural Numbers The successor function succ n or succ n Addition add n1 n2 Multiplication mult n1 n2 Testing equality with 0 iszero n Subtraction =def f. s. f (n f s) =def f. s. n f (f s) =def n1 succ n2 =def n1 (add n2) 0 =def n ( b. false) true Is not instructive, but makes a fun exercise #20

Computation Example What is the result of the application add 0? ( n1. n2. n1 succ n2) 0! n2. 0 succ n2 = n2. ( f. s. s) succ n2! n2. n2 = 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 #21

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? #22

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 #23

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 define 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 #24

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 #25

Still Going! One minute break Stretch! #26

Q: Books (711 / 842) In this 1943 Antoine de SaintExupery novel the title character lives on an asteroid with a rose but eventually travels to Earth.

Springfield, Illinois Animaniacs Two player game Players take turns naming State Capitals until one player says Springfield, Illinois The player to say Springfield, Illinois receives points equal to the number of previously-named Capitals Why is this game interesting? #28

Q: Computer Science ( 姚期智) This Shanghai-born Turing Award winner is known for contributions to the theory of computation. He formulated the Millionaire's Problem and stated this minimax principle: the expected cost of any randomized algorithm for solving a given problem, on the worst case input for that algorithm, can be no better than the expected cost, for a worst-case random probability distribution on the inputs, of the deterministic algorithm that performs best against that distribution.

Types 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 #30

Typed and Untyped Languages 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 #31

The Purpose Of Types 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 #32

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) #33

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 #34

Preventing Forbidden Errors Dynamic Checking 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) #35

Safe Languages There are typed languages that are not safe ( weakly typed languages ) All safe languages use types (static or dynamic) Typed Static Safe ML, Java, Ada, C#, Haskell,... Unsafe C, C++, Pascal,... Untyped Dynamic Lisp, Scheme, Ruby, -calculus Perl, Smalltalk, PHP, Python,? Assembly We focus on statically typed languages #36

Why Typed Languages? 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 #37

Homework Read Cardelli article Read Wright & Matthias article Homework 5 Due Soon #38