{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleInstances #-}
{-# 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
class AsEvaluationFailure err where
_EvaluationFailure :: Prism' err ()
evaluationFailure :: AsEvaluationFailure err => err
evaluationFailure :: err
evaluationFailure = Tagged () (Identity ()) -> Tagged err (Identity err)
forall err. AsEvaluationFailure err => Prism' err ()
_EvaluationFailure (Tagged () (Identity ()) -> Tagged err (Identity err)) -> () -> err
forall t b. AReview t b -> b -> t
# ()
_EvaluationFailureVia :: Eq err => err -> Prism' err ()
_EvaluationFailureVia :: err -> Prism' err ()
_EvaluationFailureVia err
failure = (() -> err) -> (err -> Either err ()) -> Prism' err ()
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism (err -> () -> err
forall a b. a -> b -> a
const err
failure) ((err -> Either err ()) -> Prism' err ())
-> (err -> Either err ()) -> Prism' err ()
forall a b. (a -> b) -> a -> b
$ \err
a -> Bool -> Either err () -> Either err ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (err
a err -> err -> Bool
forall a. Eq a => a -> a -> Bool
/= err
failure) (Either err () -> Either err ()) -> Either err () -> Either err ()
forall a b. (a -> b) -> a -> b
$ err -> Either err ()
forall a b. a -> Either a b
Left err
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
showList :: [EvaluationResult a] -> ShowS
$cshowList :: forall a. Show a => [EvaluationResult a] -> ShowS
show :: EvaluationResult a -> String
$cshow :: forall a. Show a => EvaluationResult a -> String
showsPrec :: Int -> EvaluationResult a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> 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
/= :: EvaluationResult a -> EvaluationResult a -> Bool
$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
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
$cto :: forall a x. Rep (EvaluationResult a) x -> EvaluationResult a
$cfrom :: forall a x. EvaluationResult a -> Rep (EvaluationResult a) x
Generic, a -> EvaluationResult b -> EvaluationResult a
(a -> b) -> EvaluationResult a -> EvaluationResult b
(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
<$ :: a -> EvaluationResult b -> EvaluationResult a
$c<$ :: forall a b. a -> EvaluationResult b -> EvaluationResult a
fmap :: (a -> b) -> EvaluationResult a -> EvaluationResult b
$cfmap :: forall a b. (a -> b) -> EvaluationResult a -> EvaluationResult b
Functor, EvaluationResult a -> Bool
(a -> m) -> EvaluationResult a -> m
(a -> b -> b) -> b -> EvaluationResult a -> b
(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
product :: EvaluationResult a -> a
$cproduct :: forall a. Num a => EvaluationResult a -> a
sum :: EvaluationResult a -> a
$csum :: forall a. Num a => EvaluationResult a -> a
minimum :: EvaluationResult a -> a
$cminimum :: forall a. Ord a => EvaluationResult a -> a
maximum :: EvaluationResult a -> a
$cmaximum :: forall a. Ord a => EvaluationResult a -> a
elem :: a -> EvaluationResult a -> Bool
$celem :: forall a. Eq a => a -> EvaluationResult a -> Bool
length :: EvaluationResult a -> Int
$clength :: forall a. EvaluationResult a -> Int
null :: EvaluationResult a -> Bool
$cnull :: forall a. EvaluationResult a -> Bool
toList :: EvaluationResult a -> [a]
$ctoList :: forall a. EvaluationResult a -> [a]
foldl1 :: (a -> a -> a) -> EvaluationResult a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
foldr1 :: (a -> a -> a) -> EvaluationResult a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> EvaluationResult a -> a
foldl' :: (b -> a -> b) -> b -> EvaluationResult a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
foldl :: (b -> a -> b) -> b -> EvaluationResult a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> EvaluationResult a -> b
foldr' :: (a -> b -> b) -> b -> EvaluationResult a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
foldr :: (a -> b -> b) -> b -> EvaluationResult a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> EvaluationResult a -> b
foldMap' :: (a -> m) -> EvaluationResult a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
foldMap :: (a -> m) -> EvaluationResult a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> EvaluationResult a -> m
fold :: EvaluationResult m -> m
$cfold :: forall m. Monoid m => EvaluationResult m -> m
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
(a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
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)
sequence :: EvaluationResult (m a) -> m (EvaluationResult a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
EvaluationResult (m a) -> m (EvaluationResult a)
mapM :: (a -> m b) -> EvaluationResult a -> m (EvaluationResult b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> EvaluationResult a -> m (EvaluationResult b)
sequenceA :: EvaluationResult (f a) -> f (EvaluationResult a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
EvaluationResult (f a) -> f (EvaluationResult a)
traverse :: (a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> EvaluationResult a -> f (EvaluationResult b)
$cp2Traversable :: Foldable EvaluationResult
$cp1Traversable :: Functor EvaluationResult
Traversable)
deriving anyclass (EvaluationResult a -> ()
(EvaluationResult a -> ()) -> NFData (EvaluationResult a)
forall a. NFData a => EvaluationResult a -> ()
forall a. (a -> ()) -> NFData a
rnf :: EvaluationResult a -> ()
$crnf :: forall a. NFData a => EvaluationResult a -> ()
NFData)
instance AsEvaluationFailure () where
_EvaluationFailure :: p () (f ()) -> p () (f ())
_EvaluationFailure = p () (f ()) -> p () (f ())
forall a. a -> a
id
instance MonadError () EvaluationResult where
throwError :: () -> EvaluationResult a
throwError () = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
catchError :: 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
instance Applicative EvaluationResult where
pure :: a -> EvaluationResult a
pure = a -> EvaluationResult a
forall a. a -> EvaluationResult a
EvaluationSuccess
EvaluationSuccess a -> b
f <*> :: EvaluationResult (a -> b)
-> EvaluationResult a -> EvaluationResult b
<*> EvaluationResult a
a = (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
instance Monad EvaluationResult where
EvaluationSuccess a
x >>= :: 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
instance Alternative EvaluationResult where
empty :: EvaluationResult a
empty = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
EvaluationSuccess a
x <|> :: EvaluationResult a -> EvaluationResult a -> EvaluationResult a
<|> EvaluationResult a
_ = a -> EvaluationResult a
forall a. a -> EvaluationResult a
EvaluationSuccess a
x
EvaluationResult a
EvaluationFailure <|> EvaluationResult a
a = EvaluationResult a
a
instance MonadFail EvaluationResult where
fail :: String -> EvaluationResult a
fail String
_ = EvaluationResult a
forall a. EvaluationResult a
EvaluationFailure
instance PrettyBy config a => PrettyBy config (EvaluationResult a) where
prettyBy :: config -> EvaluationResult a -> Doc ann
prettyBy config
config (EvaluationSuccess a
x) = 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 :: EvaluationResult a -> Doc ann
pretty = EvaluationResult a -> Doc ann
forall a ann. PrettyClassic a => a -> Doc ann
prettyClassicDef
isEvaluationSuccess :: EvaluationResult a -> Bool
isEvaluationSuccess :: 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 (t :: * -> *) a. Foldable t => t a -> Bool
null
isEvaluationFailure :: EvaluationResult a -> Bool
isEvaluationFailure :: EvaluationResult a -> Bool
isEvaluationFailure = EvaluationResult a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null