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.IntDiv

Description

Proof of correctness of an imperative integer division algorithm, using weakest preconditions. The algorithm simply keeps subtracting the divisor until the desired quotient and the remainder is found.

Synopsis

# Program state

data DivS a Source #

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

Constructors

 DivS Fieldsx :: aThe dividendy :: aThe divisorq :: aThe quotientr :: aThe remainder
Instances
 Source # Instance details Methodsfmap :: (a -> b) -> DivS a -> DivS b #(<\$) :: a -> DivS b -> DivS a # Source # Instance details Methodsfold :: Monoid m => DivS m -> m #foldMap :: Monoid m => (a -> m) -> DivS a -> m #foldr :: (a -> b -> b) -> b -> DivS a -> b #foldr' :: (a -> b -> b) -> b -> DivS a -> b #foldl :: (b -> a -> b) -> b -> DivS a -> b #foldl' :: (b -> a -> b) -> b -> DivS a -> b #foldr1 :: (a -> a -> a) -> DivS a -> a #foldl1 :: (a -> a -> a) -> DivS a -> a #toList :: DivS a -> [a] #null :: DivS a -> Bool #length :: DivS a -> Int #elem :: Eq a => a -> DivS a -> Bool #maximum :: Ord a => DivS a -> a #minimum :: Ord a => DivS a -> a #sum :: Num a => DivS a -> a #product :: Num a => DivS a -> a # Source # Instance details Methodstraverse :: Applicative f => (a -> f b) -> DivS a -> f (DivS b) #sequenceA :: Applicative f => DivS (f a) -> f (DivS a) #mapM :: Monad m => (a -> m b) -> DivS a -> m (DivS b) #sequence :: Monad m => DivS (m a) -> m (DivS a) # (SymVal a, SMTValue a) => Fresh IO (DivS (SBV a)) Source # Fresh instance for the program state Instance details Methodsfresh :: QueryT IO (DivS (SBV a)) Source # Show a => Show (DivS a) Source # Instance details MethodsshowsPrec :: Int -> DivS a -> ShowS #show :: DivS a -> String #showList :: [DivS a] -> ShowS # (SymVal a, Show a) => Show (DivS (SBV a)) Source # Show instance for DivS. 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 -> DivS (SBV a) -> ShowS #show :: DivS (SBV a) -> String #showList :: [DivS (SBV a)] -> ShowS # Generic (DivS a) Source # Instance details Associated Typestype Rep (DivS a) :: Type -> Type # Methodsfrom :: DivS a -> Rep (DivS a) x #to :: Rep (DivS a) x -> DivS a # Mergeable a => Mergeable (DivS a) Source # Instance details MethodssymbolicMerge :: Bool -> SBool -> DivS a -> DivS a -> DivS a Source #select :: (Ord b, SymVal b, Num b) => [DivS a] -> DivS a -> SBV b -> DivS a Source # type Rep (DivS a) Source # Instance details type Rep (DivS a) = D1 (MetaData "DivS" "Documentation.SBV.Examples.WeakestPreconditions.IntDiv" "sbv-8.1-KJ81LMQmRNq7C7R4pcgIua" False) (C1 (MetaCons "DivS" PrefixI True) ((S1 (MetaSel (Just "x") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: S1 (MetaSel (Just "y") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) :*: (S1 (MetaSel (Just "q") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: S1 (MetaSel (Just "r") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))))

type D = DivS SInteger Source #

Helper type synonym

# The algorithm

The imperative division algorithm, assuming non-negative x and strictly positive y:

   r = x                     -- set remainder to x
q = 0                     -- set quotient  to 0
while y <= r              -- while we can still subtract
r = r - y                    -- reduce the remainder
q = q + 1                    -- increase the quotient


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 :: D -> SBool Source #

Precondition for our program: x must non-negative and y must be strictly positive. 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: Remainder must be non-negative and less than y, and it must hold that x = q*y + r:

Stability: x and y must remain unchanged.

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

# Correctness

The invariant is simply that x = q * y + r holds at all times and r is strictly positive. We need the y > 0 part of the invariant to establish the measure decreases, which is guaranteed by our precondition.

The measure. In each iteration r decreases, but always remains positive. Since y is strictly positive, r can serve as a measure for the loop.

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

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