- Prolog - Discussion
- Prolog - Useful Resources
- Prolog - Quick Guide
- Monkey and Banana Problem
- Prolog - Linked Lists
- Towers of Hanoi Problem
- Prolog - Examples of Cuts
- Prolog - Basic Programs
- Tree Data Structure (Case Study)
- Prolog - Built-In Predicates
- Prolog - Inputs and Outputs
- Prolog - Different and Not
- Prolog - Backtracking
- Recursion and Structures
- Prolog - Lists
- Conjunctions & Disjunctions
- Loop & Decision Making
- Prolog - Operators
- Prolog - Data Objects
- Prolog - Relations
- Prolog - Basics
- Prolog - Hello World
- Prolog - Environment Setup
- Prolog - Introduction
- Prolog - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Prolog - Data Objects
In this chapter, we will learn data objects in Prolog. They can be spanided into few different categories as shown below −
![Data Objects](/prolog/images/data_objects.jpg)
Below are some examples of different kinds of data objects −
Atoms − tom, pat, x100, x_45
Numbers − 100, 1235, 2000.45
Variables − X, Y, Xval, _X
Structures − day(9, jun, 2017), point(10, 25)
Atoms and Variables
In this section, we will discuss the atoms, numbers and the variables of Prolog.
Atoms
Atoms are one variation of constants. They can be any names or objects. There are few rules that should be followed when we are trying to use Atoms as given below −
Strings of letters, digits and the underscore character, ‘_ , starting with a lower-case letter. For example −
azahar
b59
b_59
b_59AB
b_x25
antara_sarkar
Strings of special characters
We have to keep in mind that when using atoms of this form, some care is necessary as some strings of special characters already have a predefined meaning; for example :- .
<--->
=======>
...
.:.
::=
Strings of characters enclosed in single quotes.
This is useful if we want to have an atom that starts with a capital letter. By enclosing it in quotes, we make it distinguishable from variables −
‘Rubai
‘Arindam_Chatterjee
‘Sumit Mitra
Numbers
Another variation of constants is the Numbers. So integer numbers can be represented as 100, 4, -81, 1202. In Prolog, the normal range of integers is from -16383 to 16383.
Prolog also supports real numbers, but normally the use-case of floating point number is very less in Prolog programs, because Prolog is for symbopc, non-numeric computation. The treatment of real numbers depends on the implementation of Prolog. Example of real numbers are 3.14159, -0.00062, 450.18, etc.
The variables come under the Simple Objects section. Variables can be used in many such cases in our Prolog program, that we have seen earper. So there are some rules of defining variables in Prolog.
We can define Prolog variables, such that variables are strings of letters, digits and underscore characters. They start with an upper-case letter or an underscore character. Some examples of Variables are −
X
Sum
Memer_name
Student_pst
Shoppingpst
_a50
_15
Anonymous Variables in Prolog
Anonymous variables have no names. The anonymous variables in prolog is written by a single underscore character ‘_’. And one important thing is that each inspanidual anonymous variable is treated as different. They are not same.
Now the question is, where should we use these anonymous variables?
Suppose in our knowledge base we have some facts — “jim hates tom”, “pat hates bob”. So if tom wants to find out who hates him, then he can use variables. However, if he wants to check whether there is someone who hates him, we can use anonymous variables. So when we want to use the variable, but do not want to reveal the value of the variable, then we can use anonymous variables.
So let us see its practical implementation −
Knowledge Base (var_anonymous.pl)
hates(jim,tom). hates(pat,bob). hates(dog,fox). hates(peter,tom).
Output
| ?- [var_anonymous]. compipng D:/TP Prolog/Sample_Codes/var_anonymous.pl for byte code... D:/TP Prolog/Sample_Codes/var_anonymous.pl compiled, 3 pnes read - 536 bytes written, 16 ms yes | ?- hates(X,tom). X = jim ? ; X = peter yes | ?- hates(_,tom). true ? ; (16 ms) yes | ?- hates(_,pat). no | ?- hates(_,fox). true ? ; no | ?-Advertisements