on b0beee4 help sign in
elk/src/Validate.hs
420 lines 11 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Validate (declarations) where
4import Control.Applicative ((<$>),(<*>))
5import qualified Data.Map as Map
6import qualified Data.Set as Set
7import qualified Data.Foldable as F
8import qualified Data.Traversable as T
10import AST.Expression.General
11import qualified AST.Expression.Source as Source
12import qualified AST.Expression.Valid as Valid
13import qualified AST.Declaration as D
14import qualified AST.Pattern as Pattern
15import qualified AST.Type as Type
16import Elm.Utils ((|>))
17import qualified Reporting.Annotation as A
18import qualified Reporting.Error.Syntax as Error
19import qualified Reporting.Region as R
20import qualified Reporting.Result as Result
23-- VALIDATE DECLARATIONS
25declarations
26 :: Bool
27 -> [D.SourceDecl]
28 -> Result.Result wrn Error.Error [D.ValidDecl]
29declarations isRoot sourceDecls =
30 do validDecls <- validateDecls Nothing sourceDecls
31 (\_ _ -> validDecls)
32 <$> declDuplicates validDecls
33 <*> T.traverse (checkDecl isRoot) validDecls
36validateDecls
37 :: Maybe String
38 -> [D.SourceDecl]
39 -> Result.Result wrn Error.Error [D.ValidDecl]
40validateDecls maybeComment sourceDecls =
41 case sourceDecls of
42 [] ->
43 return []
45 sourceDecl : decls ->
46 case sourceDecl of
47 D.Comment comment ->
48 validateDecls (Just comment) decls
50 D.Decl decl ->
51 validateDeclsHelp maybeComment decl decls
54validateDeclsHelp
55 :: Maybe String
56 -> A.Located D.SourceDecl'
57 -> [D.SourceDecl]
58 -> Result.Result wrn Error.Error [D.ValidDecl]
59validateDeclsHelp comment (A.A region decl) decls =
60 let addRest validDecl =
61 (:) (A.A (region, comment) validDecl)
62 <$> validateDecls Nothing decls
63 in
64 case decl of
65 D.Datatype name tvars ctors ->
66 addRest (D.Datatype name tvars ctors)
68 D.TypeAlias name tvars alias ->
69 addRest (D.TypeAlias name tvars alias)
71 D.Fixity assoc prec op ->
72 addRest (D.Fixity assoc prec op)
74 D.Definition def ->
75 defHelp comment def decls
77 D.Port port ->
78 portHelp comment region port decls
81-- VALIDATE DEFINITIONS IN DECLARATIONS
83defHelp
84 :: Maybe String
85 -> Source.Def
86 -> [D.SourceDecl]
87 -> Result.Result wrn Error.Error [D.ValidDecl]
88defHelp comment (A.A region def) decls =
89 let addRest def' rest =
90 (:) (A.A (region, comment) (D.Definition def'))
91 <$> validateDecls Nothing rest
92 in
93 case def of
94 Source.Definition pat expr ->
95 do expr' <- Validate.expression expr
96 let def' = Valid.Definition pat expr' Nothing
97 addRest def' decls
99 Source.TypeAnnotation name tipe ->
100 case decls of
101 D.Decl (A.A _ (D.Definition (A.A _
102 (Source.Definition pat@(A.A _ (Pattern.Var name')) expr)))) : rest
103 | name == name' ->
104 do expr' <- Validate.expression expr
105 let def' = Valid.Definition pat expr' (Just tipe)
106 addRest def' rest
108 _ ->
109 Result.throw region (Error.TypeWithoutDefinition name)
112-- VALIDATE PORTS IN DECLARATIONS
114portHelp
115 :: Maybe String
116 -> R.Region
117 -> D.SourcePort
118 -> [D.SourceDecl]
119 -> Result.Result wrn Error.Error [D.ValidDecl]
120portHelp comment region port decls =
121 let addRest port' rest =
122 (:) (A.A (region,comment) (D.Port port'))
123 <$> validateDecls Nothing rest
124 in
125 case port of
126 D.PortDefinition name _ ->
127 Result.throw region (Error.PortWithoutAnnotation name)
129 D.PortAnnotation name tipe ->
130 case decls of
131 D.Decl (A.A _ (D.Port (D.PortDefinition name' expr))) : rest
132 | name == name' ->
133 do expr' <- Validate.expression expr
134 let port' = D.Out name expr' tipe
135 addRest port' rest
137 _ ->
138 addRest (D.In name tipe) decls
141-- VALIDATE DEFINITIONS
143definitions :: [Source.Def] -> Result.Result wrn Error.Error [Valid.Def]
144definitions sourceDefs =
145 do validDefs <- definitionsHelp sourceDefs
146 let patterns = map (\(Valid.Definition p _ _) -> p) validDefs
147 defDuplicates patterns
148 return validDefs
151definitionsHelp :: [Source.Def] -> Result.Result wrn Error.Error [Valid.Def]
152definitionsHelp sourceDefs =
153 case sourceDefs of
154 [] ->
155 return []
157 A.A _ (Source.Definition pat expr) : rest ->
158 do expr' <- expression expr
159 let def = Valid.Definition pat expr' Nothing
160 (:) def <$> definitionsHelp rest
162 A.A region (Source.TypeAnnotation name tipe) : rest ->
163 case rest of
164 A.A _ (Source.Definition pat@(A.A _ (Pattern.Var name')) expr) : rest'
165 | name == name' ->
166 do expr' <- expression expr
167 let def = Valid.Definition pat expr' (Just tipe)
168 (:) def <$> definitionsHelp rest'
170 _ ->
171 Result.throw region (Error.TypeWithoutDefinition name)
174-- VALIDATE EXPRESSIONS
176expression :: Source.Expr -> Result.Result wrn Error.Error Valid.Expr
177expression (A.A ann sourceExpression) =
178 A.A ann <$>
179 case sourceExpression of
180 Var x ->
181 return (Var x)
183 Lambda pattern body ->
184 Lambda pattern <$> expression body
186 Binop op leftExpr rightExpr ->
187 Binop op
188 <$> expression leftExpr
189 <*> expression rightExpr
191 Case e branches ->
192 Case
193 <$> expression e
194 <*> T.traverse second branches
196 Data name args ->
197 Data name <$> T.traverse expression args
199 Literal lit ->
200 return (Literal lit)
202 Range lowExpr highExpr ->
203 Range
204 <$> expression lowExpr
205 <*> expression highExpr
207 ExplicitList expressions ->
208 ExplicitList
209 <$> T.traverse expression expressions
211 App funcExpr argExpr ->
212 App
213 <$> expression funcExpr
214 <*> expression argExpr
216 If branches finally ->
217 If
218 <$> T.traverse both branches
219 <*> expression finally
221 Access record field ->
222 Access
223 <$> expression record
224 <*> return field
226 Update record fields ->
227 Update
228 <$> expression record
229 <*> T.traverse second fields
231 Record fields ->
232 Record
233 <$> T.traverse second fields
235 Let defs body ->
236 Let
237 <$> definitions defs
238 <*> expression body
240 GLShader uid src gltipe ->
241 return (GLShader uid src gltipe)
243 Port impl ->
244 Port <$>
245 case impl of
246 In name tipe ->
247 return (In name tipe)
249 Out name expr tipe ->
250 do expr' <- expression expr
251 return (Out name expr' tipe)
253 Task name expr tipe ->
254 do expr' <- expression expr
255 return (Task name expr' tipe)
257 Crash details ->
258 return (Crash details)
261second :: (a, Source.Expr) -> Result.Result wrn Error.Error (a, Valid.Expr)
262second (value, expr) =
263 (,) value <$> expression expr
266both
267 :: (Source.Expr, Source.Expr)
268 -> Result.Result wrn Error.Error (Valid.Expr, Valid.Expr)
269both (expr1, expr2) =
270 (,) <$> expression expr1 <*> expression expr2
273-- DETECT DUPLICATES
275detectDuplicates
276 :: (String -> Error.Error)
277 -> [A.Located String]
278 -> Result.Result wrn Error.Error ()
279detectDuplicates tag names =
280 let add (A.A region name) dict =
281 Map.insertWith (++) name [region] dict
283 makeGroups pairs =
284 Map.toList (foldr add Map.empty pairs)
286 check (name, regions) =
287 case regions of
288 _ : region : _ ->
289 Result.throw region (tag name)
291 _ ->
292 return ()
293 in
294 F.traverse_ check (makeGroups names)
297defDuplicates
298 :: [Pattern.RawPattern]
299 -> Result.Result wrn Error.Error ()
300defDuplicates patterns =
301 concatMap Pattern.boundVars patterns
302 |> detectDuplicates Error.DuplicateDefinition
305declDuplicates :: [D.ValidDecl] -> Result.Result wrn Error.Error ()
306declDuplicates decls =
307 let (valueLists, typeLists) = unzip (map extractValues decls)
308 in
309 (\_ _ -> ())
310 <$> detectDuplicates Error.DuplicateValueDeclaration (concat valueLists)
311 <*> detectDuplicates Error.DuplicateTypeDeclaration (concat typeLists)
314extractValues :: D.ValidDecl -> ([A.Located String], [A.Located String])
315extractValues (A.A (region, _) decl) =
316 case decl of
317 D.Definition (Valid.Definition pattern _ _) ->
318 ( Pattern.boundVars pattern
319 , []
320 )
322 D.Datatype name _ ctors ->
323 ( map (A.A region . fst) ctors
324 , [A.A region name]
325 )
327 D.TypeAlias name _ (A.A _ (Type.RRecord _ _)) ->
328 ( [A.A region name]
329 , [A.A region name]
330 )
332 D.TypeAlias name _ _ ->
333 ( []
334 , [A.A region name]
335 )
337 D.Port port ->
338 ( [A.A region (D.validPortName port)]
339 , []
340 )
342 D.Fixity _ _ _ ->
343 ( []
344 , []
345 )
348-- UNBOUND TYPE VARIABLES
350checkDecl :: Bool -> D.ValidDecl -> Result.Result wrn Error.Error ()
351checkDecl isRoot (A.A (region,_) decl) =
352 case decl of
353 D.Definition _ ->
354 return ()
356 D.Datatype name boundVars ctors ->
357 let frees = concatMap freeVars (concatMap snd ctors)
358 in
359 case findUnbound boundVars frees of
360 [] ->
361 return ()
363 unbound ->
364 let
365 (v:vs) = Set.toList (Set.fromList unbound)
366 in
367 Result.throw region
368 (Error.UnboundTypeVarsInUnion name boundVars v vs ctors)
370 D.TypeAlias name boundVars tipe ->
371 case findUnbound boundVars (freeVars tipe) of
372 [] ->
373 return ()
375 unbound ->
376 let
377 (v:vs) = Set.toList (Set.fromList unbound)
378 in
379 Result.throw region
380 (Error.UnboundTypeVarsInAlias name boundVars v vs tipe)
382 D.Port _ ->
383 if isRoot
384 then return ()
385 else Result.throw region Error.UnexpectedPort
387 D.Fixity _ _ _ ->
388 return ()
391findUnbound :: [String] -> [A.Located String] -> [String]
392findUnbound boundVars frees =
393 let bounds = Set.fromList boundVars
395 isUnbound x =
396 Set.notMember x bounds
397 in
398 filter isUnbound (map A.drop frees)
402freeVars :: Type.Raw -> [A.Located String]
403freeVars (A.A region tipe) =
404 case tipe of
405 Type.RLambda t1 t2 ->
406 freeVars t1 ++ freeVars t2
408 Type.RVar x ->
409 [A.A region x]
411 Type.RType _ ->
412 []
414 Type.RApp t ts ->
415 concatMap freeVars (t:ts)
417 Type.RRecord fields ext ->
418 maybe [] freeVars ext
419 ++ concatMap (freeVars . snd) fields