Formal methods in
software engineering
Lec 4
Elements of Z
Prof Engr Faiz ul haque Zeya
Topics in this lecture
 Sets and types,
 declarations,
 variables,
 expressions,
 operators,
 predicates,
 equations and
 laws.
Sets
Displaying sets
 The obvious way to describe a set is to list or enumerate all of its members or elements. This is
called a set display. In Z we follow the ordinary mathematical convention and write sets with
braces, separating elements by commas. Here is a display of the set of lamps in a traffic light:
 {red, yellow, green]
Naming sets
 To write a program that simulates a dice game, we need a set that contains
the numbers of spots found on the faces of dice: {1,2, 3,4, 5,6). Z does
provide notation for a range of consecutive numbers; we can abbreviate this 1
.. 6 (without braces) pronounced one up to six
Types
 In Z we can only form sets from objects that are similar in some way. We say
that elements of the same set must have the same type; sets in Z are typed
 {2,4, red, yellow, 6} [TYPE ERROR! Elements have different types.]
Type and set
 Types and sets are very closely related. Every type has a carrier set that
contains all of the objects of that type. For example, the carrier set for the
integer type Z is the set with every integer in it: {..., —2, — 1,0, 1, 2,...}. We
usually say that the type is its carrier set . Every type is a set, but not all sets
are types.
 Natural numbers belong to the type integer, Z, because every natural
numberis also an integer.
 Types are very important in Z, even though only one type is built in: the type
integer, appropriately named Z
Declaration
 Declarations introduce variables and tell to which set each variable belongs.
Declaration cont…
 Each name introduced in a declaration names or denotes a single element in
the set that appears to the right of the colon. This element is sometimes
called the name's value. This value may be unknown or undetermined, so the
names introduced in declarations are called variables. In the preceding
declarations, i, d1,d2 and signal are variables
Constraining variables
 In Z we can constraints the value the variable it can take. These are
axiomatic definitions that include a paragraph with contraints. Predicates are
the constraints
Constant
Constant
 . A definition where the types are explicitly spelled out in this way is said to
be normalized.
 . A signature is a declaration that names the type, as we must have in a
normalized definition: e : EVEN is a declaration, but e : Z is a signature.
Defining new types
 Two methods for defining types.
 FREE TYPE definition.
 BASIC TYPE definition.
Free types
 Similar to enumerated types. When there are not enough items.
BASIC types
 When there are too many items in the type and we dobnt; wabnt to say in
advance what the elements are.
 [NAME]
Set variable
Idnetifier
Expressions and operators
 Expressions describe the values that variables might have.
 Expressions enable us to describe values in terms of names and literals we
have already defined. Expressions are formulas where names and literal
values appear together with operators. Expressions are sometimes called
terms
Arithmetic expression
 The Z mathematical tool-kit defines the usual arithmetic operators addition,
subtraction, and multiplication +, —, and *. The tool-kit doesn't provide any
way to represent fractions — it doesn't define real or rational numbers — so
ordinary division is not available. However, the tool-kit does provide integer
division div and remainder or modulus mod
 12 div 5=2
 12 mod 5=2
Set expression
Expression and type
 Every expression has a type: the type of the value it denotes.
 Some operators are generic; they can work with different types as long as
types are combined correctly
 Some operators take operands of one type and denote values of a different
type. For example, the size (or cardinality) operator # counts the elements of
a set. Its operand is a set, but its value is a number:
Erroneous expressions
 Expressions must have the correct appearance or syntax. In Z, as in
traditional mathematics, most binary operators have infix syntax: They
appear between their operands, as in 5 -f 3OTODD U EVEN.Many unary
operators in Z have pre/u syntax: They appear before their operands, as in —
x or #DICE. Using a prefix operator as if it were postfix is an example of a
syntax error.
 DICE# is an error.
Predicates , equations and law
 Three kinds of predicates: equations such as size = 2048, inequalities such as
size > 640, and membership predicates such as e € EVEN.
Equations-
 An equation is a predicate where two expressions are joined by an equal
sign: e1 = e2. means that e1 and e2 both have the same value. Equations are
perhaps the most common predicates
 Size=2048
Law
 They are also used to describe the operators themselves. Predicates used in
this way are called laws

Formal methods in software engineering lecture 4

  • 1.
    Formal methods in softwareengineering Lec 4 Elements of Z Prof Engr Faiz ul haque Zeya
  • 2.
    Topics in thislecture  Sets and types,  declarations,  variables,  expressions,  operators,  predicates,  equations and  laws.
  • 3.
    Sets Displaying sets  Theobvious way to describe a set is to list or enumerate all of its members or elements. This is called a set display. In Z we follow the ordinary mathematical convention and write sets with braces, separating elements by commas. Here is a display of the set of lamps in a traffic light:  {red, yellow, green]
  • 4.
    Naming sets  Towrite a program that simulates a dice game, we need a set that contains the numbers of spots found on the faces of dice: {1,2, 3,4, 5,6). Z does provide notation for a range of consecutive numbers; we can abbreviate this 1 .. 6 (without braces) pronounced one up to six
  • 5.
    Types  In Zwe can only form sets from objects that are similar in some way. We say that elements of the same set must have the same type; sets in Z are typed  {2,4, red, yellow, 6} [TYPE ERROR! Elements have different types.]
  • 6.
    Type and set Types and sets are very closely related. Every type has a carrier set that contains all of the objects of that type. For example, the carrier set for the integer type Z is the set with every integer in it: {..., —2, — 1,0, 1, 2,...}. We usually say that the type is its carrier set . Every type is a set, but not all sets are types.  Natural numbers belong to the type integer, Z, because every natural numberis also an integer.  Types are very important in Z, even though only one type is built in: the type integer, appropriately named Z
  • 7.
    Declaration  Declarations introducevariables and tell to which set each variable belongs.
  • 8.
    Declaration cont…  Eachname introduced in a declaration names or denotes a single element in the set that appears to the right of the colon. This element is sometimes called the name's value. This value may be unknown or undetermined, so the names introduced in declarations are called variables. In the preceding declarations, i, d1,d2 and signal are variables
  • 9.
    Constraining variables  InZ we can constraints the value the variable it can take. These are axiomatic definitions that include a paragraph with contraints. Predicates are the constraints
  • 11.
  • 12.
  • 13.
     . Adefinition where the types are explicitly spelled out in this way is said to be normalized.  . A signature is a declaration that names the type, as we must have in a normalized definition: e : EVEN is a declaration, but e : Z is a signature.
  • 14.
    Defining new types Two methods for defining types.  FREE TYPE definition.  BASIC TYPE definition.
  • 15.
    Free types  Similarto enumerated types. When there are not enough items.
  • 16.
    BASIC types  Whenthere are too many items in the type and we dobnt; wabnt to say in advance what the elements are.  [NAME]
  • 17.
  • 18.
  • 19.
    Expressions and operators Expressions describe the values that variables might have.  Expressions enable us to describe values in terms of names and literals we have already defined. Expressions are formulas where names and literal values appear together with operators. Expressions are sometimes called terms
  • 20.
    Arithmetic expression  TheZ mathematical tool-kit defines the usual arithmetic operators addition, subtraction, and multiplication +, —, and *. The tool-kit doesn't provide any way to represent fractions — it doesn't define real or rational numbers — so ordinary division is not available. However, the tool-kit does provide integer division div and remainder or modulus mod  12 div 5=2  12 mod 5=2
  • 21.
  • 22.
    Expression and type Every expression has a type: the type of the value it denotes.  Some operators are generic; they can work with different types as long as types are combined correctly  Some operators take operands of one type and denote values of a different type. For example, the size (or cardinality) operator # counts the elements of a set. Its operand is a set, but its value is a number:
  • 23.
    Erroneous expressions  Expressionsmust have the correct appearance or syntax. In Z, as in traditional mathematics, most binary operators have infix syntax: They appear between their operands, as in 5 -f 3OTODD U EVEN.Many unary operators in Z have pre/u syntax: They appear before their operands, as in — x or #DICE. Using a prefix operator as if it were postfix is an example of a syntax error.  DICE# is an error.
  • 24.
    Predicates , equationsand law  Three kinds of predicates: equations such as size = 2048, inequalities such as size > 640, and membership predicates such as e € EVEN.
  • 25.
    Equations-  An equationis a predicate where two expressions are joined by an equal sign: e1 = e2. means that e1 and e2 both have the same value. Equations are perhaps the most common predicates  Size=2048
  • 26.
    Law  They arealso used to describe the operators themselves. Predicates used in this way are called laws