1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Environment
4-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Environment where
20import Data.Maybe (fromMaybe)
21import qualified Data.Map as M
23import Language.PureScript.Kinds
24import Language.PureScript.Names
25import Language.PureScript.TypeClassDictionaries
26import Language.PureScript.Types
27import qualified Language.PureScript.Constants as C
30-- The @Environment@ defines all values and types which are currently in scope:
32data Environment = Environment {
34 -- Value names currently in scope
36 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
38 -- Type names currently in scope
40 , types :: M.Map (Qualified ProperName) (Kind, TypeKind)
42 -- Data constructors currently in scope, along with their associated data type constructors
44 , dataConstructors :: M.Map (Qualified ProperName) (DataDeclType, ProperName, Type, [Ident])
46 -- Type synonyms currently in scope
48 , typeSynonyms :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], Type)
50 -- Available type class dictionaries
52 , typeClassDictionaries :: M.Map (Qualified Ident, Maybe ModuleName) TypeClassDictionaryInScope
56 , typeClasses :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
60-- The initial environment with no values and only the default javascript types defined
62initEnvironment :: Environment
63initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty M.empty
66-- The visibility of a name in scope
70 -- The name is defined in the current binding group, but is not visible
74 -- The name is defined in the another binding group, or has been made visible by a function binder
76 | Defined deriving (Show, Eq)
79-- A flag for whether a name is for an private or public value - only public values will be
80-- included in a generated externs file.
84 -- A private value introduced as an artifact of code generation (class instances, class member
89 -- A public value for a module member or foreing import declaration
93 -- A name for member introduced by foreign import
95 | External deriving (Show, Eq, Data, Typeable)
104 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
114 -- A local type variable
118 -- A scoped type variable
121 deriving (Show, Eq, Data, Typeable)
124-- The type ('data' or 'newtype') of a data type declaration
128 -- A standard data constructor
132 -- A newtype constructor
134 | Newtype deriving (Eq, Ord, Data, Typeable)
136instance Show DataDeclType where
138 show Newtype = "newtype"
141-- Construct a ProperName in the Prim module
143primName :: String -> Qualified ProperName
144primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
147-- Construct a type in the Prim module
149primTy :: String -> Type
150primTy = TypeConstructor . primName
153-- Type constructor for functions
156tyFunction = primTy "Function"
159-- Type constructor for strings
162tyString = primTy "String"
165-- Type constructor for strings
168tyChar = primTy "Char"
171-- Type constructor for numbers
174tyNumber = primTy "Number"
177-- Type constructor for integers
183-- Type constructor for booleans
186tyBoolean = primTy "Boolean"
189-- Type constructor for arrays
192tyArray = primTy "Array"
195-- Type constructor for objects
198tyObject = primTy "Object"
201-- Check whether a type is an object
203isObject :: Type -> Bool
204isObject = (==) tyObject . extract
205 where extract (TypeApp t _) = t
209-- Smart constructor for function types
211function :: Type -> Type -> Type
212function t1 = TypeApp (TypeApp tyFunction t1)
215-- The primitive types in the external javascript environment with their associated kinds.
217primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
218primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
219 , (primName "Array" , (FunKind Star Star, ExternData))
220 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
221 , (primName "String" , (Star, ExternData))
222 , (primName "Char" , (Star, ExternData))
223 , (primName "Number" , (Star, ExternData))
224 , (primName "Int" , (Star, ExternData))
225 , (primName "Boolean" , (Star, ExternData)) ]
228-- Finds information about data constructors from the current environment.
230lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
231lookupConstructor env ctor =
232 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
235-- Checks whether a data constructor is for a newtype.
237isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
238isNewtypeConstructor e ctor = case lookupConstructor e ctor of
239 (Newtype, _, _, _) -> True
240 (Data, _, _, _) -> False
243-- Finds information about values from the current environment.
245lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
246lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
247lookupValue _ _ = Nothing
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