summaryrefslogtreecommitdiff
path: root/tools/resolver-visualize/Resolver/Types.hs
blob: 9adbef93b68283b33d88fe69c86454f9a915f90c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
-- | The core data types used to represent packages, versions, and
-- other problem resolver structures.

module Resolver.Types where

import Data.ByteString.Char8(ByteString)
import Data.Set(Set)
import Data.List
import Data.Map(Map)

-- | Represents a package.
--
-- The whole resolver database is not available when parsing the log,
-- so packages can't contain the list of their versions.
data Package = Package { pkgName :: ByteString }
               deriving(Ord, Eq, Show)

-- | Represents a single version of a package.
data Version = Version { -- | The package that this version is
                         -- associated with.
                         verPkg :: Package,
                         -- | A unique string that identifies this
                         -- version among the versions of the package.
                         verName :: ByteString }
               deriving(Ord, Eq, Show)

-- | Represents a single dependency.
--
-- TODO: the resolver should state whether a dependency is "soft" when
-- logging it.  (e.g.: pkg S-> { v1 v2 })
data Dep = Dep { -- | The version that declares this dependency.
                 depSource :: Version,
                 -- | A list of versions that solve this dependency.
                 depSolvers :: [Version],
                 depIsSoft :: Bool}
         deriving(Ord, Eq, Show)


-- | Represents a choice made by the dependency resolver.
data Choice = InstallVersion { -- | The version to install.
                               choiceVer :: Version,
                               -- | The dependency that triggered this
                               -- installation.
                               --
                               -- Will be Nothing if it is not known.
                               choiceVerReason :: Maybe Dep,
                               -- | True if this choice was made to
                               -- alter the source of the dependency.
                               -- Nothing if it is not known.
                               choiceFromDepSource :: Maybe Bool }
            -- ^ Install a single version.
            -- | Leave a soft dependency unresolved.
            | BreakSoftDep { -- | The dependency that was not
                             -- resolved.
                             choiceDep :: Dep }
              deriving(Ord, Eq, Show)


-- | Represents a single solution created by the dependency resolver.
data Solution = Solution { -- | The choices made in this solution.
                           -- Each choice is mapped to an integer
                           -- representing its order in the
                           -- solution, or to Nothing if the order
                           -- is unknown.
                           solChoices :: Map Choice (Maybe Int),
                           -- | The versions that may not be
                           -- installed in this solution.
                           solForbiddenVersions :: Set Version,
                           -- | Dependencies that are broken under
                           -- this solution.
                           solBrokenDeps :: Set Dep,
                           -- | The score of this solution.
                           solScore :: Integer,
                           -- | The tier of this solution.
                           solTier  :: Tier }
              deriving(Show)

-- Custom Eq and Ord instances here to adjust the order in which the
-- parts of the Solution structure are compared.  As in the C++ code,
-- this exploits the fact that scores are very nearly unique among the
-- solutions in a particular run to avoid comparing sets most of the
-- time.
instance Eq Solution where
    sol1 == sol2 =
        solScore sol1 == solScore sol2 &&
        solTier sol1 == solTier sol2 &&
        solBrokenDeps sol1 == solBrokenDeps sol2 &&
        solForbiddenVersions sol1 == solForbiddenVersions sol2 &&
        solChoices sol1 == solChoices sol2

instance Ord Solution where
    sol1 `compare` sol2 =
        foldr combine EQ [solScore sol1 `compare` solScore sol2,
                          solTier sol1 `compare` solTier sol2,
                          solBrokenDeps sol1 `compare` solBrokenDeps sol2,
                          solForbiddenVersions sol1 `compare` solForbiddenVersions sol2,
                          solChoices sol1 `compare` solChoices sol2]
            where combine EQ o2 = o2
                  combine o1 _  = o1

maximumTierNum = 2147483647
alreadyGeneratedTierNum = maximumTierNum - 1
deferTierNum = alreadyGeneratedTierNum - 1
minimumTierNum = -2147483648
newtype Tier = Tier { tierLevels :: [Integer] } deriving(Ord, Eq)
maximumTier = Tier [maximumTierNum]
conflictTier = maximumTier
alreadyGeneratedTier = Tier [alreadyGeneratedTierNum]
deferTier = Tier [deferTierNum]
minimumTier = Tier [minimumTierNum]

-- The Show instance mainly special-cases the special tiers so they
-- get pretty-printed.
instance Show Tier where
    showsPrec _ (Tier [num]) = ('T':) . showsTierComponent num
    showsPrec _ (Tier nums)  = ("T("++) .
                               foldr (.) id (intersperse (", "++) (map showsTierComponent nums)) .
                               (')':)

-- | Display a user-friendly description of a tier number.
showsTierComponent tierNum
    | tierNum == maximumTierNum          = ("maximum"++)
    | tierNum == deferTierNum            = ("defer"++)
    | tierNum == alreadyGeneratedTierNum = ("redundant"++)
    | tierNum == minimumTierNum          = ("minimum"++)
    | otherwise                          = shows tierNum

-- | Represents a promotion to a tier.
data Promotion = Promotion { -- | The choices that produced this promotion.
                             promotionChoices :: Set Choice,
                             -- | The tier of this promotion.
                             promotionTier :: Tier }
               deriving(Ord, Eq, Show)