{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Queries.GuessNumber where
import Data.SBV
import Data.SBV.Control
guess :: Integer -> Symbolic [Integer]
guess :: Integer -> Symbolic [Integer]
guess Integer
input = do SInteger
g <- String -> Symbolic SInteger
sInteger String
"guess"
let loop :: Integer -> Integer -> [Integer] -> QueryT IO [Integer]
loop Integer
lb Integer
ub [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
"Current bounds: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Integer
lb, Integer
ub)
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
g forall a. OrdSymbolic a => a -> a -> SBool
.>= forall a. SymVal a => a -> SBV a
literal Integer
lb
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain forall a b. (a -> b) -> a -> b
$ SInteger
g forall a. OrdSymbolic a => a -> a -> SBool
.<= forall a. SymVal a => a -> SBV a
literal Integer
ub
CheckSatResult
cs <- Query CheckSatResult
checkSat
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 delta-sat result.."
CheckSatResult
Unsat ->
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines [ String
"There's no solution!"
, String
"Guess sequence: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall a. [a] -> [a]
reverse [Integer]
sofar)
]
CheckSatResult
Sat -> do Integer
gv <- forall a. SymVal a => SBV a -> Query a
getValue SInteger
g
case Integer
gv forall a. Ord a => a -> a -> Ordering
`compare` Integer
input of
Ordering
EQ ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. [a] -> [a]
reverse (Integer
gv forall a. a -> [a] -> [a]
: [Integer]
sofar))
Ordering
LT ->
Integer -> Integer -> [Integer] -> QueryT IO [Integer]
loop ((Integer
lbforall a. Num a => a -> a -> a
+Integer
1) forall a. Ord a => a -> a -> a
`max` (Integer
lb forall a. Num a => a -> a -> a
+ (Integer
input forall a. Num a => a -> a -> a
- Integer
lb) forall a. Integral a => a -> a -> a
`div` Integer
2)) Integer
ub (Integer
gv forall a. a -> [a] -> [a]
: [Integer]
sofar)
Ordering
GT ->
Integer -> Integer -> [Integer] -> QueryT IO [Integer]
loop Integer
lb ((Integer
ubforall a. Num a => a -> a -> a
-Integer
1) forall a. Ord a => a -> a -> a
`min` (Integer
ub forall a. Num a => a -> a -> a
- (Integer
ub forall a. Num a => a -> a -> a
- Integer
input) forall a. Integral a => a -> a -> a
`div` Integer
2)) (Integer
gv forall a. a -> [a] -> [a]
: [Integer]
sofar)
forall a. Query a -> Symbolic a
query forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> [Integer] -> QueryT IO [Integer]
loop Integer
0 Integer
1000 []
play :: IO ()
play :: IO ()
play = do [Integer]
gs <- forall a. Symbolic a -> IO a
runSMT (Integer -> Symbolic [Integer]
guess Integer
42)
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Solved in: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Integer]
gs) forall a. [a] -> [a] -> [a]
++ String
" guesses:"
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
" " forall a. [a] -> [a] -> [a]
++ [String] -> String
unwords (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [Integer]
gs)