Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 2 IO Streams (concept same as unix) 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 Two Levels of IO in Prolog There are two levels of IO built-ins in Prolog: 1. High level - dealing with Prolog terms; reading/writing a term at one time write(X) read(X) - X must be a term. 2. Low level - dealing with characters; reading/writing an ASCII code at one time get_code(X), put_code(X) – similar to getc/putc in C

5 5 Difference between standard IO and files IO Just add Stream Name! - Standard IO - write(Term) read(Term) get_code(X) nl tab(N) - Files IO - write(Stream, Term) read(Stream, Term) get_code(Stream,X) nl(Stream) tab(Stream, N)

6 6 An example – open a file and write something :- use_module(library(system)). % for datime mk_report:- open(‘report.txt’, write, S), datime(Year, Month, D, H, M, _), write(S, ‘Report’), nl(S), write(S, Year-Month-D), write(S, ‘ ’), write(S, H:M), nl(S), write(S, ‘I have talked to ’), userinfo(name, X), write(S, X), write(S,‘. He likes ’), userinfo(hobby, L), writelist(S, L), nl(S), close(S).

7 7 Debugging (1) How to fix syntax errors Read error messages carefully Common problems: missing brackets, semi-colons, full stops. Watch out ‘here’ which tells you where went wrong

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

9 9 Debugging (2) Run time errors: Existence error in user:progr/1 This 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 10 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 * *

11 11 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([]) ?

12 12 Options when trace the program ?- program([_,_,_]). 11 Call: program([_435,_451,_467]) ? You can type the following options: - creep (one step at the time) s - skipg - view ancestors a - abortn - switch off debug h - print out all options

13 13 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]).

14 14 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) ? (debugger stops at the first failed point)

15 15 More help on course work (1) From ‘cango’ program’s output, you need to write out a detailed direction. It is very similar to write_list(L), if you just say to east, south, etc. % give_direction(DirList, DistanceList) give_direction([],[]):- nl. give_direction([H1|T1],[H2|T2]):- write(‘go ’), write(H1), write(‘ ’), write(H2), write(‘ meters, ’), give_direction(T1,T2). For a more human-like solution, you can say ‘turn left/right’, and indicate which side the room is. You need to use same trick used in ‘cango’: add pre- direction as an extra parameter

16 More help on course work (2) % last week’s code % fail-loop chatbot:- print_welcome, conversations. conversations:- repeat, print_prompt(you), readin(S), gen_reply(S,R), print_prompt(me), write_list(R), is_quit(S). % an example of forward-loop ask_location(X):- print_prompt(me), write(‘Please tell me where you are’), nl, print_prompt(you), readin(S), cont_ask_location(X, S). cont_ask_location(X, [H|_]):- is_in_db(H), !, X=H. % done cont_ask_location(X, _):- ask_location(X). last term we gave you this program which handles conversation in a simple read/reply fashion, there is no continuation between dialogues. But, we do need some continued conversation. For example, chatbot may want to ask a few questions, it wants continually to ask until it get all answers.


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

Similar presentations


Ads by Google