9.1 9. PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
1 Input/Output and Debugging  How to use IO Streams  How to debug programs  Help on coursework.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
4. PROLOG Data Objects And PROLOG Arithmetic
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
FATIH UNIVERSITY Department of Computer Engineering Input and Output Notes for Ch.6 of Bratko For CENG 421 Fall03.
Part 1 The Prolog Language Chapter 6 Input and Output
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Input and Output Notes for Ch.6 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
04/11/04 AIPP Lecture 12: I/O1 Input/Output Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 12 04/11/04.
Program Input and the Software Design Process ROBERT REAVES.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
F28PL1 Programming Languages Lecture 18: Prolog 3.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Artificial Intelligence Programming in Prolog
ifstreams and ofstreams
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Computing Fundamentals
Chapter 7 Text Input/Output Objectives
Text File Input/Output
Variables, Expressions, and IO
Getting Started with C.
Strings in Python Creating a string.
Chapter 2 part #3 C++ Input / Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to C++ Programming
Input and Output and Built_in Predicates
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 2: Introduction to C++.
Chapter 2 part #3 C++ Input / Output
ifstreams and ofstreams
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O with files. Turning lists of characters into atoms.

9.2 I//O And The Relational Model I/O does not fit into the relational model of computation. –I/O cannot be backtracked. –Theoretically could backtrack over input.  Very inefficient though. –Cannot backtrack output.  What’s said is said. PROLOG provides I/O operators as an “add on”. PROLOG I/O operators are not really predicates.

9.3 PROLOG I/O Operators PROLOG handles I/O via constructs which are essentially C++/Java style statements : get0(Ch) –Next input character = Ch. get(Ch) –Next valid input character = Ch. –A valid input character is any printable character except spaces, tabs and end of line characters. put(Ch) –Next output character = Ch. PROLOG treats characters as ordinal numbers (i.e. as ASCII/Unicode numbers). –The arguments of get0, get and put must be numbers, not characters.

9.4 PROLOG I/O Operators II nl –Next output character = end of line. read(X) –Next input term = X. write(X) –Next output term = X. Note that = means match. get0, get, put, nl, read and write cannot be backtracked in the usual way. –GNU PROLOG will backtrack over them but it cannot undo the I/O. –Some PROLOGs will not backtrack over them at all.

9.5 I/O With Characters Reading a single character : | ?- get0(Ch). a Ch = 97 yes | ?- Matching a single character : | ?- get0(97). a yes | ?- A common mistake ( ‘9’ has ASCII code 57 ) : | ?- get0(97). 97 no | ?-

9.6 I/O With Characters II Using ; (logical or) : | ?- get0(Ch) ; true. | ?- q yes | ?- get0(97) ; get0(97). | ?- qa yes | ?- First query works because of the true. Second query works because the second input character is ‘a’. get is identical to get0 except that it ignores all non-printable characters, spaces, tabs and end of line characters.

9.7 I/O With Characters III put takes a numeric argument and prints the corresponding character. | ?- put(97). a yes | ?- put(X). uncaught exception... | ?- X is , put(X). d | ?- put(97), nl, put(100). a d yes | ?- put requires its argument to be instantiated. –Same as the other arithmetic operators.

9.8 I/O With Terms PROLOG will input and output whole terms : | ?- read(X), read(Y). | ?- p(1,2). q(jim,joe). X = p(1,2) Y = q(jim,joe) yes | ?- write(q(a,b,c)), write(p(1,2,3)). q(a,b,c)p(1,2,3) yes | ?- X = 1, Y = 2, write(p(X,Y)). p(1,2) X = 1 Y = 2 yes | ?-

9.9 I/O With Lists Read characters up to a ‘.’ and compute a checksum : checkSum(Chs, N) :- get0(Ch), (Ch = 46, %. character Chs = [], N = 0 ; checkSum(NewChs, NewN), Chs = [Ch | NewChs], N is Ch + NewN ). | ?- checkSum(L, R). hello world. L = [104, ] R = 1116 | ?-

9.10 I/O With Lists II Using get : checkSum2(Chs, N) :- get(Ch), (Ch = 46, %. character Chs = [], N = 0 ; checkSum2(NewChs, NewN), Chs = [Ch | NewChs], N is Ch + NewN ). | ?- checkSum2(L, R). hello world. L = [104, ] R = 1084 | ?-

9.11 I/O With Lists III Printing a list of terms : printList([]). printList([T | Ts]) :- write(T), nl, printList(Ts). | ?- printList([hello, world]). hello world yes | ?-

9.12 I/O With Files PROLOG reads input from the standard input stream. –Normally connected to the keyboard. PROLOG writes output to the standard output stream. –Normally connected to the screen. The standard I/O streams can be redirected to files. see(fileName) : Connect input stream to file fileName. seen : Disconnect the input stream. tell(fileName) : Connect the output stream to file fileName. told : Disconnect the output stream. Similar mechanism to open and close in C++. –Voodoo in Java.

9.13 I/O With Files II see, seen, tell and told cannot be (properly) backtracked over. –GNU PROLOG backtracks over them but does not undo their effect on the file system. –Some PROLOGs will refuse to backtrack over them at all. Logical variables can be used as arguments to see and tell. –Arguments must be valid terms. The filename user means the keyboard or screen. – see(user) : connect the input stream to the keyboard. – tell(user) : connect the output stream to the screen.

9.14 Copying A File copyFile :- read(InFileName), read(OutFileName), see(InFileName), tell(OutFileName), echo, seen, told, see(user), tell(user). echo :- get0(Ch), (Ch = -1, ; put(Ch), echo ).

9.15 Copying A File II Most PROLOGs connect back to the standard streams after seen and told but it is safest not to rely on it. Not all PROLOGs return ASCII -1 when reading from an empty file. | ?- copyFile. temp. temp1. true ? (10 ms) yes | ?- The query succeeds and as a side effect copies the contents of the file temp to the file temp1.

9.16 Echoing The Keyboard To The Screen Could use echo on the standard I/O streams : | ?- echo. sljdkljhsdfljhsdf rwpwirnpfwpfhpwf true ? ; uncaught exception... | ?- Must use to send the end of file character. If we force a backtrack GNU PROLOG gives an error message. –Don’t try to backtrack I/O operations.

9.17 Turning Lists Of Characters Into Atoms name(A, Str) –Succeeds if the atom A matches with the contents of the string Str. To PROLOG a string is a list of ASCII numbers. –Goal succeeds if Str is the list of ASCII numbers of the characters which make up A. | ?- name(fred, Str). Str = [102,114,101,100] | ?- name(X,[102,114,101,100]) X = fred. | ?- name is most useful for converting text read from input into atoms. –Particularly useful with assert.

9.18 Turning Lists Of Characters Into Atoms II readAtom reads a string of characters from input and converts it into an atom. –The string is terminated by the ‘.’ character (ASCII 46 ). readAtom(A) :- readStr(Str), name(A,Str). readStr(Str) :- get0(Ch), (Ch = 46, Str = [] ; readStr(NewStr), Str = [Ch | NewStr] ).

9.19 Turning Lists Of Characters Into Atoms III | ?- readAtom(A). fred. A = fred ? yes | ?- readAtom(A), assertz(word(A)). fred. A = fred ? yes | ?- word(X). X = fred yes | ?- readAtom(A). a123->>>--<<<< aqa. A = ‘a123->>>--<<<< aqa’ yes | ?- read(A). a123->>>--<<<< aqa. uncaught exception.... | ?- readAtom is more powerful than PROLOG’s read.

9.20 Summary I/O is an “add on” to PROLOG. –Not relational. –PROLOG cannot undo I/O operations on backtracking. get0(Ch) : Next input character = Ch. get(Ch) : Next valid input character = Ch. put(Ch) : Next output character = Ch. PROLOG treats characters as ASCII/Unicode numbers. nl : Next output character = end of line. read(X) : Next input term = X. write(X) : Next output term = X.

9.21 Summary II PROLOG reads input from the standard input stream. PROLOG writes output to the standard output stream. see(fileName) : Connect input stream to file filename. seen : Disconnect input stream from file filename. tell(fileName) : Connect output stream to file filename. told : Disconnect output stream from file filename. Filename user means keyboard / screen. name(A, Str) : Succeeds if A matches with the string Str. –Can use name to convert strings into atoms. –Particularly useful with asserta and assertz.