@@ -14,7 +14,6 @@
1414-----------------------------------------------------------------------------
1515
1616{-# OPTIONS_GHC -fno-warn-orphans #-}
17−{-# LANGUAGE FlexibleInstances #-}
1817{-# LANGUAGE FlexibleContexts #-}
1918{-# LANGUAGE MultiParamTypeClasses #-}
2019
@@ -71,7 +70,6 @@ import Control.Arrow (Arrow(..))
7170import qualified Data.Map as M
7271import Data.Function (on)
7372import Data.Ord (comparing)
74−import Data.Monoid ((<>))
7573
7674instance Partial Type where
7775 unknown = TUnknown
@@ -85,7 +83,7 @@ instance Unifiable Check Type where
8583-- Unify two types, updating the current substitution
8684--
8785unifyTypes :: Type -> Type -> UnifyT Type Check ()
88−unifyTypes t1 t2 = rethrow (mkUnifyErrorStack ("Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2) Nothing <>) $
86+unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ e) $
8987 unifyTypes' t1 t2
9088 where
9189 unifyTypes' (TUnknown u1) (TUnknown u2) | u1 == u2 = return ()
@@ -102,16 +100,16 @@ unifyTypes t1 t2 = rethrow (mkUnifyErrorStack ("Error unifying type " ++ prettyP
102100 let sk1 = skolemize ident1 sko sc1' ty1
103101 let sk2 = skolemize ident2 sko sc2' ty2
104102 sk1 `unifyTypes` sk2
105− _ -> error "Skolemized type variable was not given a scope"
103+ _ -> throwError (prettyPrintType ty1)
106104 unifyTypes' (ForAll ident ty1 (Just sc)) ty2 = do
107105 sko <- newSkolemConstant
108106 let sk = skolemize ident sko sc ty1
109107 sk `unifyTypes` ty2
110− unifyTypes' ForAll{} _ = throwError . strMsg $ "Skolem variable scope is unspecified"
108+ unifyTypes' ForAll{} _ = throwError "Skolem variable scope is unspecified"
111109 unifyTypes' ty f@ForAll{} = f `unifyTypes` ty
112110 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
113111 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) =
114− guardWith (strMsg ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".")) (c1 == c2)
112+ guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (c1 == c2)
115113 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
116114 t3 `unifyTypes` t5
117115 t4 `unifyTypes` t6
@@ -120,9 +118,10 @@ unifyTypes t1 t2 = rethrow (mkUnifyErrorStack ("Error unifying type " ++ prettyP
120118 unifyTypes' r1 r2@RCons{} = unifyRows r1 r2
121119 unifyTypes' r1@REmpty r2 = unifyRows r1 r2
122120 unifyTypes' r1 r2@REmpty = unifyRows r1 r2
123− unifyTypes' t@(ConstrainedType _ _) _ = throwError . strMsg $ "Attempted to unify a constrained type " ++ prettyPrintType t ++ " with another type."
121+ unifyTypes' t@(ConstrainedType _ _) _ = throwError $ "Attempted to unify a constrained type " ++ prettyPrintType t ++
122+ " with another type."
124123 unifyTypes' t3 t4@(ConstrainedType _ _) = unifyTypes' t4 t3
125− unifyTypes' t3 t4 = throwError . strMsg $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
124+ unifyTypes' t3 t4 = throwError $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
126125
127126-- |
128127-- Unify two rows, updating the current substitution
@@ -155,7 +154,7 @@ unifyRows r1 r2 =
155154 unifyRows' [] REmpty [] REmpty = return ()
156155 unifyRows' [] (TypeVar v1) [] (TypeVar v2) | v1 == v2 = return ()
157156 unifyRows' [] (Skolem s1 _) [] (Skolem s2 _) | s1 == s2 = return ()
158− unifyRows' sd3 r3 sd4 r4 = throwError . strMsg $ "Cannot unify (" ++ prettyPrintRow (rowFromList (sd3, r3)) ++ ") with (" ++ prettyPrintRow (rowFromList (sd4, r4)) ++ ")"
157+ unifyRows' sd3 r3 sd4 r4 = throwError $ "Cannot unify (" ++ prettyPrintRow (rowFromList (sd3, r3)) ++ ") with (" ++ prettyPrintRow (rowFromList (sd4, r4)) ++ ")."
159158
160159-- |
161160-- Infer the types of multiple mutually-recursive values, and return elaborated values including
@@ -218,7 +217,7 @@ typeForBindingGroupElement moduleName e@(_, (val, _)) dict untypedDict = do
218217 (ident, (val', Just (ty, checkType))) -> do
219218 -- Kind check
220219 kind <- liftCheck $ kindOf moduleName ty
221− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
220+ guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
222221 -- Check the type with the new names in scope
223222 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
224223 val'' <- bindNames dict' $ if checkType
@@ -271,7 +270,7 @@ replaceTypeClassDictionaries mn = everywhereM' (mkM go)
271270entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> (Qualified ProperName, [Type]) -> Check Value
272271entails env moduleName context goal@(className, tys) = do
273272 case go goal of
274− [] -> throwError . strMsg $ "No " ++ show className ++ " instance found for " ++ intercalate ", " (map prettyPrintType tys)
273+ [] -> throwError $ "No " ++ show className ++ " instance found for " ++ intercalate ", " (map prettyPrintType tys)
275274 (dict : _) -> return dict
276275 where
277276 go (className', tys') =
@@ -363,7 +362,7 @@ skolemEscapeCheck root@TypedValue{} =
363362 -- an escaped skolem variable.
364363 case everythingWithContext [] (++) (mkQ ((,) []) go) root of
365364 [] -> return ()
366− ((binding, val) : _) -> throwError $ mkUnifyErrorStack ("Rigid/skolem type variable " ++ maybe "" (("bound by " ++) . prettyPrintValue) binding ++ " has escaped.") (Just (ValueError val))
365+ ((binding, val) : _) -> throwError $ "Rigid/skolem type variable bound by " ++ maybe "<unknown>" prettyPrintValue binding ++ " has escaped at " ++ prettyPrintValue val
367366 where
368367 go :: Value -> [(SkolemScope, Value)] -> ([(Maybe Value, Value)], [(SkolemScope, Value)])
369368 go val@(TypedValue _ _ (ForAll _ _ (Just sco))) scos = ([], (sco, val) : scos)
@@ -382,7 +381,7 @@ skolemEscapeCheck root@TypedValue{} =
382381 where
383382 go' val@(TypedValue _ _ (ForAll _ _ (Just sco'))) | sco == sco' = Just val
384383 go' _ = Nothing
385−skolemEscapeCheck val = throwError $ mkUnifyErrorStack "Untyped value passed to skolemEscapeCheck" (Just (ValueError val))
384+skolemEscapeCheck val = throwError $ "Untyped value passed to skolemEscapeCheck: " ++ prettyPrintValue val
386385
387386-- |
388387-- Ensure a row contains no duplicate labels
@@ -445,10 +444,10 @@ replaceAllTypeSynonyms' env d =
445444 in
446445 saturateAllTypeSynonyms syns d
447446
448−replaceAllTypeSynonyms :: (Error e, Functor m, Monad m, MonadState CheckState m, MonadError e m) => (D.Data d) => d -> m d
447+replaceAllTypeSynonyms :: (Functor m, Monad m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
449448replaceAllTypeSynonyms d = do
450449 env <- getEnv
451− either (throwError . strMsg) return $ replaceAllTypeSynonyms' env d
450+ either throwError return $ replaceAllTypeSynonyms' env d
452451
453452-- |
454453-- \"Desaturate\" @SaturatedTypeSynonym@s
@@ -470,12 +469,12 @@ expandTypeSynonym' env name args =
470469 replaceAllTypeSynonyms' env repl
471470 Nothing -> error "Type synonym was not defined"
472471
473−expandTypeSynonym :: (Error e, Functor m, Monad m, MonadState CheckState m, MonadError e m) => Qualified ProperName -> [Type] -> m Type
472+expandTypeSynonym :: (Functor m, Monad m, MonadState CheckState m, MonadError String m) => Qualified ProperName -> [Type] -> m Type
474473expandTypeSynonym name args = do
475474 env <- getEnv
476− either (throwError . strMsg) return $ expandTypeSynonym' env name args
475+ either throwError return $ expandTypeSynonym' env name args
477476
478−expandAllTypeSynonyms :: (Error e, Functor m, Monad m, MonadState CheckState m, MonadError e m) => Type -> m Type
477+expandAllTypeSynonyms :: (Functor m, Monad m, MonadState CheckState m, MonadError String m) => Type -> m Type
479478expandAllTypeSynonyms = everywhereM' (mkM go)
480479 where
481480 go (SaturatedTypeSynonym name args) = expandTypeSynonym name args
@@ -484,14 +483,14 @@ expandAllTypeSynonyms = everywhereM' (mkM go)
484483-- |
485484-- Ensure a set of property names and value does not contain duplicate labels
486485--
487−ensureNoDuplicateProperties :: (Error e, MonadError e m) => [(String, Value)] -> m ()
488−ensureNoDuplicateProperties ps = guardWith (strMsg "Duplicate property names") $ length (nub . map fst $ ps) == length ps
486+ensureNoDuplicateProperties :: (MonadError String m) => [(String, Value)] -> m ()
487+ensureNoDuplicateProperties ps = guardWith "Duplicate property names" $ length (nub . map fst $ ps) == length ps
489488
490489-- |
491490-- Infer a type for a value, rethrowing any error to provide a more useful error message
492491--
493492infer :: Value -> UnifyT Type Check Value
494−infer val = rethrow (mkUnifyErrorStack "Error inferring type of value" (Just (ValueError val)) <>) $ infer' val
493+infer val = rethrow (\e -> "Error inferring type of term " ++ prettyPrintValue val ++ ":\n" ++ e) $ infer' val
495494
496495-- |
497496-- Infer a type for a value
@@ -552,7 +551,7 @@ infer' (Var var) = do
552551infer' v@(Constructor c) = do
553552 env <- getEnv
554553 case M.lookup c (dataConstructors env) of
555− Nothing -> throwError . strMsg $ "Constructor " ++ show c ++ " is undefined"
554+ Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
556555 Just (_, ty) -> do ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
557556 return $ TypedValue True v ty'
558557infer' (Case vals binders) = do
@@ -572,7 +571,7 @@ infer' (Let ds val) = do
572571infer' (TypedValue checkType val ty) = do
573572 Just moduleName <- checkCurrentModule <$> get
574573 kind <- liftCheck $ kindOf moduleName ty
575− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
574+ guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
576575 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
577576 val' <- if checkType then check val ty' else return val
578577 return $ TypedValue True val' ty'
@@ -632,8 +631,8 @@ inferBinder val (ConstructorBinder ctor binders) = do
632631 return M.empty
633632 go (binder : binders') (TypeApp (TypeApp t obj) ret) | t == tyFunction =
634633 M.union <$> inferBinder obj binder <*> go binders' ret
635− go _ _ = throwError . strMsg $ "Wrong number of arguments to constructor " ++ show ctor
636− _ -> throwError . strMsg $ "Constructor " ++ show ctor ++ " is not defined"
634+ go _ _ = throwError $ "Wrong number of arguments to constructor " ++ show ctor
635+ _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
637636inferBinder val (ObjectBinder props) = do
638637 row <- fresh
639638 rest <- fresh
@@ -712,13 +711,15 @@ introduceSkolemScope = everywhereM (mkM go)
712711-- Check the type of a value, rethrowing errors to provide a better error message
713712--
714713check :: Value -> Type -> UnifyT Type Check Value
715−check val ty = rethrow (mkUnifyErrorStack errorMessage (Just (ValueError val)) <>) $ check' val ty
714+check val ty = rethrow errorMessage $ check' val ty
716715 where
717− errorMessage =
716+ errorMessage msg =
718717 "Error checking type of term " ++
719718 prettyPrintValue val ++
720719 " against type " ++
721− prettyPrintType ty
720+ prettyPrintType ty ++
721+ ":\n" ++
722+ msg
722723
723724-- |
724725-- Check the type of a value
@@ -772,16 +773,16 @@ check' v@(Var var) ty = do
772773 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
773774 v' <- subsumes (Just v) repl ty'
774775 case v' of
775− Nothing -> throwError . strMsg $ "Unable to check type subsumption"
776+ Nothing -> throwError "Unable to check type subsumption"
776777 Just v'' -> return $ TypedValue True v'' ty'
777778check' (TypedValue checkType val ty1) ty2 = do
778779 Just moduleName <- checkCurrentModule <$> get
779780 kind <- liftCheck $ kindOf moduleName ty1
780− guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
781+ guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
781782 ty1' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty1
782783 val' <- subsumes (Just val) ty1' ty2
783784 case val' of
784− Nothing -> throwError . strMsg $ "Unable to check type subsumption"
785+ Nothing -> throwError "Unable to check type subsumption"
785786 Just val'' -> do
786787 val''' <- if checkType then check val'' ty1' else return val''
787788 return $ TypedValue checkType (TypedValue True val''' ty1) ty2
@@ -815,7 +816,7 @@ check' (Accessor prop val) ty = do
815816check' (Constructor c) ty = do
816817 env <- getEnv
817818 case M.lookup c (dataConstructors env) of
818− Nothing -> throwError . strMsg $ "Constructor " ++ show c ++ " is undefined"
819+ Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
819820 Just (_, ty1) -> do
820821 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty1
821822 _ <- subsumes Nothing repl ty
@@ -826,7 +827,7 @@ check' (Let ds val) ty = do
826827check' val ty | containsTypeSynonyms ty = do
827828 ty' <- introduceSkolemScope <=< expandAllTypeSynonyms $ ty
828829 check val ty'
829−check' val ty = throwError $ mkUnifyErrorStack ("Value does not have type " ++ prettyPrintType ty) (Just (ValueError val))
830+check' val ty = throwError $ prettyPrintValue val ++ " does not have type " ++ prettyPrintType ty
830831
831832containsTypeSynonyms :: Type -> Bool
832833containsTypeSynonyms = everything (||) (mkQ False go) where
@@ -845,8 +846,8 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
845846 return []
846847 go [] [] (Skolem _ _) | lax = return []
847848 go [] ((p, _): _) _ | lax = return []
848− | otherwise = throwError $ mkUnifyErrorStack ("Object does not have property " ++ p) (Just (ValueError (ObjectLiteral ps)))
849− go ((p,_):_) [] REmpty = throwError $ mkUnifyErrorStack ("Property " ++ p ++ " is not present in closed object type " ++ prettyPrintRow row) (Just (ValueError (ObjectLiteral ps)))
849+ | otherwise = throwError $ prettyPrintValue (ObjectLiteral ps) ++ " does not have property " ++ p
850+ go ((p,_):_) [] REmpty = throwError $ "Property " ++ p ++ " is not present in closed object type " ++ prettyPrintRow row
850851 go ((p,v):ps') [] u@(TUnknown _) = do
851852 v'@(TypedValue _ _ ty) <- infer v
852853 rest <- fresh
@@ -865,17 +866,18 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
865866 v' <- check v ty
866867 ps'' <- go ps' (delete (p, ty) ts) r
867868 return $ (p, v') : ps''
868− go _ _ _ = throwError $ mkUnifyErrorStack ("Object does not have type " ++ prettyPrintType (TypeApp tyObject row)) (Just (ValueError (ObjectLiteral ps)))
869+ go _ _ _ = throwError $ prettyPrintValue (ObjectLiteral ps) ++ " does not have type " ++ prettyPrintType (TypeApp tyObject row)
869870
870871-- |
871872-- Check the type of a function application, rethrowing errors to provide a better error message
872873--
873874checkFunctionApplication :: Value -> Type -> Value -> Maybe Type -> UnifyT Type Check (Type, Value)
874−checkFunctionApplication fn fnTy arg ret = rethrow (mkUnifyErrorStack errorMessage (Just (ValueError fn)) <>) $ checkFunctionApplication' fn fnTy arg ret
875+checkFunctionApplication fn fnTy arg ret = rethrow errorMessage $ checkFunctionApplication' fn fnTy arg ret
875876 where
876− errorMessage = "Error applying function of type "
877+ errorMessage msg = "Error applying function of type "
877878 ++ prettyPrintType fnTy
878879 ++ " to argument " ++ prettyPrintValue arg
880+ ++ ":\n" ++ msg
879881
880882-- |
881883-- Check the type of a function application
@@ -905,7 +907,7 @@ checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
905907checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
906908 dicts <- getTypeClassDictionaries
907909 checkFunctionApplication' (foldl App fn (map (flip TypeClassDictionary dicts) constraints)) fnTy arg ret
908−checkFunctionApplication' _ fnTy arg _ = throwError . strMsg $ "Cannot apply a function of type "
910+checkFunctionApplication' _ fnTy arg _ = throwError $ "Cannot apply a function of type "
909911 ++ prettyPrintType fnTy
910912 ++ " to argument " ++ prettyPrintValue arg
911913
@@ -913,12 +915,13 @@ checkFunctionApplication' _ fnTy arg _ = throwError . strMsg $ "Cannot apply a f
913915-- Check whether one type subsumes another, rethrowing errors to provide a better error message
914916--
915917subsumes :: Maybe Value -> Type -> Type -> UnifyT Type Check (Maybe Value)
916−subsumes val ty1 ty2 = rethrow (mkUnifyErrorStack errorMessage (ValueError <$> val) <>) $ subsumes' val ty1 ty2
918+subsumes val ty1 ty2 = rethrow errorMessage $ subsumes' val ty1 ty2
917919 where
918− errorMessage = "Error checking that type "
920+ errorMessage msg = "Error checking that type "
919921 ++ prettyPrintType ty1
920922 ++ " subsumes type "
921923 ++ prettyPrintType ty2
924+ ++ ":\n" ++ msg
922925
923926-- |
924927-- Check whether one type subsumes another
@@ -933,7 +936,7 @@ subsumes' val ty1 (ForAll ident ty2 sco) =
933936 sko <- newSkolemConstant
934937 let sk = skolemize ident sko sco' ty2
935938 subsumes val ty1 sk
936− Nothing -> throwError . strMsg $ "Skolem variable scope is unspecified"
939+ Nothing -> throwError "Skolem variable scope is unspecified"
937940subsumes' val (TypeApp (TypeApp f1 arg1) ret1) (TypeApp (TypeApp f2 arg2) ret2) | f1 == tyFunction && f2 == tyFunction = do
938941 _ <- subsumes Nothing arg2 arg1
939942 _ <- subsumes Nothing ret1 ret2
@@ -973,4 +976,3 @@ subsumes' val ty1 ty2 = do
973976 ty1 =?= ty2
974977 return val
975978
976−