1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.TypeChecker
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE FlexibleInstances #-}
17module Language.PureScript.TypeChecker (
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
28import qualified Data.Map as M
30import Language.PureScript.Values
31import Language.PureScript.Types
32import Language.PureScript.Names
33import Language.PureScript.Kinds
34import Language.PureScript.Declarations
36import Control.Monad.State
37import Control.Monad.Error
39typeCheckAll :: [Declaration] -> Check ()
40typeCheckAll [] = return ()
41typeCheckAll (DataDeclaration name args dctors : rest) = do
42 rethrow (("Error in type constructor " ++ show name ++ ":\n") ++) $ do
44 modulePath <- checkModulePath `fmap` get
45 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
46 ctorKind <- kindsOf (Just name) args (mapMaybe snd dctors)
47 putEnv $ env { types = M.insert (modulePath, name) (ctorKind, Data) (types env) }
48 forM_ dctors $ \(dctor, maybeTy) ->
49 rethrow (("Error in data constructor " ++ show name ++ ":\n") ++) $ do
51 guardWith (show dctor ++ " is already defined") $ not $ M.member (modulePath, dctor) (dataConstructors env')
52 let retTy = foldl TypeApp (TypeConstructor (Qualified modulePath name)) (map TypeVar args)
53 let dctorTy = maybe retTy (\ty -> Function [ty] retTy) maybeTy
54 let polyType = mkForAll args dctorTy
55 putEnv $ env' { dataConstructors = M.insert (modulePath, dctor) polyType (dataConstructors env') }
57typeCheckAll (TypeSynonymDeclaration name args ty : rest) = do
58 rethrow (("Error in type synonym " ++ show name ++ ":\n") ++) $ do
60 modulePath <- checkModulePath `fmap` get
61 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
62 kind <- kindsOf (Just name) args [ty]
63 putEnv $ env { types = M.insert (modulePath, name) (kind, TypeSynonym) (types env)
64 , typeSynonyms = M.insert (modulePath, name) (args, ty) (typeSynonyms env) }
66typeCheckAll (TypeDeclaration name ty : ValueDeclaration name' val : rest) | name == name' =
67 typeCheckAll (ValueDeclaration name (TypedValue val ty) : rest)
68typeCheckAll (TypeDeclaration name _ : _) = throwError $ "Orphan type declaration for " ++ show name
69typeCheckAll (ValueDeclaration name val : rest) = do
70 rethrow (("Error in declaration " ++ show name ++ ":\n") ++) $ do
72 modulePath <- checkModulePath `fmap` get
73 case M.lookup (modulePath, name) (names env) of
74 Just _ -> throwError $ show name ++ " is already defined"
76 ty <- typeOf (Just name) val
77 putEnv (env { names = M.insert (modulePath, name) (ty, Value) (names env) })
79typeCheckAll (ExternDataDeclaration name kind : rest) = do
81 modulePath <- checkModulePath `fmap` get
82 guardWith (show name ++ " is already defined") $ not $ M.member (modulePath, name) (types env)
83 putEnv $ env { types = M.insert (modulePath, name) (kind, TypeSynonym) (types env) }
85typeCheckAll (ExternMemberDeclaration member name ty : rest) = do
86 rethrow (("Error in foreign import member declaration " ++ show name ++ ":\n") ++) $ do
88 modulePath <- checkModulePath `fmap` get
90 guardWith "Expected kind *" $ kind == Star
91 case M.lookup (modulePath, name) (names env) of
92 Just _ -> throwError $ show name ++ " is already defined"
94 _ | isSingleArgumentFunction ty -> do
95 putEnv (env { names = M.insert (modulePath, name) (ty, Extern) (names env)
96 , members = M.insert (modulePath, name) member (members env) })
97 | otherwise -> throwError "Foreign member declarations must have function types, with an single argument."
100 isSingleArgumentFunction (Function [_] _) = True
101 isSingleArgumentFunction (ForAll _ t) = isSingleArgumentFunction t
102 isSingleArgumentFunction _ = False
103typeCheckAll (ExternDeclaration name ty : rest) = do
104 rethrow (("Error in foreign import declaration " ++ show name ++ ":\n") ++) $ do
106 modulePath <- checkModulePath `fmap` get
108 guardWith "Expected kind *" $ kind == Star
109 case M.lookup (modulePath, name) (names env) of
110 Just _ -> throwError $ show name ++ " is already defined"
111 Nothing -> putEnv (env { names = M.insert (modulePath, name) (ty, Extern) (names env) })
113typeCheckAll (FixityDeclaration _ name : rest) = do
116 modulePath <- checkModulePath `fmap` get
117 guardWith ("Fixity declaration with no binding: " ++ name) $ M.member (modulePath, Op name) $ names env
118typeCheckAll (ModuleDeclaration name decls : rest) = do
119 withModule name $ typeCheckAll decls
121typeCheckAll (ImportDeclaration modulePath idents : rest) = do
123 currentModule <- checkModulePath `fmap` get
124 rethrow errorMessage $ do
125 guardWith ("Module " ++ show modulePath ++ " does not exist") $ moduleExists env
127 Nothing -> bindIdents (map snd $ filterModule env) currentModule env
128 Just idents' -> bindIdents idents' currentModule env
130 where errorMessage = (("Error in import declaration " ++ show modulePath ++ ":\n") ++)
131 filterModule = filter ((== modulePath) . fst) . M.keys . names
132 moduleExists env = not $ null $ filterModule env
133 bindIdents idents' currentModule env =
134 forM_ idents' $ \ident -> do
135 guardWith (show currentModule ++ "." ++ show ident ++ " is already defined") $ (currentModule, ident) `M.notMember` names env
136 case (modulePath, ident) `M.lookup` names env of
137 Just (pt, _) -> modifyEnv (\e -> e { names = M.insert (currentModule, ident) (pt, Alias modulePath ident) (names e) })
138 Nothing -> throwError (show modulePath ++ "." ++ show ident ++ " is undefined")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list