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 #-}
16{-# LANGUAGE OverloadedStrings #-}
18module Language.PureScript.Environment where
21import Data.Maybe (fromMaybe)
22import qualified Data.Map as M
23import qualified Data.Text as T
24import qualified Data.Aeson as A
26import Language.PureScript.Kinds
27import Language.PureScript.Names
28import Language.PureScript.TypeClassDictionaries
29import Language.PureScript.Types
30import qualified Language.PureScript.Constants as C
33-- The @Environment@ defines all values and types which are currently in scope:
35data Environment = Environment {
37 -- Value names currently in scope
39 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
41 -- Type names currently in scope
43 , types :: M.Map (Qualified ProperName) (Kind, TypeKind)
45 -- Data constructors currently in scope, along with their associated data type constructors
47 , dataConstructors :: M.Map (Qualified ProperName) (DataDeclType, ProperName, Type, [Ident])
49 -- Type synonyms currently in scope
51 , typeSynonyms :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], Type)
53 -- Available type class dictionaries
55 , typeClassDictionaries :: M.Map (Maybe ModuleName) (M.Map (Qualified ProperName) (M.Map (Qualified Ident) TypeClassDictionaryInScope))
59 , typeClasses :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
60 } deriving (Show, Read)
63-- The initial environment with no values and only the default javascript types defined
65initEnvironment :: Environment
66initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty M.empty
69-- The visibility of a name in scope
73 -- The name is defined in the current binding group, but is not visible
77 -- The name is defined in the another binding group, or has been made visible by a function binder
79 | Defined deriving (Show, Read, Eq)
82-- A flag for whether a name is for an private or public value - only public values will be
83-- included in a generated externs file.
87 -- A private value introduced as an artifact of code generation (class instances, class member
92 -- A public value for a module member or foreing import declaration
96 -- A name for member introduced by foreign import
98 | External deriving (Show, Read, Eq, Data, Typeable)
101-- The kinds of a type
107 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
117 -- A local type variable
121 -- A scoped type variable
124 deriving (Show, Read, Eq, Data, Typeable)
127-- The type ('data' or 'newtype') of a data type declaration
131 -- A standard data constructor
135 -- A newtype constructor
137 | Newtype deriving (Show, Read, Eq, Ord, Data, Typeable)
139showDataDeclType :: DataDeclType -> String
140showDataDeclType Data = "data"
141showDataDeclType Newtype = "newtype"
143instance A.ToJSON DataDeclType where
144 toJSON = A.toJSON . showDataDeclType
146instance A.FromJSON DataDeclType where
147 parseJSON = A.withText "DataDeclType" $ \str ->
149 "data" -> return Data
150 "newtype" -> return Newtype
151 other -> fail $ "invalid type: '" ++ T.unpack other ++ "'"
154-- Construct a ProperName in the Prim module
156primName :: String -> Qualified ProperName
157primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
160-- Construct a type in the Prim module
162primTy :: String -> Type
163primTy = TypeConstructor . primName
166-- Type constructor for functions
169tyFunction = primTy "Function"
172-- Type constructor for strings
175tyString = primTy "String"
178-- Type constructor for strings
181tyChar = primTy "Char"
184-- Type constructor for numbers
187tyNumber = primTy "Number"
190-- Type constructor for integers
196-- Type constructor for booleans
199tyBoolean = primTy "Boolean"
202-- Type constructor for arrays
205tyArray = primTy "Array"
208-- Type constructor for objects
211tyObject = primTy "Object"
214-- Check whether a type is an object
216isObject :: Type -> Bool
217isObject = isTypeOrApplied tyObject
220-- Check whether a type is a function
222isFunction :: Type -> Bool
223isFunction = isTypeOrApplied tyFunction
225isTypeOrApplied :: Type -> Type -> Bool
226isTypeOrApplied t1 (TypeApp t2 _) = t1 == t2
227isTypeOrApplied t1 t2 = t1 == t2
230-- Smart constructor for function types
232function :: Type -> Type -> Type
233function t1 = TypeApp (TypeApp tyFunction t1)
236-- The primitive types in the external javascript environment with their associated kinds.
238primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
239primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
240 , (primName "Array" , (FunKind Star Star, ExternData))
241 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
242 , (primName "String" , (Star, ExternData))
243 , (primName "Char" , (Star, ExternData))
244 , (primName "Number" , (Star, ExternData))
245 , (primName "Int" , (Star, ExternData))
246 , (primName "Boolean" , (Star, ExternData)) ]
249-- Finds information about data constructors from the current environment.
251lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
252lookupConstructor env ctor =
253 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
256-- Checks whether a data constructor is for a newtype.
258isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
259isNewtypeConstructor e ctor = case lookupConstructor e ctor of
260 (Newtype, _, _, _) -> True
261 (Data, _, _, _) -> False
264-- Finds information about values from the current environment.
266lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
267lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
268lookupValue _ _ = 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