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

Similar documents
The Untyped Lambda Calculus

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

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

Chapter 5: The Untyped Lambda Calculus

Concepts of programming languages

Functional Programming. Big Picture. Design of Programming Languages

Introduction to the Lambda Calculus

Type Systems Winter Semester 2006

More Lambda Calculus and Intro to Type Systems

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

CITS3211 FUNCTIONAL PROGRAMMING

Chapter 11 :: Functional Languages

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Functional Programming

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

CIS 500 Software Foundations Fall September 25

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

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

Lecture 5: The Untyped λ-calculus

Fundamentals and lambda calculus

Introduction. chapter Functions

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

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

Lambda Calculus. Lecture 4 CS /26/10

Untyped Lambda Calculus

Programming Language Pragmatics

More Lambda Calculus and Intro to Type Systems

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

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Untyped Lambda Calculus

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

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.

Elixir, functional programming and the Lambda calculus.

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

CS 242. Fundamentals. Reading: See last slide

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages

Introduction to the Lambda Calculus. Chris Lomont

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

CSE 505: Concepts of Programming Languages

Functional Languages and Higher-Order Functions

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Functional Programming

Recursive Definitions, Fixed Points and the Combinator

Lambda Calculus. Variables and Functions. cs3723 1

CSCE 314 Programming Languages

From the λ-calculus to Functional Programming Drew McDermott Posted

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Lambda Calculus. Lambda Calculus

Pure Lambda Calculus. Lecture 17

Chapter 5: The Untyped Lambda Calculus

The Untyped Lambda Calculus

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

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

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

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

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.

Lecture 5: Lazy Evaluation and Infinite Data Structures

Denotational Semantics. Domain Theory

Introduction to the λ-calculus

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

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

The Untyped Lambda Calculus

Functional Programming

CIS 500 Software Foundations Midterm I

Functional Languages. Hwansoo Han

More Lambda Calculus and Intro to Type Systems

Lambda Calculus see notes on Lambda Calculus

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

CMSC 330: Organization of Programming Languages

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

Lexicografie computationala Feb., 2012

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

Lambda Calculus.

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

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

Lecture Notes on Data Representation

CMSC 330: Organization of Programming Languages

301AA - Advanced Programming

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

VU Semantik von Programmiersprachen

Lambda Calculus. Gunnar Gotshalks LC-1

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

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

CSE-321 Programming Languages 2012 Midterm

LECTURE 16. Functional Programming

CMSC 330: Organization of Programming Languages

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

Programming Languages

15. Functional Programming

Transcription:

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

Software Development Challenges Growing size and complexity of modern computer programs Complicated architectures Massively parallel architectures, Memory hierarchy, distributed systems, Fast and cost effective software development Above all: Correctness! Proof that the program works for all cases 1

Well-structured Software Easy to write and debug Reusable modules Amenable to proofs Permit rapid prototyping Solutions to the development challenges. Programming style to support development of well-structured software. 2

Functional Languages Fundamental operation is the application of functions to arguments. Main features to improve modularity: No (almost none!!) side effects Higher order functions Lazy evaluation 3

Example Summing the integers 1 to 10 in C: int total = 0, i; for (i = 1; i <= 10; ++i) total = total+i; Values change for both total and i during program execution 4

Example Summing integers 1 to 10 in a pure functional language No side effect => No assignments to variables! sum (m, n) = if (m > n) 0 else m + sum (m+1, n) sum (1, 10) // main function 5

Historical Background [source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt] 1930s: Alonzo Church develops the lambda calculus, a simple but powerful theory of functions. 6

Historical Background [source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt] 1950s: John McCarthy develops Lisp, the first functional language, with some influences from the lambda calculus, but retaining variable assignments. 7

Historical Background [source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt] 1970s: John Backus develops FP, a functional language that emphasizes higher-order functions and reasoning about programs. 8

Trivia John Backus : Proposed (in 1954) a program that translated high level expressions into native machine code. Fortran I project (1954-1957): The first compiler was released 1977 ACM Turing Award for profound, influential, and lasting contributions to the design of practical high-level programming systems, notably through his work on FORTRAN, and for publication of formal procedures for the specification of programming languages. Introduced FP in his Turing Award lecture "Can Programming be Liberated from the von Neumann Style?". 9

Quicksort: English description 1. Empty list is already sorted. 2. For a non empty list a. Pick the first element, pivot, from the array. b. Recursively quicksort the array of elements with values less than the pivot. Call it S. c. Recursively quicksort the array of elements with values greater than or equal to the pivot, except the pivot. Call it G. d. The final sorted array is: the elements of S followed by pivot, followed by the elements of G. 10

Quicksort: Functional (Haskell) description* quicksort [] = [] quicksort (x:xs) = quicksort [y y <- xs, y<x] ++ [x] ++ quicksort [y y <- xs, y>=x] * source: https://www.haskell.org/tutorial/haskell-98-tutorial.pdf 11

Higher order function add x y = x + y inc = add 1 map f [] = [] map f (x:xs) = f x : map f xs map is a higher order function. It takes a function as argument. Functional programming treats functions as firstclass citizens. There is no discrimination between function and data. map inc [1, 2, 3] => [2, 3, 4] 12

Lazy evaluation Do not evaluate an expression unless it is needed Never evaluate an expression more than once length [1/1, 2/2, 0/0, 4/4] => 4 numsfrom n = n : numsfrom (n+1) squares = map (^2) (numsfrom 0) take 5 squares => [0,1,4,9,16] 13

Lambda calculus The assembly language of functional programming

The Abstract Syntax A really tiny language of expressions // An expression can be a e x λλ. e 1 e 1 e 2 // Variable // Function Definition // Function Application (e 1 ) That s all the Syntax!! 15

Conventions λλ. e 1 e 2 e 3 is an abbreviation for λλ. e 1 e 2 e 3, i.e., the scope of x is as far to the right as possible until it is terminated by a ) whose matching ( occurs to the left of the λ, or terminated by the end of the term Application associates to the left:e 1 e 2 e 3 is to be read as (e 1 e 2 )e 3 and not as e 1 (e 2 e 3 ) λλλλ. e is an abbreviation for λλλyλz. e which in turn is actually λλ. (λy. λλ. e ) 16

α-renaming The name of a bound variable has no meaning except for its use to identify the bounding λ. Renaming a λ variable including all its bound occurrences does not change the meaning of an expression. For example, λλ. x x y is equivalent to λu. u u y But it is not same as λλ. x x w Can not change free variable! 17

β-reduction(execution) if an abstraction λλ. e 1 is applied to a term e 2 then the result of the application is the body of the abstraction e 1 with all free occurrences of the formal parameter x replaced with e 2. For example, λλλλ. f (f x) ttttt β ttttt (ttttt x) 18

Caution During β-reduction, make sure a free variable is not captured inadvertently. The following reduction is WRONG λλ. λλ. x λλ. y λλ. λλ. y Use α-renaming to avoid variable capture λλ. λλ. x λλ. y λuuu. u λx. y λλ. λλ. y 19

Exercise Apply β-reduction as far as possible 1. (λλ y z. x z y z ) λλ y. x (λλ. y) 2. λ x. x x λx. x x 3. λλ y z. x z (y z) λλ y. x ( λλ. x x λλ. x x ) 20

Church-Rosser Theorem Multiple ways to apply β-reduction Some may not terminate However, if two different reduction sequences terminate then they always terminate in the same term e e 1 e 2 e Leftmost, outermost reduction will find the normal form if it exists 21

But what about other stuff? Constants? Numbers Booleans Complex Types? Lists Arrays Don t we need data? Recall: functions are first-class citizens! Function is data and data is function. 22

Numbers We need a Zero Absence of item And something to count Presence of item Intuition: Whiteboard and Marker Blank board represents Zero Each mark by marker represents a count. However, other pairs of objects will work as well Lets translate this intuition into λ-expr 23

Numbers Zero = λλ. λλ. w No mark on whiteboard One = λm. λw. m w Two = λm. λw. m m w What about operations? add, multiply, subtract, divide 24

Operations on Numbers succ = λλλλ. m (x m w) Verify that ssss N = N + 1 add = λxxxw. x m y m w Verify that aaa N M = N + M mult = λxxxx. x y m w Verify that mmmm N M = N M called Church Numerals. 25

Booleans True and False Intuition: Select one out of two possible choices. λ-expressions True = λλ λλ. x False = λλ λλ. y 26

Operations on Booleans Logical operations aaa = λp q. p q p nnn = λλ t f. p f t The conditional function ii ii c e 1 e 2 reduces to e 1 if c reduces to True and e 2 if c reduces to False ii = λλ e t e f. (c e t e f ) 27

More More such types can be found at https://en.wikipedia.org/wiki/church_enc oding It is fun to come up with your own definitions for constants and operations over different types or to develop understanding for existing definitions. 28

We are missing something!! The machinery described so far does not allow us to define Recursive functions factorial, Fibonacci There is no concept of named functions So no way to refer to a function recursively? Fix-point computation comes to rescue 29

Fix-point and Y-combinator A fix-point of a function f is a value p such that f p = p Assume existence of a magic expression, called Y-combinator, that when applied to a λ-expression, gives its fixed point Y f = f (Y f) Y combinator gives us a way to apply a function recursively 30

Factorial fact = λn. if (iszero n) One (mult n (fact (pred n))) = (λfλn. if (iszero n) One (mult n (f (pred n)))) fact fact = g fact fact is a fixed point of function g = λfλn. if (iszero n) One (mult n (f (pred n)))) Using Y-combinator, fact = Y (λfλn. if (iszero n) One (mult n (f (pred n)))) = Y g 31

Verify fact 2 = (Y g) 2 = g (Y g) 2 // Y f = f (Y f), definition of Y-combinator = (λfλn. if (is0 n) 1 (* n (f (pred n)))) (Y g) 2 = (λn. if (is0 n) 1 (* n ((Y g) (pred n)))) 2 = if (is0 2) 1 (* 2 ((Y g) (pred 2))) = (* 2 ((Y g) 1)) = (* 2 (* 1 (if (is0 0) 1 (* 0 ((Y g) (pred 0))))) = (* 2 (* 1 1)) = 2 32

Recursion Y-combinator allows to unroll the body of loop once similar to one unfolding of recursive call Sequence of Y-combinator applications allow complete unfolding of recursive calls BUT, what about the existence of Y- combinator? 33

Y-combinators Many candidates exist Y 1 = λλ λλ. f x x λλ. f x x T = λλλλλλλλλλλλλλλλλλλλλλλλλλλ. r ttttttttttttttttttttttttttt Y fffff = TTTTTTTTTTTTTTTTTTTTTTTTTT Verify that (Y f) = f (Y f) for each 34

Summary A cursory look at λ-calculus to understand how Functional Programming works How it is different from imperative programming Functions are data, and Data are functions! Church Turing Thesis => The power of λ calculus equivalent to that of Turing Machine 35

36