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

Add evalTermNoEmit and internal functions used by this function #671

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Revision history for plutarch

# 1.5.1 - 25-05-2024

## Added

* `evalTermNoEmit` and internal function used by this function. This function have identical functionality as `evalTerm`
but, internally, runs CEK machine with budget and log tracing disabled.

# 1.5.0 - 26-01-2024

## Changed
Expand Down
16 changes: 15 additions & 1 deletion Plutarch/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module Plutarch.Evaluate (
E.evalScript',
E.EvalError,
evalTerm,
evalTermNoEmit,
) where

import Plutarch.Internal.Evaluate qualified as E

import Data.Text (Text)
import Plutarch.Internal (ClosedTerm, Config, RawTerm (RCompiled), Term (..), TermResult (TermResult), compile)
import Plutarch.Internal (ClosedTerm, Config (Config), RawTerm (RCompiled), Term (..), TermResult (TermResult), TracingMode (NoTracing), compile)
import Plutarch.Script (Script (Script))
import PlutusCore.Evaluation.Machine.ExBudget (ExBudget)
import UntypedPlutusCore qualified as UPLC
Expand All @@ -31,3 +32,16 @@ evalTerm config term =
fromScript :: Script -> ClosedTerm a
fromScript (Script script) =
Term $ const $ pure $ TermResult (RCompiled $ UPLC._progTerm script) []

evalTermNoEmit ::
ClosedTerm a ->
Either Text (Either E.NoEmitEvaluateError (ClosedTerm a))
evalTermNoEmit term =
case compile (Config NoTracing) term of
Right script ->
Right $ fromScript <$> E.evalScriptNoEmit script
Left a -> Left a
where
fromScript :: Script -> ClosedTerm a
fromScript (Script script) =
Term $ const $ pure $ TermResult (RCompiled $ UPLC._progTerm script) []
37 changes: 36 additions & 1 deletion Plutarch/Internal/Evaluate.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

module Plutarch.Internal.Evaluate (uplcVersion, evalScript, evalScriptHuge, evalScript', EvalError) where
module Plutarch.Internal.Evaluate (
uplcVersion,
evalScript,
evalScriptHuge,
evalScript',
evalTermNoEmit,
evalScriptNoEmit,
EvalError,
NoEmitEvaluateError (..),
) where

import Data.Text (Text)
import Plutarch.Script (Script (Script))
Expand All @@ -12,12 +21,14 @@ import PlutusCore.Evaluation.Machine.ExBudget (
)
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults (defaultCekParameters)
import PlutusCore.Evaluation.Machine.ExMemory (ExCPU (ExCPU), ExMemory (ExMemory))
import PlutusCore.Quote qualified as Quote
import UntypedPlutusCore (
Program (Program),
Term,
Version (Version),
)
import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.DeBruijn qualified as DeBruijn
import UntypedPlutusCore.Evaluation.Machine.Cek qualified as Cek

type EvalError = (Cek.CekEvaluationException PLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun)
Expand Down Expand Up @@ -56,3 +67,27 @@ evalTerm ::
evalTerm budget t =
case Cek.runCekDeBruijn defaultCekParameters (Cek.restricting (ExRestrictingBudget budget)) Cek.logEmitter t of
(errOrRes, Cek.RestrictingSt (ExRestrictingBudget final), logs) -> (errOrRes, budget `minusExBudget` final, logs)

data NoEmitEvaluateError
= NoEmitEvaluateFreeVariableError DeBruijn.FreeVariableError
| NoEmitEvaluateFailure

-- Evaluates given script using `unsafeEvaluateCekNoEmit`. This will run with unbound budget and without log output
evalTermNoEmit ::
Term PLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun () ->
Either NoEmitEvaluateError (Term PLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun ())
evalTermNoEmit t =
case Quote.runQuoteT $ DeBruijn.unDeBruijnTerm t of
Right namedUPLC ->
case Cek.unsafeEvaluateCekNoEmit defaultCekParameters namedUPLC of
Cek.EvaluationSuccess evaluated ->
case DeBruijn.deBruijnTerm evaluated of
Right evaluatedTerm -> Right evaluatedTerm
Left err -> Left $ NoEmitEvaluateFreeVariableError err
Cek.EvaluationFailure -> Left NoEmitEvaluateFailure
Left err -> Left $ NoEmitEvaluateFreeVariableError err

evalScriptNoEmit :: Script -> Either NoEmitEvaluateError Script
evalScriptNoEmit (Script (Program _ _ script)) =
Script . Program () uplcVersion . UPLC.termMapNames UPLC.unNameDeBruijn
<$> evalTermNoEmit (UPLC.termMapNames UPLC.fakeNameDeBruijn script)
2 changes: 1 addition & 1 deletion plutarch.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: plutarch
version: 1.5.0
version: 1.5.1
author:
Las Safin <[email protected]>, Koz Ross <[email protected]>, Seungheon Oh <[email protected]>

Expand Down