The Formal Semantics of Programming Languages: an Introduction, Glynn Winskel, 1993 the Formal Semantics of Programming Languages an Introduction
Total Page:16
File Type:pdf, Size:1020Kb
Foundations of Computing Michael Garey and Albert Meyer, editors Complexity Issues in VLSI: Optimal Layouts for the Shuffle-Exchange Graph and Other Networks, Frank Thomson Leighton, 1983 Equational Logic as a Programming Language, Michael J. 0 'Donnell, 1985 General Theory of Deductive Systems and Its Applications, S. Yu Maslov, 1987 Resource Allocation Problems: Algorithmic Approaches, Toshihide Ibaraki and Naoki Katoh, 1988 Algebraic Theory of Processes, Matthew Hennessy, 1988 PX: A Computational Logic, Susumu Hayashi and Hiroshi Nakano, 1989 The Stable Marriage Problem: Structure and Algorithms, Dan Gusfield and Robert Irving, 1989 Realistic Compiler Generation, Peter Lee, 1989 Single-Layer Wire Routing and Compaction,F. Miller Maley, 1990 Basic Category Theory for Computer Scientists, Benjamin C. Pierce, 1991 Categories, Types, and Structures: An Introduction to Category Theory for the Working Computer Scientist, Andrea Asperti and Giuseppe Longo, 1991 Semantics of Programming Languages: Structures and Techniques, Carl A. Gunter, 1992 The Formal Semantics of Programming Languages: An Introduction, Glynn Winskel, 1993 The Formal Semantics of Programming Languages An Introduction Glynn Winskel The MIT Press Cambridge, Massachusetts London, England Second printing, 1994 ©1993 Massachusetts Institute of Technology All rights reserved. No part ofthis book may be reproduced in any form by any electronic or mechanical means (including photocopying, recording, or information storage and retrieval) without permission in writing from the publisher. This book was printed and bound in the United States of America. Library of Congress Cataloging-in-Publication Data Winskel, G. (Glynn) The formal semantics of programming languages : an introduction Glynn Winskel. p. cm. - (Foundations of computing) Includes bibliographical references and index. ISBN 0-262-23169-7 1. Programming languages (Electronic computers )-Semantics. 1. Title. II. Series. QA76.7.W555 1993 005.13'1--dc20 92-36718 CIP To Kirsten, Sofie and Stine Contents Series foreword xiii Preface xv 1 Basic set theory 1 1.1 Logical notation 1 1.2 Sets 2 1.2.1 Sets and properties 3 1.2.2 Some important sets 3 1.2.3 Constructions on sets 4 1.2.4 The axiom of foundation 6 1.3 Relations and functions 6 1.3.1 Lambda notation 7 1.3.2 Composing relations and functions 7 1.3.3 Direct and inverse image of a relation 9 1.3.4 Equivalence relations 9 1.4 FUrther reading 10 2 Introduction to operational semantics 11 2.1 IMP-a simple imperative language 11 2.2 The evaluation of arithmetic expressions 13 2.3 The evaluation of boolean expressions 17 2.4 The execution of commands 19 2.5 A simple proof 20 2.6 Alternative semantics 24 2.7 FUrther reading 26 3 Some principles of induction 27 3.1 Mathematical induction 27 3.2 Structural induction 28 3.3 Well-founded induction 31 3.4 Induction on derivations 35 3.5 Definitions by induction 39 viii Contents 3.6 Further reading 40 4 Inductive definitions 41 4.1 Rule induction 41 4.2 Special rule induction 44 4.3 Proof rules for operational semantics 45 4.3.1 Rule induction for arithmetic expressions 45 4.3.2 Rule induction for boolean expressions 46 4.3.3 Rule induction for commands 47 4.4 Operators and their least fixed points 52 4.5 Further reading 54 5 The denotational semantics of IMP 55 5.1 Motivation 55 5.2 Denotational semantics 56 5.3 Equivalence of the semantics 61 5.4 Complete partial orders and continuous functions 68 5.5 The Knaster-Tarski Theorem 74 5.6 Further reading 75 6 The axiomatic semantics of IMP 77 6.1 The idea 77 6.2 The assertion language Assn 80 6.2.1 Free and bound variables 81 6.2.2 Substitution 82 6.3 Semantics of assertions 84 6.4 Proof rules for partial correctness 89 6.5 Soundness 91 6.6 Using the Hoare rules-an example 93 6.7 Further reading 96 7 Completeness of the Hoare rules 99 Contents ix 7.1 Codel's Incompleteness Theorem 99 7.2 Weakest preconditions and expressiveness 100 7.3 Proof of Codel's Theorem 110 7.4 Verification conditions 112 7.5 Predicate transformers 115 7.6 Further reading 117 8 Introduction to domain theory 119 8.1 Basic definitions 119 8.2 Streams-an example 121 8.3 Constructions on cpo's 123 8.3.1 Discrete cpo's 124 8.3.2 Finite products 125 8.3.3 Function space 128 8.3.4 Lifting 131 8.3.5 Sums 133 8.4 A metalanguage 135 8.5 Further reading 139 9 Recursion equations 141 9.1 The language REC 141 9.2 Operational semantics of call-by-value 143 9.3 Denotational semantics of call-by-value 144 9.4 Equivalence of semantics for call-by-value 149 9.5 Operational semantics of call-by-name 153 9.6 Denotational semantics of call-by-name 154 9.7 Equivalence of semantics for call-by-name 157 9.8 Local declarations 161 9.9 Further reading 162 10 Techniques for recursion 163 10.1 BekiC's Theorem 163 x Contents 10.2 Fixed-point induction 166 10.3 Well-founded induction 174 10.4 Well-founded recursion 176 10.5 An exercise 179 10.6 Further reading 181 11 Languages with higher types 183 11.1 An eager language 183 11.2 Eager operational semantics 186 11.3 Eager denotational semantics 188 11.4 Agreement of eager semantics 190 11.5 A lazy language 200 11.6 Lazy operational semantics 201 11.7 Lazy denotational semantics 203 11.8 Agreement of lazy semantics 204 11.9 Fixed-point operators 209 11.10 Observations and full abstraction 215 11.11 Sums 219 11.12 Further reading 221 12 Information systems 223 12.1 Recursive types 223 12.2 Information systems 225 12.3 Closed families and Scott predomains 228 12.4 A cpo of information systems 233 12.5 Constructions 236 12.5.1 Lifting 237 12.5.2 Sums 239 12.5.3 Product 241 12.5.4 Lifted function space 243 12.6 Further reading 249 Contents xi 13 Recursive types 251 13.1 An eager language 251 13.2 Eager operational semantics 255 13.3 Eager denotational semantics 257 13.4 Adequacy of eager semantics 262 13.5 The eager A-calculus 267 13.5.1 Equational theory 269 13.5.2 A fixed-point operator 272 13.6 A lazy language 278 13.7 Lazy operational semantics 278 13.8 Lazy denotational semantics 281 13.9 Adequacy of lazy semantics 288 13.10 The lazy A-calculus 290 13.10.1 Equational theory 291 13.10.2 A fixed-point operator 292 13.11 Further reading 295 14 N ondeterminism and parallelism 297 14.1 Introduction 297 14.2 Guarded commands 298 14.3 Communicating processes 303 14.4 Milner's CCS 308 14.5 Pure CCS 311 14.6 A specification language 316 14.7 The modal v-calculus 321 14.8 Local model checking 327 14.9 Further reading 335 A Incompleteness and undecidability 337 Bibliography 353 Index 357 Series foreword Theoretical computer science has now undergone several decades of development. The "classical" topics of automata theory, formal languages, and computational complexity have become firmly established, and their importance to other theoretical work and to practice is widely recognized. Stimulated by technological advances, theoreticians have been rapidly expanding the areas under study, and the time delay between theoreti cal progress and its practical impact has been decreasing dramatically. Much publicity has been given recently to breakthroughs in cryptography and linear programming, and steady progress is being made on programming language semantics, computational ge ometry, and efficient data structures. Newer, more speculative, areas of study include relational databases, VLSI theory, and parallel and distributed computation. As this list of topics continues expanding, it is becoming more and more difficult to stay abreast of the progress that is being made and increasingly important that the most significant work be distilled and communicated in a manner that will facilitate further research and application of this work. By publishing comprehensive books and specialized monographs on the theoretical aspects of computer science, the series on Foundations of Computing provides a forum in which important research topics can be presented in their entirety and placed in perspective for researchers, students, and practitioners alike. Michael R. Garey Albert R. Meyer Preface In giving a formal semantics to a programming language we are concerned with building a mathematical model. Its purpose is to serve as a basis for understanding and reasoning about how programs behave. Not only is a mathematical model useful for various kinds of analysis and verification, but also, at a more fundamental level, because simply the activity of trying to define the meaning of program constructions precisely can reveal all kinds of subtleties of which it is important to be aware. This book introduces the mathematics, techniques and concepts on which formal semantics rests. For historical reasons the semantics of programming languages is often viewed as con sisting of three strands: Operational semantics describes the meaning of a programming language by spec ifying how it executes on an abstract machine. We concentrate on the method advocated by Gordon Plotkin in his lectures at Aarhus on "structural operational semantics" in which evaluation and execution relations are specified by rules in a way directed by the syntax. Denotational semantics is a technique for defining the meaning of programming languages pioneered by Christopher Strachey and provided with a mathematical foundation by Dana Scott. At one time called "mathematical semantics," it uses the more abstract mathematical concepts of complete partial orders, continuous functions and least fixed points.