@@ -194,7 +194,7 @@ typesOf mainModuleName moduleName vals = do
194 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
194 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
195 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
195 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
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)])
198typeDictionaryForBindingGroup moduleName vals = do
198typeDictionaryForBindingGroup moduleName vals = do
200 -- Map each declaration to a name/value pair, with an optional type, if the declaration is typed
200 -- 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
212 -- Make a map of names to the unification variables of untyped declarations
212 -- Make a map of names to the unification variables of untyped declarations
213 untypedDict = zip (map fst untyped) untypedNames
213 untypedDict = zip (map fst untyped) untypedNames
214 -- Create the dictionary of all name/type pairs, which will be added to the environment during type checking
214 -- 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)
216 return (es, dict, untypedDict)
216 return (es, dict, untypedDict)
218−typeForBindingGroupElement :: ModuleName -> (Ident, (Value, Maybe (Type, Bool))) -> M.Map (ModuleName, Ident) (Type, NameKind) -> [(Ident, Type)] -> UnifyT Type Check (Ident, (Value, Type))
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 e@(_, (val, _)) dict untypedDict = do
219+typeForBindingGroupElement moduleName el dict untypedDict =
220 -- If the declaration is a function, it has access to other values in the binding group.
220 -- If the declaration is a function, it has access to other values in the binding group.
221 -- If not, the generated code might fail at runtime since those values might be undefined.
221 -- 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
224 -- Typed declarations
223 -- Typed declarations
225 (ident, (val', Just (ty, checkType))) -> do
224 (ident, (val', Just (ty, checkType))) -> do
226 -- Kind check
225 -- Kind check
@@ -228,26 +227,17 @@ typeForBindingGroupElement moduleName e@(_, (val, _)) dict untypedDict = do
228 guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
227 guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
229 -- Check the type with the new names in scope
228 -- Check the type with the new names in scope
230 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
229 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
231− val'' <- bindNames dict' $ if checkType
230+ val'' <- bindNames dict $ if checkType
232− then TypedValue True <$> check val' ty' <*> pure ty'
231+ then TypedValue True <$> check val' ty' <*> pure ty'
233− else return (TypedValue False val' ty')
232+ else return (TypedValue False val' ty')
234 return (ident, (val'', ty'))
233 return (ident, (val'', ty'))
235 -- Untyped declarations
234 -- Untyped declarations
236 (ident, (val', Nothing)) -> do
235 (ident, (val', Nothing)) -> do
237 -- Infer the type with the new names in scope
236 -- 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'
239 ty =?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
238 ty =?= fromMaybe (error "name not found in dictionary") (lookup ident untypedDict)
240 return (ident, (TypedValue True val'' ty, ty))
239 return (ident, (TypedValue True val'' ty, ty))
243−-- Check if a value introduces a function
245−isFunction :: Value -> Bool
246−isFunction (Abs _ _) = True
247−isFunction (TypedValue _ val _) = isFunction val
248−isFunction (PositionedValue _ val) = isFunction val
252-- Check if a value contains a type annotation
242-- Check if a value contains a type annotation
@@ -630,7 +620,7 @@ infer' (Accessor prop val) = do
630infer' (Abs (Left arg) ret) = do
620infer' (Abs (Left arg) ret) = do
631 ty <- fresh
621 ty <- fresh
632 Just moduleName <- checkCurrentModule <$> get
622 Just moduleName <- checkCurrentModule <$> get
633− bindLocalVariables moduleName [(arg, ty)] $ do
623+ makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
634 body@(TypedValue _ _ bodyTy) <- infer' ret
624 body@(TypedValue _ _ bodyTy) <- infer' ret
635 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
625 return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
636infer' (Abs (Right _) _) = error "Binder was not desugared"
626infer' (Abs (Right _) _) = error "Binder was not desugared"
@@ -640,6 +630,7 @@ infer' (App f arg) = do
640 return $ TypedValue True app ret
630 return $ TypedValue True app ret
641infer' (Var var) = do
631infer' (Var var) = do
642 Just moduleName <- checkCurrentModule <$> get
632 Just moduleName <- checkCurrentModule <$> get
633+ checkVisibility moduleName var
643 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
634 ty <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
644 case ty of
635 case ty of
645 ConstrainedType constraints ty' -> do
636 ConstrainedType constraints ty' -> do
@@ -680,22 +671,22 @@ infer' (PositionedValue pos val) = rethrowWithPosition pos $ infer' val
680infer' _ = error "Invalid argument to infer"
671infer' _ = error "Invalid argument to infer"
682inferLetBinding :: [Declaration] -> [Declaration] -> Value -> (Value -> UnifyT Type Check Value) -> UnifyT Type Check ([Declaration], Value)
673inferLetBinding :: [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)
684inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing tv@(TypedValue checkType val ty) : rest) ret j = do
675inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing tv@(TypedValue checkType val ty) : rest) ret j = do
685 Just moduleName <- checkCurrentModule <$> get
676 Just moduleName <- checkCurrentModule <$> get
686 kind <- liftCheck $ kindOf moduleName ty
677 kind <- liftCheck $ kindOf moduleName ty
687 guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
678 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)
689 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
680 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
690 TypedValue _ val' ty'' <- if checkType then bindNames dict (check val ty') else return tv
681 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
692inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing val : rest) ret j = do
683inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing val : rest) ret j = do
693 valTy <- fresh
684 valTy <- fresh
694 Just moduleName <- checkCurrentModule <$> get
685 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)
696 TypedValue _ val' valTy' <- bindNames dict $ infer val
687 TypedValue _ val' valTy' <- bindNames dict $ infer val
697 valTy =?= valTy'
688 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
699inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
690inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
700 Just moduleName <- checkCurrentModule <$> get
691 Just moduleName <- checkCurrentModule <$> get
701 (es, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName (map (\(i, _, v) -> (i, v)) ds)
692 (es, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName (map (\(i, _, v) -> (i, v)) ds)
@@ -786,7 +777,7 @@ checkBinders _ _ [] = return []
786checkBinders nvals ret (CaseAlternative binders grd val : bs) = do
777checkBinders nvals ret (CaseAlternative binders grd val : bs) = do
787 Just moduleName <- checkCurrentModule <$> get
778 Just moduleName <- checkCurrentModule <$> get
788 m1 <- M.unions <$> zipWithM inferBinder nvals binders
779 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
790 val' <- TypedValue True <$> check val ret <*> pure ret
781 val' <- TypedValue True <$> check val ret <*> pure ret
791 case grd of
782 case grd of
792 Nothing -> return $ CaseAlternative binders Nothing val'
783 Nothing -> return $ CaseAlternative binders Nothing val'
@@ -861,7 +852,7 @@ check' val t@(ConstrainedType constraints ty) = do
861 dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
852 dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
862 n <- liftCheck freshDictionaryName
853 n <- liftCheck freshDictionaryName
863 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
854 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
864− val' <- withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
855+ val' <- makeBindingGroupVisible $ withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
865 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
856 TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
866 constraints) $ check val ty
857 constraints) $ check val ty
867 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
858 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
@@ -886,7 +877,7 @@ check' (ArrayLiteral vals) t@(TypeApp a ty) = do
886 return $ TypedValue True array t
877 return $ TypedValue True array t
887check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) | t == tyFunction = do
878check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) | t == tyFunction = do
888 Just moduleName <- checkCurrentModule <$> get
879 Just moduleName <- checkCurrentModule <$> get
889− ret' <- bindLocalVariables moduleName [(arg, argTy)] $ check ret retTy
880+ ret' <- makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
890 return $ TypedValue True (Abs (Left arg) ret') ty
881 return $ TypedValue True (Abs (Left arg) ret') ty
891check' (Abs (Right _) _) _ = error "Binder was not desugared"
882check' (Abs (Right _) _) _ = error "Binder was not desugared"
892check' (App f arg) ret = do
883check' (App f arg) ret = do
@@ -895,6 +886,7 @@ check' (App f arg) ret = do
895 return $ TypedValue True app ret
886 return $ TypedValue True app ret
896check' v@(Var var) ty = do
887check' v@(Var var) ty = do
897 Just moduleName <- checkCurrentModule <$> get
888 Just moduleName <- checkCurrentModule <$> get
889+ checkVisibility moduleName var
898 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
890 repl <- introduceSkolemScope <=< replaceAllTypeSynonyms <=< lookupVariable moduleName $ var
899 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
891 ty' <- introduceSkolemScope <=< replaceAllTypeSynonyms $ ty
900 v' <- subsumes (Just v) repl ty'
892 v' <- subsumes (Just v) repl ty'