Prolog - Inputs and Outputs
Prolog - Inputs and Outputs
In this chapter, we will see some techniques to handle inputs and outputs through prolog. We will
use some built in predicates to do these tasks, and also see file handling techniques.
Program
| ?- write(56).
56
yes
| ?- write('hello').
hello
yes
| ?- write('hello'),nl,write('world').
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 1/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
hello
world
yes
| ?- write("ABCDE")
.
[65,66,67,68,69]
yes
From the above example, we can see that the write() predicate can write the contents into the
console. We can use ’nl’ to create a new line. And from this example, it is clear that, if we want to
print some string on the console, we have to use single quotes (‘string‘). But if we use double quote
(“string”), then it will return a list of ASCII values.
Program
cube :-
write('Write a number: '),
read(Number),
process(Number).
process(stop) :- !.
process(Number) :-
C is Number * Number * Number,
write('Cube of '),write(Number),write(': '),write(C),nl, cube.
Output
| ?- [read_write].
compiling D:/TP Prolog/Sample_Codes/read_write.pl for byte code...
D:/TP Prolog/Sample_Codes/read_write.pl compiled, 9 lines read - 1226 bytes written, 12 ms
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 2/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
Program
| ?- write('hello'),tab(15),write('world').
hello world
yes
| ?- write('We'),tab(5),write('will'),tab(5),write('use'),tab(5),write('tabs').
We will use tabs
yes
| ?-
Reading/Writing Files
In this section, we will see how we can use files to read from, and write into the files. There are
some built-in predicates, that can be used to read from file and write into it.
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 3/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
Prolog Commands
| ?- told('myFile.txt').
uncaught exception: error(existence_error(procedure,told/1),top_level/0)
| ?- told("myFile.txt").
uncaught exception: error(existence_error(procedure,told/1),top_level/0)
| ?- tell('myFile.txt').
yes
| ?- tell('myFile.txt').
yes
| ?- write('Hello World').
yes
| ?- write(' Writing into a file'),tab(5),write('myFile.txt'),nl.
yes
| ?- write("Write some ASCII values").
yes
| ?- told.
yes
| ?-
Output (myFile.txt)
Hello World Writing into a file myFile.txt
[87,114,105,116,101,32,115,111,109,101,32,65,83,67,73,73,32,118,97,108,117,101,115]
Similarly, we can also read from files. Let us see some example of reading from file.
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 4/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
Output
| ?- see('sample_predicate.txt'),
read(X),
read(Y),
seen,
read(Z).
the_end.
X = end_of_file
Y = end_of_file
Z = the_end
yes
| ?-
So from this example, we can see that using the see() predicate we can read from the file. Now after
using seen command, the control transfers to the console again. So finally it takes input from
console.
Program
process_file :-
read(Line),
Line \== end_of_file, % when Line is not not end of file, call process.
process(Line).
process_file :- !. % use cut to stop backtracking
likes(lili, cat).
likes(jhon,dog).
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 5/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
domestic(dog).
domestic(cat).
Output
| ?- [process_file].
compiling D:/TP Prolog/Sample_Codes/process_file.pl for byte code...
D:/TP Prolog/Sample_Codes/process_file.pl compiled, 9 lines read - 774 bytes written, 23 m
yes
| ?- see('sample_predicate.txt'), process_file, seen.
likes(lili,cat)
likes(jhon,dog)
domestic(dog)
domestic(cat)
true ?
Manipulating characters
Using read() and write() we can read or write the value of atoms, predicates, strings, etc. Now in this
section we will see how to write single characters into the current output stream, or how to read from
current input stream. So there are some predefined predicates to do these tasks.
Program
| ?- put(97),put(98),put(99),put(100),put(101).
abcde
yes
| ?- put(97),put(66),put(99),put(100),put(101).
aBcde
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 6/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
yes
| ?-put_char('h'),put_char('e'),put_char('l'),put_char('l'),put_char('o').
hello
yes
| ?-
Program
| ?- get_char(X).
A.
X = 'A'
yes
uncaught exception: error(syntax_error('user_input:6 (char:689) expression expected')
| ?- get_code(X).
A.
X = 65
yes
uncaught exception: error(syntax_error('user_input:7 (char:14) expression expected'),
| ?-
Constructing Atoms
The atom constructing means from a list of characters, we can make one atom, or from a list of
ASCII values also we can make atoms. To do this, we have to use atom_chars() and atom_codes()
predicates. In both cases, the first argument will be one variable, and the second argument will be a
list. So atom_chars() constructs atom from characters, but atom_codes() construct atoms from
ASCII sequence.
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 7/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
Example
| ?- atom_chars(X, ['t','i','g','e','r']).
X = tiger
yes
| ?- atom_chars(A, ['t','o','m']).
A = tom
yes
| ?- atom_codes(X, [97,98,99,100,101]).
X = abcde
yes
| ?- atom_codes(A, [97,98,99]).
A = abc
yes
| ?-
Decomposing Atoms
The atom decomposing means from an atom, we can get a sequence of characters, or a sequence
ASCII codes. To do this, we have to use the same atom_chars() and atom_codes() predicates. But
one difference is that, in both cases, the first argument will be one atom, and the second argument
will be a variable. So atom_chars() decomposes atom to characters, but atom_codes() decomposes
atoms to ASCII sequence.
Example
| ?- atom_chars(tiger,X).
X = [t,i,g,e,r]
yes
| ?- atom_chars(tom,A).
A = [t,o,m]
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 8/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
yes
| ?- atom_codes(tiger,X).
X = [116,105,103,101,114]
yes
| ?- atom_codes(tom,A).
A = [116,111,109]
Program (prog1.pl)
likes(mary,cat).
likes(joy,rabbit).
likes(tim,duck).
Program (prog2.pl)
likes(suman,mouse).
likes(angshu,deer).
Output
| ?- [prog1].
compiling D:/TP Prolog/Sample_Codes/prog1.pl for byte code...
D:/TP Prolog/Sample_Codes/prog1.pl compiled, 2 lines read - 443 bytes written, 23 ms
yes
| ?- likes(joy,rabbit).
yes
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 9/10
2/22/23, 4:45 PM Prolog - Inputs and Outputs
| ?- likes(suman,mouse).
no
| ?- consult('prog2.pl').
compiling D:/TP Prolog/Sample_Codes/prog2.pl for byte code...
D:/TP Prolog/Sample_Codes/prog2.pl compiled, 1 lines read - 366 bytes written, 20 ms
warning: D:/TP Prolog/Sample_Codes/prog2.pl:1: redefining procedure likes/2
D:/TP Prolog/Sample_Codes/prog1.pl:1: previous definition
yes
| ?- likes(suman,mouse).
yes
| ?- likes(joy,rabbit).
no
| ?-
Now from this output we can understand that this is not as simple as it seems. If two files have
completely different clauses, then it will work fine. But if there are same predicates, then while we
try to consult the file, it will check the predicates from the second file, when it finds some match, it
simply deletes all of the entry of the same predicates from the local database, then load them again
from the second file.
https://www.tutorialspoint.com/prolog/prolog_inputs_and_outputs.htm 10/10