    {-# LANGUAGE ImportQualifiedPost #-}
    {-# 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.Util.Brackets
    import GHC.Util.FreeVars
    import GHC.Util.View

    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 Refact.Types qualified 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 a b' makes 'a . b'.
    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 e a. HasAnnotation e => a -> GenLocated e 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 XOpApp GhcPs
    NoExtField
    noExtField LHsExpr GhcPs
    x (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall e a. HasAnnotation e => a -> GenLocated e 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 XVar GhcPs
    NoExtField
    noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
    forall e a. HasAnnotation e => a -> GenLocated e 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 [p0, p1..pn] body@ makes @\p1 p1 .. pn -> body@
    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 e a. HasAnnotation e => a -> GenLocated e a
    noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall a b. (a -> b) -> a -> b
    $ XLam GhcPs
    -> HsLamVariant -> MatchGroup GhcPs (LHsExpr GhcPs) -> HsExpr GhcPs
    forall p.
    XLam p -> HsLamVariant -> MatchGroup p (LHsExpr p) -> HsExpr p
    HsLam XLam GhcPs
    EpAnnLam
    forall a. NoAnn a => a
    noAnn HsLamVariant
    LamSingle (XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> XRec
         GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    -> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall p body.
    XMG p body -> XRec p [LMatch p body] -> MatchGroup p body
    MG (GenReason -> DoPmc -> Origin
    Generated GenReason
    OtherExpansion DoPmc
    DoPmc) ([GenLocated
       SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> GenLocated
         SrcSpanAnnLW
         [GenLocated
            SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA [Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
     -> GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
    -> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall a b. (a -> b) -> a -> b
    $ XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> HsMatchContext (LIdP (NoGhcTc GhcPs))
    -> XRec GhcPs [LPat GhcPs]
    -> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall p body.
    XCMatch p body
    -> HsMatchContext (LIdP (NoGhcTc p))
    -> XRec p [LPat p]
    -> GRHSs p body
    -> Match p body
    Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    NoExtField
    noExtField (HsLamVariant -> HsMatchContext (GenLocated SrcSpanAnnN RdrName)
    forall fn. HsLamVariant -> HsMatchContext fn
    LamAlt HsLamVariant
    LamSingle) (EpaLocation
    -> [LocatedA (Pat GhcPs)]
    -> GenLocated EpaLocation [LocatedA (Pat GhcPs)]
    forall l e. l -> e -> GenLocated l e
    L EpaLocation
    forall a. NoAnn a => EpaLocation' a
    noSpanAnchor [LPat GhcPs]
    [LocatedA (Pat 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))
    -> GenLocated
         EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
     -> GenLocated
          EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
    -> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         EpAnnCO (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 XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    EpAnn GrhsAnn
    forall a. NoAnn a => a
    noAnn [] LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    body] (XEmptyLocalBinds GhcPs GhcPs -> HsLocalBinds GhcPs
    forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
    EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
    NoExtField
    noExtField))]))

    -- | 'paren e' wraps 'e' in parens if 'e' is non-atomic.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x  = LHsExpr GhcPs
    x
      | Bool
    otherwise = GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall a. Brackets a => a -> a
    addParen LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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' LHsExpr GhcPs
    -> LHsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall {p} {e}.
    (XApp p ~ NoExtField, HasAnnotation e) =>
    XRec p (HsExpr p) -> XRec p (HsExpr p) -> GenLocated e (HsExpr p)
    mkApp where mkApp :: XRec p (HsExpr p) -> XRec p (HsExpr p) -> GenLocated e (HsExpr p)
    mkApp XRec p (HsExpr p)
    x XRec p (HsExpr p)
    y = HsExpr p -> GenLocated e (HsExpr p)
    forall e a. HasAnnotation e => a -> GenLocated e 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 XApp p
    NoExtField
    noExtField 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]
    ++ [LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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]
    ++ [LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y]
    childrenApps LHsExpr GhcPs
    x = GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    forall on. Uniplate on => on -> [on]
    children LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x

    universeApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
    universeApps :: LHsExpr GhcPs -> [LHsExpr GhcPs]
    universeApps LHsExpr GhcPs
    x = LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs -> [LHsExpr GhcPs]
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> [GenLocated SrcSpanAnnA (HsExpr 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)
    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 XApp GhcPs
    NoExtField
    noExtField LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y) (GenLocated SrcSpanAnnA (HsExpr GhcPs)
     -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
     -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> m (GenLocated SrcSpanAnnA (HsExpr GhcPs)
          -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f 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)
    f LHsExpr GhcPs
    x m (GenLocated SrcSpanAnnA (HsExpr GhcPs)
       -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall a b. m (a -> b) -> m a -> m b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> 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 LHsExpr GhcPs -> m (LHsExpr GhcPs)
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> m (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    f LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 = LHsExpr GhcPs -> m (LHsExpr GhcPs)
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> m (GenLocated SrcSpanAnnA (HsExpr 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
        i <- StateT Int (WriterT w Identity) Int
    forall (m :: * -> *) s. Monad m => StateT s m s
    get
        modify (+1)
        lift $ f i y

    --  There are differences in pretty-printing between GHC and HSE. This
    --  version never removes brackets.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b else LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b
                where (Bool
    a, LHsExpr GhcPs
    b) = LHsExpr GhcPs -> (Bool, LHsExpr GhcPs)
    op LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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

    -- Add brackets as suggested 'needBracket at 1-level of depth.
    rebracket1 :: LHsExpr GhcPs -> LHsExpr GhcPs
    rebracket1 :: LHsExpr GhcPs -> LHsExpr GhcPs
    rebracket1 = (LHsExpr GhcPs -> (Bool, LHsExpr GhcPs))
    -> LHsExpr GhcPs -> LHsExpr GhcPs
    descendBracket (Bool
    True, )

    -- A list of application, with any necessary brackets.
    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 e a. HasAnnotation e => a -> GenLocated e 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 XApp GhcPs
    NoExtField
    noExtField LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y)

    simplifyExp :: LHsExpr GhcPs -> LHsExpr GhcPs
    -- Replace appliciations 'f $ x' with 'f (x)'.
    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 XApp GhcPs
    NoExtField
    noExtField LHsExpr GhcPs
    x (LHsExpr GhcPs -> LHsExpr GhcPs
    forall (p :: Pass).
    IsPass p =>
    LHsExpr (GhcPass p) -> LHsExpr (GhcPass p)
    nlHsPar LHsExpr GhcPs
    y))
    simplifyExp e :: LHsExpr GhcPs
    e@(L SrcSpanAnnA
    _ (HsLet XLet GhcPs
    _ ((HsValBinds XHsValBinds GhcPs GhcPs
    _ (ValBinds XValBinds GhcPs GhcPs
    _ LHsBindsLR GhcPs GhcPs
    binds []))) LHsExpr GhcPs
    z)) =
      -- An expression of the form, 'let x = y in z'.
      case LHsBindsLR GhcPs GhcPs
    binds of
        [L SrcSpanAnnA
    _ (FunBind XFunBind GhcPs GhcPs
    _ LIdP GhcPs
    _ (MG XMG GhcPs (LHsExpr GhcPs)
    _ (L SrcSpanAnnLW
    _ [L SrcSpanAnnA
    _ (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _(FunRhs (L SrcSpanAnnN
    _ RdrName
    x) LexicalFixity
    _ SrcStrictness
    _ XFunRhs
    _) (L EpaLocation
    _ []) (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _ [L EpAnnCO
    _ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _ [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y)] ((EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
    _))))])))]
             -- If 'x' is not in the free variables of 'y', beta-reduce to
             -- 'z[(y)/x]'.
          | 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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) -> LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    f LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y
                    f GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x = LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x
        LHsBindsLR GhcPs GhcPs
    _ -> LHsExpr GhcPs
    e
    simplifyExp LHsExpr GhcPs
    e = LHsExpr GhcPs
    e

    -- Rewrite '($) . b' as 'b'.
    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

    -- Generate a lambda expression but prettier if possible.
    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)-- We don't support refactorings yet.

    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
    "#"

    -- Implementation. Try to produce special forms (e.g. sections,
    -- compositions) where we can.
    niceLambdaR :: [String]
                -> LHsExpr GhcPs
                -> (LHsExpr GhcPs, R.SrcSpan -> [Refactoring R.SrcSpan])
    -- Rewrite @\ -> e@ as @e@
    -- These are encountered as recursive calls.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x

    -- Rewrite @\xs -> (e)@ as @\xs -> e@.
    niceLambdaR [String]
    xs (L SrcSpanAnnA
    _ (HsPar XPar GhcPs
    _ LHsExpr GhcPs
    x)) = [String]
    -> LHsExpr GhcPs
    -> (LHsExpr GhcPs, SrcSpan -> [Refactoring SrcSpan])
    niceLambdaR [String]
    xs LHsExpr GhcPs
    x

    -- @\vs v -> ($) e v@ ==> @\vs -> e@
    -- @\vs v -> e $ v@ ==> @\vs -> e@
    niceLambdaR ([String] -> Maybe ([String], String)
    forall a. [a] -> Maybe ([a], a)
    unsnoc -> Just ([String]
    vs, String
    v)) (LHsExpr GhcPs -> App2
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    e

    -- @\v -> thing + v@ ==> @\v -> (thing +)@  (heuristic: @v@ must be a single
    -- lexeme, or it all gets too complex)
    niceLambdaR [String
    v] (L SrcSpanAnnA
    _ (OpApp XOpApp GhcPs
    _ LHsExpr GhcPs
    e LHsExpr GhcPs
    f (LHsExpr GhcPs -> Var_
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (p :: Pass).
    IsPass p =>
    LHsExpr (GhcPass p) -> LHsExpr (GhcPass p)
    nlHsPar (LHsExpr GhcPs -> LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
    forall a b. (a -> b) -> a -> b
    $ HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall e a. HasAnnotation e => a -> GenLocated e 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 XSectionL GhcPs
    NoExtField
    noExtField 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    res)])

    -- @\vs v -> f x v@ ==> @\vs -> f x@
    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 (LHsExpr GhcPs -> Var_
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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

    -- @\vs v -> (v `f`)@ ==> @\vs -> f@
    niceLambdaR ([String] -> Maybe ([String], String)
    forall a. [a] -> Maybe ([a], a)
    unsnoc -> Just ([String]
    vs, String
    v)) (L SrcSpanAnnA
    _ (SectionL XSectionL GhcPs
    _ (LHsExpr GhcPs -> Var_
    GenLocated SrcSpanAnnA (HsExpr 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

    -- Strip one variable pattern from the end of a lambdas match, and place it in our list of factoring variables.
    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 [LPat GhcPs]
    [LocatedA (Pat GhcPs)]
    vs LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x

    -- Rewrite @\x -> x + a@ as @(+ a)@ (heuristic: @a@ must be a single
    -- lexeme, or it all gets too complex).
    niceLambdaR [String
    x] (LHsExpr GhcPs -> App2
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 e a. HasAnnotation e => a -> GenLocated e 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 XSectionR GhcPs
    NoExtField
    noExtField LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    op LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    e)])
    -- Rewrite (1) @\x -> f (b x)@ as @f . b@, (2) @\x -> f $ b x@ as @f . b@.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    z = (LHsExpr GhcPs
    z, \SrcSpan
    s -> [[LHsExpr GhcPs] -> SrcSpan -> Refactoring SrcSpan
    mkRefact [LHsExpr GhcPs]
    subts SrcSpan
    s])
      where
        -- Factor the expression with respect to x.
        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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    ini, [LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r, [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r, LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    ini GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    forall a. a -> [a] -> [a]
    : [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r, [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    r, LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    forall a. a -> [a] -> [a]
    : [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    ss)
        factor (L SrcSpanAnnA
    _ (HsPar XPar GhcPs
    _ y :: LHsExpr GhcPs
    y@(L SrcSpanAnnA
    _ HsApp{}))) = LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
    factor LHsExpr GhcPs
    y
        factor LHsExpr GhcPs
    _ = Maybe (LHsExpr GhcPs, [LHsExpr GhcPs])
    Maybe
      (GenLocated SrcSpanAnnA (HsExpr GhcPs),
       [GenLocated SrcSpanAnnA (HsExpr 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 (EpAnn a) e -> SrcSpan
    toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b)) [String]
    substVars [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr 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 -> LHsExpr GhcPs
    String -> GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    template)
    -- Rewrite @\x y -> x + y@ as @(+)@.
    niceLambdaR [String
    x,String
    y] (L SrcSpanAnnA
    _ (OpApp XOpApp GhcPs
    _ (LHsExpr GhcPs -> Var_
    GenLocated SrcSpanAnnA (HsExpr GhcPs) -> Var_
    forall a b. View a b => a -> b
    view -> Var_ String
    x1) op :: LHsExpr GhcPs
    op@(L SrcSpanAnnA
    _ HsVar {}) (LHsExpr GhcPs -> Var_
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    op)])
    -- Rewrite @\x y -> f y x@ as @flip f@.
    niceLambdaR [String
    x, String
    y] (LHsExpr GhcPs -> App2
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (EpAnn 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 e a. HasAnnotation e => a -> GenLocated e 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 XApp GhcPs
    NoExtField
    noExtField (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

    -- We're done factoring, but have no variables left, so we shouldn't make a lambda.
    -- @\ -> e@ ==> @e@
    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 (EpAnn a) e -> SrcSpan
    toSSA LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    e)] String
    "a"])
    -- Base case. Just a good old fashioned lambda.
    niceLambdaR [String]
    ss LHsExpr GhcPs
    e =
      let grhs :: LGRHS GhcPs (LHsExpr GhcPs)
    grhs = GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
     -> GenLocated
          EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
    -> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         EpAnnCO (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 XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    EpAnn GrhsAnn
    forall a. NoAnn a => a
    noAnn [] LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 (LHsExpr GhcPs)
    LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    grhs], grhssLocalBinds :: HsLocalBinds GhcPs
    grhssLocalBinds=XEmptyLocalBinds GhcPs GhcPs -> HsLocalBinds GhcPs
    forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
    EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
    NoExtField
    noExtField}
          match :: LMatch GhcPs (LHsExpr GhcPs)
    match = Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
     -> GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))))
    -> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall a b. (a -> b) -> a -> b
    $ Match {m_ext :: XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    m_ext=XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    NoExtField
    noExtField, m_ctxt :: HsMatchContext (LIdP (NoGhcTc GhcPs))
    m_ctxt=HsLamVariant -> HsMatchContext (GenLocated SrcSpanAnnN RdrName)
    forall fn. HsLamVariant -> HsMatchContext fn
    LamAlt HsLamVariant
    LamSingle, m_pats :: XRec GhcPs [LPat GhcPs]
    m_pats=[LocatedA (Pat GhcPs)]
    -> GenLocated EpaLocation [LocatedA (Pat GhcPs)]
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA ([LocatedA (Pat GhcPs)]
     -> GenLocated EpaLocation [LocatedA (Pat GhcPs)])
    -> [LocatedA (Pat GhcPs)]
    -> GenLocated EpaLocation [LocatedA (Pat GhcPs)]
    forall a b. (a -> b) -> a -> b
    $ (String -> LocatedA (Pat GhcPs))
    -> [String] -> [LocatedA (Pat GhcPs)]
    forall a b. (a -> b) -> [a] -> [b]
    map String -> LPat GhcPs
    String -> LocatedA (Pat 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=GenReason -> DoPmc -> Origin
    Generated GenReason
    OtherExpansion DoPmc
    SkipPmc, mg_alts :: XRec GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    mg_alts=[LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    -> GenLocated
         SrcSpanAnnLW [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA [LMatch GhcPs (LHsExpr GhcPs)
    LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    match]}
      in (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall a b. (a -> b) -> a -> b
    $ XLam GhcPs
    -> HsLamVariant -> MatchGroup GhcPs (LHsExpr GhcPs) -> HsExpr GhcPs
    forall p.
    XLam p -> HsLamVariant -> MatchGroup p (LHsExpr p) -> HsExpr p
    HsLam XLam GhcPs
    EpAnnLam
    forall a. NoAnn a => a
    noAnn HsLamVariant
    LamSingle MatchGroup GhcPs (LHsExpr GhcPs)
    MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    matchGroup, [Refactoring SrcSpan] -> SrcSpan -> [Refactoring SrcSpan]
    forall a b. a -> b -> a
    const [])

    -- 'case' and 'if' expressions have branches, nothing else does (this
    -- doesn't consider 'HsMultiIf' perhaps it should?).
    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 XIf GhcPs
    AnnsIf
    forall a. NoAnn a => a
    noAnn LHsExpr GhcPs
    a LHsExpr GhcPs
    b LHsExpr GhcPs
    c))

    replaceBranches (L SrcSpanAnnA
    s (HsCase XCase GhcPs
    _ LHsExpr GhcPs
    a (MG XMG GhcPs (LHsExpr GhcPs)
    Origin
    FromSource (L SrcSpanAnnLW
    l [GenLocated
       SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    bs)))) =
      ((GenLocated
       SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
     -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)])
    -> [GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap LMatch GhcPs (LHsExpr GhcPs) -> [LHsExpr GhcPs]
    GenLocated
      SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    f [GenLocated
       SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    bs, SrcSpanAnnA
    -> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnA
    s (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
    . XCase GhcPs
    -> LHsExpr GhcPs
    -> MatchGroup GhcPs (LHsExpr GhcPs)
    -> HsExpr GhcPs
    forall p.
    XCase p -> LHsExpr p -> MatchGroup p (LHsExpr p) -> HsExpr p
    HsCase XCase GhcPs
    EpAnnHsCase
    forall a. NoAnn a => a
    noAnn LHsExpr GhcPs
    a (MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
     -> HsExpr GhcPs)
    -> ([GenLocated SrcSpanAnnA (HsExpr GhcPs)]
        -> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> HsExpr GhcPs
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . XMG GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> XRec
         GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    -> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall p body.
    XMG p body -> XRec p [LMatch p body] -> MatchGroup p body
    MG (GenReason -> DoPmc -> Origin
    Generated GenReason
    OtherExpansion DoPmc
    SkipPmc)(GenLocated
       SrcSpanAnnLW
       [GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
     -> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    -> ([GenLocated SrcSpanAnnA (HsExpr GhcPs)]
        -> GenLocated
             SrcSpanAnnLW
             [GenLocated
                SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))])
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . SrcSpanAnnLW
    -> [GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> GenLocated
         SrcSpanAnnLW
         [GenLocated
            SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnLW
    l ([GenLocated
        SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
     -> GenLocated
          SrcSpanAnnLW
          [GenLocated
             SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))])
    -> ([GenLocated SrcSpanAnnA (HsExpr GhcPs)]
        -> [GenLocated
              SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))])
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> GenLocated
         SrcSpanAnnLW
         [GenLocated
            SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [LMatch GhcPs (LHsExpr GhcPs)]
    -> [LHsExpr GhcPs] -> [LMatch GhcPs (LHsExpr GhcPs)]
    g [LMatch GhcPs (LHsExpr GhcPs)]
    [GenLocated
       SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    bs)
      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 (LIdP (NoGhcTc GhcPs))
    CaseAlt XRec GhcPs [LPat GhcPs]
    _ (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _ [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    xs HsLocalBinds GhcPs
    _))) = [LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x | (L EpAnnCO
    _ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _ [GuardLStmt GhcPs]
    _ GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x)) <- [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    [GenLocated
       EpAnnCO (GRHS 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 (LIdP (NoGhcTc GhcPs))
    CaseAlt XRec GhcPs [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))
    -> GenLocated
         SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnA
    s1 (XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> HsMatchContext (LIdP (NoGhcTc GhcPs))
    -> XRec GhcPs [LPat GhcPs]
    -> GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    forall p body.
    XCMatch p body
    -> HsMatchContext (LIdP (NoGhcTc p))
    -> XRec p [LPat p]
    -> GRHSs p body
    -> Match p body
    Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    NoExtField
    noExtField HsMatchContext (LIdP (NoGhcTc GhcPs))
    HsMatchContext (GenLocated SrcSpanAnnN RdrName)
    forall fn. HsMatchContext fn
    CaseAlt XRec GhcPs [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 [EpAnnCO
    -> GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    -> GenLocated
         EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    forall l e. l -> e -> GenLocated l e
    L EpAnnCO
    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 XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    EpAnn GrhsAnn
    forall a. NoAnn a => a
    noAnn [GuardLStmt GhcPs]
    gs GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x) | (L EpAnnCO
    a (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
    _ [GuardLStmt GhcPs]
    gs GenLocated SrcSpanAnnA (HsExpr GhcPs)
    _), GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x) <- [GenLocated
       EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    -> [(GenLocated
           EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))),
         GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    forall a b. [a] -> [b] -> [(a, b)]
    zip [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    [GenLocated
       EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    ns [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    as] HsLocalBinds GhcPs
    b)) GenLocated
      SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
    -> [GenLocated
          SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> [GenLocated
          SrcSpanAnnA (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 [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr 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 ([GenLocated
       EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
    [GenLocated
       EpAnnCO (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
    ns) [LHsExpr GhcPs]
    [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
    xs
        g [] [] = []
        g [LMatch GhcPs (LHsExpr GhcPs)]
    _ [LHsExpr GhcPs]
    _ = String
    -> [GenLocated
          SrcSpanAnnA (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)

    -- Like needBracket, but with a special case for 'a . b . b', which was
    -- removed from haskell-src-exts-util-0.2.2.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    parent LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String]))
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> ((Bool, GenLocated SrcSpanAnnA (HsExpr GhcPs)),
        (GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x)

    -- Descend, and if something changes then add/remove brackets
    -- appropriately. Returns (suggested replacement, (refactor template, no bracket vars)),
    -- where "no bracket vars" is a list of substitution variables which, when expanded,
    -- should have the brackets stripped.
    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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    z [String]
    w, Int
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> [String]
    -> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
    f2 Int
    i LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    z [String]
    w) else (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    b, (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    z, [String]
    w))
          where ((Bool
    a, LHsExpr GhcPs
    b), (LHsExpr GhcPs
    z, [String]
    w)) = LHsExpr GhcPs -> ((Bool, LHsExpr GhcPs), (LHsExpr GhcPs, [String]))
    op LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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
    _ LHsExpr GhcPs
    y)) 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 = (LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    y, GenLocated SrcSpanAnnA (HsExpr GhcPs)
    -> (GenLocated SrcSpanAnnA (HsExpr GhcPs), [String])
    removeBracket GenLocated SrcSpanAnnA (HsExpr GhcPs)
    z)
          where
            -- If the template expr is a Var, record it so that we can remove the brackets
            -- later when expanding it. Otherwise, remove the enclosing brackets (if any).
            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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr 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))
          -- https://github.com/mpickering/apply-refact/issues/7
          | 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 LHsExpr GhcPs
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
    x