-
Notifications
You must be signed in to change notification settings - Fork 696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect cycles in solver #3170
Merged
Merged
Detect cycles in solver #3170
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
cabal-install/Distribution/Client/Dependency/Modular/Cycles.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{-# LANGUAGE CPP #-} | ||
module Distribution.Client.Dependency.Modular.Cycles ( | ||
detectCycles | ||
) where | ||
|
||
import Prelude hiding (cycle) | ||
import Control.Monad | ||
import Control.Monad.Reader | ||
import Data.Graph (SCC) | ||
import Data.Set (Set) | ||
import qualified Data.Graph as Gr | ||
import qualified Data.Map as Map | ||
import qualified Data.Set as Set | ||
import qualified Data.Traversable as T | ||
|
||
#if !MIN_VERSION_base(4,8,0) | ||
import Control.Applicative ((<$>)) | ||
#endif | ||
|
||
import Distribution.Client.Dependency.Modular.Dependency | ||
import Distribution.Client.Dependency.Modular.Flag | ||
import Distribution.Client.Dependency.Modular.Package | ||
import Distribution.Client.Dependency.Modular.Tree | ||
|
||
type DetectCycles = Reader (ConflictSet QPN) | ||
|
||
-- | Find any reject any solutions that are cyclic | ||
detectCycles :: Tree QGoalReasonChain -> Tree QGoalReasonChain | ||
detectCycles = (`runReader` Set.empty) . cata go | ||
where | ||
-- Most cases are simple; we just need to remember which choices we made | ||
go :: TreeF QGoalReasonChain (DetectCycles (Tree QGoalReasonChain)) -> DetectCycles (Tree QGoalReasonChain) | ||
go (PChoiceF qpn gr cs) = PChoice qpn gr <$> local (extendConflictSet $ P qpn) (T.sequence cs) | ||
go (FChoiceF qfn gr w m cs) = FChoice qfn gr w m <$> local (extendConflictSet $ F qfn) (T.sequence cs) | ||
go (SChoiceF qsn gr w cs) = SChoice qsn gr w <$> local (extendConflictSet $ S qsn) (T.sequence cs) | ||
go (GoalChoiceF cs) = GoalChoice <$> (T.sequence cs) | ||
go (FailF cs reason) = return $ Fail cs reason | ||
|
||
-- We check for cycles only if we have actually found a solution | ||
-- This minimizes the number of cycle checks we do as cycles are rare | ||
go (DoneF revDeps) = do | ||
fullSet <- ask | ||
return $ case findCycles fullSet revDeps of | ||
Nothing -> Done revDeps | ||
Just relSet -> Fail relSet CyclicDependencies | ||
|
||
-- | Given the reverse dependency map from a 'Done' node in the tree, as well | ||
-- as the full conflict set containing all decisions that led to that 'Done' | ||
-- node, check of the solution is cyclic. If it is, return the conflic set | ||
-- containing all decisions that could potentially break the cycle. | ||
findCycles :: ConflictSet QPN -> RevDepMap -> Maybe (ConflictSet QPN) | ||
findCycles fullSet revDeps = do | ||
guard $ not (null cycles) | ||
return $ relevantConflictSet (Set.fromList (concat cycles)) fullSet | ||
where | ||
cycles :: [[QPN]] | ||
cycles = [vs | Gr.CyclicSCC vs <- scc] | ||
|
||
scc :: [SCC QPN] | ||
scc = Gr.stronglyConnComp . map aux . Map.toList $ revDeps | ||
|
||
aux :: (QPN, [(comp, QPN)]) -> (QPN, QPN, [QPN]) | ||
aux (fr, to) = (fr, fr, map snd to) | ||
|
||
-- | Construct the relevant conflict set given the full conflict set that | ||
-- lead to this decision and the set of packages involved in the cycle | ||
relevantConflictSet :: Set QPN -> ConflictSet QPN -> ConflictSet QPN | ||
relevantConflictSet cycle = Set.filter isRelevant | ||
where | ||
isRelevant :: Var QPN -> Bool | ||
isRelevant (P qpn) = qpn `Set.member` cycle | ||
isRelevant (F (FN (PI qpn _i) _fn)) = qpn `Set.member` cycle | ||
isRelevant (S (SN (PI qpn _i) _sn)) = qpn `Set.member` cycle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first "any" should probably read "and".