dependencyInconsistencies :: PackageInstalled a => PackageIndex a
                          -> [(PackageName, [(PackageId, Version)])]
dependencyInconsistencies index =
  [ (name, [ (pid,packageVersion dep) | (dep,pids) <- uses, pid <- pids])
  | (name, ipid_map) <- Map.toList inverseIndex
  , let uses = Map.elems ipid_map
  , reallyIsInconsistent (map fst uses) ]

  where -- for each PackageName,
        --   for each package with that name,
        --     the InstalledPackageInfo and the package Ids of packages
        --     that depend on it.
        inverseIndex = Map.fromListWith (Map.unionWith (\(a,b) (_,b') -> (a,b++b')))
          [ (packageName dep,
             Map.fromList [(ipid,(dep,[packageId pkg]))])
          | pkg <- allPackages index
          , ipid <- installedDepends pkg
          , Just dep <- [lookupUnitId index ipid]
          ]

        -- Added in 991e52a474e2b8280432257c1771dc474a320a30,
        -- this is a special case to handle the base 3 compatibility
        -- package which shipped with GHC 6.10 and GHC 6.12
        -- (it was removed in GHC 7.0).  Remove this when GHC 6.12
        -- goes out of our support window.
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
          let pid1 = installedUnitId p1
              pid2 = installedUnitId p2
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
        reallyIsInconsistent _ = True

-- | A rough approximation of GHC's module finder, takes a
-- 'InstalledPackageIndex' and turns it into a map from module names to their
-- source packages.  It's used to initialize the @build-deps@ field in @cabal
-- init@.
dependencyInconsistencies :: PackageInstalled a => PackageIndex a
                          -> [(PackageName, [(PackageId, Version)])]
dependencyInconsistencies index =
  [ (name, [ (pid,packageVersion dep) | (dep,pids) <- uses, pid <- pids])
  | (name, ipid_map) <- Map.toList inverseIndex
  , let uses = Map.elems ipid_map
  , reallyIsInconsistent (map fst uses) ]

  where -- for each PackageName,
        --   for each package with that name,
        --     the InstalledPackageInfo and the package Ids of packages
        --     that depend on it.
        inverseIndex = Map.fromListWith (Map.unionWith (\(a,b) (_,b') -> (a,b++b')))
          [ (packageName dep,
             Map.fromList [(ipid,(dep,[packageId pkg]))])
          | pkg <- allPackages index
          , ipid <- installedDepends pkg
          , Just dep <- [lookupUnitId index ipid]
          ]

        -- Added in 991e52a474e2b8280432257c1771dc474a320a30,
        -- this is a special case to handle the base 3 compatibility
        -- package which shipped with GHC 6.10 and GHC 6.12
        -- (it was removed in GHC 7.0).  Remove this when GHC 6.12
        -- goes out of our support window.
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
          let pid1 = installedUnitId p1
              pid2 = installedUnitId p2
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
        reallyIsInconsistent _ = True

-- | A rough approximation of GHC's module finder, takes a
-- 'InstalledPackageIndex' and turns it into a map from module names to their
-- source packages.  It's used to initialize the @build-deps@ field in @cabal
-- init@.
dependencyInconsistencies index =
  [ (name, [ (pid,packageVersion dep) | (dep,pids) <- uses, pid <- pids])
  | (name, ipid_map) <- Map.toList inverseIndex
  , let uses = Map.elems ipid_map
  , reallyIsInconsistent (map fst uses) ]

  where -- for each PackageName,
        --   for each package with that name,
        --     the InstalledPackageInfo and the package Ids of packages
        --     that depend on it.
        inverseIndex = Map.fromListWith (Map.unionWith (\(a,b) (_,b') -> (a,b++b')))
          [ (packageName dep,
             Map.fromList [(ipid,(dep,[packageId pkg]))])
          | pkg <- allPackages index
          , ipid <- installedDepends pkg
          , Just dep <- [lookupUnitId index ipid]
          ]

        -- Added in 991e52a474e2b8280432257c1771dc474a320a30,
        -- this is a special case to handle the base 3 compatibility
        -- package which shipped with GHC 6.10 and GHC 6.12
        -- (it was removed in GHC 7.0).  Remove this when GHC 6.12
        -- goes out of our support window.
        reallyIsInconsistent :: PackageInstalled a => [a] -> Bool
        reallyIsInconsistent []       = False
        reallyIsInconsistent [_p]     = False
        reallyIsInconsistent [p1, p2] =
          let pid1 = installedUnitId p1
              pid2 = installedUnitId p2
          in pid1 `notElem` installedDepends p2
          && pid2 `notElem` installedDepends p1
        reallyIsInconsistent _ = True

-- | A rough approximation of GHC's module finder, takes a
-- 'InstalledPackageIndex' and turns it into a map from module names to their
-- source packages.  It's used to initialize the @build-deps@ field in @cabal
-- init@.