Introduction to the Lambda Calculus. Chris Lomont

Similar documents
Pure Lambda Calculus. Lecture 17

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

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

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

Introduction to the Lambda Calculus

Lambda Calculus. Variables and Functions. cs3723 1

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

Lambda Calculus. Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

VU Semantik von Programmiersprachen

Lambda Calculus. Lecture 4 CS /26/10

Lexicografie computationala Feb., 2012

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

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

CMSC 330: Organization of Programming Languages

Introduction to the λ-calculus

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.

Functional Languages. Hwansoo Han

CITS3211 FUNCTIONAL PROGRAMMING

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

CMSC 330: Organization of Programming Languages

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

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

Part I. Historical Origins

Elixir, functional programming and the Lambda calculus.

Concepts of programming languages

Functional Programming

3.1 λ-calculus: Syntax

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

Untyped Lambda Calculus

Programming Proofs and Proving Programs. Nick Benton Microsoft Research, Cambridge

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

CMSC 330: Organization of Programming Languages

Untyped Lambda Calculus

THE HALTING PROBLEM. Joshua Eckroth Chautauqua Nov

CMSC 330: Organization of Programming Languages. Lambda Calculus

Fundamentals and lambda calculus

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Programming Language Concepts: Lecture 19

CS 242. Fundamentals. Reading: See last slide

Advanced Seminar Softwareengineering - Denotational semantics

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

Recursive Definitions, Fixed Points and the Combinator

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

Chapter 11 :: Functional Languages

Type Systems Winter Semester 2006

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

Lambda Calculus and Type Inference

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

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

Programming Languages Third Edition

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

Functional Programming

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

Functional Programming. Big Picture. Design of Programming Languages

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

Programming Languages

Programming Language Pragmatics

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

Introduction to the l-calculus

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

λ calculus is inconsistent

Formal Systems and their Applications

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and recursion.

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Less naive type theory

Lecture 5: The Untyped λ-calculus

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

dynamically typed dynamically scoped

CSE 505: Concepts of Programming Languages

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

Grades. 10% - participation & exercises. 90% - term paper

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

The Typed λ Calculus and Type Inferencing in ML

1.3. Conditional expressions To express case distinctions like

Logical Verification Course Notes. Femke van Raamsdonk Vrije Universiteit Amsterdam

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

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

Introduction to Computer Science

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

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

Higher-Order Logic. Specification and Verification with Higher-Order Logic

Lambda Calculus and Type Inference

Lecture 5: The Halting Problem. Michael Beeson

CMSC 330: Organization of Programming Languages

CIS 500 Software Foundations Fall September 25

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

Transcription:

Introduction to the Lambda Calculus Chris Lomont 2010 2011 2012 www.lomont.org

Leibniz (1646-1716) Create a universal language in which all possible problems can be stated Find a decision method to solve all problems stated in the universal language

The Language Set theory coupled with first order logic A formal logical system used in mathematics, philosophy, linguistics, and computer science Many names: first-order predicate calculus, the lower predicate calculus, quantification theory, and predicate logic Different from propositional logic by its use of quantifiers Distinguished from higher-order logics in that quantification is allowed only over atomic entities (individuals but not sets) Frege and Russell Zermelo early 1900 s Russell paradox 1901 ends naïve sets: R = {x x x} then R R R R

Can all problems be solved? Among Hilbert s 23 problems, 1903 Problems 3, 7, 10, 11, 13, 14, 17, 19, 20, and 21 have a resolution that is accepted by consensus. Problems 1, 2, 5, 9, 15, 18+, and 22 have solutions that have partial acceptance, but there exists some controversy as to whether it resolves the problem. (18 is Kepler conjecture, requires computer proof) 8: the Riemann hypothesis, 12: abstract number theory 4, 6, 16, and 23 are too vague to ever be described as solved. 2 nd problem: Prove that the axioms of arithmetic are consistent. Continuing Leibniz dream

Can arithmetic be proven consistent? No! Godel Incompleteness Theorems (1931) First: Any effectively generated theory capable of expressing elementary arithmetic cannot be both consistent and complete. Gödel's second incompleteness theorem, proved in 1931, shows that no proof of its consistency can be carried out within arithmetic itself. For any formal effectively generated theory T including basic arithmetical truths and also certain truths about formal provability, T includes a statement of its own consistency if and only if T is inconsistent. Leads to Halting Problem in computer science (Turing, 1936) Someday we ll talk about the Turing Hierarchy

What can then be solved automatically? Independently: Alonzo Church and Alan Turing, 1936 Formalized the notion of decidable, which is equivalent to what is computable. Church (1936) invented the lambda calculus, written λ-calculus. A calculus is a method of moving symbols, not the undergrad course. Turing (1936/37) invented Turing Machines. Turing showed them to be equivalent.

Outcome Modern machines are based on Turing Machines (actually, von Neumann machines, allowing random memory access). Modern computer languages are based on concepts from lambda calculus, which allow telling such a machine what to do. Imperative languages (Pascal, C, Fortran) are based on the way Turing machine are instructed Functional languages like Miranda, Lisp, ML, Haskell, F#, are based on lambda calculus.

Reduction Functional program consists of Expression E (represents both algorithm and input) This is subject to rewrite rules Reduction: replace a part P of E by some P, Provided that P P is one of the rules E P E P

Example: first of (sort (append (`dog', `rabbit') (sort ((`mouse', `cat'))))) ->first of (sort (append (`dog', `rabbit') (`cat', `mouse'))) ->first of (sort (`dog', `rabbit', `cat', `mouse')) -first of (`cat', `dog', `mouse', `rabbit') -> `cat':

Properties Church-Rosser property: normal form is independent of order of rewrite rules Rewrite rules are called combinators

λ-calculus Two operations: application and abstraction Application F A or FA is applying data F as an algorithm to data A considered as input. Theory is type-free, so FF makes sense. Abstraction If M[x] is expression containing x, then the function λx. M[x] represents the function x M[x] Example: λx. 2x + 1 3 = 2 3 + 1 (= 7) Called the smallest programming language of the world. Introduced in 1930 by Alonzo Church to formalize the notion of computability.

Abstraction Binds the free variable x in M, that is, λx. xy has x bound and y free.

Multiple arguments: f(x, y) depends on 2 variables. Can define F x = λy. f x, y F = λx. F x Then Fx y = F x y = f(x, y) Leads to association: FM 1 M 2 M n = ( (FM 1 ) M 2 ) M n ) Called currying, after Haskell Curry Haskell computer language named after him

Notation Definition 1: λ-terms: the set Λ built from the infinite set of variables Λ = using application and abstraction: x V x Λ M, N Λ MN Λ M Λ, x V λxm Λ We use lowercase x, y, z for variables and caps M, N, L for Λ-terms v, v, v,

Notation Definition 2: The free variables in M, written FV(M) is FV x = {x} FV MN = FV M FV N FV λxm = FV M {x} A not-free variable is bound. M is a closed λ-term (also called a combinator) if FV M = Definition 3: The result of substituting N for the free occurrences of x in M is denoted M[x N]

Λ Calculus The principal axiom is λxm N = M x N Logical rules and axioms: Equality Compatibility M = M M = N N = M M = N, N = L M = L M = M MZ = M Z M = M ZM = ZM M = M λx. M = λx. M Proof: if M = N is provable in the λ calculus, write λ M = N

Standard Combinators Name Definition Action on terms Identity I = λx. x IM = M Left Right K = λxy. x K = λxy. y KMN = M KMN = N S = λxyz. xz(yz) SMNL =ML(NL) Used to solve equations

Reduction and Normality Three reduction types: α reduction: renaming a bound variable, e.g., λx. x λy. y β reduction: applying functions to arguments, e.g. λz. z 2 5 5 2 η reduction: extensionality, that is, two functions are the same if and only if they give the same value for all arguments.

Normal Form Expression cannot be rewritten any further. Not all expressions have this property: Consider λx. xxx. Applied to itself leads to (TODO keeps expanding) Type equation here. TODO state it TODO can always be done TODO expand outermost evaluation first TODO some expressions never terminate, have no normal form.

Arithmetic Define F 0 M M, F n+1 M F n M Church numerals: c 0, c 1, c 2, defined via c n λfx. f n (x) Operations: A + λxypq. xp ypq A λxyz. x yz A exp λxy. yx Then, for all nonnegative integers n, m A + c n c m = c n+m A c n c m = c n m A exp = c n m A + c n c m =

Booleans true K ( λxy. x), false K λxy. y If B can be considered a boolean (i.e., true or false) then If B then P else Q can be represented by BPQ Ordered pair: M, N λz. if z then M else N( λz. zmn)

Y-Combinator Creates recursion from single steps It is a function Y such that for any function g, Y(f)=g(Y(g)). Y finds fixed-points for functions, Y(g)=p where p=g(p) Does it exist? Yes, there are infinitely many. Y-Combinator Y = λf.(λx.f (x x)) (λx.f (x x)) Another for amusement: Y k = (L L L L L L L L L L L L L L L L L L L L L L L L L L) with L = λabcdefghijklmnopqstuvwxyzr. (r (t h i s i s a f i x e d p o i n t c o m b i n a t o r))

Y-Combinator Definition: Y = λf.(λx.f (x x)) (λx.f (x x)). Apply to arbitrary function g Y g= (λf. (λx. f (x x)) (λx. f (x x))) g (by definition of Y) = (λx. g (x x)) (λx. g (x x)) (β-reduction of λf: applied main function to g) = (λy. g (y y)) (λx. g (x x)) (α-conversion: renamed bound variable) = g ((λx. g (x x)) (λx. g (x x))) (β-reduction of λy: applied left function to right function) = g (Y g) (by second equality) Repeat, giving g (g (g (g ( Y g)))) as deep as desired Recursion.

TODO Y-Combinator Y = λf.(λx.f (x x)) (λx.f (x x)) Church numerals, add, exponent, True, false, pairs Definability, recursive functyions, all computable functions. Reduction

Types So far the λ-calculus is a type free theory Example, I = λx. x can be applied to itself: II Typed λ-calculus introduced in Curry(1934) and Church (1940). Type is an thing assigned to a term. Type may be thought of as a units dimension, prevents adding meters to mass.

Type benefits Allows easier checking for correctness Allows easier compilation and running (if type of an expression is arithmetical, can use ALU instead of generalized code). Implicit typing, in ML, assigned by compiler Explicit typing, common, assigned by programmer Not all computable functions can be represented by a typed term!

Typing Results Worked out in theory a long time ago Computer languages keep playing with various balances between the various flavors of typing.

C# Lambda Functions (x, y) => x == y TestDelegate mydel = n => { string s = n + " " + "World"; Console.WriteLine(s); }; mydel("hello");

C++0x Lambda Functions [](int x, int y) { return x + y; } std::vector<int> some_list; int total = 0; std::for_each(some_list.begin(), some_list.end(), [&total](int x) { total += x; });

F# Lambda Functions

Bibliography [1] Introduction to Lambda Calculus, Henk Barendregt and Erik Barendsen, March 2000. [2] Wikipedia [3] http://en.wikipedia.org/wiki/currying [4] http://www.inf.fu-berlin.de/lehre/ws03/alpi/lambda.pdf, A Tutorial Intorduction to the Lambda Calculus