74
ISBN 0-321-33025-0 Chapter 15 Functional Programming Languages

ISBN 0-321-33025-0 Chapter 15 Functional Programming Languages

  • View
    225

  • Download
    2

Embed Size (px)

Citation preview

ISBN 0-321-33025-0

Chapter 15

Functional Programming Languages

Copyright © 2006 Addison-Wesley. All rights reserved. 1-2

Chapter 15 Topics

• Introduction• Mathematical Functions• Fundamentals of Functional Programming

Languages • The First Functional Programming Language: LISP• Introduction to Scheme• COMMON LISP• ML & Haskell – not covered• Closures – Added Topic• Applications of Functional Languages• Comparison of Functional and Imperative Languages

Copyright © 2006 Addison-Wesley. All rights reserved. 1-3

Introduction

• The design of the imperative languages is based directly on the von Neumann architecture– Efficiency is the primary concern, rather than

the suitability of the language for software development

• The design of the functional languages is based on mathematical functions– A solid theoretical basis that is also closer to

the user, but relatively unconcerned with the architecture of the machines on which programs will run

Copyright © 2006 Addison-Wesley. All rights reserved. 1-4

Backus – 1978 Turing Award lecture

• Made a case for pure functional languages– more readable, more reliable, more likely to be

correct– meaning of expressions are independent of

their context (no side effects)

• Proposed pure functional language FP

Copyright © 2006 Addison-Wesley. All rights reserved. 1-5

Mathematical Functions

• A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set

• Mapping is described by an expression or in some cases a table

• Evaluation order is controlled by recursion and conditional expressions rather than by sequencing and iteration

• A math function defines a value rather than a sequence of operations on values in memory. No variables, no side effects.

Copyright © 2006 Addison-Wesley. All rights reserved. 1-6

Lambda Expression

• A lambda expression specifies the parameter(s) and the mapping of a function in the following form

(x) x * x * x for the function cube (x) = x * x * x

Copyright © 2006 Addison-Wesley. All rights reserved. 1-7

Lambda Expressions

• Lambda expressions describe nameless functions

• Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expressione.g., ((x) x * x * x)(2)which evaluates to 8

• Lambda expression may have more than one parameter

Copyright © 2006 Addison-Wesley. All rights reserved. 1-8

Functional Forms

• A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both

Great for AI!

Copyright © 2006 Addison-Wesley. All rights reserved. 1-9

Function Composition

• A functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the secondForm: h f ° gwhich means h (x) f ( g ( x))Example: f (x) x + 2 and g (x) 3 * x,h f ° g yields (3 * x)+ 2

Copyright © 2006 Addison-Wesley. All rights reserved. 1-10

Apply-to-all

• A functional form that takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parametersForm: For h (x) x * x( h, (2, 3, 4)) yields (4, 9, 16)

Copyright © 2006 Addison-Wesley. All rights reserved. 1-11

Fundamentals of Functional Programming Languages

• The objective of the design of a FPL is to mimic mathematical functions to the greatest extent possible

• The basic process of computation is fundamentally different in a FPL than in an imperative language– In an imperative language, operations are done and the

results are stored in variables for later use– Management of variables is a constant concern and

source of complexity for imperative programming

• In an FPL, variables are not necessary, as is the case in mathematics

Copyright © 2006 Addison-Wesley. All rights reserved. 1-12

Referential Transparency

• In an FPL, the evaluation of a function always produces the same result given the same parameters

• Means the expression can be replaced with its result without changing the meaning of the program (true because no side effects; must be determinate)

• Semantics of purely functional languages are therefore simpler than imperative languages

Copyright © 2006 Addison-Wesley. All rights reserved. 1-13

LISP Data Types and Structures

• Data object types: originally only atoms and lists

• Atoms are either symbols or numbers• List form: parenthesized collections of

sublists and/or atomse.g., (A B (C D) E)

• Originally, LISP was a typeless language• LISP lists are stored internally as single-

linked lists (example above is 4 nodes)

Copyright © 2006 Addison-Wesley. All rights reserved.

LISP Lists

A B C D

(A B C D)

A

B C E

(A (B C) D (E (F G))

F G

D

Copyright © 2006 Addison-Wesley. All rights reserved.

Intro to Scheme

• We’ll use DrScheme• Language Pack: Swindle

Copyright © 2006 Addison-Wesley. All rights reserved. 1-16

LISP Interpretation

• Lambda notation is used to specify functions and function definitions. Function applications and data have the same form.e.g., If the list (A B C) is interpreted as data it isa simple list of three atoms, A, B, and CIf it is interpreted as a function application,it means that the function named A isapplied to the two parameters, B and C

• The first LISP interpreter appeared only as a demonstration of the universality of the computational capabilities of the notation

Copyright © 2006 Addison-Wesley. All rights reserved. 1-17

Origins of Scheme

• A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP

• Uses only static scoping• Functions are first-class entities

– They can be the values of expressions and elements of lists

– They can be assigned to variables and passed as parameters

Remember this?

Cool… very useful for AI!

Copyright © 2006 Addison-Wesley. All rights reserved. 1-18

Scheme Interpreter

• Interpreter is read-evaluate-write infinite loop

• Expressions evaluated by EVAL function• Parameters are evaluated, in no particular

order (literals EVAL to themselves)• The values of the parameters are

substituted into the function body• The function body is evaluated• The value of the last expression in the

body is the value of the function

Copyright © 2006 Addison-Wesley. All rights reserved. 1-19

Primitive Functions

Copyright © 2006 Addison-Wesley. All rights reserved. 1-20

Function Definition: LAMBDA

• Scheme program is collection of function definitions

• Lambda Expressions– Form is based on notation

e.g., (lambda (x) (* x x) x is called a bound variable• Lambda expressions can be applied

e.g., ((lambda (x) (* x x)) 7)

Copyright © 2006 Addison-Wesley. All rights reserved. 1-21

Special Form Function: DEFINE

A Function for Constructing Functions DEFINE - two forms:1. To bind a symbol to an expression (a constant)

e.g., (define pi 3.141593)Example use: (define two_pi (* 2 pi))

2. To bind names to lambda expressionse.g., (define (square x) (* x x))Example use: (square 5)

Names are case-insensitive, include letters, digits and special characters

Copyright © 2006 Addison-Wesley. All rights reserved.

Defining values & functions

Type definitions in upper window Must press Run

System loudly reminds you to press Run!

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme Run – an Error

indicates edited

here’s thesyntax error

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme Run – still an error!

not very informative

Remember: we call functions (fn-name parm). For example, (square 5). SO, we need a ( in front of square, with a closing ) of course!

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme – old habits die hard…

No no, the call is (square 5) NOT (square (5))….

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme – we got it right!

use ctrl-UpArrow to repeat a command

Copyright © 2006 Addison-Wesley. All rights reserved. 1-27

Output Functions

• (DISPLAY expression)• (NEWLINE)

Notice statementsperformed in sequence

Copyright © 2006 Addison-Wesley. All rights reserved. 1-28

List Functions: CAR and CDR

• CAR takes a list parameter; returns the first element of that list

• FIRST is equivalent to CAR• CDR takes a list parameter; returns the list after removing its

first element

• REST = CDRmust test for ‘()before doing car

recursion using cdreventually ends up with ‘()

common errors: 1) treatan atom like a list or 2) forget quote

Copyright © 2006 Addison-Wesley. All rights reserved. 1-29

Predicate Functions: LIST? and NULL?

• list? takes one parameter; it returns #T if the parameter is a list; otherwise()

• null? takes one parameter; it returns #T if the parameter is the empty list; otherwise()

– Note that null? returns #T if the parameter is()

Copyright © 2006 Addison-Wesley. All rights reserved. 1-30

Control Flow: IF

• Selection- the special form, IF(if predicate then_exp else_exp)

e.g., (if (<> count 0)

(/ sum count)

0) compoundelse

Copyright © 2006 Addison-Wesley. All rights reserved. 1-31

Numeric Predicate Functions

• #t (or non-null list) is true and ()or #f is false

• =, <>, >, <, >=, <=• even?, odd?, zero?, negative?

Copyright © 2006 Addison-Wesley. All rights reserved. 1-32

Control Flow: COND

• Multiple Selection - the special form, CONDGeneral form:(COND

(predicate_1 expr {expr})(predicate_1 expr {expr})...(predicate_1 expr {expr})(ELSE expr {expr}))

• Returns the value of the last expr in the first pair whose predicate evaluates to true. Based on guarded expressions.

Copyright © 2006 Addison-Wesley. All rights reserved.

COND example

Notice () aroundentire clause

Notice () aroundentire clause

close cond, close defineCould have used

else here

Copyright © 2006 Addison-Wesley. All rights reserved. 1-34

List Functions: Quote

• QUOTE - takes one parameter; returns the parameter without evaluation

• QUOTE is required because the Scheme interpreter, EVAL, always evaluates parameters before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate

• QUOTE can be abbreviated with the apostrophe prefix operator

Copyright © 2006 Addison-Wesley. All rights reserved. 1-35

List Functions: CONS

• cons takes two parameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its resulte.g., (cons 'A '(B C)) returns (A B C)

• Applying CONS to two atoms results in a dotted list (cell with two atoms, no pointer), e.g., (cons 'A 'B) returns (A . B)

A B

Copyright © 2006 Addison-Wesley. All rights reserved.

Cons Example

Copyright © 2006 Addison-Wesley. All rights reserved. 1-37

List Functions: List

• list takes any number of parameters; returns a list with the parameters as elements

Repeated for comparison

Copyright © 2006 Addison-Wesley. All rights reserved. 1-38

Example Scheme Function: append

• append takes two lists as parameters; returns the first parameter list with the elements of the second parameter list appended at the end(define (append_lists lis1 lis2)

(cond

((null? lis1) lis2)

(else (cons (car lis1)

(append_lists (cdr lis1) lis2)))

))• Ex: (append '((A B) C) '(D (E F)))

– returns ((A B) C D (E F))

Copyright © 2006 Addison-Wesley. All rights reserved.

Append Example

Copyright © 2006 Addison-Wesley. All rights reserved. 1-40

Scheme Functional Forms

• Composition– The previous examples have used it– (cdr (cdr ‘(A B C))) returns ( C )– (car (car '((A B) B C))) returns A– (cdr (car '((A B C) D))) returns (B C)– (cons (car '(A B))(cdr '(A B))) returns (A B)– (caar x) is equivalent to (car (car x))– (cadr x) is equivalent to (car (cdr x))– (caddar x is equivalent to (car (cdr (cdr (car x))))

Copyright © 2006 Addison-Wesley. All rights reserved.

Intro Exercises

• Do exercises 1-7

Copyright © 2006 Addison-Wesley. All rights reserved. 1-42

Predicate Function: eq?

• eq? takes two symbolic parameters; it returns #t if both parameters are atoms and the two are the samee.g., (eq? 'A 'A) yields #t(eq? 'A 'B) yields #f– Note that if eq? is called with list parameters,

the result is not reliable– Also eq? does not work for numeric atoms,

should use = [appears to work???]

• eqv? works on both symbols and numbers

we’ll create function in two slides

Copyright © 2006 Addison-Wesley. All rights reserved. 1-43

Example Scheme Function: member

• member takes an atom and a simple list; returns #T if the atom is in the list; () otherwise(define (list_member atm lis)(cond

((null? lis) #f)((eq? atm (car lis)) #t)(else (list_member atm (cdr lis)))

))

State recursively:an element is a member of the list if:- the element matches the first item in the list OR- the element is a member of the rest of the list

Copyright © 2006 Addison-Wesley. All rights reserved. 1-44

Example Scheme Function: equalsimp

• equalsimp takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise. Won’t handle list such as ‘(a (b c))(define (equalsimp lis1 lis2) (cond

((null? lis1) (null? lis2)) ((null? lis2) '()) ((eq? (car lis1) (car lis2))

(equalsimp(cdr lis1)(cdr lis2))) (else '())

))State recursively:two lists are equal if:- the first elements of each list match AND- the remaining lists (minus first elements) are equal)

Copyright © 2006 Addison-Wesley. All rights reserved. 1-45

Example Scheme Function: equal

• equal takes two general lists as parameters; returns #T if the two lists are equal; ()otherwise(define (equal lis1 lis2) (cond

((not (list? lis1))(eq? lis1 lis2)) ((not (list? lis2)) '()) ((null? lis1) (null? lis2)) ((null? lis2) '()) ((equal (car lis1) (car lis2))

(equal (cdr lis1) (cdr lis2))) (else '())

))This one can handle ‘(a (b c))

Copyright © 2006 Addison-Wesley. All rights reserved.

Equal Example

Copyright © 2006 Addison-Wesley. All rights reserved. 1-47

Example Scheme Function: LET

• General form:(let (

(name_1 expression_1)

(name_2 expression_2)

...

(name_n expression_n))

body

)• Evaluate all expressions, then bind the values to

the names; evaluate the body. Only local.• Often used to factor out common subexpressions

Copyright © 2006 Addison-Wesley. All rights reserved. 1-48

LET Example

(define (quadratic_roots a b c)(let ( (root_part_over_2a

(/ (sqrt (- (* b b) (* 4 a c)))(* 2 a))) (minus_b_over_2a (/ (- 0 b) (* 2 a))))

(begin (display (+ minus_b_over_2a root_part_over_2a)) (newline) (display (- minus_b_over_2a root_part_over_2a)) ) ))

Copyright © 2006 Addison-Wesley. All rights reserved.

Recursion – two ways to build list

Copyright © 2006 Addison-Wesley. All rights reserved.

Some Scheme Errors

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme errors continued

Extra parenthesis, this is the action: (display result) (evenSquareErr2…) so scheme doesn’t know what to do with:

((display result)(evenSquareErr2…)Don’t try to use ( .. ) to indicate a block!!

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme errors continued…

You would expect this to be a syntax error or something! But itjust “falls through” when it hits the else condition.

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme debugging

watch recursive calls

see whichconditions matched

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme Debugging

• Press Debug

current step

Copyright © 2006 Addison-Wesley. All rights reserved.

N-Queens Exercises

• Do Exercises 7-11

Copyright © 2006 Addison-Wesley. All rights reserved. 1-56

Functions That Build Code

• It is possible in Scheme to define a function that builds Scheme code and requests its interpretation

• This is possible because the interpreter is a user-available function, eval

+ quoted to prevent evaluation

cons inserts+ into list

Copyright © 2006 Addison-Wesley. All rights reserved.

Function to applies a function

• Map – applies (maps) a function

Copyright © 2006 Addison-Wesley. All rights reserved.

Apply

(apply FN ARG-LIST)• Returns the result of applying FN to the

list of arguments

Copyright © 2006 Addison-Wesley. All rights reserved.

Scheme Exercises

• Continue with all exercises

Copyright © 2006 Addison-Wesley. All rights reserved.

Closures

From Wikipedia:• a closure is a function that is evaluated in an environment

containing one or more bound variables. When called, the function can access these variables.

• Can also be expressed as: a closure is a function that captures the lexical environment in which it was created.

• Constructs such as objects in other languages can also be modeled with closures.

• In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function

Copyright © 2006 Addison-Wesley. All rights reserved.

Closures, continued

• A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. However, the variable is of indefinite extent, so a value established in one invocation remains available in the next.

• The concept of closures was developed in the 60’s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures.

Copyright © 2006 Addison-Wesley. All rights reserved.

Closures and first-class values

• Closures typically appear in languages in which functions are first-class values - in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers

; Return a list of all books with at least ; THRESHOLD copies sold. (define (best-selling-books threshold)

(filter (lambda (book)

(>= (book-sales book) threshold)) book-list))

closure for lambda code & threshold created passed to

filter will be called repeatedly with the closure

Copyright © 2006 Addison-Wesley. All rights reserved.

Closure in another language

• ECMAScript – uses function keyword instead of lambda, Array.filter

// Return a list of all books with at least // THRESHOLD copies sold.function bestSellingBooks(threshold) { return bookList.filter( function(book) {

return book.sales >= threshold; } ); }

Copyright © 2006 Addison-Wesley. All rights reserved.

Another closure - Ruby

xhr.onreadystatechange = function() {

if (xhr.readyState == 4) {

var result = xhr.responseText;

var place = result.split(‘,’_;

document.getElementById(“city”).value = place[0];

document.getElementById(“state”).value = place[1];

}

}

Nameless function, inherits environment in which it is defined. In this case environment includes xhr object anddocument object.

Copyright © 2006 Addison-Wesley. All rights reserved.

Lexical Closure – from Wiki

"A lexical closure is, first of all, an object--just like a string, integer, list and so on. It's a special object that is constructed from a piece of the program itself, when it evaluates a special notation. That notation specifies an anonymous piece of code that can take arguments, and is in fact an anonymous function. The anonymous function is a component of the closure object, but not the only component. The other component of the object is the lexical environment: a snapshot of all of the current bindings of all of the local variables which are visible at that point." •The bindings of the variables are the values of the variables, not the addresses that the names point to.

Copyright © 2006 Addison-Wesley. All rights reserved.

More Terminology

• Within some scope a free variable is a variable that is not bound within that scope, i.e. that within some scope there is no declaration of that variable. The name "Closure" comes from the terminology that the function-procedure "closes-over" its environment, capturing the current bindings of its free variables."

Copyright © 2006 Addison-Wesley. All rights reserved.

Callback Example – from gnu.org

• C example:typedef void (event_handler_t)

(int event_type, void *user_data);

void register_callback (int event_type, event_handler_t *handler, void *user_data);

Copyright © 2006 Addison-Wesley. All rights reserved.

Callback with Closures

;; In the library:

(define (register-callback event-type handler-proc) ...)

;; In the application:

(define (make-handler event-type user-data)

(lambda () ...

<code referencing event-type and user-data> ...))

(register-callback event-type (make-handler event-type ...))

library sees handler-proc as procedure with no argumentshandler procedure has used closure to capture environment – eventtype and user data

Copyright © 2006 Addison-Wesley. All rights reserved.

Closure Implementation

• Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (e.g., the set of available variables and their values) at the time when the closure was created.

• A language implementation cannot easily support full closures if its run-time memory model allocates all local variables on a linear stack. In such languages, a function's local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore those variables must be allocated so that they persist until no longer needed. This explains why typically languages that natively support closures use garbage collection.

Copyright © 2006 Addison-Wesley. All rights reserved.

Closure Implementation (continued)

• A typical modern Scheme implementation allocates local variables that might be used by closures dynamically and stores all other local variables on the stack.

Copyright © 2006 Addison-Wesley. All rights reserved.

Interesting Perspectives

• http://home.tiac.net/~cri/2005/closure.html

• http://gafter.blogspot.com/2007/01/definition-of-closures.html

Copyright © 2006 Addison-Wesley. All rights reserved. 1-72

Applications of Functional Languages• APL is used for throw-away programs• LISP is used for artificial intelligence

– Knowledge representation/Expert systems– Machine learning– Natural language processing– Modeling of speech and vision– Emacs editor

• Scheme is used to teach introductory programming at a significant number of universities

• Haskell and ML in research labs

Copyright © 2006 Addison-Wesley. All rights reserved. 1-73

Comparing Functional and Imperative Languages

• Imperative Languages:– Efficient execution– Complex semantics– Complex syntax– Concurrency is programmer designed

• Functional Languages:– Simple semantics (like math functions, may or

may not feel “natural” to programmers)– Simple syntax– Inefficient execution (not as bad if compiled)– Programs can automatically be made

concurrent

Copyright © 2006 Addison-Wesley. All rights reserved. 1-74

Summary• Functional programming languages use function

application, conditional expressions, recursion, and functional forms to control program execution instead of imperative features such as variables and assignments

• LISP began as a purely functional language and later included imperative features

• Scheme is a relatively simple dialect of LISP that uses static scoping exclusively

• COMMON LISP is a large LISP-based language• ML is a static-scoped and strongly typed functional

language which includes type inference, exception handling, and a variety of data structures and abstract data types

• Haskell is a lazy functional language supporting infinite lists and set comprehension.

• Purely functional languages have advantages over imperative alternatives, but their lower efficiency on existing machine architectures has prevented them from enjoying widespread use