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

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

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

In tro duction to F unctional Programming: Lecture 2 1 In tro duction to F unctional Programming John Harrison Univ ersit y of Cam bridge Lecture 2 Basics of ML T opics co v ered: V ersions of ML


slide-1
SLIDE 1 In tro duction to F unctional Programming: Lecture 2 1 In tro duction to F unctional Programming John Harrison Univ ersit y
  • f
Cam bridge Lecture 2 Basics
  • f
ML T
  • pics
co v ered:
  • V
ersions
  • f
ML
  • Running
ML
  • n
Thor
  • In
teracting with ML
  • Higher
  • rder
functions and currying
  • Ev
aluation
  • rder
John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-2
SLIDE 2 In tro duction to F unctional Programming: Lecture 2 2 The ML family ML is not a single language. Apart from Standard ML, whic h w e use here, there are man y descendan ts
  • f
the
  • riginal
ML used as the metalanguage
  • f
Edin burgh LCF, e.g.
  • CAML
Ligh t | an excellen t ligh t w eigh t system dev elop ed at INRIA.
  • Ob
jectiv e CAML | a new v ersion
  • f
CAML Ligh t including
  • b
ject-orien ted features.
  • Lazy
ML | a v ersion from Gothen burg using lazy evaluation.
  • Standard
ML | an agreed `standard v ersion' Standard ML has t w
  • parts:
the Cor e language and the Mo dules. W e will not co v er the mo dule system at all. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-3
SLIDE 3 In tro duction to F unctional Programming: Lecture 2 3 Implemen tations
  • f
Standard ML There are sev eral implemen tations
  • f
(something similar to) Standard ML:
  • Standard
ML
  • f
New Jersey | free but v ery resource-h ungry .
  • Mosco
w ML | free but do esn't ha v e the Mo dules.
  • P
  • ly
ML | go
  • d,
but a commercial pro duct, and do esn't run under Lin ux
  • Harlequin
ML | a new er commercial system with in tegrated dev elopmen t en vironmen t. W e'll use Mosco w ML. This is a go
  • d
ligh t w eigh t implemen tation based
  • n
CAML Ligh t, written b y Sergei Romanenk
  • (Mosco
w) and P eter Sestoft (Cop enhagen). The features w e use are general, and can easily b e translated to
  • ther
dialects. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-4
SLIDE 4 In tro duction to F unctional Programming: Lecture 2 4 Starting up ML Mosco w ML is already installed
  • n
Thor. In
  • rder
to start it up, t yp e: /group/clteach/jr h/m
  • s
ml/ `a rch `/ bin /m
  • sm
l This will in v
  • k
e the appropriate v ersion, dep ending
  • n
the mac hine arc hitecture. The system should start up and presen t its prompt. $ /group/clteach/jr h/m
  • s
ml/ `a rch `/ bin /m
  • sm
l Moscow ML version 1.42 (July 1997) Enter `quit();' to quit.
  • If
y
  • u
w an t to install Mosco w ML
  • n
y
  • ur
  • wn
computer, see the W eb page: http://www.dina. kv l.d k/ ~se st
  • ft
/m
  • sm
l. htm l John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-5
SLIDE 5 In tro duction to F unctional Programming: Lecture 2 5 In teracting with ML W e will run ML as an in terpreter, rather lik e a sophisticated calculator. Y
  • u
just t yp e an expression in to it, terminated b y a semicolon, and it prin ts the result
  • f
ev aluating it, e.g.
  • 10
+ 5; > val it = 15 : int ML not
  • nly
prin ts the result, but also infers the typ e
  • f
the expression, namely int. If ML cannot assign a t yp e to an expression then it is rejected:
  • 1
+ true; ! .... Type clash: .... ML kno ws the t yp e
  • f
the built-in
  • p
erator +, and that is ho w it mak es its t yp e inference. W e'll treat the t yp e system more systematically next time; for the momen t, it should b e in tuitiv ely clear. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-6
SLIDE 6 In tro duction to F unctional Programming: Lecture 2 6 Loading from les W e ha v e just b een t yping things in to ML and thinking ab
  • ut
the results. Ho w ev er
  • ne
do esn't write real programs in this w a y . T ypically ,
  • ne
writes the expressions and declarations in a le. T
  • try
them
  • ut
as y
  • u
go, these can b e inserted in the ML windo w using cut and paste. Y
  • u
can cut and paste using X-windo ws and similar systems,
  • r
an editor lik e Emacs with m ultiple buers. F
  • r
larger programs, it's con v enien t simply to load them from le in to the ML session. This can b e done using the use command in ML, e.g: use "myprog.ml"; Programs can also include commen ts written b et w een (* and *). These are ignored b y ML, but are useful for p eople reading the co de. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-7
SLIDE 7 In tro duction to F unctional Programming: Lecture 2 7 Using functions Since ML is a functional language, expressions are allo w ed to ha v e function t yp e. The ML syn tax for a function taking x to t[x] is fn x => t[x]. F
  • r
example w e can dene the successor function:
  • fn
x => x + 1; > val it = fn : int
  • >
int Again, the t yp e
  • f
the expression, this time int
  • >
int, is inferred and displa y ed. Ho w ev er the function itself is not prin ted; the system merely writes fn. F unctions are applied b y juxtap
  • sition,
with
  • r
without brac k eting:
  • (fn
x => x + 1) 4; > val it = 5 : int
  • (fn
x => x + 1)(3); > val it = 4 : int John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-8
SLIDE 8 In tro duction to F unctional Programming: Lecture 2 8 Curried functions (1) Ev ery function in ML tak es a single argumen t. One w a y to get the eect
  • f
m ultiple argumen ts is to use a p air for the argumen t | w e'll discuss this next time. Another w a y ,
  • ften
more p
  • w
erful, is to use Currying, making the function tak e its argumen ts
  • ne
at a time, e.g.
  • fn
x => (fn y => x + y); > val it = fn : int
  • >
(int
  • >
int) This function tak es the rst argumen t and giv es a new function. F
  • r
example:
  • (fn
x => (fn y => x + y)) 1; > val it = fn : int
  • >
int is just the successor function again, e.g:
  • ((fn
x => (fn y => x + y)) 1) 6; > val it = 7 : int John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-9
SLIDE 9 In tro duction to F unctional Programming: Lecture 2 9 Curried functions (2) Because curried functions are so common in functional programming, ML xes the rules
  • f
asso ciation to a v
  • id
the need for man y paren theses. When
  • ne
writes E 1 E 2 E 3 , ML asso ciates it as (E 1 E 2 ) E 3 . F
  • r
example, all the follo wing are equiv alen t:
  • ((fn
x => (fn y => x + y))(1))(6); > val it = 7 : int
  • ((fn
x => (fn y => x + y)) 1) 6; > val it = 7 : int
  • (fn
x => (fn y => x + y)) 1 6; > val it = 7 : int
  • (fn
x => fn y => x + y) 1 6; > val it = 7 : int John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-10
SLIDE 10 In tro duction to F unctional Programming: Lecture 2 10 Bindings (1) It is not necessary to ev aluate an expresion all in
  • ne
piece. Y
  • u
can bind an expression to a name using val:
  • val
successor = fn x => x + 1; > val successor = fn : int
  • >
int
  • successor(successo
r( suc ce sso r 0)); > val it = 3 : int Note that this isn't an assignmen t statemen t, merely an abbreviation. But bindings can b e recursiv e: just add rec:
  • val
rec fact = fn n => if n = then 1 else n * fact(n
  • 1);
> val fact = fn : int
  • >
int
  • fact
3; > val it = 6 : int John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-11
SLIDE 11 In tro duction to F unctional Programming: Lecture 2 11 Bindings (2) When binding functions,
  • ne
can also use fun. The follo wing are equiv alen t:
  • val
successor = fn x => x + 1;
  • fun
successor x = x + 1; and
  • val
rec fact = fn n => if n = then 1 else n * fact(n
  • 1);
  • fun
fact n = if n = then 1 else n * fact(n
  • 1);
Note that bindings with fun are always recursiv e. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-12
SLIDE 12 In tro duction to F unctional Programming: Lecture 2 12 Higher
  • rder
functions Curried functions are an example
  • f
a function that giv es another function as a result. W e can also dene functions that tak e
  • ther
functions as argumen ts. This
  • ne
tak es a p
  • sitiv
e in teger n and a function f and returns f n , i.e. f
  • f
(n times):
  • fun
funpow n f x = if n = then x else funpow (n
  • 1)
f (f x); > val funpow = fn : int
  • >
('a
  • >
'a)
  • >
'a
  • >
'a
  • funpow
20 (fn x => 2 * x) 1; > val it = 1048576 : int John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-13
SLIDE 13 In tro duction to F unctional Programming: Lecture 2 13 Lo cal declarations Bindings can b e made lo cal using let decs in expr end F
  • r
example:
  • let
fun fact n = if n = then 1 else n * fact(n
  • 1)
in fact 6 end; This binding is no w in visible b ey
  • nd
the terminating end. Similarly
  • ne
can mak e a declaration lo cal to another de clar ation b y: local decs in decs end John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-14
SLIDE 14 In tro duction to F unctional Programming: Lecture 2 14 ML's ev aluation strategy Execution
  • f
an ML program just means ev aluating an expression. Y
  • u
can think
  • f
this ev aluation as happ ening b y a kind
  • f
syn tactic unfolding
  • f
the program.
  • Constan
ts lik e 1 and + ev aluate to themselv es.
  • Ev
aluation stops immediately at something
  • f
the form fn x => ... and do es not `lo
  • k
inside' the ....
  • When
ev aluating a com bination s t, then rst b
  • th
s and t are ev aluated to s' and t'. If s' is not
  • f
the form fn x => u[x] then things stop there. Otherwise u[t'], the result
  • f
replacing the dumm y v ariable x b y the ev aluated form
  • f
t is ev aluated. It is imp
  • rtan
t to grasp this ev aluation strategy in
  • rder
to understand ML prop erly . John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-15
SLIDE 15 In tro duction to F unctional Programming: Lecture 2 15 Whic h ev aluation strategy But do es ev aluation
  • rder
actually matter? One migh t guess not. F
  • r
example: (1 + 2) + (3 + 4) = 3 + (3 + 4) = 3 + 7 = 10 and (1 + 2) + (3 + 4) = (1 + 2) + 7 = 3 + 7 = 10 giv e the same answ er. Do es this generalize? In fact, for `pure' functional programs, if t w
  • dieren
t ev aluation
  • rders
terminate, they giv e the same answ er. (This is roughly the Chur ch-R
  • sser
the
  • r
em.) But there are still reasons to care ab
  • ut
ev aluation
  • rder:
termination, eciency , and imp erativ e features. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-16
SLIDE 16 In tro duction to F unctional Programming: Lecture 2 16 Lazy ev aluation ML's ev aluation strategy is called e ager
  • r
c al l-by-value b ecause the argumen t to a function is ev aluated ev en if it nev er ends up b eing used. An alternativ e w
  • uld
b e, when ev aluating (fn x => u[x]) t, to ev aluate u[t] without y et ev aluating t. This is c al l-by-name ev aluation. The adv an tage is: w e a v
  • id
ev aluating t if it isn't actually used, and this ev aluation migh t b e v ery costly
  • r
ev en fail to terminate. The disadv an tage
  • f
a naiv e implemen tation is that
  • ne
w
  • uld
re-ev aluate t if it's used more than
  • nce.
So
  • ne
needs a clev er sharing implemen tation | lazy evaluation. Lazy ev aluation seems b etter in principle, and man y functional languages, ev en ML's relativ e Lazy ML, use it. But it's harder to implemen t ecien tly and do esn't seem to t with imp erativ e features. Hence ML's c hoice. John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-17
SLIDE 17 In tro duction to F unctional Programming: Lecture 2 17 ML ev aluation examples (fn x => (fn y => y + y) x) (2 + 2) (fn x => (fn y => y + y) x) 4 (fn y => y + y) 4 4 + 4 8 ((fn f => fn x => f x) (fn y => y + y)) (2 + 2) (fn x => (fn y => y + y) x) (2 + 2) (fn x => (fn y => y + y) x) 4 (fn y => y + y) 4 4 + 4 8 John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998
slide-18
SLIDE 18 In tro duction to F unctional Programming: Lecture 2 18 The conditional Consider ev aluating a factorial (as dened earlier) where the conditional is ev aluated eagerly . T
  • ev
aluate fact(0) w e unfold it to: if = then 1 else * fact(0
  • 1)
W e need to cut this do wn to 1. But under the standard eager rules, w e w
  • uld
rst ev aluate all three sub expressions, including fact(0
  • 1).
This leads to an innite lo
  • p.
So w e can't regard the conditional as an
  • rdinary
function
  • f
three argumen ts: it has to ha v e its
  • wn
`lazy' reduction strategy . The test expression is ev aluated rst, and according to its v alue, precisely
  • ne
  • f
the arms, nev er b
  • th,
John Harrison Univ ersit y
  • f
Cam bridge, 16 Jan uary 1998