{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module PlutusCore.Builtin.HasConstant
( throwNotAConstant
, 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 HasConstant term where
asConstant
:: AsUnliftingError err
=> Maybe cause -> term -> Either (ErrorWithCause err cause) (Some (ValueOf (UniOf term)))
fromConstant :: Some (ValueOf (UniOf term)) -> term
type HasConstantIn uni term = (UniOf term ~ uni, HasConstant term)
instance HasConstant (Term TyName Name uni fun ()) where
asConstant :: Maybe cause
-> Term TyName Name uni fun ()
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Term TyName Name uni fun ()))))
asConstant Maybe cause
_ (Constant ()
_ Some (ValueOf uni)
val) = Some (ValueOf uni)
-> Either (ErrorWithCause err cause) (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 ()
_ = Maybe cause
-> Either (ErrorWithCause err cause) (Some (ValueOf uni))
forall err cause (m :: * -> *) r.
(MonadError (ErrorWithCause err cause) m, AsUnliftingError err) =>
Maybe cause -> m r
throwNotAConstant Maybe cause
mayCause
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 ()