@@ -26,7 +26,6 @@ module Language.PureScript.TypeChecker.Types
27import Prelude.Compat
27import Prelude.Compat
29−import Control.Arrow (second)
30import Control.Monad
29import Control.Monad
31import Control.Monad.Error.Class (MonadError(..))
30import Control.Monad.Error.Class (MonadError(..))
32import Control.Monad.State.Class (MonadState(..), gets)
31import Control.Monad.State.Class (MonadState(..), gets)
@@ -71,7 +70,7 @@ typesOf ::
71 m [(Ident, (Expr, Type))]
70 m [(Ident, (Expr, Type))]
72typesOf bindingGroupType moduleName vals = do
71typesOf bindingGroupType moduleName vals = do
73 tys <- fmap tidyUp . escalateWarningWhen isHoleError . liftUnifyWarnings replace $ do
72 tys <- fmap tidyUp . escalateWarningWhen isHoleError . liftUnifyWarnings replace $ do
74− (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
73+ (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup (Just moduleName) vals
75 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
74 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
76 ds2 <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
75 ds2 <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
77 return (map (\x -> (False, x)) ds1 ++ map (\x -> (True, x)) ds2)
76 return (map (\x -> (False, x)) ds1 ++ map (\x -> (True, x)) ds2)
@@ -117,26 +116,21 @@ typesOf bindingGroupType moduleName vals = do
117 tidyUp (ts, sub) = map (\(b, (i, (val, ty))) -> (b, (i, (overTypes (substituteType sub) val, substituteType sub ty)))) ts
116 tidyUp (ts, sub) = map (\(b, (i, (val, ty))) -> (b, (i, (overTypes (substituteType sub) val, substituteType sub ty)))) ts
119 -- Replace all the wildcards types with their inferred types
118 -- Replace all the wildcards types with their inferred types
120− replace sub (ErrorMessage hints (WildcardInferredType ty)) =
121− ErrorMessage hints . WildcardInferredType $ substituteType sub ty
122− replace sub (ErrorMessage hints (HoleInferredType name ty env)) =
123− ErrorMessage hints $ HoleInferredType name (substituteType sub ty)
124− (map (second (substituteType sub)) env)
119+ replace sub = onTypesInErrorMessage (substituteType sub)
127 isHoleError :: ErrorMessage -> Bool
121 isHoleError :: ErrorMessage -> Bool
128 isHoleError (ErrorMessage _ HoleInferredType{}) = True
122 isHoleError (ErrorMessage _ HoleInferredType{}) = True
129 isHoleError _ = False
123 isHoleError _ = False
131−type TypeData = M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
125+type TypeData = M.Map (Qualified Ident) (Type, NameKind, NameVisibility)
133type UntypedData = [(Ident, Type)]
127type UntypedData = [(Ident, Type)]
135−typeDictionaryForBindingGroup ::
129+typeDictionaryForBindingGroup
136− (MonadState CheckState m) =>
130+ :: (MonadState CheckState m)
137− ModuleName ->
131+ => Maybe ModuleName
138− [(Ident, Expr)] ->
132+ -> [(Ident, Expr)]
139− m ([(Ident, Expr)], [(Ident, (Expr, Type, Bool))], TypeData, UntypedData)
133+ -> m ([(Ident, Expr)], [(Ident, (Expr, Type, Bool))], TypeData, UntypedData)
140typeDictionaryForBindingGroup moduleName vals = do
134typeDictionaryForBindingGroup moduleName vals = do
142 -- Map each declaration to a name/value pair, with an optional type, if the declaration is typed
136 -- Map each declaration to a name/value pair, with an optional type, if the declaration is typed
@@ -154,15 +148,15 @@ typeDictionaryForBindingGroup moduleName vals = do
154 -- Make a map of names to the unification variables of untyped declarations
148 -- Make a map of names to the unification variables of untyped declarations
155 untypedDict = zip (map fst untyped) untypedNames
149 untypedDict = zip (map fst untyped) untypedNames
156 -- Create the dictionary of all name/type pairs, which will be added to the environment during type checking
150 -- Create the dictionary of all name/type pairs, which will be added to the environment during type checking
157− dict = M.fromList (map (\(ident, ty) -> ((moduleName, ident), (ty, Private, Undefined))) $ typedDict ++ untypedDict)
151+ dict = M.fromList (map (\(ident, ty) -> ((Qualified moduleName ident), (ty, Private, Undefined))) $ typedDict ++ untypedDict)
158 return (untyped, typed, dict, untypedDict)
152 return (untyped, typed, dict, untypedDict)
160−checkTypedBindingGroupElement ::
154+checkTypedBindingGroupElement
161− (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
155+ :: (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
162− ModuleName ->
156+ => ModuleName
163− (Ident, (Expr, Type, Bool)) ->
157+ -> (Ident, (Expr, Type, Bool))
164− TypeData ->
158+ -> TypeData
165− m (Ident, (Expr, Type))
159+ -> m (Ident, (Expr, Type))
166checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
160checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
167 -- Replace type wildcards
161 -- Replace type wildcards
168 ty' <- replaceTypeWildcards ty
162 ty' <- replaceTypeWildcards ty
@@ -176,12 +170,12 @@ checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
176 else return (TypedValue False val' ty'')
170 else return (TypedValue False val' ty'')
177 return (ident, (val'', ty''))
171 return (ident, (val'', ty''))
179−typeForBindingGroupElement ::
173+typeForBindingGroupElement
180− (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
174+ :: (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
181− (Ident, Expr) ->
175+ => (Ident, Expr)
182− TypeData ->
176+ -> TypeData
183− UntypedData ->
177+ -> UntypedData
184− m (Ident, (Expr, Type))
178+ -> m (Ident, (Expr, Type))
185typeForBindingGroupElement (ident, val) dict untypedDict = do
179typeForBindingGroupElement (ident, val) dict untypedDict = do
186 -- Infer the type with the new names in scope
180 -- Infer the type with the new names in scope
187 TypedValue _ val' ty <- bindNames dict $ infer val
181 TypedValue _ val' ty <- bindNames dict $ infer val
@@ -201,7 +195,7 @@ overTypes f = let (_, f', _) = everywhereOnValues id g id in f'
202 g :: Expr -> Expr
196 g :: Expr -> Expr
203 g (TypedValue checkTy val t) = TypedValue checkTy val (f t)
197 g (TypedValue checkTy val t) = TypedValue checkTy val (f t)
204− g (TypeClassDictionary c sco) = TypeClassDictionary (mapConstraintArgs (map f) c) sco
198+ g (TypeClassDictionary c sco hints) = TypeClassDictionary (mapConstraintArgs (map f) c) sco hints
205 g other = other
199 g other = other
207-- | Check the kind of a type, failing if it is not of kind *.
201-- | Check the kind of a type, failing if it is not of kind *.
@@ -227,7 +221,8 @@ instantiatePolyTypeWithUnknowns val (ForAll ident ty _) = do
227 instantiatePolyTypeWithUnknowns val ty'
221 instantiatePolyTypeWithUnknowns val ty'
228instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
222instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
229 dicts <- getTypeClassDictionaries
223 dicts <- getTypeClassDictionaries
224+ hints <- gets checkHints
230− instantiatePolyTypeWithUnknowns (foldl App val (map (flip TypeClassDictionary dicts) constraints)) ty
225+ instantiatePolyTypeWithUnknowns (foldl App val (map (\cs -> TypeClassDictionary cs dicts hints) constraints)) ty
231instantiatePolyTypeWithUnknowns val ty = return (val, ty)
226instantiatePolyTypeWithUnknowns val ty = return (val, ty)
233-- | Infer a type for a value, rethrowing any error to provide a more useful error message
228-- | Infer a type for a value, rethrowing any error to provide a more useful error message
@@ -235,7 +230,7 @@ infer ::
235 (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
230 (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
238−infer val = rethrow (addHint (ErrorInferringType val)) $ infer' val
233+infer val = withErrorMessageHint (ErrorInferringType val) $ infer' val
240-- | Infer a type for a value
235-- | Infer a type for a value
@@ -250,8 +245,11 @@ infer' v@(Literal (BooleanLiteral _)) = return $ TypedValue True v tyBoolean
250infer' (Literal (ArrayLiteral vals)) = do
245infer' (Literal (ArrayLiteral vals)) = do
251 ts <- traverse infer vals
246 ts <- traverse infer vals
252 els <- freshType
247 els <- freshType
248+ ts' <- forM ts $ \(TypedValue ch val t) -> do
249+ (val', t') <- instantiatePolyTypeWithUnknowns val t
253− forM_ ts $ \(TypedValue _ _ t) -> unifyTypes els t
250+ unifyTypes els t'
251+ return (TypedValue ch val' t')
254− return $ TypedValue True (Literal (ArrayLiteral ts)) (TypeApp tyArray els)
252+ return $ TypedValue True (Literal (ArrayLiteral ts')) (TypeApp tyArray els)
255infer' (Literal (ObjectLiteral ps)) = do
253infer' (Literal (ObjectLiteral ps)) = do
256 ensureNoDuplicateProperties ps
254 ensureNoDuplicateProperties ps
257 ts <- traverse (infer . snd) ps
255 ts <- traverse (infer . snd) ps
@@ -267,15 +265,14 @@ infer' (ObjectUpdate o ps) = do
267 let oldTy = TypeApp tyRecord $ rowFromList (oldTys, row)
265 let oldTy = TypeApp tyRecord $ rowFromList (oldTys, row)
268 o' <- TypedValue True <$> check o oldTy <*> pure oldTy
266 o' <- TypedValue True <$> check o oldTy <*> pure oldTy
269 return $ TypedValue True (ObjectUpdate o' newVals) $ TypeApp tyRecord $ rowFromList (newTys, row)
267 return $ TypedValue True (ObjectUpdate o' newVals) $ TypeApp tyRecord $ rowFromList (newTys, row)
270−infer' (Accessor prop val) = rethrow (addHint (ErrorCheckingAccessor val prop)) $ do
268+infer' (Accessor prop val) = withErrorMessageHint (ErrorCheckingAccessor val prop) $ do
271 field <- freshType
269 field <- freshType
272 rest <- freshType
270 rest <- freshType
273 typed <- check val (TypeApp tyRecord (RCons prop field rest))
271 typed <- check val (TypeApp tyRecord (RCons prop field rest))
274 return $ TypedValue True (Accessor prop typed) field
272 return $ TypedValue True (Accessor prop typed) field
275infer' (Abs (Left arg) ret) = do
273infer' (Abs (Left arg) ret) = do
276 ty <- freshType
274 ty <- freshType
277− Just moduleName <- checkCurrentModule <$> get
278− withBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
275+ withBindingGroupVisible $ bindLocalVariables [(arg, ty, Defined)] $ do
279 body@(TypedValue _ _ bodyTy) <- infer' ret
276 body@(TypedValue _ _ bodyTy) <- infer' ret
280 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
277 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
281infer' (Abs (Right _) _) = internalError "Binder was not desugared"
278infer' (Abs (Right _) _) = internalError "Binder was not desugared"
@@ -284,13 +281,13 @@ infer' (App f arg) = do
284 (ret, app) <- checkFunctionApplication f' ft arg Nothing
281 (ret, app) <- checkFunctionApplication f' ft arg Nothing
285 return $ TypedValue True app ret
282 return $ TypedValue True app ret
286infer' (Var var) = do
283infer' (Var var) = do
287− Just moduleName <- checkCurrentModule <$> get
288− checkVisibility moduleName var
284+ checkVisibility var
289− ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable moduleName $ var
285+ ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable $ var
290 case ty of
286 case ty of
291 ConstrainedType constraints ty' -> do
287 ConstrainedType constraints ty' -> do
292 dicts <- getTypeClassDictionaries
288 dicts <- getTypeClassDictionaries
289+ hints <- gets checkHints
293− return $ TypedValue True (foldl App (Var var) (map (flip TypeClassDictionary dicts) constraints)) ty'
290+ return $ TypedValue True (foldl App (Var var) (map (\cs -> TypeClassDictionary cs dicts hints) constraints)) ty'
294 _ -> return $ TypedValue True (Var var) ty
291 _ -> return $ TypedValue True (Var var) ty
295infer' v@(Constructor c) = do
292infer' v@(Constructor c) = do
296 env <- getEnv
293 env <- getEnv
@@ -316,7 +313,8 @@ infer' (Let ds val) = do
316 return $ TypedValue True (Let ds' val') valTy
313 return $ TypedValue True (Let ds' val') valTy
317infer' (SuperClassDictionary className tys) = do
314infer' (SuperClassDictionary className tys) = do
318 dicts <- getTypeClassDictionaries
315 dicts <- getTypeClassDictionaries
316+ hints <- gets checkHints
319− return $ TypeClassDictionary (Constraint className tys Nothing) dicts
317+ return $ TypeClassDictionary (Constraint className tys Nothing) dicts hints
320infer' (TypedValue checkType val ty) = do
318infer' (TypedValue checkType val ty) = do
321 Just moduleName <- checkCurrentModule <$> get
319 Just moduleName <- checkCurrentModule <$> get
322 (kind, args) <- kindOfWithScopedVars ty
320 (kind, args) <- kindOfWithScopedVars ty
@@ -326,12 +324,10 @@ infer' (TypedValue checkType val ty) = do
326 return $ TypedValue True val' ty'
324 return $ TypedValue True val' ty'
327infer' (Hole name) = do
325infer' (Hole name) = do
328 ty <- freshType
326 ty <- freshType
329− env <- M.toList . names <$> getEnv
327+ ctx <- getLocalContext
330− Just moduleName <- checkCurrentModule <$> get
331− let ctx = [ (ident, ty') | ((mn, ident@Ident{}), (ty', _, Defined)) <- env, mn == moduleName ]
332 tell . errorMessage $ HoleInferredType name ty ctx
328 tell . errorMessage $ HoleInferredType name ty ctx
333 return $ TypedValue True (Hole name) ty
329 return $ TypedValue True (Hole name) ty
334−infer' (PositionedValue pos c val) = warnAndRethrowWithPosition pos $ do
330+infer' (PositionedValue pos c val) = warnAndRethrowWithPositionTC pos $ do
335 TypedValue t v ty <- infer' val
331 TypedValue t v ty <- infer' val
336 return $ TypedValue t (PositionedValue pos c v) ty
332 return $ TypedValue t (PositionedValue pos c v) ty
337infer' v = internalError $ "Invalid argument to infer: " ++ show v
333infer' v = internalError $ "Invalid argument to infer: " ++ show v
@@ -348,27 +344,26 @@ inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue
348 Just moduleName <- checkCurrentModule <$> get
344 Just moduleName <- checkCurrentModule <$> get
349 (kind, args) <- kindOfWithScopedVars ty
345 (kind, args) <- kindOfWithScopedVars ty
350 checkTypeKind ty kind
346 checkTypeKind ty kind
351− let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
347+ let dict = M.singleton (Qualified Nothing ident) (ty, nameKind, Undefined)
352 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
348 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
353 TypedValue _ val' ty'' <- if checkType then withScopedTypeVars moduleName args (bindNames dict (check val ty')) else return tv
349 TypedValue _ val' ty'' <- if checkType then withScopedTypeVars moduleName args (bindNames dict (check val ty')) else return tv
354− bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
350+ bindNames (M.singleton (Qualified Nothing ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
355inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
351inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
356 valTy <- freshType
352 valTy <- freshType
357− Just moduleName <- checkCurrentModule <$> get
358− let dict = M.singleton (moduleName, ident) (valTy, nameKind, Undefined)
353+ let dict = M.singleton (Qualified Nothing ident) (valTy, nameKind, Undefined)
359 TypedValue _ val' valTy' <- bindNames dict $ infer val
354 TypedValue _ val' valTy' <- bindNames dict $ infer val
360 unifyTypes valTy valTy'
355 unifyTypes valTy valTy'
361− bindNames (M.singleton (moduleName, ident) (valTy', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right val')]) rest ret j
356+ bindNames (M.singleton (Qualified Nothing ident) (valTy', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right val')]) rest ret j
362inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
357inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
363 Just moduleName <- checkCurrentModule <$> get
358 Just moduleName <- checkCurrentModule <$> get
364− (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName (map (\(i, _, v) -> (i, v)) ds)
359+ (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup Nothing (map (\(i, _, v) -> (i, v)) ds)
365 ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
360 ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
366 ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
361 ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
367 let ds' = [(ident, Private, val') | (ident, (val', _)) <- ds1' ++ ds2']
362 let ds' = [(ident, Private, val') | (ident, (val', _)) <- ds1' ++ ds2']
368 bindNames dict $ do
363 bindNames dict $ do
369 makeBindingGroupVisible
364 makeBindingGroupVisible
370 inferLetBinding (seen ++ [BindingGroupDeclaration ds']) rest ret j
365 inferLetBinding (seen ++ [BindingGroupDeclaration ds']) rest ret j
371−inferLetBinding seen (PositionedDeclaration pos com d : ds) ret j = warnAndRethrowWithPosition pos $ do
366+inferLetBinding seen (PositionedDeclaration pos com d : ds) ret j = warnAndRethrowWithPositionTC pos $ do
372 (d' : ds', val') <- inferLetBinding seen (d : ds) ret j
367 (d' : ds', val') <- inferLetBinding seen (d : ds) ret j
373 return (PositionedDeclaration pos com d' : ds', val')
368 return (PositionedDeclaration pos com d' : ds', val')
374inferLetBinding _ _ _ _ = internalError "Invalid argument to inferLetBinding"
369inferLetBinding _ _ _ _ = internalError "Invalid argument to inferLetBinding"
@@ -426,7 +421,7 @@ inferBinder val (NamedBinder name binder) = do
426 m <- inferBinder val binder
421 m <- inferBinder val binder
427 return $ M.insert name val m
422 return $ M.insert name val m
428inferBinder val (PositionedBinder pos _ binder) =
423inferBinder val (PositionedBinder pos _ binder) =
429− warnAndRethrowWithPosition pos $ inferBinder val binder
424+ warnAndRethrowWithPositionTC pos $ inferBinder val binder
430-- TODO: When adding support for polymorphic types, check subsumption here,
425-- TODO: When adding support for polymorphic types, check subsumption here,
431-- change the definition of `binderRequiresMonotype`,
426-- change the definition of `binderRequiresMonotype`,
432-- and use `kindOfWithScopedVars`.
427-- and use `kindOfWithScopedVars`.
@@ -480,14 +475,13 @@ checkBinders _ _ [] = return []
480checkBinders nvals ret (CaseAlternative binders result : bs) = do
475checkBinders nvals ret (CaseAlternative binders result : bs) = do
481 guardWith (errorMessage $ OverlappingArgNames Nothing) $
476 guardWith (errorMessage $ OverlappingArgNames Nothing) $
482 let ns = concatMap binderNames binders in length (nub ns) == length ns
477 let ns = concatMap binderNames binders in length (nub ns) == length ns
483− Just moduleName <- checkCurrentModule <$> get
484 m1 <- M.unions <$> zipWithM inferBinder nvals binders
478 m1 <- M.unions <$> zipWithM inferBinder nvals binders
485− r <- bindLocalVariables moduleName [ (name, ty, Defined) | (name, ty) <- M.toList m1 ] $
479+ r <- bindLocalVariables [ (name, ty, Defined) | (name, ty) <- M.toList m1 ] $
486 CaseAlternative binders <$>
480 CaseAlternative binders <$>
487 case result of
481 case result of
488 Left gs -> do
482 Left gs -> do
489 gs' <- forM gs $ \(grd, val) -> do
483 gs' <- forM gs $ \(grd, val) -> do
490− grd' <- rethrow (addHint ErrorCheckingGuard) $ check grd tyBoolean
484+ grd' <- withErrorMessageHint ErrorCheckingGuard $ check grd tyBoolean
491 val' <- TypedValue True <$> check val ret <*> pure ret
485 val' <- TypedValue True <$> check val ret <*> pure ret
492 return (grd', val')
486 return (grd', val')
493 return $ Left gs'
487 return $ Left gs'
@@ -505,7 +499,7 @@ check ::
508−check val ty = rethrow (addHint (ErrorCheckingType val ty)) $ check' val ty
502+check val ty = withErrorMessageHint (ErrorCheckingType val ty) $ check' val ty
511-- Check the type of a value
505-- Check the type of a value
@@ -574,8 +568,7 @@ check' (Literal (ArrayLiteral vals)) t@(TypeApp a ty) = do
574 return $ TypedValue True array t
568 return $ TypedValue True array t
575check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
569check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
576 unifyTypes t tyFunction
570 unifyTypes t tyFunction
577− Just moduleName <- checkCurrentModule <$> get
578− ret' <- withBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
571+ ret' <- withBindingGroupVisible $ bindLocalVariables [(arg, argTy, Defined)] $ check ret retTy
579 return $ TypedValue True (Abs (Left arg) ret') ty
572 return $ TypedValue True (Abs (Left arg) ret') ty
580check' (Abs (Right _) _) _ = internalError "Binder was not desugared"
573check' (Abs (Right _) _) _ = internalError "Binder was not desugared"
581check' (App f arg) ret = do
574check' (App f arg) ret = do
@@ -583,9 +576,8 @@ check' (App f arg) ret = do
583 (_, app) <- checkFunctionApplication f' ft arg (Just ret)
576 (_, app) <- checkFunctionApplication f' ft arg (Just ret)
584 return $ TypedValue True app ret
577 return $ TypedValue True app ret
585check' v@(Var var) ty = do
578check' v@(Var var) ty = do
586− Just moduleName <- checkCurrentModule <$> get
587− checkVisibility moduleName var
579+ checkVisibility var
588− repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
580+ repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable $ var
589 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
581 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
590 v' <- subsumes (Just v) repl ty'
582 v' <- subsumes (Just v) repl ty'
591 case v' of
583 case v' of
@@ -599,7 +591,8 @@ check' (SuperClassDictionary className tys) _ = do
599 -- declaration gets desugared.
591 -- declaration gets desugared.
601 dicts <- getTypeClassDictionaries
593 dicts <- getTypeClassDictionaries
594+ hints <- gets checkHints
602− return $ TypeClassDictionary (Constraint className tys Nothing) dicts
595+ return $ TypeClassDictionary (Constraint className tys Nothing) dicts hints
603check' (TypedValue checkType val ty1) ty2 = do
596check' (TypedValue checkType val ty1) ty2 = do
604 Just moduleName <- checkCurrentModule <$> get
597 Just moduleName <- checkCurrentModule <$> get
605 (kind, args) <- kindOfWithScopedVars ty1
598 (kind, args) <- kindOfWithScopedVars ty1
@@ -638,7 +631,7 @@ check' e@(ObjectUpdate obj ps) t@(TypeApp o row) | o == tyRecord = do
638 obj' <- check obj (TypeApp tyRecord (rowFromList (us ++ remainingProps, rest)))
631 obj' <- check obj (TypeApp tyRecord (rowFromList (us ++ remainingProps, rest)))
639 ps' <- checkProperties e ps row True
632 ps' <- checkProperties e ps row True
640 return $ TypedValue True (ObjectUpdate obj' ps') t
633 return $ TypedValue True (ObjectUpdate obj' ps') t
641−check' (Accessor prop val) ty = rethrow (addHint (ErrorCheckingAccessor val prop)) $ do
634+check' (Accessor prop val) ty = withErrorMessageHint (ErrorCheckingAccessor val prop) $ do
642 rest <- freshType
635 rest <- freshType
643 val' <- check val (TypeApp tyRecord (RCons prop ty rest))
636 val' <- check val (TypeApp tyRecord (RCons prop ty rest))
644 return $ TypedValue True (Accessor prop val') ty
637 return $ TypedValue True (Accessor prop val') ty
@@ -659,7 +652,7 @@ check' val kt@(KindedType ty kind) = do
659 checkTypeKind ty kind
652 checkTypeKind ty kind
660 val' <- check' val ty
653 val' <- check' val ty
661 return $ TypedValue True val' kt
654 return $ TypedValue True val' kt
662−check' (PositionedValue pos c val) ty = warnAndRethrowWithPosition pos $ do
655+check' (PositionedValue pos c val) ty = warnAndRethrowWithPositionTC pos $ do
663 TypedValue t v ty' <- check' val ty
656 TypedValue t v ty' <- check' val ty
664 return $ TypedValue t (PositionedValue pos c v) ty'
657 return $ TypedValue t (PositionedValue pos c v) ty'
665check' val ty = do
658check' val ty = do
@@ -713,7 +706,7 @@ checkFunctionApplication ::
714 Maybe Type ->
707 Maybe Type ->
715 m (Type, Expr)
708 m (Type, Expr)
716−checkFunctionApplication fn fnTy arg ret = rethrow (addHint (ErrorInApplication fn fnTy arg)) $ do
709+checkFunctionApplication fn fnTy arg ret = withErrorMessageHint (ErrorInApplication fn fnTy arg) $ do
717 subst <- gets checkSubstitution
710 subst <- gets checkSubstitution
718 checkFunctionApplication' fn (substituteType subst fnTy) arg (substituteType subst <$> ret)
711 checkFunctionApplication' fn (substituteType subst fnTy) arg (substituteType subst <$> ret)
@@ -749,7 +742,8 @@ checkFunctionApplication' fn (KindedType ty _) arg ret =
749 checkFunctionApplication fn ty arg ret
742 checkFunctionApplication fn ty arg ret
750checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
743checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
751 dicts <- getTypeClassDictionaries
744 dicts <- getTypeClassDictionaries
745+ hints <- gets checkHints
752− checkFunctionApplication' (foldl App fn (map (flip TypeClassDictionary dicts) constraints)) fnTy arg ret
746+ checkFunctionApplication' (foldl App fn (map (\cs -> TypeClassDictionary cs dicts hints) constraints)) fnTy arg ret
753checkFunctionApplication' fn fnTy dict@TypeClassDictionary{} _ =
747checkFunctionApplication' fn fnTy dict@TypeClassDictionary{} _ =
754 return (fnTy, App fn dict)
748 return (fnTy, App fn dict)
755checkFunctionApplication' _ fnTy arg _ = throwError . errorMessage $ CannotApplyFunction fnTy arg
749checkFunctionApplication' _ fnTy arg _ = throwError . errorMessage $ CannotApplyFunction fnTy arg