-----------------------------------------------------------------------------
-- |
-- Module    : Data.SBV.Maybe
-- Copyright : (c) Joel Burget
--                 Levent Erkok
-- License   : BSD3
-- Maintainer: erkokl@gmail.com
-- Stability : experimental
--
-- Symbolic option type, symbolic version of Haskell's 'Maybe' type.
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE Rank2Types          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}

{-# OPTIONS_GHC -Wall -Werror -fno-warn-orphans #-}

module Data.SBV.Maybe (
  -- * Constructing optional values
    sJust, sNothing, liftMaybe
  -- * Destructing optionals
  , maybe
  -- * Mapping functions
  , map, map2
  -- * Scrutinizing the branches of an option
  , 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)

-- $setup
-- >>> -- For doctest purposes only:
-- >>> import Prelude hiding (maybe, map)
-- >>> import Data.SBV

-- | The symbolic 'Nothing'.
--
-- >>> sNothing :: SMaybe Integer
-- Nothing :: SMaybe Integer
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))

-- | Check if the symbolic value is nothing.
--
-- >>> isNothing (sNothing :: SMaybe Integer)
-- True
-- >>> isNothing (sJust (literal "nope"))
-- False
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)

-- | Construct an @SMaybe a@ from an @SBV a@.
--
-- >>> sJust (3 :: SInteger)
-- Just 3 :: SMaybe Integer
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]

-- | Check if the symbolic value is not nothing.
--
-- >>> isJust (sNothing :: SMaybe Integer)
-- False
-- >>> isJust (sJust (literal "yep"))
-- True
-- >>> prove $ \x -> isJust (sJust (x :: SInteger))
-- Q.E.D.
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)

-- | Return the value of an optional value. The default is returned if Nothing. Compare to 'fromJust'.
--
-- >>> fromMaybe 2 (sNothing :: SMaybe Integer)
-- 2 :: SInteger
-- >>> fromMaybe 2 (sJust 5 :: SMaybe Integer)
-- 5 :: SInteger
-- >>> prove $ \x -> fromMaybe x (sNothing :: SMaybe Integer) .== x
-- Q.E.D.
-- >>> prove $ \x -> fromMaybe (x+1) (sJust x :: SMaybe Integer) .== x
-- Q.E.D.
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

-- | Return the value of an optional value. The behavior is undefined if
-- passed Nothing, i.e., it can return any value. Compare to 'fromMaybe'.
--
-- >>> fromJust (sJust (literal 'a'))
-- 'a' :: SChar
-- >>> prove $ \x -> fromJust (sJust x) .== (x :: SChar)
-- Q.E.D.
-- >>> sat $ \x -> x .== (fromJust sNothing :: SChar)
-- Satisfiable. Model:
--   s0 = 'A' :: Char
--
-- Note how we get a satisfying assignment in the last case: The behavior
-- is unspecified, thus the SMT solver picks whatever satisfies the
-- constraints, if there is one.
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

        -- We play the usual trick here of creating a just value
        -- and asserting equivalence under implication. This will
        -- be underspecified as required should the value
        -- received be `Nothing`.
        res :: State -> IO SV
res State
st = do -- grab an internal variable and make a Maybe out of it
                    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])

                    -- Create the condition that it is equal to the input
                    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])

                    -- Gotta make sure we do this only when input is not nothing
                    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])

                    -- register the constraint:
                    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

                    -- We're good to go:
                    forall (m :: * -> *) a. Monad m => a -> m a
return SV
e

-- | Construct an @SMaybe a@ from a @Maybe (SBV a)@.
--
-- >>> liftMaybe (Just (3 :: SInteger))
-- Just 3 :: SMaybe Integer
-- >>> liftMaybe (Nothing :: Maybe SInteger)
-- Nothing :: SMaybe Integer
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 over the 'Just' side of a 'Maybe'.
--
-- >>> prove $ \x -> fromJust (map (+1) (sJust x)) .== x+(1::SInteger)
-- Q.E.D.
-- >>> let f = uninterpret "f" :: SInteger -> SBool
-- >>> prove $ \x -> map f (sJust x) .== sJust (f x)
-- Q.E.D.
-- >>> map f sNothing .== sNothing
-- True
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)

-- | Map over two maybe values.
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

-- | Case analysis for symbolic 'Maybe's. If the value 'isNothing', return the
-- default value; if it 'isJust', apply the function.
--
-- >>> maybe 0 (`sMod` 2) (sJust (3 :: SInteger))
-- 1 :: SInteger
-- >>> maybe 0 (`sMod` 2) (sNothing :: SMaybe Integer)
-- 0 :: SInteger
-- >>> let f = uninterpret "f" :: SInteger -> SBool
-- >>> prove $ \x d -> maybe d f (sJust x) .== f x
-- Q.E.D.
-- >>> prove $ \d -> maybe d f sNothing .== d
-- Q.E.D.
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

                    -- Do we have a value?
                    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]

-- | Custom 'Num' instance over 'SMaybe'
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