Functional Programming
Mayer Goldberg \ Ben-Gurion University February 25, 2019
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 1 / 63
Functional Programming February 25, 2019 Functional Programming - - PowerPoint PPT Presentation
Functional Programming February 25, 2019 Functional Programming February 25, 2019 1 / 63 Mayer Goldberg \ Ben-Gurion University Mayer Goldberg \ Ben-Gurion University Functional Programming Contact information February 25, 2019 2 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 1 / 63
▶ Mayer Goldberg ▶ Email: gmayer@little-lisper.org ▶ Offjce Hours: Mondays, 14-16 (37:106), just before class ▶ Phone: 055-6603392 (cellular)
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 2 / 63
▶ Homework: 20% ▶ Final exam: 70% ▶ Class participation: 10%
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 3 / 63
▶ λ-Defjnability ▶ Tuples & RTTI ▶ Bases ▶ Fixed-Point Theory ▶ Functions as Data ▶ Streams ▶ Maps & Folds ▶ CPS & Threading Code ▶ Monads ▶ Possibly additional topics, depending on time
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 4 / 63
▶ Understanding the ”big picture” ▶ Establishing common language & vocabulary
▶ Imperative programming ▶ Functional programming ▶ The advantages of functional programming
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 5 / 63
▶ You’ve already been exposed to functional programming in your
▶ Functional languages are often described as languages lacking in
▶ Imperative languages have side efgects
▶ What can we say positively about functional programming?
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 6 / 63
▶ Computer science is the illegitimate child of mathematics and
▶ Electrical engineering gave us the hardware, the actual machine ▶ Nearly all ideas come from mathematics
▶ Digital electronics: Gates, fmip-fmops, latches, memory, etc ▶ Boolean functions that read their inputs from memory & write
▶ Reading & writing are synchronized using a clock (with a
▶ A fjnite-state machine
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 7 / 63
▶ We cannot design large software systems while thinking at the
▶ We need some theoretical foundation for programming &
▶ Such a theoretical foundation should enable us to use tools from
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 8 / 63
▶ A language ▶ A set of ideas, notions, defjnitions, techniques, results, all
▶ Programming is based on a computable mathematics ▶ Theoretical computer science uses all of mathematics
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 9 / 63
▶ A language ▶ Notions such as functions, variables, expressions, evaluation,
▶ Operations such as arithmetic, Boolean, structural (e.g., on n
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 10 / 63
▶ Computers can only approximate real numbers ▶ Computers cannot implement infjnite tape (Turing machines) ▶ Mathematical objects are cheaper than objects created on a
▶ Functions are mappings; They take no time! ▶ Knowing that an object exists is often all we need! ▶ Bad things cannot happen: ▶ No exceptions, errors, incorrect results ▶ Nothing is lost, nothing is ”too big” or ”too much” Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 11 / 63
▶ Closer to mathematics ▶ Easier to reason about ▶ Easier to transform ▶ Easier to generate automatically
▶ Farther from mathematics ▶ Harder to reason about ▶ Harder to transform ▶ Harder to generate automatically
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 12 / 63
▶ You teach them a simplifjed version of printf:
▶ Only takes a single string argument ▶ Returns an int: the number of characters in the string
▶ Roughly: printf : string -> int ▶ But the logician objects: He already knows of a function from
▶ strlen : string -> int ▶ He wants to know the difgerence between printf and strlen Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 13 / 63
▶ You: ”Simple, printf also prints the string to the screen!” ▶ Logician: ”What does it mean to print??” ▶ You: ”Seriously?? The printf function prints its argument to
▶ Logician: ”But you said the domain of printf is string -> int,
▶ You: ”Yes, so?” ▶ Logician: ”Then where’s the screen??” ▶ You: ”In front of you!” ▶ Logician: ”Where’s the screen in the domain of the function
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 14 / 63
▶ You: ”It isn’t in the domain. You can think of the screen as a
▶ Logician: ”I have no idea what you mean: How can the screen
▶ You: ”But that’s the whole point of this printing being a side
▶ Logician: ”Well, then printf isn’t a function!” ▶ You: ”Ummm…” ▶ Logician (having a Eureka!-moment): ”I get it now! You got the
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 15 / 63
▶ The real type of printf is string × screen → int × screen ▶ The underlined parts of the type are implicit, i.e., they are not
▶ The implicit parts of the type form the environment ▶ The function call mentions only the explicit arguments ▶ Leaving out the implicit arguments in the function call creates
▶ In fact, nothing has changed: The screen in the domain has
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 16 / 63
▶ Introducing side efgects introduces discrete time ▶ Having introduced time, we must now introduce sequencing:
▶ The notion of sequencing is, like time, illusory:
▶ The screen object in the range of printf("Hello "); is the
▶ So the two printf expressions are nested, and this is why their
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 17 / 63
▶ Closer to the mathematical notions of function, variable,
▶ Nothing is implicit ▶ Easier to reason about ▶ Side efgects are not an explicit part of the language (although
▶ Ofgers many other advantages
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 18 / 63
▶ Farther away from the mathematical notions such as function,
▶ Hides information through the use of implicit arguments (for
▶ Harder to reason about: Contains notions such as side efgects,
▶ Abstraction is harder, prone to errors ▶ Side efgects create implicit, knotty inter-dependencies between
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 19 / 63
▶ Values ⇒ Expressions ⇒ Functions ▶ Higher-order functions ▶ Mathematical operators: mapping, folding, fjltering, partitioning,
▶ The interpreter evaluates expressions
▶ State ⇒ Change ⇒ Commands ⇒ Procedures ▶ Object-oriented programming ▶ Imperative ≡ Based upon commands (imperare means to
▶ The interpreter performs commands
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 20 / 63
▶ There are very few strictly functional languages, i.e., languages
▶ Most languages are quasi-functional: They don’t make it
▶ Most new imperative languages do include features from
▶ anonymous functions (”lambda”) ▶ higher-order functions ▶ modules/namespaces/functors Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 21 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 22 / 63
▶ You can program imperatively in Scheme, ocaml, and many
▶ If your programs were checked as black boxes, you may never
▶ Two examples of functional programming:
▶ Prime numbers ▶ Determinants Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 23 / 63
▶ For all numbers up to 2:
▶ For all numbers up to 3:
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 24 / 63
▶ For all numbers up to 5:
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 25 / 63
▶ For all numbers up to 7:
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 26 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 27 / 63
▶ Can look at 2n + 1 ▶ Can look at 6n + 1, 6n + 5 ▶ etc
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 28 / 63
▶ We used closures in place of arrays
▶ Abstracted over the notion of divisibility up to a given prime
▶ Simplifjed access and traversal (application instead of iteration!) ▶ Avoided many possible bugs ▶ Resulted in fairly effjcient code ▶ Can be made far more effjcient Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 29 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 30 / 63
▶ We represent matrices as vectors of vectors of numbers ▶ For each matrix M of size n × n, we shall need to compute n
▶ We don’t want to keep allocating and copying sub-matrices!
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 31 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 32 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 33 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 34 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 35 / 63
▶ We used closures to abstract over a matrix
▶ Allowed us to simplify & streamline array access ▶ Allowed us to derive minor sub-matrices without re-allocating
▶ Higher-order functions simplify the code ▶ The minor sub-matrix is derived with hardly any computation ▶ Similar to the delegate design-pattern in OOPLs ▶ Avoided many possible bugs ▶ Resulted in very concise code ▶ Resulted in fairly effjcient code (as far as the expansion is
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 36 / 63
▶ Do not use side-efgects ▶ Make use of higher-order functions, recursion, mappings, folding,
▶ Use functions as clever data-structures ▶ Are closer to mathematics than imperative programs:
▶ Conciseness ▶ Fewer bugs ▶ Simpler to program ▶ Easier to prove correctness Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 37 / 63
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 38 / 63
▶ Tuples & RTTI ▶ Bases ▶ Fixed-Point Theory ▶ Functions as Data ▶ Streams ▶ Maps & Folds ▶ CPS & Threading Code ▶ Monads ▶ Possibly additional topics, depending on time
Mayer Goldberg \ Ben-Gurion University Functional Programming February 25, 2019 39 / 63