"where" block local to a guard?

Brian Boutel brian@boutel.co.nz
Wed, 18 Sep 2002 14:50:42 +1200


Dr Mark H Phillips wrote:
> Thanks for the explanation!
> 
> On Tue, 2002-09-17 at 19:07, Brian Boutel wrote:
> 
>>You can't do this because where clauses are not part of the expression 
>>syntax. If they were, expressions like
>>
>>	let a=b in c where d=e
>>or
>>	if a then b else c where d=e
>>
>>whould be ambiguous, unless you adopt arbitrary rules about the 
>>prededences, and such arbitrary rules are considered a bad thing.
> 
> 
> I'm trying to see how ambiguity might arise.  Do you mean something
> like:
> 
>     let a=1 in a+a where a=3

Yes.

> 
> or have you something different in mind?
> 
> And I can't yet think of a situation where
> 
>     if a then b else c where d=e
> 
> would cause problems.
> 

The question is whether the local definition of d scopes over the whole 
conditional expression, or just over the else part. As in

	if a then d else 2*d where d==e

Is the first d the one defined in the where, or one from an enclosing 
declaration?

It's instructive to write a grammar quite abstractly, with rules like

exp <- if exp then exp else exp
      | let decls in exp
      | exp where decls
      | ...

decls <- decl | decl decls
decl <- var = exp

and then feed this into a parser generator like yacc, and look at the 
conflicts it generates. Each one has to be resolved by a shift or reduce 
decision, or possibly a choice between two reduces, but whichever is 
chosen, code written on a mistaken assumption will still compile but 
produce a semantically incorrect program.

--brian

-- 
Brian Boutel
Wellington New Zealand