{"id":4248,"date":"2018-01-14T19:47:03","date_gmt":"2018-01-14T19:47:03","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4248"},"modified":"2018-01-14T19:47:03","modified_gmt":"2018-01-14T19:47:03","slug":"learning-prolog-really-basic-starting-point","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/learning-prolog-really-basic-starting-point\/","title":{"rendered":"Learning Prolog (really basic starting point)"},"content":{"rendered":"<p><strong>What do I need to get started?<\/strong><\/p>\n<p>I&#8217;ve decided to use <a href=\"http:\/\/www.swi-prolog.org\/\">SWI Prolog<\/a> and I&#8217;m running this on a Linux box. Why ? Somehow it felt right to use Linux for this &#8211; I don&#8217;t have a better reason than that, but either way it also runs on Windows as far as the website says.<\/p>\n<p>So after I&#8217;ve downloaded and installed the SWI Prolog package using <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nsudo apt install swi-prolog-nox\r\n<\/pre>\n<p>We can run the installed application by simply typing <em>prolog<\/em> at the command line.<\/p>\n<p><em>Note: I will refer to both the language and the command line tool as Prolog for here on.<\/em><\/p>\n<p><strong>Using Prolog<\/strong><\/p>\n<p>The best way to work with Prolog is by creating .pl files and loading into the Prolog interpreter, although we can enter facts into the Prolog interpreter if we prefer.<\/p>\n<p>Let&#8217;s begin with some basic commands<\/p>\n<p>Before we begin, we terminate commands using the dot . thus <em>help(halt).<\/em><\/p>\n<p><em>Loading a Prolog file<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;myprogam].\r\n<\/pre>\n<p>enclosing the filename of your Prolog file (excluding the .pl extension) and terminating with the . will load the file myprogram.pl.<\/p>\n<p>The response from Prolog will be <strong>true.<\/strong> basically meaning Prolog has successfully loaded the file.<\/p>\n<p>Alternatively simply execute the prolog command at the command line and supply the file there, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nprolog myprogram.pl\r\n<\/pre>\n<p><em>Exiting Prolog<\/em><\/p>\n<p>Before we look at executing queries against our Prolog program, let&#8217;s look at cleanly exiting the Prolog environment.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nhalt.\r\n<\/pre>\n<p><em>Listing a program<\/em><\/p>\n<p>After loading a program we might want to list it within the Prolog environment, we can use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlisting.\r\n<\/pre>\n<p><em>Executing queries<\/em><\/p>\n<p>Before we can execute a query we need a Prolog program, so here&#8217;s a simple one (we&#8217;ll discuss how it works later).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngender(leonard,male).\r\ngender(sheldon,male).\r\ngender(howard,male).\r\ngender(rajesh,male).\r\ngender(penny,female).\r\ngender(bernadette,female).\r\ngender(amy,female).\r\ngender(stuart,male).\r\ngender(leslie,female).\r\n<\/pre>\n<p>save this as bigbang.pl. Next from the folder where you saved this file, run swipl, load the file using [bigbang]. and the response from swipl should be true.<\/p>\n<p>Now query for all male cast members using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngender(X,male).\r\n<\/pre>\n<p>Basically we&#8217;re saying, query for those cast members of Big Bang Theory where the gender is male. We supply a capitalised variable name (in this case X, but it could be any valid variable name). Prolog returns<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX = leonard\r\n<\/pre>\n<p>if we want to view further evaluations of the query then we type a semi-colon and Prolog will then list the next male cast member until eventually outputting the last cast member (in this case X = stuart) terminated with a dot .<\/p>\n<p><strong>Basics of our source code<\/strong><\/p>\n<p>When we write something like <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngender(leonard,male).\r\n<\/pre>\n<p>we are defining a predicate <em>gender<\/em> which takes two arguments each of which must begin with lowercase text. An uppercase is used as a variable for querying. Hence<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngender(X,male).\r\n<\/pre>\n<p>is a query whereby we&#8217;re asking Prolog to supply the results for the variable X.<\/p>\n<p>The predicate show above is also known as a <em>fact<\/em> i.e. we&#8217;ve defined the fact that leanord is male. <\/p>\n<p>A Prolog program (as shown earlier) can be said to consist of multiple predicates which have one or more arguments (or clauses).<\/p>\n<p>As mentioned, variables begin with an uppercase\/capital letter or and underscore.<\/p>\n<p>Each predicate must be terminated with a .<\/p>\n<p><strong>More on syntax<\/strong><\/p>\n<p><em>Comments<\/em> <\/p>\n<p>Comments are declared in the usual C\/C++ way using \/* &#8230; *\/ or for a single line using %, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/* This is multi\r\n   line a comment *\/\r\n\r\n% This is a single line comment\r\n<\/pre>\n<p><em>if or provided that<\/em><\/p>\n<p>Let&#8217;s see some code where this is used first<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nsquare(X,Y) :- Y = X * X.\r\n\r\n@ Example usage\r\nsquare(3,Y).\r\nY = 9.\r\n<\/pre>\n<p>so in this case the square(X,Y) can be thought of as being true if Y is X * X. In this example we cannot use <em>square(X, 9)<\/em> to find values that when squared equal 9. We can, however, use <em>square(X,3*3)<\/em> which returns X = 3. <\/p>\n<p><em>Assignment<\/em><\/p>\n<p>We can assign values to variables using the standard = operator, you can also use <em>is<\/em> for evaluating arithmetic operations, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nsquare(X,Y) :- Y =X * X.\r\n\r\n@ or we could write\r\n\r\nsquare(X,Y) :- Y is X * X.\r\n<\/pre>\n<p>This new version of the square predicate will fail if we try to use <em>square(X,3*3)<\/em> as Y is not instantiated.<\/p>\n<p><em>Equality and Inequality<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX == Y\r\n<\/pre>\n<p>is true if both X and Y are identical whereas<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nX \\== Y\r\n<\/pre>\n<p>is true if X and Y are not identical.<\/p>\n<p><em>And and Or operators<\/em><\/p>\n<p>The comma is used as an AND operator, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nabs(X,Y) :- X &lt; 0, Y is -X\r\nabs(X,X) :- X &gt;= 0\r\n<\/pre>\n<p>In the first case we&#8217;re saying if X < 0 AND Y is -X then abs is true and using an overload (to use OO language) of the predicate we state that X equates to X if X >= 0.<\/p>\n<p>The semi-colon is used for OR scenarios. For example <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\noneOrTwo(X) :- X = 1 ; X = 2.\r\n<\/pre>\n<p>Okay, not a great example but basically this says, if X = 1 OR X = 2 then the result is true otherwise its false.<\/p>\n<p>I think that&#8217;s enough to a just get some very basic programs up and running. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>What do I need to get started? I&#8217;ve decided to use SWI Prolog and I&#8217;m running this on a Linux box. Why ? Somehow it felt right to use Linux for this &#8211; I don&#8217;t have a better reason than that, but either way it also runs on Windows as far as the website says. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[132],"tags":[],"class_list":["post-4248","post","type-post","status-publish","format-standard","hentry","category-prolog"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4248"}],"version-history":[{"count":16,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4248\/revisions"}],"predecessor-version":[{"id":5818,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4248\/revisions\/5818"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}