ProgrammingLanguages Computer scientists often need to learn new programming languages quickly. The best way to prepareistounderstandthefoundationalprinciplesthatunderlieeventhemostcomplicatedindustrial languages. This text for an undergraduate programming-languages course distills great languages and their designprinciplesdowntoeasy-to-learn“bridge”languagesimplementedbyinterpreterswhosekey partsareexplainedinthetext.Thebookgoesdeepintotherootsofbothfunctionalandobject-oriented programming,anditshowshowtypesandmodules,includinggenerics/polymorphism,contributeto effectiveprogramming. The book is not just about programming languages; it is also about programming. Through concepts,examples,andmorethan300practiceexercisesthatexploittheinterpreters,studentslearn not only what programming-language features are common but also how to do things with them. Substantial implementation projects include Milner’s type inference, both copying and mark-and- sweepgarbagecollection,andarithmeticonarbitrary-precisionintegers. NormanRamseyisAssociateProfessorofComputerScienceatTuftsUniversity.Sinceearninghis PhD at Princeton, he has worked in industry and has taught programming languages, advanced functional programming, programming language implementation, and technical writing at Purdue, theUniversityofVirginia,andHarvardaswellasTufts.HehasreceivedTufts’sLerman-Neubauer Prize,awardedannuallytooneoutstandingundergraduateteacher.HehasalsobeenaHertzFellow andanAlfredP.SloanResearchFellow.Hisimplementationcreditsincludeacodegeneratorforthe StandardMLofNewJerseycompilerandanotherfortheGlasgowHaskellCompiler. Programming Languages Build, Prove, and Compare Norman Ramsey TuftsUniversity,Massachusetts UniversityPrintingHouse,CambridgeCB28BS,UnitedKingdom OneLibertyPlaza,20thFloor,NewYork,NY10006,USA 477WilliamstownRoad,PortMelbourne,VIC3207,Australia 314–321,3rdFloor,Plot3,SplendorForum,JasolaDistrictCentre,NewDelhi–110025,India 103PenangRoad,#05–06/07,VisioncrestCommercial,Singapore238467 CambridgeUniversityPressispartoftheUniversityofCambridge. ItfurtherstheUniversity’smissionbydisseminatingknowledgeinthepursuitof education,learning,andresearchatthehighestinternationallevelsofexcellence. www.cambridge.org Informationonthistitle:www.cambridge.org/highereducation/isbn/9781107180185 DOI:10.1017/9781316841396 ©NormanRamsey2023 Thispublicationisincopyright.Subjecttostatutoryexception andtotheprovisionsofrelevantcollectivelicensingagreements, noreproductionofanypartmaytakeplacewithoutthewritten permissionofCambridgeUniversityPress. Firstpublished2023 AcataloguerecordforthispublicationisavailablefromtheBritishLibrary. ISBN978-1-107-18018-5Hardback Additionalresourcesforthispublicationatwww.cambridge.org/ramsey CambridgeUniversityPresshasnoresponsibilityforthepersistenceoraccuracyof URLsforexternalorthird-partyinternetwebsitesreferredtointhispublication anddoesnotguaranteethatanycontentonsuchwebsitesis,orwillremain, accurateorappropriate. To Cory, who also knows joy in creation CONTENTſ PREFACE ix ACĸNOWLEDGMENTſ xv CREDıTſ xvii TABLEſOFȷUDGMENTFORMſANDıMPORTANTFUNCTıONſ xviii SYMBOLſANDNOTATıON xx INTRODUCTıON 1 PART I. FOUNDATıONſ 1 ANıMPERATıVECORE 11 1.1 Lookingatlanguages 13 1.6 Theinterpreter 38 1.2 TheImpcorelanguage 17 1.7 Operationalsemantics 55 1.3 Abstractsyntax 27 revisited:Proofs 1.4 Environmentsandthe 28 1.8 ExtendingImpcore 66 meaningsofnames 1.9 Summary 67 1.5 Operationalsemantics 29 1.10 Exercises 72 2 SCHEME,S‐EXPREſſıONſ,ANDFıRſT‐CLAſſFUNCTıONſ 89 2.1 OverviewofµScheme… 90 2.8 PracticeIII:Higher‐order 127 2.2 LanguageI:Values,syntax, 91 functionsonlists andinitialbasis 2.9 PracticeIV:Higher‐order 131 2.3 PracticeI:Recursivefunctions 98 functionsforpolymorphism onlistsofvalues 2.10 PracticeV: 136 2.4 Recordsandtrees(moredata) 107 Continuation‐passingstyle 2.5 Combiningtheoryandpractice: 110 2.11 Operationalsemantics 144 Algebraiclaws 2.12 Theinterpreter 152 2.6 LanguageII:Localvariables 117 2.13 ExtendingµSchemewith 162 andlet syntacticsugar 2.7 LanguageIII:First‐class 120 2.14 Schemeasitreallyis 168 functions,lambda, 2.15 Summary 172 andlocations 2.16 Exercises 176 3 CONTROLOPERATORſANDAſMALL‐ſTEPſEMANTıCſ:µSCHEME+ 201 3.1 TheµScheme+language 202 3.5 AsemanticsofCoreµScheme+ 215 3.2 Proceduralprogramming 205 3.6 Theinterpreter 223 withcontroloperators 3.7 Stacks,control,andsemantics 239 3.3 Operationalsemantics: 210 astheyreallyare Evaluationusingastack 3.8 Summary 243 3.4 Operationalsemantics: 213 3.9 Exercises 248 Loweringtoacorelanguage 4 AUTOMATıCMEMORYMANAGEMENT 257 4.1 Whatgarbageisand 258 4.6 Debuggingacollector 280 whereitcomesfrom 4.7 Mark‐compactcollection 283 4.2 Garbage‐collectionbasics 259 4.8 Referencecounting 283 4.3 Themanagedheapin 263 4.9 Garbagecollection 285 µScheme+ asitreallyis 4.4 Mark‐and‐sweepcollection 266 4.10 Summary 287 4.5 Copyingcollection 271 4.11 Exercises 292 5 INTERLUDE:µSCHEMEıNML 301 5.1 Namesandenvironments… 303 5.5 Notabledifferences 314 5.2 Abstractsyntaxandvalues 306 betweenMLandC 5.3 Evaluation 309 5.6 Freeandboundvariables… 315 5.4 Definingandembedding 312 5.7 Summary 317 primitives 5.8 Exercises 320 6 TYPEſYſTEMſFORIMPCOREANDµSCHEME 327 6.1 TypedImpcore:Astatically 329 6.4 Commontypeconstructors 348 typedimperativecore 6.5 Typesoundness 350 6.2 Atype‐checkinginterpreter 337 6.6 Polymorphictypesystems 351 forTypedImpcore andTypedµScheme 6.3 ExtendingTypedImpcore 343 6.7 Typesystemsastheyreallyare 383 witharrays 6.8 Summary 383 6.9 Exercises 386 7 MLANDTYPEıNFERENCE 401 Contents 7.1 Nano‐ML:Anearly 402 7.5 Fromtypingrules 417 applicativelanguage totypeinference vii 7.2 Abstractsyntaxandvalues 404 7.6 Theinterpreter 433 ofnano‐ML 7.7 Hindley‐Milnerasitreallyis 441 7.3 Operationalsemantics 405 7.8 Summary 441 7.4 Typesystemfornano‐ML 407 7.9 Exercises 443 PART II. PROGRAMMıNG AT ſCALE 8 UſER‐DEFıNED,ALGEBRAıCTYPEſ(ANDPATTERNMATCHıNG) 457 8.1 Caseexpressionsand 459 8.6 Abstractsyntaxandvalues… 485 patternmatching 8.7 Theoryandimplementation 486 8.2 AlgebraicdatatypesinµML 466 ofuser‐definedtypes 8.3 Equationalreasoning 476 8.8 Theoryandimplementation 490 withcaseexpressions ofcaseexpressions 8.4 Syntacticsugar: 480 8.9 Algebraicdatatypes 499 Patternseverywhere astheyreallyare 8.5 Typegenerativityand 483 8.10 Summary 501 typeequivalence 8.11 Exercises 503 9 MOLECULE,ABſTRACTDATATYPEſ,ANDMODULEſ 525 9.1 Thevocabulary 527 9.7 Keyfeature:Inspecting 555 ofdataabstraction multiplerepresentations 9.2 IntroductiontoMolecule, 528 9.8 Molecule’stypesystem: 558 partI:Writingclientcode Enforcingabstraction 9.3 Introduction,partII: 530 9.9 Notesontheinterpreter 579 Implementinganabstraction 9.10 Abstractdatatypes,modules, 580 9.4 TheMoleculelanguage 534 andoverloading 9.5 Molecule’sinitialbasis 544 astheyreallyare 9.6 Programdesign:Abstractions 545 9.11 Summary 585 9.12 Exercises 589 10 SMALLTALĸANDOBȷECT‐ORıENTATıON 609 10.1 Object‐orientedprogramming 610 10.8 TechniqueIII:Multiple 662 byexample representations… 10.2 Dataabstractionalloveragain 625 10.9 TechniqueIV:Invariantsin 673 10.3 TheµSmalltalklanguage 627 object‐orientedprogramming 10.4 TheinitialbasisofµSmalltalk 636 10.10 Operationalsemantics 677 10.5 Object‐orientedprogramming 654 10.11 Theinterpreter 685 techniques 10.12 Smalltalkasitreallyis 700 10.6 TechniqueI:Methoddispatch 654 10.13 Objectsandclasses 707 replacesconditionals astheyreallyare 10.7 TechniqueII:Abstractclasses 656 10.14 Summary 707 10.15 Exercises 713 AFTERWORD 727 BıBLıOGRAPHY 733 KEYWORDſANDPHRAſEſ 745 CONCEPTıNDEX 749