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

Scope of 'where'.



Original-Via: uk.ac.ukc; Thu, 27 Sep 90 17:05:02 BST
From: Kent Karlsson <kent>
Date: Thu, 27 Sep 90 17:54:57 +0200
To: haskell@cs.glasgow.ac.uk
Subject: Scope of 'where'.
Original-Sender: kent%se.chalmers.cs@sunic.sunet.se
Sender: haskell-request@cs.glasgow.ac.uk

			THE SCOPE OF WHERE CLAUSES
			==========================
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.
...

  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

Note the indentation of 'where', it is aligned with f'!
(And  m  is a "new" variable, i.e. one that does not occur in 
the original clause here being transformed.)


			/kent k