{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.WeakestPreconditions.GCD where
import Data.SBV
import Data.SBV.Control
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
import Prelude hiding (gcd)
import qualified Prelude as P (gcd)
data GCDS a = GCDS { forall a. GCDS a -> a
x :: a
, forall a. GCDS a -> a
y :: a
, forall a. GCDS a -> a
i :: a
, forall a. GCDS a -> a
j :: a
}
deriving (Int -> GCDS a -> ShowS
forall a. Show a => Int -> GCDS a -> ShowS
forall a. Show a => [GCDS a] -> ShowS
forall a. Show a => GCDS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GCDS a] -> ShowS
$cshowList :: forall a. Show a => [GCDS a] -> ShowS
show :: GCDS a -> String
$cshow :: forall a. Show a => GCDS a -> String
showsPrec :: Int -> GCDS a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> GCDS a -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (GCDS a) x -> GCDS a
forall a x. GCDS a -> Rep (GCDS a) x
$cto :: forall a x. Rep (GCDS a) x -> GCDS a
$cfrom :: forall a x. GCDS a -> Rep (GCDS a) x
Generic, forall a.
Mergeable a =>
Bool -> SBool -> GCDS a -> GCDS a -> GCDS a
forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[GCDS a] -> GCDS a -> SBV b -> GCDS 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) =>
[GCDS a] -> GCDS a -> SBV b -> GCDS a
$cselect :: forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[GCDS a] -> GCDS a -> SBV b -> GCDS a
symbolicMerge :: Bool -> SBool -> GCDS a -> GCDS a -> GCDS a
$csymbolicMerge :: forall a.
Mergeable a =>
Bool -> SBool -> GCDS a -> GCDS a -> GCDS a
Mergeable, forall a b. a -> GCDS b -> GCDS a
forall a b. (a -> b) -> GCDS a -> GCDS 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 -> GCDS b -> GCDS a
$c<$ :: forall a b. a -> GCDS b -> GCDS a
fmap :: forall a b. (a -> b) -> GCDS a -> GCDS b
$cfmap :: forall a b. (a -> b) -> GCDS a -> GCDS b
Functor, forall a. Eq a => a -> GCDS a -> Bool
forall a. Num a => GCDS a -> a
forall a. Ord a => GCDS a -> a
forall m. Monoid m => GCDS m -> m
forall a. GCDS a -> Bool
forall a. GCDS a -> Int
forall a. GCDS a -> [a]
forall a. (a -> a -> a) -> GCDS a -> a
forall m a. Monoid m => (a -> m) -> GCDS a -> m
forall b a. (b -> a -> b) -> b -> GCDS a -> b
forall a b. (a -> b -> b) -> b -> GCDS 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 => GCDS a -> a
$cproduct :: forall a. Num a => GCDS a -> a
sum :: forall a. Num a => GCDS a -> a
$csum :: forall a. Num a => GCDS a -> a
minimum :: forall a. Ord a => GCDS a -> a
$cminimum :: forall a. Ord a => GCDS a -> a
maximum :: forall a. Ord a => GCDS a -> a
$cmaximum :: forall a. Ord a => GCDS a -> a
elem :: forall a. Eq a => a -> GCDS a -> Bool
$celem :: forall a. Eq a => a -> GCDS a -> Bool
length :: forall a. GCDS a -> Int
$clength :: forall a. GCDS a -> Int
null :: forall a. GCDS a -> Bool
$cnull :: forall a. GCDS a -> Bool
toList :: forall a. GCDS a -> [a]
$ctoList :: forall a. GCDS a -> [a]
foldl1 :: forall a. (a -> a -> a) -> GCDS a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> GCDS a -> a
foldr1 :: forall a. (a -> a -> a) -> GCDS a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> GCDS a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> GCDS a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> GCDS a -> b
foldl :: forall b a. (b -> a -> b) -> b -> GCDS a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> GCDS a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> GCDS a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> GCDS a -> b
foldr :: forall a b. (a -> b -> b) -> b -> GCDS a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> GCDS a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> GCDS a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> GCDS a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> GCDS a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> GCDS a -> m
fold :: forall m. Monoid m => GCDS m -> m
$cfold :: forall m. Monoid m => GCDS m -> m
Foldable, Functor GCDS
Foldable GCDS
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 => GCDS (m a) -> m (GCDS a)
forall (f :: * -> *) a. Applicative f => GCDS (f a) -> f (GCDS a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GCDS a -> m (GCDS b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GCDS a -> f (GCDS b)
sequence :: forall (m :: * -> *) a. Monad m => GCDS (m a) -> m (GCDS a)
$csequence :: forall (m :: * -> *) a. Monad m => GCDS (m a) -> m (GCDS a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GCDS a -> m (GCDS b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> GCDS a -> m (GCDS b)
sequenceA :: forall (f :: * -> *) a. Applicative f => GCDS (f a) -> f (GCDS a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => GCDS (f a) -> f (GCDS a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GCDS a -> f (GCDS b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GCDS a -> f (GCDS b)
Traversable)
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (GCDS (SBV a)) where
show :: GCDS (SBV a) -> String
show (GCDS SBV a
x SBV a
y SBV a
i SBV a
j) = 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
", i = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
i forall a. [a] -> [a] -> [a]
++ String
", j = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
j 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 (GCDS (SBV a)) where
fresh :: QueryT IO (GCDS (SBV a))
fresh = forall a. a -> a -> a -> a -> GCDS a
GCDS 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_ forall (f :: * -> *) a b. Applicative f => 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 G = GCDS SInteger
algorithm :: Stmt G
algorithm :: Stmt G
algorithm = forall st. [Stmt st] -> Stmt st
Seq [ forall st. String -> (st -> SBool) -> Stmt st
assert String
"x > 0, y > 0" forall a b. (a -> b) -> a -> b
$ \GCDS{SInteger
x :: SInteger
x :: forall a. GCDS a -> a
x, SInteger
y :: SInteger
y :: forall a. GCDS a -> a
y} -> SInteger
x forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger
y forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0
, forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: G
st@GCDS{SInteger
x :: SInteger
x :: forall a. GCDS a -> a
x, SInteger
y :: SInteger
y :: forall a. GCDS a -> a
y} -> G
st{i :: SInteger
i = SInteger
x, j :: SInteger
j = SInteger
y}
, forall st.
String
-> Invariant st
-> Maybe (Measure st)
-> Invariant st
-> Stmt st
-> Stmt st
While String
"i != j"
G -> SBool
inv
(forall a. a -> Maybe a
Just forall a. GCDS a -> [a]
msr)
(\GCDS{SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} -> SInteger
i forall a. EqSymbolic a => a -> a -> SBool
./= SInteger
j)
forall a b. (a -> b) -> a -> b
$ forall st. Invariant st -> Stmt st -> Stmt st -> Stmt st
If (\GCDS{SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} -> SInteger
i forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
j)
(forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: G
st@GCDS{SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} -> G
st{i :: SInteger
i = SInteger
i forall a. Num a => a -> a -> a
- SInteger
j})
(forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: G
st@GCDS{SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} -> G
st{j :: SInteger
j = SInteger
j forall a. Num a => a -> a -> a
- SInteger
i})
]
where
inv :: G -> SBool
inv GCDS{SInteger
x :: SInteger
x :: forall a. GCDS a -> a
x, SInteger
y :: SInteger
y :: forall a. GCDS a -> a
y, SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} = SInteger
x forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger
y forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger
i forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger
j forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger -> SInteger -> SInteger
gcd SInteger
x SInteger
y forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger -> SInteger
gcd SInteger
i SInteger
j
msr :: GCDS a -> [a]
msr GCDS{a
i :: a
i :: forall a. GCDS a -> a
i, a
j :: a
j :: forall a. GCDS a -> a
j} = [a
i, a
j]
gcd :: SInteger -> SInteger -> SInteger
gcd :: SInteger -> SInteger -> SInteger
gcd SInteger
x SInteger
y
| Just Integer
i <- forall a. SymVal a => SBV a -> Maybe a
unliteral SInteger
x, Just Integer
j <- forall a. SymVal a => SBV a -> Maybe a
unliteral SInteger
y
= forall a. SymVal a => a -> SBV a
literal (forall a. Integral a => a -> a -> a
P.gcd Integer
i Integer
j)
| Bool
True
= forall a. Uninterpreted a => String -> a
uninterpret String
"gcd" SInteger
x SInteger
y
axiomatizeGCD :: Symbolic ()
axiomatizeGCD :: Symbolic ()
axiomatizeGCD = do
SInteger
x <- Symbolic SInteger
sInteger_
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger -> SInteger -> SInteger
gcd SInteger
x SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
x
forall (m :: * -> *). SolverContext m => String -> [String] -> m ()
addAxiom String
"gcd_equal" [ String
"(assert (forall ((x Int))"
, String
" (=> (> x 0) (= (gcd x x) x))))"
]
forall (m :: * -> *). SolverContext m => String -> [String] -> m ()
addAxiom String
"gcd_unequal1" [ String
"(assert (forall ((x Int) (y Int))"
, String
" (=> (and (> x 0) (> y 0)) (= (gcd (+ x y) y) (gcd x y)))))"
]
forall (m :: * -> *). SolverContext m => String -> [String] -> m ()
addAxiom String
"gcd_unequal2" [ String
"(assert (forall ((x Int) (y Int))"
, String
" (=> (and (> x 0) (> y 0)) (= (gcd x (+ y x)) (gcd x y)))))"
]
pre :: G -> SBool
pre :: G -> SBool
pre GCDS{SInteger
x :: SInteger
x :: forall a. GCDS a -> a
x, SInteger
y :: SInteger
y :: forall a. GCDS a -> a
y} = SInteger
x forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0 SBool -> SBool -> SBool
.&& SInteger
y forall a. OrdSymbolic a => a -> a -> SBool
.> SInteger
0
post :: G -> SBool
post :: G -> SBool
post GCDS{SInteger
x :: SInteger
x :: forall a. GCDS a -> a
x, SInteger
y :: SInteger
y :: forall a. GCDS a -> a
y, SInteger
i :: SInteger
i :: forall a. GCDS a -> a
i, SInteger
j :: SInteger
j :: forall a. GCDS a -> a
j} = SInteger
i forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
j SBool -> SBool -> SBool
.&& SInteger
i forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger -> SInteger
gcd SInteger
x SInteger
y
noChange :: Stable G
noChange :: Stable G
noChange = [forall a st.
EqSymbolic a =>
String -> (st -> a) -> st -> st -> (String, SBool)
stable String
"x" forall a. GCDS a -> a
x, forall a st.
EqSymbolic a =>
String -> (st -> a) -> st -> st -> (String, SBool)
stable String
"y" forall a. GCDS a -> a
y]
imperativeGCD :: Program G
imperativeGCD :: Program G
imperativeGCD = Program { setup :: Symbolic ()
setup = Symbolic ()
axiomatizeGCD
, precondition :: G -> SBool
precondition = G -> SBool
pre
, program :: Stmt G
program = Stmt G
algorithm
, postcondition :: G -> SBool
postcondition = G -> SBool
post
, stability :: Stable G
stability = Stable G
noChange
}
correctness :: IO (ProofResult (GCDS Integer))
correctness :: IO (ProofResult (GCDS Integer))
correctness = 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} Program G
imperativeGCD