[mercury-users] Switches with an "else" branch?

Simon Taylor stayl at cs.mu.OZ.AU
Sat Nov 10 04:49:43 AEDT 2001


On 09-Nov-2001, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I'm trying to write deterministic switches but cover many cases in one
> branch:
> 
> :- type my_type ---> one; two; three; four.
> 
> :- pred my_pred(my_type::in) is det.
> 
> my_pred(one).
> my_pred(two).
> my_pred(Else) :-
>   Else \= one, Else \= two.
> 
> The Mercury compiler doesn't infer that all cases are covered (exactly
> once) and claims:

Mode and determinism analysis do not use information from a negated
goal to infer which constructors a variable could be bound to.
>From `Else \= one', the compiler does not infer that `Else' must
be `two', `three', or `four'. This is very unlikely to change.

A similar case that comes up often is:

	my_pred(one) :- <code for one>.
	my_pred(two) :- <code for two>.
	my_pred(Else) :-
		( Else = three
		; Else = four
		),
		<code for three or four>.

The compiler doesn't handle this case yet either,
but it probably should eventually.

> Is nested if-then-else the only way to write such "switches with default"?

Yes.

Simon.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list