@@ -3,7 +3,7 @@ module Transform.Canonicalize.Setup (environment) where
4import Control.Arrow (first)
4import Control.Arrow (first)
5import Control.Monad (foldM)
5import Control.Monad (foldM)
6−import qualified Data.Either as Either
6+import Control.Monad.Error (throwError)
7import qualified Data.Graph as Graph
7import qualified Data.Graph as Graph
8import qualified Data.List as List
8import qualified Data.List as List
9import qualified Data.Map as Map
9import qualified Data.Map as Map
@@ -22,7 +22,7 @@ import Text.PrettyPrint as P
22import Transform.Canonicalize.Environment as Env
22import Transform.Canonicalize.Environment as Env
23import qualified Transform.Canonicalize.Type as Canonicalize
23import qualified Transform.Canonicalize.Type as Canonicalize
25−environment :: Module.Interfaces -> Module.ValidModule -> Either [Doc] Environment
25+environment :: Module.Interfaces -> Module.ValidModule -> Canonicalizer [Doc] Environment
26environment interfaces modul@(Module.Module _ _ _ imports decls) =
26environment interfaces modul@(Module.Module _ _ _ imports decls) =
27 do () <- allImportsAvailable
27 do () <- allImportsAvailable
28 nonLocalEnv <- foldM (addImports interfaces) builtIns imports
28 nonLocalEnv <- foldM (addImports interfaces) builtIns imports
@@ -30,11 +30,11 @@ environment interfaces modul@(Module.Module _ _ _ imports decls) =
30 (aliases, env) = List.foldl' (addDecl moduleName) ([], nonLocalEnv) decls
30 (aliases, env) = List.foldl' (addDecl moduleName) ([], nonLocalEnv) decls
31 addTypeAliases moduleName aliases env
31 addTypeAliases moduleName aliases env
33− allImportsAvailable :: Either [Doc] ()
33+ allImportsAvailable :: Canonicalizer [Doc] ()
34 allImportsAvailable =
34 allImportsAvailable =
35 case filter (not . found) modules of
35 case filter (not . found) modules of
36− [] -> Right ()
36+ [] -> return ()
37− missings -> Left [ P.text (missingModuleError missings) ]
37+ missings -> throwError [ P.text (missingModuleError missings) ]
39 modules = map fst imports
39 modules = map fst imports
@@ -47,7 +47,7 @@ environment interfaces modul@(Module.Module _ _ _ imports decls) =
47 , "flag to detect modules you have written." ]
47 , "flag to detect modules you have written." ]
49addImports :: Module.Interfaces -> Environment -> (String, Module.ImportMethod)
49addImports :: Module.Interfaces -> Environment -> (String, Module.ImportMethod)
50− -> Either [Doc] Environment
50+ -> Canonicalizer [Doc] Environment
51addImports interfaces environ (name,method)
51addImports interfaces environ (name,method)
52 | List.isPrefixOf "Native." name = return environ
52 | List.isPrefixOf "Native." name = return environ
53 | otherwise =
53 | otherwise =
@@ -81,12 +81,12 @@ addImports interfaces environ (name,method)
81 ctors = concatMap (map (pair . fst) . snd . snd) (Map.toList (iAdts interface))
81 ctors = concatMap (map (pair . fst) . snd . snd) (Map.toList (iAdts interface))
83addValue :: String -> Module.Interface -> Environment -> Var.Value
83addValue :: String -> Module.Interface -> Environment -> Var.Value
84− -> Either [Doc] Environment
84+ -> Canonicalizer [Doc] Environment
85addValue name interface env value =
85addValue name interface env value =
86 let insert' x = insert x (Var.Canonical (Var.Module name) x)
86 let insert' x = insert x (Var.Canonical (Var.Module name) x)
87 msg x = "Import Error: Could not import value '" ++ name ++ "." ++ x ++
87 msg x = "Import Error: Could not import value '" ++ name ++ "." ++ x ++
88 "'.\n It is not exported by module " ++ name ++ "."
88 "'.\n It is not exported by module " ++ name ++ "."
89− notFound x = Left [ P.text (msg x) ]
89+ notFound x = throwError [ P.text (msg x) ]
91 case value of
91 case value of
92 Var.Value x
92 Var.Value x
@@ -136,31 +136,31 @@ node name tvars alias = ((name, tvars, alias), name, edges alias)
136 Type.Record fs ext -> maybe [] edges ext ++ concatMap (edges . snd) fs
136 Type.Record fs ext -> maybe [] edges ext ++ concatMap (edges . snd) fs
137 Type.Aliased _ t -> edges t
137 Type.Aliased _ t -> edges t
139−addTypeAliases :: String -> [Node] -> Environment -> Either [Doc] Environment
139+addTypeAliases :: String -> [Node] -> Environment -> Canonicalizer [Doc] Environment
140addTypeAliases moduleName nodes environ =
140addTypeAliases moduleName nodes environ =
141 foldM addTypeAlias environ (Graph.stronglyConnComp nodes)
141 foldM addTypeAlias environ (Graph.stronglyConnComp nodes)
143 addTypeAlias :: Environment -> Graph.SCC (String, [String], Type.RawType)
143 addTypeAlias :: Environment -> Graph.SCC (String, [String], Type.RawType)
144− -> Either [Doc] Environment
144+ -> Canonicalizer [Doc] Environment
145 addTypeAlias env scc =
145 addTypeAlias env scc =
146 case Graph.flattenSCC scc of
146 case Graph.flattenSCC scc of
147 [(name, tvars, alias)] ->
147 [(name, tvars, alias)] ->
148− do alias' <- Either.either throw return (Canonicalize.tipe env alias)
148+ do alias' <- Env.onError throw (Canonicalize.tipe env alias)
149 let value = (Var.Canonical (Var.Module moduleName) name, tvars, alias')
149 let value = (Var.Canonical (Var.Module moduleName) name, tvars, alias')
150 return $ env { _aliases = insert name value (_aliases env) }
150 return $ env { _aliases = insert name value (_aliases env) }
152 throw err =
152 throw err =
153 let msg = "Problem with type alias '" ++ name ++ "':"
153 let msg = "Problem with type alias '" ++ name ++ "':"
154− in Left [ P.vcat [ P.text msg, P.text err ] ]
154+ in P.vcat [ P.text msg, P.text err ]
156 aliases ->
156 aliases ->
157− Left [ P.vcat [ P.text (eightyCharLines 0 msg1)
157+ throwError [ P.vcat [ P.text (eightyCharLines 0 msg1)
158− , indented (map typeAlias aliases)
158+ , indented (map typeAlias aliases)
159− , P.text (eightyCharLines 0 msg2)
159+ , P.text (eightyCharLines 0 msg2)
160− , indented (map datatype aliases)
160+ , indented (map datatype aliases)
161− , P.text (eightyCharLines 0 msg3)
161+ , P.text (eightyCharLines 0 msg3)
165 typeAlias (n,ts,t) = D.TypeAlias n ts t
165 typeAlias (n,ts,t) = D.TypeAlias n ts t
166 datatype (n,ts,t) = D.Datatype n ts [(n,[t])]
166 datatype (n,ts,t) = D.Datatype n ts [(n,[t])]