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 'RuntimeScheme' #4778

Merged
merged 20 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
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
Fix some leftovers
  • Loading branch information
effectfully committed Aug 4, 2022
commit eb3467dc2a575066d3ed163babb8a1bae31ddb44
15 changes: 6 additions & 9 deletions plutus-core/plutus-core/examples/PlutusCore/Examples/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import PlutusCore
import PlutusCore.Builtin
import PlutusCore.Evaluation.Machine.ExBudget
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
import PlutusCore.Evaluation.Machine.ExMemory
import PlutusCore.Evaluation.Machine.Exception
import PlutusCore.Pretty

Expand Down Expand Up @@ -133,17 +132,15 @@ instance (ToBuiltinMeaning uni fun1, ToBuiltinMeaning uni fun2) =>
type CostingPart uni (Either fun1 fun2) = (CostingPart uni fun1, CostingPart uni fun2)

toBuiltinMeaning (Left fun) = case toBuiltinMeaning fun of
BuiltinMeaning tySch toF runtimeOpts ->
BuiltinMeaning tySch toF $ case runtimeOpts of
BuiltinRuntimeOptions immF defF -> BuiltinRuntimeOptions (immF . fst) (defF . fst)
BuiltinMeaning tySch toF (BuiltinRuntimeOptions immF defF) ->
BuiltinMeaning tySch toF (BuiltinRuntimeOptions (immF . fst) (defF . fst))
toBuiltinMeaning (Right fun) = case toBuiltinMeaning fun of
BuiltinMeaning tySch toF runtimeOpts ->
BuiltinMeaning tySch toF $ case runtimeOpts of
BuiltinRuntimeOptions immF defF -> BuiltinRuntimeOptions (immF . snd) (defF . snd)
BuiltinMeaning tySch toF (BuiltinRuntimeOptions immF defF) ->
BuiltinMeaning tySch toF (BuiltinRuntimeOptions (immF . snd) (defF . snd))

defBuiltinsRuntimeExt
:: (HasMeaningIn DefaultUni val, ExMemoryUsage val)
=> BuiltinsRuntime (Either DefaultFun ExtensionFun) val
:: HasMeaningIn DefaultUni term
=> BuiltinsRuntime (Either DefaultFun ExtensionFun) term
defBuiltinsRuntimeExt = toBuiltinsRuntime defaultUnliftingMode (defaultBuiltinCostModel, ())

data PlcListRep (a :: GHC.Type)
Expand Down
6 changes: 4 additions & 2 deletions plutus-core/plutus-core/src/PlutusCore/Builtin/Meaning.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type family GetArgs a where
GetArgs _ = '[]

{- Note [Merging the denotation and the costing function]

TODO
-}

-- | A class that allows us to derive a monotype for a builtin.
Expand All @@ -142,6 +142,8 @@ class KnownMonotype val args res where
knownMonotype :: TypeScheme val args res

-- | Convert the denotation of a builtin to its runtime counterpart with immediate unlifting.
-- We use a tuple rather than two arguments for symmetry with 'toPolyDeferredF'. It all gets
-- inlined anyway.
toMonoImmediateF
:: (FoldArgs args res, FoldArgs args ExBudget)
-> BuiltinRuntime val
Expand Down Expand Up @@ -216,7 +218,7 @@ instance
knownMonotype = TypeSchemeArrow knownMonotype

-- See Note [One-shotting runtime denotations].
-- Unlift, then recurse.
-- Unlift, then recurse.
toMonoImmediateF (f, exF) = BuiltinLamAbs . oneShot $
-- See Note [Strict application in runtime denotations].
fmap (\x -> toMonoImmediateF @val @args @res . (,) (f x) $! exF x) . readKnown
Expand Down
1 change: 0 additions & 1 deletion plutus-core/plutus-core/src/PlutusCore/Core/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

module PlutusCore.Core.Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Deriving.Aeson
import GHC.Exts
import Language.Haskell.TH.Syntax hiding (Name, newName)

-- TODO: document.
class OnMemoryUsages c a where
effectfully marked this conversation as resolved.
Show resolved Hide resolved
onMemoryUsages :: c -> a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ data MachineParameters machinecosts term (uni :: Type -> Type) (fun :: Type) =

-- See Note [Inlining meanings of builtins].
{-| This just uses 'toBuiltinsRuntime' function to convert a BuiltinCostModel to a BuiltinsRuntime. -}
mkMachineParameters
:: (ToBuiltinMeaning uni fun, HasMeaningIn uni (val uni fun))
mkMachineParameters ::
( -- In Cek.Internal we have `type instance UniOf (CekValue uni fun) = uni`, but we don't know that here.
CostingPart uni fun ~ builtincosts
, HasMeaningIn uni (val uni fun)
, ToBuiltinMeaning uni fun
)
=> UnliftingMode
-> CostModel machinecosts (CostingPart uni fun)
-> CostModel machinecosts builtincosts
-> MachineParameters machinecosts val uni fun
mkMachineParameters unlMode (CostModel mchnCosts builtinCosts) =
MachineParameters mchnCosts (inline toBuiltinsRuntime unlMode builtinCosts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ data Context uni fun
| NoFrame
deriving stock (Show)

-- TODO: document.
instance (Closed uni, uni `Everywhere` ExMemoryUsage) => ExMemoryUsage (CekValue uni fun) where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a bit weird about this, but I have no good reason for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's the same old function, except as a class method now.

I would kinda like to forbid costing of CekValues in the first place and just say that if a function accepts a term whose type is a type variable, the costing function is supposed to either ignore the ExMemory of that argument or to unlift the argument and only then look at the ExMemory of the unlifted value, but I have no idea how to express that and even if we could, I think it would be too much of a complication to worth this static guarantee.

Right now I'm just keeping the old behavior and optimizing the machinery.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason is that before it was more explicit whereas now it feels like we could accidentally use the typeclass method on something that's not a CekValue and not notice. But I don't think this is really a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it feels like we could accidentally use the typeclass method on something that's not a CekValue

But that's exactly what we do! We call the costing functions over the ExMemory of unlifted values now. It's just that we occasionally unlift into Opaque for polymorphic builtins, which is a wrapper around CekValue, hence why we need the instance. For Integer, Bool, SomeConstant etc we call memoryUsage directly without going through CekValue twice (once for unlifting, once for costing). It's the whole point of this PR!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I have no idea what the point of this PR is ;)

memoryUsage = \case
VCon c -> memoryUsage c
Expand Down