replace redundant liftM7 function with applicatives

This commit is contained in:
Sergey Vinokurov 2015-05-17 23:22:56 +03:00
parent ac31e6edc2
commit 84134e1fee
2 changed files with 17 additions and 21 deletions

View File

@ -38,7 +38,7 @@ import Paths_ghc_mod as GhcMod
-- | Only package related GHC options, sufficient for things that don't need to -- | Only package related GHC options, sufficient for things that don't need to
-- access home modules -- access home modules
getGhcPkgOptions :: (MonadIO m, GmEnv m, GmLog m) getGhcPkgOptions :: (Applicative m, MonadIO m, GmEnv m, GmLog m)
=> m [(ChComponentName, [GHCOption])] => m [(ChComponentName, [GHCOption])]
getGhcPkgOptions = map (\c -> (gmcName c, gmcGhcPkgOpts c)) `liftM` getComponents getGhcPkgOptions = map (\c -> (gmcName c, gmcGhcPkgOpts c)) `liftM` getComponents
@ -54,7 +54,7 @@ helperProgs opts = Programs {
-- --
-- The Component\'s 'gmcHomeModuleGraph' will be empty and has to be resolved by -- The Component\'s 'gmcHomeModuleGraph' will be empty and has to be resolved by
-- 'resolveGmComponents'. -- 'resolveGmComponents'.
getComponents :: (MonadIO m, GmEnv m, GmLog m) getComponents :: (Applicative m, MonadIO m, GmEnv m, GmLog m)
=> m [GmComponent GMCRaw ChEntrypoint] => m [GmComponent GMCRaw ChEntrypoint]
getComponents = do getComponents = do
opt <- options opt <- options
@ -67,35 +67,31 @@ getComponents = do
) )
withCabal $ cached cradleRootDir cabalHelperCache d withCabal $ cached cradleRootDir cabalHelperCache d
cabalHelperCache :: MonadIO m => Cached m cabalHelperCache
(Programs, FilePath, (Version, String)) :: (Functor m, Applicative m, MonadIO m)
[GmComponent GMCRaw ChEntrypoint] => Cached m (Programs, FilePath, (Version, String)) [GmComponent GMCRaw ChEntrypoint]
cabalHelperCache = Cached { cabalHelperCache = Cached {
cacheFile = cabalHelperCacheFile, cacheFile = cabalHelperCacheFile,
cachedAction = \ _ (progs, root, _) _ -> cachedAction = \ _ (progs, root, _) _ ->
runQuery' progs root $ do runQuery' progs root $ do
q <- liftM7 join7 q <- join7
ghcOptions <$> ghcOptions
ghcPkgOptions <*> ghcPkgOptions
ghcSrcOptions <*> ghcSrcOptions
ghcLangOptions <*> ghcLangOptions
entrypoints <*> entrypoints
entrypoints <*> entrypoints
sourceDirs <*> sourceDirs
let cs = flip map q $ curry8 (GmComponent mempty) let cs = flip map q $ curry8 (GmComponent mempty)
return ([setupConfigPath], cs) return ([setupConfigPath], cs)
} }
where where
curry8 fn (a, (b, (c, (d, (e, (f, (g, h))))))) = fn a b c d e f g h curry8 fn (a, (b, (c, (d, (e, (f, (g, h))))))) = fn a b c d e f g h
liftM7 fn ma mb mc md me mf mg = do
a <- ma; b <- mb; c <- mc; d <- md; e <- me; f <- mf; g <- mg
return (fn a b c d e f g)
join7 a b c d e f = join' a . join' b . join' c . join' d . join' e . join' f join7 a b c d e f = join' a . join' b . join' c . join' d . join' e . join' f
join' :: Eq a => [(a,b)] -> [(a,c)] -> [(a,(b,c))] join' :: Eq a => [(a,b)] -> [(a,c)] -> [(a,(b,c))]
join' lb lc = [ (a, (b, c)) join' lb lc = [ (a, (b, c))
| (a, b) <- lb | (a, b) <- lb
, (a', c) <- lc , (a', c) <- lc
, a == a' , a == a'
] ]

View File

@ -18,7 +18,7 @@
module Language.Haskell.GhcMod.Target where module Language.Haskell.GhcMod.Target where
import Control.Arrow import Control.Arrow
import Control.Applicative ((<$>)) import Control.Applicative (Applicative, (<$>))
import Control.Monad.Reader (runReaderT) import Control.Monad.Reader (runReaderT)
import GHC import GHC
import GHC.Paths (libdir) import GHC.Paths (libdir)
@ -257,11 +257,11 @@ findCandidates scns = foldl1 Set.intersection scns
pickComponent :: Set ChComponentName -> ChComponentName pickComponent :: Set ChComponentName -> ChComponentName
pickComponent scn = Set.findMin scn pickComponent scn = Set.findMin scn
packageGhcOptions :: (MonadIO m, GmEnv m, GmLog m) => m [GHCOption] packageGhcOptions :: (Applicative m, MonadIO m, GmEnv m, GmLog m) => m [GHCOption]
packageGhcOptions = do packageGhcOptions = do
crdl <- cradle crdl <- cradle
case cradleCabalFile crdl of case cradleCabalFile crdl of
Just _ -> do Just _ ->
(Set.toList . Set.fromList . concat . map snd) `liftM` getGhcPkgOptions (Set.toList . Set.fromList . concat . map snd) `liftM` getGhcPkgOptions
Nothing -> sandboxOpts crdl Nothing -> sandboxOpts crdl