{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PatternGuards              #-}

-- |
-- Module    : Z3.Base
-- Copyright : (c) Iago Abal, 2012-2015
--             (c) David Castro, 2012-2015
-- License   : BSD3
-- Maintainer: Iago Abal <mail@iagoabal.eu>,
--             David Castro <david.castro.dcp@gmail.com>
--
-- Low-level bindings to Z3 API.
--
-- There is (mostly) a one-to-one correspondence with Z3 C API, thus see
-- <http://z3prover.github.io/api/html/group__capi.html>
-- for further details.

{- HACKING

Add here the IO-based wrapper for a new Z3 API function:

* Take a look to a few others API functions and follow the same coding style.
    * 2-space wide indentation, no tabs.
    * No trailing spaces, please.
    * ...
* Place the API function in the right section, according to the Z3's API documentation.
* Annotate the API function with a short but concise haddock comment.
    * Look at the Z3 API documentation for inspiration.
* Add the API function to the module export list, (only) if needed.

This should be straightforward for most cases using [MARSHALLING HELPERS].

Reference counting
------------------

When an object is returned by the C API, we:
* increment its reference counter (if applicable),
* wrap the pointer into a ForeignPtr, and
* install a finalizer to decrement the counter.

Objects with explicit /delete/ functions, instead of reference
counters, are handled analogously.In this way, we move memory
management into the GC.

Remarkably, a Z3_context cannot be deleted until we "free" every
object depending on it. But ForeignPtr finalizers do not provide
any ordering guarantee, and it's not possible to establish
dependencies between finalizers. Thus, we /count references/ to
a Z3_context and only delete it when this count reaches /zero/.

-}

module Z3.Base (

  -- * Types
    Config
  , Context
  , Symbol
  , AST
  , Sort
  , FuncDecl
  , App
  , Pattern
  , Constructor
  , Model
  , FuncInterp
  , FuncEntry
  , Params
  , Solver
  , SortKind(..)
  , ASTKind(..)
  , Tactic
  , ApplyResult
  , Goal
  -- ** Satisfiability result
  , Result(..)

  -- * Create configuration
  , mkConfig
  , delConfig
  , setParamValue
  -- ** Helpers
  , withConfig

  -- * Create context
  , mkContext
  , withContext

  -- * Parameters
  , mkParams
  , paramsSetBool
  , paramsSetUInt
  , paramsSetDouble
  , paramsSetSymbol
  , paramsToString

  -- * Symbols
  , mkIntSymbol
  , mkStringSymbol

  -- * Sorts
  , mkUninterpretedSort
  , mkBoolSort
  , mkIntSort
  , mkRealSort
  , mkBvSort
  , mkFiniteDomainSort
  , mkArraySort
  , mkTupleSort
  , mkConstructor
  , mkDatatype
  , mkDatatypes
  , mkSetSort

  -- * Constants and Applications
  , mkFuncDecl
  , mkApp
  , mkConst
  , mkFreshFuncDecl
  , mkFreshConst
  -- ** Helpers
  , mkVar
  , mkBoolVar
  , mkRealVar
  , mkIntVar
  , mkBvVar
  , mkFreshVar
  , mkFreshBoolVar
  , mkFreshRealVar
  , mkFreshIntVar
  , mkFreshBvVar

  -- * Propositional Logic and Equality
  , mkTrue
  , mkFalse
  , mkEq
  , mkNot
  , mkIte
  , mkIff
  , mkImplies
  , mkXor
  , mkAnd
  , mkOr
  , mkDistinct
  -- ** Helpers
  , mkBool

  -- * Arithmetic: Integers and Reals
  , 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

  -- * Sets
  , mkEmptySet
  , mkFullSet
  , mkSetAdd
  , mkSetDel
  , mkSetUnion
  , mkSetIntersect
  , mkSetDifference
  , mkSetComplement
  , mkSetMember
  , mkSetSubset

  -- * Numerals
  , mkNumeral
  , mkReal
  , mkInt
  , mkUnsignedInt
  , mkInt64
  , mkUnsignedInt64
  -- ** Helpers
  , mkIntegral
  , mkRational
  , mkFixed
  , mkRealNum
  , mkInteger
  , mkIntNum
  , mkBitvector
  , mkBvNum

  -- * Quantifiers
  , mkPattern
  , mkBound
  , mkForall
  , mkExists
  , mkForallConst
  , mkExistsConst

  -- * Accessors
  , getSymbolString
  , getSortKind
  , getBvSortSize
  , getDatatypeSortConstructors
  , getDatatypeSortRecognizers
  , getDatatypeSortConstructorAccessors
  , getDeclName
  , getArity
  , getDomain
  , getRange
  , appToAst
  , getAppDecl
  , getAppNumArgs
  , getAppArg
  , getAppArgs
  , getSort
  , getArraySortDomain
  , getArraySortRange
  , getBoolValue
  , getAstKind
  , isApp
  , toApp
  , getNumeralString
  , simplify
  , simplifyEx
  , getIndexValue
  , isQuantifierForall
  , isQuantifierExists
  , getQuantifierWeight
  , getQuantifierNumPatterns
  , getQuantifierPatternAST
  , getQuantifierPatterns
  , getQuantifierNumNoPatterns
  , getQuantifierNoPatternAST
  , getQuantifierNoPatterns
  , getQuantifierNumBound
  , getQuantifierBoundName
  , getQuantifierBoundSort
  , getQuantifierBoundVars
  , getQuantifierBody
  -- ** Helpers
  , getBool
  , getInt
  , getReal
  , getBv

  -- * Modifiers
  , substituteVars

  -- * Models
  , modelEval
  , evalArray
  , getConstInterp
  , getFuncInterp
  , hasInterp
  , numConsts
  , numFuncs
  , getConstDecl
  , getFuncDecl
  , getConsts
  , getFuncs
  , isAsArray
  , addFuncInterp
  , addConstInterp
  , getAsArrayFuncDecl
  , funcInterpGetNumEntries
  , funcInterpGetEntry
  , funcInterpGetElse
  , funcInterpGetArity
  , funcEntryGetValue
  , funcEntryGetNumArgs
  , funcEntryGetArg
  , modelToString
  , showModel
  -- ** Helpers
  , EvalAst
  , eval
  , evalBool
  , evalInt
  , evalReal
  , evalBv
  , mapEval
  , evalT
  , FuncModel (..)
  , evalFunc

  -- * Tactics
  , mkTactic
  , andThenTactic
  , orElseTactic
  , skipTactic
  , tryForTactic
  , mkQuantifierEliminationTactic
  , mkAndInverterGraphTactic
  , applyTactic
  , getApplyResultNumSubgoals
  , getApplyResultSubgoal
  , getApplyResultSubgoals
  , mkGoal
  , goalAssert
  , getGoalSize
  , getGoalFormula
  , getGoalFormulas

  -- * String Conversion
  , ASTPrintMode(..)
  , setASTPrintMode
  , astToString
  , patternToString
  , sortToString
  , funcDeclToString
  , benchmarkToSMTLibString

  -- * Parser interface
  , parseSMTLib2String
  , parseSMTLib2File

  -- * Error Handling
  , Z3Error(..)
  , Z3ErrorCode(..)

  -- * Miscellaneous
  , Version(..)
  , getVersion

  -- * Fixedpoint
  , Fixedpoint (..)
  , mkFixedpoint
  , fixedpointAddRule
  , fixedpointSetParams
  , fixedpointRegisterRelation
  , fixedpointQueryRelations
  , fixedpointGetAnswer
  , fixedpointGetAssertions

  -- * Solvers
  , Logic(..)
  , mkSolver
  , mkSimpleSolver
  , mkSolverForLogic
  , solverGetHelp
  , solverSetParams
  , solverPush
  , solverPop
  , solverReset
  , solverGetNumScopes
  , solverAssertCnstr
  , solverAssertAndTrack
  , solverCheck
  , solverCheckAssumptions
  , solverGetModel
  , solverGetUnsatCore
  , solverGetReasonUnknown
  , solverToString
  -- ** Helpers
  , solverCheckAndGetModel
  ) where

import Z3.Base.C

import Control.Applicative ( (<$>), (<*>), (<*), pure )
import Control.Exception ( Exception, bracket, throw )
import Control.Monad ( join, when, forM )
import Data.Fixed ( Fixed, HasResolution )
import Data.Foldable ( Foldable (..) )
import Data.Int
import Data.IORef ( IORef, newIORef, atomicModifyIORef' )
import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
import Data.Maybe ( fromJust )
import Data.Ratio ( numerator, denominator, (%) )
import Data.Traversable ( Traversable )
import qualified Data.Traversable as T
import Data.Typeable ( Typeable )
import Data.Word
import Foreign hiding ( toBool, newForeignPtr )
import Foreign.C
  ( CDouble, CInt, CUInt, CLLong, CULLong, CString
  , peekCString
  , withCString )
import Foreign.Concurrent

---------------------------------------------------------------------
-- * Types

-- | A Z3 /configuration object/.
newtype Config = Config { Config -> Ptr Z3_config
unConfig :: Ptr Z3_config }
    deriving Config -> Config -> Bool
(Config -> Config -> Bool)
-> (Config -> Config -> Bool) -> Eq Config
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Config -> Config -> Bool
$c/= :: Config -> Config -> Bool
== :: Config -> Config -> Bool
$c== :: Config -> Config -> Bool
Eq

-- | A Z3 /logical context/.
data Context =
    Context {
      Context -> ForeignPtr Z3_context
unContext :: ForeignPtr Z3_context
    , Context -> IORef Word
refCount  :: !(IORef Word)
    }
    deriving Context -> Context -> Bool
(Context -> Context -> Bool)
-> (Context -> Context -> Bool) -> Eq Context
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Context -> Context -> Bool
$c/= :: Context -> Context -> Bool
== :: Context -> Context -> Bool
$c== :: Context -> Context -> Bool
Eq

-- | A Z3 /symbol/.
--
-- Used to name types, constants and functions.
newtype Symbol = Symbol { Symbol -> Ptr Z3_symbol
unSymbol :: Ptr Z3_symbol }
    deriving (Symbol -> Symbol -> Bool
(Symbol -> Symbol -> Bool)
-> (Symbol -> Symbol -> Bool) -> Eq Symbol
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Symbol -> Symbol -> Bool
$c/= :: Symbol -> Symbol -> Bool
== :: Symbol -> Symbol -> Bool
$c== :: Symbol -> Symbol -> Bool
Eq, Eq Symbol
Eq Symbol =>
(Symbol -> Symbol -> Ordering)
-> (Symbol -> Symbol -> Bool)
-> (Symbol -> Symbol -> Bool)
-> (Symbol -> Symbol -> Bool)
-> (Symbol -> Symbol -> Bool)
-> (Symbol -> Symbol -> Symbol)
-> (Symbol -> Symbol -> Symbol)
-> Ord Symbol
Symbol -> Symbol -> Bool
Symbol -> Symbol -> Ordering
Symbol -> Symbol -> Symbol
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Symbol -> Symbol -> Symbol
$cmin :: Symbol -> Symbol -> Symbol
max :: Symbol -> Symbol -> Symbol
$cmax :: Symbol -> Symbol -> Symbol
>= :: Symbol -> Symbol -> Bool
$c>= :: Symbol -> Symbol -> Bool
> :: Symbol -> Symbol -> Bool
$c> :: Symbol -> Symbol -> Bool
<= :: Symbol -> Symbol -> Bool
$c<= :: Symbol -> Symbol -> Bool
< :: Symbol -> Symbol -> Bool
$c< :: Symbol -> Symbol -> Bool
compare :: Symbol -> Symbol -> Ordering
$ccompare :: Symbol -> Symbol -> Ordering
$cp1Ord :: Eq Symbol
Ord, Int -> Symbol -> ShowS
[Symbol] -> ShowS
Symbol -> String
(Int -> Symbol -> ShowS)
-> (Symbol -> String) -> ([Symbol] -> ShowS) -> Show Symbol
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Symbol] -> ShowS
$cshowList :: [Symbol] -> ShowS
show :: Symbol -> String
$cshow :: Symbol -> String
showsPrec :: Int -> Symbol -> ShowS
$cshowsPrec :: Int -> Symbol -> ShowS
Show, Ptr b -> Int -> IO Symbol
Ptr b -> Int -> Symbol -> IO ()
Ptr Symbol -> IO Symbol
Ptr Symbol -> Int -> IO Symbol
Ptr Symbol -> Int -> Symbol -> IO ()
Ptr Symbol -> Symbol -> IO ()
Symbol -> Int
(Symbol -> Int)
-> (Symbol -> Int)
-> (Ptr Symbol -> Int -> IO Symbol)
-> (Ptr Symbol -> Int -> Symbol -> IO ())
-> (forall b. Ptr b -> Int -> IO Symbol)
-> (forall b. Ptr b -> Int -> Symbol -> IO ())
-> (Ptr Symbol -> IO Symbol)
-> (Ptr Symbol -> Symbol -> IO ())
-> Storable Symbol
forall b. Ptr b -> Int -> IO Symbol
forall b. Ptr b -> Int -> Symbol -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
poke :: Ptr Symbol -> Symbol -> IO ()
$cpoke :: Ptr Symbol -> Symbol -> IO ()
peek :: Ptr Symbol -> IO Symbol
$cpeek :: Ptr Symbol -> IO Symbol
pokeByteOff :: Ptr b -> Int -> Symbol -> IO ()
$cpokeByteOff :: forall b. Ptr b -> Int -> Symbol -> IO ()
peekByteOff :: Ptr b -> Int -> IO Symbol
$cpeekByteOff :: forall b. Ptr b -> Int -> IO Symbol
pokeElemOff :: Ptr Symbol -> Int -> Symbol -> IO ()
$cpokeElemOff :: Ptr Symbol -> Int -> Symbol -> IO ()
peekElemOff :: Ptr Symbol -> Int -> IO Symbol
$cpeekElemOff :: Ptr Symbol -> Int -> IO Symbol
alignment :: Symbol -> Int
$calignment :: Symbol -> Int
sizeOf :: Symbol -> Int
$csizeOf :: Symbol -> Int
Storable)

-- | A Z3 /AST node/.
--
-- This is the data-structure used in Z3 to represent terms, formulas and types.
newtype AST = AST { AST -> ForeignPtr Z3_ast
unAST :: ForeignPtr Z3_ast }
    deriving (AST -> AST -> Bool
(AST -> AST -> Bool) -> (AST -> AST -> Bool) -> Eq AST
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AST -> AST -> Bool
$c/= :: AST -> AST -> Bool
== :: AST -> AST -> Bool
$c== :: AST -> AST -> Bool
Eq, Eq AST
Eq AST =>
(AST -> AST -> Ordering)
-> (AST -> AST -> Bool)
-> (AST -> AST -> Bool)
-> (AST -> AST -> Bool)
-> (AST -> AST -> Bool)
-> (AST -> AST -> AST)
-> (AST -> AST -> AST)
-> Ord AST
AST -> AST -> Bool
AST -> AST -> Ordering
AST -> AST -> AST
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AST -> AST -> AST
$cmin :: AST -> AST -> AST
max :: AST -> AST -> AST
$cmax :: AST -> AST -> AST
>= :: AST -> AST -> Bool
$c>= :: AST -> AST -> Bool
> :: AST -> AST -> Bool
$c> :: AST -> AST -> Bool
<= :: AST -> AST -> Bool
$c<= :: AST -> AST -> Bool
< :: AST -> AST -> Bool
$c< :: AST -> AST -> Bool
compare :: AST -> AST -> Ordering
$ccompare :: AST -> AST -> Ordering
$cp1Ord :: Eq AST
Ord, Int -> AST -> ShowS
[AST] -> ShowS
AST -> String
(Int -> AST -> ShowS)
-> (AST -> String) -> ([AST] -> ShowS) -> Show AST
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AST] -> ShowS
$cshowList :: [AST] -> ShowS
show :: AST -> String
$cshow :: AST -> String
showsPrec :: Int -> AST -> ShowS
$cshowsPrec :: Int -> AST -> ShowS
Show, Typeable)

-- | A kind of AST representing /types/.
newtype Sort = Sort { Sort -> ForeignPtr Z3_sort
unSort :: ForeignPtr Z3_sort }
    deriving (Sort -> Sort -> Bool
(Sort -> Sort -> Bool) -> (Sort -> Sort -> Bool) -> Eq Sort
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Sort -> Sort -> Bool
$c/= :: Sort -> Sort -> Bool
== :: Sort -> Sort -> Bool
$c== :: Sort -> Sort -> Bool
Eq, Eq Sort
Eq Sort =>
(Sort -> Sort -> Ordering)
-> (Sort -> Sort -> Bool)
-> (Sort -> Sort -> Bool)
-> (Sort -> Sort -> Bool)
-> (Sort -> Sort -> Bool)
-> (Sort -> Sort -> Sort)
-> (Sort -> Sort -> Sort)
-> Ord Sort
Sort -> Sort -> Bool
Sort -> Sort -> Ordering
Sort -> Sort -> Sort
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Sort -> Sort -> Sort
$cmin :: Sort -> Sort -> Sort
max :: Sort -> Sort -> Sort
$cmax :: Sort -> Sort -> Sort
>= :: Sort -> Sort -> Bool
$c>= :: Sort -> Sort -> Bool
> :: Sort -> Sort -> Bool
$c> :: Sort -> Sort -> Bool
<= :: Sort -> Sort -> Bool
$c<= :: Sort -> Sort -> Bool
< :: Sort -> Sort -> Bool
$c< :: Sort -> Sort -> Bool
compare :: Sort -> Sort -> Ordering
$ccompare :: Sort -> Sort -> Ordering
$cp1Ord :: Eq Sort
Ord, Int -> Sort -> ShowS
[Sort] -> ShowS
Sort -> String
(Int -> Sort -> ShowS)
-> (Sort -> String) -> ([Sort] -> ShowS) -> Show Sort
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Sort] -> ShowS
$cshowList :: [Sort] -> ShowS
show :: Sort -> String
$cshow :: Sort -> String
showsPrec :: Int -> Sort -> ShowS
$cshowsPrec :: Int -> Sort -> ShowS
Show)

-- | A kind of AST representing function symbols.
newtype FuncDecl = FuncDecl { FuncDecl -> ForeignPtr Z3_func_decl
unFuncDecl :: ForeignPtr Z3_func_decl }
    deriving (FuncDecl -> FuncDecl -> Bool
(FuncDecl -> FuncDecl -> Bool)
-> (FuncDecl -> FuncDecl -> Bool) -> Eq FuncDecl
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FuncDecl -> FuncDecl -> Bool
$c/= :: FuncDecl -> FuncDecl -> Bool
== :: FuncDecl -> FuncDecl -> Bool
$c== :: FuncDecl -> FuncDecl -> Bool
Eq, Eq FuncDecl
Eq FuncDecl =>
(FuncDecl -> FuncDecl -> Ordering)
-> (FuncDecl -> FuncDecl -> Bool)
-> (FuncDecl -> FuncDecl -> Bool)
-> (FuncDecl -> FuncDecl -> Bool)
-> (FuncDecl -> FuncDecl -> Bool)
-> (FuncDecl -> FuncDecl -> FuncDecl)
-> (FuncDecl -> FuncDecl -> FuncDecl)
-> Ord FuncDecl
FuncDecl -> FuncDecl -> Bool
FuncDecl -> FuncDecl -> Ordering
FuncDecl -> FuncDecl -> FuncDecl
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FuncDecl -> FuncDecl -> FuncDecl
$cmin :: FuncDecl -> FuncDecl -> FuncDecl
max :: FuncDecl -> FuncDecl -> FuncDecl
$cmax :: FuncDecl -> FuncDecl -> FuncDecl
>= :: FuncDecl -> FuncDecl -> Bool
$c>= :: FuncDecl -> FuncDecl -> Bool
> :: FuncDecl -> FuncDecl -> Bool
$c> :: FuncDecl -> FuncDecl -> Bool
<= :: FuncDecl -> FuncDecl -> Bool
$c<= :: FuncDecl -> FuncDecl -> Bool
< :: FuncDecl -> FuncDecl -> Bool
$c< :: FuncDecl -> FuncDecl -> Bool
compare :: FuncDecl -> FuncDecl -> Ordering
$ccompare :: FuncDecl -> FuncDecl -> Ordering
$cp1Ord :: Eq FuncDecl
Ord, Int -> FuncDecl -> ShowS
[FuncDecl] -> ShowS
FuncDecl -> String
(Int -> FuncDecl -> ShowS)
-> (FuncDecl -> String) -> ([FuncDecl] -> ShowS) -> Show FuncDecl
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FuncDecl] -> ShowS
$cshowList :: [FuncDecl] -> ShowS
show :: FuncDecl -> String
$cshow :: FuncDecl -> String
showsPrec :: Int -> FuncDecl -> ShowS
$cshowsPrec :: Int -> FuncDecl -> ShowS
Show, Typeable)

-- | A kind of AST representing constant and function declarations.
newtype App = App { App -> ForeignPtr Z3_app
unApp :: ForeignPtr Z3_app }
    deriving (App -> App -> Bool
(App -> App -> Bool) -> (App -> App -> Bool) -> Eq App
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: App -> App -> Bool
$c/= :: App -> App -> Bool
== :: App -> App -> Bool
$c== :: App -> App -> Bool
Eq, Eq App
Eq App =>
(App -> App -> Ordering)
-> (App -> App -> Bool)
-> (App -> App -> Bool)
-> (App -> App -> Bool)
-> (App -> App -> Bool)
-> (App -> App -> App)
-> (App -> App -> App)
-> Ord App
App -> App -> Bool
App -> App -> Ordering
App -> App -> App
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: App -> App -> App
$cmin :: App -> App -> App
max :: App -> App -> App
$cmax :: App -> App -> App
>= :: App -> App -> Bool
$c>= :: App -> App -> Bool
> :: App -> App -> Bool
$c> :: App -> App -> Bool
<= :: App -> App -> Bool
$c<= :: App -> App -> Bool
< :: App -> App -> Bool
$c< :: App -> App -> Bool
compare :: App -> App -> Ordering
$ccompare :: App -> App -> Ordering
$cp1Ord :: Eq App
Ord, Int -> App -> ShowS
[App] -> ShowS
App -> String
(Int -> App -> ShowS)
-> (App -> String) -> ([App] -> ShowS) -> Show App
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [App] -> ShowS
$cshowList :: [App] -> ShowS
show :: App -> String
$cshow :: App -> String
showsPrec :: Int -> App -> ShowS
$cshowsPrec :: Int -> App -> ShowS
Show)

-- | A kind of AST representing pattern and multi-patterns to
-- guide quantifier instantiation.
newtype Pattern = Pattern { Pattern -> ForeignPtr Z3_pattern
unPattern :: ForeignPtr Z3_pattern }
    deriving (Pattern -> Pattern -> Bool
(Pattern -> Pattern -> Bool)
-> (Pattern -> Pattern -> Bool) -> Eq Pattern
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Pattern -> Pattern -> Bool
$c/= :: Pattern -> Pattern -> Bool
== :: Pattern -> Pattern -> Bool
$c== :: Pattern -> Pattern -> Bool
Eq, Eq Pattern
Eq Pattern =>
(Pattern -> Pattern -> Ordering)
-> (Pattern -> Pattern -> Bool)
-> (Pattern -> Pattern -> Bool)
-> (Pattern -> Pattern -> Bool)
-> (Pattern -> Pattern -> Bool)
-> (Pattern -> Pattern -> Pattern)
-> (Pattern -> Pattern -> Pattern)
-> Ord Pattern
Pattern -> Pattern -> Bool
Pattern -> Pattern -> Ordering
Pattern -> Pattern -> Pattern
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Pattern -> Pattern -> Pattern
$cmin :: Pattern -> Pattern -> Pattern
max :: Pattern -> Pattern -> Pattern
$cmax :: Pattern -> Pattern -> Pattern
>= :: Pattern -> Pattern -> Bool
$c>= :: Pattern -> Pattern -> Bool
> :: Pattern -> Pattern -> Bool
$c> :: Pattern -> Pattern -> Bool
<= :: Pattern -> Pattern -> Bool
$c<= :: Pattern -> Pattern -> Bool
< :: Pattern -> Pattern -> Bool
$c< :: Pattern -> Pattern -> Bool
compare :: Pattern -> Pattern -> Ordering
$ccompare :: Pattern -> Pattern -> Ordering
$cp1Ord :: Eq Pattern
Ord, Int -> Pattern -> ShowS
[Pattern] -> ShowS
Pattern -> String
(Int -> Pattern -> ShowS)
-> (Pattern -> String) -> ([Pattern] -> ShowS) -> Show Pattern
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Pattern] -> ShowS
$cshowList :: [Pattern] -> ShowS
show :: Pattern -> String
$cshow :: Pattern -> String
showsPrec :: Int -> Pattern -> ShowS
$cshowsPrec :: Int -> Pattern -> ShowS
Show)

-- | A type contructor for a (recursive) datatype.
newtype Constructor = Constructor { Constructor -> ForeignPtr Z3_constructor
unConstructor :: ForeignPtr Z3_constructor }
    deriving (Constructor -> Constructor -> Bool
(Constructor -> Constructor -> Bool)
-> (Constructor -> Constructor -> Bool) -> Eq Constructor
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Constructor -> Constructor -> Bool
$c/= :: Constructor -> Constructor -> Bool
== :: Constructor -> Constructor -> Bool
$c== :: Constructor -> Constructor -> Bool
Eq, Eq Constructor
Eq Constructor =>
(Constructor -> Constructor -> Ordering)
-> (Constructor -> Constructor -> Bool)
-> (Constructor -> Constructor -> Bool)
-> (Constructor -> Constructor -> Bool)
-> (Constructor -> Constructor -> Bool)
-> (Constructor -> Constructor -> Constructor)
-> (Constructor -> Constructor -> Constructor)
-> Ord Constructor
Constructor -> Constructor -> Bool
Constructor -> Constructor -> Ordering
Constructor -> Constructor -> Constructor
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Constructor -> Constructor -> Constructor
$cmin :: Constructor -> Constructor -> Constructor
max :: Constructor -> Constructor -> Constructor
$cmax :: Constructor -> Constructor -> Constructor
>= :: Constructor -> Constructor -> Bool
$c>= :: Constructor -> Constructor -> Bool
> :: Constructor -> Constructor -> Bool
$c> :: Constructor -> Constructor -> Bool
<= :: Constructor -> Constructor -> Bool
$c<= :: Constructor -> Constructor -> Bool
< :: Constructor -> Constructor -> Bool
$c< :: Constructor -> Constructor -> Bool
compare :: Constructor -> Constructor -> Ordering
$ccompare :: Constructor -> Constructor -> Ordering
$cp1Ord :: Eq Constructor
Ord, Int -> Constructor -> ShowS
[Constructor] -> ShowS
Constructor -> String
(Int -> Constructor -> ShowS)
-> (Constructor -> String)
-> ([Constructor] -> ShowS)
-> Show Constructor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Constructor] -> ShowS
$cshowList :: [Constructor] -> ShowS
show :: Constructor -> String
$cshow :: Constructor -> String
showsPrec :: Int -> Constructor -> ShowS
$cshowsPrec :: Int -> Constructor -> ShowS
Show)

-- | A list of type contructors for (recursive) datatypes.
newtype ConstructorList = ConstructorList { ConstructorList -> ForeignPtr Z3_constructor_list
unConstructorList :: ForeignPtr Z3_constructor_list }
    deriving (ConstructorList -> ConstructorList -> Bool
(ConstructorList -> ConstructorList -> Bool)
-> (ConstructorList -> ConstructorList -> Bool)
-> Eq ConstructorList
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConstructorList -> ConstructorList -> Bool
$c/= :: ConstructorList -> ConstructorList -> Bool
== :: ConstructorList -> ConstructorList -> Bool
$c== :: ConstructorList -> ConstructorList -> Bool
Eq, Eq ConstructorList
Eq ConstructorList =>
(ConstructorList -> ConstructorList -> Ordering)
-> (ConstructorList -> ConstructorList -> Bool)
-> (ConstructorList -> ConstructorList -> Bool)
-> (ConstructorList -> ConstructorList -> Bool)
-> (ConstructorList -> ConstructorList -> Bool)
-> (ConstructorList -> ConstructorList -> ConstructorList)
-> (ConstructorList -> ConstructorList -> ConstructorList)
-> Ord ConstructorList
ConstructorList -> ConstructorList -> Bool
ConstructorList -> ConstructorList -> Ordering
ConstructorList -> ConstructorList -> ConstructorList
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ConstructorList -> ConstructorList -> ConstructorList
$cmin :: ConstructorList -> ConstructorList -> ConstructorList
max :: ConstructorList -> ConstructorList -> ConstructorList
$cmax :: ConstructorList -> ConstructorList -> ConstructorList
>= :: ConstructorList -> ConstructorList -> Bool
$c>= :: ConstructorList -> ConstructorList -> Bool
> :: ConstructorList -> ConstructorList -> Bool
$c> :: ConstructorList -> ConstructorList -> Bool
<= :: ConstructorList -> ConstructorList -> Bool
$c<= :: ConstructorList -> ConstructorList -> Bool
< :: ConstructorList -> ConstructorList -> Bool
$c< :: ConstructorList -> ConstructorList -> Bool
compare :: ConstructorList -> ConstructorList -> Ordering
$ccompare :: ConstructorList -> ConstructorList -> Ordering
$cp1Ord :: Eq ConstructorList
Ord, Int -> ConstructorList -> ShowS
[ConstructorList] -> ShowS
ConstructorList -> String
(Int -> ConstructorList -> ShowS)
-> (ConstructorList -> String)
-> ([ConstructorList] -> ShowS)
-> Show ConstructorList
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConstructorList] -> ShowS
$cshowList :: [ConstructorList] -> ShowS
show :: ConstructorList -> String
$cshow :: ConstructorList -> String
showsPrec :: Int -> ConstructorList -> ShowS
$cshowsPrec :: Int -> ConstructorList -> ShowS
Show)

-- | A model for the constraints asserted into the logical context.
newtype Model = Model { Model -> ForeignPtr Z3_model
unModel :: ForeignPtr Z3_model }
    deriving Model -> Model -> Bool
(Model -> Model -> Bool) -> (Model -> Model -> Bool) -> Eq Model
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Model -> Model -> Bool
$c/= :: Model -> Model -> Bool
== :: Model -> Model -> Bool
$c== :: Model -> Model -> Bool
Eq

-- | An interpretation of a function in a model.
newtype FuncInterp = FuncInterp { FuncInterp -> ForeignPtr Z3_func_interp
unFuncInterp :: ForeignPtr Z3_func_interp }
    deriving FuncInterp -> FuncInterp -> Bool
(FuncInterp -> FuncInterp -> Bool)
-> (FuncInterp -> FuncInterp -> Bool) -> Eq FuncInterp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FuncInterp -> FuncInterp -> Bool
$c/= :: FuncInterp -> FuncInterp -> Bool
== :: FuncInterp -> FuncInterp -> Bool
$c== :: FuncInterp -> FuncInterp -> Bool
Eq

-- | Representation of the value of a 'Z3_func_interp' at a particular point.
newtype FuncEntry = FuncEntry { FuncEntry -> ForeignPtr Z3_func_entry
unFuncEntry :: ForeignPtr Z3_func_entry }
    deriving FuncEntry -> FuncEntry -> Bool
(FuncEntry -> FuncEntry -> Bool)
-> (FuncEntry -> FuncEntry -> Bool) -> Eq FuncEntry
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FuncEntry -> FuncEntry -> Bool
$c/= :: FuncEntry -> FuncEntry -> Bool
== :: FuncEntry -> FuncEntry -> Bool
$c== :: FuncEntry -> FuncEntry -> Bool
Eq

-- | A tactic
newtype Tactic = Tactic { Tactic -> ForeignPtr Z3_tactic
unTactic :: ForeignPtr Z3_tactic }
    deriving Tactic -> Tactic -> Bool
(Tactic -> Tactic -> Bool)
-> (Tactic -> Tactic -> Bool) -> Eq Tactic
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tactic -> Tactic -> Bool
$c/= :: Tactic -> Tactic -> Bool
== :: Tactic -> Tactic -> Bool
$c== :: Tactic -> Tactic -> Bool
Eq

-- | A goal (aka problem)
newtype Goal = Goal { Goal -> ForeignPtr Z3_goal
unGoal :: ForeignPtr Z3_goal }
    deriving Goal -> Goal -> Bool
(Goal -> Goal -> Bool) -> (Goal -> Goal -> Bool) -> Eq Goal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Goal -> Goal -> Bool
$c/= :: Goal -> Goal -> Bool
== :: Goal -> Goal -> Bool
$c== :: Goal -> Goal -> Bool
Eq

-- | A result of applying a tactic
newtype ApplyResult = ApplyResult { ApplyResult -> ForeignPtr Z3_apply_result
unApplyResult :: ForeignPtr Z3_apply_result }
    deriving ApplyResult -> ApplyResult -> Bool
(ApplyResult -> ApplyResult -> Bool)
-> (ApplyResult -> ApplyResult -> Bool) -> Eq ApplyResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ApplyResult -> ApplyResult -> Bool
$c/= :: ApplyResult -> ApplyResult -> Bool
== :: ApplyResult -> ApplyResult -> Bool
$c== :: ApplyResult -> ApplyResult -> Bool
Eq

-- | A Z3 parameter set.
--
-- Starting at Z3 4.0, parameter sets are used to configure many components
-- such as: simplifiers, tactics, solvers, etc.
newtype Params = Params { Params -> ForeignPtr Z3_params
unParams :: ForeignPtr Z3_params }
    deriving Params -> Params -> Bool
(Params -> Params -> Bool)
-> (Params -> Params -> Bool) -> Eq Params
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Params -> Params -> Bool
$c/= :: Params -> Params -> Bool
== :: Params -> Params -> Bool
$c== :: Params -> Params -> Bool
Eq

-- | A Z3 solver engine.
--
-- A(n) (incremental) solver, possibly specialized by a particular tactic
-- or logic.
newtype Solver = Solver { Solver -> ForeignPtr Z3_solver
unSolver :: ForeignPtr Z3_solver }
    deriving Solver -> Solver -> Bool
(Solver -> Solver -> Bool)
-> (Solver -> Solver -> Bool) -> Eq Solver
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Solver -> Solver -> Bool
$c/= :: Solver -> Solver -> Bool
== :: Solver -> Solver -> Bool
$c== :: Solver -> Solver -> Bool
Eq

-- | Result of a satisfiability check.
--
-- This corresponds to the /z3_lbool/ type in the C API.
data Result
    = Sat
    | Unsat
    | Undef
    deriving (Result -> Result -> Bool
(Result -> Result -> Bool)
-> (Result -> Result -> Bool) -> Eq Result
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Result -> Result -> Bool
$c/= :: Result -> Result -> Bool
== :: Result -> Result -> Bool
$c== :: Result -> Result -> Bool
Eq, Eq Result
Eq Result =>
(Result -> Result -> Ordering)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Bool)
-> (Result -> Result -> Result)
-> (Result -> Result -> Result)
-> Ord Result
Result -> Result -> Bool
Result -> Result -> Ordering
Result -> Result -> Result
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Result -> Result -> Result
$cmin :: Result -> Result -> Result
max :: Result -> Result -> Result
$cmax :: Result -> Result -> Result
>= :: Result -> Result -> Bool
$c>= :: Result -> Result -> Bool
> :: Result -> Result -> Bool
$c> :: Result -> Result -> Bool
<= :: Result -> Result -> Bool
$c<= :: Result -> Result -> Bool
< :: Result -> Result -> Bool
$c< :: Result -> Result -> Bool
compare :: Result -> Result -> Ordering
$ccompare :: Result -> Result -> Ordering
$cp1Ord :: Eq Result
Ord, ReadPrec [Result]
ReadPrec Result
Int -> ReadS Result
ReadS [Result]
(Int -> ReadS Result)
-> ReadS [Result]
-> ReadPrec Result
-> ReadPrec [Result]
-> Read Result
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Result]
$creadListPrec :: ReadPrec [Result]
readPrec :: ReadPrec Result
$creadPrec :: ReadPrec Result
readList :: ReadS [Result]
$creadList :: ReadS [Result]
readsPrec :: Int -> ReadS Result
$creadsPrec :: Int -> ReadS Result
Read, Int -> Result -> ShowS
[Result] -> ShowS
Result -> String
(Int -> Result -> ShowS)
-> (Result -> String) -> ([Result] -> ShowS) -> Show Result
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Result] -> ShowS
$cshowList :: [Result] -> ShowS
show :: Result -> String
$cshow :: Result -> String
showsPrec :: Int -> Result -> ShowS
$cshowsPrec :: Int -> Result -> ShowS
Show)

-- | Different kinds of Z3 types.
data SortKind
    = Z3_UNINTERPRETED_SORT
    | Z3_BOOL_SORT
    | Z3_INT_SORT
    | Z3_REAL_SORT
    | Z3_BV_SORT
    | Z3_ARRAY_SORT
    | Z3_DATATYPE_SORT
    | Z3_RELATION_SORT
    | Z3_FINITE_DOMAIN_SORT
    | Z3_FLOATING_POINT_SORT
    | Z3_ROUNDING_MODE_SORT
    | Z3_UNKNOWN_SORT
    deriving (SortKind -> SortKind -> Bool
(SortKind -> SortKind -> Bool)
-> (SortKind -> SortKind -> Bool) -> Eq SortKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SortKind -> SortKind -> Bool
$c/= :: SortKind -> SortKind -> Bool
== :: SortKind -> SortKind -> Bool
$c== :: SortKind -> SortKind -> Bool
Eq, Int -> SortKind -> ShowS
[SortKind] -> ShowS
SortKind -> String
(Int -> SortKind -> ShowS)
-> (SortKind -> String) -> ([SortKind] -> ShowS) -> Show SortKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SortKind] -> ShowS
$cshowList :: [SortKind] -> ShowS
show :: SortKind -> String
$cshow :: SortKind -> String
showsPrec :: Int -> SortKind -> ShowS
$cshowsPrec :: Int -> SortKind -> ShowS
Show)

-- | Different kinds of Z3 AST nodes.
data ASTKind
    = Z3_NUMERAL_AST
    | Z3_APP_AST
    | Z3_VAR_AST
    | Z3_QUANTIFIER_AST
    | Z3_SORT_AST
    | Z3_FUNC_DECL_AST
    | Z3_UNKNOWN_AST
    deriving (ASTKind -> ASTKind -> Bool
(ASTKind -> ASTKind -> Bool)
-> (ASTKind -> ASTKind -> Bool) -> Eq ASTKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ASTKind -> ASTKind -> Bool
$c/= :: ASTKind -> ASTKind -> Bool
== :: ASTKind -> ASTKind -> Bool
$c== :: ASTKind -> ASTKind -> Bool
Eq, Int -> ASTKind -> ShowS
[ASTKind] -> ShowS
ASTKind -> String
(Int -> ASTKind -> ShowS)
-> (ASTKind -> String) -> ([ASTKind] -> ShowS) -> Show ASTKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ASTKind] -> ShowS
$cshowList :: [ASTKind] -> ShowS
show :: ASTKind -> String
$cshow :: ASTKind -> String
showsPrec :: Int -> ASTKind -> ShowS
$cshowsPrec :: Int -> ASTKind -> ShowS
Show)

---------------------------------------------------------------------
-- * Configuration

-- TODO: Z3_global_param_set
-- TODO: Z3_global_param_reset_all
-- TODO: Z3_global_param_get

---------------------------------------------------------------------
-- * Create configuration

-- | Create a configuration.
--
-- See 'withConfig'.
mkConfig :: IO Config
mkConfig :: IO Config
mkConfig = Ptr Z3_config -> Config
Config (Ptr Z3_config -> Config) -> IO (Ptr Z3_config) -> IO Config
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Ptr Z3_config)
z3_mk_config

-- | Delete a configuration.
--
-- See 'withConfig'.
delConfig :: Config -> IO ()
delConfig :: Config -> IO ()
delConfig = Ptr Z3_config -> IO ()
z3_del_config (Ptr Z3_config -> IO ())
-> (Config -> Ptr Z3_config) -> Config -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config -> Ptr Z3_config
unConfig

-- | Set a configuration parameter.
setParamValue :: Config -> String -> String -> IO ()
setParamValue :: Config -> String -> String -> IO ()
setParamValue cfg :: Config
cfg s1 :: String
s1 s2 :: String
s2 =
  String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
s1  ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \cs1 :: CString
cs1 ->
    String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
s2  ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \cs2 :: CString
cs2 ->
      Ptr Z3_config -> CString -> CString -> IO ()
z3_set_param_value (Config -> Ptr Z3_config
unConfig Config
cfg) CString
cs1 CString
cs2

-------------------------------------------------
-- ** Helpers

-- | Run a computation using a temporally created configuration.
--
-- Note that the configuration object can be thrown away once
-- it has been used to create the Z3 'Context'.
withConfig :: (Config -> IO a) -> IO a
withConfig :: (Config -> IO a) -> IO a
withConfig = IO Config -> (Config -> IO ()) -> (Config -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Config
mkConfig Config -> IO ()
delConfig

---------------------------------------------------------------------
-- Create context

mkContextWith :: (Ptr Z3_config -> IO (Ptr Z3_context)) -> Config -> IO Context
mkContextWith :: (Ptr Z3_config -> IO (Ptr Z3_context)) -> Config -> IO Context
mkContextWith mkCtx :: Ptr Z3_config -> IO (Ptr Z3_context)
mkCtx cfg :: Config
cfg = do
  Ptr Z3_context
ctxPtr <- Ptr Z3_config -> IO (Ptr Z3_context)
mkCtx (Config -> Ptr Z3_config
unConfig Config
cfg)
  Ptr Z3_context -> FunPtr Z3_error_handler -> IO ()
z3_set_error_handler Ptr Z3_context
ctxPtr FunPtr Z3_error_handler
forall a. FunPtr a
nullFunPtr
  IORef Word
count <- Word -> IO (IORef Word)
forall a. a -> IO (IORef a)
newIORef 1
  ForeignPtr Z3_context -> IORef Word -> Context
Context (ForeignPtr Z3_context -> IORef Word -> Context)
-> IO (ForeignPtr Z3_context) -> IO (IORef Word -> Context)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Z3_context -> IO () -> IO (ForeignPtr Z3_context)
forall a. Ptr a -> IO () -> IO (ForeignPtr a)
newForeignPtr Ptr Z3_context
ctxPtr (Ptr Z3_context -> IORef Word -> IO ()
contextDecRef Ptr Z3_context
ctxPtr IORef Word
count)
          IO (IORef Word -> Context) -> IO (IORef Word) -> IO Context
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IORef Word -> IO (IORef Word)
forall (f :: * -> *) a. Applicative f => a -> f a
pure IORef Word
count

-- | Create a context using the given configuration.
--
-- /Z3_del_context/ is called by Haskell's garbage collector before
-- freeing the 'Context' object.
mkContext :: Config -> IO Context
mkContext :: Config -> IO Context
mkContext = (Ptr Z3_config -> IO (Ptr Z3_context)) -> Config -> IO Context
mkContextWith Ptr Z3_config -> IO (Ptr Z3_context)
z3_mk_context_rc

-- TODO: Z3_update_param_value
-- TODO: Z3_interrupt

-------------------------------------------------
-- Reference counting of Context

contextIncRef :: Context -> IO ()
contextIncRef :: Context -> IO ()
contextIncRef ctx :: Context
ctx = IORef Word -> (Word -> (Word, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' (Context -> IORef Word
refCount Context
ctx) ((Word -> (Word, ())) -> IO ()) -> (Word -> (Word, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \n :: Word
n ->
  (Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
+1, ())

contextDecRef :: Ptr Z3_context -> IORef Word -> IO ()
contextDecRef :: Ptr Z3_context -> IORef Word -> IO ()
contextDecRef ctxPtr :: Ptr Z3_context
ctxPtr count :: IORef Word
count = IO (IO ()) -> IO ()
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (IO (IO ()) -> IO ()) -> IO (IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ IORef Word -> (Word -> (Word, IO ())) -> IO (IO ())
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef Word
count ((Word -> (Word, IO ())) -> IO (IO ()))
-> (Word -> (Word, IO ())) -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ \n :: Word
n ->
  if Word
n Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
> 1
    then (Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-1, () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
    else (  0, Ptr Z3_context -> IO ()
z3_del_context Ptr Z3_context
ctxPtr)

---------------------------------------------------------------------
-- Parameters

-- | Create a Z3 (empty) parameter set.
--
-- Starting at Z3 4.0, parameter sets are used to configure many components
-- such as: simplifiers, tactics, solvers, etc.
mkParams :: Context -> IO Params
mkParams :: Context -> IO Params
mkParams = (Ptr Z3_context -> IO (Ptr Z3_params)) -> Context -> IO Params
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_params)
z3_mk_params

-- | Add a Boolean parameter /k/ with value /v/ to the parameter set /p/.
paramsSetBool :: Context -> Params -> Symbol -> Bool -> IO ()
paramsSetBool :: Context -> Params -> Symbol -> Bool -> IO ()
paramsSetBool = (Ptr Z3_context
 -> Ptr Z3_params -> Ptr Z3_symbol -> Z3_bool -> IO ())
-> Context -> Params -> Symbol -> Bool -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_params -> Ptr Z3_symbol -> Z3_bool -> IO ()
z3_params_set_bool

-- | Add a unsigned parameter /k/ with value /v/ to the parameter set /p/.
paramsSetUInt :: Context -> Params -> Symbol -> Word -> IO ()
paramsSetUInt :: Context -> Params -> Symbol -> Word -> IO ()
paramsSetUInt = (Ptr Z3_context
 -> Ptr Z3_params -> Ptr Z3_symbol -> CUInt -> IO ())
-> Context -> Params -> Symbol -> Word -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context -> Ptr Z3_params -> Ptr Z3_symbol -> CUInt -> IO ()
z3_params_set_uint

-- | Add a double parameter /k/ with value /v/ to the parameter set /p/.
paramsSetDouble :: Context -> Params -> Symbol -> Double -> IO ()
paramsSetDouble :: Context -> Params -> Symbol -> Double -> IO ()
paramsSetDouble = (Ptr Z3_context
 -> Ptr Z3_params -> Ptr Z3_symbol -> CDouble -> IO ())
-> Context -> Params -> Symbol -> Double -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_params -> Ptr Z3_symbol -> CDouble -> IO ()
z3_params_set_double

-- | Add a symbol parameter /k/ with value /v/ to the parameter set /p/.
paramsSetSymbol :: Context -> Params -> Symbol -> Symbol -> IO ()
paramsSetSymbol :: Context -> Params -> Symbol -> Symbol -> IO ()
paramsSetSymbol = (Ptr Z3_context
 -> Ptr Z3_params -> Ptr Z3_symbol -> Ptr Z3_symbol -> IO ())
-> Context -> Params -> Symbol -> Symbol -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_params -> Ptr Z3_symbol -> Ptr Z3_symbol -> IO ()
z3_params_set_symbol

-- | Convert a parameter set into a string.
--
-- This function is mainly used for printing the contents of a parameter set.
paramsToString :: Context -> Params -> IO String
paramsToString :: Context -> Params -> IO String
paramsToString = (Ptr Z3_context -> Ptr Z3_params -> IO CString)
-> Context -> Params -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_params -> IO CString
z3_params_to_string

-- TODO: Z3_params_validate

---------------------------------------------------------------------
-- Parameter Descriptions

-- TODO

---------------------------------------------------------------------
-- Symbols

-- | Create a Z3 symbol using an integer.
--
-- @mkIntSymbol c i@ /requires/ @0 <= i < 2^30@
mkIntSymbol :: Integral int => Context -> int -> IO Symbol
mkIntSymbol :: Context -> int -> IO Symbol
mkIntSymbol c :: Context
c i :: int
i
  | 0 int -> int -> Bool
forall a. Ord a => a -> a -> Bool
<= int
i Bool -> Bool -> Bool
&& int
i int -> int -> Bool
forall a. Ord a => a -> a -> Bool
<= 2int -> Int -> int
forall a b. (Num a, Integral b) => a -> b -> a
^(30::Int)int -> int -> int
forall a. Num a => a -> a -> a
-1
  = (Ptr Z3_context -> CInt -> IO (Ptr Z3_symbol))
-> Context -> int -> IO Symbol
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> CInt -> IO (Ptr Z3_symbol)
z3_mk_int_symbol Context
c int
i
  | Bool
otherwise
  = String -> IO Symbol
forall a. HasCallStack => String -> a
error "Z3.Base.mkIntSymbol: invalid range"

-- | Create a Z3 symbol using a 'String'.
mkStringSymbol :: Context -> String -> IO Symbol
mkStringSymbol :: Context -> String -> IO Symbol
mkStringSymbol = (Ptr Z3_context -> CString -> IO (Ptr Z3_symbol))
-> Context -> String -> IO Symbol
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> CString -> IO (Ptr Z3_symbol)
z3_mk_string_symbol

---------------------------------------------------------------------
-- Sorts

-- | Create a free (uninterpreted) type using the given name (symbol).
--
-- Two free types are considered the same iff the have the same name.
mkUninterpretedSort :: Context -> Symbol -> IO Sort
mkUninterpretedSort :: Context -> Symbol -> IO Sort
mkUninterpretedSort = (Ptr Z3_context -> Ptr Z3_symbol -> IO (Ptr Z3_sort))
-> Context -> Symbol -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_symbol -> IO (Ptr Z3_sort)
z3_mk_uninterpreted_sort

-- | Create the /boolean/ type.
--
-- This type is used to create propositional variables and predicates.
mkBoolSort :: Context -> IO Sort
mkBoolSort :: Context -> IO Sort
mkBoolSort = (Ptr Z3_context -> IO (Ptr Z3_sort)) -> Context -> IO Sort
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_sort)
z3_mk_bool_sort

-- | Create the /integer/ type.
--
-- This is the type of arbitrary precision integers.
-- A machine integer can be represented using bit-vectors, see 'mkBvSort'.
mkIntSort :: Context -> IO Sort
mkIntSort :: Context -> IO Sort
mkIntSort = (Ptr Z3_context -> IO (Ptr Z3_sort)) -> Context -> IO Sort
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_sort)
z3_mk_int_sort

-- | Create the /real/ type.
--
-- This type is not a floating point number.
-- Z3 does not have support for floating point numbers yet.
mkRealSort :: Context -> IO Sort
mkRealSort :: Context -> IO Sort
mkRealSort = (Ptr Z3_context -> IO (Ptr Z3_sort)) -> Context -> IO Sort
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_sort)
z3_mk_real_sort

-- | Create a bit-vector type of the given size.
--
-- This type can also be seen as a machine integer.
--
-- @mkBvSort c sz@ /requires/ @sz >= 0@
mkBvSort :: Integral int => Context -> int -> IO Sort
mkBvSort :: Context -> int -> IO Sort
mkBvSort c :: Context
c i :: int
i
  | int
i int -> int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0    = (Ptr Z3_context -> CUInt -> IO (Ptr Z3_sort))
-> Context -> int -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> CUInt -> IO (Ptr Z3_sort)
z3_mk_bv_sort Context
c int
i
  | Bool
otherwise = String -> IO Sort
forall a. HasCallStack => String -> a
error "Z3.Base.mkBvSort: negative size"

-- | Create a finite-domain type.
mkFiniteDomainSort :: Context -> Symbol -> Word64 -> IO Sort
mkFiniteDomainSort :: Context -> Symbol -> Word64 -> IO Sort
mkFiniteDomainSort = (Ptr Z3_context -> Ptr Z3_symbol -> CULLong -> IO (Ptr Z3_sort))
-> Context -> Symbol -> Word64 -> IO Sort
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_symbol -> CULLong -> IO (Ptr Z3_sort)
z3_mk_finite_domain_sort

-- | Create an array type
--
-- We usually represent the array type as: [domain -> range].
-- Arrays are usually used to model the heap/memory in software verification.
mkArraySort :: Context -> Sort -> Sort -> IO Sort
mkArraySort :: Context -> Sort -> Sort -> IO Sort
mkArraySort = (Ptr Z3_context -> Ptr Z3_sort -> Ptr Z3_sort -> IO (Ptr Z3_sort))
-> Context -> Sort -> Sort -> IO Sort
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_sort -> Ptr Z3_sort -> IO (Ptr Z3_sort)
z3_mk_array_sort

{- TODO
data TupleTyple
  = TupleType {
      tupleSort :: Sort
    , tupleCons :: FunDecl
    , tupleProj :: [FunDecl]
    }

mkTupleSort :: ... -> IO TupleType
-}

-- | Create a tuple type
--
-- A tuple with n fields has a constructor and n projections.
-- This function will also declare the constructor and projection functions.
mkTupleSort :: Context                         -- ^ Context
            -> Symbol                          -- ^ Name of the sort
            -> [(Symbol, Sort)]                -- ^ Name and sort of each field
            -> IO (Sort, FuncDecl, [FuncDecl]) -- ^ Resulting sort, and function
                                               -- declarations for the
                                               -- constructor and projections.
mkTupleSort :: Context
-> Symbol -> [(Symbol, Sort)] -> IO (Sort, FuncDecl, [FuncDecl])
mkTupleSort c :: Context
c sym :: Symbol
sym symSorts :: [(Symbol, Sort)]
symSorts = Context
-> (Ptr Z3_context -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Ptr Z3_context -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Symbol
-> (Ptr Z3_symbol -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Symbol
sym ((Ptr Z3_symbol -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Ptr Z3_symbol -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \symPtr :: Ptr Z3_symbol
symPtr ->
  [Symbol]
-> (Int -> Ptr (Ptr Z3_symbol) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Symbol]
syms ((Int -> Ptr (Ptr Z3_symbol) -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Int -> Ptr (Ptr Z3_symbol) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \ n :: Int
n symsPtr :: Ptr (Ptr Z3_symbol)
symsPtr ->
  [Sort]
-> (Ptr (Ptr Z3_sort) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Sort]
sorts ((Ptr (Ptr Z3_sort) -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Ptr (Ptr Z3_sort) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \ sortsPtr :: Ptr (Ptr Z3_sort)
sortsPtr ->
  (Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \ outConstrPtr :: Ptr (Ptr Z3_func_decl)
outConstrPtr ->
  Int
-> (Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
n ((Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
 -> IO (Sort, FuncDecl, [FuncDecl]))
-> (Ptr (Ptr Z3_func_decl) -> IO (Sort, FuncDecl, [FuncDecl]))
-> IO (Sort, FuncDecl, [FuncDecl])
forall a b. (a -> b) -> a -> b
$ \ outProjsPtr :: Ptr (Ptr Z3_func_decl)
outProjsPtr -> do
    Ptr Z3_sort
srtPtr <- Ptr Z3_context -> IO (Ptr Z3_sort) -> IO (Ptr Z3_sort)
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (IO (Ptr Z3_sort) -> IO (Ptr Z3_sort))
-> IO (Ptr Z3_sort) -> IO (Ptr Z3_sort)
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context
-> Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> Ptr (Ptr Z3_func_decl)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_sort)
z3_mk_tuple_sort Ptr Z3_context
cPtr Ptr Z3_symbol
symPtr
                                  (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) Ptr (Ptr Z3_symbol)
symsPtr Ptr (Ptr Z3_sort)
sortsPtr
                                  Ptr (Ptr Z3_func_decl)
outConstrPtr Ptr (Ptr Z3_func_decl)
outProjsPtr
    Ptr Z3_func_decl
outConstr <- Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_func_decl)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Z3_func_decl)
outConstrPtr
    [Ptr Z3_func_decl]
outProjs  <- Int -> Ptr (Ptr Z3_func_decl) -> IO [Ptr Z3_func_decl]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray Int
n Ptr (Ptr Z3_func_decl)
outProjsPtr
    Sort
sort <- Context -> Ptr Z3_sort -> IO Sort
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c Ptr Z3_sort
srtPtr
    FuncDecl
constrFd <- Context -> Ptr Z3_func_decl -> IO FuncDecl
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c Ptr Z3_func_decl
outConstr
    [FuncDecl]
projsFds <- (Ptr Z3_func_decl -> IO FuncDecl)
-> [Ptr Z3_func_decl] -> IO [FuncDecl]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> Ptr Z3_func_decl -> IO FuncDecl
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c) [Ptr Z3_func_decl]
outProjs
    (Sort, FuncDecl, [FuncDecl]) -> IO (Sort, FuncDecl, [FuncDecl])
forall (m :: * -> *) a. Monad m => a -> m a
return (Sort
sort, FuncDecl
constrFd, [FuncDecl]
projsFds)
  where (syms :: [Symbol]
syms, sorts :: [Sort]
sorts) = [(Symbol, Sort)] -> ([Symbol], [Sort])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Symbol, Sort)]
symSorts

-- TODO: Z3_mk_enumeration_sort
-- TODO: Z3_mk_list_sort

-- | Create a contructor
mkConstructor :: Context                      -- ^ Context
              -> Symbol                       -- ^ Name of the constructor
              -> Symbol                       -- ^ Name of recognizer function
              -> [(Symbol, Maybe Sort, Int)]  -- ^ Name, sort option, and sortRefs
              -> IO Constructor
mkConstructor :: Context
-> Symbol
-> Symbol
-> [(Symbol, Maybe Sort, Int)]
-> IO Constructor
mkConstructor c :: Context
c sym :: Symbol
sym recog :: Symbol
recog symSortsRefs :: [(Symbol, Maybe Sort, Int)]
symSortsRefs =
  Context -> (Ptr Z3_context -> IO Constructor) -> IO Constructor
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO Constructor) -> IO Constructor)
-> (Ptr Z3_context -> IO Constructor) -> IO Constructor
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Symbol -> (Ptr Z3_symbol -> IO Constructor) -> IO Constructor
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Symbol
sym ((Ptr Z3_symbol -> IO Constructor) -> IO Constructor)
-> (Ptr Z3_symbol -> IO Constructor) -> IO Constructor
forall a b. (a -> b) -> a -> b
$ \symPtr :: Ptr Z3_symbol
symPtr ->
  Symbol -> (Ptr Z3_symbol -> IO Constructor) -> IO Constructor
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Symbol
recog ((Ptr Z3_symbol -> IO Constructor) -> IO Constructor)
-> (Ptr Z3_symbol -> IO Constructor) -> IO Constructor
forall a b. (a -> b) -> a -> b
$ \recogPtr :: Ptr Z3_symbol
recogPtr ->
  [Symbol]
-> (CUInt -> Ptr (Ptr Z3_symbol) -> IO Constructor)
-> IO Constructor
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Symbol]
syms ((CUInt -> Ptr (Ptr Z3_symbol) -> IO Constructor)
 -> IO Constructor)
-> (CUInt -> Ptr (Ptr Z3_symbol) -> IO Constructor)
-> IO Constructor
forall a b. (a -> b) -> a -> b
$ \ n :: CUInt
n symsPtr :: Ptr (Ptr Z3_symbol)
symsPtr ->
  [Maybe Sort]
-> (Ptr (Ptr Z3_sort) -> IO Constructor) -> IO Constructor
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Maybe Sort]
maybeSorts ((Ptr (Ptr Z3_sort) -> IO Constructor) -> IO Constructor)
-> (Ptr (Ptr Z3_sort) -> IO Constructor) -> IO Constructor
forall a b. (a -> b) -> a -> b
$ \ sortsPtr :: Ptr (Ptr Z3_sort)
sortsPtr ->
  [Integer] -> (Ptr CUInt -> IO Constructor) -> IO Constructor
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray ((Int -> Integer) -> [Int] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Integer
forall a. Integral a => a -> Integer
toInteger [Int]
refs) ((Ptr CUInt -> IO Constructor) -> IO Constructor)
-> (Ptr CUInt -> IO Constructor) -> IO Constructor
forall a b. (a -> b) -> a -> b
$ \ refsPtr :: Ptr CUInt
refsPtr -> do
    Ptr Z3_constructor
constructor <- Ptr Z3_context
-> IO (Ptr Z3_constructor) -> IO (Ptr Z3_constructor)
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (IO (Ptr Z3_constructor) -> IO (Ptr Z3_constructor))
-> IO (Ptr Z3_constructor) -> IO (Ptr Z3_constructor)
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context
-> Ptr Z3_symbol
-> Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> Ptr CUInt
-> IO (Ptr Z3_constructor)
z3_mk_constructor
                       Ptr Z3_context
cPtr Ptr Z3_symbol
symPtr Ptr Z3_symbol
recogPtr CUInt
n Ptr (Ptr Z3_symbol)
symsPtr Ptr (Ptr Z3_sort)
sortsPtr Ptr CUInt
refsPtr
    Context -> Ptr Z3_constructor -> IO Constructor
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c Ptr Z3_constructor
constructor
  where (syms :: [Symbol]
syms, maybeSorts :: [Maybe Sort]
maybeSorts, refs :: [Int]
refs) = [(Symbol, Maybe Sort, Int)] -> ([Symbol], [Maybe Sort], [Int])
forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 [(Symbol, Maybe Sort, Int)]
symSortsRefs

-- | Create datatype, such as lists, trees, records,
-- enumerations or unions of records.
--
-- The datatype may be recursive.
-- Returns the datatype sort.
mkDatatype :: Context
           -> Symbol
           -> [Constructor]
           -> IO Sort
mkDatatype :: Context -> Symbol -> [Constructor] -> IO Sort
mkDatatype c :: Context
c sym :: Symbol
sym consList :: [Constructor]
consList = [Constructor]
-> (CUInt -> Ptr (Ptr Z3_constructor) -> IO Sort) -> IO Sort
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Constructor]
consList ((CUInt -> Ptr (Ptr Z3_constructor) -> IO Sort) -> IO Sort)
-> (CUInt -> Ptr (Ptr Z3_constructor) -> IO Sort) -> IO Sort
forall a b. (a -> b) -> a -> b
$ \ n :: CUInt
n consPtrs :: Ptr (Ptr Z3_constructor)
consPtrs -> do
  Context -> (Ptr Z3_context -> IO (Ptr Z3_sort)) -> IO Sort
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_sort)) -> IO Sort)
-> (Ptr Z3_context -> IO (Ptr Z3_sort)) -> IO Sort
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context
-> Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_constructor)
-> IO (Ptr Z3_sort)
z3_mk_datatype Ptr Z3_context
cPtr (Symbol -> Ptr Z3_symbol
unSymbol Symbol
sym) CUInt
n Ptr (Ptr Z3_constructor)
consPtrs

-- | Create list of constructors
mkConstructorList :: Context                      -- ^ Context
                  -> [Constructor]                -- ^ List of Constructors
                  -> IO ConstructorList
mkConstructorList :: Context -> [Constructor] -> IO ConstructorList
mkConstructorList c :: Context
c consList :: [Constructor]
consList = [Constructor]
-> (CUInt -> Ptr (Ptr Z3_constructor) -> IO ConstructorList)
-> IO ConstructorList
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Constructor]
consList ((CUInt -> Ptr (Ptr Z3_constructor) -> IO ConstructorList)
 -> IO ConstructorList)
-> (CUInt -> Ptr (Ptr Z3_constructor) -> IO ConstructorList)
-> IO ConstructorList
forall a b. (a -> b) -> a -> b
$ \ n :: CUInt
n consPtrs :: Ptr (Ptr Z3_constructor)
consPtrs -> do
  Context
-> (Ptr Z3_context -> IO (Ptr Z3_constructor_list))
-> IO ConstructorList
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_constructor_list))
 -> IO ConstructorList)
-> (Ptr Z3_context -> IO (Ptr Z3_constructor_list))
-> IO ConstructorList
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context
-> CUInt
-> Ptr (Ptr Z3_constructor)
-> IO (Ptr Z3_constructor_list)
z3_mk_constructor_list Ptr Z3_context
cPtr CUInt
n Ptr (Ptr Z3_constructor)
consPtrs

-- | Create mutually recursive datatypes, such as a tree and forest.
--
-- Returns the datatype sorts
mkDatatypes :: Context
            -> [Symbol]
            -> [[Constructor]]
            -> IO ([Sort])
mkDatatypes :: Context -> [Symbol] -> [[Constructor]] -> IO [Sort]
mkDatatypes c :: Context
c syms :: [Symbol]
syms consLists :: [[Constructor]]
consLists =
  if [[Constructor]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Constructor]]
consLists Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n then do
    [ConstructorList]
consLists' <- ([Constructor] -> IO ConstructorList)
-> [[Constructor]] -> IO [ConstructorList]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> [Constructor] -> IO ConstructorList
mkConstructorList Context
c) [[Constructor]]
consLists

    Context -> (Ptr Z3_context -> IO [Sort]) -> IO [Sort]
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO [Sort]) -> IO [Sort])
-> (Ptr Z3_context -> IO [Sort]) -> IO [Sort]
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
      [Symbol]
-> (CUInt -> Ptr (Ptr Z3_symbol) -> IO [Sort]) -> IO [Sort]
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Symbol]
syms ((CUInt -> Ptr (Ptr Z3_symbol) -> IO [Sort]) -> IO [Sort])
-> (CUInt -> Ptr (Ptr Z3_symbol) -> IO [Sort]) -> IO [Sort]
forall a b. (a -> b) -> a -> b
$ \ symLen :: CUInt
symLen symsPtr :: Ptr (Ptr Z3_symbol)
symsPtr ->
        [ConstructorList]
-> (Ptr (Ptr Z3_constructor_list) -> IO [Sort]) -> IO [Sort]
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [ConstructorList]
consLists' ((Ptr (Ptr Z3_constructor_list) -> IO [Sort]) -> IO [Sort])
-> (Ptr (Ptr Z3_constructor_list) -> IO [Sort]) -> IO [Sort]
forall a b. (a -> b) -> a -> b
$ \ consPtr :: Ptr (Ptr Z3_constructor_list)
consPtr ->
          Int -> (Ptr (Ptr Z3_sort) -> IO [Sort]) -> IO [Sort]
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
n ((Ptr (Ptr Z3_sort) -> IO [Sort]) -> IO [Sort])
-> (Ptr (Ptr Z3_sort) -> IO [Sort]) -> IO [Sort]
forall a b. (a -> b) -> a -> b
$ \ sortsPtr :: Ptr (Ptr Z3_sort)
sortsPtr -> do
            Ptr Z3_context
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> Ptr (Ptr Z3_constructor_list)
-> IO ()
z3_mk_datatypes Ptr Z3_context
cPtr CUInt
symLen Ptr (Ptr Z3_symbol)
symsPtr Ptr (Ptr Z3_sort)
sortsPtr Ptr (Ptr Z3_constructor_list)
consPtr
            Context -> Int -> Ptr (Ptr Z3_sort) -> IO [Sort]
forall h c.
(Marshal h c, Storable c) =>
Context -> Int -> Ptr c -> IO [h]
peekArrayToHs Context
c Int
n Ptr (Ptr Z3_sort)
sortsPtr
    else
      String -> IO [Sort]
forall a. HasCallStack => String -> a
error "Z3.Base.mkDatatypes: lists of different length"
  where n :: Int
n = [Symbol] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Symbol]
syms

-- | Create an set type with a given domain type
mkSetSort :: Context -> Sort -> IO Sort
mkSetSort :: Context -> Sort -> IO Sort
mkSetSort = (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort))
-> Context -> Sort -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort)
z3_mk_set_sort

---------------------------------------------------------------------
-- * Constants and Applications

-- | Declare a constant or function.
mkFuncDecl :: Context   -- ^ Logical context.
            -> Symbol   -- ^ Name of the function (or constant).
            -> [Sort]   -- ^ Function domain (empty for constants).
            -> Sort     -- ^ Return sort of the function.
            -> IO FuncDecl
mkFuncDecl :: Context -> Symbol -> [Sort] -> Sort -> IO FuncDecl
mkFuncDecl ctx :: Context
ctx smb :: Symbol
smb dom :: [Sort]
dom rng :: Sort
rng =
  (Ptr Z3_context
 -> Ptr Z3_symbol
 -> CUInt
 -> Ptr (Ptr Z3_sort)
 -> Ptr Z3_sort
 -> IO (Ptr Z3_func_decl))
-> Context
-> ((Ptr Z3_symbol
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr Z3_sort
     -> IO (Ptr Z3_func_decl))
    -> IO (Ptr Z3_func_decl))
-> IO FuncDecl
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
z3_mk_func_decl Context
ctx (((Ptr Z3_symbol
   -> CUInt
   -> Ptr (Ptr Z3_sort)
   -> Ptr Z3_sort
   -> IO (Ptr Z3_func_decl))
  -> IO (Ptr Z3_func_decl))
 -> IO FuncDecl)
-> ((Ptr Z3_symbol
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr Z3_sort
     -> IO (Ptr Z3_func_decl))
    -> IO (Ptr Z3_func_decl))
-> IO FuncDecl
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
f ->
    Symbol
-> (Ptr Z3_symbol -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Symbol
smb ((Ptr Z3_symbol -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl))
-> (Ptr Z3_symbol -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \ptrSym :: Ptr Z3_symbol
ptrSym ->
    [Sort]
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Sort]
dom ((CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
 -> IO (Ptr Z3_func_decl))
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \domNum :: CUInt
domNum domArr :: Ptr (Ptr Z3_sort)
domArr ->
    Sort
-> (Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Sort
rng ((Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl))
-> (Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \ptrRange :: Ptr Z3_sort
ptrRange ->
      Ptr Z3_symbol
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
f Ptr Z3_symbol
ptrSym CUInt
domNum Ptr (Ptr Z3_sort)
domArr Ptr Z3_sort
ptrRange

-- | Create a constant or function application.
mkApp :: Context -> FuncDecl -> [AST] -> IO AST
mkApp :: Context -> FuncDecl -> [AST] -> IO AST
mkApp ctx :: Context
ctx fd :: FuncDecl
fd args :: [AST]
args = (Ptr Z3_context
 -> Ptr Z3_func_decl
 -> CUInt
 -> Ptr (Ptr Z3_ast)
 -> IO (Ptr Z3_ast))
-> Context
-> ((Ptr Z3_func_decl
     -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_app Context
ctx (((Ptr Z3_func_decl
   -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((Ptr Z3_func_decl
     -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f ->
  FuncDecl
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c FuncDecl
fd ((Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \fdPtr :: Ptr Z3_func_decl
fdPtr ->
  [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
args ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \argsNum :: CUInt
argsNum argsArr :: Ptr (Ptr Z3_ast)
argsArr ->
    Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f Ptr Z3_func_decl
fdPtr CUInt
argsNum Ptr (Ptr Z3_ast)
argsArr

-- | Declare and create a constant.
--
-- This is a shorthand for:
-- @do xd <- mkFunDecl c x [] s; mkApp c xd []@
mkConst :: Context -> Symbol -> Sort -> IO AST
mkConst :: Context -> Symbol -> Sort -> IO AST
mkConst = (Ptr Z3_context -> Ptr Z3_symbol -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Symbol -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_symbol -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_const

-- | Declare a fresh constant or function.
mkFreshFuncDecl :: Context -> String -> [Sort] -> Sort -> IO FuncDecl
mkFreshFuncDecl :: Context -> String -> [Sort] -> Sort -> IO FuncDecl
mkFreshFuncDecl ctx :: Context
ctx str :: String
str dom :: [Sort]
dom rng :: Sort
rng =
  String -> (CString -> IO FuncDecl) -> IO FuncDecl
forall a. String -> (CString -> IO a) -> IO a
withCString String
str ((CString -> IO FuncDecl) -> IO FuncDecl)
-> (CString -> IO FuncDecl) -> IO FuncDecl
forall a b. (a -> b) -> a -> b
$ \cstr :: CString
cstr ->
    (Ptr Z3_context
 -> CString
 -> CUInt
 -> Ptr (Ptr Z3_sort)
 -> Ptr Z3_sort
 -> IO (Ptr Z3_func_decl))
-> Context
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr Z3_sort
     -> IO (Ptr Z3_func_decl))
    -> IO (Ptr Z3_func_decl))
-> IO FuncDecl
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> CString
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
forall z3_context.
Ptr z3_context
-> CString
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
z3_mk_fresh_func_decl Context
ctx (((CString
   -> CUInt
   -> Ptr (Ptr Z3_sort)
   -> Ptr Z3_sort
   -> IO (Ptr Z3_func_decl))
  -> IO (Ptr Z3_func_decl))
 -> IO FuncDecl)
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr Z3_sort
     -> IO (Ptr Z3_func_decl))
    -> IO (Ptr Z3_func_decl))
-> IO FuncDecl
forall a b. (a -> b) -> a -> b
$ \f :: CString
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
f ->
    [Sort]
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Sort]
dom ((CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
 -> IO (Ptr Z3_func_decl))
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \domNum :: CUInt
domNum domArr :: Ptr (Ptr Z3_sort)
domArr ->
    Sort
-> (Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Sort
rng ((Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl))
-> (Ptr Z3_sort -> IO (Ptr Z3_func_decl)) -> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \ptrRange :: Ptr Z3_sort
ptrRange ->
      CString
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr Z3_sort
-> IO (Ptr Z3_func_decl)
f CString
cstr CUInt
domNum Ptr (Ptr Z3_sort)
domArr Ptr Z3_sort
ptrRange

-- | Declare and create a fresh constant.
mkFreshConst :: Context -- ^ Logical context.
             -> String  -- ^ Prefix.
             -> Sort    -- ^ Sort of the constant.
             -> IO AST
mkFreshConst :: Context -> String -> Sort -> IO AST
mkFreshConst = (Ptr Z3_context -> CString -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> String -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CString -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_fresh_const

-------------------------------------------------
-- ** Helpers

-- | Declare and create a variable (aka /constant/).
--
-- An alias for 'mkConst'.
mkVar :: Context -> Symbol -> Sort -> IO AST
mkVar :: Context -> Symbol -> Sort -> IO AST
mkVar = Context -> Symbol -> Sort -> IO AST
mkConst

-- | Declarate and create a variable of sort /bool/.
--
-- See 'mkVar'.
mkBoolVar :: Context -> Symbol -> IO AST
mkBoolVar :: Context -> Symbol -> IO AST
mkBoolVar ctx :: Context
ctx sym :: Symbol
sym = Context -> Symbol -> Sort -> IO AST
mkVar Context
ctx Symbol
sym (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkBoolSort Context
ctx

-- | Declarate and create a variable of sort /real/.
--
-- See 'mkVar'.
mkRealVar :: Context -> Symbol -> IO AST
mkRealVar :: Context -> Symbol -> IO AST
mkRealVar ctx :: Context
ctx sym :: Symbol
sym = Context -> Symbol -> Sort -> IO AST
mkVar Context
ctx Symbol
sym (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkRealSort Context
ctx

-- | Declarate and create a variable of sort /int/.
--
-- See 'mkVar'.
mkIntVar :: Context -> Symbol -> IO AST
mkIntVar :: Context -> Symbol -> IO AST
mkIntVar ctx :: Context
ctx sym :: Symbol
sym = Context -> Symbol -> Sort -> IO AST
mkVar Context
ctx Symbol
sym (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkIntSort Context
ctx

-- | Declarate and create a variable of sort /bit-vector/.
--
-- See 'mkVar'.
mkBvVar :: Context -> Symbol
                   -> Int     -- ^ bit-width
                   -> IO AST
mkBvVar :: Context -> Symbol -> Int -> IO AST
mkBvVar ctx :: Context
ctx sym :: Symbol
sym sz :: Int
sz = Context -> Symbol -> Sort -> IO AST
mkVar Context
ctx Symbol
sym (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> Int -> IO Sort
forall int. Integral int => Context -> int -> IO Sort
mkBvSort Context
ctx Int
sz

-- | Declare and create a /fresh/ variable (aka /constant/).
--
-- An alias for 'mkFreshConst'.
mkFreshVar :: Context -> String -> Sort -> IO AST
mkFreshVar :: Context -> String -> Sort -> IO AST
mkFreshVar = Context -> String -> Sort -> IO AST
mkFreshConst

-- | Declarate and create a /fresh/ variable of sort /bool/.
--
-- See 'mkFreshVar'.
mkFreshBoolVar :: Context -> String -> IO AST
mkFreshBoolVar :: Context -> String -> IO AST
mkFreshBoolVar ctx :: Context
ctx str :: String
str = Context -> String -> Sort -> IO AST
mkFreshVar Context
ctx String
str (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkBoolSort Context
ctx

-- | Declarate and create a /fresh/ variable of sort /real/.
--
-- See 'mkFreshVar'.
mkFreshRealVar :: Context -> String -> IO AST
mkFreshRealVar :: Context -> String -> IO AST
mkFreshRealVar ctx :: Context
ctx str :: String
str = Context -> String -> Sort -> IO AST
mkFreshVar Context
ctx String
str (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkRealSort Context
ctx

-- | Declarate and create a /fresh/ variable of sort /int/.
--
-- See 'mkFreshVar'.
mkFreshIntVar :: Context -> String -> IO AST
mkFreshIntVar :: Context -> String -> IO AST
mkFreshIntVar ctx :: Context
ctx str :: String
str = Context -> String -> Sort -> IO AST
mkFreshVar Context
ctx String
str (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkIntSort Context
ctx

-- | Declarate and create a /fresh/ variable of sort /bit-vector/.
--
-- See 'mkFreshVar'.
mkFreshBvVar :: Context -> String
                        -> Int     -- ^ bit-width
                        -> IO AST
mkFreshBvVar :: Context -> String -> Int -> IO AST
mkFreshBvVar ctx :: Context
ctx str :: String
str sz :: Int
sz = Context -> String -> Sort -> IO AST
mkFreshVar Context
ctx String
str (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> Int -> IO Sort
forall int. Integral int => Context -> int -> IO Sort
mkBvSort Context
ctx Int
sz

---------------------------------------------------------------------
-- Propositional Logic and Equality

-- | Create an AST node representing /true/.
mkTrue :: Context -> IO AST
mkTrue :: Context -> IO AST
mkTrue = (Ptr Z3_context -> IO (Ptr Z3_ast)) -> Context -> IO AST
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_ast)
z3_mk_true

-- | Create an AST node representing /false/.
mkFalse :: Context -> IO AST
mkFalse :: Context -> IO AST
mkFalse = (Ptr Z3_context -> IO (Ptr Z3_ast)) -> Context -> IO AST
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_ast)
z3_mk_false

-- | Create an AST node representing /l = r/.
mkEq :: Context -> AST -> AST -> IO AST
mkEq :: Context -> AST -> AST -> IO AST
mkEq = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_eq

-- | The distinct construct is used for declaring the arguments pairwise
-- distinct.
--
-- That is, @and [ args!!i /= args!!j | i <- [0..length(args)-1], j <- [i+1..length(args)-1] ]@
mkDistinct :: Context -> [AST] -> IO AST
mkDistinct :: Context -> [AST] -> IO AST
mkDistinct = (Ptr Z3_context -> IO (Ptr Z3_ast))
-> (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> [AST]
-> IO AST
liftAstN Ptr Z3_context -> IO (Ptr Z3_ast)
z3_mk_true Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_distinct

-- | Create an AST node representing /not(a)/.
mkNot :: Context -> AST -> IO AST
mkNot :: Context -> AST -> IO AST
mkNot = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_not

-- | Create an AST node representing an if-then-else: /ite(t1, t2, t3)/.
mkIte :: Context -> AST -> AST -> AST -> IO AST
mkIte :: Context -> AST -> AST -> AST -> IO AST
mkIte = (Ptr Z3_context
 -> Ptr Z3_ast -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> AST -> IO AST
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_ast -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_ite

-- | Create an AST node representing /t1 iff t2/.
mkIff :: Context -> AST -> AST -> IO AST
mkIff :: Context -> AST -> AST -> IO AST
mkIff = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_iff

-- | Create an AST node representing /t1 implies t2/.
mkImplies :: Context -> AST -> AST -> IO AST
mkImplies :: Context -> AST -> AST -> IO AST
mkImplies = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_implies

-- | Create an AST node representing /t1 xor t2/.
mkXor :: Context -> AST -> AST -> IO AST
mkXor :: Context -> AST -> AST -> IO AST
mkXor = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_xor

-- | Create an AST node representing args[0] and ... and args[num_args-1].
mkAnd :: Context -> [AST] -> IO AST
mkAnd :: Context -> [AST] -> IO AST
mkAnd = (Ptr Z3_context -> IO (Ptr Z3_ast))
-> (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> [AST]
-> IO AST
liftAstN Ptr Z3_context -> IO (Ptr Z3_ast)
z3_mk_true Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_and

-- | Create an AST node representing args[0] or ... or args[num_args-1].
mkOr :: Context -> [AST] -> IO AST
mkOr :: Context -> [AST] -> IO AST
mkOr = (Ptr Z3_context -> IO (Ptr Z3_ast))
-> (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> [AST]
-> IO AST
liftAstN Ptr Z3_context -> IO (Ptr Z3_ast)
z3_mk_false Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_or

-------------------------------------------------
-- ** Helpers

-- | Create an AST node representing the given boolean.
mkBool :: Context -> Bool -> IO AST
mkBool :: Context -> Bool -> IO AST
mkBool ctx :: Context
ctx False = Context -> IO AST
mkFalse Context
ctx
mkBool ctx :: Context
ctx True  = Context -> IO AST
mkTrue  Context
ctx

---------------------------------------------------------------------
-- Arithmetic: Integers and Reals

-- | Create an AST node representing args[0] + ... + args[num_args-1].
mkAdd :: Context -> [AST] -> IO AST
mkAdd :: Context -> [AST] -> IO AST
mkAdd ctx :: Context
ctx = IO AST
-> (NonEmpty AST -> IO AST) -> Maybe (NonEmpty AST) -> IO AST
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Context -> Integer -> IO AST
mkInteger Context
ctx 0) ((Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context -> NonEmpty AST -> IO AST
liftAstN1 Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_add Context
ctx) (Maybe (NonEmpty AST) -> IO AST)
-> ([AST] -> Maybe (NonEmpty AST)) -> [AST] -> IO AST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AST] -> Maybe (NonEmpty AST)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty

-- | Create an AST node representing args[0] * ... * args[num_args-1].
mkMul :: Context -> [AST] -> IO AST
mkMul :: Context -> [AST] -> IO AST
mkMul ctx :: Context
ctx = IO AST
-> (NonEmpty AST -> IO AST) -> Maybe (NonEmpty AST) -> IO AST
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Context -> Integer -> IO AST
mkInteger Context
ctx 1) ((Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context -> NonEmpty AST -> IO AST
liftAstN1 Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_mul Context
ctx) (Maybe (NonEmpty AST) -> IO AST)
-> ([AST] -> Maybe (NonEmpty AST)) -> [AST] -> IO AST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AST] -> Maybe (NonEmpty AST)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty

-- | Create an AST node representing args[0] - ... - args[num_args - 1].
mkSub :: Context -> NonEmpty AST -> IO AST
mkSub :: Context -> NonEmpty AST -> IO AST
mkSub = (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context -> NonEmpty AST -> IO AST
liftAstN1 Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_sub

-- | Create an AST node representing -arg.
mkUnaryMinus :: Context -> AST -> IO AST
mkUnaryMinus :: Context -> AST -> IO AST
mkUnaryMinus = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_unary_minus

-- | Create an AST node representing arg1 div arg2.
mkDiv :: Context -> AST -> AST -> IO AST
mkDiv :: Context -> AST -> AST -> IO AST
mkDiv = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_div

-- | Create an AST node representing arg1 mod arg2.
mkMod :: Context -> AST -> AST -> IO AST
mkMod :: Context -> AST -> AST -> IO AST
mkMod = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_mod

-- | Create an AST node representing arg1 rem arg2.
mkRem :: Context -> AST -> AST -> IO AST
mkRem :: Context -> AST -> AST -> IO AST
mkRem = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_rem

-- TODO: Z3_mk_power

-- | Create less than.
mkLt :: Context -> AST -> AST -> IO AST
mkLt :: Context -> AST -> AST -> IO AST
mkLt = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_lt

-- | Create less than or equal to.
mkLe :: Context -> AST -> AST -> IO AST
mkLe :: Context -> AST -> AST -> IO AST
mkLe = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_le

-- | Create greater than.
mkGt :: Context -> AST -> AST -> IO AST
mkGt :: Context -> AST -> AST -> IO AST
mkGt = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_gt

-- | Create greater than or equal to.
mkGe :: Context -> AST -> AST -> IO AST
mkGe :: Context -> AST -> AST -> IO AST
mkGe = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_ge

-- | Coerce an integer to a real.
mkInt2Real :: Context -> AST -> IO AST
mkInt2Real :: Context -> AST -> IO AST
mkInt2Real = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_int2real

-- | Coerce a real to an integer.
mkReal2Int :: Context -> AST -> IO AST
mkReal2Int :: Context -> AST -> IO AST
mkReal2Int = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_real2int

-- | Check if a real number is an integer.
mkIsInt :: Context -> AST -> IO AST
mkIsInt :: Context -> AST -> IO AST
mkIsInt = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_is_int

---------------------------------------------------------------------
-- Bit-vectors

-- | Bitwise negation.
mkBvnot :: Context -> AST -> IO AST
mkBvnot :: Context -> AST -> IO AST
mkBvnot = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvnot

-- | Take conjunction of bits in vector, return vector of length 1.
mkBvredand :: Context -> AST -> IO AST
mkBvredand :: Context -> AST -> IO AST
mkBvredand = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvredand

-- | Take disjunction of bits in vector, return vector of length 1.
mkBvredor :: Context -> AST -> IO AST
mkBvredor :: Context -> AST -> IO AST
mkBvredor = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvredor

-- | Bitwise and.
mkBvand :: Context -> AST -> AST -> IO AST
mkBvand :: Context -> AST -> AST -> IO AST
mkBvand = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvand

-- | Bitwise or.
mkBvor :: Context -> AST -> AST -> IO AST
mkBvor :: Context -> AST -> AST -> IO AST
mkBvor = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvor

-- | Bitwise exclusive-or.
mkBvxor :: Context -> AST -> AST -> IO AST
mkBvxor :: Context -> AST -> AST -> IO AST
mkBvxor = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvxor

-- | Bitwise nand.
mkBvnand :: Context -> AST -> AST -> IO AST
mkBvnand :: Context -> AST -> AST -> IO AST
mkBvnand = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvnand

-- | Bitwise nor.
mkBvnor :: Context -> AST -> AST -> IO AST
mkBvnor :: Context -> AST -> AST -> IO AST
mkBvnor = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvnor

-- | Bitwise xnor.
mkBvxnor :: Context -> AST -> AST -> IO AST
mkBvxnor :: Context -> AST -> AST -> IO AST
mkBvxnor = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvxnor

-- | Standard two's complement unary minus.
mkBvneg :: Context -> AST -> IO AST
mkBvneg :: Context -> AST -> IO AST
mkBvneg = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvneg

-- | Standard two's complement addition.
mkBvadd :: Context -> AST -> AST -> IO AST
mkBvadd :: Context -> AST -> AST -> IO AST
mkBvadd = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvadd

-- | Standard two's complement subtraction.
mkBvsub :: Context -> AST -> AST -> IO AST
mkBvsub :: Context -> AST -> AST -> IO AST
mkBvsub = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsub

-- | Standard two's complement multiplication.
mkBvmul :: Context -> AST -> AST -> IO AST
mkBvmul :: Context -> AST -> AST -> IO AST
mkBvmul = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvmul

-- | Unsigned division.
mkBvudiv :: Context -> AST -> AST -> IO AST
mkBvudiv :: Context -> AST -> AST -> IO AST
mkBvudiv = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvudiv

-- | Two's complement signed division.
mkBvsdiv :: Context -> AST -> AST -> IO AST
mkBvsdiv :: Context -> AST -> AST -> IO AST
mkBvsdiv = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsdiv

-- | Unsigned remainder.
mkBvurem :: Context -> AST -> AST -> IO AST
mkBvurem :: Context -> AST -> AST -> IO AST
mkBvurem = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvurem

-- | Two's complement signed remainder (sign follows dividend).
mkBvsrem :: Context -> AST -> AST -> IO AST
mkBvsrem :: Context -> AST -> AST -> IO AST
mkBvsrem = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsrem

-- | Two's complement signed remainder (sign follows divisor).
mkBvsmod :: Context -> AST -> AST -> IO AST
mkBvsmod :: Context -> AST -> AST -> IO AST
mkBvsmod = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsmod

-- | Unsigned less than.
mkBvult :: Context -> AST -> AST -> IO AST
mkBvult :: Context -> AST -> AST -> IO AST
mkBvult = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvult

-- | Two's complement signed less than.
mkBvslt :: Context -> AST -> AST -> IO AST
mkBvslt :: Context -> AST -> AST -> IO AST
mkBvslt = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvslt

-- | Unsigned less than or equal to.
mkBvule :: Context -> AST -> AST -> IO AST
mkBvule :: Context -> AST -> AST -> IO AST
mkBvule = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvule

-- | Two's complement signed less than or equal to.
mkBvsle :: Context -> AST -> AST -> IO AST
mkBvsle :: Context -> AST -> AST -> IO AST
mkBvsle = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsle

-- | Unsigned greater than or equal to.
mkBvuge :: Context -> AST -> AST -> IO AST
mkBvuge :: Context -> AST -> AST -> IO AST
mkBvuge = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvuge

-- | Two's complement signed greater than or equal to.
mkBvsge :: Context -> AST -> AST -> IO AST
mkBvsge :: Context -> AST -> AST -> IO AST
mkBvsge = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsge

-- | Unsigned greater than.
mkBvugt :: Context -> AST -> AST -> IO AST
mkBvugt :: Context -> AST -> AST -> IO AST
mkBvugt = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvugt

-- | Two's complement signed greater than.
mkBvsgt :: Context -> AST -> AST -> IO AST
mkBvsgt :: Context -> AST -> AST -> IO AST
mkBvsgt = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsgt

-- | Concatenate the given bit-vectors.
mkConcat :: Context -> AST -> AST -> IO AST
mkConcat :: Context -> AST -> AST -> IO AST
mkConcat = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_concat

-- | 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/.
mkExtract :: Context -> Int -> Int -> AST -> IO AST
mkExtract :: Context -> Int -> Int -> AST -> IO AST
mkExtract = (Ptr Z3_context -> CUInt -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> Int -> AST -> IO AST
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context -> CUInt -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_extract

-- | 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.
mkSignExt :: Context -> Int -> AST -> IO AST
mkSignExt :: Context -> Int -> AST -> IO AST
mkSignExt = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_sign_ext

-- | 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.
mkZeroExt :: Context -> Int -> AST -> IO AST
mkZeroExt :: Context -> Int -> AST -> IO AST
mkZeroExt = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_zero_ext

-- | Repeat the given bit-vector up length /i/.
mkRepeat :: Context -> Int -> AST -> IO AST
mkRepeat :: Context -> Int -> AST -> IO AST
mkRepeat = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_repeat

-- | Shift left.
mkBvshl :: Context -> AST -> AST -> IO AST
mkBvshl :: Context -> AST -> AST -> IO AST
mkBvshl = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvshl

-- | Logical shift right.
mkBvlshr :: Context -> AST -> AST -> IO AST
mkBvlshr :: Context -> AST -> AST -> IO AST
mkBvlshr = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvlshr

-- | Arithmetic shift right.
mkBvashr :: Context -> AST -> AST -> IO AST
mkBvashr :: Context -> AST -> AST -> IO AST
mkBvashr = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvashr

-- | Rotate bits of /t1/ to the left /i/ times.
mkRotateLeft :: Context -> Int -> AST -> IO AST
mkRotateLeft :: Context -> Int -> AST -> IO AST
mkRotateLeft = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_rotate_left

-- | Rotate bits of /t1/ to the right /i/ times.
mkRotateRight :: Context -> Int -> AST -> IO AST
mkRotateRight :: Context -> Int -> AST -> IO AST
mkRotateRight = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_rotate_right

-- | Rotate bits of /t1/ to the left /t2/ times.
mkExtRotateLeft :: Context -> AST -> AST -> IO AST
mkExtRotateLeft :: Context -> AST -> AST -> IO AST
mkExtRotateLeft = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_ext_rotate_left

-- | Rotate bits of /t1/ to the right /t2/ times.
mkExtRotateRight :: Context -> AST -> AST -> IO AST
mkExtRotateRight :: Context -> AST -> AST -> IO AST
mkExtRotateRight = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_ext_rotate_right

-- | Create an /n/ bit bit-vector from the integer argument /t1/.
mkInt2bv :: Context -> Int -> AST -> IO AST
mkInt2bv :: Context -> Int -> AST -> IO AST
mkInt2bv = (Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Int -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_int2bv

-- | 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.
mkBv2int :: Context -> AST -> Bool -> IO AST
mkBv2int :: Context -> AST -> Bool -> IO AST
mkBv2int = (Ptr Z3_context -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast))
-> Context -> AST -> Bool -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast)
z3_mk_bv2int

-- | Create a predicate that checks that the bit-wise addition of /t1/ and /t2/
-- does not overflow.
mkBvaddNoOverflow :: Context -> AST -> AST -> Bool -> IO AST
mkBvaddNoOverflow :: Context -> AST -> AST -> Bool -> IO AST
mkBvaddNoOverflow = (Ptr Z3_context
 -> Ptr Z3_ast -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> Bool -> IO AST
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_ast -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast)
z3_mk_bvadd_no_overflow

-- | Create a predicate that checks that the bit-wise signed addition of /t1/
-- and /t2/ does not underflow.
mkBvaddNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvaddNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvaddNoUnderflow = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvadd_no_underflow

-- | Create a predicate that checks that the bit-wise signed subtraction of /t1/
-- and /t2/ does not overflow.
mkBvsubNoOverflow :: Context -> AST -> AST -> IO AST
mkBvsubNoOverflow :: Context -> AST -> AST -> IO AST
mkBvsubNoOverflow = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsub_no_overflow

-- | Create a predicate that checks that the bit-wise subtraction of /t1/ and
-- /t2/ does not underflow.
mkBvsubNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvsubNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvsubNoUnderflow = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsub_no_underflow

-- | Create a predicate that checks that the bit-wise signed division of /t1/
-- and /t2/ does not overflow.
mkBvsdivNoOverflow :: Context -> AST -> AST -> IO AST
mkBvsdivNoOverflow :: Context -> AST -> AST -> IO AST
mkBvsdivNoOverflow = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvsdiv_no_overflow

-- | Check that bit-wise negation does not overflow when /t1/ is interpreted as
-- a signed bit-vector.
mkBvnegNoOverflow :: Context -> AST -> IO AST
mkBvnegNoOverflow :: Context -> AST -> IO AST
mkBvnegNoOverflow = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvneg_no_overflow

-- | Create a predicate that checks that the bit-wise multiplication of /t1/ and
-- /t2/ does not overflow.
mkBvmulNoOverflow :: Context -> AST -> AST -> Bool -> IO AST
mkBvmulNoOverflow :: Context -> AST -> AST -> Bool -> IO AST
mkBvmulNoOverflow = (Ptr Z3_context
 -> Ptr Z3_ast -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> Bool -> IO AST
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_ast -> Ptr Z3_ast -> Z3_bool -> IO (Ptr Z3_ast)
z3_mk_bvmul_no_overflow

-- | Create a predicate that checks that the bit-wise signed multiplication of
-- /t1/ and /t2/ does not underflow.
mkBvmulNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvmulNoUnderflow :: Context -> AST -> AST -> IO AST
mkBvmulNoUnderflow = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_bvmul_no_underflow

---------------------------------------------------------------------
-- Arrays

-- | Array read. The argument a is the array and i is the index of the array
-- that gets read.
mkSelect :: Context
            -> AST      -- ^ Array.
            -> AST      -- ^ Index of the array to read.
            -> IO AST
mkSelect :: Context -> AST -> AST -> IO AST
mkSelect = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_select

-- | Array update.
--
-- The result of this function is an array that is equal to the input array
-- (with respect to select) on all indices except for i, where it maps to v.
--
-- The semantics of this function is given by the theory of arrays described
-- in the SMT-LIB standard. See <http://smtlib.org> for more details.
mkStore :: Context
          -> AST      -- ^ Array.
          -> AST      -- ^ Index /i/ of the array.
          -> AST      -- ^ New value for /i/.
          -> IO AST
mkStore :: Context -> AST -> AST -> AST -> IO AST
mkStore = (Ptr Z3_context
 -> Ptr Z3_ast -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> AST -> IO AST
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_ast -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_store

-- | Create the constant array.
--
-- The resulting term is an array, such that a select on an arbitrary index
-- produces the value /v/.
mkConstArray :: Context
            -> Sort   -- ^ Domain sort of the array.
            -> AST    -- ^ Value /v/ that the array maps to.
            -> IO AST
mkConstArray :: Context -> Sort -> AST -> IO AST
mkConstArray = (Ptr Z3_context -> Ptr Z3_sort -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> Sort -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_sort -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_const_array

-- | Map a function /f/ on the the argument arrays.
--
-- The /n/ nodes args must be of array sorts [domain -> range_i].
-- The function declaration /f/ must have type range_1 .. range_n -> range.
-- The sort of the result is [domain -> range].
mkMap :: Context
        -> FuncDecl   -- ^ Function /f/.
        -> [AST]      -- ^ List of arrays.
        -> IO AST
mkMap :: Context -> FuncDecl -> [AST] -> IO AST
mkMap ctx :: Context
ctx fun :: FuncDecl
fun args :: [AST]
args = (Ptr Z3_context
 -> Ptr Z3_func_decl
 -> CUInt
 -> Ptr (Ptr Z3_ast)
 -> IO (Ptr Z3_ast))
-> Context
-> ((Ptr Z3_func_decl
     -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_map Context
ctx (((Ptr Z3_func_decl
   -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((Ptr Z3_func_decl
     -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f ->
  FuncDecl
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c FuncDecl
fun ((Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \funPtr :: Ptr Z3_func_decl
funPtr ->
  [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
args ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \argsNum :: CUInt
argsNum argsArr :: Ptr (Ptr Z3_ast)
argsArr ->
    Ptr Z3_func_decl -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f Ptr Z3_func_decl
funPtr CUInt
argsNum Ptr (Ptr Z3_ast)
argsArr

-- | Access the array default value.
--
-- Produces the default range value, for arrays that can be represented as
-- finite maps with a default range value.
mkArrayDefault :: Context
                -> AST      -- ^ Array.
                -> IO AST
mkArrayDefault :: Context -> AST -> IO AST
mkArrayDefault = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_array_default

---------------------------------------------------------------------
-- Sets

-- | Create the empty set.
mkEmptySet :: Context
            -> Sort   -- ^ Domain sort of the set.
            -> IO AST
mkEmptySet :: Context -> Sort -> IO AST
mkEmptySet = (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Sort -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_empty_set

-- | Create the full set.
mkFullSet :: Context
            -> Sort   -- ^ Domain sort of the set.
            -> IO AST
mkFullSet :: Context -> Sort -> IO AST
mkFullSet = (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Sort -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_full_set

-- | Add an element to a set.
mkSetAdd :: Context
          -> AST      -- ^ Set.
          -> AST      -- ^ Element.
          -> IO AST
mkSetAdd :: Context -> AST -> AST -> IO AST
mkSetAdd = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_add

-- | Remove an element from a set.
mkSetDel :: Context
          -> AST      -- ^ Set.
          -> AST      -- ^ Element.
          -> IO AST
mkSetDel :: Context -> AST -> AST -> IO AST
mkSetDel = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_del

-- | Take the union of a list of sets.
mkSetUnion :: Context -> [AST] -> IO AST
mkSetUnion :: Context -> [AST] -> IO AST
mkSetUnion ctx :: Context
ctx args :: [AST]
args = (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_set_union Context
ctx (((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f ->
  [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
args ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \argsNum :: CUInt
argsNum argsArr :: Ptr (Ptr Z3_ast)
argsArr ->
    CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f CUInt
argsNum Ptr (Ptr Z3_ast)
argsArr

-- | Take the intersection of a list of sets.
mkSetIntersect :: Context -> [AST] -> IO AST
mkSetIntersect :: Context -> [AST] -> IO AST
mkSetIntersect ctx :: Context
ctx args :: [AST]
args = (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_mk_set_intersect Context
ctx (((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f ->
  [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
args ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \argsNum :: CUInt
argsNum argsArr :: Ptr (Ptr Z3_ast)
argsArr ->
    CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f CUInt
argsNum Ptr (Ptr Z3_ast)
argsArr

-- | Take the set difference between two sets.
mkSetDifference :: Context
          -> AST      -- ^ First set.
          -> AST      -- ^ Second set.
          -> IO AST
mkSetDifference :: Context -> AST -> AST -> IO AST
mkSetDifference = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_difference

-- | Take the complement of a set.
mkSetComplement :: Context
          -> AST      -- ^ Set.
          -> IO AST
mkSetComplement :: Context -> AST -> IO AST
mkSetComplement = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_complement

-- | Check for set membership.
mkSetMember :: Context
          -> AST      -- ^ Element.
          -> AST      -- ^ Set.
          -> IO AST
mkSetMember :: Context -> AST -> AST -> IO AST
mkSetMember = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_member

-- | Check if the first set is a subset of the second set.
mkSetSubset :: Context
          -> AST      -- ^ First set.
          -> AST      -- ^ Second set.
          -> IO AST
mkSetSubset :: Context -> AST -> AST -> IO AST
mkSetSubset = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> AST -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_mk_set_subset

---------------------------------------------------------------------
-- * Numerals

-- | Create a numeral of a given sort.
mkNumeral :: Context -> String -> Sort -> IO AST
mkNumeral :: Context -> String -> Sort -> IO AST
mkNumeral = (Ptr Z3_context -> CString -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> String -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CString -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_numeral

-- | Create a real from a fraction.
mkReal :: Context -> Int   -- ^ numerator
                  -> Int   -- ^ denominator (/= 0)
                  -> IO AST
mkReal :: Context -> Int -> Int -> IO AST
mkReal ctx :: Context
ctx num :: Int
num den :: Int
den
  | Int
den Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 0  = (Ptr Z3_context -> CInt -> CInt -> IO (Ptr Z3_ast))
-> Context -> Int -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CInt -> CInt -> IO (Ptr Z3_ast)
z3_mk_real Context
ctx Int
num Int
den
  | Bool
otherwise = String -> IO AST
forall a. HasCallStack => String -> a
error "Z3.Base.mkReal: zero denominator"

-- | Create a numeral of an int, bit-vector, or finite-domain sort.
--
-- This function can be use to create numerals that fit in a
-- /machine integer/.
-- It is slightly faster than 'mkNumeral' since it is not necessary
-- to parse a string.
mkInt :: Context -> Int -> Sort -> IO AST
mkInt :: Context -> Int -> Sort -> IO AST
mkInt = (Ptr Z3_context -> CInt -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Int -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CInt -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_int

-- | Create a numeral of an int, bit-vector, or finite-domain sort.
--
-- This function can be use to create numerals that fit in a
-- /machine unsigned integer/.
-- It is slightly faster than 'mkNumeral' since it is not necessary
-- to parse a string.
mkUnsignedInt :: Context -> Word -> Sort -> IO AST
mkUnsignedInt :: Context -> Word -> Sort -> IO AST
mkUnsignedInt = (Ptr Z3_context -> CUInt -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Word -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_unsigned_int

-- | Create a numeral of an int, bit-vector, or finite-domain sort.
--
-- This function can be use to create numerals that fit in a
-- /machine 64-bit integer/.
-- It is slightly faster than 'mkNumeral' since it is not necessary
-- to parse a string.
mkInt64 :: Context -> Int64 -> Sort -> IO AST
mkInt64 :: Context -> Int64 -> Sort -> IO AST
mkInt64 = (Ptr Z3_context -> CLLong -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Int64 -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CLLong -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_int64

-- | Create a numeral of an int, bit-vector, or finite-domain sort.
--
-- This function can be use to create numerals that fit in a
-- /machine unsigned 64-bit integer/.
-- It is slightly faster than 'mkNumeral' since it is not necessary
-- to parse a string.
mkUnsignedInt64 :: Context -> Word64 -> Sort -> IO AST
mkUnsignedInt64 :: Context -> Word64 -> Sort -> IO AST
mkUnsignedInt64 = (Ptr Z3_context -> CULLong -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Word64 -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CULLong -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_unsigned_int64

-------------------------------------------------
-- ** Helpers

-- | Create a numeral of an int, bit-vector, or finite-domain sort.
mkIntegral :: Integral a => Context -> a -> Sort -> IO AST
mkIntegral :: Context -> a -> Sort -> IO AST
mkIntegral c :: Context
c n :: a
n s :: Sort
s = Context -> String -> Sort -> IO AST
mkNumeral Context
c String
n_str Sort
s
  where n_str :: String
n_str = Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> Integer -> String
forall a b. (a -> b) -> a -> b
$ a -> Integer
forall a. Integral a => a -> Integer
toInteger a
n

-- | Create a numeral of sort /real/ from a 'Rational'.
mkRational :: Context -> Rational -> IO AST
mkRational :: Context -> Rational -> IO AST
mkRational = Context -> Rational -> IO AST
forall r. Real r => Context -> r -> IO AST
mkRealNum

-- | Create a numeral of sort /real/ from a 'Fixed'.
mkFixed :: HasResolution a => Context -> Fixed a -> IO AST
mkFixed :: Context -> Fixed a -> IO AST
mkFixed ctx :: Context
ctx = Context -> Rational -> IO AST
mkRational Context
ctx (Rational -> IO AST) -> (Fixed a -> Rational) -> Fixed a -> IO AST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fixed a -> Rational
forall a. Real a => a -> Rational
toRational

-- | Create a numeral of sort /real/ from a 'Real'.
mkRealNum :: Real r => Context -> r -> IO AST
mkRealNum :: Context -> r -> IO AST
mkRealNum c :: Context
c n :: r
n = Context -> String -> Sort -> IO AST
mkNumeral Context
c String
n_str (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkRealSort Context
c
  where r :: Rational
r     = r -> Rational
forall a. Real a => a -> Rational
toRational r
n
        r_n :: Integer
r_n   = Integer -> Integer
forall a. Integral a => a -> Integer
toInteger (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Rational -> Integer
forall a. Ratio a -> a
numerator Rational
r
        r_d :: Integer
r_d   = Integer -> Integer
forall a. Integral a => a -> Integer
toInteger (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Rational -> Integer
forall a. Ratio a -> a
denominator Rational
r
        n_str :: String
n_str = Integer -> String
forall a. Show a => a -> String
show Integer
r_n String -> ShowS
forall a. [a] -> [a] -> [a]
++ " / " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
r_d

-- | Create a numeral of sort /int/ from an 'Integer'.
mkInteger :: Context -> Integer -> IO AST
mkInteger :: Context -> Integer -> IO AST
mkInteger = Context -> Integer -> IO AST
forall a. Integral a => Context -> a -> IO AST
mkIntNum

-- | Create a numeral of sort /int/ from an 'Integral'.
mkIntNum :: Integral a => Context -> a -> IO AST
mkIntNum :: Context -> a -> IO AST
mkIntNum ctx :: Context
ctx n :: a
n = Context -> a -> Sort -> IO AST
forall a. Integral a => Context -> a -> Sort -> IO AST
mkIntegral Context
ctx a
n (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> IO Sort
mkIntSort Context
ctx

-- | Create a numeral of sort /Bit-vector/ from an 'Integer'.
mkBitvector :: Context -> Int      -- ^ bit-width
                       -> Integer  -- ^ integer value
                       -> IO AST
mkBitvector :: Context -> Int -> Integer -> IO AST
mkBitvector = Context -> Int -> Integer -> IO AST
forall i. Integral i => Context -> Int -> i -> IO AST
mkBvNum

-- | Create a numeral of sort /Bit-vector/ from an 'Integral'.
mkBvNum :: Integral i => Context -> Int    -- ^ bit-width
                                 -> i      -- ^ integer value
                                 -> IO AST
mkBvNum :: Context -> Int -> i -> IO AST
mkBvNum ctx :: Context
ctx s :: Int
s n :: i
n = Context -> i -> Sort -> IO AST
forall a. Integral a => Context -> a -> Sort -> IO AST
mkIntegral Context
ctx i
n (Sort -> IO AST) -> IO Sort -> IO AST
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> Int -> IO Sort
forall int. Integral int => Context -> int -> IO Sort
mkBvSort Context
ctx Int
s

---------------------------------------------------------------------
-- Quantifiers

-- | Create a pattern for quantifier instantiation.
--
-- Z3 uses pattern matching to instantiate quantifiers.
-- If a pattern is not provided for a quantifier, then Z3 will automatically
-- compute a set of patterns for it. However, for optimal performance,
-- the user should provide the patterns.
--
-- Patterns comprise a list of terms.
-- If the list comprises of more than one term, it is a called a multi-pattern.
--
-- In general, one can pass in a list of (multi-)patterns in the quantifier
-- constructor.
mkPattern :: Context
              -> NonEmpty AST        -- ^ Terms.
              -> IO Pattern
mkPattern :: Context -> NonEmpty AST -> IO Pattern
mkPattern c :: Context
c es :: NonEmpty AST
es = (Ptr Z3_context
 -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern))
-> Context
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern))
    -> IO (Ptr Z3_pattern))
-> IO Pattern
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern)
z3_mk_pattern Context
c (((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern))
  -> IO (Ptr Z3_pattern))
 -> IO Pattern)
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern))
    -> IO (Ptr Z3_pattern))
-> IO Pattern
forall a b. (a -> b) -> a -> b
$ [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_pattern))
-> IO (Ptr Z3_pattern)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen (NonEmpty AST -> [AST]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty AST
es)

-- | Create a bound variable.
--
-- Bound variables are indexed by de-Bruijn indices.
--
-- See <http://z3prover.github.io/api/html/group__capi.html#ga1d4da8849fca699b345322f8ee1fa31e>
mkBound :: Context
            -> Int    -- ^ de-Bruijn index.
            -> Sort
            -> IO AST
mkBound :: Context -> Int -> Sort -> IO AST
mkBound c :: Context
c i :: Int
i s :: Sort
s
  | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0    = (Ptr Z3_context -> CUInt -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Context -> Int -> Sort -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> CUInt -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_mk_bound Context
c Int
i Sort
s
  | Bool
otherwise = String -> IO AST
forall a. HasCallStack => String -> a
error "Z3.Base.mkBound: negative de-Bruijn index"

type MkZ3Quantifier = Ptr Z3_context -> CUInt
                      -> CUInt -> Ptr (Ptr Z3_pattern)
                      -> CUInt -> Ptr (Ptr Z3_sort) -> Ptr (Ptr Z3_symbol)
                      -> Ptr Z3_ast
                      -> IO (Ptr Z3_ast)

-- TODO: Allow the user to specify the quantifier weight!
marshalMkQ :: MkZ3Quantifier
          -> Context
          -> [Pattern]
          -> [Symbol]
          -> [Sort]
          -> AST
          -> IO AST
marshalMkQ :: MkZ3Quantifier
-> Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
marshalMkQ z3_mk_Q :: MkZ3Quantifier
z3_mk_Q ctx :: Context
ctx pats :: [Pattern]
pats x :: [Symbol]
x s :: [Sort]
s body :: AST
body = MkZ3Quantifier
-> Context
-> ((CUInt
     -> CUInt
     -> Ptr (Ptr Z3_pattern)
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr (Ptr Z3_symbol)
     -> Ptr Z3_ast
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal MkZ3Quantifier
z3_mk_Q Context
ctx (((CUInt
   -> CUInt
   -> Ptr (Ptr Z3_pattern)
   -> CUInt
   -> Ptr (Ptr Z3_sort)
   -> Ptr (Ptr Z3_symbol)
   -> Ptr Z3_ast
   -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CUInt
     -> CUInt
     -> Ptr (Ptr Z3_pattern)
     -> CUInt
     -> Ptr (Ptr Z3_sort)
     -> Ptr (Ptr Z3_symbol)
     -> Ptr Z3_ast
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CUInt
-> CUInt
-> Ptr (Ptr Z3_pattern)
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr (Ptr Z3_symbol)
-> Ptr Z3_ast
-> IO (Ptr Z3_ast)
f ->
  [Pattern]
-> (CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Pattern]
pats ((CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \n :: CUInt
n patsArr :: Ptr (Ptr Z3_pattern)
patsArr ->
  [Symbol]
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Symbol]
x ((Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \xArr :: Ptr (Ptr Z3_symbol)
xArr ->
  [Sort] -> (Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Sort]
s ((Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \sArr :: Ptr (Ptr Z3_sort)
sArr ->
  AST -> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
body ((Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \bodyPtr :: Ptr Z3_ast
bodyPtr ->
    CUInt
-> CUInt
-> Ptr (Ptr Z3_pattern)
-> CUInt
-> Ptr (Ptr Z3_sort)
-> Ptr (Ptr Z3_symbol)
-> Ptr Z3_ast
-> IO (Ptr Z3_ast)
f 0 CUInt
n Ptr (Ptr Z3_pattern)
patsArr CUInt
len Ptr (Ptr Z3_sort)
sArr Ptr (Ptr Z3_symbol)
xArr Ptr Z3_ast
bodyPtr
  where len :: CUInt
len
          | Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0        = String -> CUInt
forall a. HasCallStack => String -> a
error "Z3.Base.mkQuantifier:\
              \ quantifier with 0 bound variables"
          | Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [Symbol] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Symbol]
x = String -> CUInt
forall a. HasCallStack => String -> a
error "Z3.Base.mkQuantifier:\
              \ different number of symbols and sorts"
          | Bool
otherwise     = Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l
          where l :: Int
l = [Sort] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Sort]
s

-- | Create a forall formula.
--
-- The bound variables are de-Bruijn indices created using 'mkBound'.
--
-- Z3 applies the convention that the last element in /xs/ refers to the
-- variable with index 0, the second to last element of /xs/ refers to the
-- variable with index 1, etc.
mkForall :: Context
          -> [Pattern]  -- ^ Instantiation patterns (see 'mkPattern').
          -> [Symbol]   -- ^ Bound (quantified) variables /xs/.
          -> [Sort]     -- ^ Sorts of the bound variables.
          -> AST        -- ^ Body of the quantifier.
          -> IO AST
mkForall :: Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
mkForall = MkZ3Quantifier
-> Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
marshalMkQ MkZ3Quantifier
z3_mk_forall

-- | Create an exists formula.
--
-- Similar to 'mkForall'.
mkExists :: Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
mkExists :: Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
mkExists = MkZ3Quantifier
-> Context -> [Pattern] -> [Symbol] -> [Sort] -> AST -> IO AST
marshalMkQ MkZ3Quantifier
z3_mk_exists

-- TODO: Z3_mk_quantifier
-- TODO: Z3_mk_quantifier_ex

type MkZ3QuantifierConst = Ptr Z3_context
                           -> CUInt
                           -> CUInt
                           -> Ptr (Ptr Z3_app)
                           -> CUInt
                           -> Ptr (Ptr Z3_pattern)
                           -> Ptr Z3_ast
                           -> IO (Ptr Z3_ast)

marshalMkQConst :: MkZ3QuantifierConst
                  -> Context
                  -> [Pattern]
                  -> [App]
                  -> AST
                -> IO AST
marshalMkQConst :: MkZ3QuantifierConst
-> Context -> [Pattern] -> [App] -> AST -> IO AST
marshalMkQConst z3_mk_Q_const :: MkZ3QuantifierConst
z3_mk_Q_const ctx :: Context
ctx pats :: [Pattern]
pats apps :: [App]
apps body :: AST
body =
  MkZ3QuantifierConst
-> Context
-> ((CUInt
     -> CUInt
     -> Ptr (Ptr Z3_app)
     -> CUInt
     -> Ptr (Ptr Z3_pattern)
     -> Ptr Z3_ast
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal MkZ3QuantifierConst
z3_mk_Q_const Context
ctx (((CUInt
   -> CUInt
   -> Ptr (Ptr Z3_app)
   -> CUInt
   -> Ptr (Ptr Z3_pattern)
   -> Ptr Z3_ast
   -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CUInt
     -> CUInt
     -> Ptr (Ptr Z3_app)
     -> CUInt
     -> Ptr (Ptr Z3_pattern)
     -> Ptr Z3_ast
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CUInt
-> CUInt
-> Ptr (Ptr Z3_app)
-> CUInt
-> Ptr (Ptr Z3_pattern)
-> Ptr Z3_ast
-> IO (Ptr Z3_ast)
f ->
    [Pattern]
-> (CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Pattern]
pats ((CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_pattern) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \patsNum :: CUInt
patsNum patsArr :: Ptr (Ptr Z3_pattern)
patsArr ->
    [App] -> (Ptr (Ptr Z3_app) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray    [App]
apps ((Ptr (Ptr Z3_app) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_app) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \appsArr :: Ptr (Ptr Z3_app)
appsArr ->
    AST -> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
body ((Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \bodyPtr :: Ptr Z3_ast
bodyPtr ->
      CUInt
-> CUInt
-> Ptr (Ptr Z3_app)
-> CUInt
-> Ptr (Ptr Z3_pattern)
-> Ptr Z3_ast
-> IO (Ptr Z3_ast)
f 0 CUInt
len Ptr (Ptr Z3_app)
appsArr CUInt
patsNum Ptr (Ptr Z3_pattern)
patsArr Ptr Z3_ast
bodyPtr
  where len :: CUInt
len
          | Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0        = String -> CUInt
forall a. HasCallStack => String -> a
error "Z3.Base.mkQuantifierConst:\
              \ quantifier with 0 bound variables"
          | Bool
otherwise     = Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l
          where l :: Int
l = [App] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [App]
apps
-- TODO: Allow the user to specify the quantifier weight!

-- | Create a universal quantifier using a list of constants that will form the
-- set of bound variables.
mkForallConst :: Context
              -> [Pattern] -- ^ Instantiation patterns (see 'mkPattern').
              -> [App]     -- ^ Constants to be abstracted into bound variables.
              -> AST       -- ^ Quantifier body.
              -> IO AST
mkForallConst :: Context -> [Pattern] -> [App] -> AST -> IO AST
mkForallConst = MkZ3QuantifierConst
-> Context -> [Pattern] -> [App] -> AST -> IO AST
marshalMkQConst MkZ3QuantifierConst
z3_mk_forall_const

-- | Create a existential quantifier using a list of constants that will form
-- the set of bound variables.
mkExistsConst :: Context
              -> [Pattern] -- ^ Instantiation patterns (see 'mkPattern').
              -> [App]     -- ^ Constants to be abstracted into bound variables.
              -> AST       -- ^ Quantifier body.
              -> IO AST
mkExistsConst :: Context -> [Pattern] -> [App] -> AST -> IO AST
mkExistsConst = MkZ3QuantifierConst
-> Context -> [Pattern] -> [App] -> AST -> IO AST
marshalMkQConst MkZ3QuantifierConst
z3_mk_exists_const

-- TODO: Z3_mk_quantifier_const
-- TODO: Z3_mk_quantifier_const_ex

---------------------------------------------------------------------
-- Accessors

-- TODO: Z3_get_symbol_kind

-- TODO: Z3_get_symbol_int

-- | Return the symbol name.
getSymbolString :: Context -> Symbol -> IO String
getSymbolString :: Context -> Symbol -> IO String
getSymbolString = (Ptr Z3_context -> Ptr Z3_symbol -> IO CString)
-> Context -> Symbol -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_symbol -> IO CString
z3_get_symbol_string

-- TODO: Z3_get_sort_name

-- TODO: Z3_get_sort_id

-- TODO: Z3_sort_to_ast

-- TODO: Z3_is_eq_sort

-- | Return the sort kind of the given sort.
getSortKind :: Context -> Sort -> IO SortKind
getSortKind :: Context -> Sort -> IO SortKind
getSortKind ctx :: Context
ctx sort :: Sort
sort = CInt -> SortKind
toSortKind (CInt -> SortKind) -> IO CInt -> IO SortKind
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ptr Z3_context -> Ptr Z3_sort -> IO CInt)
-> Context -> Sort -> IO CInt
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO CInt
z3_get_sort_kind Context
ctx Sort
sort
  where toSortKind :: Z3_sort_kind -> SortKind
        toSortKind :: CInt -> SortKind
toSortKind k :: CInt
k
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_uninterpreted_sort      = SortKind
Z3_UNINTERPRETED_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_bool_sort               = SortKind
Z3_BOOL_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_int_sort                = SortKind
Z3_INT_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_real_sort               = SortKind
Z3_REAL_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_bv_sort                 = SortKind
Z3_BV_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_array_sort              = SortKind
Z3_ARRAY_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_datatype_sort           = SortKind
Z3_DATATYPE_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_relation_sort           = SortKind
Z3_RELATION_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_finite_domain_sort      = SortKind
Z3_FINITE_DOMAIN_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_floating_point_sort     = SortKind
Z3_FLOATING_POINT_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_rounding_mode_sort      = SortKind
Z3_ROUNDING_MODE_SORT
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_unknown_sort            = SortKind
Z3_UNKNOWN_SORT
          | Bool
otherwise                 =
              String -> SortKind
forall a. HasCallStack => String -> a
error "Z3.Base.getSortKind: unknown `Z3_sort_kind'"

-- | Return the size of the given bit-vector sort.
getBvSortSize :: Context -> Sort -> IO Int
getBvSortSize :: Context -> Sort -> IO Int
getBvSortSize = (Ptr Z3_context -> Ptr Z3_sort -> IO CUInt)
-> Context -> Sort -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO CUInt
z3_get_bv_sort_size

-- TODO: Z3_get_finite_domain_sort_size

-- TODO: Z3_get_array_sort_size

getArraySortDomain :: Context -> Sort -> IO Sort
getArraySortDomain :: Context -> Sort -> IO Sort
getArraySortDomain = (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort))
-> Context -> Sort -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort)
z3_get_array_sort_domain

getArraySortRange :: Context -> Sort -> IO Sort
getArraySortRange :: Context -> Sort -> IO Sort
getArraySortRange = (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort))
-> Context -> Sort -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_sort)
z3_get_array_sort_range

-- TODO: Z3_get_tuple_sort_mk_decl

-- TODO: Z3_get_tuple_sort_num_fields

-- TODO: Z3_get_tuple_sort_field_decl

-- | Get list of constructors for datatype.
getDatatypeSortConstructors :: Context
                            -> Sort           -- ^ Datatype sort.
                            -> IO [FuncDecl]  -- ^ Constructor declarations.
getDatatypeSortConstructors :: Context -> Sort -> IO [FuncDecl]
getDatatypeSortConstructors c :: Context
c dtSort :: Sort
dtSort =
  Context -> (Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl]
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl])
-> (Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Sort -> (Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl]
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Sort
dtSort ((Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl])
-> (Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \dtSortPtr :: Ptr Z3_sort
dtSortPtr -> do
    CUInt
numCons <- Ptr Z3_context -> IO CUInt -> IO CUInt
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (IO CUInt -> IO CUInt) -> IO CUInt -> IO CUInt
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context -> Ptr Z3_sort -> IO CUInt
z3_get_datatype_sort_num_constructors Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr
    (CUInt -> IO FuncDecl) -> [CUInt] -> IO [FuncDecl]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (Ptr Z3_sort -> CUInt -> IO FuncDecl
forall h.
Marshal h (Ptr Z3_func_decl) =>
Ptr Z3_sort -> CUInt -> IO h
getConstructor Ptr Z3_sort
dtSortPtr) [0..(CUInt
numConsCUInt -> CUInt -> CUInt
forall a. Num a => a -> a -> a
-1)]
  where
    getConstructor :: Ptr Z3_sort -> CUInt -> IO h
getConstructor dtSortPtr :: Ptr Z3_sort
dtSortPtr idx :: CUInt
idx =
      Context -> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h)
-> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_sort -> CUInt -> IO (Ptr Z3_func_decl)
z3_get_datatype_sort_constructor Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr CUInt
idx

-- | Get list of recognizers for datatype.
getDatatypeSortRecognizers :: Context
                           -> Sort           -- ^ Datatype sort.
                           -> IO [FuncDecl]  -- ^ Constructor recognizers.
getDatatypeSortRecognizers :: Context -> Sort -> IO [FuncDecl]
getDatatypeSortRecognizers c :: Context
c dtSort :: Sort
dtSort =
  Context -> (Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl]
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl])
-> (Ptr Z3_context -> IO [FuncDecl]) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Sort -> (Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl]
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Sort
dtSort ((Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl])
-> (Ptr Z3_sort -> IO [FuncDecl]) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \dtSortPtr :: Ptr Z3_sort
dtSortPtr -> do
    CUInt
numCons <- Ptr Z3_context -> IO CUInt -> IO CUInt
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (IO CUInt -> IO CUInt) -> IO CUInt -> IO CUInt
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context -> Ptr Z3_sort -> IO CUInt
z3_get_datatype_sort_num_constructors Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr
    (CUInt -> IO FuncDecl) -> [CUInt] -> IO [FuncDecl]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (Ptr Z3_sort -> CUInt -> IO FuncDecl
forall h.
Marshal h (Ptr Z3_func_decl) =>
Ptr Z3_sort -> CUInt -> IO h
getRecognizer Ptr Z3_sort
dtSortPtr) [0..(CUInt
numConsCUInt -> CUInt -> CUInt
forall a. Num a => a -> a -> a
-1)]
  where
    getRecognizer :: Ptr Z3_sort -> CUInt -> IO h
getRecognizer dtSortPtr :: Ptr Z3_sort
dtSortPtr idx :: CUInt
idx =
      Context -> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h)
-> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_sort -> CUInt -> IO (Ptr Z3_func_decl)
z3_get_datatype_sort_recognizer Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr CUInt
idx

-- | Get list of accessors for the datatype constructor.
getDatatypeSortConstructorAccessors :: Context
                                    -> Sort             -- ^ Datatype sort.
                                    -> IO [[FuncDecl]]  -- ^ Constructor accessors.
getDatatypeSortConstructorAccessors :: Context -> Sort -> IO [[FuncDecl]]
getDatatypeSortConstructorAccessors c :: Context
c dtSort :: Sort
dtSort =
  Context -> (Ptr Z3_context -> IO [[FuncDecl]]) -> IO [[FuncDecl]]
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c ((Ptr Z3_context -> IO [[FuncDecl]]) -> IO [[FuncDecl]])
-> (Ptr Z3_context -> IO [[FuncDecl]]) -> IO [[FuncDecl]]
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Sort -> (Ptr Z3_sort -> IO [[FuncDecl]]) -> IO [[FuncDecl]]
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Sort
dtSort ((Ptr Z3_sort -> IO [[FuncDecl]]) -> IO [[FuncDecl]])
-> (Ptr Z3_sort -> IO [[FuncDecl]]) -> IO [[FuncDecl]]
forall a b. (a -> b) -> a -> b
$ \dtSortPtr :: Ptr Z3_sort
dtSortPtr -> do
    CUInt
numCons <- Ptr Z3_context -> IO CUInt -> IO CUInt
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (IO CUInt -> IO CUInt) -> IO CUInt -> IO CUInt
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context -> Ptr Z3_sort -> IO CUInt
z3_get_datatype_sort_num_constructors Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr
    (CUInt -> IO [FuncDecl]) -> [CUInt] -> IO [[FuncDecl]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (Ptr Z3_sort -> CUInt -> IO [FuncDecl]
forall b.
Marshal b (Ptr Z3_func_decl) =>
Ptr Z3_sort -> CUInt -> IO [b]
getAccessors Ptr Z3_sort
dtSortPtr) [0..(CUInt
numConsCUInt -> CUInt -> CUInt
forall a. Num a => a -> a -> a
-1)]
  where
    getConstructor :: Ptr Z3_sort -> CUInt -> IO (Ptr Z3_func_decl)
getConstructor dtSortPtr :: Ptr Z3_sort
dtSortPtr idx_c :: CUInt
idx_c =
      Context
-> (Ptr Z3_context -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
c ((Ptr Z3_context -> IO (Ptr Z3_func_decl))
 -> IO (Ptr Z3_func_decl))
-> (Ptr Z3_context -> IO (Ptr Z3_func_decl))
-> IO (Ptr Z3_func_decl)
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_sort -> CUInt -> IO (Ptr Z3_func_decl)
z3_get_datatype_sort_constructor Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr CUInt
idx_c

    getAccessors :: Ptr Z3_sort -> CUInt -> IO [b]
getAccessors dtSortPtr :: Ptr Z3_sort
dtSortPtr idx_c :: CUInt
idx_c = do
      Ptr Z3_func_decl
consPtr <- Ptr Z3_sort -> CUInt -> IO (Ptr Z3_func_decl)
getConstructor Ptr Z3_sort
dtSortPtr CUInt
idx_c
      CUInt
numAs <- Context -> (Ptr Z3_context -> IO CUInt) -> IO CUInt
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO CUInt) -> IO CUInt)
-> (Ptr Z3_context -> IO CUInt) -> IO CUInt
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_func_decl -> IO CUInt
z3_get_arity Ptr Z3_context
cPtr Ptr Z3_func_decl
consPtr
      if CUInt
numAs CUInt -> CUInt -> Bool
forall a. Ord a => a -> a -> Bool
> 0  -- NB: (0::CUInt) - 1 == 0
        then (CUInt -> IO b) -> [CUInt] -> IO [b]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (\idx_a :: CUInt
idx_a -> Ptr Z3_sort -> CUInt -> CUInt -> IO b
forall h.
Marshal h (Ptr Z3_func_decl) =>
Ptr Z3_sort -> CUInt -> CUInt -> IO h
getAccessors' Ptr Z3_sort
dtSortPtr CUInt
idx_c CUInt
idx_a) [0..(CUInt
numAs CUInt -> CUInt -> CUInt
forall a. Num a => a -> a -> a
- 1)]
        else [b] -> IO [b]
forall (m :: * -> *) a. Monad m => a -> m a
return []

    getAccessors' :: Ptr Z3_sort -> CUInt -> CUInt -> IO h
getAccessors' dtSortPtr :: Ptr Z3_sort
dtSortPtr idx_c :: CUInt
idx_c idx_a :: CUInt
idx_a =
      Context -> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h)
-> (Ptr Z3_context -> IO (Ptr Z3_func_decl)) -> IO h
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context
-> Ptr Z3_sort -> CUInt -> CUInt -> IO (Ptr Z3_func_decl)
z3_get_datatype_sort_constructor_accessor Ptr Z3_context
cPtr Ptr Z3_sort
dtSortPtr CUInt
idx_c CUInt
idx_a

-- TODO: Z3_get_relation_arity

-- TODO: Z3_get_relation_column

-- TODO: Z3_func_decl_to_ast

-- TODO: Z3_is_eq_func_decl

-- TODO: Z3_get_func_decl_id

-- | Return the constant declaration name as a symbol.
getDeclName :: Context -> FuncDecl -> IO Symbol
getDeclName :: Context -> FuncDecl -> IO Symbol
getDeclName c :: Context
c decl :: FuncDecl
decl = FuncDecl -> (Ptr Z3_func_decl -> IO Symbol) -> IO Symbol
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c FuncDecl
decl ((Ptr Z3_func_decl -> IO Symbol) -> IO Symbol)
-> (Ptr Z3_func_decl -> IO Symbol) -> IO Symbol
forall a b. (a -> b) -> a -> b
$ \declPtr :: Ptr Z3_func_decl
declPtr ->
  Ptr Z3_symbol -> Symbol
Symbol (Ptr Z3_symbol -> Symbol) -> IO (Ptr Z3_symbol) -> IO Symbol
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context
-> (Ptr Z3_context -> IO (Ptr Z3_symbol)) -> IO (Ptr Z3_symbol)
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c (\cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_symbol)
z3_get_decl_name Ptr Z3_context
cPtr Ptr Z3_func_decl
declPtr)

-- TODO: Z3_get_decl_kind

-- TODO: Z3_get_domain_size

-- | Returns the number of parameters of the given declaration
getArity :: Context -> FuncDecl -> IO Int
getArity :: Context -> FuncDecl -> IO Int
getArity = (Ptr Z3_context -> Ptr Z3_func_decl -> IO CUInt)
-> Context -> FuncDecl -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_decl -> IO CUInt
z3_get_arity

-- | Returns the sort of the i-th parameter of the given function declaration
getDomain :: Context
             -> FuncDecl         -- ^ A function declaration
             -> Int              -- ^ i
             -> IO Sort
getDomain :: Context -> FuncDecl -> Int -> IO Sort
getDomain = (Ptr Z3_context -> Ptr Z3_func_decl -> CUInt -> IO (Ptr Z3_sort))
-> Context -> FuncDecl -> Int -> IO Sort
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_func_decl -> CUInt -> IO (Ptr Z3_sort)
z3_get_domain

-- | Returns the range of the given declaration.
getRange :: Context -> FuncDecl -> IO Sort
getRange :: Context -> FuncDecl -> IO Sort
getRange = (Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_sort))
-> Context -> FuncDecl -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_sort)
z3_get_range

-- TODO: Z3_get_decl_num_parameters

-- TODO: Z3_get_decl_parameter_kind

-- TODO: Z3_get_decl_int_parameter

-- TODO: Z3_get_decl_double_parameter

-- TODO: Z3_get_decl_symbol_parameter

-- TODO: Z3_get_decl_sort_parameter

-- TODO: Z3_get_decl_ast_parameter

-- TODO: Z3_get_decl_func_decl_parameter

-- TODO: Z3_get_decl_rational_parameter

-- | Convert an app into AST. This is just type casting.
appToAst :: Context -> App -> IO AST
appToAst :: Context -> App -> IO AST
appToAst = (Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast))
-> Context -> App -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast)
z3_app_to_ast

-- | Return the declaration of a constant or function application.
getAppDecl :: Context -> App -> IO FuncDecl
getAppDecl :: Context -> App -> IO FuncDecl
getAppDecl = (Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_func_decl))
-> Context -> App -> IO FuncDecl
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_func_decl)
z3_get_app_decl

-- | Return the number of argument of an application. If t is an constant, then the number of arguments is 0.
getAppNumArgs :: Context -> App -> IO Int
getAppNumArgs :: Context -> App -> IO Int
getAppNumArgs = (Ptr Z3_context -> Ptr Z3_app -> IO CUInt)
-> Context -> App -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_app -> IO CUInt
z3_get_app_num_args

-- | Return the i-th argument of the given application.
getAppArg :: Context -> App -> Int -> IO AST
getAppArg :: Context -> App -> Int -> IO AST
getAppArg = (Ptr Z3_context -> Ptr Z3_app -> CUInt -> IO (Ptr Z3_ast))
-> Context -> App -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_app -> CUInt -> IO (Ptr Z3_ast)
z3_get_app_arg

-- | Return a list of all the arguments of the given application.
getAppArgs :: Context -> App -> IO [AST]
getAppArgs :: Context -> App -> IO [AST]
getAppArgs ctx :: Context
ctx a :: App
a = do
  Int
n <- Context -> App -> IO Int
getAppNumArgs Context
ctx App
a
  [Int] -> (Int -> IO AST) -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] (Context -> App -> Int -> IO AST
getAppArg Context
ctx App
a)

-- TODO: Z3_is_eq_ast

-- TODO: Z3_get_ast_id

-- TODO: Z3_get_ast_hash

-- | Return the sort of an AST node.
getSort :: Context -> AST -> IO Sort
getSort :: Context -> AST -> IO Sort
getSort = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_sort))
-> Context -> AST -> IO Sort
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_sort)
z3_get_sort

-- TODO: Z3_is_well_sorted

-- TODO: fix doc
-- | Return Z3_L_TRUE if a is true, Z3_L_FALSE if it is false, and Z3_L_UNDEF
-- otherwise.
getBoolValue :: Context -> AST -> IO (Maybe Bool)
getBoolValue :: Context -> AST -> IO (Maybe Bool)
getBoolValue c :: Context
c a :: AST
a = AST -> (Ptr Z3_ast -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
a ((Ptr Z3_ast -> IO (Maybe Bool)) -> IO (Maybe Bool))
-> (Ptr Z3_ast -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ \astPtr :: Ptr Z3_ast
astPtr ->
  Z3_lbool -> Maybe Bool
castLBool (Z3_lbool -> Maybe Bool) -> IO Z3_lbool -> IO (Maybe Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> (Ptr Z3_context -> IO Z3_lbool) -> IO Z3_lbool
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
c (\cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_ast -> IO Z3_lbool
z3_get_bool_value Ptr Z3_context
cPtr Ptr Z3_ast
astPtr)
  where castLBool :: Z3_lbool -> Maybe Bool
        castLBool :: Z3_lbool -> Maybe Bool
castLBool lb :: Z3_lbool
lb
          | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_true  = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
          | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_false = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
          | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_undef = Maybe Bool
forall a. Maybe a
Nothing
          | Bool
otherwise        =
              String -> Maybe Bool
forall a. HasCallStack => String -> a
error "Z3.Base.castLBool: illegal `Z3_lbool' value"

-- | Return the kind of the given AST.
getAstKind :: Context -> AST -> IO ASTKind
getAstKind :: Context -> AST -> IO ASTKind
getAstKind ctx :: Context
ctx ast :: AST
ast = CInt -> ASTKind
toAstKind (CInt -> ASTKind) -> IO CInt -> IO ASTKind
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Ptr Z3_context -> Ptr Z3_ast -> IO CInt)
-> Context -> AST -> IO CInt
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CInt
z3_get_ast_kind Context
ctx AST
ast
  where toAstKind :: Z3_ast_kind -> ASTKind
        toAstKind :: CInt -> ASTKind
toAstKind k :: CInt
k
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_numeral_ast       = ASTKind
Z3_NUMERAL_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_app_ast           = ASTKind
Z3_APP_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_var_ast           = ASTKind
Z3_VAR_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_quantifier_ast    = ASTKind
Z3_QUANTIFIER_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_sort_ast          = ASTKind
Z3_SORT_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_func_decl_ast     = ASTKind
Z3_FUNC_DECL_AST
          | CInt
k CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_unknown_ast       = ASTKind
Z3_UNKNOWN_AST
          | Bool
otherwise                 =
              String -> ASTKind
forall a. HasCallStack => String -> a
error "Z3.Base.getAstKind: unknown `Z3_ast_kind'"

-- | Return True if an ast is APP_AST, False otherwise.
isApp :: Context -> AST -> IO Bool
isApp :: Context -> AST -> IO Bool
isApp = (Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool)
-> Context -> AST -> IO Bool
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool
z3_is_app

-- TODO: Z3_is_numeral_ast

-- TODO: Z3_is_algebraic_number

-- | Convert an ast into an APP_AST. This is just type casting.
toApp :: Context -> AST -> IO App
toApp :: Context -> AST -> IO App
toApp = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_app))
-> Context -> AST -> IO App
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_app)
z3_to_app

-- TODO: Z3_to_func_decl

-- | Return numeral value, as a string of a numeric constant term.
getNumeralString :: Context -> AST -> IO String
getNumeralString :: Context -> AST -> IO String
getNumeralString = (Ptr Z3_context -> Ptr Z3_ast -> IO CString)
-> Context -> AST -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CString
z3_get_numeral_string

-- TODO: Z3_get_numeral_decimal_string

-- TODO: Z3_get_numerator

-- TODO: Z3_get_denominator

-- TODO: Z3_get_numeral_small

-- TODO: Z3_get_numeral_int

-- TODO: Z3_get_numeral_int

-- TODO: Z3_get_numeral_uint

-- TODO: Z3_get_numeral_uint64

-- TODO: Z3_get_numeral_int64

-- TODO: Z3_get_numeral_rational_int64

-- TODO: Z3_get_algebraic_number_lower

-- TODO: Z3_get_algebraic_number_upper

-- TODO: Z3_pattern_to_ast

-- TODO: Z3_get_pattern_num_terms

-- TODO: Z3_get_pattern

getIndexValue :: Context -> AST -> IO Int
getIndexValue :: Context -> AST -> IO Int
getIndexValue = (Ptr Z3_context -> Ptr Z3_ast -> IO CUInt)
-> Context -> AST -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CUInt
z3_get_index_value

isQuantifierForall :: Context -> AST -> IO Bool
isQuantifierForall :: Context -> AST -> IO Bool
isQuantifierForall = (Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool)
-> Context -> AST -> IO Bool
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool
z3_is_quantifier_forall

isQuantifierExists :: Context -> AST -> IO Bool
isQuantifierExists :: Context -> AST -> IO Bool
isQuantifierExists ctx :: Context
ctx = (Bool -> Bool) -> IO Bool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not (IO Bool -> IO Bool) -> (AST -> IO Bool) -> AST -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context -> AST -> IO Bool
isQuantifierForall Context
ctx

getQuantifierWeight :: Context -> AST -> IO Int
getQuantifierWeight :: Context -> AST -> IO Int
getQuantifierWeight = (Ptr Z3_context -> Ptr Z3_ast -> IO CUInt)
-> Context -> AST -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CUInt
z3_get_quantifier_weight

getQuantifierNumPatterns :: Context -> AST -> IO Int
getQuantifierNumPatterns :: Context -> AST -> IO Int
getQuantifierNumPatterns = (Ptr Z3_context -> Ptr Z3_ast -> IO CUInt)
-> Context -> AST -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CUInt
z3_get_quantifier_num_patterns

getQuantifierPatternAST :: Context -> AST -> Int -> IO AST
getQuantifierPatternAST :: Context -> AST -> Int -> IO AST
getQuantifierPatternAST = (Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_ast))
-> Context -> AST -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_ast)
z3_get_quantifier_pattern_ast

getQuantifierPatterns :: Context -> AST -> IO [AST]
getQuantifierPatterns :: Context -> AST -> IO [AST]
getQuantifierPatterns ctx :: Context
ctx a :: AST
a = do
  Int
n <- Context -> AST -> IO Int
getQuantifierNumPatterns Context
ctx AST
a
  [Int] -> (Int -> IO AST) -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] (Context -> AST -> Int -> IO AST
getQuantifierPatternAST Context
ctx AST
a)

getQuantifierNumNoPatterns :: Context -> AST -> IO Int
getQuantifierNumNoPatterns :: Context -> AST -> IO Int
getQuantifierNumNoPatterns = (Ptr Z3_context -> Ptr Z3_ast -> IO CUInt)
-> Context -> AST -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CUInt
z3_get_quantifier_num_no_patterns

getQuantifierNoPatternAST :: Context -> AST -> Int -> IO AST
getQuantifierNoPatternAST :: Context -> AST -> Int -> IO AST
getQuantifierNoPatternAST = (Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_ast))
-> Context -> AST -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_ast)
z3_get_quantifier_no_pattern_ast

getQuantifierNoPatterns :: Context -> AST -> IO [AST]
getQuantifierNoPatterns :: Context -> AST -> IO [AST]
getQuantifierNoPatterns ctx :: Context
ctx a :: AST
a = do
  Int
n <- Context -> AST -> IO Int
getQuantifierNumNoPatterns Context
ctx AST
a
  [Int] -> (Int -> IO AST) -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] (Context -> AST -> Int -> IO AST
getQuantifierNoPatternAST Context
ctx AST
a)

getQuantifierNumBound :: Context -> AST -> IO Int
getQuantifierNumBound :: Context -> AST -> IO Int
getQuantifierNumBound = (Ptr Z3_context -> Ptr Z3_ast -> IO CUInt)
-> Context -> AST -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CUInt
z3_get_quantifier_num_bound

getQuantifierBoundName :: Context -> AST -> Int -> IO Symbol
getQuantifierBoundName :: Context -> AST -> Int -> IO Symbol
getQuantifierBoundName = (Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_symbol))
-> Context -> AST -> Int -> IO Symbol
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_symbol)
z3_get_quantifier_bound_name

getQuantifierBoundSort :: Context -> AST -> Int -> IO Sort
getQuantifierBoundSort :: Context -> AST -> Int -> IO Sort
getQuantifierBoundSort = (Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_sort))
-> Context -> AST -> Int -> IO Sort
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> CUInt -> IO (Ptr Z3_sort)
z3_get_quantifier_bound_sort

getQuantifierBoundVars :: Context -> AST -> IO [AST]
getQuantifierBoundVars :: Context -> AST -> IO [AST]
getQuantifierBoundVars ctx :: Context
ctx a :: AST
a = do
  Int
n <- Context -> AST -> IO Int
getQuantifierNumBound Context
ctx AST
a
  [Int] -> (Int -> IO AST) -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] ((Int -> IO AST) -> IO [AST]) -> (Int -> IO AST) -> IO [AST]
forall a b. (a -> b) -> a -> b
$ \i :: Int
i -> do
    Symbol
b <- Context -> AST -> Int -> IO Symbol
getQuantifierBoundName Context
ctx AST
a Int
i
    Sort
s <- Context -> AST -> Int -> IO Sort
getQuantifierBoundSort Context
ctx AST
a Int
i
    Context -> Symbol -> Sort -> IO AST
mkVar Context
ctx Symbol
b Sort
s

getQuantifierBody :: Context -> AST -> IO AST
getQuantifierBody :: Context -> AST -> IO AST
getQuantifierBody = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_get_quantifier_body

simplify :: Context -> AST -> IO AST
simplify :: Context -> AST -> IO AST
simplify = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast))
-> Context -> AST -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_ast)
z3_simplify

simplifyEx :: Context -> AST -> Params -> IO AST
simplifyEx :: Context -> AST -> Params -> IO AST
simplifyEx = (Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_params -> IO (Ptr Z3_ast))
-> Context -> AST -> Params -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_ast -> Ptr Z3_params -> IO (Ptr Z3_ast)
z3_simplify_ex

-- TODO: Z3_simplify_get_help

-- TODO: Z3_simplify_get_param_descrs

-------------------------------------------------
-- ** Helpers

-- | Read a 'Bool' value from an 'AST'
getBool :: Context -> AST -> IO Bool
getBool :: Context -> AST -> IO Bool
getBool c :: Context
c a :: AST
a = Maybe Bool -> Bool
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Bool -> Bool) -> IO (Maybe Bool) -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> AST -> IO (Maybe Bool)
getBoolValue Context
c AST
a
  -- TODO: throw an custom error if Nothing?
  -- TODO: Refactor castLBool?

-- | Read an 'Integer' value from an 'AST'
getInt :: Context -> AST -> IO Integer
getInt :: Context -> AST -> IO Integer
getInt c :: Context
c a :: AST
a = String -> Integer
forall a. Read a => String -> a
read (String -> Integer) -> IO String -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> AST -> IO String
getNumeralString Context
c AST
a

-- | Read a 'Rational' value from an 'AST'
getReal :: Context -> AST -> IO Rational
getReal :: Context -> AST -> IO Rational
getReal c :: Context
c a :: AST
a = String -> Rational
parse (String -> Rational) -> IO String -> IO Rational
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> AST -> IO String
getNumeralString Context
c AST
a
  where parse :: String -> Rational
        parse :: String -> Rational
parse s :: String
s
          | [(i :: Integer
i, sj :: String
sj)] <- ReadS Integer
forall a. Read a => ReadS a
reads String
s = Integer
i Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% String -> Integer
parseDen ((Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ' ') String
sj)
          | Bool
otherwise            = String -> Rational
forall a. HasCallStack => String -> a
error "Z3.Base.getReal: no parse"

        parseDen :: String -> Integer
        parseDen :: String -> Integer
parseDen ""       = 1
        parseDen ('/':sj :: String
sj) = String -> Integer
forall a. Read a => String -> a
read String
sj
        parseDen _        = String -> Integer
forall a. HasCallStack => String -> a
error "Z3.Base.getReal: no parse"

-- | Read the 'Integer' value from an 'AST' of sort /bit-vector/.
--
-- See 'mkBv2int'.
getBv :: Context -> AST
                 -> Bool  -- ^ signed?
                 -> IO Integer
getBv :: Context -> AST -> Bool -> IO Integer
getBv c :: Context
c a :: AST
a signed :: Bool
signed = Context -> AST -> IO Integer
getInt Context
c (AST -> IO Integer) -> IO AST -> IO Integer
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Context -> AST -> Bool -> IO AST
mkBv2int Context
c AST
a Bool
signed

---------------------------------------------------------------------
-- Modifiers

-- TODO Modifiers

substituteVars :: Context -> AST -> [AST] -> IO AST
substituteVars :: Context -> AST -> [AST] -> IO AST
substituteVars ctx :: Context
ctx a :: AST
a vars :: [AST]
vars =
  (Ptr Z3_context
 -> Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> ((Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
z3_substitute_vars Context
ctx (((Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f ->
    AST -> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
a ((Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_ast -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \aPtr :: Ptr Z3_ast
aPtr ->
    [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
vars ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \varsNum :: CUInt
varsNum varsArr :: Ptr (Ptr Z3_ast)
varsArr ->
      Ptr Z3_ast -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f Ptr Z3_ast
aPtr CUInt
varsNum Ptr (Ptr Z3_ast)
varsArr

---------------------------------------------------------------------
-- Models

-- | Evaluate an AST node in the given model.
--
-- The evaluation may fail for the following reasons:
--
--     * /t/ contains a quantifier.
--     * the model /m/ is partial.
--     * /t/ is type incorrect.
modelEval :: Context
            -> Model  -- ^ Model /m/.
            -> AST    -- ^ Expression to evaluate /t/.
            -> Bool   -- ^ Model completion?
            -> IO (Maybe AST)
modelEval :: Context -> Model -> AST -> Bool -> IO (Maybe AST)
modelEval ctx :: Context
ctx m :: Model
m a :: AST
a b :: Bool
b =
  Context -> (Ptr Z3_context -> IO (Maybe AST)) -> IO (Maybe AST)
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
ctx ((Ptr Z3_context -> IO (Maybe AST)) -> IO (Maybe AST))
-> (Ptr Z3_context -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \ctxPtr :: Ptr Z3_context
ctxPtr ->
  (Ptr (Ptr Z3_ast) -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Z3_ast) -> IO (Maybe AST)) -> IO (Maybe AST))
-> (Ptr (Ptr Z3_ast) -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \aptr2 :: Ptr (Ptr Z3_ast)
aptr2 ->
    AST -> (Ptr Z3_ast -> IO (Maybe AST)) -> IO (Maybe AST)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
a ((Ptr Z3_ast -> IO (Maybe AST)) -> IO (Maybe AST))
-> (Ptr Z3_ast -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \astPtr :: Ptr Z3_ast
astPtr ->
    Model -> (Ptr Z3_model -> IO (Maybe AST)) -> IO (Maybe AST)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Model
m ((Ptr Z3_model -> IO (Maybe AST)) -> IO (Maybe AST))
-> (Ptr Z3_model -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \mPtr :: Ptr Z3_model
mPtr ->
    Bool -> (Z3_bool -> IO (Maybe AST)) -> IO (Maybe AST)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Bool
b ((Z3_bool -> IO (Maybe AST)) -> IO (Maybe AST))
-> (Z3_bool -> IO (Maybe AST)) -> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \b' :: Z3_bool
b' ->
    Ptr Z3_context -> IO Z3_bool -> IO Z3_bool
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
ctxPtr
      (Ptr Z3_context
-> Ptr Z3_model
-> Ptr Z3_ast
-> Z3_bool
-> Ptr (Ptr Z3_ast)
-> IO Z3_bool
z3_model_eval Ptr Z3_context
ctxPtr Ptr Z3_model
mPtr Ptr Z3_ast
astPtr Z3_bool
b' Ptr (Ptr Z3_ast)
aptr2) IO Z3_bool -> (Z3_bool -> IO (Maybe AST)) -> IO (Maybe AST)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr (Ptr Z3_ast) -> Bool -> IO (Maybe AST)
peekAST Ptr (Ptr Z3_ast)
aptr2 (Bool -> IO (Maybe AST))
-> (Z3_bool -> Bool) -> Z3_bool -> IO (Maybe AST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Z3_bool -> Bool
toBool
  where peekAST :: Ptr (Ptr Z3_ast) -> Bool -> IO (Maybe AST)
        peekAST :: Ptr (Ptr Z3_ast) -> Bool -> IO (Maybe AST)
peekAST _p :: Ptr (Ptr Z3_ast)
_p False = Maybe AST -> IO (Maybe AST)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe AST
forall a. Maybe a
Nothing
        peekAST  p :: Ptr (Ptr Z3_ast)
p True  = (AST -> Maybe AST) -> IO AST -> IO (Maybe AST)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap AST -> Maybe AST
forall a. a -> Maybe a
Just (IO AST -> IO (Maybe AST))
-> (Ptr Z3_ast -> IO AST) -> Ptr Z3_ast -> IO (Maybe AST)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context -> Ptr Z3_ast -> IO AST
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
ctx (Ptr Z3_ast -> IO (Maybe AST)) -> IO (Ptr Z3_ast) -> IO (Maybe AST)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Z3_ast)
p

getConstInterp :: Context -> Model -> FuncDecl -> IO (Maybe AST)
getConstInterp :: Context -> Model -> FuncDecl -> IO (Maybe AST)
getConstInterp ctx :: Context
ctx m :: Model
m fd :: FuncDecl
fd = (Ptr Z3_context
 -> Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
-> Context
-> ((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO (Maybe AST)
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast)
z3_model_get_const_interp Context
ctx (((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO (Maybe AST))
-> ((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO (Maybe AST)
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast)
f ->
  Model -> (Ptr Z3_model -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Model
m ((Ptr Z3_model -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_model -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \mPtr :: Ptr Z3_model
mPtr ->
  FuncDecl
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c FuncDecl
fd ((Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr Z3_func_decl -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \fdPtr :: Ptr Z3_func_decl
fdPtr ->
    Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_ast)
f Ptr Z3_model
mPtr Ptr Z3_func_decl
fdPtr

hasInterp :: Context -> Model -> FuncDecl -> IO Bool
hasInterp :: Context -> Model -> FuncDecl -> IO Bool
hasInterp = (Ptr Z3_context -> Ptr Z3_model -> Ptr Z3_func_decl -> IO Z3_bool)
-> Context -> Model -> FuncDecl -> IO Bool
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_model -> Ptr Z3_func_decl -> IO Z3_bool
z3_model_has_interp

numConsts :: Context -> Model -> IO Word
numConsts :: Context -> Model -> IO Word
numConsts = (Ptr Z3_context -> Ptr Z3_model -> IO CUInt)
-> Context -> Model -> IO Word
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_model -> IO CUInt
z3_model_get_num_consts

numFuncs :: Context -> Model -> IO Word
numFuncs :: Context -> Model -> IO Word
numFuncs = (Ptr Z3_context -> Ptr Z3_model -> IO CUInt)
-> Context -> Model -> IO Word
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_model -> IO CUInt
z3_model_get_num_funcs

getConstDecl :: Context -> Model -> Word -> IO FuncDecl
getConstDecl :: Context -> Model -> Word -> IO FuncDecl
getConstDecl = (Ptr Z3_context -> Ptr Z3_model -> CUInt -> IO (Ptr Z3_func_decl))
-> Context -> Model -> Word -> IO FuncDecl
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_model -> CUInt -> IO (Ptr Z3_func_decl)
z3_model_get_const_decl

getFuncDecl :: Context -> Model -> Word -> IO FuncDecl
getFuncDecl :: Context -> Model -> Word -> IO FuncDecl
getFuncDecl = (Ptr Z3_context -> Ptr Z3_model -> CUInt -> IO (Ptr Z3_func_decl))
-> Context -> Model -> Word -> IO FuncDecl
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_model -> CUInt -> IO (Ptr Z3_func_decl)
z3_model_get_func_decl

getConsts :: Context -> Model -> IO [FuncDecl]
getConsts :: Context -> Model -> IO [FuncDecl]
getConsts ctx :: Context
ctx m :: Model
m = do
  Word
n <- Context -> Model -> IO Word
numConsts Context
ctx Model
m
  [Word] -> (Word -> IO FuncDecl) -> IO [FuncDecl]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [0..Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-1] ((Word -> IO FuncDecl) -> IO [FuncDecl])
-> (Word -> IO FuncDecl) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \i :: Word
i -> Context -> Model -> Word -> IO FuncDecl
getConstDecl Context
ctx Model
m Word
i

getFuncs :: Context -> Model -> IO [FuncDecl]
getFuncs :: Context -> Model -> IO [FuncDecl]
getFuncs ctx :: Context
ctx m :: Model
m = do
  Word
n <- Context -> Model -> IO Word
numFuncs Context
ctx Model
m
  [Word] -> (Word -> IO FuncDecl) -> IO [FuncDecl]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [0..Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-1] ((Word -> IO FuncDecl) -> IO [FuncDecl])
-> (Word -> IO FuncDecl) -> IO [FuncDecl]
forall a b. (a -> b) -> a -> b
$ \i :: Word
i -> Context -> Model -> Word -> IO FuncDecl
getFuncDecl Context
ctx Model
m Word
i

-- | Evaluate an array as a function, if possible.
evalArray :: Context -> Model -> AST -> IO (Maybe FuncModel)
evalArray :: Context -> Model -> AST -> IO (Maybe FuncModel)
evalArray ctx :: Context
ctx model :: Model
model array :: AST
array =
    do -- The array must first be evaluated normally, to get it into
       -- 'as-array' form, which is required to acquire the FuncDecl.
       Maybe AST
evaldArrayMb <- Context -> EvalAst AST
eval Context
ctx Model
model AST
array
       case Maybe AST
evaldArrayMb of
         Nothing -> Maybe FuncModel -> IO (Maybe FuncModel)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FuncModel
forall a. Maybe a
Nothing
         Just evaldArray :: AST
evaldArray ->
             do Bool
canConvert <- Context -> AST -> IO Bool
isAsArray Context
ctx AST
evaldArray
                if Bool
canConvert
                  then
                    do FuncDecl
arrayDecl <- Context -> AST -> IO FuncDecl
getAsArrayFuncDecl Context
ctx AST
evaldArray
                       Context -> Model -> FuncDecl -> IO (Maybe FuncModel)
evalFunc Context
ctx Model
model FuncDecl
arrayDecl
                  else Maybe FuncModel -> IO (Maybe FuncModel)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FuncModel
forall a. Maybe a
Nothing


-- | Return the function declaration f associated with a (_ as_array f) node.
getAsArrayFuncDecl :: Context -> AST -> IO FuncDecl
getAsArrayFuncDecl :: Context -> AST -> IO FuncDecl
getAsArrayFuncDecl = (Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_func_decl))
-> Context -> AST -> IO FuncDecl
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO (Ptr Z3_func_decl)
z3_get_as_array_func_decl

-- | 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.
isAsArray :: Context -> AST -> IO Bool
isAsArray :: Context -> AST -> IO Bool
isAsArray = (Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool)
-> Context -> AST -> IO Bool
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO Z3_bool
z3_is_as_array

addFuncInterp :: Context -> Model -> FuncDecl -> AST -> IO FuncInterp
addFuncInterp :: Context -> Model -> FuncDecl -> AST -> IO FuncInterp
addFuncInterp = (Ptr Z3_context
 -> Ptr Z3_model
 -> Ptr Z3_func_decl
 -> Ptr Z3_ast
 -> IO (Ptr Z3_func_interp))
-> Context -> Model -> FuncDecl -> AST -> IO FuncInterp
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_model
-> Ptr Z3_func_decl
-> Ptr Z3_ast
-> IO (Ptr Z3_func_interp)
z3_add_func_interp

addConstInterp :: Context -> Model -> FuncDecl -> AST -> IO ()
addConstInterp :: Context -> Model -> FuncDecl -> AST -> IO ()
addConstInterp = (Ptr Z3_context
 -> Ptr Z3_model -> Ptr Z3_func_decl -> Ptr Z3_ast -> IO ())
-> Context -> Model -> FuncDecl -> AST -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_model -> Ptr Z3_func_decl -> Ptr Z3_ast -> IO ()
z3_add_const_interp


getMapFromInterp :: Context -> FuncInterp -> IO [([AST], AST)]
getMapFromInterp :: Context -> FuncInterp -> IO [([AST], AST)]
getMapFromInterp ctx :: Context
ctx interp :: FuncInterp
interp =
    do Int
n <- Context -> FuncInterp -> IO Int
funcInterpGetNumEntries Context
ctx FuncInterp
interp
       [FuncEntry]
entries <- (Int -> IO FuncEntry) -> [Int] -> IO [FuncEntry]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> FuncInterp -> Int -> IO FuncEntry
funcInterpGetEntry Context
ctx FuncInterp
interp) [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1]
       (FuncEntry -> IO ([AST], AST)) -> [FuncEntry] -> IO [([AST], AST)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> FuncEntry -> IO ([AST], AST)
getEntry Context
ctx) [FuncEntry]
entries

getEntry :: Context -> FuncEntry -> IO ([AST], AST)
getEntry :: Context -> FuncEntry -> IO ([AST], AST)
getEntry ctx :: Context
ctx entry :: FuncEntry
entry =
    do AST
val <- Context -> FuncEntry -> IO AST
funcEntryGetValue Context
ctx FuncEntry
entry
       [AST]
args <- Context -> FuncEntry -> IO [AST]
getEntryArgs Context
ctx FuncEntry
entry
       ([AST], AST) -> IO ([AST], AST)
forall (m :: * -> *) a. Monad m => a -> m a
return ([AST]
args, AST
val)

getEntryArgs :: Context -> FuncEntry -> IO [AST]
getEntryArgs :: Context -> FuncEntry -> IO [AST]
getEntryArgs ctx :: Context
ctx entry :: FuncEntry
entry =
    do Int
n <- Context -> FuncEntry -> IO Int
funcEntryGetNumArgs Context
ctx FuncEntry
entry
       (Int -> IO AST) -> [Int] -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> FuncEntry -> Int -> IO AST
funcEntryGetArg Context
ctx FuncEntry
entry) [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1]

-- | 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.
getFuncInterp :: Context -> Model -> FuncDecl -> IO (Maybe FuncInterp)
getFuncInterp :: Context -> Model -> FuncDecl -> IO (Maybe FuncInterp)
getFuncInterp ctx :: Context
ctx m :: Model
m fd :: FuncDecl
fd = (Ptr Z3_context
 -> Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
-> Context
-> ((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
    -> IO (Ptr Z3_func_interp))
-> IO (Maybe FuncInterp)
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp)
z3_model_get_func_interp Context
ctx (((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
  -> IO (Ptr Z3_func_interp))
 -> IO (Maybe FuncInterp))
-> ((Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
    -> IO (Ptr Z3_func_interp))
-> IO (Maybe FuncInterp)
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp)
f ->
  Model
-> (Ptr Z3_model -> IO (Ptr Z3_func_interp))
-> IO (Ptr Z3_func_interp)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Model
m ((Ptr Z3_model -> IO (Ptr Z3_func_interp))
 -> IO (Ptr Z3_func_interp))
-> (Ptr Z3_model -> IO (Ptr Z3_func_interp))
-> IO (Ptr Z3_func_interp)
forall a b. (a -> b) -> a -> b
$ \mPtr :: Ptr Z3_model
mPtr ->
  FuncDecl
-> (Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
-> IO (Ptr Z3_func_interp)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c FuncDecl
fd ((Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
 -> IO (Ptr Z3_func_interp))
-> (Ptr Z3_func_decl -> IO (Ptr Z3_func_interp))
-> IO (Ptr Z3_func_interp)
forall a b. (a -> b) -> a -> b
$ \fdPtr :: Ptr Z3_func_decl
fdPtr ->
    Ptr Z3_model -> Ptr Z3_func_decl -> IO (Ptr Z3_func_interp)
f Ptr Z3_model
mPtr Ptr Z3_func_decl
fdPtr

-- | Return the number of entries in the given function interpretation.
funcInterpGetNumEntries :: Context -> FuncInterp -> IO Int
funcInterpGetNumEntries :: Context -> FuncInterp -> IO Int
funcInterpGetNumEntries = (Ptr Z3_context -> Ptr Z3_func_interp -> IO CUInt)
-> Context -> FuncInterp -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_interp -> IO CUInt
z3_func_interp_get_num_entries

-- | Return a _point_ of the given function intepretation.
-- It represents the value of f in a particular point.
funcInterpGetEntry :: Context -> FuncInterp -> Int -> IO FuncEntry
funcInterpGetEntry :: Context -> FuncInterp -> Int -> IO FuncEntry
funcInterpGetEntry = (Ptr Z3_context
 -> Ptr Z3_func_interp -> CUInt -> IO (Ptr Z3_func_entry))
-> Context -> FuncInterp -> Int -> IO FuncEntry
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context
-> Ptr Z3_func_interp -> CUInt -> IO (Ptr Z3_func_entry)
z3_func_interp_get_entry

-- | Return the 'else' value of the given function interpretation.
funcInterpGetElse :: Context -> FuncInterp -> IO AST
funcInterpGetElse :: Context -> FuncInterp -> IO AST
funcInterpGetElse = (Ptr Z3_context -> Ptr Z3_func_interp -> IO (Ptr Z3_ast))
-> Context -> FuncInterp -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_interp -> IO (Ptr Z3_ast)
z3_func_interp_get_else

-- | Return the arity (number of arguments) of the given function
-- interpretation.
funcInterpGetArity :: Context -> FuncInterp -> IO Int
funcInterpGetArity :: Context -> FuncInterp -> IO Int
funcInterpGetArity = (Ptr Z3_context -> Ptr Z3_func_interp -> IO CUInt)
-> Context -> FuncInterp -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_interp -> IO CUInt
z3_func_interp_get_arity

-- | Return the value of this point.
funcEntryGetValue :: Context -> FuncEntry -> IO AST
funcEntryGetValue :: Context -> FuncEntry -> IO AST
funcEntryGetValue = (Ptr Z3_context -> Ptr Z3_func_entry -> IO (Ptr Z3_ast))
-> Context -> FuncEntry -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_entry -> IO (Ptr Z3_ast)
z3_func_entry_get_value

-- | Return the number of arguments in a Z3_func_entry object.
funcEntryGetNumArgs :: Context -> FuncEntry -> IO Int
funcEntryGetNumArgs :: Context -> FuncEntry -> IO Int
funcEntryGetNumArgs = (Ptr Z3_context -> Ptr Z3_func_entry -> IO CUInt)
-> Context -> FuncEntry -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_entry -> IO CUInt
z3_func_entry_get_num_args

-- | Return an argument of a Z3_func_entry object.
funcEntryGetArg :: Context -> FuncEntry -> Int -> IO AST
funcEntryGetArg :: Context -> FuncEntry -> Int -> IO AST
funcEntryGetArg = (Ptr Z3_context -> Ptr Z3_func_entry -> CUInt -> IO (Ptr Z3_ast))
-> Context -> FuncEntry -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_func_entry -> CUInt -> IO (Ptr Z3_ast)
z3_func_entry_get_arg

-- | Convert the given model into a string.
modelToString :: Context -> Model -> IO String
modelToString :: Context -> Model -> IO String
modelToString = (Ptr Z3_context -> Ptr Z3_model -> IO CString)
-> Context -> Model -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_model -> IO CString
z3_model_to_string

-- | Alias for 'modelToString'.
showModel :: Context -> Model -> IO String
showModel :: Context -> Model -> IO String
showModel = Context -> Model -> IO String
modelToString
{-# DEPRECATED showModel "Use modelToString instead." #-}

-------------------------------------------------
-- ** Helpers

-- | Type of an evaluation function for 'AST'.
--
-- Evaluation may fail (i.e. return 'Nothing') for a few
-- reasons, see 'modelEval'.
type EvalAst a = Model -> AST -> IO (Maybe a)

-- | An alias for 'modelEval' with model completion enabled.
eval :: Context -> EvalAst AST
eval :: Context -> EvalAst AST
eval ctx :: Context
ctx m :: Model
m a :: AST
a = Context -> Model -> AST -> Bool -> IO (Maybe AST)
modelEval Context
ctx Model
m AST
a Bool
True

-- | Evaluate an AST node of sort /bool/ in the given model.
--
-- See 'modelEval' and 'getBool'.
evalBool :: Context -> EvalAst Bool
evalBool :: Context -> EvalAst Bool
evalBool ctx :: Context
ctx m :: Model
m ast :: AST
ast = Context -> EvalAst AST
eval Context
ctx Model
m AST
ast IO (Maybe AST) -> (Maybe AST -> IO (Maybe Bool)) -> IO (Maybe Bool)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (AST -> IO Bool) -> Maybe AST -> IO (Maybe Bool)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (Context -> AST -> IO Bool
getBool Context
ctx)

-- | Evaluate an AST node of sort /int/ in the given model.
--
-- See 'modelEval' and 'getInt'.
evalInt :: Context -> EvalAst Integer
evalInt :: Context -> EvalAst Integer
evalInt ctx :: Context
ctx m :: Model
m ast :: AST
ast = Context -> EvalAst AST
eval Context
ctx Model
m AST
ast IO (Maybe AST)
-> (Maybe AST -> IO (Maybe Integer)) -> IO (Maybe Integer)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (AST -> IO Integer) -> Maybe AST -> IO (Maybe Integer)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (Context -> AST -> IO Integer
getInt Context
ctx)

-- | Evaluate an AST node of sort /real/ in the given model.
--
-- See 'modelEval' and 'getReal'.
evalReal :: Context -> EvalAst Rational
evalReal :: Context -> EvalAst Rational
evalReal ctx :: Context
ctx m :: Model
m ast :: AST
ast = Context -> EvalAst AST
eval Context
ctx Model
m AST
ast IO (Maybe AST)
-> (Maybe AST -> IO (Maybe Rational)) -> IO (Maybe Rational)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (AST -> IO Rational) -> Maybe AST -> IO (Maybe Rational)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (Context -> AST -> IO Rational
getReal Context
ctx)

-- | Evaluate an AST node of sort /bit-vector/ in the given model.
--
-- The flag /signed/ decides whether the bit-vector value is
-- interpreted as a signed or unsigned integer.
--
-- See 'modelEval' and 'getBv'.
evalBv :: Context -> Bool -- ^ signed?
                  -> EvalAst Integer
evalBv :: Context -> Bool -> EvalAst Integer
evalBv ctx :: Context
ctx signed :: Bool
signed m :: Model
m ast :: AST
ast =
  Context -> EvalAst AST
eval Context
ctx Model
m AST
ast IO (Maybe AST)
-> (Maybe AST -> IO (Maybe Integer)) -> IO (Maybe Integer)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (AST -> IO Integer) -> Maybe AST -> IO (Maybe Integer)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
T.traverse (\a :: AST
a -> Context -> AST -> Bool -> IO Integer
getBv Context
ctx AST
a Bool
signed)

-- | Evaluate a /collection/ of AST nodes in the given model.
evalT :: Traversable t => Context -> Model -> t AST -> IO (Maybe (t AST))
evalT :: Context -> Model -> t AST -> IO (Maybe (t AST))
evalT c :: Context
c = EvalAst AST -> Model -> t AST -> IO (Maybe (t AST))
forall (t :: * -> *) a.
Traversable t =>
EvalAst a -> Model -> t AST -> IO (Maybe (t a))
mapEval (Context -> EvalAst AST
eval Context
c)

-- | Run a evaluation function on a 'Traversable' data structure of 'AST's
-- (e.g. @[AST]@, @Vector AST@, @Maybe AST@, etc).
--
-- This a generic version of 'evalT' which can be used in combination with
-- other helpers. For instance, @mapEval (evalInt c)@ can be used to obtain
-- the 'Integer' interpretation of a list of 'AST' of sort /int/.
mapEval :: Traversable t => EvalAst a -> Model -> t AST -> IO (Maybe (t a))
mapEval :: EvalAst a -> Model -> t AST -> IO (Maybe (t a))
mapEval f :: EvalAst a
f m :: Model
m = (t (Maybe a) -> Maybe (t a))
-> IO (t (Maybe a)) -> IO (Maybe (t a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap t (Maybe a) -> Maybe (t a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
T.sequence (IO (t (Maybe a)) -> IO (Maybe (t a)))
-> (t AST -> IO (t (Maybe a))) -> t AST -> IO (Maybe (t a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AST -> IO (Maybe a)) -> t AST -> IO (t (Maybe a))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (EvalAst a
f Model
m)

{- TODO: Parameterize FuncModel

data FuncModel a b = FuncModel [([a], b)] b

type FuncModelAST = FuncModel AST AST

evalFuncWith :: Context -> Model -> EvalAst a -> EvalAst b -> FuncDecl -> IO (Maybe (FuncModel a b))

-}

-- | The interpretation of a function.
data FuncModel = FuncModel
    { FuncModel -> [([AST], AST)]
interpMap :: [([AST], AST)]
      -- ^ Mapping from arguments to values.
    , FuncModel -> AST
interpElse :: AST
      -- ^ Default value.
    }

-- | Evaluate a function declaration to a list of argument/value pairs.
evalFunc :: Context -> Model -> FuncDecl -> IO (Maybe FuncModel)
evalFunc :: Context -> Model -> FuncDecl -> IO (Maybe FuncModel)
evalFunc ctx :: Context
ctx m :: Model
m fDecl :: FuncDecl
fDecl =
    do Maybe FuncInterp
interpMb <- Context -> Model -> FuncDecl -> IO (Maybe FuncInterp)
getFuncInterp Context
ctx Model
m FuncDecl
fDecl
       case Maybe FuncInterp
interpMb of
         Nothing -> Maybe FuncModel -> IO (Maybe FuncModel)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FuncModel
forall a. Maybe a
Nothing
         Just interp :: FuncInterp
interp ->
             do [([AST], AST)]
funcMap  <- Context -> FuncInterp -> IO [([AST], AST)]
getMapFromInterp Context
ctx FuncInterp
interp
                AST
elsePart <- Context -> FuncInterp -> IO AST
funcInterpGetElse Context
ctx FuncInterp
interp
                Maybe FuncModel -> IO (Maybe FuncModel)
forall (m :: * -> *) a. Monad m => a -> m a
return (FuncModel -> Maybe FuncModel
forall a. a -> Maybe a
Just (FuncModel -> Maybe FuncModel) -> FuncModel -> Maybe FuncModel
forall a b. (a -> b) -> a -> b
$ [([AST], AST)] -> AST -> FuncModel
FuncModel [([AST], AST)]
funcMap AST
elsePart)

---------------------------------------------------------------------
-- Interaction logging

-- TODO

---------------------------------------------------------------------
-- String Conversion

-- | Pretty-printing mode for converting ASTs to strings.  The mode
-- can be one of the following:
--
-- * Z3_PRINT_SMTLIB_FULL: Print AST nodes in SMTLIB verbose format.
--
-- * Z3_PRINT_LOW_LEVEL: Print AST nodes using a low-level format.
--
-- * Z3_PRINT_SMTLIB_COMPLIANT: Print AST nodes in SMTLIB 1.x
-- compliant format.
--
-- * Z3_PRINT_SMTLIB2_COMPLIANT: Print AST nodes in SMTLIB 2.x
-- compliant format.
data ASTPrintMode
  = Z3_PRINT_SMTLIB_FULL
  | Z3_PRINT_LOW_LEVEL
  | Z3_PRINT_SMTLIB2_COMPLIANT

-- | Set the pretty-printing mode for converting ASTs to strings.
setASTPrintMode :: Context -> ASTPrintMode -> IO ()
setASTPrintMode :: Context -> ASTPrintMode -> IO ()
setASTPrintMode ctx :: Context
ctx mode :: ASTPrintMode
mode = Context -> (Ptr Z3_context -> IO ()) -> IO ()
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError Context
ctx ((Ptr Z3_context -> IO ()) -> IO ())
-> (Ptr Z3_context -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ctxPtr :: Ptr Z3_context
ctxPtr ->
  case ASTPrintMode
mode of
       Z3_PRINT_SMTLIB_FULL ->
         Z3_error_handler
z3_set_ast_print_mode Ptr Z3_context
ctxPtr CInt
z3_print_smtlib_full
       Z3_PRINT_LOW_LEVEL ->
         Z3_error_handler
z3_set_ast_print_mode Ptr Z3_context
ctxPtr CInt
z3_print_low_level
       Z3_PRINT_SMTLIB2_COMPLIANT ->
         Z3_error_handler
z3_set_ast_print_mode Ptr Z3_context
ctxPtr CInt
z3_print_smtlib2_compliant

-- | Convert an AST to a string.
astToString :: Context -> AST -> IO String
astToString :: Context -> AST -> IO String
astToString = (Ptr Z3_context -> Ptr Z3_ast -> IO CString)
-> Context -> AST -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_ast -> IO CString
z3_ast_to_string

-- | Convert a pattern to a string.
patternToString :: Context -> Pattern -> IO String
patternToString :: Context -> Pattern -> IO String
patternToString = (Ptr Z3_context -> Ptr Z3_pattern -> IO CString)
-> Context -> Pattern -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_pattern -> IO CString
z3_pattern_to_string

-- | Convert a sort to a string.
sortToString :: Context -> Sort -> IO String
sortToString :: Context -> Sort -> IO String
sortToString = (Ptr Z3_context -> Ptr Z3_sort -> IO CString)
-> Context -> Sort -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_sort -> IO CString
z3_sort_to_string

-- | Convert a FuncDecl to a string.
funcDeclToString :: Context -> FuncDecl -> IO String
funcDeclToString :: Context -> FuncDecl -> IO String
funcDeclToString = (Ptr Z3_context -> Ptr Z3_func_decl -> IO CString)
-> Context -> FuncDecl -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_func_decl -> IO CString
z3_func_decl_to_string

-- | Convert the given benchmark into SMT-LIB formatted string.
--
-- The output format can be configured via 'setASTPrintMode'.
benchmarkToSMTLibString :: Context
                            -> String   -- ^ name
                            -> String   -- ^ logic
                            -> String   -- ^ status
                            -> String   -- ^ attributes
                            -> [AST]    -- ^ assumptions
                            -> AST      -- ^ formula
                            -> IO String
benchmarkToSMTLibString :: Context
-> String
-> String
-> String
-> String
-> [AST]
-> AST
-> IO String
benchmarkToSMTLibString ctx :: Context
ctx name :: String
name logic :: String
logic status :: String
status attr :: String
attr assump :: [AST]
assump form :: AST
form =
  (Ptr Z3_context
 -> CString
 -> CString
 -> CString
 -> CString
 -> CUInt
 -> Ptr (Ptr Z3_ast)
 -> Ptr Z3_ast
 -> IO CString)
-> Context
-> ((CString
     -> CString
     -> CString
     -> CString
     -> CUInt
     -> Ptr (Ptr Z3_ast)
     -> Ptr Z3_ast
     -> IO CString)
    -> IO CString)
-> IO String
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> CString
-> CString
-> CString
-> CString
-> CUInt
-> Ptr (Ptr Z3_ast)
-> Ptr Z3_ast
-> IO CString
z3_benchmark_to_smtlib_string Context
ctx (((CString
   -> CString
   -> CString
   -> CString
   -> CUInt
   -> Ptr (Ptr Z3_ast)
   -> Ptr Z3_ast
   -> IO CString)
  -> IO CString)
 -> IO String)
-> ((CString
     -> CString
     -> CString
     -> CString
     -> CUInt
     -> Ptr (Ptr Z3_ast)
     -> Ptr Z3_ast
     -> IO CString)
    -> IO CString)
-> IO String
forall a b. (a -> b) -> a -> b
$ \f :: CString
-> CString
-> CString
-> CString
-> CUInt
-> Ptr (Ptr Z3_ast)
-> Ptr Z3_ast
-> IO CString
f ->
    String -> (CString -> IO CString) -> IO CString
forall a. String -> (CString -> IO a) -> IO a
withCString String
name ((CString -> IO CString) -> IO CString)
-> (CString -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \namePtr :: CString
namePtr ->
    String -> (CString -> IO CString) -> IO CString
forall a. String -> (CString -> IO a) -> IO a
withCString String
logic ((CString -> IO CString) -> IO CString)
-> (CString -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \logicPtr :: CString
logicPtr ->
    String -> (CString -> IO CString) -> IO CString
forall a. String -> (CString -> IO a) -> IO a
withCString String
status ((CString -> IO CString) -> IO CString)
-> (CString -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \statusPtr :: CString
statusPtr ->
    String -> (CString -> IO CString) -> IO CString
forall a. String -> (CString -> IO a) -> IO a
withCString String
attr ((CString -> IO CString) -> IO CString)
-> (CString -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \attrPtr :: CString
attrPtr ->
    [AST] -> (CUInt -> Ptr (Ptr Z3_ast) -> IO CString) -> IO CString
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
assump ((CUInt -> Ptr (Ptr Z3_ast) -> IO CString) -> IO CString)
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \assumpNum :: CUInt
assumpNum assumpArr :: Ptr (Ptr Z3_ast)
assumpArr ->
    AST -> (Ptr Z3_ast -> IO CString) -> IO CString
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c AST
form ((Ptr Z3_ast -> IO CString) -> IO CString)
-> (Ptr Z3_ast -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \formPtr :: Ptr Z3_ast
formPtr ->
      CString
-> CString
-> CString
-> CString
-> CUInt
-> Ptr (Ptr Z3_ast)
-> Ptr Z3_ast
-> IO CString
f CString
namePtr CString
logicPtr CString
statusPtr CString
attrPtr CUInt
assumpNum Ptr (Ptr Z3_ast)
assumpArr Ptr Z3_ast
formPtr

---------------------------------------------------------------------
-- Tactics

mkTactic :: Context -> String -> IO Tactic
mkTactic :: Context -> String -> IO Tactic
mkTactic = (Ptr Z3_context -> CString -> IO (Ptr Z3_tactic))
-> Context -> String -> IO Tactic
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> CString -> IO (Ptr Z3_tactic)
z3_mk_tactic

andThenTactic :: Context -> Tactic -> Tactic -> IO Tactic
andThenTactic :: Context -> Tactic -> Tactic -> IO Tactic
andThenTactic = (Ptr Z3_context
 -> Ptr Z3_tactic -> Ptr Z3_tactic -> IO (Ptr Z3_tactic))
-> Context -> Tactic -> Tactic -> IO Tactic
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context
-> Ptr Z3_tactic -> Ptr Z3_tactic -> IO (Ptr Z3_tactic)
z3_tactic_and_then

orElseTactic :: Context -> Tactic -> Tactic -> IO Tactic
orElseTactic :: Context -> Tactic -> Tactic -> IO Tactic
orElseTactic = (Ptr Z3_context
 -> Ptr Z3_tactic -> Ptr Z3_tactic -> IO (Ptr Z3_tactic))
-> Context -> Tactic -> Tactic -> IO Tactic
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context
-> Ptr Z3_tactic -> Ptr Z3_tactic -> IO (Ptr Z3_tactic)
z3_tactic_or_else

skipTactic :: Context -> IO Tactic
skipTactic :: Context -> IO Tactic
skipTactic = (Ptr Z3_context -> IO (Ptr Z3_tactic)) -> Context -> IO Tactic
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_tactic)
z3_tactic_skip

tryForTactic :: Context -> Tactic -> Int -> IO Tactic
tryForTactic :: Context -> Tactic -> Int -> IO Tactic
tryForTactic = (Ptr Z3_context -> Ptr Z3_tactic -> CUInt -> IO (Ptr Z3_tactic))
-> Context -> Tactic -> Int -> IO Tactic
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_tactic -> CUInt -> IO (Ptr Z3_tactic)
z3_tactic_try_for

mkQuantifierEliminationTactic :: Context -> IO Tactic
mkQuantifierEliminationTactic :: Context -> IO Tactic
mkQuantifierEliminationTactic ctx :: Context
ctx = Context -> String -> IO Tactic
mkTactic Context
ctx "qe"

mkAndInverterGraphTactic :: Context -> IO Tactic
mkAndInverterGraphTactic :: Context -> IO Tactic
mkAndInverterGraphTactic ctx :: Context
ctx = Context -> String -> IO Tactic
mkTactic Context
ctx "aig"

applyTactic :: Context -> Tactic -> Goal -> IO ApplyResult
applyTactic :: Context -> Tactic -> Goal -> IO ApplyResult
applyTactic = (Ptr Z3_context
 -> Ptr Z3_tactic -> Ptr Z3_goal -> IO (Ptr Z3_apply_result))
-> Context -> Tactic -> Goal -> IO ApplyResult
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context
-> Ptr Z3_tactic -> Ptr Z3_goal -> IO (Ptr Z3_apply_result)
z3_tactic_apply

getApplyResultNumSubgoals :: Context -> ApplyResult -> IO Int
getApplyResultNumSubgoals :: Context -> ApplyResult -> IO Int
getApplyResultNumSubgoals = (Ptr Z3_context -> Ptr Z3_apply_result -> IO CUInt)
-> Context -> ApplyResult -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_apply_result -> IO CUInt
z3_apply_result_get_num_subgoals

getApplyResultSubgoal :: Context -> ApplyResult -> Int -> IO Goal
getApplyResultSubgoal :: Context -> ApplyResult -> Int -> IO Goal
getApplyResultSubgoal = (Ptr Z3_context
 -> Ptr Z3_apply_result -> CUInt -> IO (Ptr Z3_goal))
-> Context -> ApplyResult -> Int -> IO Goal
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_apply_result -> CUInt -> IO (Ptr Z3_goal)
z3_apply_result_get_subgoal

getApplyResultSubgoals :: Context -> ApplyResult -> IO [Goal]
getApplyResultSubgoals :: Context -> ApplyResult -> IO [Goal]
getApplyResultSubgoals ctx :: Context
ctx a :: ApplyResult
a = do
  Int
n <- Context -> ApplyResult -> IO Int
getApplyResultNumSubgoals Context
ctx ApplyResult
a
  [Int] -> (Int -> IO Goal) -> IO [Goal]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] (Context -> ApplyResult -> Int -> IO Goal
getApplyResultSubgoal Context
ctx ApplyResult
a)

mkGoal :: Context -> Bool -> Bool -> Bool -> IO Goal
mkGoal :: Context -> Bool -> Bool -> Bool -> IO Goal
mkGoal = (Ptr Z3_context
 -> Z3_bool -> Z3_bool -> Z3_bool -> IO (Ptr Z3_goal))
-> Context -> Bool -> Bool -> Bool -> IO Goal
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context -> Z3_bool -> Z3_bool -> Z3_bool -> IO (Ptr Z3_goal)
z3_mk_goal

goalAssert :: Context -> Goal -> AST -> IO ()
goalAssert :: Context -> Goal -> AST -> IO ()
goalAssert = (Ptr Z3_context -> Ptr Z3_goal -> Ptr Z3_ast -> IO ())
-> Context -> Goal -> AST -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_goal -> Ptr Z3_ast -> IO ()
z3_goal_assert

getGoalSize :: Context -> Goal -> IO Int
getGoalSize :: Context -> Goal -> IO Int
getGoalSize = (Ptr Z3_context -> Ptr Z3_goal -> IO CUInt)
-> Context -> Goal -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_goal -> IO CUInt
z3_goal_size

getGoalFormula :: Context -> Goal -> Int -> IO AST
getGoalFormula :: Context -> Goal -> Int -> IO AST
getGoalFormula = (Ptr Z3_context -> Ptr Z3_goal -> CUInt -> IO (Ptr Z3_ast))
-> Context -> Goal -> Int -> IO AST
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_goal -> CUInt -> IO (Ptr Z3_ast)
z3_goal_formula

getGoalFormulas :: Context -> Goal -> IO [AST]
getGoalFormulas :: Context -> Goal -> IO [AST]
getGoalFormulas ctx :: Context
ctx g :: Goal
g = do
  Int
n <- Context -> Goal -> IO Int
getGoalSize Context
ctx Goal
g
  [Int] -> (Int -> IO AST) -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
T.forM [0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1] (Context -> Goal -> Int -> IO AST
getGoalFormula Context
ctx Goal
g)

---------------------------------------------------------------------
-- Parser interface

parseSMTLib2String :: Context
                   -> String     -- ^ string to parse
                   -> [Symbol]   -- ^ sort names
                   -> [Sort]     -- ^ sorts
                   -> [Symbol]   -- ^ declaration names
                   -> [FuncDecl] -- ^ declarations
                   -> IO AST
parseSMTLib2String :: Context
-> String -> [Symbol] -> [Sort] -> [Symbol] -> [FuncDecl] -> IO AST
parseSMTLib2String ctx :: Context
ctx str :: String
str sortNames :: [Symbol]
sortNames sorts :: [Sort]
sorts declNames :: [Symbol]
declNames decls :: [FuncDecl]
decls =
  (Ptr Z3_context
 -> CString
 -> CUInt
 -> Ptr (Ptr Z3_symbol)
 -> Ptr (Ptr Z3_sort)
 -> CUInt
 -> Ptr (Ptr Z3_symbol)
 -> Ptr (Ptr Z3_func_decl)
 -> IO (Ptr Z3_ast))
-> Context
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_sort)
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_func_decl)
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
z3_parse_smtlib2_string Context
ctx (((CString
   -> CUInt
   -> Ptr (Ptr Z3_symbol)
   -> Ptr (Ptr Z3_sort)
   -> CUInt
   -> Ptr (Ptr Z3_symbol)
   -> Ptr (Ptr Z3_func_decl)
   -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_sort)
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_func_decl)
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
f ->
    String -> (CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a. String -> (CString -> IO a) -> IO a
withCString String
str ((CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \cstr :: CString
cstr ->
    [Sort]
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Sort]
sorts ((CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \sortNum :: CUInt
sortNum sortArr :: Ptr (Ptr Z3_sort)
sortArr ->
    [Symbol]
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Symbol]
sortNames ((Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \sortNameArr :: Ptr (Ptr Z3_symbol)
sortNameArr ->
    [FuncDecl]
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [FuncDecl]
decls ((CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \declNum :: CUInt
declNum declArr :: Ptr (Ptr Z3_func_decl)
declArr ->
    [Symbol]
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Symbol]
declNames ((Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \declNameArr :: Ptr (Ptr Z3_symbol)
declNameArr ->
      CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
f CString
cstr CUInt
sortNum Ptr (Ptr Z3_symbol)
sortNameArr Ptr (Ptr Z3_sort)
sortArr CUInt
declNum Ptr (Ptr Z3_symbol)
declNameArr Ptr (Ptr Z3_func_decl)
declArr

parseSMTLib2File :: Context
                 -> String     -- ^ file name
                 -> [Symbol]   -- ^ sort names
                 -> [Sort]     -- ^ sorts
                 -> [Symbol]   -- ^ declaration names
                 -> [FuncDecl] -- ^ declarations
                 -> IO AST
parseSMTLib2File :: Context
-> String -> [Symbol] -> [Sort] -> [Symbol] -> [FuncDecl] -> IO AST
parseSMTLib2File ctx :: Context
ctx file :: String
file sortNames :: [Symbol]
sortNames sorts :: [Sort]
sorts declNames :: [Symbol]
declNames decls :: [FuncDecl]
decls =
  (Ptr Z3_context
 -> CString
 -> CUInt
 -> Ptr (Ptr Z3_symbol)
 -> Ptr (Ptr Z3_sort)
 -> CUInt
 -> Ptr (Ptr Z3_symbol)
 -> Ptr (Ptr Z3_func_decl)
 -> IO (Ptr Z3_ast))
-> Context
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_sort)
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_func_decl)
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
z3_parse_smtlib2_string Context
ctx (((CString
   -> CUInt
   -> Ptr (Ptr Z3_symbol)
   -> Ptr (Ptr Z3_sort)
   -> CUInt
   -> Ptr (Ptr Z3_symbol)
   -> Ptr (Ptr Z3_func_decl)
   -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> ((CString
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_sort)
     -> CUInt
     -> Ptr (Ptr Z3_symbol)
     -> Ptr (Ptr Z3_func_decl)
     -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall a b. (a -> b) -> a -> b
$ \f :: CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
f ->
    String -> (CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a. String -> (CString -> IO a) -> IO a
withCString String
file ((CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (CString -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \fileName :: CString
fileName ->
    [Sort]
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [Sort]
sorts ((CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_sort) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \sortNum :: CUInt
sortNum sortArr :: Ptr (Ptr Z3_sort)
sortArr ->
    [Symbol]
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Symbol]
sortNames ((Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \sortNameArr :: Ptr (Ptr Z3_symbol)
sortNameArr ->
    [FuncDecl]
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [FuncDecl]
decls ((CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \declNum :: CUInt
declNum declArr :: Ptr (Ptr Z3_func_decl)
declArr ->
    [Symbol]
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall h c a.
(Marshal h c, Storable c) =>
[h] -> (Ptr c -> IO a) -> IO a
marshalArray [Symbol]
declNames ((Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast))
-> (Ptr (Ptr Z3_symbol) -> IO (Ptr Z3_ast)) -> IO (Ptr Z3_ast)
forall a b. (a -> b) -> a -> b
$ \declNameArr :: Ptr (Ptr Z3_symbol)
declNameArr ->
      CString
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_sort)
-> CUInt
-> Ptr (Ptr Z3_symbol)
-> Ptr (Ptr Z3_func_decl)
-> IO (Ptr Z3_ast)
f CString
fileName CUInt
sortNum Ptr (Ptr Z3_symbol)
sortNameArr Ptr (Ptr Z3_sort)
sortArr CUInt
declNum Ptr (Ptr Z3_symbol)
declNameArr Ptr (Ptr Z3_func_decl)
declArr


---------------------------------------------------------------------
-- Error handling

-- | Z3 exceptions.
--
-- Z3 errors are re-thrown as Haskell 'Z3Error' exceptions,
-- see 'Control.Exception'.
data Z3Error = Z3Error
    { Z3Error -> Z3ErrorCode
errCode :: Z3ErrorCode
    , Z3Error -> String
errMsg  :: String
    }
  deriving Typeable

instance Show Z3Error where
  show :: Z3Error -> String
show (Z3Error _ s :: String
s) = "Z3 error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s

-- | Z3 error codes.
data Z3ErrorCode = SortError | IOB | InvalidArg | ParserError | NoParser
  | InvalidPattern | MemoutFail  | FileAccessError | InternalFatal
  | InvalidUsage   | DecRefError | Z3Exception
  deriving (Int -> Z3ErrorCode -> ShowS
[Z3ErrorCode] -> ShowS
Z3ErrorCode -> String
(Int -> Z3ErrorCode -> ShowS)
-> (Z3ErrorCode -> String)
-> ([Z3ErrorCode] -> ShowS)
-> Show Z3ErrorCode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Z3ErrorCode] -> ShowS
$cshowList :: [Z3ErrorCode] -> ShowS
show :: Z3ErrorCode -> String
$cshow :: Z3ErrorCode -> String
showsPrec :: Int -> Z3ErrorCode -> ShowS
$cshowsPrec :: Int -> Z3ErrorCode -> ShowS
Show, Typeable)

toZ3Error :: Z3_error_code -> Z3ErrorCode
toZ3Error :: CInt -> Z3ErrorCode
toZ3Error e :: CInt
e
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_sort_error        = Z3ErrorCode
SortError
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_iob               = Z3ErrorCode
IOB
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_invalid_arg       = Z3ErrorCode
InvalidArg
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_parser_error      = Z3ErrorCode
ParserError
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_no_parser         = Z3ErrorCode
NoParser
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_invalid_pattern   = Z3ErrorCode
InvalidPattern
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_memout_fail       = Z3ErrorCode
MemoutFail
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_file_access_error = Z3ErrorCode
FileAccessError
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_internal_fatal    = Z3ErrorCode
InternalFatal
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_invalid_usage     = Z3ErrorCode
InvalidUsage
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_dec_ref_error     = Z3ErrorCode
DecRefError
  | CInt
e CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
z3_exception         = Z3ErrorCode
Z3Exception
  | Bool
otherwise                 = String -> Z3ErrorCode
forall a. HasCallStack => String -> a
error "Z3.Base.toZ3Error: illegal `Z3_error_code' value"

instance Exception Z3Error

-- | Throws a z3 error
z3Error :: Z3ErrorCode -> String -> IO ()
z3Error :: Z3ErrorCode -> String -> IO ()
z3Error cd :: Z3ErrorCode
cd = Z3Error -> IO ()
forall a e. Exception e => e -> a
throw (Z3Error -> IO ()) -> (String -> Z3Error) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Z3ErrorCode -> String -> Z3Error
Z3Error Z3ErrorCode
cd

-- | Throw an exception if a Z3 error happened
checkError :: Ptr Z3_context -> IO a -> IO a
checkError :: Ptr Z3_context -> IO a -> IO a
checkError cPtr :: Ptr Z3_context
cPtr m :: IO a
m = do
  IO a
m IO a -> IO () -> IO a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (Ptr Z3_context -> IO CInt
z3_get_error_code Ptr Z3_context
cPtr IO CInt -> (CInt -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CInt -> IO ()
throwZ3Exn)
  where getErrStr :: CInt -> IO String
getErrStr i :: CInt
i  = CString -> IO String
peekCString (CString -> IO String) -> IO CString -> IO String
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Z3_context -> CInt -> IO CString
z3_get_error_msg_ex Ptr Z3_context
cPtr CInt
i
        throwZ3Exn :: CInt -> IO ()
throwZ3Exn i :: CInt
i = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (CInt
i CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
z3_ok) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ CInt -> IO String
getErrStr CInt
i IO String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Z3ErrorCode -> String -> IO ()
z3Error (CInt -> Z3ErrorCode
toZ3Error CInt
i)

---------------------------------------------------------------------
-- Miscellaneous

data Version
  = Version {
      Version -> Int
z3Major    :: !Int
    , Version -> Int
z3Minor    :: !Int
    , Version -> Int
z3Build    :: !Int
    , Version -> Int
z3Revision :: !Int
    }
  deriving (Version -> Version -> Bool
(Version -> Version -> Bool)
-> (Version -> Version -> Bool) -> Eq Version
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Version -> Version -> Bool
$c/= :: Version -> Version -> Bool
== :: Version -> Version -> Bool
$c== :: Version -> Version -> Bool
Eq,Eq Version
Eq Version =>
(Version -> Version -> Ordering)
-> (Version -> Version -> Bool)
-> (Version -> Version -> Bool)
-> (Version -> Version -> Bool)
-> (Version -> Version -> Bool)
-> (Version -> Version -> Version)
-> (Version -> Version -> Version)
-> Ord Version
Version -> Version -> Bool
Version -> Version -> Ordering
Version -> Version -> Version
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Version -> Version -> Version
$cmin :: Version -> Version -> Version
max :: Version -> Version -> Version
$cmax :: Version -> Version -> Version
>= :: Version -> Version -> Bool
$c>= :: Version -> Version -> Bool
> :: Version -> Version -> Bool
$c> :: Version -> Version -> Bool
<= :: Version -> Version -> Bool
$c<= :: Version -> Version -> Bool
< :: Version -> Version -> Bool
$c< :: Version -> Version -> Bool
compare :: Version -> Version -> Ordering
$ccompare :: Version -> Version -> Ordering
$cp1Ord :: Eq Version
Ord)

instance Show Version where
  show :: Version -> String
show (Version major :: Int
major minor :: Int
minor build :: Int
build _) =
    Int -> String
forall a. Show a => a -> String
show Int
major String -> ShowS
forall a. [a] -> [a] -> [a]
++ "." String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
minor String -> ShowS
forall a. [a] -> [a] -> [a]
++ "." String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
build

-- | Return Z3 version number information.
getVersion :: IO Version
getVersion :: IO Version
getVersion =
  (Ptr CUInt -> IO Version) -> IO Version
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CUInt -> IO Version) -> IO Version)
-> (Ptr CUInt -> IO Version) -> IO Version
forall a b. (a -> b) -> a -> b
$ \ptrMinor :: Ptr CUInt
ptrMinor ->
  (Ptr CUInt -> IO Version) -> IO Version
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CUInt -> IO Version) -> IO Version)
-> (Ptr CUInt -> IO Version) -> IO Version
forall a b. (a -> b) -> a -> b
$ \ptrMajor :: Ptr CUInt
ptrMajor ->
  (Ptr CUInt -> IO Version) -> IO Version
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CUInt -> IO Version) -> IO Version)
-> (Ptr CUInt -> IO Version) -> IO Version
forall a b. (a -> b) -> a -> b
$ \ptrBuild :: Ptr CUInt
ptrBuild ->
  (Ptr CUInt -> IO Version) -> IO Version
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CUInt -> IO Version) -> IO Version)
-> (Ptr CUInt -> IO Version) -> IO Version
forall a b. (a -> b) -> a -> b
$ \ptrRevision :: Ptr CUInt
ptrRevision -> do
    Ptr CUInt -> Ptr CUInt -> Ptr CUInt -> Ptr CUInt -> IO ()
z3_get_version Ptr CUInt
ptrMinor Ptr CUInt
ptrMajor Ptr CUInt
ptrBuild Ptr CUInt
ptrRevision
    Int
minor    <- CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Int) -> IO CUInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
ptrMinor
    Int
major    <- CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Int) -> IO CUInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
ptrMajor
    Int
build    <- CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Int) -> IO CUInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
ptrBuild
    Int
revision <- CUInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUInt -> Int) -> IO CUInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CUInt -> IO CUInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CUInt
ptrRevision
    Version -> IO Version
forall (m :: * -> *) a. Monad m => a -> m a
return (Version -> IO Version) -> Version -> IO Version
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int -> Int -> Version
Version Int
minor Int
major Int
build Int
revision

---------------------------------------------------------------------
-- Externalm Theory Plugins

-- TODO

---------------------------------------------------------------------
-- Fixedpoint facilities

newtype Fixedpoint = Fixedpoint { Fixedpoint -> ForeignPtr Z3_fixedpoint
unFixedpoint :: ForeignPtr Z3_fixedpoint }
    deriving Fixedpoint -> Fixedpoint -> Bool
(Fixedpoint -> Fixedpoint -> Bool)
-> (Fixedpoint -> Fixedpoint -> Bool) -> Eq Fixedpoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Fixedpoint -> Fixedpoint -> Bool
$c/= :: Fixedpoint -> Fixedpoint -> Bool
== :: Fixedpoint -> Fixedpoint -> Bool
$c== :: Fixedpoint -> Fixedpoint -> Bool
Eq

instance Marshal Fixedpoint (Ptr Z3_fixedpoint) where
  c2h :: Context -> Ptr Z3_fixedpoint -> IO Fixedpoint
c2h = (ForeignPtr Z3_fixedpoint -> Fixedpoint)
-> Z3IncRefFun Z3_fixedpoint
-> Z3IncRefFun Z3_fixedpoint
-> Context
-> Ptr Z3_fixedpoint
-> IO Fixedpoint
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_fixedpoint -> Fixedpoint
Fixedpoint Z3IncRefFun Z3_fixedpoint
z3_fixedpoint_inc_ref Z3IncRefFun Z3_fixedpoint
z3_fixedpoint_dec_ref
  h2c :: Fixedpoint -> (Ptr Z3_fixedpoint -> IO r) -> IO r
h2c fp :: Fixedpoint
fp = ForeignPtr Z3_fixedpoint -> (Ptr Z3_fixedpoint -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Fixedpoint -> ForeignPtr Z3_fixedpoint
unFixedpoint Fixedpoint
fp)

mkFixedpoint :: Context -> IO Fixedpoint
mkFixedpoint :: Context -> IO Fixedpoint
mkFixedpoint = (Ptr Z3_context -> IO (Ptr Z3_fixedpoint))
-> Context -> IO Fixedpoint
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_fixedpoint)
z3_mk_fixedpoint

fixedpointAddRule :: Context -> Fixedpoint -> AST -> Symbol -> IO ()
fixedpointAddRule :: Context -> Fixedpoint -> AST -> Symbol -> IO ()
fixedpointAddRule = (Ptr Z3_context
 -> Ptr Z3_fixedpoint -> Ptr Z3_ast -> Ptr Z3_symbol -> IO ())
-> Context -> Fixedpoint -> AST -> Symbol -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_fixedpoint -> Ptr Z3_ast -> Ptr Z3_symbol -> IO ()
z3_fixedpoint_add_rule

fixedpointSetParams :: Context -> Fixedpoint -> Params -> IO ()
fixedpointSetParams :: Context -> Fixedpoint -> Params -> IO ()
fixedpointSetParams = (Ptr Z3_context -> Ptr Z3_fixedpoint -> Ptr Z3_params -> IO ())
-> Context -> Fixedpoint -> Params -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_fixedpoint -> Ptr Z3_params -> IO ()
z3_fixedpoint_set_params

fixedpointRegisterRelation :: Context -> Fixedpoint -> FuncDecl -> IO ()
fixedpointRegisterRelation :: Context -> Fixedpoint -> FuncDecl -> IO ()
fixedpointRegisterRelation = (Ptr Z3_context -> Ptr Z3_fixedpoint -> Ptr Z3_func_decl -> IO ())
-> Context -> Fixedpoint -> FuncDecl -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_fixedpoint -> Ptr Z3_func_decl -> IO ()
z3_fixedpoint_register_relation

fixedpointQueryRelations :: Context -> Fixedpoint -> [FuncDecl] -> IO Result
fixedpointQueryRelations :: Context -> Fixedpoint -> [FuncDecl] -> IO Result
fixedpointQueryRelations ctx :: Context
ctx fixedpoint :: Fixedpoint
fixedpoint fds :: [FuncDecl]
fds =
  (Ptr Z3_context
 -> Ptr Z3_fixedpoint
 -> CUInt
 -> Ptr (Ptr Z3_func_decl)
 -> IO Z3_lbool)
-> Context
-> ((Ptr Z3_fixedpoint
     -> CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool)
    -> IO Z3_lbool)
-> IO Result
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_fixedpoint
-> CUInt
-> Ptr (Ptr Z3_func_decl)
-> IO Z3_lbool
z3_fixedpoint_query_relations Context
ctx (((Ptr Z3_fixedpoint
   -> CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool)
  -> IO Z3_lbool)
 -> IO Result)
-> ((Ptr Z3_fixedpoint
     -> CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool)
    -> IO Z3_lbool)
-> IO Result
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_fixedpoint -> CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool
f ->
    Fixedpoint -> (Ptr Z3_fixedpoint -> IO Z3_lbool) -> IO Z3_lbool
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Fixedpoint
fixedpoint ((Ptr Z3_fixedpoint -> IO Z3_lbool) -> IO Z3_lbool)
-> (Ptr Z3_fixedpoint -> IO Z3_lbool) -> IO Z3_lbool
forall a b. (a -> b) -> a -> b
$ \fixedpointPtr :: Ptr Z3_fixedpoint
fixedpointPtr ->
    [FuncDecl]
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool) -> IO Z3_lbool
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [FuncDecl]
fds ((CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool) -> IO Z3_lbool)
-> (CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool) -> IO Z3_lbool
forall a b. (a -> b) -> a -> b
$ \fdsNum :: CUInt
fdsNum fdsArr :: Ptr (Ptr Z3_func_decl)
fdsArr ->
      Ptr Z3_fixedpoint -> CUInt -> Ptr (Ptr Z3_func_decl) -> IO Z3_lbool
f Ptr Z3_fixedpoint
fixedpointPtr CUInt
fdsNum Ptr (Ptr Z3_func_decl)
fdsArr

fixedpointGetAnswer :: Context -> Fixedpoint -> IO AST
fixedpointGetAnswer :: Context -> Fixedpoint -> IO AST
fixedpointGetAnswer = (Ptr Z3_context -> Ptr Z3_fixedpoint -> IO (Ptr Z3_ast))
-> Context -> Fixedpoint -> IO AST
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_fixedpoint -> IO (Ptr Z3_ast)
z3_fixedpoint_get_answer

fixedpointGetAssertions :: Context -> Fixedpoint -> IO [AST]
fixedpointGetAssertions :: Context -> Fixedpoint -> IO [AST]
fixedpointGetAssertions = (Ptr Z3_context -> Ptr Z3_fixedpoint -> IO (Ptr Z3_ast_vector))
-> Context -> Fixedpoint -> IO [AST]
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_fixedpoint -> IO (Ptr Z3_ast_vector)
z3_fixedpoint_get_assertions

-- AST vectors ?

-- AST maps ?

---------------------------------------------------------------------
-- Goals

-- TODO

---------------------------------------------------------------------
-- Tactics and Probes

-- TODO

---------------------------------------------------------------------
-- Solvers

-- | Solvers available in Z3.
--
-- These are described at <http://smtlib.cs.uiowa.edu/logics.html>
data Logic
  = AUFLIA
    -- ^ Closed formulas over the theory of linear integer arithmetic
    -- and arrays extended with free sort and function symbols but
    -- restricted to arrays with integer indices and values.

  | AUFLIRA
    -- ^ Closed linear formulas with free sort and function symbols over
    -- one- and two-dimentional arrays of integer index and real
    -- value.

  | AUFNIRA
    -- ^ Closed formulas with free function and predicate symbols over a
    -- theory of arrays of arrays of integer index and real value.

  | LRA
    -- ^ Closed linear formulas in linear real arithmetic.

  | QF_ABV
    -- ^ Closed quantifier-free formulas over the theory of bitvectors
    -- and bitvector arrays.

  | QF_AUFBV
    -- ^ Closed quantifier-free formulas over the theory of bitvectors
    -- and bitvector arrays extended with free sort and function
    -- symbols.

  | QF_AUFLIA
    -- ^ Closed quantifier-free linear formulas over the theory of
    -- integer arrays extended with free sort and function symbols.

  | QF_AX
    -- ^ Closed quantifier-free formulas over the theory of arrays with
    -- extensionality.

  | QF_BV
    -- ^ Closed quantifier-free formulas over the theory of fixed-size
    -- bitvectors.

  | QF_IDL
    -- ^ Difference Logic over the integers. In essence, Boolean
    -- combinations of inequations of the form x - y < b where x and y
    -- are integer variables and b is an integer constant.

  | QF_LIA
    -- ^ Unquantified linear integer arithmetic. In essence, Boolean
    -- combinations of inequations between linear polynomials over
    -- integer variables.

  | QF_LRA
    -- ^ Unquantified linear real arithmetic. In essence, Boolean
    -- combinations of inequations between linear polynomials over
    -- real variables.

  | QF_NIA
    -- ^ Quantifier-free integer arithmetic.

  | QF_NRA
    -- ^ Quantifier-free real arithmetic.

  | QF_RDL
    -- ^ Difference Logic over the reals. In essence, Boolean
    -- combinations of inequations of the form x - y < b where x and y
    -- are real variables and b is a rational constant.

  | QF_UF
    -- ^ Unquantified formulas built over a signature of uninterpreted
    -- (i.e., free) sort and function symbols.

  | QF_UFBV
    -- ^ Unquantified formulas over bitvectors with uninterpreted sort
    -- function and symbols.

  | QF_UFIDL
    -- ^ Difference Logic over the integers (in essence) but with
    -- uninterpreted sort and function symbols.

  | QF_UFLIA
    -- ^ Unquantified linear integer arithmetic with uninterpreted sort
    -- and function symbols.

  | QF_UFLRA
    -- ^ Unquantified linear real arithmetic with uninterpreted sort and
    -- function symbols.

  | QF_UFNRA
    -- ^ Unquantified non-linear real arithmetic with uninterpreted sort
    -- and function symbols.

  | UFLRA
    -- ^ Linear real arithmetic with uninterpreted sort and function
    -- symbols.

  | UFNIA
    -- ^ Non-linear integer arithmetic with uninterpreted sort and
    -- function symbols.

instance Show Logic where
  show :: Logic -> String
show AUFLIA    = "AUFLIA"
  show AUFLIRA   = "AUFLIRA"
  show AUFNIRA   = "AUFNIRA"
  show LRA       = "LRA"
  show QF_ABV    = "QF_ABV"
  show QF_AUFBV  = "QF_AUFBV"
  show QF_AUFLIA = "QF_AUFLIA"
  show QF_AX     = "QF_AX"
  show QF_BV     = "QF_BV"
  show QF_IDL    = "QF_IDL"
  show QF_LIA    = "QF_LIA"
  show QF_LRA    = "QF_LRA"
  show QF_NIA    = "QF_NIA"
  show QF_NRA    = "QF_NRA"
  show QF_RDL    = "QF_RDL"
  show QF_UF     = "QF_UF"
  show QF_UFBV   = "QF_UFBV"
  show QF_UFIDL  = "QF_UFIDL"
  show QF_UFLIA  = "QF_UFLIA"
  show QF_UFLRA  = "QF_UFLRA"
  show QF_UFNRA  = "QF_UFNRA"
  show UFLRA     = "UFLRA"
  show UFNIA     = "UFNIA"

mkSolver :: Context -> IO Solver
mkSolver :: Context -> IO Solver
mkSolver = (Ptr Z3_context -> IO (Ptr Z3_solver)) -> Context -> IO Solver
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_solver)
z3_mk_solver

mkSimpleSolver :: Context -> IO Solver
mkSimpleSolver :: Context -> IO Solver
mkSimpleSolver = (Ptr Z3_context -> IO (Ptr Z3_solver)) -> Context -> IO Solver
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_solver)
z3_mk_simple_solver

mkSolverForLogic :: Context -> Logic -> IO Solver
mkSolverForLogic :: Context -> Logic -> IO Solver
mkSolverForLogic c :: Context
c logic :: Logic
logic = Context -> String -> IO Symbol
mkStringSymbol Context
c (Logic -> String
forall a. Show a => a -> String
show Logic
logic) IO Symbol -> (Symbol -> IO Solver) -> IO Solver
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \sym :: Symbol
sym ->
  Context -> (Ptr Z3_context -> IO (Ptr Z3_solver)) -> IO Solver
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO (Ptr Z3_solver)) -> IO Solver)
-> (Ptr Z3_context -> IO (Ptr Z3_solver)) -> IO Solver
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> Ptr Z3_symbol -> IO (Ptr Z3_solver)
z3_mk_solver_for_logic Ptr Z3_context
cPtr (Ptr Z3_symbol -> IO (Ptr Z3_solver))
-> Ptr Z3_symbol -> IO (Ptr Z3_solver)
forall a b. (a -> b) -> a -> b
$ Symbol -> Ptr Z3_symbol
unSymbol Symbol
sym

-- | Return a string describing all solver available parameters.
solverGetHelp :: Context -> Solver -> IO String
solverGetHelp :: Context -> Solver -> IO String
solverGetHelp = (Ptr Z3_context -> Ptr Z3_solver -> IO CString)
-> Context -> Solver -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO CString
z3_solver_get_help

-- | Set the given solver using the given parameters.
solverSetParams :: Context -> Solver -> Params -> IO ()
solverSetParams :: Context -> Solver -> Params -> IO ()
solverSetParams = (Ptr Z3_context -> Ptr Z3_solver -> Ptr Z3_params -> IO ())
-> Context -> Solver -> Params -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_solver -> Ptr Z3_params -> IO ()
z3_solver_set_params

solverPush :: Context -> Solver -> IO ()
solverPush :: Context -> Solver -> IO ()
solverPush = (Ptr Z3_context -> Ptr Z3_solver -> IO ())
-> Context -> Solver -> IO ()
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO ()
z3_solver_push

solverPop :: Context -> Solver -> Int -> IO ()
solverPop :: Context -> Solver -> Int -> IO ()
solverPop = (Ptr Z3_context -> Ptr Z3_solver -> CUInt -> IO ())
-> Context -> Solver -> Int -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_solver -> CUInt -> IO ()
z3_solver_pop

solverReset :: Context -> Solver -> IO ()
solverReset :: Context -> Solver -> IO ()
solverReset = (Ptr Z3_context -> Ptr Z3_solver -> IO ())
-> Context -> Solver -> IO ()
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO ()
z3_solver_reset

-- | Number of backtracking points.
solverGetNumScopes :: Context -> Solver -> IO Int
solverGetNumScopes :: Context -> Solver -> IO Int
solverGetNumScopes = (Ptr Z3_context -> Ptr Z3_solver -> IO CUInt)
-> Context -> Solver -> IO Int
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO CUInt
z3_solver_get_num_scopes

solverAssertCnstr :: Context -> Solver -> AST -> IO ()
solverAssertCnstr :: Context -> Solver -> AST -> IO ()
solverAssertCnstr = (Ptr Z3_context -> Ptr Z3_solver -> Ptr Z3_ast -> IO ())
-> Context -> Solver -> AST -> IO ()
forall ah ac bh bc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 Ptr Z3_context -> Ptr Z3_solver -> Ptr Z3_ast -> IO ()
z3_solver_assert

solverAssertAndTrack :: Context -> Solver -> AST -> AST -> IO ()
solverAssertAndTrack :: Context -> Solver -> AST -> AST -> IO ()
solverAssertAndTrack = (Ptr Z3_context
 -> Ptr Z3_solver -> Ptr Z3_ast -> Ptr Z3_ast -> IO ())
-> Context -> Solver -> AST -> AST -> IO ()
forall ah ac bh bc ch cc rh rc.
(Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
(Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 Ptr Z3_context
-> Ptr Z3_solver -> Ptr Z3_ast -> Ptr Z3_ast -> IO ()
z3_solver_assert_and_track

-- | Check whether the assertions in a given solver are consistent or not.
solverCheck :: Context -> Solver -> IO Result
solverCheck :: Context -> Solver -> IO Result
solverCheck ctx :: Context
ctx solver :: Solver
solver = (Ptr Z3_context -> Ptr Z3_solver -> IO Z3_lbool)
-> Context
-> ((Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool)
-> IO Result
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> Ptr Z3_solver -> IO Z3_lbool
z3_solver_check Context
ctx (((Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool) -> IO Result)
-> ((Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool) -> IO Result
forall a b. (a -> b) -> a -> b
$ Solver -> (Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Solver
solver

-- | Check whether the assertions in the given solver and optional assumptions are consistent or not.
solverCheckAssumptions :: Context -> Solver -> [AST] -> IO Result
solverCheckAssumptions :: Context -> Solver -> [AST] -> IO Result
solverCheckAssumptions ctx :: Context
ctx solver :: Solver
solver assump :: [AST]
assump =
  (Ptr Z3_context
 -> Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool)
-> Context
-> ((Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool)
    -> IO Z3_lbool)
-> IO Result
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context
-> Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool
z3_solver_check_assumptions Context
ctx (((Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool)
  -> IO Z3_lbool)
 -> IO Result)
-> ((Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool)
    -> IO Z3_lbool)
-> IO Result
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool
f ->
    Solver -> (Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Solver
solver ((Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool)
-> (Ptr Z3_solver -> IO Z3_lbool) -> IO Z3_lbool
forall a b. (a -> b) -> a -> b
$ \solverPtr :: Ptr Z3_solver
solverPtr ->
    [AST] -> (CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool) -> IO Z3_lbool
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen [AST]
assump ((CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool) -> IO Z3_lbool)
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool) -> IO Z3_lbool
forall a b. (a -> b) -> a -> b
$ \assumpNum :: CUInt
assumpNum assumpArr :: Ptr (Ptr Z3_ast)
assumpArr ->
      Ptr Z3_solver -> CUInt -> Ptr (Ptr Z3_ast) -> IO Z3_lbool
f Ptr Z3_solver
solverPtr CUInt
assumpNum Ptr (Ptr Z3_ast)
assumpArr

-- | Retrieve the model for the last 'solverCheck'.
--
-- The error handler is invoked if a model is not available because
-- the commands above were not invoked for the given solver,
-- or if the result was 'Unsat'.
solverGetModel :: Context -> Solver -> IO Model
solverGetModel :: Context -> Solver -> IO Model
solverGetModel ctx :: Context
ctx solver :: Solver
solver = (Ptr Z3_context -> Ptr Z3_solver -> IO (Ptr Z3_model))
-> Context
-> ((Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model))
-> IO Model
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> Ptr Z3_solver -> IO (Ptr Z3_model)
z3_solver_get_model Context
ctx (((Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model))
 -> IO Model)
-> ((Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model))
-> IO Model
forall a b. (a -> b) -> a -> b
$ \f :: Ptr Z3_solver -> IO (Ptr Z3_model)
f ->
  Solver -> (Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model)
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c Solver
solver ((Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model))
-> (Ptr Z3_solver -> IO (Ptr Z3_model)) -> IO (Ptr Z3_model)
forall a b. (a -> b) -> a -> b
$ \solverPtr :: Ptr Z3_solver
solverPtr ->
    Ptr Z3_solver -> IO (Ptr Z3_model)
f Ptr Z3_solver
solverPtr

-- | Retrieve the unsat core for the last 'solverCheckAssumptions'; the unsat core is a subset of the assumptions
solverGetUnsatCore :: Context -> Solver -> IO [AST]
solverGetUnsatCore :: Context -> Solver -> IO [AST]
solverGetUnsatCore = (Ptr Z3_context -> Ptr Z3_solver -> IO (Ptr Z3_ast_vector))
-> Context -> Solver -> IO [AST]
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO (Ptr Z3_ast_vector)
z3_solver_get_unsat_core

-- | Return a brief justification for an 'Unknown' result for the commands 'solverCheck' and 'solverCheckAssumptions'.
solverGetReasonUnknown :: Context -> Solver -> IO String
solverGetReasonUnknown :: Context -> Solver -> IO String
solverGetReasonUnknown = (Ptr Z3_context -> Ptr Z3_solver -> IO CString)
-> Context -> Solver -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO CString
z3_solver_get_reason_unknown

-- | Convert the given solver into a string.
solverToString :: Context -> Solver -> IO String
solverToString :: Context -> Solver -> IO String
solverToString = (Ptr Z3_context -> Ptr Z3_solver -> IO CString)
-> Context -> Solver -> IO String
forall ah ac rh rc.
(Marshal ah ac, Marshal rh rc) =>
(Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 Ptr Z3_context -> Ptr Z3_solver -> IO CString
z3_solver_to_string

-------------------------------------------------
-- ** Helpers

solverCheckAndGetModel :: Context -> Solver -> IO (Result, Maybe Model)
solverCheckAndGetModel :: Context -> Solver -> IO (Result, Maybe Model)
solverCheckAndGetModel ctx :: Context
ctx solver :: Solver
solver =
  do Result
res <- Context -> Solver -> IO Result
solverCheck Context
ctx Solver
solver
     Maybe Model
mbModel <- case Result
res of
                  Unsat -> Maybe Model -> IO (Maybe Model)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Model
forall a. Maybe a
Nothing
                  _     -> Model -> Maybe Model
forall a. a -> Maybe a
Just (Model -> Maybe Model) -> IO Model -> IO (Maybe Model)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Context -> Solver -> IO Model
solverGetModel Context
ctx Solver
solver
     (Result, Maybe Model) -> IO (Result, Maybe Model)
forall (m :: * -> *) a. Monad m => a -> m a
return (Result
res, Maybe Model
mbModel)

---------------------------------------------------------------------
-- Marshalling

{- MARSHALLING HELPERS

We try to get rid of most of the marshalling boilerplate which, by the way,
is going to be essential for transitioning to Z3 4 API.

Most API functions can be lifted using 'liftFun'{0-3} helpers. Otherwise try
using 'marshal'. Worst case scenario, write the marshalling code yourself.

-}

-- withIntegral :: (Integral a, Integral b) => a -> (b -> r) -> r
-- withIntegral x f = f (fromIntegral x)

withContext :: Context -> (Ptr Z3_context -> IO r) -> IO r
withContext :: Context -> (Ptr Z3_context -> IO r) -> IO r
withContext c :: Context
c = ForeignPtr Z3_context -> (Ptr Z3_context -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Context -> ForeignPtr Z3_context
unContext Context
c)

withContextError :: Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError :: Context -> (Ptr Z3_context -> IO r) -> IO r
withContextError c :: Context
c f :: Ptr Z3_context -> IO r
f = Context -> (Ptr Z3_context -> IO r) -> IO r
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
c ((Ptr Z3_context -> IO r) -> IO r)
-> (Ptr Z3_context -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> IO r -> IO r
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (Ptr Z3_context -> IO r
f Ptr Z3_context
cPtr)

marshalArray :: (Marshal h c, Storable c) => [h] -> (Ptr c -> IO a) -> IO a
marshalArray :: [h] -> (Ptr c -> IO a) -> IO a
marshalArray hs :: [h]
hs f :: Ptr c -> IO a
f = [h] -> ([c] -> IO a) -> IO a
forall h c r. Marshal h c => [h] -> ([c] -> IO r) -> IO r
hs2cs [h]
hs (([c] -> IO a) -> IO a) -> ([c] -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \cs :: [c]
cs -> [c] -> (Ptr c -> IO a) -> IO a
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [c]
cs Ptr c -> IO a
f

marshalArrayLen :: (Marshal h c, Storable c, Integral i) =>
    [h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen :: [h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen hs :: [h]
hs f :: i -> Ptr c -> IO a
f =
  [h] -> ([c] -> IO a) -> IO a
forall h c r. Marshal h c => [h] -> ([c] -> IO r) -> IO r
hs2cs [h]
hs (([c] -> IO a) -> IO a) -> ([c] -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \cs :: [c]
cs -> [c] -> (Int -> Ptr c -> IO a) -> IO a
forall a b. Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b
withArrayLen [c]
cs ((Int -> Ptr c -> IO a) -> IO a) -> (Int -> Ptr c -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \n :: Int
n -> i -> Ptr c -> IO a
f (Int -> i
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)

-- marshalMaybeArray :: (Marshal h c, Storable c) => Maybe [h] -> (Ptr c -> IO a) -> IO a
-- marshalMaybeArray Nothing   f = f nullPtr
-- marshalMaybeArray (Just hs) f = marshalArray hs f

liftAstN :: (Ptr Z3_context -> IO (Ptr Z3_ast))
         -> (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
         -> Context -> [AST] -> IO AST
liftAstN :: (Ptr Z3_context -> IO (Ptr Z3_ast))
-> (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> [AST]
-> IO AST
liftAstN s :: Ptr Z3_context -> IO (Ptr Z3_ast)
s f :: Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f c :: Context
c = IO AST
-> (NonEmpty AST -> IO AST) -> Maybe (NonEmpty AST) -> IO AST
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((Ptr Z3_context -> IO (Ptr Z3_ast)) -> Context -> IO AST
forall rh rc.
Marshal rh rc =>
(Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 Ptr Z3_context -> IO (Ptr Z3_ast)
s Context
c) ((Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context -> NonEmpty AST -> IO AST
liftAstN1 Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f Context
c) (Maybe (NonEmpty AST) -> IO AST)
-> ([AST] -> Maybe (NonEmpty AST)) -> [AST] -> IO AST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AST] -> Maybe (NonEmpty AST)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty
{-# INLINE liftAstN #-}

liftAstN1 :: (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
          -> Context -> NonEmpty AST -> IO AST
liftAstN1 :: (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context -> NonEmpty AST -> IO AST
liftAstN1 f :: Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f c :: Context
c = (Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> Context
-> ((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> IO AST
forall rh rc t.
Marshal rh rc =>
(Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal Ptr Z3_context -> CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast)
f Context
c (((CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
  -> IO (Ptr Z3_ast))
 -> IO AST)
-> (NonEmpty AST
    -> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
    -> IO (Ptr Z3_ast))
-> NonEmpty AST
-> IO AST
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AST]
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall h c i a.
(Marshal h c, Storable c, Integral i) =>
[h] -> (i -> Ptr c -> IO a) -> IO a
marshalArrayLen ([AST]
 -> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
 -> IO (Ptr Z3_ast))
-> (NonEmpty AST -> [AST])
-> NonEmpty AST
-> (CUInt -> Ptr (Ptr Z3_ast) -> IO (Ptr Z3_ast))
-> IO (Ptr Z3_ast)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty AST -> [AST]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
{-# INLINE liftAstN1 #-}

class Marshal h c where
  c2h :: Context -> c -> IO h
  h2c :: h -> (c -> IO r) -> IO r

hs2cs :: Marshal h c => [h] -> ([c] -> IO r) -> IO r
hs2cs :: [h] -> ([c] -> IO r) -> IO r
hs2cs []     f :: [c] -> IO r
f = [c] -> IO r
f []
hs2cs (h :: h
h:hs :: [h]
hs) f :: [c] -> IO r
f =
  h -> (c -> IO r) -> IO r
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c h
h ((c -> IO r) -> IO r) -> (c -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \c :: c
c ->
  [h] -> ([c] -> IO r) -> IO r
forall h c r. Marshal h c => [h] -> ([c] -> IO r) -> IO r
hs2cs [h]
hs (([c] -> IO r) -> IO r) -> ([c] -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \cs :: [c]
cs -> [c] -> IO r
f (c
cc -> [c] -> [c]
forall a. a -> [a] -> [a]
:[c]
cs)

cs2hs :: Marshal h c => Context -> [c] -> IO [h]
cs2hs :: Context -> [c] -> IO [h]
cs2hs ctx :: Context
ctx = (c -> IO h) -> [c] -> IO [h]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Context -> c -> IO h
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
ctx)

-- peekToHs :: (Marshal h c, Storable c) => Context -> Ptr c -> IO h
-- peekToHs c ptr = c2h c =<< peek ptr

-- peekToMaybeHs :: (Marshal h (Ptr c), Storable c) => Context -> Ptr (Ptr c) -> IO (Maybe h)
-- peekToMaybeHs c = peek >=> (c2h c `T.traverse`) . ptrToMaybe

peekArrayToHs :: (Marshal h c, Storable c) => Context -> Int -> Ptr c -> IO [h]
peekArrayToHs :: Context -> Int -> Ptr c -> IO [h]
peekArrayToHs c :: Context
c n :: Int
n dPtr :: Ptr c
dPtr =
  Context -> [c] -> IO [h]
forall h c. Marshal h c => Context -> [c] -> IO [h]
cs2hs Context
c ([c] -> IO [h]) -> IO [c] -> IO [h]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> Ptr c -> IO [c]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray Int
n Ptr c
dPtr

-- peekPtrArrayToHs :: (Marshal h c, Storable c) => Context -> Int -> Ptr (Ptr c) -> IO [h]
-- peekPtrArrayToHs c n = peekArray n >=> mapM (peekToHs c)

toHsCheckError :: Marshal h c => Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError :: Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError c :: Context
c f :: Ptr Z3_context -> IO c
f = Context -> (Ptr Z3_context -> IO h) -> IO h
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
c ((Ptr Z3_context -> IO h) -> IO h)
-> (Ptr Z3_context -> IO h) -> IO h
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr ->
  Context -> c -> IO h
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c (c -> IO h) -> IO c -> IO h
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Z3_context -> IO c -> IO c
forall a. Ptr Z3_context -> IO a -> IO a
checkError Ptr Z3_context
cPtr (Ptr Z3_context -> IO c
f Ptr Z3_context
cPtr)

type Z3SetRefCount c = Ptr Z3_context -> Ptr c -> IO ()
type Z3IncRefFun c = Z3SetRefCount c
type Z3DecRefFun c = Z3SetRefCount c

mkC2hRefCount :: (ForeignPtr c -> h)
                   -> Z3IncRefFun c
                   -> Z3DecRefFun c
                   -> Context -> Ptr c -> IO h
mkC2hRefCount :: (ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount mk :: ForeignPtr c -> h
mk incRef :: Z3IncRefFun c
incRef decRef :: Z3IncRefFun c
decRef ctx :: Context
ctx xPtr :: Ptr c
xPtr =
  Context -> (Ptr Z3_context -> IO h) -> IO h
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
ctx ((Ptr Z3_context -> IO h) -> IO h)
-> (Ptr Z3_context -> IO h) -> IO h
forall a b. (a -> b) -> a -> b
$ \ctxPtr :: Ptr Z3_context
ctxPtr -> do
    Z3IncRefFun c
incRef Ptr Z3_context
ctxPtr Ptr c
xPtr
    Context -> IO ()
contextIncRef Context
ctx
    let xFinalizer :: IO ()
xFinalizer = do
        Z3IncRefFun c
decRef Ptr Z3_context
ctxPtr Ptr c
xPtr
        Ptr Z3_context -> IORef Word -> IO ()
contextDecRef Ptr Z3_context
ctxPtr (Context -> IORef Word
refCount Context
ctx)
    ForeignPtr c -> h
mk (ForeignPtr c -> h) -> IO (ForeignPtr c) -> IO h
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr c -> IO () -> IO (ForeignPtr c)
forall a. Ptr a -> IO () -> IO (ForeignPtr a)
newForeignPtr Ptr c
xPtr IO ()
xFinalizer

dummy_inc_ref :: Z3IncRefFun c
dummy_inc_ref :: Z3IncRefFun c
dummy_inc_ref _ _ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

on_ast_ptr :: Z3SetRefCount Z3_ast
            -> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast))
            -> Z3SetRefCount a
f :: Z3SetRefCount Z3_ast
f on_ast_ptr :: Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` t :: Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)
t = \ctxPtr :: Ptr Z3_context
ctxPtr ptr :: Ptr a
ptr -> Z3SetRefCount Z3_ast
f Ptr Z3_context
ctxPtr (Ptr Z3_ast -> IO ()) -> IO (Ptr Z3_ast) -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)
t Ptr Z3_context
ctxPtr Ptr a
ptr

instance Marshal h (Ptr x) => Marshal (Maybe h) (Ptr x) where
  c2h :: Context -> Ptr x -> IO (Maybe h)
c2h c :: Context
c = (Ptr x -> IO h) -> Maybe (Ptr x) -> IO (Maybe h)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
T.mapM (Context -> Ptr x -> IO h
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
c) (Maybe (Ptr x) -> IO (Maybe h))
-> (Ptr x -> Maybe (Ptr x)) -> Ptr x -> IO (Maybe h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr x -> Maybe (Ptr x)
forall a. Ptr a -> Maybe (Ptr a)
ptrToMaybe
  h2c :: Maybe h -> (Ptr x -> IO r) -> IO r
h2c Nothing  f :: Ptr x -> IO r
f = Ptr x -> IO r
f Ptr x
forall a. Ptr a
nullPtr
  h2c (Just x :: h
x) f :: Ptr x -> IO r
f = h -> (Ptr x -> IO r) -> IO r
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c h
x Ptr x -> IO r
f

instance Marshal () () where
  c2h :: Context -> () -> IO ()
c2h _ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return
  h2c :: () -> (() -> IO r) -> IO r
h2c x :: ()
x f :: () -> IO r
f = () -> IO r
f ()
x

instance Marshal Bool Z3_bool where
  c2h :: Context -> Z3_bool -> IO Bool
c2h _ = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> IO Bool) -> (Z3_bool -> Bool) -> Z3_bool -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Z3_bool -> Bool
toBool
  h2c :: Bool -> (Z3_bool -> IO r) -> IO r
h2c b :: Bool
b f :: Z3_bool -> IO r
f = Z3_bool -> IO r
f (Bool -> Z3_bool
unBool Bool
b)

instance Marshal Result Z3_lbool where
  c2h :: Context -> Z3_lbool -> IO Result
c2h _ = Result -> IO Result
forall (m :: * -> *) a. Monad m => a -> m a
return (Result -> IO Result)
-> (Z3_lbool -> Result) -> Z3_lbool -> IO Result
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Z3_lbool -> Result
toResult
  h2c :: Result -> (Z3_lbool -> IO r) -> IO r
h2c = String -> Result -> (Z3_lbool -> IO r) -> IO r
forall a. HasCallStack => String -> a
error "Marshal Result Z3_lbool => h2c not implemented"

instance Integral h => Marshal h CInt where
  c2h :: Context -> CInt -> IO h
c2h _ = h -> IO h
forall (m :: * -> *) a. Monad m => a -> m a
return (h -> IO h) -> (CInt -> h) -> CInt -> IO h
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> h
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  h2c :: h -> (CInt -> IO r) -> IO r
h2c i :: h
i f :: CInt -> IO r
f = CInt -> IO r
f (h -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral h
i)

instance Integral h => Marshal h CUInt where
  c2h :: Context -> CUInt -> IO h
c2h _ = h -> IO h
forall (m :: * -> *) a. Monad m => a -> m a
return (h -> IO h) -> (CUInt -> h) -> CUInt -> IO h
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CUInt -> h
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  h2c :: h -> (CUInt -> IO r) -> IO r
h2c i :: h
i f :: CUInt -> IO r
f = CUInt -> IO r
f (h -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral h
i)

instance Integral h => Marshal h CLLong where
  c2h :: Context -> CLLong -> IO h
c2h _ = h -> IO h
forall (m :: * -> *) a. Monad m => a -> m a
return (h -> IO h) -> (CLLong -> h) -> CLLong -> IO h
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CLLong -> h
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  h2c :: h -> (CLLong -> IO r) -> IO r
h2c i :: h
i f :: CLLong -> IO r
f = CLLong -> IO r
f (h -> CLLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral h
i)

instance Integral h => Marshal h CULLong where
  c2h :: Context -> CULLong -> IO h
c2h _ = h -> IO h
forall (m :: * -> *) a. Monad m => a -> m a
return (h -> IO h) -> (CULLong -> h) -> CULLong -> IO h
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CULLong -> h
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  h2c :: h -> (CULLong -> IO r) -> IO r
h2c i :: h
i f :: CULLong -> IO r
f = CULLong -> IO r
f (h -> CULLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral h
i)

instance Marshal Double CDouble where
  c2h :: Context -> CDouble -> IO Double
c2h _ = Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Double -> IO Double)
-> (CDouble -> Double) -> CDouble -> IO Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac
  h2c :: Double -> (CDouble -> IO r) -> IO r
h2c d :: Double
d f :: CDouble -> IO r
f = CDouble -> IO r
f (Double -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
d)

instance Marshal String CString where
  c2h :: Context -> CString -> IO String
c2h _ = CString -> IO String
peekCString
  h2c :: String -> (CString -> IO r) -> IO r
h2c   = String -> (CString -> IO r) -> IO r
forall a. String -> (CString -> IO a) -> IO a
withCString

instance Marshal App (Ptr Z3_app) where
  c2h :: Context -> Ptr Z3_app -> IO App
c2h = (ForeignPtr Z3_app -> App)
-> Z3IncRefFun Z3_app
-> Z3IncRefFun Z3_app
-> Context
-> Ptr Z3_app
-> IO App
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_app -> App
App
            (Z3SetRefCount Z3_ast
z3_inc_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_app
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast)
z3_app_to_ast)
            (Z3SetRefCount Z3_ast
z3_dec_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_app
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_app -> IO (Ptr Z3_ast)
z3_app_to_ast)
  h2c :: App -> (Ptr Z3_app -> IO r) -> IO r
h2c app :: App
app = ForeignPtr Z3_app -> (Ptr Z3_app -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (App -> ForeignPtr Z3_app
unApp App
app)

instance Marshal Params (Ptr Z3_params) where
  c2h :: Context -> Ptr Z3_params -> IO Params
c2h = (ForeignPtr Z3_params -> Params)
-> Z3IncRefFun Z3_params
-> Z3IncRefFun Z3_params
-> Context
-> Ptr Z3_params
-> IO Params
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_params -> Params
Params Z3IncRefFun Z3_params
z3_params_inc_ref Z3IncRefFun Z3_params
z3_params_dec_ref
  h2c :: Params -> (Ptr Z3_params -> IO r) -> IO r
h2c prm :: Params
prm = ForeignPtr Z3_params -> (Ptr Z3_params -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Params -> ForeignPtr Z3_params
unParams Params
prm)

instance Marshal Symbol (Ptr Z3_symbol) where
  c2h :: Context -> Ptr Z3_symbol -> IO Symbol
c2h _ = Symbol -> IO Symbol
forall (m :: * -> *) a. Monad m => a -> m a
return (Symbol -> IO Symbol)
-> (Ptr Z3_symbol -> Symbol) -> Ptr Z3_symbol -> IO Symbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Z3_symbol -> Symbol
Symbol
  h2c :: Symbol -> (Ptr Z3_symbol -> IO r) -> IO r
h2c s :: Symbol
s f :: Ptr Z3_symbol -> IO r
f = Ptr Z3_symbol -> IO r
f (Symbol -> Ptr Z3_symbol
unSymbol Symbol
s)

instance Marshal AST (Ptr Z3_ast) where
  c2h :: Context -> Ptr Z3_ast -> IO AST
c2h = (ForeignPtr Z3_ast -> AST)
-> Z3SetRefCount Z3_ast
-> Z3SetRefCount Z3_ast
-> Context
-> Ptr Z3_ast
-> IO AST
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_ast -> AST
AST Z3SetRefCount Z3_ast
z3_inc_ref Z3SetRefCount Z3_ast
z3_dec_ref
  h2c :: AST -> (Ptr Z3_ast -> IO r) -> IO r
h2c a :: AST
a f :: Ptr Z3_ast -> IO r
f = ForeignPtr Z3_ast -> (Ptr Z3_ast -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (AST -> ForeignPtr Z3_ast
unAST AST
a) Ptr Z3_ast -> IO r
f

instance Marshal [AST] (Ptr Z3_ast_vector) where
  c2h :: Context -> Ptr Z3_ast_vector -> IO [AST]
c2h ctx :: Context
ctx vecPtr :: Ptr Z3_ast_vector
vecPtr = Context -> (Ptr Z3_context -> IO [AST]) -> IO [AST]
forall r. Context -> (Ptr Z3_context -> IO r) -> IO r
withContext Context
ctx ((Ptr Z3_context -> IO [AST]) -> IO [AST])
-> (Ptr Z3_context -> IO [AST]) -> IO [AST]
forall a b. (a -> b) -> a -> b
$ \ctxPtr :: Ptr Z3_context
ctxPtr -> do
    Ptr Z3_context -> Ptr Z3_ast_vector -> IO ()
z3_ast_vector_inc_ref Ptr Z3_context
ctxPtr Ptr Z3_ast_vector
vecPtr
    CUInt
n <- Ptr Z3_context -> Ptr Z3_ast_vector -> IO CUInt
z3_ast_vector_size Ptr Z3_context
ctxPtr Ptr Z3_ast_vector
vecPtr
    [AST]
res <- if CUInt
n CUInt -> CUInt -> Bool
forall a. Eq a => a -> a -> Bool
== 0 -- Need an explicit check, since n is unsigned so n - 1 might overflow
              then [AST] -> IO [AST]
forall (m :: * -> *) a. Monad m => a -> m a
return []
              else (CUInt -> IO AST) -> [CUInt] -> IO [AST]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\i :: CUInt
i -> Ptr Z3_context -> Ptr Z3_ast_vector -> CUInt -> IO (Ptr Z3_ast)
z3_ast_vector_get Ptr Z3_context
ctxPtr Ptr Z3_ast_vector
vecPtr CUInt
i IO (Ptr Z3_ast) -> (Ptr Z3_ast -> IO AST) -> IO AST
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Context -> Ptr Z3_ast -> IO AST
forall h c. Marshal h c => Context -> c -> IO h
c2h Context
ctx) [0 .. (CUInt
n CUInt -> CUInt -> CUInt
forall a. Num a => a -> a -> a
- 1)]
    Ptr Z3_context -> Ptr Z3_ast_vector -> IO ()
z3_ast_vector_dec_ref Ptr Z3_context
ctxPtr Ptr Z3_ast_vector
vecPtr
    [AST] -> IO [AST]
forall (m :: * -> *) a. Monad m => a -> m a
return [AST]
res
  h2c :: [AST] -> (Ptr Z3_ast_vector -> IO r) -> IO r
h2c _ _ = String -> IO r
forall a. HasCallStack => String -> a
error "Marshal [AST] (Ptr Z3_ast_vector) => h2c not implemented"

instance Marshal Sort (Ptr Z3_sort) where
  c2h :: Context -> Ptr Z3_sort -> IO Sort
c2h = (ForeignPtr Z3_sort -> Sort)
-> Z3IncRefFun Z3_sort
-> Z3IncRefFun Z3_sort
-> Context
-> Ptr Z3_sort
-> IO Sort
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_sort -> Sort
Sort
            (Z3SetRefCount Z3_ast
z3_inc_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_sort
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_sort_to_ast)
            (Z3SetRefCount Z3_ast
z3_dec_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_sort
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_sort -> IO (Ptr Z3_ast)
z3_sort_to_ast)
  h2c :: Sort -> (Ptr Z3_sort -> IO r) -> IO r
h2c srt :: Sort
srt = ForeignPtr Z3_sort -> (Ptr Z3_sort -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Sort -> ForeignPtr Z3_sort
unSort Sort
srt)

instance Marshal FuncDecl (Ptr Z3_func_decl) where
  c2h :: Context -> Ptr Z3_func_decl -> IO FuncDecl
c2h = (ForeignPtr Z3_func_decl -> FuncDecl)
-> Z3IncRefFun Z3_func_decl
-> Z3IncRefFun Z3_func_decl
-> Context
-> Ptr Z3_func_decl
-> IO FuncDecl
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_func_decl -> FuncDecl
FuncDecl
            (Z3SetRefCount Z3_ast
z3_inc_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_func_decl
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_ast)
z3_func_decl_to_ast)
            (Z3SetRefCount Z3_ast
z3_dec_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_func_decl
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_func_decl -> IO (Ptr Z3_ast)
z3_func_decl_to_ast)
  h2c :: FuncDecl -> (Ptr Z3_func_decl -> IO r) -> IO r
h2c fnd :: FuncDecl
fnd = ForeignPtr Z3_func_decl -> (Ptr Z3_func_decl -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (FuncDecl -> ForeignPtr Z3_func_decl
unFuncDecl FuncDecl
fnd)

instance Marshal FuncEntry (Ptr Z3_func_entry) where
  c2h :: Context -> Ptr Z3_func_entry -> IO FuncEntry
c2h = (ForeignPtr Z3_func_entry -> FuncEntry)
-> Z3IncRefFun Z3_func_entry
-> Z3IncRefFun Z3_func_entry
-> Context
-> Ptr Z3_func_entry
-> IO FuncEntry
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_func_entry -> FuncEntry
FuncEntry Z3IncRefFun Z3_func_entry
z3_func_entry_inc_ref
                                Z3IncRefFun Z3_func_entry
z3_func_entry_dec_ref
  h2c :: FuncEntry -> (Ptr Z3_func_entry -> IO r) -> IO r
h2c fne :: FuncEntry
fne = ForeignPtr Z3_func_entry -> (Ptr Z3_func_entry -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (FuncEntry -> ForeignPtr Z3_func_entry
unFuncEntry FuncEntry
fne)

instance Marshal FuncInterp (Ptr Z3_func_interp) where
  c2h :: Context -> Ptr Z3_func_interp -> IO FuncInterp
c2h = (ForeignPtr Z3_func_interp -> FuncInterp)
-> Z3IncRefFun Z3_func_interp
-> Z3IncRefFun Z3_func_interp
-> Context
-> Ptr Z3_func_interp
-> IO FuncInterp
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_func_interp -> FuncInterp
FuncInterp Z3IncRefFun Z3_func_interp
z3_func_interp_inc_ref
                                 Z3IncRefFun Z3_func_interp
z3_func_interp_dec_ref
  h2c :: FuncInterp -> (Ptr Z3_func_interp -> IO r) -> IO r
h2c fni :: FuncInterp
fni = ForeignPtr Z3_func_interp -> (Ptr Z3_func_interp -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (FuncInterp -> ForeignPtr Z3_func_interp
unFuncInterp FuncInterp
fni)

instance Marshal Model (Ptr Z3_model) where
  c2h :: Context -> Ptr Z3_model -> IO Model
c2h = (ForeignPtr Z3_model -> Model)
-> Z3IncRefFun Z3_model
-> Z3IncRefFun Z3_model
-> Context
-> Ptr Z3_model
-> IO Model
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_model -> Model
Model Z3IncRefFun Z3_model
z3_model_inc_ref Z3IncRefFun Z3_model
z3_model_dec_ref
  h2c :: Model -> (Ptr Z3_model -> IO r) -> IO r
h2c m :: Model
m = ForeignPtr Z3_model -> (Ptr Z3_model -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Model -> ForeignPtr Z3_model
unModel Model
m)

instance Marshal Pattern (Ptr Z3_pattern) where
  c2h :: Context -> Ptr Z3_pattern -> IO Pattern
c2h = (ForeignPtr Z3_pattern -> Pattern)
-> Z3IncRefFun Z3_pattern
-> Z3IncRefFun Z3_pattern
-> Context
-> Ptr Z3_pattern
-> IO Pattern
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_pattern -> Pattern
Pattern
            (Z3SetRefCount Z3_ast
z3_inc_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_pattern -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_pattern
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_pattern -> IO (Ptr Z3_ast)
z3_pattern_to_ast)
            (Z3SetRefCount Z3_ast
z3_dec_ref Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr Z3_pattern -> IO (Ptr Z3_ast))
-> Z3IncRefFun Z3_pattern
forall a.
Z3SetRefCount Z3_ast
-> (Ptr Z3_context -> Ptr a -> IO (Ptr Z3_ast)) -> Z3SetRefCount a
`on_ast_ptr` Ptr Z3_context -> Ptr Z3_pattern -> IO (Ptr Z3_ast)
z3_pattern_to_ast)
  h2c :: Pattern -> (Ptr Z3_pattern -> IO r) -> IO r
h2c pat :: Pattern
pat = ForeignPtr Z3_pattern -> (Ptr Z3_pattern -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Pattern -> ForeignPtr Z3_pattern
unPattern Pattern
pat)

instance Marshal Constructor (Ptr Z3_constructor) where
  c2h :: Context -> Ptr Z3_constructor -> IO Constructor
c2h = (ForeignPtr Z3_constructor -> Constructor)
-> Z3IncRefFun Z3_constructor
-> Z3IncRefFun Z3_constructor
-> Context
-> Ptr Z3_constructor
-> IO Constructor
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_constructor -> Constructor
Constructor Z3IncRefFun Z3_constructor
forall c. Z3IncRefFun c
dummy_inc_ref Z3IncRefFun Z3_constructor
z3_del_constructor
  h2c :: Constructor -> (Ptr Z3_constructor -> IO r) -> IO r
h2c cns :: Constructor
cns = ForeignPtr Z3_constructor -> (Ptr Z3_constructor -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Constructor -> ForeignPtr Z3_constructor
unConstructor Constructor
cns)

instance Marshal ConstructorList (Ptr Z3_constructor_list) where
  c2h :: Context -> Ptr Z3_constructor_list -> IO ConstructorList
c2h = (ForeignPtr Z3_constructor_list -> ConstructorList)
-> Z3IncRefFun Z3_constructor_list
-> Z3IncRefFun Z3_constructor_list
-> Context
-> Ptr Z3_constructor_list
-> IO ConstructorList
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_constructor_list -> ConstructorList
ConstructorList Z3IncRefFun Z3_constructor_list
forall c. Z3IncRefFun c
dummy_inc_ref Z3IncRefFun Z3_constructor_list
z3_del_constructor_list
  h2c :: ConstructorList -> (Ptr Z3_constructor_list -> IO r) -> IO r
h2c cl :: ConstructorList
cl = ForeignPtr Z3_constructor_list
-> (Ptr Z3_constructor_list -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (ConstructorList -> ForeignPtr Z3_constructor_list
unConstructorList ConstructorList
cl)

instance Marshal Solver (Ptr Z3_solver) where
  c2h :: Context -> Ptr Z3_solver -> IO Solver
c2h = (ForeignPtr Z3_solver -> Solver)
-> (Ptr Z3_context -> Ptr Z3_solver -> IO ())
-> (Ptr Z3_context -> Ptr Z3_solver -> IO ())
-> Context
-> Ptr Z3_solver
-> IO Solver
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_solver -> Solver
Solver Ptr Z3_context -> Ptr Z3_solver -> IO ()
z3_solver_inc_ref Ptr Z3_context -> Ptr Z3_solver -> IO ()
z3_solver_dec_ref
  h2c :: Solver -> (Ptr Z3_solver -> IO r) -> IO r
h2c slv :: Solver
slv = ForeignPtr Z3_solver -> (Ptr Z3_solver -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Solver -> ForeignPtr Z3_solver
unSolver Solver
slv)

instance Marshal Tactic (Ptr Z3_tactic) where
  c2h :: Context -> Ptr Z3_tactic -> IO Tactic
c2h = (ForeignPtr Z3_tactic -> Tactic)
-> Z3IncRefFun Z3_tactic
-> Z3IncRefFun Z3_tactic
-> Context
-> Ptr Z3_tactic
-> IO Tactic
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_tactic -> Tactic
Tactic Z3IncRefFun Z3_tactic
z3_tactic_inc_ref Z3IncRefFun Z3_tactic
z3_tactic_dec_ref
  h2c :: Tactic -> (Ptr Z3_tactic -> IO r) -> IO r
h2c tac :: Tactic
tac = ForeignPtr Z3_tactic -> (Ptr Z3_tactic -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Tactic -> ForeignPtr Z3_tactic
unTactic Tactic
tac)

instance Marshal ApplyResult (Ptr Z3_apply_result) where
  c2h :: Context -> Ptr Z3_apply_result -> IO ApplyResult
c2h = (ForeignPtr Z3_apply_result -> ApplyResult)
-> Z3IncRefFun Z3_apply_result
-> Z3IncRefFun Z3_apply_result
-> Context
-> Ptr Z3_apply_result
-> IO ApplyResult
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_apply_result -> ApplyResult
ApplyResult Z3IncRefFun Z3_apply_result
z3_apply_result_inc_ref Z3IncRefFun Z3_apply_result
z3_apply_result_dec_ref
  h2c :: ApplyResult -> (Ptr Z3_apply_result -> IO r) -> IO r
h2c apl :: ApplyResult
apl = ForeignPtr Z3_apply_result -> (Ptr Z3_apply_result -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (ApplyResult -> ForeignPtr Z3_apply_result
unApplyResult ApplyResult
apl)

instance Marshal Goal (Ptr Z3_goal) where
  c2h :: Context -> Ptr Z3_goal -> IO Goal
c2h = (ForeignPtr Z3_goal -> Goal)
-> Z3IncRefFun Z3_goal
-> Z3IncRefFun Z3_goal
-> Context
-> Ptr Z3_goal
-> IO Goal
forall c h.
(ForeignPtr c -> h)
-> Z3IncRefFun c -> Z3IncRefFun c -> Context -> Ptr c -> IO h
mkC2hRefCount ForeignPtr Z3_goal -> Goal
Goal Z3IncRefFun Z3_goal
z3_goal_inc_ref Z3IncRefFun Z3_goal
z3_goal_dec_ref
  h2c :: Goal -> (Ptr Z3_goal -> IO r) -> IO r
h2c goa :: Goal
goa = ForeignPtr Z3_goal -> (Ptr Z3_goal -> IO r) -> IO r
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (Goal -> ForeignPtr Z3_goal
unGoal Goal
goa)

marshal :: Marshal rh rc => (Ptr Z3_context -> t) ->
              Context -> (t -> IO rc) -> IO rh
marshal :: (Ptr Z3_context -> t) -> Context -> (t -> IO rc) -> IO rh
marshal f :: Ptr Z3_context -> t
f c :: Context
c cont :: t -> IO rc
cont = Context -> (Ptr Z3_context -> IO rc) -> IO rh
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO rc) -> IO rh)
-> (Ptr Z3_context -> IO rc) -> IO rh
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> t -> IO rc
cont (t -> IO rc) -> t -> IO rc
forall a b. (a -> b) -> a -> b
$ Ptr Z3_context -> t
f Ptr Z3_context
cPtr

liftFun0 :: Marshal rh rc => (Ptr Z3_context -> IO rc) ->
              Context -> IO rh
liftFun0 :: (Ptr Z3_context -> IO rc) -> Context -> IO rh
liftFun0 = (Context -> (Ptr Z3_context -> IO rc) -> IO rh)
-> (Ptr Z3_context -> IO rc) -> Context -> IO rh
forall a b c. (a -> b -> c) -> b -> a -> c
flip Context -> (Ptr Z3_context -> IO rc) -> IO rh
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError
{-# INLINE liftFun0 #-}

liftFun1 :: (Marshal ah ac, Marshal rh rc) =>
              (Ptr Z3_context -> ac -> IO rc) ->
              Context -> ah -> IO rh
liftFun1 :: (Ptr Z3_context -> ac -> IO rc) -> Context -> ah -> IO rh
liftFun1 f :: Ptr Z3_context -> ac -> IO rc
f c :: Context
c x :: ah
x = ah -> (ac -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c ah
x ((ac -> IO rh) -> IO rh) -> (ac -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \a :: ac
a ->
  Context -> (Ptr Z3_context -> IO rc) -> IO rh
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO rc) -> IO rh)
-> (Ptr Z3_context -> IO rc) -> IO rh
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> ac -> IO rc
f Ptr Z3_context
cPtr ac
a
{-# INLINE liftFun1 #-}

liftFun2 :: (Marshal ah ac, Marshal bh bc, Marshal rh rc) =>
              (Ptr Z3_context -> ac -> bc -> IO rc) ->
              Context -> ah -> bh -> IO rh
liftFun2 :: (Ptr Z3_context -> ac -> bc -> IO rc)
-> Context -> ah -> bh -> IO rh
liftFun2 f :: Ptr Z3_context -> ac -> bc -> IO rc
f c :: Context
c x :: ah
x y :: bh
y = ah -> (ac -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c ah
x ((ac -> IO rh) -> IO rh) -> (ac -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \a :: ac
a -> bh -> (bc -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c bh
y ((bc -> IO rh) -> IO rh) -> (bc -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \b :: bc
b ->
  Context -> (Ptr Z3_context -> IO rc) -> IO rh
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO rc) -> IO rh)
-> (Ptr Z3_context -> IO rc) -> IO rh
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> ac -> bc -> IO rc
f Ptr Z3_context
cPtr ac
a bc
b
{-# INLINE liftFun2 #-}

liftFun3 :: (Marshal ah ac, Marshal bh bc, Marshal ch cc, Marshal rh rc) =>
              (Ptr Z3_context -> ac -> bc -> cc -> IO rc) ->
              Context -> ah -> bh -> ch -> IO rh
liftFun3 :: (Ptr Z3_context -> ac -> bc -> cc -> IO rc)
-> Context -> ah -> bh -> ch -> IO rh
liftFun3 f :: Ptr Z3_context -> ac -> bc -> cc -> IO rc
f c :: Context
c x :: ah
x y :: bh
y z :: ch
z = ah -> (ac -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c ah
x ((ac -> IO rh) -> IO rh) -> (ac -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \x1 :: ac
x1 -> bh -> (bc -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c bh
y ((bc -> IO rh) -> IO rh) -> (bc -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \y1 :: bc
y1 -> ch -> (cc -> IO rh) -> IO rh
forall h c r. Marshal h c => h -> (c -> IO r) -> IO r
h2c ch
z ((cc -> IO rh) -> IO rh) -> (cc -> IO rh) -> IO rh
forall a b. (a -> b) -> a -> b
$ \z1 :: cc
z1 ->
  Context -> (Ptr Z3_context -> IO rc) -> IO rh
forall h c.
Marshal h c =>
Context -> (Ptr Z3_context -> IO c) -> IO h
toHsCheckError Context
c ((Ptr Z3_context -> IO rc) -> IO rh)
-> (Ptr Z3_context -> IO rc) -> IO rh
forall a b. (a -> b) -> a -> b
$ \cPtr :: Ptr Z3_context
cPtr -> Ptr Z3_context -> ac -> bc -> cc -> IO rc
f Ptr Z3_context
cPtr ac
x1 bc
y1 cc
z1
{-# INLINE liftFun3 #-}

---------------------------------------------------------------------
-- Utils

-- | Convert 'Z3_lbool' from Z3.Base.C to 'Result'
toResult :: Z3_lbool -> Result
toResult :: Z3_lbool -> Result
toResult lb :: Z3_lbool
lb
    | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_true  = Result
Sat
    | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_false = Result
Unsat
    | Z3_lbool
lb Z3_lbool -> Z3_lbool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_lbool
z3_l_undef = Result
Undef
    | Bool
otherwise        = String -> Result
forall a. HasCallStack => String -> a
error "Z3.Base.toResult: illegal `Z3_lbool' value"

-- | Convert 'Z3_bool' to 'Bool'.
--
-- 'Foreign.toBool' should be OK but this is more convenient.
toBool :: Z3_bool -> Bool
toBool :: Z3_bool -> Bool
toBool b :: Z3_bool
b
    | Z3_bool
b Z3_bool -> Z3_bool -> Bool
forall a. Eq a => a -> a -> Bool
== Z3_bool
z3_false = Bool
False
    -- As of March 2019, OS X has an issue where z3 uses other
    -- values than 'z3_true' as truthy. Our work around is to be permissive.
    | Bool
otherwise = Bool
True

-- | Convert 'Bool' to 'Z3_bool'.
unBool :: Bool -> Z3_bool
unBool :: Bool -> Z3_bool
unBool True  = Z3_bool
z3_true
unBool False = Z3_bool
z3_false

-- | Wraps a non-null pointer with 'Just', or else returns 'Nothing'.
ptrToMaybe :: Ptr a -> Maybe (Ptr a)
ptrToMaybe :: Ptr a -> Maybe (Ptr a)
ptrToMaybe ptr :: Ptr a
ptr | Ptr a
ptr Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
forall a. Ptr a
nullPtr = Maybe (Ptr a)
forall a. Maybe a
Nothing
               | Bool
otherwise      = Ptr a -> Maybe (Ptr a)
forall a. a -> Maybe a
Just Ptr a
ptr