Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.

Slides:



Advertisements
Similar presentations
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Advertisements

CS 345: Chapter 9 Algorithmic Universality and Its Robustness
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
Foundations of Programming Languages: Introduction to Lambda Calculus
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
Turing Machines CS 105: Introduction to Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Lecture 14: Church-Turing Thesis Alonzo Church ( ) Alan Turing ( )
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
1 CO Games Development 2 Week 21 Turing Machines & Computability Gareth Bellaby.
Introduction to CS Theory Lecture 15 –Turing Machines Piotr Faliszewski
Spring 2006ICOM 4036 Programming Laguages Lecture 2 1 The Nature of Computing Prof. Bienvenido Velez ICOM 4036 Lecture 2.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
Lecture 8: The Meaning of Life, Searching for Truth and The Stuff Primitives are Made Of (and a smattering of Quantum Physics) David Evans
David Evans CS150: Computer Science University of Virginia Computer Science Class 33: Computing with Photons From The.
Cs3102: Theory of Computation Class 14: Turing Machines Spring 2010 University of Virginia David Evans.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 311 Foundations of Computing I Lecture 26 Computability: Turing machines, Undecidability of the Halting Problem Spring
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Dr. Philip Cannata 1 Simple Lisp John McCarthy Alonzo Church David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
Lecture 3: Count Programs, While Programs and Recursively Defined Functions 虞台文 大同大學資工所 智慧型多媒體研究室.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Overview of the theory of computation Episode 3 0 Turing machines The traditional concepts of computability, decidability and recursive enumerability.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 3 The Church-Turing Thesis Contents Turing Machines definitions, examples,
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Unit – 5 : STATE MACHINES Syllabus: Languages and Grammars – Finite State Machines State machines and languages – Turing Machines – Computational Complexity.
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
The Church-Turing Thesis Chapter Are We Done? FSM  PDA  Turing machine Is this the end of the line? There are still problems we cannot solve:
MA/CSSE 474 Theory of Computation Universal Turing Machine Church-Turing Thesis (Winter 2016, these slides were also used for Day 33)
Fall 2013 Lecture 27: Turing machines and decidability CSE 311: Foundations of Computing.
CS 3304 Comparative Languages
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
6.001 SICP Variations on a Scheme
Class 27: Universal Turing Machines CS150: Computer Science
Lecture 37: A Universal Computer
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lambda Calculus Revisited
Closures and Streams cs784(Prasad) L11Clos
فصل سوم The Church-Turing Thesis
Theory of Computation Turing Machines.
Class 36: The Meaning of Truth CS200: Computer Science
Class 37: Making Lists, Numbers and Recursion from Glue Alone
CS21 Decidability and Tractability
Class 33: Making Recursion M.C. Escher, Ascending and Descending
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Class 31: Universal Turing Machines CS200: Computer Science
Class 33: Learning to Count CS200: Computer Science
CSE S. Tanimoto Lambda Calculus
The Church-Turing Thesis and Turing-completeness
CO Games Development 2 Week 21 Turing Machines & Computability
Presentation transcript:

Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia

Lecture 20: λ Calculus 2 Formalism Abstract Problems Language Problems Computation Computability vs. Decidability Review of the Turing Machine ( Q ; ¡ ; § ; ± ; q s t ar t ; q accep t ; q re j ec t ) Today we are looking at a completely different formal computation model – the λ -Calculus!

Lecture 20: λ Calculus 3 Calculus What is calculus? –Calculus is a branch of mathematics that includes the study of limits, derivatives, integrals, and infinite series. Examples The product rule The chain rule

Lecture 20: λ Calculus 4 Real Definition Calculus is just a bunch of rules for manipulating symbols. People can give meaning to those symbols, but that’s not part of the calculus. Differential calculus is a bunch of rules for manipulating symbols. There is an interpretation of those symbols corresponds with physics, geometry, etc.

Lecture 20: λ Calculus 5 λ Calculus Formalism (Grammar) Key words: λ. ( ) terminals term → variable | ( term ) | λ variable. term | term term Humans can give meaning to those symbols in a way that corresponds to computations.

Lecture 20: λ Calculus 6 λ Calculus Formalism (rules) Rules  -reduction(renaming) y. M   v. (M [y v]) where v does not occur in M.  -reduction(substitution) ( x. M)N   M [ x N ] Replace all x’s in M with N Try Example 1, 2, & 3 on the notes now!

Lecture 20: λ Calculus 7 Free and Bound variables In λ Calculus all variable s are local to function definitions Examples –λx.xy x is bound, while y is free; –(λx.x)(λy.yx) x is bound in the first function, but free in the second function –λx.(λy.yx) x and y are both bound variables. (it can be abbreviated as λxy.yx )

Lecture 20: λ Calculus 8 Be careful about  - Reduction ( x. M)N  M [ x N ] Replace all x ’s in M with N If the substitution would bring a free variable of N in an expression where this variable occurs bound, we rename the bound variable before the substitution. Try Example 4 on the notes now!

Lecture 20: λ Calculus 9 Computing Model for Calculus redex: a term of the form ( x. M)N Something that can be  - reduced An expression is in normal form if it contains no redexes (redices). To evaluate a lambda expression, keep doing reductions until you get to normal form.  -Reduction represents all the computation capability of Lambda calculus.

Lecture 20: λ Calculus 10 Another exercise ( f. (( x. f (xx)) ( x. f (xx)))) ( z.z)

Lecture 20: λ Calculus 11 Possible Answer ( f. (( x. f (xx)) ( x. f (xx)))) ( z.z)   ( x. ( z.z)(xx)) ( x. ( z.z)(xx))   ( z.z) ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( z.z) ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( x.( z.z)(xx)) ( x.( z.z)(xx))  ...

Lecture 20: λ Calculus 12 Alternate Answer ( f. (( x. f (xx)) ( x. f (xx)))) ( z.z)   ( x. ( z.z)(xx)) ( x. ( z.z)(xx))   ( x. xx) ( x.( z.z)(xx))   ( x. xx) ( x.xx)  ...

Lecture 20: λ Calculus 13 Be Very Afraid! Some -calculus terms can be  -reduced forever! The order in which you choose to do the reductions might change the result!

Lecture 20: λ Calculus 14 Take on Faith All ways of choosing reductions that reduce a lambda expression to normal form will produce the same normal form (but some might never produce a normal form). If we always apply the outermost lambda first, we will find the normal form if there is one. –This is normal order reduction – corresponds to normal order (lazy) evaluation

Lecture 20: λ Calculus 15 Alonzo Church (1903~1995) Lambda Calculus Church-Turing thesis If an algorithm (a procedure that terminates) exists then there is an equivalent Turing Machine or applicable λ-function for that algorithm.

Lecture 20: λ Calculus 16 Alan M. Turing (1912~1954) Turing Machine Turing Test Head of Hut 8 Advisor: Alonzo Church

Lecture 20: λ Calculus 17 Equivalence in Computability λ Calculus ↔ Turing Machine –(1) Everything computable by λ Calculus can be computed using the Turing Machine. –(2) Everything computable by the Turing Machine can be computed with λ Calculus.

Lecture 20: λ Calculus 18 Simulate λ Calculus with TM The initial tape is filled with the initial λ expression Finite number of reduction rules can be implemented by the finite state automata in the Turing Machine Start the Turing Machine; it either stops – ending with the λ expression on tape in normal form, or continues forever – the  - reductions never ends.

Lecture 20: λ Calculus 19 WPI hacker implemented it on Z8 microcontroller On Zilog Z8 Encore

Lecture 20: λ Calculus 20 Calculus in a Can Project LambdaCan Refer to /lambdacan.html for instructions to build your own -can! /lambdacan.html

Lecture 20: λ Calculus 21 Equivalence in Computability λ Calculus ↔ Turing Machine –(1) Everything computable by λ Calculus can be computed using the Turing Machine. –(2) Everything computable by the Turing Machine can be computed with λ Calculus.

Lecture 20: λ Calculus 22 Simulate TM with λ Calculus Simulating the Universal Turing Machine zzzzzzzzzzzzzzzzzz 1 Start HALT ), X, L 2: look for ( #, 1, -  ), #, R  (, #, L (, X, R #, 0, - Finite State Machine Read/Write Infinite Tape Mutable Lists Finite State Machine Numbers Processing Way to make decisions (if) Way to keep going

Lecture 20: λ Calculus 23 Making Decisions What does decision mean? –Choosing different strategies depending on the predicate What does True mean? –True is something that when used as the first operand of if, makes the value of the if the value of its second operand: if T M N  M if F M N  N

Lecture 20: λ Calculus 24 Finding the Truth if  pca. pca T  xy. x F  xy. y if T M N (( pca. pca) ( xy. x)) M N   ( ca. ( xy. x) ca)) M N     ( xy. x) M N   ( y. M )) N   M Try out reducing (if F T F) on your notes now!

Lecture 20: λ Calculus 25 and and or? and  xy.(if x y F)   xy.(( pca.pca ) x y F)   xy.( x y F)   xy.( x y ( uv. v )) or  xy.(if x T y) much more human-readable!

Lecture 20: λ Calculus 26 Simulate TM with λ Calculus Simulating the Universal Turing Machine zzzzzzzzzzzzzzzzzz 1 Start HALT ), X, L 2: look for ( #, 1, -  ), #, R  (, #, L (, X, R #, 0, - Finite State Machine Read/Write Infinite Tape Mutable Lists Finite State Machine Numbers Processing Way to make decisions (if) Way to keep going X

Lecture 20: λ Calculus 27 What is 11? eleven 11 elf 十一 одиннадцать أحد عش once イレブン onze undici XI

Lecture 20: λ Calculus 28 Numbers The natural numbers had their origins in the words used to count things Numbers as abstractions pred (succ N)  N succ (pred N)  N pred (0)  0 succ (zero)  1

Lecture 20: λ Calculus 29 Defining Numbers In Church numerals, n is represented as a function that maps any function f to its n- fold composition. 0 ≡ λ f x. x 1 ≡ λ f x. f (x) 2 ≡ λ f x. f (f (x))

Lecture 20: λ Calculus 30 Defining succ and pred succ ≡ λ n f x. f (n f x) pred ≡ λ n f x. n (λgh. h (g f )) (λu. x) (λu. u) succ 1   ? We’ll see later how to deduce the term for pred using knowledge about pairs.

Lecture 20: λ Calculus 31 Simulate TM with λ Calculus Simulating the Universal Turing Machine zzzzzzzzzzzzzzzzzz 1 Start HALT ), X, L 2: look for ( #, 1, -  ), #, R  (, #, L (, X, R #, 0, - Finite State Machine Read/Write Infinite Tape Mutable Lists Finite State Machine Numbers Processing Way to make decisions (if) Way to keep going X X

Lecture 20: λ Calculus 32 Defining List List is either –(1) null; or –(2) a pair whose second element is a list. How to define null and pair then?

Lecture 20: λ Calculus 33 null, null?, pair, first, rest null? null  T null? ( pair M N )  F first ( pair M N )  M rest ( pair M N)  N

Lecture 20: λ Calculus 34 null and null? null  x.T null?  x.( x yz.F) null? null   ( x.( x yz.F)) ( x. T)   ( x. T)( yz.F)   T

Lecture 20: λ Calculus 35 Defining Pair A pair [a, b] = (pair a b) is represented as λ z.z a b first ≡ λp.p T rest ≡ λp.p F pair ≡ λ x y z.z x y first (cons M N)   ( λp.p T ) (pair M N)   (pair M N) T   ( λ z.z M N) T   T M N  M M

Lecture 20: λ Calculus 36 Defining pred C ≡ λpz. ( z ( succ ( first p ) ) ( first p ) ) Obviously, C [n, n-1]   [n+1, n], i.e., C turns a pair [n, n-1] to be [n+1, n]. pred ≡ rest ( λn. n C (λz.z 0 0 ) )

Lecture 20: λ Calculus 37 Simulate TM with λ Calculus Simulating the Universal Turing Machine zzzzzzzzzzzzzzzzzz 1 Start HALT ), X, L 2: look for ( #, 1, -  ), #, R  (, #, L (, X, R #, 0, - Finite State Machine Read/Write Infinite Tape Mutable Lists Finite State Machine Numbers Processing Way to make decisions (if) Way to keep going X X X

Lecture 20: λ Calculus 38 Simulate Recursion ( f. (( x. f (xx)) ( x. f (xx)))) ( z.z)   ( x. ( z.z)(xx)) ( x. ( z.z)(xx))   ( z.z) ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( z.z) ( x.( z.z)(xx)) ( x.( z.z)(xx))   ( x.( z.z)(xx)) ( x.( z.z)(xx))  ... This should give you some belief that we might be able to do it. We won’t cover the details of why this works in this class.

Lecture 20: λ Calculus 39 Simulate TM with λ Calculus Simulating the Universal Turing Machine zzzzzzzzzzzzzzzzzz 1 Start HALT ), X, L 2: look for ( #, 1, -  ), #, R  (, #, L (, X, R #, 0, - Finite State Machine Read/Write Infinite Tape Mutable Lists Finite State Machine Numbers Processing Way to make decisions (if) Way to keep going X X X X

Lecture 20: λ Calculus 40 Introducing Scheme Scheme is a dialect of LISP programming language Computation in Scheme is a little higher level than in -Calculus in the sense that the more “human-readable” primitives (like T, F, if, natural numbers, null, null?, and cons, etc) have already been defined for you. The basic reduction rules are exactly the same.

Lecture 20: λ Calculus 41 A Turing simulator in Scheme

Lecture 20: λ Calculus 42 TM Simulator demonstration AT ur i ng M ac h i nerecogn i z i nga n b n E nco d i ngo f t h e FSM i n S c h eme.

Lecture 20: λ Calculus 43 Summary: TM and λ Calculus λ Calculus emphasizes the use of transformation rules and does not care about the actual machine implementing them. It is an approach more related to software than to hardware Many slides and examples are adapted from materials developed for Univ. of Virginia CS150 by David Evans.