{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.WeakestPreconditions.Sum where
import Data.SBV
import Data.SBV.Control
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
data SumS a = SumS { SumS a -> a
n :: a
, SumS a -> a
i :: a
, SumS a -> a
s :: a
}
deriving (Int -> SumS a -> ShowS
[SumS a] -> ShowS
SumS a -> String
(Int -> SumS a -> ShowS)
-> (SumS a -> String) -> ([SumS a] -> ShowS) -> Show (SumS a)
forall a. Show a => Int -> SumS a -> ShowS
forall a. Show a => [SumS a] -> ShowS
forall a. Show a => SumS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SumS a] -> ShowS
$cshowList :: forall a. Show a => [SumS a] -> ShowS
show :: SumS a -> String
$cshow :: forall a. Show a => SumS a -> String
showsPrec :: Int -> SumS a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SumS a -> ShowS
Show, (forall x. SumS a -> Rep (SumS a) x)
-> (forall x. Rep (SumS a) x -> SumS a) -> Generic (SumS a)
forall x. Rep (SumS a) x -> SumS a
forall x. SumS a -> Rep (SumS a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SumS a) x -> SumS a
forall a x. SumS a -> Rep (SumS a) x
$cto :: forall a x. Rep (SumS a) x -> SumS a
$cfrom :: forall a x. SumS a -> Rep (SumS a) x
Generic, Bool -> SBool -> SumS a -> SumS a -> SumS a
(Bool -> SBool -> SumS a -> SumS a -> SumS a)
-> (forall b.
(Ord b, SymVal b, Num b) =>
[SumS a] -> SumS a -> SBV b -> SumS a)
-> Mergeable (SumS a)
forall b.
(Ord b, SymVal b, Num b) =>
[SumS a] -> SumS a -> SBV b -> SumS a
forall a.
Mergeable a =>
Bool -> SBool -> SumS a -> SumS a -> SumS a
forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[SumS a] -> SumS a -> SBV b -> SumS a
forall a.
(Bool -> SBool -> a -> a -> a)
-> (forall b. (Ord b, SymVal b, Num b) => [a] -> a -> SBV b -> a)
-> Mergeable a
select :: [SumS a] -> SumS a -> SBV b -> SumS a
$cselect :: forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[SumS a] -> SumS a -> SBV b -> SumS a
symbolicMerge :: Bool -> SBool -> SumS a -> SumS a -> SumS a
$csymbolicMerge :: forall a.
Mergeable a =>
Bool -> SBool -> SumS a -> SumS a -> SumS a
Mergeable, a -> SumS b -> SumS a
(a -> b) -> SumS a -> SumS b
(forall a b. (a -> b) -> SumS a -> SumS b)
-> (forall a b. a -> SumS b -> SumS a) -> Functor SumS
forall a b. a -> SumS b -> SumS a
forall a b. (a -> b) -> SumS a -> SumS b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SumS b -> SumS a
$c<$ :: forall a b. a -> SumS b -> SumS a
fmap :: (a -> b) -> SumS a -> SumS b
$cfmap :: forall a b. (a -> b) -> SumS a -> SumS b
Functor, SumS a -> Bool
(a -> m) -> SumS a -> m
(a -> b -> b) -> b -> SumS a -> b
(forall m. Monoid m => SumS m -> m)
-> (forall m a. Monoid m => (a -> m) -> SumS a -> m)
-> (forall m a. Monoid m => (a -> m) -> SumS a -> m)
-> (forall a b. (a -> b -> b) -> b -> SumS a -> b)
-> (forall a b. (a -> b -> b) -> b -> SumS a -> b)
-> (forall b a. (b -> a -> b) -> b -> SumS a -> b)
-> (forall b a. (b -> a -> b) -> b -> SumS a -> b)
-> (forall a. (a -> a -> a) -> SumS a -> a)
-> (forall a. (a -> a -> a) -> SumS a -> a)
-> (forall a. SumS a -> [a])
-> (forall a. SumS a -> Bool)
-> (forall a. SumS a -> Int)
-> (forall a. Eq a => a -> SumS a -> Bool)
-> (forall a. Ord a => SumS a -> a)
-> (forall a. Ord a => SumS a -> a)
-> (forall a. Num a => SumS a -> a)
-> (forall a. Num a => SumS a -> a)
-> Foldable SumS
forall a. Eq a => a -> SumS a -> Bool
forall a. Num a => SumS a -> a
forall a. Ord a => SumS a -> a
forall m. Monoid m => SumS m -> m
forall a. SumS a -> Bool
forall a. SumS a -> Int
forall a. SumS a -> [a]
forall a. (a -> a -> a) -> SumS a -> a
forall m a. Monoid m => (a -> m) -> SumS a -> m
forall b a. (b -> a -> b) -> b -> SumS a -> b
forall a b. (a -> b -> b) -> b -> SumS 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 :: SumS a -> a
$cproduct :: forall a. Num a => SumS a -> a
sum :: SumS a -> a
$csum :: forall a. Num a => SumS a -> a
minimum :: SumS a -> a
$cminimum :: forall a. Ord a => SumS a -> a
maximum :: SumS a -> a
$cmaximum :: forall a. Ord a => SumS a -> a
elem :: a -> SumS a -> Bool
$celem :: forall a. Eq a => a -> SumS a -> Bool
length :: SumS a -> Int
$clength :: forall a. SumS a -> Int
null :: SumS a -> Bool
$cnull :: forall a. SumS a -> Bool
toList :: SumS a -> [a]
$ctoList :: forall a. SumS a -> [a]
foldl1 :: (a -> a -> a) -> SumS a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SumS a -> a
foldr1 :: (a -> a -> a) -> SumS a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SumS a -> a
foldl' :: (b -> a -> b) -> b -> SumS a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SumS a -> b
foldl :: (b -> a -> b) -> b -> SumS a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SumS a -> b
foldr' :: (a -> b -> b) -> b -> SumS a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SumS a -> b
foldr :: (a -> b -> b) -> b -> SumS a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SumS a -> b
foldMap' :: (a -> m) -> SumS a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SumS a -> m
foldMap :: (a -> m) -> SumS a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SumS a -> m
fold :: SumS m -> m
$cfold :: forall m. Monoid m => SumS m -> m
Foldable, Functor SumS
Foldable SumS
Functor SumS
-> Foldable SumS
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SumS a -> f (SumS b))
-> (forall (f :: * -> *) a.
Applicative f =>
SumS (f a) -> f (SumS a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SumS a -> m (SumS b))
-> (forall (m :: * -> *) a. Monad m => SumS (m a) -> m (SumS a))
-> Traversable SumS
(a -> f b) -> SumS a -> f (SumS 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 => SumS (m a) -> m (SumS a)
forall (f :: * -> *) a. Applicative f => SumS (f a) -> f (SumS a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SumS a -> m (SumS b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SumS a -> f (SumS b)
sequence :: SumS (m a) -> m (SumS a)
$csequence :: forall (m :: * -> *) a. Monad m => SumS (m a) -> m (SumS a)
mapM :: (a -> m b) -> SumS a -> m (SumS b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SumS a -> m (SumS b)
sequenceA :: SumS (f a) -> f (SumS a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SumS (f a) -> f (SumS a)
traverse :: (a -> f b) -> SumS a -> f (SumS b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SumS a -> f (SumS b)
$cp2Traversable :: Foldable SumS
$cp1Traversable :: Functor SumS
Traversable)
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (SumS (SBV a)) where
show :: SumS (SBV a) -> String
show (SumS SBV a
n SBV a
i SBV a
s) = String
"{n = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (SymVal a, Show a) => SBV a -> String
sh SBV a
n String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", i = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (SymVal a, Show a) => SBV a -> String
sh SBV a
i String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", s = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SBV a -> String
forall a. (SymVal a, Show a) => SBV a -> String
sh SBV a
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"}"
where sh :: SBV a -> String
sh SBV a
v = case SBV a -> Maybe a
forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
v of
Maybe a
Nothing -> String
"<symbolic>"
Just a
l -> a -> String
forall a. Show a => a -> String
show a
l
instance SymVal a => Fresh IO (SumS (SBV a)) where
fresh :: QueryT IO (SumS (SBV a))
fresh = SBV a -> SBV a -> SBV a -> SumS (SBV a)
forall a. a -> a -> a -> SumS a
SumS (SBV a -> SBV a -> SBV a -> SumS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SBV a -> SBV a -> SumS (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 -> SumS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SBV a -> SumS (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 -> SumS (SBV a))
-> QueryT IO (SBV a) -> QueryT IO (SumS (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 = SumS 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 [ (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{i :: SInteger
i = SInteger
0, s :: SInteger
s = SInteger
0}
, String -> Invariant S -> Stmt S
forall st. String -> (st -> SBool) -> Stmt st
assert String
"n >= 0" (Invariant S -> Stmt S) -> Invariant S -> Stmt S
forall a b. (a -> b) -> a -> b
$ \SumS{SInteger
n :: SInteger
n :: forall a. SumS a -> a
n} -> SInteger
n SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
, 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 < n"
Invariant S
inv
Maybe (Measure S)
msr
(\SumS{SInteger
i :: SInteger
i :: forall a. SumS a -> a
i, SInteger
n :: SInteger
n :: forall a. SumS a -> a
n} -> SInteger
i SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.< SInteger
n)
(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@SumS{SInteger
i :: SInteger
i :: forall a. SumS a -> a
i} -> S
st{i :: SInteger
i = SInteger
iSInteger -> 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@SumS{SInteger
i :: SInteger
i :: forall a. SumS a -> a
i, SInteger
s :: SInteger
s :: forall a. SumS a -> a
s} -> S
st{s :: SInteger
s = SInteger
sSInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+SInteger
i}
]
]
pre :: S -> SBool
pre :: Invariant S
pre SumS{SInteger
n :: SInteger
n :: forall a. SumS a -> a
n} = SInteger
n SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
post :: S -> SBool
post :: Invariant S
post SumS{SInteger
n :: SInteger
n :: forall a. SumS a -> a
n, SInteger
s :: SInteger
s :: forall a. SumS a -> a
s} = SInteger
s SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== (SInteger
n SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
* (SInteger
nSInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+SInteger
1)) SInteger -> SInteger -> SInteger
forall a. SDivisible a => a -> a -> a
`sDiv` SInteger
2
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
"n" S -> SInteger
forall a. SumS a -> a
n]
imperativeSum :: Invariant S -> Maybe (Measure S) -> Program S
imperativeSum :: Invariant S -> Maybe (Measure S) -> Program S
imperativeSum 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
}
correctness :: Invariant S -> Maybe (Measure S) -> IO (ProofResult (SumS Integer))
correctness :: Invariant S -> Maybe (Measure S) -> IO (ProofResult (SumS Integer))
correctness Invariant S
inv Maybe (Measure S)
msr = WPConfig -> Program S -> IO (ProofResult (SumS 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
imperativeSum Invariant S
inv Maybe (Measure S)
msr)