{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.WeakestPreconditions.Basics where
import Data.SBV
import Data.SBV.Control
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
data IncS a = IncS { forall a. IncS a -> a
x :: a
, forall a. IncS a -> a
y :: a
}
deriving (Int -> IncS a -> ShowS
forall a. Show a => Int -> IncS a -> ShowS
forall a. Show a => [IncS a] -> ShowS
forall a. Show a => IncS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IncS a] -> ShowS
$cshowList :: forall a. Show a => [IncS a] -> ShowS
show :: IncS a -> String
$cshow :: forall a. Show a => IncS a -> String
showsPrec :: Int -> IncS a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> IncS a -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (IncS a) x -> IncS a
forall a x. IncS a -> Rep (IncS a) x
$cto :: forall a x. Rep (IncS a) x -> IncS a
$cfrom :: forall a x. IncS a -> Rep (IncS a) x
Generic, forall a.
Mergeable a =>
Bool -> SBool -> IncS a -> IncS a -> IncS a
forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[IncS a] -> IncS a -> SBV b -> IncS a
forall a.
(Bool -> SBool -> a -> a -> a)
-> (forall b. (Ord b, SymVal b, Num b) => [a] -> a -> SBV b -> a)
-> Mergeable a
select :: forall b.
(Ord b, SymVal b, Num b) =>
[IncS a] -> IncS a -> SBV b -> IncS a
$cselect :: forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[IncS a] -> IncS a -> SBV b -> IncS a
symbolicMerge :: Bool -> SBool -> IncS a -> IncS a -> IncS a
$csymbolicMerge :: forall a.
Mergeable a =>
Bool -> SBool -> IncS a -> IncS a -> IncS a
Mergeable, forall a b. a -> IncS b -> IncS a
forall a b. (a -> b) -> IncS a -> IncS b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> IncS b -> IncS a
$c<$ :: forall a b. a -> IncS b -> IncS a
fmap :: forall a b. (a -> b) -> IncS a -> IncS b
$cfmap :: forall a b. (a -> b) -> IncS a -> IncS b
Functor, forall a. Eq a => a -> IncS a -> Bool
forall a. Num a => IncS a -> a
forall a. Ord a => IncS a -> a
forall m. Monoid m => IncS m -> m
forall a. IncS a -> Bool
forall a. IncS a -> Int
forall a. IncS a -> [a]
forall a. (a -> a -> a) -> IncS a -> a
forall m a. Monoid m => (a -> m) -> IncS a -> m
forall b a. (b -> a -> b) -> b -> IncS a -> b
forall a b. (a -> b -> b) -> b -> IncS 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 :: forall a. Num a => IncS a -> a
$cproduct :: forall a. Num a => IncS a -> a
sum :: forall a. Num a => IncS a -> a
$csum :: forall a. Num a => IncS a -> a
minimum :: forall a. Ord a => IncS a -> a
$cminimum :: forall a. Ord a => IncS a -> a
maximum :: forall a. Ord a => IncS a -> a
$cmaximum :: forall a. Ord a => IncS a -> a
elem :: forall a. Eq a => a -> IncS a -> Bool
$celem :: forall a. Eq a => a -> IncS a -> Bool
length :: forall a. IncS a -> Int
$clength :: forall a. IncS a -> Int
null :: forall a. IncS a -> Bool
$cnull :: forall a. IncS a -> Bool
toList :: forall a. IncS a -> [a]
$ctoList :: forall a. IncS a -> [a]
foldl1 :: forall a. (a -> a -> a) -> IncS a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> IncS a -> a
foldr1 :: forall a. (a -> a -> a) -> IncS a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> IncS a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> IncS a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> IncS a -> b
foldl :: forall b a. (b -> a -> b) -> b -> IncS a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> IncS a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> IncS a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> IncS a -> b
foldr :: forall a b. (a -> b -> b) -> b -> IncS a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> IncS a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> IncS a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> IncS a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> IncS a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> IncS a -> m
fold :: forall m. Monoid m => IncS m -> m
$cfold :: forall m. Monoid m => IncS m -> m
Foldable, Functor IncS
Foldable IncS
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 => IncS (m a) -> m (IncS a)
forall (f :: * -> *) a. Applicative f => IncS (f a) -> f (IncS a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> IncS a -> m (IncS b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IncS a -> f (IncS b)
sequence :: forall (m :: * -> *) a. Monad m => IncS (m a) -> m (IncS a)
$csequence :: forall (m :: * -> *) a. Monad m => IncS (m a) -> m (IncS a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> IncS a -> m (IncS b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> IncS a -> m (IncS b)
sequenceA :: forall (f :: * -> *) a. Applicative f => IncS (f a) -> f (IncS a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => IncS (f a) -> f (IncS a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IncS a -> f (IncS b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IncS a -> f (IncS b)
Traversable)
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (IncS (SBV a)) where
show :: IncS (SBV a) -> String
show (IncS SBV a
x SBV a
y) = String
"{x = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
x forall a. [a] -> [a] -> [a]
++ String
", y = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
y forall a. [a] -> [a] -> [a]
++ String
"}"
where sh :: SBV a -> String
sh SBV a
v = forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"<symbolic>" forall a. Show a => a -> String
show (forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
v)
instance SymVal a => Fresh IO (IncS (SBV a)) where
fresh :: QueryT IO (IncS (SBV a))
fresh = forall a. a -> a -> IncS a
IncS forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SymVal a => Query (SBV a)
freshVar_ forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. SymVal a => Query (SBV a)
freshVar_
type I = IncS SInteger
algorithm :: Stmt I -> Stmt I -> Stmt I
algorithm :: Stmt I -> Stmt I -> Stmt I
algorithm Stmt I
before Stmt I
after = forall st. [Stmt st] -> Stmt st
Seq [ Stmt I
before
, forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: I
st@IncS{SInteger
x :: SInteger
x :: forall a. IncS a -> a
x} -> I
st{y :: SInteger
y = SInteger
xforall a. Num a => a -> a -> a
+SInteger
1}
, Stmt I
after
]
pre :: I -> SBool
pre :: I -> SBool
pre IncS{SInteger
x :: SInteger
x :: forall a. IncS a -> a
x} = SInteger
x forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
post :: I -> SBool
post :: I -> SBool
post IncS{SInteger
x :: SInteger
x :: forall a. IncS a -> a
x, SInteger
y :: SInteger
y :: forall a. IncS a -> a
y} = SInteger
y forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
xforall a. Num a => a -> a -> a
+SInteger
1
noChange :: Stable I
noChange :: Stable I
noChange = [forall a st.
EqSymbolic a =>
String -> (st -> a) -> st -> st -> (String, SBool)
stable String
"x" forall a. IncS a -> a
x]
imperativeInc :: Stmt I -> Stmt I -> Program I
imperativeInc :: Stmt I -> Stmt I -> Program I
imperativeInc Stmt I
before Stmt I
after = Program { setup :: Symbolic ()
setup = forall (m :: * -> *) a. Monad m => a -> m a
return ()
, precondition :: I -> SBool
precondition = I -> SBool
pre
, program :: Stmt I
program = Stmt I -> Stmt I -> Stmt I
algorithm Stmt I
before Stmt I
after
, postcondition :: I -> SBool
postcondition = I -> SBool
post
, stability :: Stable I
stability = Stable I
noChange
}
correctness :: Stmt I -> Stmt I -> IO (ProofResult (IncS Integer))
correctness :: Stmt I -> Stmt I -> IO (ProofResult (IncS Integer))
correctness Stmt I
before Stmt I
after = 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} (Stmt I -> Stmt I -> Program I
imperativeInc Stmt I
before Stmt I
after)