{"id":605,"date":"2010-09-22T21:23:07","date_gmt":"2010-09-23T01:23:07","guid":{"rendered":"http:\/\/www.elblender.com\/wordpress\/?p=605"},"modified":"2010-10-06T01:30:20","modified_gmt":"2010-10-06T05:30:20","slug":"prolog","status":"publish","type":"post","link":"http:\/\/blog.acarlstein.com\/?p=605","title":{"rendered":"Prolog"},"content":{"rendered":"<p>(This document is in process, it will be\u00a0continually\u00a0updated until\u00a0completion. Please be patience).<\/p>\n<p>Right now, I am learning how to program in PROLOG (PROgramming in LOGic) in my course of programming languages at Binghamton University.\u00a0I must confess that I find it to be very different to what I thought it would looks like.<\/p>\n<p>Prolog was mainly used for\u00a0artificial\u00a0intelligence and computational linguistics (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Prolog\">http:\/\/en.wikipedia.org\/wiki\/Prolog<\/a>). I would really like to know if it is used outside of the academic environment. If anyone have any info about this let me know.<\/p>\n<p>The following article at <a href=\"http:\/\/www.kuro5hin.org\/story\/2004\/2\/25\/124713\/784\">http:\/\/www.kuro5hin.org\/story\/2004\/2\/25\/124713\/784<\/a> written by <a href=\"http:\/\/www.kuro5hin.org\/user\/tkatchev\">tkatchev<\/a> says that Prolog can be used for hacking. Even do I agree with tkatchev that is\u00a0plausible, I would love to see an actual example. Some encryption braking, etc. \u00a0Also, I agree that the way it is presented could be more effective if applied to real life uses than IA.<\/p>\n<p>At the bottom I will add a list of links related with this topic.<\/p>\n<p>Prolog is a programming language that let the programmer write a declarative program instead of a procedural program. Just to make sure we are in the same page, a declarative program is when the programmer used &#8220;truth and logical deduction&#8221; (<em><a href=\"mailto:bartak@kti.mff.cuni.cz\">Bart\u00e1k<\/a><\/em><em>). <\/em>In declarative programming, the programmer must use a\u00a0descriptive\u00a0style that let know which are the relationship between the entities (Brna).<br \/>\nIn a\u00a0procedural\u00a0program, the programmer indicate the machine what to do, step by step.<\/p>\n<p>Prolog perform the execution of the program in an tree dept-first fashion or other way to call it: First Order Predicate Logic. Later I will go more in deep and clarify this.<\/p>\n<p><strong>Let start with installing the basics:<\/strong><\/p>\n<ol>\n<li>Install a text editor. It seems that Emacs is the right editor for this\n<ol>\n<li><a href=\"http:\/\/bruda.ca\/emacs-prolog\/\">http:\/\/bruda.ca\/emacs-prolog\/<\/a><\/li>\n<li>If you have Ubuntu, you can install the emacs editor for prolog. Just go to the Ubuntu Software Center. Type Prolog in the search box and install &#8220;Emacs major mode for editing Prolog code&#8221; [prolog-el]<\/li>\n<\/ol>\n<\/li>\n<li>Install one of these prolog compiler:\n<ol>\n<li><a href=\"http:\/\/www.gnu.org\/software\/gprolog\/\">http:\/\/www.gnu.org\/software\/gprolog\/<\/a><\/li>\n<li><a href=\"http:\/\/www.swi-prolog.org\/download\/stable\">http:\/\/www.swi-prolog.org\/download\/stable<\/a> (recommended)<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Prolog works using atoms, terms, variables, facts, rules, recursion, and queries between others. Little by little we will be introducing this concepts.<\/p>\n<p><strong>Lets analyse the following examples:<\/strong><\/p>\n<p>1. pens have inside ink<\/p>\n<p style=\"padding-left: 30px;\">a. This should be written as:<\/p>\n<p style=\"padding-left: 60px;\"><em><span style=\"color: #008000;\">have_inside(pens, ink).<\/span><\/em><\/p>\n<p style=\"padding-left: 30px;\">b. Notice that pens and ink do not have the first letter in capital. Words that are start with capital letters are considerate logical variables.<\/p>\n<p style=\"padding-left: 60px;\">Logical Variable: <em>Pens<\/em><\/p>\n<p style=\"padding-left: 60px;\">No logical variable: <em>pens<\/em><\/p>\n<p style=\"padding-left: 30px;\">c. A predicate cannot be a logical variable<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #ff0000;\">Wrong:<\/span> <em>Have_inside(pens, ink).<\/em><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #00ff00;\">Correct:<\/span> <em><span style=\"color: #008000;\">have_inside(pens, ink).<\/span><\/em><\/p>\n<p style=\"padding-left: 30px;\">d. <em>pens<\/em> and <em>ink<\/em> are called <em>atoms<\/em> (or <em>constants<\/em>).<\/p>\n<p style=\"padding-left: 30px;\">e. This <em>functor<\/em> is a prefix called a <em>relationship<\/em> (or <em>predicate<\/em>).<\/p>\n<p style=\"padding-left: 30px;\">f. This <em>predicate<\/em> have a <em>arity<\/em> (number of arguments) of 2.<\/p>\n<p style=\"padding-left: 60px;\">i. A <em>predicate<\/em> with no arguments is a <em>single proposition<\/em>, a <em>predicate<\/em> with one argument is <em>unary<\/em>,\u00a0with two arguments is <em>binary<\/em>, with three arguments is <em>ternary<\/em>, &#8230;<\/p>\n<p style=\"padding-left: 30px;\">g. This is a clause therefore they must end with a period (.) and an space after or with a period(.) and a new line.<\/p>\n<p style=\"padding-left: 30px;\">h. More examples:<\/p>\n<p style=\"padding-left: 60px;\">i. pepe is bold:<\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">bold(pepe).<\/span><\/em><\/p>\n<p style=\"padding-left: 60px;\">ii. juan saw tim by the coffe shop:<\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">saw(juan, tim, coffee_shop).<\/span><\/em><\/p>\n<p>2. In the previous example, we named <em>atoms<\/em> (or\u00a0<em>constants<\/em>) and <em>logical variables<\/em><\/p>\n<p style=\"padding-left: 30px;\">a. Examples of <em>atoms<\/em>:<\/p>\n<p style=\"padding-left: 60px;\"><em>countryA, &#8211;+&#8211;, \u00a0&#8216;Hello World&#8217;<\/em> (Notice that we are using a single tilde (&#8221;) not a double tilde (&#8220;&#8221;))<\/p>\n<p>3. Difference between a <em>goal<\/em> and a <em>unit clause<\/em><\/p>\n<p style=\"padding-left: 30px;\">a. Goal:<\/p>\n<p style=\"padding-left: 60px;\"><em><span style=\"color: #008000;\">miss(maria, jose)<\/span><\/em><\/p>\n<p style=\"padding-left: 30px;\">b. Unit Clause:<\/p>\n<p style=\"padding-left: 60px;\"><em><span style=\"color: #008000;\">miss(maria, jose).<\/span><\/em><\/p>\n<p>What are the difference between both? The period at the end. A <em>goal<\/em> is called a <em>unit<\/em> while a <em>clause<\/em> is called a <em>non-unit<\/em>. More details about this later.<\/p>\n<p>4. Multi Clause: We can have more than one clause with the same name for example:<\/p>\n<p style=\"padding-left: 30px;\"><em><span style=\"color: #008000;\">square_root(1, 1).<br \/>\nsquare_root(4, 2).<br \/>\nsquare_root(9, 3).<\/span><\/em><\/p>\n<p>In this case we are saying: the square root of 1 is 1 OR the square root of 4 is 2 OR the square root of 9 is 3.<br \/>\nEvery time you use the period(.) and a newline, this is the equivalent of saying OR.<\/p>\n<p>5. Rules:\u00a0<em>Rules<\/em> are <em>non-unit<\/em> conditionals in Prolog. For example:<\/p>\n<p style=\"padding-left: 30px;\">a. If tim have a job, then tim can pay his bills<\/p>\n<p style=\"padding-left: 60px;\"><em><span style=\"color: #008000;\">can_pay_bills(tim) :- <\/span><\/em><\/p>\n<p style=\"padding-left: 180px;\"><em><span style=\"color: #008000;\">have_job(tim).<\/span><\/em><\/p>\n<p style=\"padding-left: 60px;\">i. <em>:-<\/em> is the equivalent of <em>if<\/em><\/p>\n<p style=\"padding-left: 60px;\">ii. This is a <em>non-unit clause<\/em><\/p>\n<p style=\"padding-left: 30px;\">b.Let say we want to use a rule with a variable:<\/p>\n<p style=\"padding-left: 60px;\">i. if a number is divisible by 1 AND also the number is divisible by three then the number is odd<\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">number_is_odd(X) :-<\/span><\/em><\/p>\n<p style=\"padding-left: 210px;\"><em><span style=\"color: #008000;\">number_divisible_by_one(X),<\/span><\/em><\/p>\n<p style=\"padding-left: 210px;\"><em><span style=\"color: #008000;\">number_divisible_by_three(X)<\/span><\/em><span style=\"color: #008000;\">.<\/span><\/p>\n<p style=\"padding-left: 90px;\">I. The comma (,) is the representation of the statement AND. If number_divisible_by_one is true AND number_divisible_by_three \u00a0 \u00a0 is true, then number_is_odd is true.<\/p>\n<p style=\"padding-left: 60px;\">ii. classes are cancel if the day is a holiday OR the professor is sick<\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">cancel(classes) :-<\/span><\/em><\/p>\n<p style=\"padding-left: 180px;\"><em><span style=\"color: #008000;\">day_is_holiday(classes).<\/span><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">cancel(classes) :-<\/span><\/em><\/p>\n<p style=\"padding-left: 180px;\"><em><span style=\"color: #008000;\">proffesor_is_sick(classes).<\/span><\/em><\/p>\n<p style=\"padding-left: 60px;\">iii. There is an special case in which two rules seems to be talking about the same variable but they are not:<\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">number_is_divisible_by_two(X) :-<\/span><\/em><\/p>\n<p style=\"padding-left: 300px;\"><em><span style=\"color: #008000;\">number_is_odd(X).<\/span><\/em><\/p>\n<p style=\"padding-left: 90px;\"><em><span style=\"color: #008000;\">number_is_divisible_by_two(X) :-<\/span><\/em><\/p>\n<p style=\"padding-left: 300px;\"><em><span style=\"color: #008000;\">number_is_odd(X).<\/span><\/em><\/p>\n<p style=\"padding-left: 90px;\">I. In this case it says that a number is divisible by one is the number is odd or a number is divisible by three if the number is odd. Believed or not, we are not talking about the same variable. Why? Because <span style=\"text-decoration: underline;\"><span style=\"color: #ff0000;\">&#8220;local variables cannot be overwritten with a new value&#8221;<\/span><\/span> (Paul Brna). Example:<\/p>\n<p style=\"padding-left: 120px;\">A. if we say that X = 1 and later we try to change this variable (X = 2) this will not work for Prolog<\/p>\n<p>I will try to continue this topic in another posting.<\/p>\n<p style=\"padding-left: 120px;\">\n<p style=\"padding-left: 120px;\">\n<p style=\"padding-left: 120px;\">\n<p style=\"padding-left: 30px;\">\n<p style=\"padding-left: 60px;\"><em> <\/em><\/p>\n<p style=\"padding-left: 30px;\">\n<p style=\"padding-left: 90px;\">\n<p style=\"padding-left: 90px;\">\n<p>Related Links:<\/p>\n<ol>\n<li><a href=\"http:\/\/www.learnprolognow.org\/\">http:\/\/www.learnprolognow.org\/<\/a><\/li>\n<li><a href=\"http:\/\/kti.ms.mff.cuni.cz\/~bartak\/prolog\/\">http:\/\/kti.ms.mff.cuni.cz\/~bartak\/prolog\/<\/a><\/li>\n<li><a href=\"http:\/\/cs.union.edu\/~striegnk\/learn-prolog-now\/html\/\">http:\/\/cs.union.edu\/~striegnk\/learn-prolog-now\/html\/<\/a><\/li>\n<li><a href=\"http:\/\/www.swi-prolog.org\/\">http:\/\/www.swi-prolog.org\/<\/a><\/li>\n<li><a href=\"http:\/\/www.cse.unsw.edu.au\/~billw\/prologdict.html\">http:\/\/www.cse.unsw.edu.au\/~billw\/prologdict.html<\/a><\/li>\n<li><a href=\"http:\/\/homepages.inf.ed.ac.uk\/pbrna\/prologbook\/index.html\">http:\/\/homepages.inf.ed.ac.uk\/pbrna\/prologbook\/index.html<\/a><\/li>\n<li><a href=\"http:\/\/www.doc.gold.ac.uk\/~mas02gw\/prolog_tutorial\/prologpages\/\">http:\/\/www.doc.gold.ac.uk\/~mas02gw\/prolog_tutorial\/prologpages\/<\/a><\/li>\n<\/ol>\n<p><em>Work Citied<\/em><\/p>\n<p><span style=\"font-family: 'Times New Roman', Times;\"><em><em><a href=\"mailto:bartak@kti.mff.cuni.cz\">Roman Bart\u00e1k<\/a>, <\/em><a href=\"http:\/\/kti.mff.cuni.cz\/~bartak\/prolog\/intro.html\">http:\/\/kti.mff.cuni.cz\/~bartak\/prolog\/intro.html<\/a><\/em><\/span><\/p>\n<p><span style=\"font-family: 'Times New Roman', Times;\"><em><em>Paul Brna,\u00a0<a href=\"http:\/\/homepages.inf.ed.ac.uk\/pbrna\/prologbook\/node11.html\">http:\/\/homepages.inf.ed.ac.uk\/pbrna\/prologbook\/node11.html<\/a><\/em><\/em><\/span><\/p>\n\n<script>\nvar zbPregResult = '0';\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>(This document is in process, it will be\u00a0continually\u00a0updated until\u00a0completion. Please be patience). Right now, I am learning how to program in PROLOG (PROgramming in LOGic) in my course of programming languages at Binghamton University.\u00a0I must confess that I find it to be very different to what I thought it would looks like. Prolog was mainly [&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":[1312],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-prolog"],"_links":{"self":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/605","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=605"}],"version-history":[{"count":26,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":638,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions\/638"}],"wp:attachment":[{"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.acarlstein.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}