1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Declarations
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
11-- | Data types for modules and declarations
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
17module Language.PureScript.Declarations where
19import Data.Monoid (Monoid(..), mconcat)
21import qualified Data.Data as D
23import Control.Applicative
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
35-- A precedence level for an infix operator
37type Precedence = Integer
40-- Associativity for infix operators
42data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
44instance Show Associativity where
45 show Infixl = "infixl"
46 show Infixr = "infixr"
50-- Source position information
52data SourcePos = SourcePos
60 , sourcePosLine :: Int
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)
71-- Fixity data for infix operators
73data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
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.
79data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
82-- An item in a list of explicit imports or exports
86 -- A type constructor with data constructors
88 = TypeRef ProperName (Maybe [ProperName])
96 | TypeClassRef ProperName
98 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
100 | TypeInstanceRef Ident
102 -- A declaration reference with source position information
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'
117-- The data type of declarations
121 -- A data type declaration (data or newtype, name, arguments, data constructors)
123 = DataDeclaration DataDeclType ProperName [String] [(ProperName, [Type])]
125 -- A minimal mutually recursive set of data type declarations
127 | DataBindingGroupDeclaration [Declaration]
129 -- A type synonym declaration (name, arguments, type)
131 | TypeSynonymDeclaration ProperName [String] Type
133 -- A type declaration for a value (name, ty)
135 | TypeDeclaration Ident Type
137 -- A value declaration (name, top-level binders, optional guard, value)
139 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
141 -- A minimal mutually recursive set of value declarations
143 | BindingGroupDeclaration [(Ident, NameKind, Value)]
145 -- A foreign import declaration (type, name, optional inline Javascript, type)
147 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
149 -- A data type foreign import (name, kind)
151 | ExternDataDeclaration ProperName Kind
153 -- A type class instance foreign import
155 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
157 -- A fixity declaration (fixity data, operator name)
159 | FixityDeclaration Fixity String
161 -- A module import (module name, optional set of identifiers to import,
162 -- optional set of identifiers to hide, optional "qualified as" name)
164 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe [DeclarationRef]) (Maybe ModuleName)
166 -- A type class declaration (name, argument, implies, member declarations)
168 | TypeClassDeclaration ProperName [String] [(Qualified ProperName, [Type])] [Declaration]
170 -- A type instance declaration (name, dependencies, class name, instance types, member
173 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
175 -- A declaration with source position information
177 | PositionedDeclaration SourcePos Declaration
178 deriving (Show, D.Data, D.Typeable)
181-- Test if a declaration is a value declaration
183isValueDecl :: Declaration -> Bool
184isValueDecl ValueDeclaration{} = True
185isValueDecl (PositionedDeclaration _ d) = isValueDecl d
189-- Test if a declaration is a data type or type synonym declaration
191isDataDecl :: Declaration -> Bool
192isDataDecl DataDeclaration{} = True
193isDataDecl TypeSynonymDeclaration{} = True
194isDataDecl (PositionedDeclaration _ d) = isDataDecl d
198-- Test if a declaration is a module import
200isImportDecl :: Declaration -> Bool
201isImportDecl ImportDeclaration{} = True
202isImportDecl (PositionedDeclaration _ d) = isImportDecl d
203isImportDecl _ = False
206-- Test if a declaration is a data type foreign import
208isExternDataDecl :: Declaration -> Bool
209isExternDataDecl ExternDataDeclaration{} = True
210isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
211isExternDataDecl _ = False
214-- Test if a declaration is a type class instance foreign import
216isExternInstanceDecl :: Declaration -> Bool
217isExternInstanceDecl ExternInstanceDeclaration{} = True
218isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
219isExternInstanceDecl _ = False
222-- Test if a declaration is a fixity declaration
224isFixityDecl :: Declaration -> Bool
225isFixityDecl FixityDeclaration{} = True
226isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
227isFixityDecl _ = False
230-- Test if a declaration is a foreign import
232isExternDecl :: Declaration -> Bool
233isExternDecl ExternDeclaration{} = True
234isExternDecl (PositionedDeclaration _ d) = isExternDecl d
235isExternDecl _ = False
238-- Test if a declaration is a type class or instance declaration
240isTypeClassDeclaration :: Declaration -> Bool
241isTypeClassDeclaration TypeClassDeclaration{} = True
242isTypeClassDeclaration TypeInstanceDeclaration{} = True
243isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
244isTypeClassDeclaration _ = False
247-- A guard is just a boolean-valued expression that appears alongside a set of binders
252-- Data type for values
258 = NumericLiteral (Either Integer Double)
262 | StringLiteral String
266 | BooleanLiteral Bool
268 -- A prefix -, will be desugared
272 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
275 | BinaryNoParens (Qualified Ident) Value Value
277 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
284 | ArrayLiteral [Value]
288 | ObjectLiteral [(String, Value)]
290 -- An record property accessor expression
292 | Accessor String Value
294 -- Partial record update
296 | ObjectUpdate Value [(String, Value)]
298 -- Function introduction
300 | Abs (Either Ident Binder) Value
302 -- Function application
308 | Var (Qualified Ident)
310 -- Conditional (if-then-else expression)
312 | IfThenElse Value Value Value
314 -- A data constructor
316 | Constructor (Qualified ProperName)
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.
321 | Case [Value] [CaseAlternative]
323 -- A value with a type annotation
325 | TypedValue Bool Value Type
329 | Let [Declaration] Value
331 -- A do-notation block
333 | Do [DoNotationElement]
335 -- An application of a typeclass dictionary constructor. The value should be
338 | TypeClassDictionaryConstructorApp (Qualified ProperName) Value
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.
346 | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
348 -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
350 | SuperClassDictionary (Qualified ProperName) [Type]
352 -- A value with source position information
354 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
357-- An alternative in a case statement
359data CaseAlternative = CaseAlternative
361 -- A collection of binders with which to match the inputs
363 caseAlternativeBinders :: [Binder]
367 , caseAlternativeGuard :: Maybe Guard
369 -- The result expression
371 , caseAlternativeResult :: Value
372 } deriving (Show, D.Data, D.Typeable)
375-- Find the original dictionary which a type class dictionary in scope refers to
377canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
378canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
379canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
382-- A statement in a do-notation block
384data DoNotationElement
386 -- A monadic value without a binder
388 = DoNotationValue Value
390 -- A monadic value with a binder
392 | DoNotationBind Binder Value
394 -- A let statement, i.e. a pure value with a binder
396 | DoNotationLet [Declaration]
398 -- A do notation element with source position information
400 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
403-- Data type for binders
411 -- A binder which matches a boolean literal
415 -- A binder which matches a string literal
417 | StringBinder String
419 -- A binder which matches a numeric literal
421 | NumberBinder (Either Integer Double)
423 -- A binder which binds an identifier
427 -- A binder which matches a data constructor
429 | ConstructorBinder (Qualified ProperName) [Binder]
431 -- A binder which matches a record and binds its properties
433 | ObjectBinder [(String, Binder)]
435 -- A binder which matches an array and binds its elements
437 | ArrayBinder [Binder]
439 -- A binder which matches an array and binds its head and tail
441 | ConsBinder Binder Binder
443 -- A binder which binds its input to an identifier
445 | NamedBinder Ident Binder
447 -- A binder with source position information
449 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
452-- Collect all names introduced in binders in an expression
454binderNames :: Binder -> [Ident]
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
470everywhereOnValues :: (Declaration -> Declaration) ->
472 (Binder -> Binder) ->
473 (Declaration -> Declaration, Value -> Value, Binder -> Binder)
474everywhereOnValues f g h = (f', g', h')
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))
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))
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))
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)
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)
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')
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')
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')
568 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h' <=< h) bs
569 <*> maybeM (g' <=< g) grd
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)
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
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
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
618 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM h' bs
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) ->
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')
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
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
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
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 ::
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)) ->
692 , CaseAlternative -> r
693 , DoNotationElement -> r)
694everythingWithContextOnValues s0 r0 (<>) f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
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
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
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
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) =>
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
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)
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)
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
826 forValues (TypeClassDictionary _ (_, cs) _) = mconcat (map f cs)
827 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
828 forValues (TypedValue _ _ ty) = f ty
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list