140 likes | 158 Views
Input/Output and Debugging. How to use IO Streams How to debug programs. IO Streams. In default, both input stream is computer keyboard and output stream is screen.
E N D
Input/Output and Debugging How to use IO Streams How to debug programs
IO Streams • In default, both input stream is computer keyboard and output stream is screen. • When we want to input from or output to files, we need to open IO streams, and close them at the end. These invoke the UNIX function fopen and fclose.
Built-ins for open/close IO Streams open(FileName, Mode, Stream) Close(Stream) FileName is an atom. Mode is one of: • read - open the file for input. • write - open the file for output. The file is created if it does not already exist, the file will otherwise be truncated. • append - open the file for output. The file is created if it does not already exist, the file will otherwise be appended to.
Built-ins for open/close IO Streams An example: mk_html_file:- open(‘tmp.html’, write, S), write(S, '<HTML>'), nl(S), write(S, '<HEAD>'), nl(S), write(S, '<TITLE>tmp</TITLE>'), nl(S), write(S, '</HEAD>'), nl(S), write(S, '<BODY>'), nl(S), write(S, '<H1>a tmp file</H1>'), nl(S), write(S, '</BODY></HTML>'), 4close(S).
- Standard IO - write(Term) read(term) nl tab(N) - Files IO - write(Stream, Term) read(Stream, term) nl(Stream) tab(Stream, N) Just Add Stream Name!
Debugging How to fix syntax errors • Read error messages carefully • Common problems: missing brackets, semi-colons, full stops. • Watch out ‘here’
program([H|T):- do_something(H), program(T) --------------------------- ! Syntax error ! ] or operator expected ! in line 2 ! program ( [ H | T ! <<here>> ! ) :- do_something ( H ) , program ( T ) . ! Syntax error ! operator expected after expression ! in line 16 ! program ( [ H | T ] ) :- do_something ( H ) , program ( T ) ! <<here>> Examples of Error Messages
Debugging • You can use ?- listing(prog_name). to checking if loaded program is correct. • Existence error in user:progr/1means progr/1 is not defined • In order to track down what is wrong, use ?- trace. To turn the debug mode on (notrace to switch it off)
The Procedure Box Control Flow Model *--------------------------* Call | | Exit ----> | d(X,Y) :- o(X,Y). | ----> | | | d(X,Z) :- | <---- | o(X,Y), d(Y,Z). | <---- Fail | | Redo *--------------------------*
The code is program([H|T]):- H=1, program(T). --------------------- | ?- program([_,_,_]). no | ?- trace. % The debugger will first creep – showing everything (trace) yes ?- program([_,_,_]). 1 1 Call: program([_435,_451,_467]) ? 2 2 Call: _435=1 ? 2 2 Exit: 1=1 ? 3 2 Call: program([_451,_467]) ? 4 3 Call: _451=1 ? 4 3 Exit: 1=1 ? 5 3 Call: program([_467]) ? 6 4 Call: _467=1 ? 6 4 Exit: 1=1 ? 7 4 Call: program([]) ? 7 4 Fail: program([]) ? When the trace mode is on, we can see everything step by step
Options when trace the program ?- program([_,_,_]). • 1 Call: program([_435,_451,_467]) ? You can type the following options: RET - creep s - skip a - abort n - switch off debug g - view ancestors h - print out all options
If you don’t want to trace every step • leash(+Mode) • Leashing Mode determines the ports of invocation boxes at which you are to be prompted when you creep through your program • Mode is a list which can the following options: [call,exit,redo,fail,exception]).
try:- do1(X), do2(Y), do3(1). do1(a). do2(b). do3(c). | ?- try. no |?- leash([fail]). % Using leashing stopping at [fail] ports yes | ?- trace. % The debugger will first creep -- showing everything (trace) | ?- try. 1 1 Call: try 2 2 Call: do1(_550) 2 2 Exit: do1(a) 3 2 Call: do2(_545) 3 2 Exit: do2(b) 4 2 Call: do3(1) 4 2 Fail: do3(1) ? Example of using “leash”
Hint for course work 1 cango/4 needs to extend to cango/6 cango(X,Y, Path, Line):- cango(X,Y, [X], Path, [], Line). Why should it like this? As part of documentation, you need to explain it in your code