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

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

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

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.

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

CMSC 330: Organization of Programming Languages

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

CIS 500 Software Foundations Fall September 25

CSE 505: Concepts of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Type Systems Winter Semester 2006

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

CMSC 330: Organization of Programming Languages

Concepts of programming languages

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CIS 500 Software Foundations Midterm I

Exercise 1 ( = 24 points)

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

More Lambda Calculus and Intro to Type Systems

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

Introduction to the λ-calculus

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

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

Introductory Example

Exercise 1 ( = 18 points)

Lambda Calculus as a Programming Language

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

CSE-321 Programming Languages 2010 Midterm

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

Lecture 5: The Untyped λ-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

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

Pure Lambda Calculus. Lecture 17

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.

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

Graphical Untyped Lambda Calculus Interactive Interpreter

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

Lambda Calculus as a Programming Language

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

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

More Lambda Calculus and Intro to Type Systems

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

Exercise 1 ( = 22 points)

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

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

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

Costly software bugs that could have been averted with type checking

CMSC330. Objects, Functional Programming, and lambda calculus

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

The University of Nottingham

An Introduction to the Lambda Calculus

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

Exercise 1 (2+2+2 points)

Untyped Lambda Calculus

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Denotational Semantics. Domain Theory

Untyped Lambda Calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CMSC 330: Organization of Programming Languages. Lambda Calculus

dynamically typed dynamically scoped

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

The Untyped Lambda Calculus

Elixir, functional programming and the Lambda calculus.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Functional Programming. Overview. Topics. Recall λ-terms. Examples

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

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Fundamentals and lambda calculus

Accurate Step Counting

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Polymorphism and System-F (OV)

Programming Languages

Denotational semantics

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

6.001 Notes: Section 8.1

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

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

Functional Programming

Functional Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

301AA - Advanced Programming [AP-2017]

Programming Languages

Accurate Step Counting

Functional Languages. Hwansoo Han

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 320: Concepts of Programming Languages

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

Introduction to the Lambda Calculus. Chris Lomont

Transcription:

CITS3211 FUNCTIONAL PROGRAMMING 10. Programming in the pure λ calculus Summary: This lecture demonstates the power of the pure λ calculus by showing that there are λ expressions that can be used to emulate some basic features of Haskell, namely definitions, booleans, pairs, lists, numbers and recursion. R.Davies 2003 7.

Pure λ calculus In the λ calculus we can use built in constants to represent primitive Haskell values like True, False, 0, 1, 2,..., ==, <, etc. We can even represent if... then... else... as a built in constant that is a primitive function taking three parameters: if True E E' E if False E E' E' This is what we'll do in later lectures when we consider implementing interpreters and compilers for Haskell. In this lecture we'll see that such constants are not essential. Instead the λ calculus is powerful enough that we can implement Booleans, pairs, numbers, lists, etc. and even recursion. When we restrict ourselves to λ expressions without constants we say that we are using the pure λ calculus. Historically, the pure λ calculus was studied first, by Alonzo Church in 1932 as a simple language that could express every computable function. CITS3211 Functional Programming 2 10. Programming in the pure λ calculus

λ Expressions for Definitions We want to be able to write code in the pure λ calculus that is structured like a Haskell program. Suppose our program has a bunch of definitions. E.g. f x y = x g z = z Now, suppose we want to evaluate an expression. E.g. f g g We can turn the expression we want to evaluate into a function that takes expressions for f and g as arguments. λfg. f g g Then, the translation of the whole program is the application of this function to the translation of the definitions. (λfg. f g g) (λxy. x) (λz. z) The same technique can be used to translate local definitions (i. e. where definitions) into the λ calculus. CITS3211 Functional Programming 3 10. Programming in the pure λ calculus

Booleans We want to implement True, False and if as closed λ expressions in such a way that: if True E E' β E if False E E' β E' Everything is a function in the pure λ calculus, so True and False must be some kind of functions. We can make True and False the functions that take two arguments and select the first/second argument. True = λxy.x False = λxy.y Then if is a higher order function that applies the first argument to the second and third. if = λbxy. b x y E.g. if True E E' = (λxyz. x y z) (λxy.x) E E' β ( λyz. (λxy.x) y z) E E' β E Evaluation is done by β-reduction. CITS3211 Functional Programming 4 10. Programming in the pure λ calculus

Pairs To program with pairs, we need at least an operation mkpair to construct a pair from two expressions, and fst and snd to extract parts from a pair. As before, in the λ calculus pairs must be represented as some kind of functions. We can consider a pair to be a function that takes a Boolean as input, and returns either the first or second part of the pair. mkpair E E' True = E mkpair E E' False = E' Then we can represent mkpair by the λ expression: mkpair = λxyb. b x y fst and snd each apply the pair to a selector function (i.e. True or False): fst = λz.z (λxy.x) Then we can use β-reduction to show: fst (mkpair E E') β E snd (mkpair E E') β E' snd = λz.z (λxy.y) CITS3211 Functional Programming 5 10. Programming in the pure λ calculus

Lists Lists are built using two constructor functions. Nil for the empty list ([]) Cons h t for the list with head h and tail t (h:t) We can represent lists using λ expressions by combining the techniques from Booleans and pairs. Nil is just the same selection function as True. Nil = λxy.x Cons h t is like the other select, but also combines features of similar to the pair (h, t). Cons h t = λxy. y h t Cons = λhtxy. y h t Compare this to: False = λxy.y mkpair = λxyz. z x y To check whether a list is empty, we can use the following function: null = λl.l True (λht. False) To take the head and tail of a list, or return a default d if the list is empty, we can use the following functions: head = λld. l d (λht. h) tail = λld. l d (λht. t) CITS3211 Functional Programming 6 10. Programming in the pure λ calculus

Numbers As a first step in representing numbers using λ expressions, we show an inefficient representation of just the natural (non negative) numbers. Every natural number is either zero, or it is some other natural number plus one. In Haskell we could represent natural numbers using the data type: data Nat = Zero Successor Nat Zero is just the same selection function as True. Zero = λxy.x Successor a should be the other select, but also applies the second argument to a. Successor a = λxy. y a Successor = λaxy. y a To check whether a number is zero, we can use the following function: iszero = λn. n True (λm. False) To find the predecessor of a number or return default d if the number is zero, we can use the following function: predecessor = λnd. d (λm. m) We can then write recursive functions for +, -, *, div, ==, <, >, etc. CITS3211 Functional Programming 7 10. Programming in the pure λ calculus

Recursion We can't write many interesting programs with lists or numbers unless we have recursion. E.g. we can't write the map function (or + or == for natural numbers). But the λ calculus doesn t allow recursive definitions Is there a way to emulate recursive definitions in the λ calculus? If we have a recursive definition like f = E with E already converted to a λ expression, then λf. E is a function that captures one step of the recursion, using the parameter f if further steps are required. Is there a λ expression Y that repeatedly applies a λ expression f to itself? Y f β f (Y f) β... β f (f (f (f...) ) ) Yes. Let Y be the following λ expression. Y = λf. (λx. f (x x)) (λx. f (x x)) This is all we need to write recursive functions using pure λ expressions. CITS3211 Functional Programming 8 10. Programming in the pure λ calculus

Why Y? Why do we need Y? Suppose we try to translate the following Haskell code for the map function into a pure λ calculus expression. map f [] = [] map f (h:t) = (f h) : (map f t) We might try to translate this to the following expression for map. λf l. l Nil (λh t. Cons (f h) (map f t)) But, this isn't a λ calculus expression unless we know what term the map stands for! We know what Nil and Cons stand for, so the above expression is an abbreviation for: λf l. l (λxy.x) (λh t. (λabxy.y a b) (f h) (map f t)) But we don't know what to replace the map with. We'd like it to recursively stand for the whole expression! CITS3211 Functional Programming 9 10. Programming in the pure λ calculus

Recursion using Y This is where we need to use Y. To map we add an extra parameter m. λm f l. l (λxy.x) (λh t. (λabxy.y a b) (f h) (m f t)) This is definitely a pure λ expression, so let's give it a name, and go back to using the abbreviations Nil, Cons. mapstep = λm f l. l Nil (λh t. Cons (f h) (m f t)) Now, mapstep doesn't do what we want unless we supply the argument m, which needs to be a function to do a map. Applying Y to mapstep does exactly this. Y mapstep β mapstep (Y mapstep)... β mapstep (mapstep (mapstep (...))) So, this pure λ expression does what we want, so lets call it map. map = Y mapstep Note: Y is equivalent to the recursive Haskell function y f = f (y f) Also, Y is known as a fixed point combinator, because it finds an object that f maps to itself. Such fixed points play a key role in mathematical semantics of programs and an in the study of computability. CITS3211 Functional Programming 10 10. Programming in the pure λ calculus