git.iko.soy/rowtype-yoga/purescript-native
on e7a32d5 help sign in
204 lines 5.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
21import Language.PureScript.Names
22import Language.PureScript.Types
23import Language.PureScript.Kinds
24import Language.PureScript.TypeClassDictionaries
25import qualified Language.PureScript.Constants as C
27import qualified Data.Map as M
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) (ProperName, Type)
45 -- |
46 -- Type synonyms currently in scope
47 --
48 , typeSynonyms :: M.Map (Qualified ProperName) ([String], 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], [(Ident, Type)], [(Qualified ProperName, [Type])])
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] [(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 deriving (Show, Eq, Data, Typeable)
141-- |
142-- Construct a ProperName in the Prim module
143--
144primName :: String -> Qualified ProperName
145primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
147-- |
148-- Construct a type in the Prim module
149--
150primTy :: String -> Type
151primTy = TypeConstructor . primName
153-- |
154-- Type constructor for functions
155--
156tyFunction :: Type
157tyFunction = primTy "Function"
159-- |
160-- Type constructor for strings
161--
162tyString :: Type
163tyString = primTy "String"
165-- |
166-- Type constructor for numbers
167--
168tyNumber :: Type
169tyNumber = primTy "Number"
171-- |
172-- Type constructor for booleans
173--
174tyBoolean :: Type
175tyBoolean = primTy "Boolean"
177-- |
178-- Type constructor for arrays
179--
180tyArray :: Type
181tyArray = primTy "Array"
183-- |
184-- Type constructor for objects
185--
186tyObject :: Type
187tyObject = primTy "Object"
189-- |
190-- Smart constructor for function types
191--
192function :: Type -> Type -> Type
193function t1 = TypeApp (TypeApp tyFunction t1)
195-- |
196-- The primitive types in the external javascript environment with their associated kinds.
197--
198primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
199primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
200 , (primName "Array" , (FunKind Star Star, ExternData))
201 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
202 , (primName "String" , (Star, ExternData))
203 , (primName "Number" , (Star, ExternData))
204 , (primName "Boolean" , (Star, ExternData)) ]