CMCS 312: Programming Languages Lecture 3: Lambda Calculus (Syntax, Substitution, Beta Reduction) Acar & Ahmed 17 January 2008



Similar documents
A Tutorial Introduction to the Lambda Calculus

Chapter 7: Functional Programming Languages

CIS 500 Software Foundations Midterm I, Review Questions

[Refer Slide Time: 05:10]

Types, Polymorphism, and Type Reconstruction

Bindings, mobility of bindings, and the -quantifier

Summary Last Lecture. Automated Reasoning. Outline of the Lecture. Definition sequent calculus. Theorem (Normalisation and Strong Normalisation)

Reading 13 : Finite State Automata and Regular Expressions

Fixed-Point Logics and Computation

The Heat Equation. Lectures INF2320 p. 1/88

Probability Using Dice


Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Lecture 1. Basic Concepts of Set Theory, Functions and Relations

Full and Complete Binary Trees

discuss how to describe points, lines and planes in 3 space.

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Static Typing for Object-Oriented Programming

Rigorous Software Development CSCI-GA

Lecture 10: CPA Encryption, MACs, Hash Functions. 2 Recap of last lecture - PRGs for one time pads

Turing Machines: An Introduction

Blame for All. Jeremy G. Siek. Amal Ahmed. Robert Bruce Findler. Philip Wadler. Abstract. 1. Introduction


9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

Specification and Analysis of Contracts Lecture 1 Introduction

Teaching Formal Methods for Computational Linguistics at Uppsala University

3 More on Accumulation and Discount Functions

A STUDY OF SEMANTICS, TYPES AND LANGUAGES FOR DATABASES AND OBJECT-ORIENTED PROGRAMMING ATSUSHI OHORI. Computer and Information Science

Pushdown Automata. International PhD School in Formal Languages and Applications Rovira i Virgili University Tarragona, Spain

2. The Language of First-order Logic

2) Write in detail the issues in the design of code generator.

Homework #1 Solutions

Lines & Planes. Packages: linalg, plots. Commands: evalm, spacecurve, plot3d, display, solve, implicitplot, dotprod, seq, implicitplot3d.

Simulation-Based Security with Inexhaustible Interactive Turing Machines

FIRST ORDER THEORY OF THE s-degrees AND ARITHMETIC

How To Understand The Theory Of Computer Science

Introduction to Type Theory

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Nonparametric adaptive age replacement with a one-cycle criterion

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

Observational Program Calculi and the Correctness of Translations

Welcome to Math Video Lessons. Stanley Ocken. Department of Mathematics The City College of New York Fall 2013

Solutions to Homework 6 Mathematics 503 Foundations of Mathematics Spring 2014

AN INTRODUCTION TO FUNCTIONAL PROGRAMMING THROUGH LAMBDA CALCULUS. Greg Michaelson

CS422 - Programming Language Design

COMPUTER SCIENCE TRIPOS

Formal Engineering for Industrial Software Development

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Great Theoretical Ideas in Computer Science

Degrees that are not degrees of categoricity

Polynomials and Factoring. Unit Lesson Plan

JOINING UP TO THE GENERALIZED HIGH DEGREES

Programming Languages

Chi Square Tests. Chapter Introduction

Automata and Formal Languages

Chapter 4 Lecture Notes

CONTINUED FRACTIONS AND FACTORING. Niels Lauritzen

Computer Science Theory. From the course description:

C H A P T E R Regular Expressions regular expression

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Lecture 2: Universality

a 1 x + a 0 =0. (3) ax 2 + bx + c =0. (4)

TU e. Advanced Algorithms: experimentation project. The problem: load balancing with bounded look-ahead. Input: integer m 2: number of machines

Forecasting in supply chains

EMBEDDING COUNTABLE PARTIAL ORDERINGS IN THE DEGREES

Lecture 2: Regular Languages [Fa 14]

1.5. Factorisation. Introduction. Prerequisites. Learning Outcomes. Learning Style

The Goldberg Rao Algorithm for the Maximum Flow Problem

CSE 459/598: Logic for Computer Scientists (Spring 2012)

Predicate Logic. For example, consider the following argument:

Correspondence analysis for strong three-valued logic

Mathematical Induction

INCOMPLETE CONTRACTS AND COMPLEXITY COSTS

Andrew Pitts chapter for D. Sangorgi and J. Rutten (eds), Advanced Topics in Bisimulation and Coinduction, Cambridge Tracts in Theoretical Computer

BANACH AND HILBERT SPACE REVIEW

How To Write A Program Verification And Programming Book

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

the recursion-tree method

Adding GADTs to OCaml the direct approach

A NOTE ON INITIAL SEGMENTS OF THE ENUMERATION DEGREES

1 Portfolio mean and variance

Microphone Modem Model, and MinML Modification

If n is odd, then 3n + 7 is even.

CHAPTER II THE LIMIT OF A SEQUENCE OF NUMBERS DEFINITION OF THE NUMBER e.

Lecture 13 - Basic Number Theory.

Computer Science/Software Engineering

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Turing Degrees and Definability of the Jump. Theodore A. Slaman. University of California, Berkeley. CJuly, 2005

Math 115A HW4 Solutions University of California, Los Angeles. 5 2i 6 + 4i. (5 2i)7i (6 + 4i)( 3 + i) = 35i + 14 ( 22 6i) = i.

Formal Grammars and Languages

Set-theoretic Foundation of Parametric Polymorphism and Subtyping

(LMCS, p. 317) V.1. First Order Logic. This is the most powerful, most expressive logic that we will examine.

Complexity of Higher-Order Queries

15.3. Calculating centres of mass. Introduction. Prerequisites. Learning Outcomes. Learning Style

Constrained optimization.

A Little Set Theory (Never Hurt Anybody)

Transcription:

CMCS 312: Programming Languages Lecture 3: Lambda Calculus (Syntax, Substitution, Beta Reduction) Acar & Ahmed 17 January 2008 Contents 1 Announcements 1 2 Introduction 1 3 Abstract Syntax 1 4 Bound and Free Variables 3 5 Substitution 4 6 Alpha Conversion and Alpha Equivalence 5 7 β-reduction 6 8 Homework Exercise 7 1 Introduction Today we will talk about lambda calculus. Lambda calculus is tiny calculus that is Turing complete. It is important because it enables us both to write reasonably sophisticated programs and also study about the properties of such programs using mathematics. 2 Abstract Syntax Let V be a countable set V of variables. lambda calculus as follows. We define the abstract syntax for Definition 1 (Inductive Definition) T is the least set of the terms that satisfy the following. 1. if x V then x T 2. if t 1 T and t 2 T then t 1 t 2 T 3. if x V and t T then λx.t T 4. T is the least set verifying the above properties 1

Each term in T is called a lambda term. Some examples are x, λx.x, λx.x y. The term λx.x is also known as (lambda) abstraction, and the term t 1 t 2 is known as application. Note that this is the definition of the abstract syntax. That is, it defines the set of properly parsed terms (i.e., abstract syntax trees). It does not tell us how to read a lambda term. For example, we can parse λx.x y as (λx.x y) or (λx.x)y. Similarly, we can parse t 1 t 2 t 3 as (t 1 t 2 ) t 3 or t 1 (t 2 t 3 ). We will use parenthesis to aid in parsing (to disambiguate the syntax). To minimize parenthesis, we will have the following conventions: 1. Application associates to the left. 2. The body of a lambda terms extends as far as right as possible. With this convention λx.x y is parsed as λx.(x y) and t 1 t 2 t 3 is parsed as (t 1 t 2 ) t 3. We can define the same syntax based on inference rules: Definition 2 (Inference Rules) Given a countable set of variables V, the set of lambda terms is defined as follows. 1. 2. 3. x V x T t 1 T t 2 T t 1 t 2 T x V t T λx.t T The most succinct definition is the BNF style. Definition 3 (BNF Style) Assuming that x ranges over a countable set of variables, the set of lambda terms t is defined as follows. t : : = x t 1 t 2 λx.t So we have defined the syntax for lambda terms but what do they mean? An intuitive way of thinking of λx.t is as a function that takes x and computes the result in its body t. To develop this intuition a bit further let s abuse our notation a bit. Suppose that our syntax allow us to write natural numbers and add them. For example, we may have terms like this 1, 2, + 1 2. Now, what does the following lambda terms do? 1. λx. + x 1, 2. λx.λy. + x y, 3. λx.λy.λ.z.z(+ x y). 2

3 Bound and Free Variables A lambda abstraction denotes a function. For example, the abstraction λx.+ x 1 denotes a function that takes a parameter, x, and returns the value x + 1. The parameter x is called the formal parameter and we say that λ binds it. In a lambda abstraction, the formal parameter is followed by a. and the body of the function. A lambda abstraction always consists of the four parts: the λ, the formal parameter, the., and the body. Consider the lambda abstraction, λx+ x y. In order to evaluate the function, for a particular input parameter x, we need to know the value of y. The variable y in this case is free and x is bound; λ binds x. An occurrence of a variable is bound if there is an enclosing lambda abstraction that binds the variable, and is free otherwise. Example 4 Some example lambda abstractions and their bound and free variables. 1. λx.x is the identity function. Here x is a bound variable. 2. λx.y is the constant y function. Here y is a free variable. 3. λx.x y has x as a bound variable and y as a free variable. 4. λx.(λy.x y) has both variables bound. 5. x λx. + x 1. In this term the first occurrence of x is free and the second is bound. We define the set of free variables of a term, t, as the set of variables that occur free in the term and denote it with F V (t). For example, F V (+ x λx. + x 1) = x}. Formally, we define the set of free variables of a term as follows. Definition 5 (Free Variables) The set of free variables of a term t is F V (t) V is defined recursively as follows 1. If x V then F V (x) = x} 2. If t 1, t 2 T then F V (t 1 t 2 ) = F V (t 1 ) F V (t 2 ) 3. If t T and x V then F V (λx.t) = F V (t) \ x} Now that we have a definition of the set of free variables in a term, we can distinguish between two terms such as λx.x and λx. + xy. Definition 6 (Closed and Open Terms) A term t is closed if F V (t) =. Otherwise t is open. 3

4 Substitution A lambda abstraction denotes a function. How do we evaluate a function or a lambda abstraction at a particular value? For example, the term (λx. + 1 x)2 denotes the application of (λx. + 1 x) to the parameter 2. To evaluate such a term we would like to replace the occurrences of the formal parameter x with the value 2. For example, for (λx. + 1 x) 2 evaluates to (+ 1 2). This is an example substitution, where we substitute the value 2 for the variable x in (λx. + 1 x). Formally, we define substitution as follows. Definition 7 The substitution of a term, t for a variable x V in a term t, denoted by [t /x]t, is an instance of t where all the free occurrences x is replaced by the term t. Throughout this course, we will use the notation [t /x]t to denote a substitution of t for x in t. Different notations are preferred by different textbooks or authors. Two other commonly used notations are t[t /x] and [x := t ]t. Pierce s book uses the notation [x t ]t. Example 8 Some example substitutions. 1. [λy.y/x](x x) = (λy.y) (λy.y) 2. [λy.y/x](λx.x) = λx.x (x is not free.) 3. [y/x](λz.x) = λz.y A substitution, as we defined it, is actually incorrect. For example, consider the lambda abstraction λy.x, the constant function, and the substitution [y/x](λy.x), which is equal to λy.y, the identity function. Thus, the substitution would naively change the constant function into the identity function. Here, the problem is that the variable x, a free variable, is substituted with y, which then becomes bound by the lambda abstraction. In this case, we say that y is captured in the substitution. We do not want to transform free variables into bound variables during the substitution process. We need to redefine substitution to avoid capture. What can we do? Consider the following situation: given a program which has a function that takes a variable x and has a variable y in its body, we can change the name of y to z without any modification of the results, but we cannot change the name of y to x without adversely affecting the results. The next definition avoids capture. Definition 9 (Capture Avoiding substitution) t if y = x [t/x]y = y if y x 4

[t /x](λy.t) = [t/x](t 1 t 2 ) = [t/x]t 1 [t/x]t 2. λy.t if x = y (y is bound) λy.[t /x]t if x y and y F V (t ) Question: What does the substitution [λy.x y y/z](λx.x z) yield? Solution: This substitution is undefined. Question: How about the substitution [λy.x y y/z](λy.y z) Solution: λy.y (λy.x y y) According to this definition a substitution that causes capture is undefined. But something is just not right, because the lambda abstractions λx.x z and λy.y z differ only in the name of their bound variables. Lambda abstractions denote functions, thus these two lambda abstractions are the same; it should not matter what the bound variables are named. 5 Alpha Conversion and Alpha Equivalence One way to avoid capture is to rename the bound variables in a lambda abstraction. To goal is to ensure that no bound variable has the same name as a free variable in the term being substituted. This process of renaming the bound variables is called α-conversion or α- variation. Two terms that are reducible to each other by α-conversions are alpha equivalent. We denote α-equivalent terms t 1 and t 2 as t 1 = α t 2. 1. λx.x = α λy.y. 2. λx.λy.λz.x y z = α λz.λy.λx.z y x. 3. λx.t = α λy.[y/x]t if y F V (t). Exercise: Show that = α is an equivalence relation. Alpha conversion gives us a way to define substitution without worrying about capture. Definition 10 (Substitution with Explicit Alpha Conversion) t if y = x [t/x]y = y if y x [t /x](λy.t) = [t/x](t 1 t 2 ) = [t/x]t 1 [t/x]t 2. λy.t if x = y λz.[t /x][z/y]t if x y z F V (t) F V (t ) 5

The idea of this definition is to rename formal variable of the lambda abstraction so that it does not occur freely in t. This ensures that no free variables of t are captured. This is not enough, however, because we also have to make sure that we do not capture the free variables of t itself. Note that, in this definition, substitution is a relation not a function. That is the result of a substitution is a set of terms where the renaming take different forms. More precisely, the variable z can take many values (the names of difference variables). All such terms are α-equivalent. Now that we made precise the idea of alpha conversion, we can now forget about it. From now on, we will work modulo α-equivalence. That is, we will not distinguish between two terms that are α-equivalent. We can use our old definition, with implicit alpha conversion applied as required. Definition 11 (Substitution with Implicit Alpha Conversion) t if y = x [t/x]y = y if y x [t/x](t 1 t 2 ) = [t/x]t 1 [t/x]t 2. [t /x](λy.t) = 6 β-reduction λy.t if x = y (y is bound) λy.[t /x]t if x y y F V (t ) In Section 5, we briefly discussed how to evaluate a function with a given parameter by substitution. We can give an operational semantics for lambda terms based on substitution. The idea is to take each application and reduce it to another term by applying substitution this is called β reduction, and is denoted β. Definition 12 (Single-step β-reduction) (λx.t 1 ) t 2 β [t 2 /x]t 1 t 1 β t 1 t 1 t 2 β t 1 t 2 t 2 β t 2 t 1 t 2 β t 1 t 2 t β t λx.t β λx.t 6

We define multi-step beta reduction, denoted β, as 0 or more applications of single-step beta reduction rules. The use of to denote reductions suggests their use in Kleene Algebra of Automata Theory. Definition 13 (Multi-step β-reduction) t β t t β t t β t t 1 β t 2 t 2 β t 3 t 1 β t 3 Two terms that are β-reducible to each other are called β-equivalent, denoted by = β. Definition 14 (β-equivalence) t = β t 7 Homework Exercise t β t t = β t t 1 = β t 2 t 2 = β t 3 t 1 = β t 3 t = β t t = β t A term t is in normal form if there is no t such that t β t. We say that a term t is normalizable if there is some t such that t β t and t is in normal form. Answer the following questions and prove your answer (if your answer is yes, an example suffices). 1. Are there any normalizable terms? 2. Are there any non-normalizable terms? 7