[ Pobierz całość w formacie PDF ]
.As we will see later inthe book, there are many good reasons to have dataflow behavior.For now, letus see how dataflow and concurrency work together.Take for example:declare X inthread {Delay 10000} X=99 end{Browse start} {Browse X*X}Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 18 Introduction to Programming ConceptsThe multiplication X*X waits until X is bound.The first Browse immediatelydisplays start.The second Browse waits for the multiplication, so it displaysnothing yet.The {Delay 10000} call pauses for 10000 milliseconds (i.e., 10seconds).X is bound only after the delay continues.When X is bound, then themultiplication continues and the second browse displays 9801.The two operationsX=99andX*Xcan be done in any order with any kind of delay; dataflow executionwill always give the same result.The only effect a delay can have is to slow thingsdown.For example:declare X inthread {Browse start} {Browse X*X} end{Delay 10000} X=99This behaves exactly as before: the browser displays 9801 after 10 seconds.Thisillustrates two nice properties of dataflow.First, calculations work correctlyindependent of how they are partitioned between threads.Second, calculationsare patient: they do not signal errors, but simply wait.Adding threads and delays to a program can radically change a program sappearance.But as long as the same operations are invoked with the same argu-ments, it does not change the program s results at all.This is the key propertyof dataflow concurrency.This is why dataflow concurrency gives most of theadvantages of concurrency without the complexities that are usually associatedwith it.1.12 StateHow can we let a function learn from its past? That is, we would like the functionto have some kind of internal memory, which helps it do its job.Memory is neededfor functions that can change their behavior and learn from their past.This kindof memory is called explicit state.Just like for concurrency, explicit state modelsan essential aspect of how the real world works.We would like to be able to dothis in the system as well.Later in the book we will see deeper reasons for havingexplicit state.For now, let us just see how it works.For example, we would like to see how often theFastPascalfunction is used.Is there some way FastPascalcan remember how many times it was called? Wecan do this by adding explicit state.A memory cellThere are lots of ways to define explicit state.The simplest way is to define asingle memory cell.This is a kind of box in which you can put any content.Many programming languages call this a  variable.We call it a  cell to avoidconfusion with the variables we used before, which are more like mathemati-cal variables, i.e., just short-cuts for values.There are three functions on cells:NewCell creates a new cell, := (assignment) puts a new value in a cell, and @Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 1.13 Objects 19(access) gets the current value stored in the cell.Access and assignment are alsocalled read and write.For example:declareC={NewCell 0}C:=@C+1{Browse @C}This creates a cell C with initial content 0, adds one to the content, and thendisplays it.Adding memory to FastPascalWith a memory cell, we can let FastPascal count how many times it is called.First we create a cell outside of FastPascal.Then, inside of FastPascal, weadd one to the cell s content.This gives the following:declareC={NewCell 0}fun {FastPascal N}C:=@C+1{GenericPascal Add N}end(To keep it short, this definition uses GenericPascal.)1.13 ObjectsFunctions with internal memory are usually called objects.The extended versionof FastPascalwe defined in the previous section is an object.It turns out thatobjects are very useful beasts.Let us give another example.We will define acounter object.The counter has a cell that keeps track of the current count.Thecounter has two operations, Bumpand Read.Bumpadds one and then returns theresulting count.Read just returns the count.Here is the definition:declarelocal C inC={NewCell 0}fun {Bump}C:=@C+1@Cendfun {Read}@CendendThere is something special going on here: the cell is referenced by a local variable,so it is completely invisible from the outside.This property is called encapsu-Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 20 Introduction to Programming Conceptslation.It means that nobody can mess with the counter s internals.We canguarantee that the counter will always work correctly no matter how it is used.This was not true for the extended FastPascal because anyone could look atand modify the cell.We can bump the counter up:{Browse {Bump}}{Browse {Bump}}What does this display? Bump can be used anywhere in a program to count howmany times something happens.For example, FastPascal could use Bump:declarefun {FastPascal N}{Browse {Bump}}{GenericPascal Add N}end1.14 ClassesThe last section defined one counter object.What do we do if we need morethan one counter? It would be nice to have a  factory that can make as manycounters as we need.Such a factory is called a class.Here is one way to defineit:declarefun {NewCounter}C Bump Read inC={NewCell 0}fun {Bump}C:=@C+1@Cendfun {Read}@Cendcounter(bump:Bump read:Read)endNewCounteris a function that creates a new cell and returns newBumpandReadfunctions for it.Returning functions as results of functions is another form ofhigher-order programming.We group the Bump and Read functions together into one compound datastructure called a record.The record counter(bump:Bump read:Read)is char-acterized by its label counter and by its two fields, called bump and read.Letus create two counters:declareCtr1={NewCounter}Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 1.15 Nondeterminism and time 21timeC={NewCell 0} C:=1 C:=2 First execution:final content of C is 2C={NewCell 0} C:=2 C:=1 Second execution:final content of C is 1Figure 1.4: All possible executions of the first nondeterministic exampleCtr2={NewCounter}Each counter has its own internal memory and its own Bump and Readfunctions.We can access these functions by using the . (dot) operator.Ctr1.bumpaccesses the Bumpfunction of the first counter.Let us bump the first counter anddisplay its result:{Browse {Ctr1.bump}}Towards object-oriented programmingWe have given an example of a simple class, NewCounter, that defines two op-erations, Bump and Read.Operations defined inside classes are usually calledmethods.The class can be used to make as many counter objects as we need.All these objects share the same methods, but each has its own separate internalmemory.Programming with classes and objects is called object-based program-ming.Adding one new idea, inheritance, to object-based programming gives object-oriented programming.Inheritance means that a new class can be defined interms of existing classes by specifying just how the new class is different.We saythe new class inherits from the existing classes.Inheritance is a powerful conceptfor structuring programs.It lets a class be defined incrementally, in differentparts of the program.Inheritance is quite a tricky concept to use correctly.Tomake inheritance easy to use, object-oriented languages add special syntax for it.Chapter 7 covers object-oriented programming and shows how to program withinheritance.1.15 Nondeterminism and timeWe have seen how to add concurrency and state to a program separately.Whathappens when a program has both? It turns out that having both at the sametime is a tricky business, because the same program can give different resultsfrom one execution to the next [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •