Structure in Prolog : An Intro to Data Structures
Structures in Prolog are objects that have several components or we can say sub-parts but are treated as a single object. What is significance of using a structure in Prolog? What circumstances make us to think about creating a structure? Most importantly how to create a structure and retrieve data from it at the time of requirement? By the end of this article you will find the answers to all these questions. Before starting I would suggest if you have not read the previous articles of this series, kindly go through them as they will help you understand the core of prolog programming links are — article 1, article 2. Okay, with that being said let’s get started.
Why do we need structures ?
let’s suppose we want to store the data of all the employees of an organization. Typically employee’s data will have different attributes with different data types like.. there will be an employee id associated with every employee that is probably going to be of integer type, similarly we would like to store the name of the employee, the department in which the employee is working both of these are going to be of character data type lastly we would also like to store the salary of the employee which is going to be in numbers obviously.. :) Hence, it can be concluded that we need such a self defined composite data type that comprises of different individual attributes of varying data types. i.e. we wish to create something like —
Now, if some of you are able to recall C Programming we had a construct there which facilitated us to do the same known as structure.
Revisiting C Structures
Consider the same problem statement of storing the employee record the way we would use C structures to deal with the issue would be, we would create a structure named employee and then each employee whose data needs to be saved can be defined to be of the data type of that very structure. The way we would create the employee structure in C is —
Ps. I missed a semicolon at the termination :(
To store the information of different individual employees using the structure created we can now create variables of the type struct employee as —
Structures in Prolog
Prolog structures are objects that have different individual components but are referred to as a single object. Consider the same problem statement as of storing the details of an employee, those details will be usually expressed using employee id, name, department, salary but viewed as a single object. To combine different components in a single structure we chose a functor say employee. A functor in Prolog is the name to be given to the structure.
Creating Structure in Prolog
Let suppose we want to create a structure representing information of a book. Different components of the information to be stored about a book in a library would typically be.. 1. Title of the book, 2. Author of the book, 3. ISBN number etc. Our assumption is that there are currently five books in the library we need to create the structure for all 5 of them based on the 3 attributes mentioned above.
here, book is the functor i.e. the name of the structure. The functor will always be followed by parenthesis those parenthesis in turn will enclose the different individual components in this case those components are title, author and ISBN number of the book. Everything in prolog can either be an integer (numbers) or atom (anything other than numbers) so, the title of the book and the author of the book are represented as atoms. Notice that there are no white spaces between the words in both those attributes this is because, it is not legit in prolog. Therefore to separate the words in the title of the book the underscore symbol can be used. Same can be done to separate the first name and the last name of the author.
Querying a Structure
The above clauses created a knowledge base which now consists the information about five different books. What if we want to query the knowledge base. Here are different possible scenarios and the corresponding queries —
- Determine the title and author of a book given, the ISBN number of the book
In the above query two of the three attributes from the structure are left with variables X and Y only a single attribute i.e. the ISBN number is mentioned. Therefore, when this query is fired the knowledge base will be traversed for a record with the matching ISBN number and the corresponding title and author will be returned. As simple as that.
- Determine the author and ISBN number of the book wings of fire.
Quite similar to the previous query here in, the knowledge base will be traversed against the given title and the corresponding author and ISBN number.
- Display the details of all the books stored in the knowledge base.
- Display all the books written by Ruskin Bond.
What if a book had multiple authors
Now, this is so much a real world problem some books can have multiple authors. I know a few with two authors like —
Good Omens — Neil Gaiman and Terry Pratchett
Between the Lines — Jodi Picoult and Samantha van Leer
Now, how in the world we are going to store the details of such books (with multiple authors) in prolog structures. The answer is nesting of structures.
Nesting of Structures
Just like nested if-else in C,C++ programming, a structure consisting a structure is termed as a nested structure.
Creating a Nested Structure
Notice that in the above prolog clause author is further a structure consisting of two components i.e. the first author and the second author.
Nested structures can be queried the same way as structures.
That’ s all for this one. Hope it helped you. Follow for regular updates.
See you soon..
Code & Chill :)