<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>A Termination Checker for Isabelle Hoare Logic</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jia Meng</string-name>
          <email>jia.meng@nicta.com.au</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Lawrence C. Paulson</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Gerwin Klein</string-name>
          <email>gerwin.klein@nicta.com</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Computer Laboratory, University of Cambridge</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>National ICT</institution>
          <country country="AU">Australia</country>
        </aff>
      </contrib-group>
      <fpage>104</fpage>
      <lpage>118</lpage>
      <abstract>
        <p>Hoare logic is widely used for software specification and verification. Frequently we need to prove the total correctness of a program: to prove that the program not only satisfies its pre- and post-conditions but also terminates. We have implemented a termination checker for Isabelle's Hoare logic. The tool can be used as an oracle, where Isabelle accepts its claim of termination. The tool can also be used as an Isabelle method for proving the entire total correctness specification. For many loop structures, verifying the tool's termination claim within Isabelle is essentially automatic.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>For many critical systems, such as operating systems kernels, testing is not
adequate to ensure correctness. Formal methods have become popular in research
as well as industry. There are several approaches to verifying program
correctness. For example, Hoare logic has been used to specify a program’s pre- and
post-conditions; using logical deduction, one can prove the program meets its
specification. A program that fails to terminate satisfies its post-condition by
default, so total correctness requires a proof that the program terminates.</p>
      <p>
        Total correctness proofs are complicated and require a lot of human effort.
A different approach is to focus on the termination property of a program,
which can usually be automated with some human assistance. An example is
the Terminator program developed at Microsoft Research [
        <xref ref-type="bibr" rid="ref12 ref3 ref8">3, 8</xref>
        ], which checks
whether a C program terminates.
      </p>
      <p>
        A proof of termination is useful, but it does not guarantee that a program
does what it is supposed to do. Full specification and verification is still what we
are aiming at, and the most important formalism for that purpose is Hoare logic.
Our current work is part of a larger project to verify the functional correctness
of the L4 operating system micro kernel. An automatic termination checker can
reduce manual work. Since Terminator is not publicly available, we have
implemented a termination checker in the spirit of Terminator and have integrated
this tool into Isabelle [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. The tool can be used by Isabelle’s Hoare logic to prove
total correctness specifications. In this paper, we concentrate on termination of
WHILE constructs.
      </p>
      <p>Although our termination checker and Terminator are based on the same
technology, we have a different emphasis. In addition to implementing the
termination tool, we also investigate how we can have the tool’s results used by
Isabelle’s Hoare logic. The termination tool is based on model checking and it
returns a set of well-founded (WF) relations for each cyclic path. However, a
total correctness specification in Hoare logic requires one single WF relation
(the variant) for each looping construct. An explicit variant represents evidence
of termination, but this variant is not given as a result by either Terminator
or our tool, which are both based on the model checking technology. Much of
our investigations concern how to make the results from the termination tool
usable to Isabelle. As far as we know, our tool is the first integration of a
modelchecking-based termination tool with an interactive prover, and our work is the
first integration of the terminator technology to Hoare logic.</p>
      <p>
        In addition, we invented an optimization for our termination checker (§4.3)
to meet our particular requirements on the generated WF relations. Finally,
Podelski and Rybalchenko [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] have proved the mathematical theory behind
Terminator. In order that we can use the tool in Isabelle, we have had to
formalize their proofs in Isabelle, and this is the first formalization of the proofs
in any interactive prover.
      </p>
      <p>
        Another method for termination analysis is to translate imperative programs
into functional programs so that one can prove the termination property of
an imperative program by proving its functional counterpart terminates [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].
However, we decided to adopt another approach, which is based on disjunctively
well-founded relations. This approach is comparatively novel and is a promising
method that has been applied to full C programs.
      </p>
      <p>Paper outline. We first present background information on Isabelle’s Hoare
logic and termination requirements of programs (§2). We then describe how we
have implemented our termination checker (§3). Subsequently, we illustrate the
two approaches we have used to integrate the termination checker into Isabelle
(§4 and §5). In order to show how we can use our termination checker with
Isabelle, we give some examples (§6). Finally, we conclude the paper (§7).
2
2.1</p>
    </sec>
    <sec id="sec-2">
      <title>Isabelle and Termination Properties</title>
      <sec id="sec-2-1">
        <title>Hoare logic in Isabelle</title>
        <p>
          We base our work on Norbert Schirmer’s implementation of Hoare logic in
Isabelle/HOL [
          <xref ref-type="bibr" rid="ref9">9</xref>
          ]. Schirmer has designed a small but expressive imperative
language, called SIMPL, with recursive procedures. He defines an operational
semantics for SIMPL and derives a sound and complete Hoare logic. The Hoare
logic implementation includes an automated verification condition generator
(vcg). In other work [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ], we have provided an Isabelle front-end for mapping C
into SIMPL.
        </p>
        <p>Since SIMPL treats procedures, the Hoare triple format that we show in
examples later also mentions procedure environments, Γ . Partial correctness
is written Γ ⊢ {|P |}C{|Q|}; total correctness is written with subscript t, as
Γ ⊢t {|P |}C{|Q|}. Partial correctness means if C is executed in a state where
P is true, and if C terminates, then it will end in a state where Q is true.
Total correctness includes a termination requirement: if C is executed in a state
satisfying P , then it will terminate in a state satisfying Q.</p>
        <p>For proving total correctness, there are two approaches. One approach is to
separate partial correctness from termination, as with the following rule
Γ ⊢ {|P |} C {|Q|} Γ ⊢t {|P |} C {|⊤|}</p>
        <p>Γ ⊢t {|P |} C {|Q|}
where ⊤ is logical truth. The second premise says that the program C started in
a state satisfying P will terminate. If we assert it without giving any evidence,
we have implemented an oracle that accepts an external claim of termination.
Here we follow the convention that unmentioned variables do not change their
values.</p>
        <p>The second approach is to use Hoare logic rules for total correctness. These
rely on the concept of a well-founded (WF) relation: one that has no infinite
descending chains. In this paper we do not consider the recursive procedure
call of SIMPL, which makes WHILE the only looping construct. Schirmer uses
sets of states to formalize all predicates, such as pre- and post-conditions and
the loop’s boolean expression. For the verification condition generator, a typical
WHILE statement is annotated with an invariant, which is again a set of states,
and a variant, which is some WF relation. This means the WHILE-rule for total
correctness in Schirmer’s Hoare logic is the following:
∀σ. Γ ⊢t {| {σ} ∩ I ∩ b |} C {| {t | (t, σ) ∈ V } ∩ I |}</p>
        <p>P ⊆ I I ∩ −b ⊆ Q wf V
Γ ⊢t {|P |} WHILE b INV I VAR V DO C {|Q|}
The first premise fixes the pre-state σ and requires that the loop body C
decreases the variant while maintaining the invariant I. The set {t | (t, σ) ∈ V }
consists of all post-states t such that (t, σ) are in the variant V . If V is a WF
relation, the loop must terminate. This approach is good for generating full
termination proofs, because the variant gives explicit evidence for termination.</p>
        <p>The rest of this paper shows how we can integrate externals tools into
Isabelle/HOL to produce the variants mentioned above and how to prove them
well-founded. This reduces the manual proof effort for total correctness goals,
either by oracle or by proof fully verified in Isabelle/HOL.
2.2</p>
      </sec>
      <sec id="sec-2-2">
        <title>Termination Properties</title>
        <p>
          Before we explain the termination properties, it is helpful to have a brief review
of the transition system that is usually used as an abstraction of programming
languages. For more information, we refer readers to the book by Manna [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ].
        </p>
        <p>Each program has an associated transition system. A transition system
consists of four parts: Π, Σ, R and Θ.
– Π is a finite set of state variables, which consists of program variables that
appear in the program statements and also control variables, such as the
program counter (PC).
– Σ is a set of states. Each state s is an interpretation of Π, which assigns
values to each variable in Π. For example, x s is the value of variable x in
the state s.
– R is a finite set of transitions. For a deterministic program, a transition τ is a
fausnsct−→iτonsΣ′),→theΣn. sIf′ aistrreaancshitaibonle τfrloemadss.a Wsteatseays ttohaatnosthaenrdstsa′taerse′ p(rwer-itatnedn
post-states of τ .
– Finally Θ is the initial condition, which is a set of initial states</p>
        <p>Each transition τ is characterized by its transition relation ρτ , where (s′, s) ∈
ρτ if and only if s′ is reachable from s by τ . In addition, we can extract one or
more transition relations from each program statement. For example, if we have a
WHILE statement at program location L as WHILE(x &gt; 0){x = x -1;}, then its
transition relations are {(s′, s) | P C s = L∧x s &gt; 0∧x s′ = x s−1∧P C s′ = L}
and {(s′, s) | P C s = L ∧ x s ≤ 0 ∧ x s′ = x s ∧ P C s′ = M }, where M is the
exit location of the WHILE construct.</p>
        <p>This set notation for transition relations is sometimes abbreviated by a logical
formula: the value of a variable in the pre-state is represented directly by the
variable name and the value in the post-state is represented by the primed
version of the variable name. When the PC value is understood from the context,
its pre- and post-values are also ignored. For example, the first transition relation
above can be abbreviated to x &gt; 0 ∧ x′ = x − 1.</p>
        <p>A computation is a possibly infinite sequence of states s0, s1 . . ., such that s0
is in an initial state and each si+1 is reachable from si via some transition.</p>
        <p>A path in a transition system is a sequence of transitions π = τ1 . . . τn, such
that the PC’s value in the post-state of τi is the same as the value in the
prestate of τi+1. There is a path transition relation for each path, which is just the
relational composition of each consecutive transition relation involved in this
path. The path above is cyclic if the PC value in the pre-state of τ1 is the same
as the value in the post-state of τn.</p>
        <p>
          A program is terminating if there is no infinite computation. Theoretically,
this can be proved by showing that there is a WF relation T , such that each
consecutive pair of states si and si+1 has (si+1, si) ∈ T . However, it is often
too difficult to find one single WF relation T for this purpose. Podelski and
Rybalchenko [
          <xref ref-type="bibr" rid="ref8">8</xref>
          ] have proved that it is sufficient to find a finite set of WF relations
T = {T1, . . . , Tn} to show the program is terminating, provided we can prove
RI+ ⊆ T1 ∪ . . . ∪ Tn, where R is the program’s transition relation, R+ is the
transitive closure of R and RI+ is a reachable subset of R+. This means for each
reachable path, its path transition relation must be a member of some Ti of T .
        </p>
        <p>
          Since each non-cyclic path induces a WF relation, to show the program is
terminating, we only need to show there is a WF relation Ti for each reachable
cyclic-path. Cook et al. have shown [
          <xref ref-type="bibr" rid="ref12 ref3">3</xref>
          ] that program termination checking can
be translated into program reachability analysis. For this to work, auxiliary
program variables are introduced and the relations between them and the original
variables are established to mimic the transition relations of the program. A
designated program location ERROR is used: it is only reachable if there is no
WF relation Tk such that the post-state s′ and pre-state s of a cyclic path’s
transition relation has (s′, s) ∈ Tk. If this happens, one can try to find a WF
relation for that cyclic path. If no WF relation can be found, then the program
is reported as possibily non-terminating. If a WF relation can be generated for
the path, then the process continues with other cyclic paths, until ERROR is really
not reachable, which means all cyclic paths are covered by some WF relations.
2.3
        </p>
      </sec>
      <sec id="sec-2-3">
        <title>Using a Termination Tool for Generating Variants</title>
        <p>Although we can use the termination tool to generate WF relations for all cyclic
paths, these relations cannot be used as variants for Hoare logic.</p>
        <p>First, there may be nested WHILE constructs. The variant V for the outer
WHILE has to be one single WF relation so that the pre-state s and the
poststate s′ of the body transition relation satisfy (s′, s) ∈ V , regardless how many
times the inner WHILE are entered and whether they are entered at all. This
means V has to satisfy multiple cyclic paths. We can use the termination tool
to generate WF relations to cover all these cyclic paths, but the variant must be
one single WF relation, and we believe that automatically combining multiple
WF relations into one is impossible in general.</p>
        <p>Second, even if a program has no nested WHILEs, there may be more than one
(cyclic) path and hence more than one path transition relation between the start
and the end of the WHILE loop. Each transition relation has to be a conjunction
of positive atomic formulae, and each atomic formula is a mathematical
assertion over state variables. This is because we use an external ranking function
generator to synthesis WF relations and the tool only accepts a transition
relation expressed as a conjunctive formula without negations. Consequently, if the
guard of the WHILE has disjunctions or negations or the body has IF constructs,
then we will have multiple path transition relations. We again need to combine
multiple WF relations into one.</p>
        <p>Instead of generating a single WF relation, we could investigate another
approach that tries to construct a termination argument from these WF relations.
However, our work aims to use the termination checker to support proofs
performed in Isabelle Hoare logic, and the total correctness rule requires the variant
as the termination argument.
3</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Implementing a Termination Checker</title>
      <p>
        We have implemented a termination checker that checks C programs involving
integer variables, in the spirit of Terminator [
        <xref ref-type="bibr" rid="ref12 ref3">3</xref>
        ], in the sense that we check the
termination of the entire program by generating one or more WF relations for
its cyclic paths. Currently, we only use the tool to check cyclic paths produced
by WHILE constructs and the tool does not handle pointers. Moreover, we use
two external tools: Blast and Rankfinder.
3.1
      </p>
      <p>
        Using Blast and Rankfinder
Blast [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] is a model checker for C programs. It checks that software behavior
meets its specification using lazy abstraction. For our purpose, we use Blast to
check if a designated location called ERROR is reachable. If ERROR is not reachable,
then Blast reports the program is safe. If ERROR is reachable, then Blast returns
a trace: a sequence of locations from the start of the program to ERROR.
      </p>
      <p>Here, we are using the location ERROR to signal a possibly non-terminating
cyclic path. If ERROR is not reachable, then there is no non-terminating cyclic
path. However, if Blast reports a trace that leads to ERROR, then we can extract
a cyclic path from it, and then we can examine if we can generate a WF relation
to show the path is terminating, using Rankfinder.</p>
      <p>
        Rankfinder [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] is a ranking function (a.k.a. measure function) generator. A
ranking function is a decreasing function, with a lower bound. Given a transition
relation τ , Rankfinder tries to synthesize a decreasing ranking function, with
two parameters: an integer bound b and a positive integer d that is the minimum
decrease of the ranking function during the transition relation. For example, if
the transition relation is x ≥ 0 ∧ x′ = x − 1, then the ranking function from
Rankfinder is x, the bound is 0 and minimum decrease is 1. Each ranking
function F induces the well-founded relation
      </p>
      <p>{(s′, s) | b s ≤ F s ∧ F s′ ≤ F s − d}
where s′ and s are the post- and pre-states of the transition.
3.2</p>
      <sec id="sec-3-1">
        <title>The Termination Checker</title>
        <p>Our tool is closely integrated with Isabelle and it is called via an Isabelle
invocation. It works as follows.
1. The C program embedded in SIMPL is extracted to generate a control flow
graph. For better performance, we “compact” the flow graph so that only
WHILE locations and the program entrance point are kept in the graph. All
the remaining program locations are removed from the graph by joining the
path transition relations. If the pre-condition P of the Hoare specification
is non-empty (i.e. there are initial conditions on program variables), then
we modify the flow graph to include the initial conditions. Subsequently, we
examine each WHILE construct in turn, and the order in which we examine
the WHILE constructs does not matter.
2. For each WHILE construct, writing its program location as L, check the
termination of all cyclic paths that start from and finish at L:
(a) Insert the already-generated WF relations for L into the C program and
generate a text file, then call Blast. Initially no WF relation is generated,
so nothing is inserted.
(b) If Blast reports the program is safe, i.e. ERROR is not reachable, then move
to the next WHILE construct. If Blast reports an error trace, then we
extract the cyclic paths from the trace and calculate the reachable transition
relations. We then call Rankfinder to generate a ranking function for each
transition relation. If Rankfinder succeeds, then use the newly generated
WF relations to modify the C program and re-run Blast. If Rankfinder
cannot generate a well-founded relation for a transition relation, then the
program is reported as possibily non-terminating.
3. If ERROR is no longer reachable, Blast will report the program is safe. We
can then move on to the next WHILE construct if available.
4. If for each WHILE construct, its cyclic paths are reported to be terminating,
then the entire program is terminating; the generated WF relations are also
reported. Otherwise, the program is reported as possibly non-terminating.
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Integrating the Termination Checker into Isabelle</title>
      <p>Our termination checker has been used as a tool for Isabelle, both as an oracle
and as a proof method. Isabelle’s oracle mechanism accepts an external tool’s
result without verifying it. When used as a proof method, the result is used to
create an Isabelle proof that is verified through Isabelle’s kernel.
4.1</p>
      <sec id="sec-4-1">
        <title>Integration as an Oracle</title>
        <p>Recall that a total correctness goal can be proved separately as a partial
correctness goal and a termination goal (§2.1). When used as an oracle, we only
use the tool to prove the second subgoal, namely the program is terminating,
if started from a state satisfying P . We do not need to generate variants in
this case. Therefore, if the tool reports the program is terminating, the second
subgoal is removed from the proof state.
4.2</p>
      </sec>
      <sec id="sec-4-2">
        <title>Integration as an Isabelle Proof Method</title>
        <p>Using the termination checker as an Isabelle oracle gives us a quick answer to
whether the program is terminating. Of course, using the tool as an Isabelle
proof method would yield greater confidence. This requires us to use the tool to
generate a variant for each WHILE construct. Moreover, we would like the variant
to be as simple as possible. The form of the variant generated depends on the
complexity of the program, as well as the WF relations generated for WHILE
constructs.</p>
        <p>For this purpose, we divide the WF relations for each WHILE construct (W )
into two sets: Tin is generated for cyclic paths that do not leave W and Tout is
generated for cyclic paths that leave W and re-enter. This is because a variant
for W is essentially a set of transition relations of paths that do not leave W .
Therefore, if we define a variant using a relation that does not include any path
that leaves W , we only need Tin for WF proofs. Tout is needed when we try to
prove the entire program R is WF, since we need to prove R+ is WF and R+
contains paths that leave W .</p>
        <p>In this section, we describe the form of the variants generated and will
informally explain why they are variants and why they are WF. We will show some
formal proofs in the next section.</p>
        <p>Programs with No Nested Loops If a program has a single loop, then the
variant generated only depends on the number of WF relations in Tin because
we are not concerned about the paths leaving and re-entering the WHILE (W ).
There are two cases to consider.</p>
        <p>First, if there is only one ranking function F generated, then we generate an
Isabelle measure function M from it. The difference between F and M is that F
involves integers whereas M uses natural numbers only, but this is easily dealt
with.</p>
        <p>Hoare logic requires us to prove that the loop body decreases the variant.
More formally, V must be a set containing all the post- and pre-states pairs of
the loop. We can indeed prove that the transition relations of each cyclic path
starting and finishing at location W form a subset of V .</p>
        <p>Second, if there is more than one ranking function in Tin, then we define
the variant to be the intersection RL ∩ I of the transition relation of the loop
and the invariant of the loop. Frequently we can use RL as the variant, which
is weaker. The use of the invariant1 is important when reachability becomes
a concern (§4.4). As there is only one WHILE construct, RL does not need to
mention its PC value. As an example, consider the following C program.
WHILE (x &gt; 0 || p &gt; 2){x = x - 1; p = p - 2;}
1 Currently, invariants are generated manually, but we plan to incorporate automatic invariant
generation in the future.
Its RL is
{(s′, s) | x s &gt; 0 ∧ x s′ = x s − 1 ∧ p s′ = p s − 2 ∨</p>
        <p>p s &gt; 2 ∧ x s′ = x s − 1 ∧ p s′ = p s − 2}
Obviously, the transition relation is a variant, and we can prove (see §5) that
RL is well-founded.</p>
        <p>Programs with Nested Loops This is the complicated case. We generate the
variant for each WHILE in turn. The form of the generated variant also depends
on the complexity of the WHILE construct.</p>
        <p>If there is only one ranking function F in Tin, then we generate its
corresponding Isabelle measure function M as above.</p>
        <p>If there are multiple ranking functions, then the variants are defined in terms
of transition relations. Since there are multiple WHILE loops, the transition
relation must mention PC values. There are two cases to consider.</p>
        <p>First, if the WHILE construct with location L has no nested inner loop, then
we define the variant to be</p>
        <p>V = fix pc L (RL ∩ I)
where RL, I are transition relation and invariant of the WHILE construct and the
definition of fix pc is
definition
fix pc :: "int ⇒((α * int) * (α * int)) set ⇒(α * α) set"
where "fix pc pc R = {(s’,s). ((s’,pc),(s,pc)) ∈R}"
The function fix pc removes the dependence on the PC by restricting a relation
to the given PC value. Again, RL can replace RL ∩ I sometimes.</p>
        <p>Second, if the loop has inner nested loops, then we define its variant V as
V = fix pc L R+
where R is the transition relation of the entire program. The formula on the right
hand side is indeed a variant, because any path that starts from and finishes at
the WHILE with location L must have its corresponding transition relation ρ as
a subset of R+, i.e. ρ ⊆ R+. In addition, ρ must be a set containing tuples
of the form ((s′, pc′), (s, pc)), where pc = L and pc′ = L. We will discuss the
well-foundedness property of V in section 5.
4.3</p>
      </sec>
      <sec id="sec-4-3">
        <title>An Optimization</title>
        <p>The complexity of the WF proofs for variants largely depends on the number of
WF relations our tool generates for the WHILE constructs. If a WHILE construct
is shown terminating using a set of WF relations, then it may also be possible
to find another smaller set of WF relations that does the job. Suppose we have
two WF relations T1 and T2, which show the termination of two cyclic paths π1
and π2 using ρ1 ⊆ T1 ∧ ρ2 ⊆ T2, where ρ1 and ρ2 are the path transition relations
for the two paths. Then if we can generate a weaker WF relation S such that
ρ1 ⊆ S ∧ ρ2 ⊆ S, then we can replace T1 and T2 by S. Please note, in general we
cannot derive S by simply making a union of the two WF relations, since the
result of the union may not be well-founded.</p>
        <p>For each WHILE construct, our tool attempts to generate a WF relation when
a possibly non-terminating cyclic path is found. Therefore, the order in which the
WF relations are generated depends on the order in which these cyclic paths are
detected. Since we use Blast to detect these cyclic paths, we have no control
over its searching strategy and so we cannot ask for any specific paths to be
reported first.</p>
        <p>For a WHILE construct W that has one or more inner loops, some of W ’s
cyclic paths (i.e. those start from and finish at W ) enter inner loops (call them
P1) while some do not (call them P2). It may happen that there is something
decreasing along the execution of W , regardless whether any of W ’s inner loops
are entered. More precisely, there may be a set T with one or more WF relations
that cover paths from both P1 and P2. This means, we may be able to generate
T without entering any of W ’s inner loops. We call this set of WF relations the
global WF relations for W , since it exists regardless of the inner loops’ behaviour.</p>
        <p>Suppose there is one inner WHILE U of W , and the path transition relation
from W to U is ρ1, the path transition of U loop is ρu and the path transition
relation from U back to W is ¬b∧ρ2, where b is the guard of U , i.e. the condition
when U is entered. The path transition relation from W back to W is
ρ = (¬b ∧ ρ2) o ρu+ o ρ1.</p>
        <p>To generate the required T , we generate WF relations for ρA = ρ2 o ρ1. This
path corresponds to a program W ′, which is W with U completely commented
out. We do not generate WF relations for ρB = (¬b ∧ ρ2) o ρ1. since this path
simulates the effect that the guard of U is not true. Clearly ρA is weaker than ρB
and if a WF relation can be used for ρA, then it can be used for ρB. Nevertheless,
our aim is to have the generated WF relations to work for ρ as well; if the WF
relation for ρB is too strong, then it may not work for ρ.</p>
        <p>Of course, a given WHILE construct W may not have this global set T of WF
relations. As a result, we still need to generate WF relations for all cyclic paths
that enter inner loops. The advantage of generating T is that some relations in
T may make it unnecessary to generate new WF relations for some cyclic paths,
thereby reducing the number of relations generated. We have implemented this
optimization in our termination checker.
4.4</p>
      </sec>
      <sec id="sec-4-4">
        <title>An Issue of Reachability</title>
        <p>As we have mentioned, the technique of termination checking works by
ensuring all reachable cyclic paths are terminating. When we use Blast to check
the reachability of the ERROR location, the notion of reachable cyclic path is
already present implicitly with Blast. However, sometimes we need to express
reachability explicitly, for two reasons.</p>
        <p>The first one is for Rankfinder to generate WF relations. For example, we
may have a program
y = 2; WHILE (x &gt; 0){x = x - y;}
Without knowing y &gt; 0, the transition relation of WHILE’s cyclic path is not
well-founded and so Rankfinder will fail to generate a WF relation for it. To
strengthen the transition relation, we note that y &gt; 0 is an invariant and by
adding it to the transition relation of the path, the new relation is indeed WF.</p>
        <p>The second reason for including the reachability condition is to have a strong
enough variant. There may be non-terminating cyclic paths that do not concern
Blast since they are deemed to be unreachable, and so Rankfinder will not
have to generate WF relations for them. However, when defining the variant,
which are effectively the transition relations of paths, we need to incorporate in
it the reachability condition so that unreachable paths are removed from it.</p>
        <p>We have tried several ways of expressing this reachability requirement of
loop variants. A simple way is to include the loop’s invariant in the variant. For
single-looped program, we define the variant as RL ∩ I. For an innermost loop,
we define its variant as fix pc L (RL ∩ I). We have used this method to prove
problems that were not provable otherwise.</p>
        <p>If the WHILE construct has nested inner loops, its variant can also be
strengthened by adding invariants. To discover an invariant can require much thought,
and ideally we would use an automatic tool for generating invariants. At present
we are using no such tool and have decided to use fix pc L R+ as the variant.
This heuristic choice does not affect the soundness of our tool and will not affect
the way the tool is used as an oracle. When the tool is used as a proof method, if
a non-reachable non-terminating cyclic path is included, then a user will fail to
prove that a (non-WF!) path transition is well-founded. This is a signal that the
path may be in fact not reachable. The user can then make another attempt:
either trust the oracle or try to strengthen the variant by finding a strong invariant
of the entire program.
5</p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>Proving Variants being Well-Founded</title>
      <p>Having generated the required variants, we need to show their well-foundedness.
Showing that the relation defined for V is a variant is a separate task, which
requires the users to have found the correct invariants.</p>
      <p>When the generated V has the form measure M , we can apply Isabelle’s
existing methods to show it is WF automatically. Otherwise, there are several
possibilities:
– A single-loop program: the variant has the form RL ∩ I or RL.
– A multi-loop program: the variant has the form fix pc L R, where R is either</p>
      <p>
        RL ∩ I or RL.
– In the most complicated case, the variant has the form fix pc L R+.
The proofs are based on disjunctively well-founded relations of Podelski and
Rybalchenko [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]. A relation is disjunctively well-founded, if it is the union of
finitely many well-founded relations. We need to formalize them for Isabelle
proofs to work.
5.1
      </p>
    </sec>
    <sec id="sec-6">
      <title>Proving that R and fix pc L R are Well-Founded</title>
      <p>These are the simpler of the three cases. In order to show that R is WF, we
need to prove R+ is WF. We have proved the following two essential theorems
in Isabelle:
theorem union wf disj wf1:</p>
      <p>" [ Vs. s ∈ R =⇒ wf s; r ⊆ S R; finite R ] =⇒ disj wf r"
theorem trans disj wf implies wf:</p>
      <p>
        " [ trans r; disj wf r ] =⇒ wf r"
The first theorem characterizes what it means for a relation r to be disjunctively
well-founded (disj wf ). The second theorem states the crucial result that if a
relation is both transitive and disjunctively WF, then it is WF. The Isabelle
proof follows the informal argument [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] in using Ramsey’s theorem. Using these
two theorems, we can prove that R+ is WF by proving R+ ⊆ S T , where T is
the set of WF relations the tool generates. We can prove that R is well-founded
using another Isabelle lemma:
"wf (r+) =⇒ wf r"
theorem fix pc wf:
      </p>
      <p>"wf R =⇒ wf (fix pc pc R)"
Finally, we have used another theorem to prove variants of the form fix pc L R.
5.2</p>
    </sec>
    <sec id="sec-7">
      <title>Proving that fix pc L R+ is Well-Founded</title>
      <p>This is the complicated case and we have tried two approaches.</p>
      <p>In the first approach, we tried to prove R+ is WF by restricting attention to
cyclic paths. In order to restrict R+ to the transitions of cyclic paths, we have
defined the constant same pc :
definition
same pc :: "((α * int) * (α * int)) set"
where "same pc = {((s’,pc’),(s,pc)). pc’ = pc}"
Now R+ ∩ same pc denotes the subset of R+ concerning cyclic paths. We need to
prove that the relation fix pc L R+ is well-founded. It suffices to show</p>
      <p>R+ ∩ same pc ⊆ [ T,
where T is the set of generated WF relations. We cannot prove this by induction
because the same pc property is not preserved from one transition to the next. To
make this approach work, we need to identify an invariant S of R+, such that
we can prove R+ ⊆ S by induction and then prove S ∩ same pc ⊆ S T .</p>
      <p>In the second approach, we attempted to prove that R+ ⊆ S T directly.
Since R+ includes both cyclic and non-cyclic paths, we tried modifying the
tool to generate WF relations for non-cyclic paths as well. However, we found
that R+ ⊆ S T still could not be proved by induction, apparently because the
induction hypothesis was too weak: the set S T was too large. We suspect that
it is not practical to generate sufficiently strong WF relations for all non-cyclic
paths because there are simply too many such paths.
5.3</p>
      <sec id="sec-7-1">
        <title>Automation in WF Proofs</title>
        <p>We have implemented several Isabelle proof methods to invoke the termination
checker. When the tool generates all the required WF relations and shows the
program is terminating, the goal will be modified with variants inserted. The
generated WF relations are also proved automatically to be WF. There is also
an option to insert the WF relations as theorems to the assumption of the proof
goal for users to inspect.</p>
        <p>After this step, we can use vcg followed by auto to finish the proof, if the
variants are measure functions. For the variants of the forms R, R∩ I, fix pc L R
or fix pc L (R∩ I), we have implemented proof methods check wf sw and check wf mw
to prove their well-foundedness automatically.
6</p>
      </sec>
    </sec>
    <sec id="sec-8">
      <title>Examples and Experiments</title>
      <p>Our termination tool is still in the early stage of development. At the moment,
it does not support pointers or data structures, such as arrays. However, based
on our current development, we can easily add in support for arrays, though
pointers require more effort.</p>
      <p>Users invoke the termination tool via Isabelle methods: check termination oracle
uses the tool as an oracle; check termination and check terminationH construct
variants and the latter uses the optimization(§4.3). If variants are generated, users
will need to apply a a few more Isabelle methods to prove the variants being
WF. For this step to work, users usually also need to construct invariants. For
example, in order to prove the lemma
lemma " Γ ⊢t {|True |}</p>
      <p>WHILE (x &gt;= 0) INV {|True |}
DO
x :== x + 1;; y :== 1;;
(WHILE (y &lt;= x ∨ p &gt; 0) FIX X. INV {|x = X |}
DO y :== y + 1;; p :== p - 2 OD);;
x :== x - 2
OD
{|True |}"
we first apply check terminationH to generate variants and then finish the proof
with vcg, auto and check wf mw. check wf mw is an Isabelle method that we have</p>
      <p>
        Result Remark
proved by oracle Fibonacci series with two nested WHILEs, no invariants
proved by oracle Factorial with two nested WHILEs, no invariants
proved by oracle Arithmetic exponentiation with two nested WHILEs, no invariants
proved by oracle Example from [
        <xref ref-type="bibr" rid="ref11 ref2">2</xref>
        ]. Two nested WHILEs, no invariants
proved by method Example lemma shown above
proved by method Artificial example with two nested WHILEs, with invariants
proved by oracle Artificial example with three nested WHILEs, no invariants
Blast failed to terminate Arithmetic exponentiation with three nested WHILEs
Blast failed to terminate Artificial example with three nested WHILEs
reported as non-terminating A non-terminating program
reported as non-terminating A terminating Euclid algorithm, no invariants
Automatic termination checking is too valuable a tool to reserve for the field of
automated program analysis. Interactive program verifiers would like to benefit
from as much automation as possible. We have shown how techniques designed
for automatic termination checking can, in many cases, be incorporated in a
Hoare logic proof, with the termination argument made explicit as variant
functions in WHILE loops. The resulting subgoals can often be proved automatically,
using dedicated proof methods that we have written. In the most complicated
loop structures, the information returned by the automated analysis does not
appear to be detailed enough to allow the proof to be reproduced in Isabelle/HOL.
To handle those cases, we have also implemented an interface to the tool that
accepts the termination as an oracle.
      </p>
      <p>
        In order to meet our objectives, we have formalized Podelski and
Rybalchenko’s theory of disjunctive well-foundedness [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] in Isabelle/HOL2, and we
have optimized the termination tool to eliminate redundant outputs that would
complicated the proofs.
      </p>
      <p>Acknowledgements. The research was funded by the L4.verified project of
National ICT Australia.3 Norbert Schirmer answered our questions about his
implementation of Hoare logic, and Ranjit Jhala answered questions about Blast.
In formalizing the theory of disjunctive well-foundedness, we used an Isabelle
proof of Ramsey’s theorem contributed by Tom Ridge.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Ju</surname>
          </string-name>
          <article-title>¨rgen Brauburger and Ju¨rgen Giesl. Approximating the domains of functional and imperative programs</article-title>
          .
          <source>Sci. Comput</source>
          . Program.,
          <volume>35</volume>
          (
          <issue>2</issue>
          ):
          <fpage>113</fpage>
          -
          <lpage>136</lpage>
          ,
          <year>1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Byron</given-names>
            <surname>Cook</surname>
          </string-name>
          , Andreas Podelski, and
          <string-name>
            <given-names>Andrey</given-names>
            <surname>Rybalchenko</surname>
          </string-name>
          .
          <article-title>Abstraction refinement for termination</article-title>
          .
          <source>In Chris Hankin and Igor Siveroni</source>
          , editors,
          <source>SAS</source>
          , volume
          <volume>3672</volume>
          of Lecture Notes in Computer Science, pages
          <fpage>87</fpage>
          -
          <lpage>101</lpage>
          . Springer,
          <year>2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <given-names>Byron</given-names>
            <surname>Cook</surname>
          </string-name>
          , Andreas Podelski, and
          <string-name>
            <given-names>Andrey</given-names>
            <surname>Rybalchenko</surname>
          </string-name>
          .
          <article-title>Termination proofs for systems code</article-title>
          .
          <source>In PLDI '06: Proceedings of the 2006 ACM SIGPLAN conference on Programming language design and implementation</source>
          , pages
          <fpage>415</fpage>
          -
          <lpage>426</lpage>
          , New York, NY, USA,
          <year>2006</year>
          . ACM Press.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Thomas</surname>
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Henzinger</surname>
            , Ranjit Jhala, Rupak Majumdar, and
            <given-names>Gregoire</given-names>
          </string-name>
          <string-name>
            <surname>Sutre</surname>
          </string-name>
          .
          <article-title>Software verification with Blast</article-title>
          . In Thomas Ball and
          <article-title>Sriram</article-title>
          K. Rajamani, editors,
          <source>Model Checking Software, 10th International SPIN Workshop, Lecture Notes in Computer Science</source>
          <volume>2648</volume>
          , pages
          <fpage>235</fpage>
          -
          <lpage>239</lpage>
          . Springer-Verlag,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <given-names>Zohar</given-names>
            <surname>Manna</surname>
          </string-name>
          and
          <string-name>
            <given-names>Amir</given-names>
            <surname>Pnueli</surname>
          </string-name>
          .
          <article-title>The temporal logic of reactive and concurrent systems</article-title>
          . Springer-Verlag New York, Inc., New York, NY, USA,
          <year>1992</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>Tobias</given-names>
            <surname>Nipkow</surname>
          </string-name>
          , Lawrence C. Paulson, and Markus Wenzel. Isabelle/HOL:
          <article-title>A Proof Assistant for Higher-Order Logic</article-title>
          . Springer,
          <year>2002</year>
          . LNCS Tutorial 2283.
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <given-names>Andreas</given-names>
            <surname>Podelski</surname>
          </string-name>
          and
          <string-name>
            <given-names>Andrey</given-names>
            <surname>Rybalchenko</surname>
          </string-name>
          .
          <article-title>A complete method for the synthesis of linear ranking functions</article-title>
          .
          <source>In Bernhard Steffen and Giorgio Levi</source>
          , editors,
          <source>VMCAI</source>
          , volume
          <volume>2937</volume>
          of Lecture Notes in Computer Science, pages
          <fpage>239</fpage>
          -
          <lpage>251</lpage>
          . Springer,
          <year>2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>Andreas</given-names>
            <surname>Podelski</surname>
          </string-name>
          and
          <string-name>
            <given-names>Andrey</given-names>
            <surname>Rybalchenko</surname>
          </string-name>
          .
          <article-title>Transition invariants</article-title>
          . In Harald Ganzinger, editor,
          <source>Proceedings of the Nineteenth Annual IEEE Symp. on Logic in Computer Science, LICS 2004</source>
          , pages
          <fpage>32</fpage>
          -
          <lpage>41</lpage>
          . IEEE Computer Society Press,
          <year>July 2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <given-names>Norbert</given-names>
            <surname>Schirmer</surname>
          </string-name>
          .
          <article-title>Verification of Sequential Imperative Programs in Isabelle/HOL</article-title>
          .
          <source>PhD thesis</source>
          , Technische Universit¨at Mu¨nchen,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Harvey</surname>
            <given-names>Tuch</given-names>
          </string-name>
          , Gerwin Klein, and
          <string-name>
            <given-names>Michael</given-names>
            <surname>Norrish</surname>
          </string-name>
          . Types, bytes, and
          <article-title>separation logic</article-title>
          .
          <source>In Martin Hofmann and Matthias Felleisen</source>
          , editors,
          <source>Proc. 34th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'07)</source>
          , pages
          <fpage>97</fpage>
          -
          <lpage>108</lpage>
          , Nice, France,
          <year>January 2007</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          <article-title>2 The proofs will appear in a future technical report</article-title>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          <article-title>3 National ICT Australia is funded by the Australian Government's Department of Communications, Information Technology, and the Arts and the Australian Research Council through Backing Australia's Ability and the ICT Research Centre of Excellence programs</article-title>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>