git.iko.soy/rowtype-yoga/purescript-native
on 74c2628 help sign in
383 lines 10 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Declarations
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- | Data types for modules and declarations
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Declarations where
19import Language.PureScript.Types
20import Language.PureScript.Names
21import Language.PureScript.Kinds
22import Language.PureScript.TypeClassDictionaries
23import Language.PureScript.CodeGen.JS.AST
24import Language.PureScript.Environment
26import qualified Data.Data as D
27import Data.Generics (mkQ, everything)
29-- |
30-- A precedence level for an infix operator
31--
32type Precedence = Integer
34-- |
35-- Associativity for infix operators
36--
37data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
39instance Show Associativity where
40 show Infixl = "infixl"
41 show Infixr = "infixr"
42 show Infix = "infix"
44-- |
45-- Fixity data for infix operators
46--
47data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
49-- |
50-- A module declaration, consisting of a module name, a list of declarations, and a list of the
51-- declarations that are explicitly imported. If the export list is Nothing, everything is exported.
52--
53data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
55-- |
56-- An item in a list of explicit imports or exports
57--
58data DeclarationRef
59 -- |
60 -- A type constructor with data constructors
61 --
62 = TypeRef ProperName (Maybe [ProperName])
63 -- |
64 -- A value
65 --
66 | ValueRef Ident
67 -- |
68 -- A type class
69 --
70 | TypeClassRef ProperName
71 -- |
72 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
73 --
74 | TypeInstanceRef Ident
75 deriving (Show, Eq, D.Data, D.Typeable)
77-- |
78-- The data type of declarations
79--
80data Declaration
81 -- |
82 -- A data type declaration (name, arguments, data constructors)
83 --
84 = DataDeclaration ProperName [String] [(ProperName, [Type])]
85 -- |
86 -- A minimal mutually recursive set of data type declarations
87 --
88 | DataBindingGroupDeclaration [Declaration]
89 -- |
90 -- A type synonym declaration (name, arguments, type)
91 --
92 | TypeSynonymDeclaration ProperName [String] Type
93 -- |
94 -- A type declaration for a value (name, ty)
95 --
96 | TypeDeclaration Ident Type
97 -- |
98 -- A value declaration (name, top-level binders, optional guard, value)
99 --
100 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
101 -- |
102 -- A minimal mutually recursive set of value declarations
103 --
104 | BindingGroupDeclaration [(Ident, NameKind, Value)]
105 -- |
106 -- A foreign import declaration (type, name, optional inline Javascript, type)
107 --
108 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
109 -- |
110 -- A data type foreign import (name, kind)
111 --
112 | ExternDataDeclaration ProperName Kind
113 -- |
114 -- A type class instance foreign import
115 --
116 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
117 -- |
118 -- A fixity declaration (fixity data, operator name)
119 --
120 | FixityDeclaration Fixity String
121 -- |
122 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
123 -- name)
124 --
125 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
126 -- |
127 -- A type class declaration (name, argument, member declarations)
128 --
129 | TypeClassDeclaration ProperName [String] [Declaration]
130 -- |
131 -- A type instance declaration (name, dependencies, class name, instance types, member
132 -- declarations)
133 --
134 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
135 deriving (Show, D.Data, D.Typeable)
137-- |
138-- Test if a declaration is a value declaration
139--
140isValueDecl :: Declaration -> Bool
141isValueDecl ValueDeclaration{} = True
142isValueDecl _ = False
144-- |
145-- Test if a declaration is a data type or type synonym declaration
146--
147isDataDecl :: Declaration -> Bool
148isDataDecl DataDeclaration{} = True
149isDataDecl TypeSynonymDeclaration{} = True
150isDataDecl _ = False
152-- |
153-- Test if a declaration is a module import
154--
155isImportDecl :: Declaration -> Bool
156isImportDecl ImportDeclaration{} = True
157isImportDecl _ = False
159-- |
160-- Test if a declaration is a data type foreign import
161--
162isExternDataDecl :: Declaration -> Bool
163isExternDataDecl ExternDataDeclaration{} = True
164isExternDataDecl _ = False
166-- |
167-- Test if a declaration is a type class instance foreign import
168--
169isExternInstanceDecl :: Declaration -> Bool
170isExternInstanceDecl ExternInstanceDeclaration{} = True
171isExternInstanceDecl _ = False
173-- |
174-- Test if a declaration is a fixity declaration
175--
176isFixityDecl :: Declaration -> Bool
177isFixityDecl FixityDeclaration{} = True
178isFixityDecl _ = False
180-- |
181-- Test if a declaration is a foreign import
182--
183isExternDecl :: Declaration -> Bool
184isExternDecl ExternDeclaration{} = True
185isExternDecl _ = False
187-- |
188-- Test if a declaration is a type class or instance declaration
189--
190isTypeClassDeclaration :: Declaration -> Bool
191isTypeClassDeclaration TypeClassDeclaration{} = True
192isTypeClassDeclaration TypeInstanceDeclaration{} = True
193isTypeClassDeclaration _ = False
195-- |
196-- A guard is just a boolean-valued expression that appears alongside a set of binders
197--
198type Guard = Value
200-- |
201-- Data type for values
202--
203data Value
204 -- |
205 -- A numeric literal
206 --
207 = NumericLiteral (Either Integer Double)
208 -- |
209 -- A string literal
210 --
211 | StringLiteral String
212 -- |
213 -- A boolean literal
214 --
215 | BooleanLiteral Bool
216 -- |
217 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
218 -- will be removed.
219 --
220 | BinaryNoParens (Qualified Ident) Value Value
221 -- |
222 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
223 -- will be removed.
224 --
225 | Parens Value
226 -- |
227 -- An array literal
228 --
229 | ArrayLiteral [Value]
230 -- |
231 -- An object literal
232 --
233 | ObjectLiteral [(String, Value)]
234 -- |
235 -- An record property accessor expression
236 --
237 | Accessor String Value
238 -- |
239 -- Partial record update
240 --
241 | ObjectUpdate Value [(String, Value)]
242 -- |
243 -- Function introduction
244 --
245 | Abs (Either Ident Binder) Value
246 -- |
247 -- Function application
248 --
249 | App Value Value
250 -- |
251 -- Variable
252 --
253 | Var (Qualified Ident)
254 -- |
255 -- Conditional (if-then-else expression)
256 --
257 | IfThenElse Value Value Value
258 -- |
259 -- A data constructor
260 --
261 | Constructor (Qualified ProperName)
262 -- |
263 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
264 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
265 --
266 | Case [Value] [CaseAlternative]
267 -- |
268 -- A value with a type annotation
269 --
270 | TypedValue Bool Value Type
271 -- |
272 -- A let binding
273 --
274 | Let [Declaration] Value
275 -- |
276 -- A do-notation block
277 --
278 | Do [DoNotationElement]
279 -- |
280 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
281 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
282 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
283 -- instance type, and the type class dictionaries in scope.
284 --
285 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, D.Data, D.Typeable)
287-- |
288-- An alternative in a case statement
289--
290data CaseAlternative = CaseAlternative
291 { -- |
292 -- A collection of binders with which to match the inputs
293 --
294 caseAlternativeBinders :: [Binder]
295 -- |
296 -- An optional guard
297 --
298 , caseAlternativeGuard :: Maybe Guard
299 -- |
300 -- The result expression
301 --
302 , caseAlternativeResult :: Value
303 } deriving (Show, D.Data, D.Typeable)
305-- |
306-- Find the original dictionary which a type class dictionary in scope refers to
307--
308canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
309canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
310canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
312-- |
313-- A statement in a do-notation block
314--
315data DoNotationElement
316 -- |
317 -- A monadic value without a binder
318 --
319 = DoNotationValue Value
320 -- |
321 -- A monadic value with a binder
322 --
323 | DoNotationBind Binder Value
324 -- |
325 -- A let statement, i.e. a pure value with a binder
326 --
327 | DoNotationLet Binder Value deriving (Show, D.Data, D.Typeable)
329-- |
330-- Data type for binders
331--
332data Binder
333 -- |
334 -- Wildcard binder
335 --
336 = NullBinder
337 -- |
338 -- A binder which matches a boolean literal
339 --
340 | BooleanBinder Bool
341 -- |
342 -- A binder which matches a string literal
343 --
344 | StringBinder String
345 -- |
346 -- A binder which matches a numeric literal
347 --
348 | NumberBinder (Either Integer Double)
349 -- |
350 -- A binder which binds an identifier
351 --
352 | VarBinder Ident
353 -- |
354 -- A binder which matches a data constructor
355 --
356 | ConstructorBinder (Qualified ProperName) [Binder]
357 -- |
358 -- A binder which matches a record and binds its properties
359 --
360 | ObjectBinder [(String, Binder)]
361 -- |
362 -- A binder which matches an array and binds its elements
363 --
364 | ArrayBinder [Binder]
365 -- |
366 -- A binder which matches an array and binds its head and tail
367 --
368 | ConsBinder Binder Binder
369 -- |
370 -- A binder which binds its input to an identifier
371 --
372 | NamedBinder Ident Binder deriving (Show, D.Data, D.Typeable)
375-- |
376-- Collect all names introduced in binders in an expression
377--
378binderNames :: (D.Data d) => d -> [Ident]
379binderNames = everything (++) (mkQ [] go)
380 where
381 go (VarBinder ident) = [ident]
382 go (NamedBinder ident _) = [ident]
383 go _ = []