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 deriving (D.Data, D.Typeable)
39instance Show Associativity where
40 show Infixl = "infixl"
41 show Infixr = "infixr"
44-- Fixity data for infix operators
46data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
49-- A module declaration, consisting of a module name, a list of declarations, and a list of the
50-- declarations that are explicitly imported. If the export list is Nothing, everything is exported.
52data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
55-- An item in a list of explicit imports or exports
59 -- A type constructor with data constructors
61 = TypeRef ProperName (Maybe [ProperName])
69 | TypeClassRef ProperName
71 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
73 | TypeInstanceRef Ident
74 deriving (Show, Eq, D.Data, D.Typeable)
77-- The data type of declarations
81 -- A data type declaration (name, arguments, data constructors)
83 = DataDeclaration ProperName [String] [(ProperName, [Type])]
85 -- A minimal mutually recursive set of data type declarations
87 | DataBindingGroupDeclaration [Declaration]
89 -- A type synonym declaration (name, arguments, type)
91 | TypeSynonymDeclaration ProperName [String] Type
93 -- A type declaration for a value (name, ty)
95 | TypeDeclaration Ident Type
97 -- A value declaration (name, top-level binders, optional guard, value)
99 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
101 -- A minimal mutually recursive set of value declarations
103 | BindingGroupDeclaration [(Ident, NameKind, Value)]
105 -- A foreign import declaration (type, name, optional inline Javascript, type)
107 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
109 -- A data type foreign import (name, kind)
111 | ExternDataDeclaration ProperName Kind
113 -- A type class instance foreign import
115 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
117 -- A fixity declaration (fixity data, operator name)
119 | FixityDeclaration Fixity String
121 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
124 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
126 -- A type class declaration (name, argument, member declarations)
128 | TypeClassDeclaration ProperName [String] [Declaration]
130 -- A type instance declaration (name, dependencies, class name, instance types, member
133 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
134 deriving (Show, D.Data, D.Typeable)
137-- Test if a declaration is a value declaration
139isValueDecl :: Declaration -> Bool
140isValueDecl ValueDeclaration{} = True
144-- Test if a declaration is a data type or type synonym declaration
146isDataDecl :: Declaration -> Bool
147isDataDecl DataDeclaration{} = True
148isDataDecl TypeSynonymDeclaration{} = True
152-- Test if a declaration is a module import
154isImportDecl :: Declaration -> Bool
155isImportDecl ImportDeclaration{} = True
156isImportDecl _ = False
159-- Test if a declaration is a data type foreign import
161isExternDataDecl :: Declaration -> Bool
162isExternDataDecl ExternDataDeclaration{} = True
163isExternDataDecl _ = False
166-- Test if a declaration is a type class instance foreign import
168isExternInstanceDecl :: Declaration -> Bool
169isExternInstanceDecl ExternInstanceDeclaration{} = True
170isExternInstanceDecl _ = False
173-- Test if a declaration is a fixity declaration
175isFixityDecl :: Declaration -> Bool
176isFixityDecl FixityDeclaration{} = True
177isFixityDecl _ = False
180-- Test if a declaration is a foreign import
182isExternDecl :: Declaration -> Bool
183isExternDecl ExternDeclaration{} = True
184isExternDecl _ = False
187-- Test if a declaration is a type class or instance declaration
189isTypeClassDeclaration :: Declaration -> Bool
190isTypeClassDeclaration TypeClassDeclaration{} = True
191isTypeClassDeclaration TypeInstanceDeclaration{} = True
192isTypeClassDeclaration _ = False
195-- A guard is just a boolean-valued expression that appears alongside a set of binders
200-- Data type for values
206 = NumericLiteral (Either Integer Double)
210 | StringLiteral String
214 | BooleanLiteral Bool
216 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
219 | BinaryNoParens (Qualified Ident) Value Value
221 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
228 | ArrayLiteral [Value]
232 | ObjectLiteral [(String, Value)]
234 -- An record property accessor expression
236 | Accessor String Value
238 -- Partial record update
240 | ObjectUpdate Value [(String, Value)]
242 -- Function introduction
244 | Abs (Either Ident Binder) Value
246 -- Function application
252 | Var (Qualified Ident)
254 -- Conditional (if-then-else expression)
256 | IfThenElse Value Value Value
258 -- A data constructor
260 | Constructor (Qualified ProperName)
262 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
263 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
265 | Case [Value] [CaseAlternative]
267 -- A value with a type annotation
269 | TypedValue Bool Value Type
273 | Let [Declaration] Value
275 -- A do-notation block
277 | Do [DoNotationElement]
279 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
280 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
281 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
282 -- instance type, and the type class dictionaries in scope.
284 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, D.Data, D.Typeable)
287-- An alternative in a case statement
289data CaseAlternative = CaseAlternative
291 -- A collection of binders with which to match the inputs
293 caseAlternativeBinders :: [Binder]
297 , caseAlternativeGuard :: Maybe Guard
299 -- The result expression
301 , caseAlternativeResult :: Value
302 } deriving (Show, D.Data, D.Typeable)
305-- Find the original dictionary which a type class dictionary in scope refers to
307canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
308canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
309canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
312-- A statement in a do-notation block
314data DoNotationElement
316 -- A monadic value without a binder
318 = DoNotationValue Value
320 -- A monadic value with a binder
322 | DoNotationBind Binder Value
324 -- A let statement, i.e. a pure value with a binder
326 | DoNotationLet Binder Value deriving (Show, D.Data, D.Typeable)
329-- Data type for binders
337 -- A binder which matches a boolean literal
341 -- A binder which matches a string literal
343 | StringBinder String
345 -- A binder which matches a numeric literal
347 | NumberBinder (Either Integer Double)
349 -- A binder which binds an identifier
353 -- A binder which matches a data constructor
355 | ConstructorBinder (Qualified ProperName) [Binder]
357 -- A binder which matches a record and binds its properties
359 | ObjectBinder [(String, Binder)]
361 -- A binder which matches an array and binds its elements
363 | ArrayBinder [Binder]
365 -- A binder which matches an array and binds its head and tail
367 | ConsBinder Binder Binder
369 -- A binder which binds its input to an identifier
371 | NamedBinder Ident Binder deriving (Show, D.Data, D.Typeable)
375-- Collect all names introduced in binders in an expression
377binderNames :: (D.Data d) => d -> [Ident]
378binderNames = everything (++) (mkQ [] go)
380 go (VarBinder ident) = [ident]
381 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