79
1 Formal Specification Formal Specification (continued) (continued) Lecture 3: Lecture 3: (SE 252/502) Dr. RAJIB Mall

1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

Embed Size (px)

Citation preview

Page 1: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

1

Formal SpecificationFormal Specification

(continued) (continued) Lecture 3: Lecture 3: (SE 252/502)

Dr. RAJIB Mall

Page 2: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

2

Organization of this Organization of this LectureLecture• Overview of last lecture• Algebraic specification:

– Development technique– Rewrite rules– Example– Pros and cons

• State machine modelling• Summary

Page 3: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

3

Review of last Review of last lecturelecture• Formal specification

techniques:–model-oriented

–property-oriented

Page 4: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

4

Review of last Review of last lecturelecture• Property-oriented techniques:

–axiomatic specification–algebraic specification

• Model-oriented techniques:–Z, VDM, Petri net, State

machine, etc.

Page 5: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

5

Overview of last Overview of last lecturelecture• Axiomatic techniques:

–based on early work on program verification.

–Use first-order predicate logic:• specify operations through pre and post conditions

Page 6: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

6

Review of last Review of last lecturelecture• Algebraic technique:

–data types are viewed as heterogeneous algebra

–axioms are used to state properties of data type operations

Page 7: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

7

Algebraic SpecificationAlgebraic Specification

• Using algebraic specification: –the meaning of a set of

interface procedures is defined by using equations.

Page 8: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

8

Algebraic Algebraic SpecificationSpecification• Algebraic specifications are

usually presented in four parts:– types section

– exceptions section

– signature section

– rewrite rules section

Page 9: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

9

Types SectionTypes Section• Types Section Lists:

–sorts (or types) being specified

–sorts being imported

–Importing a sort:• makes it available in specification.

Page 10: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

10

Exception SectionException Section• Lists names of exceptional

conditions used in later sections:–under exceptional conditions

error should be indicated.

Page 11: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

11

Signature SectionSignature Section• Defines signatures of interface

procedures: –e.g. PUSH takes a stack and an

element and returns a new stack.

–push: • stack element stack

Page 12: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

12

Rewrite rules Rewrite rules sectionsection• Lists the properties of the

operators:–In the form of a set of axioms

or rewrite rules. • allowed to have conditional expressions

Page 13: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

13

Developing Algebraic Developing Algebraic SpecificationSpecification• The first step in defining an

algebraic specification: –identify the set of required

operations.–e.g. for string identify operations:

• create, compare, concatenate, length, etc.

Page 14: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

14

Developing Algebraic Developing Algebraic SpecificationSpecification• Generally operations fall into

2 classes:

• Constructor Operations :–Operations which create or

modify entities of the sort e.g., create, update, add, etc.

Page 15: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

15

Developing Algebraic Developing Algebraic SpecificationSpecification• Inspection Operations :

–Operations which evaluate attributes of the sort, e.g., eval, get, etc.

Page 16: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

16

Developing Algebraic Developing Algebraic SpecificationSpecification• A rule of thumb for writing

algebraic specifications:

–first establish constructor and inspection operations

Page 17: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

17

Developing Algebraic Developing Algebraic SpecificationsSpecifications• Next, write down

axioms:– compose each inspection operator over each constructor operator.

Page 18: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

18

Developing Algebraic Developing Algebraic SpecificationsSpecifications• If there are m constructors

and n inspection operators: –we should normally have m*n

axioms.

–However, an exception to this rule exists.

Page 19: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

19

Developing Algebraic Developing Algebraic SpecificationsSpecifications• If a constructor operation can

be defined using other constructors:–we need to define inspection

operations using only primitive constructors.

Page 20: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

20

Example: StackExample: Stack• Let us specify an unbounded

stack supporting:–push, –pop, –newstack, –top, –empty.

Page 21: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

21

Example: StackExample: Stack• Types:

• defines stack

• uses boolean, element

• Exception:

• underflow, novalue

Page 22: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

22

Example: stackExample: stack• Syntax:

• push: –stack element stack

• pop: –stack stack+{underflow}

Page 23: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

23

Example: stackExample: stack• top:

–stack element+{novalue}• empty:

–stack boolean • newstack:

– stack

Page 24: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

24

Equations: stackEquations: stack• pop(newstack)=underflow

• pop(push(s,e))=s

• top(newstack)=novalue

• top(push(s,e))=e

• empty(newstack)=true

• empty(push(s,e))=false

Page 25: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

25

Rewrite rulesRewrite rules• Rewrite rules let you

determine:–the meaning of any sequence

of calls on the stack functions.

Page 26: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

26

Rewrite rulesRewrite rules• Empty(push(pop(push(newsta

ck,e1)),e2)):

– you can eliminate the call on pop by observing: • it is of the form pop(push(s,e)).

Page 27: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

27

Rewrite rulesRewrite rules

• After simplification:–empty(push(newstack,e1))

–false

Page 28: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

28

Two important Two important questionsquestions• Finite termination property:

– Does application of rewrite rules terminate after a finite number of steps?

– We might endlessly go on applying rewrite rules without coming to any conclusion?

Page 29: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

29

Two important Two important questionsquestions• Unique termination property:

– Can different sequence in application of the rewrite rules always give the same answer?

– If we choose to simplify different terms of the expression in different experiments:• shall we always get the same answer?

Page 30: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

30

Algebraic Algebraic SpecificationSpecification• For arbitrary algebraic

equations: – convergence is undecidable.

• If the r.h.s. of each rewrite rule has fewer terms than the left: – rewrite process must terminate.

Page 31: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

31

Auxiliary FunctionsAuxiliary Functions• Sometimes development of a

specification requires:–extra functions not part of the

system:• to define the meaning of some interface procedures.

Page 32: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

32

Auxiliary Functions: Auxiliary Functions: ExampleExample• To specify bounded stacks:

–need to add a depth function: •push returns either a stack or

•an exception “overflow” when depth is exceeded.

Page 33: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

33

Bounded stackBounded stack• In order to specify a bounded

stack: –we need to make changes to

different sections to include auxiliary functions.

Page 34: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

34

Auxiliary FunctionsAuxiliary Functions• Syntax:

• push: –stack element ==> stack

• depth: –stack integer

Page 35: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

35

Auxiliary FunctionsAuxiliary Functions• Equations:

–depth(newstack)=0

–depth(push(s,e))=depth(s)+1

–push(s,e)=overflow if depth(s) >= Max

Page 36: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

36

Example 2: coordExample 2: coord

• Types:–sort coord

–imports integer, boolean

Page 37: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

37

Example: coordExample: coord• Signature:

–create(integer,integer) coord

–X(coord) integer

–Y(coord) integer

–Eq(coord,coord) boolean

Page 38: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

38

Example: coordExample: coord• Rewrite rules:

–X(create(x,y))=x

–Y(create(x,y))=y

–Eq(create(x1,y1),create(x2,y2))= ((x1=x2) and (y1=y2))

Page 39: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

39

Structured Structured SpecificationsSpecifications• Writing formal specifications is time

consuming.

• To reduce effort, we need to reuse specifications:– instantiation of generic specifications

– incremental development of specifications

Page 40: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

40

Specification Specification InstantiationInstantiation• Take an existing

specification:–specified with some generic

parameter

–Instantiate with some sort

Page 41: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

41

Incremental Incremental DevelopmentDevelopment• Develop specifications for

simple sorts:–using these specify more

complex entities.

Page 42: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

42

Pros and Cons Pros and Cons • Algebraic specifications

have a strong mathematical basis:–can be viewed as

heterogeneous algebra.

Page 43: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

43

Pros and ConsPros and Cons• An important shortcoming of

algebraic specifications: –cannot deal with side effects

–difficult to use with common programming languages.

Page 44: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

44

Pros and ConsPros and Cons• Algebraic specifications are

hard to understand: –also changing a single property

of the system • may require changing several equations.

Page 45: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

45

Specification of Specification of timing constraintstiming constraints• Timing constraints:

–expressed in terms of occurrence of certain events.

Page 46: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

46

EventsEvents• A stimulus to the system from its

environment.• Can also be an externally

observable response: – that the system makes to its

environment• Events can be instantaneous

– or assumed to have a duration

Page 47: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

47

Types of timing Types of timing constraintsconstraints• Performance constraints:

– constraints imposed on the response of the system.

• Behavioral constraints:– constraints imposed on the action

and reaction time of the environment (or the user).

Page 48: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

48

Specification of Specification of timing constraintstiming constraints• We will specify timing

constraints:–in terms of stimuli and

response

–modelled as FSMs (Finite State Machines)

Page 49: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

49

State machine State machine modellingmodelling• Assumes that at any time:

–the system is in one of a number of possible states.

• When a stimulus is received, –it may cause a transition to a

different state.

Page 50: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

50

Finite Automaton Finite Automaton with Outputwith Output• A set of states• final state:

– some states designated as final states• an alphabet of input symbols• an alphabet of output symbols• a transition function:

– maps a combination of states and input symbols to states

Page 51: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

51

RepresentationRepresentation

Await firstdigit

Await seconddigit

DialAgain

Await nextdigit

First digit/start-timer(20)

Timer alarm/dial-tone

Second digit/silence

Event

Action

Page 52: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

52

Types of finite Types of finite automatonautomaton• A finite automaton with output

may be organized in two ways:– A Moore machine is an automaton

• each state is associated with an output symbol

– A Mealy machine associates each transition with an output symbol

Page 53: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

53

Classification:Classification: • Three types of timing

constraints:–Minimum

–Maximum

–Durational

Page 54: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

54

MinimumMinimum•Between two events:

–No less than t time units may elapse

e1 e2

Page 55: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

55

MaximumMaximum•Between two events:

–No more than t time units may elapse

e1 e2

Page 56: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

56

DurationalDurational

•An event must occur for t units of time

Page 57: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

57

MaximumMaximum• S-S (stimulus-stimulus)

• S-R (stimulus-response)

• R-S (response-stimulus)

• R-R (response - response)

Page 58: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

58

Maximum S-SMaximum S-S• Maximum time between

occurrence of two stimuli:–e.g. after dialling first digit,

• the second digit should be dialled no more than 20 secs later.

Page 59: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

59

FSM RepresentationFSM Representation• To represent timing

constraints in an FSM:–a timer alarm is used as an artificial stimulus.

Page 60: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

60

RepresentationRepresentation

Await firstdigit

Await seconddigit

DialAgain

Await nextdigit

First digit/start-timer(20)

Timer alarm/dial-tone

Second digit/silence

Event

Action

Page 61: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

61

Maximum S-RMaximum S-R• Maximum time between

stimulus and response:–e.g. caller shall receive dial

tone no later than 20 secs after lifting the receiver.

Page 62: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

62

RepresentationRepresentationAwait FirstDigit

Offhook/Start timer(20)

Await SecondDigit

DialAgain

Alarm/Dial tone

Alarm/Dial toneStart

timer (20)

FirstDigit

SecondDigit

Page 63: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

63

Complete Complete RepresentationRepresentation

Await FirstDigit

Offhook/Start timer(20)

Await SecondDigit

DialAgain

Alarm/Dial toneStart

timer (20)

SecondDigit

Await EighthDigit

DialComplete

Alarm/Dial tone

Alarm/Dial tone

Eighth digit/Ring tone

Page 64: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

64

Maximum R-SMaximum R-S• Maximum time between system’s

response and the next stimulus from the environment:– e.g after receiving the dial tone,

the caller shall dial the first digit within 20 sec.

Page 65: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

65

Maximum R-RMaximum R-R• Maximum time allowed between

two responses:– after a connection has been made,

• the caller will receive a ring back tone no more than 0.5 sec after the callee has received the ring tone.

Page 66: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

66

Minimum Minimum constraintsconstraints• S-S

• R-S

• R-R

Page 67: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

67

Minimum S-SMinimum S-S• A minimum time is required

between two stimuli:–e.g. a minimum of 0.5 sec must

elapse between the dialling of one digit and the dialling of the next.

Page 68: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

68

Minimum S-SMinimum S-S• This is an example of behavioral

constraints on system users:– the complete specification should

include • response the system should make if

the user responds too soon.

Page 69: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

69

RepresentationRepresentation

Handledigit

Await Next Digit

Await Caller

On hook

First digit/start-timer(0.5)

Timer alarm/

Second digit/beeping

Second digit/silence

Page 70: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

70

Durational timing Durational timing constraintsconstraints• To go back to the

international operator–press the button for at least

15 secs (but no more than 30 secs)

Page 71: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

71

Durational timing Durational timing constraintsconstraints• To get back to the local

operator:–press the button for at least 30

secs, but no more than 45 secs

• To receive a dial tone press the button for at least 45 secs.

Page 72: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

72

RepresentationRepresentation

A B C

D

E

Set timer 15,30,45

Timer alarm(15)

Timer alarm(30)

Timer alarm(45)

Page 73: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

73

ReferenceReference• Ian Somerville, Chapter

Chapter 10

• R. Mall, Chapter 3

• B. Dasarathy, “Timing constraints of R-T systems,” IEEE TSE, 1985, pp. 80--86.

Page 74: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

74

SummarySummary• We started by discussing

some general concepts in:– formal specification

techniques.

Page 75: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

75

SummarySummary• Formal requirements

specifications: –have several positive

characteristics.

–But the major shortcoming is that they are hard to use.

Page 76: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

76

SummarySummary• It is possible that formal

techniques will become more usable in future –with the development of

suitable front-ends.

Page 77: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

77

SummarySummary• We discussed a sample

specification technique, –algebraic specification

–gives us a flavour of the issues involved in formal specification.

Page 78: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

78

SummarySummary• We discussed specification of

timing constraints:–classification

–modelling using FSMs

Page 79: 1 Formal Specification (continued) Lecture 3: (SE 252/502) Dr. RAJIB Mall

79

Next LectureNext Lecture• Real-time system design:

–Structured analysis of system behavior

–Ward and Mellor technique