In tro duction to F unctional Programming: Lecture 1 1 In - - PDF document

in tro duction to f unctional programming lecture 1 1 in
SMART_READER_LITE
LIVE PREVIEW

In tro duction to F unctional Programming: Lecture 1 1 In - - PDF document

In tro duction to F unctional Programming: Lecture 1 1 In tro duction to F unctional Programming John Harrison Univ ersit y of Cam bridge Lecture 1 In tro duction and Ov erview T opics co v ered: Imp


slide-1
SLIDE 1 In tro duction to F unctional Programming: Lecture 1 1 In tro duction to F unctional Programming John Harrison Univ ersit y
  • f
Cam bridge Lecture 1 In tro duction and Ov erview T
  • pics
co v ered:
  • Imp
erativ e programming
  • F
unctional programming
  • The
merits
  • f
functional programming
  • Historical
remarks
  • Ov
erview
  • f
the course John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-2
SLIDE 2 In tro duction to F unctional Programming: Lecture 1 2 Imp erativ e programming Imp erativ e (or pro cedural) programs rely
  • n
mo difying a state b y using a sequence
  • f
c
  • mmands.
The state is mainly mo died b y the assignment command, written v = E
  • r
v := E. W e can execute
  • ne
command b efore another b y writing them in sequence, p erhaps separated b y a semicolon: C 1 ; C 2 . Commands can b e executed conditionally using if, and rep eatedly using while. Programs are a series
  • f
instructions
  • n
ho w to mo dify the state. Imp erativ e languages, e.g. F OR TRAN, Algol, C, Mo dula-3 supp
  • rt
this st yle
  • f
programming. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-3
SLIDE 3 In tro duction to F unctional Programming: Lecture 1 3 An abstract view W e ignore input-output
  • p
erations, and assume that a program runs for a limited time, pro ducing a result. W e can consider the execution in an abstract w a y as:
  • !
  • 1
!
  • 2
!
  • !
  • n
The program is started with the computer in an initial state
  • ,
including the inputs to the program. The program nishes with the computer in a nal state
  • n
, con taining the
  • utput(s)
  • f
the program. The state passes through a nite sequence
  • f
c hanges to get from
  • to
  • n
; in general, eac h command ma y mo dify the state. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-4
SLIDE 4 In tro duction to F unctional Programming: Lecture 1 4 F unctional programming A functional program is simply an expr ession, and executing the program means evaluating the expression. W e can relate this to the imp erativ e view b y writing
  • n
= E [ ].
  • There
is no state, i.e. there are no v ariables.
  • Therefore
there is no assignmen t, since there's nothing to assign to.
  • And
there is no sequencing and no rep etition, since
  • ne
expression do es not aect another. But
  • n
the p
  • sitiv
e side:
  • W
e can ha v e recursiv e functions, giving something comparable to rep etition.
  • F
unctions can b e used m uc h more exibly , e.g. w e can ha v e higher
  • rder
functions. F unctional languages supp
  • rt
this st yle
  • f
programming. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-5
SLIDE 5 In tro duction to F unctional Programming: Lecture 1 5 Example: the factorial The factorial function can b e written imp erativ ely in C as follo ws: int fact(int n) { int x = 1; while (n > 0) { x = x * n; n = n
  • 1;
} return x; } whereas it w
  • uld
b e expressed in ML as a recursiv e function: fun fact n = if n = then 1 else n * fact(n
  • 1);
John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-6
SLIDE 6 In tro duction to F unctional Programming: Lecture 1 6 Wh y? A t rst sigh t a language without v ariables, assignmen t and sequencing lo
  • ks
v ery impractical. W e will sho w in this course ho w a lot
  • f
in teresting programming can b e done in the functional st yle. Imp erativ e programming languages ha v e arisen as an abstraction
  • f
the hardw are, from mac hine co de, through assem blers and macro assem blers, to F OR TRAN and b ey
  • nd.
P erhaps this is the wrong approac h and w e should approac h the task from the h uman side. Ma yb e functional languages are b etter suited to p eople. But what concrete reasons are there for preferring functional languages? John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-7
SLIDE 7 In tro duction to F unctional Programming: Lecture 1 7 Merits
  • f
functional programming By a v
  • iding
v ariables and assignmen ts, w e gain the follo wing adv an tages:
  • Clearer
seman tics. Programs corresp
  • nd
more directly to abstract mathematical
  • b
jects.
  • More
freedom in implemen tation, e.g. parallelizabilit y . By the more exible use
  • f
functions, w e gain:
  • Conciseness
and elegance.
  • Better
parametrization and mo dularit y
  • f
programs.
  • Con
v enien t w a ys
  • f
represen ting innite data. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-8
SLIDE 8 In tro duction to F unctional Programming: Lecture 1 8 Denotational seman tics W e can iden tify
  • ur
ML factorial function with an abstract mathematical (partial) function Z ! Z : [ [fact ] ](n) = 8 < : n! if n
  • ?
  • therwise
where ? denotes undenedness, since for negativ e argumen ts, the program fails to terminate. Once w e ha v e a state, this simple in terpretation no longer w
  • rks.
Here is a C `function' that do esn't corresp
  • nd
to an y mathematical function: int rand(void) { static int n = 0; return n = 2147001325 * n + 715136305; } This giv es dieren t results
  • n
successiv e calls! John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-9
SLIDE 9 In tro duction to F unctional Programming: Lecture 1 9 Seman tics
  • f
imp erativ e programs In
  • rder
to giv e a corresp
  • nding
seman tics to imp erativ e programs, w e need to mak e the state explicit. F
  • r
example w e can mo del commands as:
  • P
artial functions
  • !
  • (Strac
hey)
  • Relations
  • n
  • (Hoare)
  • Predicate
transformers, i.e. total functions ( ! bool ) ! ( ! bool ) (Dijkstra) If w e allo w the goto statemen t, ev en these are not enough, and w e need a seman tics based
  • n
c
  • ntinuations
(W adsw
  • rth,
Morris). All these metho ds are quite complicated. With functional programs, w e ha v e a real c hance
  • f
pro ving their correctness,
  • r
the correctness
  • f
certain transformations
  • r
  • ptimizations.
John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-10
SLIDE 10 In tro duction to F unctional Programming: Lecture 1 10 Problems with functional programs F unctional programming is not without its deciencies. Some things are harder to t in to a purely functional mo del, e.g.
  • Input-output
  • In
teractiv e
  • r
con tin uously running programs (e.g. editors, pro cess con trollers). Ho w ev er, in man y w a ys, innite data structures can b e used to accommo date these things. F unctional languages also corresp
  • nd
less closely to curren t hardw are, so they can b e less ecien t, and it can b e hard to reason ab
  • ut
their time and space usage. ML is not a pure functional language, so y
  • u
can use v ariables and assignmen ts if required. Ho w ev er most
  • f
  • ur
w
  • rk
is in the pure functional subset. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-11
SLIDE 11 In tro duction to F unctional Programming: Lecture 1 11 Historical remarks Some
  • f
the ideas b ehind functional programming go bac k a long w a y , e.g. to `lam b da calculus', a logical formalism due to Alonzo Ch urc h, in v en ted in the 1930s b efore electronic computers. The earliest real functional programming language w as LISP , in v en ted b y McCarth y in the 50s. Ho w ev er this had a n um b er
  • f
defects, whic h w e will discuss later. The mo dern trend really b egins with ISWIM, in v en ted b y P eter Landin in the 1960s. The ML family started with Robin Milner's theorem pro v er `Edin burgh LCF' in the late 70s. The language w e shall study is essen tially (core) Standard ML, but there are
  • ther
imp
  • rtan
t dialects, notably CAML and Ob jectiv e CAML. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-12
SLIDE 12 In tro duction to F unctional Programming: Lecture 1 12 Ov erview
  • f
the course (1)
  • Practicalities
  • f
in teracting with ML.
  • Key
functional concepts, e.g. ev aluation strategy , higher
  • rder
functions.
  • P
  • lymorphic
t yp es.
  • Recursiv
e functions and recursiv e structures.
  • Hin
ts for eectiv e programming.
  • Exceptions,
references and
  • ther
imp erativ e features.
  • Pro
ving programs correct. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998
slide-13
SLIDE 13 In tro duction to F unctional Programming: Lecture 1 13 Ov erview
  • f
the course (2) W e w an t to sho w the p
  • w
er
  • f
ML, so w e'll nish with more substan tial examples that illustrate some
  • f
the p
  • ssibilities:
  • Sym
b
  • lic
dieren tiation
  • Recursiv
e descen t parsing
  • A
Prolog in terpreter
  • A
theorem pro v er The co de for these examples will b e made a v ailable
  • n
Thor. John Harrison Univ ersit y
  • f
Cam bridge, 15 Jan uary 1998