+ All Categories
Home > Documents > Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki [email protected] Models and...

Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki [email protected] Models and...

Date post: 02-Jan-2016
Category:
Upload: wilfred-lloyd
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
43
Introduction to Introduction to VDM VDM Copyright, 2003 © Jerzy R. Nawrocki [email protected] www.cs.put.poznan.pl/jnawrocki/ models/ Models and Analysis of Models and Analysis of Software Software Lecture Lecture 3 3
Transcript
Page 1: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

Introduction to Introduction to VDMVDM

Copyright, 2003 © Jerzy R. Nawrocki

[email protected]

www.cs.put.poznan.pl/jnawrocki/models/

Models and Analysis of Software Models and Analysis of Software

Lecture Lecture 33

Models and Analysis of Software Models and Analysis of Software

Lecture Lecture 33

Page 2: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

IntroductionIntroductionIntroductionIntroduction

• VDM = Vienna Development Method, IBM Laboratory

• Model-based: basic types (integer, real, ..) and compound types (sets, sequences, ..)

• Implicit specification (what?) and explicit one (how?).

• No explicit support for concurrency and time.

• Math & text notations.

VDM

VDM = VeryDifficult Method

Page 3: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

IntroductionIntroductionIntroductionIntroduction

add (a, b: N) result: Npost result = a + badd (a, b: N) result: Npost result = a + b

Simple example

It’s trivial!

Page 4: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

Introduction

Identifiers and commentsInteger numbersBoolean valuesPredicatesImplicit functionsNon-integer numbersSequencesSets

Page 5: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

IdentifiersIdentifiersIdentifiersIdentifiers

add (a, b: N) result: Npost result = a + badd (a, b: N) result: Npost result = a + b

Letter (Letter | Digit | Greek_letter | Underscore)*

Letter case is significant.

First_element

First_Element

Value_6

First_element

First_Element

Value_6

Page 6: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

CommentsCommentsCommentsComments

-- adding two numbersadd (a, b: N) result: Npost result = a + b

-- adding two numbersadd (a, b: N) result: Npost result = a + b

annotations Author: J.R. Nawrocki Written on: March 6, 2002end annotations

add (a, b: N) result: Npost result = a + b

annotations Author: J.R. Nawrocki Written on: March 6, 2002end annotations

add (a, b: N) result: Npost result = a + b

I prefer double hyphen.

Page 7: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and comments

Integer numbersBoolean valuesPredicatesImplicit functionsNon-integer numbersSequencesSets

Page 8: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Integer numbersInteger numbersInteger numbersInteger numbers

Integer types

NN Natural numbers (0, 1, 2, ..)

N1N1 Positive integers (1, 2, ..)

ZZ Integers (.., -2, -1, 0, 1, 2, ..)

Page 9: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Operators

Integer numbersInteger numbersInteger numbersInteger numbers

a + b 3 + 2 = 5

a - b 5 - 2 = 3

a b 3 2 = 6

a / b 6 / 4 = 1.5

a div b 11 div 4 = 2

a mod b 11 mod 4 = 3

a b 2 3 = 8

abs a abs -3 = 3

a + b 3 + 2 = 5

a - b 5 - 2 = 3

a b 3 2 = 6

a / b 6 / 4 = 1.5

a div b 11 div 4 = 2

a mod b 11 mod 4 = 3

a b 2 3 = 8

abs a abs -3 = 3

Looks likePascal

except for ‘’.

Page 10: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example

Integer numbersInteger numbersInteger numbersInteger numbers

f(0) = 0f(1) = 1f(2) = 3

-- f(n) = 1 + 2 + .. + nf (n: N) res: Npost res = (n+1) n / 2

-- f(n) = 1 + 2 + .. + nf (n: N) res: Npost res = (n+1) n / 2

Page 11: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbers

Boolean valuesPredicatesImplicit functionsNon-integer numbersSequencesSets

Page 12: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Boolean valuesBoolean valuesBoolean valuesBoolean values

a = b

a b

a < b

a b

a > b

a b

a = b

a b

a < b

a b

a > b

a b

Relations

true

false

true

false

Constants

a not a

a b a and b

a b a or b

a ba implies b

a b a equivalent to b

a not a

a b a and b

a b a or b

a ba implies b

a b a equivalent to b

Boolean operators

BB Boolean values

Page 13: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example

Boolean valuesBoolean valuesBoolean valuesBoolean values

Is_CD(12, 16, 4)=true -- CD = Common Divisor

-- Is k a CD for a and b?Is_CD (a, b, k: N) res: Bpost res (a mod k = 0 b mod k = 0)

-- CD = Common Divisor-- Is k a CD for a and b?Is_CD (a, b, k: N) res: Bpost res (a mod k = 0 b mod k = 0)

Page 14: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbersBoolean values

PredicatesImplicit functionsNon-integer numbersSequencesSets

Page 15: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Quantifiers

PredicatesPredicatesPredicatesPredicates

For all (universal q.)

Exists (existential q.)

!! Exists one (unique q.)

Page 16: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

-- A prime number, n, is-- divisible only by 1 and n.

IsPrime (n: N1) res: B

post res k N1 (1 < k k < n)

n mod k 0

-- A prime number, n, is-- divisible only by 1 and n.

IsPrime (n: N1) res: B

post res k N1 (1 < k k < n)

n mod k 0

Example

PredicatesPredicatesPredicatesPredicates

That’s reallydifferent from

Pascal!

Page 17: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbersBoolean valuesPredicates

Implicit functionsNon-integer numbersSequencesSets

Page 18: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

General form

Implicit functionsImplicit functionsImplicit functionsImplicit functions

function_name (Ids1: T1, .., Idsk: Tk) Id_r: T

pre B

post B’

function_name (Ids1: T1, .., Idsk: Tk) Id_r: T

pre B

post B’

Optionalpre-condition

Page 19: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example

Implicit functionsImplicit functionsImplicit functionsImplicit functions

Quotient (-6, 2) = 3

Quotient (a, b: Z) res: Npre b 0post res = (abs a) div (abs b)

Quotient (a, b: Z) res: Npre b 0post res = (abs a) div (abs b)

Page 20: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbersBoolean valuesPredicatesImplicit functions

Non-integer numbersSequencesSets

Page 21: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Non-integer numbersNon-integer numbersNon-integer numbersNon-integer numbers

Non-integer types

QQ Rationals (2, 1/4, 3.8, ..)

RR Real numbers (2.0, 3.8, 2, ..)

Page 22: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Operators

Non-integer numbersNon-integer numbersNon-integer numbersNon-integer numbers

a + b 3 + 0.2 = 3.2

a - b 5 - 0.2 = 4.8

a b 3.1 2 = 6.2

a / b 6.0 / 4 = 1.5

a b 2.0 3 = 8.0

abs a abs -3.1 = 3.1

floor a floor 3.9 = 3

a + b 3 + 0.2 = 3.2

a - b 5 - 0.2 = 4.8

a b 3.1 2 = 6.2

a / b 6.0 / 4 = 1.5

a b 2.0 3 = 8.0

abs a abs -3.1 = 3.1

floor a floor 3.9 = 3

Where isdiv and mod?

Page 23: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example

Non-integer numbersNon-integer numbersNon-integer numbersNon-integer numbers

-- CV = Cuboid VolumeCV (a, b, h: R) res: Rpost res = a b h

-- CV = Cuboid VolumeCV (a, b, h: R) res: Rpost res = a b h

h

ab

Page 24: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbersBoolean valuesPredicatesImplicit functionsNon-integer numbers

SequencesSets

Page 25: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SequencesSequencesSequencesSequences

Type constructors

T*T* General sequence (possibly empty)

T+T+ Non-empty sequence

What isa sequence?

[ 1, 5, 5, 1]

First Second Third

Page 26: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Operators

SequencesSequencesSequencesSequences

[ ] empty sequence

hd X hd [14, 15, 16] = 14

tl X tl [14, 15, 16] = [15, 16]

len X len [14, 15, 16] = 3

inds X inds [14, 15, 16] = {1, 2, 3}

elems X elems [14, 15, 14] = {14, 15}

X(n) [14, 15, 14](2) = 15

X(l,...,u)[14, 15, 16](2,...,3) = [15, 16]

[ ] empty sequence

hd X hd [14, 15, 16] = 14

tl X tl [14, 15, 16] = [15, 16]

len X len [14, 15, 16] = 3

inds X inds [14, 15, 16] = {1, 2, 3}

elems X elems [14, 15, 14] = {14, 15}

X(n) [14, 15, 14](2) = 15

X(l,...,u)[14, 15, 16](2,...,3) = [15, 16]

Page 27: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SequencesSequencesSequencesSequences

s1 s2 [6, 5] [2, 4, 9] = [6, 5, 2, 4, 9]s1 s2 [6, 5] [2, 4, 9] = [6, 5, 2, 4, 9]

Sequence concatenation

Page 28: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Sequence comprehension

SequencesSequencesSequencesSequences

[ E | Id S Boolean_condition ][ E | Id S Boolean_condition ]

Expression Subset of R Selects a finitesubset of S

Evens_to_10 = [ 2n | n N1 n < 6 ]

Evens_to_10 = [ 2, 4, 6, 8, 10 ]

Evens_to_10 = [ 2n | n N1 n < 6 ]

Evens_to_10 = [ 2, 4, 6, 8, 10 ]

Page 29: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example (I)

SequencesSequencesSequencesSequences

-- CDs = sequence of Common Divisors

CDs (a, b: N1) res: N1+

post res = [k | k N1 a mod k = 0 b mod k = 0]

-- CDs = sequence of Common Divisors

CDs (a, b: N1) res: N1+

post res = [k | k N1 a mod k = 0 b mod k = 0]

Page 30: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example (II)

SequencesSequencesSequencesSequences

-- Max = maximum element of a sequence

Max (s: N1+) m: N1

post (tl s = [ ] m = hd s) (tl s [ ] hd s Max(tl s) m = hd s) (tl s [ ] hd s < Max(tl s) m = Max(tl s))

-- Max = maximum element of a sequence

Max (s: N1+) m: N1

post (tl s = [ ] m = hd s) (tl s [ ] hd s Max(tl s) m = hd s) (tl s [ ] hd s < Max(tl s) m = Max(tl s))

Recursion Recursion

Page 31: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Example (III)

SequencesSequencesSequencesSequences

-- GCD = Greatest Common Divisor

GCD (a,b: N1) res: N1

post res= Max (CDs (a, b))

-- GCD = Greatest Common Divisor

GCD (a,b: N1) res: N1

post res= Max (CDs (a, b))

Is Max necessary?Can’t we make it

simpler?

Page 32: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Plan of the lecturePlan of the lecturePlan of the lecturePlan of the lecture

IntroductionIdentifiers and commentsInteger numbersBoolean valuesPredicatesImplicit functionsNon-integer numbersSequences

Sets

Page 33: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

B - Boolean (true, false)

N1 - positive integers (1, 2, 3, ..)

N - natural numbers (including 0)

Z - integers

Q - rationals

R - reals

B - Boolean (true, false)

N1 - positive integers (1, 2, 3, ..)

N - natural numbers (including 0)

Z - integers

Q - rationals

R - reals

SetsSetsSetsSets

Basic sets

x BasicSet x BasicSet

Basic setsor

basic types?

Page 34: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

T-set a finite set of values of type TT-set a finite set of values of type T

SetsSetsSetsSets

Finite sets

N-set a finite set of natural numbers

R-set a finite set of reals

R-set-set a finite set of finite sets of reals

N-set a finite set of natural numbers

R-set a finite set of reals

R-set-set a finite set of finite sets of reals

Page 35: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

{E | B1, B2, ..., Bn Boolean_condition }{E | B1, B2, ..., Bn Boolean_condition }

SetsSetsSetsSets

Set values

{ } empty set

{0, 2, 4} explicit set value

{2, ..., 5} = {2, 3, 4, 5}

{2n | nN n<3} = {0, 2, 4}

{ } empty set

{0, 2, 4} explicit set value

{2, ..., 5} = {2, 3, 4, 5}

{2n | nN n<3} = {0, 2, 4}

{[a, b] | aN, bN b = aa a 3}{[a, b] | aN, bN b = aa a 3}

Onlyfinitesets!

Page 36: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SetsSetsSetsSets

Finite set operators (I)

x S belongs to

x S does not belong to

card S cardinality of S

S1 = S2 equals

S1 S2 does not equal

S1 S2 S1 is a subset of S2

S1 S2 S1 is a proper subset of S2

x S belongs to

x S does not belong to

card S cardinality of S

S1 = S2 equals

S1 S2 does not equal

S1 S2 S1 is a subset of S2

S1 S2 S1 is a proper subset of S2

Onlyfinitesets!

Page 37: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SetsSetsSetsSets

Finite set operators (II)

S1 S2 union

S1 S2 intersection

S1\ S2difference

F S power set of S

S1 S2 union

S1 S2 intersection

S1\ S2difference

F S power set of S

Onlyfinitesets!

Page 38: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SetsSetsSetsSets

A set of decimal digits of a number k

digit = {0, ..., 9}

digits1(k: N) res: digit-setpost res = {k mod 10} digits1(k div 10)

digit = {0, ..., 9}

digits1(k: N) res: digit-setpost res = {k mod 10} digits1(k div 10)

Doesnot

work!

Page 39: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SetsSetsSetsSets

A set of decimal digits of a number k

digits2(k: N) res: digit-setpost (k=0 res = { }) (k>0 res = {k mod 10} digits2(k div 10))

digits2(k: N) res: digit-setpost (k=0 res = { }) (k>0 res = {k mod 10} digits2(k div 10))

Whatif

k=0?

digits3(k: N) res: digit-setpost (k=0 res = { 0 }) (k>0 res = digits2(k))

digits3(k: N) res: digit-setpost (k=0 res = { 0 }) (k>0 res = digits2(k))

Page 40: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

SummarySummarySummarySummary

VDM is a formal method.

Its basic types are similar to those in Pascal, C, ..

It contains quantifiers.

Finite sequence is quite a powerful mechanism.

VDM allows for recursion.

Page 41: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Further readingsFurther readingsFurther readingsFurther readings

• A. Harry, Formal Methods Fact File, John Wiley & Sons, Chichester, 1996, pages 93-170.

Page 42: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

HomeworkHomeworkHomeworkHomework

• Write a shorter definition of GCD.• Specify the factorial.• Specify the least common

multiply.• Specify a function that checks if

n is an automorphic number (i.e. if n appears in a decimal representation of its square).

• Specify a total of decimal digits of a given number n.

Page 43: Introduction to VDM Copyright, 2003 © Jerzy R. Nawrocki Jerzy.Nawrocki@put.poznan.pl  Models and Analysis of Software.

J. Nawrocki, Models ... (3)

Quality assessmentQuality assessmentQuality assessmentQuality assessment

1. What is your general impression? (1 - 6)

2. Was it too slow or too fast?

3. What important did you learn during the lecture?

4. What to improve and how?


Recommended