{"id":720,"date":"2010-10-12T14:29:27","date_gmt":"2010-10-12T18:29:27","guid":{"rendered":"http:\/\/www.elblender.com\/wordpress\/?p=720"},"modified":"2010-10-14T09:31:22","modified_gmt":"2010-10-14T13:31:22","slug":"prolog-using-examples","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=720","title":{"rendered":"Prolog using Examples &#8211; Part 1"},"content":{"rendered":"<p>Prolog is a programming language which works around relations not functions.<br \/>\nProlog is homoiconic due the structure of its clauses and database that provide it the ability to represent itself.<br \/>\n<span style=\"text-decoration: underline;\">Note: <\/span> A language is homoiconic when the language is self-defined. This means that if we run on the interpreter a copy of itself, it should work. Another behaviour of an homoiconic language is to lets the programmer to create, change, and run the code on the fly.<br \/>\nProlog is not fully reflective, but close, Prolog seems like reflective language because\u00a0 a program that can resolve a problem by itself by just using relationships, facts, rules, structures, and a database. A fully reflective language should be able to create relationships, rules, structures, and database by itself as needed to find a solution.<\/p>\n<p>In Prolog, <em><span style=\"text-decoration: underline;\">facts<\/span><\/em> are clauses with an empty body, eg:<\/p>\n<pre><span style=\"color: #0000ff;\">is_mother_of(Maria, Matias). \/* Maria is_mother_of Matias *\/<\/span><\/pre>\n<p><em><span style=\"text-decoration: underline;\">Rules<\/span><\/em> are <em>clauses<\/em> with a head and body (head():- body), eg:<\/p>\n<pre><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">\/* A Person is_parent_of the Child if (:-) the Person is_mother_of the Child *\/\r\nis_parent_of(Person, Child) :- is_mother_of(Person, Child)<\/span>.<\/span><\/pre>\n<p><em><span style=\"text-decoration: underline;\">Predicate<\/span><\/em> are <em>clauses<\/em> using the same head with different bodies, eg:<\/p>\n<pre><span style=\"color: #0000ff;\">\/* A Person is_parent_of the Child if (:-) the Person is_mother_of the Child *\/\r\nis_parent_of(Person, Child) :- is_mother_of(Person, Child).<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* A Person is_parent_of the Child if (:-) the Person is_father_of the Child *\/\r\nis_parent_of(Person, Child) :- is_father_of(Person, Child).<\/span><\/pre>\n<p><em><span style=\"text-decoration: underline;\">Note:<\/span> Every code written before &#8216;?-&#8216; are facts and rules. Code written following &#8216;?-&#8216; are queries, and all code after &#8216;?-&#8216; are the results that prolog returns. eg:<\/em><\/p>\n<pre><span style=\"color: #0000ff;\">is_mother_of(Marina, Marcos).\u00a0\u00a0 \/* This is a fact *\/\r\nis_parent_of(Person, Child) :- is_mother_of(Person, Child). \/* This is a rule *\/\r\n?- is_parent_of(Marina, Marcos). \/* This is a query *\/\r\nTrue \/* This is a result. note that this comment will not show up *\/\r\n?- is_parent_of(Marina, Lucia). \/* This is a query *\/\r\nFalse<\/span><\/pre>\n<p>Lets talk about <em><span style=\"text-decoration: underline;\">unification.<\/span><\/em><\/p>\n<pre><span style=\"color: #0000ff;\">\/* A unified with B if and only if A and B are the same *\/\r\n?- =(A, B).\r\nA = B.\r\n<\/span>\r\n<pre><span style=\"color: #0000ff;\">\/* This is the same as =(A, B) *\/\r\n?- A = B.\r\nA = B.<\/span><\/pre>\n<p><em>Note that A = 1 + 2 will <span style=\"text-decoration: underline;\">not<\/span> give you 3 but A = 1 + 2 which means you are unifying A with 1 + 2.<br \/>\nIf you to obtain a mathemarical result you should use the following code, eg:<\/em><\/p>\n<pre><span style=\"color: #0000ff;\">?- A is 1 + 2.\r\nA = 3.<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\">The follows are some examples of unification:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">\/* a is a constant (also known as an atom) *\/\r\n\/* Since constants can unify with themselves then this is true *\/\r\n?- a = a.\r\nTrue\r\n\r\n\/* a and b are constants, but b is not the same as a, when trying to unify a with b, this will give you false *\/\r\n?- a = b.\r\nFalse\r\n\r\n\/* In case you are unifying facts with constants *\/\r\n?- foo(a, b) = foo(a, b).\r\nTrue.\r\n\r\n\/* This case fails since in both facts, at least one of the constants are nor the same *\/\r\n?- foo(a, b) = foo(a, c).\r\nFalse.\r\n\r\n?- foo(a, b) = foo(b, a).\r\nFalse.<\/span><\/pre>\n<p><em> Unification <\/em>can also being use to unify a constant with a variable (Variables start with capital letter), eg:<\/p>\n<pre><span style=\"color: #0000ff;\">?- A = a.\r\nA = a.\r\n\r\n\/* In this case for example we can unify A with c and B with d *\/\r\n?- foo(A, B) = foo(c, d).\r\nA = c,\r\nB = d.<\/span><\/pre>\n<p>Note that in Prolog, a variable cannot be modified after<\/p>\n<p>You may have notice the comma after <strong>A = c<\/strong>. In Prolog, &#8216;,&#8217; have the same meaning as AND, while a new line or &#8216;;&#8217; means OR.<\/p>\n<p><span style=\"text-decoration: underline;\">Example:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">\/* Lets say that prolog provide us with an answer *\/\r\n?- foo(a, X) = foo(a, b).\r\nX = b; \/* &lt;- if we add this semicolon ';' instead of a newline to ask another answer (OR) *\/\r\nFalse\u00a0 \/* We get a false because there is no other case *\/\r\n\r\n\/* Person is_parent_of Child if Person is_mother_of Child OR Person is_father_of Child *\/\r\nis_parent_of(Person, Child) :- is_mother_of(Person, Child).\r\nis_parent_of(Person, Child) :- is_father_of(Person, Child).\r\n\r\n\/* Person is_mother_of Child if Person is_parent_of Child AND Person is_female *\/\r\nis_mother_of(Person, Child) :- is_parent_of(Person, Child), is_female(Person).\r\n\r\n\/* Another example of AND and unification *\/\r\n\/* A is unified with B AND A is unified with constant z AND B is unified with Y *\/\r\n?- A = B, A = z, B = Y.\r\nA = z, \/* A was unified with z *\/\r\nB = z, \/* Since A = B and A = z then B = z *\/\r\nY = z. \/* Since B = A = z then Y = z *\/<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\"> Explicit versus Implicit:<\/span><\/p>\n<p><span style=\"text-decoration: underline;\">Explicit:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">relationship(Z, Z).\r\nis_related(X, Y):-\r\n\u00a0\u00a0\u00a0 relationship(X, b),\r\n\u00a0\u00a0\u00a0 relationship(a, Y).\r\n?- check(First, Second).\r\nFirst = b,\r\nSecond = a.<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\"> Implicit:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">relationship(Z, Z).\r\n?- is_related(relationship(First, b), relationship(a, Second)).\r\nFirst = a,\r\nSecond = b.<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\"> More examples of simple <em>Facts<\/em> and <em>Rules<\/em>:<\/span><\/p>\n<p><span style=\"text-decoration: underline;\">Example 1:<\/span> Lets do a modification of the game of Yahtzee (http:\/\/en.wikipedia.org\/wiki\/Yahtzee).<br \/>\nYahtzee is played with 5 dices, and depending of the combination you can obtain: Chance, Three-Of-A-Kind, Four-Of-A-Kind, Full House, Small Straight, Large Straight, and Yahtzee.<br \/>\nWe are going to do only the Yahtzee combination for this example and the user will input values from 1 to 6.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* Establish the facts and rules *\/\r\n\r\n\/* Yahtzee: All five dices show the same face. *\/\r\nyahtzee(A, A, A, A, A).\r\n?- yahtzee(5,5,5,5,5).\r\nTrue\r\n\r\n?- yahtzee(5,4,5,4,5).\r\nFalse<\/span><\/pre>\n<p><span style=\"text-decoration: underline;\"> Example 2:<\/span> Lets do some adding. This is a way that you can do adding by using successor(0)<br \/>\nas a numeral.<\/p>\n<pre><span style=\"color: #0000ff;\">\/* When 0 is the same as 0, then Y = 0 *\/\r\nadd(0, Y, Y).<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* *\/\r\nadd(successor(X), Y, successor(Z)) :- add(X, Y, Z).<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Example of behavior when used in the wrong way *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">?- add(A, B, C).\r\nA = 0,\r\nB = C;\r\nA = successor(0),\r\nC = successor(B);\r\nA = successor(successor(0)),\r\nC = successor(successor(B));\r\n... \/* This keeps on going if you use OR ';' *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Example of behavior when used in the wrong way *\/\r\n?- add(A, 0, C).\r\nA = 0,\r\nC = 0;\r\nA = successor(0),\r\nC = successor(0);\r\nA = successor(successor(0)),\r\nC = successor(successor(0));\r\n... \/* This keeps on going if you use OR ';' *\/<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Example of behavior when used in the wrong way *\/\r\n?- add(0, B, C).\r\nA = 0,\r\nB = 0.<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Example of behavior when used in the wrong way *\/\r\n?- add(0, 1, C).\r\nfalse.<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Do 1 + 1 = 2: successor(0) + successor(0) = successor(successor(0)). *\/\r\n?- add(successor(0), successor(0), X).\r\nX = successor(successor(0)).<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Do 2 + 1 = 3. *\/\r\n\/* successor(successor(0)) + successor(0) = successor(successor(successor(0))). *\/\r\n?- add(successor(successor(0)), successor(0), X).\r\nX = successor(successor(successor(0))).<\/span><\/pre>\n<p>Notice that a rule can behave different depending who you define your inputs and outputs.<\/p>\n<p><span style=\"text-decoration: underline;\">Example:<\/span><\/p>\n<pre><span style=\"color: #0000ff;\">\/* Here we find that we are doing -1 + 1 = 0 *\/\r\n?- add(successor(0), X, successor(0)).\r\nX = 0.<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Here we have 2 - 1 = 1 *\/\r\n?- add(successor(0), X, successor(successor(0))).\r\nX = successor(0).<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* While this give you X = 0, it would fail in a different case *\/\r\n?- add(X, successor(0), successor(0)).\r\nX = 0<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">\/* Here this fails *\/\r\n?- add(X, successor(successor(0)), successor(0)).\r\nfalse.<\/span><\/pre>\n<p>In the next submittion we are going to see recursion in Prolog.<\/p>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Prolog is a programming language which works around relations not functions. Prolog is homoiconic due the structure of its clauses and database that provide it the ability to represent itself. Note: A language is homoiconic when the language is self-defined. This means that if we run on the interpreter a copy of itself, it should [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,146],"tags":[175,1312,176,182],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-facts","tag-prolog","tag-rules","tag-unification"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=720"}],"version-history":[{"count":19,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/720\/revisions"}],"predecessor-version":[{"id":760,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/720\/revisions\/760"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}