@@ -1,7 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Validate (declarations) where
33
4−import Control.Monad (foldM)
4+import Control.Monad (foldM_)
55import qualified Data.Map as Map
66import qualified Data.Set as Set
77import qualified Data.Foldable as F
@@ -183,8 +183,8 @@ checkDefinition (Valid.Definition pattern body _) =
183183
184184 args ->
185185 case pattern of
186− A.A _ (Pattern.Var _) ->
187− checkArguments args
186+ A.A _ (Pattern.Var funcName) ->
187+ checkArguments funcName args
188188
189189 _ ->
190190 let
@@ -194,36 +194,20 @@ checkDefinition (Valid.Definition pattern body _) =
194194 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
195195
196196
197−
198−checkArguments :: [Pattern.Pattern R.Region var] -> Result.Result wrn Error.Error ()
199−checkArguments args =
197+checkArguments :: String -> [Pattern.RawPattern] -> Result.Result wrn Error.Error ()
198+checkArguments funcName args =
200199 let
201− vars = concatMap (Pattern.boundVarList) args
202− in
203− case firstDuplicate vars of
204− Nothing ->
205− return ()
206−
207− Just var ->
208− let
209− (A.A start _) = head args
210− (A.A end _) = last args
211− in
212− Result.throw (R.merge start end) (Error.RepeatedArgument var)
213−
200+ vars =
201+ concatMap Pattern.boundVars args
214202
215−firstDuplicate :: [String] -> Maybe String
216−firstDuplicate vars =
217− case vars of
218− [] ->
219− Nothing
220−
221− var : rest ->
222− if var `elem` rest then
223− Just var
203+ checkDups seenArgs (A.A region arg) =
204+ if Set.member arg seenArgs then
205+ Result.throw region (Error.DuplicateArgument funcName arg)
224206
225− else
226− firstDuplicate rest
207+ else
208+ return (Set.insert arg seenArgs)
209+ in
210+ foldM_ checkDups Set.empty vars
227211
228212
229213-- VALIDATE EXPRESSIONS
@@ -294,7 +278,7 @@ expression (A.A ann sourceExpression) =
294278 else
295279 return (Set.insert field seenFields)
296280 in
297− do _ <- foldM checkDups Set.empty fields
281+ do foldM_ checkDups Set.empty fields
298282 Record <$> T.traverse second fields
299283
300284 Let defs body ->