Copyright | (c) Levent Erkok |
---|---|
License | BSD3 |
Maintainer | erkokl@gmail.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Proof of correctness of an imperative list-length algorithm, using weakest preconditions. Illustrates the use of SBV's symbolic lists together with the WP algorithm.
Synopsis
- data LenS a b = LenS {}
- type S = LenS (SList Integer) SInteger
- algorithm :: Invariant S -> Maybe (Measure S) -> Stmt S
- pre :: S -> SBool
- post :: S -> SBool
- noChange :: Stable S
- imperativeLength :: Invariant S -> Maybe (Measure S) -> Program S
- invariant :: Invariant S
- measure :: Measure S
- correctness :: IO ()
Program state
The state of the length program, paramaterized over the element type a
Instances
The algorithm
algorithm :: Invariant S -> Maybe (Measure S) -> Stmt S Source #
The imperative length algorithm:
ys = xs l = 0 while not (null ys) l = l+1 ys = tail ys
Note that we need to explicitly annotate each loop with its invariant and the termination measure. For convenience, we take those two as parameters, so we can experiment later.
imperativeLength :: Invariant S -> Maybe (Measure S) -> Program S Source #
A program is the algorithm, together with its pre- and post-conditions.
invariant :: Invariant S Source #
The invariant simply relates the length of the input to the length of the current suffix and the length of the prefix traversed so far.
The measure is obviously the length of ys
, as we peel elements off of it through the loop.
Correctness
correctness :: IO () Source #
We check that l
is the length of the input list xs
upon termination.
Note that even though this is an inductive proof, it is fairly easy to prove with our SMT based
technology, which doesn't really handle induction at all! The usual inductive proof steps are baked
into the invariant establishment phase of the WP proof. We have:
>>>
correctness
Total correctness is established. Q.E.D.