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

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

Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

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

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

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

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

The Untyped Lambda Calculus

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

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

Introduction to the Lambda Calculus

Chapter 11 :: Functional Languages

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

Pure Lambda Calculus. Lecture 17

Programming Language Pragmatics

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

Functional Programming. Big Picture. Design of Programming Languages

CS 314 Principles of Programming Languages. Lecture 21

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

Introduction to the Lambda Calculus. Chris Lomont

Lecture 5: The Untyped λ-calculus

Fundamentals and lambda calculus

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Functional Languages. Hwansoo Han

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lambda Calculus. Lecture 4 CS /26/10

Functional Languages and Higher-Order Functions

301AA - Advanced Programming [AP-2017]

More Lambda Calculus and Intro to Type Systems

Lambda Calculus. Lambda Calculus

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Lambda Calculus.

CSE 505: Concepts of Programming Languages

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

Part I. Historical Origins

VU Semantik von Programmiersprachen

Recursive Definitions, Fixed Points and the Combinator

Introduction to the l-calculus

Lambda Calculus. Variables and Functions. cs3723 1

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

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

More Lambda Calculus and Intro to Type Systems

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.

Introduction. chapter Functions

Functional Programming

LECTURE 16. Functional Programming

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

Costly software bugs that could have been averted with type checking

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages

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

Lambda Calculus-2. Church Rosser Property

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

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

CIS 500 Software Foundations Midterm I

Chapter 5: The Untyped Lambda Calculus

Programming Languages

1 Scope, Bound and Free Occurrences, Closed Terms

301AA - Advanced Programming

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Concepts of programming languages

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

MATH Iris Loeb.

ITT8060 Advanced Programming

COS 326 Functional programming: an elegant weapon for the modern age

Functional Programming

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

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Lecture Notes on Data Representation

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.

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

Introduction to the λ-calculus

CSE-321 Programming Languages 2012 Midterm

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

CS 242. Fundamentals. Reading: See last slide

CSCI-GA Scripting Languages

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

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

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

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

λ calculus is inconsistent

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

Transcription:

Recursion Lecture 6: More Lambda Calculus Programming CSC 131! Fall, 2014!! Kim Bruce Recursive definitions are handy! - fact = λn. cond (iszero n) 1 (Mult n (fact (Pred n)))! - Not a legal definition in lambda calculus because can t name functions!! Compute by expanding:! - fact 2! = cond (iszero 2) 1 (Mult 2 (fact (Pred 2)))! = Mult 2 (fact 1)! = Mult 2 (cond (iszero 1) 1 (Mult 1 (fact (Pred 1)))! = Mult 2 (Mult 1 (fact 0)) =... = Mult 2 (Mult 1 1) = 2 Recursion Fixed Points A different perspective: Start with! - fact = λn. cond (iszero n) 1 (Mult n (fact (Pred n)))! Let F stand for the closed term:! - λf. λn. cond (iszero n) 1 (Mult n (f (Pred n)))! - Notice F(fact) = fact.! - fact is a fixed point of F! - To find fact, need only find fixed point of F!! Easy w/ g(x) = x * x, but F???? Several fixed point operators:! - Ex: Y = λf. (λx. f (xx))(λx. f (xx))! Claim for all g, Y g = g (Y g)! Y g = (λf. (λx. f (xx))(λx. f (xx))) g! = (λx. g(xx))(λx. g(xx))! = g((λx. g(xx)) (λx. g(xx)))! = g (Y g)! If let x 0 = Y g, then g (x 0 ) = x 0. Invented by Haskell Curry

Factorial Computing Factorials Recursive definition:! - let F = λf. λn. cond (iszero n) 1 (Mult n (f (Pred n)))! - let fact = Y F! - then F(fact) = fact because Y always gives fixed points! Compute:! fact 0 = (F (fact)) 0 because fact is a fixed point of F! = cond (iszero 0) 1 (Mult 0 (fact (Pred 0)))! fact 1 = (F (fact)) 1 because fact is a fixed point of F! = (λn. cond (iszero n) 1 (Mult n (fact (Pred n)))) 1 expanding F! = cond (iszero 1) 1 (Mult 1 (fact (Pred 1))) applying it" = Mult 1 (fact (Pred 1)) by the definition of cond! = fact 0 by the definition of Mult and Pred! = 1 by the above calculation! = 1 by the definition of cond Lambda Calculus λ-calculus invented in 1928 by Church in Princeton & first published in 1932.! Goal to provide a foundation for logic! First to state explicit conversion rules.! Original version inconsistent, but corrected! - If this sentence is true then 1 = 2 problematic!!! 1933, definition of natural numbers Collaborators 1931-1934: Grad students:! - J. Barkley Rosser and Stephen Kleene! - Church-Rosser confluence theorem ensured consistency (earlier version inconsistent)! - Kleene showed λ-definable functions very rich! Equivalent to Herbrand-Gödel recursive functions! Equivalent to Turing-computable functions.! Founder of recursion theory, invented regular expressions! Church s thesis:! - λ-definability effectively computable O

Undecidability Alan Turing Turing! Convertibility problem for λ-calculus undecidable.! Validity in first-order predicate logic undecidable.! Proved independently year later by Turing.! - First showed halting problem undecidable - 1936, in Cambridge, England, definition of Turing machine! - 1936-38, in Princeton to get Ph.D. under Church.! - 1937, first published fixed point combinator! (λx. λy. (y (x x y))) (λx. λy. (y (x x y)))! - Kleene did not use fixed-point operator in defining functions on natural numbers!" - Broke German enigma code in WW2, Turing test AI! - Persecuted as homosexual, committed suicide in 1954 Combinatory Logic Schönfinkel - get rid of bound variables! Combinators K, S sufficient to write all computable functions:! - K x y = x! - S x y z = (x y)(x z)! Typed Lambda Calculus CL terms include K, S, variables and all terms created by function application.! All computable fcns, equivalent to λ-calculus

Types Definitions Start with base type e and build up types and terms:! - Type ::= e Type Type! - M ::= v (M M) λv : Type. M! Examples:! - Types: e, e e, e e e, (e e) e,...! - Terms: λx: e. x, λf: e e. λz: e. f(f(z)) Earlier definitions generalize over types t:! - true t = λx:t. λy:u. x! - n t = λs: t t. λz: t. s (n) (z)! Some untyped terms can t be typed:! - Ω = (λx. (x x))(λx. (x x))! - Y = λf. (λx. f (x x))(λx. f (x x)) Totally Awesome!! Theorem: If M is a term of the typed lambda calculus, then M has a unique normal form. I.e., every term of the typed lambda calculus is total.! Functional Languages Corollary: The typed lambda calculus does not include all computable functions.

Commands/Statements Imperative Languages Characteristics! - Support for variables! represent memory locations for storing updatable values.! - Assignment operation! progress in computation depends on changes in values stored in variables.! - Repetition! flow of control guided by conditional and looping statements controlling order in which assignment statements are executed. Organized around notion of commands! Meaning of a statement is operation which, based on current contents of memory, and explicit values supplied to it, modifies the current contents of memory.! How are results of one command communicated to next? Problems Expressions... return a value, depending on the state of the computation.! Too low level.! Architecture dependent.! Does not generalize well to concurrent computation. Examples:! - Literals: 3, true, hello, 42.56! - Aggregates: arrays, records, sets, lists, etc. E.g. [1,3,5]! - Function calls: f(a,b), a + b * (c - d), (if x > 0 then sin else cos)(... )! - Conditional expressions: if x <> 0 then a/x else 1, case (historically only in functional languages)! - Named constants and variables: pi, x

Declarative Language Test Problems Within the scope of specific declarations of x 1,, x n, all occurrences of an expression e containing only variables x 1,, x n have the same value.! - Pure expressions are well behaved.! Fails w/imperative languages:! - x := x + y; y := 2 * x;! - Two occurrences of x have different values.! If DLT holds, then allows for optimizations Order of evaluation! - if (i >= 0 && a[i]!= 100)...! - What happens if i = -1?" - Is && commutative?" Side effects! - Destroy nice properties! - a[++x] = (y = x+1) + y + (++x)! - Does 2*(x++) = (x++) + (x++)? History of Functional Languages Functional Languages LISP 1957! Backus Turing award lecture (1977) - FP / FL! ML (1977) - Turing award for Robin Milner! - Meta language for theorem prover! SASL, KRC, and Miranda (Turner), Haskell (1990).! - Lazy languages! - Implemented using combinatory logic Pure LISP/Scheme (and Rex) are essentially sugared versions of untyped lambda calculus with constants.! ML & Haskell are typed lambda calculus w/ type inference and recursion definitions at each type.