Updated April 6, 2023
Introduction to Prolog print
The prolog print is defined as the ‘print’ is an in-built predicate to show explicit output in the prolog. Explicit means output will be clear and without any doubt. it can bind the variables and it will return all the information which we are required to show in the output. it takes the number of variables in the argument if the argument is a string then that string must be in single quotes and the string will be printed without quotes in the current output stream. it will allow showing output in the new line as well as in the next line, it uses new-line and tab operation.
Syntax:
Following is the syntax for ‘print’ predicate.
o "print('Hello World').":
The ‘print’ predicate takes an argument in the form of a variable or sentence and so it can take any number of variables, in this case of syntax the output will be the quoted sentence but without quotes.
o "print(+Stream, +Term)." :
In this case, ‘print’ can use the term as a variable and the out will be shown on the current output stream.
How print works in Prolog?
The Prolog has some in-built predicates, in which their working is internal and out of those ‘print’ is another predicate used in prolog to show the output in user understandable formats. It takes command as ‘print/1’and it is primarily used through the escape sequence which is used for formatting, it is used by ‘print_message’ to provide the message in the output stream. The definition of this predicate is similar to the definition of ‘write’, in which print predicate facilitates the term during debugging to easy understanding of the output.
Internally this predicate uses the ‘portray’ option which allows the user to implement the term which is specified by the application and print terms or variables during debugging. Portray is a dynamic predicate used in prolog. The print/2 is used when it takes terms or variables and the output of it will be printed on the current stream.
o “print(E).” : If we input as ‘print(E).’, this has a single character to the current output stream, it will be treated as a character code in this case so equivalent character code will be displayed in the output, but if we put E = ‘b’, in this case, the character b will be printed in the output which is bounded to the characters.
Input:
"print(E)."
Output:
| ?- _26
yes
| ?-
o \n: The \n operator is used with ‘print’, to get the next line in output or where we want the line, it can be between two words or two or more lines, if we have two words America and Belgium and we want to print America on one line and Belgium on another line, so we can take input as ‘print(‘America \n Belgium’).’ and then the output will be.
For example :
Input:
"print('America \n Belgium')."
Output:
| ?- America
Belgium
yes
| ?-
o \t: It is a tab operator, which is represented as ‘\t’, and it works with the ‘print’ predicate where it is needful. This operator provides the number of spaces on the output stream. When we required one space then we can use one ‘\t’ where it is required or where it is needful, if we required two spaces then we can write two ‘\t’ at the place where it is needful, in this way we can write 1 to many tab operators.
For example:
Input:
"print('\t\t America \n Belgium')."
Output:
| ?- America
Begium
yes
| ?-
o “print(x),print(y).” : The ‘print’ operator allows to use two ‘print’ predicate together, in this case, we can write the two print predicate together, if we input the value “print(x), print(y).” then it will combine variables from both together as ‘xy’.
For example:
Input:
"print(x),print(y)."
Output:
| ?- xy
yes
| ?-
o “print(‘Hello welcome to print predicate’).”: This predicate can be used in the current output stream as well as to provide the output also. When we use “print(‘Hello welcome to print predicate’).” as a statement in the current output stream then the output will be the words or sentence from that bracket but that is without quotes, which means all the variables from the bracket will be shown as it is in the output excluding the quote, single quotes are also necessary to write the number of variables in one predicate.
When we want to show some data about the program in the output for the understanding.
Note: We have to give dot(.) after every line for termination.
Example #1
:- initialization(main).
main :- print('\nWelcome to prolog print\n').
Input:
1. print('A,B').
2. print('\tABCD').
Output:
In the above example we input the value as “print(‘A, B’).”, we get output as shown in the screenshot but if we do not give single quotes to the variables in the bracket of input then it will give uncaught exception because it does not allow it. If we use input “print(‘\tABCD’).”, then in the output there is a space before ABCD, which means \t works as a tab with the print predicate in prolog.
Example #2
:- initialization(main).
main :- print('\n The Print predicate \n').
Input:
1. print(1+2+3).
2. print('Welcome').
3. print(m),print(n).
Output:
In the above program we have given input as “print(‘Welcome’).”, and we get the output string but without quotes, if we do not use quotes then it gets an error. If we put “print(1+2+3).” then the value from the bracket will get as it is also if we put “print(‘1+2+3’).” this also gives the output as 1+2+3 and print allow to write two print together for example if we input “print(m), print(n).” then it also works.
Conclusion
In the above article, we conclude that this is another built-in predicate used in prolog like ‘write’ predicate, the only difference is that ‘write’ takes terms as arguments and ‘print’ takes the number of variables in argument, in which this predicate also used to give explicit output in the current output stream.
Recommended Articles
We hope that this EDUCBA information on “Prolog print” was beneficial to you. You can view EDUCBA’s recommended articles for more information.