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)
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 type of a foreign import
70 -- A regular foreign import
74 -- A foreign import which contains inline Javascript as a string literal
76 | InlineJavascript deriving (Show, Eq, Data, Typeable)
79-- The visibility of a name in scope
83 -- The name is defined in the current binding group, but is not visible
87 -- The name is defined in the another binding group, or has been made visible by a function binder
89 | Defined deriving (Show, Eq)
96 -- A value introduced as a binding in a module
100 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
102 | TypeClassAccessorImport
106 | Extern ForeignImportType
108 -- A local name introduced using a lambda abstraction, variable introduction or binder
112 -- A data constructor
116 -- A type class dictionary, generated during desugaring of type class declarations
118 | TypeInstanceDictionaryValue deriving (Show, Eq, Data, Typeable)
121-- The kinds of a type
127 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
137 -- A local type variable
139 | LocalTypeVariable deriving (Show, Eq, Data, Typeable)
142-- The type ('data' or 'newtype') of a data type declaration
146 -- A standard data constructor
150 -- A newtype constructor
152 | Newtype deriving (Eq, Ord, Data, Typeable)
154instance Show DataDeclType where
156 show Newtype = "newtype"
159-- Construct a ProperName in the Prim module
161primName :: String -> Qualified ProperName
162primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
165-- Construct a type in the Prim module
167primTy :: String -> Type
168primTy = TypeConstructor . primName
171-- Type constructor for functions
174tyFunction = primTy "Function"
177-- Type constructor for strings
180tyString = primTy "String"
183-- Type constructor for numbers
186tyNumber = primTy "Number"
189-- Type constructor for booleans
192tyBoolean = primTy "Boolean"
195-- Type constructor for arrays
198tyArray = primTy "Array"
201-- Type constructor for objects
204tyObject = primTy "Object"
207-- Smart constructor for function types
209function :: Type -> Type -> Type
210function t1 = TypeApp (TypeApp tyFunction t1)
213-- The primitive types in the external javascript environment with their associated kinds.
215primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
216primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
217 , (primName "Array" , (FunKind Star Star, ExternData))
218 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
219 , (primName "String" , (Star, ExternData))
220 , (primName "Number" , (Star, ExternData))
221 , (primName "Boolean" , (Star, ExternData)) ]
224-- Finds information about data constructors from the current environment.
226lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type)
227lookupConstructor env ctor =
228 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
231-- Checks whether a data constructor is for a newtype.
233isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
234isNewtypeConstructor e ctor = case lookupConstructor e ctor of
235 (Newtype, _, _) -> True
236 (Data, _, _) -> False
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