Formal Methods
Formal Methods
L5
1
2
Testing: Static vs Dynamic
Analysis
■ Static analysis of code^Does not require
execution of code
» Lexical analysis of the program syntax and
investigates and checks the structure and usage
of individual statements; often automated
■ Dynamic Analysis of code^ Involves
running the system (testing)
» Program run formally under controlled
conditions with specific results expected
» Path and Branch Testing
3
L5
4
Why Consider Formal Methods?
■ Complexity of systems
with embedded software
has increased rapidly
■ Maintaining reliability in
software-intensive
L5
systems is very difficult 5
L5
6
Formal Specifications
■ Translation of a non-mathematical
description (diagrams, tables, English text)
into a formal specification language
■ Concise description of high-level behavior
and properties of a system
■ Well-defined language semantics support
formal deduction about specification
L4
7
Types of Specifications I
■ Informal
» Free form, natural language
» Ambiguity and lack of organization can lead to
incompleteness, inconsistency, and misunderstandings
■ Formatted
» Standardized Syntax
» Basic consistency and completeness checks
» Imprecise semantics implies other sources of error may
still be present
8
Types of Specifications II
■ Formal
» Syntax and semantics rigorously defined
» Precise form, perhaps mathematical
» Eliminate imprecision and ambiguity
» Provide basis for mathematically verifying
equivalence between specification and
implementation
» May be hard to read without training
» Semantic distance?
9
Formal Specifications
■ Goal: Describe external behaviour without
describing or constraining implementation
■ Formal Method has 2 parts:
» Logical Theory: Means by which one reasons about
specifications, properties and programs
- First order predicate calculus (quantification over variables)
- Second order predicate calculus (quantification over relations)
- Temporal logic
» Structuring Theory: Defines elements being reasoned
about
10
Types of Formal Specifications
■ Property Oriented: State desired properties in a
purely declarative way
» Algebraic: Data type viewed as an algebra, axioms
state properties of data type’s operations
» Axiomatic: Uses first order predicate logic, pre and
post conditions Operational Specification: Describe
desired behaviour by providing model of system
■ Model Oriented: Provide direct way of describing
system behaviour (sets, sequences, tuples, maps) :
» Abstract Model (in terms previously defined
mathematical objects eg. sets, sequences, functions,
mappings)
» State machines
■ Uses
» Input-Output Assertions
» Sets of operations
» Axioms specifying behaviour of operations
■ Two parts to a specification
» syntax
» axioms
12
Model Oriented: Abstract Model
Specifications
■ Build an abstract model of required software
behaviour using mathematically defined types
(sets, relations)
■ Define operations by showing effects of that
operation on the model
■ Specification includes:
» Model Type
» Invariant properties of model
» For each operation
- Name, parameters, return values
» Pre- and Post- conditions
13
Example Problem:
The English Specification
C1
C2
C3
I/O Channels
Instruments
L5
15
Z Specification:
Basic Variables and Invariants
IO_Channel_Assignments
Basic_Types
active_instruments : P Platform_Instruments
assigned_to :
Communications_Channels « Platform_Instruments
available, busy: P Communications_Channels
Z Specification:
Schema for Error Condition
Instrument_Not_Active
X IO_Channel_Assignments
instrument? : Platform_Instruments
message! : Possible_Message
instrument? g active_instruments
message! = instrument_not_active
L5
18
Interesting Thought Problem
■ Alice and Bill throw a party, and invite 4
other couples. As each couple arrived, there
are greetings and handshakes. At the end of
the party, Bill asked everyone, including
Alice, how many people they shook hands
with: Every answer was different!
■ How many hands did Alice shake?
»N.B: No one shakes hands with their own
partner or themselves. You greet a person only
once. 19
L5
20
Formal Proofs
■ Complete and convincing argument for validity
of some property of the system description
■ Constructed as a series of steps, each of which
is justified from a small set of rules
■ Eliminates ambiguity and subjectivity inherent
when drawing informal conclusions
■ May be manual but usually constructed with
automated assistance
L4
21
Model Checking
■ Operational rather than analytic
■ State machine model of a system is expressed in a
suitable language
■ Model checker determines if the given finite state
machine model satisfies requirements expressed as
formulas in a given logic
■ Basic method is to explore all reachable paths in a
computational tree derived from the state machine
model
L4
22
Abstraction
■ Simplify and ignore irrelevant details
■ Focus on and generalize important central
properties and characteristics
■ Avoid premature commitment to design and
implementation choices
L4
23
F(I)^O
24
Formal Specification Languages
■■■■■■■■ IID
■ Based on formal mathematical logic, with
some programming language enhancements
(such as type systems and parameterization)
■ Generally non-executable -- designed to
specify what is to be computed, not how the
computation is to accomplished
■ Most are based on axiomatic set theory or
higher-order logic
L5
25
■ Modularization
» breaking a specification into modules is an
important organizational feature
» parameterized modules allow reusability
L5
28
Features of Specification Languages (cont’d)
Categories of
Logical Anomalies
Likely ( _ Accuracy -- does the specification
► mean what is intended? System
invariants can help in detection.
L5
31
Formal Specifications as
a System Description
L5
32
Benefits of Formal Specifications
L4
33
L4
34
Benefits of Formal Specifications (cont’d)
■ Encourages an abstract view of system -¬
focusing on what a proposed system
should accomplish as opposed to how to
accomplish it
■ Abstract formal view helps separate
specification from design
■ Enhances existing review processes by
adding a degree of rigor
L4
35
L5
36
Cautions in the Use of Formal Methods
■ Judicious application to suitable project
environments is critical if benefits are to
exceed costs
■ FM and problem domain expertise must be
fully integrated to achieve positive results
L5 37
Conclusion
■ FM are no panacea
■ FM can detect defects earlier in life cycle
■ FM can be applied at various levels of
resource investment
■ FM can be integrated within existing project
process models
■ FM can improve quality assurance when
applied judiciously to appropriate projects
L5 38