git.iko.soy/rowtype-yoga/purescript-native
on 7b5ab61 help sign in
247 lines 6.3 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Environment
4-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Environment where
19import Data.Data
20import Data.Maybe (fromMaybe)
21import qualified Data.Map as M
23import Language.PureScript.Kinds
24import Language.PureScript.Names
25import Language.PureScript.TypeClassDictionaries
26import Language.PureScript.Types
27import qualified Language.PureScript.Constants as C
29-- |
30-- The @Environment@ defines all values and types which are currently in scope:
31--
32data Environment = Environment {
33 -- |
34 -- Value names currently in scope
35 --
36 names :: M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility)
37 -- |
38 -- Type names currently in scope
39 --
40 , types :: M.Map (Qualified ProperName) (Kind, TypeKind)
41 -- |
42 -- Data constructors currently in scope, along with their associated data type constructors
43 --
44 , dataConstructors :: M.Map (Qualified ProperName) (DataDeclType, ProperName, Type, [Ident])
45 -- |
46 -- Type synonyms currently in scope
47 --
48 , typeSynonyms :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], Type)
49 -- |
50 -- Available type class dictionaries
51 --
52 , typeClassDictionaries :: M.Map (Qualified Ident, Maybe ModuleName) TypeClassDictionaryInScope
53 -- |
54 -- Type classes
55 --
56 , typeClasses :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
57 } deriving (Show)
59-- |
60-- The initial environment with no values and only the default javascript types defined
61--
62initEnvironment :: Environment
63initEnvironment = Environment M.empty primTypes M.empty M.empty M.empty M.empty
65-- |
66-- The visibility of a name in scope
67--
68data NameVisibility
69 -- |
70 -- The name is defined in the current binding group, but is not visible
71 --
72 = Undefined
73 -- |
74 -- The name is defined in the another binding group, or has been made visible by a function binder
75 --
76 | Defined deriving (Show, Eq)
78-- |
79-- A flag for whether a name is for an private or public value - only public values will be
80-- included in a generated externs file.
81--
82data NameKind
83 -- |
84 -- A private value introduced as an artifact of code generation (class instances, class member
85 -- accessors, etc.)
86 --
87 = Private
88 -- |
89 -- A public value for a module member or foreing import declaration
90 --
91 | Public
92 -- |
93 -- A name for member introduced by foreign import
94 --
95 | External deriving (Show, Eq, Data, Typeable)
97-- |
98-- The kinds of a type
99--
100data TypeKind
101 -- |
102 -- Data type
103 --
104 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
105 -- |
106 -- Type synonym
107 --
108 | TypeSynonym
109 -- |
110 -- Foreign data
111 --
112 | ExternData
113 -- |
114 -- A local type variable
115 --
116 | LocalTypeVariable
117 -- |
118 -- A scoped type variable
119 --
120 | ScopedTypeVar
121 deriving (Show, Eq, Data, Typeable)
123-- |
124-- The type ('data' or 'newtype') of a data type declaration
125--
126data DataDeclType
127 -- |
128 -- A standard data constructor
129 --
130 = Data
131 -- |
132 -- A newtype constructor
133 --
134 | Newtype deriving (Eq, Ord, Data, Typeable)
136instance Show DataDeclType where
137 show Data = "data"
138 show Newtype = "newtype"
140-- |
141-- Construct a ProperName in the Prim module
142--
143primName :: String -> Qualified ProperName
144primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
146-- |
147-- Construct a type in the Prim module
148--
149primTy :: String -> Type
150primTy = TypeConstructor . primName
152-- |
153-- Type constructor for functions
154--
155tyFunction :: Type
156tyFunction = primTy "Function"
158-- |
159-- Type constructor for strings
160--
161tyString :: Type
162tyString = primTy "String"
164-- |
165-- Type constructor for strings
166--
167tyChar :: Type
168tyChar = primTy "Char"
170-- |
171-- Type constructor for numbers
172--
173tyNumber :: Type
174tyNumber = primTy "Number"
176-- |
177-- Type constructor for integers
178--
179tyInt :: Type
180tyInt = primTy "Int"
182-- |
183-- Type constructor for booleans
184--
185tyBoolean :: Type
186tyBoolean = primTy "Boolean"
188-- |
189-- Type constructor for arrays
190--
191tyArray :: Type
192tyArray = primTy "Array"
194-- |
195-- Type constructor for objects
196--
197tyObject :: Type
198tyObject = primTy "Object"
200-- |
201-- Check whether a type is an object
202--
203isObject :: Type -> Bool
204isObject = (==) tyObject . extract
205 where extract (TypeApp t _) = t
206 extract t = t
208-- |
209-- Smart constructor for function types
210--
211function :: Type -> Type -> Type
212function t1 = TypeApp (TypeApp tyFunction t1)
214-- |
215-- The primitive types in the external javascript environment with their associated kinds.
216--
217primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
218primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
219 , (primName "Array" , (FunKind Star Star, ExternData))
220 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
221 , (primName "String" , (Star, ExternData))
222 , (primName "Char" , (Star, ExternData))
223 , (primName "Number" , (Star, ExternData))
224 , (primName "Int" , (Star, ExternData))
225 , (primName "Boolean" , (Star, ExternData)) ]
227-- |
228-- Finds information about data constructors from the current environment.
229--
230lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
231lookupConstructor env ctor =
232 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
234-- |
235-- Checks whether a data constructor is for a newtype.
236--
237isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
238isNewtypeConstructor e ctor = case lookupConstructor e ctor of
239 (Newtype, _, _, _) -> True
240 (Data, _, _, _) -> False
242-- |
243-- Finds information about values from the current environment.
244--
245lookupValue :: Environment -> Qualified Ident -> Maybe (Type, NameKind, NameVisibility)
246lookupValue env (Qualified (Just mn) ident) = (mn, ident) `M.lookup` names env
247lookupValue _ _ = Nothing