Skip to content

Commit 12ee913

Browse files
committed
make 'isFirstVarBeforeEffects' a pure function to simplify testing.
1 parent ea51c68 commit 12ee913

File tree

3 files changed

+15
-49
lines changed

3 files changed

+15
-49
lines changed

plutus-core/plutus-core.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ test-suite untyped-plutus-core-test
458458
, hedgehog
459459
, lens
460460
, mtl
461-
, multiset
462461
, plutus-core ^>=1.44
463462
, plutus-core:plutus-core-testlib
464463
, pretty-show

plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Transform/Inline.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import Control.Monad.Reader (ReaderT (runReaderT))
4040
import Control.Monad.State (StateT, evalStateT, gets, modify')
4141
import PlutusCore qualified as PLC
4242
import PlutusCore.Annotation (Inline (AlwaysInline, SafeToInline), InlineHints (..))
43+
import PlutusCore.Builtin (ToBuiltinMeaning (BuiltinSemanticsVariant))
4344
import PlutusCore.Builtin qualified as PLC
4445
import PlutusCore.MkPlc (mkIterApp)
4546
import PlutusCore.Name.Unique (HasUnique, TermUnique (..), Unique (..))
@@ -399,16 +400,16 @@ nameUsedAtMostOnce n = do
399400
isFirstVarBeforeEffects
400401
:: forall name uni fun ann
401402
. (InliningConstraints name uni fun)
402-
=> name
403+
=> BuiltinSemanticsVariant fun
404+
-> name
403405
-> Term name uni fun ann
404-
-> InlineM name uni fun ann Bool
405-
isFirstVarBeforeEffects n t = do
406-
builtinSemanticsVariant <- view iiBuiltinSemanticsVariant
406+
-> Bool
407+
isFirstVarBeforeEffects builtinSemanticsVariant n t =
407408
-- This can in the worst case traverse a lot of the term, which could lead to
408409
-- us doing ~quadratic work as we process the program. However in practice
409410
-- most terms have a relatively short evaluation order before we hit Unknown,
410411
-- so it's not too bad.
411-
pure $ go (unEvalOrder (termEvaluationOrder builtinSemanticsVariant t))
412+
go (unEvalOrder (termEvaluationOrder builtinSemanticsVariant t))
412413
where
413414
-- Found the variable we're looking for!
414415
go ((EvalTerm _ _ (Var _ n')) : _) | n == n' = True
@@ -471,7 +472,8 @@ effectSafe
471472
-- ^ is it pure?
472473
-> InlineM name uni fun a Bool
473474
effectSafe body n purity = do
474-
immediatelyEvaluated <- isFirstVarBeforeEffects n body
475+
builtinSemantics <- view iiBuiltinSemanticsVariant
476+
let immediatelyEvaluated = isFirstVarBeforeEffects builtinSemantics n body
475477
pure $ purity || immediatelyEvaluated
476478

477479
{-| Should we inline? Should only inline things that won't duplicate work

plutus-core/untyped-plutus-core/test/Transform/Inline/Spec.hs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
{-# LANGUAGE BlockArguments #-}
2-
{-# LANGUAGE NumericUnderscores #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE ScopedTypeVariables #-}
54

65
module Transform.Inline.Spec where
76

8-
import Control.Monad.Reader (runReaderT)
9-
import Control.Monad.State (runStateT)
107
import Data.Maybe (fromMaybe, isNothing)
11-
import Data.MultiSet qualified as MultiSet
12-
import PlutusCore.Annotation (Inline (MayInline))
13-
import PlutusCore.Quote (runQuote)
14-
import PlutusCore.Size (Size (..))
158
import PlutusPrelude (def)
169
import Test.Tasty (TestTree, testGroup)
1710
import Test.Tasty.HUnit (Assertion, assertBool, testCase)
1811
import UntypedPlutusCore (DefaultFun, DefaultUni, Name (..), Term (..))
1912
import UntypedPlutusCore.Test.Term.Construction (addInteger, app, case_, delay, lam, uniqueNames3,
2013
uniqueNames4, var)
21-
import UntypedPlutusCore.Transform.Inline (InlineHints (InlineHints), InlineInfo (..), InlineM,
22-
S (..), Subst (Subst), TermEnv (TermEnv),
23-
isFirstVarBeforeEffects, isVarDelayed)
24-
25-
--------------------------------------------------------------------------------
26-
-- Tests -----------------------------------------------------------------------
14+
import UntypedPlutusCore.Transform.Inline (isFirstVarBeforeEffects, isVarDelayed)
2715

2816
test_inline :: TestTree
2917
test_inline =
3018
testGroup
3119
"Inline"
32-
[ testCase "var is before or after effects" testVarBeforeAfterEffects
20+
[ testCase
21+
"var is before or after effects"
22+
testVarBeforeAfterEffects
3323
, testGroup
3424
"isVarDelayed"
3525
[ testCase
@@ -47,11 +37,11 @@ test_inline =
4737
testVarBeforeAfterEffects :: Assertion
4838
testVarBeforeAfterEffects = do
4939
assertBool "a is evaluated before effects" do
50-
testFirstVarBeforeEffects a term
40+
isFirstVarBeforeEffects def a term
5141
assertBool "b is evaluated before effects" do
52-
testFirstVarBeforeEffects b term
42+
isFirstVarBeforeEffects def b term
5343
assertBool "c is not evaluated after effects" $ not do
54-
testFirstVarBeforeEffects c term
44+
isFirstVarBeforeEffects def c term
5545
where
5646
term :: Term Name DefaultUni DefaultFun ()
5747
term =
@@ -107,28 +97,3 @@ testVarIsDelayedInCaseBranch = do
10797
term = case_ (var b) [var a, var c]
10898

10999
(a, b, c, d) = uniqueNames4 "a" "b" "c" "d"
110-
111-
--------------------------------------------------------------------------------
112-
-- Helper functions: -----------------------------------------------------------
113-
114-
testFirstVarBeforeEffects :: Name -> Term Name DefaultUni DefaultFun () -> Bool
115-
testFirstVarBeforeEffects name = runInlineM . isFirstVarBeforeEffects name
116-
117-
runInlineM :: InlineM Name DefaultUni DefaultFun () r -> r
118-
runInlineM m = result
119-
where
120-
(result, _finalState) =
121-
runQuote (runStateT (runReaderT m inlineInfo) initialState)
122-
123-
inlineInfo :: InlineInfo Name DefaultFun ()
124-
inlineInfo =
125-
InlineInfo
126-
{ _iiUsages = MultiSet.empty
127-
, _iiHints = InlineHints \_ann _name -> MayInline
128-
, _iiBuiltinSemanticsVariant = def
129-
, _iiInlineConstants = True
130-
, _iiInlineCallsiteGrowth = Size 1_000_000
131-
}
132-
133-
initialState :: S Name DefaultUni DefaultFun ()
134-
initialState = S{_subst = Subst (TermEnv mempty), _vars = mempty}

0 commit comments

Comments
 (0)