Models and Analysis of Software Lecture 4 VDM

  • Slides: 34
Download presentation
Models and Analysis of Software Lecture 4 VDM - Part II Jerzy. Nawrocki@put. poznan.

Models and Analysis of Software Lecture 4 VDM - Part II Jerzy. Nawrocki@put. poznan. pl www. cs. put. poznan. pl/jnawrocki/mse/models/ J. Nawrocki, Models &. . . Copyright, 2003 © Jerzy R. Nawrocki

From the previous lecture. . Introduction to VDM = Very Difficult Method • Model-based:

From the previous lecture. . Introduction to VDM = Very Difficult Method • 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. J. Nawrocki, Models &. . .

From the previous lecture. . Quantifiers That’s really different from Pascal! -- A prime

From the previous lecture. . Quantifiers That’s really different from Pascal! -- A prime number, n, is -- divisible only by 1 and n. Is. Prime (n: N 1) res: B post res k N 1 (1 < k k < n) n mod k 0 J. Nawrocki, Models &. . .

From the previous lecture. . Pre-conditions Quotient (-6, 2) = 3 Quotient (a, b:

From the previous lecture. . Pre-conditions Quotient (-6, 2) = 3 Quotient (a, b: Z) res: N pre b 0 post res = (abs a) div (abs b) J. Nawrocki, Models &. . .

From the previous lecture. . Sequences (I) -- CDs = sequence of Common Divisors

From the previous lecture. . Sequences (I) -- CDs = sequence of Common Divisors CDs (a, b: N 1) res: N 1+ post res = [k | k N 1 a mod k = 0 b mod k = 0] J. Nawrocki, Models &. . .

Plan of the lecture From the previous lecture. . Characters and strings Type invariants

Plan of the lecture From the previous lecture. . Characters and strings Type invariants Records Miscellaneous J. Nawrocki, Models &. . .

Characters and strings char - alfanumeric characters char* - possibly empty sequence of char+

Characters and strings char - alfanumeric characters char* - possibly empty sequence of char+ - nonempty sequence of char 'a' - a character literal "ABBA" - a string of chars (text) "S. Covey" = ['S', 'C', 'o', 'v', 'e', 'y'] "S. Covey"(1)= 'S' J. Nawrocki, Models &. . .

Characters and strings Reversing a string -- Reversing a string of characters reverse(t: char*)

Characters and strings Reversing a string -- Reversing a string of characters reverse(t: char*) res: char* post (t = [ ] res = [ ]) (t [ ] res = (tl t) [hd t] reverse("top") = "pot" J. Nawrocki, Models &. . .

Characters and strings Reversing a string -- Reversing a string of characters reverse(t: char*)

Characters and strings Reversing a string -- Reversing a string of characters reverse(t: char*) res: char* post (t = [ ] res = [ ]) (t [ ] res = reverse(tl t) [hd t] reverse("top") = "pot" J. Nawrocki, Models &. . . Important modification

Characters and strings Integer to text conversion Can’t be simpler? d_seq= ['0', '1', '2',

Characters and strings Integer to text conversion Can’t be simpler? d_seq= ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] -- Integer to text conversion i 2 t(i: N) t: char+ post (i=0 t="0") (i>0 t=i 2 t 1(i)) i 2 t 1(i: N) t: char* post (i=0 t= [ ]) (i>0 t=i 2 t 1(i div 10) [d_seq(i mod 10 + 1)]) J. Nawrocki, Models &. . .

Plan of the lecture From the previous lecture. . Characters and strings Type invariants

Plan of the lecture From the previous lecture. . Characters and strings Type invariants Records Miscellaneous J. Nawrocki, Models &. . .

Type invariants Declaration of invariants 0 b b 1 resembles 0 b 1 Id

Type invariants Declaration of invariants 0 b b 1 resembles 0 b 1 Id = T inv Pattern Boolean_condition Bit = N inv Bit 0 b b 1 Bit = {b | b N 0 b b 1} J. Nawrocki, Models &. . .

Type invariants Defining prime numbers More reusable and readable! Prime = N 1 inv

Type invariants Defining prime numbers More reusable and readable! Prime = N 1 inv Prime i N 1 (1<i i<a) a mod i 0 is_prime(a: N 1) res: B post res = i N 1 (1<i i<a) a mod i 0 Prime = N 1 inv Prime is_prime(a) J. Nawrocki, Models &. . .

Type invariants Using prime numbers -- Checking if every even number between a and

Type invariants Using prime numbers -- Checking if every even number between a and b -- can be represented as a sum of 2 prime numbers goldbach(a, b: N 1) res: B pre a b post res = i N 1 (a i i b i mod 2 = 0) x, y: Prime i= x+y Here the defined type is used. J. Nawrocki, Models &. . .

Plan of the lecture From the previous lecture. . Characters and strings Type invariants

Plan of the lecture From the previous lecture. . Characters and strings Type invariants Records Miscellaneous J. Nawrocki, Models &. . .

Records Record definition ‘Family. N’ stands for ‘Family Name’ Rec: : Field 1 :

Records Record definition ‘Family. N’ stands for ‘Family Name’ Rec: : Field 1 : T 1 Field 2 : T 2. . . Fieldn : Tn Worker: : Family. N: char+ First. N: char+ Hours: N J. Nawrocki, Models &. . .

Records Field selection Rec. Field Workers. File = Worker* total_hours(w: Workers. File) res: N

Records Field selection Rec. Field Workers. File = Worker* total_hours(w: Workers. File) res: N post (w=[ ] res = 0) (w [ ] res = (hd w). Hours + total_hours(tl w) Selecting the field ‘Hours’. J. Nawrocki, Models &. . .

Plan of the lecture From the previous lecture. . Characters and strings Type invariants

Plan of the lecture From the previous lecture. . Characters and strings Type invariants Records Miscellaneous J. Nawrocki, Models &. . .

Unions T 1 | T 2 Enumerated types: Signal = RED | AMBER |

Unions T 1 | T 2 Enumerated types: Signal = RED | AMBER | GREEN J. Nawrocki, Models &. . .

Optional types nil - absence of a value Optional type: type [ ] =

Optional types nil - absence of a value Optional type: type [ ] = | nil [ ] or Optional type operator: operator Expression = nil if next(P) = nil. . J. Nawrocki, Models &. . .

Explicit functions func_name: T 1 x T 2 x. . x Tn T func_name(Id

Explicit functions func_name: T 1 x T 2 x. . x Tn T func_name(Id 1, Id 2, . . , Idn) E pre B max: x x max (x, y, z) if (y x) (z x) then x elseif (x y) (z y) then y else z J. Nawrocki, Models &. . .

Polymorphic functions max [ @num ]: @num x @num max (x, y, z) if

Polymorphic functions max [ @num ]: @num x @num max (x, y, z) if (y x) (z x) then x elseif (x y) (z y) then y else z result = max [ J. Nawrocki, Models &. . . ] (1, 2, 3) ] (1. 1, 2. 2, 3. 3)

State state Id of field_list invariant_definition initialisation end state maximum of max: init mk_maximum(m)

State state Id of field_list invariant_definition initialisation end state maximum of max: init mk_maximum(m) m=0 end J. Nawrocki, Models &. . .

State state Id of Another example field_list invariant_definition initialisation end state aircraft of speed:

State state Id of Another example field_list invariant_definition initialisation end state aircraft of speed: height: inv mk_aircraft(-, h) (h 0. 0) init mk_aircraft(s, h) (s=0. 0) (h= 0. 0) end J. Nawrocki, Models &. . .

Implicit operations Op_name (Id 1: T 1, . . , Idk: Tk) Idr: Tr

Implicit operations Op_name (Id 1: T 1, . . , Idk: Tk) Idr: Tr ext Access_vars pre B post B’ Access_vars: rd or wr prefix MAX 3() ext rd x, y, z: wr max: post (x max) (y max) (z max) (max {x, y, z}) J. Nawrocki, Models &. . .

Implicit operations Old state: variable MAX_NUM(n: ) ext wr max: post (n max) (max

Implicit operations Old state: variable MAX_NUM(n: ) ext wr max: post (n max) (max = n) J. Nawrocki, Models &. . .

Error definitions PUT_YEAR(year: ) ext wr yr: pre year 1994 post yr = year

Error definitions PUT_YEAR(year: ) ext wr yr: pre year 1994 post yr = year errs yr 2 d. XIX: 94 year 99 yr= year+1900 yr 2 d. XX: year < 94 yr = year+2000 J. Nawrocki, Models &. . .

Explicit operations o T OPER_NAME: T 1 x. . x Tn OPER_NAME (Id 1,

Explicit operations o T OPER_NAME: T 1 x. . x Tn OPER_NAME (Id 1, Id 2, . . , Idn) Expression pre B o () MAX_NUM: MAX_NUM (n) if max < n then max: = n else skip J. Nawrocki, Models &. . .

Conditionals if B 1 then ES 1 elseif B 2 then ES 2. .

Conditionals if B 1 then ES 1 elseif B 2 then ES 2. . . elseif Bn then ESn else ES J. Nawrocki, Models &. . . cases Es: P 1 ES 1. . . Pn ESn others ES end

Iteration statements for Id= E 1 to E 2 by Inc do St for

Iteration statements for Id= E 1 to E 2 by Inc do St for Id in Sq do St for Id in reverse Sq do St for all Id E do St while B do St J. Nawrocki, Models &. . .

Summary Character string = sequence. Type invariants allow to define quite complicated types (e.

Summary Character string = sequence. Type invariants allow to define quite complicated types (e. g. prime numbers). Records allow do specify database-like computations. J. Nawrocki, Models &. . .

Homework • Specify a function digit 5 that returns a sequence of decimal digits

Homework • Specify a function digit 5 that returns a sequence of decimal digits of a number k (see functions digits 3 and digits 2). • Specify an example of a function that would be an implementation of a JOIN operation in a relational database. • Specify a polymorphic projection and selection operation. J. Nawrocki, Models &. . .

Further readings • A. Harry, Formal Methods Fact File, John Wiley & Sons, Chichester,

Further readings • A. Harry, Formal Methods Fact File, John Wiley & Sons, Chichester, 1996. J. Nawrocki, Models &. . .

Quality assessment 1. What is your general impression? (1 - 6) 2. Was it

Quality 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? J. Nawrocki, Models &. . .