Download presentation
Presentation is loading. Please wait.
1
Functional Programming
2
1. Introduction to FP 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
3
1.1 Principles of FP treats computation as evaluation of mathematical functions (and avoids state) data and programs are represented in the same way functions as first-class values – higher-order functions: functions that operate on, or create, other functions – functions as components of data structures lamda calculus provides a theoretical framework for describing functions and their evaluation it is a mathematical abstraction rather than a programming language
4
1.2 History lambda calculus (Church, 1932)
simply typed lambda calculus (Church, 1940) lambda calculus as prog. lang. (McCarthy(?), 1960, Landin 1965) polymorphic types (Girard, Reynolds, early 70s) algebraic types ( Burstall & Landin, 1969) type inference (Hindley, 1969, Milner, mid 70s) lazy evaluation (Wadsworth, early 70s) Equational definitions Miranda 80s Type classes Haskell 1990s
5
1.3 Varieties of FP languages
typed (ML, Haskell) vs untyped (Scheme, Erlang) Pure vs Impure impure have state and imperative features pure have no side effects, “referential transparency” Strict vs Lazy evaluation
6
1.4 Declarative style of programming
Declarative Style of programming - emphasis is placed on describing what a program should do rather than prescribing how it should do it. Functional programming - good illustration of the declarative style of programming. A program is viewed as a function from input to output. Logic programming – another paradigm A program is viewed as a collection of logical rules and facts (a knowledge-based system). Using logical reasoning, the computer system can derive new facts from existing ones.
7
1.5 Functional style of programming
A computing system is viewed as a function which takes input and delivers output. The function transforms the input into output . Functions are the basic building blocks from which programs are constructed. The definition of each function specifies what the function does. It describes the relationship between the input and the output of the function.
8
Examples Describing a game as a function Text processing
Text processing: translation Compiler
9
1.6 Why functional programming
Functional programming languages are carefully designed to support problem solving. There are many features in these languages which help the user to design clear, concise, abstract, modular, correct and reusable solutions to problems. The functional Style of Programming allows the formulation of solutions to problems to be as easy, clear, and intuitive as possible. Since any functional program is typically built by combining well understood simpler functions, the functional style naturally enforces modularity.
10
Why functional programming
Programs are easy to write because the system relieves the user from dealing with many tedious implementation considerations such as memory management, variable declaration, etc . Programs are concise (typically about 1/10 of the size of a program in non-FPL) Programs are easy to understand because functional programs have nice mathematical properties (unlike imperative programs) . Functional programs are referentially transparent , that is, if a variable is set to be a certain value in a program; this value cannot be changed again. That is, there is no assignment but only a true mathematical equality.
11
Why functional programming
Programs are easy to reason about because functional programs are just mathematical functions; hence, we can prove or disprove claims about our programs using familiar mathematical methods and ordinary proof techniques (such as those encountered in high school Algebra). For example we can always replace the left hand side of a function definition by the corresponding right hand side.
12
1.7 Examples of FP languages
Lisp (1960, the first functional language….dinosaur, has no type system) Hope (1970s an equational fp language) ML (1970s introduced Polymorphic typing systems) Scheme (1975, static scoping) Miranda (1980s equational definitions, polymorphic typing Haskell (introduced in 1990, all the benefits of above + facilities for programming in the large.) Erlang ( a general-purpose concurrent programming language and runtime system, introduced by Ericsson) The sequential subset of Erlang is a functional language, with dynamic typing.
13
2. Mathematical functions
Def: A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set 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
14
Mathematical functions
Lambda expressions describe nameless functions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression e.g. ((x) x * x * x)(3) which evaluates to 27
15
Mathematical functions
Functional Forms Def: A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both
16
Functional forms 1. Function Composition Form: h f ° g
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 second Form: h f ° g which means h (x) f ( g ( x)) For f (x) x * x * x and g (x) x + 3, h f ° g yields (x + 3)* (x + 3)* (x + 3)
17
Functional forms 2. Construction Form: [f, g]
A functional form that takes a list of functions as parameters and yields a list of the results of applying each of its parameter functions to a given parameter Form: [f, g] For f (x) x * x * x and g (x) x + 3, [f, g] (4) yields (64, 7)
18
Functional forms 3. Apply-to-all Form: For h (x) x * x * x
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 parameters Form: For h (x) x * x * x ( h, (3, 2, 4)) yields (27, 8, 64)
19
3. LISP 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 is a simple list of three atoms, A, B, and C If it is interpreted as a function application, it means that the function named A is applied 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
20
4. Introduction to Scheme
A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP Invented by Guy Lewis Steele Jr. and Gerald Jay Sussman Emerged from MIT Originally called Schemer Shortened to Scheme because of a 6 character limitation on file names
21
Introduction to Scheme
Designed to have very few regular constructs which compose well to support a variety of programming styles Functional, object-oriented, and imperative All data type are equal What one can do to one data type, one can do to all data types Data values, or objects, are dynamically allocated in a heap where they are retained until no longer needed, then automatically deallocated. Objects are first-class data values; because they are heap-allocated and retained indefinitely, they may be passed freely as arguments to procedures, returned as values from procedures, and combined to form new objects. This is in contrast with most other languages where composite data values such as arrays are either statically allocated and never deallocated, allocated on entry to a block of code and unconditionally deallocated on exit from the block, or explicitly allocated and deallocated by the programmer.
22
Introduction to Scheme
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
23
Introduction to Scheme
Primitive Functions 1. Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX e.g., (+ 5 2) yields 7
24
Introduction to Scheme
2. QUOTE -takes one parameter; returns the parameter without evaluation QUOTE is required because the Scheme interpreter, named EVAL, always evaluates parameters to function applications 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 e.g., '(A B) is equivalent to (QUOTE (A B))
25
Introduction to Scheme
3. CAR takes a list parameter; returns the first element of that list e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B) 4. CDR takes a list parameter; returns the list after removing its first element e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)
26
Introduction to Scheme
5. 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 result e.g., (CONS 'A '(B C)) returns (A B C)
27
Introduction to Scheme
6. LIST - takes any number of parameters; returns a list with the parameters as elements
28
Introduction to Scheme
Lambda Expressions Form is based on notation e.g., (LAMBDA (L) (CAR (CAR L))) L is called a bound variable Lambda expressions can be applied e.g., ((LAMBDA (L) (CAR (CAR L))) '((A B) C D))
29
Introduction to Scheme
A Function for Constructing Functions DEFINE - Two forms: 1. To bind a symbol to an expression e.g., (DEFINE pi ) (DEFINE two_pi (* 2 pi))
30
Introduction to Scheme
2. To bind names to lambda expressions e.g., (DEFINE (cube x) (* x x x)) Example use: (cube 4)
31
Introduction to Scheme
Evaluation process (for normal functions): 1. Parameters are evaluated, in no particular order 2. The values of the parameters are substituted into the function body 3. The function body is evaluated 4. The value of the last expression in the body is the value of the function (Special forms use a different evaluation process)
32
Introduction to Scheme
Example: (DEFINE (square x) (* x x)) (DEFINE (hypotenuse side1 side1) (SQRT (+ (square side1) (square side2))) )
33
Introduction to Scheme
Example: (define (list-sum lst) (cond ((null? lst) 0) ((pair? (car lst)) (+(list-sum (car lst)) (list-sum (cdr lst)))) (else (+ (car lst) (list-sum (cdr lst))))))
34
Some conventions Naming Conventions
A predicate is a procedure that always returns a boolean value (#t or #f). By convention, predicates usually have names that end in `?'. A mutation procedure is a procedure that alters a data structure. By convention, mutation procedures usually have names that end in `!'.
35
Cases Uppercase and Lowercase
Scheme doesn't distinguish uppercase and lowercase forms of a letter except within character and string constants; in other words, Scheme is case-insensitive. For example, Foo is the same identifier as FOO, but 'a' and 'A' are different characters.
36
Predicate functions Predicate Functions: (#t is true and #f or ()is false) 1. EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (EQ? 'A 'A) yields #t (EQ? 'A '(A B)) yields () Note that if EQ? is called with list parameters, the result is not reliable Also, EQ? does not work for numeric atoms .
37
Predicate functions Predicate Functions:
2. LIST? takes one parameter; it returns #T if the parameter is a list; otherwise() 3. NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise() Note that NULL? returns #T if the parameter is() 4. Numeric Predicate Functions =, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?, NEGATIVE?
38
Control flow Control Flow 1. Selection- the special form, IF
(IF predicate then_exp else_exp) e.g., (IF (<> count 0) (/ sum count) )
39
Control flow Control Flow
2. Multiple Selection - the special form, COND General form: (COND (predicate_1 expr {expr}) ... (ELSE expr {expr}) ) Returns the value of the last expr in the first pair whose predicate evaluates to true
40
Example (DEFINE (compare x y) (COND
((> x y) (DISPLAY “x is greater than y”)) ((< x y) (DISPLAY “y is greater than x”)) (ELSE (DISPLAY “x and y are equal”)) )
41
Example 1. member - takes an atom and a simple list; returns #T if the atom is in the list; () otherwise (DEFINE (member atm lis) (COND ((NULL? lis) '()) ((EQ? atm (CAR lis)) #T) ((ELSE (member atm (CDR lis))) ))
42
Example 2. equalsimp - takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise (DEFINE (equalsimp lis1 lis2) (COND ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((EQ? (CAR lis1) (CAR lis2)) (equalsimp(CDR lis1)(CDR lis2))) (ELSE '()) ))
43
Example 3. 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 '()) ))
44
Example 4. 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 lis1 lis2) (COND ((NULL? lis1) lis2) (ELSE (CONS (CAR lis1) (append (CDR lis1) lis2))) ))
45
Introduction to ML
46
ML General-purpose, non-C-like, non-OO language
Related languages: Haskell, Ocaml, F#, … Combination of Lisp and Algol-like features Expression-oriented Higher-order functions Garbage collection Abstract data types Module system Exceptions Originally intended for interactive use
47
Why Study ML ? Types and type checking Memory management Control
General issues in static/dynamic typing Polymorphic type inference Memory management Static scope and block structure, activation records Higher-order functions Control Type-safe exceptions Tail recursion and continuations
48
History of ML Robin Milner Logic for Computable Functions (LCF)
Stanford, U. of Edinburgh, Cambridge 1991 Turing Award Logic for Computable Functions (LCF) One of the first automated theorem provers Meta-Language of the LCF system
49
Logic for Computable Functions
Dana Scott (1969) Formulated a logic for proving properties of typed functional programs Robin Milner (1972) Project to automate logic Notation for programs Notation for assertions and proofs Need to write programs that find proofs Too much work to construct full formal proof by hand Make sure proofs are correct
50
LCF Proof Search Tactic: function that tries to find proof
succeed and return proof tactic(formula) = search forever fail Express tactics in the Meta-Language (ML) Use type system to facilitate correctness
51
Tactics in ML Type System
Tactic has a functional type tactic : formula proof Type system must allow “failure” succeed and return proof tactic(formula) = search forever fail and raise exception
52
Function Types in ML f : A B means for every x A,
some element y=f(x) B f(x) = run forever terminate by raising an exception In words, “if f(x) terminates normally, then f(x) B.” Addition never occurs in f(x)+3 if f(x) raises exception. This form of function type arises directly from motivating application for ML. Integration of type system and exception mechanism mentioned in Milner’s 1991 Turing Award lecture.
53
Higher-Order Functions
Tactic is a function Method for combining tactics is a function on functions Example: f(tactic1, tactic2) = formula. try tactic1(formula) else tactic2 (formula) We haven’t seen -expressions yet (think of them as functions for now)
54
Basic Overview of ML Interactive compiler: read-eval-print Examples
Compiler infers type before compiling or executing Type system does not allow casts or other loopholes Examples - (5+3)-2; > val it = 6 : int - if 5>3 then “Bob” else “Fido”; > val it = “Bob” : string - 5=4; > val it = false : bool
55
Basic Types Booleans Integers Strings Reals true, false : bool
if … then … else … (types must match) Integers 0, 1, 2, … : int +, * , … : int * int int and so on … Strings “Austin Powers” Reals 1.0, 2.2, , … decimal point used to disambiguate
56
Compound Types Tuples Lists Records
(4, 5, “noxious”) : int * int * string Lists nil 1 :: [2, 3, 4] Records {name = “Fido”, hungry=true} : {name : string, hungry : bool} type type
57
Patterns and Declarations
Patterns can be used in place of variables <pat> ::= <var> | <tuple> | <cons> | <record> … Value declarations General form: val <pat> = <exp> val myTuple = (“Conrad”, “Lorenz”); val (x,y) = myTuple; val myList = [1, 2, 3, 4]; val x::rest = myList; Local declarations let val x = 2+3 in x*4 end;
58
Functions and Pattern Matching
Anonymous function fn x => x+1; like function (…) in JavaScript Declaration form fun <name> <pat1> = <exp1> | <name> <pat2> = <exp2> … | <name> <patn> = <expn> … Examples fun f (x,y) = x+y; actual argument must match pattern (x,y) fun length nil = 0 | length (x::s) = 1 + length(s);
59
Functions on Lists Apply function to every element of list
fun map (f, nil) = nil | map (f, x::xs) = f(x) :: map (f,xs); Reverse a list fun reverse nil = nil | reverse (x::xs) = append ((reverse xs), [x]); Append lists fun append (nil, ys) = ys | append (x::xs, ys) = x :: append(xs, ys); Example: map (fn x => x+1, [1,2,3]); [2,3,4] How efficient is this? Can you do it with only one pass through the list?
60
More Efficient Reverse Function
fun reverse xs = let fun rev(nil, z) = z | rev(y::ys, z) = rev(ys, y::z) in rev( xs, nil ) end; 1 2 3 1 2 3 1 2 3 1 2 3
61
Datatype Declarations
General form datatype <name> = <clause> | … | <clause> <clause> ::= <constructor> |<constructor> of <type> Examples datatype color = red | yellow | blue Elements are red, yellow, blue datatype atom = atm of string | nmbr of int Elements are atm(“A”), atm(“B”), …, nmbr(0), nmbr(1), ... datatype list = nil | cons of atom*list Elements are nil, cons(atm(“A”), nil), … cons(nmbr(2), cons(atm(“ugh”), nil)), ...
62
Datatypes and Pattern Matching
Recursively defined data structure datatype tree = leaf of int | node of int*tree*tree node(4, node(3,leaf(1), leaf(2)), node(5,leaf(6), leaf(7)) ) Recursive function fun sum (leaf n) = n | sum (node(n,t1,t2)) = n + sum(t1) + sum(t2) 4 5 7 6 3 2 1
63
Example: Evaluating Expressions
Define datatype of expressions datatype exp = Var of int | Const of int | Plus of exp*exp; Write (x+3)+y as Plus(Plus(Var(1),Const(3)), Var(2)) Evaluation function fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = … ev(Plus(Const(3),Const(2))) Const(5) ev(Plus(Var(1),Plus(Const(2),Const(3)))) ev(Plus(Var(1), Const(5))
64
Case Expression Datatype Case expression
datatype exp = Var of int | Const of int | Plus of exp*exp; Case expression case e of Var(n) => … | Const(n) => …. | Plus(e1,e2) => …
65
Evaluation by Cases datatype exp = Var of int | Const of int | Plus of exp*exp; fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = (case ev(e1) of Var(n) => Plus(Var(n),ev(e2)) | Const(n) => (case ev(e2) of Var(m) => Plus(Const(n),Var(m)) | Const(m) => Const(n+m) | Plus(e3,e4) => Plus(Const(n),Plus(e3,e4)) ) | Plus(e3,e4) => Plus(Plus(e3,e4),ev(e2)) );
66
ML Imperative Features
Remember l-values and r-values? Assignment y := x+3 ML reference cells and assignment Different types for location and contents x : int non-assignable integer value y : int ref location whose contents must be integer !y the contents of cell y ref x expression creating new cell initialized to x ML form of assignment y := x place value of x+3 in location (cell) y y := !y add 3 to contents of y and store in location y Refers to location (l-value) Refers to contents (r-value)
67
Reference Cells in ML Variables in most languages ML reference cells
Variable names a storage location Contents of location can be read, can be changed ML reference cells A mutable cell is another type of value Explicit operations to read contents or change contents Separates naming (declaration of identifiers) from “variables”
68
Imperative Examples in ML
Create cell and change contents val x = ref “Bob”; x := “Bill”; Create cell and increment val y = ref 0; y := !y + 1; “while” loop val i = ref 0; while !i < 10 do i := !i +1; !i; x Bob Bill y 1
69
Core ML Basic Types Patterns Declarations Functions Polymorphism
Unit Booleans Integers Strings Reals Tuples Lists Records Patterns Declarations Functions Polymorphism Overloading Type declarations Exceptions Reference cells
70
Related Languages ML family Haskell F#
Standard ML – Edinburgh, Bell Labs, Princeton, … CAML, OCAML – INRIA (France) Some syntactic differences from Standard ML (SML) Object system Haskell Lazy evaluation, extended type system, monads F# ML-like language for Microsoft .NET platform “Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of .NET. “ Compiler produces .NET intermediate language
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.