summaryrefslogtreecommitdiff
path: root/tools/resolver-visualize/Resolver/Types.hs
blob: 9b5fc088f23c8a6d889a8f3bb8a5bdf26015b581 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- | The core data types used to represent packages, versions, and
-- other problem resolver structures.

module Resolver.Types(
                      Package(..),
                      Version(..),
                      Dep(..),
                      ChoiceDepInfo(..),
                      Choice(..),
                      Solution(..),
                      FastSolution(fastSol, fastSolHash),
                      makeFastSolution,
                      maximumTierNum,
                      alreadyGeneratedTierNum,
                      deferTierNum,
                      minimumTierNum,
                      Tier(..),
                      maximumTier,
                      conflictTier,
                      alreadyGeneratedTier,
                      deferTier,
                      minimumTier,
                      showsTierComponent,
                      Promotion(..),
                      Hashed(..),
                      makeHashed,
                      FastPromotion,
                      fastPromotionHash,
                      fastPromotion,
                      makeFastPromotion
                     ) where

import Data.ByteString.Char8(ByteString)
import Data.HashTable(hashString)
import Data.Int(Int32)
import Data.Set(Set)
import qualified Data.Set as Set
import Data.List
import Data.Map(Map)
import qualified Data.Map as 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)

data ChoiceDepInfo = Unscoped   { depInfoDep :: Dep }
                   | Scoped     { depInfoDep :: Dep }
                   | FromSource { depInfoDep :: Dep }
              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.
                               choiceVerDepInfo :: Maybe ChoiceDepInfo }
            -- ^ 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,
                          Set.size (solBrokenDeps sol1) `compare` Set.size (solBrokenDeps sol2),
                          Set.size (solForbiddenVersions sol1) `compare` Set.size (solForbiddenVersions sol2),
                          Map.size (solChoices sol1) `compare` Map.size (solChoices sol2),
                          solBrokenDeps sol1 `compare` solBrokenDeps sol2,
                          solForbiddenVersions sol1 `compare` solForbiddenVersions sol2,
                          solChoices sol1 `compare` solChoices sol2]
            where combine EQ o2 = o2
                  combine o1 _  = o1

-- | Used to insert solutions into hash tables reasonably quickly.
--
-- Created by hashing the output of the Show instance.
data FastSolution = FastSolution { fastSol :: Solution, fastSolHash :: Int32 }
                  deriving(Show)

instance Eq FastSolution where
    fs1 == fs2 = fastSolHash fs1 == fastSolHash fs2 &&
                 fastSol fs1 == fastSol fs2

instance Ord FastSolution where
    fs1 `compare` fs2 = case fastSolHash fs1 `compare` fastSolHash fs2 of
                          EQ -> fastSol fs1 `compare` fastSol fs2
                          o  -> o

makeFastSolution :: Solution -> FastSolution
makeFastSolution sol = FastSolution { fastSol = sol,
                                      fastSolHash = hashString $ show sol }

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 (first:_))
        | first == maximumTierNum          = ("T(conflict)"++)
        | first == deferTierNum            = ("T(defer)"++)
        | first == alreadyGeneratedTierNum = ("T(redundant)"++)
    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 == 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(Show)

instance Eq Promotion where
    p1 == p2 =
        promotionTier p1 == promotionTier p2 &&
        Set.size (promotionChoices p1) == Set.size (promotionChoices p2) &&
        promotionChoices p1 == promotionChoices p2

instance Ord Promotion where
    p1 `compare` p2 =
        foldr combine EQ [promotionTier p1 `compare` promotionTier p2,
                          Set.size (promotionChoices p1) `compare` Set.size (promotionChoices p2),
                          promotionChoices p1 `compare` promotionChoices p2]
        where combine EQ o2 = o2
              combine o1 _  = o1

data Hashed a = Hashed { hashedVal :: a,
                         hashedHash :: Int32 }

instance Eq a => Eq (Hashed a) where
    h1 == h2 =
        hashedHash h1 == hashedHash h2 &&
        hashedVal h1 == hashedVal h2

instance Ord a => Ord (Hashed a) where
    h1 `compare` h2 =
        case hashedHash h1 `compare` hashedHash h2 of
          EQ -> hashedVal h1 `compare` hashedVal h2
          o  -> o

-- | Use a value's Show instance to hash it.
makeHashed :: Show a => a -> Hashed a
makeHashed a = Hashed { hashedVal = a,
                        hashedHash = hashString $ show a }

type FastPromotion = Hashed Promotion

fastPromotion :: FastPromotion -> Promotion
fastPromotion = hashedVal

fastPromotionHash :: FastPromotion -> Int32
fastPromotionHash = hashedHash

makeFastPromotion :: Promotion -> FastPromotion
makeFastPromotion = makeHashed