{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.WeakestPreconditions.IntSqrt where
import Data.SBV
import Data.SBV.Control
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
import Prelude hiding (sqrt)
data SqrtS a = SqrtS { SqrtS a -> a
x :: a
, SqrtS a -> a
sqrt :: a
, SqrtS a -> a
i :: a
, SqrtS a -> a
j :: a
}
deriving (Int -> SqrtS a -> ShowS
[SqrtS a] -> ShowS
SqrtS a -> String
(Int -> SqrtS a -> ShowS)
-> (SqrtS a -> String) -> ([SqrtS a] -> ShowS) -> Show (SqrtS a)
forall a. Show a => Int -> SqrtS a -> ShowS
forall a. Show a => [SqrtS a] -> ShowS
forall a. Show a => SqrtS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SqrtS a] -> ShowS
$cshowList :: forall a. Show a => [SqrtS a] -> ShowS
show :: SqrtS a -> String
$cshow :: forall a. Show a => SqrtS a -> String
showsPrec :: Int -> SqrtS a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SqrtS a -> ShowS
Show, (forall x. SqrtS a -> Rep (SqrtS a) x)
-> (forall x. Rep (SqrtS a) x -> SqrtS a) -> Generic (SqrtS a)
forall x. Rep (SqrtS a) x -> SqrtS a
forall x. SqrtS a -> Rep (SqrtS a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SqrtS a) x -> SqrtS a
forall a x. SqrtS a -> Rep (SqrtS a) x
$cto :: forall a x. Rep (SqrtS a) x -> SqrtS a
$cfrom :: forall a x. SqrtS a -> Rep (SqrtS a) x
Generic, Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a
(Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a)
-> (forall b.
(Ord b, SymVal b, Num b) =>
[SqrtS a] -> SqrtS a -> SBV b -> SqrtS a)
-> Mergeable (SqrtS a)
forall b.
(Ord b, SymVal b, Num b) =>
[SqrtS a] -> SqrtS a -> SBV b -> SqrtS a
forall a.
Mergeable a =>
Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a
forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[SqrtS a] -> SqrtS a -> SBV b -> SqrtS a
forall a.
(Bool -> SBool -> a -> a -> a)
-> (forall b. (Ord b, SymVal b, Num b) => [a] -> a -> SBV b -> a)
-> Mergeable a
select :: [SqrtS a] -> SqrtS a -> SBV b -> SqrtS a
$cselect :: forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[SqrtS a] -> SqrtS a -> SBV b -> SqrtS a
symbolicMerge :: Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a
$csymbolicMerge :: forall a.
Mergeable a =>
Bool -> SBool -> SqrtS a -> SqrtS a -> SqrtS a
Mergeable, a -> SqrtS b -> SqrtS a
(a -> b) -> SqrtS a -> SqrtS b
(forall a b. (a -> b) -> SqrtS a -> SqrtS b)
-> (forall a b. a -> SqrtS b -> SqrtS a) -> Functor SqrtS
forall a b. a -> SqrtS b -> SqrtS a
forall a b. (a -> b) -> SqrtS a -> SqrtS b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SqrtS b -> SqrtS a
$c<$ :: forall a b. a -> SqrtS b -> SqrtS a
fmap :: (a -> b) -> SqrtS a -> SqrtS b
$cfmap :: forall a b. (a -> b) -> SqrtS a -> SqrtS b
Functor, SqrtS a -> Bool
(a -> m) -> SqrtS a -> m
(a -> b -> b) -> b -> SqrtS a -> b
(forall m. Monoid m => SqrtS m -> m)
-> (forall m a. Monoid m => (a -> m) -> SqrtS a -> m)
-> (forall m a. Monoid m => (a -> m) -> SqrtS a -> m)
-> (forall a b. (a -> b -> b) -> b -> SqrtS a -> b)
-> (forall a b. (a -> b -> b) -> b -> SqrtS a -> b)
-> (forall b a. (b -> a -> b) -> b -> SqrtS a -> b)
-> (forall b a. (b -> a -> b) -> b -> SqrtS a -> b)
-> (forall a. (a -> a -> a) -> SqrtS a -> a)
-> (forall a. (a -> a -> a) -> SqrtS a -> a)
-> (forall a. SqrtS a -> [a])
-> (forall a. SqrtS a -> Bool)
-> (forall a. SqrtS a -> Int)
-> (forall a. Eq a => a -> SqrtS a -> Bool)
-> (forall a. Ord a => SqrtS a -> a)
-> (forall a. Ord a => SqrtS a -> a)
-> (forall a. Num a => SqrtS a -> a)
-> (forall a. Num a => SqrtS a -> a)
-> Foldable SqrtS
forall a. Eq a => a -> SqrtS a -> Bool
forall a. Num a => SqrtS a -> a
forall a. Ord a => SqrtS a -> a
forall m. Monoid m => SqrtS m -> m
forall a. SqrtS a -> Bool
forall a. SqrtS a -> Int
forall a. SqrtS a -> [a]
forall a. (a -> a -> a) -> SqrtS a -> a
forall m a. Monoid m => (a -> m) -> SqrtS a -> m
forall b a. (b -> a -> b) -> b -> SqrtS a -> b
forall a b. (a -> b -> b) -> b -> SqrtS a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: SqrtS a -> a
$cproduct :: forall a. Num a => SqrtS a -> a
sum :: SqrtS a -> a
$csum :: forall a. Num a => SqrtS a -> a
minimum :: SqrtS a -> a
$cminimum :: forall a. Ord a => SqrtS a -> a
maximum :: SqrtS a -> a
$cmaximum :: forall a. Ord a => SqrtS a -> a
elem :: a -> SqrtS a -> Bool
$celem :: forall a. Eq a => a -> SqrtS a -> Bool
length :: SqrtS a -> Int
$clength :: forall a. SqrtS a -> Int
null :: SqrtS a -> Bool
$cnull :: forall a. SqrtS a -> Bool
toList :: SqrtS a -> [a]
$ctoList :: forall a. SqrtS a -> [a]
foldl1 :: (a -> a -> a) -> SqrtS a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SqrtS a -> a
foldr1 :: (a -> a -> a) -> SqrtS a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SqrtS a -> a
foldl' :: (b -> a -> b) -> b -> SqrtS a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SqrtS a -> b
foldl :: (b -> a -> b) -> b -> SqrtS a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SqrtS a -> b
foldr' :: (a -> b -> b) -> b -> SqrtS a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SqrtS a -> b
foldr :: (a -> b -> b) -> b -> SqrtS a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SqrtS a -> b
foldMap' :: (a -> m) -> SqrtS a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SqrtS a -> m
foldMap :: (a -> m) -> SqrtS a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SqrtS a -> m
fold :: SqrtS m -> m
$cfold :: forall m. Monoid m => SqrtS m -> m
Foldable, Functor SqrtS
Foldable SqrtS
Functor SqrtS
-> Foldable SqrtS
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SqrtS a -> f (SqrtS b))
-> (forall (f :: * -> *) a.
Applicative f =>
SqrtS (f a) -> f (SqrtS a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SqrtS a -> m (SqrtS b))
-> (forall (m :: * -> *) a. Monad m => SqrtS (m a) -> m (SqrtS a))
-> Traversable SqrtS
(a -> f b) -> SqrtS a -> f (SqrtS b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => SqrtS (m a) -> m (SqrtS a)
forall (f :: * -> *) a. Applicative f => SqrtS (f a) -> f (SqrtS a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SqrtS a -> m (SqrtS b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SqrtS a -> f (SqrtS b)
sequence :: SqrtS (m a) -> m (SqrtS a)
$csequence :: forall (m :: * -> *) a. Monad m => SqrtS (m a) -> m (SqrtS a)
mapM :: (a -> m b) -> SqrtS a -> m (SqrtS b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SqrtS a -> m (SqrtS b)
sequenceA :: SqrtS (f a) -> f (SqrtS a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SqrtS (f a) -> f (SqrtS a)
traverse :: (a -> f b) -> SqrtS a -> f (SqrtS b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SqrtS a -> f (SqrtS b)
$cp2Traversable :: Foldable SqrtS
$cp1Traversable :: Functor SqrtS
Traversable)
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (SqrtS (SBV a)) where
show :: SqrtS (SBV a) -> String
show (SqrtS SBV a
x SBV a
sqrt SBV a
i SBV a
j) = String
"{x = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (Show a, SymVal a) => SBV a -> String
sh SBV a
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", sqrt = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (Show a, SymVal a) => SBV a -> String
sh SBV a
sqrt String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", i = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (Show a, SymVal a) => SBV a -> String
sh SBV a
i String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", j = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (Show a, SymVal a) => SBV a -> String
sh SBV a
j String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"}"
where sh :: SBV a -> String
sh SBV a
v = String -> (a -> String) -> Maybe a -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"<symbolic>" a -> String
forall a. Show a => a -> String
show (SBV a -> Maybe a
forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
v)
instance SymVal a => Fresh IO (SqrtS (SBV a)) where
fresh :: QueryT IO (SqrtS (SBV a))
fresh = SBV a -> SBV a -> SBV a -> SBV a -> SqrtS (SBV a)
forall a. a -> a -> a -> a -> SqrtS a
SqrtS (SBV a -> SBV a -> SBV a -> SBV a -> SqrtS (SBV a))
-> QueryT IO (SBV a)
-> QueryT IO (SBV a -> SBV a -> SBV a -> SqrtS (SBV a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QueryT IO (SBV a)
forall a. SymVal a => Query (SBV a)
freshVar_ QueryT IO (SBV a -> SBV a -> SBV a -> SqrtS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SBV a -> SBV a -> SqrtS (SBV a))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> QueryT IO (SBV a)
forall a. SymVal a => Query (SBV a)
freshVar_ QueryT IO (SBV a -> SBV a -> SqrtS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SBV a -> SqrtS (SBV a))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> QueryT IO (SBV a)
forall a. SymVal a => Query (SBV a)
freshVar_ QueryT IO (SBV a -> SqrtS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SqrtS (SBV a))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> QueryT IO (SBV a)
forall a. SymVal a => Query (SBV a)
freshVar_
type S = SqrtS SInteger
algorithm :: Invariant S -> Maybe (Measure S) -> Stmt S
algorithm :: Invariant S -> Maybe (Measure S) -> Stmt S
algorithm Invariant S
inv Maybe (Measure S)
msr = [Stmt S] -> Stmt S
forall st. [Stmt st] -> Stmt st
Seq [ String -> Invariant S -> Stmt S
forall st. String -> (st -> SBool) -> Stmt st
assert String
"x >= 0" (Invariant S -> Stmt S) -> Invariant S -> Stmt S
forall a b. (a -> b) -> a -> b
$ \SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x} -> SInteger
x SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
, (S -> S) -> Stmt S
forall st. (st -> st) -> Stmt st
Assign ((S -> S) -> Stmt S) -> (S -> S) -> Stmt S
forall a b. (a -> b) -> a -> b
$ \S
st -> S
st{sqrt :: SInteger
sqrt = SInteger
0, i :: SInteger
i = SInteger
1, j :: SInteger
j = SInteger
1}
, String
-> Invariant S
-> Maybe (Measure S)
-> Invariant S
-> Stmt S
-> Stmt S
forall st.
String
-> Invariant st
-> Maybe (Measure st)
-> Invariant st
-> Stmt st
-> Stmt st
While String
"i <= x"
Invariant S
inv
Maybe (Measure S)
msr
(\SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x, SInteger
i :: SInteger
i :: forall a. SqrtS a -> a
i} -> SInteger
i SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.<= SInteger
x)
(Stmt S -> Stmt S) -> Stmt S -> Stmt S
forall a b. (a -> b) -> a -> b
$ [Stmt S] -> Stmt S
forall st. [Stmt st] -> Stmt st
Seq [ (S -> S) -> Stmt S
forall st. (st -> st) -> Stmt st
Assign ((S -> S) -> Stmt S) -> (S -> S) -> Stmt S
forall a b. (a -> b) -> a -> b
$ \st :: S
st@SqrtS{SInteger
sqrt :: SInteger
sqrt :: forall a. SqrtS a -> a
sqrt} -> S
st{sqrt :: SInteger
sqrt = SInteger
sqrt SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
1}
, (S -> S) -> Stmt S
forall st. (st -> st) -> Stmt st
Assign ((S -> S) -> Stmt S) -> (S -> S) -> Stmt S
forall a b. (a -> b) -> a -> b
$ \st :: S
st@SqrtS{SInteger
j :: SInteger
j :: forall a. SqrtS a -> a
j} -> S
st{j :: SInteger
j = SInteger
j SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
2}
, (S -> S) -> Stmt S
forall st. (st -> st) -> Stmt st
Assign ((S -> S) -> Stmt S) -> (S -> S) -> Stmt S
forall a b. (a -> b) -> a -> b
$ \st :: S
st@SqrtS{SInteger
i :: SInteger
i :: forall a. SqrtS a -> a
i, SInteger
j :: SInteger
j :: forall a. SqrtS a -> a
j} -> S
st{i :: SInteger
i = SInteger
i SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
j}
]
]
pre :: S -> SBool
pre :: Invariant S
pre SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x} = SInteger
x SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
post :: S -> SBool
post :: Invariant S
post SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x, SInteger
sqrt :: SInteger
sqrt :: forall a. SqrtS a -> a
sqrt} = SInteger -> SInteger
forall a. Num a => a -> a
sq SInteger
sqrt SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.<= SInteger
x SBool -> SBool -> SBool
.&& SInteger -> SInteger
forall a. Num a => a -> a
sq (SInteger
sqrtSInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+SInteger
1) SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
x
where sq :: a -> a
sq a
n = a
n a -> a -> a
forall a. Num a => a -> a -> a
* a
n
noChange :: Stable S
noChange :: Stable S
noChange = [String -> (S -> SInteger) -> S -> S -> (String, SBool)
forall a st.
EqSymbolic a =>
String -> (st -> a) -> st -> st -> (String, SBool)
stable String
"x" S -> SInteger
forall a. SqrtS a -> a
x]
imperativeSqrt :: Invariant S -> Maybe (Measure S) -> Program S
imperativeSqrt :: Invariant S -> Maybe (Measure S) -> Program S
imperativeSqrt Invariant S
inv Maybe (Measure S)
msr = Program :: forall st.
Symbolic ()
-> (st -> SBool)
-> Stmt st
-> (st -> SBool)
-> Stable st
-> Program st
Program { setup :: Symbolic ()
setup = () -> Symbolic ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
, precondition :: Invariant S
precondition = Invariant S
pre
, program :: Stmt S
program = Invariant S -> Maybe (Measure S) -> Stmt S
algorithm Invariant S
inv Maybe (Measure S)
msr
, postcondition :: Invariant S
postcondition = Invariant S
post
, stability :: Stable S
stability = Stable S
noChange
}
invariant :: Invariant S
invariant :: Invariant S
invariant SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x, SInteger
sqrt :: SInteger
sqrt :: forall a. SqrtS a -> a
sqrt, SInteger
i :: SInteger
i :: forall a. SqrtS a -> a
i, SInteger
j :: SInteger
j :: forall a. SqrtS a -> a
j} = SInteger
j SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger -> SInteger
forall a. Num a => a -> a
sq SInteger
sqrt SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.<= SInteger
x SBool -> SBool -> SBool
.&& SInteger
i SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger
forall a. Num a => a -> a
sq (SInteger
sqrt SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
1) SBool -> SBool -> SBool
.&& SInteger
j SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
2SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
*SInteger
sqrt SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
1
where sq :: a -> a
sq a
n = a
n a -> a -> a
forall a. Num a => a -> a -> a
* a
n
measure :: Measure S
measure :: Measure S
measure SqrtS{SInteger
x :: SInteger
x :: forall a. SqrtS a -> a
x, SInteger
i :: SInteger
i :: forall a. SqrtS a -> a
i} = [SInteger
x SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
- SInteger
i]
correctness :: IO ()
correctness :: IO ()
correctness = ProofResult (SqrtS Integer) -> IO ()
forall a. Show a => a -> IO ()
print (ProofResult (SqrtS Integer) -> IO ())
-> IO (ProofResult (SqrtS Integer)) -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< WPConfig -> Program S -> IO (ProofResult (SqrtS Integer))
forall st res.
(Show res, Mergeable st, Queriable IO st res) =>
WPConfig -> Program st -> IO (ProofResult res)
wpProveWith WPConfig
defaultWPCfg{wpVerbose :: Bool
wpVerbose=Bool
True} (Invariant S -> Maybe (Measure S) -> Program S
imperativeSqrt Invariant S
invariant (Measure S -> Maybe (Measure S)
forall a. a -> Maybe a
Just Measure S
measure))