Skip to content

Commit 776f729

Browse files
committed
Simplify away single-use let bindings
1 parent b4259b8 commit 776f729

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

plugins/hls-tactics-plugin/src/Wingman/GHC.hs

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
module Wingman.GHC where
55

6+
import Bag (bagToList)
67
import ConLike
78
import Control.Applicative (empty)
89
import Control.Monad.State
@@ -196,6 +197,15 @@ pattern AMatch ctx pats body <-
196197
}
197198

198199

200+
pattern SingleLet :: IdP GhcPs -> [Pat GhcPs] -> HsExpr GhcPs -> HsExpr GhcPs -> HsExpr GhcPs
201+
pattern SingleLet bind pats val expr <-
202+
HsLet _
203+
(L _ (HsValBinds _
204+
(ValBinds _ (bagToList ->
205+
[(L _ (FunBind _ (L _ bind) (MG _ (L _ [L _ (AMatch _ pats val)]) _) _ _))]) _)))
206+
(L _ expr)
207+
208+
199209
------------------------------------------------------------------------------
200210
-- | A pattern over the otherwise (extremely) messy AST for lambdas.
201211
pattern Lambda :: [Pat GhcPs] -> HsExpr GhcPs -> HsExpr GhcPs

plugins/hls-tactics-plugin/src/Wingman/Simplify.hs

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Development.IDE.GHC.Compat
1111
import GHC.SourceGen (var)
1212
import GHC.SourceGen.Expr (lambda)
1313
import Wingman.CodeGen.Utils
14-
import Wingman.GHC (containsHsVar, fromPatCompat)
14+
import Wingman.GHC (containsHsVar, fromPatCompat, pattern SingleLet)
1515

1616

1717
------------------------------------------------------------------------------
@@ -30,6 +30,7 @@ pattern Lambda pats body <-
3030
Lambda pats body = lambda pats body
3131

3232

33+
3334
------------------------------------------------------------------------------
3435
-- | Simlify an expression.
3536
simplify :: LHsExpr GhcPs -> LHsExpr GhcPs
@@ -41,6 +42,7 @@ simplify
4142
[ simplifyEtaReduce
4243
, simplifyRemoveParens
4344
, simplifyCompose
45+
, simplifySingleLet
4446
])
4547

4648

@@ -68,6 +70,14 @@ simplifyEtaReduce = mkT $ \case
6870
Lambda pats f
6971
x -> x
7072

73+
------------------------------------------------------------------------------
74+
-- | Perform an eta reduction. For example, transforms @\x -> (f g) x@ into
75+
-- @f g@.
76+
simplifySingleLet :: GenericT
77+
simplifySingleLet = mkT $ \case
78+
SingleLet bind [] val (HsVar _ (L _ a)) | a == bind -> val
79+
x -> x
80+
7181

7282
------------------------------------------------------------------------------
7383
-- | Perform an eta-reducing function composition. For example, transforms

plugins/hls-tactics-plugin/test/golden/MetaCataCollapseUnary.expected.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ class Yo f where
44
yo :: f x -> Int
55

66
instance (Yo f) => Yo (M1 _1 _2 f) where
7-
yo (M1 fx) = let fx_c = yo fx in fx_c
7+
yo (M1 fx) = yo fx
88

0 commit comments

Comments
 (0)