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

Size: px
Start display at page:

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

Transcription

1 COMP80 Programming Languages Slides Courtesy of Prof. Sam Guyer Lambda Calculus Formal system with three parts Notation for functions Proof system for equations Calculation rules called reduction Idea: What is the simplest possible programming language that is still capable of expressing any program? Tufts University Computer Science 2 History Alonzo Church, Created -calculus in the 30 s Study of functions Definition, application Question: what things are computable? -calculus is equivalent to Turing Machines Important part of CS history and current theory Influenced design of Lisp, ML, Haskell, Big ideas Lambda calculus Simplest possible programming language Almost nothing is built in Provides a model for computability Question: what is it possible to compute? Examples: Easy: what is nth fibonacci number? Hard: what are the prime factors of a number? Undecidable: do two BNFs produce the same language? Turns out: Decidable (computable) functions are those that can be expressed using lambda calculus Tufts University Computer Science 3 Tufts University Computer Science 4 Expressions and Functions Definitions Expressions x + y x + 2*y + z -calculus is a formal notation for defining functions expressions in this notation are called -expressions every -expression denotes a function Functions x. (x + y) z. (x + 2*y + z) Application (x. (x + y)) 3 (z. (x + 2*y + z)) y x + 2*y + 5 A -expression consists of 3 kinds of terms: Variables: x, y, z, we use V for arbitrary variables Abstractions: V.E Where V is some variable and E is another -term Applications: E 1 E 2 Where E 1 and E 2 are -terms Tufts University Computer Science 5 Tufts University Computer Science 6 1

2 Formal Syntax Formal Syntax in BNF Term ::= Variable Variable. Term Term Term ( Term ) Variable ::= x y z Or, more compactly: E ::= V V.E E E (E) V ::= x y z Application is juxtaposition (no operator) Application has higher precedence than Application is left-associative Tufts University Computer Science 7 Highly ambiguous Again: E ::= V V.E E E (E) V ::= x y z Application is juxtaposition (no operator) Application has higher precedence than Application is left-associative Examples x. x y z x.x y.y f. f(y.y) g(z) f(g)(h x) x. (x(y))(z) x. (x(y.y)) f. f(y.y) (g(z)) f(g)(h(x)) Tufts University Computer Science 8 Abstractions V.E is an abstraction V is a bound variable over the body E An abstraction represents a function V is the head and E is the body. For example the abstraction: I = x.x is the identity function Applications E 1 E 2 is an application Apply the function E 1 to the argument E 2 -calculus only has single-argument functions For example, given the identity function, I = x.x some applications using it (x.x)1 1 (x.x)a a (x.x)(y.y) (y.y) Tufts University Computer Science 9 Tufts University Computer Science 10 Reduction Basic computation rule is -reduction (x. e 1 ) e 2 [e 2 /x]e 1 Reduction Apply reduction to any subexpression Repeat until no more reductions possible Result is called normal form Confluence Church-Rosser theorem Final result (if there is one) is uniquely determined Two functions compute the same thing iff they reduce to the same normal form Tufts University Computer Science 11 Free and Bound Variables Bound variable is placeholder Variable x is bound in x.(x+y) Function x.(x+y) is same function as z.(z+y) Compare x+y dx = z+y dz x P(x) = z P(z) Name of free (=unbound) variable does matter Variable y is free in x.(x+y) Function x.(x+y) is not same as x.(x+z) Occurrences y is free and bound in x. ((y. y+2) x) + y Tufts University Computer Science 12 2

3 Rename Bound Variables Function application (f. x. f (f x)) (y. y+x) Substitute blindly x. [(y. y+x) ((y. y+x) x)] = x. x+x+x Rename bound variables (f. z. f (f z)) (y. y+x) = z. [(y. y+x) ((y. y+x) z))] = z. z+x+x Easy rule: always rename variables to be distinct Substitution [N/x]E means replace x with N in E Variables [N/x]x [N/x]y Application N y [N/x](M 1 M 2 ) ([N/x]M 1 ) ([N/x]M 2 ) Abstraction [N/x] x.m x.m [N/x] y.m y.[n/x]m where y not free in N Tufts University Computer Science 13 Tufts University Computer Science 14 Conversion/reduction rules -conversion x.e y.[y/x]e where y not free in E Rename bound variables to avoid naming conflicts -reduction (x.m)n [N/x]M Function application defined by substitution -conversion v.ev E where v is not free in E Remove redundant abstractions Proof: (x. f x) y = f y therefore x. f x = f Higher-Order Functions Given function f, return function f f f. x. f (f x) How does this work? (f. x. f (f x)) (y. y+1) = x. (y. y+1) ((y. y+1) x) = x. (y. y+1) (x+1) = x. (x+1)+1 Tufts University Computer Science 15 Tufts University Computer Science 16 Programming -calculus -calculus is Turing complete What does that mean? Equivalent to any other complete programming language How to make Booleans and conditional functions numerals and arithmetic functions data structures, such as ordered pairs, lists, etc. recursion For convenience x.y.m is written xy.m cuts down on number of s Church Booleans We define Booleans and logical operators in the -calculus as functions: True = t. f. t False = t. f. f And = x. y. x y (t.f. f) = x y. x y False Or = x.y. x (t.f. t) y = x y. x True y Neg = x. x (u.v. v) (a.b. a) = x. x False True Example: NEG True = (x.x(uv.v)(ab.a))(tf.t) (t.f.t)(uv.v)(ab.a) (f.(uv.v)) (ab.a) (uv.v) = False Tufts University Computer Science 17 Tufts University Computer Science 18 3

4 Church Booleans: If statements Given True = tf.t False = tf.f Define a conditional test function if C then X else Y If C X Y If = cxy.cxy If True X Y True X Y X If False X Y False X Y Y Note If = c.x.(y.(cx)y) c.(x.cx) c.c = I by -conversion Booleans are if functions Church Numerals The natural numbers may be defined using zero and the successor function: 0, 1=succ(0), 2=succ(succ(0)),, etc. In the -calculus, we only have functions Define the natural numbers as functions: 0 = fx.x What else has this definition? 1 = fx.f(x) 2 = fx.f(f(x)) 3 = fx.f(f(f(x)))) n = fx.f n (x) Idea: we represent a number as a lambda expression that applies some function (doesn t matter what function) that number of times. Tufts University Computer Science 19 Tufts University Computer Science 20 Successor function So how do we write a successor function? S = n.fx.f (n f x) Let s test it on zero = gy.y S 0 = (n.fx.f (n f x)) (gy.y) fx.f((gy.y) f x) fx.f((y.y) x) fx.f(x) 1 beta reduction eta conversion beta reduction Note that yx.y(x) = fx.f(z) by -conversion bound variables names are dummy variables More Arithmetic: PLUS := m n f x. n f (m f x) Interpretation: m, n are functions that apply f n times To add them together, start with m, and use the function n to apply f n more times Lists: CONS := f. s. b. b f s CAR := p. p TRUE CDR := p. p FALSE NIL := x.true Tufts University Computer Science 21 Tufts University Computer Science 22 Recursive Functions How to represent recursive functions? let Fact(n) = if (n=0) then 1 else n*fact(n-1) in Fact(5) Write this as -calculus expression P = (Fact. Fact(5)) (n. If (Eq n 0) 1 (Mult n (Fact (Sub n 1)))) assuming definitions for Eq, Mult, Sub What s the problem? Fact is a free variable in the second part of P Cannot use a function before it is defined Tufts University Computer Science 23 Recursive Functions Back up & focus on the recursive definition Fact = n. if (n=0) then 1 else n * Fact(n-1) Abstract Fact to make non-recursive generator G( f ) = n. if (n=0) then 1 else n * f(n-1) performs 1 step of factorial computation, then calls f G = f. n. if (n=0) then 1 else n * f(n-1) How do we compute more steps? Two steps: G(G(f)) Three steps: G(G(G(f))) Actually, we want f = G Intuitively: call G recursively as many times as it takes Tufts University Computer Science 24 4

5 Fixed point function Called a fixpoint Keep calling G recursively until stops When will that happen? G( 0 ) = n. if (n=0) then 1 else n * G(n-1) How do we make this work in -calculus? Idea: a function that computes the fixpoint of other functions Called a combinator Example: Y combinator Y = f. (x. f(x x)) (x. f(x x)) Fixed point function Y combinator in -calculus: Y = f. (x. f(x x)) (x. f(x x)) Has special property that Y( G ) = G( Y(G) ) = G ( G ( Y (G) ) ) = G ( G ( G ( G (... )))) Uses self-application in (x x) Y = f. (x. f (x x)) (y. f(y y)) = f. f ( (y. f(y y)) (y. f(y y)) ) = f. f ( (y. f(y y)) (y. f(y y)) ) = f. f ( f ( (y. f(y y)) (y. f(y y)) ) We will see self-application again when we talk about objects Tufts University Computer Science 25 Tufts University Computer Science 26 Reduction Order Reduction strategy An way of defining which reduction to perform Normal order: always perform left-most -reduction Applicative order: reduce arguments before application Confluence All reduction strategies that return a normal form return the same normal form but some strategies may not terminate, while others do Cost Different reduction strategies can involve different number of reduction steps What is a functional language? Functional languages have Functions as first-class values A data type is first-class if it can be used anywhere: passed to and returned from functions, assigned to variables May also have imperative constructs Examples: Lisp, Scheme, ML, Erlang Pure functional languages Have no implicit side effects or other imperative features Example: Miranda, Haskell Tufts University Computer Science 27 Tufts University Computer Science 28 Functional languages Two big ideas: Functions as data A program can create and manipulate functions on the fly Higher-order functions Twice and compose Currying: customize a function for one input No side-effects Given the same inputs, a function always returns the same result A function has no effects other than producing the return value Variables don t change value there s no store operation We can reason mathematically about functional programs Goal: Translate a program into a mathematic expression Get rid of syntactic details Give a meaning that is completely unambiguous Often: generate a functional program (e.g. in lambda calculus) How is this useful? Systematic conversion into formal notation We can reason about the correctness In practice Most programming languages have informal semantics Example: A break statement terminates the nearest enclosing loop or switch statement Tufts University Computer Science 29 Tufts University Computer Science 30 5

6 Example Syntax B ::= 0 1 N ::= B N B E ::= N E + E value function : E number [[ 0 ]] = 0 [[ 1 ]] = 1 [[ N B ]] = 2 * [[ N ]] + [[ B ]] [[ E 1 + E 2 ]] = [[ E 1 ]] + [[ E 2 ]] Key ideas: Gives a meaning to the syntax Eliminates syntactic details Example: 001 = 01 = 1 Tufts University Computer Science 31 Denotational semantics Idea: Translate a program into a function More mathematical, more precise Program s meaning: Translate each construct into a function from inputs to outputs Compose functions for a program into a large function that computes the whole thing Roughly like compilation Translation to assembly code => for execution We will compile down to lambda calculus => for reasoning Tufts University Computer Science 32 Translation into functions Translating a program into a function How do we do that? Translate each primitive construct into a function Define rules to assemble these functions What kinds of constructs? Assignment: x = y + 5; Conditional: if (c < 10) z = 20; Loops: while (z!= 0) z--; How to represent these things as functions? Example: what does assignment do? Updates a location in memory (whose name is x ) Tufts University Computer Science 33 Statements as functions Statement: x = 5; Meaning: Update the memory location called x with value 5 How do I represent x=5 as a function? Mapping from one program state to another What is a program state? Typically: a mapping from variables to values Example: s = { (x=0), (y=10), (z=999) } Key: Each function produces a new state never modify a state Now we re functional Tufts University Computer Science 34 Example Statement x = 5; : [[ x=5 ]]: state state Start state: s = { (x=0), (y=10), (z=999) } Next state: this is a function t = [[ x=5 ]] (s) = { (x=5), (y=10), (z=999) } Voila: no store operation, no side-effects Basic principle of denotational semantics Compositionality The meaning of a compound program must be defined from the meanings of its parts (not the syntax of its parts). Syntax guides translation to functions Tells us how to compose the functions of the parts Example { P; Q; } composition of two functions, state state Q : state state and P : state state Complete function: Q ( P ( start state ) ) Tufts University Computer Science 35 Tufts University Computer Science 36 6

7 of Imperative Programs Syntax P ::= x := E P; P if E then P else P while E do P : Program (State State) State = Variable Value = v. (give me the value of v) of Assignment Assignment [[ x := E ]] : state state change state with x updated to new value [[ x := E ]]s = s where s = v. IF ( v=x ) ( [[ E ]]s )( s(v) ) s : variable value state = (variable value) s is identical to s except for value of variable x Tufts University Computer Science 37 Tufts University Computer Science 38 Expressions More straightforward [[ E ]]s : [[ E + E ]]s =? = [[ E ]]s + [[ E ]]s [[ E / E ]]s =? = [[ E ]]s / [[ E ]]s [[ (E) ]]s =? = [[ E ]]s [[ v ]]s =? = s(v) of Sequences Sequence [[ P 1 ; P 2 ]] : state state Perform changes from P 1, then perform changes in P 2 [[ P 1 ; P 2 ]] s = [[ P 2 ]] ( [[ P 1 ]] s ) [[ P 1 ; P 2 ]] = s.[[ P 2 ]] ( [[ P 1 ]] s ) [[ P 1 ; P 2 ]] = [[ P 2 ]] [[ P 1 ]] Where is function composition F G = s.f(g(s)) Tufts University Computer Science 39 Tufts University Computer Science 40 of Conditional Conditional [[ if B then P else Q ]] : state state test B in input state, then perform either P or Q on state [[ if B then P else Q ]] s = = IF ( [[ B ]]s ) ( [[ P ]]s ) ( [[ Q ]]s ) Simplification: assume B does not have side effects What would we have to do if B had side effects? Tufts University Computer Science 41 of Iteration Iteration [[ while B do P ]] : state state test B in input state, if true do P, then while B do P Two parts: [[ B ]] the evaluation of expression B [[ P ]] the translation of the body P What do iterations look like? One: if [[ B ]]s then [[ P ]]s else s Two: if [[ B ]]s then let s = [[ P ]]s in (if [[ B ]]s then [[ P ]]s else s ) else s Tufts University Computer Science 42 7

8 of Iteration Generalize: [[ while B do P ]] = the function f such that f(s) = IF ( [[ B ]]s ) ( f( [[ P ]]s ) ) ( s ) What does this look like? I want X, such that X(P) = f(x(p)) = f(f(x(p))) Yep, it s defined by the Y combinator This is what we expect, right? Iteration (e.g., while) becomes recursion Recursion is defined as a fixpoint on the body of the loop How does this help me? Problem: Real languages have many constructs They can be combined in wacky ways Need a clear understanding of what they mean Examples: x = i i; a = b = c = 0; while (*p++ = *q++) ; Tufts University Computer Science 43 Tufts University Computer Science 44 Example Statement: x = 5; y = f(10) + x; What if f modifies x? int f(int a) { x = a; return a; } What about: y = x + f(10); How can I explain this? [[ f ]]s produces a new state s [[ f(10) + x ]] = let s = [[ t = f(10) ]]s in [[ t + x ]]s Tufts University Computer Science 45 8

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus. Variables and Functions. cs3723 1 Lambda Calculus Variables and Functions cs3723 1 Lambda Calculus Mathematical system for functions Computation with functions Captures essence of variable binding Function parameters and substitution Can

More information

CS 242. Fundamentals. Reading: See last slide

CS 242. Fundamentals. Reading: See last slide CS 242 Fundamentals Reading: See last slide Syntax and Semantics of Programs Syntax The symbols used to write a program Semantics The actions that occur when a program is executed Programming language

More information

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

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete Dr A Sahu Dept of Computer Science & Engineering IIT Guwahati Why study? Lambda calculus Syntax Evaluation Relationship to programming languages Church Rosser theorem Completeness of Lambda Calculus: Turing

More information

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242 Spring 2013 Foundations Yu Zhang Acknowledgement: modified from Stanford CS242 https://courseware.stanford.edu/pg/courses/317431/ Course web site: http://staff.ustc.edu.cn/~yuzhang/fpl Reading Concepts

More information

Introduction to the Lambda Calculus

Introduction to the Lambda Calculus Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et

More information

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

Last class. CS Principles of Programming Languages. Introduction. Outline Last class CS6848 - Principles of Programming Languages Principles of Programming Languages V. Krishna Nandivada IIT Madras Interpreters A Environment B Cells C Closures D Recursive environments E Interpreting

More information

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson Lambda Calculus CS 550 Programming Languages Jeremy Johnson 1 Lambda Calculus The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Use currying

More information

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

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

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

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting:

More information

Type Systems Winter Semester 2006

Type Systems Winter Semester 2006 Type Systems Winter Semester 2006 Week 4 November 8 November 15, 2006 - version 1.1 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial

More information

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

5. Introduction to the Lambda Calculus. Oscar Nierstrasz 5. Introduction to the Lambda Calculus Oscar Nierstrasz Roadmap > What is Computability? Church s Thesis > Lambda Calculus operational semantics > The Church-Rosser Property > Modelling basic programming

More information

Pure Lambda Calculus. Lecture 17

Pure Lambda Calculus. Lecture 17 Pure Lambda Calculus Lecture 17 Lambda Calculus Lambda Calculus (λ-calculus) is a functional notation introduced by Alonzo Church in the early 1930s to formalize the notion of computability. Pure λ-calculus

More information

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

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013 Constraint-based Analysis Harry Xu CS 253/INF 212 Spring 2013 Acknowledgements Many slides in this file were taken from Prof. Crista Lope s slides on functional programming as well as slides provided by

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

Lambda Calculus. Lecture 4 CS /26/10

Lambda Calculus. Lecture 4 CS /26/10 Lambda Calculus Lecture 4 CS 565 10/26/10 Pure (Untyped) Lambda Calculus The only value is a function Variables denote functions Functions always take functions as arguments Functions always return functions

More information

Lambda Calculus.

Lambda Calculus. Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto, Stephen A. Edwards) Benjamin Pierce Types and Programming Languages http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html

More information

Fundamentals and lambda calculus

Fundamentals and lambda calculus Fundamentals and lambda calculus Again: JavaScript functions JavaScript functions are first-class Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function ()

More information

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

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus CS 4110 Programming Languages & Logics Lecture 17 Programming in the λ-calculus 10 October 2014 Announcements 2 Foster Office Hours 11-12 Enjoy fall break! Review: Church Booleans 3 We can encode TRUE,

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus 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... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

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

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations Outline FP Foundations, Scheme In Text: Chapter 15 Mathematical foundations Functional programming λ-calculus LISP Scheme 2 Imperative Languages We have been discussing imperative languages C/C++, Java,

More information

1 Scope, Bound and Free Occurrences, Closed Terms

1 Scope, Bound and Free Occurrences, Closed Terms CS 6110 S18 Lecture 2 The λ-calculus Last time we introduced the λ-calculus, a mathematical system for studying the interaction of functional abstraction and functional application. We discussed the syntax

More information

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

Introduction to Lambda Calculus. Lecture 7 CS /08/09 Introduction to Lambda Calculus Lecture 7 CS 565 02/08/09 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus 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... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

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

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

More information

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

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

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

More information

Recursive Definitions, Fixed Points and the Combinator

Recursive Definitions, Fixed Points and the Combinator Recursive Definitions, Fixed Points and the Combinator Dr. Greg Lavender Department of Computer Sciences University of Texas at Austin Recursive Self-Reference Recursive self-reference occurs regularly

More information

Formal Systems and their Applications

Formal Systems and their Applications Formal Systems and their Applications Dave Clarke (Dave.Clarke@cs.kuleuven.be) Acknowledgment: these slides are based in part on slides from Benjamin Pierce and Frank Piessens 1 Course Overview Introduction

More information

Lecture 5: The Untyped λ-calculus

Lecture 5: The Untyped λ-calculus Lecture 5: The Untyped λ-calculus Syntax and basic examples Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Pratikakis (CSD) Untyped λ-calculus I CS49040,

More information

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

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus A Quick Overview CAS 701 Class Presentation 18 November 2008 Lambda Department of Computing & Software McMaster University 1.1 Outline 1 2 3 Lambda 4 5 6 7 Type Problem Lambda 1.2 Lambda calculus is a

More information

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

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors. INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright Instructors. History Formal mathematical system Simplest programming language Intended for studying functions, recursion

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

Introduction to the Lambda Calculus. Chris Lomont

Introduction to the Lambda Calculus. Chris Lomont 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

More information

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

4/19/2018. Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Untyped Lambda Calculus

Untyped Lambda Calculus Advanced Topics in Programming Languages Untyped Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming

More information

CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Fall September 25 CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional

More information

Introduction to the l-calculus

Introduction to the l-calculus Introduction to the l-calculus CS345 - Programming Languages Dr. Greg Lavender Department of Computer Sciences The University of Texas at Austin l-calculus in Computer Science a formal notation, theory,

More information

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

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu, Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

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

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

More information

Untyped Lambda Calculus

Untyped Lambda Calculus Concepts in Programming Languages Recitation 5: Untyped Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and

More information

Lambda Calculus. Lambda Calculus

Lambda Calculus. Lambda Calculus Lambda Calculus Formalism to describe semantics of operations in functional PLs Variables are free or bound Function definition vs function abstraction Substitution rules for evaluating functions Normal

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 74/2/70/17 (2:30,2:30,3:35,7:30) PS2 due Thursday 9/20 11:59PM Guest lecture on Tuesday 9/25 o No RDZ office hours next Friday, I am on travel A brief comment

More information

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham λ-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

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

From the λ-calculus to Functional Programming Drew McDermott Posted

From the λ-calculus to Functional Programming Drew McDermott Posted From the λ-calculus to Functional Programming Drew McDermott drew.mcdermott@yale.edu 2015-09-28 Posted 2015-10-24 The λ-calculus was intended from its inception as a model of computation. It was used by

More information

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus Chapter 5: The Untyped Lambda Calculus What is lambda calculus for? Basics: syntax and operational semantics Programming in the Lambda Calculus Formalities (formal definitions) What is Lambda calculus

More information

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

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP) Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

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

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08 Introduction to Lambda Calculus Lecture 5 CS 565 1/24/08 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

Programming Language Pragmatics

Programming Language Pragmatics Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen

More information

Part I. Historical Origins

Part I. Historical Origins Introduction to the λ-calculus Part I CS 209 - Functional Programming Dr. Greg Lavender Department of Computer Science Stanford University Historical Origins Foundations of Mathematics (1879-1936) Paradoxes

More information

The Untyped Lambda Calculus

The Untyped Lambda Calculus Resources: The slides of this lecture were derived from x = 1 [Järvi], with permission of the original author, by copy & let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Formal Semantics. Aspects to formalize. Lambda calculus. Approach Formal Semantics Aspects to formalize Why formalize? some language features are tricky, e.g. generalizable type variables, nested functions some features have subtle interactions, e.g. polymorphism and

More information

Lexicografie computationala Feb., 2012

Lexicografie computationala Feb., 2012 Lexicografie computationala Feb., 2012 Anca Dinu University of Bucharest Introduction When we construct meaning representations systematically, we integrate information from two different sources: 1. The

More information

10.6 Theoretical Foundations

10.6 Theoretical Foundations 10 Functional Languages 10.6 Theoretical Foundations EXAMPLE 10.44 Functions as mappings Mathematically,a function is a single-valued mapping: it associates every element in one set (the domain) with (at

More information

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

More Untyped Lambda Calculus & Simply Typed Lambda Calculus Concepts in Programming Languages Recitation 6: More Untyped Lambda Calculus & Simply Typed Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky,

More information

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

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur 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

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

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

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility λ Calculus Church s λ Calculus: Brief History One of a number of approaches to a mathematical challenge at the time (1930): Constructibility (What does it mean for an object, e.g. a natural number, to

More information

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

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1 Lecture #3: Lambda Calculus Last modified: Sat Mar 25 04:05:39 2017 CS198: Extra Lecture #3 1 Simplifying Python Python is full of features. Most are there to make programming concise and clear. Some are

More information

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m. CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics.

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings CMSC 330: Organization of Programming Languages Lambda Calculus Encodings CMSC330 Spring 2018 1 The Power of Lambdas Despite its simplicity, the lambda calculus is quite expressive: it is Turing complete!

More information

What does my program mean?

What does my program mean? September 16, 2015 L02-1 What does my program mean? Armando Solar Lezama Computer Science and Artificial Intelligence Laboratory M.I.T. Adapted from Arvind 2010. Used with permission. September 16, 2015

More information

Software Paradigms (Lesson 4) Functional Programming Paradigm

Software Paradigms (Lesson 4) Functional Programming Paradigm Software Paradigms (Lesson 4) Functional Programming Paradigm Table of Contents 1 Introduction... 2 2 Evaluation of Functions... 3 3 Compositional (Construct) Operators... 4 4 Some Implementation Issues...

More information

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

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus 1 Computer Science 203 Programming Languages Fall 2004 Lecture 10 Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus Plan Informal discussion of procedures and bindings Introduction

More information

Software System Design and Implementation

Software System Design and Implementation Software System Design and Implementation Motivation & Introduction Gabriele Keller (Manuel M. T. Chakravarty) The University of New South Wales School of Computer Science and Engineering Sydney, Australia

More information

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15 Lambda Calculus: Implementation Techniques and a Proof COS 441 Slides 15 Last Time: The Lambda Calculus A language of pure functions: values e ::= x \x.e e e v ::= \x.e With a call-by-value operational

More information

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7 Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

More information

Lambda Calculus and Computation

Lambda Calculus and Computation 6.037 Structure and Interpretation of Computer Programs Chelsea Voss csvoss@mit.edu Massachusetts Institute of Technology With material from Mike Phillips and Nelson Elhage February 1, 2018 Limits to Computation

More information

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus Encodings CMSC 330 Summer 2017 1 The Power of Lambdas Despite its simplicity, the lambda calculus is quite expressive: it is Turing complete!

More information

An introduction to Scheme

An introduction to Scheme An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

More information

Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

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

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable? Activity CSCI 334: Principles of Programming Languages Lecture 4: Fundamentals II Write a function firsts that, when given a list of cons cells, returns a list of the left element of each cons. ( (a. b)

More information

Denotational semantics

Denotational semantics 1 Denotational semantics 2 What we're doing today We're looking at how to reason about the effect of a program by mapping it into mathematical objects Specifically, answering the question which function

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus. Concepts in Programming Languages Recitation 6: Concepts in Programming Languages Recitation 6: Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming

More information

FP Foundations, Scheme

FP Foundations, Scheme FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

More information

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

More information

Control in Sequential Languages

Control in Sequential Languages CS 242 2012 Control in Sequential Languages Reading: Chapter 8, Sections 8.1 8.3 (only) Section 7.3 of The Haskell 98 Report, Exception Handling in the I/O Monad, http://www.haskell.org/onlinelibrary/io-13.html

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC330. Objects, Functional Programming, and lambda calculus CMSC330 Objects, Functional Programming, and lambda calculus 1 OOP vs. FP Object-oriented programming (OOP) Computation as interactions between objects Objects encapsulate mutable data (state) Accessed

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 Summer 2017 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of

More information

Elixir, functional programming and the Lambda calculus.

Elixir, functional programming and the Lambda calculus. Elixir, functional programming and the Lambda calculus. Programming II - Elixir Version Johan Montelius Spring Term 2018 Introduction In this tutorial you re going to explore lambda calculus and how it

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus Redefinition of an identifier is OK, but this is redefinition not assignment; Thus val x = 100; val x = (x=100); is fine; there is no type error even though the first x is an integer and then it is a boolean.

More information