@@ -3,7 +3,7 @@ module Transform.Canonicalize.Setup (environment) where
33
44import Control.Arrow (first)
55import Control.Monad (foldM)
6−import qualified Data.Either as Either
6+import Control.Monad.Error (throwError)
77import qualified Data.Graph as Graph
88import qualified Data.List as List
99import qualified Data.Map as Map
@@ -22,7 +22,7 @@ import Text.PrettyPrint as P
2222import Transform.Canonicalize.Environment as Env
2323import qualified Transform.Canonicalize.Type as Canonicalize
2424
25−environment :: Module.Interfaces -> Module.ValidModule -> Either [Doc] Environment
25+environment :: Module.Interfaces -> Module.ValidModule -> Canonicalizer [Doc] Environment
2626environment interfaces modul@(Module.Module _ _ _ imports decls) =
2727 do () <- allImportsAvailable
2828 nonLocalEnv <- foldM (addImports interfaces) builtIns imports
@@ -30,11 +30,11 @@ environment interfaces modul@(Module.Module _ _ _ imports decls) =
3030 (aliases, env) = List.foldl' (addDecl moduleName) ([], nonLocalEnv) decls
3131 addTypeAliases moduleName aliases env
3232 where
33− allImportsAvailable :: Either [Doc] ()
33+ allImportsAvailable :: Canonicalizer [Doc] ()
3434 allImportsAvailable =
3535 case filter (not . found) modules of
36− [] -> Right ()
37− missings -> Left [ P.text (missingModuleError missings) ]
36+ [] -> return ()
37+ missings -> throwError [ P.text (missingModuleError missings) ]
3838 where
3939 modules = map fst imports
4040
@@ -47,7 +47,7 @@ environment interfaces modul@(Module.Module _ _ _ imports decls) =
4747 , "flag to detect modules you have written." ]
4848
4949addImports :: Module.Interfaces -> Environment -> (String, Module.ImportMethod)
50− -> Either [Doc] Environment
50+ -> Canonicalizer [Doc] Environment
5151addImports interfaces environ (name,method)
5252 | List.isPrefixOf "Native." name = return environ
5353 | otherwise =
@@ -81,12 +81,12 @@ addImports interfaces environ (name,method)
8181 ctors = concatMap (map (pair . fst) . snd . snd) (Map.toList (iAdts interface))
8282
8383addValue :: String -> Module.Interface -> Environment -> Var.Value
84− -> Either [Doc] Environment
84+ -> Canonicalizer [Doc] Environment
8585addValue name interface env value =
8686 let insert' x = insert x (Var.Canonical (Var.Module name) x)
8787 msg x = "Import Error: Could not import value '" ++ name ++ "." ++ x ++
8888 "'.\n It is not exported by module " ++ name ++ "."
89− notFound x = Left [ P.text (msg x) ]
89+ notFound x = throwError [ P.text (msg x) ]
9090 in
9191 case value of
9292 Var.Value x
@@ -136,31 +136,31 @@ node name tvars alias = ((name, tvars, alias), name, edges alias)
136136 Type.Record fs ext -> maybe [] edges ext ++ concatMap (edges . snd) fs
137137 Type.Aliased _ t -> edges t
138138
139−addTypeAliases :: String -> [Node] -> Environment -> Either [Doc] Environment
139+addTypeAliases :: String -> [Node] -> Environment -> Canonicalizer [Doc] Environment
140140addTypeAliases moduleName nodes environ =
141141 foldM addTypeAlias environ (Graph.stronglyConnComp nodes)
142142 where
143143 addTypeAlias :: Environment -> Graph.SCC (String, [String], Type.RawType)
144− -> Either [Doc] Environment
144+ -> Canonicalizer [Doc] Environment
145145 addTypeAlias env scc =
146146 case Graph.flattenSCC scc of
147147 [(name, tvars, alias)] ->
148− do alias' <- Either.either throw return (Canonicalize.tipe env alias)
148+ do alias' <- Env.onError throw (Canonicalize.tipe env alias)
149149 let value = (Var.Canonical (Var.Module moduleName) name, tvars, alias')
150150 return $ env { _aliases = insert name value (_aliases env) }
151151 where
152152 throw err =
153153 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 ]
155155
156156 aliases ->
157− Left [ P.vcat [ P.text (eightyCharLines 0 msg1)
158− , indented (map typeAlias aliases)
159− , P.text (eightyCharLines 0 msg2)
160− , indented (map datatype aliases)
161− , P.text (eightyCharLines 0 msg3)
162− ]
163− ]
157+ throwError [ P.vcat [ P.text (eightyCharLines 0 msg1)
158+ , indented (map typeAlias aliases)
159+ , P.text (eightyCharLines 0 msg2)
160+ , indented (map datatype aliases)
161+ , P.text (eightyCharLines 0 msg3)
162+ ]
163+ ]
164164
165165 typeAlias (n,ts,t) = D.TypeAlias n ts t
166166 datatype (n,ts,t) = D.Datatype n ts [(n,[t])]