{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Queries.AllSat where
import Data.SBV
import Data.SBV.Control
goodSum :: Symbolic [(Integer, Integer)]
goodSum :: Symbolic [(Integer, Integer)]
goodSum = do SInteger
x <- String -> Symbolic SInteger
sInteger String
"x"
SInteger
y <- String -> Symbolic SInteger
sInteger String
"y"
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
x forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
y forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
x forall a. Num a => a -> a -> a
+ SInteger
y forall a. EqSymbolic a => a -> a -> SBool
.== SInteger
10
let next :: Integer -> [(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
next Integer
i [(Integer, Integer)]
sofar = do
forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Iteration: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Integer
i :: Integer)
CheckSatResult
cs <- [SBool] -> Query CheckSatResult
checkSatAssuming [SInteger
x forall a. EqSymbolic a => a -> a -> SBool
.== forall a. SymVal a => a -> SBV a
literal (Integer
iforall a. Num a => a -> a -> a
-Integer
1)]
case CheckSatResult
cs of
CheckSatResult
Unk -> forall a. HasCallStack => String -> a
error String
"Too bad, solver said unknown.."
DSat{} -> forall a. HasCallStack => String -> a
error String
"Unexpected dsat result.."
CheckSatResult
Unsat -> do forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn String
"No other solution!"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse [(Integer, Integer)]
sofar
CheckSatResult
Sat -> do Integer
xv <- forall a. SymVal a => SBV a -> Query a
getValue SInteger
x
Integer
yv <- forall a. SymVal a => SBV a -> Query a
getValue SInteger
y
forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Current solution is: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Integer
xv, Integer
yv)
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
x forall a. EqSymbolic a => a -> a -> SBool
./= forall a. SymVal a => a -> SBV a
literal Integer
xv
SBool -> SBool -> SBool
.|| SInteger
y forall a. EqSymbolic a => a -> a -> SBool
./= forall a. SymVal a => a -> SBV a
literal Integer
yv
Integer -> [(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
next (Integer
iforall a. Num a => a -> a -> a
+Integer
1) ((Integer
xv, Integer
yv) forall a. a -> [a] -> [a]
: [(Integer, Integer)]
sofar)
forall a. Query a -> Symbolic a
query forall a b. (a -> b) -> a -> b
$ do forall a. IO a -> Query a
io forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn String
"Starting the all-sat engine!"
Integer -> [(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
next Integer
1 []
demo :: IO ()
demo :: IO ()
demo = forall a. Show a => a -> IO ()
print forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Symbolic a -> IO a
runSMT Symbolic [(Integer, Integer)]
goodSum