1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE TemplateHaskell #-}
4module Language.PureScript.Environment where
6import Data.Maybe (fromMaybe)
8import qualified Data.Map as M
9import qualified Data.Text as T
10import qualified Data.Aeson as A
12import Language.PureScript.Crash
13import Language.PureScript.Kinds
14import Language.PureScript.Names
15import Language.PureScript.TypeClassDictionaries
16import Language.PureScript.Types
17import qualified Language.PureScript.Constants as C
20-- The @Environment@ defines all values and types which are currently in scope:
22data Environment = Environment {
24 -- Value names currently in scope
26 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
28 -- Type names currently in scope
30 , types :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
32 -- Data constructors currently in scope, along with their associated type
33 -- constructor name, argument types and return type.
34 , dataConstructors :: M.Map (Qualified (ProperName 'ConstructorName)) (DataDeclType, ProperName 'TypeName, Type, [Ident])
36 -- Type synonyms currently in scope
38 , typeSynonyms :: M.Map (Qualified (ProperName 'TypeName)) ([(String, Maybe Kind)], Type)
40 -- Available type class dictionaries
42 , typeClassDictionaries :: M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope))
46 , typeClasses :: M.Map (Qualified (ProperName 'ClassName)) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
47 } deriving (Show, Read)
50-- The initial environment with no values and only the default javascript types defined
52initEnvironment :: Environment
53initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty primClasses
56-- The visibility of a name in scope
60 -- The name is defined in the current binding group, but is not visible
64 -- The name is defined in the another binding group, or has been made visible by a function binder
66 | Defined deriving (Show, Read, Eq)
69-- A flag for whether a name is for an private or public value - only public values will be
70-- included in a generated externs file.
74 -- A private value introduced as an artifact of code generation (class instances, class member
79 -- A public value for a module member or foreing import declaration
83 -- A name for member introduced by foreign import
86 deriving (Show, Read, Eq)
95 = DataType [(String, Maybe Kind)] [(ProperName 'ConstructorName, [Type])]
105 -- A local type variable
109 -- A scoped type variable
112 deriving (Show, Read, Eq)
115-- The type ('data' or 'newtype') of a data type declaration
119 -- A standard data constructor
123 -- A newtype constructor
126 deriving (Show, Read, Eq, Ord)
128showDataDeclType :: DataDeclType -> String
129showDataDeclType Data = "data"
130showDataDeclType Newtype = "newtype"
132instance A.ToJSON DataDeclType where
133 toJSON = A.toJSON . showDataDeclType
135instance A.FromJSON DataDeclType where
136 parseJSON = A.withText "DataDeclType" $ \str ->
138 "data" -> return Data
139 "newtype" -> return Newtype
140 other -> fail $ "invalid type: '" ++ T.unpack other ++ "'"
143-- Construct a ProperName in the Prim module
145primName :: String -> Qualified (ProperName a)
146primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
149-- Construct a type in the Prim module
151primTy :: String -> Type
152primTy = TypeConstructor . primName
155-- Type constructor for functions
158tyFunction = primTy "Function"
161-- Type constructor for strings
164tyString = primTy "String"
167-- Type constructor for strings
170tyChar = primTy "Char"
173-- Type constructor for numbers
176tyNumber = primTy "Number"
179-- Type constructor for integers
185-- Type constructor for booleans
188tyBoolean = primTy "Boolean"
191-- Type constructor for arrays
194tyArray = primTy "Array"
197-- Type constructor for objects
200tyObject = primTy "Object"
203-- Check whether a type is an object
205isObject :: Type -> Bool
206isObject = isTypeOrApplied tyObject
209-- Check whether a type is a function
211isFunction :: Type -> Bool
212isFunction = isTypeOrApplied tyFunction
214isTypeOrApplied :: Type -> Type -> Bool
215isTypeOrApplied t1 (TypeApp t2 _) = t1 == t2
216isTypeOrApplied t1 t2 = t1 == t2
219-- Smart constructor for function types
221function :: Type -> Type -> Type
222function t1 = TypeApp (TypeApp tyFunction t1)
225-- The primitive types in the external javascript environment with their
226-- associated kinds. There is also a pseudo `Partial` type that corresponds to
227-- the class with the same name.
229primTypes :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
232 [ (primName "Function", (FunKind Star (FunKind Star Star), ExternData))
233 , (primName "Array", (FunKind Star Star, ExternData))
234 , (primName "Object", (FunKind (Row Star) Star, ExternData))
235 , (primName "String", (Star, ExternData))
236 , (primName "Char", (Star, ExternData))
237 , (primName "Number", (Star, ExternData))
238 , (primName "Int", (Star, ExternData))
239 , (primName "Boolean", (Star, ExternData))
240 , (primName "Partial", (Star, ExternData))
244-- The primitive class map. This just contains to `Partial` class, used as a
245-- kind of magic constraint for partial functions.
247primClasses :: M.Map (Qualified (ProperName 'ClassName)) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
250 [ (primName "Partial", ([], [], [])) ]
253-- Finds information about data constructors from the current environment.
255lookupConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> (DataDeclType, ProperName 'TypeName, Type, [Ident])
256lookupConstructor env ctor =
257 fromMaybe (internalError "Data constructor not found") $ ctor `M.lookup` dataConstructors env
260-- Checks whether a data constructor is for a newtype.
262isNewtypeConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> Bool
263isNewtypeConstructor e ctor = case lookupConstructor e ctor of
264 (Newtype, _, _, _) -> True
265 (Data, _, _, _) -> False
268-- Finds information about values from the current environment.
270lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
271lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
272lookupValue _ _ = Nothing
274$(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