Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Builtins] Drop 'val' from 'KnownType' stuff #4499

Closed
Prev Previous commit
Back to 'HasConstant'
effectfully committed Apr 8, 2022
commit a403715ff6c496e521af3ba20f91a140f062d3fb
Original file line number Diff line number Diff line change
@@ -209,7 +209,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni ExtensionFun where
where
idAssumeCheckBoolPlc :: Opaque val Bool -> EvaluationResult Bool
idAssumeCheckBoolPlc val =
case asConstant @_ @_ @UnliftingError Nothing val of
case asConstant @_ @UnliftingError Nothing val of
Right (Some (ValueOf DefaultUniBool b)) -> EvaluationSuccess b
_ -> EvaluationFailure

16 changes: 10 additions & 6 deletions plutus-core/plutus-core/src/PlutusCore/Builtin/HasConstant.hs
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@

module PlutusCore.Builtin.HasConstant
( throwNotAConstant
, HasConstantIn (..)
, HasConstant (..)
, HasConstantIn
) where

import PlutusCore.Core
@@ -22,20 +23,23 @@ throwNotAConstant
=> Maybe cause -> m r
throwNotAConstant = throwingWithCause _UnliftingError "Not a constant"

-- | Ensures that @term@ has a 'Constant'-like constructor to lift values to and unlift values from
-- and connects @term@ and its @uni@.
class UniOf term ~ uni => HasConstantIn uni term where
-- | Ensures that @term@ has a 'Constant'-like constructor to lift values to and unlift values from.
class HasConstant term where
-- Switching from 'MonadError' to 'Either' here gave us a speedup of 2-4%.
-- | Unlift from the 'Constant' constructor throwing an 'UnliftingError' if the provided @term@
-- is not a 'Constant'.
asConstant
:: AsUnliftingError err
=> Maybe cause -> term -> Either (ErrorWithCause err cause) (Some (ValueOf uni))
=> Maybe cause -> term -> Either (ErrorWithCause err cause) (Some (ValueOf (UniOf term)))

-- | Wrap a Haskell value as a @term@.
fromConstant :: Some (ValueOf (UniOf term)) -> term

instance HasConstantIn uni (Term TyName Name uni fun ()) where
-- | Ensures that @term@ has a 'Constant'-like constructor to lift values to and unlift values from
-- and connects @term@ and its @uni@.
type HasConstantIn uni term = (UniOf term ~ uni, HasConstant term)

instance HasConstant (Term TyName Name uni fun ()) where
asConstant _ (Constant _ val) = pure val
asConstant mayCause _ = throwNotAConstant mayCause

4 changes: 2 additions & 2 deletions plutus-core/plutus-core/src/PlutusCore/Builtin/KnownType.hs
Original file line number Diff line number Diff line change
@@ -251,7 +251,7 @@ the cause stored in it is not forced due to @Maybe@ being a lazy data type.
type MakeKnownIn :: (GHC.Type -> GHC.Type) -> GHC.Type -> GHC.Constraint
class MakeKnownIn uni a where
type AssociateValueMake uni a :: GHC.Type -> GHC.Constraint
type AssociateValueMake uni a = HasConstantIn uni
type AssociateValueMake uni a = HasConstant

-- See Note [Cause of failure].
-- | Convert a Haskell value to the corresponding PLC val.
@@ -274,7 +274,7 @@ type MakeKnown val a = (MakeKnownIn (UniOf val) a, AssociateValueMake (UniOf val
type ReadKnownIn :: (GHC.Type -> GHC.Type) -> GHC.Type -> GHC.Constraint
class ReadKnownIn uni a where
type AssociateValueRead uni a :: GHC.Type -> GHC.Constraint
type AssociateValueRead uni a = HasConstantIn uni
type AssociateValueRead uni a = HasConstant

-- See Note [Cause of failure].
-- | Convert a PLC val to the corresponding Haskell value.
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ We need to support polymorphism for built-in functions for these reasons:
-- Haskell and back and instead can keep it intact.
newtype Opaque val (rep :: GHC.Type) = Opaque
{ unOpaque :: val
} deriving newtype (Pretty, HasConstantIn uni)
} deriving newtype (Pretty, HasConstant)

type instance UniOf (Opaque val rep) = UniOf val

Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ emitCkM logs = do

type instance UniOf (CkValue uni fun) = uni

instance HasConstantIn uni (CkValue uni fun) where
instance HasConstant (CkValue uni fun) where
asConstant _ (VCon val) = pure val
asConstant mayCause _ = throwNotAConstant mayCause

4 changes: 2 additions & 2 deletions plutus-core/plutus-ir/src/PlutusIR/Core/Type.hs
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ import PlutusPrelude
import Control.Lens.TH
import PlutusCore (Kind, Name, TyName, Type (..))
import PlutusCore qualified as PLC
import PlutusCore.Builtin (HasConstantIn (..), throwNotAConstant)
import PlutusCore.Builtin (HasConstant (..), throwNotAConstant)
import PlutusCore.Core (UniOf)
import PlutusCore.Flat ()
import PlutusCore.MkPlc (Def (..), TermLike (..), TyVarDecl (..), VarDecl (..))
@@ -129,7 +129,7 @@ data Term tyname name uni fun a =

type instance UniOf (Term tyname name uni fun ann) = uni

instance HasConstantIn uni (Term tyname name uni fun ()) where
instance HasConstant (Term tyname name uni fun ()) where
asConstant _ (Constant _ val) = pure val
asConstant mayCause _ = throwNotAConstant mayCause

Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ instance TermLike (Term name uni fun) TPLC.TyName name uni fun where
iWrap = \_ _ _ -> id
error = \ann _ -> Error ann

instance TPLC.HasConstantIn uni (Term name uni fun ()) where
instance TPLC.HasConstant (Term name uni fun ()) where
asConstant _ (Constant _ val) = pure val
asConstant mayCause _ = TPLC.throwNotAConstant mayCause

Original file line number Diff line number Diff line change
@@ -491,7 +491,7 @@ instance (Closed uni, GShow uni, uni `Everywhere` PrettyConst, Pretty fun) =>

type instance UniOf (CekValue uni fun) = uni

instance HasConstantIn uni (CekValue uni fun) where
instance HasConstant (CekValue uni fun) where
asConstant _ (VCon val) = pure val
asConstant mayCause _ = throwNotAConstant mayCause