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