Introduction to Standard ML

Introduction to Standard ML

Intro duction to Standard ML Rob ert Harp er Scho ol of Computer Science Carnegie Mellon University Pittsburgh PA c Copyright Rob ert Harp er All rights reserved 1 With exercises by Kevin Mitchell Edinburgh University Edinburgh UK Contents Intro duction The Core Language Interacting with ML Basic expressions values and typ es Unit Bo oleans Integers Strings Real Numb ers Tuples Lists Records Identiers bindings and declarations Patterns Dening functions Polymorphism and Overloading Dening typ es Exceptions Imp erative features The Mo dules System Overview Structures and Signatures Abstractions Functors The mo dules system in practice iii iv CONTENTS InputOutput A Answers Acknowledgements Several of the examples were cribb ed from Luca Cardellis intro duction to his dialect of ML from Robin Milners core language denition from Dave MacQueens mo dules pap er and from Ab elson and Sussmans b o ok Joachim Parrow Don Sannella and David Walker made many helpful sug gestions vii Chapter Intro duction These notes are an intro duction to the Standard ML programming language Here are some of the highlights of Standard ML ML is a functional programming language Functions are rstclass data ob jects they may b e passed as arguments returned as results and stored in variables The principal control mechanism in ML is recursive function application ML is an interactive language Every phrase read is analyzed compiled and executed and the value of the phrase is rep orted together with its typ e ML is strongly typed Every legal expression has a typ e which is deter mined automatically by the compiler Strong typing guarantees that no program can incur a typ e error at run time a common source of bugs ML has a polymorphic typ e system Each legal phrase has a uniquely determined most general typing that determines the set of contexts in which that phrase may b e legally used ML supp orts abstract types Abstract typ es are a useful mechanism for program mo dularization New typ es together with a set of functions on ob jects of that typ e may b e dened The details of the implementation are hidden from the user of the typ e achieving a degree of isolation that is crucial to program maintenance CHAPTER INTRODUCTION ML is statical ly scoped ML resolves identier references at compile time leading to more mo dular and more ecient programs ML has a typ esafe exception mechanism Exceptions are a useful means of handling unusual or deviant conditions arising at runtime ML has a modules facility to supp ort the incremental construction of large programs An ML program is constructed as an interdep endent collection of structures which are glued together using functors Sepa rate compilation is supp orted through the ability to exp ort and imp ort functors Standard ML is the newest memb er of a family of languages tracing its origins to the ML language develop ed at Edinburgh by Mike Gordon Robin Milner and Chris Wadsworth in the midseventies Since then numerous dialects and implementations have arisen b oth at Edinburgh and elsewhere Standard ML is a synthesis of many of the ideas that were explored in the variant languages notably Luca Cardellis dialect and in the functional language HOPE develop ed by Ro d Burstall Dave MacQueen and Don San nella The most recent addition to the language is the mo dules system develop ed by Dave MacQueen These notes are intended as an informal intro duction to the language and its use and should not b e regarded as a denitive description of Standard ML They have evolved over a numb er of years and are in need of revision b oth to reect changes in the language and the exp erience gained with it since its inception Comments and suggestions from readers are welcome The denition of Standard ML is available from MIT Press A less formal but in many ways obsolete account is available as an Edinburgh University technical rep ort The reader is encouraged to consult the denition for precise details ab out the language Chapter The Core Language Interacting with ML Most implementations of ML are interactive with the basic form of interac tion b eing the readevalprint dialogue familiar to LISP users in which an expression is entered ML analyzes compiles and executes it and the 1 result is printed on the terminal Here is a sample interaction int ML prompts with and precedes its output with The user entered the phrase ML evaluated this expression and printed the value of the phrase together with its typ e int Various sorts of errors can arise during an interaction with ML Most of these fall into three categories syntax errors typ e errors and runtime faults You are probably familiar with syntax errors and runtime errors from your exp erience with other programming languages Here is an example of what happ ens when you enter a syntactically incorrect phrase let x in x end Parse error Was expecting in in let x 1 The details of the interaction with the ML top level vary from one implementation to another but the overall feel is similar in all systems known to the author These notes were prepared using the Edinburgh compiler circa CHAPTER THE CORE LANGUAGE Runtime errors such as dividing by zero are a form of exception ab out which we shall have more to say b elow For now we simply illustrate the sort of output that you can exp ect from a runtime error div Failure Div The notion of a typ e error is somewhat more unusual We shall have more to say ab out typ es and typ e errors later For now it suces to remark that a typ e error arises from the improp er use of a value such as trying to add to true true Type clash in true Looking for a int I have found a bool One particularly irksome form of error that ML cannot diagnose is the innite lo op If you susp ect that your program is lo oping then it is often p os sible to break the lo op by typing the interrupt character typically Control C ML will resp ond with a message indicating that the exception interrupt has b een raised and return to the top level Some implementations have a debugging facility that may b e helpful in diagnosing the problem Other forms of errors do arise but they are relatively less common and are often dicult to explain outside of context If you do get an error message that you cannot understand then try to nd someone with more exp erience with ML to help you The details of the user interface vary from one implementation to another particularly with regard to output format and error messages The examples that follow are based on the Edinburgh ML compiler you should have no diculty interpreting the output and relating it to that of other compilers Basic expressions values and typ es We b egin our intro duction to ML by intro ducing a set of basic typ es In ML a typ e is a collection of values For example the integers form a typ e as do the character strings and the b o oleans Given any two typ es and the set of ordered pairs of values with the left comp onent a value of typ e and BASIC EXPRESSIONS VALUES AND TYPES the right a value of typ e is a typ e More signicantly the set of functions mapping one typ e to another form a typ e In addition to these and other basic typ es ML allows for userdened typ es We shall return to this p oint later Expressions in ML denote values in the same way that numerals denote numb ers The typ e of an expression is determined by a set of rules that guarantee that if the expression has a value then the value of the expression is a value of the typ e assigned to the expression got that For example every numeral has typ e int since the value of a numeral is an integer We shall illustrate the typing system of ML by example Unit The typ e unit consists of a single value written sometimes pronounced unit as well This typ e is used whenever an expression has no interesting value or when a function is to have no arguments Bo oleans The typ e bool consists of the values true and false The ordinary b o olean negation is available as not the b o olean functions andalso and orelse are also provided as primitive The conditional expression if e then e else e is also considered 1 2 here b ecause its rst argument e must b e a b o olean Note that the else clause is not optional The reason is that this if is a conditional expres sion rather than a conditional command such as in Pascal If the else clause were omitted and the test were false then the expression would have no value Note to o that b oth the then expression and the else expression must have the same typ e The expression if true then true else is type incorrect or il ltyped since the typ e of the then clause is bool whereas the typ e of the else clause is unit not true false bool false andalso true CHAPTER THE CORE LANGUAGE false bool false orelse true true bool if false then false else true true bool if true then false else true false bool Integers The typ e int is the set of p ositive and negative integers Integers are written in the

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    106 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us