{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module PlutusCore.Builtin.HasConstant
( throwNotAConstant
, AsConstant (..)
, FromConstant (..)
, HasConstant
, HasConstantIn
) where
import PlutusCore.Core
import PlutusCore.Evaluation.Machine.Exception
import PlutusCore.Name
import Control.Monad.Except
import Universe
throwNotAConstant
:: (MonadError (ErrorWithCause err cause) m, AsUnliftingError err)
=> Maybe cause -> m r
throwNotAConstant :: Maybe cause -> m r
throwNotAConstant = AReview err UnliftingError -> UnliftingError -> Maybe cause -> m r
forall exc e t term (m :: * -> *) x.
(exc ~ ErrorWithCause e term, MonadError exc m) =>
AReview e t -> t -> Maybe term -> m x
throwingWithCause AReview err UnliftingError
forall r. AsUnliftingError r => Prism' r UnliftingError
_UnliftingError UnliftingError
"Not a constant"
class AsConstant term where
asConstant
:: (MonadError (ErrorWithCause err cause) m, AsUnliftingError err)
=> Maybe cause -> term -> m (Some (ValueOf (UniOf term)))
class FromConstant term where
fromConstant :: Some (ValueOf (UniOf term)) -> term
instance AsConstant (Term TyName Name uni fun ann) where
asConstant :: Maybe cause
-> Term TyName Name uni fun ann
-> m (Some (ValueOf (UniOf (Term TyName Name uni fun ann))))
asConstant Maybe cause
_ (Constant ann
_ Some (ValueOf uni)
val) = Some (ValueOf uni) -> m (Some (ValueOf uni))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Some (ValueOf uni)
val
asConstant Maybe cause
mayCause Term TyName Name uni fun ann
_ = Maybe cause -> m (Some (ValueOf uni))
forall err cause (m :: * -> *) r.
(MonadError (ErrorWithCause err cause) m, AsUnliftingError err) =>
Maybe cause -> m r
throwNotAConstant Maybe cause
mayCause
instance FromConstant (Term tyname name uni fun ()) where
fromConstant :: Some (ValueOf (UniOf (Term tyname name uni fun ())))
-> Term tyname name uni fun ()
fromConstant = () -> Some (ValueOf uni) -> Term tyname name uni fun ()
forall tyname name (uni :: * -> *) fun ann.
ann -> Some (ValueOf uni) -> Term tyname name uni fun ann
Constant ()
type HasConstant term = (AsConstant term, FromConstant term)
type HasConstantIn uni term = (UniOf term ~ uni, HasConstant term)