on 74bc3f2 help sign in
elk/src/Validate.hs
480 lines 13 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 funcName) ->
187 checkArguments funcName args
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))
197checkArguments :: String -> [Pattern.RawPattern] -> Result.Result wrn Error.Error ()
198checkArguments funcName args =
199 let
200 vars =
201 concatMap Pattern.boundVars args
203 checkDups seenArgs (A.A region arg) =
204 if Set.member arg seenArgs then
205 Result.throw region (Error.DuplicateArgument funcName arg)
207 else
208 return (Set.insert arg seenArgs)
209 in
210 foldM_ checkDups Set.empty vars
213-- VALIDATE EXPRESSIONS
215expression :: Source.Expr -> Result.Result wrn Error.Error Valid.Expr
216expression (A.A ann sourceExpression) =
217 A.A ann <$>
218 case sourceExpression of
219 Var x ->
220 return (Var x)
222 Lambda pattern body ->
223 Lambda
224 <$> validatePattern pattern
225 <*> expression body
227 Binop op leftExpr rightExpr ->
228 Binop op
229 <$> expression leftExpr
230 <*> expression rightExpr
232 Case e branches ->
233 Case
234 <$> expression e
235 <*> T.traverse (\(p,b) -> (,) <$> validatePattern p <*> expression b) branches
237 Data name args ->
238 Data name <$> T.traverse expression args
240 Literal lit ->
241 return (Literal lit)
243 Range lowExpr highExpr ->
244 Range
245 <$> expression lowExpr
246 <*> expression highExpr
248 ExplicitList expressions ->
249 ExplicitList
250 <$> T.traverse expression expressions
252 App funcExpr argExpr ->
253 App
254 <$> expression funcExpr
255 <*> expression argExpr
257 If branches finally ->
258 If
259 <$> T.traverse both branches
260 <*> expression finally
262 Access record field ->
263 Access
264 <$> expression record
265 <*> return field
267 Update record fields ->
268 Update
269 <$> expression record
270 <*> T.traverse second fields
272 Record fields ->
273 let
274 checkDups seenFields (field,_) =
275 if Set.member field seenFields then
276 Result.throw ann (Error.DuplicateFieldName field)
278 else
279 return (Set.insert field seenFields)
280 in
281 do foldM_ checkDups Set.empty fields
282 Record <$> T.traverse second fields
284 Let defs body ->
285 Let
286 <$> definitions defs
287 <*> expression body
289 GLShader uid src gltipe ->
290 return (GLShader uid src gltipe)
292 Port impl ->
293 Port <$>
294 case impl of
295 In name tipe ->
296 return (In name tipe)
298 Out name expr tipe ->
299 do expr' <- expression expr
300 return (Out name expr' tipe)
302 Task name expr tipe ->
303 do expr' <- expression expr
304 return (Task name expr' tipe)
307second :: (a, Source.Expr) -> Result.Result wrn Error.Error (a, Valid.Expr)
308second (value, expr) =
309 (,) value <$> expression expr
312both
313 :: (Source.Expr, Source.Expr)
314 -> Result.Result wrn Error.Error (Valid.Expr, Valid.Expr)
315both (expr1, expr2) =
316 (,) <$> expression expr1 <*> expression expr2
319-- VALIDATE PATTERNS
321validatePattern :: Pattern.RawPattern -> Result.Result wrn Error.Error Pattern.RawPattern
322validatePattern pattern =
323 do detectDuplicates Error.BadPattern (Pattern.boundVars pattern)
324 return pattern
327-- DETECT DUPLICATES
329detectDuplicates
330 :: (String -> Error.Error)
331 -> [A.Located String]
332 -> Result.Result wrn Error.Error ()
333detectDuplicates tag names =
334 let add (A.A region name) dict =
335 Map.insertWith (++) name [region] dict
337 makeGroups pairs =
338 Map.toList (foldr add Map.empty pairs)
340 check (name, regions) =
341 case regions of
342 _ : region : _ ->
343 Result.throw region (tag name)
345 _ ->
346 return ()
347 in
348 F.traverse_ check (makeGroups names)
351defDuplicates
352 :: [Pattern.RawPattern]
353 -> Result.Result wrn Error.Error ()
354defDuplicates patterns =
355 concatMap Pattern.boundVars patterns
356 |> detectDuplicates Error.DuplicateDefinition
359declDuplicates :: [D.ValidDecl] -> Result.Result wrn Error.Error ()
360declDuplicates decls =
361 let (valueLists, typeLists) = unzip (map extractValues decls)
362 in
363 (\_ _ -> ())
364 <$> detectDuplicates Error.DuplicateValueDeclaration (concat valueLists)
365 <*> detectDuplicates Error.DuplicateTypeDeclaration (concat typeLists)
368extractValues :: D.ValidDecl -> ([A.Located String], [A.Located String])
369extractValues (A.A (region, _) decl) =
370 case decl of
371 D.Definition (Valid.Definition pattern _ _) ->
372 ( Pattern.boundVars pattern
373 , []
374 )
376 D.Datatype name _ ctors ->
377 ( map (A.A region . fst) ctors
378 , [A.A region name]
379 )
381 D.TypeAlias name _ (A.A _ (Type.RRecord _ _)) ->
382 ( [A.A region name]
383 , [A.A region name]
384 )
386 D.TypeAlias name _ _ ->
387 ( []
388 , [A.A region name]
389 )
391 D.Port port ->
392 ( [A.A region (D.validPortName port)]
393 , []
394 )
396 D.Fixity _ _ _ ->
397 ( []
398 , []
399 )
402-- UNBOUND TYPE VARIABLES
404checkDecl :: Bool -> D.ValidDecl -> Result.Result wrn Error.Error ()
405checkDecl isRoot (A.A (region,_) decl) =
406 case decl of
407 D.Definition _ ->
408 return ()
410 D.Datatype name boundVars ctors ->
411 case diff boundVars (concatMap freeVars (concatMap snd ctors)) of
412 (_, []) ->
413 return ()
415 (_, unbound) ->
416 Result.throw region
417 (Error.UnboundTypeVarsInUnion name boundVars unbound)
419 D.TypeAlias name boundVars tipe ->
420 case diff boundVars (freeVars tipe) of
421 ([], []) ->
422 return ()
424 ([], unbound) ->
425 Result.throw region
426 (Error.UnboundTypeVarsInAlias name boundVars unbound)
428 (unused, []) ->
429 Result.throw region
430 (Error.UnusedTypeVarsInAlias name boundVars unused)
432 (unused, unbound) ->
433 Result.throw region
434 (Error.MessyTypeVarsInAlias name boundVars unused unbound)
436 D.Port _ ->
437 if isRoot then
438 return ()
440 else
441 Result.throw region Error.UnexpectedPort
443 D.Fixity _ _ _ ->
444 return ()
447diff :: [String] -> [A.Located String] -> ([String], [String])
448diff left right =
449 let
450 leftSet =
451 Set.fromList left
453 rightSet =
454 Set.fromList (map A.drop right)
455 in
456 ( Set.toList (Set.difference leftSet rightSet)
457 , Set.toList (Set.difference rightSet leftSet)
458 )
462freeVars :: Type.Raw -> [A.Located String]
463freeVars (A.A region tipe) =
464 case tipe of
465 Type.RLambda t1 t2 ->
466 freeVars t1 ++ freeVars t2
468 Type.RVar x ->
469 [A.A region x]
471 Type.RType _ ->
472 []
474 Type.RApp t ts ->
475 concatMap freeVars (t:ts)
477 Type.RRecord fields ext ->
478 maybe [] freeVars ext
479 ++ concatMap (freeVars . snd) fields