Concepts of programming languages

Similar documents
Chapter 5: The Untyped Lambda Calculus

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

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

Type Systems Winter Semester 2006

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.

CIS 500 Software Foundations Fall September 25

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

CIS 500 Software Foundations Midterm I

Pure Lambda Calculus. Lecture 17

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

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

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

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

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

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

Costly software bugs that could have been averted with type checking

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Untyped Lambda Calculus

CIS 500 Software Foundations Fall October 2

Fundamentals and lambda calculus

Lambda Calculus. Lecture 4 CS /26/10

Untyped Lambda Calculus

CSE 505: Concepts of Programming Languages

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

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

Introduction to the Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

More Lambda Calculus and Intro to Type Systems

Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

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

Formal Systems and their Applications

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

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

More Lambda Calculus and Intro to Type Systems

λ calculus is inconsistent

Programming Languages Fall 2014

CSE-321 Programming Languages 2010 Midterm

The Untyped Lambda Calculus

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

Last class. CS Principles of Programming Languages. Introduction. Outline

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Lecture Notes on Data Representation

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

1 Introduction. 3 Syntax

The Untyped Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Graphical Untyped Lambda Calculus Interactive Interpreter

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

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Functional Programming

The Untyped Lambda Calculus

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

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

CMSC 330: Organization of Programming Languages

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

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

Chapter 5: The Untyped Lambda Calculus

Lambda expressions, functions and binding

Elixir, functional programming and the Lambda calculus.

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

Lecture 9: 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...

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

CSE-321 Programming Languages 2010 Final

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

Introduction to the λ-calculus

Introduction to the Lambda Calculus. Chris Lomont

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Lambda Calculus-2. Church Rosser Property

Functional Languages and Higher-Order Functions

Exercise 1 (2+2+2 points)

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2012 Midterm

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

dynamically typed dynamically scoped

If a program is well-typed, then a type can be inferred. For example, consider the program

Transcription:

Concepts of programming languages Lecture 5 Wouter Swierstra 1

Announcements Submit your project proposal to me by email on Friday; The presentation schedule in now online Exercise session after the lecture. 2

Last time Study an example domain specific language (SVG). What different approaches are their to implementing domain specific languages? 3

This time How can we define the semantics of trivial languages? What is the λ-calculus and what is its semantics? 4

Micro programming languages Previously, we studied SVG a realistic, but limited language. The drawback is that our semantics is cluttered by many other language design choices. For now, let s focus on smaller languages 5

A simple language e ::= True False if e then e else e We would like to define a dynamic semantics by defining an inductive relation e e, to denote that the expression e can take a single reduction step to a new expression e. Question: What are the values of this language? Question: How should this relation be defined? 6

Dynamic semantics if True then e else e e E-True if False then e else e e E-False c c if c then e else e if c then e else e E-If The first two rules are fairly obvious 7

Dynamic semantics if True then e else e e E-True if False then e else e e E-False c c if c then e else e if c then e else e E-If The first two rules are fairly obvious Question: Why are there no rules for True and False? Question: Why do we need the last rule? 7

Stuck terms A good semantics should ensure that each term: is either a value; or can make a reduction step. If this property does not hold it usually indicates that there are rules missing. As a result there are stuck terms that we cannot evaluate further, even if they are not yet values. 8

Funny rules Question: What happens if we add the following rule: e e if c then e else e if c then e else e 9

Funny rules Question: What happens if we add the following rule: e e if c then e else e if c then e else e We have more than one way to evaluate certain expressions we have too many rules. 9

Uniqueness of derivations A good semantics should also ensure that there is only one way to evaluate any expression. In this language, this is not too important we will always end up with the same value even if there is more than one derivation. But if multiple derivations exist, we cannot accurately predict how long it will take to evaluate an expression (or the order that certain effects may be triggered, when a program crashes, etc.) 10

Unicity of reduction steps Lemma If e e and e e then e = e. The proof is by rule induction on the derivations. Intuitively, we only have one possible rule to apply to a term. 11

Multiple steps Although this defines a single reduction step, how can we evaluate: if (if True then False else False) then True else True This requires more than one step! 12

Multiple steps Although this defines a single reduction step, how can we evaluate: if (if True then False else False) then True else True This requires more than one step! For that reason, we typically consider the relation e e consisting of zero or more reduction steps: e e 12 e e e e e e

Further extensions e ::= True False if e then e else e zero the number 0 succ e add one to a number iszero e check if a number is 0 Question: What new rules do we need to add to our semantics? 13

New rules e e succ e succ e e e iszero e iszero e iszero zero True iszero (succ nv) False To ensure the second and fourth rule don t overlap, we have the convention that nv refers to a numeric value, built only of only zero and succ. Question: Are there any stuck terms now? 14

New rules e e succ e succ e e e iszero e iszero e iszero zero True iszero (succ nv) False To ensure the second and fourth rule don t overlap, we have the convention that nv refers to a numeric value, built only of only zero and succ. Question: Are there any stuck terms now? 14 Yes! For example: iszero True what can we do?

Types By introducing a static semantics we can rule out such invalid this fixes our dynamic semantics. Alternatively, we could introduce an explicit error state that throws a dynamic exception when a run-time type error is encountered. But let s try to rule out these illegal expressions using types. 15

Types t ::= Bool Nat There are two types booleans and natural numbers. Next, let s try to define a relation e : t when the expression e has type t. Question: What rules should we add? 16

Static semantics True : Bool zero : Nat False : Bool e : Nat succ e : Nat e : Nat iszero e : Bool c : Bool e : t e : t if c then e else e : t 17

Soundness At this point, we typically aim to show type soundness every well-typed term is a value or can be reduced further (progress). if a well-typed term e : t can take a next step e e, then e : t (preservation) Together, these two properties guarantee that a well-typed term will never get stuck, but instead can be evaluated to a value. To do so, we typically use rule induction over our static and dynamic semantics. 18

Exercise Suppose we want to add a new construct to our language that represents addition: add e e Question: What new rules do we need to add to our static and dynamic semantics? Do progress and preservation still hold? 19

Solution Many possible choices exist. One possible choice is: e : Nat e : Nat add e e : Nat add v v (v + v ) e e e 1 e1 add v e add v e add e 1 e 2 add e1 e 2 But we could also evaluate in a different order, interpret booleans as 0 and 1, 20

More features At this point, we could define yet more features, but our language is growing rapidly as is the number of rules that we need to define. Instead, many programming language researchers choose to work on a smaller, more expressive language the lambda calculus. 21

The lambda calculus The lambda calculus is the smallest programming language imaginable. It was originally introduced by Alonzo Church (1930 s) as a foundation for mathematics but surprisingly, it perfectly captures computation! There are only three constructs: e ::= x (variables) e e (application) λx. e (abstraction) 22

The lambda calculus examples and conventions Example terms: λx. x x λx. (λy. x z) (λx. x a) Note: application associates to the left: a b c = (a b) c Note: only unary functions and unary application but we sometimes write λx y. e for λx. λy. e. Note: the function body of a lambda extends as far as possible to the right: λx. e f should be read as λx. (e f). 23

Bound and free variables An abstraction λx. e binds variable x in its body e. An occurrence of a variable that is not bound by a lambda is called free. Examples: x occurs free in λy. y (λz. x) (λx. x z) y x has one bound and one free occurrence of x. A term without free variables is called a closed term. 24 We consider two lambda terms that are equal, up to renaming of bound variables, equivalent (or more formally α-equivalent).

Equivalence examples For example, the following two terms are equivalent: λx. λt. x y t λz. λu. z y u But these are both different from: λa. λs. a z s we can only rename the bound variables. 25

λ-calculus: β-reduction A term of the form λx. e is called an abstraction or lambda binding; e is called the abstraction s body. The central rewrite rule of the λ-calculus is β-reduction: (λx. e) a β e [x 7 a] [x 7 a] := substitution of all free occurrences of variable x by a (λf. λx. λy. f y x) a b c β (λx. λy. a y x) b c β (λy. a y b) c β a c b 26 An expression of the form (λx. e) (t) is called a β-redex.

β-reduction: properties The definition of beta-reduction should be familiar from any other programming language: it allows you to fill in the arguments of a function call in the function s body. In that sense, the lambda calculus really is a tiny programming language! Beta reduction has several properties: arguments are consumed one by one functions are gradually destroyed when applied function definitions do not live in a separate space 27

λ-calculus: Name Capturing and α-conversion Consider the following example: (λy. (λb. y b)) a β (λb. y b) [y 7 a] = λb. a b And now we can reduce the following equivalent expression: (λy. (λa. y a)) a β (λa. y a) [y 7 a] = λa. a a What went wrong? Two equivalent expressions produced different results! 28

λ-calculus: Name Capturing and α-conversion Consider the following example: (λy. (λb. y b)) a β (λb. y b) [y 7 a] = λb. a b And now we can reduce the following equivalent expression: (λy. (λa. y a)) a β (λa. y a) [y 7 a] = λa. a a What went wrong? Two equivalent expressions produced different results! 28 Problem: a is captured by the innermost lambda binding!

Capture avoiding substitution The substitution [x 7 y] must be a capture-avoiding substitution that is, it should renames the abstraction variable if necessary: (λy. (λa. y a)) a β (λa. y a) [y 7 a] α (λa. y a ) [y 7 a] = λa. a a Note that we introduce an explicit α-conversion step, renaming a to a. 29

Capture avoiding substitution We can define such a capture avoiding substitution as follows: x [x 7 t] = t y [x 7 t] = y when x y (t 1 t 2 ) [x 7 t] = (t 1 [x 7 t]) (t 2 [x 7 t]) (λy. s) [x 7 t] = λy. s [x 7 t] provided y x and y does not occur free in t. Note that this last rule may require α-renaming the variable y in order to proceed with the substitution. 30