Lecture 9: More Lambda Calculus / Types

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

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

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Introduction to the Lambda Calculus

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

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

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

Type Systems Winter Semester 2006

Introduction to the Lambda Calculus. Chris Lomont

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

More Lambda Calculus and Intro to Type Systems

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

Functional Languages and Higher-Order Functions

CSE 505: Concepts of Programming Languages

Chapter 5: The Untyped Lambda Calculus

Untyped Lambda Calculus

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

Lambda Calculus. Lecture 4 CS /26/10

λ calculus is inconsistent

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

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Untyped Lambda Calculus

The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

Functional Programming

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

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

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

1 Scope, Bound and Free Occurrences, Closed Terms

Lecture 5: The Untyped λ-calculus

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

301AA - Advanced Programming

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

CMSC 330: Organization of Programming Languages

Pure Lambda Calculus. Lecture 17

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

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

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

Simply-Typed Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Concepts of programming languages

Lambda Calculus. Variables and Functions. cs3723 1

Introduction to the λ-calculus

Functional Programming

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Part I. Historical Origins

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

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

Chapter 11 :: Functional Languages

VU Semantik von Programmiersprachen

CMSC 330: Organization of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Costly software bugs that could have been averted with type checking

Lambda Calculus-2. Church Rosser Property

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

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

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

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

Fundamentals and lambda calculus

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

CMSC 330: Organization of Programming Languages

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

From the λ-calculus to Functional Programming Drew McDermott Posted

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.

The Untyped Lambda Calculus

Lambda Calculus. Lambda Calculus

4/19/2018. Chapter 11 :: Functional Languages

Derivation for the Necessity of Recursive Types and its Basic Usage

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

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

SOFTWARE ARCHITECTURE 6. LISP

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

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

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

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

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

Presented By : Abhinav Aggarwal CSI-IDD, V th yr Indian Institute of Technology Roorkee. Joint work with: Prof. Padam Kumar

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Recursive Definitions, Fixed Points and the Combinator

The Untyped Lambda Calculus

Elixir, functional programming and the Lambda calculus.

Lecture 5: The Halting Problem. Michael Beeson

LECTURE 16. Functional Programming

MATH Iris Loeb.

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

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Lecture Notes on Data Representation

CS558 Programming Languages

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Lexicografie computationala Feb., 2012

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

Transcription:

Lecture 9: More Lambda Calculus / Types CSC 131 Spring, 2019 Kim Bruce

Pure Lambda Calculus Terms of pure lambda calculus - M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete Left associative: M N P = (M N) P. Computation based on substituting actual parameter for formal parameters

Computation Rules Reduction rules for lambda calculus: (α) λx. M λy. ([y/x] M), if y FV(M). change name of parameters if new not capture old (β) (λx. M) N [N/x] M. computation by subst function argument for formal parameter (η) λx. (M x) M. Optional rule to get rid of excess λ s

Computability Can encode all computable functions in pure untyped lambda calculus. - true = λ u. λ v. u true a b = a - false = λ u. λ v. v false a b = b - cond = λ u. λ v. λ w. u v w cond true a b =? cond false a b =?

Encoding Natural Numbers Natural numbers: - 0 = λ s. λ z. z. - 1 = λ s. λ z. s z. - 2 = λ s. λ z. s (s z). Integers encode repetition: - 2 f x = f (f x) - 3 f x = f( f (f x)) - n f x = f (n) (x)

Arithmetic Succ = λ n. λ s. λ z. s (n s z) - Succ n = λ s. λ z. s (n s z) = λ s. λ z. s (s (n) z) = n+1 Plus = λ n. λ m. λ s. λ z. m s (n s z). Mult = λ n. λ m. (m ( Plus n) 0). iszero = λ n. n (λ x. false) true Subtraction is hard!!

Recursion A different perspective: Start with - fact = λn. cond (iszero n) 1 (Mult n (fact (Pred n))) Let F stand for the closed term: - λf. λn. cond (iszero n) 1 (Mult n (f (Pred n))) - Notice F(fact) = fact. - fact is a fixed point of F - To find fact, need only find fixed point of F! Easy w/ g(x) = x * x, but F????

Fixed Points Several fixed point operators: - Ex: Y = λf. (λx. f (xx))(λx. f (xx)) Claim for all g, Y g = g (Y g) Y g = (λf. (λx. f (xx))(λx. f (xx))) g Invented by Haskell Curry = (λx. g(xx))(λx. g(xx)) = g((λx. g(xx)) (λx. g(xx))) = g (Y g) If let x 0 = Y g, then g (x 0 ) = x 0.

Lambda Calculus λ-calculus invented in 1928 by Church in Princeton & first published in 1932. Goal to provide a foundation for logic First to state explicit conversion rules. Original version inconsistent, but corrected - If this sentence is true then 1 = 2 problematic!! 1933, definition of natural numbers

Collaborators 1931-1934: Grad students: - J. Barkley Rosser and Stephen Kleene - Church-Rosser confluence theorem ensured consistency (earlier version inconsistent) O - Kleene showed λ-definable functions very rich Equivalent to Herbrand-Gödel recursive functions Equivalent to Turing-computable functions. Founder of recursion theory, invented regular expressions Church s thesis: - λ-definability effectively computable

Undecidability Convertibility problem for λ-calculus undecidable. Validity in first-order predicate logic undecidable. Proved independently year later by Turing. - First showed halting problem undecidable

Alan Turing Turing - 1936, in Cambridge, England, definition of Turing machine - 1936-38, in Princeton to get Ph.D. under Church. - 1937, first published fixed point combinator (λx. λy. (y (x x y))) (λx. λy. (y (x x y))) - Kleene did not use fixed-point operator in defining functions on natural numbers! - Broke German enigma code in WW2, Turing test AI - Persecuted as homosexual, committed suicide in 1954

Typed Lambda Calculus

Types Can specify types of identifiers Start with base type e and build up types and terms: - Type ::= e Type Type - M ::= v (M M) λv : Type. M Examples: - Types: e, e e, e e e, (e e) e,... - Terms: λx: e. x, λf: e e. λz: e. f(f(z))

Definitions Earlier definitions generalize over types t: - true t = λx:t. λy:u. x - n t = λs: t t. λz: t. s (n) (z) Some untyped terms can t be typed: - Ω = (λx. (x x))(λx. (x x)) - Y = λf. (λx. f (x x))(λx. f (x x))

Totally Awesome!! Theorem: If M is a term of the typed lambda calculus, then M has a unique normal form. I.e., every term of the typed lambda calculus is total. Corollary: The typed lambda calculus does not include all computable functions.

Types

Why (Static) Types? Increase readability, esp. for libraries Hide representation Detection of errors. Help disambiguate operators Compiler optimization. E.g. know where fields of record/struct are. Help ensure different components in separately compiled units will interoperate properly Provide basis for code completion in editors

Types & Constructors Built-in types - primitive types (incl. size) Aggregate types - records/structs Mapping types - arrays/functions Recursive types - lists/trees Sequence types - files and strings (primitive?) User-defined types

Aggregate Types Cartesian products (tuples) Records / Structs Union Types - C: typedef union {int i; float r;} utype - unsafe - Discriminated union safer - Haskell type defs safe

Discriminated Union: Ada type geometric (Kind: (Triangle, Square) := Square) is record color : ColorType := Red ; case Kind of Kind is tag when Triangle => pt1,pt2,pt3:point; when Square => upperleft : Point; length : INTEGER range 1..100; end case; end record; ob1 : geometric -- default is Square ob2 : geometric(triangle) -- frozen, can't be changed

Mappings Arrays - Static - location & size frozen at compile time (FORTRAN) - Semi-static - size bound at compile time, location at invocation (Pascal, C) - Dynamic - size and location bound at creation (ALGOL 60, Ada, Java) - Flex - size and location can be changed any time (Java vectors) Function Types - update less efficient - update f arg nuval = fn x => if x = arg then nuval else f x

Recursive Types In Haskell: data List = Nil Cons (Integer, List) In C: struct list { int x; list *next; }; Solutions to: list = { Nil } (int list) 1. finite seqs of ints followed by Nil: e.g., (2,(5,Nil)) 2. finite or infinite seqs: if finite then end w/ Nil Recursive eqn s always have a least solution - least fixed point!

Least Recursive Solutions list 0 = {Nil} list 1 = {Nil} (int list 0 ) = {Nil} {(n, Nil) n int} list 2 = {Nil} (int list 1 ) = {Nil} {(n, Nil) n int} {(m, (n, Nil)) m, n int} list... = n list n Some solutions inconsistent w/classical math!

User-Defined Types Named types - More readable - Easy to modify if localized - Factorization (why repeat same def?) - Added consistency checking if generative Enumeration types added to Java 5

What does it mean for a language to be type-safe?

Safe Languages Two kinds of execution errors - Trapped errors: cause computation to halt immediately. Divide by zero, null pointer exception - Untrapped errors: go unnoticed and later cause problems. Access an illegal address, e.g., array bounds error. Program fragment is safe if it causes no untrapped errors. - Language is safe if all program fragments are safe. See Type Systems by Luca Cardelli http://lucacardelli.name/papers/typesystems%201st%20edition.us.pdf

Strongly Typed Languages Language designates forbidden errors - those that are not allowed to happen. - should include all untrapped errors Program fragment is well behaved if it generates no forbidden errors. Language where all legal programs are well behaved is strongly typed

Static vs. Dynamic Typing Most use static typing - including C/Java/ML/Haskell - binding of types to variables done at translation time. - Find errors earlier, but conservative. dynamic typing - LISP/Scheme/Racket/Python/Javascript/Grace - binding of type to value, not variable. thus binding of type to variable changes dynamically - Dynamic more flexible, but more overhead.

(Static) Type Checking

Static Type Checking Static type-checkers for strongly-typed languages (i.e., rule out all bad programs) must be conservative: - Rule out some programs without errors. if (program-that-could-run-forever) { expression-w-type-error; } else { expression-w-type-error; }