@@ -37,6 +37,7 @@ import Language.PureScript.Unknown
3737
3838import Control.Monad.State
3939import Control.Monad.Error
40+import Control.Monad.Reader
4041
4142import Control.Applicative
4243import Control.Arrow (Arrow(..))
@@ -132,28 +133,27 @@ unifyTypes t1 t2 = rethrow (\e -> "Error unifying type " ++ prettyPrintType t1 +
132133 ret1 `unifyTypes` ret2
133134 unifyTypes' (TypeVar v1) (TypeVar v2) | v1 == v2 = return ()
134135 unifyTypes' (TypeConstructor c1) (TypeConstructor c2) = do
135− modulePath <- checkModulePath `fmap` get
136− guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (qualify modulePath c1 == qualify modulePath c2)
136+ moduleName <- substCurrentModule `fmap` ask
137+ guardWith ("Cannot unify " ++ show c1 ++ " with " ++ show c2 ++ ".") (qualify moduleName c1 == qualify moduleName c2)
137138 unifyTypes' (TypeApp t3 t4) (TypeApp t5 t6) = do
138139 t3 `unifyTypes` t5
139140 t4 `unifyTypes` t6
140141 unifyTypes' (Skolem s1) (Skolem s2) | s1 == s2 = return ()
141142 unifyTypes' t3 t4 = throwError $ "Cannot unify " ++ prettyPrintType t3 ++ " with " ++ prettyPrintType t4 ++ "."
142143
143−typesOf :: [(Ident, Value)] -> Check [Type]
144−typesOf vals = do
145− (tys, sub, checks) <- runSubst $ do
146− modulePath <- checkModulePath <$> get
144+typesOf :: ModuleName -> [(Ident, Value)] -> Check [Type]
145+typesOf moduleName vals = do
146+ (tys, sub, checks) <- runSubst (SubstContext moduleName) $ do
147147 let es = map isTyped vals
148148 typed = lefts es
149149 untyped = rights es
150150 typedDict = map (\(ident, ty, _) -> (ident, ty)) typed
151151 untypedNames <- replicateM (length untyped) fresh
152152 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)
154154 tys <- forM es $ \e -> case e of
155155 Left (_, ty, val) -> do
156− kind <- liftCheck $ kindOf ty
156+ kind <- liftCheck $ kindOf moduleName ty
157157 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
158158 ty' <- replaceAllTypeSynonyms ty
159159 bindNames dict $ check val ty'
@@ -241,7 +241,7 @@ replaceVarWithUnknown ident ty = do
241241replaceAllTypeSynonyms :: (Functor m, MonadState CheckState m, MonadError String m) => (D.Data d) => d -> m d
242242replaceAllTypeSynonyms d = do
243243 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
245245 either throwError return $ saturateAllTypeSynonyms syns d
246246
247247desaturateAllTypeSynonyms :: (D.Data d) => d -> d
@@ -253,8 +253,8 @@ desaturateAllTypeSynonyms = everywhere (mkT replaceSaturatedTypeSynonym)
253253expandTypeSynonym :: Qualified ProperName -> [Type] -> Subst Type
254254expandTypeSynonym name args = do
255255 env <- getEnv
256− modulePath <- checkModulePath `fmap` get
257− case M.lookup (qualify modulePath name) (typeSynonyms env) of
256+ moduleName <- substCurrentModule `fmap` ask
257+ case M.lookup (qualify moduleName name) (typeSynonyms env) of
258258 Just (synArgs, body) -> return $ replaceAllTypeVars (zip synArgs args) body
259259 Nothing -> error "Type synonym was not defined"
260260
@@ -312,7 +312,8 @@ infer' (Accessor prop val) = do
312312 Just ty -> return ty
313313infer' (Abs args ret) = do
314314 ts <- replicateM (length args) fresh
315− bindLocalVariables (zip args ts) $ do
315+ moduleName <- substCurrentModule <$> ask
316+ bindLocalVariables moduleName (zip args ts) $ do
316317 body <- infer' ret
317318 return $ Function ts body
318319infer' app@(App _ _) = do
@@ -322,7 +323,8 @@ infer' app@(App _ _) = do
322323 checkFunctionApplications ft argss ret
323324 return ret
324325infer' (Var var) = do
325− ty <- lookupVariable var
326+ moduleName <- substCurrentModule <$> ask
327+ ty <- lookupVariable moduleName var
326328 replaceAllTypeSynonyms ty
327329infer' (Block ss) = do
328330 ret <- fresh
@@ -331,8 +333,8 @@ infer' (Block ss) = do
331333 return ret
332334infer' (Constructor c) = do
333335 env <- getEnv
334− modulePath <- checkModulePath `fmap` get
335− case M.lookup (qualify modulePath c) (dataConstructors env) of
336+ moduleName <- substCurrentModule `fmap` ask
337+ case M.lookup (qualify moduleName c) (dataConstructors env) of
336338 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
337339 Just ty -> replaceAllTypeSynonyms ty
338340infer' (Case vals binders) = do
@@ -347,7 +349,8 @@ infer' (IfThenElse cond th el) = do
347349 t2 ~~ t3
348350 return t2
349351infer' (TypedValue val ty) = do
350− kind <- liftCheck $ kindOf ty
352+ moduleName <- substCurrentModule <$> ask
353+ kind <- liftCheck $ kindOf moduleName ty
351354 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
352355 ty' <- replaceAllTypeSynonyms ty
353356 check val ty'
@@ -444,16 +447,16 @@ inferBinder val (BooleanBinder _) = val ~~ Boolean >> return M.empty
444447inferBinder val (VarBinder name) = return $ M.singleton name val
445448inferBinder val (NullaryBinder ctor) = do
446449 env <- getEnv
447− modulePath <- checkModulePath <$> get
448− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
450+ moduleName <- substCurrentModule <$> ask
451+ case M.lookup (qualify moduleName ctor) (dataConstructors env) of
449452 Just ty -> do
450453 ty `subsumes` val
451454 return M.empty
452455 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
453456inferBinder val (UnaryBinder ctor binder) = do
454457 env <- getEnv
455− modulePath <- checkModulePath <$> get
456− case M.lookup (qualify modulePath ctor) (dataConstructors env) of
458+ moduleName <- substCurrentModule <$> ask
459+ case M.lookup (qualify moduleName ctor) (dataConstructors env) of
457460 Just ty -> do
458461 fn <- replaceAllVarsWithUnknowns ty
459462 case fn of
@@ -494,8 +497,9 @@ inferBinder val (NamedBinder name binder) = do
494497checkBinders :: [Type] -> Type -> [([Binder], Maybe Guard, Value)] -> Subst ()
495498checkBinders _ _ [] = return ()
496499checkBinders nvals ret ((binders, grd, val):bs) = do
500+ moduleName <- substCurrentModule <$> ask
497501 m1 <- M.unions <$> zipWithM inferBinder nvals binders
498− bindLocalVariables (M.toList m1) $ do
502+ bindLocalVariables moduleName (M.toList m1) $ do
499503 check val ret
500504 case grd of
501505 Nothing -> return ()
@@ -505,8 +509,8 @@ checkBinders nvals ret ((binders, grd, val):bs) = do
505509assignVariable :: Ident -> Subst ()
506510assignVariable name = do
507511 env <- checkEnv <$> get
508− modulePath <- checkModulePath <$> get
509− case M.lookup (modulePath, name) (names env) of
512+ moduleName <- substCurrentModule <$> ask
513+ case M.lookup (moduleName, name) (names env) of
510514 Just (_, LocalVariable) -> throwError $ "Variable with name " ++ show name ++ " already exists."
511515 _ -> return ()
512516
@@ -529,16 +533,18 @@ checkStatement mass ret (If ifst) = do
529533 allCodePathsReturn <- checkIfStatement mass ret ifst
530534 return (allCodePathsReturn, mass)
531535checkStatement mass ret (For ident start end inner) = do
536+ moduleName <- substCurrentModule <$> ask
532537 assignVariable ident
533538 check start Number
534539 check end Number
535− (allCodePathsReturn, _) <- bindLocalVariables [(ident, Number)] $ checkBlock mass ret inner
540+ (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, Number)] $ checkBlock mass ret inner
536541 return (allCodePathsReturn, mass)
537542checkStatement mass ret (ForEach ident vals inner) = do
543+ moduleName <- substCurrentModule <$> ask
538544 assignVariable ident
539545 val <- fresh
540546 check vals (Array val)
541− (allCodePathsReturn, _) <- bindLocalVariables [(ident, val)] $ checkBlock mass ret inner
547+ (allCodePathsReturn, _) <- bindLocalVariables moduleName [(ident, val)] $ checkBlock mass ret inner
542548 guardWith "Cannot return from within a foreach block" $ not allCodePathsReturn
543549 return (False, mass)
544550checkStatement mass _ (ValueStatement val) = do
@@ -566,8 +572,9 @@ checkElseStatement mass ret (ElseIf ifst) = checkIfStatement mass ret ifst
566572checkBlock :: M.Map Ident Type -> Type -> [Statement] -> Subst (Bool, M.Map Ident Type)
567573checkBlock mass _ [] = return (False, mass)
568574checkBlock mass ret (s:ss) = do
575+ moduleName <- substCurrentModule <$> ask
569576 (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
571578 (True, []) -> return (True, mass1)
572579 (True, _) -> throwError "Unreachable code"
573580 (False, ss') -> checkBlock mass1 ret ss'
@@ -606,18 +613,21 @@ check' (Binary op left right) ty = checkBinary op left right ty
606613check' (ArrayLiteral vals) (Array ty) = forM_ vals (\val -> check val ty)
607614check' (Indexer index vals) ty = check index Number >> check vals (Array ty)
608615check' (Abs args ret) (Function argTys retTy) = do
616+ moduleName <- substCurrentModule <$> ask
609617 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
611619check' app@(App _ _) ret = do
612620 let (f, argss) = unfoldApplication app
613621 ft <- infer f
614622 checkFunctionApplications ft argss ret
615623check' (Var var) ty = do
616− ty1 <- lookupVariable var
624+ moduleName <- substCurrentModule <$> ask
625+ ty1 <- lookupVariable moduleName var
617626 repl <- replaceAllTypeSynonyms ty1
618627 repl `subsumes` ty
619628check' (TypedValue val ty1) ty2 = do
620− kind <- liftCheck $ kindOf ty1
629+ moduleName <- substCurrentModule <$> ask
630+ kind <- liftCheck $ kindOf moduleName ty1
621631 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
622632 ty1 `subsumes` ty2
623633 check val ty1
@@ -647,8 +657,8 @@ check' (Block ss) ret = do
647657 guardWith "Block is missing a return statement" allCodePathsReturn
648658check' (Constructor c) ty = do
649659 env <- getEnv
650− modulePath <- checkModulePath <$> get
651− case M.lookup (qualify modulePath c) (dataConstructors env) of
660+ moduleName <- substCurrentModule <$> ask
661+ case M.lookup (qualify moduleName c) (dataConstructors env) of
652662 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
653663 Just ty1 -> do
654664 repl <- replaceAllTypeSynonyms ty1