-- | Attempts to globally replace all temporary aliases for types with their
--   true values, or fails with an error for the user if a cycle is found.
fixAliasReferences :: L2.L2NamespaceID -> ModelTranslation ()
fixAliasReferences nsid = do
  initialMaps@(tmpdts, tmpunitexs) <- (\mts -> (mtsTempIDDomainType mts, mtsTempIDUnitEx mts)) <$> get
  let tmpList = map Left (M.keys tmpdts) ++ map Right (M.keys tmpunitexs)
  ((finaldts, finalunitexs), _) <- flip (flip foldM (initialMaps, S.empty)) tmpList $
    \(currentMaps, done) toFix ->
      tryResolveOneMapEntry [] currentMaps done toFix
  modifyL2Model $ \mod -> 
    transformBi (replaceTempDomainEx finaldts) .
    transformBi (replaceTempUnitEx finalunitexs) $ mod

-- | Applies a substitution map to domain type.
fixAliasReferences :: L2.L2NamespaceID -> ModelTranslation ()
fixAliasReferences nsid = do
  initialMaps@(tmpdts, tmpunitexs) <- (\mts -> (mtsTempIDDomainType mts, mtsTempIDUnitEx mts)) <$> get
  let tmpList = map Left (M.keys tmpdts) ++ map Right (M.keys tmpunitexs)
  ((finaldts, finalunitexs), _) <- flip (flip foldM (initialMaps, S.empty)) tmpList $
    \(currentMaps, done) toFix ->
      tryResolveOneMapEntry [] currentMaps done toFix
  modifyL2Model $ \mod -> 
    transformBi (replaceTempDomainEx finaldts) .
    transformBi (replaceTempUnitEx finalunitexs) $ mod

-- | Applies a substitution map to domain type.
fixAliasReferences nsid = do
  initialMaps@(tmpdts, tmpunitexs) <- (\mts -> (mtsTempIDDomainType mts, mtsTempIDUnitEx mts)) <$> get
  let tmpList = map Left (M.keys tmpdts) ++ map Right (M.keys tmpunitexs)
  ((finaldts, finalunitexs), _) <- flip (flip foldM (initialMaps, S.empty)) tmpList $
    \(currentMaps, done) toFix ->
      tryResolveOneMapEntry [] currentMaps done toFix
  modifyL2Model $ \mod -> 
    transformBi (replaceTempDomainEx finaldts) .
    transformBi (replaceTempUnitEx finalunitexs) $ mod

-- | Applies a substitution map to domain type.