Copcepts, Techniques and Models of Computer Programming

Concepts, Techniques, and Models
of Computer Programming
PETER VAN ROY1
Université catholique de Louvain (at Louvain-la-Neuve)
Swedish Institute of Computer Science
SEIF HARIDI2
Royal Institute of Technology (KTH)
Swedish Institute of Computer Science
June 5, 2003

1 Email:
2 Email:

pvr@info.ucl.ac.be, Web: http://www.info.ucl.ac.be/~pvr
seif@it.kth.se, Web: http://www.it.kth.se/~seif

ii

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright


Contents
List of Figures

xvi

List of Tables

xxiv

Preface

xxvii

Running the example programs

I

Introduction

1 Introduction to Programming

1.1 A calculator . . . . . . . . .
1.2 Variables . . . . . . . . . . .
1.3 Functions . . . . . . . . . .
1.4 Lists . . . . . . . . . . . . .
1.5 Functions over lists . . . . .
1.6 Correctness . . . . . . . . .
1.7 Complexity . . . . . . . . .
1.8 Lazy evaluation . . . . . . .
1.9 Higher-order programming .
1.10 Concurrency . . . . . . . . .
1.11 Dataflow . . . . . . . . . . .
1.12 State . . . . . . . . . . . . .
1.13 Objects . . . . . . . . . . .
1.14 Classes . . . . . . . . . . . .
1.15 Nondeterminism and time .
1.16 Atomicity . . . . . . . . . .
1.17 Where do we go from here .
1.18 Exercises . . . . . . . . . . .

II


xliii

1
Concepts
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

. . . . . . .
. . . . . . .
. . . . . . .

General Computation Models

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

3
3
4
4
6
9
11
12
13
15
16
17
18
19
20
21
23
24
24

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

29

2 Declarative Computation Model
c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

31

iv

CONTENTS
2.1 Defining practical programming languages . . . . . .
2.1.1 Language syntax . . . . . . . . . . . . . . . .
2.1.2 Language semantics . . . . . . . . . . . . . . .
2.2 The single-assignment store . . . . . . . . . . . . . .
2.2.1 Declarative variables . . . . . . . . . . . . . .
2.2.2 Value store . . . . . . . . . . . . . . . . . . .
2.2.3 Value creation . . . . . . . . . . . . . . . . . .
2.2.4 Variable identifiers . . . . . . . . . . . . . . .
2.2.5 Value creation with identifiers . . . . . . . . .
2.2.6 Partial values . . . . . . . . . . . . . . . . . .
2.2.7 Variable-variable binding . . . . . . . . . . . .
2.2.8 Dataflow variables . . . . . . . . . . . . . . .
2.3 Kernel language . . . . . . . . . . . . . . . . . . . . .
2.3.1 Syntax . . . . . . . . . . . . . . . . . . . . . .
2.3.2 Values and types . . . . . . . . . . . . . . . .
2.3.3 Basic types . . . . . . . . . . . . . . . . . . .
2.3.4 Records and procedures . . . . . . . . . . . .
2.3.5 Basic operations . . . . . . . . . . . . . . . .
2.4 Kernel language semantics . . . . . . . . . . . . . . .
2.4.1 Basic concepts . . . . . . . . . . . . . . . . . .
2.4.2 The abstract machine . . . . . . . . . . . . . .
2.4.3 Non-suspendable statements . . . . . . . . . .
2.4.4 Suspendable statements . . . . . . . . . . . .
2.4.5 Basic concepts revisited . . . . . . . . . . . .
2.4.6 Last call optimization . . . . . . . . . . . . .
2.4.7 Active memory and memory management . .
2.5 From kernel language to practical language . . . . . .
2.5.1 Syntactic conveniences . . . . . . . . . . . . .
2.5.2 Functions (the fun statement) . . . . . . . . .
2.5.3 Interactive interface (the declare statement)
2.6 Exceptions . . . . . . . . . . . . . . . . . . . . . . . .
2.6.1 Motivation and basic concepts . . . . . . . . .
2.6.2 The declarative model with exceptions . . . .
2.6.3 Full syntax . . . . . . . . . . . . . . . . . . .
2.6.4 System exceptions . . . . . . . . . . . . . . .
2.7 Advanced topics . . . . . . . . . . . . . . . . . . . . .
2.7.1 Functional programming languages . . . . . .
2.7.2 Unification and entailment . . . . . . . . . . .
2.7.3 Dynamic and static typing . . . . . . . . . . .
2.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .
c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 33
. 33
. 38
. 44
. 44
. 44
. 45
. 46
. 47
. 47
. 48
. 49
. 50
. 50
. 51
. 53
. 54
. 56
. 57
. 57
. 61
. 64
. 67
. 69
. 74
. 75
. 80
. 80
. 85
. 88
. 91
. 91
. 93
. 95
. 97
. 98
. 98
. 100
. 106
. 108

CONTENTS

v

3 Declarative Programming Techniques
3.1 What is declarativeness? . . . . . . . . . . . . . . . . . . .
3.1.1 A classification of declarative programming . . . . .
3.1.2 Specification languages . . . . . . . . . . . . . . . .
3.1.3 Implementing components in the declarative model
3.2 Iterative computation . . . . . . . . . . . . . . . . . . . . .
3.2.1 A general schema . . . . . . . . . . . . . . . . . . .
3.2.2 Iteration with numbers . . . . . . . . . . . . . . . .
3.2.3 Using local procedures . . . . . . . . . . . . . . . .
3.2.4 From general schema to control abstraction . . . .
3.3 Recursive computation . . . . . . . . . . . . . . . . . . . .
3.3.1 Growing stack size . . . . . . . . . . . . . . . . . .
3.3.2 Substitution-based abstract machine . . . . . . . .
3.3.3 Converting a recursive to an iterative computation
3.4 Programming with recursion . . . . . . . . . . . . . . . . .
3.4.1 Type notation . . . . . . . . . . . . . . . . . . . . .
3.4.2 Programming with lists . . . . . . . . . . . . . . . .
3.4.3 Accumulators . . . . . . . . . . . . . . . . . . . . .
3.4.4 Difference lists . . . . . . . . . . . . . . . . . . . .
3.4.5 Queues . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.6 Trees . . . . . . . . . . . . . . . . . . . . . . . . . .
3.4.7 Drawing trees . . . . . . . . . . . . . . . . . . . . .
3.4.8 Parsing . . . . . . . . . . . . . . . . . . . . . . . .
3.5 Time and space efficiency . . . . . . . . . . . . . . . . . .
3.5.1 Execution time . . . . . . . . . . . . . . . . . . . .
3.5.2 Memory usage . . . . . . . . . . . . . . . . . . . . .
3.5.3 Amortized complexity . . . . . . . . . . . . . . . .
3.5.4 Reflections on performance . . . . . . . . . . . . . .
3.6 Higher-order programming . . . . . . . . . . . . . . . . . .
3.6.1 Basic operations . . . . . . . . . . . . . . . . . . .
3.6.2 Loop abstractions . . . . . . . . . . . . . . . . . . .
3.6.3 Linguistic support for loops . . . . . . . . . . . . .
3.6.4 Data-driven techniques . . . . . . . . . . . . . . . .
3.6.5 Explicit lazy evaluation . . . . . . . . . . . . . . . .
3.6.6 Currying . . . . . . . . . . . . . . . . . . . . . . . .
3.7 Abstract data types . . . . . . . . . . . . . . . . . . . . . .
3.7.1 A declarative stack . . . . . . . . . . . . . . . . . .
3.7.2 A declarative dictionary . . . . . . . . . . . . . . .
3.7.3 A word frequency application . . . . . . . . . . . .
3.7.4 Secure abstract data types . . . . . . . . . . . . . .
3.7.5 The declarative model with secure types . . . . . .
3.7.6 A secure declarative dictionary . . . . . . . . . . .
3.7.7 Capabilities and security . . . . . . . . . . . . . . .
3.8 Nondeclarative needs . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

113
117
117
119
119
120
120
122
122
125
126
127
128
129
130
131
132
142
144
149
153
161
163
169
169
175
177
178
180
180
186
190
193
196
196
197
198
199
201
204
205
210
210
213

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

vi

CONTENTS
3.8.1 Text input/output with a file . . . . . . . . . . .
3.8.2 Text input/output with a graphical user interface
3.8.3 Stateless data I/O with files . . . . . . . . . . . .
3.9 Program design in the small . . . . . . . . . . . . . . . .
3.9.1 Design methodology . . . . . . . . . . . . . . . .
3.9.2 Example of program design . . . . . . . . . . . .
3.9.3 Software components . . . . . . . . . . . . . . . .
3.9.4 Example of a standalone program . . . . . . . . .
3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Declarative Concurrency
4.1 The data-driven concurrent model . . . . . . . . . . .
4.1.1 Basic concepts . . . . . . . . . . . . . . . . . .
4.1.2 Semantics of threads . . . . . . . . . . . . . .
4.1.3 Example execution . . . . . . . . . . . . . . .
4.1.4 What is declarative concurrency? . . . . . . .
4.2 Basic thread programming techniques . . . . . . . . .
4.2.1 Creating threads . . . . . . . . . . . . . . . .
4.2.2 Threads and the browser . . . . . . . . . . . .
4.2.3 Dataflow computation with threads . . . . . .
4.2.4 Thread scheduling . . . . . . . . . . . . . . .
4.2.5 Cooperative and competitive concurrency . . .
4.2.6 Thread operations . . . . . . . . . . . . . . .
4.3 Streams . . . . . . . . . . . . . . . . . . . . . . . . .
4.3.1 Basic producer/consumer . . . . . . . . . . .
4.3.2 Transducers and pipelines . . . . . . . . . . .
4.3.3 Managing resources and improving throughput
4.3.4 Stream objects . . . . . . . . . . . . . . . . .
4.3.5 Digital logic simulation . . . . . . . . . . . . .
4.4 Using the declarative concurrent model directly . . .
4.4.1 Order-determining concurrency . . . . . . . .
4.4.2 Coroutines . . . . . . . . . . . . . . . . . . . .
4.4.3 Concurrent composition . . . . . . . . . . . .
4.5 Lazy execution . . . . . . . . . . . . . . . . . . . . .
4.5.1 The demand-driven concurrent model . . . . .
4.5.2 Declarative computation models . . . . . . . .
4.5.3 Lazy streams . . . . . . . . . . . . . . . . . .
4.5.4 Bounded buffer . . . . . . . . . . . . . . . . .
4.5.5 Reading a file lazily . . . . . . . . . . . . . . .
4.5.6 The Hamming problem . . . . . . . . . . . . .
4.5.7 Lazy list operations . . . . . . . . . . . . . . .
4.5.8 Persistent queues and algorithm design . . . .
4.5.9 List comprehensions . . . . . . . . . . . . . .
4.6 Soft real-time programming . . . . . . . . . . . . . .
c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

213
216
219
221
221
222
223
228
233

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

237
239
241
243
246
247
251
251
251
252
256
259
260
261
261
263
265
270
271
277
277
279
281
283
286
290
293
295
297
298
299
303
307
309

CONTENTS

vii

4.6.1 Basic operations . . . . . . . . . . . . . . . . . .
4.6.2 Ticking . . . . . . . . . . . . . . . . . . . . . . .
4.7 Limitations and extensions of declarative programming .
4.7.1 Efficiency . . . . . . . . . . . . . . . . . . . . . .
4.7.2 Modularity . . . . . . . . . . . . . . . . . . . . .
4.7.3 Nondeterminism . . . . . . . . . . . . . . . . . .
4.7.4 The real world . . . . . . . . . . . . . . . . . . .
4.7.5 Picking the right model . . . . . . . . . . . . . .
4.7.6 Extended models . . . . . . . . . . . . . . . . . .
4.7.7 Using different models together . . . . . . . . . .
4.8 The Haskell language . . . . . . . . . . . . . . . . . . . .
4.8.1 Computation model . . . . . . . . . . . . . . . . .
4.8.2 Lazy evaluation . . . . . . . . . . . . . . . . . . .
4.8.3 Currying . . . . . . . . . . . . . . . . . . . . . . .
4.8.4 Polymorphic types . . . . . . . . . . . . . . . . .
4.8.5 Type classes . . . . . . . . . . . . . . . . . . . . .
4.9 Advanced topics . . . . . . . . . . . . . . . . . . . . . . .
4.9.1 The declarative concurrent model with exceptions
4.9.2 More on lazy execution . . . . . . . . . . . . . . .
4.9.3 Dataflow variables as communication channels . .
4.9.4 More on synchronization . . . . . . . . . . . . . .
4.9.5 Usefulness of dataflow variables . . . . . . . . . .
4.10 Historical notes . . . . . . . . . . . . . . . . . . . . . . .
4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . .
5 Message-Passing Concurrency
5.1 The message-passing concurrent model . . . . . . . . . .
5.1.1 Ports . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.2 Semantics of ports . . . . . . . . . . . . . . . . .
5.2 Port objects . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.1 The NewPortObject abstraction . . . . . . . . .
5.2.2 An example . . . . . . . . . . . . . . . . . . . . .
5.2.3 Reasoning with port objects . . . . . . . . . . . .
5.3 Simple message protocols . . . . . . . . . . . . . . . . . .
5.3.1 RMI (Remote Method Invocation) . . . . . . . .
5.3.2 Asynchronous RMI . . . . . . . . . . . . . . . . .
5.3.3 RMI with callback (using thread) . . . . . . . . .
5.3.4 RMI with callback (using record continuation) . .
5.3.5 RMI with callback (using procedure continuation)
5.3.6 Error reporting . . . . . . . . . . . . . . . . . . .
5.3.7 Asynchronous RMI with callback . . . . . . . . .
5.3.8 Double callbacks . . . . . . . . . . . . . . . . . .
5.4 Program design for concurrency . . . . . . . . . . . . . .
5.4.1 Programming with concurrent components . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

309
311
314
314
315
319
322
323
323
325
327
328
328
329
330
331
332
332
334
337
339
340
343
344

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

353
354
354
355
357
358
359
360
361
361
364
364
366
367
367
368
369
370
370

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

viii

CONTENTS

5.5

5.6

5.7
5.8

5.4.2 Design methodology . . . . . . . . . . . . . . .
5.4.3 List operations as concurrency patterns . . . . .
5.4.4 Lift control system . . . . . . . . . . . . . . . .
5.4.5 Improvements to the lift control system . . . . .
Using the message-passing concurrent model directly .
5.5.1 Port objects that share one thread . . . . . . .
5.5.2 A concurrent queue with ports . . . . . . . . . .
5.5.3 A thread abstraction with termination detection
5.5.4 Eliminating sequential dependencies . . . . . . .
The Erlang language . . . . . . . . . . . . . . . . . . .
5.6.1 Computation model . . . . . . . . . . . . . . . .
5.6.2 Introduction to Erlang programming . . . . . .
5.6.3 The receive operation . . . . . . . . . . . . . .
Advanced topics . . . . . . . . . . . . . . . . . . . . . .
5.7.1 The nondeterministic concurrent model . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . .

6 Explicit State
6.1 What is state? . . . . . . . . . . . . . . . . .
6.1.1 Implicit (declarative) state . . . . . .
6.1.2 Explicit state . . . . . . . . . . . . .
6.2 State and system building . . . . . . . . . .
6.2.1 System properties . . . . . . . . . . .
6.2.2 Component-based programming . . .
6.2.3 Object-oriented programming . . . .
6.3 The declarative model with explicit state . .
6.3.1 Cells . . . . . . . . . . . . . . . . . .
6.3.2 Semantics of cells . . . . . . . . . . .
6.3.3 Relation to declarative programming
6.3.4 Sharing and equality . . . . . . . . .
6.4 Abstract data types . . . . . . . . . . . . . .
6.4.1 Eight ways to organize ADTs . . . .
6.4.2 Variations on a stack . . . . . . . . .
6.4.3 Revocable capabilities . . . . . . . .
6.4.4 Parameter passing . . . . . . . . . .
6.5 Stateful collections . . . . . . . . . . . . . .
6.5.1 Indexed collections . . . . . . . . . .
6.5.2 Choosing an indexed collection . . .
6.5.3 Other collections . . . . . . . . . . .
6.6 Reasoning with state . . . . . . . . . . . . .
6.6.1 Invariant assertions . . . . . . . . . .
6.6.2 An example . . . . . . . . . . . . . .
6.6.3 Assertions . . . . . . . . . . . . . . .
6.6.4 Proof rules . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

372
373
374
383
385
385
387
390
393
394
394
395
398
402
402
407

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

413
416
416
417
418
419
420
421
421
422
424
425
426
427
427
429
433
434
438
439
441
442
444
444
445
448
449

CONTENTS

ix

6.6.5 Normal termination . . . . . . . . . . . . . . .
6.7 Program design in the large . . . . . . . . . . . . . .
6.7.1 Design methodology . . . . . . . . . . . . . .
6.7.2 Hierarchical system structure . . . . . . . . .
6.7.3 Maintainability . . . . . . . . . . . . . . . . .
6.7.4 Future developments . . . . . . . . . . . . . .
6.7.5 Further reading . . . . . . . . . . . . . . . . .
6.8 Case studies . . . . . . . . . . . . . . . . . . . . . . .
6.8.1 Transitive closure . . . . . . . . . . . . . . . .
6.8.2 Word frequencies (with stateful dictionary) . .
6.8.3 Generating random numbers . . . . . . . . . .
6.8.4 “Word of Mouth” simulation . . . . . . . . . .
6.9 Advanced topics . . . . . . . . . . . . . . . . . . . . .
6.9.1 Limitations of stateful programming . . . . .
6.9.2 Memory management and external references
6.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

452
453
454
456
461
464
466
467
467
475
476
481
484
484
485
487

7 Object-Oriented Programming
7.1 Motivations . . . . . . . . . . . . . . . . . . . . . . .
7.1.1 Inheritance . . . . . . . . . . . . . . . . . . .
7.1.2 Encapsulated state and inheritance . . . . . .
7.1.3 Objects and classes . . . . . . . . . . . . . . .
7.2 Classes as complete ADTs . . . . . . . . . . . . . . .
7.2.1 An example . . . . . . . . . . . . . . . . . . .
7.2.2 Semantics of the example . . . . . . . . . . .
7.2.3 Defining classes . . . . . . . . . . . . . . . . .
7.2.4 Initializing attributes . . . . . . . . . . . . . .
7.2.5 First-class messages . . . . . . . . . . . . . . .
7.2.6 First-class attributes . . . . . . . . . . . . . .
7.2.7 Programming techniques . . . . . . . . . . . .
7.3 Classes as incremental ADTs . . . . . . . . . . . . . .
7.3.1 Inheritance . . . . . . . . . . . . . . . . . . .
7.3.2 Static and dynamic binding . . . . . . . . . .
7.3.3 Controlling encapsulation . . . . . . . . . . .
7.3.4 Forwarding and delegation . . . . . . . . . . .
7.3.5 Reflection . . . . . . . . . . . . . . . . . . . .
7.4 Programming with inheritance . . . . . . . . . . . . .
7.4.1 The correct use of inheritance . . . . . . . . .
7.4.2 Constructing a hierarchy by following the type
7.4.3 Generic classes . . . . . . . . . . . . . . . . .
7.4.4 Multiple inheritance . . . . . . . . . . . . . .
7.4.5 Rules of thumb for multiple inheritance . . . .
7.4.6 The purpose of class diagrams . . . . . . . . .
7.4.7 Design patterns . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

493
495
495
497
497
498
499
500
501
503
504
507
507
507
508
511
512
517
522
524
524
528
531
533
539
539
540

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

x

CONTENTS
7.5 Relation to other computation models . . . . . . . . . . . .
7.5.1 Object-based and component-based programming . .
7.5.2 Higher-order programming . . . . . . . . . . . . . . .
7.5.3 Functional decomposition versus type decomposition
7.5.4 Should everything be an object? . . . . . . . . . . . .
7.6 Implementing the object system . . . . . . . . . . . . . . . .
7.6.1 Abstraction diagram . . . . . . . . . . . . . . . . . .
7.6.2 Implementing classes . . . . . . . . . . . . . . . . . .
7.6.3 Implementing objects . . . . . . . . . . . . . . . . . .
7.6.4 Implementing inheritance . . . . . . . . . . . . . . .
7.7 The Java language (sequential part) . . . . . . . . . . . . . .
7.7.1 Computation model . . . . . . . . . . . . . . . . . . .
7.7.2 Introduction to Java programming . . . . . . . . . .
7.8 Active objects . . . . . . . . . . . . . . . . . . . . . . . . . .
7.8.1 An example . . . . . . . . . . . . . . . . . . . . . . .
7.8.2 The NewActive abstraction . . . . . . . . . . . . . .
7.8.3 The Flavius Josephus problem . . . . . . . . . . . . .
7.8.4 Other active object abstractions . . . . . . . . . . . .
7.8.5 Event manager with active objects . . . . . . . . . .
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Shared-State Concurrency
8.1 The shared-state concurrent model . . . . .
8.2 Programming with concurrency . . . . . . .
8.2.1 Overview of the different approaches
8.2.2 Using the shared-state model directly
8.2.3 Programming with atomic actions . .
8.2.4 Further reading . . . . . . . . . . . .
8.3 Locks . . . . . . . . . . . . . . . . . . . . . .
8.3.1 Building stateful concurrent ADTs .
8.3.2 Tuple spaces (“Linda”) . . . . . . . .
8.3.3 Implementing locks . . . . . . . . . .
8.4 Monitors . . . . . . . . . . . . . . . . . . . .
8.4.1 Bounded buffer . . . . . . . . . . . .
8.4.2 Programming with monitors . . . . .
8.4.3 Implementing monitors . . . . . . . .
8.4.4 Another semantics for monitors . . .
8.5 Transactions . . . . . . . . . . . . . . . . . .
8.5.1 Concurrency control . . . . . . . . .
8.5.2 A simple transaction manager . . . .
8.5.3 Transactions on cells . . . . . . . . .
8.5.4 Implementing transactions on cells .
8.5.5 More on transactions . . . . . . . . .
8.6 The Java language (concurrent part) . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

543
543
544
547
548
552
552
554
555
556
556
557
558
563
564
564
565
568
569
574

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

577
581
581
581
585
588
589
590
592
594
599
600
602
605
605
607
608
610
613
616
619
623
625

CONTENTS

8.7

xi

8.6.1 Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
8.6.2 Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626

9 Relational Programming
9.1 The relational computation model . . . . . . . . . .
9.1.1 The choice and fail statements . . . . . .
9.1.2 Search tree . . . . . . . . . . . . . . . . . .
9.1.3 Encapsulated search . . . . . . . . . . . . .
9.1.4 The Solve function . . . . . . . . . . . . . .
9.2 Further examples . . . . . . . . . . . . . . . . . . .
9.2.1 Numeric examples . . . . . . . . . . . . . .
9.2.2 Puzzles and the n-queens problem . . . . . .
9.3 Relation to logic programming . . . . . . . . . . . .
9.3.1 Logic and logic programming . . . . . . . .
9.3.2 Operational and logical semantics . . . . . .
9.3.3 Nondeterministic logic programming . . . .
9.3.4 Relation to pure Prolog . . . . . . . . . . .
9.3.5 Logic programming in other models . . . . .
9.4 Natural language parsing . . . . . . . . . . . . . . .
9.4.1 A simple grammar . . . . . . . . . . . . . .
9.4.2 Parsing with the grammar . . . . . . . . . .
9.4.3 Generating a parse tree . . . . . . . . . . . .
9.4.4 Generating quantifiers . . . . . . . . . . . .
9.4.5 Running the parser . . . . . . . . . . . . . .
9.4.6 Running the parser “backwards” . . . . . .
9.4.7 Unification grammars . . . . . . . . . . . . .
9.5 A grammar interpreter . . . . . . . . . . . . . . . .
9.5.1 A simple grammar . . . . . . . . . . . . . .
9.5.2 Encoding the grammar . . . . . . . . . . . .
9.5.3 Running the grammar interpreter . . . . . .
9.5.4 Implementing the grammar interpreter . . .
9.6 Databases . . . . . . . . . . . . . . . . . . . . . . .
9.6.1 Defining a relation . . . . . . . . . . . . . .
9.6.2 Calculating with relations . . . . . . . . . .
9.6.3 Implementing relations . . . . . . . . . . . .
9.7 The Prolog language . . . . . . . . . . . . . . . . .
9.7.1 Computation model . . . . . . . . . . . . . .
9.7.2 Introduction to Prolog programming . . . .
9.7.3 Translating Prolog into a relational program
9.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

633
635
635
636
637
638
639
639
641
644
644
647
650
652
653
654
655
656
656
657
660
660
661
662
663
663
664
665
667
668
669
671
673
674
676
681
684

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

xii

CONTENTS

III

Specialized Computation Models

687

10 Graphical User Interface Programming
10.1 Basic concepts . . . . . . . . . . . . . . . . . . .
10.2 Using the declarative/procedural approach . . .
10.2.1 Basic user interface elements . . . . . . .
10.2.2 Building the graphical user interface . .
10.2.3 Declarative geometry . . . . . . . . . . .
10.2.4 Declarative resize behavior . . . . . . . .
10.2.5 Dynamic behavior of widgets . . . . . .
10.3 Case studies . . . . . . . . . . . . . . . . . . . .
10.3.1 A simple progress monitor . . . . . . . .
10.3.2 A simple calendar widget . . . . . . . . .
10.3.3 Automatic generation of a user interface
10.3.4 A context-sensitive clock . . . . . . . . .
10.4 Implementing the GUI tool . . . . . . . . . . .
10.5 Exercises . . . . . . . . . . . . . . . . . . . . . .
11 Distributed Programming
11.1 Taxonomy of distributed systems . . . . . .
11.2 The distribution model . . . . . . . . . . . .
11.3 Distribution of declarative data . . . . . . .
11.3.1 Open distribution and global naming
11.3.2 Sharing declarative data . . . . . . .
11.3.3 Ticket distribution . . . . . . . . . .
11.3.4 Stream communication . . . . . . . .
11.4 Distribution of state . . . . . . . . . . . . .
11.4.1 Simple state sharing . . . . . . . . .
11.4.2 Distributed lexical scoping . . . . . .
11.5 Network awareness . . . . . . . . . . . . . .
11.6 Common distributed programming patterns
11.6.1 Stationary and mobile objects . . . .
11.6.2 Asynchronous objects and dataflow .
11.6.3 Servers . . . . . . . . . . . . . . . . .
11.6.4 Closed distribution . . . . . . . . . .
11.7 Distribution protocols . . . . . . . . . . . .
11.7.1 Language entities . . . . . . . . . . .
11.7.2 Mobile state protocol . . . . . . . . .
11.7.3 Distributed binding protocol . . . . .
11.7.4 Memory management . . . . . . . . .
11.8 Partial failure . . . . . . . . . . . . . . . . .
11.8.1 Fault model . . . . . . . . . . . . . .
11.8.2 Simple cases of failure handling . . .
11.8.3 A resilient server . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

689
691
692
693
694
696
697
698
699
699
700
703
707
712
712

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

713
716
718
720
720
722
723
725
726
726
728
729
730
730
732
734
737
738
738
740
742
743
744
745
747
748

CONTENTS

xiii

11.8.4 Active fault tolerance . . . . . . .
11.9 Security . . . . . . . . . . . . . . . . . .
11.10Building applications . . . . . . . . . . .
11.10.1 Centralized first, distributed later
11.10.2 Handling partial failure . . . . . .
11.10.3 Distributed components . . . . .
11.11Exercises . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

12 Constraint Programming
12.1 Propagate and search . . . . . . . . . . . . . . . . . .
12.1.1 Basic ideas . . . . . . . . . . . . . . . . . . .
12.1.2 Calculating with partial information . . . . .
12.1.3 An example . . . . . . . . . . . . . . . . . . .
12.1.4 Executing the example . . . . . . . . . . . . .
12.1.5 Summary . . . . . . . . . . . . . . . . . . . .
12.2 Programming techniques . . . . . . . . . . . . . . . .
12.2.1 A cryptarithmetic problem . . . . . . . . . . .
12.2.2 Palindrome products revisited . . . . . . . . .
12.3 The constraint-based computation model . . . . . . .
12.3.1 Basic constraints and propagators . . . . . . .
12.4 Computation spaces . . . . . . . . . . . . . . . . . .
12.4.1 Programming search with computation spaces
12.4.2 Definition . . . . . . . . . . . . . . . . . . . .
12.5 Implementing the relational computation model . . .
12.5.1 The choice statement . . . . . . . . . . . . .
12.5.2 Implementing the Solve function . . . . . . .
12.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .

IV

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

749
749
751
751
751
752
752

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

755
756
756
757
758
760
761
761
761
763
764
766
766
767
767
777
778
778
778

Semantics

13 Language Semantics
13.1 The shared-state concurrent model . . . . . . . . . . . .
13.1.1 The store . . . . . . . . . . . . . . . . . . . . . .
13.1.2 The single-assignment (constraint) store . . . . .
13.1.3 Abstract syntax . . . . . . . . . . . . . . . . . . .
13.1.4 Structural rules . . . . . . . . . . . . . . . . . . .
13.1.5 Sequential and concurrent execution . . . . . . .
13.1.6 Comparison with the abstract machine semantics
13.1.7 Variable introduction . . . . . . . . . . . . . . . .
13.1.8 Imposing equality (tell) . . . . . . . . . . . . . . .
13.1.9 Conditional statements (ask) . . . . . . . . . . . .
13.1.10 Names . . . . . . . . . . . . . . . . . . . . . . . .
13.1.11 Procedural abstraction . . . . . . . . . . . . . . .

781
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

783
784
785
785
786
787
789
789
790
791
793
795
795

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

xiv

CONTENTS

13.2
13.3
13.4
13.5
13.6

V

13.1.12 Explicit state . . . . . . . .
13.1.13 By-need triggers . . . . . . .
13.1.14 Read-only variables . . . . .
13.1.15 Exception handling . . . . .
13.1.16 Failed values . . . . . . . . .
13.1.17 Variable substitution . . . .
Declarative concurrency . . . . . .
Eight computation models . . . . .
Semantics of common abstractions
Historical notes . . . . . . . . . . .
Exercises . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

Appendices

815

A Mozart System Development Environment
A.1 Interactive interface . . . . . . . . . . . . . .
A.1.1 Interface commands . . . . . . . . . .
A.1.2 Using functors interactively . . . . .
A.2 Batch interface . . . . . . . . . . . . . . . .
B Basic Data Types
B.1 Numbers (integers, floats, and characters)
B.1.1 Operations on numbers . . . . . . .
B.1.2 Operations on characters . . . . . .
B.2 Literals (atoms and names) . . . . . . . .
B.2.1 Operations on atoms . . . . . . . .
B.3 Records and tuples . . . . . . . . . . . . .
B.3.1 Tuples . . . . . . . . . . . . . . . .
B.3.2 Operations on records . . . . . . .
B.3.3 Operations on tuples . . . . . . . .
B.4 Chunks (limited records) . . . . . . . . . .
B.5 Lists . . . . . . . . . . . . . . . . . . . . .
B.5.1 Operations on lists . . . . . . . . .
B.6 Strings . . . . . . . . . . . . . . . . . . . .
B.7 Virtual strings . . . . . . . . . . . . . . . .

797
798
800
801
804
805
806
808
809
810
811

.
.
.
.
.
.
.
.
.
.
.
.
.
.

C Language Syntax
C.1 Interactive statements . . . . . . . . . . . .
C.2 Statements and expressions . . . . . . . . .
C.3 Nonterminals for statements and expressions
C.4 Operators . . . . . . . . . . . . . . . . . . .
C.4.1 Ternary operator . . . . . . . . . . .
C.5 Keywords . . . . . . . . . . . . . . . . . . .
C.6 Lexical syntax . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

817
817
817
818
819

.
.
.
.
.
.
.
.
.
.
.
.
.
.

821
821
823
824
825
826
826
827
828
829
829
830
831
832
833

.
.
.
.
.
.
.

835
836
836
838
838
841
841
843

CONTENTS

xv

C.6.1 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
C.6.2 Blank space and comments . . . . . . . . . . . . . . . . . . 843
D General Computation Model
D.1 Creative extension principle . .
D.2 Kernel language . . . . . . . . .
D.3 Concepts . . . . . . . . . . . . .
D.3.1 Declarative models . . .
D.3.2 Security . . . . . . . . .
D.3.3 Exceptions . . . . . . . .
D.3.4 Explicit state . . . . . .
D.4 Different forms of state . . . . .
D.5 Other concepts . . . . . . . . .
D.5.1 What’s next? . . . . . .
D.5.2 Domain-specific concepts
D.6 Layered language design . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

845
846
847
848
848
849
849
850
850
851
851
851
852

Bibliography

853

Index

869

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

xvi

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

List of Figures
1.1
1.2
1.3
1.4
1.5

Taking apart the list [5 6 7 8] . . . . . . . . . . . . . . . .
Calculating the fifth row of Pascal’s triangle . . . . . . . . . .
A simple example of dataflow execution . . . . . . . . . . . . .
All possible executions of the first nondeterministic example .
One possible execution of the second nondeterministic example

2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13
2.14
2.15
2.16
2.17
2.18
2.19
2.20
2.21
2.22

From characters to statements . . . . . . . . . . . . . . . . . . .
The context-free approach to language syntax . . . . . . . . . .
Ambiguity in a context-free grammar . . . . . . . . . . . . . . .
The kernel language approach to semantics . . . . . . . . . . . .
Translation approaches to language semantics . . . . . . . . . .
A single-assignment store with three unbound variables . . . . .
Two of the variables are bound to values . . . . . . . . . . . . .
A value store: all variables are bound to values . . . . . . . . .
A variable identifier referring to an unbound variable . . . . . .
A variable identifier referring to a bound variable . . . . . . . .
A variable identifier referring to a value . . . . . . . . . . . . . .
A partial value . . . . . . . . . . . . . . . . . . . . . . . . . . .
A partial value with no unbound variables, i.e., a complete value
Two variables bound together . . . . . . . . . . . . . . . . . . .
The store after binding one of the variables . . . . . . . . . . . .
The type hierarchy of the declarative model . . . . . . . . . . .
The declarative computation model . . . . . . . . . . . . . . . .
Lifecycle of a memory block . . . . . . . . . . . . . . . . . . . .
Declaring global variables . . . . . . . . . . . . . . . . . . . . .
The Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception handling . . . . . . . . . . . . . . . . . . . . . . . . .
Unification of cyclic structures . . . . . . . . . . . . . . . . . . .

. 33
. 35
. 36
. 39
. 42
. 44
. 44
. 45
. 46
. 46
. 47
. 47
. 48
. 48
. 49
. 53
. 62
. 76
. 88
. 90
. 92
. 102

3.1
3.2
3.3
3.4
3.5

A declarative operation inside a general computation .
Structure of the chapter . . . . . . . . . . . . . . . . .
A classification of declarative programming . . . . . . .
Finding roots using Newton’s method (first version) . .
Finding roots using Newton’s method (second version)

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

7
8
17
21
23

114
115
116
121
123

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

xviii

LIST OF FIGURES
3.6
3.7
3.8
3.9
3.10
3.11
3.12
3.13
3.14
3.15
3.16
3.17
3.18
3.19
3.20
3.21
3.22
3.23
3.24
3.25
3.26
3.27
3.28
3.29
3.30
3.31
3.32
3.33
3.34
3.35
3.36

Finding roots using Newton’s method (third version) . . . . . . .
Finding roots using Newton’s method (fourth version) . . . . . . .
Finding roots using Newton’s method (fifth version) . . . . . . . .
Sorting with mergesort . . . . . . . . . . . . . . . . . . . . . . . .
Control flow with threaded state . . . . . . . . . . . . . . . . . . .
Deleting node Y when one subtree is a leaf (easy case) . . . . . . .
Deleting node Y when neither subtree is a leaf (hard case) . . . .
Breadth-first traversal . . . . . . . . . . . . . . . . . . . . . . . .
Breadth-first traversal with accumulator . . . . . . . . . . . . . .
Depth-first traversal with explicit stack . . . . . . . . . . . . . . .
The tree drawing constraints . . . . . . . . . . . . . . . . . . . . .
An example tree . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tree drawing algorithm . . . . . . . . . . . . . . . . . . . . . . . .
The example tree displayed with the tree drawing algorithm . . .
Delayed execution of a procedure value . . . . . . . . . . . . . . .
Defining an integer loop . . . . . . . . . . . . . . . . . . . . . . .
Defining a list loop . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple loops over integers and lists . . . . . . . . . . . . . . . . .
Defining accumulator loops . . . . . . . . . . . . . . . . . . . . . .
Accumulator loops over integers and lists . . . . . . . . . . . . . .
Folding a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declarative dictionary (with linear list) . . . . . . . . . . . . . . .
Declarative dictionary (with ordered binary tree) . . . . . . . . .
Word frequencies (with declarative dictionary) . . . . . . . . . . .
Internal structure of binary tree dictionary in WordFreq (in part)
Doing S1={Pop S X} with a secure stack . . . . . . . . . . . . .
A simple graphical I/O interface for text . . . . . . . . . . . . . .
Screen shot of the word frequency application . . . . . . . . . . .
Standalone dictionary library (file Dict.oz) . . . . . . . . . . . .
Standalone word frequency application (file WordApp.oz) . . . . .
Component dependencies for the word frequency application . . .

124
124
125
140
141
156
157
159
160
160
162
162
164
165
181
186
186
187
188
189
190
199
201
202
203
208
217
228
229
230
231

4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12

The declarative concurrent model . . . . . . . . . . . . . . . .
Causal orders of sequential and concurrent executions . . . . .
Relationship between causal order and interleaving executions
Execution of the thread statement . . . . . . . . . . . . . . .
Thread creations for the call {Fib 6} . . . . . . . . . . . . .
The Oz Panel showing thread creation in {Fib 26 X} . . . .
Dataflow and rubber bands . . . . . . . . . . . . . . . . . . .
Cooperative and competitive concurrency . . . . . . . . . . . .
Operations on threads . . . . . . . . . . . . . . . . . . . . . .
Producer-consumer stream communication . . . . . . . . . . .
Filtering a stream . . . . . . . . . . . . . . . . . . . . . . . . .
A prime-number sieve with streams . . . . . . . . . . . . . . .

240
242
242
245
254
255
256
259
260
261
264
264

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

LIST OF FIGURES

xix

4.13
4.14
4.15
4.16
4.17
4.18
4.19
4.20
4.21
4.22
4.23
4.24
4.25
4.26
4.27
4.28
4.29
4.30
4.31
4.32
4.33
4.34
4.35

Pipeline of filters generated by {Sieve Xs 316} . . . . . .
Bounded buffer . . . . . . . . . . . . . . . . . . . . . . . . .
Bounded buffer (data-driven concurrent version) . . . . . . .
Digital logic gates . . . . . . . . . . . . . . . . . . . . . . . .
A full adder . . . . . . . . . . . . . . . . . . . . . . . . . . .
A latch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A linguistic abstraction for logic gates . . . . . . . . . . . . .
Tree drawing algorithm with order-determining concurrency
Procedures, coroutines, and threads . . . . . . . . . . . . . .
Implementing coroutines using the Thread module . . . . .
Concurrent composition . . . . . . . . . . . . . . . . . . . .
The by-need protocol . . . . . . . . . . . . . . . . . . . . . .
Stages in a variable’s lifetime . . . . . . . . . . . . . . . . .
Practical declarative computation models . . . . . . . . . . .
Bounded buffer (naive lazy version) . . . . . . . . . . . . . .
Bounded buffer (correct lazy version) . . . . . . . . . . . . .
Lazy solution to the Hamming problem . . . . . . . . . . . .
A simple ‘Ping Pong’ program . . . . . . . . . . . . . . . . .
A standalone ‘Ping Pong’ program . . . . . . . . . . . . . .
A standalone ‘Ping Pong’ program that exits cleanly . . . .
Changes needed for instrumenting procedure P1 . . . . . . .
How can two clients send to the same server? They cannot! .
Impedance matching: example of a serializer . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

266
267
267
272
273
275
276
278
280
281
282
287
289
291
296
296
298
310
311
312
317
319
326

5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.16
5.17
5.18
5.19
5.20

The message-passing concurrent model . . . . . . . . . . . . . . . 356
Three port objects playing ball . . . . . . . . . . . . . . . . . . . 359
Message diagrams of simple protocols . . . . . . . . . . . . . . . . 362
Schematic overview of a building with lifts . . . . . . . . . . . . . 374
Component diagram of the lift control system . . . . . . . . . . . 375
Notation for state diagrams . . . . . . . . . . . . . . . . . . . . . 375
State diagram of a lift controller . . . . . . . . . . . . . . . . . . . 377
Implementation of the timer and controller components . . . . . . 378
State diagram of a floor . . . . . . . . . . . . . . . . . . . . . . . 379
Implementation of the floor component . . . . . . . . . . . . . . . 380
State diagram of a lift . . . . . . . . . . . . . . . . . . . . . . . . 381
Implementation of the lift component . . . . . . . . . . . . . . . . 382
Hierarchical component diagram of the lift control system . . . . . 383
Defining port objects that share one thread . . . . . . . . . . . . . 386
Screenshot of the ‘Ping-Pong’ program . . . . . . . . . . . . . . . 386
The ‘Ping-Pong’ program: using port objects that share one thread 387
Queue (naive version with ports) . . . . . . . . . . . . . . . . . . 388
Queue (correct version with ports) . . . . . . . . . . . . . . . . . 389
A thread abstraction with termination detection . . . . . . . . . . 391
A concurrent filter without sequential dependencies . . . . . . . . 392
c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Copyright

xx

LIST OF FIGURES
5.21
5.22
5.23
5.24
5.25
5.26

Translation of receive without time out . . . . . . .
Translation of receive with time out . . . . . . . . .
Translation of receive with zero time out . . . . . .
Connecting two clients using a stream merger . . . .
Symmetric nondeterministic choice (using exceptions)
Asymmetric nondeterministic choice (using IsDet) .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

400
401
402
404
407
407

6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14

The declarative model with explicit state . . .
Five ways to package a stack . . . . . . . . . .
Four versions of a secure stack . . . . . . . . .
Different varieties of indexed collections . . . .
Extensible array (stateful implementation) . .
A system structured as a hierarchical graph .
System structure – static and dynamic . . . .
A directed graph and its transitive closure . .
One step in the transitive closure algorithm .
Transitive closure (first declarative version) . .
Transitive closure (stateful version) . . . . . .
Transitive closure (second declarative version)
Transitive closure (concurrent/parallel version)
Word frequencies (with stateful dictionary) . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

422
429
430
439
443
456
458
466
467
469
471
472
474
476

7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
7.10
7.11
7.12
7.13
7.14
7.15
7.16
7.17
7.18
7.19
7.20
7.21
7.22

An example class Counter (with class syntax) . . . . .
Defining the Counter class (without syntactic support) .
Creating a Counter object . . . . . . . . . . . . . . . . .
Illegal and legal class hierarchies . . . . . . . . . . . . . .
A class declaration is an executable statement . . . . . .
An example class Account . . . . . . . . . . . . . . . . .
The meaning of “private” . . . . . . . . . . . . . . . . .
Different ways to extend functionality . . . . . . . . . . .
Implementing delegation . . . . . . . . . . . . . . . . . .
An example of delegation . . . . . . . . . . . . . . . . . .
A simple hierarchy with three classes . . . . . . . . . . .
Constructing a hierarchy by following the type . . . . . .
Lists in object-or