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

Similar documents
More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems

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

More Lambda Calculus and Intro to Type Systems

Programming Languages Lecture 15: Recursive Types & Subtyping

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

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

Concepts of programming languages

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.

Programming Languages

Chapter 5: The Untyped Lambda Calculus

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

The Untyped Lambda Calculus

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

CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

CIS 500 Software Foundations Midterm I

CSE 505: Concepts of Programming Languages

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

Simply-Typed Lambda Calculus

Costly software bugs that could have been averted with type checking

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

CMSC 330: Organization of Programming Languages

Exercise 1 ( = 24 points)

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

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

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

CMSC 330: Organization of Programming Languages

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

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

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

Exercise 1 ( = 18 points)

CSE-321 Programming Languages 2010 Final

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

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

Functional Languages and Higher-Order Functions

Lecture 5: The Untyped λ-calculus

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

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

CMSC 330: Organization of Programming Languages

Exercise 1 ( = 22 points)

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

Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

CSE-321 Programming Languages 2012 Midterm

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CMSC 330: Organization of Programming Languages

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

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

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

CSE-321 Programming Languages 2010 Midterm

Programming Languages

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

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.

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

Introductory Example

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

Programming Languages

Fundamentals and lambda calculus

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

1 Introduction. 3 Syntax

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

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

Programming Languages

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

Exercise 1 (2+2+2 points)

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Introduction to the Lambda Calculus. Chris Lomont

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

The Untyped Lambda Calculus

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

Programming Languages

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Recursive Definitions, Fixed Points and the Combinator

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Programming Languages

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Programming Languages

The University of Nottingham

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

CMSC 330: Organization of Programming Languages

Introduction to the Lambda Calculus

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

Part I. Historical Origins

λ calculus is inconsistent

Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

CSE-321 Programming Languages 2011 Final

Formal Systems and their Applications

Programming Languages

Transcription:

CSE 230: Winter 2010 Principles of Programming Languages Lecture 11: Type Systems News New HW up soon Special Hour to discuss HW? Ranjit Jhala UC San Diego Programming with λ-calculus Encode: bool if-then-else int recursion as functions Encoding Natural Numbers Q: What can we do with a natural number? A: Iterate a number of times over some function Nat: function that takes fun f, starting value s: returns: f applied to s a number of times 0 = def λf. f λs. s 1 = def λf. λs. f s 2 = def λf. λs. f (f s) M Called Church numerals, unary representation Note: (n f s) : apply f to s n times, i.e. f n (s)

Operating on Natural Numbers Testing equality with 0 iszero n = def n (λb. false) true iszero = def λn. n (λ b.false) true The successor function succ n = def λf. λs. f (n f s) succ = def λn. λf. λs. f (n f s) Addition add n 1 n 2 = def n 1 succ n 2 add = def λn 1.λn 2. n 1 succ n 2 Multiplication mult n 1 n 2 = def n 1 (add n 2 ) 0 mult = def λn 1.λn 2. n 1 (add n 2 ) 0 Ex: Computing with Naturals What is the result of 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 Ex: Computing with Naturals mult 2 2 2 (add 2) 0 (add 2) ((add 2) 0) 2 succ (add 2 0) 2 succ (2 succ 0) succ (succ (succ (succ 0))) succ (succ (succ (λf. λs. f (0 f s)))) succ (succ (succ (λf. λs. f s))) succ (succ (λg. λy. g ((λf. λs. f s) g y))) succ (succ (λg. λy. g (g y))) * λg. λy. g (g (g (g y))) = 4 λ Calculus Review Equivalent to Turing machine Encodes several datatypes bool, int, pairs, (HW: lists ) Recursion

Encoding Recursion Write a function find that: takes predicate P, natural n returns: smallest natural larger than n satisfying i P find can encode all recursion but how to write it? Encoding Recursion 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)) A fixpoint of F is an x st s.t. x = F x find is a fixpoint of F! as find p n = F find p n so find = F find Q: Given λ-term F, how to write its fixpoint? The Y-Combinator Define: Y = def λf. (λy.f(y y)) (λx. F(x x)) Called the fixpoint combinator as Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F (Y F) ie i.e. Y F = β F (Y F) Can get fixpoint for any λ-calculus function Whoa! Define: F = λf.λp.λn.(p n) n (f p (succ n)) and: find = Y F Whats going on? find p n = β Y F p n = β F (Y F) p n = β F find p n = β (p n) n (find p (succ n))

Fixpoint Combinators Y = def λf. (λy.f(y y)) (λx. F(x x)) How does this mix with Call-by-Value? Y F β (λy.f (y y)) (λx. F (x x)) β F ((λx.f (x x))(λz. F (z z))) β F (F ((λx.f (x x))(λz. F (z z)))) β F (F (F ((λx.f (x x))(λz. F (z z))))) β Many other fixpoint combinators Including those that work for CBV Including Klop s Combinator: Y k = def (L L L L L L L L L L L L L L L L L L L L L L L L L L) where: L = def λaλbλcλdλeλfλgλhλiλjλkλlλmλnλoλpλqλsλtλuλvλwλxλyλzλr. r (t h i s i s a f i x p o i n t c o m b i n a t o r) Expressiveness of λ-calculus Encodings are fun but programming in pure λ-calculus is not Encodings complicate static analysis Type Systems Know the λ-calculus encodes them, so we add 0,1,2,,true,false,if-then-else then else to language Next, we will add types

Types Variables have many values during execution Type: Overapprox bound on set of values Var : bool only takes on boolean values If x : bool then expression not(x) has a sensible meaning during every run Type = property/predicate satisfied by variable at all times during execution i.e. type = Invariant Why types? To prevent classes of execution errors: abc + true if ranjit then else Accessing missing fields Null pointer dereference Using tainted format strings printf(s, ) Writing user pointers inside kernel Preventing Errors by Static Checking Finds errors at compile-time before testing Types provide necessary information Types are invariants Java, C#, ML, For some errors static check difficult Preventing Errors by Dynamic Checking when static checking is difficult e.g. array-bounds checking, divide by 0 Run-time encoding of types (e.g. Lisp) Delays manifestation of errors but better late than never

Safe Languages Types restrict programs to prohibit classes of errors statically or dynamically Safe Typed Static Dynamic ML, Java, C#, F# Lisp, Python Untyped λ-calculus Unsafe C, C++,...? Assembly CSE 230 = statically typed languages