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
56 , sourcePosColumn :: Int
57 } deriving (D.Data, D.Typeable)
59instance Show SourcePos where
60 show sp = "line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
63-- Fixity data for infix operators
65data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
68-- A module declaration, consisting of a module name, a list of declarations, and a list of the
69-- declarations that are explicitly imported. If the export list is Nothing, everything is exported.
71data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
74-- An item in a list of explicit imports or exports
78 -- A type constructor with data constructors
80 = TypeRef ProperName (Maybe [ProperName])
88 | TypeClassRef ProperName
90 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
92 | TypeInstanceRef Ident
93 deriving (Show, Eq, D.Data, D.Typeable)
96-- The data type of declarations
100 -- A data type declaration (name, arguments, data constructors)
102 = DataDeclaration ProperName [String] [(ProperName, [Type])]
104 -- A minimal mutually recursive set of data type declarations
106 | DataBindingGroupDeclaration [Declaration]
108 -- A type synonym declaration (name, arguments, type)
110 | TypeSynonymDeclaration ProperName [String] Type
112 -- A type declaration for a value (name, ty)
114 | TypeDeclaration Ident Type
116 -- A value declaration (name, top-level binders, optional guard, value)
118 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
120 -- A minimal mutually recursive set of value declarations
122 | BindingGroupDeclaration [(Ident, NameKind, Value)]
124 -- A foreign import declaration (type, name, optional inline Javascript, type)
126 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
128 -- A data type foreign import (name, kind)
130 | ExternDataDeclaration ProperName Kind
132 -- A type class instance foreign import
134 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
136 -- A fixity declaration (fixity data, operator name)
138 | FixityDeclaration Fixity String
140 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
143 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
145 -- A type class declaration (name, argument, member declarations)
147 | TypeClassDeclaration ProperName [String] [Declaration]
149 -- A type instance declaration (name, dependencies, class name, instance types, member
152 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
154 -- A declaration with source position information
156 | PositionedDeclaration SourcePos Declaration
157 deriving (Show, D.Data, D.Typeable)
160-- Test if a declaration is a value declaration
162isValueDecl :: Declaration -> Bool
163isValueDecl ValueDeclaration{} = True
164isValueDecl (PositionedDeclaration _ d) = isValueDecl d
168-- Test if a declaration is a data type or type synonym declaration
170isDataDecl :: Declaration -> Bool
171isDataDecl DataDeclaration{} = True
172isDataDecl TypeSynonymDeclaration{} = True
173isDataDecl (PositionedDeclaration _ d) = isDataDecl d
177-- Test if a declaration is a module import
179isImportDecl :: Declaration -> Bool
180isImportDecl ImportDeclaration{} = True
181isImportDecl (PositionedDeclaration _ d) = isImportDecl d
182isImportDecl _ = False
185-- Test if a declaration is a data type foreign import
187isExternDataDecl :: Declaration -> Bool
188isExternDataDecl ExternDataDeclaration{} = True
189isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
190isExternDataDecl _ = False
193-- Test if a declaration is a type class instance foreign import
195isExternInstanceDecl :: Declaration -> Bool
196isExternInstanceDecl ExternInstanceDeclaration{} = True
197isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
198isExternInstanceDecl _ = False
201-- Test if a declaration is a fixity declaration
203isFixityDecl :: Declaration -> Bool
204isFixityDecl FixityDeclaration{} = True
205isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
206isFixityDecl _ = False
209-- Test if a declaration is a foreign import
211isExternDecl :: Declaration -> Bool
212isExternDecl ExternDeclaration{} = True
213isExternDecl (PositionedDeclaration _ d) = isExternDecl d
214isExternDecl _ = False
217-- Test if a declaration is a type class or instance declaration
219isTypeClassDeclaration :: Declaration -> Bool
220isTypeClassDeclaration TypeClassDeclaration{} = True
221isTypeClassDeclaration TypeInstanceDeclaration{} = True
222isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
223isTypeClassDeclaration _ = False
226-- A guard is just a boolean-valued expression that appears alongside a set of binders
231-- Data type for values
237 = NumericLiteral (Either Integer Double)
241 | StringLiteral String
245 | BooleanLiteral Bool
247 -- A prefix -, will be desugared
251 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
254 | BinaryNoParens (Qualified Ident) Value Value
256 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
263 | ArrayLiteral [Value]
267 | ObjectLiteral [(String, Value)]
269 -- An record property accessor expression
271 | Accessor String Value
273 -- Partial record update
275 | ObjectUpdate Value [(String, Value)]
277 -- Function introduction
279 | Abs (Either Ident Binder) Value
281 -- Function application
287 | Var (Qualified Ident)
289 -- Conditional (if-then-else expression)
291 | IfThenElse Value Value Value
293 -- A data constructor
295 | Constructor (Qualified ProperName)
297 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
298 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
300 | Case [Value] [CaseAlternative]
302 -- A value with a type annotation
304 | TypedValue Bool Value Type
308 | Let [Declaration] Value
310 -- A do-notation block
312 | Do [DoNotationElement]
314 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
315 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
316 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
317 -- instance type, and the type class dictionaries in scope.
319 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
321 -- A value with source position information
323 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
326-- An alternative in a case statement
328data CaseAlternative = CaseAlternative
330 -- A collection of binders with which to match the inputs
332 caseAlternativeBinders :: [Binder]
336 , caseAlternativeGuard :: Maybe Guard
338 -- The result expression
340 , caseAlternativeResult :: Value
341 } deriving (Show, D.Data, D.Typeable)
344-- Find the original dictionary which a type class dictionary in scope refers to
346canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
347canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
348canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
351-- A statement in a do-notation block
353data DoNotationElement
355 -- A monadic value without a binder
357 = DoNotationValue Value
359 -- A monadic value with a binder
361 | DoNotationBind Binder Value
363 -- A let statement, i.e. a pure value with a binder
365 | DoNotationLet Binder Value
367 -- A do notation element with source position information
369 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
372-- Data type for binders
380 -- A binder which matches a boolean literal
384 -- A binder which matches a string literal
386 | StringBinder String
388 -- A binder which matches a numeric literal
390 | NumberBinder (Either Integer Double)
392 -- A binder which binds an identifier
396 -- A binder which matches a data constructor
398 | ConstructorBinder (Qualified ProperName) [Binder]
400 -- A binder which matches a record and binds its properties
402 | ObjectBinder [(String, Binder)]
404 -- A binder which matches an array and binds its elements
406 | ArrayBinder [Binder]
408 -- A binder which matches an array and binds its head and tail
410 | ConsBinder Binder Binder
412 -- A binder which binds its input to an identifier
414 | NamedBinder Ident Binder
416 -- A binder with source position information
418 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
421-- Collect all names introduced in binders in an expression
423binderNames :: (D.Data d) => d -> [Ident]
424binderNames = everything (++) (mkQ [] go)
426 go (VarBinder ident) = [ident]
427 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