{-# OPTIONS_GHC -Wall -Werror #-}
module Documentation.SBV.Examples.Misc.ModelExtract where
import Data.SBV
outside :: [Integer] -> IO SatResult
outside :: [Integer] -> IO SatResult
outside [Integer]
disallow = SymbolicT IO SBool -> IO SatResult
forall a. Provable a => a -> IO SatResult
sat (SymbolicT IO SBool -> IO SatResult)
-> SymbolicT IO SBool -> IO SatResult
forall a b. (a -> b) -> a -> b
$ do SInteger
x <- String -> Symbolic SInteger
sInteger String
"x"
let notEq :: Integer -> m ()
notEq Integer
i = SBool -> m ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> m ()) -> SBool -> m ()
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
i
(Integer -> SymbolicT IO ()) -> [Integer] -> SymbolicT IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Integer -> SymbolicT IO ()
forall (m :: * -> *). SolverContext m => Integer -> m ()
notEq [Integer]
disallow
SBool -> SymbolicT IO SBool
forall (m :: * -> *) a. Monad m => a -> m a
return (SBool -> SymbolicT IO SBool) -> SBool -> SymbolicT IO SBool
forall a b. (a -> b) -> a -> b
$ SInteger
x SInteger -> SInteger -> SBool
forall a. OrdSymbolic a => a -> a -> SBool
.>= SInteger
0
genVals :: IO [Integer]
genVals :: IO [Integer]
genVals = [Integer] -> [Integer] -> IO [Integer]
go [] []
where go :: [Integer] -> [Integer] -> IO [Integer]
go [Integer]
_ [Integer]
model
| [Integer] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Integer]
model Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
10 = [Integer] -> IO [Integer]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer]
model
go [Integer]
disallow [Integer]
model
= do SatResult
res <- [Integer] -> IO SatResult
outside [Integer]
disallow
case String
"x" String -> SatResult -> Maybe Integer
forall a b. (Modelable a, SymVal b) => String -> a -> Maybe b
`getModelValue` SatResult
res of
Just Integer
c -> [Integer] -> [Integer] -> IO [Integer]
go ([Integer
cInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
4 .. Integer
cInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
4] [Integer] -> [Integer] -> [Integer]
forall a. [a] -> [a] -> [a]
++ [Integer]
disallow) (Integer
c Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: [Integer]
model)
Maybe Integer
_ -> [Integer] -> IO [Integer]
forall (m :: * -> *) a. Monad m => a -> m a
return [Integer]
model