sbv-8.1: SMT Based Verification: Symbolic Haskell theorem prover using SMT solving.

Copyright (c) Levent Erkok BSD3 erkokl@gmail.com experimental None Haskell2010

Documentation.SBV.Examples.WeakestPreconditions.IntSqrt

Description

Proof of correctness of an imperative integer square-root algorithm, using weakest preconditions. The algorithm computes the floor of the square-root of a given non-negative integer by keeping a running some of all odd numbers starting from 1. Recall that 1+3+5+...+(2n+1) = (n+1)^2, thus we can stop the counting when we exceed the input number.

Synopsis

# Program state

data SqrtS a Source #

The state for the division program, parameterized over a base type a.

Constructors

 SqrtS Fieldsx :: aThe inputsqrt :: aThe floor of the square rooti :: aSuccessive squares, as the sum of j'sj :: aSuccessive odds
Instances
 Source # Instance details Methodsfmap :: (a -> b) -> SqrtS a -> SqrtS b #(<\$) :: a -> SqrtS b -> SqrtS a # Source # Instance details Methodsfold :: Monoid m => SqrtS m -> m #foldMap :: Monoid m => (a -> m) -> SqrtS a -> m #foldr :: (a -> b -> b) -> b -> SqrtS a -> b #foldr' :: (a -> b -> b) -> b -> SqrtS a -> b #foldl :: (b -> a -> b) -> b -> SqrtS a -> b #foldl' :: (b -> a -> b) -> b -> SqrtS a -> b #foldr1 :: (a -> a -> a) -> SqrtS a -> a #foldl1 :: (a -> a -> a) -> SqrtS a -> a #toList :: SqrtS a -> [a] #null :: SqrtS a -> Bool #length :: SqrtS a -> Int #elem :: Eq a => a -> SqrtS a -> Bool #maximum :: Ord a => SqrtS a -> a #minimum :: Ord a => SqrtS a -> a #sum :: Num a => SqrtS a -> a #product :: Num a => SqrtS a -> a # Source # Instance details Methodstraverse :: Applicative f => (a -> f b) -> SqrtS a -> f (SqrtS b) #sequenceA :: Applicative f => SqrtS (f a) -> f (SqrtS a) #mapM :: Monad m => (a -> m b) -> SqrtS a -> m (SqrtS b) #sequence :: Monad m => SqrtS (m a) -> m (SqrtS a) # (SymVal a, SMTValue a) => Fresh IO (SqrtS (SBV a)) Source # Fresh instance for the program state Instance details Methodsfresh :: QueryT IO (SqrtS (SBV a)) Source # Show a => Show (SqrtS a) Source # Instance details MethodsshowsPrec :: Int -> SqrtS a -> ShowS #show :: SqrtS a -> String #showList :: [SqrtS a] -> ShowS # (SymVal a, Show a) => Show (SqrtS (SBV a)) Source # Show instance for SqrtS. The above deriving clause would work just as well, but we want it to be a little prettier here, and hence the OVERLAPS directive. Instance details MethodsshowsPrec :: Int -> SqrtS (SBV a) -> ShowS #show :: SqrtS (SBV a) -> String #showList :: [SqrtS (SBV a)] -> ShowS # Generic (SqrtS a) Source # Instance details Associated Typestype Rep (SqrtS a) :: Type -> Type # Methodsfrom :: SqrtS a -> Rep (SqrtS a) x #to :: Rep (SqrtS a) x -> SqrtS a # Mergeable a => Mergeable (SqrtS a) Source # Instance details MethodssymbolicMerge :: Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a Source #select :: (Ord b, SymVal b, Num b) => [SqrtS a] -> SqrtS a -> SBV b -> SqrtS a Source # type Rep (SqrtS a) Source # Instance details type Rep (SqrtS a) = D1 (MetaData "SqrtS" "Documentation.SBV.Examples.WeakestPreconditions.IntSqrt" "sbv-8.1-KJ81LMQmRNq7C7R4pcgIua" False) (C1 (MetaCons "SqrtS" PrefixI True) ((S1 (MetaSel (Just "x") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: S1 (MetaSel (Just "sqrt") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) :*: (S1 (MetaSel (Just "i") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: S1 (MetaSel (Just "j") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))))

Helper type synonym

# The algorithm

The imperative square-root algorithm, assuming non-negative x

   sqrt = 0                  -- set sqrt to 0
i    = 1                  -- set i to 1, sum of j's so far
j    = 1                  -- set j to be the first odd number i
while i <= x              -- while the sum hasn't exceeded x yet
sqrt = sqrt + 1              -- increase the sqrt
j    = j + 2                 -- next odd number
i    = i + j                 -- running sum of j's


Note that we need to explicitly annotate each loop with its invariant and the termination measure. For convenience, we take those two as parameters for simplicity.

pre :: S -> SBool Source #

Precondition for our program: x must be non-negative. Note that there is an explicit call to abort in our program to protect against this case, so if we do not have this precondition, all programs will fail.

Postcondition for our program: The sqrt squared must be less than or equal to x, and sqrt+1 squared must strictly exceed x.

Stability condition: Program must leave x unchanged.

A program is the algorithm, together with its pre- and post-conditions.

# Correctness

The invariant is that at each iteration of the loop sqrt remains below or equal to the actual square-root, and i tracks the square of the next value. We also have that j is the sqrt'th odd value. Coming up with this invariant is not for the faint of heart, for details I would strongly recommend looking at Manna's seminal Mathematical Theory of Computation book (chapter 3). The j .> 0 part is needed to establish the termination.

The measure. In each iteration i strictly increases, thus reducing the differential x - i

Check that the program terminates and the post condition holds. We have:

>>> correctness
Total correctness is established.
Q.E.D.