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-- Fixity data for infix operators
47data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
50-- A module declaration, consisting of a module name, a list of declarations, and a list of the
51-- declarations that are explicitly imported. If the export list is Nothing, everything is exported.
53data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
56-- An item in a list of explicit imports or exports
60 -- A type constructor with data constructors
62 = TypeRef ProperName (Maybe [ProperName])
70 | TypeClassRef ProperName
72 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
74 | TypeInstanceRef Ident
75 deriving (Show, Eq, D.Data, D.Typeable)
78-- The data type of declarations
82 -- A data type declaration (name, arguments, data constructors)
84 = DataDeclaration ProperName [String] [(ProperName, [Type])]
86 -- A minimal mutually recursive set of data type declarations
88 | DataBindingGroupDeclaration [Declaration]
90 -- A type synonym declaration (name, arguments, type)
92 | TypeSynonymDeclaration ProperName [String] Type
94 -- A type declaration for a value (name, ty)
96 | TypeDeclaration Ident Type
98 -- A value declaration (name, top-level binders, optional guard, value)
100 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
102 -- A minimal mutually recursive set of value declarations
104 | BindingGroupDeclaration [(Ident, NameKind, Value)]
106 -- A foreign import declaration (type, name, optional inline Javascript, type)
108 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
110 -- A data type foreign import (name, kind)
112 | ExternDataDeclaration ProperName Kind
114 -- A type class instance foreign import
116 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
118 -- A fixity declaration (fixity data, operator name)
120 | FixityDeclaration Fixity String
122 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
125 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
127 -- A type class declaration (name, argument, member declarations)
129 | TypeClassDeclaration ProperName [String] [Declaration]
131 -- A type instance declaration (name, dependencies, class name, instance types, member
134 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
135 deriving (Show, D.Data, D.Typeable)
138-- Test if a declaration is a value declaration
140isValueDecl :: Declaration -> Bool
141isValueDecl ValueDeclaration{} = True
145-- Test if a declaration is a data type or type synonym declaration
147isDataDecl :: Declaration -> Bool
148isDataDecl DataDeclaration{} = True
149isDataDecl TypeSynonymDeclaration{} = True
153-- Test if a declaration is a module import
155isImportDecl :: Declaration -> Bool
156isImportDecl ImportDeclaration{} = True
157isImportDecl _ = False
160-- Test if a declaration is a data type foreign import
162isExternDataDecl :: Declaration -> Bool
163isExternDataDecl ExternDataDeclaration{} = True
164isExternDataDecl _ = False
167-- Test if a declaration is a type class instance foreign import
169isExternInstanceDecl :: Declaration -> Bool
170isExternInstanceDecl ExternInstanceDeclaration{} = True
171isExternInstanceDecl _ = False
174-- Test if a declaration is a fixity declaration
176isFixityDecl :: Declaration -> Bool
177isFixityDecl FixityDeclaration{} = True
178isFixityDecl _ = False
181-- Test if a declaration is a foreign import
183isExternDecl :: Declaration -> Bool
184isExternDecl ExternDeclaration{} = True
185isExternDecl _ = False
188-- Test if a declaration is a type class or instance declaration
190isTypeClassDeclaration :: Declaration -> Bool
191isTypeClassDeclaration TypeClassDeclaration{} = True
192isTypeClassDeclaration TypeInstanceDeclaration{} = True
193isTypeClassDeclaration _ = False
196-- A guard is just a boolean-valued expression that appears alongside a set of binders
201-- Data type for values
207 = NumericLiteral (Either Integer Double)
211 | StringLiteral String
215 | BooleanLiteral Bool
217 -- A prefix -, will be desugared
221 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
224 | BinaryNoParens (Qualified Ident) Value Value
226 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
233 | ArrayLiteral [Value]
237 | ObjectLiteral [(String, Value)]
239 -- An record property accessor expression
241 | Accessor String Value
243 -- Partial record update
245 | ObjectUpdate Value [(String, Value)]
247 -- Function introduction
249 | Abs (Either Ident Binder) Value
251 -- Function application
257 | Var (Qualified Ident)
259 -- Conditional (if-then-else expression)
261 | IfThenElse Value Value Value
263 -- A data constructor
265 | Constructor (Qualified ProperName)
267 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
268 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
270 | Case [Value] [CaseAlternative]
272 -- A value with a type annotation
274 | TypedValue Bool Value Type
278 | Let [Declaration] Value
280 -- A do-notation block
282 | Do [DoNotationElement]
284 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
285 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
286 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
287 -- instance type, and the type class dictionaries in scope.
289 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, D.Data, D.Typeable)
292-- An alternative in a case statement
294data CaseAlternative = CaseAlternative
296 -- A collection of binders with which to match the inputs
298 caseAlternativeBinders :: [Binder]
302 , caseAlternativeGuard :: Maybe Guard
304 -- The result expression
306 , caseAlternativeResult :: Value
307 } deriving (Show, D.Data, D.Typeable)
310-- Find the original dictionary which a type class dictionary in scope refers to
312canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
313canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
314canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
317-- A statement in a do-notation block
319data DoNotationElement
321 -- A monadic value without a binder
323 = DoNotationValue Value
325 -- A monadic value with a binder
327 | DoNotationBind Binder Value
329 -- A let statement, i.e. a pure value with a binder
331 | DoNotationLet Binder Value deriving (Show, D.Data, D.Typeable)
334-- Data type for binders
342 -- A binder which matches a boolean literal
346 -- A binder which matches a string literal
348 | StringBinder String
350 -- A binder which matches a numeric literal
352 | NumberBinder (Either Integer Double)
354 -- A binder which binds an identifier
358 -- A binder which matches a data constructor
360 | ConstructorBinder (Qualified ProperName) [Binder]
362 -- A binder which matches a record and binds its properties
364 | ObjectBinder [(String, Binder)]
366 -- A binder which matches an array and binds its elements
368 | ArrayBinder [Binder]
370 -- A binder which matches an array and binds its head and tail
372 | ConsBinder Binder Binder
374 -- A binder which binds its input to an identifier
376 | NamedBinder Ident Binder deriving (Show, D.Data, D.Typeable)
380-- Collect all names introduced in binders in an expression
382binderNames :: (D.Data d) => d -> [Ident]
383binderNames = everything (++) (mkQ [] go)
385 go (VarBinder ident) = [ident]
386 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