Functional Programming Chapter 14 History of Functional Languages • Lisp is the second oldest language • Motivated by a need to do symbolic, rather than numerical, manipulations. • Common LISP and Scheme are the two most popular dialects of the traditional LISP. • Haskell is a modern functional language that adds strong typing to the more usual features. History/Characteristics • LISt Processing • The linked list is the major data structure • Both programs and data can be represented this way – Programs are able to manipulate code just like any other data – Learning programs, … Statement of Purpose • “The Lisp language is primarily for symbolic data processing. It has been used for symbolic calculations in differential and integral calculus, electrical circuit design, mathematic logic, game playing, and other fields of artificial intelligence.”, 1965, from the LISP 1.5 Programmer’s Manual Background: Functions • A function f from a set X (the domain) to a set Y (the range) maps each element x in X to a unique element y in Y. • For example, f(x) = x2 maps the set of real numbers into the set of positive real numbers (ignoring imaginary numbers). – i.e., the domain X is the set of all real numbers. Background: Functional Composition • If f is a function from X to Y and g is a function from Y to Z, then (g ◦ f) (x) is a function from X to Z defined as (g ◦ f) (x) = g(f(x)), for all x in X Simply put, it means to replace the x in g(x) with f(x). • Example: – f(x) = x2 + x – g(x) = 2x + 1 – g ◦ f = 2(x2 + x) + 1 Overview • Functional programming mirrors mathematical functions: – domain = input, range = output – A computation maps inputs to outputs • Variables are mathematical symbols; not associated with memory locations. – Compare to imperative programming Overview • Pure functional programming is state-free: no assignment statements. • Programs in pure functional languages consist of composite functions; output of each function is input to another. • Today, most functional languages have some imperative statements. 14.1 Functions and the Lambda Calculus • Typical mathematical function: Square(n) = n2 – funcName(args) = func-definition: an expression • Square : R R maps from reals to reals – domain and range are the set of real numbers • A function is total if it is defined for all values of its domain. Otherwise, it is partial. e.g., Square is total. Referential Transparency • Referential transparency: a function’s result depends only upon the values of its arguments and not on any previous computation or the order of evaluation for its arguments. • This is in contrast to the view of functions in imperative languages, where function values are based on arguments, order of evaluation, and can also have side effects (change state). – Pure functional languages have no state in the sense of state in imperative programs & so no side effects. Referential Transparency • In imperative programming a statement such as x = x + y; means “add the value of in memory cell x to the value in memory cell y and replace the value of x by the value of the function” – The name x is used in two different ways here; lhs (a memory address) versus rhs (a value) • Functional programs accept and return data, but have no side effects and do not have the concept of a variable as a memory address Lambda Calculus • The lambda calculus, invented in 1941, is the foundation of functional programming • It “…provides simple semantics for computation with functions so that properties of functional computation can be studied.” http://www.answers.com/topic/lambda-calculus#Motivation Fundamental Concepts – Functions don’t have to have a name: • e.g., ( x . x*x ) is the Square function. Relate to mathematical notation: f(x) = x*x • (( x . x*x )2): apply the function to a value, similar to f(x) = x2 for x = 2 – Argument names are arbitrary • ( x . x*x ) same as ( y . y*y ) – A function that has 2 arguments can be rewritten as a function with 1 argument which produces another function with only one argument. • ( x.( y. x*x + y* y)) Lambda Calculus • Is a general model of computation – Can express anything that can be expressed by a Turing machine; in other words, is Turing Complete • An imperative language works by changing the data store; a functional language works by applying functions and returning values – Lambda calculus models the latter approach – Some practitioners claim that it is harder to make mistakes while using this style of programming. Status of Functions • In imperative and OO programming, functions have different (lower) status than variables. • In functional programming, functions have same status as variables; they are first-class entities. – They can be passed as arguments in a call. – They can transform other functions. Example • A function that operates on other functions is called a functional form. e.g., g(f, [x1, x2, … ]) = [f(x1), f(x2), …], becomes g(Square, [2, 3, 5]) = [4, 9, 25] • Meaning: function g takes as parameters another function and a list (sequence) of values to which the parameter function is applied. Programs as Lists • Lists are written with white-spaceseparated elements enclosed in parentheses • Expressions are represented as lists • Lists are treated as functions with the first element being the operation and the remaining elements the arguments. 14.2 Scheme • A derivative of Lisp • Our subset: – omits assignments – simulates looping via recursion – simulates blocks via functional composition • Scheme is Turing complete, even without while statements – recursion is used for repetition. 14.2.1 Expressions • Cambridge prefix notation for all Scheme expressions: (f x1 x2 … xn) • • • • • e.g., (+ 2 2) ; evaluates to 4 (+(* 5 4 2)(-6 2));means 5*4*2+(6-2) Note: Scheme comments begin with ; Cambridge prefix allows operators to have an arbitrary number of arguments. The define Function • Used to define global “variables”; e.g. – (define f 120) – define changes its environment but is not equivalent to an assignment statement. It just gives a name to a value. • define can also be used for other purposes, as we will see. • setQ and setF are functions that operate more like assignments; also set! 14.2.2 Expression Evaluation • Three steps: – replace names of symbols by their current bindings. – Evaluate lists as function calls in Cambridge prefix, where a list is a set of elements enclosed in ( ); e.g., (* a 2) • The first element in the list is always treated as the function unless you specifically say not to. – Constants evaluate to themselves. Examples 5 ; evaluates to 5 Given the global definition (define f 120) then f ; evaluates to 120 (+ f 5) ; evaluates to 125 (+ 5 2 9 13) ; evaluates to 29 #f ; predefined, false #t ; predefined, true 5, #f, #t are constants, f is a bound symbol Lists as Function Calls • (+ 5 2 9 13) ; evaluates to 29 but • (+ (5 2 9 13)); an error Scheme will try to evaluate the second list, interpreting “5” as a function • (f); error - f isn’t a function Preventing Evaluation (define colors (quote (red yellow green))) or (define colors (‘ (red yellow green))) Quoting tells Scheme/LISP that the following list is not to be evaluated. Quoted Lists (define x f) ; defines x as 120 (value of f) (define x ‘f) ; defines x as the symbol f (define color ‘red) ; color is defined to be red (define color red) ; error: no definition of red 14.2.3 Lists • A list is a series of expressions enclosed in parentheses. – Lists represent both functions and data. – The empty list is written (). – e.g., (0 2 4 6 8) is a list of even numbers. – Here’s how it’s stored: List Node Structure • Each list node is a record with two fields: the car and the cdr: car is (a pointer to) the first field, cdr is (a pointer to) the second. • Note that in the previous example the cdr of the last node ( |8|nil| ) is nil. – This is equivalent to the null pointer in C++ • The nil value can be represented as ( ), which is also the representation for an empty list. “Proper” Lists & Dotted Lists • Proper lists are assumed to end with the value ( ) which is implemented by null reference • In a dotted list the last cons has some value other than nil as the value of the cdr field. – “Dotted” lists are written (0 2 4 6 . 8) The last node in the previous list would have been |6|8| Structure of a List in Scheme (a) Figure 14.1 (b) Notice the difference between the list with nil as the last entry, and the “dotted” list shown in part b. List Elements Can Be Lists Themselves This represents the list (a (b c)) a nil b c nil Lists as Binary Trees • Because each node in a Scheme list consists of two pointers, internally the lists are similar to binary trees. • Recall; – The links (pointers) are called the 'car' and the 'cdr'. – A list node (cell) is also called a cons or a pair. List Functions • car: returns the first element (“head”) of a list • cdr: returns the tail of the list, which is itself a list List Transforming Functions • Suppose we write (define evens ‘(0 2 4 6 8)). Then: (car evens) ; gives 0 (cdr evens) ; gives (2 4 6 8) ( (null? ‘()) ; gives #t, or true (cdr(cdr evens)); (4 6 8) (car‘(6 8)) ; 6 (quoted to stop eval) List Functions • cons: used to build lists – Requires two arguments: an element and a list; e.g., – (cons 8 ( )) ; gives the 1-element list (8) – (cons 6 (cons 8( ))) ; gives the list (6 8) – (cons 6 ‘(8)) ; also gives the list (6 8) – (cons 4(cons 8 9)) ; gives the dotted list ; (4 8 . 9 ) since 9 is not a ; list List Building Functions • list takes an arbitrary number of arguments and creates a list; append takes two lists and concatenates them: – (append ‘(1 3 5) evens) ;gives(1 3 5 0 2 4 6 8) – (append evens (list 3) ; gives (0 2 4 6 8 3) – (list ‘(1 3 5) evens) ; gives ((1 3 5) (0 2 4 6 8)) – (list 1 2 3 4) ; gives (1 2 3 4) – (list ‘evens ‘odds) ;gives (evens odds) Equal • The function equal? returns true if the two objects have the same structure and content. (equal? 5 ‘(5)) ; gives #f, or false (equal? 5 5) ; #t (equal? ‘(1 2 3) ‘(1 (2 3))) ; returns false 14.2.4 – Elementary Values • Numbers: integers, rationals, and floating point numbers • Characters • Functions • Symbols • Strings • Boolean values #t and #f • All values except #f and ( ) are interpreted as true when used as a predicate. Control Flow (if test then-part) (if test then-part else-part) Example: (if (< x 0) (- 0 x)) ;returns –x if x<0 (if (< x y) x y) ; returns the smaller of x, y Control Flow • The case statement is similar to a Java or C++ switch statement: • (case month (( sep apr jun nov) 30) ((feb) 28) (else 31) ; optional ) • All cases take an unquoted list of constants, except for the else. Defining Functions • define is also used to define functions; according to the following syntax: (define name (lambda (arguments) body)) or (define (name arguments) body) • From the former, one can see that Scheme is an applied lambda calculus. Examples • (define (min x y) (if (< x y) x y)) to return the minimum of x and y • (define (abs x)(if(< x 0)(- 0 x) x)) to find the absolute value of x Recursive functions • (define (factorial n) (if(< n 1) 1 (* n factorial(-n 1))) )) • (define (fun1 alist) (if (null? alist) 0 (+ (car alist)(fun1(cdr alist))) )) – Suppose ‘alist’ is replaced by ‘evens’ Built-in Functions • length: returns the number of elements in a list – (length ‘(1 2 3 4)) ; returns 4 – (length ‘((1 2 3) (5 6 7) 8)) ; returns 3 • (define (length alist) (if (null? alist) 0 (+ 1 (length (cdr alist))) )) Built-in Functions • member: tests whether an element is in a list (member 4 evens);returns (4 6 8) (member 1 ‘(4 2)); returns () • If the element is in the list, return portion of the list starting with that element, else return the empty list. Functions as Parameters • The mapcar function takes two parameters: a function fun and a list alist. • mapcar applies the function to the list of values, one at a time, and returns a list of the results. Functions as Parameters (define (mapcar fun alist) (if (null? alist) ‘() (cons (fun (car alist)) (mapcar fun (cdr alist))) )) E.g., if (define (square x) (* x x)) then (mapcar square ‘(2 3 5 7 9)) returns (4 9 25 49 81) 14.2.9 Symbolic Differentiation • Symbolic Differentiation Rules • Fig 14.2 d (c) 0 c is a constant dx d (x) 1 dx d du dv (u v) u and v are funct ions ofx dx dx dx d du dv (u v) dx dx dx d dv du (uv) u v dx dx dx du d dv 2 (u /v) v u /v dx dx dx (define (diff x expr) (if (not (list? expr)) (if (equal? x expr) 1 0) (let ((u (cadr expr)) (v (caddr expr))) (case (car expr) ((+) (list ‘+ (diff x u) (diff x v))) ((-) (list ‘- (diff x u) (diff x v))) ((*) (list ‘+ (list ‘* u (diff x v)) (list ‘* v (diff x u)))) ((/) (list ‘div (list ‘- (list ‘* v (diff x u)) (list ‘* u (diff x v))) (list ‘* u v))) )))) Implementation of LISP and its Dialects • Usually implemented as an interpreted language, although there are compiled forms. • A LISP interpreter generally consists of: – a memory manager – collection of core functions implemented in compiled form for speed – a set of support functions implemented with the core Lisp is an Expression-Oriented Language. • Unlike most other languages, no distinction is made between "expressions" and "statements" • Code and data are written as expressions. • When an expression is evaluated, it produces a value (or list of values), which then can be embedded into other expressions. Using Scheme • In Scheme, processing proceeds by typing expressions into the Scheme environment. After each expression is entered, Scheme – reads the expression, – evaluates the expression to determine its value, and – prints the resulting value. – This read-eval-print cycle forms the basis for all processing within Scheme. Memory Management in LISP • Memory is organized into cells (or nodes). • all cells are the same size, usually 2 words of memory • cells form a large pool of available storage, chained together into the initial free list. • cells are taken off the free list as needed to build atoms, lists, etc. Memory Management in Lisp • Literal atoms may require several cells to hold their definition (Remember the list diagram) • Each cell must be big enough to hold a pointer to the next list element, and a pointer to the current element value Garbage Collection • Evaluations in LISP take needed cells off of the free list. Many results are temporary and then are of no more use. • Therefore, must have mechanism for reclaiming space. • Lisp was the first language to use automatic garbage collection extensively Another Lisp Quote – Lisp has jokingly been called "the most intelligent way to misuse a computer". I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts. – — Edsger Dijkstra, CACM, 15:10 Other Programming Paradigms Event-Driven Concurrent Event-Driven P.L. Paradigm • Event-drive programs do not control the sequence in which input events occur; instead, they are written to react to any reasonable sequence of events. • Compare to imperative, OO, functional, and logical paradigms in which the ordering of input influences the way program steps are executed Examples • • • • Graphical User Interface (GUI) programs Online registration systems Airline reservation systems Embedded applications for devices such as cell phones, cars, airplane navigation systems, robots, home security systems … Event-driven control • Imperative programs get input, operate on it and produce results, either in a loop or in a single pass. Programmer determines order of input. • Event-driven programs maintain an event queue that corresponds to events that occur asynchronously and are processed as they happen. – Event-driven programs tend to run continuously, waiting for input Java Event-Processing Features • Provides certain classes and methods that can be used to design an interaction – Specify events, associate an event with an object, and provide event-handling actions • Figure 16.3 shows a partial class hierarchy with subclasses such as button, popup menu, text area, etc. • Listeners recognize an event, such as a mouse click. Concurrent Programming • Concurrency occurs when two or more separate threads or programs execute at the “same” time – actually, or apparently. • Example: several objects of the same class (e.g basketBallPlayer) may model a real-world situation in which several actual players interact concurrently • Example: a web browser downloads a page from a web site; several threads cooperate by rendering different parts of the page History of Concurrency • Concurrency is implemented in one of two ways: Concurrent program units execute – on separate processors at the same time, or – on a single processor by implementing time slicing: switching back and forth rapidly between several separate programs (processes, threads) History of Concurrency • Multiprogramming is an early operating system technique that time-shares CPU cycles between several different processes, all loaded in memory. – Originally introduced to give the CPU something to do when a process was waiting for input – Later extended by interrupting a running process to give another process a chance History of Concurrency • Concurrency was first implemented at the operating system level. Later techniques extended it to application level programs. • Concurrency at the application level requires some kind of synchronization to prevent possible data corruption – Semaphores – Monitors – Java threads – Dedicated concurrent languages: concurrent Pascal, concurrent Haskell, Cilk (concurrent C), …. Author Definitions • A concurrent program is a program designed to have two or more execution contexts. Such a program is said to be multithreaded. – A parallel program is a concurrent program in which several threads are active simultaneously (implies multiple processors, but conceptually the same thing) • A distributed program is a concurrent program that is designed to executed simultaneously on a collection of processors that don’t share memory. – Not the same – cannot share data directly, aren’t generally considered to be ‘multi-threaded’ Concurrency Issues • Race conditions / synchronization • Deadlock • Interprocess communication