I want to parse a String
that depicts a propositional formula and then find all models of the propositional formula with a SAT solver.
Now I can parse a propositional formula with the hatt package; see the testParse
function below.
I can also run a SAT solver call with the SBV library; see the testParse
function below.
Question:
How do I, at runtime, generate a value of type Predicate
like myPredicate
within the SBV library that represents the propositional formula I just parsed from a String? I only know how to manually type the forSome_ $ \x y z -> ...
expression, but not how to write a converter function from an Expr
value to a value of type Predicate
.
-- cabal install sbv hatt
import Data.Logic.Propositional
import Data.SBV
-- Random test formula:
-- (x or ~z) and (y or ~z)
-- graphical depiction, see: https://www.wolframalpha.com/input/?i=%28x+or+~z%29+and+%28y+or+~z%29
testParse = parseExpr "test source" "((X | ~Z) & (Y | ~Z))"
myPredicate :: Predicate
myPredicate = forSome_ $ \x y z -> ((x :: SBool) ||| (bnot z)) &&& (y ||| (bnot z))
testSat = do
x <- allSat $ myPredicate
putStrLn $ show x
main = do
putStrLn $ show $ testParse
testSat
{-
Need a function that dynamically creates a Predicate
(as I did with the function (like "\x y z -> ..") for an arbitrary expression of type "Expr" that is parsed from String.
-}
Information that might be helpful:
Here is the link to the BitVectors.Data: http://hackage.haskell.org/package/sbv-3.0/docs/src/Data-SBV-BitVectors-Data.html
Here is example code form Examples.Puzzles.PowerSet:
import Data.SBV
genPowerSet :: [SBool] -> SBool
genPowerSet = bAll isBool
where isBool x = x .== true ||| x .== false
powerSet :: [Word8] -> IO ()
powerSet xs = do putStrLn $ "Finding all subsets of " ++ show xs
res <- allSat $ genPowerSet `fmap` mkExistVars n
Here is the Expr data type (from hatt library):
data Expr = Variable Var
| Negation Expr
| Conjunction Expr Expr
| Disjunction Expr Expr
| Conditional Expr Expr
| Biconditional Expr Expr
deriving Eq
Working With SBV
Working with SBV requires that you follow the types and realize the
Predicate
is just aSymbolic SBool
. After that step it is important that you investigate and discoverSymbolic
is a monad - yay, a monad!Now that you you know you have a monad then anything in the haddock that is
Symbolic
should be trivial to combine to build any SAT you desire. For your problem you just need a simple interpreter over your AST that builds aPredicate
.Code Walk-Through
The code is all included in one continuous form below but I will step through the fun parts. The entry point is
solveExpr
which takes expressions and produces a SAT result:The application of SBV's
allSat
to the predicate is sort of obvious. To build that predicate we need to declare an existentialSBool
for every variable in our expression. For now lets assume we havevs :: [String]
where each string corresponds to one of theVar
from the expression.Notice how programming language fundamentals is sneaking in here. We now need an environment that maps the expressions variable names to the symbolic booleans used by SBV.
Next we interpret the expression to produce our
Predicate
. The interpret function uses the environment and just applies the SBV function that matches the intent of each constructor from hatt'sExpr
type.And that is it! The rest is just boiler-plate.
Complete Code
forSome_
is a member of theProvable
class, so it seems it would suffice to define the instanceProvable Expr
. Almost all functions inSVB
useProvable
so this would allow you to use all of those nativelyExpr
. First, we convert anExpr
to a function which looks up variable values in aVector
. You could also useData.Map.Map
or something like that, but the environment is not changed once created andVector
gives constant time lookup:Provable
essentially defines two functions:forAll_
,forAll
,forSome_
,forSome
. We have to generate all possible maps of variables to values and apply the function to the maps. Choosing how exactly to handle the results will be done by theSymbolic
monad:Where
fresh
is a function which "quantifies" the given variable by associating it with all possible values.If you define one of these functions for each of the four functions you will have quite a lot of very repetitive code. So you can generalize the above as follows:
If it is confusing exactly what is happening, the
Provable
instance may suffice to explain:Then your test case: