λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Similar documents
Introduction to the λ-calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

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

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

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

Lecture 5: The Untyped λ-calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

The Untyped Lambda Calculus

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

The University of Nottingham

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus as a Programming Language

The Untyped Lambda Calculus

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

T ::=Nat T T. zero : Nat. This rule can be instantiated for any type T, so it is actually a family of rules, one for each type.

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

Programming Languages

dynamically typed dynamically scoped

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.

Lambda Calculus and Computation

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

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

Graphical Untyped Lambda Calculus Interactive Interpreter

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

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

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

Lambda Calculus as a Programming Language

Type Systems Winter Semester 2006

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

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

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

1.3. Conditional expressions To express case distinctions like

CIS 500 Software Foundations Fall September 25

Introduction to the Lambda Calculus. Chris Lomont

CMSC330. Objects, Functional Programming, and lambda calculus

10.6 Theoretical Foundations

Organization of Programming Languages CS3200/5200N. Lecture 11

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

LECTURE 16. Functional Programming

λ calculus is inconsistent

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

The λ-calculus. 1 Background on Computability. 2 Some Intuition for the λ-calculus. 1.1 Alan Turing. 1.2 Alonzo Church

Getting Started with Haskell (10 points)

Denotational Semantics. Domain Theory

THE HALTING PROBLEM. Joshua Eckroth Chautauqua Nov

The Untyped Lambda Calculus

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

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

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

Chapter 5: The Untyped Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lambda Calculus. Lecture 4 CS /26/10

3.1 λ-calculus: Syntax

Getting Started with Haskell (10 points)

Introductory Example

Lambda Calculus and Type Inference

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.

Functional Programming

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

More Lambda Calculus and Intro to Type Systems

Programming Languages!

Introduction to the Lambda Calculus

Chapter 11 :: Functional Languages

Formal Systems and their Applications

Kurt Gödel and Computability Theory

CSE 505: Concepts of Programming Languages

4/19/2018. Chapter 11 :: Functional Languages

Untyped Lambda Calculus

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

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

CIS 500 Software Foundations Midterm I

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus. Variables and Functions. cs3723 1

Fundamental Concepts. Chapter 1

Lambda Calculus and Extensions as Foundation of 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...

Lambda Calculus-2. Church Rosser Property

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

Untyped Lambda Calculus

CSE-321 Programming Languages 2010 Midterm

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

Functional Programming Languages (FPL)

Functional Languages. Hwansoo Han

Lambda Calculus. Lambda Calculus

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

Polymorphism and Type Inference

Transcription:

λ-calculus Lecture 1 Venanzio Capretta MGS 2018 - Nottingham

Table of contents 1. History of λ-calculus 2. Definition of λ-calculus 3. Data Structures 1

History of λ-calculus

Hilbert s Program David Hilbert (1928) asked the questions Kurt Gödel (1931) answered the first question negatively Incompleteness Theorems There is no complete axiomatization of Arithmetic Consistency of a formal system is not provable inside the system itself Is there a complete consistent formalization of all Mathematics? Is there an effective procedure to determine if a formula is provable? (Entscheidungsproblem, the Decision Problem) 2

Computability Decision Problem is there an effective procedure to decide if a formula is provable? What is an effective procedure? An algorithm that takes a logical formula as input, performs a finite number of computation steps, returns Yes if the formula is provable, No otherwise. There was no precise notion of algorithm or computation yet at the time There were no electronic computers Computers where human beings performing routine calculations (like the Harvard computers or the NACA computer pool) The notion of computability had to be defined in a purely mathematical way 3

The Harvard Computers 4

Origin of the λ-calculus Alonzo Church (1936) invents the λ-calculus A theory of pure functions The first complete model of computation A negative solution to the Decision Problem: there is no algorithm to decide every mathematical formula Alan Turing (1936, few months after Church) solves the same problem using machines Undecidability of the Halting Problem The Decision Problem is undecidable Equivalence of λ-calculus and Turing machines 5

Definition of λ-calculus

Definitions In high-school we learn to define functions with a mapping notation. A function on natural numbers can be defined as: x f x 2 +3 meaning: f is the function that takes an input, which we call x, and returns as output its square plus three. Notation for functional abstraction: f = λx.x 2 +3. Once defined, a function can be applied to any argument: f(5) in high-school notation. Notation for function application: (f 5). We compute the result by replacing the variable x with 5 and performing the operations: (f 5) = ((λx.x 2 +3) 5) 5 2 +3 28 6

Formal Definition In the language of λ-calculus we use only variables, abstraction and application. No need for numbers, 3 and 5, or operations, squaring and addition. Everything can be realized by pure functions. Formal definition of λ-terms: term ::= x y z variable names (λx. term) abstraction (term term) application (We omit unnecessary parentheses: abstraction associates to the right, application associates to the left.) The only computation rule is β-reduction: ((λx.t) u) t[x := u] (in term t, substitute every free occurrence of x with u) 7

Examples of λ-terms identity: id = λx.x The function that returns its input unchanged id a = (λx.x) a x[x := a] = a first projection: proj 1 = λx.λy.x = (λx.(λy.x)) The function that, when applied to two arguments, returns the first proj 1 a b = ((proj 1 a) b) = (((λx.(λy.x)) a) b) ((λy.a) b) a second projection: proj 2 = λx.λy.y The function that returns the second argument proj 2 a b b composition: comp = λu.λv.λx.u (v x) The function computes the composition of two functions (comp g f) a g (f a) 8

Data Structures

Booleans So far we only defined simple functions that perform projections and applications of their arguments How can we represent mathematical objects and data structures? idea: To represent a set of objects, choose a separate λ-term for each object. Make uniform choices that make it easy to define basic operations. For the Booleans, we just need to pick two different terms. Let s reuse the terms for first and second projection. Booleans: true = λx.λy.x false = λx.λy.y Underlying idea: a Boolean is a way of choosing between two arguments. This makes it easy to define conditional choice if then else. 9

Conditionals and Connectives Since the Booleans are just first and second projection, the if then else operation is just the application of the Boolean test to the two branches: if = λb.λu.λv.b u v When the variable b is instantiated with the Boolean value, the term reduces to the correct branch: if true t 1 t 2 = (λb.λu.λv.b u v) true t 1 t 2 true t 1 t 2 = (λx.λy.x) t 1 t 2 t 1 if false t 1 t 2 t 2 The Boolean connectives can be realized either as applications of the conditional or directly: and = λa.λb.if a b false and = λa.λb.a b false or 10

Church Numerals To represent the Natural Numbers, we must choose infinitely many distinct terms One way to do it is with Church Numerals: 0 = λf.λx.x 1 = λf.λx.f x 2 = λf.λx.f (f x) 3 = λf.λx.f (f (f x)). n = λf.λx. f ( (f x) ) }{{} n times The numeral n is an iterator: It takes a function f and an argument x and iterates f n times on x. This makes it easy to define functions by recursion. 11

Arithmetic Operations The first simple operation is the successor: just add one extra iteration: succ = λn.λf.λx.f (n f x) Other operations can be defined directly or iterating the previous one: plus = λn.λm.λf.λx.n f (m f x) or plus = λn.n succ mult = λn.λm.λf.n (m f) or mult = λn.λm.n (plus m) 0 exp = λn.λm.n m (isn t this amazing?!) or exp = λn.λm.m (mult n) 1 Exercise: Try to define predecessor, (cut-off) subtraction, factorial. 12

Pairs A pair is an operator that offers two arguments to any function: a,b = λx.x a b Projecting from a pair is done by application to a Boolean: fst = λp.p true snd = λp.p false These definition satisfy: fst a,b a snd a,b b But the surjective pairing property is not always true: fst p,snd p p (Exercise: find a term p for which fst p,snd p does not reduce to p) There is no encoding of pairs that satisfies surjective pairing. 13

Tuples and Lists Tuples can be defined similarly with several arguments or by iterated pairing a,b,c = λx.x a b c or a,b,c = a, b,c a 0,...,a n = λx.x a 0 a n or a 0,...,a n = a 0, a 1,...,a n Lists: we can also use iterated pairs, but we need the empty list. One implementation is this: nil = false cons h t = h,t The basic operations on lists are: [a 0,a 1,...,a n ] = a 0, a 1, a n,false head = fst tail = snd isnil = λl.l (λh.λt.λx.false) true Exercise: Define a representation of lists as iterators, in the same style as Church Numerals. 14