[Haskell-cafe] Re: coding standard question

Christian Maeder Christian.Maeder at dfki.de
Mon Jun 22 08:58:53 EDT 2009


Malcolm Wallace wrote:
> Johan Tibell <johan.tibell at gmail.com> wrote:
> 
>>> Example:
>>>  f a b = g (a+b) (b-a)
>>>        where g a c = a*c

The proper way to avoid shadowing in this simple case would be to make g
global (and don't export it).

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

>>> ---->
>>>  f a b = g (a+b) (b-a)
>>>        where g a' c = a*c
>> Actually there's a warning:
>> <interactive>:1:34: Warning: Defined but not used: `a''
> 
> Clearly I simplified the example too far.  Try this, only slightly more
> complex, example instead.  Remember, the larger the example, the more
> likely you are to miss an occurrence.
> 
>      f a b = g (a+b) (b-a)
>            where g a c = a*(c-a)
> ---->
>      f a b = g (a+b) (b-a)
>            where g a' c = a'*(c-a)
> 
> 
> Perhaps I should advocate for a new warning in GHC to cover this case:
> -fwarn-mixed-scopes, which could flag the use of the unprimed a, due to
> being bound at an outer scope.

The main reason for let and where are such "mixed scopes". (And maybe
you should get a warning if the scope is not mixed, as in the initial
example.)

In any case I think, it does make sense to avoid name shadowing in the
same way as it make sense to avoid shadowing of imported (or declared)
global names by local variables (i.e. by using "pi" or "id" as variables).

Surely, it may be convenient to reuse names in local scopes, but it is
as hard to read such code as it is to replace shadowed variables
consistently by hand. (A refactoring tool could rename shadowed
variables as reliable as the compiler treats them.)

Surely there's no help against mixing up names with the same type.

Cheers Christian



More information about the Haskell-Cafe mailing list