{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Data.SBV.Core.Data
( SBool, SWord8, SWord16, SWord32, SWord64
, SInt8, SInt16, SInt32, SInt64, SInteger, SReal, SFloat, SDouble
, SFloatingPoint, SFPHalf, SFPBFloat, SFPSingle, SFPDouble, SFPQuad
, SRational
, SChar, SString, SList
, SEither, SMaybe
, STuple, STuple2, STuple3, STuple4, STuple5, STuple6, STuple7, STuple8
, RCSet(..), SSet
, nan, infinity, sNaN, sInfinity, RoundingMode(..), SRoundingMode
, sRoundNearestTiesToEven, sRoundNearestTiesToAway, sRoundTowardPositive, sRoundTowardNegative, sRoundTowardZero
, sRNE, sRNA, sRTP, sRTN, sRTZ
, SymVal(..)
, CV(..), CVal(..), AlgReal(..), AlgRealPoly(..), ExtCV(..), GeneralizedCV(..), isRegularCV, cvSameType, cvToBool
, mkConstCV ,liftCV2, mapCV, mapCV2
, SV(..), trueSV, falseSV, trueCV, falseCV, normCV
, SVal(..)
, sTrue, sFalse, sNot, (.&&), (.||), (.<+>), (.~&), (.~|), (.=>), (.<=>), sAnd, sOr, sAny, sAll, fromBool
, SBV(..), NodeId(..), mkSymSBV
, ArrayContext(..), ArrayInfo, SymArray(..), SArray(..)
, sbvToSV, sbvToSymSV, forceSVArg
, SBVExpr(..), newExpr
, cache, Cached, uncache, uncacheAI, HasKind(..)
, Op(..), PBOp(..), FPOp(..), StrOp(..), RegExOp(..), SeqOp(..), RegExp(..), NamedSymVar(..), getTableIndex
, SBVPgm(..), Symbolic, runSymbolic, State, getPathCondition, extendPathCondition
, inSMTMode, SBVRunMode(..), Kind(..), Outputtable(..), Result(..)
, SolverContext(..), internalVariable, internalConstraint, isCodeGenMode
, SBVType(..), newUninterpreted
, Quantifier(..), needsExistentials
, SMTLibPgm(..), SMTLibVersion(..), smtLibVersionExtension, smtLibReservedNames
, SolverCapabilities(..)
, extractSymbolicSimulationState
, SMTScript(..), Solver(..), SMTSolver(..), SMTResult(..), SMTModel(..), SMTConfig(..)
, OptimizeStyle(..), Penalty(..), Objective(..)
, QueryState(..), QueryT(..), SMTProblem(..)
) where
import GHC.TypeLits
import GHC.Generics (Generic)
import GHC.Exts (IsList(..))
import Control.DeepSeq (NFData(..))
import Control.Monad.Trans (liftIO)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import Data.List (elemIndex)
import Data.Maybe (fromMaybe)
import Data.Proxy
import Data.Typeable (Typeable)
import qualified Data.Generics as G (Data(..))
import qualified Data.IORef as R (readIORef)
import qualified Data.IntMap.Strict as IMap (size, insert)
import System.Random
import Data.SBV.Core.AlgReals
import Data.SBV.Core.SizedFloats
import Data.SBV.Core.Kind
import Data.SBV.Core.Concrete
import Data.SBV.Core.Symbolic
import Data.SBV.Core.Operations
import Data.SBV.Control.Types
import Data.SBV.SMT.SMTLibNames
import Data.SBV.Utils.Lib
getPathCondition :: State -> SBool
getPathCondition :: State -> SBool
getPathCondition State
st = forall a. SVal -> SBV a
SBV (State -> SVal
getSValPathCondition State
st)
extendPathCondition :: State -> (SBool -> SBool) -> State
extendPathCondition :: State -> (SBool -> SBool) -> State
extendPathCondition State
st SBool -> SBool
f = State -> (SVal -> SVal) -> State
extendSValPathCondition State
st (forall a. SBV a -> SVal
unSBV forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBool -> SBool
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SVal -> SBV a
SBV)
newtype SBV a = SBV { forall a. SBV a -> SVal
unSBV :: SVal }
deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SBV a) x -> SBV a
forall a x. SBV a -> Rep (SBV a) x
$cto :: forall a x. Rep (SBV a) x -> SBV a
$cfrom :: forall a x. SBV a -> Rep (SBV a) x
Generic, forall a. SBV a -> ()
forall a. (a -> ()) -> NFData a
rnf :: SBV a -> ()
$crnf :: forall a. SBV a -> ()
NFData)
type SBool = SBV Bool
type SWord8 = SBV Word8
type SWord16 = SBV Word16
type SWord32 = SBV Word32
type SWord64 = SBV Word64
type SInt8 = SBV Int8
type SInt16 = SBV Int16
type SInt32 = SBV Int32
type SInt64 = SBV Int64
type SInteger = SBV Integer
type SReal = SBV AlgReal
type SFloat = SBV Float
type SDouble = SBV Double
type SFloatingPoint (eb :: Nat) (sb :: Nat) = SBV (FloatingPoint eb sb)
type SFPHalf = SBV FPHalf
type SFPBFloat = SBV FPBFloat
type SFPSingle = SBV FPSingle
type SFPDouble = SBV FPDouble
type SFPQuad = SBV FPQuad
type SChar = SBV Char
type SString = SBV String
type SRational = SBV Rational
type SList a = SBV [a]
type SEither a b = SBV (Either a b)
type SMaybe a = SBV (Maybe a)
type SSet a = SBV (RCSet a)
type STuple a b = SBV (a, b)
type STuple2 a b = SBV (a, b)
type STuple3 a b c = SBV (a, b, c)
type STuple4 a b c d = SBV (a, b, c, d)
type STuple5 a b c d e = SBV (a, b, c, d, e)
type STuple6 a b c d e f = SBV (a, b, c, d, e, f)
type STuple7 a b c d e f g = SBV (a, b, c, d, e, f, g)
type STuple8 a b c d e f g h = SBV (a, b, c, d, e, f, g, h)
instance SymVal [a] => IsList (SList a) where
type Item (SList a) = a
fromList :: [Item (SList a)] -> SList a
fromList = forall a. SymVal a => a -> SBV a
literal
toList :: SList a -> [Item (SList a)]
toList SList a
x = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"IsList.toList used in a symbolic context!") (forall a. SymVal a => SBV a -> Maybe a
unliteral SList a
x)
nan :: Floating a => a
nan :: forall a. Floating a => a
nan = a
0forall a. Fractional a => a -> a -> a
/a
0
infinity :: Floating a => a
infinity :: forall a. Floating a => a
infinity = a
1forall a. Fractional a => a -> a -> a
/a
0
sNaN :: (Floating a, SymVal a) => SBV a
sNaN :: forall a. (Floating a, SymVal a) => SBV a
sNaN = forall a. SymVal a => a -> SBV a
literal forall a. Floating a => a
nan
sInfinity :: (Floating a, SymVal a) => SBV a
sInfinity :: forall a. (Floating a, SymVal a) => SBV a
sInfinity = forall a. SymVal a => a -> SBV a
literal forall a. Floating a => a
infinity
newtype SMTProblem = SMTProblem {SMTProblem -> SMTConfig -> SMTLibPgm
smtLibPgm :: SMTConfig -> SMTLibPgm}
sTrue :: SBool
sTrue :: SBool
sTrue = forall a. SVal -> SBV a
SBV (Bool -> SVal
svBool Bool
True)
sFalse :: SBool
sFalse :: SBool
sFalse = forall a. SVal -> SBV a
SBV (Bool -> SVal
svBool Bool
False)
sNot :: SBool -> SBool
sNot :: SBool -> SBool
sNot (SBV SVal
b) = forall a. SVal -> SBV a
SBV (SVal -> SVal
svNot SVal
b)
infixr 3 .&&
(.&&) :: SBool -> SBool -> SBool
SBV SVal
x .&& :: SBool -> SBool -> SBool
.&& SBV SVal
y = forall a. SVal -> SBV a
SBV (SVal
x SVal -> SVal -> SVal
`svAnd` SVal
y)
infixr 2 .||
(.||) :: SBool -> SBool -> SBool
SBV SVal
x .|| :: SBool -> SBool -> SBool
.|| SBV SVal
y = forall a. SVal -> SBV a
SBV (SVal
x SVal -> SVal -> SVal
`svOr` SVal
y)
infixl 6 .<+>
(.<+>) :: SBool -> SBool -> SBool
SBV SVal
x .<+> :: SBool -> SBool -> SBool
.<+> SBV SVal
y = forall a. SVal -> SBV a
SBV (SVal
x SVal -> SVal -> SVal
`svXOr` SVal
y)
infixr 3 .~&
(.~&) :: SBool -> SBool -> SBool
SBool
x .~& :: SBool -> SBool -> SBool
.~& SBool
y = SBool -> SBool
sNot (SBool
x SBool -> SBool -> SBool
.&& SBool
y)
infixr 2 .~|
(.~|) :: SBool -> SBool -> SBool
SBool
x .~| :: SBool -> SBool -> SBool
.~| SBool
y = SBool -> SBool
sNot (SBool
x SBool -> SBool -> SBool
.|| SBool
y)
infixr 1 .=>
(.=>) :: SBool -> SBool -> SBool
SBool
x .=> :: SBool -> SBool -> SBool
.=> SBool
y = SBool -> SBool
sNot SBool
x SBool -> SBool -> SBool
.|| SBool
y
infixr 1 .<=>
(.<=>) :: SBool -> SBool -> SBool
SBool
x .<=> :: SBool -> SBool -> SBool
.<=> SBool
y = (SBool
x SBool -> SBool -> SBool
.&& SBool
y) SBool -> SBool -> SBool
.|| (SBool -> SBool
sNot SBool
x SBool -> SBool -> SBool
.&& SBool -> SBool
sNot SBool
y)
fromBool :: Bool -> SBool
fromBool :: Bool -> SBool
fromBool Bool
True = SBool
sTrue
fromBool Bool
False = SBool
sFalse
sAnd :: [SBool] -> SBool
sAnd :: [SBool] -> SBool
sAnd = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SBool -> SBool -> SBool
(.&&) SBool
sTrue
sOr :: [SBool] -> SBool
sOr :: [SBool] -> SBool
sOr = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr SBool -> SBool -> SBool
(.||) SBool
sFalse
sAny :: (a -> SBool) -> [a] -> SBool
sAny :: forall a. (a -> SBool) -> [a] -> SBool
sAny a -> SBool
f = [SBool] -> SBool
sOr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map a -> SBool
f
sAll :: (a -> SBool) -> [a] -> SBool
sAll :: forall a. (a -> SBool) -> [a] -> SBool
sAll a -> SBool
f = [SBool] -> SBool
sAnd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map a -> SBool
f
instance SymVal RoundingMode
type SRoundingMode = SBV RoundingMode
sRoundNearestTiesToEven :: SRoundingMode
sRoundNearestTiesToEven :: SBV RoundingMode
sRoundNearestTiesToEven = forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundNearestTiesToEven
sRoundNearestTiesToAway :: SRoundingMode
sRoundNearestTiesToAway :: SBV RoundingMode
sRoundNearestTiesToAway = forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundNearestTiesToAway
sRoundTowardPositive :: SRoundingMode
sRoundTowardPositive :: SBV RoundingMode
sRoundTowardPositive = forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundTowardPositive
sRoundTowardNegative :: SRoundingMode
sRoundTowardNegative :: SBV RoundingMode
sRoundTowardNegative = forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundTowardNegative
sRoundTowardZero :: SRoundingMode
sRoundTowardZero :: SBV RoundingMode
sRoundTowardZero = forall a. SymVal a => a -> SBV a
literal RoundingMode
RoundTowardZero
sRNE :: SRoundingMode
sRNE :: SBV RoundingMode
sRNE = SBV RoundingMode
sRoundNearestTiesToEven
sRNA :: SRoundingMode
sRNA :: SBV RoundingMode
sRNA = SBV RoundingMode
sRoundNearestTiesToAway
sRTP :: SRoundingMode
sRTP :: SBV RoundingMode
sRTP = SBV RoundingMode
sRoundTowardPositive
sRTN :: SRoundingMode
sRTN :: SBV RoundingMode
sRTN = SBV RoundingMode
sRoundTowardNegative
sRTZ :: SRoundingMode
sRTZ :: SBV RoundingMode
sRTZ = SBV RoundingMode
sRoundTowardZero
instance Show (SBV a) where
show :: SBV a -> [Char]
show (SBV SVal
sv) = forall a. Show a => a -> [Char]
show SVal
sv
instance Eq (SBV a) where
SBV SVal
a == :: SBV a -> SBV a -> Bool
== SBV SVal
b = SVal
a forall a. Eq a => a -> a -> Bool
== SVal
b
SBV SVal
a /= :: SBV a -> SBV a -> Bool
/= SBV SVal
b = SVal
a forall a. Eq a => a -> a -> Bool
/= SVal
b
instance HasKind a => HasKind (SBV a) where
kindOf :: SBV a -> Kind
kindOf SBV a
_ = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
sbvToSV :: State -> SBV a -> IO SV
sbvToSV :: forall a. State -> SBV a -> IO SV
sbvToSV State
st (SBV SVal
s) = State -> SVal -> IO SV
svToSV State
st SVal
s
mkSymSBV :: forall a m. MonadSymbolic m => VarContext -> Kind -> Maybe String -> m (SBV a)
mkSymSBV :: forall a (m :: * -> *).
MonadSymbolic m =>
VarContext -> Kind -> Maybe [Char] -> m (SBV a)
mkSymSBV VarContext
vc Kind
k Maybe [Char]
mbNm = forall a. SVal -> SBV a
SBV forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (m :: * -> *). MonadSymbolic m => m State
symbolicEnv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarContext -> Kind -> Maybe [Char] -> State -> IO SVal
svMkSymVar VarContext
vc Kind
k Maybe [Char]
mbNm)
sbvToSymSV :: MonadSymbolic m => SBV a -> m SV
sbvToSymSV :: forall (m :: * -> *) a. MonadSymbolic m => SBV a -> m SV
sbvToSymSV SBV a
sbv = do
State
st <- forall (m :: * -> *). MonadSymbolic m => m State
symbolicEnv
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV a
sbv
class SolverContext m where
constrain :: SBool -> m ()
softConstrain :: SBool -> m ()
namedConstraint :: String -> SBool -> m ()
constrainWithAttribute :: [(String, String)] -> SBool -> m ()
setInfo :: String -> [String] -> m ()
setOption :: SMTOption -> m ()
setLogic :: Logic -> m ()
addAxiom :: String -> [String] -> m ()
addSMTDefinition :: String -> [String] -> m ()
setTimeOut :: Integer -> m ()
contextState :: m State
{-# MINIMAL constrain, softConstrain, namedConstraint, constrainWithAttribute, setOption, addAxiom, addSMTDefinition, contextState #-}
setTimeOut Integer
t = forall (m :: * -> *). SolverContext m => SMTOption -> m ()
setOption forall a b. (a -> b) -> a -> b
$ [Char] -> [[Char]] -> SMTOption
OptionKeyword [Char]
":timeout" [forall a. Show a => a -> [Char]
show Integer
t]
setLogic = forall (m :: * -> *). SolverContext m => SMTOption -> m ()
setOption forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logic -> SMTOption
SetLogic
setInfo [Char]
k = forall (m :: * -> *). SolverContext m => SMTOption -> m ()
setOption forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]] -> SMTOption
SetInfo [Char]
k
class Outputtable a where
output :: MonadSymbolic m => a -> m a
instance Outputtable (SBV a) where
output :: forall (m :: * -> *). MonadSymbolic m => SBV a -> m (SBV a)
output SBV a
i = do
forall (m :: * -> *). MonadSymbolic m => SVal -> m ()
outputSVal (forall a. SBV a -> SVal
unSBV SBV a
i)
forall (m :: * -> *) a. Monad m => a -> m a
return SBV a
i
instance Outputtable a => Outputtable [a] where
output :: forall (m :: * -> *). MonadSymbolic m => [a] -> m [a]
output = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance Outputtable () where
output :: forall (m :: * -> *). MonadSymbolic m => () -> m ()
output = forall (m :: * -> *) a. Monad m => a -> m a
return
instance (Outputtable a, Outputtable b) => Outputtable (a, b) where
output :: forall (m :: * -> *). MonadSymbolic m => (a, b) -> m (a, b)
output = forall (m :: * -> *) a' b' r a b.
Monad m =>
(a' -> b' -> r) -> (a -> m a') -> (b -> m b') -> (a, b) -> m r
mlift2 (,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c) => Outputtable (a, b, c) where
output :: forall (m :: * -> *). MonadSymbolic m => (a, b, c) -> m (a, b, c)
output = forall (m :: * -> *) a' b' c' r a b c.
Monad m =>
(a' -> b' -> c' -> r)
-> (a -> m a') -> (b -> m b') -> (c -> m c') -> (a, b, c) -> m r
mlift3 (,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c, Outputtable d) => Outputtable (a, b, c, d) where
output :: forall (m :: * -> *).
MonadSymbolic m =>
(a, b, c, d) -> m (a, b, c, d)
output = forall (m :: * -> *) a' b' c' d' r a b c d.
Monad m =>
(a' -> b' -> c' -> d' -> r)
-> (a -> m a')
-> (b -> m b')
-> (c -> m c')
-> (d -> m d')
-> (a, b, c, d)
-> m r
mlift4 (,,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c, Outputtable d, Outputtable e) => Outputtable (a, b, c, d, e) where
output :: forall (m :: * -> *).
MonadSymbolic m =>
(a, b, c, d, e) -> m (a, b, c, d, e)
output = forall (m :: * -> *) a' b' c' d' e' r a b c d e.
Monad m =>
(a' -> b' -> c' -> d' -> e' -> r)
-> (a -> m a')
-> (b -> m b')
-> (c -> m c')
-> (d -> m d')
-> (e -> m e')
-> (a, b, c, d, e)
-> m r
mlift5 (,,,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c, Outputtable d, Outputtable e, Outputtable f) => Outputtable (a, b, c, d, e, f) where
output :: forall (m :: * -> *).
MonadSymbolic m =>
(a, b, c, d, e, f) -> m (a, b, c, d, e, f)
output = forall (m :: * -> *) a' b' c' d' e' f' r a b c d e f.
Monad m =>
(a' -> b' -> c' -> d' -> e' -> f' -> r)
-> (a -> m a')
-> (b -> m b')
-> (c -> m c')
-> (d -> m d')
-> (e -> m e')
-> (f -> m f')
-> (a, b, c, d, e, f)
-> m r
mlift6 (,,,,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c, Outputtable d, Outputtable e, Outputtable f, Outputtable g) => Outputtable (a, b, c, d, e, f, g) where
output :: forall (m :: * -> *).
MonadSymbolic m =>
(a, b, c, d, e, f, g) -> m (a, b, c, d, e, f, g)
output = forall (m :: * -> *) a' b' c' d' e' f' g' r a b c d e f g.
Monad m =>
(a' -> b' -> c' -> d' -> e' -> f' -> g' -> r)
-> (a -> m a')
-> (b -> m b')
-> (c -> m c')
-> (d -> m d')
-> (e -> m e')
-> (f -> m f')
-> (g -> m g')
-> (a, b, c, d, e, f, g)
-> m r
mlift7 (,,,,,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
instance (Outputtable a, Outputtable b, Outputtable c, Outputtable d, Outputtable e, Outputtable f, Outputtable g, Outputtable h) => Outputtable (a, b, c, d, e, f, g, h) where
output :: forall (m :: * -> *).
MonadSymbolic m =>
(a, b, c, d, e, f, g, h) -> m (a, b, c, d, e, f, g, h)
output = forall (m :: * -> *) a' b' c' d' e' f' g' h' r a b c d e f g h.
Monad m =>
(a' -> b' -> c' -> d' -> e' -> f' -> g' -> h' -> r)
-> (a -> m a')
-> (b -> m b')
-> (c -> m c')
-> (d -> m d')
-> (e -> m e')
-> (f -> m f')
-> (g -> m g')
-> (h -> m h')
-> (a, b, c, d, e, f, g, h)
-> m r
mlift8 (,,,,,,,) forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output forall a (m :: * -> *).
(Outputtable a, MonadSymbolic m) =>
a -> m a
output
class (HasKind a, Typeable a) => SymVal a where
mkSymVal :: MonadSymbolic m => VarContext -> Maybe String -> m (SBV a)
literal :: a -> SBV a
fromCV :: CV -> a
isConcretely :: SBV a -> (a -> Bool) -> Bool
default mkSymVal :: (MonadSymbolic m, Read a, G.Data a) => VarContext -> Maybe String -> m (SBV a)
mkSymVal VarContext
vc Maybe [Char]
mbNm = forall a. SVal -> SBV a
SBV forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall (m :: * -> *). MonadSymbolic m => m State
symbolicEnv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarContext -> Kind -> Maybe [Char] -> State -> IO SVal
svMkSymVar VarContext
vc Kind
k Maybe [Char]
mbNm)
where
k :: Kind
k = forall a. (Read a, Data a) => a -> Kind
constructUKind (forall a. HasCallStack => a
undefined :: a)
default literal :: Show a => a -> SBV a
literal a
x = let k :: Kind
k = forall a. HasKind a => a -> Kind
kindOf a
x
sx :: [Char]
sx = forall a. Show a => a -> [Char]
show a
x
conts :: Maybe [[Char]]
conts = case Kind
k of
KUserSort [Char]
_ Maybe [[Char]]
cts -> Maybe [[Char]]
cts
Kind
_ -> forall a. Maybe a
Nothing
mbIdx :: Maybe Int
mbIdx = case Maybe [[Char]]
conts of
Just [[Char]]
xs -> [Char]
sx forall a. Eq a => a -> [a] -> Maybe Int
`elemIndex` [[Char]]
xs
Maybe [[Char]]
Nothing -> forall a. Maybe a
Nothing
in forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
k (forall a b. a -> Either a b
Left (Kind -> CVal -> CV
CV Kind
k ((Maybe Int, [Char]) -> CVal
CUserSort (Maybe Int
mbIdx, [Char]
sx))))
default fromCV :: Read a => CV -> a
fromCV (CV Kind
_ (CUserSort (Maybe Int
_, [Char]
s))) = forall a. Read a => [Char] -> a
read [Char]
s
fromCV CV
cv = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Cannot convert CV " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show CV
cv forall a. [a] -> [a] -> [a]
++ [Char]
" to kind " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a))
isConcretely SBV a
s a -> Bool
p
| Just a
i <- forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
s = a -> Bool
p a
i
| Bool
True = Bool
False
sbvForall :: MonadSymbolic m => String -> m (SBV a)
sbvForall = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar (forall a. a -> Maybe a
Just Quantifier
ALL)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just
sbvForall_ :: MonadSymbolic m => m (SBV a)
sbvForall_ = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar (forall a. a -> Maybe a
Just Quantifier
ALL)) forall a. Maybe a
Nothing
mkForallVars :: MonadSymbolic m => Int -> m [SBV a]
mkForallVars Int
n = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const forall a (m :: * -> *). (SymVal a, MonadSymbolic m) => m (SBV a)
sbvForall_) [Int
1 .. Int
n]
sbvExists :: MonadSymbolic m => String -> m (SBV a)
sbvExists = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar (forall a. a -> Maybe a
Just Quantifier
EX)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just
sbvExists_ :: MonadSymbolic m => m (SBV a)
sbvExists_ = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar (forall a. a -> Maybe a
Just Quantifier
EX)) forall a. Maybe a
Nothing
mkExistVars :: MonadSymbolic m => Int -> m [SBV a]
mkExistVars Int
n = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const forall a (m :: * -> *). (SymVal a, MonadSymbolic m) => m (SBV a)
sbvExists_) [Int
1 .. Int
n]
free :: MonadSymbolic m => String -> m (SBV a)
free = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar forall a. Maybe a
Nothing) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just
free_ :: MonadSymbolic m => m (SBV a)
free_ = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
VarContext -> Maybe [Char] -> m (SBV a)
mkSymVal (Maybe Quantifier -> VarContext
NonQueryVar forall a. Maybe a
Nothing) forall a. Maybe a
Nothing
mkFreeVars :: MonadSymbolic m => Int -> m [SBV a]
mkFreeVars Int
n = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b. a -> b -> a
const forall a (m :: * -> *). (SymVal a, MonadSymbolic m) => m (SBV a)
free_) [Int
1 .. Int
n]
symbolic :: MonadSymbolic m => String -> m (SBV a)
symbolic = forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
[Char] -> m (SBV a)
free
symbolics :: MonadSymbolic m => [String] -> m [SBV a]
symbolics = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a (m :: * -> *).
(SymVal a, MonadSymbolic m) =>
[Char] -> m (SBV a)
symbolic
unliteral :: SBV a -> Maybe a
unliteral (SBV (SVal Kind
_ (Left CV
c))) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. SymVal a => CV -> a
fromCV CV
c
unliteral SBV a
_ = forall a. Maybe a
Nothing
isConcrete :: SBV a -> Bool
isConcrete (SBV (SVal Kind
_ (Left CV
_))) = Bool
True
isConcrete SBV a
_ = Bool
False
isSymbolic :: SBV a -> Bool
isSymbolic = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SymVal a => SBV a -> Bool
isConcrete
instance (Random a, SymVal a) => Random (SBV a) where
randomR :: forall g. RandomGen g => (SBV a, SBV a) -> g -> (SBV a, g)
randomR (SBV a
l, SBV a
h) g
g = case (forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
l, forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
h) of
(Just a
lb, Just a
hb) -> let (a
v, g
g') = forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
lb, a
hb) g
g in (forall a. SymVal a => a -> SBV a
literal (a
v :: a), g
g')
(Maybe a, Maybe a)
_ -> forall a. HasCallStack => [Char] -> a
error [Char]
"SBV.Random: Cannot generate random values with symbolic bounds"
random :: forall g. RandomGen g => g -> (SBV a, g)
random g
g = let (a
v, g
g') = forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g in (forall a. SymVal a => a -> SBV a
literal (a
v :: a) , g
g')
class SymArray array where
newArray_ :: (MonadSymbolic m, HasKind a, HasKind b) => Maybe (SBV b) -> m (array a b)
newArray :: (MonadSymbolic m, HasKind a, HasKind b) => String -> Maybe (SBV b) -> m (array a b)
sListArray :: (HasKind a, SymVal b) => b -> [(SBV a, SBV b)] -> array a b
readArray :: array a b -> SBV a -> SBV b
writeArray :: SymVal b => array a b -> SBV a -> SBV b -> array a b
mergeArrays :: SymVal b => SBV Bool -> array a b -> array a b -> array a b
newArrayInState :: (HasKind a, HasKind b) => Maybe String -> Maybe (SBV b) -> State -> IO (array a b)
{-# MINIMAL readArray, writeArray, mergeArrays, ((newArray_, newArray) | newArrayInState), sListArray #-}
newArray_ Maybe (SBV b)
mbVal = forall (m :: * -> *). MonadSymbolic m => m State
symbolicEnv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (array :: * -> * -> *) a b.
(SymArray array, HasKind a, HasKind b) =>
Maybe [Char] -> Maybe (SBV b) -> State -> IO (array a b)
newArrayInState forall a. Maybe a
Nothing Maybe (SBV b)
mbVal
newArray [Char]
nm Maybe (SBV b)
mbVal = forall (m :: * -> *). MonadSymbolic m => m State
symbolicEnv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (array :: * -> * -> *) a b.
(SymArray array, HasKind a, HasKind b) =>
Maybe [Char] -> Maybe (SBV b) -> State -> IO (array a b)
newArrayInState (forall a. a -> Maybe a
Just [Char]
nm) Maybe (SBV b)
mbVal
newArrayInState = forall a. HasCallStack => [Char] -> a
error [Char]
"undefined: newArrayInState"
newtype SArray a b = SArray { forall a b. SArray a b -> SArr
unSArray :: SArr }
instance (HasKind a, HasKind b) => Show (SArray a b) where
show :: SArray a b -> [Char]
show SArray{} = [Char]
"SArray<" forall a. [a] -> [a] -> [a]
++ forall a. HasKind a => a -> [Char]
showType (forall {k} (t :: k). Proxy t
Proxy @a) forall a. [a] -> [a] -> [a]
++ [Char]
":" forall a. [a] -> [a] -> [a]
++ forall a. HasKind a => a -> [Char]
showType (forall {k} (t :: k). Proxy t
Proxy @b) forall a. [a] -> [a] -> [a]
++ [Char]
">"
instance SymArray SArray where
readArray :: forall a b. SArray a b -> SBV a -> SBV b
readArray (SArray SArr
arr) (SBV SVal
a) = forall a. SVal -> SBV a
SBV (SArr -> SVal -> SVal
readSArr SArr
arr SVal
a)
writeArray :: forall b a. SymVal b => SArray a b -> SBV a -> SBV b -> SArray a b
writeArray (SArray SArr
arr) (SBV SVal
a) (SBV SVal
b) = forall a b. SArr -> SArray a b
SArray (SArr -> SVal -> SVal -> SArr
writeSArr SArr
arr SVal
a SVal
b)
mergeArrays :: forall b a.
SymVal b =>
SBool -> SArray a b -> SArray a b -> SArray a b
mergeArrays (SBV SVal
t) (SArray SArr
a) (SArray SArr
b) = forall a b. SArr -> SArray a b
SArray (SVal -> SArr -> SArr -> SArr
mergeSArr SVal
t SArr
a SArr
b)
sListArray :: forall a b. (HasKind a, SymVal b) => b -> [(SBV a, SBV b)] -> SArray a b
sListArray :: forall a b.
(HasKind a, SymVal b) =>
b -> [(SBV a, SBV b)] -> SArray a b
sListArray b
initializer = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (array :: * -> * -> *) b a.
(SymArray array, SymVal b) =>
array a b -> SBV a -> SBV b -> array a b
writeArray) SArray a b
arr
where arr :: SArray a b
arr = forall a b. SArr -> SArray a b
SArray forall a b. (a -> b) -> a -> b
$ (Kind, Kind) -> Cached ArrayIndex -> SArr
SArr (Kind, Kind)
ks forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache State -> IO ArrayIndex
r
where ks :: (Kind, Kind)
ks = (forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a), forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b))
r :: State -> IO ArrayIndex
r State
st = do ArrayMap
amap <- forall a. IORef a -> IO a
R.readIORef (State -> IORef ArrayMap
rArrayMap State
st)
let k :: ArrayIndex
k = Int -> ArrayIndex
ArrayIndex forall a b. (a -> b) -> a -> b
$ forall a. IntMap a -> Int
IMap.size ArrayMap
amap
iVal :: SBV b
iVal = forall a. SymVal a => a -> SBV a
literal b
initializer
SV
iSV <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV b
iVal
let upd :: ArrayMap -> ArrayMap
upd = forall a. Int -> a -> IntMap a -> IntMap a
IMap.insert (ArrayIndex -> Int
unArrayIndex ArrayIndex
k) ([Char]
"array_" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show ArrayIndex
k, (Kind, Kind)
ks, Maybe SV -> ArrayContext
ArrayFree (forall a. a -> Maybe a
Just SV
iSV))
ArrayIndex
k seq :: forall a b. a -> b -> b
`seq` forall a. State -> (State -> IORef a) -> (a -> a) -> IO () -> IO ()
modifyState State
st State -> IORef ArrayMap
rArrayMap ArrayMap -> ArrayMap
upd forall a b. (a -> b) -> a -> b
$ forall a. State -> (IncState -> IORef a) -> (a -> a) -> IO ()
modifyIncState State
st IncState -> IORef ArrayMap
rNewArrs ArrayMap -> ArrayMap
upd
forall (m :: * -> *) a. Monad m => a -> m a
return ArrayIndex
k
newArrayInState :: forall a b. (HasKind a, HasKind b) => Maybe String -> Maybe (SBV b) -> State -> IO (SArray a b)
newArrayInState :: forall a b.
(HasKind a, HasKind b) =>
Maybe [Char] -> Maybe (SBV b) -> State -> IO (SArray a b)
newArrayInState Maybe [Char]
mbNm Maybe (SBV b)
mbVal State
st = do forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (State -> Kind -> IO ()
registerKind State
st) [Kind
aknd, Kind
bknd]
forall a b. SArr -> SArray a b
SArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State -> (Kind, Kind) -> (Int -> [Char]) -> Maybe SVal -> IO SArr
newSArr State
st (Kind
aknd, Kind
bknd) (forall {a}. Show a => Maybe [Char] -> a -> [Char]
mkNm Maybe [Char]
mbNm) (forall a. SBV a -> SVal
unSBV forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (SBV b)
mbVal)
where mkNm :: Maybe [Char] -> a -> [Char]
mkNm Maybe [Char]
Nothing a
t = [Char]
"array_" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show a
t
mkNm (Just [Char]
nm) a
_ = [Char]
nm
aknd :: Kind
aknd = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
bknd :: Kind
bknd = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b)