@@ -194,7 +194,7 @@ typesOf mainModuleName moduleName vals = do
194194 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
195195 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
196196
197−typeDictionaryForBindingGroup :: ModuleName -> [(Ident, Value)] -> UnifyT Type Check ([(Ident, (Value, Maybe (Type, Bool)))], M.Map (ModuleName, Ident) (Type, NameKind), [(Ident, Type)])
197+typeDictionaryForBindingGroup :: ModuleName -> [(Ident, Value)] -> UnifyT Type Check ([(Ident, (Value, Maybe (Type, Bool)))], M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility), [(Ident, Type)])
198198typeDictionaryForBindingGroup moduleName vals = do
199199 let
200200 -- Map each declaration to a name/value pair, with an optional type, if the declaration is typed
@@ -212,15 +212,14 @@ typeDictionaryForBindingGroup moduleName vals = do
212212 -- Make a map of names to the unification variables of untyped declarations
213213 untypedDict = zip (map fst untyped) untypedNames
214214 -- Create the dictionary of all name/type pairs, which will be added to the environment during type checking
215− dict = M.fromList (map (\(ident, ty) -> ((moduleName, ident), (ty, LocalVariable))) $ typedDict ++ untypedDict)
215+ dict = M.fromList (map (\(ident, ty) -> ((moduleName, ident), (ty, LocalVariable, Undefined))) $ typedDict ++ untypedDict)
216216 return (es, dict, untypedDict)
217217
218−typeForBindingGroupElement :: ModuleName -> (Ident, (Value, Maybe (Type, Bool))) -> M.Map (ModuleName, Ident) (Type, NameKind) -> [(Ident, Type)] -> UnifyT Type Check (Ident, (Value, Type))
219−typeForBindingGroupElement moduleName e@(_, (val, _)) dict untypedDict = do
218+typeForBindingGroupElement :: ModuleName -> (Ident, (Value, Maybe (Type, Bool))) -> M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility) -> [(Ident, Type)] -> UnifyT Type Check (Ident, (Value, Type))
219+typeForBindingGroupElement moduleName el dict untypedDict =
220220 -- If the declaration is a function, it has access to other values in the binding group.
221221 -- If not, the generated code might fail at runtime since those values might be undefined.
222− let dict' = if isFunction val then dict else M.empty
223− case e of
222+ case el of
224223 -- Typed declarations
225224 (ident, (val', Just (ty, checkType))) -> do
226225 -- Kind check
@@ -228,26 +227,17 @@ typeForBindingGroupElement moduleName e@(_, (val, _)) dict untypedDict = do
228227 guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
229228 -- Check the type with the new names in scope
230229 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
231− val'' <- bindNames dict' $ if checkType
232− then TypedValue True <$> check val' ty' <*> pure ty'
233− else return (TypedValue False val' ty')
230+ val'' <- bindNames dict $ if checkType
231+ then TypedValue True <$> check val' ty' <*> pure ty'
232+ else return (TypedValue False val' ty')
234233 return (ident, (val'', ty'))
235234 -- Untyped declarations
236235 (ident, (val', Nothing)) -> do
237236 -- Infer the type with the new names in scope
238− TypedValue _ val'' ty <- bindNames dict' $ infer val'
237+ TypedValue _ val'' ty <- bindNames dict $ infer val'
239238 ty =?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
240239 return (ident, (TypedValue True val'' ty, ty))
241240
242−-- |
243−-- Check if a value introduces a function
244−--
245−isFunction :: Value -> Bool
246−isFunction (Abs _ _) = True
247−isFunction (TypedValue _ val _) = isFunction val
248−isFunction (PositionedValue _ val) = isFunction val
249−isFunction _ = False
250−
251241-- |
252242-- Check if a value contains a type annotation
253243--
@@ -630,7 +620,7 @@ infer' (Accessor prop val) = do
630620infer' (Abs (Left arg) ret) = do
631621 ty <- fresh
632622 Just moduleName <- checkCurrentModule <$> get
633− bindLocalVariables moduleName [(arg, ty)] $ do
623+ makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
634624 body@(TypedValue _ _ bodyTy) <- infer' ret
635625 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
636626infer' (Abs (Right _) _) = error "Binder was not desugared"
@@ -640,6 +630,7 @@ infer' (App f arg) = do
640630 return $ TypedValue True app ret
641631infer' (Var var) = do
642632 Just moduleName <- checkCurrentModule <$> get
633+ checkVisibility moduleName var
643634 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
644635 case ty of
645636 ConstrainedType constraints ty' -> do
@@ -680,22 +671,22 @@ infer' (PositionedValue pos val) = rethrowWithPosition pos $ infer' val
680671infer' _ = error "Invalid argument to infer"
681672
682673inferLetBinding :: [Declaration] -> [Declaration] -> Value -> (Value -> UnifyT Type Check Value) -> UnifyT Type Check ([Declaration], Value)
683−inferLetBinding seen [] ret j = (,) seen <$> j ret
674+inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
684675inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing tv@(TypedValue checkType val ty) : rest) ret j = do
685676 Just moduleName <- checkCurrentModule <$> get
686677 kind <- liftCheck $ kindOf moduleName ty
687678 guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
688− let dict = if isFunction val then M.singleton (moduleName, ident) (ty, nameKind) else M.empty
679+ let dict = M.singleton (moduleName, ident) (ty, nameKind, Undefined)
689680 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
690681 TypedValue _ val' ty'' <- if checkType then bindNames dict (check val ty') else return tv
691− bindNames (M.singleton (moduleName, ident) (ty'', nameKind)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] Nothing (TypedValue checkType val' ty'')]) rest ret j
682+ bindNames (M.singleton (moduleName, ident) (ty'', nameKind, Undefined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] Nothing (TypedValue checkType val' ty'')]) rest ret j
692683inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing val : rest) ret j = do
693684 valTy <- fresh
694685 Just moduleName <- checkCurrentModule <$> get
695− let dict = if isFunction val then M.singleton (moduleName, ident) (valTy, nameKind) else M.empty
686+ let dict = M.singleton (moduleName, ident) (valTy, nameKind, Undefined)
696687 TypedValue _ val' valTy' <- bindNames dict $ infer val
697688 valTy =?= valTy'
698− bindNames (M.singleton (moduleName, ident) (valTy', nameKind)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] Nothing val']) rest ret j
689+ bindNames (M.singleton (moduleName, ident) (valTy', nameKind, Undefined)) $ inferLetBinding (seen ++ [ValueDeclaration ident nameKind [] Nothing val']) rest ret j
699690inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
700691 Just moduleName <- checkCurrentModule <$> get
701692 (es, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName (map (\(i, _, v) -> (i, v)) ds)
@@ -786,7 +777,7 @@ checkBinders _ _ [] = return []
786777checkBinders nvals ret (CaseAlternative binders grd val : bs) = do
787778 Just moduleName <- checkCurrentModule <$> get
788779 m1 <- M.unions <$> zipWithM inferBinder nvals binders
789− r <- bindLocalVariables moduleName (M.toList m1) $ do
780+ r <- bindLocalVariables moduleName [ (name, ty, Defined) | (name, ty) <- M.toList m1 ] $ do
790781 val' <- TypedValue True <$> check val ret <*> pure ret
791782 case grd of
792783 Nothing -> return $ CaseAlternative binders Nothing val'
@@ -861,7 +852,7 @@ check' val t@(ConstrainedType constraints ty) = do
861852 dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
862853 n <- liftCheck freshDictionaryName
863854 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
864− val' <- withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
855+ val' <- makeBindingGroupVisible $ withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
865856 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
866857 constraints) $ check val ty
867858 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
@@ -886,7 +877,7 @@ check' (ArrayLiteral vals) t@(TypeApp a ty) = do
886877 return $ TypedValue True array t
887878check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) | t == tyFunction = do
888879 Just moduleName <- checkCurrentModule <$> get
889− ret' <- bindLocalVariables moduleName [(arg, argTy)] $ check ret retTy
880+ ret' <- makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
890881 return $ TypedValue True (Abs (Left arg) ret') ty
891882check' (Abs (Right _) _) _ = error "Binder was not desugared"
892883check' (App f arg) ret = do
@@ -895,6 +886,7 @@ check' (App f arg) ret = do
895886 return $ TypedValue True app ret
896887check' v@(Var var) ty = do
897888 Just moduleName <- checkCurrentModule <$> get
889+ checkVisibility moduleName var
898890 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
899891 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
900892 v' <- subsumes (Just v) repl ty'