CMSC 330: Organization of Programming Languages Functional Programming with OCaml.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
CMSC 330: Organization of Programming Languages Evaluation Strategies.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 8 - Control II: Procedures and Environments
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
CMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
CMSC 330: Organization of Programming Languages Operational Semantics.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Closure Compiler Baojian Hua Closure An implementation technique traditionally for functional languages Lisp, Scheme, Haskell, Ocaml,
CMSC 330: Organization of Programming Languages Object Oriented Programming with OCaml.
Functions CSC 358/
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
Principles of programming languages 4: Parameter passing, Scope rules
Memory Locations For Variables
CMSC 330: Organization of Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
CSCE 314: Programming Languages Dr. Dylan Shell
CISC124 Labs start this week in JEFF 155. Fall 2018
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
Function.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
PROGRAMMING IN HASKELL
Peer Instruction 4 Control Loops.
CSE 341 Lecture 11 b closures; scoping rules
PROGRAMMING IN HASKELL
CSC215 Lecture Control Flow.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
Function.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

CMSC 330: Organization of Programming Languages Functional Programming with OCaml

CMSC 3302 Review Recursion is how all looping is done OCaml can easily pass and return functions

CMSC 3303 The Call Stack in C/Java/etc. void f(void) { int x; x = g(3); } x int g(int x) { int y; y = h(x); return y; } int h (int z) { return z + 1; } x 3 y z int main(){ f(); return 0; } f g h

CMSC 3304 Nested Functions In OCaml, you can define functions anywhere –Even inside of other functions let pick_one n = if n > 0 then (fun x -> x + 1)‏ else (fun x -> x - 1)‏ (pick_one -5) 6 (* returns 5 *)‏ let sum l = fold ((fun (a, x) -> a + x), 0, l)‏

CMSC 3305 Nested Functions (cont’d)‏ You can also use let to define functions inside of other functions let sum l = let add (a, x) = a + x in fold (add, 0, l)‏ let pick_one n = let add_one x = x + 1 in let sub_one x = x - 1 in if n > 0 then add_one else sub_one

CMSC 3306 How About This? –(Equivalent to...)‏ let addN (n, l) = let add x = n + x in map (add, l)‏ Accessing variable from outer scope let addN (n, l) = map ((fun x -> n + x), l)‏ takes a number n and list l and adds n to every element in l

CMSC 3307 Consider the Call Stack Again How does add know the value of n? –Read it off the stack? Possible, but the semantics are wrong (see above)‏ –OCaml uses static scoping like C, C++, Java, and Ruby let addN (n, l) = map (add, l)‏ let map (f, n) = match n with [] -> [] | (h::t) -> (f h)::(map (f, t))‏ addN (3, [1; 2; 3])‏ let add x = n + x in n 3 l f n x 1

CMSC 3308 Static Scoping In static or lexical scoping, (nonlocal) names refer to their nearest binding in the program text –Going from inner to outer scope –C example: –In our example, add accesses addN’s n int x; void f() { x = 3; } void g() { char *x = "hello"; f(); } Refers to the x at file scope – that’s the nearest x going from inner scope to outer scope in the source code

CMSC 3309 Returned Functions As we saw, in OCaml a function can return another function as a result –So consider the following example –When the anonymous function is called, n isn’t even on the stack any more! We need some way to keep n around after addN returns let addN n = (fun x -> x + n)‏ (addN 3) 4 (* returns 7 *)‏

CMSC Environments and Closures An environment is a mapping from variable names to values –Just like a stack frame A closure is a pair (f, e) consisting of function code f and an environment e When you invoke a closure, f is evaluated using e to look up variable bindings

CMSC Example let add x = (fun y -> x + y)‏ (add 3) 4→ 4 → → 7

CMSC Another Example let mult_sum (x, y) = let z = x + y in fun w -> w * z (mult_sum (3, 4)) 5→ 5→ 5 * 7 → 35

CMSC Yet Another Example let twice (n, y) = let f x = x + n in f (f y)‏ twice (3, 4)‏→ ( 4)‏→ 7→ 10

CMSC Still Another Example let add x = (fun y -> (fun z -> x + y + z))‏ (((add 1) 2) 3)‏→ (( 2) 3)‏→ ( 3)‏→ 1+2+3

CMSC Currying We just saw another way for a function to take multiple arguments –The function consumes one argument at a time, creating closures until all the arguments are available This is called currying the function –Discovered by Schönfinkel and Frege –Named after the logician Haskell B. Curry Also called partial evaluation

CMSC Curried Functions in OCaml OCaml has a really simple syntax for currying –This is identical to all of the following: Thus: –add has type int -> (int -> int)‏ –add 3 has type int -> int The return of add x evaluated with x = 3 add 3 is a function that adds 3 to its argument –(add 3) 4 = 7 This works for any number of arguments let add x y = x + y let add = (fun x -> (fun y -> x + y))‏ let add = (fun x y -> x + y)‏ let add x = (fun y -> x+y)‏

CMSC Curried Functions in OCaml (cont’d)‏ Because currying is so common, OCaml uses the following conventions: –-> associates to the right Thus int -> int -> int is the same as int -> (int -> int)‏ –function application associates to the left Thus add 3 4 is the same as (add 3) 4

CMSC Another Example of Currying A curried add function with three arguments: –The same as Then... –add_th has type int -> (int -> (int -> int))‏ –add_th 4 has type int -> (int -> int)‏ –add_th 4 5 has type int -> int –add_th is 15 let add_th x y z = x + y + z let add_th x = (fun y -> (fun z -> x+y+z))‏

CMSC Currying and the map Function Examples let negate x = -x map negate [1; 2; 3] (* returns [-1; -2; -3 ] *)‏ let negate_list = map negate negate_list [-1; -2; -3] let sum_pairs_list = map (fun (a, b) -> a + b)‏ sum_pairs_list [(1, 2); (3, 4)] (* [3; 7] *)‏ What's the type of this form of map ? let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t)‏ map : ('a -> 'b) -> 'a list -> 'b list

CMSC Currying and the fold Function let rec fold f a l = match l with [] -> a | (h::t) -> fold f (f a h) t let add x y = x + y fold add 0 [1; 2; 3] let sum = fold add 0 sum [1; 2; 3] let next n _ = n + 1 let length = fold next 0 (* warning: not polymorphic *)‏ length [4; 5; 6; 7] What's the type of this form of fold ? fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

CMSC Another Convention New keyword: function can often be used instead of match –function declares an anonymous function of one argument –Instead of –It could be written let rec sum l = match l with [] -> 0 | (h::t) -> h + (sum t)‏ let rec sum = function [] -> 0 | (h::t) -> h + (sum t)‏

CMSC Another Convention (cont’d)‏ Instead of It could be written let rec map f l = match l with [] -> [] | (h::t) -> (f h)::(map f t)‏ let rec map f = function [] -> [] | (h::t) -> (f h)::(map f t)‏

CMSC Currying is Standard in OCaml Pretty much all functions are curried –Like the standard library map, fold, etc. OCaml plays a lot of tricks to avoid creating closures and to avoid allocating on the heap –It's unnecessary much of the time, since functions are usually called with all arguments How does this compare to C or Ruby?

CMSC Higher-Order Functions in C C has function pointers but no closures –(gcc has closures)‏ typedef int (*int_func)(int); void app(int_func f, int *a, int n) { int i; for (i = 0; i < n; i++)‏ a[i] = f(a[i]); } int add_one(int x) { return x + 1; } int main() { int a[] = {1, 2, 3, 4}; app(add_one, a, 4); }

CMSC Higher-Order Functions in Ruby Use yield within a method to call a code block argument def my_collect(a)‏ b = Array.new(a.length)‏ i = 0 while i < a.length b[i] = yield(a[i])‏ i = i + 1 end return b end b = my_collect([1, 2, 3, 4, 5]) { |x| -x }

CMSC Higher-Order Functions in Java/C++ An object in Java or C++ is kind of like a closure –it’s some data (like an environment)‏ –along with some methods (i.e., function code)‏ So objects can be used to simulate closures

CMSC Exercises let times x y = x * y in let mystery w = times w in mystery 42 2 What is the type of times? What is the type of mystery? What is the final return value?

CMSC Exercises let times x y = x * y in let mystery w = times w in mystery 42 2 What is the type of times? int -> int -> int What is the type of mystery? int -> int -> int What is the final return value? 84

CMSC Exercises let bigger x y = if y > x then y else x in let biggest = function h::t -> fold bigger h t in biggest [1; 2; 3; 2; 1] What is the type of bigger? What is the type of biggest? What is the final return value?

CMSC Exercises let bigger x y = if y > x then y else x in let biggest = function h::t -> fold bigger h t in biggest [1; 2; 3; 2; 1] What is the type of bigger? 'a -> 'a -> 'a What is the type of biggest? 'a list -> 'a What is the final return value? 3

CMSC Exercises let foo a b = (a, b) in let bar x = map (foo x) in bar 4 [1; 2; 3] What is the type of foo? What is the type of bar? What is the final return value?

CMSC Exercises let foo a b = (a, b) in let bar x = map (foo x) in bar 4 [1; 2; 3] What is the type of foo? 'a -> 'b -> 'a * 'b What is the type of bar? 'a -> 'b list -> ('a * 'b) list What is the final return value? [(4, 1); (4, 2); (4, 3)]