-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trace removal re-implemented as a IR rewrite rule (#5907)
* refactor: Internal module for RewriteRules, Monoid Instance * Trace removal re-implemented as a IR rewrite rule * Test case for an impure trace message, added note.
- Loading branch information
Showing
12 changed files
with
139 additions
and
75 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
44 changes: 44 additions & 0 deletions
44
plutus-core/plutus-ir/src/PlutusIR/Transform/RewriteRules/Internal.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,44 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module PlutusIR.Transform.RewriteRules.Internal | ||
( RewriteRules (..) | ||
, defaultUniRewriteRules | ||
) where | ||
|
||
import PlutusCore.Default (DefaultFun, DefaultUni) | ||
import PlutusCore.Name.Unique (Name) | ||
import PlutusCore.Quote (MonadQuote) | ||
import PlutusIR.Analysis.VarInfo (VarsInfo) | ||
import PlutusIR.Core.Type qualified as PIR | ||
import PlutusIR.Transform.RewriteRules.CommuteFnWithConst (commuteFnWithConst) | ||
import PlutusIR.Transform.RewriteRules.UnConstrConstrData (unConstrConstrData) | ||
import PlutusPrelude (Default (..), (>=>)) | ||
|
||
-- | A bundle of composed `RewriteRules`, to be passed at entrypoint of the compiler. | ||
newtype RewriteRules uni fun where | ||
RewriteRules | ||
:: { unRewriteRules | ||
:: forall tyname m a | ||
. (MonadQuote m, Monoid a) | ||
=> VarsInfo tyname Name uni a | ||
-> PIR.Term tyname Name uni fun a | ||
-> m (PIR.Term tyname Name uni fun a) | ||
} | ||
-> RewriteRules uni fun | ||
|
||
-- | The rules for the Default Universe/Builtin. | ||
defaultUniRewriteRules :: RewriteRules DefaultUni DefaultFun | ||
defaultUniRewriteRules = RewriteRules $ \varsInfo -> | ||
-- The rules are composed from left to right. | ||
pure . commuteFnWithConst >=> unConstrConstrData def varsInfo | ||
|
||
instance Default (RewriteRules DefaultUni DefaultFun) where | ||
def = defaultUniRewriteRules | ||
|
||
instance Semigroup (RewriteRules uni fun) where | ||
RewriteRules r1 <> RewriteRules r2 = RewriteRules (\varsInfo -> r1 varsInfo >=> r2 varsInfo) | ||
|
||
instance Monoid (RewriteRules uni fun) where | ||
mempty = RewriteRules (const pure) |
29 changes: 29 additions & 0 deletions
29
plutus-core/plutus-ir/src/PlutusIR/Transform/RewriteRules/RemoveTrace.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,29 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
|
||
module PlutusIR.Transform.RewriteRules.RemoveTrace | ||
( rewriteRuleRemoveTrace | ||
) where | ||
|
||
import PlutusCore.Default (DefaultFun) | ||
import PlutusCore.Default.Builtins qualified as Builtin | ||
import PlutusIR.Transform.RewriteRules.Common (pattern A, pattern B, pattern I) | ||
import PlutusIR.Transform.RewriteRules.Internal (RewriteRules (..)) | ||
|
||
{- Note [Impure trace messages] | ||
Removing of traces could change behavior of those programs that use impure trace messages | ||
e.g. `trace (error ()) foo`. | ||
While it is possible to force evaluation of a trace message when removing a trace call | ||
for the sake of a behavior preservation, this has a downside that pure messages remain | ||
in the program and are not elimitated as a "dead" code. | ||
This downside would defeat the purpose of removing traces, so we decided to not force. | ||
-} | ||
|
||
rewriteRuleRemoveTrace :: RewriteRules uni DefaultFun | ||
rewriteRuleRemoveTrace = RewriteRules \_varsInfo -> \case | ||
B Builtin.Trace `I` _argTy `A` _msg `A` arg -> pure arg | ||
term -> pure term |
32 changes: 0 additions & 32 deletions
32
plutus-core/plutus-ir/src/PlutusIR/Transform/RewriteRules/Rules.hs
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.