{"id":2922,"date":"2022-10-06T11:31:33","date_gmt":"2022-10-06T06:01:33","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2922"},"modified":"2022-10-06T11:31:33","modified_gmt":"2022-10-06T06:01:33","slug":"why-learn-prolog","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/06\/why-learn-prolog\/","title":{"rendered":"Why Learn Prolog?"},"content":{"rendered":"<p>There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the <em><strong>popularity<\/strong><\/em> of a programming language varies over time and hence a language that was <em><strong>in demand<\/strong><\/em> a few years ago might be less popular today.<\/p>\n<p>The most common reason for someone to learn a programming language is to improve one\u2019s career prospects. Sometimes the project one is working on might require a specific language, and so one might end up learning it. Whatever be the reason, learning a new programming language is a welcome experience because it broadens one\u2019s perspective of expressing different algorithms using different dialects of languages and possibly different paradigms.<\/p>\n<p>What are my favourite languages? I am familiar with a handful of languages and have used them in various contexts over the past forty years of my software development career. I vividly remember starting with <em><strong>Fortran<\/strong><\/em> and <em><strong>PL\/1<\/strong><\/em> and then moving on to <em><strong>Pascal<\/strong><\/em>, <em><strong>Lisp<\/strong><\/em>, <em><strong>Prolog<\/strong><\/em>, and <em><strong>C++<\/strong><\/em> in that order. In my opinion, programmers should learn three languages, and <em><strong>Prolog<\/strong><\/em> is one of them (I will reveal the other two in future articles).<\/p>\n<p>In today\u2019s article, let me focus on why we should learn <em><strong>Prolog<\/strong><\/em>.<\/p>\n<p>Each of the points I am enumerating can be elaborated as a separate article (maybe even a book). However, I am intentionally keeping the discussion brief.<\/p>\n<h3>1) Easy to Learn<\/h3>\n<p>I strongly believe that <em><strong>Prolog<\/strong><\/em> is the first language one should learn. In fact, I am in favour of teaching it in High School. One of the nice things about <em><strong>Prolog<\/strong><\/em> is that It has a simple and uniform syntax with clear semantics. To get started with <em><strong>Prolog<\/strong><\/em>, you do not need a background in Computer Science. The excellent book <em><strong>\u201cThinking as Computation &#8211; A First Course\u201d<\/strong><\/em> [1] is based on a course taught at the University of Toronto for first-year undergraduate students majoring in sociology, criminology, economics, political science, etc. The interesting point to note is that <em><strong>Prolog<\/strong><\/em> was the language taught as part of the course. Another good book along similar lines is <em><strong>\u201cComputational Logic and Human Thinking: How to be Artificially Intelligent\u201d<\/strong><\/em> [2] by Robert Kowalski.<\/p>\n<h3>2) Logic-based, Declarative Language<\/h3>\n<p>Prolog is a <em><strong>declarative<\/strong><\/em> language in that it specifies <em><strong>\u201cwhat\u201d<\/strong><\/em> is computed rather than <em><strong>\u201chow\u201d<\/strong><\/em> it is computed. This is different from traditional <em><strong>procedural<\/strong><\/em> languages that express computation as a series of steps to be executed in a specific order. <em><strong>Prolog<\/strong><\/em> is also <em><strong>logic-based<\/strong><\/em>; It uses a restricted form of first-order predicate calculus, permitting relations to be expressed as <em><strong>Horn clauses<\/strong><\/em>. The <em><strong>Prolog<\/strong><\/em> engine acts as a <em><strong>theorem prover<\/strong><\/em> and attempts to prove a <em><strong>query<\/strong><\/em> as true or false, by applying <em><strong>unification<\/strong><\/em> (a form of complex pattern matching).<\/p>\n<h3>3) Built-in Nondeterminism<\/h3>\n<p>Languages such as <em><strong>C++<\/strong><\/em>,<em><strong> Python<\/strong><\/em>,<em><strong> Java<\/strong><\/em>, and <em><strong>Rust<\/strong><\/em> are <em><strong>deterministic<\/strong><\/em>, meaning that at any point in the execution of a program, the <em><strong>\u201cnext\u201d<\/strong><\/em> step is precisely defined. In contrast, <em><strong>Prolog<\/strong><\/em> is <em><strong>nondeterministic<\/strong><\/em> by nature and hence at certain points in the execution of a <em><strong>Prolog<\/strong><\/em> program, there can be <em><strong>\u201cmultiple next\u201d<\/strong><\/em> steps and each of these is valid. This happens because the same relation (predicate) may be defined multiple times. When a <em><strong>query<\/strong><\/em> is posed, the <em><strong>Prolog<\/strong><\/em> engine can therefore generate more than one solution by <em><strong>backtracking<\/strong><\/em> to earlier <em><strong>choice points<\/strong><\/em>. Such a nondeterministic behaviour allows us to use the <em><strong>\u201cgenerate and test\u201d<\/strong><\/em> strategy for solving certain problems.<\/p>\n<h3>4) Constraint Logic Programming (CLP)<\/h3>\n<p>Constraint programming is useful for solving problems that can be expressed as constraints among a set of variables. Constraint logic programming takes advantage of logic in this context. <em><strong>CLP<\/strong><\/em> is thus a natural extension to <em><strong>Prolog<\/strong><\/em>, allowing constraints to be included in the body of a predicate. There are usually dedicated constraint solvers for different domains. For example, <em><strong>CLP(FD)<\/strong><\/em> denotes constraint logic programming over <em><strong>integers<\/strong><\/em> and <em><strong>CLP(Q)<\/strong><\/em> is for <em><strong>rational numbers<\/strong><\/em>.<\/p>\n<h3>5) Definite Clause Grammars<\/h3>\n<p>Definite Clause Grammars (<em><strong>DCG<\/strong><\/em>) in <em><strong>Prolog<\/strong><\/em> provide an elegant way to model <em><strong>Phrase Structure<\/strong><\/em> rules. If needed, these can be suitably augmented with extra arguments to yield parse trees. It is also possible to introduce complex constraints to add some <em><strong>context sensitiveness<\/strong><\/em> into the grammar to take care of number agreement, etc. Likewise, an appropriate semantic representation can also be synthesized without too much work. The beauty of <em><strong>DCG<\/strong><\/em> is that the corresponding <em><strong>&#8220;<\/strong><strong>nondeterministic, top-down, left-right parser&#8221;<\/strong><\/em> comes to us for &#8220;free&#8221;!<\/p>\n<h3>6) Metaprogramming<\/h3>\n<p>Metaprogramming is an advanced technique that treats programs as data. Because of <em><strong>Prolog<\/strong><\/em>\u2019s ability to manipulate symbols, it is relatively straightforward to implement <em><strong>meta-interpreters<\/strong><\/em>, <em><strong>program transformers<\/strong><\/em>, <em><strong>compilers<\/strong><\/em>, and <em><strong>equation solvers<\/strong><\/em> in the language. Predicates such as <em><strong>\u201cgoal\u201d<\/strong><\/em> and <em><strong>\u201cclause\u201d<\/strong><\/em> (in addition to other second-order predicates) allow program introspection and dynamic evaluation.<\/p>\n<h3>7) ISO Standard<\/h3>\n<p><em><strong>Prolog<\/strong><\/em> was standardised by <em><strong>ISO<\/strong><\/em> in <em><strong>1995<\/strong><\/em>. Having a standard helps in code portability across conforming implementations. Two of the popular implementations <a href=\"https:\/\/sicstus.sics.se\/\" target=\"_blank\" rel=\"noopener\"><em><strong>SICStus Prolog<\/strong><\/em><\/a> and <a href=\"https:\/\/www.swi-prolog.org\/\" target=\"_blank\" rel=\"noopener\"><em><strong>SWI-Prolog<\/strong><\/em><\/a> support the <em><strong>ISO<\/strong><\/em> standard.<\/p>\n<h3>8) Fun to Program<\/h3>\n<p>More than anything else, once you get acquainted with <em><strong>Prolog<\/strong><\/em> and its <em><strong>declarative<\/strong><\/em> style, programming can be a lot of fun. Whether you solve puzzles, implement <a href=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/\" target=\"_blank\" rel=\"noopener\"><em><strong>games<\/strong><\/em><\/a> or model complex <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/02\/16\/automatically-converting-active-voice-to-passive-voice-and-vice-versa\/\" target=\"_blank\" rel=\"noopener\"><em><strong>phrase-structure grammars<\/strong><\/em><\/a>, you are sure to enjoy the experience of coding in <em><strong>Prolog<\/strong><\/em>.<\/p>\n<p>Is <em><strong>Prolog<\/strong><\/em> used in the real world? One company I am aware of is <em><strong>\u201c<a href=\"https:\/\/www.textrazor.com\" target=\"_blank\" rel=\"noopener\">TextRazor<\/a>\u201d<\/strong><\/em>, an API-based text analysis company. Their API allows you to define <em><strong>Prolog<\/strong><\/em> rules in order to customize text mining. This is a very elegant approach and I have written about it in an <a href=\"https:\/\/www.rangakrish.com\/index.php\/2019\/07\/21\/custom-text-analysis-using-textrazors-prolog-engine\/\" target=\"_blank\" rel=\"noopener\"><em><strong>article<\/strong><\/em><\/a>.<\/p>\n<p>I am sure <em><strong>Prolog<\/strong><\/em> in some form is being used by many more companies.<\/p>\n<p>In summary, I feel that <em><strong>Prolog<\/strong><\/em> must be learnt by all programmers as early as possible in their career. Incidentally, <em><strong>Bruce Tate<\/strong><\/em>, in his book <em><strong>\u201cSeven Languages in Seven Weeks\u201d<\/strong><\/em> [3] recommends <em><strong>Prolog<\/strong><\/em> as one of the seven languages to learn.<\/p>\n<p>Have a great week!<\/p>\n<h3 style=\"text-align: center;\">References<\/h3>\n<p>1) Hector J. Levesque, \u201cThinking as Computation: A First Course\u201d, MIT Press, 2012.<br \/>\n2) Robert Kowalski, \u201cComputational Logic and Human Thinking: How to be Artificially Intelligent\u201d, Cambridge University Press, 2011.<br \/>\n3) Bruce A. Tate, \u201cSeven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages\u201d, 2010.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the popularity of a programming language varies over time and hence a language that was in demand a few years ago [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[17,147],"tags":[336,148],"class_list":["post-2922","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-good-languages","tag-prolog"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-L8","jetpack-related-posts":[{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":2922,"position":0},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":2922,"position":1},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2935,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","url_meta":{"origin":2922,"position":2},"title":"Why Learn C++","author":"admin","date":"November 7, 2022","format":false,"excerpt":"This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":534,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/05\/22\/definite-clause-grammars-dcg-in-lisp\/","url_meta":{"origin":2922,"position":3},"title":"Definite Clause Grammars (DCG) in Lisp","author":"admin","date":"May 22, 2017","format":false,"excerpt":"Definite Clause Grammars (DCG) are an elegant formalism for specifying context free grammars, and part of their popularity is due to their support in the Prolog language. Most books on Natural Language processing usually include a brief coverage of DCGs, even though Natural languages are not context-free. Because of the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2433,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/06\/getting-to-know-visual-prolog-10\/","url_meta":{"origin":2922,"position":4},"title":"Getting to Know Visual Prolog 10","author":"admin","date":"June 6, 2021","format":false,"excerpt":"Visual Prolog\u00a0has its roots in Turbo Prolog, which was popularized by Borland in the mid 1980s. It is developed and supported by PDC A\/S, Denmark. So how is Visual Prolog different from other Prolog implementations out there, for example, SWI-Prolog or Sicstus Prolog? Here are the key differences: 1) It\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Visual Prolog Project","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2506,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/08\/21\/loop-command-in-ring-programming-language\/","url_meta":{"origin":2922,"position":5},"title":"&#8220;Loop&#8221; Command in Ring Programming Language","author":"admin","date":"August 21, 2021","format":false,"excerpt":"In my last article, I had written about how easy it is to execute Ring code from within C\/C++. Today's article takes a look at the \"Loop\" command. This command is very similar to \"continue\" in many languages such as C++, Java, Python. etc. It is used to skip the\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"\"Continue\" in Java","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/08\/javacode-300x183.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2922"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2922\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}