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 #-}
17{-# LANGUAGE TemplateHaskell #-}
19module Language.PureScript.Environment where
22import Data.Maybe (fromMaybe)
24import qualified Data.Map as M
25import qualified Data.Text as T
26import qualified Data.Aeson as A
28import Language.PureScript.Crash
29import Language.PureScript.Kinds
30import Language.PureScript.Names
31import Language.PureScript.TypeClassDictionaries
32import Language.PureScript.Types
33import qualified Language.PureScript.Constants as C
36-- The @Environment@ defines all values and types which are currently in scope:
38data Environment = Environment {
40 -- Value names currently in scope
42 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
44 -- Type names currently in scope
46 , types :: M.Map (Qualified ProperName) (Kind, TypeKind)
48 -- Data constructors currently in scope, along with their associated type
49 -- constructor name, argument types and return type.
50 , dataConstructors :: M.Map (Qualified ProperName) (DataDeclType, ProperName, Type, [Ident])
52 -- Type synonyms currently in scope
54 , typeSynonyms :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], Type)
56 -- Available type class dictionaries
58 , typeClassDictionaries :: M.Map (Maybe ModuleName) (M.Map (Qualified ProperName) (M.Map (Qualified Ident) TypeClassDictionaryInScope))
62 , typeClasses :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
63 } deriving (Show, Read)
66-- The initial environment with no values and only the default javascript types defined
68initEnvironment :: Environment
69initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty M.empty
72-- The visibility of a name in scope
76 -- The name is defined in the current binding group, but is not visible
80 -- The name is defined in the another binding group, or has been made visible by a function binder
82 | Defined deriving (Show, Read, Eq)
85-- A flag for whether a name is for an private or public value - only public values will be
86-- included in a generated externs file.
90 -- A private value introduced as an artifact of code generation (class instances, class member
95 -- A public value for a module member or foreing import declaration
99 -- A name for member introduced by foreign import
101 | External deriving (Show, Read, Eq, Data, Typeable)
104-- The kinds of a type
110 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
120 -- A local type variable
124 -- A scoped type variable
127 deriving (Show, Read, Eq, Data, Typeable)
130-- The type ('data' or 'newtype') of a data type declaration
134 -- A standard data constructor
138 -- A newtype constructor
140 | Newtype deriving (Show, Read, Eq, Ord, Data, Typeable)
142showDataDeclType :: DataDeclType -> String
143showDataDeclType Data = "data"
144showDataDeclType Newtype = "newtype"
146instance A.ToJSON DataDeclType where
147 toJSON = A.toJSON . showDataDeclType
149instance A.FromJSON DataDeclType where
150 parseJSON = A.withText "DataDeclType" $ \str ->
152 "data" -> return Data
153 "newtype" -> return Newtype
154 other -> fail $ "invalid type: '" ++ T.unpack other ++ "'"
157-- Construct a ProperName in the Prim module
159primName :: String -> Qualified ProperName
160primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
163-- Construct a type in the Prim module
165primTy :: String -> Type
166primTy = TypeConstructor . primName
169-- Type constructor for functions
172tyFunction = primTy "Function"
175-- Type constructor for strings
178tyString = primTy "String"
181-- Type constructor for strings
184tyChar = primTy "Char"
187-- Type constructor for numbers
190tyNumber = primTy "Number"
193-- Type constructor for integers
199-- Type constructor for booleans
202tyBoolean = primTy "Boolean"
205-- Type constructor for arrays
208tyArray = primTy "Array"
211-- Type constructor for objects
214tyObject = primTy "Object"
217-- Check whether a type is an object
219isObject :: Type -> Bool
220isObject = isTypeOrApplied tyObject
223-- Check whether a type is a function
225isFunction :: Type -> Bool
226isFunction = isTypeOrApplied tyFunction
228isTypeOrApplied :: Type -> Type -> Bool
229isTypeOrApplied t1 (TypeApp t2 _) = t1 == t2
230isTypeOrApplied t1 t2 = t1 == t2
233-- Smart constructor for function types
235function :: Type -> Type -> Type
236function t1 = TypeApp (TypeApp tyFunction t1)
239-- The primitive types in the external javascript environment with their associated kinds.
241primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
242primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
243 , (primName "Array" , (FunKind Star Star, ExternData))
244 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
245 , (primName "String" , (Star, ExternData))
246 , (primName "Char" , (Star, ExternData))
247 , (primName "Number" , (Star, ExternData))
248 , (primName "Int" , (Star, ExternData))
249 , (primName "Boolean" , (Star, ExternData)) ]
252-- Finds information about data constructors from the current environment.
254lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
255lookupConstructor env ctor =
256 fromMaybe (internalError "Data constructor not found") $ ctor `M.lookup` dataConstructors env
259-- Checks whether a data constructor is for a newtype.
261isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
262isNewtypeConstructor e ctor = case lookupConstructor e ctor of
263 (Newtype, _, _, _) -> True
264 (Data, _, _, _) -> False
267-- Finds information about values from the current environment.
269lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
270lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
271lookupValue _ _ = Nothing
273$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''TypeKind)
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