Title: Functional Programming
1 Functional Programming Standard ML
2By doing it mathematically, you provide a firm
foundation which will enable people to go
further. Robin Milner, Turing Award
Lecturer(1991) and the ML designer
3Outline
- Introduction
- Why functional programming?
- Some History
- Standard ML
- ML Syntax
- Programming in Standard ML
- Some Good References
4Introduction
5Introduction
- We use a zillion different programming languages
- general purpose programming Fortran, Lisp,
Basic, C, Pascal, C, Java, etc. - scripting Visual Basic, awk, sed, perl, tcl,
sh, csh, bash, REXX, Scheme, etc. - search regular expressions, browser queries,
SQL, etc.
6Introduction
- display and rendering PostScript, HTML, XML,
VRML, etc. - hardware SystemC, VHDL, Esterelle
- theorem proving and mathematics Mathematica,
Maple, Matlab, NuPRL, Coq - others?
7Introduction
- ML is very different from what most of us have
seen it is functional - Before considering ML, we will summarize the
imperative languages properties
8Introduction
9Introduction
- Design of imperative languages is based directly
on the von Neumann architecture
10Introduction
- Programs in imperative languages rely heavily on
modifying the values of a collection of
variables, called the state - Before execution, the state has some initial
value s - During execution, each command changes the state
11Introduction
- Example
- In a sorting program, the state initially
includes an array of values - When the program has finished, the state has been
modified in such a way that these values are
sorted - Intermediate states represent progress towards
this goal
12Introduction
- The state is typically modified by assignment
commands - By using control structures, one can execute
these commands conditionally, or repeatedly,
depending on other properties of the current state
13Introduction
14Introduction
- Functional programs dont use variables - there
is no state - Therefore they cannot use assignments there is
nothing to assign to
15Introduction
- The idea of executing commands in sequence is
meaningless - The first command can make no difference to the
second there are not any state between them - They are based on Mathematical functions
16Introduction
- Functions can be treated in exactly the same way
as simpler objects like integers - They can be passed to other functions as
arguments and returned as results - Most traditional languages provide poor
facilities in these areas
17Introduction
- Instead of sequencing and looping, functional
languages use recursive functions
18Introduction
- Example
- f(n)
- 1 if n1
- f(5n-1) if n is odd, n?1
- f(n/4-3) if n is even
- Question You said there are not any variables,
but what about n?
19Introduction
- n is an identifier
- In a Functional Language, the identifier bind to
values - Variable is something that can be assigned a
value - Functions have no side effect
- They do not update any variables
- It is easy to define the semantics
20Introduction
- Haskell is an example of a pure functional
language. - Haskell is, as of 2002, the functional language
on which the most research is being performed. - ML is not a pure functional language in that it
is possible to write procedural programs (with
assignments and side-effects)
21Why Functional Programming?
22Why Functional Programming?
The von Neumann bottleneck
- Backus' famous paper encouraged much interest in
functional languages as a means of breaking the
von-Neumann bottleneck
23Why Functional Programming?
- Von Neumann bottleneck pumping single words back
and forth between CPU and store - Task of a program change store in some major
way. - It has kept us tied to word-at-a-time thinking
instead of of encouraging us to think in terms of
the larger conceptual units of the task at hand.
24Why Functional Programming?
- The assignment statement is the von Neumann
bottleneck of programming languages - Pure functional programming languages remove
state and assignments - Concurrency possible order of evaluation doesnt
matter
25Why Functional Programming?
26Why Functional Programming?
- System is referentially transparent if, in a
fixed context, the meaning of the whole can be
determined solely by the meaning of its parts. - Independent of the surrounding expression.
27Why Functional Programming?
- Do we have such property in imperative languages?
- If the function has side-effects (updating a
global variable, doing input or output), then
f(3) f(3) may not be the same as 2 f(3). - The second f(3) has a different meaning than the
rst
28Why Functional Programming?
- Purely declarative languages guarantee
referential transparency - It makes it easier to understand how a program
works
29Why Functional Programming?
- Many features of imperative languages have arisen
by a process of abstraction from typical computer
hardware - Perhaps the right approach is not to start from
the hardware and work upwards
30Why Functional Programming?
- Start with programming languages as an abstract
notation for specifying algorithms and then work
down to the hardware. (Dijkstra 1976)
31Why Functional Programming?
- Makes programming into an engineering discipline
rather than a trial-and-error process - The Formalism Principle Correctness should be
confirmed by reasoning and not by experiment-
Marjan Sirjani
32Why Functional Programming?
- As a matter of fact, it is unlikely that
programmers will have the patience to perform
such proofs the proofs are usually long and
boring
33- Beware of bugs in the above code I have only
proved it correct, not tried it. ,Donald Knuth
34Why Functional Programming?
- Functional Programming is an area of current
research - ACM Conference on LISP and Functional Programming
35Some History
36Some History
- 1940s
- Alonzo Church and Haskell Curry developed the
lambda calculus, a simple but powerful
mathematical theory of functions.
37Some History
- Alonzo Church is a famous computer scientist
- He had many doctoral students , such as Stephen
C. Kleene or Alan Turing
38Some History
- 1960s
- John McCarthy developed Lisp, the first
functional language. Some influences from the
lambda calculus, but still retained variable
assignments.
39Some History
- 1978
- John Backus publishes award winning article on
FP, a functional language that emphasizes
higher-order functions and calculating with
programs.
40Some History
- Mid 1970s
- Robin Milner develops ML, the first of the modern
functional languages, which introduced type
inference and polymorphic types.
41Some History
- Late 1970s - 1980s
- David Turner develops a number of lazy functional
languages leading up to Miranda, a commercial
product. - 1988
- A committee of prominent researchers publishes
the first definition of Haskell, a standard lazy
functional language.
42Some History
- 1999
- The committee publishes the definition of Haskell
98, providing a long-awaited stable version of
the language.
43Standard ML
44Standard ML
- Historically, ML stands for metalanguage
- General-purpose functional programming language
- Developed by Robin Milner and others in the late
1970s at Edinburgh University
45Standard ML
- In 1969 Dana Scott introduced LCF, his Logic for
Computable Functions - It was a core higher-order call-by-name
functional programming language with arithmetic,
booleans and recursion at all types - That lead to Milner et al's LCF system and then
the programming language ML
46ML Syntax
- A program in ML, like any other language, is made
up of various kinds of expressions.
47ML Syntax
syntactic class syntactic variable(s) and grammar rule(s) examples
identifiers x, y a, x, y, x_y, ...
constants c ...2, 1, 0, 1, 2 (integers) 1.0, 0.001, 3.141 (reals)true, false (booleans)"hello", "", "!" (strings)"A", " " (characters)
unary operator u , not, size, ...
binary operators b , , -, gt, lt, gt, lt, , ...
expressions (terms) e x c u e e1 b e2 if e then e else e let d1...dn in e end e (e1, ..., en) foo, 0.001, not b, 2 2,
declarations d val x e fun y (x1t1, ..., xntn) t e val one 1fun square(x int) int
types t int real bool string char t1...tn-gtt int, string, int-gtint, boolint-gtbool
Adapted from Cornell lectures
48Programming in Standard ML
- Example A simple function declaration that
computes the absolute value of a real number
49Programming in Standard ML
- The SML prompt lets you type either a term or a
declaration that binds a variable to a term - Running an ML program is just evaluating a term
- The ML evaluator takes the left-most expression
that is not a value and reduces it to some
simpler expression. Eventually the whole
expression is a value and then evaluation stops
the program is done
50Programming in Standard ML
- For example consider evaluating abs(2.01.0)
- abs(2.01.0) ?
- abs(3.0) ?
- if 3.0 lt 0.0 then 3.0 else 3.0 ?
- if false then 3.0 else 3.0 ?
- 3.0
51Programming in Standard ML
- The let expression works by
- Evaluating all of its bindings.
- Those bindings are substituted into the body of
the let expression (the expression in between
in...end) - Example
- let val x 14 in x3 ?
- let val x 5 in x3 ?
- 53 ? 15
52Programming in Standard ML
53Programming in Standard ML
54Some Good References
- Functional Programming Using Standard ML Ake
Wikstrom - Introduction to Functional Programming John
Harrison - Introduction to Standard ML,Robert Harper
- Cornell CS312 lectures
- And lots more
55Any Questions ?!