[mercury-users] Switch detection question
Fergus Henderson
fjh at cs.mu.OZ.AU
Wed May 22 17:01:10 AEST 2002
On 21-May-2002, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> How can I write a code like this:
...
> :- pred bar(int::in, int::out) is semidet.
> bar(X, Y) :-
> X = 5,
> Y = 2;
> (X = 3; X = 4),
> Y = 7
> .
>
> ...without confusing the switch detection algorithm?
...
> I prefer not to hear about if-then-else decistion tree or explicit
> enumeration of all the possible values:
I'm afraid those are the only alternatives.
Note that if the code in the shared case is more complicated than `Y = 7',
you can avoid duplicating it in the source code by abstracting it out
into a separate predicate or even just a separate lambda expression.
For example:
bar(X, Y) :-
( X = 5, Y = 2
; X = 3, P(Y)
; X = 4, P(Y)
),
P = (pred(YY::out) is det :- YY = 7 /* etc. */).
When optimization is enabled, the compiler will generate the same code
for this as would be generated if you had written it out in full,
with the code for the shared case duplicated:
bar(X, Y) :-
( X = 5, Y = 2
; X = 3, Y = 7 /* etc. */
; X = 4, Y = 7 /* etc. */
).
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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