1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Values
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
12-- Data types for values, statements, binders and do notation
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Values where
20import Language.PureScript.Types
21import Language.PureScript.Names
24import Data.Generics (mkQ, everything)
27-- A guard is just a boolean-valued expression that appears alongside a set of binders
32-- Data type for values
38 = NumericLiteral (Either Integer Double)
42 | StringLiteral String
48 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
51 | BinaryNoParens (Qualified Ident) Value Value
53 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
60 | ArrayLiteral [Value]
64 | ObjectLiteral [(String, Value)]
66 -- An record property accessor expression
68 | Accessor String Value
70 -- Partial record update
72 | ObjectUpdate Value [(String, Value)]
74 -- Function introduction
76 | Abs (Either Ident Binder) Value
78 -- Function application
84 | Var (Qualified Ident)
86 -- Conditional (if-then-else expression)
88 | IfThenElse Value Value Value
92 | Constructor (Qualified ProperName)
94 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
95 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
97 | Case [Value] [CaseAlternative]
99 -- A value with a type annotation
101 | TypedValue Bool Value Type
105 | Let (Either Binder (Ident, [Either Ident Binder])) Value Value
107 -- A do-notation block
109 | Do [DoNotationElement]
111 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
112 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
113 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
114 -- instance type, and the type class dictionaries in scope.
116 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, Data, Typeable)
119-- An alternative in a case statement
121data CaseAlternative = CaseAlternative
123 -- A collection of binders with which to match the inputs
125 caseAlternativeBinders :: [Binder]
129 , caseAlternativeGuard :: Maybe Guard
131 -- The result expression
133 , caseAlternativeResult :: Value
134 } deriving (Show, Data, Typeable)
137-- Data representing a type class dictionary which is in scope
139data TypeClassDictionaryInScope
140 = TypeClassDictionaryInScope {
142 -- The identifier with which the dictionary can be accessed at runtime
144 tcdName :: Qualified Ident
146 -- The name of the type class to which this type class instance applies
148 , tcdClassName :: Qualified ProperName
150 -- The types to which this type class instance applies
152 , tcdInstanceTypes :: [Type]
154 -- Type class dependencies which must be satisfied to construct this dictionary
156 , tcdDependencies :: Maybe [(Qualified ProperName, [Type])]
158 -- The type of this dictionary
160 , tcdType :: TypeClassDictionaryType
161 } deriving (Show, Data, Typeable)
164-- The type of a type class dictionary
166data TypeClassDictionaryType
168 -- A regular type class dictionary
172 -- A type class dictionary which is an alias for an imported dictionary from another module
174 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
177-- Find the original dictionary which a type class dictionary in scope refers to
179canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
180canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
181canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
184-- A statement in a do-notation block
186data DoNotationElement
188 -- A monadic value without a binder
190 = DoNotationValue Value
192 -- A monadic value with a binder
194 | DoNotationBind Binder Value
196 -- A let statement, i.e. a pure value with a binder
198 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
201-- Data type for binders
209 -- A binder which matches a boolean literal
213 -- A binder which matches a string literal
215 | StringBinder String
217 -- A binder which matches a numeric literal
219 | NumberBinder (Either Integer Double)
221 -- A binder which binds an identifier
225 -- A binder which matches a data constructor
227 | ConstructorBinder (Qualified ProperName) [Binder]
229 -- A binder which matches a record and binds its properties
231 | ObjectBinder [(String, Binder)]
233 -- A binder which matches an array and binds its elements
235 | ArrayBinder [Binder]
237 -- A binder which matches an array and binds its head and tail
239 | ConsBinder Binder Binder
241 -- A binder which binds its input to an identifier
243 | NamedBinder Ident Binder deriving (Show, Data, Typeable)
247-- Collect all names introduced in binders in an expression
249binderNames :: (Data d) => d -> [Ident]
250binderNames = everything (++) (mkQ [] go)
252 go (VarBinder ident) = [ident]
253 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