git.iko.soy/rowtype-yoga/purescript-native
on 613716c help sign in
831 lines 32 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, ScopedTypeVariables #-}
17module Language.PureScript.Declarations where
19import Data.Monoid (Monoid(..), mconcat)
21import qualified Data.Data as D
23import Control.Applicative
24import Control.Monad
26import Language.PureScript.Types
27import Language.PureScript.Names
28import Language.PureScript.Kinds
29import Language.PureScript.TypeClassDictionaries
30import Language.PureScript.CodeGen.JS.AST
31import Language.PureScript.Environment
32import Language.PureScript.Traversals
34-- |
35-- A precedence level for an infix operator
36--
37type Precedence = Integer
39-- |
40-- Associativity for infix operators
41--
42data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
44instance Show Associativity where
45 show Infixl = "infixl"
46 show Infixr = "infixr"
47 show Infix = "infix"
49-- |
50-- Source position information
51--
52data SourcePos = SourcePos
53 { -- |
54 -- Source name
55 --
56 sourceName :: String
57 -- |
58 -- Line number
59 --
60 , sourcePosLine :: Int
61 -- |
62 -- Column number
63 --
64 , sourcePosColumn :: Int
65 } deriving (D.Data, D.Typeable)
67instance Show SourcePos where
68 show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
70-- |
71-- Fixity data for infix operators
72--
73data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
75-- |
76-- A module declaration, consisting of a module name, a list of declarations, and a list of the
77-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
78--
79data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
81-- |
82-- An item in a list of explicit imports or exports
83--
84data DeclarationRef
85 -- |
86 -- A type constructor with data constructors
87 --
88 = TypeRef ProperName (Maybe [ProperName])
89 -- |
90 -- A value
91 --
92 | ValueRef Ident
93 -- |
94 -- A type class
95 --
96 | TypeClassRef ProperName
97 -- |
98 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
99 --
100 | TypeInstanceRef Ident
101 -- |
102 -- A declaration reference with source position information
103 --
104 | PositionedDeclarationRef SourcePos DeclarationRef
105 deriving (Show, D.Data, D.Typeable)
107instance Eq DeclarationRef where
108 (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
109 (ValueRef name) == (ValueRef name') = name == name'
110 (TypeClassRef name) == (TypeClassRef name') = name == name'
111 (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
112 (PositionedDeclarationRef _ r) == r' = r == r'
113 r == (PositionedDeclarationRef _ r') = r == r'
114 _ == _ = False
116-- |
117-- The data type of declarations
118--
119data Declaration
120 -- |
121 -- A data type declaration (data or newtype, name, arguments, data constructors)
122 --
123 = DataDeclaration DataDeclType ProperName [String] [(ProperName, [Type])]
124 -- |
125 -- A minimal mutually recursive set of data type declarations
126 --
127 | DataBindingGroupDeclaration [Declaration]
128 -- |
129 -- A type synonym declaration (name, arguments, type)
130 --
131 | TypeSynonymDeclaration ProperName [String] Type
132 -- |
133 -- A type declaration for a value (name, ty)
134 --
135 | TypeDeclaration Ident Type
136 -- |
137 -- A value declaration (name, top-level binders, optional guard, value)
138 --
139 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
140 -- |
141 -- A minimal mutually recursive set of value declarations
142 --
143 | BindingGroupDeclaration [(Ident, NameKind, Value)]
144 -- |
145 -- A foreign import declaration (type, name, optional inline Javascript, type)
146 --
147 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
148 -- |
149 -- A data type foreign import (name, kind)
150 --
151 | ExternDataDeclaration ProperName Kind
152 -- |
153 -- A type class instance foreign import
154 --
155 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
156 -- |
157 -- A fixity declaration (fixity data, operator name)
158 --
159 | FixityDeclaration Fixity String
160 -- |
161 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
162 -- name)
163 --
164 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
165 -- |
166 -- A type class declaration (name, argument, implies, member declarations)
167 --
168 | TypeClassDeclaration ProperName [String] [(Qualified ProperName, [Type])] [Declaration]
169 -- |
170 -- A type instance declaration (name, dependencies, class name, instance types, member
171 -- declarations)
172 --
173 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
174 -- |
175 -- A declaration with source position information
176 --
177 | PositionedDeclaration SourcePos Declaration
178 deriving (Show, D.Data, D.Typeable)
180-- |
181-- Test if a declaration is a value declaration
182--
183isValueDecl :: Declaration -> Bool
184isValueDecl ValueDeclaration{} = True
185isValueDecl (PositionedDeclaration _ d) = isValueDecl d
186isValueDecl _ = False
188-- |
189-- Test if a declaration is a data type or type synonym declaration
190--
191isDataDecl :: Declaration -> Bool
192isDataDecl DataDeclaration{} = True
193isDataDecl TypeSynonymDeclaration{} = True
194isDataDecl (PositionedDeclaration _ d) = isDataDecl d
195isDataDecl _ = False
197-- |
198-- Test if a declaration is a module import
199--
200isImportDecl :: Declaration -> Bool
201isImportDecl ImportDeclaration{} = True
202isImportDecl (PositionedDeclaration _ d) = isImportDecl d
203isImportDecl _ = False
205-- |
206-- Test if a declaration is a data type foreign import
207--
208isExternDataDecl :: Declaration -> Bool
209isExternDataDecl ExternDataDeclaration{} = True
210isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
211isExternDataDecl _ = False
213-- |
214-- Test if a declaration is a type class instance foreign import
215--
216isExternInstanceDecl :: Declaration -> Bool
217isExternInstanceDecl ExternInstanceDeclaration{} = True
218isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
219isExternInstanceDecl _ = False
221-- |
222-- Test if a declaration is a fixity declaration
223--
224isFixityDecl :: Declaration -> Bool
225isFixityDecl FixityDeclaration{} = True
226isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
227isFixityDecl _ = False
229-- |
230-- Test if a declaration is a foreign import
231--
232isExternDecl :: Declaration -> Bool
233isExternDecl ExternDeclaration{} = True
234isExternDecl (PositionedDeclaration _ d) = isExternDecl d
235isExternDecl _ = False
237-- |
238-- Test if a declaration is a type class or instance declaration
239--
240isTypeClassDeclaration :: Declaration -> Bool
241isTypeClassDeclaration TypeClassDeclaration{} = True
242isTypeClassDeclaration TypeInstanceDeclaration{} = True
243isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
244isTypeClassDeclaration _ = False
246-- |
247-- A guard is just a boolean-valued expression that appears alongside a set of binders
248--
249type Guard = Value
251-- |
252-- Data type for values
253--
254data Value
255 -- |
256 -- A numeric literal
257 --
258 = NumericLiteral (Either Integer Double)
259 -- |
260 -- A string literal
261 --
262 | StringLiteral String
263 -- |
264 -- A boolean literal
265 --
266 | BooleanLiteral Bool
267 -- |
268 -- A prefix -, will be desugared
269 --
270 | UnaryMinus Value
271 -- |
272 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
273 -- will be removed.
274 --
275 | BinaryNoParens (Qualified Ident) Value Value
276 -- |
277 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
278 -- will be removed.
279 --
280 | Parens Value
281 -- |
282 -- An array literal
283 --
284 | ArrayLiteral [Value]
285 -- |
286 -- An object literal
287 --
288 | ObjectLiteral [(String, Value)]
289 -- |
290 -- An record property accessor expression
291 --
292 | Accessor String Value
293 -- |
294 -- Partial record update
295 --
296 | ObjectUpdate Value [(String, Value)]
297 -- |
298 -- Function introduction
299 --
300 | Abs (Either Ident Binder) Value
301 -- |
302 -- Function application
303 --
304 | App Value Value
305 -- |
306 -- Variable
307 --
308 | Var (Qualified Ident)
309 -- |
310 -- Conditional (if-then-else expression)
311 --
312 | IfThenElse Value Value Value
313 -- |
314 -- A data constructor
315 --
316 | Constructor (Qualified ProperName)
317 -- |
318 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
319 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
320 --
321 | Case [Value] [CaseAlternative]
322 -- |
323 -- A value with a type annotation
324 --
325 | TypedValue Bool Value Type
326 -- |
327 -- A let binding
328 --
329 | Let [Declaration] Value
330 -- |
331 -- A do-notation block
332 --
333 | Do [DoNotationElement]
334 -- |
335 -- An application of a typeclass dictionary constructor. The value should be
336 -- an ObjectLiteral.
337 --
338 | TypeClassDictionaryConstructorApp (Qualified ProperName) Value
339 -- |
340 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
341 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
342 -- can be evaluated at runtime. The constructor arguments represent (in order): whether or not to look
343 -- at superclass implementations when searching for a dictionary, the type class name and
344 -- instance type, and the type class dictionaries in scope.
345 --
346 | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
347 -- |
348 -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
349 --
350 | SuperClassDictionary (Qualified ProperName) [Type]
351 -- |
352 -- A value with source position information
353 --
354 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
356-- |
357-- An alternative in a case statement
358--
359data CaseAlternative = CaseAlternative
360 { -- |
361 -- A collection of binders with which to match the inputs
362 --
363 caseAlternativeBinders :: [Binder]
364 -- |
365 -- An optional guard
366 --
367 , caseAlternativeGuard :: Maybe Guard
368 -- |
369 -- The result expression
370 --
371 , caseAlternativeResult :: Value
372 } deriving (Show, D.Data, D.Typeable)
374-- |
375-- Find the original dictionary which a type class dictionary in scope refers to
376--
377canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
378canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
379canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
381-- |
382-- A statement in a do-notation block
383--
384data DoNotationElement
385 -- |
386 -- A monadic value without a binder
387 --
388 = DoNotationValue Value
389 -- |
390 -- A monadic value with a binder
391 --
392 | DoNotationBind Binder Value
393 -- |
394 -- A let statement, i.e. a pure value with a binder
395 --
396 | DoNotationLet [Declaration]
397 -- |
398 -- A do notation element with source position information
399 --
400 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
402-- |
403-- Data type for binders
404--
405data Binder
406 -- |
407 -- Wildcard binder
408 --
409 = NullBinder
410 -- |
411 -- A binder which matches a boolean literal
412 --
413 | BooleanBinder Bool
414 -- |
415 -- A binder which matches a string literal
416 --
417 | StringBinder String
418 -- |
419 -- A binder which matches a numeric literal
420 --
421 | NumberBinder (Either Integer Double)
422 -- |
423 -- A binder which binds an identifier
424 --
425 | VarBinder Ident
426 -- |
427 -- A binder which matches a data constructor
428 --
429 | ConstructorBinder (Qualified ProperName) [Binder]
430 -- |
431 -- A binder which matches a record and binds its properties
432 --
433 | ObjectBinder [(String, Binder)]
434 -- |
435 -- A binder which matches an array and binds its elements
436 --
437 | ArrayBinder [Binder]
438 -- |
439 -- A binder which matches an array and binds its head and tail
440 --
441 | ConsBinder Binder Binder
442 -- |
443 -- A binder which binds its input to an identifier
444 --
445 | NamedBinder Ident Binder
446 -- |
447 -- A binder with source position information
448 --
449 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
451-- |
452-- Collect all names introduced in binders in an expression
453--
454binderNames :: Binder -> [Ident]
455binderNames = go []
456 where
457 go ns (VarBinder name) = name : ns
458 go ns (ConstructorBinder _ bs) = foldl go ns bs
459 go ns (ObjectBinder bs) = foldl go ns (map snd bs)
460 go ns (ArrayBinder bs) = foldl go ns bs
461 go ns (ConsBinder b1 b2) = go (go ns b1) b2
462 go ns (NamedBinder name b) = go (name : ns) b
463 go ns (PositionedBinder _ b) = go ns b
464 go ns _ = ns
466--
467-- Traversals
468--
470everywhereOnValues :: (Declaration -> Declaration) ->
471 (Value -> Value) ->
472 (Binder -> Binder) ->
473 (Declaration -> Declaration, Value -> Value, Binder -> Binder)
474everywhereOnValues f g h = (f', g', h')
475 where
476 f' :: Declaration -> Declaration
477 f' (DataBindingGroupDeclaration ds) = f (DataBindingGroupDeclaration (map f' ds))
478 f' (ValueDeclaration name nameKind bs grd val) = f (ValueDeclaration name nameKind (map h' bs) (fmap g' grd) (g' val))
479 f' (BindingGroupDeclaration ds) = f (BindingGroupDeclaration (map (\(name, nameKind, val) -> (name, nameKind, g' val)) ds))
480 f' (TypeClassDeclaration name args implies ds) = f (TypeClassDeclaration name args implies (map f' ds))
481 f' (TypeInstanceDeclaration name cs className args ds) = f (TypeInstanceDeclaration name cs className args (map f' ds))
482 f' (PositionedDeclaration pos d) = f (PositionedDeclaration pos (f' d))
483 f' other = f other
485 g' :: Value -> Value
486 g' (UnaryMinus v) = g (UnaryMinus (g' v))
487 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens op (g' v1) (g' v2))
488 g' (Parens v) = g (Parens (g' v))
489 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
490 g' (ObjectLiteral vs) = g (ObjectLiteral (map (fmap g') vs))
491 g' (TypeClassDictionaryConstructorApp name v) = g (TypeClassDictionaryConstructorApp name (g' v))
492 g' (Accessor prop v) = g (Accessor prop (g' v))
493 g' (ObjectUpdate obj vs) = g (ObjectUpdate (g' obj) (map (fmap g') vs))
494 g' (Abs name v) = g (Abs name (g' v))
495 g' (App v1 v2) = g (App (g' v1) (g' v2))
496 g' (IfThenElse v1 v2 v3) = g (IfThenElse (g' v1) (g' v2) (g' v3))
497 g' (Case vs alts) = g (Case (map g' vs) (map handleCaseAlternative alts))
498 g' (TypedValue check v ty) = g (TypedValue check (g' v) ty)
499 g' (Let ds v) = g (Let (map f' ds) (g' v))
500 g' (Do es) = g (Do (map handleDoNotationElement es))
501 g' (PositionedValue pos v) = g (PositionedValue pos (g' v))
502 g' other = g other
504 h' :: Binder -> Binder
505 h' (ConstructorBinder ctor bs) = h (ConstructorBinder ctor (map h' bs))
506 h' (ObjectBinder bs) = h (ObjectBinder (map (fmap h') bs))
507 h' (ArrayBinder bs) = h (ArrayBinder (map h' bs))
508 h' (ConsBinder b1 b2) = h (ConsBinder (h' b1) (h' b2))
509 h' (NamedBinder name b) = h (NamedBinder name (h' b))
510 h' (PositionedBinder pos b) = h (PositionedBinder pos (h' b))
511 h' other = h other
513 handleCaseAlternative :: CaseAlternative -> CaseAlternative
514 handleCaseAlternative ca =
515 ca { caseAlternativeBinders = map h' (caseAlternativeBinders ca)
516 , caseAlternativeGuard = fmap g' (caseAlternativeGuard ca)
517 , caseAlternativeResult = g' (caseAlternativeResult ca)
518 }
520 handleDoNotationElement :: DoNotationElement -> DoNotationElement
521 handleDoNotationElement (DoNotationValue v) = DoNotationValue (g' v)
522 handleDoNotationElement (DoNotationBind b v) = DoNotationBind (h' b) (g' v)
523 handleDoNotationElement (DoNotationLet ds) = DoNotationLet (map f' ds)
524 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos (handleDoNotationElement e)
527everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
528 (Declaration -> m Declaration) ->
529 (Value -> m Value) ->
530 (Binder -> m Binder) ->
531 (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
532everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
533 where
534 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
535 f' (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> maybeM (g' <=< g) grd <*> (g val >>= g')
536 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
537 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
538 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
539 f' (PositionedDeclaration pos d) = PositionedDeclaration pos <$> (f d >>= f')
540 f' other = f other
542 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
543 g' (BinaryNoParens op v1 v2) = BinaryNoParens op <$> (g v1 >>= g') <*> (g v2 >>= g')
544 g' (Parens v) = Parens <$> (g v >>= g')
545 g' (ArrayLiteral vs) = ArrayLiteral <$> mapM (g' <=< g) vs
546 g' (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g' <=< g)) vs
547 g' (TypeClassDictionaryConstructorApp name v) = TypeClassDictionaryConstructorApp name <$> (g v >>= g')
548 g' (Accessor prop v) = Accessor prop <$> (g v >>= g')
549 g' (ObjectUpdate obj vs) = ObjectUpdate <$> (g obj >>= g') <*> mapM (sndM (g' <=< g)) vs
550 g' (Abs name v) = Abs name <$> (g v >>= g')
551 g' (App v1 v2) = App <$> (g v1 >>= g') <*> (g v2 >>= g')
552 g' (IfThenElse v1 v2 v3) = IfThenElse <$> (g v1 >>= g') <*> (g v2 >>= g') <*> (g v3 >>= g')
553 g' (Case vs alts) = Case <$> mapM (g' <=< g) vs <*> mapM handleCaseAlternative alts
554 g' (TypedValue check v ty) = TypedValue check <$> (g v >>= g') <*> pure ty
555 g' (Let ds v) = Let <$> mapM (f' <=< f) ds <*> (g v >>= g')
556 g' (Do es) = Do <$> mapM handleDoNotationElement es
557 g' (PositionedValue pos v) = PositionedValue pos <$> (g v >>= g')
558 g' other = g other
560 h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h' <=< h) bs
561 h' (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h' <=< h)) bs
562 h' (ArrayBinder bs) = ArrayBinder <$> mapM (h' <=< h) bs
563 h' (ConsBinder b1 b2) = ConsBinder <$> (h b1 >>= h') <*> (h b2 >>= h')
564 h' (NamedBinder name b) = NamedBinder name <$> (h b >>= h')
565 h' (PositionedBinder pos b) = PositionedBinder pos <$> (h b >>= h')
566 h' other = h other
568 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h' <=< h) bs
569 <*> maybeM (g' <=< g) grd
570 <*> (g' <=< g) val
572 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
573 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
574 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM (f' <=< f) ds
575 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
577everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
578 (Declaration -> m Declaration) ->
579 (Value -> m Value) ->
580 (Binder -> m Binder) ->
581 (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
582everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
583 where
584 f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
585 f' (ValueDeclaration name nameKind bs grd val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> maybeM g' grd <*> g' val) >>= f
586 f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
587 f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
588 f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> mapM f' ds) >>= f
589 f' (PositionedDeclaration pos d) = (PositionedDeclaration pos <$> f' d) >>= f
590 f' other = f other
592 g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
593 g' (BinaryNoParens op v1 v2) = (BinaryNoParens op <$> (g' v1) <*> (g' v2)) >>= g
594 g' (Parens v) = (Parens <$> g' v) >>= g
595 g' (ArrayLiteral vs) = (ArrayLiteral <$> mapM g' vs) >>= g
596 g' (ObjectLiteral vs) = (ObjectLiteral <$> mapM (sndM g') vs) >>= g
597 g' (TypeClassDictionaryConstructorApp name v) = (TypeClassDictionaryConstructorApp name <$> g' v) >>= g
598 g' (Accessor prop v) = (Accessor prop <$> g' v) >>= g
599 g' (ObjectUpdate obj vs) = (ObjectUpdate <$> g' obj <*> mapM (sndM g') vs) >>= g
600 g' (Abs name v) = (Abs name <$> g' v) >>= g
601 g' (App v1 v2) = (App <$> g' v1 <*> g' v2) >>= g
602 g' (IfThenElse v1 v2 v3) = (IfThenElse <$> g' v1 <*> g' v2 <*> g' v3) >>= g
603 g' (Case vs alts) = (Case <$> mapM g' vs <*> mapM handleCaseAlternative alts) >>= g
604 g' (TypedValue check v ty) = (TypedValue check <$> g' v <*> pure ty) >>= g
605 g' (Let ds v) = (Let <$> mapM f' ds <*> g' v) >>= g
606 g' (Do es) = (Do <$> mapM handleDoNotationElement es) >>= g
607 g' (PositionedValue pos v) = (PositionedValue pos <$> g' v) >>= g
608 g' other = g other
610 h' (ConstructorBinder ctor bs) = (ConstructorBinder ctor <$> mapM h' bs) >>= h
611 h' (ObjectBinder bs) = (ObjectBinder <$> mapM (sndM h') bs) >>= h
612 h' (ArrayBinder bs) = (ArrayBinder <$> mapM h' bs) >>= h
613 h' (ConsBinder b1 b2) = (ConsBinder <$> h' b1 <*> h' b2) >>= h
614 h' (NamedBinder name b) = (NamedBinder name <$> h' b) >>= h
615 h' (PositionedBinder pos b) = (PositionedBinder pos <$> h' b) >>= h
616 h' other = h other
618 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM h' bs
619 <*> maybeM g' grd
620 <*> g' val
622 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
623 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
624 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM f' ds
625 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
627everythingOnValues :: (r -> r -> r) ->
628 (Declaration -> r) ->
629 (Value -> r) ->
630 (Binder -> r) ->
631 (CaseAlternative -> r) ->
632 (DoNotationElement -> r) ->
633 (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
634everythingOnValues (<>) f g h i j = (f', g', h', i', j')
635 where
636 f' d@(DataBindingGroupDeclaration ds) = foldl (<>) (f d) (map f' ds)
637 f' d@(ValueDeclaration _ _ bs Nothing val) = foldl (<>) (f d) (map h' bs) <> g' val
638 f' d@(ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) (f d) (map h' bs) <> g' grd <> g' val
639 f' d@(BindingGroupDeclaration ds) = foldl (<>) (f d) (map (\(_, _, val) -> g' val) ds)
640 f' d@(TypeClassDeclaration _ _ _ ds) = foldl (<>) (f d) (map f' ds)
641 f' d@(TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) (f d) (map f' ds)
642 f' d@(PositionedDeclaration _ d1) = f d <> f' d1
643 f' d = f d
645 g' v@(UnaryMinus v1) = g v <> g' v1
646 g' v@(BinaryNoParens _ v1 v2) = g v <> g' v1 <> g' v2
647 g' v@(Parens v1) = g v <> g' v1
648 g' v@(ArrayLiteral vs) = foldl (<>) (g v) (map g' vs)
649 g' v@(ObjectLiteral vs) = foldl (<>) (g v) (map (g' . snd) vs)
650 g' v@(TypeClassDictionaryConstructorApp _ v1) = g v <> g' v1
651 g' v@(Accessor _ v1) = g v <> g' v1
652 g' v@(ObjectUpdate obj vs) = foldl (<>) (g v <> g' obj) (map (g' . snd) vs)
653 g' v@(Abs _ v1) = g v <> g' v1
654 g' v@(App v1 v2) = g v <> g' v1 <> g' v2
655 g' v@(IfThenElse v1 v2 v3) = g v <> g' v1 <> g' v2 <> g' v3
656 g' v@(Case vs alts) = foldl (<>) (foldl (<>) (g v) (map g' vs)) (map i' alts)
657 g' v@(TypedValue _ v1 _) = g v <> g' v1
658 g' v@(Let ds v1) = (foldl (<>) (g v) (map f' ds)) <> g' v1
659 g' v@(Do es) = foldl (<>) (g v) (map j' es)
660 g' v@(PositionedValue _ v1) = g v <> g' v1
661 g' v = g v
663 h' b@(ConstructorBinder _ bs) = foldl (<>) (h b) (map h' bs)
664 h' b@(ObjectBinder bs) = foldl (<>) (h b) (map (h' . snd) bs)
665 h' b@(ArrayBinder bs) = foldl (<>) (h b) (map h' bs)
666 h' b@(ConsBinder b1 b2) = h b <> h' b1 <> h' b2
667 h' b@(NamedBinder _ b1) = h b <> h' b1
668 h' b@(PositionedBinder _ b1) = h b <> h' b1
669 h' b = h b
671 i' ca = case caseAlternativeGuard ca of
672 Nothing -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' (caseAlternativeResult ca)
673 Just grd -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' grd <> g' (caseAlternativeResult ca)
675 j' e@(DoNotationValue v) = j e <> g' v
676 j' e@(DoNotationBind b v) = j e <> h' b <> g' v
677 j' e@(DoNotationLet ds) = foldl (<>) (j e) (map f' ds)
678 j' e@(PositionedDoNotationElement _ e1) = j e <> j' e1
680everythingWithContextOnValues ::
681 s ->
682 r ->
683 (r -> r -> r) ->
684 (s -> Declaration -> (s, r)) ->
685 (s -> Value -> (s, r)) ->
686 (s -> Binder -> (s, r)) ->
687 (s -> CaseAlternative -> (s, r)) ->
688 (s -> DoNotationElement -> (s, r)) ->
689 ( Declaration -> r
690 , Value -> r
691 , Binder -> r
692 , CaseAlternative -> r
693 , DoNotationElement -> r)
694everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
695 where
696 f'' s d = let (s', r) = f s d in r <> f' s' d
698 f' s (DataBindingGroupDeclaration ds) = foldl (<>) r0 (map (f'' s) ds)
699 f' s (ValueDeclaration _ _ bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
700 f' s (ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
701 f' s (BindingGroupDeclaration ds) = foldl (<>) r0 (map (\(_, _, val) -> (g'' s) val) ds)
702 f' s (TypeClassDeclaration _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
703 f' s (TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
704 f' s (PositionedDeclaration _ d1) = (f'' s) d1
705 f' _ _ = r0
707 g'' s v = let (s', r) = g s v in r <> g' s' v
709 g' s (UnaryMinus v1) = (g'' s) v1
710 g' s (BinaryNoParens _ v1 v2) = (g'' s) v1 <> (g'' s) v2
711 g' s (Parens v1) = (g'' s) v1
712 g' s (ArrayLiteral vs) = foldl (<>) r0 (map (g'' s) vs)
713 g' s (ObjectLiteral vs) = foldl (<>) r0 (map (g'' s . snd) vs)
714 g' s (TypeClassDictionaryConstructorApp _ v1) = (g'' s) v1
715 g' s (Accessor _ v1) = (g'' s) v1
716 g' s (ObjectUpdate obj vs) = foldl (<>) ((g'' s) obj) (map (g'' s . snd) vs)
717 g' s (Abs _ v1) = (g'' s) v1
718 g' s (App v1 v2) = (g'' s) v1 <> (g'' s) v2
719 g' s (IfThenElse v1 v2 v3) = (g'' s) v1 <> (g'' s) v2 <> (g'' s) v3
720 g' s (Case vs alts) = foldl (<>) (foldl (<>) r0 (map (g'' s) vs)) (map (i'' s) alts)
721 g' s (TypedValue _ v1 _) = (g'' s) v1
722 g' s (Let ds v1) = (foldl (<>) r0 (map (f'' s) ds)) <> (g'' s) v1
723 g' s (Do es) = foldl (<>) r0 (map (j'' s) es)
724 g' s (PositionedValue _ v1) = (g'' s) v1
725 g' _ _ = r0
727 h'' s b = let (s', r) = h s b in r <> h' s' b
729 h' s (ConstructorBinder _ bs) = foldl (<>) r0 (map (h'' s) bs)
730 h' s (ObjectBinder bs) = foldl (<>) r0 (map (h'' s . snd) bs)
731 h' s (ArrayBinder bs) = foldl (<>) r0 (map (h'' s) bs)
732 h' s (ConsBinder b1 b2) = (h'' s) b1 <> (h'' s) b2
733 h' s (NamedBinder _ b1) = (h'' s) b1
734 h' s (PositionedBinder _ b1) = (h'' s) b1
735 h' _ _ = r0
737 i'' s ca = let (s', r) = i s ca in r <> i' s' ca
739 i' s (CaseAlternative bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
740 i' s (CaseAlternative bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
742 j'' s e = let (s', r) = j s e in r <> j' s' e
744 j' s (DoNotationValue v) = (g'' s) v
745 j' s (DoNotationBind b v) = (h'' s) b <> (g'' s) v
746 j' s (DoNotationLet ds) = foldl (<>) r0 (map (f'' s) ds)
747 j' s (PositionedDoNotationElement _ e1) = (j'' s) e1
749everywhereWithContextOnValuesM :: (Functor m, Applicative m, Monad m) =>
750 s ->
751 (s -> Declaration -> m (s, Declaration)) ->
752 (s -> Value -> m (s, Value)) ->
753 (s -> Binder -> m (s, Binder)) ->
754 (s -> CaseAlternative -> m (s, CaseAlternative)) ->
755 (s -> DoNotationElement -> m (s, DoNotationElement)) ->
756 ( Declaration -> m Declaration
757 , Value -> m Value
758 , Binder -> m Binder
759 , CaseAlternative -> m CaseAlternative
760 , DoNotationElement -> m DoNotationElement)
761everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
762 where
763 f'' s = uncurry f' <=< f s
765 f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f'' s) ds
766 f' s (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
767 f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (thirdM (g'' s)) ds
768 f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f'' s) ds
769 f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f'' s) ds
770 f' s (PositionedDeclaration pos d1) = PositionedDeclaration pos <$> f'' s d1
771 f' _ other = return other
773 g'' s = uncurry g' <=< g s
775 g' s (UnaryMinus v) = UnaryMinus <$> g'' s v
776 g' s (BinaryNoParens op v1 v2) = BinaryNoParens op <$> g'' s v1 <*> g'' s v2
777 g' s (Parens v) = Parens <$> g'' s v
778 g' s (ArrayLiteral vs) = ArrayLiteral <$> mapM (g'' s) vs
779 g' s (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g'' s)) vs
780 g' s (TypeClassDictionaryConstructorApp name v) = TypeClassDictionaryConstructorApp name <$> (g'' s) v
781 g' s (Accessor prop v) = Accessor prop <$> g'' s v
782 g' s (ObjectUpdate obj vs) = ObjectUpdate <$> g'' s obj <*> mapM (sndM (g'' s)) vs
783 g' s (Abs name v) = Abs name <$> g'' s v
784 g' s (App v1 v2) = App <$> g'' s v1 <*> g'' s v2
785 g' s (IfThenElse v1 v2 v3) = IfThenElse <$> g'' s v1 <*> g'' s v2 <*> g'' s v3
786 g' s (Case vs alts) = Case <$> mapM (g'' s) vs <*> mapM (i'' s) alts
787 g' s (TypedValue check v ty) = TypedValue check <$> g'' s v <*> pure ty
788 g' s (Let ds v) = Let <$> mapM (f'' s) ds <*> g'' s v
789 g' s (Do es) = Do <$> mapM (j'' s) es
790 g' s (PositionedValue pos v) = PositionedValue pos <$> g'' s v
791 g' _ other = return other
793 h'' s = uncurry h' <=< h s
795 h' s (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h'' s) bs
796 h' s (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h'' s)) bs
797 h' s (ArrayBinder bs) = ArrayBinder <$> mapM (h'' s) bs
798 h' s (ConsBinder b1 b2) = ConsBinder <$> h'' s b1 <*> h'' s b2
799 h' s (NamedBinder name b) = NamedBinder name <$> h'' s b
800 h' s (PositionedBinder pos b) = PositionedBinder pos <$> h'' s b
801 h' _ other = return other
803 i'' s = uncurry i' <=< i s
805 i' s (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
807 j'' s = uncurry j' <=< j s
809 j' s (DoNotationValue v) = DoNotationValue <$> g'' s v
810 j' s (DoNotationBind b v) = DoNotationBind <$> h'' s b <*> g'' s v
811 j' s (DoNotationLet ds) = DoNotationLet <$> mapM (f'' s) ds
812 j' s (PositionedDoNotationElement pos e1) = PositionedDoNotationElement pos <$> j'' s e1
814accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
815accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)
816 where
817 forDecls (DataDeclaration _ _ _ dctors) = mconcat (concatMap (map f . snd) dctors)
818 forDecls (ExternDeclaration _ _ _ ty) = f ty
819 forDecls (ExternInstanceDeclaration _ cs _ tys) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
820 forDecls (TypeClassDeclaration _ _ implies _) = mconcat (concatMap (map f . snd) implies)
821 forDecls (TypeInstanceDeclaration _ cs _ tys _) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
822 forDecls (TypeSynonymDeclaration _ _ ty) = f ty
823 forDecls (TypeDeclaration _ ty) = f ty
824 forDecls _ = mempty
826 forValues (TypeClassDictionary _ (_, cs) _) = mconcat (map f cs)
827 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
828 forValues (TypedValue _ _ ty) = f ty
829 forValues _ = mempty