-- | This module defines a common type various evaluation machine use to return their results.

{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE UndecidableInstances  #-}

module PlutusCore.Evaluation.Result
    ( AsEvaluationFailure (..)
    , evaluationFailure
    , _EvaluationFailureVia
    , EvaluationResult (..)
    , isEvaluationSuccess
    , isEvaluationFailure
    ) where

import PlutusPrelude

import PlutusCore.Pretty

import Control.Lens
import Control.Monad.Except (MonadError, catchError, throwError)

-- Note that we can't just use 'makeClassyPrisms' for 'EvaluationResult' as that would generate
-- @_EvaluationSuccess@ as well as @_EvaluationFailure@, which would no longer be prismatic error
-- handling: it'd constrain the monad, not the error in it.
--
-- We could define
--
--     data EvaluationFailure = EvaluationFailure
--
-- just for the purpose of using 'makeClassyPrisms' over it, but then it's clearer to write out
-- the class definition manually than to hide it behind a TH function called over a redundant
-- data type name-clashing with the useful 'EvaluationResult'.
-- | A class for viewing errors as evaluation failures (in the sense of Plutus).
class AsEvaluationFailure err where
    _EvaluationFailure :: Prism' err ()

evaluationFailure :: AsEvaluationFailure err => err
evaluationFailure :: forall err. AsEvaluationFailure err => err
evaluationFailure = Tagged () (Identity ()) -> Tagged err (Identity err)
forall err. AsEvaluationFailure err => Prism' err ()
Prism' err ()
_EvaluationFailure (Tagged () (Identity ()) -> Tagged err (Identity err)) -> () -> err
forall t b. AReview t b -> b -> t
# ()
{-# INLINE evaluationFailure #-}

-- | Construct a prism focusing on the @*EvaluationFailure@ part of @err@ by taking
-- that @*EvaluationFailure@ and
--
-- 1. returning it for the setter part of the prism
-- 2. checking the error for equality with @*EvaluationFailure@ for the opposite direction.
_EvaluationFailureVia :: Eq err => err -> Prism' err ()
_EvaluationFailureVia :: forall err. Eq err => err -> Prism' err ()
_EvaluationFailureVia = err -> p () (f ()) -> p err (f err)
err -> Prism' err ()
forall err. Eq err => err -> Prism' err ()
only
{-# INLINE _EvaluationFailureVia #-}

-- | The parameterized type of results various evaluation engines return.
-- On the PLC side this becomes (via @makeKnown@) either a call to 'Error' or
-- a value of the PLC counterpart of type @a@.
data EvaluationResult a
    = EvaluationSuccess !a
    | EvaluationFailure
    deriving stock (Int -> EvaluationResult a -> ShowS
[EvaluationResult a] -> ShowS
EvaluationResult a -> String
(Int -> EvaluationResult a -> ShowS)
-> (EvaluationResult a -> String)
-> ([EvaluationResult a] -> ShowS)
-> Show (EvaluationResult a)
forall a. Show a => Int -> EvaluationResult a -> ShowS
forall a. Show a => [EvaluationResult a] -> ShowS
forall a. Show a => EvaluationResult a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> EvaluationResult a -> ShowS
showsPrec :: Int -> EvaluationResult a -> ShowS
$cshow :: forall a. Show a => EvaluationResult a -> String
show :: EvaluationResult a -> String
$cshowList :: forall a. Show a => [EvaluationResult a] -> ShowS
showList :: [EvaluationResult a] -> ShowS
Show, EvaluationResult a -> EvaluationResult a -> Bool
(EvaluationResult a -> EvaluationResult a -> Bool)
-> (EvaluationResult a -> EvaluationResult a -> Bool)
-> Eq (EvaluationResult a)
forall a. Eq a => EvaluationResult a -> EvaluationResult a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => EvaluationResult a -> EvaluationResult a -> Bool
== :: EvaluationResult a -> EvaluationResult a -> Bool
$c/= :: forall a. Eq a => EvaluationResult a -> EvaluationResult a -> Bool
/= :: EvaluationResult a -> EvaluationResult a -> Bool
Eq, (forall x. EvaluationResult a -> Rep (EvaluationResult a) x)
-> (forall x. Rep (EvaluationResult a) x -> EvaluationResult a)
-> Generic (EvaluationResult a)
forall x. Rep (EvaluationResult a) x -> EvaluationResult a
forall x. EvaluationResult a -> Rep (EvaluationResult a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (EvaluationResult a) x -> EvaluationResult a
forall a x. EvaluationResult a -> Rep (EvaluationResult a) x
$cfrom :: forall a x. EvaluationResult a -> Rep (EvaluationResult a) x
from :: forall x. EvaluationResult a -> Rep (EvaluationResult a) x
$cto :: forall a x. Rep (EvaluationResult a) x -> EvaluationResult a
to :: forall x. Rep (EvaluationResult a) x -> EvaluationResult a
Generic, (forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b)
-> (forall a b. a -> EvaluationResult b -> EvaluationResult a)
-> Functor EvaluationResult
forall a b. a -> EvaluationResult b -> EvaluationResult a
forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b
fmap :: forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b
$c<$ :: forall a b. a -> EvaluationResult b -> EvaluationResult a
<$ :: forall a b. a -> EvaluationResult b -> EvaluationResult a
Functor, (forall m. Monoid m => EvaluationResult m -> m)
-> (forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m)
-> (forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m)
-> (forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b)
-> (forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b)
-> (forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b)
-> (forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b)
-> (forall a. (a -> a -> a) -> EvaluationResult a -> a)
-> (forall a. (a -> a -> a) -> EvaluationResult a -> a)
-> (forall a. EvaluationResult a -> [a])
-> (forall a. EvaluationResult a -> Bool)
-> (forall a. EvaluationResult a -> Int)
-> (forall a. Eq a => a -> EvaluationResult a -> Bool)
-> (forall a. Ord a => EvaluationResult a -> a)
-> (forall a. Ord a => EvaluationResult a -> a)
-> (forall a. Num a => EvaluationResult a -> a)
-> (forall a. Num a => EvaluationResult a -> a)
-> Foldable EvaluationResult
forall a. Eq a => a -> EvaluationResult a -> Bool
forall a. Num a => EvaluationResult a -> a
forall a. Ord a => EvaluationResult a -> a
forall m. Monoid m => EvaluationResult m -> m
forall a. EvaluationResult a -> Bool
forall a. EvaluationResult a -> Int
forall a. EvaluationResult a -> [a]
forall a. (a -> a -> a) -> EvaluationResult a -> a
forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => EvaluationResult m -> m
fold :: forall m. Monoid m => EvaluationResult m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
foldr :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
foldl :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
foldr1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
foldl1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
$ctoList :: forall a. EvaluationResult a -> [a]
toList :: forall a. EvaluationResult a -> [a]
$cnull :: forall a. EvaluationResult a -> Bool
null :: forall a. EvaluationResult a -> Bool
$clength :: forall a. EvaluationResult a -> Int
length :: forall a. EvaluationResult a -> Int
$celem :: forall a. Eq a => a -> EvaluationResult a -> Bool
elem :: forall a. Eq a => a -> EvaluationResult a -> Bool
$cmaximum :: forall a. Ord a => EvaluationResult a -> a
maximum :: forall a. Ord a => EvaluationResult a -> a
$cminimum :: forall a. Ord a => EvaluationResult a -> a
minimum :: forall a. Ord a => EvaluationResult a -> a
$csum :: forall a. Num a => EvaluationResult a -> a
sum :: forall a. Num a => EvaluationResult a -> a
$cproduct :: forall a. Num a => EvaluationResult a -> a
product :: forall a. Num a => EvaluationResult a -> a
Foldable, Functor EvaluationResult
Foldable EvaluationResult
(Functor EvaluationResult, Foldable EvaluationResult) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> EvaluationResult a -> f (EvaluationResult b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    EvaluationResult (f a) -> f (EvaluationResult a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> EvaluationResult a -> m (EvaluationResult b))
-> (forall (m :: * -> *) a.
    Monad m =>
    EvaluationResult (m a) -> m (EvaluationResult a))
-> Traversable EvaluationResult
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
EvaluationResult (m a) -> m (EvaluationResult a)
forall (f :: * -> *) a.
Applicative f =>
EvaluationResult (f a) -> f (EvaluationResult a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> EvaluationResult a -> m (EvaluationResult b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
EvaluationResult (f a) -> f (EvaluationResult a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
EvaluationResult (f a) -> f (EvaluationResult a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> EvaluationResult a -> m (EvaluationResult b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> EvaluationResult a -> m (EvaluationResult b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
EvaluationResult (m a) -> m (EvaluationResult a)
sequence :: forall (m :: * -> *) a.
Monad m =>
EvaluationResult (m a) -> m (EvaluationResult a)
Traversable)
    deriving anyclass (EvaluationResult a -> ()
(EvaluationResult a -> ()) -> NFData (EvaluationResult a)
forall a. NFData a => EvaluationResult a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => EvaluationResult a -> ()
rnf :: EvaluationResult a -> ()
NFData)

-- >>> evaluationFailure :: EvaluationResult Bool
-- EvaluationFailure
--
-- >>> import Control.Lens
-- >>> matching _EvaluationFailure (EvaluationFailure :: EvaluationResult Bool)
-- Right ()
--
-- >>> matching _EvaluationFailure $ EvaluationSuccess True
-- Left (EvaluationSuccess True)
instance AsEvaluationFailure (EvaluationResult a) where
    _EvaluationFailure :: Prism' (EvaluationResult a) ()
_EvaluationFailure = (() -> EvaluationResult a)
-> (EvaluationResult a -> Either (EvaluationResult a) ())
-> Prism' (EvaluationResult a) ()
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (EvaluationResult a -> () -> EvaluationResult a
forall a b. a -> b -> a
const EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure) ((EvaluationResult a -> Either (EvaluationResult a) ())
 -> Prism' (EvaluationResult a) ())
-> (EvaluationResult a -> Either (EvaluationResult a) ())
-> Prism' (EvaluationResult a) ()
forall a b. (a -> b) -> a -> b
$ \case
        a :: EvaluationResult a
a@EvaluationSuccess{} -> EvaluationResult a -> Either (EvaluationResult a) ()
forall a b. a -> Either a b
Left EvaluationResult a
a
        EvaluationResult a
EvaluationFailure     -> () -> Either (EvaluationResult a) ()
forall a b. b -> Either a b
Right ()
    {-# INLINE _EvaluationFailure #-}

-- This and the next one are two instances that allow us to write the following:
--
-- >>> import Control.Monad.Error.Lens
-- >>> throwing_ _EvaluationFailure :: EvaluationResult Bool
-- EvaluationFailure
instance AsEvaluationFailure () where
    _EvaluationFailure :: Prism' () ()
_EvaluationFailure = p () (f ()) -> p () (f ())
forall a. a -> a
id
    {-# INLINE _EvaluationFailure #-}

instance MonadError () EvaluationResult where
    throwError :: forall a. () -> EvaluationResult a
throwError () = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE throwError #-}

    catchError :: forall a.
EvaluationResult a
-> (() -> EvaluationResult a) -> EvaluationResult a
catchError EvaluationResult a
EvaluationFailure () -> EvaluationResult a
f = () -> EvaluationResult a
f ()
    catchError EvaluationResult a
x                 () -> EvaluationResult a
_ = EvaluationResult a
x
    {-# INLINE catchError #-}

instance Applicative EvaluationResult where
    pure :: forall a. a -> EvaluationResult a
pure = a -> EvaluationResult a
forall a. a -> EvaluationResult a
EvaluationSuccess
    {-# INLINE pure #-}

    EvaluationSuccess a -> b
f <*> :: forall a b.
EvaluationResult (a -> b)
-> EvaluationResult a -> EvaluationResult b
<*> EvaluationResult a
a = (a -> b) -> EvaluationResult a -> EvaluationResult b
forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f EvaluationResult a
a
    EvaluationResult (a -> b)
EvaluationFailure   <*> EvaluationResult a
_ = EvaluationResult b
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE (<*>) #-}

    EvaluationSuccess a
_ *> :: forall a b.
EvaluationResult a -> EvaluationResult b -> EvaluationResult b
*> EvaluationResult b
b = EvaluationResult b
b
    EvaluationResult a
EvaluationFailure   *> EvaluationResult b
_ = EvaluationResult b
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE (*>) #-}

instance Monad EvaluationResult where
    EvaluationSuccess a
x >>= :: forall a b.
EvaluationResult a
-> (a -> EvaluationResult b) -> EvaluationResult b
>>= a -> EvaluationResult b
f = a -> EvaluationResult b
f a
x
    EvaluationResult a
EvaluationFailure   >>= a -> EvaluationResult b
_ = EvaluationResult b
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE (>>=) #-}

    >> :: forall a b.
EvaluationResult a -> EvaluationResult b -> EvaluationResult b
(>>) = EvaluationResult a -> EvaluationResult b -> EvaluationResult b
forall a b.
EvaluationResult a -> EvaluationResult b -> EvaluationResult b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
    {-# INLINE (>>) #-}

instance Alternative EvaluationResult where
    empty :: forall a. EvaluationResult a
empty = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE empty #-}

    a :: EvaluationResult a
a@EvaluationSuccess{} <|> :: forall a.
EvaluationResult a -> EvaluationResult a -> EvaluationResult a
<|> EvaluationResult a
_ = EvaluationResult a
a
    EvaluationResult a
EvaluationFailure     <|> EvaluationResult a
b = EvaluationResult a
b
    {-# INLINE (<|>) #-}

instance MonadFail EvaluationResult where
    fail :: forall a. String -> EvaluationResult a
fail String
_ = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
    {-# INLINE fail #-}

instance PrettyBy config a => PrettyBy config (EvaluationResult a) where
    prettyBy :: forall ann. config -> EvaluationResult a -> Doc ann
prettyBy config
config (EvaluationSuccess a
x) = config -> a -> Doc ann
forall ann. config -> a -> Doc ann
forall config a ann. PrettyBy config a => config -> a -> Doc ann
prettyBy config
config a
x
    prettyBy config
_      EvaluationResult a
EvaluationFailure     = Doc ann
"Failure"

instance PrettyClassic a => Pretty (EvaluationResult a) where
    pretty :: forall ann. EvaluationResult a -> Doc ann
pretty = EvaluationResult a -> Doc ann
forall a ann. PrettyClassic a => a -> Doc ann
prettyClassic

-- | Check whether an 'EvaluationResult' is an 'EvaluationSuccess'.
isEvaluationSuccess :: EvaluationResult a -> Bool
isEvaluationSuccess :: forall a. EvaluationResult a -> Bool
isEvaluationSuccess = Bool -> Bool
not (Bool -> Bool)
-> (EvaluationResult a -> Bool) -> EvaluationResult a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EvaluationResult a -> Bool
forall a. EvaluationResult a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null

-- | Check whether an 'EvaluationResult' is an 'EvaluationFailure'.
isEvaluationFailure :: EvaluationResult a -> Bool
isEvaluationFailure :: forall a. EvaluationResult a -> Bool
isEvaluationFailure = EvaluationResult a -> Bool
forall a. EvaluationResult a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null