@@ -6,8 +6,9 @@ import Data.Binary
66import Data.List (intercalate)
77import qualified Data.Map as Map
88import Control.Applicative ((<$>), (<*>))
9+import Control.Arrow (second)
910
10−import SourceSyntax.Expression
11+import SourceSyntax.Expression (LExpr)
1112import SourceSyntax.Declaration
1213import SourceSyntax.Type
1314import System.FilePath (joinPath)
@@ -46,4 +47,35 @@ data ModuleInterface = ModuleInterface {
4647
4748instance Binary ModuleInterface where
4849 put modul = put (iTypes modul) >> put (iAdts modul)
49− get = ModuleInterface <$> get <*> get
50+ get = ModuleInterface <$> get <*> get
51+
52+
53+canonicalize :: String -> ImportMethod -> ModuleInterface -> ModuleInterface
54+canonicalize prefix importMethod interface =
55+ let addPrefix name = prefix ++ "." ++ name
56+
57+ newName (tipe,_,_) =
58+ case importMethod of
59+ As name -> (tipe, name ++ "." ++ tipe)
60+ Hiding vs -> (tipe, if tipe `elem` vs then addPrefix tipe else tipe)
61+ Importing vs -> (tipe, if tipe `elem` vs then tipe else addPrefix tipe)
62+ newNames = Map.fromList . map newName $ iAdts interface
63+
64+ rename name = Map.findWithDefault name name newNames
65+
66+ renameADT (name, tvars, ctors) =
67+ (rename name, tvars, map (second (map (renameType rename))) ctors)
68+ in ModuleInterface {
69+ iTypes = Map.map (renameType rename) (iTypes interface),
70+ iAdts = map renameADT (iAdts interface)
71+ }
72+
73+renameType :: (String -> String) -> Type -> Type
74+renameType find tipe =
75+ let rnm = renameType find in
76+ case tipe of
77+ Lambda a b -> Lambda (rnm a) (rnm b)
78+ Var x -> Var x
79+ Data name ts -> Data (find name) (map rnm ts)
80+ EmptyRecord -> EmptyRecord
81+ Record fields ext -> Record (Map.map (map rnm) fields) (rnm ext)