git.iko.soy/rowtype-yoga/purescript-native
on 7e08760 help sign in
247 lines 6.0 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Values
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for values, statements, binders and do notation
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Values where
20import Language.PureScript.Types
21import Language.PureScript.Names
23import Data.Data
24import Data.Generics (mkQ, everything)
26-- |
27-- A guard is just a boolean-valued expression that appears alongside a set of binders
28--
29type Guard = Value
31-- |
32-- Data type for values
33--
34data Value
35 -- |
36 -- A numeric literal
37 --
38 = NumericLiteral (Either Integer Double)
39 -- |
40 -- A string literal
41 --
42 | StringLiteral String
43 -- |
44 -- A boolean literal
45 --
46 | BooleanLiteral Bool
47 -- |
48 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
49 -- will be removed.
50 --
51 | BinaryNoParens (Qualified Ident) Value Value
52 -- |
53 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
54 -- will be removed.
55 --
56 | Parens Value
57 -- |
58 -- An array literal
59 --
60 | ArrayLiteral [Value]
61 -- |
62 -- An object literal
63 --
64 | ObjectLiteral [(String, Value)]
65 -- |
66 -- An record property accessor expression
67 --
68 | Accessor String Value
69 -- |
70 -- Partial record update
71 --
72 | ObjectUpdate Value [(String, Value)]
73 -- |
74 -- Function introduction
75 --
76 | Abs (Either Ident Binder) Value
77 -- |
78 -- Function application
79 --
80 | App Value Value
81 -- |
82 -- Variable
83 --
84 | Var (Qualified Ident)
85 -- |
86 -- Conditional (if-then-else expression)
87 --
88 | IfThenElse Value Value Value
89 -- |
90 -- A data constructor
91 --
92 | Constructor (Qualified ProperName)
93 -- |
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.
96 --
97 | Case [Value] [CaseAlternative]
98 -- |
99 -- A value with a type annotation
100 --
101 | TypedValue Bool Value Type
102 -- |
103 -- A let binding
104 --
105 | Let Binder Value Value
106 -- |
107 -- A do-notation block
108 --
109 | Do [DoNotationElement]
110 -- |
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.
115 --
116 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope] deriving (Show, Data, Typeable)
118-- |
119-- An alternative in a case statement
120--
121data CaseAlternative = CaseAlternative
122 { -- |
123 -- A collection of binders with which to match the inputs
124 --
125 caseAlternativeBinders :: [Binder]
126 -- |
127 -- An optional guard
128 --
129 , caseAlternativeGuard :: Maybe Guard
130 -- |
131 -- The result expression
132 --
133 , caseAlternativeResult :: Value
134 } deriving (Show, Data, Typeable)
136-- |
137-- Data representing a type class dictionary which is in scope
138--
139data TypeClassDictionaryInScope
140 = TypeClassDictionaryInScope {
141 -- |
142 -- The identifier with which the dictionary can be accessed at runtime
143 --
144 tcdName :: Qualified Ident
145 -- |
146 -- The name of the type class to which this type class instance applies
147 --
148 , tcdClassName :: Qualified ProperName
149 -- |
150 -- The types to which this type class instance applies
151 --
152 , tcdInstanceTypes :: [Type]
153 -- |
154 -- Type class dependencies which must be satisfied to construct this dictionary
155 --
156 , tcdDependencies :: Maybe [(Qualified ProperName, [Type])]
157 -- |
158 -- The type of this dictionary
159 --
160 , tcdType :: TypeClassDictionaryType
161 } deriving (Show, Data, Typeable)
163-- |
164-- The type of a type class dictionary
165--
166data TypeClassDictionaryType
167 -- |
168 -- A regular type class dictionary
169 --
170 = TCDRegular
171 -- |
172 -- A type class dictionary which is an alias for an imported dictionary from another module
173 --
174 | TCDAlias (Qualified Ident) deriving (Show, Eq, Data, Typeable)
176-- |
177-- A statement in a do-notation block
178--
179data DoNotationElement
180 -- |
181 -- A monadic value without a binder
182 --
183 = DoNotationValue Value
184 -- |
185 -- A monadic value with a binder
186 --
187 | DoNotationBind Binder Value
188 -- |
189 -- A let statement, i.e. a pure value with a binder
190 --
191 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
193-- |
194-- Data type for binders
195--
196data Binder
197 -- |
198 -- Wildcard binder
199 --
200 = NullBinder
201 -- |
202 -- A binder which matches a boolean literal
203 --
204 | BooleanBinder Bool
205 -- |
206 -- A binder which matches a string literal
207 --
208 | StringBinder String
209 -- |
210 -- A binder which matches a numeric literal
211 --
212 | NumberBinder (Either Integer Double)
213 -- |
214 -- A binder which binds an identifier
215 --
216 | VarBinder Ident
217 -- |
218 -- A binder which matches a data constructor
219 --
220 | ConstructorBinder (Qualified ProperName) [Binder]
221 -- |
222 -- A binder which matches a record and binds its properties
223 --
224 | ObjectBinder [(String, Binder)]
225 -- |
226 -- A binder which matches an array and binds its elements
227 --
228 | ArrayBinder [Binder]
229 -- |
230 -- A binder which matches an array and binds its head and tail
231 --
232 | ConsBinder Binder Binder
233 -- |
234 -- A binder which binds its input to an identifier
235 --
236 | NamedBinder Ident Binder deriving (Show, Data, Typeable)
239-- |
240-- Collect all names introduced in binders in an expression
241--
242binderNames :: (Data d) => d -> [Ident]
243binderNames = everything (++) (mkQ [] go)
244 where
245 go (VarBinder ident) = [ident]
246 go (NamedBinder ident _) = [ident]
247 go _ = []