@@ -26,7 +26,6 @@ module Language.PureScript.TypeChecker.Types
2626
2727import Prelude.Compat
2828
29−import Control.Arrow (second)
3029import Control.Monad
3130import Control.Monad.Error.Class (MonadError(..))
3231import Control.Monad.State.Class (MonadState(..), gets)
@@ -71,7 +70,7 @@ typesOf ::
7170 m [(Ident, (Expr, Type))]
7271typesOf bindingGroupType moduleName vals = do
7372 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
7574 ds1 <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
7675 ds2 <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
7776 return (map (\x -> (False, x)) ds1 ++ map (\x -> (True, x)) ds2)
@@ -117,26 +116,21 @@ typesOf bindingGroupType moduleName vals = do
117116 tidyUp (ts, sub) = map (\(b, (i, (val, ty))) -> (b, (i, (overTypes (substituteType sub) val, substituteType sub ty)))) ts
118117
119118 -- 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)
125− replace _ em = em
119+ replace sub = onTypesInErrorMessage (substituteType sub)
126120
127121 isHoleError :: ErrorMessage -> Bool
128122 isHoleError (ErrorMessage _ HoleInferredType{}) = True
129123 isHoleError _ = False
130124
131−type TypeData = M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
125+type TypeData = M.Map (Qualified Ident) (Type, NameKind, NameVisibility)
132126
133127type UntypedData = [(Ident, Type)]
134128
135−typeDictionaryForBindingGroup ::
136− (MonadState CheckState m) =>
137− ModuleName ->
138− [(Ident, Expr)] ->
139− m ([(Ident, Expr)], [(Ident, (Expr, Type, Bool))], TypeData, UntypedData)
129+typeDictionaryForBindingGroup
130+ :: (MonadState CheckState m)
131+ => Maybe ModuleName
132+ -> [(Ident, Expr)]
133+ -> m ([(Ident, Expr)], [(Ident, (Expr, Type, Bool))], TypeData, UntypedData)
140134typeDictionaryForBindingGroup moduleName vals = do
141135 let
142136 -- 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
154148 -- Make a map of names to the unification variables of untyped declarations
155149 untypedDict = zip (map fst untyped) untypedNames
156150 -- 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)
158152 return (untyped, typed, dict, untypedDict)
159153
160−checkTypedBindingGroupElement ::
161− (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
162− ModuleName ->
163− (Ident, (Expr, Type, Bool)) ->
164− TypeData ->
165− m (Ident, (Expr, Type))
154+checkTypedBindingGroupElement
155+ :: (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
156+ => ModuleName
157+ -> (Ident, (Expr, Type, Bool))
158+ -> TypeData
159+ -> m (Ident, (Expr, Type))
166160checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
167161 -- Replace type wildcards
168162 ty' <- replaceTypeWildcards ty
@@ -176,12 +170,12 @@ checkTypedBindingGroupElement mn (ident, (val', ty, checkType)) dict = do
176170 else return (TypedValue False val' ty'')
177171 return (ident, (val'', ty''))
178172
179−typeForBindingGroupElement ::
180− (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>
181− (Ident, Expr) ->
182− TypeData ->
183− UntypedData ->
184− m (Ident, (Expr, Type))
173+typeForBindingGroupElement
174+ :: (MonadSupply m, MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m)
175+ => (Ident, Expr)
176+ -> TypeData
177+ -> UntypedData
178+ -> m (Ident, (Expr, Type))
185179typeForBindingGroupElement (ident, val) dict untypedDict = do
186180 -- Infer the type with the new names in scope
187181 TypedValue _ val' ty <- bindNames dict $ infer val
@@ -275,8 +269,7 @@ infer' (Accessor prop val) = withErrorMessageHint (ErrorCheckingAccessor val pro
275269 return $ TypedValue True (Accessor prop typed) field
276270infer' (Abs (Left arg) ret) = do
277271 ty <- freshType
278− Just moduleName <- checkCurrentModule <$> get
279− withBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
272+ withBindingGroupVisible $ bindLocalVariables [(arg, ty, Defined)] $ do
280273 body@(TypedValue _ _ bodyTy) <- infer' ret
281274 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
282275infer' (Abs (Right _) _) = internalError "Binder was not desugared"
@@ -285,9 +278,8 @@ infer' (App f arg) = do
285278 (ret, app) <- checkFunctionApplication f' ft arg Nothing
286279 return $ TypedValue True app ret
287280infer' (Var var) = do
288− Just moduleName <- checkCurrentModule <$> get
289− checkVisibility moduleName var
290− ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable moduleName $ var
281+ checkVisibility var
282+ ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards <=< lookupVariable $ var
291283 case ty of
292284 ConstrainedType constraints ty' -> do
293285 dicts <- getTypeClassDictionaries
@@ -329,9 +321,7 @@ infer' (TypedValue checkType val ty) = do
329321 return $ TypedValue True val' ty'
330322infer' (Hole name) = do
331323 ty <- freshType
332− env <- M.toList . names <$> getEnv
333− Just moduleName <- checkCurrentModule <$> get
334− let ctx = [ (ident, ty') | ((mn, ident@Ident{}), (ty', _, Defined)) <- env, mn == moduleName ]
324+ ctx <- getLocalContext
335325 tell . errorMessage $ HoleInferredType name ty ctx
336326 return $ TypedValue True (Hole name) ty
337327infer' (PositionedValue pos c val) = warnAndRethrowWithPositionTC pos $ do
@@ -351,20 +341,19 @@ inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue
351341 Just moduleName <- checkCurrentModule <$> get
352342 (kind, args) <- kindOfWithScopedVars ty
353343 checkTypeKind ty kind
354− let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
344+ let dict = M.singleton (Qualified Nothing ident) (ty, nameKind, Undefined)
355345 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
356346 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
358348inferLetBinding seen (ValueDeclaration ident nameKind [] (Right val) : rest) ret j = do
359349 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)
362351 TypedValue _ val' valTy' <- bindNames dict $ infer val
363352 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
365354inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
366355 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)
368357 ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
369358 ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
370359 let ds' = [(ident, Private, val') | (ident, (val', _)) <- ds1' ++ ds2']
@@ -483,9 +472,8 @@ checkBinders _ _ [] = return []
483472checkBinders nvals ret (CaseAlternative binders result : bs) = do
484473 guardWith (errorMessage $ OverlappingArgNames Nothing) $
485474 let ns = concatMap binderNames binders in length (nub ns) == length ns
486− Just moduleName <- checkCurrentModule <$> get
487475 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 ] $
489477 CaseAlternative binders <$>
490478 case result of
491479 Left gs -> do
@@ -577,8 +565,7 @@ check' (Literal (ArrayLiteral vals)) t@(TypeApp a ty) = do
577565 return $ TypedValue True array t
578566check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
579567 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
582569 return $ TypedValue True (Abs (Left arg) ret') ty
583570check' (Abs (Right _) _) _ = internalError "Binder was not desugared"
584571check' (App f arg) ret = do
@@ -586,9 +573,8 @@ check' (App f arg) ret = do
586573 (_, app) <- checkFunctionApplication f' ft arg (Just ret)
587574 return $ TypedValue True app ret
588575check' v@(Var var) ty = do
589− Just moduleName <- checkCurrentModule <$> get
590− checkVisibility moduleName var
591− repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
576+ checkVisibility var
577+ repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable $ var
592578 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< replaceTypeWildcards $ ty
593579 v' <- subsumes (Just v) repl ty'
594580 case v' of