git.iko.soy/rowtype-yoga/purescript-native
on 24872f8 help sign in
148 lines 7.1 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.TypeChecker
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE FlexibleInstances #-}
17module Language.PureScript.TypeChecker (
18 module T,
19 typeCheckAll
20) where
22import Language.PureScript.TypeChecker.Monad as T
23import Language.PureScript.TypeChecker.Kinds as T
24import Language.PureScript.TypeChecker.Types as T
25import Language.PureScript.TypeChecker.Synonyms as T
27import Data.Maybe
28import qualified Data.Map as M
30import Language.PureScript.Types
31import Language.PureScript.Names
32import Language.PureScript.Kinds
33import Language.PureScript.Declarations
35import Control.Monad.State
36import Control.Monad.Error
38typeCheckAll :: [Declaration] -> Check ()
39typeCheckAll [] = return ()
40typeCheckAll (DataDeclaration name args dctors : rest) = do
41 rethrow (("Error in type constructor " ++ show name ++ ":\n") ++) $ do
42 env <- getEnv
43 modulePath <- checkModulePath `fmap` get
44 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
45 ctorKind <- kindsOf (Just name) args (mapMaybe snd dctors)
46 putEnv $ env { types = M.insert (modulePath, name) (ctorKind, Data) (types env) }
47 forM_ dctors $ \(dctor, maybeTy) ->
48 rethrow (("Error in data constructor " ++ show name ++ ":\n") ++) $ do
49 env' <- getEnv
50 guardWith (show dctor ++ " is already defined") $ not $ M.member (modulePath, dctor) (dataConstructors env')
51 let retTy = foldl TypeApp (TypeConstructor (Qualified modulePath name)) (map TypeVar args)
52 let dctorTy = maybe retTy (\ty -> Function [ty] retTy) maybeTy
53 let polyType = mkForAll args dctorTy
54 putEnv $ env' { dataConstructors = M.insert (modulePath, dctor) polyType (dataConstructors env') }
55 typeCheckAll rest
56typeCheckAll (TypeSynonymDeclaration name args ty : rest) = do
57 rethrow (("Error in type synonym " ++ show name ++ ":\n") ++) $ do
58 env <- getEnv
59 modulePath <- checkModulePath `fmap` get
60 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
61 kind <- kindsOf (Just name) args [ty]
62 putEnv $ env { types = M.insert (modulePath, name) (kind, TypeSynonym) (types env)
63 , typeSynonyms = M.insert (modulePath, name) (args, ty) (typeSynonyms env) }
64 typeCheckAll rest
65typeCheckAll (TypeDeclaration _ _ : _) = error "Type declarations should have been removed"
66typeCheckAll (ValueDeclaration name [] Nothing val : rest) = do
67 rethrow (("Error in declaration " ++ show name ++ ":\n") ++) $ do
68 env <- getEnv
69 modulePath <- checkModulePath `fmap` get
70 case M.lookup (modulePath, name) (names env) of
71 Just _ -> throwError $ show name ++ " is already defined"
72 Nothing -> do
73 [ty] <- typesOf [(name, val)]
74 putEnv (env { names = M.insert (modulePath, name) (ty, Value) (names env) })
75 typeCheckAll rest
76typeCheckAll (ValueDeclaration _ _ _ _ : _) = error "Binders were not desugared"
77typeCheckAll (BindingGroupDeclaration vals : rest) = do
78 rethrow (("Error in binding group " ++ show (map fst vals) ++ ":\n") ++) $ do
79 modulePath <- checkModulePath `fmap` get
80 forM_ (map fst vals) $ \name -> do
81 env <- getEnv
82 case M.lookup (modulePath, name) (names env) of
83 Just _ -> throwError $ show name ++ " is already defined"
84 Nothing -> return ()
85 tys <- typesOf vals
86 forM (zip (map fst vals) tys) $ \(name, ty) ->
87 modifyEnv $ \env -> env { names = M.insert (modulePath, name) (ty, Value) (names env) }
88 typeCheckAll rest
89typeCheckAll (ExternDataDeclaration name kind : rest) = do
90 env <- getEnv
91 modulePath <- checkModulePath `fmap` get
92 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
93 putEnv $ env { types = M.insert (modulePath, name) (kind, TypeSynonym) (types env) }
94 typeCheckAll rest
95typeCheckAll (ExternMemberDeclaration member name ty : rest) = do
96 rethrow (("Error in foreign import member declaration " ++ show name ++ ":\n") ++) $ do
97 env <- getEnv
98 modulePath <- checkModulePath `fmap` get
99 kind <- kindOf ty
100 guardWith "Expected kind *" $ kind == Star
101 case M.lookup (modulePath, name) (names env) of
102 Just _ -> throwError $ show name ++ " is already defined"
103 Nothing -> case ty of
104 _ | isSingleArgumentFunction ty -> do
105 putEnv (env { names = M.insert (modulePath, name) (ty, Extern) (names env)
106 , members = M.insert (modulePath, name) member (members env) })
107 | otherwise -> throwError "Foreign member declarations must have function types, with an single argument."
108 typeCheckAll rest
109 where
110 isSingleArgumentFunction (Function [_] _) = True
111 isSingleArgumentFunction (ForAll _ t) = isSingleArgumentFunction t
112 isSingleArgumentFunction _ = False
113typeCheckAll (ExternDeclaration name ty : rest) = do
114 rethrow (("Error in foreign import declaration " ++ show name ++ ":\n") ++) $ do
115 env <- getEnv
116 modulePath <- checkModulePath `fmap` get
117 kind <- kindOf ty
118 guardWith "Expected kind *" $ kind == Star
119 case M.lookup (modulePath, name) (names env) of
120 Just _ -> throwError $ show name ++ " is already defined"
121 Nothing -> putEnv (env { names = M.insert (modulePath, name) (ty, Extern) (names env) })
122 typeCheckAll rest
123typeCheckAll (FixityDeclaration _ name : rest) = do
124 typeCheckAll rest
125 env <- getEnv
126 modulePath <- checkModulePath `fmap` get
127 guardWith ("Fixity declaration with no binding: " ++ name) $ M.member (modulePath, Op name) $ names env
128typeCheckAll (ModuleDeclaration name decls : rest) = do
129 withModule name $ typeCheckAll decls
130 typeCheckAll rest
131typeCheckAll (ImportDeclaration modulePath idents : rest) = do
132 env <- getEnv
133 currentModule <- checkModulePath `fmap` get
134 rethrow errorMessage $ do
135 guardWith ("Module " ++ show modulePath ++ " does not exist") $ moduleExists env
136 case idents of
137 Nothing -> bindIdents (map snd $ filterModule env) currentModule env
138 Just idents' -> bindIdents idents' currentModule env
139 typeCheckAll rest
140 where errorMessage = (("Error in import declaration " ++ show modulePath ++ ":\n") ++)
141 filterModule = filter ((== modulePath) . fst) . M.keys . names
142 moduleExists env = not $ null $ filterModule env
143 bindIdents idents' currentModule env =
144 forM_ idents' $ \ident -> do
145 guardWith (show currentModule ++ "." ++ show ident ++ " is already defined") $ (currentModule, ident) `M.notMember` names env
146 case (modulePath, ident) `M.lookup` names env of
147 Just (pt, _) -> modifyEnv (\e -> e { names = M.insert (currentModule, ident) (pt, Alias modulePath ident) (names e) })
148 Nothing -> throwError (show modulePath ++ "." ++ show ident ++ " is undefined")