{-# 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"
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SInteger
x SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SInteger
y SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
SBool -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> SymbolicT IO ()) -> SBool -> SymbolicT IO ()
forall a b. (a -> b) -> a -> b
$ SInteger
x SInteger -> SInteger -> SInteger
forall a. Num a => a -> a -> a
+ SInteger
y SInteger -> SInteger -> SBool
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
IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Iteration: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show (Integer
i :: Integer)
CheckSatResult
cs <- [SBool] -> Query CheckSatResult
checkSatAssuming [SInteger
x SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== Integer -> SInteger
forall a. SymVal a => a -> SBV a
literal (Integer
iInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
1)]
case CheckSatResult
cs of
CheckSatResult
Unk -> String -> QueryT IO [(Integer, Integer)]
forall a. HasCallStack => String -> a
error String
"Too bad, solver said unknown.."
DSat{} -> String -> QueryT IO [(Integer, Integer)]
forall a. HasCallStack => String -> a
error String
"Unexpected dsat result.."
CheckSatResult
Unsat -> do IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn String
"No other solution!"
[(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Integer, Integer)] -> QueryT IO [(Integer, Integer)])
-> [(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
forall a b. (a -> b) -> a -> b
$ [(Integer, Integer)] -> [(Integer, Integer)]
forall a. [a] -> [a]
reverse [(Integer, Integer)]
sofar
CheckSatResult
Sat -> do Integer
xv <- SInteger -> Query Integer
forall a. SymVal a => SBV a -> Query a
getValue SInteger
x
Integer
yv <- SInteger -> Query Integer
forall a. SymVal a => SBV a -> Query a
getValue SInteger
y
IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Current solution is: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Integer, Integer) -> String
forall a. Show a => a -> String
show (Integer
xv, Integer
yv)
SBool -> Query ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> Query ()) -> SBool -> Query ()
forall a b. (a -> b) -> a -> b
$ SInteger
x SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Integer -> SInteger
forall a. SymVal a => a -> SBV a
literal Integer
xv
SBool -> SBool -> SBool
.|| SInteger
y SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
./= Integer -> SInteger
forall a. SymVal a => a -> SBV a
literal Integer
yv
Integer -> [(Integer, Integer)] -> QueryT IO [(Integer, Integer)]
next (Integer
iInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
1) ((Integer
xv, Integer
yv) (Integer, Integer) -> [(Integer, Integer)] -> [(Integer, Integer)]
forall a. a -> [a] -> [a]
: [(Integer, Integer)]
sofar)
QueryT IO [(Integer, Integer)] -> Symbolic [(Integer, Integer)]
forall a. Query a -> Symbolic a
query (QueryT IO [(Integer, Integer)] -> Symbolic [(Integer, Integer)])
-> QueryT IO [(Integer, Integer)] -> Symbolic [(Integer, Integer)]
forall a b. (a -> b) -> a -> b
$ do IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
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 = [(Integer, Integer)] -> IO ()
forall a. Show a => a -> IO ()
print ([(Integer, Integer)] -> IO ()) -> IO [(Integer, Integer)] -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Symbolic [(Integer, Integer)] -> IO [(Integer, Integer)]
forall a. Symbolic a -> IO a
runSMT Symbolic [(Integer, Integer)]
goodSum