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] Introduce 'HiddenValueOf' #4249

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Cache 'hiddenValueOf' in 'HiddenValueOf DefaultUni'"
This reverts commit 7f4903e.
effectfully committed Nov 28, 2021

Unverified

This user has not yet uploaded their public signing key.
commit 05db7ed2e2adf1bb8f70a6c8459a7bb92694e8aa
16 changes: 8 additions & 8 deletions plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs
Original file line number Diff line number Diff line change
@@ -288,7 +288,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
where
choosePlc :: SomeConstantPoly uni [] '[a] -> b -> b -> EvaluationResult b
choosePlc (SomeConstantPoly val) a b = do
ValueOfDefaultUniList _ _ xs <- pure val
ValueOfDefaultUniList _ xs <- pure val
pure $ case xs of
[] -> a
_ : _ -> b
@@ -305,7 +305,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
(SomeConstant xOfUniA)
-- (SomeConstantOfArg uniA (SomeConstantOfRes uniListA xs)) =
(SomeConstantPoly val) = do
ValueOfDefaultUniList valOf uniA xs <- pure val
ValueOfDefaultUniList uniA xs <- pure val
-- Checking that the type of the constant is the same as the type of the elements
-- of the unlifted list. Note that there's no way we could enforce this statically
-- since in UPLC one can create an ill-typed program that attempts to prepend
@@ -315,17 +315,17 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
-- https://github.com/input-output-hk/plutus/pull/3035
Nothing -> EvaluationFailure
Just x ->
EvaluationSuccess . fromConstant . ValueOfDefaultUniList valOf uniA $ x : xs
EvaluationSuccess . fromConstant . ValueOfDefaultUniList uniA $ x : xs
toBuiltinMeaning HeadList =
makeBuiltinMeaning
headPlc
(runCostingFunOneArgument . paramHeadList)
where
headPlc :: SomeConstantPoly uni [] '[a] -> EvaluationResult (Opaque term a)
headPlc (SomeConstantPoly val) = do
ValueOfDefaultUniList valOf _ xs <- pure val
ValueOfDefaultUniList uniA xs <- pure val
x : _ <- pure xs
pure . fromConstant $ valOf x
pure . fromConstant $ hiddenValueOf uniA x
toBuiltinMeaning TailList =
makeBuiltinMeaning
tailPlc
@@ -335,17 +335,17 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where
:: listA ~ SomeConstantPoly uni [] '[a]
=> listA -> EvaluationResult (Opaque term listA)
tailPlc (SomeConstantPoly val) = do
ValueOfDefaultUniList valOf uniA xs <- pure val
ValueOfDefaultUniList uniA xs <- pure val
_ : xs' <- pure xs
pure . fromConstant $ ValueOfDefaultUniList valOf uniA xs'
pure . fromConstant $ ValueOfDefaultUniList uniA xs'
toBuiltinMeaning NullList =
makeBuiltinMeaning
nullPlc
(runCostingFunOneArgument . paramNullList)
where
nullPlc :: SomeConstantPoly uni [] '[a] -> EvaluationResult Bool
nullPlc (SomeConstantPoly val) = do
ValueOfDefaultUniList _ _ xs <- pure val
ValueOfDefaultUniList _ xs <- pure val
pure $ null xs

-- Data
32 changes: 15 additions & 17 deletions plutus-core/plutus-core/src/PlutusCore/Default/Universe.hs
Original file line number Diff line number Diff line change
@@ -111,31 +111,29 @@ data instance HiddenValueOf DefaultUni
| ValueOfDefaultUniString {-# UNPACK #-} !Text.Text
| ValueOfDefaultUniUnit
| ValueOfDefaultUniBool !Bool
-- TODO: doc on [HiddenValueOf DefaultUni]
| forall a. ValueOfDefaultUniList !(a -> HiddenValueOf DefaultUni) !(DefaultUni (Esc a)) ![a]
| forall a. ValueOfDefaultUniList !(DefaultUni (Esc a)) ![a] -- TODO: doc on [HiddenValueOf DefaultUni]
| forall a b. ValueOfDefaultUniPair !(DefaultUni (Esc a)) !(DefaultUni (Esc b)) !(a, b)
| ValueOfDefaultUniData !Data

instance HasHiddenValueOf DefaultUni where
hiddenValueOf DefaultUniInteger = ValueOfDefaultUniInteger
hiddenValueOf DefaultUniByteString = ValueOfDefaultUniByteString
hiddenValueOf DefaultUniString = ValueOfDefaultUniString
hiddenValueOf DefaultUniUnit = const ValueOfDefaultUniUnit
hiddenValueOf DefaultUniBool = ValueOfDefaultUniBool
hiddenValueOf (DefaultUniProtoList `DefaultUniApply` uniA) =
ValueOfDefaultUniList (hiddenValueOf uniA) uniA
hiddenValueOf (DefaultUniProtoPair `DefaultUniApply` uniA `DefaultUniApply` uniB) =
ValueOfDefaultUniPair uniA uniB
hiddenValueOf (f `DefaultUniApply` _ `DefaultUniApply` _ `DefaultUniApply` _) =
const $ noMoreTypeFunctions f
hiddenValueOf DefaultUniData = ValueOfDefaultUniData
hiddenValueOf DefaultUniInteger i = ValueOfDefaultUniInteger i
hiddenValueOf DefaultUniByteString bs = ValueOfDefaultUniByteString bs
hiddenValueOf DefaultUniString txt = ValueOfDefaultUniString txt
hiddenValueOf DefaultUniUnit () = ValueOfDefaultUniUnit
hiddenValueOf DefaultUniBool b = ValueOfDefaultUniBool b
hiddenValueOf (DefaultUniProtoList `DefaultUniApply` uniA) xs = ValueOfDefaultUniList uniA xs
hiddenValueOf (DefaultUniProtoPair `DefaultUniApply` uniA `DefaultUniApply` uniB) p =
ValueOfDefaultUniPair uniA uniB p
hiddenValueOf (f `DefaultUniApply` _ `DefaultUniApply` _ `DefaultUniApply` _) _ =
noMoreTypeFunctions f
hiddenValueOf DefaultUniData d = ValueOfDefaultUniData d

extractValueOf DefaultUniInteger (ValueOfDefaultUniInteger i) = Just i
extractValueOf DefaultUniByteString (ValueOfDefaultUniByteString bs) = Just bs
extractValueOf DefaultUniString (ValueOfDefaultUniString txt) = Just txt
extractValueOf DefaultUniUnit ValueOfDefaultUniUnit = Just ()
extractValueOf DefaultUniBool (ValueOfDefaultUniBool b) = Just b
extractValueOf (DefaultUniList uniA) (ValueOfDefaultUniList _ uniA' xs) = do
extractValueOf (DefaultUniList uniA) (ValueOfDefaultUniList uniA' xs) = do
Refl <- uniA `geq` uniA'
Just xs
extractValueOf (DefaultUniPair uniA uniB) (ValueOfDefaultUniPair uniA' uniB' p) = do
@@ -159,7 +157,7 @@ instance HasHiddenValueOf DefaultUni where
toSomeValueOf (ValueOfDefaultUniString txt) = someValue txt
toSomeValueOf ValueOfDefaultUniUnit = someValue ()
toSomeValueOf (ValueOfDefaultUniBool b) = someValue b
toSomeValueOf (ValueOfDefaultUniList _ uniA xs) = someValueOf (DefaultUniList uniA) xs
toSomeValueOf (ValueOfDefaultUniList uniA xs) = someValueOf (DefaultUniList uniA) xs
toSomeValueOf (ValueOfDefaultUniPair uniA uniB p) = someValueOf (DefaultUniPair uniA uniB) p
toSomeValueOf (ValueOfDefaultUniData d) = someValue d

@@ -361,7 +359,7 @@ instance ExMemoryUsage (HiddenValueOf DefaultUni) where
memoryUsage (ValueOfDefaultUniBool b) = memoryUsage b
-- Built-in functions like 'Cons' have linear complexity because of this line, since
-- 'memoryUsage' traverses the entire list to calculate how much memory it retains.
memoryUsage (ValueOfDefaultUniList _ uniA xs) =
memoryUsage (ValueOfDefaultUniList uniA xs) =
bring (Proxy @ExMemoryUsage) uniA $ memoryUsage xs
memoryUsage (ValueOfDefaultUniPair uniA uniB p) =
bring (Proxy @ExMemoryUsage) uniA $ bring (Proxy @ExMemoryUsage) uniB $ memoryUsage p