Question about scope of 'let' and 'where'

Graham Klyne gk@ninebynine.org
Sun, 16 Mar 2003 13:13:05 +0000


In the function body (rhs):

     let
         { a = (e1) }
     in
        (e2)
        where
             { b = f a }

Does the Haskell specification indicate that the definition of 'a' is 
in-scope for the definition of 'b'?

Practical experience using HUGS suggests the answer is no, but my intuition 
is that the answer should be yes.

I was unable to find anything in either the report or the "gentle 
introduction" that made the correct answer clear to me (which is not to say 
it's not there, just that I didn't find it).  I think the tutorial might 
benefit from a discussion of what is in-scope for where clauses.

<comment>
FWIW, my intuition was that

     e where defs

was a form of expression (like let ... in), in which some sub-expressions 
were factored out as subsidiary definitions;  i.e. that I'd expect to be 
able to replace each occurrence of a name defined by 'where' with the body 
of the corresponding definition.  Hence I'd expect the let definitions to 
be in-scope.

I now see that use of 'where' is restricted to specific contexts.  I wonder 
if such restriction is needed?  The differences between let and where in 
Haskell are something I find to be confusing.
</comment>

#g


-------------------
Graham Klyne
<GK@NineByNine.org>
PGP: 0FAA 69FF C083 000B A2E9  A131 01B9 1C7A DBCA CB5E