{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wall -Werror #-}
module Data.SBV.Either (
sLeft, sRight, liftEither
, either
, bimap, first, second
, isLeft, isRight, fromLeft, fromRight
) where
import Prelude hiding (either)
import qualified Prelude
import Data.Proxy (Proxy(Proxy))
import Data.SBV.Core.Data
import Data.SBV.Core.Model ()
sLeft :: forall a b. (SymVal a, SymVal b) => SBV a -> SEither a b
sLeft :: forall a b. (SymVal a, SymVal b) => SBV a -> SEither a b
sLeft 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 b. a -> Either a b
Left 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
k 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 k1 :: Kind
k1 = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
k2 :: Kind
k2 = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b)
k :: Kind
k = Kind -> Kind -> Kind
KEither Kind
k1 Kind
k2
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
k forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Kind -> Kind -> Bool -> Op
EitherConstructor Kind
k1 Kind
k2 Bool
False) [SV
asv]
isLeft :: (SymVal a, SymVal b) => SEither a b -> SBV Bool
isLeft :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV Bool
isLeft = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV c) -> (SBV b -> SBV c) -> SEither a b -> SBV c
either (forall a b. a -> b -> a
const SBV Bool
sTrue) (forall a b. a -> b -> a
const SBV Bool
sFalse)
sRight :: forall a b. (SymVal a, SymVal b) => SBV b -> SEither a b
sRight :: forall a b. (SymVal a, SymVal b) => SBV b -> SEither a b
sRight SBV b
sb
| Just b
b <- forall a. SymVal a => SBV a -> Maybe a
unliteral SBV b
sb
= forall a. SymVal a => a -> SBV a
literal (forall a b. b -> Either a b
Right b
b)
| Bool
True
= 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. 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 k1 :: Kind
k1 = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @a)
k2 :: Kind
k2 = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b)
k :: Kind
k = Kind -> Kind -> Kind
KEither Kind
k1 Kind
k2
res :: State -> IO SV
res State
st = do SV
bsv <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV b
sb
State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
k forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Kind -> Kind -> Bool -> Op
EitherConstructor Kind
k1 Kind
k2 Bool
True) [SV
bsv]
isRight :: (SymVal a, SymVal b) => SEither a b -> SBV Bool
isRight :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV Bool
isRight = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV c) -> (SBV b -> SBV c) -> SEither a b -> SBV c
either (forall a b. a -> b -> a
const SBV Bool
sFalse) (forall a b. a -> b -> a
const SBV Bool
sTrue)
liftEither :: (SymVal a, SymVal b) => Either (SBV a) (SBV b) -> SEither a b
liftEither :: forall a b.
(SymVal a, SymVal b) =>
Either (SBV a) (SBV b) -> SEither a b
liftEither = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
Prelude.either forall a b. (SymVal a, SymVal b) => SBV a -> SEither a b
sLeft forall a b. (SymVal a, SymVal b) => SBV b -> SEither a b
sRight
either :: forall a b c. (SymVal a, SymVal b, SymVal c)
=> (SBV a -> SBV c)
-> (SBV b -> SBV c)
-> SEither a b
-> SBV c
either :: forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV c) -> (SBV b -> SBV c) -> SEither a b -> SBV c
either SBV a -> SBV c
brA SBV b -> SBV c
brB SEither a b
sab
| Just (Left a
a) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SEither a b
sab
= SBV a -> SBV c
brA forall a b. (a -> b) -> a -> b
$ forall a. SymVal a => a -> SBV a
literal a
a
| Just (Right b
b) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SEither a b
sab
= SBV b -> SBV c
brB forall a b. (a -> b) -> a -> b
$ forall a. SymVal a => a -> SBV a
literal b
b
| Bool
True
= forall a. SVal -> SBV a
SBV forall a b. (a -> b) -> a -> b
$ Kind -> Either CV (Cached SV) -> SVal
SVal Kind
kc 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)
kc :: Kind
kc = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @c)
res :: State -> IO SV
res State
st = do SV
abv <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SEither a b
sab
let leftVal :: SBV a
leftVal = 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 (Bool -> Op
EitherAccess Bool
False) [SV
abv]
rightVal :: SBV a
rightVal = 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 forall a b. (a -> b) -> a -> b
$ \State
_ -> State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kb forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Bool -> Op
EitherAccess Bool
True) [SV
abv]
leftRes :: SBV c
leftRes = SBV a -> SBV c
brA forall {a}. SBV a
leftVal
rightRes :: SBV c
rightRes = SBV b -> SBV c
brB forall {a}. SBV a
rightVal
SV
br1 <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV c
leftRes
SV
br2 <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SBV c
rightRes
SV
onLeft <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp (Kind -> Kind -> Bool -> Op
EitherIs Kind
ka Kind
kb Bool
False) [SV
abv]
State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kc forall a b. (a -> b) -> a -> b
$ Op -> [SV] -> SBVExpr
SBVApp Op
Ite [SV
onLeft, SV
br1, SV
br2]
bimap :: forall a b c d. (SymVal a, SymVal b, SymVal c, SymVal d)
=> (SBV a -> SBV b)
-> (SBV c -> SBV d)
-> SEither a c
-> SEither b d
bimap :: forall a b c d.
(SymVal a, SymVal b, SymVal c, SymVal d) =>
(SBV a -> SBV b) -> (SBV c -> SBV d) -> SEither a c -> SEither b d
bimap SBV a -> SBV b
brA SBV c -> SBV d
brC = forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV c) -> (SBV b -> SBV c) -> SEither a b -> SBV c
either (forall a b. (SymVal a, SymVal b) => SBV a -> SEither a b
sLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBV a -> SBV b
brA) (forall a b. (SymVal a, SymVal b) => SBV b -> SEither a b
sRight forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBV c -> SBV d
brC)
first :: (SymVal a, SymVal b, SymVal c) => (SBV a -> SBV b) -> SEither a c -> SEither b c
first :: forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV a -> SBV b) -> SEither a c -> SEither b c
first SBV a -> SBV b
f = forall a b c d.
(SymVal a, SymVal b, SymVal c, SymVal d) =>
(SBV a -> SBV b) -> (SBV c -> SBV d) -> SEither a c -> SEither b d
bimap SBV a -> SBV b
f forall a. a -> a
id
second :: (SymVal a, SymVal b, SymVal c) => (SBV b -> SBV c) -> SEither a b -> SEither a c
second :: forall a b c.
(SymVal a, SymVal b, SymVal c) =>
(SBV b -> SBV c) -> SEither a b -> SEither a c
second = forall a b c d.
(SymVal a, SymVal b, SymVal c, SymVal d) =>
(SBV a -> SBV b) -> (SBV c -> SBV d) -> SEither a c -> SEither b d
bimap forall a. a -> a
id
fromLeft :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV a
fromLeft :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV a
fromLeft SEither a b
sab
| Just (Left a
a) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SEither a b
sab
= forall a. SymVal a => a -> SBV a
literal 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
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)
kb :: Kind
kb = forall a. HasKind a => a -> Kind
kindOf (forall {k} (t :: k). Proxy t
Proxy @b)
kEither :: Kind
kEither = Kind -> Kind -> Kind
KEither Kind
ka Kind
kb
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
kEither (Op -> [SV] -> SBVExpr
SBVApp (Kind -> Kind -> Bool -> Op
EitherConstructor Kind
ka Kind
kb Bool
False) [SV
e])
SV
ms <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SEither a b
sab
SV
eq <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Equal [SV
es, SV
ms])
SV
caseRight <- forall a. State -> SBV a -> IO SV
sbvToSV State
st (forall a b. (SymVal a, SymVal b) => SEither a b -> SBV Bool
isRight SEither a b
sab)
SV
require <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Or [SV
caseRight, 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
fromRight :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV b
fromRight :: forall a b. (SymVal a, SymVal b) => SEither a b -> SBV b
fromRight SEither a b
sab
| Just (Right b
b) <- forall a. SymVal a => SBV a -> Maybe a
unliteral SEither a b
sab
= forall a. SymVal a => a -> SBV a
literal b
b
| 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)
kEither :: Kind
kEither = Kind -> Kind -> Kind
KEither Kind
ka Kind
kb
res :: State -> IO SV
res State
st = do
SV
e <- State -> Kind -> IO SV
internalVariable State
st Kind
kb
SV
es <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
kEither (Op -> [SV] -> SBVExpr
SBVApp (Kind -> Kind -> Bool -> Op
EitherConstructor Kind
ka Kind
kb Bool
True) [SV
e])
SV
ms <- forall a. State -> SBV a -> IO SV
sbvToSV State
st SEither a b
sab
SV
eq <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Equal [SV
es, SV
ms])
SV
caseLeft <- forall a. State -> SBV a -> IO SV
sbvToSV State
st (forall a b. (SymVal a, SymVal b) => SEither a b -> SBV Bool
isLeft SEither a b
sab)
SV
require <- State -> Kind -> SBVExpr -> IO SV
newExpr State
st Kind
KBool (Op -> [SV] -> SBVExpr
SBVApp Op
Or [SV
caseLeft, 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
{-# ANN module ("HLint: ignore Reduce duplication" :: String) #-}