{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.WeakestPreconditions.Fib where
import Data.SBV
import Data.SBV.Control
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
data FibS a = FibS { forall a. FibS a -> a
n :: a
, forall a. FibS a -> a
i :: a
, forall a. FibS a -> a
k :: a
, forall a. FibS a -> a
m :: a
}
deriving (Int -> FibS a -> ShowS
forall a. Show a => Int -> FibS a -> ShowS
forall a. Show a => [FibS a] -> ShowS
forall a. Show a => FibS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FibS a] -> ShowS
$cshowList :: forall a. Show a => [FibS a] -> ShowS
show :: FibS a -> String
$cshow :: forall a. Show a => FibS a -> String
showsPrec :: Int -> FibS a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> FibS a -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (FibS a) x -> FibS a
forall a x. FibS a -> Rep (FibS a) x
$cto :: forall a x. Rep (FibS a) x -> FibS a
$cfrom :: forall a x. FibS a -> Rep (FibS a) x
Generic, forall a.
Mergeable a =>
Bool -> SBool -> FibS a -> FibS a -> FibS a
forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[FibS a] -> FibS a -> SBV b -> FibS 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) =>
[FibS a] -> FibS a -> SBV b -> FibS a
$cselect :: forall a b.
(Mergeable a, Ord b, SymVal b, Num b) =>
[FibS a] -> FibS a -> SBV b -> FibS a
symbolicMerge :: Bool -> SBool -> FibS a -> FibS a -> FibS a
$csymbolicMerge :: forall a.
Mergeable a =>
Bool -> SBool -> FibS a -> FibS a -> FibS a
Mergeable, forall a b. a -> FibS b -> FibS a
forall a b. (a -> b) -> FibS a -> FibS 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 -> FibS b -> FibS a
$c<$ :: forall a b. a -> FibS b -> FibS a
fmap :: forall a b. (a -> b) -> FibS a -> FibS b
$cfmap :: forall a b. (a -> b) -> FibS a -> FibS b
Functor, forall a. Eq a => a -> FibS a -> Bool
forall a. Num a => FibS a -> a
forall a. Ord a => FibS a -> a
forall m. Monoid m => FibS m -> m
forall a. FibS a -> Bool
forall a. FibS a -> Int
forall a. FibS a -> [a]
forall a. (a -> a -> a) -> FibS a -> a
forall m a. Monoid m => (a -> m) -> FibS a -> m
forall b a. (b -> a -> b) -> b -> FibS a -> b
forall a b. (a -> b -> b) -> b -> FibS 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 => FibS a -> a
$cproduct :: forall a. Num a => FibS a -> a
sum :: forall a. Num a => FibS a -> a
$csum :: forall a. Num a => FibS a -> a
minimum :: forall a. Ord a => FibS a -> a
$cminimum :: forall a. Ord a => FibS a -> a
maximum :: forall a. Ord a => FibS a -> a
$cmaximum :: forall a. Ord a => FibS a -> a
elem :: forall a. Eq a => a -> FibS a -> Bool
$celem :: forall a. Eq a => a -> FibS a -> Bool
length :: forall a. FibS a -> Int
$clength :: forall a. FibS a -> Int
null :: forall a. FibS a -> Bool
$cnull :: forall a. FibS a -> Bool
toList :: forall a. FibS a -> [a]
$ctoList :: forall a. FibS a -> [a]
foldl1 :: forall a. (a -> a -> a) -> FibS a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> FibS a -> a
foldr1 :: forall a. (a -> a -> a) -> FibS a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> FibS a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> FibS a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> FibS a -> b
foldl :: forall b a. (b -> a -> b) -> b -> FibS a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> FibS a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> FibS a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> FibS a -> b
foldr :: forall a b. (a -> b -> b) -> b -> FibS a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> FibS a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> FibS a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> FibS a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> FibS a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> FibS a -> m
fold :: forall m. Monoid m => FibS m -> m
$cfold :: forall m. Monoid m => FibS m -> m
Foldable, Functor FibS
Foldable FibS
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 => FibS (m a) -> m (FibS a)
forall (f :: * -> *) a. Applicative f => FibS (f a) -> f (FibS a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FibS a -> m (FibS b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FibS a -> f (FibS b)
sequence :: forall (m :: * -> *) a. Monad m => FibS (m a) -> m (FibS a)
$csequence :: forall (m :: * -> *) a. Monad m => FibS (m a) -> m (FibS a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FibS a -> m (FibS b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> FibS a -> m (FibS b)
sequenceA :: forall (f :: * -> *) a. Applicative f => FibS (f a) -> f (FibS a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => FibS (f a) -> f (FibS a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FibS a -> f (FibS b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> FibS a -> f (FibS b)
Traversable)
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (FibS (SBV a)) where
show :: FibS (SBV a) -> String
show (FibS SBV a
n SBV a
i SBV a
k SBV a
m) = String
"{n = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
n 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
", k = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
k forall a. [a] -> [a] -> [a]
++ String
", m = " forall a. [a] -> [a] -> [a]
++ forall {a}. (Show a, SymVal a) => SBV a -> String
sh SBV a
m 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 (FibS (SBV a)) where
fresh :: QueryT IO (FibS (SBV a))
fresh = forall a. a -> a -> a -> a -> FibS a
FibS 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 F = FibS SInteger
algorithm :: Stmt F
algorithm :: Stmt F
algorithm = forall st. [Stmt st] -> Stmt st
Seq [ forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \F
st -> F
st{i :: SInteger
i = SInteger
0, k :: SInteger
k = SInteger
1, m :: SInteger
m = SInteger
0}
, forall st. String -> (st -> SBool) -> Stmt st
assert String
"n >= 0" forall a b. (a -> b) -> a -> b
$ \FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n} -> SInteger
n forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
, forall st.
String
-> Invariant st
-> Maybe (Measure st)
-> Invariant st
-> Stmt st
-> Stmt st
While String
"i < n"
(\FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n, SInteger
i :: SInteger
i :: forall a. FibS a -> a
i, SInteger
k :: SInteger
k :: forall a. FibS a -> a
k, SInteger
m :: SInteger
m :: forall a. FibS a -> a
m} -> SInteger
i forall a. OrdSymbolic a => a -> a -> SBool
.<= SInteger
n SBool -> SBool -> SBool
.&& SInteger
k forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger
fib (SInteger
iforall a. Num a => a -> a -> a
+SInteger
1) SBool -> SBool -> SBool
.&& SInteger
m forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger
fib SInteger
i)
(forall a. a -> Maybe a
Just (\FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n, SInteger
i :: SInteger
i :: forall a. FibS a -> a
i} -> [SInteger
nforall a. Num a => a -> a -> a
-SInteger
i]))
(\FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n, SInteger
i :: SInteger
i :: forall a. FibS a -> a
i} -> SInteger
i forall a. OrdSymbolic a => a -> a -> SBool
.< SInteger
n)
forall a b. (a -> b) -> a -> b
$ forall st. [Stmt st] -> Stmt st
Seq [ forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: F
st@FibS{SInteger
m :: SInteger
m :: forall a. FibS a -> a
m, SInteger
k :: SInteger
k :: forall a. FibS a -> a
k} -> F
st{m :: SInteger
m = SInteger
k, k :: SInteger
k = SInteger
m forall a. Num a => a -> a -> a
+ SInteger
k}
, forall st. (st -> st) -> Stmt st
Assign forall a b. (a -> b) -> a -> b
$ \st :: F
st@FibS{SInteger
i :: SInteger
i :: forall a. FibS a -> a
i} -> F
st{i :: SInteger
i = SInteger
iforall a. Num a => a -> a -> a
+SInteger
1}
]
]
fib :: SInteger -> SInteger
fib :: SInteger -> SInteger
fib SInteger
x
| forall a. SymVal a => SBV a -> Bool
isSymbolic SInteger
x = forall a. Uninterpreted a => String -> a
uninterpret String
"fib" SInteger
x
| Bool
True = forall {a} {t}. (Mergeable a, EqSymbolic t, Num t, Num a) => t -> a
go SInteger
x
where go :: t -> a
go t
i = forall a. Mergeable a => SBool -> a -> a -> a
ite (t
i forall a. EqSymbolic a => a -> a -> SBool
.== t
0) a
0
forall a b. (a -> b) -> a -> b
$ forall a. Mergeable a => SBool -> a -> a -> a
ite (t
i forall a. EqSymbolic a => a -> a -> SBool
.== t
1) a
1
forall a b. (a -> b) -> a -> b
$ t -> a
go (t
iforall a. Num a => a -> a -> a
-t
1) forall a. Num a => a -> a -> a
+ t -> a
go (t
iforall a. Num a => a -> a -> a
-t
2)
axiomatizeFib :: Symbolic ()
axiomatizeFib :: Symbolic ()
axiomatizeFib = do
SInteger
x <- Symbolic SInteger
sInteger_
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
0 SBool -> SBool -> SBool
.=> SInteger -> SInteger
fib SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
0
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
1 SBool -> SBool -> SBool
.=> SInteger -> SInteger
fib SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
1
forall (m :: * -> *). SolverContext m => String -> [String] -> m ()
addAxiom String
"fib_n" [ String
"(assert (forall ((x Int))"
, String
" (= (fib (+ x 2)) (+ (fib (+ x 1)) (fib x)))))"
]
pre :: F -> SBool
pre :: F -> SBool
pre FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n} = SInteger
n forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
post :: F -> SBool
post :: F -> SBool
post FibS{SInteger
n :: SInteger
n :: forall a. FibS a -> a
n, SInteger
m :: SInteger
m :: forall a. FibS a -> a
m} = SInteger
m forall a. EqSymbolic a => a -> a -> SBool
.== SInteger -> SInteger
fib SInteger
n
noChange :: Stable F
noChange :: Stable F
noChange = [forall a st.
EqSymbolic a =>
String -> (st -> a) -> st -> st -> (String, SBool)
stable String
"n" forall a. FibS a -> a
n]
imperativeFib :: Program F
imperativeFib :: Program F
imperativeFib = Program { setup :: Symbolic ()
setup = Symbolic ()
axiomatizeFib
, precondition :: F -> SBool
precondition = F -> SBool
pre
, program :: Stmt F
program = Stmt F
algorithm
, postcondition :: F -> SBool
postcondition = F -> SBool
post
, stability :: Stable F
stability = Stable F
noChange
}
correctness :: IO (ProofResult (FibS Integer))
correctness :: IO (ProofResult (FibS 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 F
imperativeFib