Skip to content
The Lumenary

Each worksheet prints cleanly on A4 or Letter. Answer keys are on a separate page.

The Lumenary · First-year graduate

Formal methods

Name: ______________________

Date: ______________________

You will learn to

  • Give a big-step operational semantics for a small imperative language and prove properties such as determinism by induction on derivations
  • State the rules of Hoare logic, prove their soundness, and verify a loop program with an invariant and a variant
  • Model-check finite transition systems against CTL formulas by fixed-point iteration, and explain the cost of the labelling algorithm
  • Prove type soundness by progress and preservation, and explain what it does and does not guarantee
  • Explain what a verification result is relative to — its specification, semantics and trusted base — and why Rice's theorem rules out full automation
  1. 1.

    Which of these are valid partial-correctness triples over the integers? (Choose all that apply.)

    • A.{x=1} while x>0 do x:=x+1 {x=0}\{x = 1\}\ \mathtt{while}\ x \gt 0\ \mathtt{do}\ x := x + 1\ \{x = 0\}
    • B.{⊤} x:=5 {x=5}\{\top\}\ x := 5\ \{x = 5\}
    • C.{x=3} x:=x+1 {x=3}\{x = 3\}\ x := x + 1\ \{x = 3\}
    • D.{x≥0} while x>0 do x:=x−1 {x=0}\{x \ge 0\}\ \mathtt{while}\ x \gt 0\ \mathtt{do}\ x := x - 1\ \{x = 0\}
  2. 2.

    Compute wp(y:=x+3; z:=2y,   z=20)\mathrm{wp}(y := x + 3;\ z := 2y,\ \; z = 20). It has the form x=nx = n. What is nn?

  3. 3.

    The program s:=0; i:=0; while i<n do (i:=i+1; s:=s+i)s := 0;\ i := 0;\ \mathtt{while}\ i \lt n\ \mathtt{do}\ (i := i + 1;\ s := s + i) should satisfy {n≥0} ⋅ {s=n(n+1)/2}\{n \ge 0\}\ \cdot\ \{s = n(n+1)/2\}. Which loop invariant lets the loop rule and the rule of consequence prove this?

    • A.s=i(i+1)/2∧i≤ns = i(i+1)/2 \wedge i \le n
    • B.s=i(i+1)/2s = i(i+1)/2
    • C.s=n(n+1)/2s = n(n+1)/2
    • D.i≤ni \le n
  4. 4.

    In the soundness proof of Hoare logic, the loop case is proved by induction on the derivation of ⟨while b do c,s⟩⇓t\langle \mathtt{while}\ b\ \mathtt{do}\ c, s\rangle \Downarrow t. Why would structural induction on the command not work?

    • A.The premise of the rule for b(s)=ttb(s) = \mathrm{tt} contains the same loop while b do c\mathtt{while}\ b\ \mathtt{do}\ c, which is not a smaller command.
    • B.Because loops might not terminate.
    • C.Because the invariant may mention variables not assigned in the loop.
    • D.Because the rule of consequence is not syntax-directed.
  5. 5.

    Which CTL formula says: from every reachable state it is possible to get back to an initial state (a "reset" property)?

    • A.AG EF init\mathsf{AG}\,\mathsf{EF}\,\mathit{init}
    • B.EF AG init\mathsf{EF}\,\mathsf{AG}\,\mathit{init}
    • C.AF init\mathsf{AF}\,\mathit{init}
    • D.EG EF init\mathsf{EG}\,\mathsf{EF}\,\mathit{init}
  6. 6.

    A Kripke structure has states 1,…,61, \dots, 6 and transitions 1→21 \to 2, 2→32 \to 3, 3→33 \to 3, 4→54 \to 5, 5→45 \to 4, 6→16 \to 1. The proposition pp holds only in state 33. Compute EF p\mathsf{EF}\,p by the iteration Z0=∅Z_0 = \emptyset, Zk+1={3}∪pre(Zk)Z_{k+1} = \{3\} \cup \mathrm{pre}(Z_k). How many states satisfy EF p\mathsf{EF}\,p?

  7. 7.

    Programs are written in a Turing-complete language. Which of these problems are undecidable? (Choose all that apply.)

    • A.Given a program P, does P halt on input 0?
    • B.Given a program P, does P compute a total function?
    • C.Given a program P, does its source text contain a while loop?
    • D.Given a finite Kripke structure and a state, does AG ¬bad\mathsf{AG}\,\neg \mathit{bad} hold?
    • E.Given P, does P output 1 on every input 0, …, 10 within a million steps?
  8. 8.

    Which Lean definition correctly formalises the partial-correctness triple {P} c {Q}\{P\}\ c\ \{Q\}, where BigStep c s t means that c run from s terminates in t?

    • A.∀ s t, P s → BigStep c s t → Q t
    • B.∀ s, P s → ∃ t, BigStep c s t ∧ Q t
    • C.∃ s t, P s ∧ BigStep c s t ∧ Q t
    • D.∀ s t, Q t → BigStep c s t → P s
  9. 9.

    A compiler comes with a machine-checked proof that, whenever it succeeds, the generated assembly has only behaviours allowed by the source program, according to formal semantics of the source and assembly languages. Which of the following could still lead to a wrongly behaving executable? (Choose all that apply.)

    • A.The formal semantics of the source language differs from what programmers (or the standard) expect.
    • B.An unverified parser or assembler used around the verified core has a bug.
    • C.The processor does not implement its instruction set as the assembly semantics says.
    • D.One of the verified optimisation passes contains an error in its proof.
  10. 10.

    Write find_violation(initial, successors, is_bad) for explicit-state model checking of a safety property. successors(s) returns a list of states and is_bad(s) a boolean. Return a shortest path [initial, ..., bad_state] to a reachable bad state, or None if no reachable state is bad. Your search must terminate on finite systems with cycles.

    Computational — Practice

Code: Apache-2.0 · Content: CC BY-SA 4.0