[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scope of 'where'.



Original-Via: uk.ac.ukc; Fri, 28 Sep 90 12:24:53 BST
From: Thomas Johnsson <johnsson>
Date: Fri, 28 Sep 90 10:37:53-0100
To: kent, haskell@cs.glasgow.ac.uk
In-Reply-To: Kent Karlsson's message of Thu, 27 Sep 90 17:54:57 +0200 <9009271555.AA02881@birk.cs.chalmers.se>
Subject: Re: Scope of 'where'.
Original-Sender: johnsson%se.chalmers.cs@mcsun.eu.net
Sender: haskell-request@cs.glasgow.ac.uk


Kent Karlsson writes:
|   Tony Davie writes:
|
|  > We are having some problems with Haskell scoping which we don't
|  > know how to get round except by extremely clumsy reprogramming.
|  > 
|  > The trouble arises when you want to declare a local object with a scope
|  > spanning several right hand sides or possibly guards. Here are two
|  > examples, the first trivial, to show the problem, and the second actually
|  > extracted from a module we are trying to develop.
|  > 
|  > f x
|  > 	|	x>=0	=1
|  > 	|	hx==1	=2+hx
|  > 			where hx = x
|  > 
|  > Here hx is not in scope in the guard. We don't see an easy way to get it
|  > in scope.
|  ...
|
|  To which Simon Peyton Jones replies:
|
|  ...
|  > To allow local definitions to span guards and several RHSs would require
|  > a new construct.  There is no technical complication (use "with" instead
|  > of "where"), but the trouble is that MOST OF THE TIME THERE IS NO
|  > DIFFERENCE; so the distinction between with and where would be a
|  > plentiful source of misunderstandings and trip-ups to the unwary.
|  ...


The straightforward solution to Tony's programming problem is to use a
case expression instead of guarded equations:

f x = case x of
      x | x  >= 0  -> 1
      x | hx == 0  -> 2+hx
      where hx = x

(I do not find this reprogramming clumsy.) In Miranda, Orwell and other
languages which do not have a case construct the need for a where
which is in scope in the guards is greater.

Kent proposes using indentation in combination with the keyword where
to achieve the desired effect:

|  I would propose to use ";where" as the new appearance of "with" (note that
|  the ";" may be a real one or (more likely) introduced by the layout rule).
|
|    There would now be no two bewilderingly similar constructs ('where'
|  and 'with' definitions), but the functionality of 'with' definitions
|  is maintained, and there is now a simple and intuitive way of indicating
|  the extended scope (just change the indentation of 'where')!
|     Let's take a program transformation example to illustrate its use
|  (using "===" for meta-equality):
|
|  f' (n+1) | (g n) = ...n...
|  ===
|  f' (n+1)                     | (g n) = ...n...
|  ===
|  f' m | (m >= fromInteger 1  &&  g n) = ...n...
|  where  n = m-fromInteger 1

This idea has a certain appeal, but the possibilities of confusion
are still there, I think. 

I don't think that this situation occurs sufficiently often to justify
a new construct in the language.

-- Thomas Johnsson