Concepts-Based Teaching
of Programming
Oct. 10-11, 2005
Peter Van Roy
Universit catholique de Louvain
Louvain-la-Neuve, Belgium
Invited tutorials, CIESC 2005
10/10/2005
10/10/2005
Conclusions
10/10/2005
Mozart
Programming System
10/10/2005
10/10/2005
Some Course
Organizations
10/10/2005
10/10/2005
Declarative
programming
+ threads
Declarative
concurrency
+ ports
Message-passing
concurrency
Declarative
programming
+ cells
Stateful
programming and
data abstraction
+ threads
Declarative
concurrency
and agents
10/10/2005
+ threads
Declarative
concurrency
Declarative
programming
+ ports
Message-passing
concurrency
+ by-need
Lazy
evaluation
+ cells
Shared-state
concurrency
+ cells
Stateful
programming
+ classes
Object-oriented
programming
+ threads
Shared-state
concurrency
+ threads
Declarative
concurrency
+ by-need
Lazy
evaluation
+ search
Relational
programming
+ ports
Message-passing
concurrency
+ constraints
Constraint
programming
10/10/2005
10
Example Lectures
10/10/2005
11
Example lectures
10/10/2005
12
Lectures in French
See http://www.info.ucl.ac.be/notes_de_cours/FSAB1402/
See also http://www.info.ucl.ac.be/people/PVR/coursfrancais.html
10/10/2005
13
The Textbook
10/10/2005
14
The textbook
10/10/2005
15
Lesson 1:
Hands-on Introduction
to Programming Concepts
10/10/2005
16
Lesson 1
10/10/2005
17
Overview of programming
concepts
Simple calculator
Declarative variables
Functions
Structured data (example: lists)
Functions over lists
Correctness and complexity
Lazy functions
Concurrency and dataflow
State, objects, and classes
Nondeterminism and atomicity
10/10/2005
18
A calculator
{Browse 9999*9999}
10/10/2005
19
Variables
10/10/2005
20
Functions
10/10/2005
21
Composing functions
Combinations of r items taken from n.
The number of subsets of size r taken from a set of size n
n
n!
Comb
=
r r!(n r )!
declare
fun {Comb N R}
{Fact N} div ({Fact R}*{Fact N-R})
end
Fact
Fact
Fact
10/10/2005
22
1
1
1
1
1
1
2
3
4
1
3
1
4
declare
H=1
T = [2 3 4 5]
{Browse H|T} % Shows [1 2 3 4 5]
10/10/2005
23
More on lists
7
8
10/10/2005
nil
24
Pattern matching
10/10/2005
25
1.
2.
3.
10/10/2005
1
1
(0) 1
1
1
2
3
4
1
3
1 (0)
4
Shift right [0 1 3 3 1]
Shift left [1 3 3 1 0]
26
10/10/2005
Pascal N-1
Pascal N-1
ShiftLeft
ShiftRight
AddList
27
10/10/2005
28
Top-down program
development
10/10/2005
29
10/10/2005
30
Mathematical induction
10/10/2005
31
Correctness of factorial
fun {Fact N}
if N==0 then 1 else N*{Fact N-1} end
end
10/10/2005
32
Complexity
declare
fun {Pascal N}
if N==1 then [1]
else
{AddList
{ShiftLeft {Pascal N-1}}
{ShiftRight {Pascal N-1}}}
end
end
33
Faster Pascal
10/10/2005
fun {FastPascal N}
if N==1 then [1]
else
local L in
L={FastPascal N-1}
{AddList {ShiftLeft L}
{ShiftRight L}}
end
end
end
34
10/10/2005
declare
fun lazy {Ints N}
N|{Ints N+1}
end
35
36
10/10/2005
declare
L = {PascalList [1]}
{Browse L}
{Browse L.1}
{Browse L.2.1}
L<Future>
[1]
[1 1]
37
Higher-order programming
10/10/2005
38
Variations of Pascal
10/10/2005
1
2
3
4
1
1
1
3
1
1
1
1
1
0
1
0
1
1
1
0
39
Higher-order programming
fun {GenericPascal Op N}
if N==1 then [1]
else L in L = {GenericPascal Op N-1}
{OpList Op {ShiftLeft L} {ShiftRight L}}
end
end
fun {OpList Op L1 L2}
case L1 of H1|T1 then
case L2 of H2|T2 then
{Op H1 H2}|{OpList Op T1 T2}
end
end
else nil end
end
10/10/2005
40
Concurrency
10/10/2005
thread
P in
P = {Pascal 21}
{Browse P}
end
{Browse 99*99}
41
Dataflow (1)
10/10/2005
42
Dataflow (2)
10/10/2005
declare X
thread
{Delay 5000} X=99
end
{Browse Start}
{Browse X*X}
declare X
thread
{Browse Start}
{Browse X*X}
end
{Delay 5000} X=99
43
State
10/10/2005
declare
C = {NewCell 0}
C:= @C + 1
{Browse @C}
44
Example of state
Add memory to
Pascal to remember
how many times it is
called
The memory (state)
is global here
Memory that is local
to a function is called
encapsulated state
10/10/2005
declare
C = {NewCell 0}
fun {FastPascal N}
C:=@C+1
{GenericPascal Add N}
end
45
Objects
declare
fun {FastPascal N}
{Browse {Bump}}
{GenericPascal Add N}
end
10/10/2005
declare
local C in
C = {NewCell 0}
fun {Bump}
C:=@C+1
@C
end
end
46
Classes (1)
A class is a factory
of objects where
each object has its
own internal state
Let us create many
independent counter
objects with the
same behavior
10/10/2005
fun {NewCounter}
local C Bump in
C = {NewCell 0}
fun {Bump}
C:=@C+1
@C
end
Bump
end
end
47
Classes (2)
10/10/2005
fun {NewCounter}
local C Bump in
C = {NewCell 0}
fun {Bump}
C:=@C+1
@C
end
fun {Read}
@C
end
[Bump Read]
end
end
48
Object-oriented programming
State encapsulation
Object factories
10/10/2005
49
Nondeterminism (1)
10/10/2005
50
Nondeterminism (2)
declare
C = {NewCell 0}
thread C:=1 end
thread C:=2 end
t0
C = {NewCell 0}
cell C contains 0
t1
C:=1
cell C contains 1
t2
C:=2
cell C contains 2 (final value)
time
10/10/2005
51
Nondeterminism (3)
declare
C = {NewCell 0}
thread C:=1 end
thread C:=2 end
t0
C = {NewCell 0}
cell C contains 0
t1
C:=2
cell C contains 2
t2
C:=1
cell C contains 1 (final value)
time
10/10/2005
52
Nondeterminism (4)
declare
C = {NewCell 0}
thread I in
I = @C
C:= I+1
end
thread J in
J = @C
C:= J+1
end
10/10/2005
53
Nondeterminism (5)
10/10/2005
C = {NewCell 0}
t1
I = @C
t2
J = @C
I equal 0
J equal 0
t3
C:= J+1
t4
C:= I+1
C contains 1
C contains 1
time
54
Insights gained
1.
2.
3.
10/10/2005
55
Atomicity (1)
10/10/2005
56
Atomicity (2)
declare
L = {NewLock}
lock L then
sequence of ops 1
end
10/10/2005
Thread 1
lock L then
sequence of ops 2
end
Thread 2
57
Using atomicity
declare
C = {NewCell 0}
L = {NewLock}
thread
lock L then I in
I = @C
C:= I+1
end
end
thread
lock L then J in
J = @C
C:= J+1
end
end
10/10/2005
58
Lesson 2:
Language Syntax
10/10/2005
59
Description of a language
10/10/2005
60
lexical
phrasal
10/10/2005
61
10/10/2005
62
Parser is a program
characters
Lexical analyzer
tokens
Parser
sentences
63
Backus-Naur Form
10/10/2005
64
Examples of BNF
(A) BNF rules for robot commands
A robot arm only accepts a command from
{up, down, left, right}
move ::= cmd
move ::= cmd move
cmd ::= up
cmd ::= down
cmd ::= left
cmd ::= right
10/10/2005
65
Grammar rules
10/10/2005
66
10/10/2005
up
down left
up down down up right left
P. Van Roy, CIESC tutorials
67
Integers
integer ::= digit | digit integer
digit ::= 0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9
integer is defined as the sequence of a digit
followed by zero or more digits
10/10/2005
68
10/10/2005
69
Grammar rules
10/10/2005
70
x
x ::= Body
x | y
x y
{ x }
{ x }+
[ x ]
10/10/2005
nonterminal x
x is defined by Body
either x or y (choice)
the sequence x followed by y
sequence of zero or more
occurrences of x
sequence of one or more
occurrences of x
zero or one occurrence of x
P. Van Roy, CIESC tutorials
71
10/10/2005
72
Examples
10/10/2005
73
10/10/2005
74
111100 L(G)
2nd rule
???
1st rule
110
11
0
011
2nd rule
110
10/10/2005
75
::=
::=
::=
::=
<int-part> . <fraction>
<digit> | <int-part> <digit>
<digit> | <digit> <fraction>
0|1|2|3|4|5|6|7|8|9
<real-#>
<int-part>
<fraction>
<int-part>
<digit>
<digit>
<fraction>
<digit>
<digit>
1
10/10/2005
9
P. Van Roy, CIESC tutorials
76
Ambiguity (1)
10/10/2005
77
Ambiguity (2)
expr ::= num|expr-expr
Parse: (1-2)-3
expr
expr
expr
10/10/2005
expr
num
num
expr
num
78
Ambiguity (3)
expr ::= num|expr-expr
Parse: 1-(2-3)
Which parse tree?
expr
expr
num
expr
expr
expr
num
num
10/10/2005
79
Ambiguity resolution
for binary operators (1)
10/10/2005
80
Ambiguity resolution
for binary operators (2)
| expr expr
10/10/2005
Left associative:
expr ::= num | expr num
Right Associative:
expr ::= num | num expr
81
Ambiguity resolution
for binary operators (3)
10/10/2005
82
Ambiguity resolution
for binary operators (4)
1+2*3
<expr
>
<expr
>
<num
>
1
<expr
>
<expr
>
<expr <num
>
>
<num
3
>
2
<expr
>
<expr + <expr
>
>
<num <expr
* <expr
>
>
>
<num
<num
1
>
>
Which One?
(1+2)*3
10/10/2005
1+(2*3)
P. Van Roy, CIESC tutorials
83
Ambiguity resolution
for binary operators (5)
Example: This BNF is ambiguous:
<expr> ::= <num> | <expr> + <expr> | <expr> * <expr>
To make it unambiguous, I want
(Case 1) + to be of a higher precedence than *
<expr> ::= <expr2> | <expr2> * <expr>
<expr2>::= <num> | <num> + <expr2>
<expr
2>
1+2*3
<num
>
1
10/10/2005
(1+2)*3
<expr
>
<expr
>
<expr <expr
2>
2>
<num <num
>
>
2
3
84
Ambiguity resolution
for binary operators (6)
Example: This BNF is ambiguous:
<expr> ::= <num> | <expr> + <expr> | <expr> * <expr>
To make it unambiguous, I want
(Case 2) * to be of a higher precedence than +
<expr> ::= <expr2> | <expr2> + <expr>
<expr2>::= <num> | <num> * <expr2>
1+2*3
<expr
2>
<num
>
1
1+(2*3)
10/10/2005
<expr
>
<expr
>
<expr
2>
<num
>
<expr
2>
<num
>
3
85
Ambiguity of operators
10/10/2005
86
Ambiguity of operators
Version #1 of BNF:
<E> ::= <E> + <E> |
<E> - <E> | <E> * <E> | <E> /<E>|
<num> | <var> | (<E>)
Is the grammar ambiguous? Yes
Version #2 of BNF:
<E> ::= <E> + <T> | <E> - <T> | <T>
<T> ::= <T> * <F> | <T> / <F> | <F>
<F> ::= <num> | <var> | (<E>)
10/10/2005
87
Ambiguity in general
String:
if <E1> then if <E2> then <S1> else <S2>
Parse Tree???
<S>
<S>
if <E1> then<S>
10/10/2005
88
Context-sensitive grammars
10/10/2005
89
Context-free and
context-sensitive grammars
Context-free grammar
(e.g. with EBNF)
Expresses restrictions
imposed by language
Renders grammar rules
context sensitive
10/10/2005
90
Lesson 3:
Introduction to
Language Semantics
10/10/2005
91
Language semantics
simple
allow programmer to reason about program
(correctness, execution time, and memory use)
10/10/2005
92
10/10/2005
linguistic abstractions
syntactic sugar
P. Van Roy, CIESC tutorials
93
practical language
translation
kernel language
Easy to understand and reason
with
Has a precise (formal) semantics
10/10/2005
proc {Sqr X Y}
{ * X X Y}
end
local T in
{Sqr A T}
{Sqr T B}
end
94
10/10/2005
95
Linguistic abstractions:
provide higher level concepts
Syntactic sugar:
short cuts and conveniences to
improve readability
end
end
end
10/10/2005
96
Approaches to semantics
Programming Language
Operational model
Kernel Language
Aid programmer
in reasoning and
understanding
Formal calculus
Abstract machine
Our goal!
10/10/2005
97
Sequential declarative
computation model (first model)
maps textual variable names (variable identifiers) into entities in the store
10/10/2005
98
10/10/2005
99
proc {P X}
local Y in Y = 1 {Browse Y} end
X=Y
end
Free Occurrences
10/10/2005
Bound Occurrences
100
101
local Res in
local Arg1 Arg2 in
Arg1 = 111*111
Arg2 = 999*999
Res = Arg1*Arg2
end
{Browse Res}
end
This is a runnable program (there are no free identifier occurrences) !
10/10/2005
102
10/10/2005
103
Procedure abstraction
10/10/2005
104
local P Q in
P = proc {$} {Q} end
Q = proc {$} {Browse hello} end
local Q in
Q = proc {$} {Browse hi} end
{P}
end
end
10/10/2005
105
x1
( , )
Q x2
local P Q in
P = proc {$} {Q
Q} end
Q = proc {$} {Browse hello} end
x2 ( , )
local Q in
Q = proc {$} {Browse hi} end
{P}
end
proc {$} {Browse hello} end
end
10/10/2005
Browse x0
106
10/10/2005
107
Lesson 4:
Declarative
Programming
10/10/2005
108
10/10/2005
109
Declarative
operation
Results
rest of computation
10/10/2005
110
10/10/2005
111
10/10/2005
112
Classification of
declarative programming
Descriptive
Declarative
programming
Observational
Functional
programming
Programmable
Definitional
Declarative
model
Deterministic
logic programming
Nondeterministic
logic programming
113
Descriptive language
s ::=
|
|
|
|
skip
x = y
x = record
s1 s2
local x in s1 end
empty statement
variable-variable binding
variable-value binding
sequential composition
declaration
10/10/2005
114
Descriptive language
<person id = 530101-xxx>
<name> Seif </name>
<age> 48 </age>
</person>
10/10/2005
115
skip
empty statement
x = y
variable-variable binding
x = v
variable-value binding
s1 s2
sequential composition
local x in s1 end
declaration
if x then s1 else s2 end
conditional
{ x y1 yn }
procedural application
case x of pattern then s1 else s2 end
pattern matching
v ::= ...
pattern
10/10/2005
value expression
::= ...
P. Van Roy, CIESC tutorials
116
Value expressions
v ::= procedure | record | number
procedure ::= proc {$ y1 yn} s end
record, pattern ::= literal
|
literal (feature1 : x1 featuren : xn)
literal ::= atom | bool
feature ::= int | atom | bool
bool
number
10/10/2005
117
sequential composition
local statement
procedure definition
procedure call
if statement
try statement
118
Defining types
Simple list functions
Mergesort
10/10/2005
119
10/10/2005
120
Defining types
list ::= value | list [] nil
10/10/2005
121
Lists
1.
2.
10/10/2005
122
1.
2.
3.
10/10/2005
123
1.
2.
3.
4.
5.
10/10/2005
124
10/10/2005
125
10/10/2005
fun {Nth Xs N}
if N==1 then Xs.1
elseif N>1 then {Nth Xs.2 N-1}
end
end
126
10/10/2005
127
10/10/2005
129
130
10/10/2005
131
Reverse
fun {Reverse Xs}
case Xs
of nil then nil
[] X|Xr then {Append {Reverse Xr} [X]}
end
end
Xs0 = 1 | [2 3 4]
reverse of Xs1 [4 3 2]
Xs1
append [4 3 2] and [1]
10/10/2005
132
Length
fun {Length Xs}
case Xs
of nil then 0
[] X|Xr then 1+{Length Xr}
end
end
Inductive reasoning on Xs
10/10/2005
133
134
1.
2.
3.
10/10/2005
135
split
merge
L12
L
S1
S12
split
merge
L21
L2
split
S21
merge
L22
10/10/2005
S11
S2
S22
136
1.
2.
3.
137
Split
proc {Split Xs Ys Zs}
case Xs
of nil then Ys = nil Zs = nil
[] [X] then Ys = Xs Zs = nil
[] X1|X2|Xr then Yr Zr in
Ys = X1|Yr
Zs = X2|Zr
{Split Xr Yr Zr}
end
end
10/10/2005
138
Lesson 5:
Stateful Programming
10/10/2005
139
Lesson 5
10/10/2005
140
State as a group
of memory cells
Group of functions and
procedures that operate
on the state
10/10/2005
141
State as a group
of memory cells
Group of functions and
procedures that operate
on the state
10/10/2005
142
What is state?
State is a sequence of
values in time that
contains the
intermediate results of a
desired computation
Declarative programs
can also have state
according to this
definition
Consider the following
program
10/10/2005
fun {Sum Xs A}
case Xs
of X|Xr then {Sum Xr A+X}
[] nil then A
end
end
{Show {Sum [1 2 3 4] 0}}
143
fun {Sum Xs A}
case Xs
of X|Xr then {Sum Xr A+X}
[] nil then A
end
end
{Show {Sum [1 2 3 4] 0}}
144
X
C
5
C
10/10/2005
145
X
C
5
C
10/10/2005
146
X = {NewCell I}
Example: X = {NewCell 0}
X:=J
Y = @X
10/10/2005
147
Examples (1)
X = {NewCell 0}
X:=5
Y=X
Y:=10
10/10/2005
@X == 10 % returns true
X == Y % returns true
10
148
Examples (2)
X = {NewCell 10}
Y = {NewCell 10}
X == Y % returns false
Because X and Y refer to
different cells, with different
identities
10/10/2005
10
10
@X == @Y
returns true
P. Van Roy, CIESC tutorials
149
Examples (3)
X = {NewCell 0}
X:=5
Y=X
Y:=10
X == 10
returns true
10/10/2005
Y
X
10
Y
P. Van Roy, CIESC tutorials
150
10/10/2005
.....
Semantic stack
(Thread n)
w = f(x)
z = person(a:y)
y = 1
u = 2
x
1: w
2: x
....
....
single assignment
store
mutable store
151
empty statement
statement sequence
thread creation
cell creation
cell exchange
152
cell creation
cell exchange
153
10/10/2005
154
Program
10/10/2005
155
Modular programs
10/10/2005
156
Assume we have
three persons, P, U1,
and U2
P is a programmer
who developed a
component M that
provides two functions
F and G
U1 and U2 are
system builders that
use the component M
10/10/2005
fun {MF}
fun {F ...}
Definition of F
end
fun {G ...}
Definition of G
end
in export(f:F g:G)
end
M = {MF}
157
Assume we have
three persons, P, U1,
and U2
P is a programmer
who developed a
component M that
provides two functions
F and G
U1 and U2 are
system builders that
use the component M
10/10/2005
functor MF
export f:F g:G
define
fun {F ...}
Definition of F
end
fun {G ...}
Definition of G
end
end
158
10/10/2005
fun {M}
fun {F ...}
Definition of F
end
fun {G ...}
Definition of G
end
in export(f:F g:G)
end
159
10/10/2005
160
10/10/2005
fun {MF}
X = {NewCell 0}
fun {F ...}
X := @X+1
Definition of F
end
fun {G ...} Definition of G end
fun {Count} @ X end
in export(f:F g:G c:Count)
end
M = {MF}
161
10/10/2005
162
10/10/2005
163
164
10/10/2005
165
10/10/2005
166
Lesson 6:
Language Semantics and
the Abstract Machine
10/10/2005
167
10/10/2005
168
Semantic concepts
Single-assignment store
Environment
Semantic statement
Execution state
Computation
10/10/2005
169
Abstract machine
Performs a computation
A computation is a sequence of execution states
Execution state
Semantic statement
10/10/2005
170
Single-assignment store
Example store
10/10/2005
x1
unbound
x2, x3 equal and unbound
x4
bound to partial value a|x2
P. Van Roy, CIESC tutorials
171
Environment
Environment
variable identifier
store variable
X
x
Example environment
10/10/2005
{ X x, Y y }
172
Example:
={x1, x2=x3, x4=a|x2}
E = { X x1, Y x4 }
E(X) = x1
and no information in on x1
E(Y) = x4
and binds x4 to a|x2
10/10/2005
173
assume store
given variable identifier x
E(x) is value in store
10/10/2005
174
Environment adjunction
Given: Environment E
E + {x1x1, , xnxn}
is new environment E with mappings added:
10/10/2005
175
Environment restriction
Given: Environment E
E | {x1, , xn}
is new environment E where only mappings
for {x1, , xn} are retained from E
10/10/2005
176
Adjunction example
E0 = { Y 1 }
E1 = E0 + { X 2 }
corresponds to { X 2, Y 1 }
E1(X) = 2
E2 = E1 + { X 3 }
10/10/2005
corresponds to { X 3, Y 1 }
E2(X) = 3
P. Van Roy, CIESC tutorials
177
Why adjunction?
E0
local X in
X=2
local X in
X=3
{ Y}
end
end
E2
E1
E0
10/10/2005
178
Semantic statements
Semantic statement
10/10/2005
( s, E )
179
Stacks of statements
10/10/2005
180
Execution state
Execution state
( ST, )
Computation
(ST1, 1) (ST2, 2) (ST3, 3)
10/10/2005
181
Program execution
empty store
stack with semantic statement
[(s,)]
10/10/2005
182
terminated
runnable
suspended
stack is empty
can do execution step
stack not empty, no execution
step possible
Statements
10/10/2005
non-suspending
suspending
183
Summary of lesson 6
Semantic statements
Semantic stacks
Execution state
Program execution
adjunction, restriction
E
E + {} E | {}
(s, E)
[(s, E) ]
(ST, )
Statements
10/10/2005
suspending, non-suspending
P. Van Roy, CIESC tutorials
184
Lesson 7:
Executing the
Abstract Machine
10/10/2005
185
Simple statements
10/10/2005
lexical scoping
closures
procedures as values
procedure call
P. Van Roy, CIESC tutorials
186
Simple statements
s denotes a statement
s ::=
|
|
|
|
|
skip
x = y
x = v
s1 s2
local x in s1 end
if x then s1 else s2 end
v ::= ...
10/10/2005
empty statement
variable-variable binding
variable-value binding
sequential composition
declaration
conditional
value expression
187
Executing skip
10/10/2005
188
skip
(skip, E)
ST
ST
No effect on store
Non-suspending statement
10/10/2005
189
skip
(skip, E)
ST
ST
10/10/2005
190
Executing sequential
composition
Semantic statement is
(s1 s2, E)
Push in following order
s2
s1
executes after
executes next
Statement is non-suspending
10/10/2005
191
Sequential composition
(s1, E)
(s1 s2, E)
ST
(s2, E)
ST
10/10/2005
192
Executing local
Semantic statement is
(local x in s end, E)
Execute as follows
Statement is non-suspending
10/10/2005
193
local
local x in
s
end
ST
(s, E)
ST
With E = E + {x y}
10/10/2005
194
Variable-variable equality
Semantic statement is
(x = y, E)
Execute as follows
Statement is non-suspending
10/10/2005
195
Variable-value equality
Semantic statement is
(x = v, E)
with v number or record
Execute as follows
Statement is non-suspending
10/10/2005
196
Variable-value equality
Semantic statement is
(x = v, E)
with v number or record
Execute as follows
What about
procedures?
Statement is non-suspending
10/10/2005
197
Suspending statements
Conditional?
10/10/2005
198
Executing if
Semantic statement is
(if x then s1 else s2 end, E)
If activation condition x bound true
push s1
push s2
Otherwise, suspend
10/10/2005
199
Example
local X in
local B in
B=true
if B then X=1 else skip end
end
end
10/10/2005
200
10/10/2005
201
Example: local
([(local B in
B=true
if B then X=1 else skip end
end,
{X x})],
{x})
10/10/2005
202
Example: local
([(B=true
if B then X=1 else skip end
,
{B b, X x})],
{b,x})
10/10/2005
203
Example:
sequential composition
([(B=true, {B b, X x}),
(if B then X=1
else skip end, {B b, X x})],
{b,x})
10/10/2005
204
Example:
variable-value assignment
([(if B then X=1
else skip end, {B b, X x})],
{b=true, x})
Environment maps B to b
Bind b to true
10/10/2005
205
Example: if
([(X=1, {B b, X x})],
{b=true, x})
Environment maps B to b
Store binds b to true, continue with then
10/10/2005
206
Example:
variable-value assignment
([],
{b=true, x=1})
Environment maps X to x
Binds x to 1
Computation terminates as stack is empty
10/10/2005
207
Summary
popping itself
creating environment
manipulating store
pushing new statements
always
local
local, =
local, if
sequential composition
10/10/2005
activation condition
read store
P. Van Roy, CIESC tutorials
208
Lesson 8:
Declarative Concurrency
10/10/2005
209
Declarative concurrency
10/10/2005
210
Concurrency in general
10/10/2005
211
Overview
Lazy execution
10/10/2005
212
Single-assignment
store
10/10/2005
w=a
z = person(age: y)
x
y = 42
u
213
Single-assignment
store
10/10/2005
Semantic
Stack 1
Semantic
Stack N
w=a
z = person(age: y)
x
y = 42
u
214
10/10/2005
skip
empty statement
x = y
variable-variable binding
x = v
variable-value binding
s1 s2
sequential composition
local x in s1 end
declaration
proc {x y1 yn } s1 end
procedure introduction
if x then s1 else s2 end
conditional
{ x y1 yn }
procedure application
case x of pat then s1 else s2 end
pattern matching
thread s1 end
thread creation
215
ST
thread s1 end,E
Single-assignment
store
10/10/2005
216
ST
s1, E
Single-assignment
store
10/10/2005
217
Basic concepts
10/10/2005
218
Causal order
10/10/2005
219
Total order
sequential
execution
computation step
10/10/2005
220
thread T3
thread T2
fork a thread
thread T1
computation step
10/10/2005
221
thread T2
thread T1
computation step
10/10/2005
222
Nondeterminism
10/10/2005
223
Example of nondeterminism
store
Thread 1
Thread 2
x
y=5
x=1
x=3
time
time
The thread that binds x first will continue,
the other thread will raise an exception
10/10/2005
224
Nondeterminism
10/10/2005
225
Scheduling
The choice of which thread to execute next and for how long
is done by a part of the system called the scheduler
A thread is runnable if its next statement to execute is not
blocked on a dataflow variable, otherwise the thread is
suspended
A scheduler is fair if it does not starve a runnable thread
I.e. all runnable thread execute eventually
Fair scheduling make it easy to reason about programs
Otherwise some prefectly runnable program will never get its
share
10/10/2005
226
The semantics
10/10/2005
227
({ [ (s,) ] }, )
stack
store
multiset
10/10/2005
228
10/10/2005
229
230
Dataflow computation
declare X
{Browse X}
local Y in
thread {Delay 1000} Y = 10*10 end
X = Y + 100*100
end
10/10/2005
231
Enter incrementally
the values of X0 to X3
When X0 is bound the
thread will compute
Y0=X0+1, and will
suspend again until
X1 is bound
232
10/10/2005
233
234
The main thread will traverse the list creating two threads for
the first two arguments of the list
10/10/2005
235
10/10/2005
236
Declarative programs
can be easily made
concurrent
Just use the thread
statement where
concurrent is needed
10/10/2005
fun {Fib X}
if X=<2 then 1
else
thread {Fib X-1} end
+ {Fib X-2}
end
end
237
Understanding why
fun {Fib X}
if X=<2 then 1
else F1 F2 in
F1 = thread {Fib X-1} end
F2 = {Fib X-2}
Dataflow dependency
F1 + F2
end
end
10/10/2005
238
Execution of {Fib 6}
F2
F3
F4
Fork a thread
F1
F2
Synchronize on
result
F2
F5
F3
F1
F2
F3
F6
F4
10/10/2005
Running thread
F1
F2
P. Van Roy, CIESC tutorials
239
10/10/2005
240
Streams
producer
10/10/2005
x5 x4 x3 x2 x1
consumer
241
10/10/2005
242
thread 2
producer
transducer
10/10/2005
thread 3
transducer
thread N
consumer
243
Program patterns
10/10/2005
244
Producer pattern
fun {Producer State}
if {More State} then
X = {Produce State} in
X | {Producer {Transform State}}
else nil end
end
The definitions of More, Produce, and Transform are
problem-dependent
The State could be multiple arguments
The above definition is not a complete program!
10/10/2005
245
Example producer
fun {Generate N Limit}
if N=<Limit then
N | {Generate N+1 Limit}
else nil end
end
10/10/2005
246
Consumer pattern
fun {Consumer State InStream}
The consumer suspends until
case InStream
InStream is either a cons or a nil
of nil then {Final State}
[] X | RestInStream then
NextState = {Consume X State} in
{Consumer NextState RestInStream}
end
end
Final and Consume are problem-dependent
10/10/2005
247
Example consumer
fun {Sum A Xs}
case Xs
of X|Xr then {Sum A+X Xr}
[] nil then A
end
end
The State is A
Final is just the identity function on State
Consume takes X and State X + State
10/10/2005
248
249
250
Example transducer
IsOdd
6 5 4 3 2 1
Generate
fun {Filter Xs F}
case Xs
of nil then nil
[] X|Xr then
if {F X} then X|{Filter Xr
F}
else {Filter Xr F} end
end
end
10/10/2005
5 3
Filter
251
Larger example:
The sieve of Eratosthenes
Sieve
Xs
Xr
10/10/2005
X|Zs
Filter
Ys
Sieve
Zs
252
Sieve
fun {Sieve Xs}
case Xs
of nil then nil
[] X|Xr then Ys in
thread
Ys = {Filter Xr fun {$ Y} Y mod X \= 0 end}
end
X | {Sieve Ys}
end
end
The program forks a filter thread on each sieve call
10/10/2005
253
Example call
local Xs Ys in
thread Xs = {Generate 2 100000} end
thread Ys = {Sieve Xs} end
thread for Y in Ys do {Show Y} end end
end
10/10/2005
254
Larger example:
The sieve of Eratosthenes
7 | 11 |...
Filter 2
10/10/2005
Filter 3
Filter 5
Sieve
255
10/10/2005
x5 x4 x3 x2 x1
consumer
256
Solutions
1.
2.
3.
10/10/2005
257
Lesson 9:
Object-Oriented
Programming
10/10/2005
258
Object-oriented programming
10/10/2005
259
Conclusions
10/10/2005
260
Conclusions
10/10/2005
261