git.iko.soy/rowtype-yoga/purescript-native
on b3741cd help sign in
191 lines 4.7 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Prim
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)
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 kind of a name
80--
81data NameKind
82 -- |
83 -- A value introduced as a binding in a module
84 --
85 = Value
86 -- |
87 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
88 --
89 | TypeClassAccessorImport
90 -- |
91 -- A foreign import
92 --
93 | Extern ForeignImportType
94 -- |
95 -- A local name introduced using a lambda abstraction, variable introduction or binder
96 --
97 | LocalVariable
98 -- |
99 -- A data constructor
100 --
101 | DataConstructor
102 -- |
103 -- A type class dictionary, generated during desugaring of type class declarations
104 --
105 | TypeInstanceDictionaryValue deriving (Show, Eq, Data, Typeable)
107-- |
108-- The kinds of a type
109--
110data TypeKind
111 -- |
112 -- Data type
113 --
114 = DataType [String] [(ProperName, [Type])]
115 -- |
116 -- Type synonym
117 --
118 | TypeSynonym
119 -- |
120 -- Foreign data
121 --
122 | ExternData
123 -- |
124 -- A local type variable
125 --
126 | LocalTypeVariable deriving (Show, Eq, Data, Typeable)
128-- |
129-- Construct a ProperName in the Prim module
130--
131primName :: String -> Qualified ProperName
132primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
134-- |
135-- Construct a type in the Prim module
136--
137primTy :: String -> Type
138primTy = TypeConstructor . primName
140-- |
141-- Type constructor for functions
142--
143tyFunction :: Type
144tyFunction = primTy "Function"
146-- |
147-- Type constructor for strings
148--
149tyString :: Type
150tyString = primTy "String"
152-- |
153-- Type constructor for numbers
154--
155tyNumber :: Type
156tyNumber = primTy "Number"
158-- |
159-- Type constructor for booleans
160--
161tyBoolean :: Type
162tyBoolean = primTy "Boolean"
164-- |
165-- Type constructor for arrays
166--
167tyArray :: Type
168tyArray = primTy "Array"
170-- |
171-- Type constructor for objects
172--
173tyObject :: Type
174tyObject = primTy "Object"
176-- |
177-- Smart constructor for function types
178--
179function :: Type -> Type -> Type
180function t1 = TypeApp (TypeApp tyFunction t1)
182-- |
183-- The primitive types in the external javascript environment with their associated kinds.
184--
185primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
186primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
187 , (primName "Array" , (FunKind Star Star, ExternData))
188 , (primName "Object" , (FunKind (Row Star) Star, ExternData))
189 , (primName "String" , (Star, ExternData))
190 , (primName "Number" , (Star, ExternData))
191 , (primName "Boolean" , (Star, ExternData)) ]