ebook img

Programming in Standard ML PDF

2011·0.72 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming in Standard ML

Programming in Standard ML (DRAFT: VERSION 1.2 OF 11.02.11.) Robert Harper Carnegie Mellon University Spring Semester, 2011 Copyright(cid:13)c 2011byRobertHarper. AllRightsReserved. ThisworkislicensedundertheCreativeCommons Attribution-Noncommercial-NoDerivativeWorks3.0UnitedStates License. Toviewacopyofthislicense,visit http://creativecommons.org/licenses/by-nc-nd/3.0/us/,orsenda lettertoCreativeCommons,171SecondStreet,Suite300,SanFrancisco, 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 Science15–212: PrinciplesofProgramming,thesecondsemesterofthein- troductorysequenceintheundergraduatecomputersciencecurriculumat Carnegie MellonUniversity. It hassubsequently beenused inmany 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 hasbeenwidelycirculatedoverthelasttenyears. 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- monbasisoftypesthataresharedbyallimplementationsofthelanguage. CommentaryonStandardMLdiscussessomeofthedecisionsthatwentinto thedesignofthefirstversionofthelanguage. ThereareseveralimplementationsofStandardMLavailableforawide variety of hardware and software platforms. 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 itsimplementations. 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, MatthiasFelleisen,AndreiFormiga,StephenHarris,NilsJa¨hnig,JoelJones, David Koppstein, John Lafferty, Johannes Laire, Flavio Lerda, Daniel R. Licata, Adrian Moos, Bryce Nichols, Michael Norrish, Arthur J. O’Dwyer, Frank Pfenning, Chris Stone, Dave Swasey, Michael Velten, Johan Wallen, ScottWilliams,andJeannetteWing. RichardC.Cobbehelpedwithfontse- lection. I am also grateful to the many students of 15-212 who used these notesandsentintheirsuggestionsovertheyears. These notes are a work in progress. Corrections, comments and sug- gestionsaremostwelcome. Contents Preface ii I Overview 1 1 ProgramminginStandardML 3 1.1 ARegularExpressionPackage . . . . . . . . . . . . . . . . . 3 1.2 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 II The Core Language 13 2 Types,Values,andEffects 15 2.1 EvaluationandExecution . . . . . . . . . . . . . . . . . . . . 15 2.2 TheMLComputationModel . . . . . . . . . . . . . . . . . . 16 2.2.1 TypeChecking . . . . . . . . . . . . . . . . . . . . . . 17 2.2.2 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Types,Types,Types . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 TypeErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3 Declarations 24 3.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.2 BasicBindings . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2.1 TypeBindings . . . . . . . . . . . . . . . . . . . . . . . 25 3.2.2 ValueBindings . . . . . . . . . . . . . . . . . . . . . . 26 3.3 CompoundDeclarations . . . . . . . . . . . . . . . . . . . . . 27 3.4 LimitingScope . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 CONTENTS vi 3.5 TypingandEvaluation . . . . . . . . . . . . . . . . . . . . . . 29 3.6 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 4 Functions 33 4.1 FunctionsasTemplates . . . . . . . . . . . . . . . . . . . . . . 33 4.2 FunctionsandApplication . . . . . . . . . . . . . . . . . . . . 34 4.3 BindingandScope,Revisited . . . . . . . . . . . . . . . . . . 37 4.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 5 ProductsandRecords 40 5.1 ProductTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.1.1 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.1.2 TuplePatterns . . . . . . . . . . . . . . . . . . . . . . . 42 5.2 RecordTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.3 MultipleArgumentsandMultipleResults . . . . . . . . . . . 48 5.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6 CaseAnalysis 51 6.1 HomogeneousandHeterogeneousTypes . . . . . . . . . . . 51 6.2 ClausalFunctionExpressions . . . . . . . . . . . . . . . . . . 52 6.3 BooleansandConditionals,Revisited . . . . . . . . . . . . . 53 6.4 ExhaustivenessandRedundancy . . . . . . . . . . . . . . . . 54 6.5 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7 RecursiveFunctions 57 7.1 Self-ReferenceandRecursion . . . . . . . . . . . . . . . . . . 58 7.2 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 7.3 InductiveReasoning . . . . . . . . . . . . . . . . . . . . . . . 62 7.4 MutualRecursion . . . . . . . . . . . . . . . . . . . . . . . . . 65 7.5 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 8 TypeInferenceandPolymorphism 67 8.1 TypeInference . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8.2 PolymorphicDefinitions . . . . . . . . . . . . . . . . . . . . . 70 8.3 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 8.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 REVISED 11.02.11 DRAFT VERSION 1.2 CONTENTS vii 9 ProgrammingwithLists 77 9.1 ListPrimitives . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 9.2 ComputingWithLists . . . . . . . . . . . . . . . . . . . . . . 79 9.3 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 10 ConcreteDataTypes 82 10.1 DatatypeDeclarations . . . . . . . . . . . . . . . . . . . . . . 82 10.2 Non-RecursiveDatatypes . . . . . . . . . . . . . . . . . . . . 83 10.3 RecursiveDatatypes . . . . . . . . . . . . . . . . . . . . . . . 85 10.4 HeterogeneousDataStructures . . . . . . . . . . . . . . . . . 88 10.5 AbstractSyntax . . . . . . . . . . . . . . . . . . . . . . . . . . 89 10.6 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 11 Higher-OrderFunctions 92 11.1 FunctionsasValues . . . . . . . . . . . . . . . . . . . . . . . . 92 11.2 BindingandScope . . . . . . . . . . . . . . . . . . . . . . . . 93 11.3 ReturningFunctions . . . . . . . . . . . . . . . . . . . . . . . 95 11.4 PatternsofControl . . . . . . . . . . . . . . . . . . . . . . . . 97 11.5 Staging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 11.6 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 12 Exceptions 103 12.1 ExceptionsasErrors . . . . . . . . . . . . . . . . . . . . . . . 104 12.1.1 PrimitiveExceptions . . . . . . . . . . . . . . . . . . . 104 12.1.2 User-DefinedExceptions . . . . . . . . . . . . . . . . . 105 12.2 ExceptionHandlers . . . . . . . . . . . . . . . . . . . . . . . . 107 12.3 Value-CarryingExceptions . . . . . . . . . . . . . . . . . . . . 110 12.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 13 MutableStorage 113 13.1 ReferenceCells . . . . . . . . . . . . . . . . . . . . . . . . . . 113 13.2 ReferencePatterns . . . . . . . . . . . . . . . . . . . . . . . . 115 13.3 Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 13.4 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 13.5 ProgrammingWellWithReferences . . . . . . . . . . . . . . 119 13.5.1 PrivateStorage . . . . . . . . . . . . . . . . . . . . . . 120 13.5.2 MutableDataStructures . . . . . . . . . . . . . . . . . 122 13.6 MutableArrays . . . . . . . . . . . . . . . . . . . . . . . . . . 124 REVISED 11.02.11 DRAFT VERSION 1.2 CONTENTS viii 13.7 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 14 Input/Output 127 14.1 TextualInput/Output . . . . . . . . . . . . . . . . . . . . . . 127 14.2 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 15 LazyDataStructures 130 15.1 LazyDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 132 15.2 LazyFunctionDefinitions . . . . . . . . . . . . . . . . . . . . 133 15.3 ProgrammingwithStreams . . . . . . . . . . . . . . . . . . . 135 15.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 16 EqualityandEqualityTypes 138 16.1 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 17 Concurrency 139 17.1 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 III The Module Language 140 18 SignaturesandStructures 142 18.1 Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 18.1.1 BasicSignatures . . . . . . . . . . . . . . . . . . . . . . 143 18.1.2 SignatureInheritance . . . . . . . . . . . . . . . . . . . 144 18.2 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 18.2.1 BasicStructures . . . . . . . . . . . . . . . . . . . . . . 147 18.2.2 LongandShortIdentifiers . . . . . . . . . . . . . . . . 148 18.3 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 19 SignatureMatching 151 19.1 PrincipalSignatures . . . . . . . . . . . . . . . . . . . . . . . . 152 19.2 Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 19.3 Satisfaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 19.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 20 SignatureAscription 158 20.1 AscribedStructureBindings . . . . . . . . . . . . . . . . . . . 158 20.2 OpaqueAscription . . . . . . . . . . . . . . . . . . . . . . . . 159 REVISED 11.02.11 DRAFT VERSION 1.2 CONTENTS ix 20.3 TransparentAscription . . . . . . . . . . . . . . . . . . . . . . 162 20.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 21 ModuleHierarchies 165 21.1 Substructures . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 21.2 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 22 SharingSpecifications 174 22.1 CombiningAbstractions . . . . . . . . . . . . . . . . . . . . . 174 22.2 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 23 Parameterization 182 23.1 FunctorBindingsandApplications . . . . . . . . . . . . . . . 182 23.2 FunctorsandSharingSpecifications . . . . . . . . . . . . . . 185 23.3 AvoidingSharingSpecifications . . . . . . . . . . . . . . . . . 187 23.4 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 IV Programming Techniques 192 24 SpecificationsandCorrectness 194 24.1 Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 24.2 CorrectnessProofs . . . . . . . . . . . . . . . . . . . . . . . . 196 24.3 EnforcementandCompliance . . . . . . . . . . . . . . . . . . 199 25 InductionandRecursion 202 25.1 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . 202 25.2 TheGCDAlgorithm . . . . . . . . . . . . . . . . . . . . . . . 207 25.3 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 26 StructuralInduction 212 26.1 NaturalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . 212 26.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 26.3 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 26.4 GeneralizationsandLimitations . . . . . . . . . . . . . . . . 216 26.5 AbstractingInduction . . . . . . . . . . . . . . . . . . . . . . 217 26.6 SampleCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 REVISED 11.02.11 DRAFT VERSION 1.2

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.