Notes on Programming Standard ML of New Jersey (Version 110.0.6)
Total Page:16
File Type:pdf, Size:1020Kb
Notes on Programming Standard ML of New Jersey (version 110.0.6) Riccardo Pucella Department of Computer Science Cornell University Last revised: January 10, 2001 ii Preface The impetus behind these notes was the desire to provide a cohesive description of Standard ML of New Jersey, an interactive compiler and environment for Standard ML. The goal is to end up with a complete user guide to the system, inclduing the libraries, the tools and the extensions, as well as a tutorial on how to write “real” applications, centered around the use of the module system and the compilation manager. Other reasons include the desire to provide hands-on examples of how to use some maybe poorly understood features or features with an interface different from what one may be used to. Examples of such features include sockets, the input and output subsystem, the readers approach to text scanning and the use of continuations to handle non-local control-flow. All in all, this would be a repository for snippets of SML and SML/NJ programming lore. These notes are not a tutorial introduction to Standard ML. There exists excel- lent introductory material, available both commercially or freely over the Internet. These notes complement this literature by focusing on the Standard ML of New Jersey environment. The first part of these notes does given an overview of the language, but a quick one and without highlighting some of the subtleties of the language. Better writers than I have written better introductory material and I urge you to read those first. References are given in the chapter notes of Chapter 1. I go in somewhat more details when describing the Basis Library, since some of the underlying ideas are fundamental to the overall programming philosophy. Unfor- tunately, that chapter is long, boring and reads more like a reference manual than a tutorial. Throughness and precision at odds with readability. With luck, enough sample code and tutorial material is interspersed to lighten the mood. In the course of the notes, I take the opportunity to describe more advanced topics typically not covered in introductory material, such as sockets programming, signals handling, continuations, concurrency. Some of these subjects are discussed in advanced pro- gramming language courses, which not every one has taken or plan to take. Some of these subjects are hardly discussed and one needs to rummage technical papers or source code. These notes are quite obviously a work in progress. As such, any comments or iii iv PREFACE suggestions will be more than welcome. This version includes chapters 1 through 7. Planned chapters for the end of spring of 2001 include: Chap. 8 : ML-Lex: A Lexical Analyzer Generator Chap. 9 : ML-Yacc: A Parser Generator Chap. 10 : Input and Output Chap. 11 : Sockets Chap. 12 : Regular Expressions Chap. 13 : SML/NJ Extensions Chap. 14 : Continuations In the long run, chapters on the foreign function interface, CML, eXene, reader- based lexing and parsing, prettyprinting and programming reactive systems are planned, as well as sample applications including a matrix package, an interactive theorem prover, a tool to generate simple language front ends, a graphical game a` la MineSweeper, and a simulation. Suggestions on content will also be welcome. Notation In the text, sample code is written in italics. Types are written using more mathe- matical notation, namely tuple types are given as int int, and function types as int int. × ! Acknowledgments Contents Preface iii 1 Introduction 1 1.1 Standard ML . 1 1.2 Standard ML of New Jersey . 2 1.3 History of the system . 3 1.4 Availability and resources . 4 1.5 Installation . 5 1.6 Getting started . 6 Notes . 12 I Standard ML 15 2 The Core Language 17 2.1 Basic types and expressions . 17 2.2 Tuples and records . 19 2.3 Declarations . 20 2.4 Pattern matching . 23 2.5 Functions . 27 2.6 Polymorphism . 32 2.7 Recursion . 34 2.8 Lists . 38 2.9 Equality . 38 2.10 References . 40 2.11 Exceptions . 41 Notes . 42 v vi CONTENTS 3 The Module System 45 3.1 Structures . 45 3.2 Signatures . 50 3.3 Functors . 58 3.4 Programming with modules . 62 Notes . 62 4 The Basis Library 65 4.1 Overview . 65 4.2 Basic types . 68 4.3 More on strings . 86 4.4 Aggregate types . 92 4.5 Input and output . 102 4.6 System functions . 106 4.7 Time and dates . 112 4.8 Compatibility with SML’90 . 118 Notes . 120 II Standard ML of New Jersey 123 5 The Interactive Compiler 125 5.1 Controlling the runtime system . 125 5.2 Controlling the compiler . 129 5.3 Prettyprinting . 135 5.4 Heap images . 144 5.5 Unsafe operations . 151 Notes . 155 6 The Compilation Manager 157 6.1 Overview of CM . 158 6.2 Group hierarchies . 161 6.3 Tools . 164 6.4 A simple configuration tool . 170 6.5 Technicalities . 177 Notes . 177 7 The SML/NJ Library 179 7.1 Overview . 179 7.2 Types and data structures . 180 CONTENTS vii 7.3 Arrays and vectors . 186 7.4 Sets and maps . 191 7.5 Hash tables . 196 7.6 Sorting . 202 7.7 Formatting . 206 7.8 Handling command-line arguments . 212 7.9 Miscellaneous functionality . 216 Notes . 223 Bibliography 225 A SML/NJ Grammar 235 viii CONTENTS List of Figures 4.1 The structure General ........................ 68 4.2 The structure Bool .......................... 69 4.3 The structure Option ........................ 69 4.4 The signature CHAR ........................ 72 4.5 The signature STRING ....................... 74 4.6 The signature INTEGER ...................... 77 4.7 The signature WORD ........................ 80 4.8 The signature REAL ......................... 82 4.9 The signature MATH ........................ 85 4.10 The signature SUBSTRING ..................... 87 4.11 The structure StringCvt ....................... 89 4.12 The structure List .......................... 93 4.13 The structure ListPair ........................ 95 4.14 The structure Vector ......................... 96 4.15 The structure Subvector ....................... 97 4.16 The structure Array ......................... 99 4.17 The structure Array2 . 101 4.18 The structure TextIO . 103 4.19 The structure OS . 106 4.20 The structure OS.FileSys . 107 4.21 The structure OS.Path . 109 4.22 The structure OS.Process . 111 4.23 The structure Unix . 111 4.24 The structure CommandLine . 112 4.25 The structure Time . 113 4.26 The structure Date . 115 4.27 Formatting characters for Date.fmt . 116 4.28 The structure TimeZone . 117 4.29 The structure Timer . 118 4.30 The structure SML90 . 119 ix x LIST OF FIGURES 5.1 The structure SMLofNJ . 126 5.2 The structure SMLofNJ.SysInfo . 127 5.3 The structure IntervalTimer . 127 5.4 The structure SMLofNJ.Internals . 127 5.5 The structure CleanUp . 128 5.6 The structure GC . 129 5.7 The structure Compiler . 130 5.8 The structure Control . 131 5.9 The structure Print . 132 5.10 The structure Interact . 134 5.11 The structure Compiler.PrettyPrint . 135 5.12 The structure SimpleXML . 136 5.13 A passage from Shakespeare . 137 5.14 The structure Unsafe . 152 5.15 The structure Unsafe.Object . 153 5.16 The structure Unsafe.CInterface . 154 6.1 The Hello World program . 158 6.2 The structure CM . 160 6.3 The structure CM.Tools . 166 6.4 The signature LOOKUP TABLE . 171 6.5 The structure NaiveLookupTable . 171 6.6 The functor CfgFun . 172 6.7 The structure InstallMLConfig . 175 7.1 The structure LibBase . 180 7.2 The structure Atom . 181 7.3 The structure CharMap . 181 7.4 The structure Fifo . ..