In his 1978 Turing award lecture (granted in recognition of his role in the development of FORTRAN, ALGOL 60, and BNF-grammars), John Backus attacked the pernicious influence of imperative programming languages and their dependence on the von Neumann architecture.
What is problem with imperative languages?
Designed around architectures available in 1950's.
Components:
To execute an instruction, go through fetch, decode, execute cycle.
Ex. To execute statement stored in location 97 (ADD 162):
Imperative program can be seen as control statements guiding execution of a series of assignment statements (accesses and stores to memory).
Variable in programming language refers to location whose contents may vary with time.
Hard to reason about variables whose values are always changing, even within same procedure or function.
Math notation not like that. Static. If want to add time, add new parameter. Gives static reasoning about dynamic processes.
Important notion called referential transparency. Can replace an expression anywhere that it occurs by its value.
Very important for parallelism, since compute once and then reuse.
Not true of imperative languages. Can't compute x+1 once and replace all occurrences by its value.
Order of execution in imperative programs very important - inhibits parallel execution.
We will see several advantages of functional programming.
Meaning of a statement is operation which, based on current contents of memory, and explicit values supplied to it, modifies the current contents of memory.
How are results of one command communicated to the next? Via changes to values in memory.
Problems: Too low level and architecture dependent.
Expressions: Return a value, depending on the state of the computation:
Examples:
Expressions (at least in math) better behaved than commands.Meaning of a (pure) expression is operation which, based on current contents of memory, and explicit values supplied to it, returns a value.
Referential transparency: 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.
Therefore once have evaluated an expression in a particular context, never have to evaluate it again in that context since value won't change.
Math. expressions are referentially transparent.
Ex. To evaluate "(2ax + b) (2ax +c)" in a context in which a = 3, b = 4, c = 7, and x = 2, sufficent to evaluate "2ax" only once.
Can determine meaning of f(g(x)) by only knowing the value of f, g, and x (independently).
Moreover if meaning of g' is same as g, then f(g(x)) = f(g'(x)).
(Note importance of replacing construct by equivalent one in compiler optimizations)
Lose referential transparency if allow functions with side effects.
I.e. suppose call to f(x) results in incrementing x by 1.
Then f(x) + f(x) != 2 * f(x).
Program supporting referential transparency much easier to prove correct since only need be concerned about meaning of components and then put them together.
With imperative languages, lose referential transparency.
x := x + y; y := 2 * x; versus y := 2 * x; x := x + y;
Since each command changes underlying state of computation and evaluation depends on state, ordering is critical.
Also correctness of program depends on contents of all memory cells.
Even when try to isolate portions of computations into procedures, can have non-local effects because of use of non-local variables and reference parameters.
Issues with expressions:
Order of evaluation
e.g. short-cut evaluations of boolean expressions.
If i > 0 and A[i] <> 99 then ....
What happens if A : ARRAY [1..100] OF INTEGER and i = 0 ?
Pascal vs. Modula-2 conventions.
Side-effects - destroy referential transparency.
Some language conflate (identify) expressions and commands (ALGOL 68 and C).
Often artificial and results in loss of advantages of expressions (e.g., referential transparency).
Ex: x = (y = x+1) + y + (x++)
Compare 2*(x++) and (x++) + (x++)
We will restrict our attention (for the most part) to functional languages with pure expressions.
Try to eliminate problems of commands and take advantage of referential transparency.
Promote reasoning about programs & implementation on parallel computers.
Idea - Program is simply application of a function to data.
No notion of memory or assignment - like a mathematical function - No side effects.
Very rich expressions - virtually all expressions first-class (unlike most imperative languages) in particular, functions are first class objects.
John McCarthy (then at MIT) in 1958-60 introduced a functional language (LISP), originally in study of symbolic differentiation with linked lists. Key article published in 1960 showing examples of important programs could be expressed as pure functions operating on lists. (LISP since been revised into competing dialects - Common LISP and Scheme.)
Functional languages or notation used in describing denotational semantics of programming languages starting in 1960's.
Most stunning event was Backus' Turing award lecture in 1978.
Proposed
language FP (since replaced by FL) supporting "functional" style of
programming.
First ML compiler was put out in 1977 (originally in support of interactive theorem proving system - text Edinburgh LCF by Gordon, Milner, and Wadsworth published). (Milner recently won Turing award.) Standardized in about 1986.
Other important languages include SASL, KRC, and Miranda (all by David Turner). Haskell is successor. All support lazy evaluation.
Currently 3 main schools of functional languages:
First uses dynamic typing, other two support static typing w/ polymorphic functions and type inference.
We choose ML for somewhat arbitrary reasons. Heavily used to develop real software, supports modern programming constructs.
The point of this part of the course is NOT to teach you ML, it is to teach familiarity with thinking in the functional paradigm with ML as the example language (though talk about others as well). I expect you to mainly learn ML on your own in the lab while I lecture on related material.
First 10 or so pages of Backus' Turing award lecture
(For WHY of functional
programming)
Designed by Robin Milnor, Mike Gordon and Chris Wadsworth.
Success led to adoption and strengthening as programming language.
Important attributes:
How to use the run-time system.Type:
sml
System responds with message saying in ML, and then "-" prompt.
Can load definitions from UNIX file by typing:
use ["myfile.sml"];
where myfile.sml is the name of your file. It should be in the same directory you were in when you typed sml.
Terminate session by typing control-D.
Evaluate expression by typing in and following with ";", e.g.
- 3 + 5;
> val it = 8 : int
"it" refers to last value computed. Can also bind value to an identifier:
- val six = 6;
> val six = 6 : int;
Thus typing an expression, exp, is equivalent to typing: val it = exp;
Identifier often called a variable, but really a constant declaration ("val" for value).
Can also define functions.
- fun succ x = x + 1;
> val succ = fn : int -> int
- succ 12;
> val it = 13 : int
- 17 * (succ 3);
> val it = 68 : int;
Can also write:
- val succ = fn x => x + 1;
> val succ = fn : int -> int
"fun" declaration tells compiler to look for fcn arguments.
Note semi-colon at top-level terminates parsing and causes evaluation.
No loops in the language, all functions written via recursion and if.. then.. else:
- fun fact n = if n = 0 then 1 else n * fact (n-1);
unit has only one value: ()
bool includes true, false and operators: not, andalso, orelse
int includes positive and negative: ...,~2, ~1,0,1,2...
supports +,-,*,div,mod,<,<=,>,>=
real of form 3.17, 2.4E17 with +,-,*,/,<,<=,>,>=, log, exp, sin,arctan
string of form "my string" - \t = tab, \n = newline.
supports ^ (concatenation), length, substring where substring("hello",1,3) -> "ell"
- fun double x = x + x;
Type checking error in: (syntactic context unknown)
Unresolvable overloaded identifier: +
Definition cannot be found for the type: ('a * 'a) -> 'a
- fun succ (x:int) = x + 1;
or
-fun succ x : int = x + 1;
or even
-fun succ (x:int) : int = x + 1;
Tuples are abbreviations of records where labels are 1,2,3,...
Thus (17,"abc", true) = {1 = 17, 2 = "abc", 3 = true}
Selectors: #lab : {lab : 'a,...} -> 'a
Thus #rank({name = "bob",salary = 50000.99, rank=1}) = 1
#2((17,"abc", true)) -> "abc"
Ex. of function on tuples:
- fun power (m,n):int = if n = 0 then 1
else m * power (m,n-1);
> val power = fn : (int * int) -> int
On the other hand
- fun cpower m n :int = if n = 0 then 1
else m * cpower m (n-1);
> val cpower = fn : int -> (int -> int)
Note these are different functions!
Latter said to be in "Curried" form (after Haskell Curry).
Can define
- val twopower = cpower 2
> val twopower = fn : int -> int
- twopower 3;
> val it = 8 : int
Operations: length
@ append - e.g. [1,2,3]@[4,5,6] = [1,2,3,4,5,6]
:: prefix (e.g. 1::x = [1,2,3,4,5,6])
map apply function to all elements of a list,
e.g. map sqr [1,2,4] = [1,4,16]
rev reverses list
[],nil empty list
Many kinds of lists:
int list: [1,2,3]
string list: ["ab","cd","ef"]
nil is part of any list type,
- nil;
> val it = [] : 'a list
where 'a stands for a type variable. Similarly write:
- map;
> val it = fn: ('a -> 'b) -> (('a list) -> ('b list))
Map is first example of a polymorphic function.
Lists are built up using ::, can also be decomposed the same way,
i.e., [1,2,3] = 1::[2,3] = 1::2::[3] = 1::2::3::nil
Can define functions by cases.
- fun product [] : int = 1
= | product (fst::rest) = fst * (product rest);
Note that "=" is automatically printed on continuation line. Don't include it in your program files!