File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed
src/Transformations/ExtendedSyntax/Optimising Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -146,6 +146,8 @@ library
146146 Transformations.ExtendedSyntax.GenerateEval
147147 Transformations.ExtendedSyntax.MangleNames
148148 Transformations.ExtendedSyntax.StaticSingleAssignment
149+ Transformations.ExtendedSyntax.Optimising.EvaluatedCaseElimination
150+ Transformations.ExtendedSyntax.Optimising.TrivialCaseElimination
149151
150152 Transformations.BindNormalisation
151153 Transformations.CountVariableUse
Original file line number Diff line number Diff line change 1+ {-# LANGUAGE LambdaCase, TupleSections #-}
2+ module Transformations.ExtendedSyntax.Optimising.EvaluatedCaseElimination where
3+
4+ import Data.Functor.Foldable as Foldable
5+ import Grin.Grin
6+
7+ evaluatedCaseElimination :: Exp -> Exp
8+ evaluatedCaseElimination = ana builder where
9+ builder :: Exp -> ExpF Exp
10+ builder = \ case
11+ ECase val alts | all (altBodyEQ $ SReturn val) alts -> SReturnF val
12+ exp -> project exp
13+
14+ altBodyEQ :: Exp -> Alt -> Bool
15+ altBodyEQ exp (Alt _cpat body) = exp == body
Original file line number Diff line number Diff line change 1+ {-# LANGUAGE LambdaCase, TupleSections #-}
2+ module Transformations.ExtendedSyntax.Optimising.TrivialCaseElimination where
3+
4+ import Data.Functor.Foldable as Foldable
5+ import Grin.Grin
6+ import Transformations.Util
7+
8+ trivialCaseElimination :: Exp -> Exp
9+ trivialCaseElimination = ana builder where
10+ builder :: Exp -> ExpF Exp
11+ builder = \ case
12+ ECase val [Alt DefaultPat body] -> SBlockF body
13+ ECase val [Alt cpat body] -> SBlockF $ EBind (SReturn val) (cpatToLPat cpat) body
14+ exp -> project exp
You can’t perform that action at this time.
0 commit comments