{"id":3794,"date":"2024-09-07T18:21:47","date_gmt":"2024-09-07T18:21:47","guid":{"rendered":"https:\/\/workhouse.sweetdishy.com\/?p=3794"},"modified":"2024-09-07T18:21:48","modified_gmt":"2024-09-07T18:21:48","slug":"working-with-relationships-in-prolog","status":"publish","type":"post","link":"https:\/\/workhouse.sweetdishy.com\/index.php\/2024\/09\/07\/working-with-relationships-in-prolog\/","title":{"rendered":"Working with Relationships in Prolog"},"content":{"rendered":"\n<p id=\"Arti00011751\">We can easily express relationships in Prolog. These relationships exist between objects and their properties. For example, if we say, Tina has a car, then, it means Tina is the owner of a car. Here, owner is the relationship that exist between two objects- Tina and car.<\/p>\n\n\n\n<p id=\"Arti00011752\">When we pose a query, \u201cDoes Tina own a car?\u201d, we are actually trying to find out whether this relationship exists or not. Some relationships are expressed using rules. A rule can find out about a relationship even if it is not defined explicitly as a fact.<\/p>\n\n\n\n<p id=\"Arti00011753\">Create a new file kb3.pl and write the following facts and rules in it. These are used to find whether two persons are sisters to each other or not.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p571-1.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011756\">Now, open GNU-Prolog and find out between which two individuals this relationship exists.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p571.png\" alt=\"images\" width=\"507\" height=\"226\"><\/p>\n\n\n\n<p><em>Credit<\/em>: Copyright (C) 1999-2021 Daniel Diaz<\/p>\n\n\n\n<p id=\"Arti00011760\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00011761\">You can add information about more individuals int it. You can even define more relationships. For example,<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011762\">\n<li>mother(X,Y):- parent(X,Y), female(X).<\/li>\n\n\n\n<li>brother(X,Y):- parent(Z,X), parent(Z,Y), male(X), X \\== Y.<\/li>\n\n\n\n<li>haschild(X):- parent(X,_).<\/li>\n\n\n\n<li>grandparent(X,Y):- parent(X,Z), parent(Z,Y).<\/li>\n\n\n\n<li>grandmother(X,Z):- mother(X,Y), parent(Y,Z).<\/li>\n\n\n\n<li>grandfather(X,Z):- father(X,Y), parent(Y,Z).<\/li>\n\n\n\n<li>wife(X,Y):- parent(X,Z),parent(Y,Z), female(X),male(Y).<\/li>\n\n\n\n<li>uncle(X,Z):- brother(X,Y), parent(Y,Z).<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011771\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00011773\">In Prolog we can trace the execution to see how output was obtained. To enter into the trace mode, type \u201ctrace\u201d in the Prolog console.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00011774\"><a><\/a><strong>Data Objects<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011775\">In Prolog, we can divide data objects into the following categories.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p572.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011778\">Below are some examples of different kinds of data objects:<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011779\">\n<li>Atoms \u2212<\/li>\n\n\n\n<li>Numbers \u2212 100, 1235, 2000.45<\/li>\n\n\n\n<li>Variables \u2212 X, Y, Xval, _X<\/li>\n\n\n\n<li>Structures \u2212 day(9, jun, 2017), point(10, 25)<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011784\"><strong>Atoms:<\/strong>&nbsp;Atoms are like constants. They can be any names or objects. For example, amyra, num_of_digits, c3, etc. While using atoms, we must consider the following points:<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011785\">\n<li>An atom can be a string of alphabets, digits and the underscore character, \u2018_\u2019.<\/li>\n\n\n\n<li>It should start with a lower-case letter.<\/li>\n\n\n\n<li>An atom can be a string of special characters that already have a predefined meaning. For example, :-\u2019 &lt;&#8212;&gt;, =======&gt;, \u2026, .:., ::=<\/li>\n\n\n\n<li>An atom can be a string of characters enclosed in single quotes. This is important when we have an atom that starts with a capital letter. By enclosing it in quotes, the string becomes easily distinguishable from variables. For example, \u2018Delhi\u2019, \u2018Narendra_Modi.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011790\"><strong>Numbers:<\/strong>&nbsp;They represent values that can be integers like 100, 4, -81, 2022. In Prolog, range of integers is from -16383 to 16383. Numbers can also be real numbers, but floating-point numbers are rarely used in Prolog programs as Prolog is usually used for symbolic, non-numeric computation. For example, 3.14159, -0.00029, 123.45, etc., are real numbers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00011791\"><strong>Defining Variables in Prolog<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011792\">Like every programming language, Prolog also lays some rules to define new variables. These rules states,<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011793\">\n<li>Variables are strings of letters, digits and underscore characters.<\/li>\n\n\n\n<li>They start with an upper-case letter or an underscore character.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011796\"><a><\/a><em>Examples of valid names of variables are<\/em>&nbsp;X, Val, Student_name, Shoppinglist, _a50, _39.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00011797\"><strong>Anonymous Variables in Prolog<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011798\">Anonymous variables have no names. They are written using a single underscore character \u2018_\u2019. And each anonymous variable is treated as different. They are not same. These variables are very useful in certain situations. For example, if we have facts:<\/p>\n\n\n\n<p id=\"Arti00011799\">\u201cJohn plays cricket\u201d and \u201cShivansh plays tennis\u201d, then we can easily find out who plays cricket using an ordinary variable. But, if the task is to find whether anyone plays cricket. Here, the name of the person who plays cricket is not required, so in such a case we must use anonymous variables.<\/p>\n\n\n\n<p id=\"Arti00011800\">In general, anonymous variables are used when there is a need to use a variable without knowing its value. Look at the code given below that demonstrates the use of anonymous variables. Here, we have used knowledge base 1 to find whether we have any student in the KB. Note that the console replies only \u2018yes\u2019 or \u2018no\u2019, I does not show names (or values) of the students.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p573-1.png\" alt=\"images\" width=\"539\" height=\"216\"><\/p>\n\n\n\n<p><em>Credit<\/em>: Copyright (C) 1999-2021 Daniel Diaz<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00011804\"><strong>Comparison Operators<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011805\">Comparison operators are used to compare two equations or states. Some comparison operators are listed in Table.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p573-2.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011808\">Let us try these operators in GNU-Prolog now.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p574-1.png\" alt=\"images\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We can easily express relationships in Prolog. These relationships exist between objects and their properties. For example, if we say, Tina has a car, then, it means Tina is the owner of a car. Here, owner is the relationship that exist between two objects- Tina and car. When we pose a query, \u201cDoes Tina own [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3341,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[464],"tags":[],"class_list":["post-3794","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-language-prolog"],"jetpack_featured_media_url":"https:\/\/workhouse.sweetdishy.com\/wp-content\/uploads\/2024\/08\/programming.png","_links":{"self":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/comments?post=3794"}],"version-history":[{"count":1,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3794\/revisions"}],"predecessor-version":[{"id":3795,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3794\/revisions\/3795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media\/3341"}],"wp:attachment":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media?parent=3794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/categories?post=3794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/tags?post=3794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}