{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}
module GHC.Util.HsExpr (
dotApps, lambda
, simplifyExp, niceLambda, niceLambdaR
, Brackets(..)
, rebracket1, appsBracket, transformAppsM, fromApps, apps, universeApps, universeParentExp
, paren
, replaceBranches
, needBracketOld, transformBracketOld, fromParen1
, allowLeftSection, allowRightSection
) where
import GHC.Hs
import GHC.Types.Basic
import GHC.Types.SrcLoc
import GHC.Data.FastString
import GHC.Types.Name.Reader
import GHC.Types.Name.Occurrence
import GHC.Data.Bag(bagToList)
import GHC.Util.Brackets
import GHC.Util.FreeVars
import GHC.Util.View
import Control.Applicative
import Control.Monad.Trans.Class
import Control.Monad.Trans.State
import Control.Monad.Trans.Writer.CPS
import Data.Data
import Data.Generics.Uniplate.DataOnly
import Data.List.Extra
import Data.Tuple.Extra
import Data.Maybe
import Refact (substVars, toSSA)
import Refact.Types hiding (SrcSpan, Match)
import qualified Refact.Types as R (SrcSpan)
import Language.Haskell.GhclibParserEx.GHC.Hs.Pat
import Language.Haskell.GhclibParserEx.GHC.Hs.Expr
import Language.Haskell.GhclibParserEx.GHC.Hs.ExtendInstances
import Language.Haskell.GhclibParserEx.GHC.Utils.Outputable
import Language.Haskell.GhclibParserEx.GHC.Types.Name.Reader
dotApp :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
dotApp :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
dotApp LHsExpr GhcPs
x LHsExpr GhcPs
y = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XOpApp GhcPs
-> LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp EpAnn [AddEpAnn]
XOpApp GhcPs
forall ann. EpAnn ann
EpAnnNotUsed LHsExpr GhcPs
x (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XVar GhcPs -> LIdP GhcPs -> HsExpr GhcPs
forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall a an. a -> LocatedAn an a
noLocA (RdrName -> GenLocated SrcSpanAnnN RdrName)
-> RdrName -> GenLocated SrcSpanAnnN RdrName
forall a b. (a -> b) -> a -> b
$ FastString -> RdrName
mkVarUnqual (String -> FastString
fsLit String
"."))) LHsExpr GhcPs
y
dotApps :: [LHsExpr GhcPs] -> LHsExpr GhcPs
dotApps :: [LHsExpr GhcPs] -> LHsExpr GhcPs
dotApps [] = String -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. HasCallStack => String -> a
error String
"GHC.Util.HsExpr.dotApps', does not work on an empty list"
dotApps [LHsExpr GhcPs
x] = LHsExpr GhcPs
x
dotApps (LHsExpr GhcPs
x : [LHsExpr GhcPs]
xs) = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
dotApp LHsExpr GhcPs
x ([LHsExpr GhcPs] -> LHsExpr GhcPs
dotApps [LHsExpr GhcPs]
xs)
lambda :: [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs
lambda :: [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs
lambda [LPat GhcPs]
vs LHsExpr GhcPs
body = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XLam GhcPs -> MatchGroup GhcPs (LHsExpr GhcPs) -> HsExpr GhcPs
forall p. XLam p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLam NoExtField
XLam GhcPs
noExtField (XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> XRec
GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> Origin
-> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XMG p body -> XRec p [LMatch p body] -> Origin -> MatchGroup p body
MG NoExtField
XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
noExtField ([LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> LocatedAn
AnnList
[LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
forall a an. a -> LocatedAn an a
noLocA [Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a an. a -> LocatedAn an a
noLocA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b. (a -> b) -> a -> b
$ XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsMatchContext GhcPs
-> [LPat GhcPs]
-> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCMatch p body
-> HsMatchContext p -> [LPat p] -> GRHSs p body -> Match p body
Match EpAnn [AddEpAnn]
XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed HsMatchContext GhcPs
forall p. HsMatchContext p
LambdaExpr [LPat GhcPs]
vs (XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> HsLocalBinds GhcPs
-> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
EpAnnComments
emptyComments [GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a an. a -> LocatedAn an a
noLocA (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b. (a -> b) -> a -> b
$ XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [GuardLStmt GhcPs]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS EpAnn GrhsAnn
XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
body] (XEmptyLocalBinds GhcPs GhcPs -> HsLocalBinds GhcPs
forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
EmptyLocalBinds NoExtField
XEmptyLocalBinds GhcPs GhcPs
noExtField))]) Origin
Generated)
paren :: LHsExpr GhcPs -> LHsExpr GhcPs
paren :: LHsExpr GhcPs -> LHsExpr GhcPs
paren LHsExpr GhcPs
x
| GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Bool
forall a. Brackets a => a -> Bool
isAtom GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x = LHsExpr GhcPs
x
| Bool
otherwise = GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
universeParentExp :: Data a => a -> [(Maybe (Int, LHsExpr GhcPs), LHsExpr GhcPs)]
universeParentExp :: forall a.
Data a =>
a -> [(Maybe (Int, LHsExpr GhcPs), LHsExpr GhcPs)]
universeParentExp a
xs = [[(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]]
-> [(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall a. Maybe a
Nothing, GenLocated SrcSpanAnnA (HsExpr GhcPs)
x) (Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> [(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]
forall a. a -> [a] -> [a]
: GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [(Maybe (Int, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]
forall {a} {t}. (Enum a, Num a, Data t) => t -> [(Maybe (a, t), t)]
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
x | GenLocated SrcSpanAnnA (HsExpr GhcPs)
x <- a -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall from to. Biplate from to => from -> [to]
childrenBi a
xs]
where f :: t -> [(Maybe (a, t), t)]
f t
p = [[(Maybe (a, t), t)]] -> [(Maybe (a, t), t)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [((a, t) -> Maybe (a, t)
forall a. a -> Maybe a
Just (a
i,t
p), t
c) (Maybe (a, t), t) -> [(Maybe (a, t), t)] -> [(Maybe (a, t), t)]
forall a. a -> [a] -> [a]
: t -> [(Maybe (a, t), t)]
f t
c | (a
i,t
c) <- a -> [t] -> [(a, t)]
forall a b. Enum a => a -> [b] -> [(a, b)]
zipFrom a
0 ([t] -> [(a, t)]) -> [t] -> [(a, t)]
forall a b. (a -> b) -> a -> b
$ t -> [t]
forall on. Uniplate on => on -> [on]
children t
p]
apps :: [LHsExpr GhcPs] -> LHsExpr GhcPs
apps :: [LHsExpr GhcPs] -> LHsExpr GhcPs
apps = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. HasCallStack => (a -> a -> a) -> [a] -> a
foldl1' GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
-> LHsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall {p} {ann} {an}.
(XApp p ~ EpAnn ann) =>
XRec p (HsExpr p) -> XRec p (HsExpr p) -> LocatedAn an (HsExpr p)
mkApp where mkApp :: XRec p (HsExpr p) -> XRec p (HsExpr p) -> LocatedAn an (HsExpr p)
mkApp XRec p (HsExpr p)
x XRec p (HsExpr p)
y = HsExpr p -> LocatedAn an (HsExpr p)
forall a an. a -> LocatedAn an a
noLocA (XApp p -> XRec p (HsExpr p) -> XRec p (HsExpr p) -> HsExpr p
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnn ann
XApp p
forall ann. EpAnn ann
EpAnnNotUsed XRec p (HsExpr p)
x XRec p (HsExpr p)
y)
fromApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
fromApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
fromApps (L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ LHsExpr GhcPs
x LHsExpr GhcPs
y)) = LHsExpr GhcPs -> [LHsExpr GhcPs]
fromApps LHsExpr GhcPs
x [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. [a] -> [a] -> [a]
++ [GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y]
fromApps LHsExpr GhcPs
x = [LHsExpr GhcPs
x]
childrenApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
childrenApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
childrenApps (L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ LHsExpr GhcPs
x LHsExpr GhcPs
y)) = LHsExpr GhcPs -> [LHsExpr GhcPs]
childrenApps LHsExpr GhcPs
x [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. [a] -> [a] -> [a]
++ [GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y]
childrenApps LHsExpr GhcPs
x = GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall on. Uniplate on => on -> [on]
children GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
universeApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
universeApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
universeApps LHsExpr GhcPs
x = GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. a -> [a] -> [a]
: (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
LHsExpr GhcPs -> [LHsExpr GhcPs]
universeApps (LHsExpr GhcPs -> [LHsExpr GhcPs]
childrenApps LHsExpr GhcPs
x)
descendAppsM :: Monad m => (LHsExpr GhcPs -> m (LHsExpr GhcPs)) -> LHsExpr GhcPs -> m (LHsExpr GhcPs)
descendAppsM :: forall (m :: * -> *).
Monad m =>
(LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
descendAppsM LHsExpr GhcPs -> m (LHsExpr GhcPs)
f (L SrcSpanAnnA
l (HsApp XApp GhcPs
_ LHsExpr GhcPs
x LHsExpr GhcPs
y)) = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall a b c. (a -> b -> c) -> m a -> m b -> m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\GenLocated SrcSpanAnnA (HsExpr GhcPs)
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
y -> SrcSpanAnnA
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnn NoEpAnns
XApp GhcPs
forall ann. EpAnn ann
EpAnnNotUsed GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y) ((LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
forall (m :: * -> *).
Monad m =>
(LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
descendAppsM LHsExpr GhcPs -> m (LHsExpr GhcPs)
f LHsExpr GhcPs
x) (LHsExpr GhcPs -> m (LHsExpr GhcPs)
f LHsExpr GhcPs
y)
descendAppsM LHsExpr GhcPs -> m (LHsExpr GhcPs)
f LHsExpr GhcPs
x = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall on (m :: * -> *).
(Uniplate on, Applicative m) =>
(on -> m on) -> on -> m on
forall (m :: * -> *).
Applicative m =>
(GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
descendM GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
LHsExpr GhcPs -> m (LHsExpr GhcPs)
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
transformAppsM :: Monad m => (LHsExpr GhcPs -> m (LHsExpr GhcPs)) -> LHsExpr GhcPs -> m (LHsExpr GhcPs)
transformAppsM :: forall (m :: * -> *).
Monad m =>
(LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
transformAppsM LHsExpr GhcPs -> m (LHsExpr GhcPs)
f LHsExpr GhcPs
x = GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
LHsExpr GhcPs -> m (LHsExpr GhcPs)
f (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
forall (m :: * -> *).
Monad m =>
(LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
descendAppsM ((LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
forall (m :: * -> *).
Monad m =>
(LHsExpr GhcPs -> m (LHsExpr GhcPs))
-> LHsExpr GhcPs -> m (LHsExpr GhcPs)
transformAppsM LHsExpr GhcPs -> m (LHsExpr GhcPs)
f) LHsExpr GhcPs
x
descendIndex :: Data a => (Int -> a -> a) -> a -> a
descendIndex :: forall a. Data a => (Int -> a -> a) -> a -> a
descendIndex Int -> a -> a
f = (a, ()) -> a
forall a b. (a, b) -> a
fst ((a, ()) -> a) -> (a -> (a, ())) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> Writer () a) -> a -> (a, ())
forall a w.
(Data a, Monoid w) =>
(Int -> a -> Writer w a) -> a -> (a, w)
descendIndex' (\Int
x a
a -> (a, ()) -> Writer () a
forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(a, w) -> WriterT w m a
writer (Int -> a -> a
f Int
x a
a, ()))
descendIndex' :: (Data a, Monoid w) => (Int -> a -> Writer w a) -> a -> (a, w)
descendIndex' :: forall a w.
(Data a, Monoid w) =>
(Int -> a -> Writer w a) -> a -> (a, w)
descendIndex' Int -> a -> Writer w a
f a
x = Writer w a -> (a, w)
forall w a. Monoid w => Writer w a -> (a, w)
runWriter (Writer w a -> (a, w)) -> Writer w a -> (a, w)
forall a b. (a -> b) -> a -> b
$ (StateT Int (WriterT w Identity) a -> Int -> Writer w a)
-> Int -> StateT Int (WriterT w Identity) a -> Writer w a
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Int (WriterT w Identity) a -> Int -> Writer w a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT Int
0 (StateT Int (WriterT w Identity) a -> Writer w a)
-> StateT Int (WriterT w Identity) a -> Writer w a
forall a b. (a -> b) -> a -> b
$ ((a -> StateT Int (WriterT w Identity) a)
-> a -> StateT Int (WriterT w Identity) a)
-> a
-> (a -> StateT Int (WriterT w Identity) a)
-> StateT Int (WriterT w Identity) a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> StateT Int (WriterT w Identity) a)
-> a -> StateT Int (WriterT w Identity) a
forall on (m :: * -> *).
(Uniplate on, Applicative m) =>
(on -> m on) -> on -> m on
forall (m :: * -> *). Applicative m => (a -> m a) -> a -> m a
descendM a
x ((a -> StateT Int (WriterT w Identity) a)
-> StateT Int (WriterT w Identity) a)
-> (a -> StateT Int (WriterT w Identity) a)
-> StateT Int (WriterT w Identity) a
forall a b. (a -> b) -> a -> b
$ \a
y -> do
Int
i <- StateT Int (WriterT w Identity) Int
forall (m :: * -> *) s. Monad m => StateT s m s
get
(Int -> Int) -> StateT Int (WriterT w Identity) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify (Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Writer w a -> StateT Int (WriterT w Identity) a
forall (m :: * -> *) a. Monad m => m a -> StateT Int m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Writer w a -> StateT Int (WriterT w Identity) a)
-> Writer w a -> StateT Int (WriterT w Identity) a
forall a b. (a -> b) -> a -> b
$ Int -> a -> Writer w a
f Int
i a
y
descendBracket :: (LHsExpr GhcPs -> (Bool, LHsExpr GhcPs)) -> LHsExpr GhcPs -> LHsExpr GhcPs
descendBracket :: (LHsExpr GhcPs -> (Bool, LHsExpr GhcPs))
-> LHsExpr GhcPs -> LHsExpr GhcPs
descendBracket LHsExpr GhcPs -> (Bool, LHsExpr GhcPs)
op LHsExpr GhcPs
x = (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Data a => (Int -> a -> a) -> a -> a
descendIndex Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
g GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
where
g :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
g Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
y = if Bool
a then Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
f Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
b else GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
b
where (Bool
a, LHsExpr GhcPs
b) = LHsExpr GhcPs -> (Bool, LHsExpr GhcPs)
op GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y
f :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
f Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
y = if Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Bool
forall a. Brackets a => Int -> a -> a -> Bool
needBracket Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
y then GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
y else GenLocated SrcSpanAnnA (HsExpr GhcPs)
y
rebracket1 :: LHsExpr GhcPs -> LHsExpr GhcPs
rebracket1 :: LHsExpr GhcPs -> LHsExpr GhcPs
rebracket1 = (LHsExpr GhcPs -> (Bool, LHsExpr GhcPs))
-> LHsExpr GhcPs -> LHsExpr GhcPs
descendBracket (Bool
True, )
appsBracket :: [LHsExpr GhcPs] -> LHsExpr GhcPs
appsBracket :: [LHsExpr GhcPs] -> LHsExpr GhcPs
appsBracket = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
mkApp
where mkApp :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> LHsExpr GhcPs
mkApp GenLocated SrcSpanAnnA (HsExpr GhcPs)
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
y = LHsExpr GhcPs -> LHsExpr GhcPs
rebracket1 (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnn NoEpAnns
XApp GhcPs
forall ann. EpAnn ann
EpAnnNotUsed GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y)
simplifyExp :: LHsExpr GhcPs -> LHsExpr GhcPs
simplifyExp :: LHsExpr GhcPs -> LHsExpr GhcPs
simplifyExp (L SrcSpanAnnA
l (OpApp XOpApp GhcPs
_ LHsExpr GhcPs
x LHsExpr GhcPs
op LHsExpr GhcPs
y)) | LHsExpr GhcPs -> Bool
isDol LHsExpr GhcPs
op = SrcSpanAnnA
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnn NoEpAnns
XApp GhcPs
forall ann. EpAnn ann
EpAnnNotUsed LHsExpr GhcPs
x (LHsExpr GhcPs -> LHsExpr GhcPs
forall (id :: Pass). LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
nlHsPar LHsExpr GhcPs
y))
simplifyExp e :: LHsExpr GhcPs
e@(L SrcSpanAnnA
_ (HsLet XLet GhcPs
_ LHsToken "let" GhcPs
_ ((HsValBinds XHsValBinds GhcPs GhcPs
_ (ValBinds XValBinds GhcPs GhcPs
_ LHsBindsLR GhcPs GhcPs
binds []))) LHsToken "in" GhcPs
_ LHsExpr GhcPs
z)) =
case Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
-> [GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)]
forall a. Bag a -> [a]
bagToList Bag (GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs))
LHsBindsLR GhcPs GhcPs
binds of
[L SrcSpanAnnA
_ (FunBind XFunBind GhcPs GhcPs
_ LIdP GhcPs
_ (MG XMG GhcPs (LHsExpr GhcPs)
_ (L SrcSpanAnnL
_ [L SrcSpanAnnA
_ (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_(FunRhs (L SrcSpanAnnN
_ RdrName
x) LexicalFixity
_ SrcStrictness
_) [] (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_[L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
y)] ((EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
_))))]) Origin
_) [CoreTickish]
_)]
| RdrName -> String
occNameStr RdrName
x String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
y Bool -> Bool -> Bool
&& [()] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [() | Unqual OccName
a <- GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [RdrName]
forall from to. Biplate from to => from -> [to]
universeBi GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z, OccName
a OccName -> OccName -> Bool
forall a. Eq a => a -> a -> Bool
== RdrName -> OccName
rdrNameOcc RdrName
x] Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 ->
(GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall on. Uniplate on => (on -> on) -> on -> on
transform GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
GenLocated SrcSpanAnnA (HsExpr GhcPs) -> LHsExpr GhcPs
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z
where f :: GenLocated SrcSpanAnnA (HsExpr GhcPs) -> LHsExpr GhcPs
f (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view -> Var_ String
x') | RdrName -> String
occNameStr RdrName
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
x' = LHsExpr GhcPs -> LHsExpr GhcPs
paren GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
x = GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
[GenLocated SrcSpanAnnA (HsBindLR GhcPs GhcPs)]
_ -> LHsExpr GhcPs
e
simplifyExp LHsExpr GhcPs
e = LHsExpr GhcPs
e
niceDotApp :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
niceDotApp :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
niceDotApp (L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
r))) LHsExpr GhcPs
b | RdrName -> String
occNameStr RdrName
r String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"$" = LHsExpr GhcPs
b
niceDotApp LHsExpr GhcPs
a LHsExpr GhcPs
b = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
dotApp LHsExpr GhcPs
a LHsExpr GhcPs
b
niceLambda :: [String] -> LHsExpr GhcPs -> LHsExpr GhcPs
niceLambda :: [String] -> LHsExpr GhcPs -> LHsExpr GhcPs
niceLambda [String]
ss LHsExpr GhcPs
e = (GenLocated SrcSpanAnnA (HsExpr GhcPs),
SrcSpan -> [Refactoring SrcSpan])
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a, b) -> a
fst ([String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
ss LHsExpr GhcPs
e)
allowRightSection :: String -> Bool
allowRightSection :: String -> Bool
allowRightSection String
x = String
x String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [String
"-",String
"#"]
allowLeftSection :: String -> Bool
allowLeftSection :: String -> Bool
allowLeftSection String
x = String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
"#"
niceLambdaR :: [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, R.SrcSpan -> [Refactoring R.SrcSpan])
niceLambdaR :: [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
xs (SimpleLambda [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
x) = [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
xs GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
niceLambdaR [String]
xs (L SrcSpanAnnA
_ (HsPar XPar GhcPs
_ LHsToken "(" GhcPs
_ LHsExpr GhcPs
x LHsToken ")" GhcPs
_)) = [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
xs LHsExpr GhcPs
x
niceLambdaR ([String] -> Maybe ([String], String)
forall a. [a] -> Maybe ([a], a)
unsnoc -> Just ([String]
vs, String
v)) (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> App2
LHsExpr GhcPs -> App2
forall a b. View a b => a -> b
view -> App2 GenLocated SrcSpanAnnA (HsExpr GhcPs)
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
e (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view -> Var_ String
v'))
| LHsExpr GhcPs -> Bool
isDol GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
f
, String
v String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
v'
, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
e [String] -> [String] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` [String
v]
= [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
vs GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
e
niceLambdaR [String
v] (L SrcSpanAnnA
_ (OpApp XOpApp GhcPs
_ LHsExpr GhcPs
e LHsExpr GhcPs
f (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
LHsExpr GhcPs -> Var_
forall a b. View a b => a -> b
view -> Var_ String
v')))
| LHsExpr GhcPs -> Bool
isLexeme LHsExpr GhcPs
e
, String
v String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
v'
, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
e [String] -> [String] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` [String
v]
, L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
fname)) <- LHsExpr GhcPs
f
, OccName -> Bool
isSymOcc (OccName -> Bool) -> OccName -> Bool
forall a b. (a -> b) -> a -> b
$ RdrName -> OccName
rdrNameOcc RdrName
fname
= let res :: LHsExpr GhcPs
res = LHsExpr GhcPs -> LHsExpr GhcPs
forall (id :: Pass). LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
nlHsPar (LHsExpr GhcPs -> LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XSectionL GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XSectionL p -> LHsExpr p -> LHsExpr p -> HsExpr p
SectionL EpAnn NoEpAnns
XSectionL GhcPs
forall ann. EpAnn ann
EpAnnNotUsed LHsExpr GhcPs
e LHsExpr GhcPs
f
in (LHsExpr GhcPs
res, \SrcSpan
s -> [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [] (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
res)])
niceLambdaR ([String] -> Maybe ([String], String)
forall a. [a] -> Maybe ([a], a)
unsnoc -> Just ([String]
vs, String
v)) (L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ LHsExpr GhcPs
f (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
LHsExpr GhcPs -> Var_
forall a b. View a b => a -> b
view -> Var_ String
v')))
| String
v String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
v'
, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
f [String] -> [String] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` [String
v]
= [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
vs LHsExpr GhcPs
f
niceLambdaR ([String] -> Maybe ([String], String)
forall a. [a] -> Maybe ([a], a)
unsnoc -> Just ([String]
vs, String
v)) (L SrcSpanAnnA
_ (SectionL XSectionL GhcPs
_ (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
LHsExpr GhcPs -> Var_
forall a b. View a b => a -> b
view -> Var_ String
v') LHsExpr GhcPs
f))
| String
v String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
v' = [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR [String]
vs LHsExpr GhcPs
f
niceLambdaR [String]
xs (SimpleLambda ((LocatedA (Pat GhcPs) -> PVar_
forall a b. View a b => a -> b
view -> PVar_ String
v):[LocatedA (Pat GhcPs)]
vs) GenLocated SrcSpanAnnA (HsExpr GhcPs)
x)
| String
v String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [String]
xs = [String]
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
niceLambdaR ([String]
xs[String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++[String
v]) (LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan]))
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
forall a b. (a -> b) -> a -> b
$ [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs
lambda [LocatedA (Pat GhcPs)]
[LPat GhcPs]
vs GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x
niceLambdaR [String
x] (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> App2
LHsExpr GhcPs -> App2
forall a b. View a b => a -> b
view -> App2 op :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
op@(L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
tag))) GenLocated SrcSpanAnnA (HsExpr GhcPs)
l GenLocated SrcSpanAnnA (HsExpr GhcPs)
r)
| LHsExpr GhcPs -> Bool
isLexeme GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view GenLocated SrcSpanAnnA (HsExpr GhcPs)
l Var_ -> Var_ -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Var_
Var_ String
x, String
x String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
r, String -> Bool
allowRightSection (RdrName -> String
occNameStr RdrName
tag) =
let e :: LHsExpr GhcPs
e = LHsExpr GhcPs -> LHsExpr GhcPs
rebracket1 (LHsExpr GhcPs -> LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XSectionR GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XSectionR p -> LHsExpr p -> LHsExpr p -> HsExpr p
SectionR EpAnn NoEpAnns
XSectionR GhcPs
forall ann. EpAnn ann
EpAnnNotUsed GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
op GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r)
in (LHsExpr GhcPs
e, \SrcSpan
s -> [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [] (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
e)])
niceLambdaR [String
x] LHsExpr GhcPs
y
| Just (LHsExpr GhcPs
z, [LHsExpr GhcPs]
subts) <- LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor LHsExpr GhcPs
y, String
x String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z = (LHsExpr GhcPs
z, \SrcSpan
s -> [[LHsExpr GhcPs] -> SrcSpan -> Refactoring SrcSpan
mkRefact [LHsExpr GhcPs]
subts SrcSpan
s])
where
factor :: LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor :: LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor (L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ LHsExpr GhcPs
ini LHsExpr GhcPs
lst)) | GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
lst Var_ -> Var_ -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Var_
Var_ String
x = (GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
ini, [GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
ini])
factor (L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ LHsExpr GhcPs
ini LHsExpr GhcPs
lst)) | Just (LHsExpr GhcPs
z, [LHsExpr GhcPs]
ss) <- LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor LHsExpr GhcPs
lst
= let r :: LHsExpr GhcPs
r = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
niceDotApp LHsExpr GhcPs
ini LHsExpr GhcPs
z
in if GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Bool
forall a. Data a => a -> a -> Bool
astEq GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z then (GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r, [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
ss) else (GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r, GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
ini GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. a -> [a] -> [a]
: [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
ss)
factor (L SrcSpanAnnA
_ (OpApp XOpApp GhcPs
_ LHsExpr GhcPs
y LHsExpr GhcPs
op (LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor -> Just (LHsExpr GhcPs
z, [LHsExpr GhcPs]
ss))))| LHsExpr GhcPs -> Bool
isDol LHsExpr GhcPs
op
= let r :: LHsExpr GhcPs
r = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
niceDotApp LHsExpr GhcPs
y LHsExpr GhcPs
z
in if GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Bool
forall a. Data a => a -> a -> Bool
astEq GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z then (GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r, [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
ss) else (GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r, GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. a -> [a] -> [a]
: [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
ss)
factor (L SrcSpanAnnA
_ (HsPar XPar GhcPs
_ LHsToken "(" GhcPs
_ y :: LHsExpr GhcPs
y@(L SrcSpanAnnA
_ HsApp{}) LHsToken ")" GhcPs
_)) = LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
factor LHsExpr GhcPs
y
factor LHsExpr GhcPs
_ = Maybe
(GenLocated SrcSpanAnnA (HsExpr GhcPs),
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
forall a. Maybe a
Nothing
mkRefact :: [LHsExpr GhcPs] -> R.SrcSpan -> Refactoring R.SrcSpan
mkRefact :: [LHsExpr GhcPs] -> SrcSpan -> Refactoring SrcSpan
mkRefact [LHsExpr GhcPs]
subts SrcSpan
s =
let tempSubts :: [(String, SrcSpan)]
tempSubts = (String
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> (String, SrcSpan))
-> [String]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [(String, SrcSpan)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\String
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b -> (String
a, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
b)) [String]
substVars [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
subts
template :: LHsExpr GhcPs
template = [LHsExpr GhcPs] -> LHsExpr GhcPs
dotApps (((String, SrcSpan) -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [(String, SrcSpan)] -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map (String -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
String -> LHsExpr GhcPs
strToVar (String -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> ((String, SrcSpan) -> String)
-> (String, SrcSpan)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, SrcSpan) -> String
forall a b. (a, b) -> a
fst) [(String, SrcSpan)]
tempSubts)
in RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [(String, SrcSpan)]
tempSubts (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
template)
niceLambdaR [String
x,String
y] (L SrcSpanAnnA
_ (OpApp XOpApp GhcPs
_ (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
LHsExpr GhcPs -> Var_
forall a b. View a b => a -> b
view -> Var_ String
x1) op :: LHsExpr GhcPs
op@(L SrcSpanAnnA
_ HsVar {}) (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
LHsExpr GhcPs -> Var_
forall a b. View a b => a -> b
view -> Var_ String
y1)))
| String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
x1, String
y String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
y1, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
op [String] -> [String] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` [String
x, String
y] = (LHsExpr GhcPs
op, \SrcSpan
s -> [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [] (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
op)])
niceLambdaR [String
x, String
y] (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> App2
LHsExpr GhcPs -> App2
forall a b. View a b => a -> b
view -> App2 GenLocated SrcSpanAnnA (HsExpr GhcPs)
op (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view -> Var_ String
y1) (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
forall a b. View a b => a -> b
view -> Var_ String
x1))
| String
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
x1, String
y String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
y1, GenLocated SrcSpanAnnA (HsExpr GhcPs) -> [String]
forall a. FreeVars a => a -> [String]
vars GenLocated SrcSpanAnnA (HsExpr GhcPs)
op [String] -> [String] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` [String
x, String
y] =
( LHsExpr GhcPs -> LHsExpr GhcPs
gen GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
op
, \SrcSpan
s -> [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [(String
"x", GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
op)] (LHsExpr GhcPs -> String
forall a. Outputable a => a -> String
unsafePrettyPrint (LHsExpr GhcPs -> String) -> LHsExpr GhcPs -> String
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> LHsExpr GhcPs
gen (String -> LHsExpr GhcPs
strToVar String
"x"))]
)
where
gen :: LHsExpr GhcPs -> LHsExpr GhcPs
gen :: LHsExpr GhcPs -> LHsExpr GhcPs
gen = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnn NoEpAnns
XApp GhcPs
forall ann. EpAnn ann
EpAnnNotUsed (String -> LHsExpr GhcPs
strToVar String
"flip")
(GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> HsExpr GhcPs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. if GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Bool
forall a. Brackets a => a -> Bool
isAtom GenLocated SrcSpanAnnA (HsExpr GhcPs)
op then GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. a -> a
id else GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen
niceLambdaR [] LHsExpr GhcPs
e = (LHsExpr GhcPs
e, \SrcSpan
s -> [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr SrcSpan
s [(String
"a", GenLocated SrcSpanAnnA (HsExpr GhcPs) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
e)] String
"a"])
niceLambdaR [String]
ss LHsExpr GhcPs
e =
let grhs :: LGRHS GhcPs (LHsExpr GhcPs)
grhs = GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a an. a -> LocatedAn an a
noLocA (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b. (a -> b) -> a -> b
$ XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [GuardLStmt GhcPs]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS EpAnn GrhsAnn
XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
e :: LGRHS GhcPs (LHsExpr GhcPs)
grhss :: GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
grhss = GRHSs {grhssExt :: XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
grhssExt = XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
EpAnnComments
emptyComments, grhssGRHSs :: [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
grhssGRHSs=[LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
LGRHS GhcPs (LHsExpr GhcPs)
grhs], grhssLocalBinds :: HsLocalBinds GhcPs
grhssLocalBinds=XEmptyLocalBinds GhcPs GhcPs -> HsLocalBinds GhcPs
forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
EmptyLocalBinds NoExtField
XEmptyLocalBinds GhcPs GhcPs
noExtField}
match :: LMatch GhcPs (LHsExpr GhcPs)
match = Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a an. a -> LocatedAn an a
noLocA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
-> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall a b. (a -> b) -> a -> b
$ Match {m_ext :: XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
m_ext=EpAnn [AddEpAnn]
XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed, m_ctxt :: HsMatchContext GhcPs
m_ctxt=HsMatchContext GhcPs
forall p. HsMatchContext p
LambdaExpr, m_pats :: [LPat GhcPs]
m_pats=(String -> LocatedA (Pat GhcPs))
-> [String] -> [LocatedA (Pat GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map String -> LocatedA (Pat GhcPs)
String -> LPat GhcPs
strToPat [String]
ss, m_grhss :: GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
m_grhss=GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
grhss} :: LMatch GhcPs (LHsExpr GhcPs)
matchGroup :: MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
matchGroup = MG {mg_ext :: XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
mg_ext=NoExtField
XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
noExtField, mg_origin :: Origin
mg_origin=Origin
Generated, mg_alts :: XRec GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
mg_alts=[LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> LocatedAn
AnnList [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
forall a an. a -> LocatedAn an a
noLocA [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
LMatch GhcPs (LHsExpr GhcPs)
match]}
in (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a an. a -> LocatedAn an a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XLam GhcPs -> MatchGroup GhcPs (LHsExpr GhcPs) -> HsExpr GhcPs
forall p. XLam p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLam NoExtField
XLam GhcPs
noExtField MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
MatchGroup GhcPs (LHsExpr GhcPs)
matchGroup, [Refactoring SrcSpan] -> SrcSpan -> [Refactoring SrcSpan]
forall a b. a -> b -> a
const [])
replaceBranches :: LHsExpr GhcPs -> ([LHsExpr GhcPs], [LHsExpr GhcPs] -> LHsExpr GhcPs)
replaceBranches :: LHsExpr GhcPs
-> ([LHsExpr GhcPs], [LHsExpr GhcPs] -> LHsExpr GhcPs)
replaceBranches (L SrcSpanAnnA
l (HsIf XIf GhcPs
_ LHsExpr GhcPs
a LHsExpr GhcPs
b LHsExpr GhcPs
c)) = ([LHsExpr GhcPs
b, LHsExpr GhcPs
c], \[LHsExpr GhcPs
b, LHsExpr GhcPs
c] -> SrcSpanAnnA
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XIf GhcPs
-> LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XIf p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsIf EpAnn AnnsIf
XIf GhcPs
forall ann. EpAnn ann
EpAnnNotUsed LHsExpr GhcPs
a LHsExpr GhcPs
b LHsExpr GhcPs
c))
replaceBranches (L SrcSpanAnnA
s (HsCase XCase GhcPs
_ LHsExpr GhcPs
a (MG XMG GhcPs (LHsExpr GhcPs)
_ (L SrcSpanAnnL
l [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
bs) Origin
FromSource))) =
((LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)])
-> [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
LMatch GhcPs (LHsExpr GhcPs) -> [LHsExpr GhcPs]
f [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
bs, \[LHsExpr GhcPs]
xs -> SrcSpanAnnA
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
s (XCase GhcPs
-> LHsExpr GhcPs
-> MatchGroup GhcPs (LHsExpr GhcPs)
-> HsExpr GhcPs
forall p.
XCase p -> LHsExpr p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsCase EpAnn EpAnnHsCase
XCase GhcPs
forall ann. EpAnn ann
EpAnnNotUsed LHsExpr GhcPs
a (XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> XRec
GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> Origin
-> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XMG p body -> XRec p [LMatch p body] -> Origin -> MatchGroup p body
MG NoExtField
XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
noExtField (SrcSpanAnnL
-> [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> LocatedAn
AnnList
[LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
l ([LMatch GhcPs (LHsExpr GhcPs)]
-> [LHsExpr GhcPs] -> [LMatch GhcPs (LHsExpr GhcPs)]
g [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LMatch GhcPs (LHsExpr GhcPs)]
bs [LHsExpr GhcPs]
xs)) Origin
Generated)))
where
f :: LMatch GhcPs (LHsExpr GhcPs) -> [LHsExpr GhcPs]
f :: LMatch GhcPs (LHsExpr GhcPs) -> [LHsExpr GhcPs]
f (L SrcSpanAnnA
_ (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext GhcPs
CaseAlt [LPat GhcPs]
_ (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
xs HsLocalBinds GhcPs
_))) = [GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x | (L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [GuardLStmt GhcPs]
_ GenLocated SrcSpanAnnA (HsExpr GhcPs)
x)) <- [LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
xs]
f LMatch GhcPs (LHsExpr GhcPs)
_ = String -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
forall a. HasCallStack => String -> a
error String
"GHC.Util.HsExpr.replaceBranches: unexpected XMatch"
g :: [LMatch GhcPs (LHsExpr GhcPs)] -> [LHsExpr GhcPs] -> [LMatch GhcPs (LHsExpr GhcPs)]
g :: [LMatch GhcPs (LHsExpr GhcPs)]
-> [LHsExpr GhcPs] -> [LMatch GhcPs (LHsExpr GhcPs)]
g (L SrcSpanAnnA
s1 (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext GhcPs
CaseAlt [LPat GhcPs]
a (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
ns HsLocalBinds GhcPs
b)) : [LMatch GhcPs (LHsExpr GhcPs)]
rest) [LHsExpr GhcPs]
xs =
SrcSpanAnnA
-> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
s1 (XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsMatchContext GhcPs
-> [LPat GhcPs]
-> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCMatch p body
-> HsMatchContext p -> [LPat p] -> GRHSs p body -> Match p body
Match EpAnn [AddEpAnn]
XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed HsMatchContext GhcPs
forall p. HsMatchContext p
CaseAlt [LPat GhcPs]
a (XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
-> HsLocalBinds GhcPs
-> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
EpAnnComments
emptyComments [SrcAnn NoEpAnns
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
a (XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> [GuardLStmt GhcPs]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS EpAnn GrhsAnn
XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall ann. EpAnn ann
EpAnnNotUsed [GuardLStmt GhcPs]
gs GenLocated SrcSpanAnnA (HsExpr GhcPs)
x) | (L SrcAnn NoEpAnns
a (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [GuardLStmt GhcPs]
gs GenLocated SrcSpanAnnA (HsExpr GhcPs)
_), GenLocated SrcSpanAnnA (HsExpr GhcPs)
x) <- [LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> [(LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))),
GenLocated SrcSpanAnnA (HsExpr GhcPs))]
forall a b. [a] -> [b] -> [(a, b)]
zip [LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
ns [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
as] HsLocalBinds GhcPs
b)) LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
forall a. a -> [a] -> [a]
: [LMatch GhcPs (LHsExpr GhcPs)]
-> [LHsExpr GhcPs] -> [LMatch GhcPs (LHsExpr GhcPs)]
g [LMatch GhcPs (LHsExpr GhcPs)]
rest [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
bs
where ([GenLocated SrcSpanAnnA (HsExpr GhcPs)]
as, [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
bs) = Int
-> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
-> ([GenLocated SrcSpanAnnA (HsExpr GhcPs)],
[GenLocated SrcSpanAnnA (HsExpr GhcPs)])
forall a. Int -> [a] -> ([a], [a])
splitAt ([LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LocatedAn
NoEpAnns (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
ns) [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
[LHsExpr GhcPs]
xs
g [] [] = []
g [LMatch GhcPs (LHsExpr GhcPs)]
_ [LHsExpr GhcPs]
_ = String
-> [LocatedAn
AnnListItem (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
forall a. HasCallStack => String -> a
error String
"GHC.Util.HsExpr.replaceBranches': internal invariant failed, lists are of differing lengths"
replaceBranches LHsExpr GhcPs
x = ([], \[] -> LHsExpr GhcPs
x)
needBracketOld :: Int -> LHsExpr GhcPs -> LHsExpr GhcPs -> Bool
needBracketOld :: Int -> LHsExpr GhcPs -> LHsExpr GhcPs -> Bool
needBracketOld Int
i LHsExpr GhcPs
parent LHsExpr GhcPs
child
| LHsExpr GhcPs -> Bool
isDotApp LHsExpr GhcPs
parent, LHsExpr GhcPs -> Bool
isDotApp LHsExpr GhcPs
child, Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2 = Bool
False
| Bool
otherwise = Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Bool
forall a. Brackets a => Int -> a -> a -> Bool
needBracket Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
parent GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
child
transformBracketOld :: (LHsExpr GhcPs -> Maybe (LHsExpr GhcPs))
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, (LHsExpr GhcPs, [String]))
transformBracketOld :: (LHsExpr GhcPs -> Maybe (LHsExpr GhcPs))
-> LHsExpr GhcPs -> (LHsExpr GhcPs, (LHsExpr GhcPs, [String]))
transformBracketOld LHsExpr GhcPs -> Maybe (LHsExpr GhcPs)
op = ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
forall a a' b. (a -> a') -> (a, b) -> (a', b)
first (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a, b) -> b
snd (((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
g
where
g :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
g = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
forall a a' b. (a -> a') -> (a, b) -> (a', b)
first GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
f ((GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LHsExpr GhcPs
-> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String])))
-> LHsExpr GhcPs -> (LHsExpr GhcPs, (LHsExpr GhcPs, [String]))
descendBracketOld GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String]))
g
f :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
f GenLocated SrcSpanAnnA (HsExpr GhcPs)
x = (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> Maybe (GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> (Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Bool
False, GenLocated SrcSpanAnnA (HsExpr GhcPs)
x) (Bool
True, ) (LHsExpr GhcPs -> Maybe (LHsExpr GhcPs)
op GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x)
descendBracketOld :: (LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String])))
-> LHsExpr GhcPs
-> (LHsExpr GhcPs, (LHsExpr GhcPs, [String]))
descendBracketOld :: (LHsExpr GhcPs
-> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String])))
-> LHsExpr GhcPs -> (LHsExpr GhcPs, (LHsExpr GhcPs, [String]))
descendBracketOld LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String]))
op LHsExpr GhcPs
x = ((Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Data a => (Int -> a -> a) -> a -> a
descendIndex Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
g1 GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x, (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
forall a w.
(Data a, Monoid w) =>
(Int -> a -> Writer w a) -> a -> (a, w)
descendIndex' Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs))
g2 GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x)
where
g :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
g Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
y = if Bool
a then (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
f1 Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z [String]
w, Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
f2 Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z [String]
w) else (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
b, (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
z, [String]
w))
where ((Bool
a, LHsExpr GhcPs
b), (LHsExpr GhcPs
z, [String]
w)) = LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String]))
op GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y
g1 :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
g1 Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b = (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a, b) -> a
fst (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
g Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b)
g2 :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs))
g2 Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b = (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(a, w) -> WriterT w m a
writer ((GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
-> Writer [String] (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall a b. (a -> b) -> a -> b
$ (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
forall a b. (a, b) -> b
snd (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
g Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b)
f :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
f Int
i (L SrcSpanAnnA
_ (HsPar XPar GhcPs
_ LHsToken "(" GhcPs
_ LHsExpr GhcPs
y LHsToken ")" GhcPs
_)) GenLocated SrcSpanAnnA (HsExpr GhcPs)
z [String]
w
| Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> LHsExpr GhcPs -> LHsExpr GhcPs -> Bool
needBracketOld Int
i LHsExpr GhcPs
x LHsExpr GhcPs
y = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y, GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
removeBracket GenLocated SrcSpanAnnA (HsExpr GhcPs)
z)
where
removeBracket :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
removeBracket = \case
var :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
var@(L SrcSpanAnnA
_ HsVar{}) -> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
z, LHsExpr GhcPs -> String
varToStr GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
var String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
w)
GenLocated SrcSpanAnnA (HsExpr GhcPs)
other -> (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
fromParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
z, [String]
w)
f Int
i GenLocated SrcSpanAnnA (HsExpr GhcPs)
y GenLocated SrcSpanAnnA (HsExpr GhcPs)
z [String]
w
| Int -> LHsExpr GhcPs -> LHsExpr GhcPs -> Bool
needBracketOld Int
i LHsExpr GhcPs
x GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
y = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
y, (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
z, [String]
w))
| GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Bool
forall {l}. GenLocated l (HsExpr GhcPs) -> Bool
isOp GenLocated SrcSpanAnnA (HsExpr GhcPs)
y = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
y, (GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a. Brackets a => a -> a
addParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
z, [String]
w))
f Int
_ GenLocated SrcSpanAnnA (HsExpr GhcPs)
y GenLocated SrcSpanAnnA (HsExpr GhcPs)
z [String]
w = (GenLocated SrcSpanAnnA (HsExpr GhcPs)
y, (GenLocated SrcSpanAnnA (HsExpr GhcPs)
z, [String]
w))
f1 :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
f1 Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
c [String]
d = (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a, b) -> a
fst (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
f Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
c [String]
d)
f2 :: Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
f2 Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
c [String]
d = (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
forall a b. (a, b) -> b
snd (Int
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [String]
-> (GenLocated SrcSpanAnnA (HsExpr GhcPs),
(GenLocated SrcSpanAnnA (HsExpr GhcPs), [String]))
f Int
a GenLocated SrcSpanAnnA (HsExpr GhcPs)
b GenLocated SrcSpanAnnA (HsExpr GhcPs)
c [String]
d)
isOp :: GenLocated l (HsExpr GhcPs) -> Bool
isOp = \case
L l
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
name)) -> RdrName -> Bool
isSymbolRdrName RdrName
name
GenLocated l (HsExpr GhcPs)
_ -> Bool
False
fromParen1 :: LHsExpr GhcPs -> LHsExpr GhcPs
fromParen1 :: LHsExpr GhcPs -> LHsExpr GhcPs
fromParen1 LHsExpr GhcPs
x = LHsExpr GhcPs -> Maybe (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. a -> Maybe a -> a
fromMaybe LHsExpr GhcPs
x (Maybe (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> Maybe (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> Maybe (GenLocated SrcSpanAnnA (HsExpr GhcPs))
forall a. Brackets a => a -> Maybe a
remParen GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
x