Blog
Formal Methods In Software Engineering BSSE 6th Term Past Paper 2017 UOS
University of Sargodha
BS 6th Term Examination 2017
Subject: Software Engineering
Paper: Formal Methods in Software Engineering (SE-4340)
Time Allowed: 2:30 Hours
Maximum Marks: 80
Objective Part (Compulsory)
Q.1 Write short answers of the following in 2–3 lines each. (2 × 16)
i. What are formal methods?
ii. What is State Transition Diagram?
iii. What is formal specification?
iv. Define Lambda expressions.
v. What is Z?
vi. Difference between vertical and horizontal schema format.
vii. How you can translate from informal requirements to formal specifications?
viii. Explain the term Quantifiers.
ix. What is Formal Verification?
x. Benefits of Formal Models.
xi. Explain Machine-Checked proof.
xii. How can be define the Intuition?
xiii. Explain predicates.
xiv. What is Eight Queens?
xv. Difference between Conjunctions and Disjunctions.
xvi. What is Formal Reasoning?
Subjective Part (4 × 12)
Q.2 Describe how formal methods are used for safety-critical systems? How we refine from abstract to concrete requirements in safety-critical systems.
Q.3 Define and explain with examples the forward and backward chaining in Z.
Q.4 Describe how Z is useful for computer graphics and computational geometry. Give explanation with example.
Q.5 What is rule-based programming? What are the elements of rule-based programming? Explain with code, how Z can be used for formalizing rule-based programming?
Q.6 Define and explain Safety-Critical Protection System.
Q.7 Write down the Z code for text processing system.
External Reference:
For detailed concepts and examples on formal methods and Z notation, visit https://formalmethods.wikia.org
Solved answers
Z schemas are written in plain text: ΔS means the schema changes state S, ΞS means S is unchanged, x? is an input, x! is an output and x′ is the value after the operation.
Q1 – Short questions
(i) Formal methods: mathematically based techniques (set theory, logic) for specifying, developing and verifying software and hardware, so that properties can be proved rather than only tested. Examples: Z, VDM, B, CSP, model checking.
(ii) State transition diagram: a diagram of a system’s states (circles/boxes) and the transitions (arrows) between them, labelled with the events or conditions that cause them, e.g. Idle → Running on “start”.
(iii) Formal specification: a description of what a system must do, written in a language with precise mathematical syntax and semantics, so it is unambiguous and can be analysed and verified.
(iv) Lambda expressions: a notation for anonymous functions: λx : ℕ • x * x is the function that squares a natural number. In Z, (λ declaration | predicate • expression) defines a function without naming it.
(v) Z: a formal specification language based on Zermelo-Fraenkel set theory and first-order predicate logic, developed at Oxford. Specifications are built from schemas describing state and operations.
(vi) Vertical vs horizontal schema format: the vertical (boxed) format draws the schema as a box with declarations above a dividing line and predicates below it — used for larger schemas. The horizontal format writes it on one line: Name ≙ [declarations | predicates], used for short schemas.
(vii) From informal requirements to formal specification: identify the basic types and objects, define the system state and its invariants, describe the initial state, specify each operation with pre- and post-conditions, handle error cases, and keep informal English text alongside the maths for explanation; then review and check consistency.
(viii) Quantifiers: symbols that state how many elements satisfy a predicate: the universal quantifier ∀ (“for all”) and the existential quantifier ∃ (“there exists”), e.g. ∀ x : ℕ • x ≥ 0.
(ix) Formal verification: proving mathematically that a design or program satisfies its formal specification, using theorem proving or model checking.
(x) Benefits of formal models: remove ambiguity, find errors early, allow proof of safety and correctness properties, give a precise basis for testing and implementation, and improve understanding of the system.
(xi) Machine-checked proof: a proof whose every step is verified by a computer program (proof assistant or theorem prover such as Isabelle, Coq, PVS), which gives very high confidence.
(xii) Intuition: the informal, common-sense understanding of what a system should do. Formal methods turn intuition into precise statements; informal explanations in a specification help readers connect the maths with that intuition.
(xiii) Predicates: statements containing variables that become true or false when values are given, e.g. x > 5, or “n ∈ members”. In a Z schema the predicate part states constraints.
(xiv) Eight Queens: the problem of placing eight queens on a chessboard so that no two attack each other (no two on the same row, column or diagonal); it is a classic example for formal specification and search algorithms.
(xv) Conjunction vs disjunction: conjunction (p ∧ q) is true only when both p and q are true; disjunction (p ∨ q) is true when at least one of them is true.
(xvi) Formal reasoning: deriving conclusions from premises by strict logical rules (inference rules), e.g. proving that an operation preserves the state invariant.
Q2 – Formal methods for safety-critical systems; refinement from abstract to concrete
Safety-critical systems are those whose failure can cause death, injury or major damage — railway signalling, aircraft control, medical devices, nuclear plants. Formal methods are used to:
- write precise, unambiguous requirements and safety properties (e.g. “two trains are never in the same block”);
- prove that the design always maintains these safety invariants;
- model-check all possible states to find hazards that testing could miss;
- satisfy standards such as IEC 61508, DO-178C and EN 50128, which recommend formal methods at the highest safety levels.
Refinement (abstract → concrete):
- Write an abstract specification describing what the system must do using sets and functions, with safety invariants.
- Data refinement: replace abstract data (sets) by concrete data structures (arrays, sequences) and define a retrieve relation linking them.
- Operation refinement: weaken pre-conditions or strengthen post-conditions while keeping the behaviour correct.
- Prove refinement obligations: the concrete initial state corresponds to an abstract one; each concrete operation is applicable whenever the abstract one is; and its results are allowed by the abstract specification.
- Repeat in steps until the design is close to code, then implement and verify.
Q3 – Forward and backward chaining in Z
Chaining is reasoning with rules of the form “if conditions then conclusion”.
Forward chaining (data-driven): start from known facts, apply every rule whose conditions are satisfied to add new facts, and repeat until the goal appears or nothing new can be added.
Backward chaining (goal-driven): start from the goal, find rules whose conclusion is the goal, and try to prove their conditions as sub-goals, until known facts are reached.
[FACT]
Rule == FACT x P FACT -- (conclusion, set of conditions)
KnowledgeBase
facts : P FACT
rules : P Rule
ForwardStep
Δ KnowledgeBase
---------------------------------------------
rules' = rules
facts' = facts ∪ { c : FACT | ∃ conds : P FACT •
(c, conds) ∈ rules ∧ conds ⊆ facts }
Proves : KnowledgeBase x FACT -- backward chaining
Proves(kb, g) ⇔ g ∈ kb.facts ∨
(∃ conds : P FACT • (g, conds) ∈ kb.rules ∧
(∀ c : conds • Proves(kb, c)))Example: facts = {has_feathers, lays_eggs}; rules: bird ⇐ {has_feathers, lays_eggs}; can_fly ⇐ {bird}. Forward chaining adds bird, then can_fly. Backward chaining for the goal can_fly needs bird, which needs has_feathers and lays_eggs — both facts, so the goal is proved.
Q4 – Z for computer graphics and computational geometry
Graphics and geometry deal with precise objects — points, lines, polygons, transformations — which map naturally onto Z’s sets, tuples and functions. Z lets us state exact properties (e.g. a polygon is closed, a transformation preserves distance) and prove algorithms correct, avoiding special-case bugs.
POINT == R x R -- a point (x, y)
Rectangle
bottomLeft, topRight : POINT
---------------------------------------
first bottomLeft < first topRight
second bottomLeft < second topRight
Inside : POINT <-> Rectangle
∀ p : POINT; r : Rectangle •
p Inside r ⇔ first r.bottomLeft ≤ first p ≤ first r.topRight ∧
second r.bottomLeft ≤ second p ≤ second r.topRight
Translate
Δ Rectangle
dx?, dy? : R
---------------------------------------
bottomLeft' = (first bottomLeft + dx?, second bottomLeft + dy?)
topRight' = (first topRight + dx?, second topRight + dy?)The Rectangle invariant guarantees a valid rectangle; Inside formally defines hit-testing (used in clipping and windowing); Translate is a transformation operation whose result can be proved to remain a valid rectangle.
Q5 – Rule-based programming and its formalisation in Z
Rule-based programming is a style in which program behaviour is described by a set of condition–action rules (“IF condition THEN action”) applied to a working memory of facts by an inference engine, as in expert systems (CLIPS, Prolog-like systems).
Elements: (1) working memory — current facts; (2) rule base — the IF–THEN rules; (3) inference engine — matches rules against facts; (4) conflict set and conflict resolution — chooses which matching rule fires (priority, recency, specificity); (5) action/execution — the fired rule adds or removes facts; the match–resolve–act cycle repeats.
[FACT]
Rule
condition : P FACT
addList, deleteList : P FACT
RuleSystem
memory : P FACT
rules : P Rule
Applicable : RuleSystem -> P Rule
∀ s : RuleSystem • Applicable(s) = { r : s.rules | r.condition ⊆ s.memory }
Fire
Δ RuleSystem
r? : Rule
---------------------------------------
r? ∈ Applicable(θRuleSystem) -- precondition: rule matches
memory' = (memory \ r?.deleteList) ∪ r?.addList
rules' = rulesThe Fire operation describes one cycle: a rule may fire only if its condition holds in memory; firing removes the delete-list facts and adds the add-list facts.
Q6 – Safety-critical protection system
A safety-critical protection system is an independent system that monitors a dangerous process and automatically moves it to a safe state when hazardous conditions occur, preventing accidents. Examples: a reactor protection (shutdown) system that drops control rods when temperature or pressure exceeds limits; train automatic protection that applies brakes when a red signal is passed; medical radiation machines’ interlocks.
Key features: sensors monitor plant variables; logic compares values with trip set-points; actuators perform the safety action (trip); redundancy (e.g. 2-out-of-3 voting) and fail-safe design ensure the system still works or fails safely when a component fails; it is separate from the normal control system. Because failures are catastrophic, its requirements are often formally specified and verified. A simple Z invariant: temperature ≥ tripLimit ⇒ state = shutdown.
Q7 – Z specification of a text processing system
[CHAR]
TEXT == seq CHAR
Editor
left, right : TEXT -- text before and after the cursor
---------------------------------------
# (left ⁀ right) ≤ maxSize
InitEditor
Editor'
---------------------------------------
left' = ⟨⟩ ∧ right' = ⟨⟩
Insert
Δ Editor
ch? : CHAR
---------------------------------------
# (left ⁀ right) < maxSize
left' = left ⁀ ⟨ch?⟩
right' = right
Delete -- delete character before cursor
Δ Editor
---------------------------------------
left ≠ ⟨⟩
left' = front left
right' = right
MoveLeft
Δ Editor
---------------------------------------
left ≠ ⟨⟩
left' = front left
right' = ⟨last left⟩ ⁀ right
MoveRight
Δ Editor
---------------------------------------
right ≠ ⟨⟩
left' = left ⁀ ⟨head right⟩
right' = tail right
WordCount
Ξ Editor
count! : N
---------------------------------------
count! = number of maximal non-space blocks in (left ⁀ right)The state stores the text as two sequences split at the cursor; the invariant limits the size; each operation states its pre-condition (e.g. you cannot delete at the start) and exactly how the text changes. Error schemas (e.g. EditorFull) can be added and combined with ∨ to make total operations.
Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
