@@ -37,6 +37,7 @@ import Language.PureScript.Unknown
38import Control.Monad.State
38import Control.Monad.State
39import Control.Monad.Error
39import Control.Monad.Error
40+import Control.Monad.Reader
41import Control.Applicative
42import Control.Applicative
42import Control.Arrow (Arrow(..))
43import Control.Arrow (Arrow(..))
@@ -132,28 +133,27 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
132 ret1 `unifyTypes` ret2
133 ret1 `unifyTypes` ret2
133 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
134 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
134 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
135 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
135− modulePath <- checkModulePath `fmap` get
136+ moduleName <- substCurrentModule `fmap` ask
136− guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (qualify modulePath c1 == qualify modulePath c2)
137+ guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (qualify moduleName c1 == qualify moduleName c2)
137 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
138 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
138 t3 `unifyTypes` t5
139 t3 `unifyTypes` t5
139 t4 `unifyTypes` t6
140 t4 `unifyTypes` t6
140 unifyTypes' (Skolem s1) (Skolem s2) | s1 == s2 = return ()
141 unifyTypes' (Skolem s1) (Skolem s2) | s1 == s2 = return ()
141 unifyTypes' t3 t4 = throwError $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
142 unifyTypes' t3 t4 = throwError $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
143−typesOf :: [(Ident, Value)] -> Check [Type]
144+typesOf :: ModuleName -> [(Ident, Value)] -> Check [Type]
144−typesOf vals = do
145+typesOf moduleName vals = do
145− (tys, sub, checks) <- runSubst $ do
146+ (tys, sub, checks) <- runSubst (SubstContext moduleName) $ do
146− modulePath <- checkModulePath <$> get
147 let es = map isTyped vals
147 let es = map isTyped vals
148 typed = lefts es
148 typed = lefts es
149 untyped = rights es
149 untyped = rights es
150 typedDict = map (\(ident, ty, _) -> (ident, ty)) typed
150 typedDict = map (\(ident, ty, _) -> (ident, ty)) typed
151 untypedNames <- replicateM (length untyped) fresh
151 untypedNames <- replicateM (length untyped) fresh
152 let untypedDict = zip (map fst untyped) untypedNames
152 let untypedDict = zip (map fst untyped) untypedNames
153− dict = M.fromList (map (\(ident, ty) -> ((modulePath, ident), (ty, LocalVariable))) $ typedDict ++ untypedDict)
153+ dict = M.fromList (map (\(ident, ty) -> ((moduleName, ident), (ty, LocalVariable))) $ typedDict ++ untypedDict)
154 tys <- forM es $ \e -> case e of
154 tys <- forM es $ \e -> case e of
155 Left (_, ty, val) -> do
155 Left (_, ty, val) -> do
156− kind <- liftCheck $ kindOf ty
156+ kind <- liftCheck $ kindOf moduleName ty
157 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
157 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
158 ty' <- replaceAllTypeSynonyms ty
158 ty' <- replaceAllTypeSynonyms ty
159 bindNames dict $ check val ty'
159 bindNames dict $ check val ty'
@@ -241,7 +241,7 @@ replaceVarWithUnknown ident ty = do
241replaceAllTypeSynonyms :: (Functor m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
241replaceAllTypeSynonyms :: (Functor m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
242replaceAllTypeSynonyms d = do
242replaceAllTypeSynonyms d = do
243 env <- getEnv
243 env <- getEnv
244− let syns = map (\((path, name), (args, _)) -> (Qualified path name, length args)) . M.toList $ typeSynonyms env
244+ let syns = map (\((path, name), (args, _)) -> (Qualified (Just path) name, length args)) . M.toList $ typeSynonyms env
245 either throwError return $ saturateAllTypeSynonyms syns d
245 either throwError return $ saturateAllTypeSynonyms syns d
247desaturateAllTypeSynonyms :: (D.Data d) => d -> d
247desaturateAllTypeSynonyms :: (D.Data d) => d -> d
@@ -253,8 +253,8 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
253expandTypeSynonym :: Qualified ProperName -> [Type] -> Subst Type
253expandTypeSynonym :: Qualified ProperName -> [Type] -> Subst Type
254expandTypeSynonym name args = do
254expandTypeSynonym name args = do
255 env <- getEnv
255 env <- getEnv
256− modulePath <- checkModulePath `fmap` get
256+ moduleName <- substCurrentModule `fmap` ask
257− case M.lookup (qualify modulePath name) (typeSynonyms env) of
257+ case M.lookup (qualify moduleName name) (typeSynonyms env) of
258 Just (synArgs, body) -> return $ replaceAllTypeVars (zip synArgs args) body
258 Just (synArgs, body) -> return $ replaceAllTypeVars (zip synArgs args) body
259 Nothing -> error "Type synonym was not defined"
259 Nothing -> error "Type synonym was not defined"
@@ -312,7 +312,8 @@ infer' (Accessor prop val) = do
312 Just ty -> return ty
312 Just ty -> return ty
313infer' (Abs args ret) = do
313infer' (Abs args ret) = do
314 ts <- replicateM (length args) fresh
314 ts <- replicateM (length args) fresh
315+ moduleName <- substCurrentModule <$> ask
315− bindLocalVariables (zip args ts) $ do
316+ bindLocalVariables moduleName (zip args ts) $ do
316 body <- infer' ret
317 body <- infer' ret
317 return $ Function ts body
318 return $ Function ts body
318infer' app@(App _ _) = do
319infer' app@(App _ _) = do
@@ -322,7 +323,8 @@ infer' app@(App _ _) = do
322 checkFunctionApplications ft argss ret
323 checkFunctionApplications ft argss ret
323 return ret
324 return ret
324infer' (Var var) = do
325infer' (Var var) = do
326+ moduleName <- substCurrentModule <$> ask
325− ty <- lookupVariable var
327+ ty <- lookupVariable moduleName var
326 replaceAllTypeSynonyms ty
328 replaceAllTypeSynonyms ty
327infer' (Block ss) = do
329infer' (Block ss) = do
328 ret <- fresh
330 ret <- fresh
@@ -331,8 +333,8 @@ infer' (Block ss) = do
331 return ret
333 return ret
332infer' (Constructor c) = do
334infer' (Constructor c) = do
333 env <- getEnv
335 env <- getEnv
334− modulePath <- checkModulePath `fmap` get
336+ moduleName <- substCurrentModule `fmap` ask
335− case M.lookup (qualify modulePath c) (dataConstructors env) of
337+ case M.lookup (qualify moduleName c) (dataConstructors env) of
336 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
338 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
337 Just ty -> replaceAllTypeSynonyms ty
339 Just ty -> replaceAllTypeSynonyms ty
338infer' (Case vals binders) = do
340infer' (Case vals binders) = do
@@ -347,7 +349,8 @@ infer' (IfThenElse cond th el) = do
348 return t2
350 return t2
349infer' (TypedValue val ty) = do
351infer' (TypedValue val ty) = do
352+ moduleName <- substCurrentModule <$> ask
350− kind <- liftCheck $ kindOf ty
353+ kind <- liftCheck $ kindOf moduleName ty
351 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
354 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
352 ty' <- replaceAllTypeSynonyms ty
355 ty' <- replaceAllTypeSynonyms ty
353 check val ty'
356 check val ty'
@@ -444,16 +447,16 @@ inferBinder val (BooleanBinder _) = val ~~ Boolean >> return M.empty
444inferBinder val (VarBinder name) = return $ M.singleton name val
447inferBinder val (VarBinder name) = return $ M.singleton name val
445inferBinder val (NullaryBinder ctor) = do
448inferBinder val (NullaryBinder ctor) = do
446 env <- getEnv
449 env <- getEnv
447− modulePath <- checkModulePath <$> get
450+ moduleName <- substCurrentModule <$> ask
448− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
451+ case M.lookup (qualify moduleName ctor) (dataConstructors env) of
449 Just ty -> do
452 Just ty -> do
450 ty `subsumes` val
453 ty `subsumes` val
451 return M.empty
454 return M.empty
452 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
455 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
453inferBinder val (UnaryBinder ctor binder) = do
456inferBinder val (UnaryBinder ctor binder) = do
454 env <- getEnv
457 env <- getEnv
455− modulePath <- checkModulePath <$> get
458+ moduleName <- substCurrentModule <$> ask
456− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
459+ case M.lookup (qualify moduleName ctor) (dataConstructors env) of
457 Just ty -> do
460 Just ty -> do
458 fn <- replaceAllVarsWithUnknowns ty
461 fn <- replaceAllVarsWithUnknowns ty
459 case fn of
462 case fn of
@@ -494,8 +497,9 @@ inferBinder val (NamedBinder name binder) = do
494checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> Subst ()
497checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> Subst ()
495checkBinders _ _ [] = return ()
498checkBinders _ _ [] = return ()
496checkBinders nvals ret ((binders, grd, val):bs) = do
499checkBinders nvals ret ((binders, grd, val):bs) = do
500+ moduleName <- substCurrentModule <$> ask
497 m1 <- M.unions <$> zipWithM inferBinder nvals binders
501 m1 <- M.unions <$> zipWithM inferBinder nvals binders
498− bindLocalVariables (M.toList m1) $ do
502+ bindLocalVariables moduleName (M.toList m1) $ do
499 check val ret
503 check val ret
500 case grd of
504 case grd of
501 Nothing -> return ()
505 Nothing -> return ()
@@ -505,8 +509,8 @@ checkBinders nvals ret ((binders, grd, val):bs) = do
505assignVariable :: Ident -> Subst ()
509assignVariable :: Ident -> Subst ()
506assignVariable name = do
510assignVariable name = do
507 env <- checkEnv <$> get
511 env <- checkEnv <$> get
508− modulePath <- checkModulePath <$> get
512+ moduleName <- substCurrentModule <$> ask
509− case M.lookup (modulePath, name) (names env) of
513+ case M.lookup (moduleName, name) (names env) of
510 Just (_, LocalVariable) -> throwError $ "Variable with name " ++ show name ++ " already exists."
514 Just (_, LocalVariable) -> throwError $ "Variable with name " ++ show name ++ " already exists."
511 _ -> return ()
515 _ -> return ()
@@ -529,16 +533,18 @@ checkStatement mass ret (If ifst) = do
529 allCodePathsReturn <- checkIfStatement mass ret ifst
533 allCodePathsReturn <- checkIfStatement mass ret ifst
530 return (allCodePathsReturn, mass)
534 return (allCodePathsReturn, mass)
531checkStatement mass ret (For ident start end inner) = do
535checkStatement mass ret (For ident start end inner) = do
536+ moduleName <- substCurrentModule <$> ask
532 assignVariable ident
537 assignVariable ident
533 check start Number
538 check start Number
534 check end Number
539 check end Number
535− (allCodePathsReturn, _) <- bindLocalVariables [(ident, Number)] $ checkBlock mass ret inner
540+ (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, Number)] $ checkBlock mass ret inner
536 return (allCodePathsReturn, mass)
541 return (allCodePathsReturn, mass)
537checkStatement mass ret (ForEach ident vals inner) = do
542checkStatement mass ret (ForEach ident vals inner) = do
543+ moduleName <- substCurrentModule <$> ask
538 assignVariable ident
544 assignVariable ident
539 val <- fresh
545 val <- fresh
540 check vals (Array val)
546 check vals (Array val)
541− (allCodePathsReturn, _) <- bindLocalVariables [(ident, val)] $ checkBlock mass ret inner
547+ (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, val)] $ checkBlock mass ret inner
542 guardWith "Cannot return from within a foreach block" $ not allCodePathsReturn
548 guardWith "Cannot return from within a foreach block" $ not allCodePathsReturn
543 return (False, mass)
549 return (False, mass)
544checkStatement mass _ (ValueStatement val) = do
550checkStatement mass _ (ValueStatement val) = do
@@ -566,8 +572,9 @@ checkElseStatement mass ret (ElseIf ifst) = checkIfStatement mass ret ifst
566checkBlock :: M.Map Ident Type -> Type -> [Statement] -> Subst (Bool, M.Map Ident Type)
572checkBlock :: M.Map Ident Type -> Type -> [Statement] -> Subst (Bool, M.Map Ident Type)
567checkBlock mass _ [] = return (False, mass)
573checkBlock mass _ [] = return (False, mass)
568checkBlock mass ret (s:ss) = do
574checkBlock mass ret (s:ss) = do
575+ moduleName <- substCurrentModule <$> ask
569 (b1, mass1) <- checkStatement mass ret s
576 (b1, mass1) <- checkStatement mass ret s
570− bindLocalVariables (M.toList mass1) $ case (b1, ss) of
577+ bindLocalVariables moduleName (M.toList mass1) $ case (b1, ss) of
571 (True, []) -> return (True, mass1)
578 (True, []) -> return (True, mass1)
572 (True, _) -> throwError "Unreachable code"
579 (True, _) -> throwError "Unreachable code"
573 (False, ss') -> checkBlock mass1 ret ss'
580 (False, ss') -> checkBlock mass1 ret ss'
@@ -606,18 +613,21 @@ check' (Binary op left right) ty = checkBinary op left right ty
606check' (ArrayLiteral vals) (Array ty) = forM_ vals (\val -> check val ty)
613check' (ArrayLiteral vals) (Array ty) = forM_ vals (\val -> check val ty)
607check' (Indexer index vals) ty = check index Number >> check vals (Array ty)
614check' (Indexer index vals) ty = check index Number >> check vals (Array ty)
608check' (Abs args ret) (Function argTys retTy) = do
615check' (Abs args ret) (Function argTys retTy) = do
616+ moduleName <- substCurrentModule <$> ask
609 guardWith "Incorrect number of function arguments" (length args == length argTys)
617 guardWith "Incorrect number of function arguments" (length args == length argTys)
610− bindLocalVariables (zip args argTys) $ check ret retTy
618+ bindLocalVariables moduleName (zip args argTys) $ check ret retTy
611check' app@(App _ _) ret = do
619check' app@(App _ _) ret = do
612 let (f, argss) = unfoldApplication app
620 let (f, argss) = unfoldApplication app
613 ft <- infer f
621 ft <- infer f
614 checkFunctionApplications ft argss ret
622 checkFunctionApplications ft argss ret
615check' (Var var) ty = do
623check' (Var var) ty = do
624+ moduleName <- substCurrentModule <$> ask
616− ty1 <- lookupVariable var
625+ ty1 <- lookupVariable moduleName var
617 repl <- replaceAllTypeSynonyms ty1
626 repl <- replaceAllTypeSynonyms ty1
618 repl `subsumes` ty
627 repl `subsumes` ty
619check' (TypedValue val ty1) ty2 = do
628check' (TypedValue val ty1) ty2 = do
629+ moduleName <- substCurrentModule <$> ask
620− kind <- liftCheck $ kindOf ty1
630+ kind <- liftCheck $ kindOf moduleName ty1
621 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
631 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
622 ty1 `subsumes` ty2
632 ty1 `subsumes` ty2
623 check val ty1
633 check val ty1
@@ -647,8 +657,8 @@ check' (Block ss) ret = do
647 guardWith "Block is missing a return statement" allCodePathsReturn
657 guardWith "Block is missing a return statement" allCodePathsReturn
648check' (Constructor c) ty = do
658check' (Constructor c) ty = do
649 env <- getEnv
659 env <- getEnv
650− modulePath <- checkModulePath <$> get
660+ moduleName <- substCurrentModule <$> ask
651− case M.lookup (qualify modulePath c) (dataConstructors env) of
661+ case M.lookup (qualify moduleName c) (dataConstructors env) of
652 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
662 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
653 Just ty1 -> do
663 Just ty1 -> do
654 repl <- replaceAllTypeSynonyms ty1
664 repl <- replaceAllTypeSynonyms ty1