Introduccion MPP
-
Upload
joe-miranda -
Category
Documents
-
view
234 -
download
0
Transcript of Introduccion MPP
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 1/261
10/10/2005 P. Van Roy, CIESC tutorials 1
Tutorial Sessions:
Concepts-Based Teachingof Programming
Oct. 10-11, 2005
Peter Van Roy
Université catholique de Louvain
Louvain-la-Neuve, Belgium
Invited tutorials, CIESC 2005
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 2/261
10/10/2005 P. Van Roy, CIESC tutorials 2
Teaching with concepts! Instead of teaching programming languages, we teach
programming concepts!
! With the multiparadigm language Oz it is possible to teachmany concepts, covering many paradigms
! Let us first see some of the course organizations! From second-year to graduate courses
! Then we will give a number of sample lectures, each in detailwith hands-on examples! These were selected to give a variety of different concepts
! We will not talk about logic programming in these tutorials, for lack of time! Although it is an important topic!
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 3/261
10/10/2005 P. Van Roy, CIESC tutorials 3
Overview of the tutorial! Examples of some course organizations
! Mozart Programming System
! Example lessons! The bulk of the tutorial will be a carefully selected
set of real lectures
! You and I together will run the code in the
lectures using the Mozart system
! Conclusions
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 4/261
10/10/2005 P. Van Roy, CIESC tutorials 4
MozartProgramming System
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 5/261
10/10/2005 P. Van Roy, CIESC tutorials 5
Mozart Programming System! All the lectures are designed to be accompanied by the
Mozart Programming System
! Mozart is a program development platform that implementsthe Oz language
! Mozart has an interactive interface based on emacs! Windows junkies tend not to like emacs, which comes from a
Unix background
! Mozart is a high-quality implementation that can execute allthe example programs in the book! Mozart is the product of more than a decade of research and
implementation by an international group
! Mozart is released under an Open Source license
! See http://www.mozart-oz.org for more information anddownloads
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 6/261
10/10/2005 P. Van Roy, CIESC tutorials 6
Some CourseOrganizations
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 7/261
10/10/2005 P. Van Roy, CIESC tutorials 7
Some courses (1)! Second-year course (Datalogi II at KTH,
CS2104 at NUS) by Seif Haridi andChristian Schulte! Start with declarative programming
! Explain declarative techniques andhigher-order programming
! Explain semantics
! Add threads: leads to declarative(dataflow) concurrency
! Add ports (communication channels):leads to message-passing concurrency(agents)
! Declarative programming, concurrency,and multi-agent systems! For many reasons, this is a better start
than OOP
Declarative
programming
Declarative
concurrency
Message-passing
concurrency
+ ports
+ threads
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 8/261
10/10/2005 P. Van Roy, CIESC tutorials 8
Some courses (2)! Second-year course (FSAC1450,
LINF1251, FSAB1402 at UCL) byPeter Van Roy! Start with declarative programming
! Explain declarative techniques
! Explain semantics
! Add cells (mutable state)
! Explain data abstraction: objects and ADTs
! Explain object-oriented programming:classes, polymorphism, andinheritance
! Add threads: leads to declarativeconcurrency
! Most comprehensive overview in oneearly course
Declarative
programming
Stateful
!programming and
!data abstraction
Declarative
concurrency
and agents
+ threads+ cells
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 9/261
10/10/2005 P. Van Roy, CIESC tutorials 9
Some courses (3)! Third-year course (INGI2131 at
UCL) by Peter Van Roy! Review of declarative programming
! Add threads: leads to declarativeconcurrency
! Add by-need synchronization:leads to lazy execution
! Combining lazy execution andconcurrency
! Add ports (communication channels):leads to message-passingconcurrency
! Designing multi-agent systems
! Add cells (mutable state): leads toshared-state concurrency
! Tuple spaces (Linda-like)
! Locks, monitors, transactions
! Focus on concurrent programming
Declarative
concurrency
Message-passingconcurrency
Shared-stateconcurrency
Declarative
programming
+ threads
+ cells+ ports
Lazy
evaluation
+ by-need
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 10/261
10/10/2005 P. Van Roy, CIESC tutorials 10
A more advanced course (4)
! This is an example of a more advanced course given at an unnamed institution
! It covers many more paradigms, their semantics, and some of their relationships
! It can be given as a graduate course
!Declarative
!programming
Statefulprogramming
Declarativeconcurrency
+ threads+ cells
Message-passing
concurrency
Relationalprogramming
Constraint
programming
Shared-state
concurrency
Object-oriented
programming
Lazy
evaluation
+ search
+ threads + by-need + ports + constraints+ classes
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 11/261
10/10/2005 P. Van Roy, CIESC tutorials 11
Example Lectures
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 12/261
10/10/2005 P. Van Roy, CIESC tutorials 12
Example lectures! Lesson 1: Hands-on Introduction to Programming Concepts
! Lesson 2: Language Syntax
! Lesson 3: Introduction to Language Semantics
! Lesson 4: Declarative Programming! Lesson 5: Stateful Programming
! Lesson 6: Language Semantics with an Abstract Machine
! Lesson 7: Executing the Abstract Machine
! Lesson 8: Declarative Concurrency
! Lesson 9: Object-Oriented Programming! Conclusions: These lessons only scratch the surface
! See the full courses for complete and pedagogical sets of concepts
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 13/261
10/10/2005 P. Van Roy, CIESC tutorials 13
Lectures in French! There is another set of course slides in French
! The French slides are the most recent ones! I am teaching a second-year course in Fall 2005, namely
FSAB1402, to 350 engineering students! See http://www.info.ucl.ac.be/notes_de_cours/FSAB1402/
! See also http://www.info.ucl.ac.be/people/PVR/coursfrancais.html
! FSAB1402 covers a lot of ground
! My CIESC tutorials are not based on the French slides but on theEnglish slides given in the courses CS2104 (NUS), DatalogiII(KTH), and LINF1251 (UCL) by Seif Haridi, Christian Schulte,and Peter Van Roy
! But I recommend you take a look at the French slides!
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 14/261
10/10/2005 P. Van Roy, CIESC tutorials 14
The Textbook
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 15/261
10/10/2005 P. Van Roy, CIESC tutorials 15
The textbook! We have written a textbook to support the
concepts-based approach! “Concepts, Techniques, and Models of
Computer Programming”, MIT Press, 2004, 929pages
! The textbook is based on more than a decade of research by an international group, the MozartConsortium
! Goals of the textbook! To present programming as a unified discipline
in which each programming paradigm has itspart
! To teach programming without the limitations of particular languages and their historicalaccidents of syntax and semantics
! All the examples and courses we will give arepart of the textbook! A single 2-semester-hour course covers about
1/3 of the book
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 16/261
10/10/2005 P. Van Roy, CIESC tutorials 16
Lesson 1:Hands-on Introduction
to Programming Concepts
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 17/261
10/10/2005 P. Van Roy, CIESC tutorials 17
Lesson 1! Hands-on introduction to programming
concepts
! This is one way to get the class used to theenvironment and also to introduceprogramming concepts, to give the studentssome intuitions to start the course
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 18/261
10/10/2005 P. Van Roy, CIESC tutorials 18
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
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 19/261
10/10/2005 P. Van Roy, CIESC tutorials 19
A calculator ! Use the Mozart system as a
calculator
{Browse 9999*9999}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 20/261
10/10/2005 P. Van Roy, CIESC tutorials 20
Variables! Variables are short-cuts for values, they cannot be assigned
more than once
declare
V = 9999*9999{Browse V*V}
! Variable identifier is what you type
! Variable in memory (store variable) is what is used in the
calculation
! The declare statement creates a store variable and assignsits memory address to the identifier ’V’ in the environmen
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 21/261
10/10/2005 P. Van Roy, CIESC tutorials 21
Functions
! Compute the factorial function:
! Start with the mathematical definition
declare
fun {Fact N}
if N==0 then 1 else N*{Fact N-1} end
end
! Fact is declared in the environment
! Try large factorial {Browse {Fact 100}}
nnn !"!!!= )1(21! L
0if )!1(!
1!0
>!"=
=
nnnn
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 22/261
10/10/2005 P. Van Roy, CIESC tutorials 22
Composing functions! Combinations of r items taken from n.
! The number of subsets of size r taken from a set of size n
)!(!
!
r nr
n
r
n
!=
"#
$
%&
'
declare
fun {Comb N R}
{Fact N} div ({Fact R}*{Fact N-R})
end
• Example of functional abstraction
Comb
Fact Fact Fact
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 23/261
10/10/2005 P. Van Roy, CIESC tutorials 23
Structured data (lists)! Calculate Pascal triangle
! Write a function that calculates the nthrow as one structured value
! A list is a sequence of elements:
[1 4 6 4 1]
! The empty list is written nil
! Lists are created by means of ”|” (cons)
declare
H=1T = [2 3 4 5]
{Browse H|T} % Shows [1 2 3 4 5]
1
11
1 2 11 3 3 1
1 4 6 4 1
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 24/261
10/10/2005 P. Van Roy, CIESC tutorials 24
More on lists
! Taking lists apart (selecting components)
!
A cons has two components a head, and taildeclare L = [5 6 7 8]
L.1 gives 5
L.2 give [6 7 8]
‘|’
‘|’
‘|’
6
7
8 nil
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 25/261
10/10/2005 P. Van Roy, CIESC tutorials 25
Pattern matching
! Another way to take a list apart is by use of
pattern matching with a case instruction
case L of H|T then
{Browse H} {Browse T}
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 26/261
10/10/2005 P. Van Roy, CIESC tutorials 26
Functions over lists (1)! Compute the function
{Pascal N}
!
Takes an integer N, andreturns the Nth row of aPascal triangle as a list
1. For row 1, the result is [1]
2. For row N, shift to left row N-1
and shift to the right row N-13. Align and add the shifted rows
element-wise to get row N
1
11
1 2 1
1 3 3 1
1 4 6 4 1
(0) (0)
[0 1 3 3 1]
[1 3 3 1 0]
Shift right
Shift left
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 27/261
10/10/2005 P. Van Roy, CIESC tutorials 27
Functions over lists (2)
declare
fun {Pascal N}
if N==1 then [1]
else
{AddList
{ShiftLeft {Pascal N-1}}
{ShiftRight {Pascal N-1}}}
endend
AddList
ShiftLeft ShiftRight
Pascal N-1 Pascal N-1
Pascal N
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 28/261
10/10/2005 P. Van Roy, CIESC tutorials 28
Functions over lists (3)
fun {ShiftLeft L}
case L of H|T then
H|{ShiftLeft T} else [0]
end
end
fun {ShiftRight L} 0|L end
fun {AddList L1 L2}
case L1 of H1|T1 then
case L2 of H2|T2 then H1+H2|{AddList T1 T2}
end
else nil end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 29/261
10/10/2005 P. Van Roy, CIESC tutorials 29
Top-down programdevelopment
! Understand how to solve the problem by hand
! Try to solve the task by decomposing it to simpler tasks
! Devise the main function (main task) in terms of suitable auxiliary functions (subtasks) thatsimplifies the solution (ShiftLeft, ShiftRight, and AddList)
! Complete the solution by writing the auxiliaryfunctions
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 30/261
10/10/2005 P. Van Roy, CIESC tutorials 30
Is your program correct?
! ”A program is correct when it does what we wouldlike it to do”
! In general we need to reason about the program:
! Semantics of the language: a precise model of theoperations of the programming language
! Program specification: a definition of the output interms of the input (usually a mathematical functionor relation)
! Use mathematical techniques to reason about theprogram, using programming language semantics
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 31/261
10/10/2005 P. Van Roy, CIESC tutorials 31
Mathematical induction
! Select one or more input to the function
! Show the program is correct for the simple cases(base case)
! Show that if the program is correct for a given case,it is then correct for the next case.
! For integers base case is either 0 or 1, and for anyinteger n the next case is n+1
!
For lists the base case is nil, or a list with one or fewelements, and for any list T the next case H|T
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 32/261
10/10/2005 P. Van Roy, CIESC tutorials 32
Correctness of factorial
fun {Fact N}
if N==0 then 1 else N*{Fact N-1} endend
! Base Case: {Fact 0} returns 1
! Inductive Case: (N>1), N*{Fact N-1} assume {Fact
N-1} is correct, from the spec we see that {Fact N} isN*{Fact N-1}
! More techniques to come!
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 33/261
10/10/2005 P. Van Roy, CIESC tutorials 33
Complexity! Pascal runs very slow,
try {Pascal 24}
! {Pascal 20} calls: {Pascal 19} twice,{Pascal 18} four times, {Pascal 17}
eight times, ..., {Pascal 1} 219
times! Execution time of a program up to a
constant factor is called program’stime complexity .
! Time complexity of {Pascal N} is
proportional to 2N (exponential)! Programs with exponential time
complexity are impractical
declare
fun {Pascal N}
if N==1 then [1]
else
{AddList
{ShiftLeft {Pascal N-1}}
{ShiftRight {Pascal N-1}}}
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 34/261
10/10/2005 P. Van Roy, CIESC tutorials 34
fun {FastPascal N}
if N==1 then [1]
else
local L in
L={FastPascal N-1}
{AddList {ShiftLeft L}{ShiftRight L}}
end
end
end
Faster Pascal! Introduce a local variable L
! Compute {FastPascal N-1}only once
! Try with 30 rows.
! FastPascal is called N times,each time a list on the averageof size N/2 is processed
! The time complexity isproportional to N2 (polynomial)
! Low order polynomialprograms are practical.
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 35/261
10/10/2005 P. Van Roy, CIESC tutorials 35
Lazy evaluation (1)
! The functions written so far are evaluated eagerly(as soon as they are called)
! Another way is lazy evaluation where a computation
is done only when the results is needed
declarefun lazy {Ints N}
N|{Ints N+1}end
• Calculates the infinite list:0 | 1 | 2 | 3 | ...
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 36/261
10/10/2005 P. Van Roy, CIESC tutorials 36
Lazy evaluation (2)
! Write a function thatcomputes as many rows of Pascal’s triangle as needed
!
We do not know how manybeforehand
! A function is lazy if it isevaluated only when its resultis needed
! The function PascalList isevaluated when needed
fun lazy {PascalList Row}
Row | {PascalList
{AddList
{ShiftLeft Row}
{ShiftRight Row}}}
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 37/261
10/10/2005 P. Van Roy, CIESC tutorials 37
Lazy evaluation (3)
! Lazy evaluation willavoid redoing work if you decide first youneed the 10th row andlater the 11th row
! The function continues
where it left off
declare
L = {PascalList [1]}
{Browse L}
{Browse L.1}
{Browse L.2.1}
L<Future>
[1]
[1 1]
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 38/261
10/10/2005 P. Van Roy, CIESC tutorials 38
Higher-order programming
! Assume we want to write another Pascal function whichinstead of adding numbers performs exclusive-or on them
! It calculates for each number whether it is odd or even (parity)
! Either write a new function each time we need a newoperation, or write one generic function that takes anoperation (another function) as argument
! The ability to pass functions as argument, or return a functionas result is called higher-order programming
! Higher-order programming is an aid to build genericabstractions
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 39/261
10/10/2005 P. Van Roy, CIESC tutorials 39
Variations of Pascal
! Compute the parity Pascal triangle
111
1 2 1
1 3 3 1
1 4 6 4 1
111
1 0 1
1 1 1 1
1 0 0 0 1
fun {Xor X Y} if X==Y then 0 else 1 end end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 40/261
10/10/2005 P. Van Roy, CIESC tutorials 40
Higher-order programmingfun {GenericPascal Op N}
if N==1 then [1]
else L in L = {GenericPascal Op N-1}
{OpList Op {ShiftLeft L} {ShiftRight L}}
end
endfun {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
fun {Add N1 N2} N1+N2 end
fun {Xor N1 N2}
if N1==N2 then 0 else 1 end
end
fun {Pascal N} {GenericPascal Add N}
end
fun {ParityPascal N}
{GenericPascal Xor N}
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 41/261
10/10/2005 P. Van Roy, CIESC tutorials 41
Concurrency
! How to do several things at once
! Concurrency: running severalactivities each running at its ownpace
! A thread is an executing sequentialprogram
! A program can have multiplethreads by using the threadinstruction
! {Browse 99*99} can immediatelyrespond while Pascal is computing
thread
P in
P = {Pascal 21}
{Browse P}
end{Browse 99*99}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 42/261
10/10/2005 P. Van Roy, CIESC tutorials 42
Dataflow (1)
! What happens when multiple threadstry to communicate?
! A simple way is to makecommunicating threads synchronize on
the availability of data (data-drivenexecution)
! If an operation tries to use a variablethat is not yet bound it will wait
! The variable is called a dataflow
variable +
* *
X Y Z U
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 43/261
10/10/2005 P. Van Roy, CIESC tutorials 43
Dataflow (2)
! Two important properties of dataflow! Calculations work correctly
independent of how they are
partitioned between threads(concurrent activities)
! Calculations are patient, they donot signal error; they wait for data availability
! The dataflow property of
variables makes sense whenprograms are composed of multiple threads
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
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 44/261
10/10/2005 P. Van Roy, CIESC tutorials 44
State! How to make a function learn from its past?
! We would like to add memory to a function toremember past results
! Adding memory as well as concurrency is anessential aspect of modeling the real world
!
Consider {FastPascal N}: we would like it toremember the previous rows it calculated inorder to avoid recalculating them
! We need a concept (a memory cell) to store,change and retrieve a value
! The simplest concept is a (memory) cell whichis a container of a value
! One can create a cell, assign a value to a cell,and access the current value of the cell
! Cells are not variables!
declare
C = {NewCell 0}
C:= @C + 1
{Browse @C}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 45/261
10/10/2005 P. Van Roy, CIESC tutorials 45
Example of state
! Add memory toPascal to remember how many times it iscalled
! The memory (state)is global here
! Memory that is localto a function is calledencapsulated state
declare
C = {NewCell 0}
fun {FastPascal N}
C:=@C+1{GenericPascal Add N}
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 46/261
10/10/2005 P. Van Roy, CIESC tutorials 46
Objects
! Functions with internalmemory are called objects
! The cell is invisible outside of
the definition
declare
local C in
C = {NewCell 0}
fun {Bump} C:=@C+1
@C
end
end
declare
fun {FastPascal N}
{Browse {Bump}}
{GenericPascal Add N}end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 47/261
10/10/2005 P. Van Roy, CIESC tutorials 47
Classes (1)
! A class is a ’factory’of objects whereeach object has itsown internal state
! Let us create manyindependent counter
objects with thesame behavior
fun {NewCounter}
local C Bump in
C = {NewCell 0}
fun {Bump}
C:=@C+1
@C
end
Bump
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 48/261
10/10/2005 P. Van Roy, CIESC tutorials 48
Classes (2)
! Here is a class withtwo operations:
Bump and Read
fun {NewCounter}
local C Bump in
C = {NewCell 0}
fun {Bump}
C:=@C+1 @C
end
fun {Read}
@C
end
[Bump Read]
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 49/261
10/10/2005 P. Van Roy, CIESC tutorials 49
Object-oriented programming
! In object-oriented programming the idea of objects and classes is pushed farther
! Classes keep the basic properties of:
! State encapsulation! Object factories
! Classes are extended with more sophisticatedproperties:! They have multiple operations (called methods)
! Methods can be called by their names (giving polymorphism)
! They can be defined by taking another class andextending it slightly (inheritance)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 50/261
10/10/2005 P. Van Roy, CIESC tutorials 50
Nondeterminism (1)
! What happens if a program has bothconcurrency and state together?
! This is very tricky
! The same program can give differentresults from one execution to the next
! This variability is called nondeterminism
! Internal nondeterminism is not a problem if it is not observable from outside
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 51/261
10/10/2005 P. Van Roy, CIESC tutorials 51
Nondeterminism (2)
declare
C = {NewCell 0}
thread C:=1 end
thread C:=2 end
time
C = {NewCell 0}cell C contains 0
C:=1cell C contains 1
C:=2cell C contains 2 (final value)
t0
t1
t2
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 52/261
10/10/2005 P. Van Roy, CIESC tutorials 52
Nondeterminism (3)
declare
C = {NewCell 0}
thread C:=1 end
thread C:=2 end
time
C = {NewCell 0}cell C contains 0
C:=2cell C contains 2
C:=1cell C contains 1 (final value)
t0
t1
t2
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 53/261
10/10/2005 P. Van Roy, CIESC tutorials 53
Nondeterminism (4)
declare
C = {NewCell 0}
thread I inI = @C
C:= I+1
end
thread J in
J = @CC:= J+1
end
! What are the possibleresults?
!
Both threadsincrement the cell Cby 1
! Expected final result
of C is 2! Is that all?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 54/261
10/10/2005 P. Van Roy, CIESC tutorials 54
Nondeterminism (5)
! Another possible final result is thecell C containing the value 1
declare
C = {NewCell 0}
thread I in
I = @C
C:= I+1
end
thread J in
J = @C
C:= J+1
endtime
C = {NewCell 0}
I = @C
I equal 0
t0
t1
t2 J = @C
J equal 0
C:= J+1
C contains 1
C:= I+1C contains 1
t3
t4
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 55/261
10/10/2005 P. Van Roy, CIESC tutorials 55
Insights gained
! Combining concurrency and state is tricky
! Complex programs have many possible interleavings
! Programming is a question of mastering the interleavings
! Famous bugs in the history of computer technology are dueto designers overlooking an interleaving (e.g., the Therac-25radiation therapy machine giving doses 1000’s of times toohigh, resulting in death or injury)
1. If possible try to avoid concurrency and state together
2. Encapsulate state and communicate between threads usingdataflow
3. Try to master interleavings by using atomic operations
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 56/261
10/10/2005 P. Van Roy, CIESC tutorials 56
Atomicity (1)! How can we master the interleavings?
! One idea is to reduce the number of interleavingsby programming with coarse-grained atomicoperations
! An operation is atomic if it is performed as awhole or nothing
! No intermediate (partial) results can be observedby any other concurrent activity
! In simple cases we can use a lock to ensureatomicity of a sequence of operations
! For this we need a new entity (a lock)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 57/261
10/10/2005 P. Van Roy, CIESC tutorials 57
Atomicity (2)
declare
L = {NewLock}
lock L then
sequence of ops 1
end
Thread 1
lock L then
sequence of ops 2
end
Thread 2
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 58/261
10/10/2005 P. Van Roy, CIESC tutorials 58
Using atomicitydeclareC = {NewCell 0}L = {NewLock}
threadlock L then I in
I = @C C:= I+1end
endthread
lock L then J in J = @C
C:= J+1endend
The final content of C isalways 2
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 59/261
10/10/2005 P. Van Roy, CIESC tutorials 59
Lesson 2:Language Syntax
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 60/261
10/10/2005 P. Van Roy, CIESC tutorials 60
Description of a language
! Language = Syntax + Semantics
! The syntax of a language is concerned with
the form of a program: how expressions,commands, declarations etc. are put together to result in the final program.
! The semantics of a language is concerned
with the meaning of a program: how theprograms behave when executed oncomputers.
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 61/261
10/10/2005 P. Van Roy, CIESC tutorials 61
Programming language definition
! Syntax: grammatical structure! lexical how words are formed
! phrasal how sentences are formed from words
! Semantics: meaning of programs! Informal: English documents (e.g. Reference manuals,
language tutorials and FAQs etc.)
! Formal:" Operational Semantics (execution on an abstract machine)
" Denotational Semantics (each construct defines a function)
" Axiomatic Semantics (each construct is defined by pre andpost conditions)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 62/261
10/10/2005 P. Van Roy, CIESC tutorials 62
Language syntax (1)
! Defines legal programs! programs that can be executed by machine
! Defined by grammar rules! define how to make ‘sentences’ out of ‘words’
! For programming languages! sentences are called statements (commands,
expressions)
! words are called tokens
! grammar rules describe both tokens and statements
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 63/261
10/10/2005 P. Van Roy, CIESC tutorials 63
Language syntax (2)! Statement is sequence of tokens
! Token is sequence of characters
! Lexical analyzer is a program! recognizes character sequence
! produces token sequence
! Parser is a program! recognizes token sequence
! produces statement representation
! Statements are represented as parse trees
Lexical analyzer
Parser
characters
tokens
sentences
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 64/261
10/10/2005 P. Van Roy, CIESC tutorials 64
Backus-Naur Form
! BNF (Backus-Naur Form) is a common notation to definegrammars for programming languages
! A BNF grammar is set of grammar (rewriting) rules !
" A set of terminal symbols#
(tokens)
! A set of Non-terminal symbols $
! One start symbol %
! A grammar rule
&nonterminal' ::= &sequence of terminal and nonterminal'
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 65/261
10/10/2005 P. Van Roy, CIESC tutorials 65
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
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 66/261
10/10/2005 P. Van Roy, CIESC tutorials 66
Grammar rules
" &digit' is defined to represent one of the ten
tokens 0, 1, …, 9&digit' ::= 0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9
" The symbol ‘|’ is read as ‘or’
" Another reading is that &digit' describes theset of tokens {0,1,…, 9}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 67/261
10/10/2005 P. Van Roy, CIESC tutorials 67
Examples of BNF (1)
(A) BNF rules for robot commands
! A robot arm only accepts a command from {up, down,left, right}
&move' ::= &cmd | &cmd' &move' &cmd' ::= up | down | left | right
! Examples of command sequences :
! up
! down left
! up down down up right left
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 68/261
10/10/2005 P. Van Roy, CIESC tutorials 68
Examples of BNF (2)
! 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 &digit'’s
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 69/261
10/10/2005 P. Van Roy, CIESC tutorials 69
Extended Backus-Naur Form
! EBNF (Extended Backus-Naur Form) is acommon notation to define grammars for programming languages
! Terminal symbols and non-terminal symbols
! Terminal symbol is a token
! Nonterminal symbol is a sequence of tokens,
and is represented by a grammar rule&nonterminal' ::= &rule body'
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 70/261
10/10/2005 P. Van Roy, CIESC tutorials 70
Grammar rules
" Grammar rules may refer to other nonterminals
&integer ' ::= &digit' { &digit' }
" &integer ' is defined as the sequence of a&digit' followed by zero or more &digit'’s
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 71/261
10/10/2005 P. Van Roy, CIESC tutorials 71
Grammar rule constructs
" & x ' nonterminal x
" & x ' ::= Body & x ' is defined by Body
" & x ' | &y ' either & x ' or &y ' (choice)
" & x ' &y ' the sequence & x ' followed by &y '" { & x ' } sequence of zero or more
occurrences of & x '
" { & x ' }+ sequence of one or more
occurrences of & x '" [ & x ' ] zero or one occurrence of & x '
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 72/261
10/10/2005 P. Van Roy, CIESC tutorials 72
How to read grammar rules
" From left to right
" Gives the following sequence
" each terminal symbol is added to the sequence" each nonterminal is replaced by its definition
" for each & x ' | &y ' pick any of the alternatives
" for each & x ' &y ' is the sequence & x ' followed by thesequence &y '
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 73/261
10/10/2005 P. Van Roy, CIESC tutorials 73
Examples
" &statement' ::= skip | &expression' ‘=‘ &expression' | …
" &expression' ::= &variable' | &integer ' | …
" &statement' ::= if &expression' then &statement' { elseif &expression' then &statement' } [ else &statement' ] end | …
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 74/261
10/10/2005 P. Van Roy, CIESC tutorials 74
Context-Free Grammars (1)
! Grammar rules can be used to
! verify that a statement is legal
! generate all possible statements
!
The set of all possible statements generated froma grammar and one nonterminal symbol is called a(formal ) language
! EBNF notation defines essentially a class of grammars called context -free grammars
! Expansion of a nonterminal is always the sameregardless of where it is used
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 75/261
10/10/2005 P. Van Roy, CIESC tutorials 75
Context-Free Grammars (2)
Example 1:! Let $ = {&a'}, T = {0,1}
! = {&a' ::= 11a0, &a' ::= 110}, % = &a'
110 ( L(G)
a
110
2nd rule
111100 ( L(G)
a
a
1st rule
11 0
110
2nd rule
But 011 ) L(G)
a
???
011
These trees are called parse trees or syntax trees
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 76/261
10/10/2005 P. Van Roy, CIESC tutorials 76
More examples of EBNF
(C) BNF rules for Real Numbers;
<real-#> ::= <int-part > . < fraction>
<int-part> ::= <digit > | <int-part > <digit >
< fraction> ::= <digit > | <digit > < fraction><digit > ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<real-#>
<int-part> <fraction>.
<digit><int-part>
<digit>
1
3 7
<digit> <fraction>
<digit>
9
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 77/261
10/10/2005 P. Van Roy, CIESC tutorials 77
Ambiguity (1)!
A grammar is ambiguous if there exists a stringwhich gives rise to more than one parse tree.
! Most common cause is due to infix binary operation
&expr' ::= &num'|&expr'’-’&expr'
Parse: 1-2-3
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 78/261
10/10/2005 P. Van Roy, CIESC tutorials 78
Ambiguity (2)
&expr' ::= &num'|&expr'’-’&expr'Parse: (1-2)-3
&expr '
&num'
&num'&expr ' ‘-’ &expr '
&expr ' ‘-’ &expr '
&num'
3
21
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 79/261
10/10/2005 P. Van Roy, CIESC tutorials 79
Ambiguity (3)
&expr' ::= &num'|&expr'’-’&expr'Parse: 1-(2-3)
&expr '
‘-’
&expr '
‘-’&expr '
&num'
3
&num'
&expr '
2
&num'
&expr '
1
Which parse tree?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 80/261
10/10/2005 P. Van Roy, CIESC tutorials 80
Ambiguity resolutionfor binary operators (1)
! (A) Associative Rules
Given a binary operator ‘op’ and a stringa1 ‘op’ a2 ‘op’ a3
! Ifa1 ‘op’ a2 ‘op’ a3 is interpreted as (a1 ‘op’ a2) ‘op’ a3,
then ‘op’ is left associative.
! Ifa1 ‘op’ a2 ‘op’ a3 is interpreted as a1 ‘op’ (a2 ‘op’ a3),then ‘op’ is right associative.
! It is possible that ‘op’ is neither left nor right
associative. In which case a1 ‘op’ a2 ‘op’ a3 will betreated as a syntax error.
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 81/261
10/10/2005 P. Van Roy, CIESC tutorials 81
Ambiguity resolutionfor binary operators (2)
! Example: We have seen that this BNF is ambiguous:
&expr' ::= &num' | &expr' – &expr'
To make it unambiguous, I want the ‘–’ to be…! Left associative:
&expr' ::= &num' | &expr' – &num'
! Right Associative: &expr' ::= &num' | &num' – &expr'
Ambiguity resolution
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 82/261
10/10/2005 P. Van Roy, CIESC tutorials 82
Ambiguity resolutionfor binary operators (3)
! (B) Precedence Rules
Given two different binary operators ‘op1’ and ‘op2’
a1 ‘op1’ a2 ‘op2’ a3
! If a1 ‘op1’ a2 ‘op2’ a3 is interpreted as (a1 ‘op1’ a2) ‘op2’ a3,then op1 has a higher precedence than op2.
!
Ifa1 ‘op’ a2 ‘op’ a3 is interpreted as a1 ‘op1’ (a2 ‘op2’ a3),then op2 has a higher precedence than op1.
Ambiguity resolution
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 83/261
10/10/2005 P. Van Roy, CIESC tutorials 83
Ambiguity resolutionfor binary operators (4)
! Example: This BNF is ambiguous:
<expr> ::= <num> | <expr> + <expr> | <expr> * <expr>
<expr ><expr
>* <expr
><expr
>+ <expr
><num
><num
><num
>
1 2
3
<expr ><expr
>+ <expr
><expr
>* <expr
><num
><num
><num
>
2 3
1
(1+2)*3 1+(2*3)
Which One?
1+2*3
Ambiguity resolution
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 84/261
10/10/2005 P. Van Roy, CIESC tutorials 84
Ambiguity resolutionfor binary operators (5)
1+2*3
<expr >
<expr 2>
* <expr >
<num> + <expr
2><num
>1
2
<expr 2>
<num>
3(1+2)*3
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>
Ambiguity resolution
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 85/261
10/10/2005 P. Van Roy, CIESC tutorials 85
Ambiguity resolutionfor 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 >
<expr 2>
+ <expr >
<num>
* <expr 2>
2
<num>3
<expr
2>
<num
>
1
1+(2*3)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 86/261
10/10/2005 P. Van Roy, CIESC tutorials 86
Ambiguity of operators
! For binary operators, we have to specify
! the associativity of the operators, and
! The precedence of the operators
! Alternatively, rewrite the grammar rules to get ridof ambiguity
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 87/261
10/10/2005 P. Van Roy, CIESC tutorials 87
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>)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 88/261
10/10/2005 P. Van Roy, CIESC tutorials 88
Ambiguity (Dangling-Else Ambiguity)
! Ambiguity in general
! Ambiguous grammar is NOT restricted to just binary operation:
! Example:
<S> ::= if <E> then <S>
| if <E> then <S> else <S>
! String: if <E 1> then if <E 2 > then <S1> else <S2 >
! Parse Tree???
<S>
if <E 1> then<S>
if <E 2 > then<S1> else <S2 >
<S>
<S>if <E 1>then else <S2 >
if <E 2 > then <S1>
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 89/261
10/10/2005 P. Van Roy, CIESC tutorials 89
Context-sensitive grammars
! For practical languages context-free grammar isnot enough
!
A condition on context is sometimes added! for example: identifier must be declared before use
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 90/261
10/10/2005 P. Van Roy, CIESC tutorials 90
Context-free andcontext-sensitive grammars
! Easy to read andunderstand
! Defines superset of language
! Expresses restrictionsimposed by language
! Renders grammar rulescontext sensitive
Context-free grammar (e.g. with EBNF)
+
Set of extra conditions
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 91/261
10/10/2005 P. Van Roy, CIESC tutorials 91
Lesson 3:
Introduction toLanguage Semantics
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 92/261
10/10/2005 P. Van Roy, CIESC tutorials 92
Language semantics
! Defines what a program does when executed
! Goals! simple
! allow programmer to reason about program(correctness, execution time, and memory use)
! How to achieve for a practical language usedto build complex systems (millions lines of code)?
! The kernel language approach
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 93/261
10/10/2005 P. Van Roy, CIESC tutorials 93
Kerne language approach (1)
! Define simple language (kernel language)
! Define its computation model! how language constructs (statements) manipulate
(create and transform) data structures
! Define mapping scheme (translation) of fullprogramming language into kernel language
!
Two kinds of translations! linguistic abstractions
! syntactic sugar
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 94/261
10/10/2005 P. Van Roy, CIESC tutorials 94
Kernel language approach (2)
practical language
kernel language
translation
fun {Sqr X} X*X end
B = {Sqr {Sqr A}}
proc {Sqr X Y}
{ * X X Y}
end
local T in
{Sqr A T}
{Sqr T B}
end
• Provides useful abstractions for programmer • Can be extended with linguistic abstractions
• Easy to understand and reason with• Has a precise (formal) semantics
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 95/261
10/10/2005 P. Van Roy, CIESC tutorials 95
Linguistic abstractions syntacticsugar (1)
! Linguistic abstractions provide higher level concepts! programmer uses to model and reason about programs
(systems)
! examples: functions (fun), iterations (for), classes and objects(class)
! Functions (calls) are translated to procedures (calls)
! Translation answers questions about functions, for example,what does this expression do: {F1 {F2 X} {F3 X}}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 96/261
10/10/2005 P. Van Roy, CIESC tutorials 96
Linguistic abstractions syntacticsugar (2)
! Linguistic abstractions:
provide higher level concepts
! Syntactic sugar :
short cuts and conveniences toimprove readability
if N==1 then [1]else local L in …
endend
if N==1 then [1]else L in …end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 97/261
10/10/2005 P. Van Roy, CIESC tutorials 97
Approaches to semantics
Programming Language
Kernel Language
Operational model
Formal calculus Abstract machine
Aid programmer
in reasoning and
understanding
Our goal!
Aid theoretician in mathematical study of programming: " -calculus,
predicate calculus, * -calculus
Aid implementor
Efficient execution on
a real machine
S ti l d l ti
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 98/261
10/10/2005 P. Van Roy, CIESC tutorials 98
Sequential declarativecomputation model (first model)
! Single assignment store
! declarative (dataflow) variables and values (together called entities)
! values and their types
! Kernel language syntax
! Environment ! maps textual variable names (variable identifiers) into entities in the store
! Execution of kernel language statements
! execution stack of statements (defines control)
! store
! transforms store by sequence of steps
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 99/261
10/10/2005 P. Van Roy, CIESC tutorials 99
Lexical scoping (1)
! Lexical scoping tells us for each identifier occurrence, exactlywhich declaration the occurrence corresponds to
! Let us first define free and bound identifier occurrences
! An identifier occurrence is bound with respect to a statement
&s' if it is in the scope of a declaration inside &s'" A variable identifier is declared either by a ‘local’ statement,
as a parameter of a procedure, or implicitly declared by acase statement
" An identifier occurrence is free otherwise
" In a running program every identifier is bound (i.e., declared)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 100/261
10/10/2005 P. Van Roy, CIESC tutorials 100
Lexical scoping (2)
! proc {PP X}local Y in YY = 1 {Browse YY} endX = YY
end
Bound OccurrencesBound OccurrencesFree OccurrencesFree Occurrences
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 101/261
10/10/2005 P. Van Roy, CIESC tutorials 101
Lexical scoping (3)
! local Arg1 Arg2 in Arg1 = 111*111 Arg2 = 999*999
ResRes = Arg1*Arg2end
BoundBound Occurrences: Arg1 and Arg2 in theOccurrences: Arg1 and Arg2 in the
body of the body of the locallocal statementstatement
FreeFree OccurrenceOccurrence
This is not a runnable program!
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 102/261
10/10/2005 P. Van Roy, CIESC tutorials 102
Lexical scoping (4)
! local Res inlocal Arg1 Arg2 in
Arg1 = 111*111
Arg2 = 999*999ResRes = Arg1*Arg2end{Browse Res}
end
This is a runnable program (there are no free identifier occurrences) !
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 103/261
10/10/2005 P. Van Roy, CIESC tutorials 103
Lexical scoping (5)
local P Q inproc {P} {QQ} endproc {QQ} {Browse hello} end
local Q inproc {Q} {Browse hi} end{P}
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 104/261
10/10/2005 P. Van Roy, CIESC tutorials 104
Procedure abstraction
! Any statement can be abstracted to a procedure by selectingsome of the free variable identifier occurrences and enclosingthe statement into a procedure with the identifiers asparamenters
!
if X >= Y then Z = X else Z = Y end! Abstracting over all variables
proc {Max X Y Z}if X >= Y then Z = X else Z = Y end
end
! Abstracting over just X and Z
proc {LowerBound X Z}if X >= Y then Z = X else Z = Y endend
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 105/261
10/10/2005 P. Van Roy, CIESC tutorials 105
Procedure values (1)
! What do procedure values look like in memory (in the store)?
! Constructing a procedure value in the store is not simplebecause a procedure may have external references
local P Q in
P = proc {$} {QQ} endQQ = proc {$} {Browse hello} endlocal Q in
Q = proc {$} {Browse hi} end{P}
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 106/261
10/10/2005 P. Van Roy, CIESC tutorials 106
Procedure values (2)
local P Q inP = proc {$} {QQ} endQQ = proc {$} {Browse hello} endlocal Q in Q = proc {$} {Browse hi} end
{P}
endend
x1( , )
proc {$} {QQ} end Q + x
2
x2( , )
proc {$} {Browse hello} end Browse + x0
P
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 107/261
10/10/2005 P. Van Roy, CIESC tutorials 107
Our roadmap to semantics
! Single assignment store
! Kernel language syntax
!
Values and types! Environments
! Statements and statement stack
! Execution steps
L 4
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 108/261
10/10/2005 P. Van Roy, CIESC tutorials 108
Lesson 4:
DeclarativeProgramming
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 109/261
10/10/2005 P. Van Roy, CIESC tutorials 109
Declarative operations (1)
! An operation is declarative if whenever it is called with thesame arguments, it returns the same results independent of any other computation state
! A declarative operation is:!
Independent (depends only on its arguments, nothing else)! Stateless (no internal state is remembered between calls)
! Deterministic (call with same operations always give sameresults)
! Declarative operations can be composed together to yieldother declarative components! All basic operations of the declarative model are declarative and
combining them always gives declarative components
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 110/261
10/10/2005 P. Van Roy, CIESC tutorials 110
Declarativeoperation
Arguments
Results
Declarative operations (2)
rest of computation
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 111/261
10/10/2005 P. Van Roy, CIESC tutorials 111
Why declarative components (1)
! There are two reasons why they are important:
! (Programming in the large) A declarative component can be written,tested, and proved correct independent of other components and of its own past history.
! The complexity (reasoning complexity) of a program composed of declarative components is the sum of the complexity of the components
! In general the reasoning complexity of programs that are composed of nondeclarative components explodes because of the intimateinteraction between components
! (Programming in the small) Programs written in the declarativemodel are much easier to reason about than programs written inmore expressive models (e.g., an object-oriented model).
! Simple algebraic and logical reasoning techniques can be used
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 112/261
10/10/2005 P. Van Roy, CIESC tutorials 112
Why declarative components (2)
! Since declarative components aremathematical functions, algebraicreasoning is possible i.e.substituting equals for equals
!
The declarative model of chapter 4guarantees that all programswritten are declarative
! Declarative components can bewritten in models that allow stateful
data types, but there is noguarantee
42
2
)(7
)(
)(
aba f b
a f
aa f
==
=
becomes
equation
other anyinreplacecanWe
Given
Classification of
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 113/261
10/10/2005 P. Van Roy, CIESC tutorials 113
Classification of declarative programming
Declarativeprogramming
Descriptive
Programmable
Observational
DefinitionalDeclarativemodel
Functionalprogramming
Nondeterministiclogic programming
Deterministiclogic programming
! The word declarative means many things tomany people. Let’s try to eliminate the
confusion.
! The basic intuition is to program by definingthe what without explaining the how
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 114/261
10/10/2005 P. Van Roy, CIESC tutorials 114
Descriptive language
&s' ::= skip empty statement
| & x ' = &y' variable-variable binding
| &x' = &record' variable-value binding
| &s1' &s2' sequential composition
| local &x' in &s1' end declaration
Other descriptive languages include HTML and XML
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 115/261
10/10/2005 P. Van Roy, CIESC tutorials 115
Descriptive language
<person id = ”530101-xxx”><name> Seif </name><age> 48 </age>
</person>
Other descriptive languages include HTML and XMLXML
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 116/261
10/10/2005 P. Van Roy, CIESC tutorials 116
Kernel language syntax
&s' ::= 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' ::= ... value expression
&pattern' ::= ...
The following defines the syntax of a statement, &s' denotes a statement
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 117/261
10/10/2005 P. Van Roy, CIESC tutorials 117
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' ::= true | false
&number ' ::= &int' | &float'
Why the kernel language is
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 118/261
10/10/2005 P. Van Roy, CIESC tutorials 118
y g gdeclarative
! All basic operations are declarative
! Given that the components (substatements) aredeclarative, we can see that! sequential composition! local statement
! procedure definition
! procedure call
! if statement
! try statement
are all declarative
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 119/261
10/10/2005 P. Van Roy, CIESC tutorials 119
Programming with lists
! Defining types
! Simple list functions
! Nth, Member, Append, Reverse, Length
! Mergesort
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 120/261
10/10/2005 P. Van Roy, CIESC tutorials 120
User defined data types
! A list is defined as a special subset of the record datatype
! A list Xs is either
! X|Xr where Xr is a list, or
! nil
! Other subsets of the record datatype are also useful, for example one may define a binary tree (btree) to be:
! node(key:K value:V left:LT right:RT) where LT and BT are binarytrees, or
! leaf
! This begs for a notation to concisely define subtypes of records
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 121/261
10/10/2005 P. Van Roy, CIESC tutorials 121
Defining types&list' ::= &value' | &list' [] nil
" defines a list type where the elements can be of any type
&list T' ::= T | &list' [] nil
" defines a type function that given the type of the parameter Treturns a type, e.g. &list &int' '
&btree T' ::= node(key: &literal' value:T left: &btree T' right: &btree T')
[] leaf(key: &literal' value:T)
" Procedure types are denoted by proc{T1 … Tn}
" Function types are denoted by fun{T1 … Tn}:T and is equivalentto proc{T1 … Tn T}
" Examples: fun{&list' &list' }: &list'
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 122/261
10/10/2005 P. Van Roy, CIESC tutorials 122
Lists
! General lists have the following definition&list T' ::= T | &list' [] nil
! The most useful elementary procedures on listscan be found in the Base module List of the Mozart
system
! Induction method on lists, assume we want toprove a property P(Xs) for all lists Xs
1. The Base: prove P(Xs) for Xs equals to nil, [X], and [X Y]
2. The Induction step: Assume P(Xs) hold, and proveP(X|Xs) for arbitrary X of type T
Constructive method for
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 123/261
10/10/2005 P. Van Roy, CIESC tutorials 123
Constructive method for programs on lists
! General lists have the following definition&list T' ::= T | &list T' [] nil
! The task is to write a program {Task Xs1 … Xsn}
1. Select one or more of the arguments Xsi
2. Construct the task for Xsi equals to nil, [X], and [XY]
3. The recursive step: assume {Task … Xsi …} isconstructed, and design the program for {Task …X|Xsi …} for arbitrary X of type T
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 124/261
10/10/2005 P. Van Roy, CIESC tutorials 124
Simple functions on lists
! Some of these functions exist in the library module List:
1. {Nth Xs N}, returns the Nth element of Xs
2. {Append Xs Ys}, returns a list which is theconcatenation of Xs followed by Ys
3. {Reverse Xs} returns the elements of Xs in a reverseorder, e.g. {Reverse [1 2 3]} is [3 2 1]
4. Sorting lists, MergeSort
5. Generic operations of lists, e.g. performing an operationon all the elements of a list, filtering a list with respect toa predicate P
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 125/261
10/10/2005 P. Van Roy, CIESC tutorials 125
The Nth function
! Define a function that gets the Nth element of a list
! Nth is of type fun{$ &list T' &int'}:&T' ,
" Reasoning: select N, two cases N=1, and N>1:
" N=1: {Nth Xs 1} , Xs.1
" N>1: assume we have the solution for {Nth Xr N-1} for asmaller list Xr, then {Nth X|Xr N} , {Nth Xr N-1}
fun {Nth Xs N}X|Xr = Xs in
if N==1 then X
elseif N>1 then {Nth Xr N-1} end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 126/261
10/10/2005 P. Van Roy, CIESC tutorials 126
The Nth functionfun {Nth Xs N}
X|Xr = Xs in
if N==1 then X
elseif N>1 then {Nth Xr N-1}
end
end
fun {Nth Xs N}if N==1 then Xs.1
elseif N>1 then {Nth Xs.2 N-1}
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 128/261
10/10/2005 P. Van Roy, CIESC tutorials 128
The Member function
! Member is of type fun{$ &value' &list &value''}:&bool' ,
fun {Member E Xs}
case Xs
of nil then false
[] X|Xr thenif X==E then true else {Member E Xr} end
end
end
" X==E orelse {Member E Xr} is equivalent to
" if X==E then true else {Member E Xr} end
" In the worst case, the whole list Xs is traversed, i.e., worstcase behavior is the length of Xs, and on average half of thelist
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 129/261
10/10/2005 P. Van Roy, CIESC tutorials 129
The Append function
fun {Append Xs Ys}
case Xs
of nil then Ys
[] X|Xr then X|{Append Xr Ys}
endend
! The inductive reasoning is on the first argument Xs
! Appending Xs and Ys is proportional to the length of the first list
! declare Xs0 = [1 2] Ys = [a b] Zs0 = {Append Xs0 Ys}
! Observe that Xs0, Ys0 and Zs0 exist after Append
! A new copy of Xs0, call it Xs0’, is constructed with an unboundvariable attached to the end: 1|2|X’, thereafter X’ is bound to Ys
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 130/261
10/10/2005 P. Van Roy, CIESC tutorials 130
The Append function
proc {Append Xs Ys Zs}
case Xs
of nil then Zs = Ys
[] X|Xr then Zr in
Zs = X|Zr {Append Xr Ys Zr}
end
end
! declare Xs0 = [1 2] Ys = [a b] Zs0 = {Append Xs0 Ys}
! Observe that Xs0, Ys and Zs0 exist after Append! A new copy of Xs0, call it Xs0’, is constructed with an unbound
variable attached to the end: 1|2|X’, thereafter X’ is bound to Ys
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 131/261
10/10/2005 P. Van Roy, CIESC tutorials 131
Append execution (overview)
Stack: [{Append 1|2|nil [a b] zs0}] Store: {zs0, ...}
Stack: [ {Append 2|nil [a b] zs1} ] Store: {zs0 = 1|zs1, zs1, ... }
Stack: [ {Append nil [a b] zs2} ] Store: {zs0 = 1|zs1, zs1=2|zs2, zs2, ...}
Stack: [ zs2 = [a b] ] Store: {zs0 = 1|zs1, zs1=2|zs2, zs2, ...}Stack: [ ] Store: {zs0 = 1|zs1, zs1=2|zs2, zs2= a|b|nil, ...}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 133/261
10/10/2005 P. Van Roy, CIESC tutorials 133
Length
fun {Length Xs}
case Xs
of nil then 0
[] X|Xr then 1+{Length Xr}
end
end
! Inductive reasoning on Xs
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 134/261
10/10/2005 P. Van Roy, CIESC tutorials 134
Merging two sorted lists
! Merging two sorted lists
! {Merge [3 5 10] [2 5 6]} , [2 3 5 5 6 10]
! fun{Merge &list T' &list T' }: &list T' , where T is either &int',&float', or &atom'
fun {Merge Xs Ys}case Xs # Ys
of nil # Ys then Ys
[] Xs # nil then Xs
[] (X|Xr) # (Y|Yr) then
if X =< Y then X|{Merge Xr Ys}else Y|{Merge Xs Yr} end
end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 135/261
10/10/2005 P. Van Roy, CIESC tutorials 135
Sorting with Mergesort
! {MergeSort &list T' }: &list T' ; T is either &int', &float',&atom'
" MergeSort uses a divide-and-conquer strategy
1. Split the list into two smaller lists of roughly equalsize
2. Use MergeSort (recursively) to sort the two smaller lists
3. Merge the two sorted lists to get the final result
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 136/261
10/10/2005 P. Van Roy, CIESC tutorials 136
Sorting with Mergesort
L
L1
L2
L11
L12
L21
L22
S11
S12
S21
S22
S
S1
S2
split
split
split merge
merge
merge
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 137/261
10/10/2005 P. Van Roy, CIESC tutorials 137
Sorting with Mergesort! {MergeSort &list T' }: &list T' ; T is either &int', &float', &atom'" MergeSort uses a divide-and-conquer strategy
1. Split the list into two smaller lists of roughly equal size
2. Use MergeSort (recursively) to sort the two smaller lists
3. Merge the two sorted lists to get the final result
fun {MergeSort Xs}case Xsof nil then nil[] [X] then Xselse Ys Zs in
{Split Xs Ys Zs}{Merge {MergeSort Ys} {MergeSort Zs}}end
end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 138/261
10/10/2005 P. Van Roy, CIESC tutorials 138
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
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 139/261
10/10/2005 P. Van Roy, CIESC tutorials 139
Lesson 5:Stateful Programming
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 141/261
10/10/2005 P. Van Roy, CIESC tutorials 141
Explicit state (1)
State as a groupof memory cells
• The box O can remember
information between
independent invocations,
it has a memory• The basic elements of
explicit state
• Index datatypes
• Basic techniques andideas of using state in
program design
Group of functions and
procedures that operate
on the state
An Interface that hides
the state
The box O
E li it t t (2)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 142/261
10/10/2005 P. Van Roy, CIESC tutorials 142
Explicit state (2)
State as a groupof memory cells
• What is the difference
between implicit state
and explicit state
• What is the difference between state in general
and encapsulate state
• Component based
programming and object-
oriented programming
• Abstract data types using
encapsulated state
Group of functions and
procedures that operate
on the state
An Interface that hides
the state
The box O
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 145/261
What is explicit state:E l ?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 146/261
10/10/2005 P. Van Roy, CIESC tutorials 146
Example?
XAn unbound
variable
XA cell C is createdwith initialvalue 5
X is bound to C
5
X
The cell C, which X is
bound to, is assignedthe value 6
6
C
C
• The cell is a value
container with a unique
identity
• X is really bound to
the identity of the cell
• When the cell is
assigned, X does not
change
Wh t i li it t t ?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 147/261
10/10/2005 P. Van Roy, CIESC tutorials 147
What is explicit state?
! X = {NewCell I}
! Creates a cell with initial value I
! Binds X to the identity of the cell
! Example: X = {NewCell 0}! X:=J
! Assumes X is bound to a cell C (otherwise exception)
! Changes the content of C to become J
! Y = @X! Assumes X is bound to a cell C (otherwise exception)
! Binds Y to the value contained in C
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 150/261
10/10/2005 P. Van Roy, CIESC tutorials 150
Examples (3)
! X = {NewCell 0}
! X:=5
! Y = X
! Y:=10
! X == 10returns true
X 0
X 5
Y
X 10
Y
The model e tended ith cells
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 151/261
10/10/2005 P. Van Roy, CIESC tutorials 151
The model extended with cells
Semantic stack
(Thread 1)
Semantic stack
(Thread n)
w = f( x)
z = person(a: y)
y = -1
u = -2
x
-1: w
-2: x
....
....
.....
single assignment
storemutable store
The stateful model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 152/261
10/10/2005 P. Van Roy, CIESC tutorials 152
The stateful model
s'
::= skip empty statement
| s1' s2' statement sequence
| ...
| thread s1'
end thread creation| {NewCell x' c'} cell creation
| {Exchange c' x' y'} cell exchange
Exchange: bind x'
to the old content of c'
and set the
content of the cell c' to y'
The stateful model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 153/261
10/10/2005 P. Van Roy, CIESC tutorials 153
The stateful model
| {NewCell x' c'} cell creation
| {Exchange c' x' y'} cell exchange
proc {Assign C X} {Exchange C _ X} end
fun {Access C} X in {Exchange C X X} X end
Exchange: bind x'
to the old content of c'
and set the
content of the cell c' to y'
{Assign C X} can be written C:=X
X={Access C} can be written X=@C
Do we need explicit state?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 154/261
10/10/2005 P. Van Roy, CIESC tutorials 154
Do we need explicit state?
! Up to now the computation model weintroduced in the previous lectures did nothave any notion of explicit state
! An important question is:Do we need explicit state?
! There are a number of reasons for introducing state. Let us present some of them important today.
Programs that changetheir behavior over time
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 155/261
10/10/2005 P. Van Roy, CIESC tutorials 155
their behavior over time
! Declarative program: all information is in the arguments
! Stateful program: new information can be put inside arunning program
Program
Program
Modular programs
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 156/261
10/10/2005 P. Van Roy, CIESC tutorials 156
Modular programs
! A system (program) is modular if changes (updates)to the program are confined to the componentswhere the functionality is changed
! Here is an example where introduction of explicit
state in a well confined way leads to programmodularity compared to programs that are writtenusing only the declarative model (where everycomponent is a function)
Modularity of explicit state (1)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 157/261
10/10/2005 P. Van Roy, CIESC tutorials 157
Modularity of explicit state (1)
! Assume we havethree persons, P, U1,and U2
! P is a programmer who developed acomponent M thatprovides two functionsF and G
! U1 and U2 aresystem builders thatuse the component M
fun {MF}
fun {F ...} &Definition of F'
endfun {G ...} &Definition of G'
end
in ’export’(f:F g:G)
end
M = {MF}
Modularity of explicit state (2)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 158/261
10/10/2005 P. Van Roy, CIESC tutorials 158
Modularity of explicit state (2)
! Assume we havethree persons, P, U1,and U2
! P is a programmer who developed acomponent M thatprovides two functionsF and G
! U1 and U2 aresystem builders thatuse the component M
functor MF
export f:F g:G
define
fun {F ...} &Definition of F'
end
fun {G ...}
&Definition of G'
end
end
Modularity of explicit state (3)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 159/261
10/10/2005 P. Van Roy, CIESC tutorials 159
Modularity of explicit state (3)
! User U2 has a demandingapplication
! He wants to extend the
module M to enable him tomonitor how many timesthe function F is invoked inhis application
! He goes to P, and asks himto do so without changingthe interface to M
fun {M}
fun {F ...} &Definition of F'
endfun {G ...} &Definition of G'
end
in ’export’(f:F g:G)end
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 160/261
Modularity of explicit state (5)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 161/261
10/10/2005 P. Van Roy, CIESC tutorials 161
fun {MF}
X = {NewCell 0}
fun {F ...}
X := @X+1&Definition of F'
endfun {G ...} &Definition of G' end
fun {Count} @ X end
in ’export’(f:F g:G c:Count)end
M = {MF}
Modularity of explicit state (5)
! A cell is created whenMF is called
! Due to lexical scopingthe cell is only visible tothe created version of Fand Count
! The M.f did not change
! New function M.c is
available! X is hidden: only
visible inside M(encapsulated state)
Relationship between the declarativemodel and the stateful model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 162/261
10/10/2005 P. Van Roy, CIESC tutorials 162
model and the stateful model
! Declarative programming guarantees byconstruction that each procedure computes afunction
! This means each component (and
subcomponent) is a function! It is possible to use encapsulated state (cells) so
that a component is declarative from outside,and stateful from the inside
!
Considered as a black-box the programprocedure is still a function
Programs with accumulators (1)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 163/261
10/10/2005 P. Van Roy, CIESC tutorials 163
Programs with accumulators (1)
localfun {Sum1 Xs A}
case Xs of X|Xr then {Sum1 Xr A+X} [] nil then A
endend
infun {Sum Xs} {Sum1 Xs 0}end
end
Programs with accumulators (2)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 164/261
10/10/2005 P. Van Roy, CIESC tutorials 164
Programs with accumulators (2)
fun {Sum Xs}fun {Sum1 Xs}
case Xs of X|Xr then
A:=X+@A{Sum1 Xr}
[] nil then @A end
end A = {NewCell 0}in {Sum1 Xs} end
localfun {Sum1 Xs A}
case Xs of X|Xr then {Sum1 Xr A+X}
[] nil then A endend
infun {Sum Xs} {Sum1 Xs 0}
endend
Programs with accumulators (3)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 165/261
10/10/2005 P. Van Roy, CIESC tutorials 165
Programs with accumulators (3)
fun {Sum Xs} A = {NewCell 0}
in{ForAll Xs proc {$ X}
A:=X+@A end}@A
end
localfun {Sum1 Xs A}
case Xs of X|Xr then {Sum1 Xr A+X}
[] nil then A endend
infun {Sum Xs} {Sum1 Xs 0}
endend
Programs with accumulators (4)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 166/261
10/10/2005 P. Van Roy, CIESC tutorials 166
Programs with accumulators (4)
fun {Sum Xs} A = {NewCell 0}
infor X in Xs do A:=X+@Aend@A
end
The state is encapsulatedinside each procedure
invocation
localfun {Sum1 Xs A}
case Xs of X|Xr then {Sum1 Xr A+X}
[] nil then A endend
infun {Sum Xs} {Sum1 Xs 0}
endend
Lesson 6:
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 167/261
10/10/2005 P. Van Roy, CIESC tutorials 167
Lesson 6:
Language Semantics andthe Abstract Machine
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 168/261
Semantic concepts
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 169/261
10/10/2005 P. Van Roy, CIESC tutorials 169
Semantic concepts
! Single-assignment store
! Environment
! Semantic statement
! Execution state
! Computation
Abstract machine
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 170/261
10/10/2005 P. Van Roy, CIESC tutorials 170
Abstract machine
! Performs a computation
! A computation is a sequence of execution states
! Execution state
!
stack of semantic statements! single assignment store
! Semantic statement
! statement
! environment
! Environment maps variable identifiers to storeentities
Single-assignment store
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 171/261
10/10/2005 P. Van Roy, CIESC tutorials 171
Single assignment store
! Single assignment store
" set of store variables
" partitioned into
. sets of variables that are equal but unbound
. variables bound to value
" Example store { x 1, x 2= x 3, x 4=a| x 2}" x 1 unbound
" x 2, x 3 equal and unbound
" x 4 bound to partial value a| x 2
Environment
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 172/261
10/10/2005 P. Van Roy, CIESC tutorials 172
Environment
! Environment E ! maps variable identifiers to entities in store %
" written as set of pairs X + x
" variable identifier X
" store variable x
! Example environment { X + x , Y + y }! maps identifier X to store variable x
! maps identifier Y to store variable y
Environment and store
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 173/261
10/10/2005 P. Van Roy, CIESC tutorials 173
Environment and store
! Given: environment E , store %
" Looking up value for variable identifier X:" find store variable in environment E (X)
" take value from % for E (X)
" Example:%={ x 1, x 2= x 3, x 4=a| x 2} E = { X + x 1, Y + x 4 }
! E(X) = x 1 and no information in % on x 1" E(Y) = x 4 and % binds x 4 to a| x 2
Calculating with environments
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 174/261
10/10/2005 P. Van Roy, CIESC tutorials 174
Calculating with environments
! Program execution looks up values! assume store %! given variable identifier &x'
" E(&x') is value in store %
! Program execution modifies environments! for example: declaration
! adding new mappings from identifiers
! overwrite existing mappings
! restricting mappings to sets of variables
Environment adjunction
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 175/261
10/10/2005 P. Van Roy, CIESC tutorials 175
Environment adjunction
! Given: Environment E
E + {&x'1+ x 1, …, &x'n+ x n}
is new environment E ’ with mappings added:! always take store entity from new mappings
! might overwrite old mappings
Environment restriction
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 176/261
10/10/2005 P. Van Roy, CIESC tutorials 176
Environment restriction
! Given: Environment E
E | {&x'1, …, &x'n}
is new environment E ’ where only mappings
for {&x'1, …, &x'n} are retained from E
Adjunction example
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 177/261
10/10/2005 P. Van Roy, CIESC tutorials 177
Adjunction example
! E 0 = { Y + 1 }
! E 1 = E 0 + { X + 2 }!
corresponds to { X + 2, Y + 1 }! E 1(X) = 2
! E 2 = E 1 + { X + 3 }! corresponds to { X + 3, Y + 1 }
! E 2(X) = 3
Why adjunction?
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 178/261
10/10/2005 P. Van Roy, CIESC tutorials 178
y j
local X in
X = 2
local X in
X = 3 {… Y}
end
…
end
E 0
E 0
E 2 E 1
Semantic statements
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 179/261
10/10/2005 P. Van Roy, CIESC tutorials 179
! To actually execute a statement, we need:! an environment to map identifiers
" modified with execution of each statement
" each statement has its own environment
! a store to find values
" all statements modify same store
" single store
! Semantic statement ( s'
, E )! pair of (statement, environment)
Stacks of statements
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 180/261
10/10/2005 P. Van Roy, CIESC tutorials 180
! Execution maintains a stack of semanticstatements ST
[(&s'1, E 1), …, (&s'n, E n)]
" always topmost statement (&s'1, E 1) executes first" rest of stack: what needs to be done
" Also called: semantic stack
Execution state
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 181/261
10/10/2005 P. Van Roy, CIESC tutorials 181
! Execution state ( ST , )" pair of ( stack of semantic statements, store )
" Computation (ST 1, 1) (ST 2, 2) (ST 3, 3) …
" sequence of execution states
Program execution
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 182/261
10/10/2005 P. Van Roy, CIESC tutorials 182
g
! Initial execution state( [(&s',/)] , /)
" empty store /
" stack with semantic statement [(&s',/)]
. single statement &s', empty environment /
" At each execution step" pop topmost element of semantic stack
" execute according to statement
" If semantic stack empty, execution stops
Semantic stack states
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 183/261
10/10/2005 P. Van Roy, CIESC tutorials 183
! Semantic stack can be in run-time states! terminated stack is empty
! runnable can do execution step
! suspended stack not empty, no execution
step possible
! Statements! non-suspending can always execute
! suspending need values from storedataflow behavior
Summary of lesson 6
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 184/261
10/10/2005 P. Van Roy, CIESC tutorials 184
y
! Single assignment store
! Environments E ! adjunction, restriction E + {…} E | {…}
! Semantic statements ( s', E )
! Semantic stacks [(
s'
, E ) … ]! Execution state (ST , )
! Program execution! runnable, terminated, suspended
! Statements! suspending, non-suspending
Lesson 7:
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 185/261
10/10/2005 P. Van Roy, CIESC tutorials 185
Executing theAbstract Machine
Lesson 7: executing theabstract machine
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 186/261
10/10/2005 P. Van Roy, CIESC tutorials 186
! Simple statements! skip and sequential composition
! variable declaration
! store manipulation
! conditional
! Computing with procedures (next lecture)! lexical scoping
! closures
! procedures as values! procedure call
Simple statements
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 187/261
10/10/2005 P. Van Roy, CIESC tutorials 187
p
&s' ::= 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
&v' ::= ... value expression
&s' denotes a statement
Executing skip
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 188/261
10/10/2005 P. Van Roy, CIESC tutorials 188
g p
! Execution of semantic statement
(skip, E )
" Do nothing" means: continue with next statement
" non-suspending statement
skip
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 189/261
10/10/2005 P. Van Roy, CIESC tutorials 189
!
No effect on store %" Non-suspending statement
(skip, E )
ST + %
ST + %
skip
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 190/261
10/10/2005 P. Van Roy, CIESC tutorials 190
! Remember: topmost statement is alwayspopped!
(skip, E )
ST + %
ST + %
Executing sequentialcomposition
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 191/261
10/10/2005 P. Van Roy, CIESC tutorials 191
! Semantic statement is
(&s'1 &s'2, E )
" Push in following order " &s'2 executes after
" &s'1 executes next
" Statement is non-suspending
Sequential composition
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 192/261
10/10/2005 P. Van Roy, CIESC tutorials 192
" Decompose statement sequences" environment is given to both statements
(&s'1 &s'2, E )
ST
+ %
(&s'2, E )
ST
+ %
(&s'1, E )
Executing local
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 193/261
10/10/2005 P. Van Roy, CIESC tutorials 193
! Semantic statement is
(local &x' in &s' end, E )
" Execute as follows" create new variable y in store
" create new environment E ’ = E + {&x' + y }
" push (&s', E ’)
" Statement is non-suspending
local
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 194/261
10/10/2005 P. Van Roy, CIESC tutorials 194
" With E ’ = E + {&x' + y }
local &x' in&s'
end
ST + %
ST +
%
(&s', E’ )
ST y
Variable-variable equality
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 195/261
10/10/2005 P. Van Roy, CIESC tutorials 195
! Semantic statement is
(&x' = &y', E )
" Execute as follows" bind E (&x') and E (&y') in store
" Statement is non-suspending
Variable-value equality
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 196/261
10/10/2005 P. Van Roy, CIESC tutorials 196
! Semantic statement is
(&x' = &v', E )
with &v' number or record
" Execute as follows" create value &v' in store
. use variables as defined by E
" bind E (&x') and &v' in store
" Statement is non-suspending
Variable-value equality
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 197/261
10/10/2005 P. Van Roy, CIESC tutorials 197
! Semantic statement is
(&x' = &v', E )
with &v' number or record
" Execute as follows" create value &v' in store
. use variables as defined by E
" bind E (&x') and &v' in store
" Statement is non-suspending
What aboutprocedures?
Suspending statements
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 198/261
10/10/2005 P. Van Roy, CIESC tutorials 198
! All statements so far can always execute! non-suspending (or immediate)
!
Conditional?! requires condition &x' to be bound variable
! activation condition: &x' is bound (determined)
" Foundation of dataflow execution
Executing if
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 199/261
10/10/2005 P. Van Roy, CIESC tutorials 199
! Semantic statement is
(if &x' then &s'1 else &s'2 end, E )
" If activation condition “&x' bound” true" if E (&x') bound to true push &s'1" if E (&x') bound to false push &s'2" otherwise, raise error
" Otherwise, suspend…
Example
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 200/261
10/10/2005 P. Van Roy, CIESC tutorials 200
local X in
local B in
B=true
if B then X=1 else skip end
end
end
Example: initial state
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 201/261
10/10/2005 P. Van Roy, CIESC tutorials 201
([(local X in
local B in
B=true
if B then X=1 else skip end
end end, /)],
/)
!
Start with empty store and emptyenvironment
Example: local
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 202/261
10/10/2005 P. Van Roy, CIESC tutorials 202
([(local B in
B=true
if B then X=1 else skip end
end,
{X + x })],
{ x })
! Create new store variable x
! Continue with new environment
Example: local
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 203/261
10/10/2005 P. Van Roy, CIESC tutorials 203
([(B=true
if B then X=1 else skip end
,
{B + b, X + x })],
{b, x })
! Create new store variable b
! Continue with new environment
Example:sequential composition
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 204/261
10/10/2005 P. Van Roy, CIESC tutorials 204
([(B=true, {B + b, X + x }),
(if B then X=1
else skip end, {B + b, X + x })],
{b, x })
! Decompose to two statements
! Stack has now two semantic statements
Example:variable-value assignment
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 205/261
10/10/2005 P. Van Roy, CIESC tutorials 205
([(if B then X=1
else skip end, {B + b, X + x })],
{b=true, x })
! Environment maps B to b
! Bind b to true
Example: if
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 206/261
10/10/2005 P. Van Roy, CIESC tutorials 206
([(X=1, {B + b, X + x })],
{b=true, x })
! Environment maps B to b
! Store binds b to true, continue with then
Example:variable-value assignment
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 207/261
10/10/2005 P. Van Roy, CIESC tutorials 207
([],
{b=true, x=1})
! Environment maps X to x
! Binds x to 1
! Computation terminates as stack is empty
Summary
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 208/261
10/10/2005 P. Van Roy, CIESC tutorials 208
! Semantic statement execute by! popping itself always
! creating environment local
! manipulating store local, =
! pushing new statements local, if
sequential composition
! Semantic statement can suspend! activation condition
! read store
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 209/261
10/10/2005 P. Van Roy, CIESC tutorials 209
Lesson 8:Declarative Concurrency
Declarative concurrency
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 210/261
10/10/2005 P. Van Roy, CIESC tutorials 210
! Declarative concurrency is the simplest formof concurrency! There are no race conditions, i.e., no observable
nondeterminism
! That is why we see this form of concurrency first
! We add one concept to the declarative kernellanguage, namely the thread
! We see the other forms of concurrency,message-passing and shared-stateconcurrency, later
Concurrency in general
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 211/261
10/10/2005 P. Van Roy, CIESC tutorials 211
! Some programs are best written as a set of activities that runindependently (concurrent programs)
! Concurrency is essential for interaction with the externalenvironment
! Examples includes GUI (Graphical User Interface), operating
systems, etc.! Also programs that are written independently but interact only
when needed (client-server applications)
! This lecture is about declarative concurrency: programs withno observable nondeterminism, the result is a function
! Independent procedures that execute on their pace and maycommunicate through shared dataflow variables
Overview
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 212/261
10/10/2005 P. Van Roy, CIESC tutorials 212
! Programming with threads! The model is augmented with just one concept, namely threads
! This introduces many new programming techniques: streamcommunication, order-determining concurrency, coroutines,concurrent composition
! Lazy execution! Lazy execution introduces a second concept, namely by-need
synchronization (waiting until a variable is needed)
! This introduces another set of new techniques: demand-drivencomputations, lazy streams, and list comprehensions
! In this tutorial we will not see more about lazy execution; see the
textbook!! Soft real-time programming
The sequential model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 213/261
10/10/2005 P. Van Roy, CIESC tutorials 213
w = a z = person(age: y)
x
y = 42
u
Single-assignmentstore
SemanticStack
Statements areexecuted sequentiallyfrom a single semanticstack
The concurrent model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 214/261
10/10/2005 P. Van Roy, CIESC tutorials 214
w = a z = person(age: y)
x
y = 42
u
Single-assignmentstore
SemanticStack 1
SemanticStack N
Multiple semanticstacks (threads)
Concurrent declarative model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 215/261
10/10/2005 P. Van Roy, CIESC tutorials 215
&s' ::= 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
The following defines the syntax of a statement, &s' denotes a statement
The concurrent model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 216/261
10/10/2005 P. Van Roy, CIESC tutorials 216
Single-assignmentstore
STthread s1' end,E
Top of Stack, Thread i
The concurrent model
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 217/261
10/10/2005 P. Van Roy, CIESC tutorials 217
Single-assignmentstore
STTop of Stack, Thread i s1', E
Basic concepts
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 218/261
10/10/2005 P. Van Roy, CIESC tutorials 218
! The model allows multiple statements to execute ”at the sametime” ?
! Imagine that these threads really execute in parallel, each hasits own processor, but share the same memory
! Reading and writing different variables can be done
simultaneously by different threads, as well as reading thesame variable
! Writing the same variable is done sequentially
! The above view is in fact equivalent to an interleaving execution: a totally ordered sequence of computation steps,
where threads take turn doing one or more steps in sequence
Causal order
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 219/261
10/10/2005 P. Van Roy, CIESC tutorials 219
! In a sequential program all execution statesare totally ordered
! In a concurrent program all execution statesof a given thread are totally ordered
! The execution state of the concurrentprogram as a whole is partially ordered
Total order
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 220/261
10/10/2005 P. Van Roy, CIESC tutorials 220
! In a sequential program all execution statesare totally ordered
computation step
sequentialexecution
Causal order in the declarativemodel
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 221/261
10/10/2005 P. Van Roy, CIESC tutorials 221
! In a concurrent program all execution states of a given threadare totally ordered
! The execution state of the concurrent program is partiallyordered
computation step
thread T1
thread T2
thread T3
fork a thread
Causal order in the declarativemodel
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 222/261
10/10/2005 P. Van Roy, CIESC tutorials 222
computation step
thread T1
thread T2
thread T3
fork a thread
bind a dataflow variable
synchronize on a dataflow variable
x
y
Nondeterminism
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 223/261
10/10/2005 P. Van Roy, CIESC tutorials 223
! An execution is nondeterministic if there is acomputation step in which there is a choice what todo next
! Nondeterminism appears naturally when there are
multiple concurrent states
Example of nondeterminism
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 224/261
10/10/2005 P. Van Roy, CIESC tutorials 224
time
Thread 1
x = 1
x
y = 5
store
time
Thread 2
x = 3
The thread that binds x first will continue,the other thread will raise an exception
Nondeterminism
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 225/261
10/10/2005 P. Van Roy, CIESC tutorials 225
! An execution is nondeterministic if there is acomputation step in which there is a choice what todo next
! Nondeterminism appears naturally when there are
multiple concurrent states! In the concurrent declarative model when there is
only one binder for each dataflow variable, thenondeterminism is not observable on the store (i.e.the store develops to the same final results)
! This means for correctness we can ignore theconcurrency!
Scheduling
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 226/261
10/10/2005 P. Van Roy, CIESC tutorials 226
! The choice of which thread to execute next and for how longis done by a part of the system called the scheduler
! A thread is runnable if its next statement to execute is notblocked on a dataflow variable, otherwise the thread issuspended
! 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 itsshare
The semantics
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 227/261
10/10/2005 P. Van Roy, CIESC tutorials 227
! In the sequential model we had:
(ST , % )
ST is a stack of semantic statements
% is the single assignment store
• In the concurrent model we have:
( MST , % )
MST is a (multi)set of stacks of semantic statements
% is the single assignment store
The initial execution state
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 228/261
10/10/2005 P. Van Roy, CIESC tutorials 228
({ [ (&s',/) ] }, /)
statement
stack
multisetstore
Execution (the scheduler)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 229/261
10/10/2005 P. Van Roy, CIESC tutorials 229
! At each step, one runnable semantic stack is selected fromMST (the multiset of stacks), call it ST, i.e., MST = ST 0 MST’
! Assume the current store is %, one computation step is donethat transforms ST to ST’ and % to %’
" The total computation state is transformed from (MST, %) to(ST’ 0 MST’, %’)
" Which stack is selected, and how many step are tasken is thetask of the scheduler, a good scheduler should be fair, i.e.each runnable ’thread’ will eventually be selected
!
The computation stops when there is no runnable stack
Example of runnable thread
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 230/261
10/10/2005 P. Van Roy, CIESC tutorials 230
proc {Loop P N} if N > 0 then
{P} {Loop P N-1}
else skip end
end
thread {Loop proc {$} {Show 1} end 1000}
end
thread {Loop
proc {$} {Show 2} end 1000}
end
• This program willinterleave the executionof two threads, one printing 1, and the other printing 2
• We assume a fair scheduler
Dataflow computation
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 231/261
10/10/2005 P. Van Roy, CIESC tutorials 231
! Threads suspend of data availability indataflow variables
! The {Delay X} primitive makes the threadsuspends for X milliseconds, after that thethread is runnable
declare X{Browse X}local Y in thread {Delay 1000} Y = 10*10 end
X = Y + 100*100end
Illustrating dataflow computation
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 232/261
10/10/2005 P. Van Roy, CIESC tutorials 232
! Enter incrementallythe values of X0 to X3
! When X0 is bound thethread will compute
Y0=X0+1, and willsuspend again untilX1 is bound
declare X0 X1 X2 X3{Browse [X0 X1 X2 X3]}thread Y0 Y1 Y2 Y3in
{Browse [Y0 Y1 Y2 Y3]} Y0 = X0 + 1 Y1 = X1 + Y0 Y2 = X2 + Y1 Y3 = X3 + Y2 {Browse completed}end
Concurrent map function (1)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 233/261
10/10/2005 P. Van Roy, CIESC tutorials 233
fun {CMap Xs F}
case Xs
of nil then nil
[] X|Xr then
thread {F X} end|{CMap Xr F} end
end
! This will fork a threadfor each individualelement in the input list
! Each thread will run
only in both theelement X and theprocedure F is known
Concurrent map function (2)fun {CMap Xs F}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 234/261
10/10/2005 P. Van Roy, CIESC tutorials 234
case Xs of nil then nil [] X|Xr then thread {F X} end |{CMap Xr F} end
end
! How this really looks like in the kernel language:proc {CMap Xs F Rs}
case Xs of nil then Rs = nil [] X|Xr then R Rr in Rs = R|Rr thread R = {F X} end
Rr = {CMap Xr F} end
end
How does it work? (1)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 235/261
10/10/2005 P. Van Roy, CIESC tutorials 235
! If we enter the following statements:declare F X Y Z{Browse thread {CMap X F} end}
! A thread executing CMap is created.
! It will suspend immediately in the case-statement because Xis unbound.
! If we thereafter enter the following statements:X = 1|2|Yfun {F X} X*X end
!
The main thread will traverse the list creating two threads for the first two arguments of the list
How does it work? (2)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 236/261
10/10/2005 P. Van Roy, CIESC tutorials 236
! The main thread will traverse the list creating twothreads for the first two arguments of the list, namely
! thread {F 1} end and thread {F 2} end,Y = 3|ZZ = nil
! and adding Y=3|nil will complete the computation of the main thread and the newly created threadthread {F 3} end, resulting in the final list [1 4 9].
Cheap concurrency anddataflow
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 237/261
10/10/2005 P. Van Roy, CIESC tutorials 237
! Declarative programscan be easily madeconcurrent
!
Just use the threadstatement whereconcurrent is needed
fun {Fib X} if X=<2 then 1
else
thread {Fib X-1} end
+ {Fib X-2} end
end
Understanding why
f { }
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 238/261
10/10/2005 P. Van Roy, CIESC tutorials 238
fun {Fib X} if X=<2 then 1
else F1 F2 in
F1 = thread {Fib X-1} end
F2 = {Fib X-2}
F1 + F2
endend
Dataflow dependency
Execution of {Fib 6}F2
F k th d
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 239/261
10/10/2005 P. Van Roy, CIESC tutorials 239
F6
F5
F4 F2
F3
F2
F1
F2
F3 F1
F4
F1F3
F2
Fork a thread
Synchronize on
result
Running thread
Fib run-time statistics
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 240/261
10/10/2005 P. Van Roy, CIESC tutorials 240
Streams
! A t i f
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 241/261
10/10/2005 P. Van Roy, CIESC tutorials 241
! A stream is a sequence of messages! A stream is a list with an unbound tail!
! A stream is First-In First-Out channel
! The producer augments the stream with new messages,and the consumer reads the messages, one by one.
x5 x4 x3 x2 x1
producer consumer
Stream communication (1)
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 242/261
10/10/2005 P. Van Roy, CIESC tutorials 242
! The dataflow property of Oz easily enableswriting threads that communicate throughstreams in a producer-consumer pattern
! A stream is a list that is created incrementally byone thread (the producer) and subsequentlyconsumed by one or more threads (theconsumers).
! The consumers consume the same elements of
the stream.
Stream communication (2)
! P d th t i t ll d th l t f
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 243/261
10/10/2005 P. Van Roy, CIESC tutorials 243
! Producer , that incrementally produces the elements of astream
! Transducer (s), that transforms the elements of the stream,giving a new stream
! Consumer , that accumulates the results of a stream
producer transducer transducer consumer
thread 1 thread 2 thread 3 thread N
Program patterns
Th d t d d th
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 244/261
10/10/2005 P. Van Roy, CIESC tutorials 244
! The producer, transducers, and theconsumer can, in general, be described bycertain program patterns
!
We show some of these patterns
Producer pattern
f {P d St t }
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 245/261
10/10/2005 P. Van Roy, CIESC tutorials 245
fun {Producer State} if {More State} then
X = {Produce State} in
X | {Producer {Transform State}}
else nil endend
! The definitions of More, Produce, and Transform areproblem-dependent
! The State could be multiple arguments! The above definition is not a complete program!
Example producer
f n {G t N Li it} fun {Producer State}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 246/261
10/10/2005 P. Van Roy, CIESC tutorials 246
fun {Generate N Limit} if N=<Limit then
N | {Generate N+1 Limit}
else nil end
end
! The State is the two arguments N and Limit
! The predicate More is the condition N=<Limit! The Transform function (N,Limit) , (N+1,Limit)
fun {Producer State}
if {More State} then
X = {Produce State} in
X | {Producer {Transform State}}
else nil end
end
Consumer pattern
fun {Consumer State InStream}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 247/261
10/10/2005 P. Van Roy, CIESC tutorials 247
fun {Consumer State InStream}
case InStream
of nil then {Final State}
[] X | RestInStream then
NextState = {Consume X State} in
{Consumer NextState RestInStream}
end
end
! Final and Consume are problem-dependent
The consumer suspends untilInStream is either a cons or a nil
Example consumer
f {S A X } fun {Consumer State InStream}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 248/261
10/10/2005 P. Van Roy, CIESC tutorials 248
fun {Sum A Xs}
case Xs
of X|Xr then {Sum A+X Xr}
[] nil then A
endend
! The State is A
!
Final is just the identity function on State! Consume takes X and State , X + State
fun {Consumer State InStream} case InStream
of nil then {Final State}
[] X | RestInStream then
NextState = {Consume X State} in
{Consumer NextState RestInStream}
end
end
Transducer pattern (1)
f {T d S I }
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 249/261
10/10/2005 P. Van Roy, CIESC tutorials 249
fun {Transducer State Instream}
case InStream
of nil then nil
[] X | RestInStream then
NextState#TX = {Transform X State}
TX | {Consumer NextState RestInStream}
end
end
! A transducer keeps its state in State, receivesmessages on InStream and sends messages onOutStream
Transducer pattern (2)
f {T d St t I t }
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 250/261
10/10/2005 P. Van Roy, CIESC tutorials 250
fun {Transducer State Instream}
case InStream
of nil then nil
[] X | RestInStream then if {Test X#State} then
NextState#TX = {Transform X State}
TX | {Consumer NextState RestInStream}else {Consumer NextState RestInStream} end
end
end! A transducer keeps its state in State, receives messages on
InStream and sends messages on OutStream
Example transducer IsOdd
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 251/261
10/10/2005 P. Van Roy, CIESC tutorials 251
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
Generate Filter
6 5 4 3 2 1 5 3 1
Filter is a transducer thattakes an Instream and incrementlyproduces an Outstream that satisfiesthe predicate FF
local Xs Ys in thread Xs = {Generate 1 100} end thread Ys = {Filter Xs IsOdd} end thread {Browse Ys} endend
Larger example:The sieve of Eratosthenes
!
Produces prime numbers
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 252/261
10/10/2005 P. Van Roy, CIESC tutorials 252
Produces prime numbers! It takes a stream 2...N, peels off 2 from the rest of the stream
! Delivers the rest to the next sieve
Sieve
Filter Sieve
Xs
Xr
X
YsZs
X|Zs
Sieve
fun {Sieve Xs}
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 253/261
10/10/2005 P. Van Roy, CIESC tutorials 253
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
Example call
local Xs Ys in
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 254/261
10/10/2005 P. Van Roy, CIESC tutorials 254
local Xs Ys in
thread Xs = {Generate 2 100000} end
thread Ys = {Sieve Xs} end
thread for Y in Ys do {Show Y} end endend
Larger example:The sieve of Eratosthenes
! Produces prime numbers
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 255/261
10/10/2005 P. Van Roy, CIESC tutorials 255
Produces prime numbers! It takes a stream 2...N, peels off 2 from the rest of the stream
! Delivers the rest to the next sieve
Filter 3 SieveFilter 2 Filter 5
7 | 11 |...
Limitation of eager streamprocessing
! The producer might be much faster than the
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 256/261
10/10/2005 P. Van Roy, CIESC tutorials 256
! The producer might be much faster than theconsumer
! This will produce a large intermediate stream
that requires potentially unbounded memorystorage
x5 x4 x3 x2 x1
producer consumer
Solutions!
There are three alternatives:Pl i h h d f h diff h d i l i h
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 257/261
10/10/2005 P. Van Roy, CIESC tutorials 257
There are three alternatives:1. Play with the speed of the different threads, i.e. play with
the scheduler to make the producer slower (not arecommended solution)
2. Create a bounded buffer, say of size N, so that the
producer waits automatically when the buffer is full3. Use demand-driven approach, where the consumer
activates the producer when it need a new element(lazy evaluation)
! The last two approaches introduce the notion of flow-
control between concurrent activities (very common)
Lesson 9:Object-Oriented
P i
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 258/261
10/10/2005 P. Van Roy, CIESC tutorials 258
Programming
Object-oriented programming
! We have gotten so far and we have said almost nothing about
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 259/261
10/10/2005 P. Van Roy, CIESC tutorials 259
We have gotten so far and we have said almost nothing aboutobject-oriented programming!
! There is much, much more to programming than just object-oriented programming! We have to resist the pressure by part of industry to reduce
programming to object-oriented programming in Java
! I will not say much on OOP in this tutorial! The textbook and the slides on the book’s Web site say a lot; I
suggest you go take a look
! Trends show that both concurrent programming andcomponent-based programming will become much more
important in the future, and that OOP will become a simplepart of these two paradigms
C l i
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 260/261
10/10/2005 P. Van Roy, CIESC tutorials 260
Conclusions
Conclusions
! We have gone through a lot of material in these two
7/23/2019 Introduccion MPP
http://slidepdf.com/reader/full/introduccion-mpp 261/261
We have gone through a lot of material in these twotwo-hour tutorials
! Probably too much to digest in one sitting
! The two main lessons of these tutorials
! Programming can be taught both broadly (many paradigms)and deeply (with semantics) to second-year students
! Programming can be taught as a unified discipline, in whicheach paradigm has its part not as a set of disjoint