{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Data.SBV.Tools.Induction (
InductionResult(..), InductionStep(..), induct, inductWith
) where
import Data.SBV
import Data.SBV.Control
import Data.List (intercalate)
import Control.Monad (when)
data InductionStep = Initiation (Maybe String)
| Consecution (Maybe String)
| PartialCorrectness
instance Show InductionStep where
show :: InductionStep -> String
show (Initiation Maybe String
Nothing) = String
"initiation"
show (Initiation (Just String
s)) = String
"initiation for strengthening " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show String
s
show (Consecution Maybe String
Nothing) = String
"consecution"
show (Consecution (Just String
s)) = String
"consecution for strengthening " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show String
s
show InductionStep
PartialCorrectness = String
"partial correctness"
data InductionResult a = Failed InductionStep a
| Proven
instance Show a => Show (InductionResult a) where
show :: InductionResult a -> String
show InductionResult a
Proven = String
"Q.E.D."
show (Failed InductionStep
s a
e) = forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" [ String
"Failed while establishing " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show InductionStep
s forall a. [a] -> [a] -> [a]
++ String
"."
, String
"Counter-example to inductiveness:"
, forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" [String
" " forall a. [a] -> [a] -> [a]
++ String
l | String
l <- String -> [String]
lines (forall a. Show a => a -> String
show a
e)]
]
induct :: (Show res, Queriable IO st res)
=> Bool
-> Symbolic ()
-> (st -> SBool)
-> (st -> [st])
-> [(String, st -> SBool)]
-> (st -> SBool)
-> (st -> (SBool, SBool))
-> IO (InductionResult res)
induct :: forall res st.
(Show res, Queriable IO st res) =>
Bool
-> Symbolic ()
-> (st -> SBool)
-> (st -> [st])
-> [(String, st -> SBool)]
-> (st -> SBool)
-> (st -> (SBool, SBool))
-> IO (InductionResult res)
induct = forall res st.
(Show res, Queriable IO st res) =>
SMTConfig
-> Bool
-> Symbolic ()
-> (st -> SBool)
-> (st -> [st])
-> [(String, st -> SBool)]
-> (st -> SBool)
-> (st -> (SBool, SBool))
-> IO (InductionResult res)
inductWith SMTConfig
defaultSMTCfg
inductWith :: (Show res, Queriable IO st res)
=> SMTConfig
-> Bool
-> Symbolic ()
-> (st -> SBool)
-> (st -> [st])
-> [(String, st -> SBool)]
-> (st -> SBool)
-> (st -> (SBool, SBool))
-> IO (InductionResult res)
inductWith :: forall res st.
(Show res, Queriable IO st res) =>
SMTConfig
-> Bool
-> Symbolic ()
-> (st -> SBool)
-> (st -> [st])
-> [(String, st -> SBool)]
-> (st -> SBool)
-> (st -> (SBool, SBool))
-> IO (InductionResult res)
inductWith SMTConfig
cfg Bool
chatty Symbolic ()
setup st -> SBool
initial st -> [st]
trans [(String, st -> SBool)]
strengthenings st -> SBool
inv st -> (SBool, SBool)
goal =
forall {a} {t} {b}.
(Queriable IO a t, Show t) =>
String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try String
"Proving initiation"
(\st
s -> st -> SBool
initial st
s SBool -> SBool -> SBool
.=> st -> SBool
inv st
s)
(forall a. InductionStep -> a -> InductionResult a
Failed (Maybe String -> InductionStep
Initiation forall a. Maybe a
Nothing))
forall a b. (a -> b) -> a -> b
$ forall {t}.
(Queriable IO st t, Show t) =>
[(String, st -> SBool)]
-> IO (InductionResult t) -> IO (InductionResult t)
strengthen [(String, st -> SBool)]
strengthenings
forall a b. (a -> b) -> a -> b
$ forall {a} {t} {b}.
(Queriable IO a t, Show t) =>
String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try String
"Proving consecution"
(\st
s -> [SBool] -> SBool
sAnd (st -> SBool
inv st
s forall a. a -> [a] -> [a]
: [st -> SBool
st st
s | (String
_, st -> SBool
st) <- [(String, st -> SBool)]
strengthenings]) SBool -> SBool -> SBool
.=> forall a. (a -> SBool) -> [a] -> SBool
sAll st -> SBool
inv (st -> [st]
trans st
s))
(forall a. InductionStep -> a -> InductionResult a
Failed (Maybe String -> InductionStep
Consecution forall a. Maybe a
Nothing))
forall a b. (a -> b) -> a -> b
$ forall {a} {t} {b}.
(Queriable IO a t, Show t) =>
String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try String
"Proving partial correctness"
(\st
s -> let (SBool
term, SBool
result) = st -> (SBool, SBool)
goal st
s in st -> SBool
inv st
s SBool -> SBool -> SBool
.&& SBool
term SBool -> SBool -> SBool
.=> SBool
result)
(forall a. InductionStep -> a -> InductionResult a
Failed InductionStep
PartialCorrectness)
(String -> IO ()
msg String
"Done" forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. InductionResult a
Proven)
where msg :: String -> IO ()
msg = forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
chatty forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ()
putStrLn
try :: String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try String
m a -> SBool
p t -> b
wrap IO b
cont = do String -> IO ()
msg String
m
Maybe t
res <- forall {a} {a}.
(Queriable IO a a, Show a) =>
(a -> SBool) -> IO (Maybe a)
check a -> SBool
p
case Maybe t
res of
Just t
ex -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ t -> b
wrap t
ex
Maybe t
Nothing -> IO b
cont
check :: (a -> SBool) -> IO (Maybe a)
check a -> SBool
p = forall a. SMTConfig -> Symbolic a -> IO a
runSMTWith SMTConfig
cfg forall a b. (a -> b) -> a -> b
$ do
Symbolic ()
setup
forall a. Query a -> Symbolic a
query forall a b. (a -> b) -> a -> b
$ do a
st <- forall (m :: * -> *) a b. Queriable m a b => QueryT m a
create
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SBool -> SBool
sNot (a -> SBool
p a
st)
CheckSatResult
cs <- Query CheckSatResult
checkSat
case CheckSatResult
cs of
CheckSatResult
Unk -> forall a. HasCallStack => String -> a
error String
"Solver said unknown"
DSat{} -> forall a. HasCallStack => String -> a
error String
"Solver returned a delta-sat result"
CheckSatResult
Unsat -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
CheckSatResult
Sat -> do forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
msg String
"Failed:"
a
ex <- forall (m :: * -> *) a b. Queriable m a b => a -> QueryT m b
project a
st
forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
msg forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show a
ex
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just a
ex
strengthen :: [(String, st -> SBool)]
-> IO (InductionResult t) -> IO (InductionResult t)
strengthen [] IO (InductionResult t)
cont = IO (InductionResult t)
cont
strengthen ((String
nm, st -> SBool
st):[(String, st -> SBool)]
sts) IO (InductionResult t)
cont = forall {a} {t} {b}.
(Queriable IO a t, Show t) =>
String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try (String
"Proving strengthening initiation : " forall a. [a] -> [a] -> [a]
++ String
nm)
(\st
s -> st -> SBool
initial st
s SBool -> SBool -> SBool
.=> st -> SBool
st st
s)
(forall a. InductionStep -> a -> InductionResult a
Failed (Maybe String -> InductionStep
Initiation (forall a. a -> Maybe a
Just String
nm)))
forall a b. (a -> b) -> a -> b
$ forall {a} {t} {b}.
(Queriable IO a t, Show t) =>
String -> (a -> SBool) -> (t -> b) -> IO b -> IO b
try (String
"Proving strengthening consecution: " forall a. [a] -> [a] -> [a]
++ String
nm)
(\st
s -> st -> SBool
st st
s SBool -> SBool -> SBool
.=> forall a. (a -> SBool) -> [a] -> SBool
sAll st -> SBool
st (st -> [st]
trans st
s))
(forall a. InductionStep -> a -> InductionResult a
Failed (Maybe String -> InductionStep
Consecution (forall a. a -> Maybe a
Just String
nm)))
([(String, st -> SBool)]
-> IO (InductionResult t) -> IO (InductionResult t)
strengthen [(String, st -> SBool)]
sts IO (InductionResult t)
cont)