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 erativ e programming � F unctional programming � The merits of functional programming � Historical remarks � Ov erview of the course John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 2 Imp erativ e programming Imp erativ e (or pro cedural) programs rely on mo difying a state b y using a sequence of c ommands . The state is mainly mo di�ed b y the assignment command, written v = E or v := E . W e can execute one command b efore another b y writing them in sequence, p erhaps separated b y a semicolon: C ; C . 1 2 Commands can b e executed conditionally using if , and rep eatedly using while . Programs are a series of instructions on ho w to mo dify the state. Imp erativ e languages, e.g. F OR TRAN, Algol, C, Mo dula-3 supp ort this st yle of programming. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 3 An abstract view W e ignore input-output op 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: � ! � ! � ! � � � ! � 0 1 2 n The program is started with the computer in an initial state � , including the inputs to the 0 program. The program �nishes with the computer in a �nal state � , con taining the output(s) of the program. n The state passes through a �nite sequence of c hanges to get from � to � ; in general, eac h 0 n command ma y mo dify the state. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
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 � = E [ � ]. n 0 � 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 one expression do es not a�ect another. But on the p ositiv 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 order functions. F unctional languages supp ort this st yle of programming. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
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 ould b e expressed in ML as a recursiv e function: fun fact n = if n = 0 then 1 else n * fact(n - 1); John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
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 oks v ery impractical. W e will sho w in this course ho w a lot of in teresting programming can b e done in the functional st yle. Imp erativ e programming languages ha v e arisen as an abstraction of the hardw are, from mac hine co de, through assem blers and macro assem blers, to F OR TRAN and b ey ond. 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 of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 7 Merits of functional programming By a v oiding v ariables and assignmen ts, w e gain the follo wing adv an tages: � Clearer seman tics. Programs corresp ond more directly to abstract mathematical ob jects. � More freedom in implemen tation, e.g. parallelizabilit y . By the more �exible use of functions, w e gain: � Conciseness and elegance. � Better parametrization and mo dularit y of programs. � Con v enien t w a ys of represen ting in�nite data. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 8 Denotational seman tics W e can iden tify our ML factorial function with an abstract mathematical (partial) function Z ! Z : 8 < n ! if n � 0 [ [fact ] ]( n ) = : ? otherwise where ? denotes unde�nedness, 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 orks. Here is a C `function' that do esn't corresp ond to an y mathematical function: int rand(void) { static int n = 0; return n = 2147001325 * n + 715136305; } This giv es di�eren t results on successiv e calls! John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 9 Seman tics of imp erativ e programs In order to giv e a corresp onding seman tics to imp erativ e programs, w e need to mak e the state explicit. F or example w e can mo del commands as: � P artial functions � ! � (Strac hey) � Relations on � � � (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 on c ontinuations (W adsw orth, Morris). All these metho ds are quite complicated. With functional programs, w e ha v e a real c hance of pro ving their correctness, or the correctness of certain transformations or optimizations. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 10 Problems with functional programs F unctional programming is not without its de�ciencies. Some things are harder to �t in to a purely functional mo del, e.g. � Input-output � In teractiv e or con tin uously running programs (e.g. editors, pro cess con trollers). Ho w ev er, in man y w a ys, in�nite data structures can b e used to accommo date these things. F unctional languages also corresp ond less closely to curren t hardw are, so they can b e less e�cien t, and it can b e hard to reason ab out their time and space usage. ML is not a pure functional language, so y ou can use v ariables and assignmen ts if required. Ho w ev er most of our w ork is in the pure functional subset. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 11 Historical remarks Some of 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 of 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 other imp ortan t dialects, notably CAML and Ob jectiv e CAML. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
In tro duction to F unctional Programming: Lecture 1 12 Ov erview of the course (1) � Practicalities of in teracting with ML. � Key functional concepts, e.g. ev aluation strategy , higher order functions. � P olymorphic t yp es. � Recursiv e functions and recursiv e structures. � Hin ts for e�ectiv e programming. � Exceptions, references and other imp erativ e features. � Pro ving programs correct. John Harrison Univ ersit y of Cam bridge, 15 Jan uary 1998
Recommend
More recommend