{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wall -Werror -fno-warn-orphans #-}
module Data.SBV.Maybe (
sJust, sNothing, liftMaybe
, maybe
, map, map2
, isNothing, isJust, fromMaybe, fromJust
) where
import Prelude hiding (maybe, map)
import qualified Prelude
import Data.Proxy (Proxy(Proxy))
import Data.SBV.Core.Data
import Data.SBV.Core.Model (ite)
sNothing :: forall a. SymVal a => SMaybe a
sNothing :: forall a. SymVal a => SMaybe a
sNothing = 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 -> b) -> a -> b
$ forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Kind -> CVal -> CV
CV Kind
k forall a b. (a -> b) -> a -> b
$ Maybe CVal -> CVal
CMaybe forall a. Maybe a
Nothing
where k :: Kind
k = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @(Maybe a))
isNothing :: SymVal a => SMaybe a -> SBool
isNothing :: forall a. SymVal a => SMaybe a -> SBool
isNothing = forall a b.
(SymVal a, SymVal b) =>
SBV b -> (SBV a -> SBV b) -> SMaybe a -> SBV b
maybe SBool
sTrue (forall a b. a -> b -> a
const SBool
sFalse)
sJust :: forall a. SymVal a => SBV a -> SMaybe a
sJust :: forall a. SymVal a => SBV a -> SMaybe a
sJust SBV a
sa
| Just a
a <- forall a. SymVal a => SBV a -> Maybe a
unliteral SBV a
sa
= forall a. SymVal a => a -> SBV a
literal (forall a. a -> Maybe a
Just a
a)
| Bool
True
= forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
kMaybe forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache State -> IO SV
res
where ka :: Kind
ka = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
kMaybe :: Kind
kMaybe = Kind -> Kind
KMaybe Kind
ka
res :: State -> IO SV
res State
st = do SV
asv <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV a
sa
State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kMaybe forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Kind -> Bool -> Op
MaybeConstructor Kind
ka Bool
True) [SV
asv]
isJust :: SymVal a => SMaybe a -> SBool
isJust :: forall a. SymVal a => SMaybe a -> SBool
isJust = forall a b.
(SymVal a, SymVal b) =>
SBV b -> (SBV a -> SBV b) -> SMaybe a -> SBV b
maybe SBool
sFalse (forall a b. a -> b -> a
const SBool
sTrue)
fromMaybe :: SymVal a => SBV a -> SMaybe a -> SBV a
fromMaybe :: forall a. SymVal a => SBV a -> SMaybe a -> SBV a
fromMaybe SBV a
def = forall a b.
(SymVal a, SymVal b) =>
SBV b -> (SBV a -> SBV b) -> SMaybe a -> SBV b
maybe SBV a
def forall a. a -> a
id
fromJust :: forall a. SymVal a => SMaybe a -> SBV a
fromJust :: forall a. SymVal a => SMaybe a -> SBV a
fromJust SMaybe a
ma
| Just (Just a
x) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SMaybe a
ma
= forall a. SymVal a => a -> SBV a
literal a
x
| Bool
True
= forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
ka forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache State -> IO SV
res
where ka :: Kind
ka = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
kMaybe :: Kind
kMaybe = Kind -> Kind
KMaybe Kind
ka
res :: State -> IO SV
res State
st = do
SV
e <- State -> Kind -> IO SV
internalVariable State
st Kind
ka
SV
es <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kMaybe (Op -> [SV] -> SBVExpr
SBVApp (Kind -> Bool -> Op
MaybeConstructor Kind
ka Bool
True) [SV
e])
SV
ms <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SMaybe a
ma
SV
eq <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Equal [SV
es, SV
ms])
SV
caseNothing <- forall a. State -> SBV a -> IO SV
sbvToSV State
st (forall a. SymVal a => SMaybe a -> SBool
isNothing SMaybe a
ma)
SV
require <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Or [SV
caseNothing, SV
eq])
State -> Bool -> [(String, String)] -> SVal -> IO ()
internalConstraint State
st Bool
False [] forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
KBool forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache forall a b. (a -> b) -> a -> b
$ \State
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return SV
require
forall (m :: * -> *) a. Monad m => a -> m a
return SV
e
liftMaybe :: SymVal a => Maybe (SBV a) -> SMaybe a
liftMaybe :: forall a. SymVal a => Maybe (SBV a) -> SMaybe a
liftMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
Prelude.maybe (forall a. SymVal a => a -> SBV a
literal forall a. Maybe a
Nothing) forall a. SymVal a => SBV a -> SMaybe a
sJust
map :: forall a b. (SymVal a, SymVal b)
=> (SBV a -> SBV b)
-> SMaybe a
-> SMaybe b
map :: forall a b.
(SymVal a, SymVal b) =>
(SBV a -> SBV b) -> SMaybe a -> SMaybe b
map SBV a -> SBV b
f = forall a b.
(SymVal a, SymVal b) =>
SBV b -> (SBV a -> SBV b) -> SMaybe a -> SBV b
maybe forall a. SymVal a => SMaybe a
sNothing (forall a. SymVal a => SBV a -> SMaybe a
sJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBV a -> SBV b
f)
map2 :: forall a b c. (SymVal a, SymVal b, SymVal c) => (SBV a -> SBV b -> SBV c) -> SMaybe a -> SMaybe b -> SMaybe c
map2 :: forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV b -> SBV c) -> SMaybe a -> SMaybe b -> SMaybe c
map2 SBV a -> SBV b -> SBV c
op SMaybe a
mx SMaybe b
my = forall a. Mergeable a => SBool -> a -> a -> a
ite (forall a. SymVal a => SMaybe a -> SBool
isJust SMaybe a
mx SBool -> SBool -> SBool
.&& forall a. SymVal a => SMaybe a -> SBool
isJust SMaybe b
my)
(forall a. SymVal a => SBV a -> SMaybe a
sJust (forall a. SymVal a => SMaybe a -> SBV a
fromJust SMaybe a
mx SBV a -> SBV b -> SBV c
`op` forall a. SymVal a => SMaybe a -> SBV a
fromJust SMaybe b
my))
forall a. SymVal a => SMaybe a
sNothing
maybe :: forall a b. (SymVal a, SymVal b)
=> SBV b
-> (SBV a -> SBV b)
-> SMaybe a
-> SBV b
maybe :: forall a b.
(SymVal a, SymVal b) =>
SBV b -> (SBV a -> SBV b) -> SMaybe a -> SBV b
maybe SBV b
brNothing SBV a -> SBV b
brJust SMaybe a
ma
| Just (Just a
a) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SMaybe a
ma
= SBV a -> SBV b
brJust (forall a. SymVal a => a -> SBV a
literal a
a)
| Just Maybe a
Nothing <- forall a. SymVal a => SBV a -> Maybe a
unliteral SMaybe a
ma
= SBV b
brNothing
| Bool
True
= forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
kb forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache State -> IO SV
res
where ka :: Kind
ka = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
kb :: Kind
kb = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b)
res :: State -> IO SV
res State
st = do SV
mav <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SMaybe a
ma
let justVal :: SBV a
justVal = forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
ka forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. (State -> IO a) -> Cached a
cache forall a b. (a -> b) -> a -> b
$ \State
_ -> State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
ka forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp Op
MaybeAccess [SV
mav]
justRes :: SBV b
justRes = SBV a -> SBV b
brJust forall {a}. SBV a
justVal
SV
br1 <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV b
brNothing
SV
br2 <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV b
justRes
SV
noVal <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Kind -> Bool -> Op
MaybeIs Kind
ka Bool
False) [SV
mav]
State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kb forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp Op
Ite [SV
noVal, SV
br1, SV
br2]
instance {-# OVERLAPPING #-} (Ord a, SymVal a, Num a) => Num (SBV (Maybe a)) where
+ :: SBV (Maybe a) -> SBV (Maybe a) -> SBV (Maybe a)
(+) = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV b -> SBV c) -> SMaybe a -> SMaybe b -> SMaybe c
map2 forall a. Num a => a -> a -> a
(+)
(-) = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV b -> SBV c) -> SMaybe a -> SMaybe b -> SMaybe c
map2 (-)
* :: SBV (Maybe a) -> SBV (Maybe a) -> SBV (Maybe a)
(*) = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV b -> SBV c) -> SMaybe a -> SMaybe b -> SMaybe c
map2 forall a. Num a => a -> a -> a
(*)
abs :: SBV (Maybe a) -> SBV (Maybe a)
abs = forall a b.
(SymVal a, SymVal b) =>
(SBV a -> SBV b) -> SMaybe a -> SMaybe b
map forall a. Num a => a -> a
abs
signum :: SBV (Maybe a) -> SBV (Maybe a)
signum = forall a b.
(SymVal a, SymVal b) =>
(SBV a -> SBV b) -> SMaybe a -> SMaybe b
map forall a. Num a => a -> a
signum
fromInteger :: Integer -> SBV (Maybe a)
fromInteger = forall a. SymVal a => SBV a -> SMaybe a
sJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger