@@ -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
@@ -275,8 +269,7 @@ infer' (Accessor prop val) = withErrorMessageHint (ErrorCheckingAccessor val pro
275 return $ TypedValue True (Accessor prop typed) field
269 return $ TypedValue True (Accessor prop typed) field
276infer' (Abs (Left arg) ret) = do
270infer' (Abs (Left arg) ret) = do
277 ty <- freshType
271 ty <- freshType
278− Just moduleName <- checkCurrentModule <$> get
279− withBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
272+ withBindingGroupVisible $ bindLocalVariables [(arg, ty, Defined)] $ do
280 body@(TypedValue _ _ bodyTy) <- infer' ret
273 body@(TypedValue _ _ bodyTy) <- infer' ret
281 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
274 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
282infer' (Abs (Right _) _) = internalError "Binder was not desugared"
275infer' (Abs (Right _) _) = internalError "Binder was not desugared"
@@ -285,9 +278,8 @@ infer' (App f arg) = do
285 (ret, app) <- checkFunctionApplication f' ft arg Nothing
278 (ret, app) <- checkFunctionApplication f' ft arg Nothing
286 return $ TypedValue True app ret
279 return $ TypedValue True app ret
287infer' (Var var) = do
280infer' (Var var) = do
288− Just moduleName <- checkCurrentModule <$> get
289− checkVisibility moduleName var
281+ checkVisibility var
290− ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable moduleName $ var
282+ ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable $ var
291 case ty of
283 case ty of
292 ConstrainedType constraints ty' -> do
284 ConstrainedType constraints ty' -> do
293 dicts <- getTypeClassDictionaries
285 dicts <- getTypeClassDictionaries
@@ -329,9 +321,7 @@ infer' (TypedValue checkType val ty) = do
329 return $ TypedValue True val' ty'
321 return $ TypedValue True val' ty'
330infer' (Hole name) = do
322infer' (Hole name) = do
331 ty <- freshType
323 ty <- freshType
332− env <- M.toList . names <$> getEnv
324+ ctx <- getLocalContext
333− Just moduleName <- checkCurrentModule <$> get
334− let ctx = [ (ident, ty') | ((mn, ident@Ident{}), (ty', _, Defined)) <- env, mn == moduleName ]
335 tell . errorMessage $ HoleInferredType name ty ctx
325 tell . errorMessage $ HoleInferredType name ty ctx
336 return $ TypedValue True (Hole name) ty
326 return $ TypedValue True (Hole name) ty
337infer' (PositionedValue pos c val) = warnAndRethrowWithPositionTC pos $ do
327infer' (PositionedValue pos c val) = warnAndRethrowWithPositionTC pos $ do
@@ -351,20 +341,19 @@ inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue
351 Just moduleName <- checkCurrentModule <$> get
341 Just moduleName <- checkCurrentModule <$> get
352 (kind, args) <- kindOfWithScopedVars ty
342 (kind, args) <- kindOfWithScopedVars ty
353 checkTypeKind ty kind
343 checkTypeKind ty kind
354− let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
344+ let dict = M.singleton (Qualified Nothing ident) (ty, nameKind, Undefined)
355 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
345 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
356 TypedValue _ val' ty'' <- if checkType then withScopedTypeVars moduleName args (bindNames dict (check val ty')) else return tv
346 TypedValue _ val' ty'' <- if checkType then withScopedTypeVars moduleName args (bindNames dict (check val ty')) else return tv
357− bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
347+ bindNames (M.singleton (Qualified Nothing ident) (ty'', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right (TypedValue checkType val' ty''))]) rest ret j
358inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
348inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
359 valTy <- freshType
349 valTy <- freshType
360− Just moduleName <- checkCurrentModule <$> get
361− let dict = M.singleton (moduleName, ident) (valTy, nameKind, Undefined)
350+ let dict = M.singleton (Qualified Nothing ident) (valTy, nameKind, Undefined)
362 TypedValue _ val' valTy' <- bindNames dict $ infer val
351 TypedValue _ val' valTy' <- bindNames dict $ infer val
363 unifyTypes valTy valTy'
352 unifyTypes valTy valTy'
364− bindNames (M.singleton (moduleName, ident) (valTy', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right val')]) rest ret j
353+ bindNames (M.singleton (Qualified Nothing ident) (valTy', nameKind, Defined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] (Right val')]) rest ret j
365inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
354inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
366 Just moduleName <- checkCurrentModule <$> get
355 Just moduleName <- checkCurrentModule <$> get
367− (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName (map (\(i, _, v) -> (i, v)) ds)
356+ (untyped, typed, dict, untypedDict) <- typeDictionaryForBindingGroup Nothing (map (\(i, _, v) -> (i, v)) ds)
368 ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
357 ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
369 ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
358 ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
370 let ds' = [(ident, Private, val') | (ident, (val', _)) <- ds1' ++ ds2']
359 let ds' = [(ident, Private, val') | (ident, (val', _)) <- ds1' ++ ds2']
@@ -483,9 +472,8 @@ checkBinders _ _ [] = return []
483checkBinders nvals ret (CaseAlternative binders result : bs) = do
472checkBinders nvals ret (CaseAlternative binders result : bs) = do
484 guardWith (errorMessage $ OverlappingArgNames Nothing) $
473 guardWith (errorMessage $ OverlappingArgNames Nothing) $
485 let ns = concatMap binderNames binders in length (nub ns) == length ns
474 let ns = concatMap binderNames binders in length (nub ns) == length ns
486− Just moduleName <- checkCurrentModule <$> get
487 m1 <- M.unions <$> zipWithM inferBinder nvals binders
475 m1 <- M.unions <$> zipWithM inferBinder nvals binders
488− r <- bindLocalVariables moduleName [ (name, ty, Defined) | (name, ty) <- M.toList m1 ] $
476+ r <- bindLocalVariables [ (name, ty, Defined) | (name, ty) <- M.toList m1 ] $
489 CaseAlternative binders <$>
477 CaseAlternative binders <$>
490 case result of
478 case result of
491 Left gs -> do
479 Left gs -> do
@@ -577,8 +565,7 @@ check' (Literal (ArrayLiteral vals)) t@(TypeApp a ty) = do
577 return $ TypedValue True array t
565 return $ TypedValue True array t
578check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
566check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
579 unifyTypes t tyFunction
567 unifyTypes t tyFunction
580− Just moduleName <- checkCurrentModule <$> get
581− ret' <- withBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
568+ ret' <- withBindingGroupVisible $ bindLocalVariables [(arg, argTy, Defined)] $ check ret retTy
582 return $ TypedValue True (Abs (Left arg) ret') ty
569 return $ TypedValue True (Abs (Left arg) ret') ty
583check' (Abs (Right _) _) _ = internalError "Binder was not desugared"
570check' (Abs (Right _) _) _ = internalError "Binder was not desugared"
584check' (App f arg) ret = do
571check' (App f arg) ret = do
@@ -586,9 +573,8 @@ check' (App f arg) ret = do
586 (_, app) <- checkFunctionApplication f' ft arg (Just ret)
573 (_, app) <- checkFunctionApplication f' ft arg (Just ret)
587 return $ TypedValue True app ret
574 return $ TypedValue True app ret
588check' v@(Var var) ty = do
575check' v@(Var var) ty = do
589− Just moduleName <- checkCurrentModule <$> get
590− checkVisibility moduleName var
576+ checkVisibility var
591− repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
577+ repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable $ var
592 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
578 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
593 v' <- subsumes (Just v) repl ty'
579 v' <- subsumes (Just v) repl ty'
594 case v' of
580 case v' of