20
Formal Specification and Z CS3300 Fall 2015

Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Embed Size (px)

Citation preview

Page 1: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Formal Specification and Z

CS3300Fall 2015

Page 2: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Formal Specification Produces a mathematical model Typically associated with analysis Differs from design diagrams because they have formal semantics Currently used in some safety critical applications, but not in

general development Issues with scaling and translation to code

Page 3: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Formal Specification Languages VDM Larch Alloy Z (zed) OCL And a host of others

Page 4: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Z Model-based notation Collection of state variables Operations that change state This is a notation, not a methodology

Page 5: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Specify a Text Editor First we define some primitive types. These are in square brackets:[CHAR]

And then some composite types:TEXT == seq CHAR

These definitions are considered global. Say we have a constraint, for this we use an axiomatic constraint

Page 6: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

maxsize :

maxsize <=65535

Then we can define a Schema – combines data and invariants

Editor

left, right : TEXT

# (left ^ right) <= maxsize

Page 7: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Now we specify what happens on initialization, Zed provides a special schema Init

Init

Editor

left = right = < >

Now we need to introduce some operations, but first we need another axiomatic definition

printing : Ƥ CHAR

Page 8: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

And now we can define our actual operation schema

Insert

Editorch? : CHAR

ch? ∈ printingleft' = left ^ <ch?>right' = right

Page 9: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

What about moving right with the arrow key?We have to recognize the difference with Insert

right_arrow : CHAR

right_arrow ∉ printingForward

Δ Editorch? : CHAR

ch ? = right_arrowleft' = left ^ < head(right) >right' = tail(right)

Page 10: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Forward

Δ Editorch? : CHAR

ch ? = right_arrowright != < >left' = left ^ < head(right) >right' = tail(right)

Completing the preconditions

But what should we do if the right is empty?

Page 11: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

T_Forward ≙ Forward ⋁ (EOF ⋀ RightArrow ⋀ Ξ Editor)

EOF

Editor

right = < >

Page 12: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Let'sTry some on our own

DeleteBackward (moves cursor back one)T_Backward

Page 13: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

How about a Birthday Book Keep track of names and datesWe define our basic types:

[NAME, DATE]

Page 14: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Basic Schema

BirthdayBookknown : ℙ NAME

birthday : NAME↦ DATE

known = dom birthday

Page 15: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

Initialization

Init

BirthdayBook

known = ∅

Page 16: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

How about adding a birthday?AddBirthday

∆ BirthdayBookname? : NAMEdate? : DATE

birthday' = birthday ∪ {name? ↦ date?}

name? ∉ known

Page 17: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

How about looking up a birthday?

FindBirthdayΞ BirthdayBookname? : NAMEdate! : DATEname? ∊ knowndate! = birthday(name?)

Page 18: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

How about find everyone whose birthday is today?

RemindΞ BirthdayBooktoday? : DATEcards! : ℙ NAMEcards! = { n : known | birthday(n) = today? }

Page 19: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

How about errors?

REPORT ::= ok | already_known | not_known

Success

result! : REPORT

result! = ok

Page 20: Formal Specification and Z CS3300 Fall 2015. Formal Specification Produces a mathematical model Typically associated with analysis Differs from design

What about adding an already known name?

AlreadyKnownΞ BirthdayBookname? : NAMEresult! : REPORTname? ∊ knownresult! = already_known

T_AddBirthday = AlreadyKnown ∨ (AddBirthday ∧ Success)