Slides1 Introduction PDF
Slides1 Introduction PDF
Stephen Brookes
LECTURE 1
Tuesday, August 26
Functional
programming
!
!
!
SML
What is SML?
A functional programming language
computation = evaluation
A typed language
only well-typed expressions are evaluated
A call-by-value language
Features
referential transparency
!
can
be used as data in lists, tuples, ...
!
! and as argument or result of other functions
Referential
transparency
for types and values
safe substitution,
compositional reasoning
Equivalence
Expressions of type int are equivalent
if they evaluate to the same integer
Equivalence
21 + 21 is equivalent to 42
[2,4,6] is equivalent to [1+1, 2+2, 3+3]
fn x => x+x is equivalent to fn y => 2*y
21 + 21 = 42
fn x => x+x = fn y => 2*y
(fn x => x+x) (21 + 21) = (fn y => 2*y) 42
We use = for equivalence
Dont confuse with = in ML
Equivalence
For every type t there is a notion of
=
=t
Compositionality
In any functional program, replacing an
Parallelism
Expression evaluation has no side-effects
can evaluate independent code in parallel
evaluation order has no effect on value
Parallel evaluation may be faster than sequential
Learn to exploit parallelism!
Principles
Expressions must be well-typed.
Principles
Large programs should be designed as modules.
Well-interfaced code is easier to maintain.
Exploit parallelism.
sum
fun sum [ ] = 0
| sum (x::L) = x + sum(L)
type
value
spec
sum
fun sum [ ] = 0
| sum (x::L) = x + sum(L)
equational
sum [1,2,3]
reasoning
= 1 + sum [2,3]
= 1 + (2 + sum [3])
= 1 + (2 + (3 + sum [ ]))
= 1 + (2 + (3 + 0))
= 6.
count
fun count [ ] = 0
| count (r::R) = (sum r) + (count R)
type
value
spec
count
Since
equational
sum [1,2,3] = 6
reasoning
and
count [[1,2,3], [1,2,3]]
= sum[1,2,3] + sum [1,2,3]
it follows that
count [[1,2,3], [1,2,3]]
= 6+6
= 12
tail recursion
fun sum [ ] = 0
| sum (x::L) = x + sum(L)
sum
fun sum ([ ], a) = a
| sum (x::L, a) = sum (L, x+a)
sum ([1,2,3], 4) = 10
For all L:int list and a:int,
sum : int list * int -> int
type
value
spec
Sum
fun sum ([ ], a) = a
| sum (x::L, a) = sum (L, x+a)
fun Sum L = sum (L, 0)
Hence...
fun count [ ] = 0
| count (r::R) = (sum r) + (count R)
fun Count [ ] = 0
| Count (r::R) = (Sum r) + (Count R)
Evaluation
fun sum [ ] = 0
| sum (x::L) = x + sum(L)
sum (1::[2,3]) =>*
=>* !
means!
finitely many steps
x:1, L:[2,3]
(x + sum(L))
x:1, L:[2,3]
Evaluation
count [[1,2,3], [1,2,3]]
=>* sum [1,2,3] + count [[1,2,3]]
=>* 6 + count [[1,2,3]]
=>* 6 + (sum [1,2,3] + count [ ])
=>* 6 + (6 + count [ ])
=>* 6 + (6 + 0)
=>* 6 + 6
=>* 12
Analysis
(details later!)
code!
fragment
time!
proportional to
sum(L), Sum(L)
length of L
count(R), Count(R)
sum of lengths of
lists in R
(tail recursion
doesnt help)
Potential for
parallelism
+ is associative
+ is commutative
The order in which we combine additions
doesnt affect the result
Exploiting parallelism
with map
Analysis
Let R be a row list of length k,
Exploiting parallelism
with map and reduce
Themes
!
!
!
functional programming
correctness, termination, and performance
types, specifications and proofs
evaluation, equivalence and referential transparency
compositional reasoning
exploiting parallelism
!
Objectives
Write functional programs
Write specifications, and use rigorous
techniques to prove correctness
Notes
Lab tomorrow
Homework 1 out tonight
Homework 1 due Tuesday, 2 Sept
Course policy: no cheating!
Do your own work!
See course website, piazza, for details.
Always attend class! Then read the notes!