git.iko.soy/rowtype-yoga/purescript-native
on 5cee177 help sign in
236 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 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 deriving (Show, Eq, Data, Typeable)
93-- |
94-- The kinds of a type
95--
96data TypeKind
97 -- |
98 -- Data type
99 --
100 = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
101 -- |
102 -- Type synonym
103 --
104 | TypeSynonym
105 -- |
106 -- Foreign data
107 --
108 | ExternData
109 -- |
110 -- A local type variable
111 --
112 | LocalTypeVariable
113 -- |
114 -- A scoped type variable
115 --
116 | ScopedTypeVar
117 deriving (Show, Eq, Data, Typeable)
119-- |
120-- The type ('data' or 'newtype') of a data type declaration
121--
122data DataDeclType
123 -- |
124 -- A standard data constructor
125 --
126 = Data
127 -- |
128 -- A newtype constructor
129 --
130 | Newtype deriving (Eq, Ord, Data, Typeable)
132instance Show DataDeclType where
133 show Data = "data"
134 show Newtype = "newtype"
136-- |
137-- Construct a ProperName in the Prim module
138--
139primName :: String -> Qualified ProperName
140primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
142-- |
143-- Construct a type in the Prim module
144--
145primTy :: String -> Type
146primTy = TypeConstructor . primName
148-- |
149-- Type constructor for functions
150--
151tyFunction :: Type
152tyFunction = primTy "Function"
154-- |
155-- Type constructor for strings
156--
157tyString :: Type
158tyString = primTy "String"
160-- |
161-- Type constructor for strings
162--
163tyChar :: Type
164tyChar = primTy "Char"
166-- |
167-- Type constructor for numbers
168--
169tyNumber :: Type
170tyNumber = primTy "Number"
172-- |
173-- Type constructor for integers
174--
175tyInt :: Type
176tyInt = primTy "Int"
178-- |
179-- Type constructor for booleans
180--
181tyBoolean :: Type
182tyBoolean = primTy "Boolean"
184-- |
185-- Type constructor for arrays
186--
187tyArray :: Type
188tyArray = primTy "Array"
190-- |
191-- Type constructor for objects
192--
193tyObject :: Type
194tyObject = primTy "Object"
196-- |
197-- Check whether a type is an object
198--
199isObject :: Type -> Bool
200isObject = (==) tyObject . extract
201 where extract (TypeApp t _) = t
202 extract t = t
204-- |
205-- Smart constructor for function types
206--
207function :: Type -> Type -> Type
208function t1 = TypeApp (TypeApp tyFunction t1)
210-- |
211-- The primitive types in the external javascript environment with their associated kinds.
212--
213primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
214primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
215 , (primName "Array" , (FunKind Star Star, ExternData))
216 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
217 , (primName "String" , (Star, ExternData))
218 , (primName "Char" , (Star, ExternData))
219 , (primName "Number" , (Star, ExternData))
220 , (primName "Int" , (Star, ExternData))
221 , (primName "Boolean" , (Star, ExternData)) ]
223-- |
224-- Finds information about data constructors from the current environment.
225--
226lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type, [Ident])
227lookupConstructor env ctor =
228 fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors env
230-- |
231-- Checks whether a data constructor is for a newtype.
232--
233isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
234isNewtypeConstructor e ctor = case lookupConstructor e ctor of
235 (Newtype, _, _, _) -> True
236 (Data, _, _, _) -> False