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 Language.PureScript.Types
20import Language.PureScript.Names
21import Language.PureScript.Kinds
22import Language.PureScript.TypeClassDictionaries
23import Language.PureScript.CodeGen.JS.AST
24import Language.PureScript.Environment
26import qualified Data.Data as D
29-- A precedence level for an infix operator
31type Precedence = Integer
34-- Associativity for infix operators
36data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
38instance Show Associativity where
39 show Infixl = "infixl"
40 show Infixr = "infixr"
44-- Source position information
46data SourcePos = SourcePos
54 , sourcePosLine :: Int
58 , sourcePosColumn :: Int
59 } deriving (D.Data, D.Typeable)
61instance Show SourcePos where
62 show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
65-- Fixity data for infix operators
67data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
70-- A module declaration, consisting of a module name, a list of declarations, and a list of the
71-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
73data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
76-- An item in a list of explicit imports or exports
80 -- A type constructor with data constructors
82 = TypeRef ProperName (Maybe [ProperName])
90 | TypeClassRef ProperName
92 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
94 | TypeInstanceRef Ident
96 -- A declaration reference with source position information
98 | PositionedDeclarationRef SourcePos DeclarationRef
99 deriving (Show, D.Data, D.Typeable)
101instance Eq DeclarationRef where
102 (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
103 (ValueRef name) == (ValueRef name') = name == name'
104 (TypeClassRef name) == (TypeClassRef name') = name == name'
105 (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
106 (PositionedDeclarationRef _ r) == r' = r == r'
107 r == (PositionedDeclarationRef _ r') = r == r'
111-- The data type of declarations
115 -- A data type declaration (name, arguments, data constructors)
117 = DataDeclaration ProperName [String] [(ProperName, [Type])]
119 -- A minimal mutually recursive set of data type declarations
121 | DataBindingGroupDeclaration [Declaration]
123 -- A type synonym declaration (name, arguments, type)
125 | TypeSynonymDeclaration ProperName [String] Type
127 -- A type declaration for a value (name, ty)
129 | TypeDeclaration Ident Type
131 -- A value declaration (name, top-level binders, optional guard, value)
133 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
135 -- A minimal mutually recursive set of value declarations
137 | BindingGroupDeclaration [(Ident, NameKind, Value)]
139 -- A foreign import declaration (type, name, optional inline Javascript, type)
141 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
143 -- A data type foreign import (name, kind)
145 | ExternDataDeclaration ProperName Kind
147 -- A type class instance foreign import
149 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
151 -- A fixity declaration (fixity data, operator name)
153 | FixityDeclaration Fixity String
155 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
158 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
160 -- A type class declaration (name, argument, implies, member declarations)
162 | TypeClassDeclaration ProperName [String] [(Qualified ProperName, [Type])] [Declaration]
164 -- A type instance declaration (name, dependencies, class name, instance types, member
167 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
169 -- A declaration with source position information
171 | PositionedDeclaration SourcePos Declaration
172 deriving (Show, D.Data, D.Typeable)
175-- Test if a declaration is a value declaration
177isValueDecl :: Declaration -> Bool
178isValueDecl ValueDeclaration{} = True
179isValueDecl (PositionedDeclaration _ d) = isValueDecl d
183-- Test if a declaration is a data type or type synonym declaration
185isDataDecl :: Declaration -> Bool
186isDataDecl DataDeclaration{} = True
187isDataDecl TypeSynonymDeclaration{} = True
188isDataDecl (PositionedDeclaration _ d) = isDataDecl d
192-- Test if a declaration is a module import
194isImportDecl :: Declaration -> Bool
195isImportDecl ImportDeclaration{} = True
196isImportDecl (PositionedDeclaration _ d) = isImportDecl d
197isImportDecl _ = False
200-- Test if a declaration is a data type foreign import
202isExternDataDecl :: Declaration -> Bool
203isExternDataDecl ExternDataDeclaration{} = True
204isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
205isExternDataDecl _ = False
208-- Test if a declaration is a type class instance foreign import
210isExternInstanceDecl :: Declaration -> Bool
211isExternInstanceDecl ExternInstanceDeclaration{} = True
212isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
213isExternInstanceDecl _ = False
216-- Test if a declaration is a fixity declaration
218isFixityDecl :: Declaration -> Bool
219isFixityDecl FixityDeclaration{} = True
220isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
221isFixityDecl _ = False
224-- Test if a declaration is a foreign import
226isExternDecl :: Declaration -> Bool
227isExternDecl ExternDeclaration{} = True
228isExternDecl (PositionedDeclaration _ d) = isExternDecl d
229isExternDecl _ = False
232-- Test if a declaration is a type class or instance declaration
234isTypeClassDeclaration :: Declaration -> Bool
235isTypeClassDeclaration TypeClassDeclaration{} = True
236isTypeClassDeclaration TypeInstanceDeclaration{} = True
237isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
238isTypeClassDeclaration _ = False
241-- A guard is just a boolean-valued expression that appears alongside a set of binders
246-- Data type for values
252 = NumericLiteral (Either Integer Double)
256 | StringLiteral String
260 | BooleanLiteral Bool
262 -- A prefix -, will be desugared
266 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
269 | BinaryNoParens (Qualified Ident) Value Value
271 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
278 | ArrayLiteral [Value]
282 | ObjectLiteral [(String, Value)]
284 -- An record property accessor expression
286 | Accessor String Value
288 -- Partial record update
290 | ObjectUpdate Value [(String, Value)]
292 -- Function introduction
294 | Abs (Either Ident Binder) Value
296 -- Function application
302 | Var (Qualified Ident)
304 -- Conditional (if-then-else expression)
306 | IfThenElse Value Value Value
308 -- A data constructor
310 | Constructor (Qualified ProperName)
312 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
313 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
315 | Case [Value] [CaseAlternative]
317 -- A value with a type annotation
319 | TypedValue Bool Value Type
323 | Let [Declaration] Value
325 -- A do-notation block
327 | Do [DoNotationElement]
329 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
330 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
331 -- can be evaluated at runtime. The constructor arguments represent (in order): whether or not to look
332 -- at superclass implementations when searching for a dictionary, the type class name and
333 -- instance type, and the type class dictionaries in scope.
335 | TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
337 -- A placeholder for a superclass dictionary to be turned into a TypeClassDictionary during typechecking
339 | SuperClassDictionary (Qualified ProperName) [Type]
341 -- A value with source position information
343 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
346-- An alternative in a case statement
348data CaseAlternative = CaseAlternative
350 -- A collection of binders with which to match the inputs
352 caseAlternativeBinders :: [Binder]
356 , caseAlternativeGuard :: Maybe Guard
358 -- The result expression
360 , caseAlternativeResult :: Value
361 } deriving (Show, D.Data, D.Typeable)
364-- Find the original dictionary which a type class dictionary in scope refers to
366canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
367canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
368canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
371-- A statement in a do-notation block
373data DoNotationElement
375 -- A monadic value without a binder
377 = DoNotationValue Value
379 -- A monadic value with a binder
381 | DoNotationBind Binder Value
383 -- A let statement, i.e. a pure value with a binder
385 | DoNotationLet [Declaration]
387 -- A do notation element with source position information
389 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
392-- Data type for binders
400 -- A binder which matches a boolean literal
404 -- A binder which matches a string literal
406 | StringBinder String
408 -- A binder which matches a numeric literal
410 | NumberBinder (Either Integer Double)
412 -- A binder which binds an identifier
416 -- A binder which matches a data constructor
418 | ConstructorBinder (Qualified ProperName) [Binder]
420 -- A binder which matches a record and binds its properties
422 | ObjectBinder [(String, Binder)]
424 -- A binder which matches an array and binds its elements
426 | ArrayBinder [Binder]
428 -- A binder which matches an array and binds its head and tail
430 | ConsBinder Binder Binder
432 -- A binder which binds its input to an identifier
434 | NamedBinder Ident Binder
436 -- A binder with source position information
438 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
441-- Collect all names introduced in binders in an expression
443binderNames :: Binder -> [Ident]
446 go ns (VarBinder name) = name : ns
447 go ns (ConstructorBinder _ bs) = foldl go ns bs
448 go ns (ObjectBinder bs) = foldl go ns (map snd bs)
449 go ns (ArrayBinder bs) = foldl go ns bs
450 go ns (ConsBinder b1 b2) = go (go ns b1) b2
451 go ns (NamedBinder name b) = go (name : ns) b
452 go ns (PositionedBinder _ b) = go ns b
459everywhereOnValues :: (Declaration -> Declaration) ->
461 (Binder -> Binder) ->
462 (Declaration -> Declaration, Value -> Value, Binder -> Binder)
463everywhereOnValues f g h = (f', g', h')
465 f' :: Declaration -> Declaration
466 f' (DataBindingGroupDeclaration ds) = f (DataBindingGroupDeclaration (map f' ds))
467 f' (ValueDeclaration name nameKind bs grd val) = f (ValueDeclaration name nameKind (map h' bs) (fmap g' grd) (g' val))
468 f' (BindingGroupDeclaration ds) = f (BindingGroupDeclaration (map (\(name, nameKind, val) -> (name, nameKind, g' val)) ds))
469 f' (TypeClassDeclaration name args implies ds) = f (TypeClassDeclaration name args implies (map f' ds))
470 f' (TypeInstanceDeclaration name cs className args ds) = f (TypeInstanceDeclaration name cs className args (map f' ds))
471 f' (PositionedDeclaration pos d) = f (PositionedDeclaration pos (f' d))
475 g' (UnaryMinus v) = g (UnaryMinus (g' v))
476 g' (BinaryNoParens op v1 v2) = g (BinaryNoParens op (g' v1) (g' v2))
477 g' (Parens v) = g (Parens (g' v))
478 g' (ArrayLiteral vs) = g (ArrayLiteral (map g' vs))
479 g' (ObjectLiteral vs) = g (ObjectLiteral (map (fmap g') vs))
480 g' (Accessor prop v) = g (Accessor prop (g' v))
481 g' (ObjectUpdate obj vs) = g (ObjectUpdate (g' obj) (map (fmap g') vs))
482 g' (Abs name v) = g (Abs name (g' v))
483 g' (App v1 v2) = g (App (g' v1) (g' v2))
484 g' (IfThenElse v1 v2 v3) = g (IfThenElse (g' v1) (g' v2) (g' v3))
485 g' (Case vs alts) = g (Case (map g' vs) (map handleCaseAlternative alts))
486 g' (TypedValue check v ty) = g (TypedValue check (g' v) ty)
487 g' (Let ds v) = g (Let (map f' ds) (g' v))
488 g' (Do es) = g (Do (map handleDoNotationElement es))
489 g' (PositionedValue pos v) = g (PositionedValue pos (g' v))
492 h' :: Binder -> Binder
493 h' (ConstructorBinder ctor bs) = h (ConstructorBinder ctor (map h' bs))
494 h' (ObjectBinder bs) = h (ObjectBinder (map (fmap h') bs))
495 h' (ArrayBinder bs) = h (ArrayBinder (map h' bs))
496 h' (ConsBinder b1 b2) = h (ConsBinder (h' b1) (h' b2))
497 h' (NamedBinder name b) = h (NamedBinder name (h' b))
498 h' (PositionedBinder pos b) = h (PositionedBinder pos (h' b))
501 handleCaseAlternative :: CaseAlternative -> CaseAlternative
502 handleCaseAlternative ca =
503 ca { caseAlternativeBinders = map h' (caseAlternativeBinders ca)
504 , caseAlternativeGuard = fmap g' (caseAlternativeGuard ca)
505 , caseAlternativeResult = g' (caseAlternativeResult ca)
508 handleDoNotationElement :: DoNotationElement -> DoNotationElement
509 handleDoNotationElement (DoNotationValue v) = DoNotationValue (g' v)
510 handleDoNotationElement (DoNotationBind b v) = DoNotationBind (h' b) (g' v)
511 handleDoNotationElement (DoNotationLet ds) = DoNotationLet (map f' ds)
512 handleDoNotationElement (PositionedDoNotationElement pos e) = PositionedDoNotationElement pos (handleDoNotationElement e)
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