git.iko.soy/rowtype-yoga/purescript-native
on c7d7bf7 help sign in
241 lines 6.0 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 type of a foreign import
67--
68data ForeignImportType
69 -- |
70 -- A regular foreign import
71 --
72 = ForeignImport
73 -- |
74 -- A foreign import which contains inline Javascript as a string literal
75 --
76 | InlineJavascript deriving (Show, Eq, Data, Typeable)
78-- |
79-- The visibility of a name in scope
80--
81data NameVisibility
82 -- |
83 -- The name is defined in the current binding group, but is not visible
84 --
85 = Undefined
86 -- |
87 -- The name is defined in the another binding group, or has been made visible by a function binder
88 --
89 | Defined deriving (Show, Eq)
91-- |
92-- The kind of a name
93--
94data NameKind
95 -- |
96 -- A value introduced as a binding in a module
97 --
98 = Value
99 -- |
100 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
101 --
102 | TypeClassAccessorImport
103 -- |
104 -- A foreign import
105 --
106 | Extern ForeignImportType
107 -- |
108 -- A local name introduced using a lambda abstraction, variable introduction or binder
109 --
110 | LocalVariable
111 -- |
112 -- A data constructor
113 --
114 | DataConstructor
115 -- |
116 -- A type class dictionary, generated during desugaring of type class declarations
117 --
118 | TypeInstanceDictionaryValue deriving (Show, Eq, Data, Typeable)
120-- |
121-- The kinds of a type
122--
123data TypeKind
124 -- |
125 -- Data type
126 --
127 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
128 -- |
129 -- Type synonym
130 --
131 | TypeSynonym
132 -- |
133 -- Foreign data
134 --
135 | ExternData
136 -- |
137 -- A local type variable
138 --
139 | LocalTypeVariable
140 -- |
141 -- A scoped type variable
142 --
143 | ScopedTypeVar
144 deriving (Show, Eq, Data, Typeable)
146-- |
147-- The type ('data' or 'newtype') of a data type declaration
148--
149data DataDeclType
150 -- |
151 -- A standard data constructor
152 --
153 = Data
154 -- |
155 -- A newtype constructor
156 --
157 | Newtype deriving (Eq, Ord, Data, Typeable)
159instance Show DataDeclType where
160 show Data = "data"
161 show Newtype = "newtype"
163-- |
164-- Construct a ProperName in the Prim module
165--
166primName :: String -> Qualified ProperName
167primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
169-- |
170-- Construct a type in the Prim module
171--
172primTy :: String -> Type
173primTy = TypeConstructor . primName
175-- |
176-- Type constructor for functions
177--
178tyFunction :: Type
179tyFunction = primTy "Function"
181-- |
182-- Type constructor for strings
183--
184tyString :: Type
185tyString = primTy "String"
187-- |
188-- Type constructor for numbers
189--
190tyNumber :: Type
191tyNumber = primTy "Number"
193-- |
194-- Type constructor for booleans
195--
196tyBoolean :: Type
197tyBoolean = primTy "Boolean"
199-- |
200-- Type constructor for arrays
201--
202tyArray :: Type
203tyArray = primTy "Array"
205-- |
206-- Type constructor for objects
207--
208tyObject :: Type
209tyObject = primTy "Object"
211-- |
212-- Smart constructor for function types
213--
214function :: Type -> Type -> Type
215function t1 = TypeApp (TypeApp tyFunction t1)
217-- |
218-- The primitive types in the external javascript environment with their associated kinds.
219--
220primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
221primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
222 , (primName "Array" , (FunKind Star Star, ExternData))
223 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
224 , (primName "String" , (Star, ExternData))
225 , (primName "Number" , (Star, ExternData))
226 , (primName "Boolean" , (Star, ExternData)) ]
228-- |
229-- Finds information about data constructors from the current environment.
230--
231lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
232lookupConstructor env ctor =
233 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
235-- |
236-- Checks whether a data constructor is for a newtype.
237--
238isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
239isNewtypeConstructor e ctor = case lookupConstructor e ctor of
240 (Newtype, _, _, _) -> True
241 (Data, _, _, _) -> False