+ where maybe_hide p
+ | pkgName (sourcePackageId p) == name = p {exposed=False}
+ | otherwise = p
+
+
+selectPackages :: (PackageConfig -> Bool) -> [PackageConfig]
+ -> UnusablePackages
+ -> Either [(PackageConfig, UnusablePackageReason)]
+ ([PackageConfig], [PackageConfig])
+selectPackages matches pkgs unusable
+ = let
+ (ps,rest) = partition matches pkgs
+ reasons = [ (p, lookupFM unusable (installedPackageId p))
+ | p <- ps ]
+ in
+ if all (isJust.snd) reasons
+ then Left [ (p, reason) | (p,Just reason) <- reasons ]
+ else Right (sortByVersion [ p | (p,Nothing) <- reasons ], rest)
+
+-- A package named on the command line can either include the
+-- version, or just the name if it is unambiguous.
+matchingStr :: String -> PackageConfig -> Bool
+matchingStr str p
+ = str == display (sourcePackageId p)
+ || str == display (pkgName (sourcePackageId p))
+
+matchingId :: String -> PackageConfig -> Bool
+matchingId str p = InstalledPackageId str == installedPackageId p
+
+sortByVersion :: [InstalledPackageInfo_ m] -> [InstalledPackageInfo_ m]
+sortByVersion = sortBy (flip (comparing (pkgVersion.sourcePackageId)))
+
+comparing :: Ord a => (t -> a) -> t -> t -> Ordering
+comparing f a b = f a `compare` f b
+
+packageFlagErr :: PackageFlag
+ -> [(PackageConfig, UnusablePackageReason)]
+ -> IO a
+packageFlagErr flag reasons = ghcError (CmdLineError (showSDoc $ err))
+ where err = text "cannot satisfy " <> ppr_flag <>
+ (if null reasons then empty else text ": ") $$
+ nest 4 (ppr_reasons $$
+ text "(use -v for more information)")
+ ppr_flag = case flag of
+ IgnorePackage p -> text "-ignore-package " <> text p
+ HidePackage p -> text "-hide-package " <> text p
+ ExposePackage p -> text "-package " <> text p
+ ExposePackageId p -> text "-package-id " <> text p
+ ppr_reasons = vcat (map ppr_reason reasons)
+ ppr_reason (p, reason) = pprReason (pprIPkg p <+> text "is") reason
+
+-- -----------------------------------------------------------------------------
+-- Hide old versions of packages
+
+--
+-- hide all packages for which there is also a later version
+-- that is already exposed. This just makes it non-fatal to have two
+-- versions of a package exposed, which can happen if you install a
+-- later version of a package in the user database, for example.
+--
+hideOldPackages :: DynFlags -> [PackageConfig] -> IO [PackageConfig]
+hideOldPackages dflags pkgs = mapM maybe_hide pkgs
+ where maybe_hide p