-
-
Notifications
You must be signed in to change notification settings - Fork 397
Simplify extracts after running tactics #1351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9038bae
More tests of overlapping methods
isovector b9b3aef
Do a simplification pass of the extract
isovector dbf7b46
Do less work when simplifiying
isovector 2ef7975
Remove unnecessary parens simplification
isovector a08773c
Implement simplify as a fold over endos
isovector 25171bc
Fix tests
isovector b47aced
Haddock for the new module
isovector 8ba082b
Minor note on implementation
isovector 1302c6d
Note a TODO
isovector 4a1650f
Use PatCompat to unpack patterns
isovector 7ba73ac
Pull out codegen utilities to break a cyclic dependency
isovector 0b2cc53
Re-export utils
isovector 1802bd6
Try a different strategy for generalizing PatCompat
isovector d8d5248
Try, try again to compat
isovector e4cb82e
Could this be the answer we've all been waiting for?
isovector 11f58b1
Merge branch 'master' into simplify
isovector File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
67 changes: 67 additions & 0 deletions
67
plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/CodeGen/Utils.hs
This file contains hidden or 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,67 @@ | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Ide.Plugin.Tactic.CodeGen.Utils where | ||
|
||
import Data.List | ||
import DataCon | ||
import Development.IDE.GHC.Compat | ||
import GHC.Exts | ||
import GHC.SourceGen (RdrNameStr) | ||
import GHC.SourceGen.Overloaded | ||
import Name | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Make a data constructor with the given arguments. | ||
mkCon :: DataCon -> [LHsExpr GhcPs] -> LHsExpr GhcPs | ||
mkCon dcon (fmap unLoc -> args) | ||
| isTupleDataCon dcon = | ||
noLoc $ tuple args | ||
| dataConIsInfix dcon | ||
, (lhs : rhs : args') <- args = | ||
noLoc $ foldl' (@@) (op lhs (coerceName dcon_name) rhs) args' | ||
| otherwise = | ||
noLoc $ foldl' (@@) (bvar' $ occName dcon_name) args | ||
where | ||
dcon_name = dataConName dcon | ||
|
||
|
||
coerceName :: HasOccName a => a -> RdrNameStr | ||
coerceName = fromString . occNameString . occName | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Like 'var', but works over standard GHC 'OccName's. | ||
var' :: Var a => OccName -> a | ||
var' = var . fromString . occNameString | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Like 'bvar', but works over standard GHC 'OccName's. | ||
bvar' :: BVar a => OccName -> a | ||
bvar' = bvar . fromString . occNameString | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Get an HsExpr corresponding to a function name. | ||
mkFunc :: String -> HsExpr GhcPs | ||
mkFunc = var' . mkVarOcc | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Get an HsExpr corresponding to a value name. | ||
mkVal :: String -> HsExpr GhcPs | ||
mkVal = var' . mkVarOcc | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Like 'op', but easier to call. | ||
infixCall :: String -> HsExpr GhcPs -> HsExpr GhcPs -> HsExpr GhcPs | ||
infixCall s = flip op (fromString s) | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Like '(@@)', but uses a dollar instead of parentheses. | ||
appDollar :: HsExpr GhcPs -> HsExpr GhcPs -> HsExpr GhcPs | ||
appDollar = infixCall "$" | ||
|
This file contains hidden or 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 hidden or 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
122 changes: 122 additions & 0 deletions
122
plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/Simplify.hs
This file contains hidden or 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,122 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Ide.Plugin.Tactic.Simplify | ||
( simplify | ||
) where | ||
|
||
import Data.Data (Data) | ||
import Data.Generics (everywhere, somewhere, something, listify, extT, mkT, GenericT, mkQ) | ||
import Data.List.Extra (unsnoc) | ||
import Data.Maybe (isJust) | ||
import Data.Monoid (Endo (..)) | ||
import Development.IDE.GHC.Compat | ||
import GHC.Exts (fromString) | ||
import GHC.SourceGen (var, op) | ||
import GHC.SourceGen.Expr (lambda) | ||
import Ide.Plugin.Tactic.CodeGen.Utils | ||
import Ide.Plugin.Tactic.GHC (fromPatCompatPs) | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | A pattern over the otherwise (extremely) messy AST for lambdas. | ||
pattern Lambda :: [Pat GhcPs] -> HsExpr GhcPs -> HsExpr GhcPs | ||
pattern Lambda pats body <- | ||
HsLam _ | ||
(MG {mg_alts = L _ [L _ | ||
(Match { m_pats = fmap fromPatCompatPs -> pats | ||
, m_grhss = GRHSs {grhssGRHSs = [L _ ( | ||
GRHS _ [] (L _ body))]} | ||
})]}) | ||
where | ||
-- If there are no patterns to bind, just stick in the body | ||
Lambda [] body = body | ||
Lambda pats body = lambda pats body | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Simlify an expression. | ||
simplify :: LHsExpr GhcPs -> LHsExpr GhcPs | ||
simplify | ||
= head | ||
. drop 3 -- Do three passes; this should be good enough for the limited | ||
-- amount of gas we give to auto | ||
. iterate (everywhere $ foldEndo | ||
[ simplifyEtaReduce | ||
, simplifyRemoveParens | ||
, simplifyCompose | ||
]) | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Like 'foldMap' but for endomorphisms. | ||
foldEndo :: Foldable t => t (a -> a) -> a -> a | ||
foldEndo = appEndo . foldMap Endo | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Does this thing contain any references to 'HsVar's with the given | ||
-- 'RdrName'? | ||
containsHsVar :: Data a => RdrName -> a -> Bool | ||
containsHsVar name x = not $ null $ listify ( | ||
\case | ||
((HsVar _ (L _ a)) :: HsExpr GhcPs) | a == name -> True | ||
_ -> False | ||
) x | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Perform an eta reduction. For example, transforms @\x -> (f g) x@ into | ||
-- @f g@. | ||
simplifyEtaReduce :: GenericT | ||
simplifyEtaReduce = mkT $ \case | ||
Lambda | ||
[VarPat _ (L _ pat)] | ||
(HsVar _ (L _ a)) | pat == a -> | ||
var "id" | ||
Lambda | ||
(unsnoc -> Just (pats, (VarPat _ (L _ pat)))) | ||
(HsApp _ (L _ f) (L _ (HsVar _ (L _ a)))) | ||
| pat == a | ||
-- We can only perform this simplifiation if @pat@ is otherwise unused. | ||
, not (containsHsVar pat f) -> | ||
Lambda pats f | ||
x -> x | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Perform an eta-reducing function composition. For example, transforms | ||
-- @\x -> f (g (h x))@ into @f . g . h@. | ||
simplifyCompose :: GenericT | ||
simplifyCompose = mkT $ \case | ||
Lambda | ||
(unsnoc -> Just (pats, (VarPat _ (L _ pat)))) | ||
(unroll -> (fs@(_:_), (HsVar _ (L _ a)))) | ||
| pat == a | ||
-- We can only perform this simplifiation if @pat@ is otherwise unused. | ||
, not (containsHsVar pat fs) -> | ||
Lambda pats (foldr1 (infixCall ".") fs) | ||
x -> x | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Removes unnecessary parentheses on any token that doesn't need them. | ||
simplifyRemoveParens :: GenericT | ||
simplifyRemoveParens = mkT $ \case | ||
HsPar _ (L _ x) | isAtomicHsExpr x -> x | ||
(x :: HsExpr GhcPs) -> x | ||
|
||
|
||
------------------------------------------------------------------------------ | ||
-- | Unrolls a right-associative function application of the form | ||
-- @HsApp f (HsApp g (HsApp h x))@ into @([f, g, h], x)@. | ||
unroll :: HsExpr GhcPs -> ([HsExpr GhcPs], HsExpr GhcPs) | ||
unroll (HsPar _ (L _ x)) = unroll x | ||
unroll (HsApp _ (L _ f) (L _ a)) = | ||
let (fs, r) = unroll a | ||
in (f : fs, r) | ||
unroll x = ([], x) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
fgmap :: (Functor f, Functor g) => (a -> b) -> (f (g a) -> f (g b)) | ||
fgmap = _ |
This file contains hidden or 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,2 @@ | ||
fgmap :: (Functor f, Functor g) => (a -> b) -> (f (g a) -> f (g b)) | ||
fgmap = (fmap . fmap) |
This file contains hidden or 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,2 @@ | ||
fJoin :: (Monad m, Monad f) => f (m (m a)) -> f (m a) | ||
fJoin = fmap _ |
This file contains hidden or 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,2 @@ | ||
fJoin :: (Monad m, Monad f) => f (m (m a)) -> f (m a) | ||
fJoin = fmap (\ mma -> (>>=) mma id) |
This file contains hidden or 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,4 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
fJoin :: forall f m a. (Monad m, Monad f) => f (m (m a)) -> f (m a) | ||
fJoin = let f = (_ :: m (m a) -> m a) in fmap f |
This file contains hidden or 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,4 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
fJoin :: forall f m a. (Monad m, Monad f) => f (m (m a)) -> f (m a) | ||
fJoin = let f = ( (\ mma -> (>>=) mma id) :: m (m a) -> m a) in fmap f |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.