CMSC330. Objects, Functional Programming, and lambda calculus

Similar documents
CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

OCaml Language Choices CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Objects and Functional Programming

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

Lecture 5: The Untyped λ-calculus

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

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

Introduction to the λ-calculus

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Type Systems Winter Semester 2006

Lambda Calculus and Type Inference

Fundamentals and lambda calculus

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

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

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Chapter 11 :: Functional Languages

CIS 500 Software Foundations Fall September 25

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

dynamically typed dynamically scoped

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

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

Functional Programming. Big Picture. Design of Programming Languages

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

CSE 505: Concepts of Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC 330: Organization of Programming Languages. Operational Semantics

The Untyped Lambda Calculus

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

The Untyped Lambda Calculus

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

Introductory Example

Functional Programming Languages (FPL)

Lambda Calculus. Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Functional Programming

Functional Languages. Hwansoo Han

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

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

Functional Programming. Functional Programming

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Implementing functional languages with abstract machines

CMSC 330: Organization of Programming Languages

Lambda Calculus and Extensions as Foundation of Functional Programming

Lambda Calculus. Lecture 4 CS /26/10

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

Programming Language Pragmatics

CMSC330 Spring 2012 Final Exam Solutions

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

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

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CMSC330 Spring 2017 Midterm 2

Programming Languages

Lambda Calculus.

CMSC330 Fall 2013 Practice Problems 6 Solutions

CSCI-GA Scripting Languages

Functional Programming

CSCI.4430/6969 Programming Languages Lecture Notes

Chapter 5: The Untyped Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

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

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Recursive Definitions, Fixed Points and the Combinator

LECTURE 16. Functional Programming

Anonymous Functions CMSC 330: Organization of Programming Languages

CS 11 Haskell track: lecture 1

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Lambda Calculus. Variables and Functions. cs3723 1

Lecture 13 CIS 341: COMPILERS

More Lambda Calculus and Intro to Type Systems

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

Typed Lambda Calculus and Exception Handling

Continuations and Continuation-Passing Style

Organization of Programming Languages CS3200/5200N. Lecture 11

CITS3211 FUNCTIONAL PROGRAMMING

Formal Systems and their Applications

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

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

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.

Introduction to the Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Elixir, functional programming and the Lambda calculus.

Transcription:

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 / modified via object s public methods Functional programming (FP) Computation as evaluation of functions Mutable data used to improve efficiency Higher-order functions implemented as closures Closure = function + environment 2

An Integer Stack Abstraction in Java class Stack { class Node { Integer val; Node next; Node(Integer v, Node n) { val = v; next = n; } }; private Node thestack; void push(integer v) { thestack = new Node(v, thestack); } Integer pop() { if (thestack == null) throw new NoSuchElementException(); Integer temp = thestack.val; thestack = thestack.next; return temp; } } 3

A Stack Abstraction in OCaml module type STACK = sig type 'a stack val new_stack : unit -> 'a stack val push : 'a stack -> 'a -> unit val pop : 'a stack -> 'a end module Stack : STACK = struct type 'a stack = 'a list ref let new_stack () = ref [] let push s x = s := (x::!s) let pop s = match!s with [] -> failwith "Empty stack" (h::t) -> s := t; h end 4

Another Stack Abstraction in OCaml let new_stack () = let this = ref [] in let push x = this := (x::!this) and pop () = match!this with [] -> failwith "Empty stack" (h::t) -> this := t; h in (push, pop) # let s = new_stack ();; val s : ('_a -> unit) * (unit -> '_a) = (<fun>, <fun>) # Pervasives.fst s 3;; (* applies 1 st part of s to 3 *) - : unit = () # Pervasives.snd s ();; (* applies 2 nd part of s to () *) - : int = 3 5

Two OCaml Stack Implementations 1 st implementation (OOP style) Based on modules Specifies methods for Creating stack Pushing value onto stack parameter Popping value from stack parameter 2 nd implementation (FP style) Based on closures Creating stack returns tuple containing Closure for pushing value onto created stack Closure for popping value from created stack 6

Relating Objects and Closures An object... Is a collection of fields (data)...and methods (code) When a method is invoked Method has implicit this parameter that can be used to access fields of object A closure... Is a pointer to an environment (data)...and a function body (code) When a closure is invoked Function has implicit environment that can be used to access variables 7

Relating Objects and Closures (cont.) class C { int x = 0; void set_x(int y) { x = y; } int get_x() { return x; } } let make () = let x = ref 0 in ( (fun y -> x := y), (fun () ->!x) ) x = 0 x = ref 0 C c = new C(); c.set_x(3); int y = c.get_x(); fun y -> x := y fun () ->!x let (set, get) = make ();; set 3;; let y = get ();; 8

Recall a Useful Higher-Order Function let rec map f = function [] -> [] (h::t) -> (f h)::(map f t) Map applies an arbitrary function f To each element of a list And returns the resulting modified list Can we encode this in Java? Using object oriented programming 9

A Map Method for Stack Problem Write a map method in Java Must pass a function into another function Solution Can be done using an object with a known method Use interface to specify what method must be present public interface Function { Integer eval(integer arg); } 10

A Map Method for Stack (cont.) Examples Two classes which both implement Function interface class AddOne implements Function { Integer eval(integer arg) { return new Integer(arg + 1); } } class MultTwo implements Function { Integer eval(integer arg) { return new Integer(arg * 2); } } 11

The New Stack Class class Stack { class Node { Integer val; Node next; Node (Integer v, Node n) { val = v; next = n; } Entry map(function f) { if (next == null) return new Node(f.eval(val), null); else return new Node(f.eval(val), next.map(f)); } } Node thestack;... Stack map(function f) { Stack s = new Stack(); s.thestack = thestack.map(f); return s; } } 12

Applying Map To A Stack Then to apply the function, we just do Stack s =...; Stack t = s.map(new AddOne()); Stack u = s.map(new MultTwo()); We make a new object That has a method that performs the function we want This is sometimes called a callback Because map calls back to the object passed into it But it s really just a higher-order function Written more awkwardly 13

Relating Closures and Objects let app f x = f x a = 3 fun b -> a + b let add a b = a + b;; let f = add 3;; app f 4;; interface F { Integer eval(integer y); } class C { static Integer app(f f, Integer x) { return f.eval(x); } } class G implements F { Integer a; G(Integer a) { this.a = a; } Integer eval(integer y) { return new Integer(a + y); } } F adder = new G(3); C.app(adder, 4); a = 3 14

Code as Data Closures and objects are related Both of them allow Data to be associated with higher-order code Pass code around the program The key insight in all of these examples Treat code as if it were data Allowing code to be passed around the program And invoked where it is needed (as callback) Approach depends on programming language Higher-order functions (OCaml, Ruby, Lisp) Function pointers (C, C++) Objects with known methods (Java) 15

Code as Data (cont.) This is a powerful programming technique Solves a number of problems quite elegantly Create new control structures (e.g., Ruby iterators) Add operations to data structures (e.g., visitor pattern) Event-driven programming (e.g., observer pattern) Keeps code separate Clean division between higher & lower-level code Promotes code reuse Lower-level code supports different callbacks 16

Lambda Calculus 17

Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Use currying or tuples Loops while (a < b) Use recursion Side effects a := 1 Use functional programming So what language features are really needed? 18

Turing Completeness Computational system that can Simulate a Turing machine Compute every Turing-computable function A programming language is Turing complete if It can map every Turing machine to a program A program can be written to emulate a Turing machine It is a superset of a known Turing-complete language Most powerful programming language possible Since Turing machine is most powerful automaton 19

Programming Language Theory Come up with a core language That s as small as possible But still Turing complete Helps illustrate important Language features Algorithms One solution Lambda calculus 20

Lambda Calculus (λ-calculus) Proposed in 1930s by Alonzo Church Stephen Cole Kleene Formal system Designed to investigate functions & recursion For exploration of foundations of mathematics Now used as Tool for investigating computability Basis of functional programming languages Lisp, Scheme, ML, OCaml, Haskell 21

Lambda Expressions A lambda calculus expression is defined as e ::= x variable λx.e function e e function application λx.e is like (fun x -> e) in OCaml That s it! Nothing but higher-order functions 22

Three Conveniences Syntactic sugar for local declarations let x = e1 in e2 is short for (λx.e2) e1 Scope of λ extends as far right as possible Subject to scope delimited by parentheses λx. λy.x y is same as λx.(λy.(x y)) Function application is left-associative x y z is (x y) z Same rule as OCaml 23

Lambda Calculus Semantics All we ve got are functions So all we can do is call them To evaluate (λx.e1) e2 Evaluate e1 with x bound to e2 This application is called beta-reduction (λx.e1) e2 e1[x/e2] e1[x/e2] is e1 where occurrences of x are replaced by e2 We allow reductions to occur anywhere in a term 24

Beta Reduction Example (λx.λz.x z) y (λx.(λz.(x z))) y (λx.(λz.(x z))) y // since λ extends to right // apply (λx.e1) e2 e1[x/e2] // where e1 = λz.(x z), e2 = y λz.(y z) // final result Equivalent OCaml code Parameters Formal Actual (fun x -> (fun z -> (x z))) y fun z -> (y z) 25

Lambda Calculus Examples (λx.x) z z (λx.y) z y (λx.x y) z z y A function that applies its argument to y 26

Lambda Calculus Examples (cont.) (λx.x y) (λz.z) (λz.z) y y (λx.λy.x y) z λy.z y A curried function of two arguments Applies its first argument to its second (λx.λy.x y) (λz.zz) x λy.((λz.zz)y)x (λz.zz)x xx 27

Static Scoping & Alpha Conversion Lambda calculus uses static scoping Consider the following (λx.x (λx.x)) z? The rightmost x refers to the second binding This is a function that Takes its argument and applies it to the identity function This function is the same as (λx.x (λy.y)) Renaming bound variables consistently is allowed This is called alpha-renaming or alpha conversion Ex. λx.x = λy.y = λz.z λy.λx.y = λz.λx.z 28

Static Scoping (cont.) How about the following? (λx.λy.x y) y? When we replace y inside, we don t want it to be captured by the inner binding of y I.e., (λx.λy.x y) y λy.y y Solution (λx.λy.x y) is the same as (λx.λz.x z) Due to alpha conversion So change (λx.λy.x y) y to (λx.λz.x z) y first Now (λx.λz.x z) y λz.y z 29

Beta-Reduction, Again Whenever we do a step of beta reduction (λx.e1) e2 e1[x/e2] We must first alpha-convert variables as necessary Usually performed implicitly (w/o showing conversion) Examples (λx.λy.x y) y = (λx.λz.x z) y λz.y z // y z (λx.x (λx.x)) z = (λy.y (λx.x)) z z (λx.x) // x y (λx.x (λx.x)) z = (λx.x (λy.y)) z z (λy.y) // x y 30

Encodings The lambda calculus is Turing complete Means we can encode any computation we want If we re sufficiently clever... Examples Booleans Pairs Natural numbers & arithmetic Looping 31

Booleans Church s encoding of mathematical logic true = λx.λy.x false = λx.λy.y if a then b else c Examples if true then b else c (λx.λy.x) b c (λy.b) c b if false then b else c (λx.λy.y) b c (λy.y) c c 32

Booleans (cont.) Other Boolean operations not = λx.((x false) true) not true λx.((x false) true) true ((true false) true) false and = λx.λy.((xy) false) or = λx.λy.((x true) y) Given these operations Can build up a logical inference system 33

Discussion Lambda calculus is Turing-complete Most powerful language possible Can represent pretty much anything in real language Using clever encodings But programs would be Pretty slow Pretty large Pretty hard to understand In practice We use richer, more expressive languages That include built-in primitives 34

The Need For Types Consider the untyped lambda calculus false = λx.λy.y 0 = λx.λy.y Since everything is encoded as a function... We can easily misuse terms false 0 λy.y if 0 then... because everything evaluates to some function The same thing happens in assembly language Everything is a machine word (a bunch of bits) All operations take machine words to machine words 35

Simply-Typed Lambda Calculus e ::= n x λx:t.e e e Added integers n as primitives Need at least two distinct types (integer & function) to have type errors Functions now include the type of their argument 36

Simply-Typed Lambda Calculus (cont.) t ::= int t t int is the type of integers t1 t2 is the type of a function That takes arguments of type t1 and returns result of type t2 t1 is the domain and t2 is the range Notice this is a recursive definition So we can give types to higher-order functions Will show how to compute types later Example of operational semantics 37

Summary Lambda calculus shows issues with Scoping Higher-order functions Types Useful for understanding how languages work 38