-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from bruderj15/quantifiers
Quantifiers
- Loading branch information
Showing
12 changed files
with
214 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Language.Hasmtlib.Example.Quantifier where | ||
|
||
import Prelude hiding (mod, (&&), (||)) | ||
import Language.Hasmtlib | ||
|
||
main :: IO () | ||
main = do | ||
res <- solveWith cvc5Debug $ do | ||
setLogic "BV" | ||
|
||
z <- var @(BvType 8) | ||
|
||
assert $ z === 0 | ||
|
||
assert $ | ||
for_all $ \x -> | ||
exists $ \y -> | ||
x - y === z | ||
|
||
return () | ||
|
||
print res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
|
||
module Language.Hasmtlib.Type.Expr | ||
( SMTType(..) | ||
, SMTVar(..) | ||
, ValueType | ||
, Value(..), extractValue, putValue | ||
, Repr(..), KnownSMTRepr(..), SomeKnownSMTRepr(..) | ||
, Expr | ||
, for_all , exists | ||
, module Language.Hasmtlib.Internal.Expr.Num | ||
) | ||
where | ||
|
||
import Language.Hasmtlib.Internal.Expr | ||
import Language.Hasmtlib.Internal.Expr.Num | ||
|
||
-- | A universal quantification for any specific type | ||
-- If the type cannot be inferred, apply a type-annotation. | ||
-- Nested quantifiers are also supported. | ||
-- | ||
-- Usage: | ||
-- assert $ | ||
-- for_all @IntType $ \x -> | ||
-- x + 0 === x && 0 + x === 0 | ||
-- | ||
-- The lambdas 'x' is all-quantified here. | ||
-- It will only be scoped for the lambdas body. | ||
for_all :: forall t. KnownSMTRepr t => (Expr t -> Expr BoolType) -> Expr BoolType | ||
for_all = ForAll Nothing | ||
|
||
-- | An existential quantification for any specific type | ||
-- If the type cannot be inferred, apply a type-annotation. | ||
-- Nested quantifiers are also supported. | ||
-- | ||
-- Usage: | ||
-- assert $ | ||
-- for_all @(BvType 8) $ \x -> | ||
-- exists $ \y -> | ||
-- x - y === 0 | ||
-- | ||
-- The lambdas 'y' is existentially quantified here. | ||
-- It will only be scoped for the lambdas body. | ||
exists :: forall t. KnownSMTRepr t => (Expr t -> Expr BoolType) -> Expr BoolType | ||
exists = Exists Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module Language.Hasmtlib.Type.MonadSMT where | ||
|
||
import Language.Hasmtlib.Internal.Expr | ||
import Language.Hasmtlib.Type.Option | ||
import Data.Proxy | ||
import Control.Monad | ||
import Control.Monad.State | ||
|
||
class MonadState s m => MonadSMT s m where | ||
-- | Construct a variable. | ||
-- Usage: | ||
-- x :: SMTVar RealType <- smtvar' (Proxy @RealType) | ||
smtvar' :: forall t. KnownSMTRepr t => Proxy t -> m (SMTVar t) | ||
|
||
-- | Construct a variable as expression. | ||
-- Usage: | ||
-- x :: Expr RealType <- var' (Proxy @RealType) | ||
var' :: forall t. KnownSMTRepr t => Proxy t -> m (Expr t) | ||
|
||
-- | Assert a boolean expression. | ||
-- Usage | ||
-- x :: Expr IntType <- var @IntType | ||
-- assert $ x + 5 === 42 | ||
assert :: Expr BoolType -> m () | ||
|
||
-- | Set an SMT-Solver-Option. | ||
setOption :: SMTOption -> m () | ||
|
||
-- | Set the logic for the SMT-Solver to use. | ||
-- Usage: | ||
-- setLogic "QF_LRA" | ||
setLogic :: String -> m () | ||
|
||
-- | Wrapper for @var'@ which hides the Proxy | ||
var :: forall t s m. (KnownSMTRepr t, MonadSMT s m) => m (Expr t) | ||
var = var' (Proxy @t) | ||
{-# INLINE var #-} | ||
|
||
-- | Wrapper for @smtvar'@ which hides the Proxy | ||
smtvar :: forall t s m. (KnownSMTRepr t, MonadSMT s m) => m (SMTVar t) | ||
smtvar = smtvar' (Proxy @t) | ||
{-# INLINE smtvar #-} | ||
|
||
-- | Create a constant. | ||
-- Usage | ||
-- >>> constant True | ||
-- Constant (BoolValue True) | ||
-- | ||
-- >>> let x :: Integer = 10 ; constant x | ||
-- Constant (IntValue 10) | ||
-- | ||
-- >>> constant @IntType 5 | ||
-- Constant (IntValue 5) | ||
-- | ||
-- >>> constant @(BvType 8) 5 | ||
-- Constant (BvValue 0000101) | ||
constant :: KnownSMTRepr t => ValueType t -> Expr t | ||
constant = Constant . putValue | ||
{-# INLINE constant #-} | ||
|
||
-- We need this separate so we get a pure API for quantifiers | ||
-- Ideally we would do that when rendering the expression | ||
-- However renderSMTLib2 is pure but we need a new quantified var which is stateful | ||
-- | Assign quantified variables to all quantified subexpressions of an expression | ||
-- This shall only be used internally. | ||
-- Usually before rendering an assert. | ||
quantify :: MonadSMT s m => Expr t -> m (Expr t) | ||
quantify (Not x) = fmap Not (quantify x) | ||
quantify (And x y) = liftM2 And (quantify x) (quantify y) | ||
quantify (Or x y) = liftM2 Or (quantify x) (quantify y) | ||
quantify (Impl x y) = liftM2 Impl (quantify x) (quantify y) | ||
quantify (Xor x y) = liftM2 Xor (quantify x) (quantify y) | ||
quantify (Ite p t f) = liftM3 Ite (quantify p) (quantify t) (quantify f) | ||
quantify (ForAll _ f) = do | ||
qVar <- smtvar | ||
qBody <- quantify $ f $ Var qVar | ||
return $ ForAll (Just qVar) (const qBody) | ||
quantify (Exists _ f) = do | ||
qVar <- smtvar | ||
qBody <- quantify $ f $ Var qVar | ||
return $ Exists (Just qVar) (const qBody) | ||
quantify expr = return expr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Language.Hasmtlib.Type.Option where | ||
|
||
import Language.Hasmtlib.Internal.Render | ||
import Data.Data (Data) | ||
import Data.ByteString.Builder | ||
|
||
data SMTOption = | ||
PrintSuccess Bool -- | Print \"success\" after each operation | ||
| ProduceModels Bool -- | Produce a satisfying assignment after each successful checkSat | ||
| Incremental Bool -- | Incremental solving | ||
deriving (Show, Eq, Ord, Data) | ||
|
||
instance RenderSMTLib2 SMTOption where | ||
renderSMTLib2 (PrintSuccess b) = renderBinary "set-option" (":print-success" :: Builder) b | ||
renderSMTLib2 (ProduceModels b) = renderBinary "set-option" (":produce-models" :: Builder) b | ||
renderSMTLib2 (Incremental b) = renderBinary "set-option" (":incremental" :: Builder) b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.