git.iko.soy/rowtype-yoga/purescript-native
on c4186db help sign in
820 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 (name, arguments, data constructors)
122 --
123 = DataDeclaration 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 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
336 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
337 -- can be evaluated at runtime. The constructor arguments represent (in order): whether or not to look
338 -- at superclass implementations when searching for a dictionary, the type class name and
339 -- instance type, and the type class dictionaries in scope.
340 --
341 | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
342 -- |
343 -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
344 --
345 | SuperClassDictionary (Qualified ProperName) [Type]
346 -- |
347 -- A value with source position information
348 --
349 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
351-- |
352-- An alternative in a case statement
353--
354data CaseAlternative = CaseAlternative
355 { -- |
356 -- A collection of binders with which to match the inputs
357 --
358 caseAlternativeBinders :: [Binder]
359 -- |
360 -- An optional guard
361 --
362 , caseAlternativeGuard :: Maybe Guard
363 -- |
364 -- The result expression
365 --
366 , caseAlternativeResult :: Value
367 } deriving (Show, D.Data, D.Typeable)
369-- |
370-- Find the original dictionary which a type class dictionary in scope refers to
371--
372canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
373canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
374canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
376-- |
377-- A statement in a do-notation block
378--
379data DoNotationElement
380 -- |
381 -- A monadic value without a binder
382 --
383 = DoNotationValue Value
384 -- |
385 -- A monadic value with a binder
386 --
387 | DoNotationBind Binder Value
388 -- |
389 -- A let statement, i.e. a pure value with a binder
390 --
391 | DoNotationLet [Declaration]
392 -- |
393 -- A do notation element with source position information
394 --
395 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
397-- |
398-- Data type for binders
399--
400data Binder
401 -- |
402 -- Wildcard binder
403 --
404 = NullBinder
405 -- |
406 -- A binder which matches a boolean literal
407 --
408 | BooleanBinder Bool
409 -- |
410 -- A binder which matches a string literal
411 --
412 | StringBinder String
413 -- |
414 -- A binder which matches a numeric literal
415 --
416 | NumberBinder (Either Integer Double)
417 -- |
418 -- A binder which binds an identifier
419 --
420 | VarBinder Ident
421 -- |
422 -- A binder which matches a data constructor
423 --
424 | ConstructorBinder (Qualified ProperName) [Binder]
425 -- |
426 -- A binder which matches a record and binds its properties
427 --
428 | ObjectBinder [(String, Binder)]
429 -- |
430 -- A binder which matches an array and binds its elements
431 --
432 | ArrayBinder [Binder]
433 -- |
434 -- A binder which matches an array and binds its head and tail
435 --
436 | ConsBinder Binder Binder
437 -- |
438 -- A binder which binds its input to an identifier
439 --
440 | NamedBinder Ident Binder
441 -- |
442 -- A binder with source position information
443 --
444 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
446-- |
447-- Collect all names introduced in binders in an expression
448--
449binderNames :: Binder -> [Ident]
450binderNames = go []
451 where
452 go ns (VarBinder name) = name : ns
453 go ns (ConstructorBinder _ bs) = foldl go ns bs
454 go ns (ObjectBinder bs) = foldl go ns (map snd bs)
455 go ns (ArrayBinder bs) = foldl go ns bs
456 go ns (ConsBinder b1 b2) = go (go ns b1) b2
457 go ns (NamedBinder name b) = go (name : ns) b
458 go ns (PositionedBinder _ b) = go ns b
459 go ns _ = ns
461--
462-- Traversals
463--
465everywhereOnValues :: (Declaration -> Declaration) ->
466 (Value -> Value) ->
467 (Binder -> Binder) ->
468 (Declaration -> Declaration, Value -> Value, Binder -> Binder)
469everywhereOnValues f g h = (f', g', h')
470 where
471 f' :: Declaration -> Declaration
472 f' (DataBindingGroupDeclaration ds) = f (DataBindingGroupDeclaration (map f' ds))
473 f' (ValueDeclaration name nameKind bs grd val) = f (ValueDeclaration name nameKind (map h' bs) (fmap g' grd) (g' val))
474 f' (BindingGroupDeclaration ds) = f (BindingGroupDeclaration (map (\(name, nameKind, val) -> (name, nameKind, g' val)) ds))
475 f' (TypeClassDeclaration name args implies ds) = f (TypeClassDeclaration name args implies (map f' ds))
476 f' (TypeInstanceDeclaration name cs className args ds) = f (TypeInstanceDeclaration name cs className args (map f' ds))
477 f' (PositionedDeclaration pos d) = f (PositionedDeclaration pos (f' d))
478 f' other = f other
480 g' :: Value -> Value
481 g' (UnaryMinus v) = g (UnaryMinus (g' v))
482 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens op (g' v1) (g' v2))
483 g' (Parens v) = g (Parens (g' v))
484 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
485 g' (ObjectLiteral vs) = g (ObjectLiteral (map (fmap g') vs))
486 g' (Accessor prop v) = g (Accessor prop (g' v))
487 g' (ObjectUpdate obj vs) = g (ObjectUpdate (g' obj) (map (fmap g') vs))
488 g' (Abs name v) = g (Abs name (g' v))
489 g' (App v1 v2) = g (App (g' v1) (g' v2))
490 g' (IfThenElse v1 v2 v3) = g (IfThenElse (g' v1) (g' v2) (g' v3))
491 g' (Case vs alts) = g (Case (map g' vs) (map handleCaseAlternative alts))
492 g' (TypedValue check v ty) = g (TypedValue check (g' v) ty)
493 g' (Let ds v) = g (Let (map f' ds) (g' v))
494 g' (Do es) = g (Do (map handleDoNotationElement es))
495 g' (PositionedValue pos v) = g (PositionedValue pos (g' v))
496 g' other = g other
498 h' :: Binder -> Binder
499 h' (ConstructorBinder ctor bs) = h (ConstructorBinder ctor (map h' bs))
500 h' (ObjectBinder bs) = h (ObjectBinder (map (fmap h') bs))
501 h' (ArrayBinder bs) = h (ArrayBinder (map h' bs))
502 h' (ConsBinder b1 b2) = h (ConsBinder (h' b1) (h' b2))
503 h' (NamedBinder name b) = h (NamedBinder name (h' b))
504 h' (PositionedBinder pos b) = h (PositionedBinder pos (h' b))
505 h' other = h other
507 handleCaseAlternative :: CaseAlternative -> CaseAlternative
508 handleCaseAlternative ca =
509 ca { caseAlternativeBinders = map h' (caseAlternativeBinders ca)
510 , caseAlternativeGuard = fmap g' (caseAlternativeGuard ca)
511 , caseAlternativeResult = g' (caseAlternativeResult ca)
512 }
514 handleDoNotationElement :: DoNotationElement -> DoNotationElement
515 handleDoNotationElement (DoNotationValue v) = DoNotationValue (g' v)
516 handleDoNotationElement (DoNotationBind b v) = DoNotationBind (h' b) (g' v)
517 handleDoNotationElement (DoNotationLet ds) = DoNotationLet (map f' ds)
518 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos (handleDoNotationElement e)
521everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
522 (Declaration -> m Declaration) ->
523 (Value -> m Value) ->
524 (Binder -> m Binder) ->
525 (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
526everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
527 where
528 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
529 f' (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> maybeM (g' <=< g) grd <*> (g val >>= g')
530 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
531 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
532 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
533 f' (PositionedDeclaration pos d) = PositionedDeclaration pos <$> (f d >>= f')
534 f' other = f other
536 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
537 g' (BinaryNoParens op v1 v2) = BinaryNoParens op <$> (g v1 >>= g') <*> (g v2 >>= g')
538 g' (Parens v) = Parens <$> (g v >>= g')
539 g' (ArrayLiteral vs) = ArrayLiteral <$> mapM (g' <=< g) vs
540 g' (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g' <=< g)) vs
541 g' (Accessor prop v) = Accessor prop <$> (g v >>= g')
542 g' (ObjectUpdate obj vs) = ObjectUpdate <$> (g obj >>= g') <*> mapM (sndM (g' <=< g)) vs
543 g' (Abs name v) = Abs name <$> (g v >>= g')
544 g' (App v1 v2) = App <$> (g v1 >>= g') <*> (g v2 >>= g')
545 g' (IfThenElse v1 v2 v3) = IfThenElse <$> (g v1 >>= g') <*> (g v2 >>= g') <*> (g v3 >>= g')
546 g' (Case vs alts) = Case <$> mapM (g' <=< g) vs <*> mapM handleCaseAlternative alts
547 g' (TypedValue check v ty) = TypedValue check <$> (g v >>= g') <*> pure ty
548 g' (Let ds v) = Let <$> mapM (f' <=< f) ds <*> (g v >>= g')
549 g' (Do es) = Do <$> mapM handleDoNotationElement es
550 g' (PositionedValue pos v) = PositionedValue pos <$> (g v >>= g')
551 g' other = g other
553 h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h' <=< h) bs
554 h' (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h' <=< h)) bs
555 h' (ArrayBinder bs) = ArrayBinder <$> mapM (h' <=< h) bs
556 h' (ConsBinder b1 b2) = ConsBinder <$> (h b1 >>= h') <*> (h b2 >>= h')
557 h' (NamedBinder name b) = NamedBinder name <$> (h b >>= h')
558 h' (PositionedBinder pos b) = PositionedBinder pos <$> (h b >>= h')
559 h' other = h other
561 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h' <=< h) bs
562 <*> maybeM (g' <=< g) grd
563 <*> (g' <=< g) val
565 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
566 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
567 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM (f' <=< f) ds
568 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
570everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
571 (Declaration -> m Declaration) ->
572 (Value -> m Value) ->
573 (Binder -> m Binder) ->
574 (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
575everywhereOnValuesM f g h = (f' <=< f, g' <=< g, h' <=< h)
576 where
577 f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
578 f' (ValueDeclaration name nameKind bs grd val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> maybeM g' grd <*> g' val) >>= f
579 f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
580 f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
581 f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> mapM f' ds) >>= f
582 f' (PositionedDeclaration pos d) = (PositionedDeclaration pos <$> f' d) >>= f
583 f' other = f other
585 g' (UnaryMinus v) = (UnaryMinus <$> g' v) >>= g
586 g' (BinaryNoParens op v1 v2) = (BinaryNoParens op <$> (g' v1) <*> (g' v2)) >>= g
587 g' (Parens v) = (Parens <$> g' v) >>= g
588 g' (ArrayLiteral vs) = (ArrayLiteral <$> mapM g' vs) >>= g
589 g' (ObjectLiteral vs) = (ObjectLiteral <$> mapM (sndM g') vs) >>= g
590 g' (Accessor prop v) = (Accessor prop <$> g' v) >>= g
591 g' (ObjectUpdate obj vs) = (ObjectUpdate <$> g' obj <*> mapM (sndM g') vs) >>= g
592 g' (Abs name v) = (Abs name <$> g' v) >>= g
593 g' (App v1 v2) = (App <$> g' v1 <*> g' v2) >>= g
594 g' (IfThenElse v1 v2 v3) = (IfThenElse <$> g' v1 <*> g' v2 <*> g' v3) >>= g
595 g' (Case vs alts) = (Case <$> mapM g' vs <*> mapM handleCaseAlternative alts) >>= g
596 g' (TypedValue check v ty) = (TypedValue check <$> g' v <*> pure ty) >>= g
597 g' (Let ds v) = (Let <$> mapM f' ds <*> g' v) >>= g
598 g' (Do es) = (Do <$> mapM handleDoNotationElement es) >>= g
599 g' (PositionedValue pos v) = (PositionedValue pos <$> g' v) >>= g
600 g' other = g other
602 h' (ConstructorBinder ctor bs) = (ConstructorBinder ctor <$> mapM h' bs) >>= h
603 h' (ObjectBinder bs) = (ObjectBinder <$> mapM (sndM h') bs) >>= h
604 h' (ArrayBinder bs) = (ArrayBinder <$> mapM h' bs) >>= h
605 h' (ConsBinder b1 b2) = (ConsBinder <$> h' b1 <*> h' b2) >>= h
606 h' (NamedBinder name b) = (NamedBinder name <$> h' b) >>= h
607 h' (PositionedBinder pos b) = (PositionedBinder pos <$> h' b) >>= h
608 h' other = h other
610 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM h' bs
611 <*> maybeM g' grd
612 <*> g' val
614 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
615 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
616 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM f' ds
617 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
619everythingOnValues :: (r -> r -> r) ->
620 (Declaration -> r) ->
621 (Value -> r) ->
622 (Binder -> r) ->
623 (CaseAlternative -> r) ->
624 (DoNotationElement -> r) ->
625 (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
626everythingOnValues (<>) f g h i j = (f', g', h', i', j')
627 where
628 f' d@(DataBindingGroupDeclaration ds) = foldl (<>) (f d) (map f' ds)
629 f' d@(ValueDeclaration _ _ bs Nothing val) = foldl (<>) (f d) (map h' bs) <> g' val
630 f' d@(ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) (f d) (map h' bs) <> g' grd <> g' val
631 f' d@(BindingGroupDeclaration ds) = foldl (<>) (f d) (map (\(_, _, val) -> g' val) ds)
632 f' d@(TypeClassDeclaration _ _ _ ds) = foldl (<>) (f d) (map f' ds)
633 f' d@(TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) (f d) (map f' ds)
634 f' d@(PositionedDeclaration _ d1) = f d <> f' d1
635 f' d = f d
637 g' v@(UnaryMinus v1) = g v <> g' v1
638 g' v@(BinaryNoParens _ v1 v2) = g v <> g' v1 <> g' v2
639 g' v@(Parens v1) = g v <> g' v1
640 g' v@(ArrayLiteral vs) = foldl (<>) (g v) (map g' vs)
641 g' v@(ObjectLiteral vs) = foldl (<>) (g v) (map (g' . snd) vs)
642 g' v@(Accessor _ v1) = g v <> g' v1
643 g' v@(ObjectUpdate obj vs) = foldl (<>) (g v <> g' obj) (map (g' . snd) vs)
644 g' v@(Abs _ v1) = g v <> g' v1
645 g' v@(App v1 v2) = g v <> g' v1 <> g' v2
646 g' v@(IfThenElse v1 v2 v3) = g v <> g' v1 <> g' v2 <> g' v3
647 g' v@(Case vs alts) = foldl (<>) (foldl (<>) (g v) (map g' vs)) (map i' alts)
648 g' v@(TypedValue _ v1 _) = g v <> g' v1
649 g' v@(Let ds v1) = (foldl (<>) (g v) (map f' ds)) <> g' v1
650 g' v@(Do es) = foldl (<>) (g v) (map j' es)
651 g' v@(PositionedValue _ v1) = g v <> g' v1
652 g' v = g v
654 h' b@(ConstructorBinder _ bs) = foldl (<>) (h b) (map h' bs)
655 h' b@(ObjectBinder bs) = foldl (<>) (h b) (map (h' . snd) bs)
656 h' b@(ArrayBinder bs) = foldl (<>) (h b) (map h' bs)
657 h' b@(ConsBinder b1 b2) = h b <> h' b1 <> h' b2
658 h' b@(NamedBinder _ b1) = h b <> h' b1
659 h' b@(PositionedBinder _ b1) = h b <> h' b1
660 h' b = h b
662 i' ca = case caseAlternativeGuard ca of
663 Nothing -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' (caseAlternativeResult ca)
664 Just grd -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' grd <> g' (caseAlternativeResult ca)
666 j' e@(DoNotationValue v) = j e <> g' v
667 j' e@(DoNotationBind b v) = j e <> h' b <> g' v
668 j' e@(DoNotationLet ds) = foldl (<>) (j e) (map f' ds)
669 j' e@(PositionedDoNotationElement _ e1) = j e <> j' e1
671everythingWithContextOnValues ::
672 s ->
673 r ->
674 (r -> r -> r) ->
675 (s -> Declaration -> (s, r)) ->
676 (s -> Value -> (s, r)) ->
677 (s -> Binder -> (s, r)) ->
678 (s -> CaseAlternative -> (s, r)) ->
679 (s -> DoNotationElement -> (s, r)) ->
680 ( Declaration -> r
681 , Value -> r
682 , Binder -> r
683 , CaseAlternative -> r
684 , DoNotationElement -> r)
685everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
686 where
687 f'' s d = let (s', r) = f s d in r <> f' s' d
689 f' s (DataBindingGroupDeclaration ds) = foldl (<>) r0 (map (f'' s) ds)
690 f' s (ValueDeclaration _ _ bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
691 f' s (ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
692 f' s (BindingGroupDeclaration ds) = foldl (<>) r0 (map (\(_, _, val) -> (g'' s) val) ds)
693 f' s (TypeClassDeclaration _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
694 f' s (TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) r0 (map (f'' s) ds)
695 f' s (PositionedDeclaration _ d1) = (f'' s) d1
696 f' _ _ = r0
698 g'' s v = let (s', r) = g s v in r <> g' s' v
700 g' s (UnaryMinus v1) = (g'' s) v1
701 g' s (BinaryNoParens _ v1 v2) = (g'' s) v1 <> (g'' s) v2
702 g' s (Parens v1) = (g'' s) v1
703 g' s (ArrayLiteral vs) = foldl (<>) r0 (map (g'' s) vs)
704 g' s (ObjectLiteral vs) = foldl (<>) r0 (map (g'' s . snd) vs)
705 g' s (Accessor _ v1) = (g'' s) v1
706 g' s (ObjectUpdate obj vs) = foldl (<>) ((g'' s) obj) (map (g'' s . snd) vs)
707 g' s (Abs _ v1) = (g'' s) v1
708 g' s (App v1 v2) = (g'' s) v1 <> (g'' s) v2
709 g' s (IfThenElse v1 v2 v3) = (g'' s) v1 <> (g'' s) v2 <> (g'' s) v3
710 g' s (Case vs alts) = foldl (<>) (foldl (<>) r0 (map (g'' s) vs)) (map (i'' s) alts)
711 g' s (TypedValue _ v1 _) = (g'' s) v1
712 g' s (Let ds v1) = (foldl (<>) r0 (map (f'' s) ds)) <> (g'' s) v1
713 g' s (Do es) = foldl (<>) r0 (map (j'' s) es)
714 g' s (PositionedValue _ v1) = (g'' s) v1
715 g' _ _ = r0
717 h'' s b = let (s', r) = h s b in r <> h' s' b
719 h' s (ConstructorBinder _ bs) = foldl (<>) r0 (map (h'' s) bs)
720 h' s (ObjectBinder bs) = foldl (<>) r0 (map (h'' s . snd) bs)
721 h' s (ArrayBinder bs) = foldl (<>) r0 (map (h'' s) bs)
722 h' s (ConsBinder b1 b2) = (h'' s) b1 <> (h'' s) b2
723 h' s (NamedBinder _ b1) = (h'' s) b1
724 h' s (PositionedBinder _ b1) = (h'' s) b1
725 h' _ _ = r0
727 i'' s ca = let (s', r) = i s ca in r <> i' s' ca
729 i' s (CaseAlternative bs Nothing val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) val
730 i' s (CaseAlternative bs (Just grd) val) = foldl (<>) r0 (map (h'' s) bs) <> (g'' s) grd <> (g'' s) val
732 j'' s e = let (s', r) = j s e in r <> j' s' e
734 j' s (DoNotationValue v) = (g'' s) v
735 j' s (DoNotationBind b v) = (h'' s) b <> (g'' s) v
736 j' s (DoNotationLet ds) = foldl (<>) r0 (map (f'' s) ds)
737 j' s (PositionedDoNotationElement _ e1) = (j'' s) e1
739everywhereWithContextOnValuesM :: (Functor m, Applicative m, Monad m) =>
740 s ->
741 (s -> Declaration -> m (s, Declaration)) ->
742 (s -> Value -> m (s, Value)) ->
743 (s -> Binder -> m (s, Binder)) ->
744 (s -> CaseAlternative -> m (s, CaseAlternative)) ->
745 (s -> DoNotationElement -> m (s, DoNotationElement)) ->
746 ( Declaration -> m Declaration
747 , Value -> m Value
748 , Binder -> m Binder
749 , CaseAlternative -> m CaseAlternative
750 , DoNotationElement -> m DoNotationElement)
751everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
752 where
753 f'' s = uncurry f' <=< f s
755 f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f'' s) ds
756 f' s (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
757 f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (thirdM (g'' s)) ds
758 f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f'' s) ds
759 f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f'' s) ds
760 f' s (PositionedDeclaration pos d1) = PositionedDeclaration pos <$> f'' s d1
761 f' _ other = return other
763 g'' s = uncurry g' <=< g s
765 g' s (UnaryMinus v) = UnaryMinus <$> g'' s v
766 g' s (BinaryNoParens op v1 v2) = BinaryNoParens op <$> g'' s v1 <*> g'' s v2
767 g' s (Parens v) = Parens <$> g'' s v
768 g' s (ArrayLiteral vs) = ArrayLiteral <$> mapM (g'' s) vs
769 g' s (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g'' s)) vs
770 g' s (Accessor prop v) = Accessor prop <$> g'' s v
771 g' s (ObjectUpdate obj vs) = ObjectUpdate <$> g'' s obj <*> mapM (sndM (g'' s)) vs
772 g' s (Abs name v) = Abs name <$> g'' s v
773 g' s (App v1 v2) = App <$> g'' s v1 <*> g'' s v2
774 g' s (IfThenElse v1 v2 v3) = IfThenElse <$> g'' s v1 <*> g'' s v2 <*> g'' s v3
775 g' s (Case vs alts) = Case <$> mapM (g'' s) vs <*> mapM (i'' s) alts
776 g' s (TypedValue check v ty) = TypedValue check <$> g'' s v <*> pure ty
777 g' s (Let ds v) = Let <$> mapM (f'' s) ds <*> g'' s v
778 g' s (Do es) = Do <$> mapM (j'' s) es
779 g' s (PositionedValue pos v) = PositionedValue pos <$> g'' s v
780 g' _ other = return other
782 h'' s = uncurry h' <=< h s
784 h' s (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h'' s) bs
785 h' s (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h'' s)) bs
786 h' s (ArrayBinder bs) = ArrayBinder <$> mapM (h'' s) bs
787 h' s (ConsBinder b1 b2) = ConsBinder <$> h'' s b1 <*> h'' s b2
788 h' s (NamedBinder name b) = NamedBinder name <$> h'' s b
789 h' s (PositionedBinder pos b) = PositionedBinder pos <$> h'' s b
790 h' _ other = return other
792 i'' s = uncurry i' <=< i s
794 i' s (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h'' s) bs <*> maybeM (g'' s) grd <*> g'' s val
796 j'' s = uncurry j' <=< j s
798 j' s (DoNotationValue v) = DoNotationValue <$> g'' s v
799 j' s (DoNotationBind b v) = DoNotationBind <$> h'' s b <*> g'' s v
800 j' s (DoNotationLet ds) = DoNotationLet <$> mapM (f'' s) ds
801 j' s (PositionedDoNotationElement pos e1) = PositionedDoNotationElement pos <$> j'' s e1
803accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
804accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)
805 where
806 forDecls (DataDeclaration _ _ dctors) = mconcat (concatMap (map f . snd) dctors)
807 forDecls (ExternDeclaration _ _ _ ty) = f ty
808 forDecls (ExternInstanceDeclaration _ cs _ tys) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
809 forDecls (TypeClassDeclaration _ _ implies _) = mconcat (concatMap (map f . snd) implies)
810 forDecls (TypeInstanceDeclaration _ cs _ tys _) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
811 forDecls (TypeSynonymDeclaration _ _ ty) = f ty
812 forDecls (TypeDeclaration _ ty) = f ty
813 forDecls _ = mempty
815 forValues (TypeClassDictionary _ (_, cs) _) = mconcat (map f cs)
816 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
817 forValues (TypedValue _ _ ty) = f ty
818 forValues _ = mempty