{"id":3790,"date":"2024-09-07T18:19:58","date_gmt":"2024-09-07T18:19:58","guid":{"rendered":"https:\/\/workhouse.sweetdishy.com\/?p=3790"},"modified":"2024-09-07T18:19:59","modified_gmt":"2024-09-07T18:19:59","slug":"creating-facts-and-rules","status":"publish","type":"post","link":"https:\/\/workhouse.sweetdishy.com\/index.php\/2024\/09\/07\/creating-facts-and-rules\/","title":{"rendered":"Creating Facts and Rules"},"content":{"rendered":"\n<p id=\"Arti00011669\">In this section, we will focus on creating the knowledge base for problem solving. This KB is the fundamental part of Logic Programming and contains facts, rules and questions\/queries.<\/p>\n\n\n\n<p id=\"Arti00011670\">Facts represent an explicit relationship between objects, and properties. They are unconditionally true in nature. For example,<\/p>\n\n\n\n<p id=\"Arti00011671\">&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Tom is a dog<\/td><td>Ria loves to eat icecream<\/td><td>Jia stood first.<\/td><\/tr><tr><td>Stars twinkle<\/td><td>Sun is hot<\/td><td>Pratyusha plays cricket<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p id=\"Arti00011681\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00011682\">To write facts in Prolog, we must remember the following points:<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011683\">\n<li>Names of properties\/relationships begin with lower case letters.<\/li>\n\n\n\n<li>Name of the relationship is written as the first term.<\/li>\n\n\n\n<li>Objects appear as comma-separated arguments within parentheses.<\/li>\n\n\n\n<li>A fact must be ended with a period \u201c.\u201d.<\/li>\n\n\n\n<li>Objects begin with lower case letters. They can also start with digits, and can be strings of characters enclosed in quotes, e.g., color(hair, \u2018black\u2019).<\/li>\n\n\n\n<li>Phone_no(anuj, 1234567890). is a predicate or clause.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011690\">The syntax for defining facts is,<\/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_p568-1.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011693\">For example,<\/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_p568-2.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011696\">Rules define an implicit relationship between objects. It considers facts to be conditionally true. That is, when an associated condition is true, then the predicate is also true. For example,<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00011697\">\n<li>Mina is happy if she sings.<\/li>\n\n\n\n<li>Krish is hungry if he is eating stale food.<\/li>\n\n\n\n<li>Parul and Disha are friends if both of them go for dancing classes.<\/li>\n\n\n\n<li>Will go to play if it is a holiday and friend is free.<\/li>\n<\/ul>\n\n\n\n<p id=\"Arti00011702\">In case of a rule, if the right-hand side is true, then the left-hand side is also true. Technically, it is represented with the symbol (:- ) and is pronounced as \u201cIf\u201d, or \u201cis implied by\u201d. This is also known as neck symbol. In a rule, the LHS of this symbol is called the Head. Correspondingly, RHS is called Body.<\/p>\n\n\n\n<p id=\"Arti00011703\">To represent conjunction in a rule, we use comma (,) and to represent disjunction, semicolon is used.<\/p>\n\n\n\n<p id=\"Arti00011704\">Syntax of writing a rule is,<\/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_p568-3.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011707\">&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>If we have a rule as, P:- Q;R.<br>Then, it can be written as,<br>P:- Q.<br>P:- R.<\/td><td>If a rule is,<br>P:- Q, R; S, T, U.<br>Then, it can also be written as,<br>P:- Q, R.<br>P:- S, T, U.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p id=\"Arti00011712\"><a><\/a><strong>Example<\/strong><\/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_p569-1.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00011715\">Queries are some questions on the relationships between objects and object properties. Logic programming language finds answers to these questions and returns them. For example, as per our facts and rules, some queries can be,<\/p>\n\n\n\n<p id=\"Arti00011716\">&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Is tom a dog?<\/td><td>Does riya loves to eat iceccream?<\/td><\/tr><tr><td>Is mina happy?<\/td><td>Will Manav play guitar?<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00011724\"><strong>Creating Knowledge Base 1 Containing Facts in Prolog<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011725\">Open a file in any text editor (like WordPad, NotePad, etc.) and write the facts given below in it. Save this file with any name and .pl extension. We have saved this file as kb1.pl in the SampleDocs folder.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"Arti00011726\">student(ben).<\/p>\n\n\n\n<p id=\"Arti00011727\">student(kiara).<\/p>\n\n\n\n<p id=\"Arti00011728\">student(madhav).<\/p>\n\n\n\n<p id=\"Arti00011729\">studies_AI(kiara).<\/p>\n<\/blockquote>\n\n\n\n<p id=\"Arti00011730\">Now, in the GNU-Prolog, we will ask queries and get a reply as shown in the figure given below. Note that every time we start GNU-Prolog, we have to change directory to set it where our files are stored. Then we have to compile our knowledge base before executing the queries.<\/p>\n\n\n\n<p id=\"Arti00011731\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00011733\">Names of persons, filenames, objects and relations are all written in lowercase letters, because in Prolog, a string starting with uppercase letter indicates a variable.<\/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_p569.png\" alt=\"images\" width=\"441\" height=\"243\"><\/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=\"Arti00011737\"><strong>Creating Knowledge Base 2 Containing Facts and Rules in Prolog<\/strong><\/h4>\n\n\n\n<p id=\"Arti00011738\">Open a new file in text editor and write the following facts and rules in it. Save this file as kb2.pl in SampleDocs folder.<\/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_p570.png\" alt=\"images\"\/><\/figure>\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_p570-1.png\" alt=\"images\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this section, we will focus on creating the knowledge base for problem solving. This KB is the fundamental part of Logic Programming and contains facts, rules and questions\/queries. Facts represent an explicit relationship between objects, and properties. They are unconditionally true in nature. For example, &nbsp; Tom is a dog Ria loves to eat [&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-3790","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\/3790","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=3790"}],"version-history":[{"count":1,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3790\/revisions"}],"predecessor-version":[{"id":3791,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3790\/revisions\/3791"}],"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=3790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/categories?post=3790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/tags?post=3790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}