
Intro duction to Functional Programming Lecture Intro duction to Functional Programming John Harrison University of Cambridge Lecture Basics of ML Topics covered Versions of ML Running ML on Thor Interacting with ML Higher order functions and currying Evaluation order John Harrison University of Cambridge January Intro duction to Functional Programming Lecture The ML family ML is not a single language Apart from Standard ML which we use here there are many descendants of the original ML used as the metalanguage of Edinburgh LCF eg CAML Light an excellent lightweight system develop ed at INRIA Ob jective CAML a new version of CAML Light including ob jectoriented features Lazy ML a version from Gothenburg using lazy evaluation Standard ML an agreed standard version Standard ML has two parts the Core language and the Modules We will not cover the mo dule system at all John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Implementations of Standard ML There are several implementations of something similar to Standard ML Standard ML of New Jersey free but very resourcehungry Moscow ML free but do esnt have the Mo dules Poly ML go o d but a commercial pro duct and do esnt run under Linux Harlequin ML a newer commercial system with integrated development environment Well use Moscow ML This is a go o d lightweight implementation based on CAML Light written by Sergei Romanenko Moscow and Peter Sestoft Cop enhagen The features we use are general and can easily b e translated to other dialects John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Starting up ML Moscow ML is already installed on Thor In order to start it up typ e groupclteachjr hm os ml a rch bin m osm l This will invoke the appropriate version dep ending on the machine architecture The system should start up and present its prompt groupclteachjr hm os ml a rch bin m osm l Moscow ML version July Enter quit to quit If you want to install Moscow ML on your own computer see the Web page httpwwwdina kv ld k se st oft m osm l htm l John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Interacting with ML We will run ML as an interpreter rather like a sophisticated calculator You just typ e an expression into it terminated by a semicolon and it prints the result of evaluating it eg val it int ML not only prints the result but also infers the int If ML cannot type of the expression namely assign a typ e to an expression then it is rejected true Type clash ML knows the typ e of the builtin op erator and that is how it makes its typ e inference Well treat the typ e system more systematically next time for the moment it should b e intuitively clear John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Loading from les We have just b een typing things into ML and thinking ab out the results However one do esnt write real programs in this way Typically one writes the expressions and declarations in a le To try them out as you go these can b e inserted in the ML window using cut and paste You can cut and paste using Xwindows and similar systems or an editor like Emacs with multiple buers For larger programs its convenient simply to load them from le into the ML session This can use command in ML eg b e done using the use myprogml Programs can also include comments written b etween and These are ignored by ML but are useful for p eople reading the co de John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Using functions Since ML is a functional language expressions are allowed to have function typ e The ML syntax for x to tx is fn x tx For a function taking example we can dene the successor function fn x x val it fn int int Again the typ e of the expression this time int is inferred and displayed However the int function itself is not printed the system merely writes fn Functions are applied by juxtap osition with or without bracketing fn x x val it int fn x x val it int John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Curried functions Every function in ML takes a single argument One way to get the eect of multiple arguments is to use a pair for the argument well discuss this next time Another way often more p owerful is to use Currying making the function take its arguments one at a time eg fn x fn y x y val it fn int int int This function takes the rst argument and gives a new function For example fn x fn y x y val it fn int int is just the successor function again eg fn x fn y x y val it int John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Curried functions Because curried functions are so common in functional programming ML xes the rules of asso ciation to avoid the need for many parentheses E E E ML asso ciates it as When one writes E E E For example all the following are equivalent fn x fn y x y val it int fn x fn y x y val it int fn x fn y x y val it int fn x fn y x y val it int John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Bindings It is not necessary to evaluate an expresion all in one piece You can bind an expression to a name using val val successor fn x x val successor fn int int successorsuccesso r suc ce sso r val it int Note that this isnt an assignment statement merely an abbreviation But bindings can b e rec recursive just add val rec fact fn n if n then else n factn val fact fn int int fact val it int John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Bindings When binding functions one can also use fun The following are equivalent val successor fn x x fun successor x x and val rec fact fn n if n then else n factn fun fact n if n then else n factn Note that bindings with fun are always recursive John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Higher order functions Curried functions are an example of a function that gives another function as a result We can also dene functions that take other functions as arguments This one takes a p ositive n n and a function f and returns f ie integer f f n times fun funpow n f x if n then x else funpow n f f x val funpow fn int a a a a funpow fn x x val it int John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Lo cal declarations Bindings can b e made lo cal using let decs in expr end For example let fun fact n if n then else n factn in fact end This binding is now invisible b eyond the end Similarly one can make a terminating declaration lo cal to another declaration by local decs in decs end John Harrison University of Cambridge January Intro duction to Functional Programming Lecture MLs evaluation strategy Execution of an ML program just means evaluating an expression You can think of this evaluation as happ ening by a kind of syntactic unfolding of the program Constants like and evaluate to themselves Evaluation stops immediately at something of fn x and do es not lo ok the form inside the When evaluating a combination s t then s and t are evaluated to s and t rst b oth s is not of the form fn x ux then If ut the things stop there Otherwise result of replacing the dummy variable x by t is evaluated the evaluated form of It is imp ortant to grasp this evaluation strategy in order to understand ML prop erly John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Which evaluation strategy But do es evaluation order actually matter One might guess not For example and give the same answer Do es this generalize In fact for pure functional programs if two dierent evaluation orders terminate they give the same answer This is roughly the ChurchRosser theorem But there are still reasons to care ab out evaluation order termination eciency and imp erative features John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Lazy evaluation MLs evaluation strategy is called eager or cal lbyvalue b ecause the argument to a function is evaluated even if it never ends up b eing used fn x An alternative would b e when evaluating to evaluate ut without yet ux t evaluating t This is cal lbyname evaluation t if it isnt The advantage is we avoid evaluating actually used and this evaluation might b e very costly or even fail to terminate The disadvantage of a naive implementation is t if its used more that one would reevaluate than once So one needs a clever sharing implementation lazy evaluation Lazy evaluation seems b etter in principle and many functional languages even MLs relative Lazy ML use it But its harder to implement eciently and do esnt seem to t with imp erative features Hence MLs choice John Harrison University of Cambridge January Intro duction to Functional Programming Lecture ML evaluation examples fn x fn y y y x fn x fn y y y x fn y y y fn f fn x f x fn y y y fn x fn y y y x fn x fn y y y x fn y y y John Harrison University of Cambridge January Intro duction to Functional Programming Lecture The conditional Consider evaluating a factorial as dened earlier where the conditional is evaluated eagerly To fact we unfold it to evaluate if then else fact We need to cut this down to But under the standard eager rules we would rst evaluate all fact three sub expressions including This leads to an innite lo op So we cant regard the conditional as an ordinary function of three arguments it has to have its own lazy reduction strategy The test expression is evaluated rst and according to its value precisely one of the arms never b oth John Harrison University of Cambridge January .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages18 Page
-
File Size-