-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify
force . delay
in UPLC (#2949)
This introduces a very simple simplification pass in the UPLC pipeline. All this does is replace `force (delay t)` with `t`. This matters because every type declaration in PIR will turn into an immediately-instantiated type abstraction, which gets erased to a `force/delay` pair. So this saves us ~2 AST nodes for every type declaration, which seems like a minor but worthwhile difference.
- Loading branch information
Showing
17 changed files
with
427 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
plutus-core/untyped-plutus-core/src/UntypedPlutusCore/Transform/Simplify.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
-- | Very basic simplifications on UPLC. | ||
module UntypedPlutusCore.Transform.Simplify | ||
( simplifyTerm | ||
, simplifyProgram | ||
) where | ||
|
||
import UntypedPlutusCore.Core | ||
|
||
import Control.Lens (transformOf) | ||
|
||
simplifyProgram :: Program name uni fun a -> Program name uni fun a | ||
simplifyProgram (Program a v t) = Program a v $ simplifyTerm t | ||
|
||
simplifyTerm :: Term name uni fun a -> Term name uni fun a | ||
simplifyTerm = transformOf termSubterms processTerm | ||
|
||
processTerm :: Term name uni fun a -> Term name uni fun a | ||
processTerm = \case | ||
Force _ (Delay _ t) -> t | ||
t -> t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
plutus-core/untyped-plutus-core/test/Transform/Simplify.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
module Transform.Simplify where | ||
|
||
import qualified PlutusCore as PLC | ||
import PlutusCore.MkPlc | ||
import PlutusCore.Pretty | ||
import UntypedPlutusCore | ||
|
||
import qualified Data.ByteString.Lazy as BSL | ||
import Data.Coerce (coerce) | ||
import Data.Text.Encoding (encodeUtf8) | ||
import Test.Tasty | ||
import Test.Tasty.Golden | ||
|
||
basic :: Term Name PLC.DefaultUni PLC.DefaultFun () | ||
basic = Force () $ Delay () $ mkConstant @Integer () 1 | ||
|
||
nested :: Term Name PLC.DefaultUni PLC.DefaultFun () | ||
nested = Force () $ Force () $ Delay () $ Delay () $ mkConstant @Integer () 1 | ||
|
||
extraDelays :: Term Name PLC.DefaultUni PLC.DefaultFun () | ||
extraDelays = Force () $ Delay () $ Delay () $ mkConstant @Integer () 1 | ||
|
||
interveningLambda :: Term Name PLC.DefaultUni PLC.DefaultFun () | ||
interveningLambda = | ||
let lam = LamAbs () (Name "" (coerce (1::Int))) $ Delay () $ mkConstant @Integer () 1 | ||
arg = mkConstant @Integer () 1 | ||
in Force () $ Apply () lam arg | ||
|
||
-- TODO Fix duplication with other golden tests, quite annoying | ||
goldenVsPretty :: PrettyPlc a => String -> String -> a -> TestTree | ||
goldenVsPretty extn name value = | ||
goldenVsString name ("untyped-plutus-core/test/Transform/" ++ name ++ extn) $ | ||
pure . BSL.fromStrict . encodeUtf8 . render $ prettyPlcClassicDebug value | ||
|
||
goldenVsSimplified :: String -> Term Name PLC.DefaultUni PLC.DefaultFun () -> TestTree | ||
goldenVsSimplified name | ||
= goldenVsPretty ".plc.golden" name | ||
. simplifyTerm | ||
|
||
test_simplify :: TestTree | ||
test_simplify = | ||
testGroup "simplify" | ||
[ goldenVsSimplified "basic" basic | ||
, goldenVsSimplified "nested" nested | ||
, goldenVsSimplified "extraDelays" extraDelays | ||
, goldenVsSimplified "interveningLambda" interveningLambda | ||
] |
1 change: 1 addition & 0 deletions
1
plutus-core/untyped-plutus-core/test/Transform/basic.plc.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(con integer 1) |
1 change: 1 addition & 0 deletions
1
plutus-core/untyped-plutus-core/test/Transform/extraDelays.plc.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(delay (con integer 1)) |
1 change: 1 addition & 0 deletions
1
plutus-core/untyped-plutus-core/test/Transform/interveningLambda.plc.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(force [ (lam _1 (delay (con integer 1))) (con integer 1) ]) |
1 change: 1 addition & 0 deletions
1
plutus-core/untyped-plutus-core/test/Transform/nested.plc.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(con integer 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.