Example: barber

Programming in Standard ML

Programming in Standard ML. (D RAFT: V ERSION OF ). Robert Harper Carnegie Mellon University Spring Semester, 2011. Copyright 2011. c by Robert Harper. All Rights Reserved. This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works United States License. To view a copy of this license, visit , or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Preface This book is an introduction to Programming with the Standard ML pro- gramming language . It began life as a set of lecture notes for Computer Science 15 212: Principles of Programming , the second semester of the in- troductory sequence in the undergraduate computer science curriculum at Carnegie Mellon University.

Preface This book is an introduction to programming with the Standard ML pro-gramming language. It began life as a set of lecture notes forComputer

Tags:

  Programming, Language, Standards, Standard ml, Standard ml pro gramming language, Gramming

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Programming in Standard ML

1 Programming in Standard ML. (D RAFT: V ERSION OF ). Robert Harper Carnegie Mellon University Spring Semester, 2011. Copyright 2011. c by Robert Harper. All Rights Reserved. This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works United States License. To view a copy of this license, visit , or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Preface This book is an introduction to Programming with the Standard ML pro- gramming language . It began life as a set of lecture notes for Computer Science 15 212: Principles of Programming , the second semester of the in- troductory sequence in the undergraduate computer science curriculum at Carnegie Mellon University.

2 It has subsequently been used in many other courses at Carnegie Mellon, and at a number of universities around the world. It is intended to supersede my Introduction to Standard ML, which has been widely circulated over the last ten years. Standard ML is a formally defined Programming language . The Defi- nition of Standard ML (Revised) is the formal definition of the language . It is supplemented by the Standard ML Basis Library, which defines a com- mon basis of types that are shared by all implementations of the language . Commentary on Standard ML discusses some of the decisions that went into the design of the first version of the language . There are several implementations of Standard ML available for a wide variety of hardware and software platforms.

3 The best-known compilers are Standard ML of New Jersey, MLton, Moscow ML, MLKit, and PolyML. These are all freely available on the worldwide web. Please refer to The Standard ML Home Page for up-to-date information on Standard ML and its implementations. Numerous people have contributed directly and indirectly to this text. I am especially grateful to the following people for their helpful com- ments and suggestions: Brian Adkins, Nels Beckman, Marc Bezem, James Bostock, Terrence Brannon, Franck van Breugel, Chris Capel, Matthew William Cox, Karl Crary, Yaakov Eisenberg, Matt Elder, Mike Erdmann, Matthias Felleisen, Andrei Formiga, Stephen Harris, Nils J ahnig, Joel Jones, David Koppstein, John Lafferty, Johannes Laire, Flavio Lerda, Daniel R.

4 Licata, Adrian Moos, Bryce Nichols, Michael Norrish, Arthur J. O'Dwyer, Frank Pfenning, Chris Stone, Dave Swasey, Michael Velten, Johan Wallen, Scott Williams, and Jeannette Wing. Richard C. Cobbe helped with font se- lection. I am also grateful to the many students of 15-212 who used these notes and sent in their suggestions over the years. These notes are a work in progress. Corrections, comments and sug- gestions are most welcome. Contents Preface ii I Overview 1. 1 Programming in Standard ML 3. A Regular Expression Package .. 3. Sample Code .. 12. II The Core language 13. 2 Types, Values, and Effects 15. Evaluation and Execution .. 15. The ML Computation Model .. 16. Type Checking .. 17. Evaluation .. 19.

5 Types, Types, Types .. 21. Type Errors .. 23. Sample Code .. 23. 3 Declarations 24. Variables .. 24. Basic Bindings .. 25. Type Bindings .. 25. Value Bindings .. 26. Compound Declarations .. 27. Limiting Scope .. 28. CONTENTS vi Typing and Evaluation .. 29. Sample Code .. 32. 4 Functions 33. Functions as Templates .. 33. Functions and Application .. 34. Binding and Scope, Revisited .. 37. Sample Code .. 39. 5 Products and Records 40. Product Types .. 40. Tuples .. 40. Tuple Patterns .. 42. Record Types .. 45. Multiple Arguments and Multiple Results .. 48. Sample Code .. 50. 6 Case Analysis 51. Homogeneous and Heterogeneous Types .. 51. Clausal Function Expressions .. 52. Booleans and Conditionals, Revisited.

6 53. Exhaustiveness and Redundancy .. 54. Sample Code .. 56. 7 Recursive Functions 57. Self-Reference and Recursion .. 58. Iteration .. 61. Inductive Reasoning .. 62. Mutual Recursion .. 65. Sample Code .. 66. 8 Type Inference and Polymorphism 67. Type Inference .. 67. Polymorphic Definitions .. 70. Overloading .. 73. Sample Code .. 76. R EVISED D RAFT V ERSION CONTENTS vii 9 Programming with Lists 77. List Primitives .. 77. Computing With Lists .. 79. Sample Code .. 81. 10 Concrete Data Types 82. Datatype Declarations .. 82. Non-Recursive Datatypes .. 83. Recursive Datatypes .. 85. Heterogeneous Data Structures .. 88. Abstract Syntax .. 89. Sample Code .. 91. 11 Higher-Order Functions 92. Functions as Values.

7 92. Binding and Scope .. 93. Returning Functions .. 95. Patterns of Control .. 97. Staging .. 99. Sample Code .. 102. 12 Exceptions 103. Exceptions as Errors .. 104. Primitive Exceptions .. 104. User-Defined Exceptions .. 105. Exception Handlers .. 107. Value-Carrying Exceptions .. 110. Sample Code .. 112. 13 Mutable Storage 113. Reference Cells .. 113. Reference Patterns .. 115. Identity .. 116. Aliasing .. 118. Programming Well With References .. 119. Private Storage .. 120. Mutable Data Structures .. 122. Mutable Arrays .. 124. R EVISED D RAFT V ERSION CONTENTS viii Sample Code .. 126. 14 Input/Output 127. Textual Input/Output .. 127. Sample Code .. 129. 15 Lazy Data Structures 130. Lazy Data Types .. 132.

8 Lazy Function Definitions .. 133. Programming with Streams .. 135. Sample Code .. 137. 16 Equality and Equality Types 138. Sample Code .. 138. 17 Concurrency 139. Sample Code .. 139. III The Module language 140. 18 Signatures and Structures 142. Signatures .. 142. Basic Signatures .. 143. Signature Inheritance .. 144. Structures .. 147. Basic Structures .. 147. Long and Short Identifiers .. 148. Sample Code .. 150. 19 Signature Matching 151. Principal Signatures .. 152. Matching .. 153. Satisfaction .. 157. Sample Code .. 157. 20 Signature Ascription 158. Ascribed Structure Bindings .. 158. Opaque Ascription .. 159. R EVISED D RAFT V ERSION CONTENTS ix Transparent Ascription .. 162. Sample Code .. 164. 21 Module Hierarchies 165.

9 Substructures .. 165. Sample Code .. 173. 22 Sharing Specifications 174. Combining Abstractions .. 174. Sample Code .. 181. 23 Parameterization 182. Functor Bindings and Applications .. 182. Functors and Sharing Specifications .. 185. Avoiding Sharing Specifications .. 187. Sample Code .. 191. IV Programming Techniques 192. 24 Specifications and Correctness 194. Specifications .. 194. Correctness Proofs .. 196. Enforcement and Compliance .. 199. 25 Induction and Recursion 202. Exponentiation .. 202. The GCD Algorithm .. 207. Sample Code .. 211. 26 Structural Induction 212. Natural Numbers .. 212. Lists .. 214. Trees .. 215. Generalizations and Limitations .. 216. Abstracting Induction .. 217. Sample Code.

10 219. R EVISED D RAFT V ERSION CONTENTS x 27 Proof-Directed Debugging 220. Regular Expressions and Languages .. 220. Specifying the Matcher .. 222. Sample Code .. 228. 28 Persistent and Ephemeral Data Structures 229. Persistent Queues .. 232. Amortized Analysis .. 235. Sample Code .. 238. 29 Options, Exceptions, and Continuations 239. The n-Queens Problem .. 239. Solution Using Options .. 241. Solution Using Exceptions .. 242. Solution Using Continuations .. 244. Sample Code .. 246. 30 Higher-Order Functions 247. Infinite Sequences .. 248. Circuit Simulation .. 251. Regular Expression Matching, Revisited .. 254. Sample Code .. 256. 31 Memoization 257. Cacheing Results .. 257. Laziness .. 259. Lazy Data Types in SML/NJ.


Related search queries