toDealiaser :: Environment -> Map.Map String String
toDealiaser (Env _ _ adts aliases _) =
  let
    dealiasAdt (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [canonicalName] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    dealiasAlias (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [(canonicalName,_,_)] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    adtPairs =
      Maybe.mapMaybe dealiasAdt (Map.toList adts)

    aliasPairs =
      Maybe.mapMaybe dealiasAlias (Map.toList aliases)

    add (key,value) dict =
      Map.insertWith (\v v' -> if length v < length v' then v else v') key value dict
  in
    adtPairs ++ aliasPairs
      |> foldr add Map.empty
toDealiaser :: Environment -> Map.Map String String
toDealiaser (Env _ _ adts aliases _) =
  let
    dealiasAdt (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [canonicalName] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    dealiasAlias (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [(canonicalName,_,_)] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    adtPairs =
      Maybe.mapMaybe dealiasAdt (Map.toList adts)

    aliasPairs =
      Maybe.mapMaybe dealiasAlias (Map.toList aliases)

    add (key,value) dict =
      Map.insertWith (\v v' -> if length v < length v' then v else v') key value dict
  in
    adtPairs ++ aliasPairs
      |> foldr add Map.empty
toDealiaser (Env _ _ adts aliases _) =
  let
    dealiasAdt (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [canonicalName] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    dealiasAlias (localName, canonicalSet) =
      case Set.toList canonicalSet of
        [(canonicalName,_,_)] ->
            Just (Var.toString canonicalName, localName)
        _ ->
            Nothing

    adtPairs =
      Maybe.mapMaybe dealiasAdt (Map.toList adts)

    aliasPairs =
      Maybe.mapMaybe dealiasAlias (Map.toList aliases)

    add (key,value) dict =
      Map.insertWith (\v v' -> if length v < length v' then v else v') key value dict
  in
    adtPairs ++ aliasPairs
      |> foldr add Map.empty