An introduction to specification in VDM-SL

62
An introduction to specification in VDM-SL At the end of this lecture you should be able to: write a formal specification of a system in VDM-SL; correlate the components of a UML class diagram with those of a VDM specification; declare constants and specify functions to enhance the specification; explain the use of a state invariant to place a global constraint on the system; explain the purpose of the nil value in VDM.

description

An introduction to specification in VDM-SL. At the end of this lecture you should be able to:. write a formal specification of a system in VDM-SL ; correlate the components of a UML class diagram with those of a VDM specification ; - PowerPoint PPT Presentation

Transcript of An introduction to specification in VDM-SL

Page 1: An introduction to specification in VDM-SL

An introduction to specification in VDM-SL

At the end of this lecture you should be able to:

• write a formal specification of a system in VDM-SL;

• correlate the components of a UML class diagram with those of a VDM specification;

• declare constants and specify functions to enhance the specification;

• explain the use of a state invariant to place a global constraint on the system;

• explain the purpose of the nil value in VDM.

Page 2: An introduction to specification in VDM-SL

The Incubator case study

The temperature of the incubator needs to be carefully controlled and monitored;

Safety requirements :

-10 Celsius TEMPERATURE +10 Celsius

Page 3: An introduction to specification in VDM-SL

The UML specification

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Page 4: An introduction to specification in VDM-SL

Specifying the ‘state’ in VDM-SL

Page 5: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Page 6: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

The VDM state refers to the permanent data stored by the system.

In VDM-SL we use mathematical types

Page 7: An introduction to specification in VDM-SL

The intrinsic types available in VDM-SL

Page 8: An introduction to specification in VDM-SL

: natural numbers (positive whole numbers)

1 : natural numbers excluding zero

: integers (positive and negative whole numbers)

: real numbers (positive and negative numbers that can include a fractional part)

: boolean values (true or false)

Char : the set of alphanumeric characters

Page 9: An introduction to specification in VDM-SL

Specifying the state of the Incubator Monitor System

Page 10: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

state IncubatorMonitor of

end

temp :

UML VDM-SL

Page 11: An introduction to specification in VDM-SL

Specifying the operations in VDM-SL

Page 12: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Each operation specified in VDM-SL as follows:

the operation headerthe external clausethe preconditionthe postcondition

Page 13: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Page 14: An introduction to specification in VDM-SL

increment()

ext ?

pre ?

post ?

temp < 10

wr ?temp :

temp = + 1temp

+ 1 = temptemp

temp - = 1temp

temp > temp

Page 15: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Page 16: An introduction to specification in VDM-SL

decrement()

ext ?

pre ?

post ?

temp > -10

temp = - 1temp

wr ?temp :

Page 17: An introduction to specification in VDM-SL

IncubatorMonitor

temp : Integer

increment() decrement()getTemp() : Integer

Page 18: An introduction to specification in VDM-SL

getTemp( )

ext ?

pre ?

post ?

currentTemp :

rd temp :

currentTemp = temp

TRUE

Page 19: An introduction to specification in VDM-SL

Declaring constants

Page 20: An introduction to specification in VDM-SL

Constants are specified using the keyword values.

The declaration would come immediately before the state definition:

valuesMAX : = 10MIN : = -10

decrement()

ext wr temp :

pre temp > -10

post temp = - 1temp

MIN

Page 21: An introduction to specification in VDM-SL

Specifying functions

Page 22: An introduction to specification in VDM-SL

hasPassed

36

79

50

FALSE

TRUE

Page 23: An introduction to specification in VDM-SL

There are two ways in which we can specify a function in VDM-SL:

Explicitly and implicitly

Page 24: An introduction to specification in VDM-SL

Specifying a function explicitly

Example

add: add(x, y) ∆ x + y

signature definition

Page 25: An introduction to specification in VDM-SL

Specifying a function implicitly

add( )pre ?post ?

x , y: : z

z = x + yTRUE

:

Page 26: An introduction to specification in VDM-SL

An absolute function defined implicitly

abs( )pre ?post ?

z :

r :

z<0 r = -z z 0 r = zTRUE

Page 27: An introduction to specification in VDM-SL

An absolute function defined explicitly

abs: abs(z) ∆ if z < 0

then -z else z

Page 28: An introduction to specification in VDM-SL

Two special functions

The state invariant and initialisation

Page 29: An introduction to specification in VDM-SL

inv

State

Returns true if the state meets global constraint and false otherwise

Page 30: An introduction to specification in VDM-SL

Adding a state invariant into the IncubatorMonitor system

inv ? ?

-10 Celsius TEMPERATURE +10 Celsius

Page 31: An introduction to specification in VDM-SL

Adding a state invariant into the IncubatorMonitor system

inv mk-IncubatorMonitor(t) ?

-10 Celsius TEMPERATURE +10 Celsius

Page 32: An introduction to specification in VDM-SL

Adding a state invariant into the IncubatorMonitor system

inv mk-IncubatorMonitor(t) MIN t MAX

-10 Celsius TEMPERATURE +10 Celsius

Page 33: An introduction to specification in VDM-SL

init

State

Returns true if the correct initial values have been given to the state and false otherwise

Page 34: An introduction to specification in VDM-SL

Specifying an initialization function

We will assume that when the incubator is turned on, its temperature should be adjusted until a steady 5 degrees Celsius is obtained.

init ? ?

Page 35: An introduction to specification in VDM-SL

Specifying an initialization function

We will assume that when the incubator is turned on, its temperature should be adjusted until a steady 5 degrees Celsius is obtained.

init mk-IncubatorMonitor(t) ?

Page 36: An introduction to specification in VDM-SL

Specifying an initialization function

We will assume that when the incubator is turned on, its temperature should be adjusted until a steady 5 degrees Celsius is obtained.

init mk-IncubatorMonitor(t) t = 5

Page 37: An introduction to specification in VDM-SL

The modified state specification

valuesMAX : = 10MIN : = -10

state IncubatorMonitor of temp : inv mk-IncubatorMonitor(t) MIN t MAX

init mk-IncubatorMonitor(t) t = 5

end

Page 38: An introduction to specification in VDM-SL

Improving the Incubator System

IncubatorController

requestedTemp : IntegeractualTemp : Integer

setIInitialTemp(Integer)requestChange(Integer) : Signalincrement( ) : Signaldecrement( ) : SignalgetRequestedTemp( ) : IntegergetActualTemp( ) : Integer

Page 39: An introduction to specification in VDM-SL

Improving the Incubator System

IncubatorController

requestedTemp : IntegeractualTemp : Integer

setIInitialTemp(Integer)requestChange(Integer) : Signalincrement( ) : Signaldecrement( ) : SignalgetRequestedTemp( ) : IntegergetActualTemp( ) : Integer

Signal is an enumerated type

Page 40: An introduction to specification in VDM-SL

A standard method of marking a UML class as an enumerated type is to add <<enumeration>> above the type name:

Enumerated types in UML

<<enumeration>>Signal

INCREASEDECREASE

DO_NOTHING

Page 41: An introduction to specification in VDM-SL

In VDM-SL the types clause is the appropriate place to define new types.

Enumerated types in VDM-SL

typesSignal = <INCREASE>|< DECREASE>|< DO_NOTHING>

values…..

state…..

end

Page 42: An introduction to specification in VDM-SL

The nil value

It is common in the programming world for a value to be undefined

VDM-SL allows for this concept by including the possibility of a term or expression having the value nil, meaning that it is undefined;

x : ‘x’ must be a natural number

Page 43: An introduction to specification in VDM-SL

The nil value

It is common in the programming world for a value to be undefined

VDM-SL allows for this concept by including the possibility of a term or expression having the value nil, meaning that it is undefined;

x : [] ‘x’ can be a natural number or nil

Page 44: An introduction to specification in VDM-SL

The nil value

It is common in the programming world for a value to be undefined

VDM-SL allows for this concept by including the possibility of a term or expression having the value nil, meaning that it is undefined;

x : []

When the incubator system first comes into being, the actual and requested values will be undefined, and must therefore be set to nil.

Page 45: An introduction to specification in VDM-SL

Specifying the IncubatorController state

state IncubatorController of

requestedTemp : ?

actualTemp : ?

IncubatorController

requestedTemp : IntegeractualTemp : Integer

setIInitialTemp(Integer)requestChange(Integer) : Signalincrement() : Signaldecrement() : SignalgetRequestedTemp() : IntegergetActualTemp() : Integer

Page 46: An introduction to specification in VDM-SL

Specifying the IncubatorController state

state IncubatorController of

requestedTemp :

actualTemp :

IncubatorController

requestedTemp : IntegeractualTemp : Integer

setIInitialTemp(Integer)requestChange(Integer) : Signalincrement() : Signaldecrement() : SignalgetRequestedTemp() : IntegergetActualTemp() : Integer

Page 47: An introduction to specification in VDM-SL

Specifying the IncubatorController state

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]

IncubatorController

requestedTemp : IntegeractualTemp : Integer

setIInitialTemp(Integer)requestChange(Integer) : Signalincrement() : Signaldecrement() : SignalgetRequestedTemp() : IntegergetActualTemp() : Integer

Page 48: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

MIN r MAX

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]

The requested temperature

must be in the range of -10 to +10 degrees

Page 49: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

MIN r MAX

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]

The requested temperature

must be in the range of -10 to +10 degrees

The requested temperature could be nil

r = nil

Page 50: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]

The requested temperature

must be in the range of -10 to +10 degrees

The requested temperature could be nil

(MIN r MAX r = nil)

Page 51: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]The actual temperature

must be in the range of -10 to +10 degrees

(MIN r MAX r = nil)

MIN a MAX

Page 52: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]The actual temperature

must be in the range of -10 to +10 degrees

(MIN r MAX r = nil)

MIN a MAX

The actual temperature could be nil

a = nil

Page 53: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]The actual temperature

must be in the range of -10 to +10 degrees

(MIN r MAX r = nil)

(MIN a MAX a = nil)

The requested temperature

must be in the range of -10 to +10 degrees

The actual temperature could be nil

The requested temperature could be nil

Page 54: An introduction to specification in VDM-SL

The invariant

inv mk-IncubatorController (r, a)

state IncubatorController of

requestedTemp : [ ]

actualTemp : [ ]

(MIN r MAX r = nil)

(MIN a MAX a = nil)

Page 55: An introduction to specification in VDM-SL

Improving the readability of the spec by using a function

inRange( )pre post

val : result :

result MIN val MAX

TRUE

inv mk-IncubatorController (r, a) (inRange(r) r = nil) (inRange(a) a = nil)

Page 56: An introduction to specification in VDM-SL

The initialisation function

init mk-IncubatorController (r, a) r = nil a = nil

Page 57: An introduction to specification in VDM-SL

Specifying the setInitialTemp operation

setInitialTemp( )

ext

pre

post

tempIn :

wr actualTemp : []

actualTemp = tempIn

inRange(tempIn) actualTemp = nil

Page 58: An introduction to specification in VDM-SL

The requestChange operation

requestChange( )

ext

pre

post

tempIn : signalOut : Signal

requestedTemp : []wr

actualTemp : []rd

requestedTemp = tempIn

(

)

signalOut = <INCREASE>

signalOut = <DECREASE>

signalOut = <DO_NOTHING>

tempIn < actualTemp

tempIn > actualTemp

tempIn = actualTemp

actualTemp nilinRange(tempIn)

Page 59: An introduction to specification in VDM-SL

The increment operation

increment ()

ext

pre

post

signalOut : Signal

requestedTemp : []rd

actualTemp : []wr

actualTemp

actualTemp = actualTemp + 1

signalOut = <INCREASE>

signalOut = <DO_NOTHING>

(

)

actualTemp < requestedTemp

actualTemp = requestedTemp

actualTemp < requestedTemp

requestedTemp nil actualTemp nil

Page 60: An introduction to specification in VDM-SL

The getRequestedTemp operation

getRequestedTemp()

ext

pre

post

currentRequested : []

requestedTemp : []rd

currentRequested = requestedTemp

TRUE

Page 61: An introduction to specification in VDM-SL

The getActualTemp operation

getActualTemp()

ext

pre

post

currentActual : []

actualTemp : []rd

currentActual = actualTemp

TRUE

Page 62: An introduction to specification in VDM-SL

A standard template for VDM-SL specifications

typesSomeType = …..

valuesconstantName : ConstantType = someValue

state SystemName ofattribute1 : Type

:attributen : Type

inv mk-SystemName(i1:Type, ..., in:Type) Expression(i1, ..., in)

init mk-SystemName(i1:Type, ..., in:Type) Expression(i1, ..., in)end functions

specification of functions .....operations

specification of operations .....