[mercury-users] Conditional compilation

Ralph Becket rafe at cs.mu.OZ.AU
Tue Aug 16 14:57:29 AEST 2005

Peter Hawkins, Tuesday, 16 August 2005:
> Hi...
> Is there a way of having conditionally compiled debugging code in Mercury?
> I tried this:
> :- pred debugging is semidet.
> debugging :- semidet_fail.
> or
> debugging :- semidet_succeed.
> main(!IO) :-
>    (if debugging then
>       do stuff
>     else
>       true
>    )
> Unfortunately the mercury compiler doesn't optimize away the 
> if-then-else, even if I add a :- pragma inline to the debugging 
> predicate. The only way to avoid a runtime slowdown is to declare the 
> debugging predicate like this:
> :- pred debugging is failure.
> or
> :- pred debugging is det.
> but this leads to a compiler warning whenever you actually use it.
> Does anyone have any suggestions as to how I can have debugging code 
> that I can turn off?

C code is inlined by default, so you should do the following:

:- pragma foreign_proc("C",
	[will_not_call_mercury, promise_pure],

then the C compiler will optimize away the if-then-else test and
non-taken branch.  You can then select debugging by compiling with
`--cflags -DWITH_DEBUGGING' (this is how array bounds checking is
handled in array.m).

-- Ralph
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