1 / 14

Input/Output and Debugging

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.

bcordova
Télécharger la présentation

Input/Output and Debugging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

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

  5. - 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!

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

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

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

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

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

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

  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

More Related