nested guards

Ron Wichers Schreur ronny@cs.kun.nl
Fri, 7 Nov 1997 17:39:39 +0100


Erik Zuurbier wrote to the Clean discussion list:

> Can anybody give me an example of 'nested guards'?

Example:

    f given sur
       | given == "Erik"
           | sur == "Zuurbier"
               =   1
               =   0
       | otherwise
           =   0


There are two things to remember.

(1) In a nested guard you always have specify the default case.

    // not allowed!
    f given sur
       | given == "Erik"
           | sur == "Zuurbier"
               =   1
       | otherwise
           =   0

    This will give the error:
	"<guard>" else part of guard missing

(2) 'otherwise' is not recognised as the default case. In fact
    otherwise is defined as 'True' in StdBool, and the compiler
    currently does not recognise that True will always succeed
    (at least not in the phase where (1) is enforced).

    // not allowed!
    f given sur
       | given == "Erik"
           | sur == "Zuurbier"
               =   1
           | otherwise
               =   0
       | otherwise
           =   0

    To work around this I write:

    f given sur
       | given == "Erik"
           | sur == "Zuurbier"
               =   1
           // otherwise
               =   0
       | otherwise
           =   0

We are considering to allow (1) and use the layout to resolve
the missing-else ambiguity. No decision has been made, however.


Cheers,

Ronny Wichers Schreur