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

Similar documents
Functions. Massimo Merro. 24 October Massimo Merro Functional language 1 / 1

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

VU Semantik von Programmiersprachen

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

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

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

The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages

Lambda Calculus. Lecture 4 CS /26/10

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

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

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

Recursive Definitions, Fixed Points and the Combinator

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

Chapter 5: The Untyped Lambda Calculus

Formal Systems and their Applications

Lecture 5: The Untyped λ-calculus

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

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

Chapter 5: The Untyped Lambda Calculus

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

Fundamentals and lambda calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

3.1 λ-calculus: Syntax

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Introduction to the λ-calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Type Systems Winter Semester 2006

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

dynamically typed dynamically scoped

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

1 Scope, Bound and Free Occurrences, Closed Terms

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

Untyped Lambda Calculus

Programming Languages

CIS 500 Software Foundations Fall September 25

Untyped Lambda Calculus

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

The Untyped Lambda Calculus

Pure Lambda Calculus. Lecture 17

Concepts of programming languages

Lambda Calculus.

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

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

Lambda Calculus. Lambda Calculus

Lexicografie computationala Feb., 2012

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

CMSC330. Objects, Functional Programming, and lambda calculus

CITS3211 FUNCTIONAL PROGRAMMING

CSE 505: Concepts of Programming Languages

Introduction to the Lambda Calculus

Organization of Programming Languages CS3200/5200N. Lecture 11

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Implementing functional languages with abstract machines

Introduction to the Lambda Calculus. Chris Lomont

Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus. Variables and Functions. cs3723 1

Week 4: Functional Programming

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...

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CSCI.4430/6969 Programming Languages Lecture Notes

Introduction. chapter Functions

Functional Programming

Elixir, functional programming and the Lambda calculus.

An Introduction to the Lambda Calculus

Lambda Calculus LC-1

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

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

Programming Language Concepts: Lecture 19

Part I. Historical Origins

Lambda Calculus and Type Inference

Lambda Calculus and Extensions as Foundation of Functional Programming

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

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

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

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Programming Languages Third Edition

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

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

Denotational Semantics. Domain Theory

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.

CS522 - Programming Language Semantics

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

Functional Languages. Hwansoo Han

1 Introduction. 3 Syntax

Functional Programming

Shell CSCE 314 TAMU. Haskell Functions

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Transcription:

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

The core of sequential programming languages In the mid 1960s, Peter Landin observed that a complex programming language can be understood by focussing on a tiny core calculus capturing the language s essential mechanisms...... together with a collection of convenient derived constructs whose behaviour is understood by translating them into the core calculus. The core language used by Landin was the λ-calculus, a formal system invented by Alonzo Church in the 1930 s as a universal language of computable functions. In 1960, John McCarthy published the design of the programming language Lisp based on the λ-calculus. Since then, the λ-calculus has seen a widespread use in the specification of programming language features in language design and implementation in the study of type systems. Massimo Merro The Lambda language 2 / 21

Expressiveness of the λ-calculus The λ-calculus can be viewed as a very simple programming language in which computations can be described as mathematical objects. It is a formal language in which every term denotes a function any term (function) can be applied to any other term, so functions are inherently higher-order Despite its simplicitiy, it is a Turing-complete language: it can express computations on natural number as does any other known programming language. Church s Thesis: any conceivable notion of computable function on natural numbers is equivalent to the λ-calculus. The force of Church s Thesis is that it postulates that all future notions of computation will be equivalent in expressive power to the λ-calculus. Massimo Merro The Lambda language 3 / 21

Encoding language features in λ-calculus The λ-calculus can be enriched in a variety of ways. It is often convenient to add special constructs for features like numbers, booleans, tuples, records, etc. However, all these features can be encoded in the λ-calculus, so they represent only syntactic sugar. Such extensions lead eventually to programming languages such as Lisp (McCarthy, 1960), ML (Milner et al., 1990), Haskell (Hudak et al., 1992), or Scheme (Sussman and Steele, 1975). In the next slide, we propose the Lambda language a simple extension of the λ-calculus with built-in operators for manipulating natural numbers and Booleans. Massimo Merro The Lambda language 4 / 21

The language Lambda M Lambda ::= n true false x (M 1 + M 1 ) (M 1 M 2 ) x Vars... M 1 and M 2 M 1 or M 2 M 1 = M 2 if M 1 then M 2 else M 3 λx.m M 1 M 2 The constructs in red constitute Churchs λ-calculus M 1 M 2 : apply function M 1 to argument M 2 λx.m: called λ-abstraction, define anonymous function which when applied to data N executes M{ N / x }. Massimo Merro The Lambda language 5 / 21

Examples of anonymous functions λx.x + 3 - from numerals to numerals λf.(f (2)) - from functions to numerals λx.if x = 0 then λy.y else λy.x - from numerals to functions λf.λx.if x = 0 then 1 else (let y = f (x 1)x y in ) - from functions to functions λf.λg.λx.f (g(x)) - from pairs of functions to functions. Massimo Merro The Lambda language 6 / 21

Conventions We recall the grammar given before describes the abstract syntax of the language; when we come to write terms down concretely we will often use brackets, as above, to clarify the structure of terms. So, to keep brackets to a minimum in this part, we will use some conventions for writing λ-terms: applications associates left: MNP means the same as (MN)P the scope of λ extends as far to the right as possible: λx.mn means λx.(mn), not (λx.m)n we sometimes write λxy.m for λx.λy.m. Massimo Merro The Lambda language 7 / 21

Free variables The construct λx.m for function definition is the only binding operator. In λx.m the variable x is bound in M which represents the scope of x. Let us provide a formal definition of free variables by structural induction: fv(x) = {x} fv(n) = fv(true) = fv(false) = fv(m 1 op M 2 ) = fv(m 1 ) fv(m 2 ) fv(m 1 M 2 ) = fv(m 1 ) fv(m 2 ) fv(λx.m) = fv(m) \ {x} Closed terms M is closed if fv(m) = Closed terms corresponds to programs. Massimo Merro The Lambda language 8 / 21

Small-step semantics Judgements: M N where M and N are programs Intuition: M performs one function application or one built-in operation and N remains to be evaluated. Massimo Merro The Lambda language 9 / 21

Semantic rules Standard rules: (S-Left) M 1 M 1 M 1 + M 2 M 1 + M 2 (S-Right) M 2 M 2 n 1 + M 2 n 1 + M 2 (S-Add) Similar rules for, and, if then else,... n 1 + n 2 n 3 n 3 = add(n 1, n 2 ) Massimo Merro The Lambda language 10 / 21

Function application To evaluate M 1 M 2 : First evaluate M 1 to a function λx.m Then it depends on the evaluation strategy. If Call-by-value: evaluate M 2 to a value v v Val ::= n true false λx.n evaluate M{ v / x }. If Call-by-name: evaluate M{ M 2 / x } Massimo Merro The Lambda language 11 / 21

Function application rules (L-App) M 1 M 1 M 1 M 2 M 1 M 2 Call-by-value: M 2 M (L-CBV.A) 2 (λx.m)m 2 (λx.m)m 2 where v Val ::= n true false (L-CBV) λx.n (λx.m)v M{ v / x } Call-by-name (L-CBN) (λx.m)m 2 M{ M 2 / x } where M 2 is a closed term (i.e. a program). Massimo Merro The Lambda language 12 / 21

Substitution of closed terms N closed: x{ N / x } = N y{ N / x } = y if y x (λx.m){ N / x } = λx.m (λy.m){ N / x } = λy.m{ N / x } if y x (M 1 M 2 ){ N / x } = (M 1 { N / x })(M 2 { N / x }) (M 1 op M 2 ){ N / x } = (M 1 { N / x }) op (M 2 { N / x })...... N open: requires α-conversion, i.e. renaming of bound variables. Massimo Merro The Lambda language 13 / 21

Self-application Is it possible to express non-terminating programs in Lambda? Yes, of course! For example, the divergent combinator Ω def = (λx.xx)(λx.xx) contains just one redex, and reducing this redex yields exactly Ω again! Ω Ω Ω...... Ω...... Non-termination is built-in in Lambda! Massimo Merro The Lambda language 14 / 21

Call-by-name vs. call-by-value Note that unlike the Fpl language, having function definitions among the constructs may lead to different results. They give different results: (λx.0)(ω) cbn 0 (λx.0)(ω) cbv (λx.0)(ω) cbv... cbv... Even more surprisingly: (λx.λy.x)(id 0) cbn λy.(id 0) (λx.λy.x)(id 0) cbv λy.0 For different evaluation strategies see Benjamin C. Pierce s Types and Programming Languages at pp. 56. Massimo Merro The Lambda language 15 / 21

Fixpoints In Mathematics, a fixpoint p (also known as an invariant point) of a function f is a point that is mapped to itself, i.e. f (p) = p. Fixpoints represent the core of what is known as recursion theory. Kleene s recursion theorems are a pair of fundamental results about the application of computable functions to their own descriptions. The two recursion theorems can be applied to construct fixed points of certain operations on computable functions, to generate quines 1, and to construct functions defined via recursive definitions. Kleene s recursion theorems is used to prove a fundamental result in computability theory: the Rice s Theorem! Rice s Theorem: For any non-trivial property of partial functions, there is no general and effective method to decide whether an algorithm computes a partial function with that property. 1 A quine is a computer program which produces a copy of its own source code as its only output. Massimo Merro The Lambda language 16 / 21

Fixpoints (via Turing s combinator) So, it is very important to prove that Lambda can express fixpoints. Let us define the two following terms in Lambda: A fix def = λx.λy.y(xxy) def = AA fix is a recursive function that given a term M returns a fixpoint of M, denoted with fix M. In fact, for any term M, using a call-by-name evaluation, we have: fix M (λy.y(aay))m M(fix M). Very often this operator is denoted using the greek letter Θ. Massimo Merro The Lambda language 17 / 21

Example: the factorial function F Fac def = λf.λx.if x = 0 then 1 else x f (x 1) def = ΘF ΘF λx.if x = 0 then 1 else x ΘF (x 1) ΘF 3 (λx.if x = 0 then 1 else x ΘF (x 1)) 3 3 ΘF 2 3 (λx.if x = 0 then 1 else x ΘF (x 1)) 2 3 2 ΘF 1 3 2 1 ΘF 0 3 2 1 1 6 Massimo Merro The Lambda language 18 / 21

Fixpoints (via Curry s combinator) Notice that there are infinitely many forms of fixpoint combinators. Probably, the best known is the one by Curry and most commonly called Y : Y def = λf.(λx.f (xx))(λx.f (xx)) We can easily check that is has the required property: Y M def = ( λf. ( λx.f (xx))(λx.f (xx) )) M ( ) λx.m(xx))(λx.m(xx) M ( (λx.m(xx))(λx.m(xx)) ) = M(Y M) M ( (λx.m(xx))(λx.m(xx)) ) Massimo Merro The Lambda language 19 / 21

Applicative order fixpoint combinator The Curry Y -combinator does not work in languages like Lisp, because of the applicative order (call-by-value) reduction strategy: Y M will keep evaluating forever. A different fixpoint combinator does work, though: the applicative order fixpoint combinator which is λf.(λg.gg)(λx.f (λa.xxa)) or alternatively λf.(λx.f (λa.xxa))(λx.f (λa.xxa)) Exercise Check that this is a fixpoint combinator. Notice that it only differs from Curry s Y by replacing terms of the form xx with λa.xxa. Inserting this λ stops Lisp s evaluation mechanism from getting into an infinite loop. Massimo Merro The Lambda language 20 / 21

Some fun: Klop s fixpoint combinator Jan Klop came up with this ridiculous one: if we define where then L def = λabcdefghijklmnopqstuvwxyzr.r(thisisafixedpointcombinator) λabcdef... means λa.λb.λc.λd.λe.λf.... thisisafixe... means ((((((((((th)i)s)i)s)a)f )i)x)e)... LLLLLLLLLLLLLLLLLLLLLLLLLL is indeed a fixpoint combinator. (26 times) Exercise Check that Klop s combinator works. Hint: the phrase this is a fixed point combinator contains 27 letters. Massimo Merro The Lambda language 21 / 21