git.iko.soy/rowtype-yoga/purescript-native
on eb505ed help sign in
254 lines 6.4 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 (Either Binder (Ident, [Either Ident 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-- Find the original dictionary which a type class dictionary in scope refers to
178--
179canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
180canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
181canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
183-- |
184-- A statement in a do-notation block
185--
186data DoNotationElement
187 -- |
188 -- A monadic value without a binder
189 --
190 = DoNotationValue Value
191 -- |
192 -- A monadic value with a binder
193 --
194 | DoNotationBind Binder Value
195 -- |
196 -- A let statement, i.e. a pure value with a binder
197 --
198 | DoNotationLet Binder Value deriving (Show, Data, Typeable)
200-- |
201-- Data type for binders
202--
203data Binder
204 -- |
205 -- Wildcard binder
206 --
207 = NullBinder
208 -- |
209 -- A binder which matches a boolean literal
210 --
211 | BooleanBinder Bool
212 -- |
213 -- A binder which matches a string literal
214 --
215 | StringBinder String
216 -- |
217 -- A binder which matches a numeric literal
218 --
219 | NumberBinder (Either Integer Double)
220 -- |
221 -- A binder which binds an identifier
222 --
223 | VarBinder Ident
224 -- |
225 -- A binder which matches a data constructor
226 --
227 | ConstructorBinder (Qualified ProperName) [Binder]
228 -- |
229 -- A binder which matches a record and binds its properties
230 --
231 | ObjectBinder [(String, Binder)]
232 -- |
233 -- A binder which matches an array and binds its elements
234 --
235 | ArrayBinder [Binder]
236 -- |
237 -- A binder which matches an array and binds its head and tail
238 --
239 | ConsBinder Binder Binder
240 -- |
241 -- A binder which binds its input to an identifier
242 --
243 | NamedBinder Ident Binder deriving (Show, Data, Typeable)
246-- |
247-- Collect all names introduced in binders in an expression
248--
249binderNames :: (Data d) => d -> [Ident]
250binderNames = everything (++) (mkQ [] go)
251 where
252 go (VarBinder ident) = [ident]
253 go (NamedBinder ident _) = [ident]
254 go _ = []