Presentation on theme: "1 Input/Output and Debugging How to use IO Streams How to debug programs."— Presentation transcript:
1 Input/Output and Debugging How to use IO Streams How to debug programs
2 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.
3 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.
6 Debugging How to fix syntax errors Read error messages carefully Common problems: missing brackets, semi-colons, full stops. Watch out ‘here’
7 Examples of Error Messages program([H|T):- do_something(H), program(T) ! Syntax error ! ] or operator expected ! in line 2 ! program ( [ H | T ! > ! ) :- do_something ( H ), program ( T ). ! Syntax error ! operator expected after expression ! in line 16 ! program ( [ H | T ] ) :- do_something ( H ), program ( T ) ! >
8 Debugging You can use ?- listing(prog_name). to checking if loaded program is correct. Existence error in user:progr/1 means 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)
10 When the trace mode is on, we can see everything step by step 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() ?
11 Options when trace the program ?- program([_,_,_]). 11 Call: program([_435,_451,_467]) ? You can type the following options: RET - creeps - skip a - abortn - switch off debug g - view ancestors h - print out all options
12 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]).
13 Example of using “leash” 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) ?
14 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