[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PrologDebug

(Prolog) Debug, to debug, debugging

Debugging is the process of removing runtime errors. This process can be aided by a debugger.

Typical predicates used are:
  •  ?trace and  ?notrace
  •  ?spy and  ?nospy

Example

The code below has a simulated error. Using a good debugger with the function 'Trace into' you can see that the call to doIt(5) checks doIt(1), doIt(2) and then ends at doIt(_).

PREDICATES
  nondeterm doIt(integer)
  
CLAUSES
  doIt(1) :-
    write("1"), nl,
    doit(5).
    
  doIt(2) :-
    write("2"), nl.
  doIt(3) :-
    write("3"), nl.
  doIt(4) :-
    write("4"), nl.
    
  doIt(_) :-
    write("ERROR"), nl.
    
   GOAL
     doIt(1).


Debugging function

% The clause 'trace' is a debugging function:
% * trace(integer DebugLevel, string Debugmessage)- (i,i)
%     * DebugLevel: only when below a certain value,
%       the DebugMessage will be stored
%     * DebugMessage: The message that will be appended to the file
%       'Trace.txt'
global domains
  FILE = myDebugFile
predicates
  trace(integer,String)- (i,i)
    
clauses
  trace(DebugLevel,DebugMessage) IF 
    DebugLevel < 50, % 50 is set as the default debug level
    openappend(myDebugFile,"Trace.txt"),
    writedevice(myDebugFile),
    write(DebugMessage),
    write("\n"),
    writedevice(stdout),
    closefile(myDebugFile),
    !.
  trace(_,_) IF
    % No problem, DebugLevel is too high
    !.
    
goal
  trace(1,"I am written to file."),
  trace(100,"I am not written to file.").


'Debug' links



last edited (December 6, 2006) by bilderbikkel, Number of views: 1609, Current Rev: 7 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.