[mercury-users] Debug prints get optimized away

Maria Garcia de la Banda Maria.GarciadelaBanda at infotech.monash.edu.au
Wed Feb 22 11:36:04 AEDT 2006


> 
> > Alternatively get friendly with the debugger :-).  We generally find
> > the debugger is sufficient for finding most faults in Mercury code
> > without the need for printf style debugging.  Features of the debugger
> > you may want to look at are conditional breakpoints and the printing and
> > term browsing facilities.
> 
> The other option is to make debug prints semidet and place them in if
> tests, like this:
> 
>     ( if dbg("message") then MyVar = 0 else error("blah") ),
> 
> where dbg always succeeds and thus the else is never called. It's a bit
> verbose though; it's easier to add them if you already have lots of if
> tests and you can add them to existing conditions.

Having semidet code is a problem if one wants to insert it in a det predicate.
A different option that I have used for debugging is to create a higher order
predicate like: 

:- pred debug((pred),string,pred(state,state)).
:- mode debug(((pred) is semidet),in,(pred(di,uo) is det)) is det.
debug(Test,String,Pred):-
        ( Test -> 
            impure unsafe_perform_io(write_string(String)),
            impure unsafe_perform_io(Pred),
            impure unsafe_perform_io(nl)
        ;   true).

where the Test checks, for example, whether the debug flag is on, and the Pred
prints whatever needs to be printed.

In order for this not to be optimised away you can simply add a dummy output
argument.

Cheers,

Maria




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