1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE TemplateHaskell #-}
5module Language.PureScript.Environment where
8import Data.Maybe (fromMaybe)
10import qualified Data.Map as M
11import qualified Data.Text as T
12import qualified Data.Aeson as A
14import Language.PureScript.Crash
15import Language.PureScript.Kinds
16import Language.PureScript.Names
17import Language.PureScript.TypeClassDictionaries
18import Language.PureScript.Types
19import qualified Language.PureScript.Constants as C
22-- The @Environment@ defines all values and types which are currently in scope:
24data Environment = Environment {
26 -- Value names currently in scope
28 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
30 -- Type names currently in scope
32 , types :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
34 -- Data constructors currently in scope, along with their associated type
35 -- constructor name, argument types and return type.
36 , dataConstructors :: M.Map (Qualified (ProperName 'ConstructorName)) (DataDeclType, ProperName 'TypeName, Type, [Ident])
38 -- Type synonyms currently in scope
40 , typeSynonyms :: M.Map (Qualified (ProperName 'TypeName)) ([(String, Maybe Kind)], Type)
42 -- Available type class dictionaries
44 , typeClassDictionaries :: M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope))
48 , typeClasses :: M.Map (Qualified (ProperName 'ClassName)) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
49 } deriving (Show, Read)
52-- The initial environment with no values and only the default javascript types defined
54initEnvironment :: Environment
55initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty primClasses
58-- The visibility of a name in scope
62 -- The name is defined in the current binding group, but is not visible
66 -- The name is defined in the another binding group, or has been made visible by a function binder
68 | Defined deriving (Show, Read, Eq)
71-- A flag for whether a name is for an private or public value - only public values will be
72-- included in a generated externs file.
76 -- A private value introduced as an artifact of code generation (class instances, class member
81 -- A public value for a module member or foreing import declaration
85 -- A name for member introduced by foreign import
87 | External deriving (Show, Read, Eq, Data, Typeable)
96 = DataType [(String, Maybe Kind)] [(ProperName 'ConstructorName, [Type])]
106 -- A local type variable
110 -- A scoped type variable
113 deriving (Show, Read, Eq, Data, Typeable)
116-- The type ('data' or 'newtype') of a data type declaration
120 -- A standard data constructor
124 -- A newtype constructor
126 | Newtype deriving (Show, Read, Eq, Ord, Data, Typeable)
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