@@ -6,7 +6,6 @@ import Control.Arrow (first)
66import Control.Monad (foldM)
77import qualified Data.Either as Either
88import qualified Data.List as List
9−import Data.Map ((!))
109import qualified Data.Map as Map
1110import qualified Data.Traversable as T
1211
@@ -24,7 +23,7 @@ import AST.PrettyPrint (pretty, commaSep)
2423import qualified AST.Pattern as P
2524import Text.PrettyPrint as P
2625
27−import Transform.Canonicalize.Environment
26+import Transform.Canonicalize.Environment as Env
2827import Transform.Canonicalize.Type
2928import Transform.Canonicalize.Variable (variable, pvar)
3029import qualified Transform.SortDefinitions as Transform
@@ -33,23 +32,26 @@ import qualified Transform.Declaration as Transform
3332environment :: Interfaces -> Module.ValidModule -> Either [Doc] Environment
3433environment interfaces (Module.Module _ _ _ ims decls) =
3534 do () <- allImportsAvailable interfaces ims
36− nonLocalEnv <- foldM (toEnv interfaces) builtIns ims
37− foldM (flip addDecl) nonLocalEnv decls
35+ nonLocalEnv <- foldM (toEnv interfaces) Env.builtIns ims
36+ foldM addDecl nonLocalEnv decls
3837
3938toEnv :: Interfaces -> Environment -> (String, ImportMethod) -> Either [Doc] Environment
40−toEnv interfaces environ (name,method) =
41− case method of
42− Module.As name' ->
43− return (env (name' ++ "."))
44−
45− Module.Open (Var.Listing vs open)
46− | open -> return (env "")
47− | otherwise -> foldM (addValue name interface) environ vs
39+toEnv interfaces environ (name,method)
40+ | List.isPrefixOf "Native." name = return environ
41+ | otherwise =
42+ case method of
43+ Module.As name' ->
44+ return (updateEnviron (name' ++ "."))
45+
46+ Module.Open (Var.Listing vs open)
47+ | open -> return (updateEnviron "")
48+ | otherwise -> foldM (addValue name interface) environ vs
4849 where
49− interface = interfaces ! name
50+ interface = (Map.!) interfaces name
5051
51− env prefix =
52+ updateEnviron prefix =
5253 let dict' = dict . map (first (prefix++)) in
54+ merge environ $
5355 Env { _values = dict' $ map pair (Map.keys (iTypes interface)) ++ ctors
5456 , _adts = dict' $ map pair (Map.keys (iAdts interface))
5557 , _aliases = dict' $ map alias (Map.toList (iAliases interface))
@@ -104,8 +106,8 @@ addValue name interface env value =
104106 [] -> return names
105107 c:_ -> notFound c
106108
107−addDecl :: D.ValidDecl -> Environment -> Either [Doc] Environment
108−addDecl decl env =
109+addDecl :: Environment -> D.ValidDecl -> Either [Doc] Environment
110+addDecl env decl =
109111 case decl of
110112 D.Definition (Valid.Definition pattern _ _) ->
111113 return $ env
@@ -247,12 +249,14 @@ declaration env decl =
247249
248250allImportsAvailable :: Interfaces -> [(String,ImportMethod)] -> Either [Doc] ()
249251allImportsAvailable interfaces imports =
250− case filter (`Map.notMember` interfaces) modules of
252+ case filter (not . found) modules of
251253 [] -> Right ()
252254 missings -> Left [ P.text (missingModuleError missings) ]
253255 where
254256 modules = map fst imports
255257
258+ found m = Map.member m interfaces || List.isPrefixOf "Native." m
259+
256260 missingModuleError missings =
257261 concat [ "The following imports were not found: "
258262 , List.intercalate ", " missings