@@ -84,10 +84,16 @@ data ImportEnvironment = ImportEnvironment
8484--
8585updateExportedModule :: ExportEnvironment -> ModuleName -> (Exports -> Either String Exports) -> Either String ExportEnvironment
8686updateExportedModule env mn update = do
87− let exports = fromMaybe (Exports S.empty S.empty S.empty) $ mn `M.lookup` env
87+ let exports = fromMaybe (error "Module was undefined in updateExportedModule") $ mn `M.lookup` env
8888 exports' <- update exports
8989 return $ M.insert mn exports' env
9090
91+-- |
92+-- Adds an empty module to an ExportEnvironment.
93+--
94+addEmptyModule :: ExportEnvironment -> ModuleName -> ExportEnvironment
95+addEmptyModule env name = M.insert name (Exports S.empty S.empty S.empty) env
96+
9197-- |
9298-- Adds a type belonging to a module to the export environment.
9399--
@@ -144,26 +150,28 @@ rethrowForModule (Module mn _) = flip catchError $ \e -> throwError ("Error in m
144150--
145151renameInModule :: ImportEnvironment -> Module -> Either String Module
146152renameInModule imports (Module mn decls) =
147− Module mn <$> mapM updateDecl decls >>= everywhereM (mkM updateType `extM` updateValue `extM` updateBinder)
153+ Module mn <$> mapM updateDecl decls >>= everywhereM (mkM updateType `extM` updateValue `extM` updateBinder `extM` updateVars)
148154 where
149155 updateDecl (TypeInstanceDeclaration cs (Qualified Nothing cn) ts ds) =
150− TypeInstanceDeclaration <$> updateConstraints cs <*> updateClassName cn <*> pure ts <*> updateVars ds
151− updateDecl (ValueDeclaration name bs grd val) =
152− ValueDeclaration name <$> updateVars bs <*> updateVars grd <*> updateVars val
156+ TypeInstanceDeclaration <$> updateConstraints cs <*> updateClassName cn <*> pure ts <*> pure ds
153157 updateDecl d = return d
154158
155− updateVars :: (Data d) => d -> Either String d
156− updateVars = everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders)
159+ updateVars :: Declaration -> Either String Declaration
160+ updateVars (ValueDeclaration name [] Nothing val) =
161+ ValueDeclaration name [] Nothing <$> everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders) val
157162 where
158163 bindFunctionArgs bound (Abs (Left arg) val) = return (arg : bound, Abs (Left arg) val)
159164 bindFunctionArgs bound (Var (Qualified Nothing ident)) | ident `notElem` bound = (,) bound <$> (Var <$> updateValueName ident)
160165 bindFunctionArgs bound other = return (bound, other)
161166 bindBinders :: [Ident] -> CaseAlternative -> Either String ([Ident], CaseAlternative)
162167 bindBinders bound c@(CaseAlternative bs _ _) = return (binderNames bs ++ bound, c)
168+ updateVars (ValueDeclaration name _ _ _) = error $ "Binders should have been desugared in " ++ show name
169+ updateVars other = return other
163170
164171 updateValue (Constructor (Qualified Nothing nm)) =
165172 Constructor <$> updateDataConstructorName nm
166173 updateValue v = return v
174+
167175 updateBinder (ConstructorBinder (Qualified Nothing nm) b) =
168176 ConstructorBinder <$> updateDataConstructorName nm <*> pure b
169177 updateBinder v = return v
@@ -189,7 +197,7 @@ renameInModule imports (Module mn decls) =
189197findExports :: [Module] -> Either String ExportEnvironment
190198findExports = foldM addModule M.empty
191199 where
192− addModule env m@(Module mn ds) = rethrowForModule m $ foldM (addDecl mn) env ds
200+ addModule env m@(Module mn ds) = rethrowForModule m $ foldM (addDecl mn) (addEmptyModule env mn) ds
193201 addDecl mn env (TypeClassDeclaration tcn _ ds) = do
194202 env' <- addTypeClass env mn tcn
195203 foldM (\env'' (TypeDeclaration name _) -> addValue env'' mn name) env' ds
@@ -226,7 +234,7 @@ resolveImports env (Module currentModule decls) =
226234 scope :: M.Map ModuleName (Maybe ExplicitImports)
227235 scope = M.insert currentModule Nothing (findImports decls)
228236 resolveImport' imp (mn, i) = do
229− let m = fromMaybe (Exports S.empty S.empty S.empty) $ mn `M.lookup` env
237+ m <- maybe (throwError $ "Cannot import unknown module '" ++ show mn ++ "'") return $ mn `M.lookup` env
230238 resolveImport currentModule mn m imp i
231239
232240-- |