invariant :: HasUnitId a => PackageIndex a -> Bool
invariant (PackageIndex pids pnames) =
     map installedUnitId (Map.elems pids)
  == sort
     [ assert pinstOk (installedUnitId pinst)
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
           pinstsOk = all (\g -> length g == 1)
                          (groupBy (equating installedUnitId) pinsts')
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]
invariant :: HasUnitId a => PackageIndex a -> Bool
invariant (PackageIndex pids pnames) =
     map installedUnitId (Map.elems pids)
  == sort
     [ assert pinstOk (installedUnitId pinst)
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
           pinstsOk = all (\g -> length g == 1)
                          (groupBy (equating installedUnitId) pinsts')
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]
invariant (PackageIndex pids pnames) =
     map installedUnitId (Map.elems pids)
  == sort
     [ assert pinstOk (installedUnitId pinst)
     | (pname, pvers)  <- Map.toList pnames
     , let pversOk = not (Map.null pvers)
     , (pver,  pinsts) <- assert pversOk $ Map.toList pvers
     , let pinsts'  = sortBy (comparing installedUnitId) pinsts
           pinstsOk = all (\g -> length g == 1)
                          (groupBy (equating installedUnitId) pinsts')
     , pinst           <- assert pinstsOk $ pinsts'
     , let pinstOk = packageName    pinst == pname
                  && packageVersion pinst == pver
     ]