Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Input/Output and Debugging  How to use IO Streams  How to debug programs.

Similar presentations


Presentation on theme: "1 Input/Output and Debugging  How to use IO Streams  How to debug programs."— Presentation transcript:

1 1 Input/Output and Debugging  How to use IO Streams  How to debug programs

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

4 4 Built-ins for open/close IO Streams An example: mk_html_file:- open(‘tmp.html’, write, S), write(S, ' '), nl(S), write(S, ' tmp '), nl(S), write(S, ' '), nl(S), write(S, ' a tmp file '), nl(S), write(S, ' '), 4close(S).

5 5 Just Add Stream Name! - Standard IO - write(Term) read(term) nl tab(N) - Files IO - write(Stream, Term) read(Stream, term) nl(Stream) tab(Stream, N)

6 6 Debugging How to fix syntax errors Read error messages carefully Common problems: missing brackets, semi-colons, full stops. Watch out ‘here’

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

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

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


Download ppt "1 Input/Output and Debugging  How to use IO Streams  How to debug programs."

Similar presentations


Ads by Google