on 6948858 help sign in
98 lines 3.3 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Module where
4import Data.Data
5import Data.Binary
6import Data.List (intercalate)
7import qualified Data.Map as Map
8import Control.Applicative ((<$>), (<*>))
9import Control.Arrow (second)
11import SourceSyntax.Expression (LExpr)
12import SourceSyntax.Declaration
13import SourceSyntax.Type
14import System.FilePath (joinPath)
15import qualified Type.Type as Type
17data Module tipe var =
18 Module [String] Exports Imports [Declaration tipe var]
19 deriving (Show)
21type Exports = [String]
23type Imports = [(String, ImportMethod)]
24data ImportMethod = As String | Importing [String] | Hiding [String]
25 deriving (Eq, Ord, Show, Data, Typeable)
27data MetadataModule t v = MetadataModule {
28 names :: [String],
29 path :: FilePath,
30 exports :: [String],
31 imports :: [(String, ImportMethod)],
32 program :: LExpr t v,
33 types :: Map.Map String Type.Variable,
34 fixities :: [(Assoc, Int, String)],
35 aliases :: [(String, [String], Type)],
36 datatypes :: [ (String, [String], [(String,[Type])]) ],
37 foreignImports :: [(String, LExpr t v, String, Type)],
38 foreignExports :: [(String, String, Type)]
41type Interfaces = Map.Map String ModuleInterface
42type ADT = (String, [String], [(String,[Type])])
44data ModuleInterface = ModuleInterface {
45 iTypes :: Map.Map String Type,
46 iAdts :: [ADT]
47} deriving Show
49instance Binary ModuleInterface where
50 put modul = put (iTypes modul) >> put (iAdts modul)
51 get = ModuleInterface <$> get <*> get
54canonicalize :: String -> [ImportMethod] -> ModuleInterface -> ModuleInterface
55canonicalize prefix importMethods interface =
56 ModuleInterface { iTypes = Map.unions (map iTypes ifaces)
57 , iAdts = concatMap iAdts ifaces }
58 where
59 ifaces = map (canonicalizeHelp prefix interface) importMethods
62canonicalizeHelp :: String -> ModuleInterface -> ImportMethod -> ModuleInterface
63canonicalizeHelp prefix interface importMethod =
64 ModuleInterface {
65 iTypes = renameNames $ Map.map (renameType rename) (iTypes interface),
66 iAdts = map renameADT (iAdts interface)
67 }
68 where
69 addPrefix name = prefix ++ "." ++ name
71 newName (tipe,_,_) =
72 case importMethod of
73 As name -> (tipe, name ++ "." ++ tipe)
74 Hiding vs -> (tipe, if tipe `elem` vs then addPrefix tipe else tipe)
75 Importing vs -> (tipe, if tipe `elem` vs then tipe else addPrefix tipe)
76 newNames = Map.fromList . map newName $ iAdts interface
78 rename name = Map.findWithDefault name name newNames
80 renameADT (name, tvars, ctors) =
81 (rename name, tvars, map (second (map (renameType rename))) ctors)
83 renameNames tipes =
84 case importMethod of
85 As alias -> Map.mapKeys (\v -> alias ++ "." ++ v) tipes
86 Hiding hidens -> foldr Map.delete tipes hidens
87 Importing visibles ->
88 Map.intersection tipes (Map.fromList [ (v,()) | v <- visibles ])
90renameType :: (String -> String) -> Type -> Type
91renameType find tipe =
92 let rnm = renameType find in
93 case tipe of
94 Lambda a b -> Lambda (rnm a) (rnm b)
95 Var x -> Var x
96 Data name ts -> Data (find name) (map rnm ts)
97 EmptyRecord -> EmptyRecord
98 Record fields ext -> Record (Map.map (map rnm) fields) (rnm ext)