@@ -6,9 +6,10 @@ import qualified Data.Set as Set
66import Control.Arrow (second)
77import Control.Applicative ((<$>),(<*>))
88import qualified Control.Monad as Monad
9−import Control.Monad.Error (ErrorT, runErrorT)
9+import Control.Monad.Error
1010import Control.Monad.State
1111import Data.Traversable (traverse)
12+import qualified Text.PrettyPrint as PP
1213
1314import SourceSyntax.Location as Loc
1415import SourceSyntax.Pattern (Pattern(PVar))
@@ -22,7 +23,7 @@ import qualified Type.Constrain.Pattern as Pattern
2223import qualified Transform.SortDefinitions as SD
2324
2425
25−constrain :: Env.Environment -> LExpr a b -> Type -> IO TypeConstraint
26+constrain :: Env.Environment -> LExpr a b -> Type -> ErrorT [PP.Doc] IO TypeConstraint
2627constrain env (L span expr) tipe =
2728 let list t = Env.get env Env.types "_List" <| t
2829 and = L span . CAnd
@@ -32,7 +33,7 @@ constrain env (L span expr) tipe =
3233 clet schemes c = L span (CLet schemes c)
3334 in
3435 case expr of
35− Literal lit -> Literal.constrain env span lit tipe
36+ Literal lit -> liftIO $ Literal.constrain env span lit tipe
3637
3738 Var name | name == saveEnvName -> return (L span CSaveEnv)
3839 | otherwise -> return (name <? tipe)
@@ -87,7 +88,7 @@ constrain env (L span expr) tipe =
8788 and . (:) ce <$> mapM branch branches
8889
8990 Data name exprs ->
90− do vars <- forM exprs $ \_ -> var Flexible
91+ do vars <- forM exprs $ \_ -> liftIO (var Flexible)
9192 let pairs = zip exprs (map VarN vars)
9293 (ctipe, cs) <- Monad.foldM step (tipe,true) (reverse pairs)
9394 return $ ex vars (cs /\ name <? ctipe)
@@ -114,11 +115,11 @@ constrain env (L span expr) tipe =
114115
115116 Modify e fields ->
116117 exists $ \t -> do
117− oldVars <- forM fields $ \_ -> var Flexible
118+ oldVars <- forM fields $ \_ -> liftIO (var Flexible)
118119 let oldFields = SrcT.fieldMap (zip (map fst fields) (map VarN oldVars))
119120 cOld <- ex oldVars <$> constrain env e (record oldFields t)
120121
121− newVars <- forM fields $ \_ -> var Flexible
122+ newVars <- forM fields $ \_ -> liftIO (var Flexible)
122123 let newFields = SrcT.fieldMap (zip (map fst fields) (map VarN newVars))
123124 let cNew = tipe === record newFields t
124125
@@ -127,7 +128,7 @@ constrain env (L span expr) tipe =
127128 return $ cOld /\ ex newVars (and (cNew : cs))
128129
129130 Record fields ->
130− do vars <- forM fields $ \_ -> var Flexible
131+ do vars <- forM fields $ \_ -> liftIO (var Flexible)
131132 cs <- zipWithM (constrain env) (map snd fields) (map VarN vars)
132133 let fields' = SrcT.fieldMap (zip (map fst fields) (map VarN vars))
133134 recordType = record fields' (TermN EmptyRecord1)
@@ -146,15 +147,16 @@ constrain env (L span expr) tipe =
146147 (clet [Scheme rqs fqs (clet [monoscheme header] c2) header ]
147148 (c1 /\ c))
148149
150+
149151constrainDef env info (pattern, expr, maybeTipe) =
150152 let qs = [] -- should come from the def, but I'm not sure what would live there...
151153 (schemes, rigidQuantifiers, flexibleQuantifiers, headers, c2, c1) = info
152154 in
153− do rigidVars <- mapM (\_ -> var Rigid) qs -- Some mistake may be happening here.
154− -- Currently, qs is always the empty list.
155+ do rigidVars <- forM qs (\_ -> liftIO $ var Rigid) -- Some mistake may be happening here.
156+ -- Currently, qs is always [].
155157 case (pattern, maybeTipe) of
156158 (PVar name, Just tipe) -> do
157− flexiVars <- mapM (\_ -> var Flexible) qs
159+ flexiVars <- forM qs (\_ -> liftIO $ var Flexible)
158160 let inserts = zipWith (\arg typ -> Map.insert arg (VarN typ)) qs flexiVars
159161 env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
160162 (vars, typ) <- Env.instantiateType env tipe Map.empty
@@ -171,7 +173,7 @@ constrainDef env info (pattern, expr, maybeTipe) =
171173 , fl rigidVars c /\ c1 )
172174
173175 (PVar name, Nothing) -> do
174− v <- var Flexible
176+ v <- liftIO $ var Flexible
175177 let tipe = VarN v
176178 inserts = zipWith (\arg typ -> Map.insert arg (VarN typ)) qs rigidVars
177179 env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
@@ -183,7 +185,6 @@ constrainDef env info (pattern, expr, maybeTipe) =
183185 , c /\ c2
184186 , c1 )
185187
186−
187188expandPattern :: (Pattern, LExpr t v, Maybe SrcT.Type)
188189 -> [(Pattern, LExpr t v, Maybe SrcT.Type)]
189190expandPattern triple@(pattern, lexpr@(L s _), maybeType) =
@@ -212,9 +213,9 @@ collapseDefs = concatMap expandPattern . go [] Map.empty Map.empty
212213 TypeAnnotation name typ ->
213214 go output defs (Map.insert name typ typs) ds
214215
215−try :: SrcSpan -> ErrorT String IO a -> IO a
216+try :: SrcSpan -> ErrorT (SrcSpan -> PP.Doc) IO a -> ErrorT [PP.Doc] IO a
216217try span computation = do
217− result <- runErrorT computation
218+ result <- liftIO $ runErrorT computation
218219 case result of
219− Left msg -> error $ "\nType error " ++ show span ++ "\n" ++ msg
220− Right value -> return value
220+ Left err -> throwError [err span]
221+ Right value -> return value