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 #-}
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 (name, arguments, data constructors)
123 = DataDeclaration 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, optional "qualified as"
164 | ImportDeclaration ModuleName (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 -- 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.
341 | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
343 -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
345 | SuperClassDictionary (Qualified ProperName) [Type]
347 -- A value with source position information
349 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
352-- An alternative in a case statement
354data CaseAlternative = CaseAlternative
356 -- A collection of binders with which to match the inputs
358 caseAlternativeBinders :: [Binder]
362 , caseAlternativeGuard :: Maybe Guard
364 -- The result expression
366 , caseAlternativeResult :: Value
367 } deriving (Show, D.Data, D.Typeable)
370-- Find the original dictionary which a type class dictionary in scope refers to
372canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
373canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
374canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
377-- A statement in a do-notation block
379data DoNotationElement
381 -- A monadic value without a binder
383 = DoNotationValue Value
385 -- A monadic value with a binder
387 | DoNotationBind Binder Value
389 -- A let statement, i.e. a pure value with a binder
391 | DoNotationLet [Declaration]
393 -- A do notation element with source position information
395 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
398-- Data type for binders
406 -- A binder which matches a boolean literal
410 -- A binder which matches a string literal
412 | StringBinder String
414 -- A binder which matches a numeric literal
416 | NumberBinder (Either Integer Double)
418 -- A binder which binds an identifier
422 -- A binder which matches a data constructor
424 | ConstructorBinder (Qualified ProperName) [Binder]
426 -- A binder which matches a record and binds its properties
428 | ObjectBinder [(String, Binder)]
430 -- A binder which matches an array and binds its elements
432 | ArrayBinder [Binder]
434 -- A binder which matches an array and binds its head and tail
436 | ConsBinder Binder Binder
438 -- A binder which binds its input to an identifier
440 | NamedBinder Ident Binder
442 -- A binder with source position information
444 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
447-- Collect all names introduced in binders in an expression
449binderNames :: Binder -> [Ident]
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
465everywhereOnValues :: (Declaration -> Declaration) ->
467 (Binder -> Binder) ->
468 (Declaration -> Declaration, Value -> Value, Binder -> Binder)
469everywhereOnValues f g h = (f', g', h')
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))
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))
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))
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)
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)
520everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
521 (Declaration -> m Declaration) ->
522 (Value -> m Value) ->
523 (Binder -> m Binder) ->
524 (Declaration -> m Declaration, Value -> m Value, Binder -> m Binder)
525everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
527 f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
528 f' (ValueDeclaration name nameKind bs grd val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> maybeM (g' <=< g) grd <*> (g val >>= g')
529 f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
530 f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
531 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> mapM (f' <=< f) ds
532 f' (PositionedDeclaration pos d) = PositionedDeclaration pos <$> (f d >>= f')
535 g' (UnaryMinus v) = UnaryMinus <$> (g v >>= g')
536 g' (BinaryNoParens op v1 v2) = BinaryNoParens op <$> (g v1 >>= g') <*> (g v2 >>= g')
537 g' (Parens v) = Parens <$> (g v >>= g')
538 g' (ArrayLiteral vs) = ArrayLiteral <$> mapM (g' <=< g) vs
539 g' (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g' <=< g)) vs
540 g' (Accessor prop v) = Accessor prop <$> (g v >>= g')
541 g' (ObjectUpdate obj vs) = ObjectUpdate <$> (g obj >>= g') <*> mapM (sndM (g' <=< g)) vs
542 g' (Abs name v) = Abs name <$> (g v >>= g')
543 g' (App v1 v2) = App <$> (g v1 >>= g') <*> (g v2 >>= g')
544 g' (IfThenElse v1 v2 v3) = IfThenElse <$> (g v1 >>= g') <*> (g v2 >>= g') <*> (g v3 >>= g')
545 g' (Case vs alts) = Case <$> mapM (g' <=< g) vs <*> mapM handleCaseAlternative alts
546 g' (TypedValue check v ty) = TypedValue check <$> (g v >>= g') <*> pure ty
547 g' (Let ds v) = Let <$> mapM (f' <=< f) ds <*> (g v >>= g')
548 g' (Do es) = Do <$> mapM handleDoNotationElement es
549 g' (PositionedValue pos v) = PositionedValue pos <$> (g v >>= g')
552 h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h' <=< h) bs
553 h' (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h' <=< h)) bs
554 h' (ArrayBinder bs) = ArrayBinder <$> mapM (h' <=< h) bs
555 h' (ConsBinder b1 b2) = ConsBinder <$> (h b1 >>= h') <*> (h b2 >>= h')
556 h' (NamedBinder name b) = NamedBinder name <$> (h b >>= h')
557 h' (PositionedBinder pos b) = PositionedBinder pos <$> (h b >>= h')
560 handleCaseAlternative (CaseAlternative bs grd val) = CaseAlternative <$> mapM (h' <=< h) bs
561 <*> maybeM (g' <=< g) grd
564 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
565 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
566 handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM (f' <=< f) ds
567 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos <$> handleDoNotationElement e
569everythingOnValues :: (r -> r -> r) ->
570 (Declaration -> r) ->
573 (CaseAlternative -> r) ->
574 (DoNotationElement -> r) ->
575 (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
576everythingOnValues (<>) f g h i j = (f', g', h', i', j')
578 f' d@(DataBindingGroupDeclaration ds) = foldl (<>) (f d) (map f' ds)
579 f' d@(ValueDeclaration _ _ bs Nothing val) = foldl (<>) (f d) (map h' bs) <> g' val
580 f' d@(ValueDeclaration _ _ bs (Just grd) val) = foldl (<>) (f d) (map h' bs) <> g' grd <> g' val
581 f' d@(BindingGroupDeclaration ds) = foldl (<>) (f d) (map (\(_, _, val) -> g' val) ds)
582 f' d@(TypeClassDeclaration _ _ _ ds) = foldl (<>) (f d) (map f' ds)
583 f' d@(TypeInstanceDeclaration _ _ _ _ ds) = foldl (<>) (f d) (map f' ds)
584 f' d@(PositionedDeclaration _ d1) = f d <> f' d1
587 g' v@(UnaryMinus v1) = g v <> g' v1
588 g' v@(BinaryNoParens _ v1 v2) = g v <> g' v1 <> g' v2
589 g' v@(Parens v1) = g v <> g' v1
590 g' v@(ArrayLiteral vs) = foldl (<>) (g v) (map g' vs)
591 g' v@(ObjectLiteral vs) = foldl (<>) (g v) (map (g' . snd) vs)
592 g' v@(Accessor _ v1) = g v <> g' v1
593 g' v@(ObjectUpdate obj vs) = foldl (<>) (g v <> g' obj) (map (g' . snd) vs)
594 g' v@(Abs _ v1) = g v <> g' v1
595 g' v@(App v1 v2) = g v <> g' v1 <> g' v2
596 g' v@(IfThenElse v1 v2 v3) = g v <> g' v1 <> g' v2 <> g' v3
597 g' v@(Case vs alts) = foldl (<>) (foldl (<>) (g v) (map g' vs)) (map i' alts)
598 g' v@(TypedValue _ v1 _) = g v <> g' v1
599 g' v@(Let ds v1) = (foldl (<>) (g v) (map f' ds)) <> g' v1
600 g' v@(Do es) = foldl (<>) (g v) (map j' es)
601 g' v@(PositionedValue _ v1) = g v <> g' v1
604 h' b@(ConstructorBinder _ bs) = foldl (<>) (h b) (map h' bs)
605 h' b@(ObjectBinder bs) = foldl (<>) (h b) (map (h' . snd) bs)
606 h' b@(ArrayBinder bs) = foldl (<>) (h b) (map h' bs)
607 h' b@(ConsBinder b1 b2) = h b <> h' b1 <> h' b2
608 h' b@(NamedBinder _ b1) = h b <> h' b1
609 h' b@(PositionedBinder _ b1) = h b <> h' b1
612 i' ca = case caseAlternativeGuard ca of
613 Nothing -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' (caseAlternativeResult ca)
614 Just grd -> foldl (<>) (i ca) (map h' (caseAlternativeBinders ca)) <> g' grd <> g' (caseAlternativeResult ca)
616 j' e@(DoNotationValue v) = j e <> g' v
617 j' e@(DoNotationBind b v) = j e <> h' b <> g' v
618 j' e@(DoNotationLet ds) = foldl (<>) (j e) (map f' ds)
619 j' e@(PositionedDoNotationElement _ e1) = j e <> j' e1
621accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Value -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)
622accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (const mempty) (const mempty)
624 forDecls (DataDeclaration _ _ dctors) = mconcat (concatMap (map f . snd) dctors)
625 forDecls (ExternDeclaration _ _ _ ty) = f ty
626 forDecls (ExternInstanceDeclaration _ cs _ tys) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
627 forDecls (TypeClassDeclaration _ _ implies _) = mconcat (concatMap (map f . snd) implies)
628 forDecls (TypeInstanceDeclaration _ cs _ tys _) = mconcat (concatMap (map f . snd) cs) `mappend` mconcat (map f tys)
629 forDecls (TypeSynonymDeclaration _ _ ty) = f ty
630 forDecls (TypeDeclaration _ ty) = f ty
633 forValues (TypeClassDictionary _ (_, cs) _) = mconcat (map f cs)
634 forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
635 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