on cb0e9ef help sign in
elk/src/Validate.hs
464 lines 12 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Validate (declarations) where
4import Control.Monad (foldM)
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 as Expr
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' <- expression expr
96 let def' = Valid.Definition pat expr' Nothing
97 checkDefinition def'
98 addRest def' decls
100 Source.TypeAnnotation name tipe ->
101 case decls of
102 D.Decl (A.A _ (D.Definition (A.A _
103 (Source.Definition pat@(A.A _ (Pattern.Var name')) expr)))) : rest
104 | name == name' ->
105 do expr' <- expression expr
106 let def' = Valid.Definition pat expr' (Just tipe)
107 checkDefinition def'
108 addRest def' rest
110 _ ->
111 Result.throw region (Error.TypeWithoutDefinition name)
114-- VALIDATE PORTS IN DECLARATIONS
116portHelp
117 :: Maybe String
118 -> R.Region
119 -> D.SourcePort
120 -> [D.SourceDecl]
121 -> Result.Result wrn Error.Error [D.ValidDecl]
122portHelp comment region port decls =
123 let addRest port' rest =
124 (:) (A.A (region,comment) (D.Port port'))
125 <$> validateDecls Nothing rest
126 in
127 case port of
128 D.PortDefinition name _ ->
129 Result.throw region (Error.PortWithoutAnnotation name)
131 D.PortAnnotation name tipe ->
132 case decls of
133 D.Decl (A.A _ (D.Port (D.PortDefinition name' expr))) : rest
134 | name == name' ->
135 do expr' <- expression expr
136 let port' = D.Out name expr' tipe
137 addRest port' rest
139 _ ->
140 addRest (D.In name tipe) decls
143-- VALIDATE DEFINITIONS
145definitions :: [Source.Def] -> Result.Result wrn Error.Error [Valid.Def]
146definitions sourceDefs =
147 do validDefs <- definitionsHelp sourceDefs
148 let patterns = map (\(Valid.Definition p _ _) -> p) validDefs
149 defDuplicates patterns
150 return validDefs
153definitionsHelp :: [Source.Def] -> Result.Result wrn Error.Error [Valid.Def]
154definitionsHelp sourceDefs =
155 case sourceDefs of
156 [] ->
157 return []
159 A.A _ (Source.Definition pat expr) : rest ->
160 do expr' <- expression expr
161 let def = Valid.Definition pat expr' Nothing
162 checkDefinition def
163 (:) def <$> definitionsHelp rest
165 A.A region (Source.TypeAnnotation name tipe) : rest ->
166 case rest of
167 A.A _ (Source.Definition pat@(A.A _ (Pattern.Var name')) expr) : rest'
168 | name == name' ->
169 do expr' <- expression expr
170 let def = Valid.Definition pat expr' (Just tipe)
171 checkDefinition def
172 (:) def <$> definitionsHelp rest'
174 _ ->
175 Result.throw region (Error.TypeWithoutDefinition name)
178checkDefinition :: Valid.Def -> Result.Result wrn Error.Error ()
179checkDefinition (Valid.Definition pattern body _) =
180 case fst (Expr.collectLambdas body) of
181 [] ->
182 return ()
184 args ->
185 case pattern of
186 A.A _ (Pattern.Var _) ->
187 return ()
189 _ ->
190 let
191 (A.A start _) = pattern
192 (A.A end _) = last args
193 in
194 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
197-- VALIDATE EXPRESSIONS
199expression :: Source.Expr -> Result.Result wrn Error.Error Valid.Expr
200expression (A.A ann sourceExpression) =
201 A.A ann <$>
202 case sourceExpression of
203 Var x ->
204 return (Var x)
206 Lambda pattern body ->
207 Lambda
208 <$> validatePattern pattern
209 <*> expression body
211 Binop op leftExpr rightExpr ->
212 Binop op
213 <$> expression leftExpr
214 <*> expression rightExpr
216 Case e branches ->
217 Case
218 <$> expression e
219 <*> T.traverse (\(p,b) -> (,) <$> validatePattern p <*> expression b) branches
221 Data name args ->
222 Data name <$> T.traverse expression args
224 Literal lit ->
225 return (Literal lit)
227 Range lowExpr highExpr ->
228 Range
229 <$> expression lowExpr
230 <*> expression highExpr
232 ExplicitList expressions ->
233 ExplicitList
234 <$> T.traverse expression expressions
236 App funcExpr argExpr ->
237 App
238 <$> expression funcExpr
239 <*> expression argExpr
241 If branches finally ->
242 If
243 <$> T.traverse both branches
244 <*> expression finally
246 Access record field ->
247 Access
248 <$> expression record
249 <*> return field
251 Update record fields ->
252 Update
253 <$> expression record
254 <*> T.traverse second fields
256 Record fields ->
257 let
258 checkDups seenFields (field,_) =
259 if Set.member field seenFields then
260 Result.throw ann (Error.DuplicateFieldName field)
262 else
263 return (Set.insert field seenFields)
264 in
265 do _ <- foldM checkDups Set.empty fields
266 Record <$> T.traverse second fields
268 Let defs body ->
269 Let
270 <$> definitions defs
271 <*> expression body
273 GLShader uid src gltipe ->
274 return (GLShader uid src gltipe)
276 Port impl ->
277 Port <$>
278 case impl of
279 In name tipe ->
280 return (In name tipe)
282 Out name expr tipe ->
283 do expr' <- expression expr
284 return (Out name expr' tipe)
286 Task name expr tipe ->
287 do expr' <- expression expr
288 return (Task name expr' tipe)
291second :: (a, Source.Expr) -> Result.Result wrn Error.Error (a, Valid.Expr)
292second (value, expr) =
293 (,) value <$> expression expr
296both
297 :: (Source.Expr, Source.Expr)
298 -> Result.Result wrn Error.Error (Valid.Expr, Valid.Expr)
299both (expr1, expr2) =
300 (,) <$> expression expr1 <*> expression expr2
303-- VALIDATE PATTERNS
305validatePattern :: Pattern.RawPattern -> Result.Result wrn Error.Error Pattern.RawPattern
306validatePattern pattern =
307 do detectDuplicates Error.BadPattern (Pattern.boundVars pattern)
308 return pattern
311-- DETECT DUPLICATES
313detectDuplicates
314 :: (String -> Error.Error)
315 -> [A.Located String]
316 -> Result.Result wrn Error.Error ()
317detectDuplicates tag names =
318 let add (A.A region name) dict =
319 Map.insertWith (++) name [region] dict
321 makeGroups pairs =
322 Map.toList (foldr add Map.empty pairs)
324 check (name, regions) =
325 case regions of
326 _ : region : _ ->
327 Result.throw region (tag name)
329 _ ->
330 return ()
331 in
332 F.traverse_ check (makeGroups names)
335defDuplicates
336 :: [Pattern.RawPattern]
337 -> Result.Result wrn Error.Error ()
338defDuplicates patterns =
339 concatMap Pattern.boundVars patterns
340 |> detectDuplicates Error.DuplicateDefinition
343declDuplicates :: [D.ValidDecl] -> Result.Result wrn Error.Error ()
344declDuplicates decls =
345 let (valueLists, typeLists) = unzip (map extractValues decls)
346 in
347 (\_ _ -> ())
348 <$> detectDuplicates Error.DuplicateValueDeclaration (concat valueLists)
349 <*> detectDuplicates Error.DuplicateTypeDeclaration (concat typeLists)
352extractValues :: D.ValidDecl -> ([A.Located String], [A.Located String])
353extractValues (A.A (region, _) decl) =
354 case decl of
355 D.Definition (Valid.Definition pattern _ _) ->
356 ( Pattern.boundVars pattern
357 , []
358 )
360 D.Datatype name _ ctors ->
361 ( map (A.A region . fst) ctors
362 , [A.A region name]
363 )
365 D.TypeAlias name _ (A.A _ (Type.RRecord _ _)) ->
366 ( [A.A region name]
367 , [A.A region name]
368 )
370 D.TypeAlias name _ _ ->
371 ( []
372 , [A.A region name]
373 )
375 D.Port port ->
376 ( [A.A region (D.validPortName port)]
377 , []
378 )
380 D.Fixity _ _ _ ->
381 ( []
382 , []
383 )
386-- UNBOUND TYPE VARIABLES
388checkDecl :: Bool -> D.ValidDecl -> Result.Result wrn Error.Error ()
389checkDecl isRoot (A.A (region,_) decl) =
390 case decl of
391 D.Definition _ ->
392 return ()
394 D.Datatype name boundVars ctors ->
395 case diff boundVars (concatMap freeVars (concatMap snd ctors)) of
396 (_, []) ->
397 return ()
399 (_, unbound) ->
400 Result.throw region
401 (Error.UnboundTypeVarsInUnion name boundVars unbound)
403 D.TypeAlias name boundVars tipe ->
404 case diff boundVars (freeVars tipe) of
405 ([], []) ->
406 return ()
408 ([], unbound) ->
409 Result.throw region
410 (Error.UnboundTypeVarsInAlias name boundVars unbound)
412 (unused, []) ->
413 Result.throw region
414 (Error.UnusedTypeVarsInAlias name boundVars unused)
416 (unused, unbound) ->
417 Result.throw region
418 (Error.MessyTypeVarsInAlias name boundVars unused unbound)
420 D.Port _ ->
421 if isRoot then
422 return ()
424 else
425 Result.throw region Error.UnexpectedPort
427 D.Fixity _ _ _ ->
428 return ()
431diff :: [String] -> [A.Located String] -> ([String], [String])
432diff left right =
433 let
434 leftSet =
435 Set.fromList left
437 rightSet =
438 Set.fromList (map A.drop right)
439 in
440 ( Set.toList (Set.difference leftSet rightSet)
441 , Set.toList (Set.difference rightSet leftSet)
442 )
446freeVars :: Type.Raw -> [A.Located String]
447freeVars (A.A region tipe) =
448 case tipe of
449 Type.RLambda t1 t2 ->
450 freeVars t1 ++ freeVars t2
452 Type.RVar x ->
453 [A.A region x]
455 Type.RType _ ->
456 []
458 Type.RApp t ts ->
459 concatMap freeVars (t:ts)
461 Type.RRecord fields ext ->
462 maybe [] freeVars ext
463 ++ concatMap (freeVars . snd) fields