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
21-- | The @Environment@ defines all values and types which are currently in scope:
22data Environment = Environment
23 { names :: M.Map (Qualified Ident) (Type, NameKind, NameVisibility)
24 -- ^ Values currently in scope
25 , types :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
26 -- ^ Type names currently in scope
27 , dataConstructors :: M.Map (Qualified (ProperName 'ConstructorName)) (DataDeclType, ProperName 'TypeName, Type, [Ident])
28 -- ^ Data constructors currently in scope, along with their associated type
29 -- constructor name, argument types and return type.
30 , typeSynonyms :: M.Map (Qualified (ProperName 'TypeName)) ([(String, Maybe Kind)], Type)
31 -- ^ Type synonyms currently in scope
32 , typeClassDictionaries :: M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope))
33 -- ^ Available type class dictionaries
34 , typeClasses :: M.Map (Qualified (ProperName 'ClassName)) TypeClassData
38-- | Information about a type class
39data TypeClassData = TypeClassData
40 { typeClassArguments :: [(String, Maybe Kind)]
41 -- ^ A list of type argument names, and their kinds, where kind annotations
43 , typeClassMembers :: [(Ident, Type)]
44 -- ^ A list of type class members and their types. Type arguments listed above
45 -- are considered bound in these types.
46 , typeClassSuperclasses :: [Constraint]
47 -- ^ A list of superclasses of this type class. Type arguments listed above
48 -- are considered bound in the types appearing in these constraints.
49 , typeClassDependencies :: [FunctionalDependency]
50 -- ^ A list of functional dependencies for the type arguments of this class.
53-- | A functional dependency indicates a relationship between two sets of
54-- type arguments in a class declaration.
55data FunctionalDependency = FunctionalDependency
56 { fdDeterminers :: [Int]
57 -- ^ the type arguments which determine the determined type arguments
58 , fdDetermined :: [Int]
59 -- ^ the determined type arguments
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 primClasses
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, 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
102-- The kinds of a type
108 = DataType [(String, Maybe Kind)] [(ProperName 'ConstructorName, [Type])]
118 -- A local type variable
122 -- A scoped type variable
128-- The type ('data' or 'newtype') of a data type declaration
132 -- A standard data constructor
136 -- A newtype constructor
139 deriving (Show, Eq, Ord)
141showDataDeclType :: DataDeclType -> String
142showDataDeclType Data = "data"
143showDataDeclType Newtype = "newtype"
145instance A.ToJSON DataDeclType where
146 toJSON = A.toJSON . showDataDeclType
148instance A.FromJSON DataDeclType where
149 parseJSON = A.withText "DataDeclType" $ \str ->
151 "data" -> return Data
152 "newtype" -> return Newtype
153 other -> fail $ "invalid type: '" ++ T.unpack other ++ "'"
156-- Construct a ProperName in the Prim module
158primName :: String -> Qualified (ProperName a)
159primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
162-- Construct a type in the Prim module
164primTy :: String -> Type
165primTy = TypeConstructor . primName
168-- Type constructor for functions
171tyFunction = primTy "Function"
174-- Type constructor for strings
177tyString = primTy "String"
180-- Type constructor for strings
183tyChar = primTy "Char"
186-- Type constructor for numbers
189tyNumber = primTy "Number"
192-- Type constructor for integers
198-- Type constructor for booleans
201tyBoolean = primTy "Boolean"
204-- Type constructor for arrays
207tyArray = primTy "Array"
210-- Type constructor for records
213tyRecord = primTy "Record"
216-- Check whether a type is a record
218isObject :: Type -> Bool
219isObject = isTypeOrApplied tyRecord
222-- Check whether a type is a function
224isFunction :: Type -> Bool
225isFunction = isTypeOrApplied tyFunction
227isTypeOrApplied :: Type -> Type -> Bool
228isTypeOrApplied t1 (TypeApp t2 _) = t1 == t2
229isTypeOrApplied t1 t2 = t1 == t2
232-- Smart constructor for function types
234function :: Type -> Type -> Type
235function t1 = TypeApp (TypeApp tyFunction t1)
238-- The primitive types in the external javascript environment with their
239-- associated kinds. There is also a pseudo `Partial` type that corresponds to
240-- the class with the same name.
242primTypes :: M.Map (Qualified (ProperName 'TypeName)) (Kind, TypeKind)
245 [ (primName "Function", (FunKind Star (FunKind Star Star), ExternData))
246 , (primName "Array", (FunKind Star Star, ExternData))
247 , (primName "Record", (FunKind (Row Star) Star, ExternData))
248 , (primName "String", (Star, ExternData))
249 , (primName "Char", (Star, ExternData))
250 , (primName "Number", (Star, ExternData))
251 , (primName "Int", (Star, ExternData))
252 , (primName "Boolean", (Star, ExternData))
253 , (primName "Partial", (Star, ExternData))
254 , (primName "Fail", (FunKind Symbol Star, ExternData))
255 , (primName "TypeString", (FunKind Star Symbol, ExternData))
256 , (primName "TypeConcat", (FunKind Symbol (FunKind Symbol Symbol), ExternData))
260-- The primitive class map. This just contains to `Partial` class, used as a
261-- kind of magic constraint for partial functions.
263primClasses :: M.Map (Qualified (ProperName 'ClassName)) TypeClassData
266 [ (primName "Partial", (TypeClassData [] [] [] []))
267 , (primName "Fail", (TypeClassData [("message", Just Symbol)] [] [] []))
271-- Finds information about data constructors from the current environment.
273lookupConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> (DataDeclType, ProperName 'TypeName, Type, [Ident])
274lookupConstructor env ctor =
275 fromMaybe (internalError "Data constructor not found") $ ctor `M.lookup` dataConstructors env
278-- Checks whether a data constructor is for a newtype.
280isNewtypeConstructor :: Environment -> Qualified (ProperName 'ConstructorName) -> Bool
281isNewtypeConstructor e ctor = case lookupConstructor e ctor of
282 (Newtype, _, _, _) -> True
283 (Data, _, _, _) -> False
286-- Finds information about values from the current environment.
288lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
289lookupValue env ident = ident `M.lookup` names env
291$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''TypeKind)
292$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''FunctionalDependency)
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