Introduction to functional programming using Ocaml

4,528 views

Published on

Key Functional Programming concepts illustrated using Ocaml

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,528
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
72
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Introduction to functional programming using Ocaml

  1. 1. Introduction to Functional Programming with Ocaml Pramode C.E http://pramode.net January 30, 2014
  2. 2. Workshop Plan Here is what we will do: Learn some Ocaml syntax and write simple Ocaml programs Learn important concepts like: closures, higher order functions, purity, lazy vs strict evaluation, currying, tail calls/TCO, immutability, persistent data structures, type inference etc! Understand the essence of the "Functional Programming" paradigm through Ocaml Not beginner-level stu - but I will try to keep things as simple as possible! Pramode C.E Introduction to Functional Programming with Ocaml
  3. 3. Ocaml - some background info Written in 1996 by Xavier Leroy and other researchers at INRIA, France A member of the ML family http://en.wikipedia.org/wiki/ML_(programming_language) Supports Functional, Imperative and Object oriented programming A powerful static type system with type inference Major inuence behind Microsoft F# Recent developments: the Core standard library (by Jane Street) and realworldocaml.org Pramode C.E Introduction to Functional Programming with Ocaml
  4. 4. Pramode C.E Introduction to Functional Programming with Ocaml
  5. 5. Where is it being used? List of companies: http://ocaml.org/learn/companies.html Facebook - pf, a tool for analysing PHP source Citrix - Uses Ocaml in XenServer, a virtualization system Jane Street - develops trading systems in Ocaml Bloomberg - uses Ocaml for developing nancial derivatives risk management applications Pramode C.E Introduction to Functional Programming with Ocaml
  6. 6. The Ocaml REPL Invoke the REPL with the utop command at the shell prompt: utop # print_string hellon;; hello - : unit = () utop # 2 * 3;; - : int = 6 utop # let x = 10;; val x : int = 10 utop # x * 2;; - : int = 20 utop # let y = 5 in y * 8;; - : int = 40 utop # Question: what is the dierence between let and let ... in? Pramode C.E Introduction to Functional Programming with Ocaml
  7. 7. Function Denition let add a b = a + b let m = add 10 20 We dene add to be a function which accepts two parameters of type int and returns a value of type int. Similarly, m is dened as a variable of type int. But wait ... we haven't actually dened all these types! Pramode C.E Introduction to Functional Programming with Ocaml
  8. 8. Type Inference We have NOT specied the return type of the function or the type of the variable m. Ocaml infers that (the technical term is HindleyMilner type inference) This is the type which utop shows us: val add : int - int - int Read this as: function takes two int's as arguments and returns an int. Pramode C.E Introduction to Functional Programming with Ocaml
  9. 9. Static vs Dynamic typing #Python - dynamic typing def foo(x): return x/2 /* C - static typing with NO type inference */ int foo(int x) { return x / 2; } (* ML - static typing with type inference *) let foo x = x / 2 Pramode C.E Introduction to Functional Programming with Ocaml
  10. 10. Expression Oriented Programming let i = 0 let p = if (i 0) then -1 else -2 let q = if (true) then hello else world Unlike languages like C/Java, almost everything in Ocaml is an expression, ie, something which computes a value! Rather than programming with statements, we program with expressions. Pramode C.E Introduction to Functional Programming with Ocaml
  11. 11. Recursion (* compute sum of first n numbers *) let rec sum n = if (n = 0) then 0 else n + sum (n - 1) let r = sum 10 Try calling the function sum with a large number (say 1000000) as parameter! You get a stack overow! Pramode C.E Introduction to Functional Programming with Ocaml
  12. 12. Tail Calls and TCO (* compute sum of first n numbers *) let rec sum n acc = if (n = 0) then acc else sum (n - 1) (acc + n) let r = sum 10 0 This is a tail-recursive version of the previous function - the Ocaml compiler converts the tail call to a loop, thereby avoiding stack overow! Pramode C.E Introduction to Functional Programming with Ocaml
  13. 13. Tail Calls and TCO (sum 4) (4 + sum 3) (4 + (3 + sum 2)) (4 + (3 + (2 + sum 1))) (4 + (3 + (2 + (1 + sum 0)))) (4 + (3 + (2 + (1 + 0)))) (4 + (3 + (2 + 1))) (4 + (3 + 3)) (4 + 6) (10) -----------------------------(sum 4 0) (sum 3 4) (sum 2 7) (sum 1 9) (sum 0 10) (10) Pramode C.E Introduction to Functional Programming with Ocaml
  14. 14. Parametric polymorphism let foo x = x Our function foo is the identity function; it takes in a value and returns the same! What is the inferred type of this function? More examples: let foo x y = x let foo2 x y = if true then x else y let foo3 x y = if x then y else y let foo4 x y = if x then y else x Pramode C.E Introduction to Functional Programming with Ocaml
  15. 15. Higher order functions let double x = 2*x let triple x = 3*x let apply f x = f x let p = apply double 10 let q = apply triple 10 What is type of apply? Pramode C.E Introduction to Functional Programming with Ocaml
  16. 16. Summation once again! let sqr x = x * x let cube x = x * x * x let rec sumSimple a b = if a = b then a else a + sumSimple (a + 1) b let rec sumSquares a b = if a = b then (sqr a) else (sqr a) + sumSquares (a + 1) b let rec sumCubes a b = if a = b then (cube a) else (cube a) + sumCubes (a + 1) b Pramode C.E Introduction to Functional Programming with Ocaml
  17. 17. Summation - a more elegant method let identity x = x let sqr x = x * x let cube x = x * x * x let rec sum f a b = if a = b then (f a) else (f a) + sum f (a + 1) b let p = sum identity 0 4 let q = sum sqr 0 4 let r = sum cube 0 4 Pramode C.E Introduction to Functional Programming with Ocaml
  18. 18. Anonymous functions let apply f x = f x let p = apply (fun x - 2*x) 10 We can create anonymous functions on-the-y! fun x - 2*x is a function which takes an x and returns 2*x. Pramode C.E Introduction to Functional Programming with Ocaml
  19. 19. Methods on collections: Map/Filter/Reduce open Core.Std let a = [1;2;3;4] let p = List.map a (fun x - x * x) let q = List.filter a (fun x - (x mod 2) = 0) let r = List.reduce a (fun x y - x + y) Map applies a function on all elements of a sequence. Filter selects a set of values from a sequence based on the boolean value returned by a function passed as its parameter - both functions return a new sequence. Reduce combines the elements of a sequence into a single element. Pramode C.E Introduction to Functional Programming with Ocaml
  20. 20. More methods on collections open Core.Std let even x = (x mod 2) = 0 let a = [1;2;3;4;5;6;7] let b = [2;4;6;5;10;11;13;12] let let let let c d e f = = = = List.for_all a even List.exists a even List.take_while a even List.partition_tf a even Pramode C.E Introduction to Functional Programming with Ocaml
  21. 21. Nested functions / functions returning functions (* foo returns sqr *) let foo () = let sqr x = x * x in sqr let f = foo () let g = f 10 Pramode C.E Introduction to Functional Programming with Ocaml
  22. 22. Lexical Closure let foo1 x = let foo2 y = x + y in foo2 let f = foo1 10 let r = f 20 Pramode C.E Introduction to Functional Programming with Ocaml
  23. 23. Lexical Closure The function foo1 returns a closure. A closure is a function which carries with it references to the environment in which it was dened. When we call f(20), the foo2 function gets executed with the environment it had when it was dened - in this environment, the value of x is 10. Pramode C.E Introduction to Functional Programming with Ocaml
  24. 24. Simple closure examples let sqr x = x * x let cube x = x * x * x let compose f g = fun x - f (g x) let f = compose sqr cube let m = f 2 Pramode C.E Introduction to Functional Programming with Ocaml
  25. 25. Simple closure examples open Core.Std let remove_low_scores xs threshold = List.filter xs (fun score - score = threshold) let m = remove_low_scores [20; 35; 80; 92; 40; 98] 50 The anonymous function fun score - score = threshold is the closure here. How do you know that it is a closure? Its body uses a variable threshold which is not in its local environment (the local environment, in this case, is the parameter list consisting of a single parameter score) Pramode C.E Introduction to Functional Programming with Ocaml
  26. 26. Currying and Partial Application Here is the denition from Wikipedia: In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or a tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument. It was originated by Moses Schonnkel and later re-discovered by Haskell Curry. Pramode C.E Introduction to Functional Programming with Ocaml
  27. 27. Currying and Partial Application let add x y = x + y let f = add 10 let g = f 20 The type of add is int - int - int. The correct way to read this is: add takes an int as argument and returns a function which maps an int to an int. Pramode C.E Introduction to Functional Programming with Ocaml
  28. 28. Currying and Partial Application let add x y z = x + y + z let f = add 10 let g = f 20 let h = g 30 Pramode C.E Introduction to Functional Programming with Ocaml
  29. 29. Algebraic Data Types - Product types let a = (1, 2) (* type: int*int *) let b = (hello, 10, 2) (* type: string*int*int *) An Algebraic Data Type is a composite type - a type formed by combining other types. Two classes of algebraic data types are the product and sum types. Pramode C.E Introduction to Functional Programming with Ocaml
  30. 30. Algebraic Data Types - Sum types type shape = Circle of int | Square of int | Rectangle of (int*int) let a = Circle 10 (* circle of radius 10 *) let b = Square 20 (* square with sides 20 *) let c = Rectangle (5,10) (* rectangle with sides 5 and 10 *) The type shape describes a shape which can be one of Circle, Square or Rectangle. Pramode C.E Introduction to Functional Programming with Ocaml
  31. 31. Pattern Matching let a = (1, (2, hello)) let (x, (y, z)) = a Pattern matching helps you pull apart complex datastructures and analyse their components in an elegant way (not to be confused with regular expression based pattern matching which is a completely dierent thing). Pramode C.E Introduction to Functional Programming with Ocaml
  32. 32. Pattern Matching type shape = Circle of float | Square of float | Rectangle of (float*float) let area s = match s with | Circle r - 3.14 *. r *. r | Square x - x *. x | Rectangle (x, y) - x *. y let a = area (Circle 10.0) let b = area (Square 20.0) let c = area (Rectangle (5.0,10.0)) Pramode C.E Introduction to Functional Programming with Ocaml
  33. 33. Pattern Matching type shape = Circle of float | Square of float | Rectangle of (float*float) let area s = match s with | Circle r - 3.14 *. r *. r | Square x - x *. x let a = area (Circle 10.0) let b = area (Square 20.0) let c = area (Rectangle (5.0,10.0)) The compiler warns us when it detects that the match is not exhaustive. Pramode C.E Introduction to Functional Programming with Ocaml
  34. 34. Pattern Matching let rec len xs = match xs with | [] - 0 | hd :: tl - 1 + len tl let a = len [1;2;3;4] A simple recursive routine for computing length of a list. If list is empty, length is 0, otherwise, it is 1 + length of remaining part of the list. Pramode C.E Introduction to Functional Programming with Ocaml
  35. 35. Immutability and the nature of variables let a = 1 let foo x = x + a; a = 2 (* simply compares a with 2 *) let a = 0 (* earlier binding gets shadowed *) let m = foo 10 (* result is 11 *) let bindings are immutable; you can shadow a binding, but an earlier binding can't be mutated. Pramode C.E Introduction to Functional Programming with Ocaml
  36. 36. Immutability Let's look at some Python list manipulation code: a = [20, 30, 40, 50] a.insert(0, 10) print a # prints [10, 20, 30, 40, 50] Python lists are mutable. The insert operation modies the original list. Pramode C.E Introduction to Functional Programming with Ocaml
  37. 37. Immutability Here is a similar program in Ocaml: let a = [20;30;40;50] let b = 10::a (* a remains unchanged *) let c = [60;70;80] let d = a @ c (* a remains unchanged *) Good Ocaml style encourages use immutable data structures. Pramode C.E Introduction to Functional Programming with Ocaml
  38. 38. Immutability: Structure sharing This is one way to implement 4::b - but it requires creating a full copy of the old list b (red-colored cells 1, 2, 3 are copies of cells containing the same values in b) and adding 4 at the head position. Ocaml does NOT do it this way because it is very inecient. Pramode C.E Introduction to Functional Programming with Ocaml
  39. 39. Immutability: Structure sharing This is how Ocaml actually does 4::b. Note that the new list shares all its elements (except 4) with the old list! Pramode C.E Introduction to Functional Programming with Ocaml
  40. 40. Immutability: Structure sharing This is what happens when you append two lists A and B (using @ in Ocaml). Note that the appending operation needs to copy the whole of list A. Pramode C.E Introduction to Functional Programming with Ocaml
  41. 41. Immutability: Structure sharing The structure sharing idea can be used eciently with tree data structures. For more info: http: //en.wikipedia.org/wiki/Persistent_data_structure Persistent vectors in Scala: http://www.codecommit.com/blog/scala/ implementing-persistent-vectors-in-scala Pramode C.E Introduction to Functional Programming with Ocaml
  42. 42. Mutability and aliasing a = [1,2,3,4] b = a b.append(5) print a Having aliases to mutable objects can create very hard to track down bugs in our code. Pramode C.E Introduction to Functional Programming with Ocaml
  43. 43. Non-strict evaluation let my_if cond then_part else_part = if cond then then_part else else_part let a = my_if (1 2) 10 20 We are trying to write a function which behaves similar to the built-in if control structure in Ocaml ... does it really work properly? Let's try another example! Pramode C.E Introduction to Functional Programming with Ocaml
  44. 44. Non-strict evaluation let foo1 () = print_string foo1n; 10 let foo2 () = print_string foo2n; 20 let my_if cond then_part else_part = if cond then then_part else else_part let a = my_if (1 2) (foo1()) (foo2()) Pramode C.E Introduction to Functional Programming with Ocaml
  45. 45. Non-strict evaluation The behaviour of if is non-strict: In the expression if (cond) then e1 else e2, if cond is true e2 is NOT EVALUATED. Also, if cond is false, e1 is NOT EVALUATED. By default, the behaviour of function calls in Ocaml is strict: In the expression fun e1 e2 ... en, ALL the expressions e1, e2 ... en are evaluated before the function is called. Pramode C.E Introduction to Functional Programming with Ocaml
  46. 46. Non-strict evaluation let foo1 () = print_string foo1n; 10 let foo2 () = print_string foo2n; 20 let my_if cond then_part else_part = if cond then (then_part()) else (else_part()) let a = my_if (1 2) (fun () - (foo1())) (fun () - (foo2())) Pramode C.E Introduction to Functional Programming with Ocaml
  47. 47. Referential Transparency If an expression can be replaced by its value without changing the behaviour of the program, it is said to be referentially transparent All occurrences of the expression 1+(2*3) can be replaced by 7 without changing the behaviour of the program. Say the variable x (in a C program) has initial value 5. It is NOT possible to replace all occurrences of the statement ++x with the value 6. Pramode C.E Introduction to Functional Programming with Ocaml
  48. 48. Pure Functions #include stdio.h int balance = 1000; int withdraw(int amt) { balance = balance - amt; return balance; } int main() { printf(%dn, withdraw(100)); printf(%dn, withdraw(100)); return 0; } Pramode C.E Introduction to Functional Programming with Ocaml
  49. 49. Pure Functions A pure function always computes the same value given the same parameters; for example, sin(0) is always 0. It is Referentially Transparent. Evaluation of a pure function does not cause any observable side eects or output - like mutation of global variables or output to I/O devices. Pramode C.E Introduction to Functional Programming with Ocaml
  50. 50. What is Functional Programming? A style of programming which emphasizes composing your program out of PURE functions and immutable data. Theoretical foundation based on Alonzo Church's Lambda Calculus In order for this style to be eective in the construction of real world programs, we make use of most of the ideas seen so far (higher order functions, lexical closures, currying, immutable and persistent datastructures, lazy evaluation etc) Pramode C.E Introduction to Functional Programming with Ocaml
  51. 51. What is Functional Programming? Questions to ask: Is this the silver bullet? How practical is a program composed completely out of pure functions? What are the benets of writing in a functional style? Pramode C.E Introduction to Functional Programming with Ocaml
  52. 52. Is FP the silver bullet? Of course, there are NO silver bullets! (http://en.wikipedia.org/wiki/No_Silver_Bullet) Writing software is tough - no one methodology or technique is going to solve all your problems Pramode C.E Introduction to Functional Programming with Ocaml
  53. 53. How practical is a program composed completely out of pure functions? Very impractical - unless your aim is to ght the chill by heating up the CPU (which, by the way, is a side eect) The idea is to write your program in such a way that it has a purely functional core, surrounded by a few impure functions at the outer layers Pramode C.E Introduction to Functional Programming with Ocaml
  54. 54. Functional core + impure outer layers This example is simple and contrived, but it serves to illustrate the idea. It is taken from the amazing book Functional Programming in Scala by Paul Chiusano and Runar Bjarnason. let declareWinner p = let (name, score) = p in print_string (name ^ n) let winner p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then (declareWinner p1) else (declareWinner p2) Note that winner is an impure function. We will now refactor it a little! Pramode C.E Introduction to Functional Programming with Ocaml
  55. 55. Functional core + impure outer layers let declareWinner p = let (name, score) = p in print_string (name ^ n) let maxScore p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then p1 else p2 let winner p1 p2 = declareWinner (maxScore p1 p2) Now we have separated the computation from the display logic; maxScore is a pure function and winner is the impure function at the outer layer! Pramode C.E Introduction to Functional Programming with Ocaml
  56. 56. Benets of functional style - easy reuse, easy testing What if we wish to nd out the winner among a set of N players? Easy! open Core.Std let maxScore p1 p2 = let ((_, score1), (_, score2)) = (p1, p2) in if (score1 score2) then p1 else p2 let a = [(Ram, 68); (John, 72); (Arun, 57)] let b = List.reduce a maxScore Pramode C.E Introduction to Functional Programming with Ocaml
  57. 57. FP as good software engineering Pure functions are easy to re-use as they have no context other than the function parameters. (Think about re-using the winner function in our rst version to compute the winner among N players). Pure functions are also easy to test. (Think about writing an automated test for the winner function in the rst version). Think of FP as Good Software Engineering! Pramode C.E Introduction to Functional Programming with Ocaml
  58. 58. Pure functions and the benet of local reasoning If your function modies an object which is accessible from many other functions, the eect of calling the function is much more complex to analyse because you now have to analyse how all these other functions get aected by the mutation. Similarly, if the value computed by your function depends on the value of an object which can be modied by many other functions, it no longer becomes possible to reason about the working of the function by only looking at the way the function's parameters are manipulated. The evaluation of pure functions can be done by a very simple process of substitution. Pramode C.E Introduction to Functional Programming with Ocaml
  59. 59. Mutability and concurrency Multi core CPU's are becoming commonplace We need concurrency in our code to make eective use of the many cores This throws up a whole bunch of complex problems A function can no longer assume that nobody else is watching when it is happily mutating some data Pramode C.E Introduction to Functional Programming with Ocaml
  60. 60. Mutability and Concurrency current_date = [2014, January, 7] def changeDate(year, month, day): current_date[0] = year current_date[1] = month current_date[2] = day def showDate(): print current_date Pramode C.E Introduction to Functional Programming with Ocaml
  61. 61. Mutability and Concurrency What happens if the functions changeDate() and showDate() run as two independent threads on two CPU cores? Will showDate() always see a consistent date value? The traditional approach to maintaining correctness in the context of multithreading is to use locks - but people who do it in practice will tell you it is extremely tricky business. It is claimed that one of the reasons for the resurgence of FP is the emergence of multi-core processors and concurrency - it seems like FP's emphasis on pure functions and immutability is a good match for concurrent programming. Pramode C.E Introduction to Functional Programming with Ocaml
  62. 62. Moving ahead ... Prof. Gregor Kiczales Introduction to Systematic Program Design is a great online course for beginners: https://www.coursera.org/course/programdesign Join Prof.Grossman's programming languages class on Coursera: https://www.coursera.org/course/proglang and learn more about functional programming using SML and Racket Join Prof.Odersky's Functional Programming with Scala course on Coursera for an introduction to both Scala and FP: https://www.coursera.org/course/progfun Watch the classic SICP lectures by Abelson and Sussman: http://groups.csail.mit.edu/mac/classes/6.001/ abelson-sussman-lectures/ A talk by Yaron Minsky of Jane Street on how to program eectively in ML: http://vimeo.com/14313378 Pramode C.E Introduction to Functional Programming with Ocaml
  63. 63. Books (not limited to Ocaml) Ocaml from the very beginning is a gentle introduction to the language suitable for beginners: http://ocaml-book.com/ Real world Ocaml is the book which will take you to Ocaml mastery: http://realworldocaml.org SICP, the classic: http://mitpress.mit.edu/sicp/ HTDP: http://www.ccs.neu.edu/home/matthias/HtDP2e/ CTM: http://www.info.ucl.ac.be/~pvr/book.html Pramode C.E Introduction to Functional Programming with Ocaml
  64. 64. Other resources (not limited to Ocaml) Ocaml for the masses - ACM Queue paper by Yaron Minsky http://queue.acm.org/detail.cfm?id=2038036 Out of the tar pit - paper by Ben Mosely and Peter Marks (google it) Lectures by Eric Meijer: google for C9 Lectures Erik Meijer functional Persistent Data Structures and Managed References - talk by Rich Hickey (author of the Clojure programming language). http://www.infoq.com/presentations/ Value-Identity-State-Rich-Hickey Can programming be liberated from the von Neumann style - by John Backus. http://www.thocp.net/biographies/ papers/backus_turingaward_lecture.pdf Pramode C.E Introduction to Functional Programming with Ocaml

×