Undergraduate Topics in Computer Science Maribel Fernández Programming Languages and Operational Semantics A Concise Overview Undergraduate Topics in Computer Science Undergraduate Topics in Computer Science (UTiCS) delivers high-quality instructional content for undergraduatesstudyinginallareasofcomputingandinformationscience.Fromcorefoundationaland theoreticalmaterialtofinal-yeartopicsandapplications,UTiCSbookstakeafresh,concise,andmod- ernapproachandareidealforself-studyorforaone-ortwo-semestercourse.Thetextsareallauthored byestablishedexpertsintheirfields,reviewedbyaninternationaladvisoryboard,andcontainnumer- ousexamplesandproblems.Manyincludefullyworkedsolutions. For furthervolumes: http://www.springer.com/series/7592 Maribel Fernández Programming Languages and Operational Semantics A Concise Overview 123 Prof.Maribel Fernández King’s CollegeLondon London UK Series editor IanMackie Advisory Board SamsonAbramsky, UniversityofOxford, Oxford, UK KarinBreitman, Pontifical CatholicUniversityof Riode Janeiro, Rio deJaneiro, Brazil ChrisHankin, ImperialCollege London, London,UK DexterKozen, Cornell University, Ithaca, USA Andrew Pitts, Universityof Cambridge,Cambridge,UK HanneRiisNielson, Technical University ofDenmark, KongensLyngby, Denmark StevenSkiena, Stony BrookUniversity, StonyBrook, USA Iain Stewart,University ofDurham,Durham,UK ISSN 1863-7310 ISSN 2197-1781 (electronic) ISBN 978-1-4471-6367-1 ISBN 978-1-4471-6368-8 (eBook) DOI 10.1007/978-1-4471-6368-8 Springer LondonHeidelberg New YorkDordrecht LibraryofCongressControlNumber:2014932000 !Springer-VerlagLondon2014 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartof the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionor informationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purposeofbeingenteredandexecutedonacomputersystem,forexclusiveusebythepurchaserofthe work. Duplication of this publication or parts thereof is permitted only under the provisions of theCopyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the CopyrightClearanceCenter.ViolationsareliabletoprosecutionundertherespectiveCopyrightLaw. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publicationdoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexempt fromtherelevantprotectivelawsandregulationsandthereforefreeforgeneraluse. While the advice and information in this book are believed to be true and accurate at the date of publication,neithertheauthorsnortheeditorsnorthepublishercanacceptanylegalresponsibilityfor anyerrorsoromissionsthatmaybemade.Thepublishermakesnowarranty,expressorimplied,with respecttothematerialcontainedherein. Printedonacid-freepaper SpringerispartofSpringerScience+BusinessMedia(www.springer.com) Preface Aim This book is addressed to undergraduate students as a complement to program- ming courses. The general aim of the book is to present and compare alternative programming language paradigms, and to provide the foundations and tools needed to design new programming languages and understand the existing ones. These are ambitious goals, and to achieve our aims, we have chosen to introduce andexplaininaconcisewaythemostsignificantconstructsofmodernimperative, functional and logic programming languages, and describe their operational semantics first in an informal way, and then using transition systems. About this Book This book provides a concise introduction to the essential concepts in program- minglanguages,usingoperationalsemanticstechniques.Thebookisforthemost part self-contained, but we assume readers have some basic programming skills and have used at least one programming language. Knowledge of more pro- gramming languages will be helpful but is not necessary. Each chapter includes exercises that provide an opportunity to apply the con- cepts and techniques presented. Doing exercises is an important part of the learningprocess:tounderstandhowaprogramminglanguageworksandbeableto use it, it is not sufficient to read about it. Of course, the same can be said about many other topics (learning to play a musical instrument or learning to ride a bicycle are familiar examples). Since practice is unavoidable, we have included not just exercises to be done on paper, but also some programming exercises that require checking on a computer. Although some of the exercises are just intro- ductory, most of the questions are designed with the goal of testing the under- standing of the subject (for instance, by requiring students to adapt a given technique to different contexts). Some challenging exercises for more advanced students are also included; these are marked with . Answers to selected ðyÞ exercises are given in the final chapter of the book. v vi Preface The book is organised as follows: Part I provides an introduction to programming languages and operational semantics in Chap. 1, followed by basic mathematical background for the rest of the book in Chap. 2. InPartIIwestudythemaincomponentsofimperativelanguages.Wefirstgive aninformaldescriptioninChap.3,andthengiveaformalspecificationofthemain constructs in imperative languages using transition systems in Chap. 4. Part III is dedicated tofunctional languages and their type systems. InChap. 5 wegiveanoverviewoffunctionalprogramming(usingHaskellforourexamples), and then give some examples of semantic descriptions offunctional languages in Chap. 6. Finally, in Part IV (Chaps. 7 and 8) we briefly study logic programming lan- guages and describe the operational semantics of Prolog. The last chapter of the book (Chap. 9) contains answers to a selection of exercises. We have not included chapters on object-oriented languages, but we do study aspects of these languages in Chaps. 1 and 3. Eachchapterincludesalistofrecommendedfurtherreading,alistofexercises, and abibliographicalsection with references torelevant articles andbookswhere the interested reader can find more information. In particular, for each program- minglanguagethereisareferencetoitsdefinition,whichisindicatedthefirsttime the language is mentioned. Acknowledgments The material presented in this book has been prepared using several different sources, including the references mentioned above and lecture notes for my coursesatKing’sCollegeLondonandinFrance.Iwouldliketothanktheeditors of the UTiCS series for inviting me to write this book. I am very grateful to the students in the Department of Informatics at King’s and the teaching assistants in my courses (in particular, Asad Ali, Christophe Calvès, Jesús Domínguez, Elliot Fairweather, Fabien Fleutot, François-Régis Sinot and Jeffrey Terrell) for their comments on previous versions of the book. Special thanks to Elliot Fairweather and Jeff Terrell for their careful reading and helpful suggestions. Last, but not least, I thank my family for their continuous support. London, January 2014 Maribel Fernández Contents Part I Preliminaries 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 Programming Languages and Software Engineering. . . . . . . . . 4 1.2 Programming Paradigms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Components of a Programming Language. . . . . . . . . . . . . . . . 5 1.3.1 Computer Architecture. . . . . . . . . . . . . . . . . . . . . . . . 6 1.3.2 Language Implementation. . . . . . . . . . . . . . . . . . . . . . 7 1.3.3 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.3.4 Semantics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.4 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 Mathematical Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.1 Sets and Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.2 Logic Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3 Transition Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.4 Induction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.5 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Part II Imperative Languages 3 General Features of Imperative Languages. . . . . . . . . . . . . . . . . . 45 3.1 Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.2 Assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.3 Control Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.3.1 Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.3.2 Iterative Statements (Loops) . . . . . . . . . . . . . . . . . . . . 53 vii viii Contents 3.4 Branching Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3.5 Structuring the Program: Blocks and Subprograms. . . . . . . . . . 56 3.6 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.7 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4 Operational Semantics of Imperative Languages. . . . . . . . . . . . . . 61 4.1 Abstract Syntax of SIMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.2 An Abstract Machine for SIMP. . . . . . . . . . . . . . . . . . . . . . . 65 4.3 Structural Operational Semantics for SIMP. . . . . . . . . . . . . . . 74 4.3.1 Reduction Semantics: Small-Step Semantics. . . . . . . . . 75 4.3.2 Evaluation Semantics: Big-Step Semantics . . . . . . . . . . 81 4.3.3 Equivalence between Small- and Big-Step Semantics. . . 83 4.4 Adding Blocks and Local Variables to SIMP . . . . . . . . . . . . . 85 4.5 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Part III Functional Languages 5 General Features of Functional Languages. . . . . . . . . . . . . . . . . . 93 5.1 Defining Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.2 Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.3 Building Functional Programs. . . . . . . . . . . . . . . . . . . . . . . . 100 5.3.1 Local Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.4 Types: General Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 5.5 Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 5.6 Type Checking and Type Inference . . . . . . . . . . . . . . . . . . . . 107 5.7 Data Structures and Data Type Definitions. . . . . . . . . . . . . . . 110 5.8 Reasoning About Programs. . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.9 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.10 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 6 Operational Semantics of Functional Languages. . . . . . . . . . . . . . 121 6.1 Abstract Syntax of SFUN. . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.2 A Type System for SFUN . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.3 Operational Semantics of SFUN . . . . . . . . . . . . . . . . . . . . . . 129 6.3.1 Call-by-Value Semantics of SFUN . . . . . . . . . . . . . . . 130 6.3.2 Properties of SFUN. . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.3.3 Call-by-Name Semantics of SFUN . . . . . . . . . . . . . . . 134 Contents ix 6.4 Extensions of SFUN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 6.4.1 Abstract Syntax of FUN. . . . . . . . . . . . . . . . . . . . . . . 137 6.4.2 A Type System for FUN . . . . . . . . . . . . . . . . . . . . . . 139 6.4.3 Operational Semantics of FUN . . . . . . . . . . . . . . . . . . 143 6.4.4 Properties of FUN. . . . . . . . . . . . . . . . . . . . . . . . . . . 147 6.5 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 6.6 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Part IV Logic Languages 7 General Features of Logic Programming Languages. . . . . . . . . . . 155 7.1 The Herbrand Universe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 7.2 Logic Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 7.3 Finding Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7.4 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 7.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 8 Operational Semantics of Prolog . . . . . . . . . . . . . . . . . . . . . . . . . 167 8.1 Unification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 8.2 The Principle of Resolution. . . . . . . . . . . . . . . . . . . . . . . . . . 169 8.2.1 SLD-Resolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 8.3 Prolog’s Search in the SLD-tree . . . . . . . . . . . . . . . . . . . . . . 173 8.4 Further Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 8.5 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 9 Answers to Selected Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207