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
27import Data.Generics (mkQ, everything)
30-- A precedence level for an infix operator
32type Precedence = Integer
35-- Associativity for infix operators
37data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
39instance Show Associativity where
40 show Infixl = "infixl"
41 show Infixr = "infixr"
45-- Source position information
47data SourcePos = SourcePos
55 , sourcePosLine :: Int
59 , sourcePosColumn :: Int
60 } deriving (D.Data, D.Typeable)
62instance Show SourcePos where
63 show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
66-- Fixity data for infix operators
68data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
71-- A module declaration, consisting of a module name, a list of declarations, and a list of the
72-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
74data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
77-- An item in a list of explicit imports or exports
81 -- A type constructor with data constructors
83 = TypeRef ProperName (Maybe [ProperName])
91 | TypeClassRef ProperName
93 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
95 | TypeInstanceRef Ident
97 -- A declaration reference with source position information
99 | PositionedDeclarationRef SourcePos DeclarationRef
100 deriving (Show, D.Data, D.Typeable)
102instance Eq DeclarationRef where
103 (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
104 (ValueRef name) == (ValueRef name') = name == name'
105 (TypeClassRef name) == (TypeClassRef name') = name == name'
106 (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
107 (PositionedDeclarationRef _ r) == r' = r == r'
108 r == (PositionedDeclarationRef _ r') = r == r'
112-- The data type of declarations
116 -- A data type declaration (name, arguments, data constructors)
118 = DataDeclaration ProperName [String] [(ProperName, [Type])]
120 -- A minimal mutually recursive set of data type declarations
122 | DataBindingGroupDeclaration [Declaration]
124 -- A type synonym declaration (name, arguments, type)
126 | TypeSynonymDeclaration ProperName [String] Type
128 -- A type declaration for a value (name, ty)
130 | TypeDeclaration Ident Type
132 -- A value declaration (name, top-level binders, optional guard, value)
134 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
136 -- A minimal mutually recursive set of value declarations
138 | BindingGroupDeclaration [(Ident, NameKind, Value)]
140 -- A foreign import declaration (type, name, optional inline Javascript, type)
142 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
144 -- A data type foreign import (name, kind)
146 | ExternDataDeclaration ProperName Kind
148 -- A type class instance foreign import
150 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
152 -- A fixity declaration (fixity data, operator name)
154 | FixityDeclaration Fixity String
156 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
159 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
161 -- A type class declaration (name, argument, member declarations)
163 | TypeClassDeclaration ProperName [String] [Declaration]
165 -- A type instance declaration (name, dependencies, class name, instance types, member
168 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
170 -- A declaration with source position information
172 | PositionedDeclaration SourcePos Declaration
173 deriving (Show, D.Data, D.Typeable)
176-- Test if a declaration is a value declaration
178isValueDecl :: Declaration -> Bool
179isValueDecl ValueDeclaration{} = True
180isValueDecl (PositionedDeclaration _ d) = isValueDecl d
184-- Test if a declaration is a data type or type synonym declaration
186isDataDecl :: Declaration -> Bool
187isDataDecl DataDeclaration{} = True
188isDataDecl TypeSynonymDeclaration{} = True
189isDataDecl (PositionedDeclaration _ d) = isDataDecl d
193-- Test if a declaration is a module import
195isImportDecl :: Declaration -> Bool
196isImportDecl ImportDeclaration{} = True
197isImportDecl (PositionedDeclaration _ d) = isImportDecl d
198isImportDecl _ = False
201-- Test if a declaration is a data type foreign import
203isExternDataDecl :: Declaration -> Bool
204isExternDataDecl ExternDataDeclaration{} = True
205isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
206isExternDataDecl _ = False
209-- Test if a declaration is a type class instance foreign import
211isExternInstanceDecl :: Declaration -> Bool
212isExternInstanceDecl ExternInstanceDeclaration{} = True
213isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
214isExternInstanceDecl _ = False
217-- Test if a declaration is a fixity declaration
219isFixityDecl :: Declaration -> Bool
220isFixityDecl FixityDeclaration{} = True
221isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
222isFixityDecl _ = False
225-- Test if a declaration is a foreign import
227isExternDecl :: Declaration -> Bool
228isExternDecl ExternDeclaration{} = True
229isExternDecl (PositionedDeclaration _ d) = isExternDecl d
230isExternDecl _ = False
233-- Test if a declaration is a type class or instance declaration
235isTypeClassDeclaration :: Declaration -> Bool
236isTypeClassDeclaration TypeClassDeclaration{} = True
237isTypeClassDeclaration TypeInstanceDeclaration{} = True
238isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
239isTypeClassDeclaration _ = False
242-- A guard is just a boolean-valued expression that appears alongside a set of binders
247-- Data type for values
253 = NumericLiteral (Either Integer Double)
257 | StringLiteral String
261 | BooleanLiteral Bool
263 -- A prefix -, will be desugared
267 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
270 | BinaryNoParens (Qualified Ident) Value Value
272 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
279 | ArrayLiteral [Value]
283 | ObjectLiteral [(String, Value)]
285 -- An record property accessor expression
287 | Accessor String Value
289 -- Partial record update
291 | ObjectUpdate Value [(String, Value)]
293 -- Function introduction
295 | Abs (Either Ident Binder) Value
297 -- Function application
303 | Var (Qualified Ident)
305 -- Conditional (if-then-else expression)
307 | IfThenElse Value Value Value
309 -- A data constructor
311 | Constructor (Qualified ProperName)
313 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
314 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
316 | Case [Value] [CaseAlternative]
318 -- A value with a type annotation
320 | TypedValue Bool Value Type
324 | Let [Declaration] Value
326 -- A do-notation block
328 | Do [DoNotationElement]
330 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
331 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
332 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
333 -- instance type, and the type class dictionaries in scope.
335 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
337 -- A value with source position information
339 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
342-- An alternative in a case statement
344data CaseAlternative = CaseAlternative
346 -- A collection of binders with which to match the inputs
348 caseAlternativeBinders :: [Binder]
352 , caseAlternativeGuard :: Maybe Guard
354 -- The result expression
356 , caseAlternativeResult :: Value
357 } deriving (Show, D.Data, D.Typeable)
360-- Find the original dictionary which a type class dictionary in scope refers to
362canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
363canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
364canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
367-- A statement in a do-notation block
369data DoNotationElement
371 -- A monadic value without a binder
373 = DoNotationValue Value
375 -- A monadic value with a binder
377 | DoNotationBind Binder Value
379 -- A let statement, i.e. a pure value with a binder
381 | DoNotationLet [Declaration]
383 -- A do notation element with source position information
385 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
388-- Data type for binders
396 -- A binder which matches a boolean literal
400 -- A binder which matches a string literal
402 | StringBinder String
404 -- A binder which matches a numeric literal
406 | NumberBinder (Either Integer Double)
408 -- A binder which binds an identifier
412 -- A binder which matches a data constructor
414 | ConstructorBinder (Qualified ProperName) [Binder]
416 -- A binder which matches a record and binds its properties
418 | ObjectBinder [(String, Binder)]
420 -- A binder which matches an array and binds its elements
422 | ArrayBinder [Binder]
424 -- A binder which matches an array and binds its head and tail
426 | ConsBinder Binder Binder
428 -- A binder which binds its input to an identifier
430 | NamedBinder Ident Binder
432 -- A binder with source position information
434 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
437-- Collect all names introduced in binders in an expression
439binderNames :: (D.Data d) => d -> [Ident]
440binderNames = everything (++) (mkQ [] go)
442 go (VarBinder ident) = [ident]
443 go (NamedBinder ident _) = [ident]
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