git.iko.soy/rowtype-yoga/purescript-native
on f8da0b9 help sign in
188 lines 4.6 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.Values
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 :: [TypeClassDictionaryInScope]
53 -- |
54 -- Type classes
55 --
56 , typeClasses :: M.Map (Qualified ProperName) ([String], [(Ident, 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
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
77 -- |
78 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
79 --
80 | TypeClassAccessorImport deriving (Show, Eq, Data, Typeable)
82-- |
83-- The kind of a name
84--
85data NameKind
86 -- |
87 -- A value introduced as a binding in a module
88 --
89 = Value
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
106 -- |
107 -- A type instance member, generated during desugaring of type class declarations
108 --
109 | TypeInstanceMember deriving (Show, Eq, Data, Typeable)
111-- |
112-- The kinds of a type
113--
114data TypeKind
115 -- |
116 -- Data type
117 --
118 = DataType [String] [(ProperName, [Type])]
119 -- |
120 -- Type synonym
121 --
122 | TypeSynonym
123 -- |
124 -- Foreign data
125 --
126 | ExternData
127 -- |
128 -- A local type variable
129 --
130 | LocalTypeVariable deriving (Show, Eq, Data, Typeable)
132-- |
133-- Construct a ProperName in the Prim module
134--
135primName :: String -> Qualified ProperName
136primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName
138-- |
139-- Construct a type in the Prim module
140--
141primTy :: String -> Type
142primTy = TypeConstructor . primName
144-- |
145-- Type constructor for functions
146--
147tyFunction :: Type
148tyFunction = primTy "Function"
150-- |
151-- Type constructor for strings
152--
153tyString :: Type
154tyString = primTy "String"
156-- |
157-- Type constructor for numbers
158--
159tyNumber :: Type
160tyNumber = primTy "Number"
162-- |
163-- Type constructor for booleans
164--
165tyBoolean :: Type
166tyBoolean = primTy "Boolean"
168-- |
169-- Type constructor for arrays
170--
171tyArray :: Type
172tyArray = primTy "Array"
174-- |
175-- Smart constructor for function types
176--
177function :: Type -> Type -> Type
178function t1 = TypeApp (TypeApp tyFunction t1)
180-- |
181-- The primitive types in the external javascript environment with their associated kinds.
182--
183primTypes :: M.Map (Qualified ProperName) (Kind, TypeKind)
184primTypes = M.fromList [ (primName "Function" , (FunKind Star (FunKind Star Star), ExternData))
185 , (primName "Array" , (FunKind Star Star, ExternData))
186 , (primName "String" , (Star, ExternData))
187 , (primName "Number" , (Star, ExternData))
188 , (primName "Boolean" , (Star, ExternData)) ]