1 introducing asml methods, values, constraints, constants, variables, sets, sequences lecture 11,...

44
1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

Post on 20-Dec-2015

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

1

Introducing ASML

Methods, Values, Constraints, Constants, Variables, Sets,

Sequences

Lecture 11, 12

Software Engineering COMP201

Page 2: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

2

I. Methods

• Methods are named operations that may be invoked in various contexts

• The definition of a method may include parameters that will be associated with particular values each time the method is invoked

• Methods may also specify a return value that is available after the operation has been performed

Page 3: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

3

The general form for a method definition

name (parameter 1 as Type, … parameter n as Type) as Type

the values sent to the method, if there any

the name of method

The type of the return value, if there is one

Page 4: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

4

Method IsWord()

IsWord ( i as Integer ) as Boolean return (0<= i and i <= 0xFFFF)Main() step if IsWord ( 123 ) then WriteLine (“123 is a word of memory.”)

• IsWord evaluates an argument i and returns the value true if i 0 and i0xFFFF (hexadecimal number).

• If i doesn’t fit these criteria, IsWord returns the value false.

• An expression in the form IsWord() is an application of the name IsWord to argument i, which is a long integer.

Page 5: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

5

Functions and update procedures

• Functions have no effect on state variables

• Functions must return a value

IsWord ( i as Integer ) as Boolean return (0<= i and i <= 0xFFFF)

• Update procedures configure the values of state variables for the next sequential step of the machine

• Update procedures may optionally return a value

Page 6: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

6

Update procedure power()

var x as Integer = 3var y as Integer = 0var index as Integer = 3

power (x as Integer, n as Integer) as Integer var result as Integer = 1 var i as Integer = 1 step while i<=n result:=result*x i:=i+1 step return resultMain() step y := power(5,3) step WriteLine (“5 cubed =” +y) WriteLine(“3 cubed =” + power(3,index)) x:= power(x, power(2,2)) step WriteLine (“x = ” +x)

Output:

5 cubed = 125

3 cubed = 27

x = 81

Page 7: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

7

Local names for values

Statement in the form

identifier = expression

introduce identifier as a local name for the value given on the right hand side of the equals sign (“ = ”) by expression.

Main() mySet = {1, 2, 3} step WriteLine(“mySet has” +

asString(Size(mySet))+ “ elements. ”)

Local names may be introduced within any block of statement.

Local names could appear after the “then” keyword of an if … then … else statement

Size (S) Cardinality of the set S

Page 8: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

8

Local names in sequences of steps

• A local name can be referenced anywhere in its statement block.

• In a sequence of steps, local names introduced within a step block are available within any following step block.

Main() step mySet1 = {1, 2, 3} WriteLine (“mySet has” +

asString(size(mySet1))+ “ elements. ”) step mySet2 = mySet1 union {4 , 5, 6} WriteLine(“mySet has” + asString(size(mySet2))

+ “ elements. ”)

Page 9: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

9

Method overloading

Method overloading allows you:• to use the same name in different

methods• in each instance to have the compiler

choose the correct instance

Must be a clear way for the compiler to decide which method should be called

for any particular instance

Page 10: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

10

Method overloading example

S = { 1, 8, 2, 12, 13, 6}

Max (i as Integer, j as Integer) as Integer return if i > j then i else j

Max (s as Set of Integer) as Integer return any m | m in S where not

(exists n in S where n>m )

Main() step writeln(“The largest number in the

set is ” + ( Max(S) ) ) step writeln(“The largest of the two

integers is ” + ( Max(2,3) ) )

A series of methods with the same name but differentiated by their parameter

lists are overloaded methods

Page 11: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

11

II.ValuesWhat are values?

If x and y are values, then we can ask whether x is equal to y

If the S is a set, then we can ask whether x is an element of S

A value is an immutable element that supports two operations:

equality testing and set membership.

The term value has the same meaning as in mathematical sets

Page 12: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

12

Structured values

• Some values are composed of other values

• Some structured values are built into AsmL; others may be defined by you

ordered pair

structure Location base as Integer offset as Integer

Location (1,4) is a value of the user-defined structure type Location

Page 13: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

13

Built in value types

Double-precision 64-bit floating-point format type as specified in IEEE 754

Double

Single-precision 32-bit floating-point format type as specified in IEEE 754

Float

64-bit signed integer typeLong

32-bit signed integer typeInteger

16-bit signed integer typeShort

8-bit unsigned integer typeByte

A type that can be assigned true and false

Boolean

MeaningData Type

Page 14: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

14

Built in value types

A set containing elements of type A

Set of A

A table whose entries map of elements of type A to type B

Map of A to B

A sequence of elements of type A

Seq of A

Unicode string typeString

Unicode character typeChar

MeaningData Type

Page 15: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

15

Built in value types

Types in the form t? includes all of the values of type t plus the special value undef.

For example, a variable declared as Boolean? could contain either of the Boolean value true or false or the value undef.

A?

A tuple consisting of elements of type A1, A2, … Tuple values are written in the same form

For example, (1,2) is of a value of the built-in type (Integer,Integer)

(A1,A2,…)

MeaningData Type

Page 16: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

16

III. Constraints Assertions

• Assertions require and ensure statements document constraints placed upon the model

• Violating a constraint at runtime is called an assertion failure

AllTickets = {1..1024}

IsTicketID (i as Integer) as Boolean require i > 0 return ( i in AllTickets)

ChooseAnyTicket() as Integer ensure IsTicketID(result) return any t | t in AllTickets

• The keyword result denotes the value of the return statement

Page 17: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

17

Type constraints

• Types of constants, variables and method parameters may be specified using the syntax :

name as type• Types may be explicitly checked

using the “ is” operator :value is type

an expression will be either true or false, depending on whether the value given is an element of the type

• Types are not themselves values. For example, you can’t pass a type as an argument

Page 18: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

18

IV. Constants

• Constants are fixed, named values

• Constants can either be global or local

• Global constants are declared in the beginning of the program

• Local constants are declared within a statement block

• The general form of declaring and initialising a constant is:

Name [as Type] = value

MaxInteger = 100Main() MinInteger = 0

Page 19: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

19

V. Variables

• Variables are names given to memory locations.

• Updating variables is the only way to change a state of a machine.

• Variables can be complex types such as structures or maps as well as simple types.

• Here is the general form for declaring and initialising variables:

var name [as Type] = value

Page 20: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

20

Types of variables

• There are three types of variables in AsmL– Global variables

– Local variables

– Instance-based variables

Instance-based variables are variables that exist for each instance of a class

var x as Integer = 10 // shows explicit typing

var x = 10 // shows implicit typing

var greeting as String = “Hello” // shows a // string

variablevar y = [1..6] // shows a sequence of

// integers from 1 to 6

var menu = {“ham”, “cheese”, “bit”} // shows a //

set

Page 21: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

21

III. Sets

• A set is an unordered collection of distinct values that are of the same type (for example, all integers or all characters)

• These values are called the elements or members of the set

• To show all the elements of a set, frame the elements between two curly braces and separate one element from the other by commas

A ={‘a’,’e’,’i’, ‘o’,’u’} //set of charactersB ={‘2’,’3’,’0’, ‘22’,’4’} //set of integersC={} //empty set

Page 22: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

22

Sets with enumerated elements

Sets given by value range

To specify a set you can either – list all the elements in the set or– you can state all the properties that

characterize the elements of the set

X={1,2,3,4}

Main() step WriteLine(X)

X={1..4}

Main() step WriteLine(X)

These methods are practical for small sets

Page 23: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

23

Sets described algorithmically

In many cases, listing all the elements of a set isn’t practical

Problem:

Suppose we have a set that includes the integers from 1 to 20 and we want to find those numbers that, when doubled, still belong to the set.

Solution:

A = {1..20}C = {i | i in A where 2*i in A}

Main() step WriteLine(C)

Page 24: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

24

Binding multiple names(example)

This example finds pairs of numbers where – the first number is a member of A and

less than 4, – while the second number is also in A

and is less than the first number

A = {1..5}C = {(i,j) | i in A where i < 4 , j in A where j < i}

Main() step

WriteLine(C)

i lt 4 i lt j

The result is:C={(2,1) (3,1) (3,2)}

Page 25: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

25

Set operations

For more complete documentation, see the AsmL documentation:http://research.microsoft.com/fse/asml/doc/StartHere.html

• Union

• Intersect

• SizeA = {1,2,3,4,5}B = {4,5,6}

Main() step

WriteLine(B union A) step

WriteLine(B intersect A) step

WriteLine(size(A))

The result is:{1, 2, 3, 4, 5, 6}

{4, 5}

5

Page 26: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

26

I. 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]

X={1,2,3,4}Y={1,1,2,3,4}Z=[1,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]

Page 27: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

27

Ordering of sequences

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 <> B”) step if C eq D then WriteLine (“C = D”) else WriteLine (“C <> D”)

A = B

The result is:

A = BC <> D

eq=

ne

lt <

gt >

in

notin

subset

superset subseteq superseteq

Page 28: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

28

Accessing 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

Page 29: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

29

II. 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

Page 30: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

30

Parallel Evaluation Example

class Person var age as IntegerAlice = 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 < 2010 WriteLine (“Alice’s”+Alice.age + “ in ” + year) WriteLine (“Bob’s”+Alice.age + “ in ” + year) WriteLine (“Ted’s”+Alice.age + “ in ” + year) GrowOlder() year := year + 1

S1

Alice.age =20Bob.age =16Ted.age =40

S2

Alice.age =21Bob.age =17Ted.age =41

S3

Alice.age =22Bob.age =18Ted.age =42

Page 31: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

31

Sequential 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

Page 32: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

32

Sequential iteration over a collection

class Person var age as IntegerAlice = 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 < 2010 WriteLine(“Alice’s”+Alice.age + “ in ” + year) WriteLine (“Bob’s”+Alice.age + “ in ” + year) WriteLine (“Ted’s”+Alice.age + “ in ” + year) GrowOlder() year := year + 1

S1

Alice.age =20Bob.age =16Ted.age =40

S2

Alice.age =20Bob.age =17Ted.age =40

S3

Alice.age =21Bob.age =17Ted.age =40

Page 33: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

33

III. 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” –>100, “Carol” |–>101}

The “ –> ” symbol associated keys with values. It is read as “map to”

Page 34: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

34

Maps with single argument

Example. Looking up values in a map

var phoneNumber as Map of String to Integer = {“Bob” –>100, “Carol” –>101}

Main() step WriteLine (“Carol’s extension is” +

phoneNumber(“Carol”))

Example. Map-based binding

var phoneNumber as Map of String to Integer ={“Bob” –>100, “Carol” –> 101, “Ted” –>102, “Alice” –>103}

Main() step y = {j | i –> j in phoneNumber where j < 103} WriteLine(“The set of extensions less than 103

is”+y)

Page 35: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

35

Map construction

• Map display is an enumeration of individual element-to-element associations in the form

{d1 –> r1, d2 –> r2, …}

• Map comprehension denotes a map in terms of iterated expressions. Its from is:

{expr1 –> expr2 | binder1, binder2, …}

Example. Constructing maps

X = {2..5}Y = {i –> i + 1 | i in X where i <4 } // same as zZ = {2 –> 3, 3 –> 4}WriteLine (z(2)) // prints 3

Page 36: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

36

Maps 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.

var phoneNumber1 as Map of (String,String) to Integer = {(“Bob”, “Home”) –> 5550000, (“Bob”, “Work”) –> 100 )}

var phoneNumber2 as Map of String to Map of String to Integer = {“Bob” –>{ “Home” –> 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 “)”.

Example. Tuples as map keys and Nested maps

Page 37: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

37

Map 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” –>100, “Carol” –>101}

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}

Page 38: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

38

Map merge

• The merge operation combines two maps:

A = {“Bob” –>100, “Carol” –>101, “Ted” –>100, “Carol” –>101}

B = {“Jeff” –>104, “George” –>105, “Ann” –>106, “Beth” –>107}

Main() step WriteLine (B merge A)

The result is:{“Bob” |–>100, “Carol” |–>101, “Ted” |–>100, “Carol” |–>101,

“Jeff” |–>104, “George” |–>105, “Ann” |–>106, “Beth” |–>107}

Page 39: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

39

Partial updates of maps

• Along with sets, partial updates are also useful with maps.

var Extension as Map of String to Integer = {–>}Main() step Extension(“Bob”) := 100 Extension(“Carol”) := 101 step WriteLine(“Bob’s extension is”+

Extension(“Bob”)) WriteLine (“Carol’s 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

Page 40: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

40

IV. 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 don’t 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

Page 41: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

41

Non-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 > 4 WriteLine (i + “ was chosen”) // prints any

// elements from A that is greater than 4

Possible ERROR !

Page 42: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

42

Non-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 > 4 WriteLine (i + “ was chosen”) // prints any

// elements from A that is greater than 4 ifnone WriteLine (“There were none to choose.”)

Page 43: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

43

External 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”)

Page 44: 1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201

44

Non-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