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
141 -- A scoped type variable
144 deriving (Show, Eq, Data, Typeable)
147-- The type ('data' or 'newtype') of a data type declaration
151 -- A standard data constructor
155 -- A newtype constructor
157 | Newtype deriving (Eq, Ord, Data, Typeable)
159instance Show DataDeclType where
161 show Newtype = "newtype"
164-- Construct a ProperName in the Prim module
166primName :: String -> Qualified ProperName
167primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
170-- Construct a type in the Prim module
172primTy :: String -> Type
173primTy = TypeConstructor . primName
176-- Type constructor for functions
179tyFunction = primTy "Function"
182-- Type constructor for strings
185tyString = primTy "String"
188-- Type constructor for numbers
191tyNumber = primTy "Number"
194-- Type constructor for booleans
197tyBoolean = primTy "Boolean"
200-- Type constructor for arrays
203tyArray = primTy "Array"
206-- Type constructor for objects
209tyObject = primTy "Object"
212-- Smart constructor for function types
214function :: Type -> Type -> Type
215function t1 = TypeApp (TypeApp tyFunction t1)
218-- The primitive types in the external javascript environment with their associated kinds.
220primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
221primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
222 , (primName "Array" , (FunKind Star Star, ExternData))
223 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
224 , (primName "String" , (Star, ExternData))
225 , (primName "Number" , (Star, ExternData))
226 , (primName "Boolean" , (Star, ExternData)) ]
229-- Finds information about data constructors from the current environment.
231lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type)
232lookupConstructor env ctor =
233 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
236-- Checks whether a data constructor is for a newtype.
238isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
239isNewtypeConstructor e ctor = case lookupConstructor e ctor of
240 (Newtype, _, _) -> True
241 (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