Skip to content

Commit

Permalink
Run over subterms too.
Browse files Browse the repository at this point in the history
  • Loading branch information
thealmarty committed May 30, 2023
1 parent 16be0f5 commit 2ebca45
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions plutus-core/plutus-ir/src/PlutusIR/Transform/CommuteFnWithConst.hs
Expand Up @@ -2,11 +2,16 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}

module PlutusIR.Transform.CommuteFnWithConst (commuteFnWithConst) where
module PlutusIR.Transform.CommuteFnWithConst
(commuteFnWithConst
, commuteDefaultFun)
where

import Control.Lens (over)
import Data.Typeable (Typeable, eqT)
import PlutusCore.Default
import PlutusIR.Core (Term (Apply, Builtin, Constant))
import PlutusIR.Core.Plated (termSubterms)
import PlutusIR.Core.Type (Term (Apply, Builtin, Constant))

{- | Commute such that constants are the first arguments. Consider:
Expand Down Expand Up @@ -38,20 +43,22 @@ isConstant :: Term tyname name uni fun a -> Bool
isConstant Constant{} = True
isConstant _ = False

commuteFnWithConstDefault ::
commuteDefaultFun ::
forall tyname name uni a.
Term tyname name uni DefaultFun a ->
Term tyname name uni DefaultFun a
commuteFnWithConstDefault tm@(Apply ann (Apply ann1 (Builtin annB fun) x) y) =
case (isCommutativeWithConstant fun, isConstant x, isConstant y) of
(True, False, True) -> Apply ann (Apply ann1 (Builtin annB fun) y) x
_ -> tm
commuteFnWithConstDefault tm = tm
commuteDefaultFun = over termSubterms commuteDefaultFun . localCommute
where
localCommute tm@(Apply ann (Apply ann1 (Builtin annB fun) x) y) =
case (isCommutativeWithConstant fun, isConstant x, isConstant y) of
(True, False, True) -> Apply ann (Apply ann1 (Builtin annB fun) y) x
_ -> tm
localCommute tm = tm

commuteFnWithConst :: forall tyname name uni fun a. Typeable fun =>
Term tyname name uni fun a -> Term tyname name uni fun a
commuteFnWithConst = case eqT @fun @DefaultFun of
Just Refl -> commuteFnWithConstDefault
Just Refl -> commuteDefaultFun
Nothing -> id

-- | Returns whether a `DefaultFun` is commutative with `Constant`'s as arguments. Not using
Expand Down

0 comments on commit 2ebca45

Please sign in to comment.