{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-} -- This is just to avoid warnings because we import fragile new Z3 API stuff -- from Z3.Base -- TODO: Error handling -- | -- Module : Z3.Monad -- Copyright : (c) Iago Abal, 2013 -- (c) David Castro, 2013 -- License : BSD3 -- Maintainer: Iago Abal , -- David Castro -- -- A simple monadic wrapper for 'Z3.Base'. module Z3.Monad ( MonadZ3(..) , Z3 , module Z3.Opts , Logic(..) , evalZ3 , evalZ3With -- * Types , Symbol , AST , Sort , FuncDecl , App , Pattern , Model , FuncInterp , FuncEntry , FuncModel(..) -- ** Satisfiability result , Result(..) -- * Context , contextToString , showContext -- * Symbols , mkIntSymbol , mkStringSymbol -- * Sorts , mkUninterpretedSort , mkBoolSort , mkIntSort , mkRealSort , mkBvSort , mkArraySort , mkTupleSort -- * Constants and Applications , mkFuncDecl , mkApp , mkConst , mkTrue , mkFalse , mkEq , mkNot , mkIte , mkIff , mkImplies , mkXor , mkAnd , mkOr , mkDistinct , mkAdd , mkMul , mkSub , mkUnaryMinus , mkDiv , mkMod , mkRem , mkLt , mkLe , mkGt , mkGe , mkInt2Real , mkReal2Int , mkIsInt -- * Bit-vectors , mkBvnot , mkBvredand , mkBvredor , mkBvand , mkBvor , mkBvxor , mkBvnand , mkBvnor , mkBvxnor , mkBvneg , mkBvadd , mkBvsub , mkBvmul , mkBvudiv , mkBvsdiv , mkBvurem , mkBvsrem , mkBvsmod , mkBvult , mkBvslt , mkBvule , mkBvsle , mkBvuge , mkBvsge , mkBvugt , mkBvsgt , mkConcat , mkExtract , mkSignExt , mkZeroExt , mkRepeat , mkBvshl , mkBvlshr , mkBvashr , mkRotateLeft , mkRotateRight , mkExtRotateLeft , mkExtRotateRight , mkInt2bv , mkBv2int , mkBvnegNoOverflow , mkBvaddNoOverflow , mkBvaddNoUnderflow , mkBvsubNoOverflow , mkBvsubNoUnderflow , mkBvmulNoOverflow , mkBvmulNoUnderflow , mkBvsdivNoOverflow -- * Arrays , mkSelect , mkStore , mkConstArray , mkMap , mkArrayDefault -- * Numerals , mkNumeral , mkInt , mkReal -- * Quantifiers , mkPattern , mkBound , mkForall , mkExists -- * Accessors , getBvSortSize , getBool , getInt , getReal -- * Models , eval , evalT , evalFunc , evalArray , getFuncInterp , isAsArray , getAsArrayFuncDecl , funcInterpGetNumEntries , funcInterpGetEntry , funcInterpGetElse , funcInterpGetArity , funcEntryGetValue , funcEntryGetNumArgs , funcEntryGetArg , modelToString , showModel -- * Constraints , assertCnstr , check , getModel , delModel , withModel , push , pop -- * String Conversion , ASTPrintMode(..) , setASTPrintMode , astToString , patternToString , sortToString , funcDeclToString , benchmarkToSMTLibString ) where import Z3.Opts import Z3.Base ( Symbol , AST , Sort , FuncDecl , App , Pattern , Model , FuncInterp , FuncEntry , FuncModel(..) , Result(..) , Logic(..) , ASTPrintMode(..) ) import qualified Z3.Base as Base import Control.Applicative ( Applicative ) import Control.Monad ( void ) import Control.Monad.Reader ( ReaderT, runReaderT, asks ) import Control.Monad.Trans ( MonadIO, liftIO ) import Data.Traversable ( Traversable ) import qualified Data.Traversable as T --------------------------------------------------------------------- -- The Z3 monad-class class (Monad m, MonadIO m) => MonadZ3 m where getSolver :: m (Maybe Base.Solver) getContext :: m Base.Context ------------------------------------------------- -- Lifting liftScalar :: MonadZ3 z3 => (Base.Context -> IO b) -> z3 b liftScalar f = liftIO . f =<< getContext liftFun1 :: MonadZ3 z3 => (Base.Context -> a -> IO b) -> a -> z3 b liftFun1 f a = getContext >>= \ctx -> liftIO (f ctx a) liftFun2 :: MonadZ3 z3 => (Base.Context -> a -> b -> IO c) -> a -> b -> z3 c liftFun2 f a b = getContext >>= \ctx -> liftIO (f ctx a b) liftFun3 :: MonadZ3 z3 => (Base.Context -> a -> b -> c -> IO d) -> a -> b -> c -> z3 d liftFun3 f a b c = getContext >>= \ctx -> liftIO (f ctx a b c) liftFun4 :: MonadZ3 z3 => (Base.Context -> a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> z3 e liftFun4 f a b c d = getContext >>= \ctx -> liftIO (f ctx a b c d) liftFun6 :: MonadZ3 z3 => (Base.Context -> a1 -> a2 -> a3 -> a4 -> a5 -> a6 -> IO b) -> a1 -> a2 -> a3 -> a4 -> a5 -> a6 -> z3 b liftFun6 f x1 x2 x3 x4 x5 x6 = getContext >>= \ctx -> liftIO (f ctx x1 x2 x3 x4 x5 x6) liftSolver0 :: MonadZ3 z3 => (Base.Context -> Base.Solver -> IO b) -> (Base.Context -> IO b) -> z3 b liftSolver0 f_s f_no_s = do ctx <- getContext liftIO . maybe (f_no_s ctx) (f_s ctx) =<< getSolver liftSolver1 :: MonadZ3 z3 => (Base.Context -> Base.Solver -> a -> IO b) -> (Base.Context -> a -> IO b) -> a -> z3 b liftSolver1 f_s f_no_s a = do ctx <- getContext liftIO . maybe (f_no_s ctx a) (\s -> f_s ctx s a) =<< getSolver ------------------------------------------------- -- A simple Z3 monad. newtype Z3 a = Z3 { _unZ3 :: ReaderT Z3Env IO a } deriving (Functor, Applicative, Monad, MonadIO) data Z3Env = Z3Env { envSolver :: Maybe Base.Solver , envContext :: Base.Context } instance MonadZ3 Z3 where getSolver = Z3 $ asks envSolver getContext = Z3 $ asks envContext -- | Eval a Z3 script. evalZ3With :: Maybe Logic -> Opts -> Z3 a -> IO a evalZ3With mbLogic opts (Z3 s) = Base.withConfig $ \cfg -> do setOpts cfg opts Base.withContext cfg $ \ctx -> do mbSolver <- T.mapM (Base.mkSolverForLogic ctx) mbLogic runReaderT s (Z3Env mbSolver ctx) -- | Eval a Z3 script with default configuration options. evalZ3 :: Z3 a -> IO a evalZ3 = evalZ3With Nothing stdOpts --------------------------------------------------------------------- -- Contexts -- | Convert Z3's logical context into a string. contextToString :: MonadZ3 z3 => z3 String contextToString = liftScalar Base.contextToString -- | Alias for 'contextToString'. showContext :: MonadZ3 z3 => z3 String showContext = contextToString --------------------------------------------------------------------- -- Symbols -- | Create a Z3 symbol using an integer. mkIntSymbol :: (MonadZ3 z3, Integral i) => i -> z3 Symbol mkIntSymbol = liftFun1 Base.mkIntSymbol -- | Create a Z3 symbol using a string. -- -- Reference: mkStringSymbol :: MonadZ3 z3 => String -> z3 Symbol mkStringSymbol = liftFun1 Base.mkStringSymbol --------------------------------------------------------------------- -- Sorts -- | Create a free (uninterpreted) type using the given name (symbol). -- -- Reference: mkUninterpretedSort :: MonadZ3 z3 => Symbol -> z3 Sort mkUninterpretedSort = liftFun1 Base.mkUninterpretedSort -- | Create the /boolean/ type. -- -- Reference: mkBoolSort :: MonadZ3 z3 => z3 Sort mkBoolSort = liftScalar Base.mkBoolSort -- | Create the /integer/ type. -- -- Reference: mkIntSort :: MonadZ3 z3 => z3 Sort mkIntSort = liftScalar Base.mkIntSort -- | Create the /real/ type. -- -- Reference: mkRealSort :: MonadZ3 z3 => z3 Sort mkRealSort = liftScalar Base.mkRealSort -- | Create a bit-vector type of the given size. -- -- This type can also be seen as a machine integer. -- -- Reference: mkBvSort :: MonadZ3 z3 => Int -> z3 Sort mkBvSort = liftFun1 Base.mkBvSort -- | Create an array type -- -- Reference: -- mkArraySort :: MonadZ3 z3 => Sort -> Sort -> z3 Sort mkArraySort = liftFun2 Base.mkArraySort -- | Create a tuple type -- -- Reference: mkTupleSort :: MonadZ3 z3 => Symbol -- ^ Name of the sort -> [(Symbol, Sort)] -- ^ Name and sort of each field -> z3 (Sort, FuncDecl, [FuncDecl]) -- ^ Resulting sort, and function -- declarations for the -- constructor and projections. mkTupleSort = liftFun2 Base.mkTupleSort --------------------------------------------------------------------- -- Constants and Applications -- | A Z3 function mkFuncDecl :: MonadZ3 z3 => Symbol -> [Sort] -> Sort -> z3 FuncDecl mkFuncDecl = liftFun3 Base.mkFuncDecl -- | Create a constant or function application. -- -- Reference: mkApp :: MonadZ3 z3 => FuncDecl -> [AST] -> z3 AST mkApp = liftFun2 Base.mkApp -- | Declare and create a constant. -- -- Reference: mkConst :: MonadZ3 z3 => Symbol -> Sort -> z3 AST mkConst = liftFun2 Base.mkConst -- | Create an AST node representing /true/. -- -- Reference: mkTrue :: MonadZ3 z3 => z3 AST mkTrue = liftScalar Base.mkTrue -- | Create an AST node representing /false/. -- -- Reference: mkFalse :: MonadZ3 z3 => z3 AST mkFalse = liftScalar Base.mkFalse -- | Create an AST node representing /l = r/. -- -- Reference: mkEq :: MonadZ3 z3 => AST -> AST -> z3 AST mkEq = liftFun2 Base.mkEq -- | The distinct construct is used for declaring the arguments pairwise -- distinct. -- -- Reference: mkDistinct :: MonadZ3 z3 => [AST] -> z3 AST mkDistinct = liftFun1 Base.mkDistinct -- | Create an AST node representing /not(a)/. -- -- Reference: mkNot :: MonadZ3 z3 => AST -> z3 AST mkNot = liftFun1 Base.mkNot -- | Create an AST node representing an if-then-else: /ite(t1, t2, t3)/. -- -- Reference: mkIte :: MonadZ3 z3 => AST -> AST -> AST -> z3 AST mkIte = liftFun3 Base.mkIte -- | Create an AST node representing /t1 iff t2/. -- -- Reference: mkIff :: MonadZ3 z3 => AST -> AST -> z3 AST mkIff = liftFun2 Base.mkIff -- | Create an AST node representing /t1 implies t2/. -- -- Reference: mkImplies :: MonadZ3 z3 => AST -> AST -> z3 AST mkImplies = liftFun2 Base.mkImplies -- | Create an AST node representing /t1 xor t2/. -- -- Reference: mkXor :: MonadZ3 z3 => AST -> AST -> z3 AST mkXor = liftFun2 Base.mkXor -- | Create an AST node representing args[0] and ... and args[num_args-1]. -- -- Reference: mkAnd :: MonadZ3 z3 => [AST] -> z3 AST mkAnd = liftFun1 Base.mkAnd -- | Create an AST node representing args[0] or ... or args[num_args-1]. -- -- Reference: mkOr :: MonadZ3 z3 => [AST] -> z3 AST mkOr = liftFun1 Base.mkOr -- | Create an AST node representing args[0] + ... + args[num_args-1]. -- -- Reference: mkAdd :: MonadZ3 z3 => [AST] -> z3 AST mkAdd = liftFun1 Base.mkAdd -- | Create an AST node representing args[0] * ... * args[num_args-1]. -- -- Reference: mkMul :: MonadZ3 z3 => [AST] -> z3 AST mkMul = liftFun1 Base.mkMul -- | Create an AST node representing args[0] - ... - args[num_args - 1]. -- -- Reference: mkSub :: MonadZ3 z3 => [AST] -> z3 AST mkSub = liftFun1 Base.mkSub -- | Create an AST node representing -arg. -- -- Reference: mkUnaryMinus :: MonadZ3 z3 => AST -> z3 AST mkUnaryMinus = liftFun1 Base.mkUnaryMinus -- | Create an AST node representing arg1 div arg2. -- -- Reference: mkDiv :: MonadZ3 z3 => AST -> AST -> z3 AST mkDiv = liftFun2 Base.mkDiv -- | Create an AST node representing arg1 mod arg2. -- -- Reference: mkMod :: MonadZ3 z3 => AST -> AST -> z3 AST mkMod = liftFun2 Base.mkMod -- | Create an AST node representing arg1 rem arg2. -- -- Reference: mkRem :: MonadZ3 z3 => AST -> AST -> z3 AST mkRem = liftFun2 Base.mkRem -- | Create less than. -- -- Reference: mkLt :: MonadZ3 z3 => AST -> AST -> z3 AST mkLt = liftFun2 Base.mkLt -- | Create less than or equal to. -- -- Reference: mkLe :: MonadZ3 z3 => AST -> AST -> z3 AST mkLe = liftFun2 Base.mkLe -- | Create greater than. -- -- Reference: mkGt :: MonadZ3 z3 => AST -> AST -> z3 AST mkGt = liftFun2 Base.mkGt -- | Create greater than or equal to. -- -- Reference: mkGe :: MonadZ3 z3 => AST -> AST -> z3 AST mkGe = liftFun2 Base.mkGe -- | Coerce an integer to a real. -- -- Reference: mkInt2Real :: MonadZ3 z3 => AST -> z3 AST mkInt2Real = liftFun1 Base.mkInt2Real -- | Coerce a real to an integer. -- -- Reference: mkReal2Int :: MonadZ3 z3 => AST -> z3 AST mkReal2Int = liftFun1 Base.mkReal2Int -- | Check if a real number is an integer. -- -- Reference: mkIsInt :: MonadZ3 z3 => AST -> z3 AST mkIsInt = liftFun1 Base.mkIsInt --------------------------------------------------------------------- -- Bit-vectors -- | Bitwise negation. -- -- Reference: mkBvnot :: MonadZ3 z3 => AST -> z3 AST mkBvnot = liftFun1 Base.mkBvnot -- | Take conjunction of bits in vector, return vector of length 1. -- -- Reference: mkBvredand :: MonadZ3 z3 => AST -> z3 AST mkBvredand = liftFun1 Base.mkBvredand -- | Take disjunction of bits in vector, return vector of length 1. -- -- Reference: mkBvredor :: MonadZ3 z3 => AST -> z3 AST mkBvredor = liftFun1 Base.mkBvredor -- | Bitwise and. -- -- Reference: mkBvand :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvand = liftFun2 Base.mkBvand -- | Bitwise or. -- -- Reference: mkBvor :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvor = liftFun2 Base.mkBvor -- | Bitwise exclusive-or. -- -- Reference: mkBvxor :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvxor = liftFun2 Base.mkBvxor -- | Bitwise nand. -- -- Reference: mkBvnand :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvnand = liftFun2 Base.mkBvnand -- | Bitwise nor. -- -- Reference: mkBvnor :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvnor = liftFun2 Base.mkBvnor -- | Bitwise xnor. -- -- Reference: mkBvxnor :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvxnor = liftFun2 Base.mkBvxnor -- | Standard two's complement unary minus. -- -- Reference: AST -> z3 AST mkBvneg = liftFun1 Base.mkBvneg -- | Standard two's complement addition. -- -- Reference: mkBvadd :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvadd = liftFun2 Base.mkBvadd -- | Standard two's complement subtraction. -- -- Reference: mkBvsub :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsub = liftFun2 Base.mkBvsub -- | Standard two's complement multiplication. -- -- Reference: mkBvmul :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvmul = liftFun2 Base.mkBvmul -- | Unsigned division. -- -- Reference: mkBvudiv :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvudiv = liftFun2 Base.mkBvudiv -- | Two's complement signed division. -- -- Reference: mkBvsdiv :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsdiv = liftFun2 Base.mkBvsdiv -- | Unsigned remainder. -- -- Reference: mkBvurem :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvurem = liftFun2 Base.mkBvurem -- | Two's complement signed remainder (sign follows dividend). -- -- Reference: mkBvsrem :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsrem = liftFun2 Base.mkBvsrem -- | Two's complement signed remainder (sign follows divisor). -- -- Reference: mkBvsmod :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsmod = liftFun2 Base.mkBvsmod -- | Unsigned less than. -- -- Reference: mkBvult :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvult = liftFun2 Base.mkBvult -- | Two's complement signed less than. -- -- Reference: mkBvslt :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvslt = liftFun2 Base.mkBvslt -- | Unsigned less than or equal to. -- -- Reference: mkBvule :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvule = liftFun2 Base.mkBvule -- | Two's complement signed less than or equal to. -- -- Reference: mkBvsle :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsle = liftFun2 Base.mkBvsle -- | Unsigned greater than or equal to. -- -- Reference: mkBvuge :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvuge = liftFun2 Base.mkBvuge -- | Two's complement signed greater than or equal to. -- -- Reference: mkBvsge :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsge = liftFun2 Base.mkBvsge -- | Unsigned greater than. -- -- Reference: mkBvugt :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvugt = liftFun2 Base.mkBvugt -- | Two's complement signed greater than. -- -- Reference: mkBvsgt :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsgt = liftFun2 Base.mkBvsgt -- | Concatenate the given bit-vectors. -- -- Reference: mkConcat :: MonadZ3 z3 => AST -> AST -> z3 AST mkConcat = liftFun2 Base.mkConcat -- | Extract the bits high down to low from a bitvector of size m to yield a new -- bitvector of size /n/, where /n = high - low + 1/. -- -- Reference: mkExtract :: MonadZ3 z3 => Int -> Int -> AST -> z3 AST mkExtract = liftFun3 Base.mkExtract -- | Sign-extend of the given bit-vector to the (signed) equivalent bitvector -- of size /m+i/, where /m/ is the size of the given bit-vector. -- -- Reference: mkSignExt :: MonadZ3 z3 => Int -> AST -> z3 AST mkSignExt = liftFun2 Base.mkSignExt -- | Extend the given bit-vector with zeros to the (unsigned) equivalent -- bitvector of size /m+i/, where /m/ is the size of the given bit-vector. -- -- Reference: mkZeroExt :: MonadZ3 z3 => Int -> AST -> z3 AST mkZeroExt = liftFun2 Base.mkZeroExt -- | Repeat the given bit-vector up length /i/. -- -- Reference: mkRepeat :: MonadZ3 z3 => Int -> AST -> z3 AST mkRepeat = liftFun2 Base.mkRepeat -- | Shift left. -- -- Reference: mkBvshl :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvshl = liftFun2 Base.mkBvshl -- | Logical shift right. -- -- Reference: mkBvlshr :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvlshr = liftFun2 Base.mkBvlshr -- | Arithmetic shift right. -- -- Reference: mkBvashr :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvashr = liftFun2 Base.mkBvashr -- | Rotate bits of /t1/ to the left /i/ times. -- -- Reference: mkRotateLeft :: MonadZ3 z3 => Int -> AST -> z3 AST mkRotateLeft = liftFun2 Base.mkRotateLeft -- | Rotate bits of /t1/ to the right /i/ times. -- -- Reference: mkRotateRight :: MonadZ3 z3 => Int -> AST -> z3 AST mkRotateRight = liftFun2 Base.mkRotateRight -- | Rotate bits of /t1/ to the left /t2/ times. -- -- Reference: mkExtRotateLeft :: MonadZ3 z3 => AST -> AST -> z3 AST mkExtRotateLeft = liftFun2 Base.mkExtRotateLeft -- | Rotate bits of /t1/ to the right /t2/ times. -- -- Reference: mkExtRotateRight :: MonadZ3 z3 => AST -> AST -> z3 AST mkExtRotateRight = liftFun2 Base.mkExtRotateRight -- | Create an /n/ bit bit-vector from the integer argument /t1/. -- -- Reference: mkInt2bv :: MonadZ3 z3 => Int -> AST -> z3 AST mkInt2bv = liftFun2 Base.mkInt2bv -- | Create an integer from the bit-vector argument /t1/. If /is_signed/ is false, -- then the bit-vector /t1/ is treated as unsigned. So the result is non-negative -- and in the range [0..2^/N/-1], where /N/ are the number of bits in /t1/. -- If /is_signed/ is true, /t1/ is treated as a signed bit-vector. -- -- Reference: mkBv2int :: MonadZ3 z3 => AST -> Bool -> z3 AST mkBv2int = liftFun2 Base.mkBv2int -- | Create a predicate that checks that the bit-wise addition of /t1/ and /t2/ -- does not overflow. -- -- Reference: mkBvaddNoOverflow :: MonadZ3 z3 => AST -> AST -> Bool -> z3 AST mkBvaddNoOverflow = liftFun3 Base.mkBvaddNoOverflow -- | Create a predicate that checks that the bit-wise signed addition of /t1/ -- and /t2/ does not underflow. -- -- Reference: mkBvaddNoUnderflow :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvaddNoUnderflow = liftFun2 Base.mkBvaddNoUnderflow -- | Create a predicate that checks that the bit-wise signed subtraction of /t1/ -- and /t2/ does not overflow. -- -- Reference: mkBvsubNoOverflow :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsubNoOverflow = liftFun2 Base.mkBvsubNoOverflow -- | Create a predicate that checks that the bit-wise subtraction of /t1/ and -- /t2/ does not underflow. -- -- Reference: mkBvsubNoUnderflow :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsubNoUnderflow = liftFun2 Base.mkBvsubNoUnderflow -- | Create a predicate that checks that the bit-wise signed division of /t1/ -- and /t2/ does not overflow. -- -- Reference: mkBvsdivNoOverflow :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvsdivNoOverflow = liftFun2 Base.mkBvsdivNoOverflow -- | Check that bit-wise negation does not overflow when /t1/ is interpreted as -- a signed bit-vector. -- -- Reference: mkBvnegNoOverflow :: MonadZ3 z3 => AST -> z3 AST mkBvnegNoOverflow = liftFun1 Base.mkBvnegNoOverflow -- | Create a predicate that checks that the bit-wise multiplication of /t1/ and -- /t2/ does not overflow. -- -- Reference: mkBvmulNoOverflow :: MonadZ3 z3 => AST -> AST -> Bool -> z3 AST mkBvmulNoOverflow = liftFun3 Base.mkBvmulNoOverflow -- | Create a predicate that checks that the bit-wise signed multiplication of -- /t1/ and /t2/ does not underflow. -- -- Reference: mkBvmulNoUnderflow :: MonadZ3 z3 => AST -> AST -> z3 AST mkBvmulNoUnderflow = liftFun2 Base.mkBvmulNoUnderflow --------------------------------------------------------------------- -- Arrays -- | Array read. The argument a is the array and i is the index of the array -- that gets read. -- -- Reference: mkSelect :: MonadZ3 z3 => AST -> AST -> z3 AST mkSelect = liftFun2 Base.mkSelect -- | Array update.   -- -- Reference: mkStore :: MonadZ3 z3 => AST -> AST -> AST -> z3 AST mkStore = liftFun3 Base.mkStore -- | Create the constant array. -- -- Reference: mkConstArray :: MonadZ3 z3 => Sort -> AST -> z3 AST mkConstArray = liftFun2 Base.mkConstArray -- | map f on the the argument arrays. -- -- Reference: mkMap :: MonadZ3 z3 => FuncDecl -> Int -> [AST] -> z3 AST mkMap = liftFun3 Base.mkMap -- | Access the array default value. Produces the default range value, for -- arrays that can be represented as finite maps with a default range value. -- -- Reference: mkArrayDefault :: MonadZ3 z3 => AST -> z3 AST mkArrayDefault = liftFun1 Base.mkArrayDefault --------------------------------------------------------------------- -- Numerals -- | Create a numeral of a given sort. -- -- Reference: mkNumeral :: MonadZ3 z3 => String -> Sort -> z3 AST mkNumeral = liftFun2 Base.mkNumeral ------------------------------------------------- -- Numerals / Integers -- | Create a numeral of sort /int/. mkInt :: (MonadZ3 z3, Integral a) => a -> z3 AST mkInt = liftFun1 Base.mkInt ------------------------------------------------- -- Numerals / Reals -- | Create a numeral of sort /real/. mkReal :: (MonadZ3 z3, Real r) => r -> z3 AST mkReal = liftFun1 Base.mkReal --------------------------------------------------------------------- -- Quantifiers mkPattern :: MonadZ3 z3 => [AST] -> z3 Pattern mkPattern = liftFun1 Base.mkPattern mkBound :: MonadZ3 z3 => Int -> Sort -> z3 AST mkBound = liftFun2 Base.mkBound mkForall :: MonadZ3 z3 => [Pattern] -> [Symbol] -> [Sort] -> AST -> z3 AST mkForall = liftFun4 Base.mkForall mkExists :: MonadZ3 z3 => [Pattern] -> [Symbol] -> [Sort] -> AST -> z3 AST mkExists = liftFun4 Base.mkExists --------------------------------------------------------------------- -- Accessors -- | Return the size of the given bit-vector sort. -- -- Reference: getBvSortSize :: MonadZ3 z3 => Sort -> z3 Int getBvSortSize = liftFun1 Base.getBvSortSize -- | Returns @Just True@, @Just False@, or @Nothing@ for /undefined/. -- -- Reference: getBool :: MonadZ3 z3 => AST -> z3 (Maybe Bool) getBool = liftFun1 Base.getBool -- | Return the integer value getInt :: MonadZ3 z3 => AST -> z3 Integer getInt = liftFun1 Base.getInt -- | Return rational value getReal :: MonadZ3 z3 => AST -> z3 Rational getReal = liftFun1 Base.getReal --------------------------------------------------------------------- -- Models -- | Evaluate an AST node in the given model. eval :: MonadZ3 z3 => Model -> AST -> z3 (Maybe AST) eval = liftFun2 Base.eval -- | Evaluate a collection of AST nodes in the given model. evalT :: (MonadZ3 z3,Traversable t) => Model -> t AST -> z3 (Maybe (t AST)) evalT = liftFun2 Base.evalT -- | Get function as a list of argument/value pairs. evalFunc :: MonadZ3 z3 => Model -> FuncDecl -> z3 (Maybe FuncModel) evalFunc = liftFun2 Base.evalFunc -- | Get array as a list of argument/value pairs, if it is -- represented as a function (ie, using as-array). evalArray :: MonadZ3 z3 => Model -> AST -> z3 (Maybe FuncModel) evalArray = liftFun2 Base.evalArray -- | Return the interpretation of the function f in the model m. -- Return NULL, if the model does not assign an interpretation for f. -- That should be interpreted as: the f does not matter. -- -- Reference: getFuncInterp :: MonadZ3 z3 => Model -> FuncDecl -> z3 (Maybe FuncInterp) getFuncInterp = liftFun2 Base.getFuncInterp -- | The (_ as-array f) AST node is a construct for assigning interpretations -- for arrays in Z3. It is the array such that forall indices i we have that -- (select (_ as-array f) i) is equal to (f i). This procedure returns Z3_TRUE -- if the a is an as-array AST node. -- -- Reference: isAsArray :: MonadZ3 z3 => AST -> z3 Bool isAsArray = liftFun1 Base.isAsArray -- | Return the function declaration f associated with a (_ as_array f) node. -- -- Reference: getAsArrayFuncDecl :: MonadZ3 z3 => AST -> z3 FuncDecl getAsArrayFuncDecl = liftFun1 Base.getAsArrayFuncDecl -- | Return the number of entries in the given function interpretation. -- -- Reference: funcInterpGetNumEntries :: MonadZ3 z3 => FuncInterp -> z3 Int funcInterpGetNumEntries = liftFun1 Base.funcInterpGetNumEntries -- | Return a "point" of the given function intepretation. -- It represents the value of f in a particular point. -- -- Reference: funcInterpGetEntry :: MonadZ3 z3 => FuncInterp -> Int -> z3 FuncEntry funcInterpGetEntry = liftFun2 Base.funcInterpGetEntry -- | Return the 'else' value of the given function interpretation. -- -- Reference: funcInterpGetElse :: MonadZ3 z3 => FuncInterp -> z3 AST funcInterpGetElse = liftFun1 Base.funcInterpGetElse -- | Return the arity (number of arguments) of the given function -- interpretation. -- -- Reference: funcInterpGetArity :: MonadZ3 z3 => FuncInterp -> z3 Int funcInterpGetArity = liftFun1 Base.funcInterpGetArity -- | Return the value of this point. -- -- Reference: funcEntryGetValue :: MonadZ3 z3 => FuncEntry -> z3 AST funcEntryGetValue = liftFun1 Base.funcEntryGetValue -- | Return the number of arguments in a Z3_func_entry object. -- -- Reference: funcEntryGetNumArgs :: MonadZ3 z3 => FuncEntry -> z3 Int funcEntryGetNumArgs = liftFun1 Base.funcEntryGetNumArgs -- | Return an argument of a Z3_func_entry object. -- -- Reference: funcEntryGetArg :: MonadZ3 z3 => FuncEntry -> Int -> z3 AST funcEntryGetArg = liftFun2 Base.funcEntryGetArg -- | Convert the given model into a string. modelToString :: MonadZ3 z3 => Model -> z3 String modelToString = liftFun1 Base.modelToString -- | Alias for 'modelToString'. showModel :: MonadZ3 z3 => Model -> z3 String showModel = modelToString --------------------------------------------------------------------- -- Constraints -- | Create a backtracking point. push :: MonadZ3 z3 => z3 () push = liftSolver0 Base.solverPush Base.push -- | Backtrack /n/ backtracking points. pop :: MonadZ3 z3 => Int -> z3 () pop = liftSolver1 Base.solverPop Base.pop -- | Assert a constraing into the logical context. -- -- Reference: assertCnstr :: MonadZ3 z3 => AST -> z3 () assertCnstr = liftSolver1 Base.solverAssertCnstr Base.assertCnstr -- | Get model. -- -- Reference : getModel :: MonadZ3 z3 => z3 (Result, Maybe Model) getModel = liftSolver0 Base.solverCheckAndGetModel Base.getModel -- | Delete a model object. -- -- Reference: delModel :: MonadZ3 z3 => Model -> z3 () delModel = liftFun1 Base.delModel withModel :: (Applicative z3, MonadZ3 z3) => (Base.Model -> z3 a) -> z3 (Result, Maybe a) withModel f = do (r,mb_m) <- getModel mb_e <- T.traverse f mb_m void $ T.traverse delModel mb_m return (r, mb_e) -- | Check whether the given logical context is consistent or not. check :: MonadZ3 z3 => z3 Result check = liftSolver0 Base.solverCheck Base.check --------------------------------------------------------------------- -- String Conversion -- | Set the mode for converting expressions to strings. setASTPrintMode :: MonadZ3 z3 => ASTPrintMode -> z3 () setASTPrintMode = liftFun1 Base.setASTPrintMode -- | Convert an AST to a string. astToString :: MonadZ3 z3 => AST -> z3 String astToString = liftFun1 Base.astToString -- | Convert a pattern to a string. patternToString :: MonadZ3 z3 => Pattern -> z3 String patternToString = liftFun1 Base.patternToString -- | Convert a sort to a string. sortToString :: MonadZ3 z3 => Sort -> z3 String sortToString = liftFun1 Base.sortToString -- | Convert a FuncDecl to a string. funcDeclToString :: MonadZ3 z3 => FuncDecl -> z3 String funcDeclToString = liftFun1 Base.funcDeclToString -- | Convert the given benchmark into SMT-LIB formatted string. -- -- The output format can be configured via 'setASTPrintMode'. benchmarkToSMTLibString :: MonadZ3 z3 => String -- ^ name -> String -- ^ logic -> String -- ^ status -> String -- ^ attributes -> [AST] -- ^ assumptions1 -> AST -- ^ formula -> z3 String benchmarkToSMTLibString = liftFun6 Base.benchmarkToSMTLibString