1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE TemplateHaskell #-}
4module Language.PureScript.Environment where
9import Data.Maybe (fromMaybe)
10import qualified Data.Aeson as A
11import qualified Data.Map as M
12import qualified Data.Text as T
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
88 deriving (Show, Read, Eq)
97 = DataType [(String, Maybe Kind)] [(ProperName 'ConstructorName, [Type])]
107 -- A local type variable
111 -- A scoped type variable
114 deriving (Show, Read, Eq)
117-- The type ('data' or 'newtype') of a data type declaration
121 -- A standard data constructor
125 -- A newtype constructor
128 deriving (Show, Read, Eq, Ord)
130showDataDeclType :: DataDeclType -> String
131showDataDeclType Data = "data"
132showDataDeclType Newtype = "newtype"
134instance A.ToJSON DataDeclType where
135 toJSON = A.toJSON . showDataDeclType
137instance A.FromJSON DataDeclType where
138 parseJSON = A.withText "DataDeclType" $ \str ->
140 "data" -> return Data
141 "newtype" -> return Newtype
142 other -> fail $ "invalid type: '" ++ T.unpack other ++ "'"
145-- Construct a ProperName in the Prim module
147primName :: String -> Qualified (ProperName a)
148primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
151-- Construct a type in the Prim module
153primTy :: String -> Type
154primTy = TypeConstructor . primName
157-- Type constructor for functions
160tyFunction = primTy "Function"
163-- Type constructor for strings
166tyString = primTy "String"
169-- Type constructor for strings
172tyChar = primTy "Char"
175-- Type constructor for numbers
178tyNumber = primTy "Number"
181-- Type constructor for integers
187-- Type constructor for booleans
190tyBoolean = primTy "Boolean"
193-- Type constructor for arrays
196tyArray = primTy "Array"
199-- Type constructor for records
202tyRecord = primTy "Record"
205-- Check whether a type is a record
207isObject :: Type -> Bool
208isObject = isTypeOrApplied tyRecord
211-- Check whether a type is a function
213isFunction :: Type -> Bool
214isFunction = isTypeOrApplied tyFunction
216isTypeOrApplied :: Type -> Type -> Bool
217isTypeOrApplied t1 (TypeApp t2 _) = t1 == t2
218isTypeOrApplied t1 t2 = t1 == t2
221-- Smart constructor for function types
223function :: Type -> Type -> Type
224function t1 = TypeApp (TypeApp tyFunction t1)
227-- The primitive types in the external javascript environment with their
228-- associated kinds. There is also a pseudo `Partial` type that corresponds to
229-- the class with the same name.
231primTypes :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
234 [ (primName "Function", (FunKind Star (FunKind Star Star), ExternData))
235 , (primName "Array", (FunKind Star Star, ExternData))
236 , (primName "Record", (FunKind (Row Star) Star, ExternData))
237 , (primName "String", (Star, ExternData))
238 , (primName "Char", (Star, ExternData))
239 , (primName "Number", (Star, ExternData))
240 , (primName "Int", (Star, ExternData))
241 , (primName "Boolean", (Star, ExternData))
242 , (primName "Partial", (Star, ExternData))
246-- The primitive class map. This just contains to `Partial` class, used as a
247-- kind of magic constraint for partial functions.
249primClasses :: M.Map (Qualified (ProperName 'ClassName)) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
252 [ (primName "Partial", ([], [], [])) ]
255-- Finds information about data constructors from the current environment.
257lookupConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> (DataDeclType, ProperName 'TypeName, Type, [Ident])
258lookupConstructor env ctor =
259 fromMaybe (internalError "Data constructor not found") $ ctor `M.lookup` dataConstructors env
262-- Checks whether a data constructor is for a newtype.
264isNewtypeConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> Bool
265isNewtypeConstructor e ctor = case lookupConstructor e ctor of
266 (Newtype, _, _, _) -> True
267 (Data, _, _, _) -> False
270-- Finds information about values from the current environment.
272lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
273lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
274lookupValue _ _ = Nothing
276$(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