Series Editor
Jean-Charles Pomerol
Concepts and Semantics of Programming Languages 2
Modular and Object-oriented Constructs with OCaml, Python, C++, Ada and Java
Thrse Hardin
Mathieu Jaume
Franois Pessaux
Vronique Vigui Donzeau-Gouge
First published 2021 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address:
ISTE Ltd
27-37 St Georges Road
London SW19 4EU
UK
www.iste.co.uk
John Wiley & Sons, Inc.
111 River Street
Hoboken, NJ 07030
USA
www.wiley.com
ISTE Ltd 2021
The rights of Thrse Hardin, Mathieu Jaume, Franois Pessaux and Vronique Vigui Donzeau-Gouge to be identified as the authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988.
Library of Congress Control Number: 2021935827
British Library Cataloguing-in-Publication Data
A CIP record for this book is available from the British Library
ISBN 978-1-78630-602-9
Foreword
Computer programs have played an increasingly central role in our lives since the 1940s, and the quality of these programs has thus become a crucial question. Writing a high-quality program a program that performs the required task and is efficient, robust, easy to modify, easy to extend, etc. is an intellectually challenging task, requiring the use of rigorous development methods. First and foremost, however, the creation of such a program is dependent on an in-depth knowledge of the programming language used, its syntax and, crucially, its semantics, i.e. what happens when a program is executed.
The description of this semantics puts the most fundamental concepts into light, including those of value, reference, exception or object. These concepts are the foundations of programming language theory. Mastering these concepts is what sets experienced programmers apart from beginners. Certain concepts like that of value are common to all programming languages; others such as the notion of functions operate differently in different languages; finally, other concepts such as that of objects only exist in certain languages. Computer scientists often refer to programming paradigms to consider sets of concepts shared by a family of languages, which imply a certain programming style: imperative, functional, object-oriented, logical, concurrent, etc. Nevertheless, an understanding of the concepts themselves is essential, as several paradigms may be interwoven within the same language.
Introductory texts on programming in any given language are not difficult to find, and a number of published books address the fundamental concepts of language semantics. Much rarer are those, like the present volume, which establish and examine the links between concepts and their implementation in languages used by programmers on a daily basis, such as C, C++, Ada, Java, OCaml and Python. The authors provide a wealth of examples in these languages, illustrating and giving life to the notions that they present. They propose general models, such as the kit presented in Volume 2, permitting a unified view of different notions; this makes it easier for readers to understand the constructs used in popular programming languages and facilitates comparison. This thorough and detailed work provides readers with an understanding of these notions and, above all, an understanding of the ways of using the latter to create high-quality programs, building a safer and more reliable future in computing.
Gilles DOWEK
Research Director, Inria
Professor at the cole normale suprieure, Paris-Saclay
Catherine DUBOIS
Professor at the cole nationale suprieure dinformatique pour lindustrie et lentreprise
January 2021
Preface
This two-volume work relates to the field of programming. First and foremost, it is intended to give readers a solid grounding in the bases of functional or imperative programming, along with a thorough knowledge of the module and class mechanisms involved. In our view, the semantics approach is most appropriate when studying programming, as the impact of interlanguage syntax differences is limited. Practical considerations, determined by the material characteristics of computers and/or smart devices, will also be addressed. The same approach will be taken in both volumes, using both mathematical formulas and memory state diagrams. With this book, we hope to help readers understand the meaning of the constructs described in the reference manuals of programming languages and to establish solid foundations for reasoning and assessing the correctness of their own programs through critical review. In short, our aim is to facilitate the development of safe and reliable software programs.
Volume 1 presented a broad overview of the functional and imperative features of programming, from notions that can be modeled mathematically to notions that are linked to the hardware configuration of computers themselves.
Volume 2 is dedicated to the study of language features (modules, classes, objects) that are known to ease the development of software systems. It builds on the foundations laid down in Volume 1 since modules, classes and objects are, in essence, the means of organizing functional or imperative constructs.
, which presents the classes of Java, C++, OCaml and Python from a unified perspective.
This work is aimed at a relatively wide audience, from experienced developers who will find valuable additional information on language semantics to beginners who have only written short programs. For beginners, we recommend working on the semantic concepts described in Volume 1 using the implementations in OCaml or Python to ease assimilation. All readers may benefit from studying the reference manual of a programming language, while comparing the presentations of constructs given in the manual with those given here, guided by the questions mentioned in Volume 2.
Note that we do not discuss the algorithmic aspect of data processing here. However, choosing the algorithm and the data representation that fit the requirements of the specification is an essential step in program development. Many excellent works have been published on this subject, and we encourage readers to explore the subject further. We also recommend using the standard libraries provided by the chosen programming language. These libraries include tried and tested implementations for many different algorithms, which may generally be assumed to be correct.
Namespaces: Model and Operations
Most programming languages currently in use offer tools to facilitate the construction and maintenance of large software. Two approaches coexist, that of languages with modules and that of object-oriented languages; approaches that are different or even historically opposed but which in fact share many characteristics. This is why this chapter elaborates on a unique model of these two approaches by introducing a structure, referred to here as
Next page