Title: Introducing ASML
1Introducing ASML
- Sequences, Parallel evaluation, Maps,
Non-Determinism - Lecture 12
- Software Engineering COMP201
2I. Sequences
- A Sequence is a collection of elements of the
same type, just as a set is. - Sequences differ from sets in two ways
- A sequence is ordered while a set is not.
- A sequence can contain duplicate elements while a
set does not. - Elements of sequences are contained within square
brackets - 1,2,3,4, 4,3,2,1, a,e,i,o,u, a,a,e,i,o,u
X1,2,3,4 Y1,1,2,3,4 Z1,1,2,3,4 Main()
step WriteLine(X X) step WriteLine (Y
Y) step WriteLine (Y Y)
The result is X 1,2,3,4 Y 1,2,3,4 Z
1,1,2,3,4
3Ordering of sequences
eq ne ? lt lt gt gt in ? notin ?
subset ? superset ? subseteq ? superseteq ?
A 1,2,3,4 B 4,3,2,1 C 4,3,2,1 D
1,2,3,4 Main() step if A eq B then
WriteLine (A B) else
WriteLine (A ltgt B) step if C eq D then
WriteLine (C D) else
WriteLine (C ltgt D)
A B
The result is A B C ltgt D
4Accessing sequence entries by index
- Sequences are zero-based, which means that the
first element in the sequence is indexed by zero
(0) - To select a specific element from the sequence,
use the sequence name followed by element number,
enclosed in parentheses
m 1..5 Main() step WriteLine (m(1))
The code displays the number 2
5II. Parallel evaluation
- It is possible to make updates in parallel using
the forall statement that evaluates all the
members of a set or sequence in a single step - Parallel evaluation is helpful because, it
greatly reduces the number of steps required to
specify an algorithm - forall binders statement-list
6Parallel Evaluation Example
class Person var age as Integer Alice new
Person(20) Bob new Person(16) Ted new
Person(40) People Alice, Bob,
Ted GrowOlder() forall p in People p.age
p.age 1 var year 2002 Main() step
while year lt 2010 WriteLine
(AlicesAlice.age in year)
WriteLine (BobsAlice.age in year)
WriteLine (TedsAlice.age in year)
GrowOlder() year year 1
7Sequential iteration
- AsmL provides for sequential iteration through
the elements in a collection using the step
foreach while and until statement - step foreach boundedvars
- step while expression
- step until ( fixpoint expression )
- If you need to use step foreach, and you are
using it with sets remember that sets have no
inherent order - If the order is important, use sequences rather
than sets
8Sequential iteration over a collection
class Person var age as Integer Alice new
Person(20) Bob new Person(16) Ted new
Person(40) People Alice, Bob,
Ted GrowOlder() step foreach p in People
p.age p.age 1 var year 2002 Main()
step while year lt 2010 WriteLine(Alices
Alice.age in year) WriteLine
(BobsAlice.age in year)
WriteLine (TedsAlice.age in year)
GrowOlder() year year 1
9III. Maps
- Maps are tables that associate keys to values
- Like arrays, maps have a set of unique keys and a
set of values associated with those keys
Example. Map declaration var phoneNumber as Map
of String to Integer
Example. Enumerating map entries phoneNumber
Bob gt100, Carol gt101
The gt symbol associated keys with values. It
is read as map to
10Maps with single argument
Example. Looking up values in a map var
phoneNumber as Map of String to Integer
Bob gt100, Carol gt101 Main() step
WriteLine (Carols extension is
phoneNumber(Carol))
Example. Map-based binding var phoneNumber as
Map of String to Integer Bob gt100, Carol
gt 101, Ted gt102, Alice gt103 Main()
step y j i gt j in phoneNumber where j
lt 103 WriteLine(The set of extensions
less than 103 isy)
11Map construction
- Map display is an enumeration of individual
element-to-element associations in the form - d1 gt r1, d2 gt r2,
- Map comprehension denotes a map in terms of
iterated expressions. Its from is - expr1 gt expr2 binder1, binder2,
Example. Constructing maps X 2..5 Y i gt
i 1 i in X where i lt4 // same as z Z 2
gt 3, 3 gt 4 WriteLine (z(2)) // prints 3
12Maps with multiple arguments
- Maps whose keys are tuples can be thought of as
multidimensional arrays. - Each argument is one of the indexers into the
table.
Example. Tuples as map keys and Nested maps
var phoneNumber1 as Map of (String,String) to
Integer (Bob, Home) gt 5550000, (Bob,
Work) gt 100 ) var phoneNumber2 as Map of
String to Map of String to Integer Bob gt
Home) gt 5550000 Main() step WriteLine
(phoneNumber1) step WriteLine (phoneNumber2)
When you declare the map, separate each of the
argument types with a comma , and enclose them
all in parentheses ( and ).
13Map Operations
- dom to find the domain of a map
- ran - to find the range of a map
var phoneNumber as Map of String to Integer
Bob gt100, Carol gt101 Main() step
WriteLine (The keys are dom(phoneNumber))
step WriteLine(The values are
ran(phoneNumber))
The result is The keys are Bob,Carol The
values are 100,101
14Map merge
- The merge operation combines two maps
A Bob gt100, Carol gt101, Ted gt100,
Carol gt101 B Jeff gt104, George
gt105, Ann gt106, Beth gt107 Main()
step WriteLine (B merge A)
The result is Bob gt100, Carol gt101,
Ted gt100, Carol gt101, Jeff gt104,
George gt105, Ann gt106, Beth gt107
15Partial updates of maps
- Along with sets, partial updates are also useful
with maps.
var Extension as Map of String to Integer
gt Main() step Extension(Bob) 100
Extension(Carol) 101 step
WriteLine(Bobs extension is
Extension(Bob)) WriteLine (Carols extension
is Extension(Bob))
- A name (a string) is associated with an extension
(an integer). - Initially, the map is empty. We can then add
entries to it, one person at a time
16IV. Non-Determinism
- Non-deterministic systems exhibits two
characteristic - There is a finite set of possibilities
- Within that set the result may be any value, but
we dont know which one - In addition to accuracy, non-determinism provides
flexibility - A specification should not limit the possible
implementations - Non-determinism can be used to show where
multiple options are possible - Using non-determinism to model aspects of your
system is an important part of keeping your model
focused - It helps to avoid being distracted by detail that
does not matter for chosen view
17Non-deterministic choice
Successful modellers are conscientious about
excluding what does not matter (for the chosen
level of abstraction) and including what does.
Example. ND choice, expression level A
1..10 Main() step x any y y in A
WriteLine(x is x) //prints any element from
A
Example. ND choice, statement level S 1, 6,
3 Main() step choose i in S where i gt 4
WriteLine (i was chosen) // prints any
// elements from A that is greater than 4
Possible ERROR !
18Non-deterministic choice ifnone
- If there are no elements that fit the
qualifications, the system generates a runtime
error - You can avoid this with ifnone
Example. Default choice S 1,6,3 Main()
step choose i in S where i gt 4
WriteLine (i was chosen) // prints any //
elements from A that is greater than 4
ifnone WriteLine (There were none to
choose.)
19External non-determinism
- Another kind of non-determinism occurs when you
make a method call outside of AsmL (for example,
into an external library) - You should not assume that external functions
appears in a method body will be the same as the
order they are invoked at runtime, unless a
step separates them
Main() step WriteLine (This could print
second) WriteLine (This could print
first) step WriteLine (This will print
last)
20Non-determinism of new
- The new operator that is used to create
instances of a class can be seen as an external
or non-deterministic function - The reason for this is that new expressions
like - new Person(Bill, 40))
- in the earlier examples) return a different
value every time they are invoked -