Mads Tofte March Lab oratory for Foundations of Computer Science Department of Computer Science Edinburgh University Four Lectures on Standard ML The following notes give an overview of Standard ML with emphasis placed on the Mo dules part of the language The notes are to the b est of my knowledge faithful to The Denition of Standard ML Version as regards syntax semantics and terminology They have b een written so as to b e indep endent of any particular implemen tation The exercises in the rst lectures can b e tackled without the use of a machine although having access to an implementation will no doubt b e b enecial The pro ject in Lecture presupp oses access to an implementation of the full language including mo dules At present the Edinburgh compiler do es not fall into this category the author used the New Jersey Standard ML compiler Lecture gives an introduction to ML aimed at the reader who is familiar with some programming language but do es not know ML Both the Core Language and the Mo dules are covered by way of example Lecture discusses the use of ML mo dules in the development of large programs A useful metho dology for programming with functors signatures and structures is presented Lecture gives a fairly detailed account of the static semantics of ML mo dules for those who really want to understand the crucial notions of sharing and signature matching Lecture presents a one day pro ject intended to give the student an opp ortunity of mo difying a nontrivial piece of software using functors signatures and structures R Harp er R Milner and M Tofte The Denition of Standard ML Version ECSLFCS Labroatory for Foundations of Computer Science Dept of Computer Science University of Edinburgh from functions and ML programmers do this ML at a Glance all the time ML is an example of a func tional language PASCAL is an example of Supp ose we were to draw a map of the land a procedural language scap e of programming languages Where LISP is also sometimes referred to as a would ML t in COBOL and ML could functional language In LISP programs can safely b e put down far apart The in b e treated as data so that LISP programs putoutput facilities in COBOL op erate on directly can decomp ose and transform LISP sp ecic kinds of inputoutput devices for in programs This is harder in ML On the other stance allowing the programmer to declare hand the type discipline of ML is extremely index sequential les ML just has the no helpful in detecting many of the mistakes that tion of streams a stream b eing a sequence pass unnoticed in a LISP program of characters much like streams in UNIX or Like ADA ML has language constructs for text les in PASCAL On the other hand writing large programs Roughly sp eaking a ML is extremely concise compared to the structure in ML corresp onds to a pack verbose COBOL and ML is much b etter age in ADA a signature corresp onds to suited for structuring data and algorithms a package interface and a functor in than COBOL is ML corresp onds to a generic package in ML is closer related to PASCAL Like PAS ADA However ML admits structures not CAL ML has data types and there is a type just types as parameters to functors checker which checks the validity of programs b efore they are run Both PASCAL and ML follow the tradition of ALGOL in that An ML session variables can have lo cal scop e which is de An ML session is an interactive dialogue b e termined statically from the source program tween the ML system and the user You type However PASCAL and ML are radically dif a program in the form of one or more dec ferent in how algorithms are expressed In larations terminated by semicolon and PASCAL as in many other languages a vari the system resp onds either by accepting the able can b e up dated using Algorithms declarations or in case the program is ill are often expressed as iterated sequences of formed by printing an error message statements using while lo ops for instance To give a concrete idea ab out what ML where the eect of executing one statement programs lo ok like we shall work through is to change the underlying store In ML the following example Consider the prob statements are replaced by expressions the lem of implementing heaps A heap eect of evaluating an expression is to pro is a binary tree of items for example duce a value Moreover variables cannot b e up dated references are sp ecial values that can b e up dated and as all other values they can b e b ound to identiers but only rarely are the values one binds to variables references Iteration is expressed using recur sive functions instead of lo ops In ML func tions are values which can b e passed as ar guments to functions and returned as results For a binary tree to b e a heap it must sat isfy that for every item i in the tree i is less type item int than or equal to all items o ccurring b elow i In the ab ove picture items are integers and fun leqp item q item bool the relation less than or equal is the nor p q mal on integers The advantage of a heap is that it always gives fast access to a minimal infix leq item and that it is easy to insert and delete fun maxp q if p leq q then q else p items from a heap This has made the heap and minp q if p leq q then p else q a p opular data structure in a number of very dierent applications It was originally con datatype tree L of item ceived under the name priority queue as a N of item tree tree means of scheduling pro cesses in an op erating system in that case the items are pro cesses val t N L N L L and the partial ordering is that pro cess p is less than or equal to pro cess q if p should fun topL i i b e executed no later than q Heaps are also topNi i used in the heap sort algorithm which is based on the observation that one can sort a list of items by rst inserting the items one We start out by considering integer heaps by one in a heap and then removing them one only therefore we rst declare the type item by one to b e an abbreviation for int Then we de clare a function leq to b e the p ervasive on integers We then declare that leq is to b e used as an inx op erator as illustrated in the declaration of the two functions max and min Every binary tree is either a leaf containing an item or it is a no de containing an item and two trees the subtrees This is expressed by Types and Values the datatype declaration datatype decla rations are automatically recursive ie data types can b e declared in terms of themselves This is illustrated by the declaration of tree In the following gures we present the ML This data type has two constructors L declarations the author provided in this par and N Note that for example is an item ticular session The resp onses from the ML but L applied to written L or just compiler are not shown For clarity the ac L is of type tree Then the heap from the tual input has b een edited using typewriter earlier picture is b ound to the value variable font for the reserved words and italics for t identiers regardless of whether these iden Exercise Declare a heap t of the same tiers are p ervasives eg int or declared by depth as t containing the integers the user eg item and Exercise Write a function size which when applied to a tree returns the total num To dene a function on trees it will suce b er of items in the tree to dene its value in the case the argument tree is a no de and in the case the tree is a Exercise The function top returns a no de The declaration of the function top il minimal item of a heap Write a recursive lustrates this top applied to a tree returns function maxItem which returns a maximal the item at the top of the tree L i and item are examples of patterns Ap Ni plying a function here top to an argument Raising Exceptions eg t is done by matching the argument against the patterns till a matching pattern One often wants to dene a function that can is found For example top t evaluates to not return a result for some of its argument values Supp ose for example that we wish to dene a function initHeap which for given Recursive Functions integer n returns a heap of depth n This only makes sense for n This can b e expressed in ML by raising an exception fun depthL in the case n The eect of evaluating depthNi l r the expression raise e where e is an excep maxdepth l depth r tion is to discontinue the current evaluation Often the exception will b e handled by a depth t handle expression not illustrated by our ex amples if no handler catches the exception fun isHeapL bool true it propagates to the toplevel where it will b e isHeapNi l r rep orted as an uncaught exception i leq top l andalso i leq top r andalso val initial isHeap l andalso isHeap r exception InitHeap fun initHeap n if n then raise InitHeap else if n then Linitial else let val t initHeapn The function depth maps trees to integers in Ninitial t t for instance depth t evaluates to As sp elled end out in the declaration of depth the depth of a leaf is and the depth of any other tree is plus the maximum of the depths of the left and right subtrees The function depth Notice the let dec in exp end expression To is recursive ie dened in terms of itself evaluate it one rst evaluates initHeapn Another example of a recursive function is the and binds the resulting value to t Then function isHeap which when applied to a tree one evaluates the b o dy Ninitial t t us returns the value true if the tree is a heap and ing this value for t Notice that the scop e false otherwise of the declaration of t is the expression
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages53 Page
-
File Size-