[Haskell-cafe] coding standard question

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Mon Jun 22 06:06:08 EDT 2009

Erik de Castro Lopo <mle+hs at mega-nerd.com> wrote:

> Vasili I. Galchin wrote:
> >  "where/let" functions use the
> > same name for function parameters as the outer function and hence
> > there is a "shadow" warning from the compiler.
> In Haskell there is an easy way around this. Variables can
> be name a, a', a'' and so on. ...
> ... its a good idea to fix these warnings.

I would _strongly_ advise not to do that.  By trying to silence the
spurious warning about shadowing, there is enormous potential to
introduce new bugs that were not there before.


  f a b = g (a+b) (b-a)
        where g a c = a*c

ghc warns that g's parameter a shadows the parameter to f.  So we
introduce a primed identifier to eliminate the warning:

  f a b = g (a+b) (b-a)
        where g a' c = a*c

Now, no warnings!  But, oops, this function does not do the same thing.
We forgot to add a prime to all occurrences of a on the right-hand-side.

Particularly in larger examples, it can be remarkably easy to miss an
occurrence of the variable whose name you are refactoring.  The key
point is that in this situation, unlike most refactorings, the compiler
_cannot_ help you find the mistake with useful error messages or

When I write code that shadows variable names, it is always deliberate.
In fact, the language's lexical rules encourage shadowing, otherwise why
have scopes at all?  I think bug-introduction by the elimination of
shadowing is much more common than bug-elimination by the same route.


More information about the Haskell-Cafe mailing list