{"id":277,"date":"2021-11-29T15:54:54","date_gmt":"2021-11-29T14:54:54","guid":{"rendered":"https:\/\/home.agh.edu.pl\/~ligeza\/?p=277"},"modified":"2022-01-06T00:07:39","modified_gmt":"2022-01-05T23:07:39","slug":"prolog","status":"publish","type":"post","link":"https:\/\/home.agh.edu.pl\/~ligeza\/courses\/prolog\/","title":{"rendered":"Prolog"},"content":{"rendered":"\n<p>To start using SWI-Prolog under Linux you need to have it installed. Fortunately, SWI-Prolog is a standard package in every reasonable Linux distribution.<\/p>\n\n\n\n<p>In Ubuntu, you can use the Synaptic Package Manager to check (if unsure) or install (if not performed yet) the SWI-Prolog.<\/p>\n\n\n\n<p>In order to start using Prolog follow the simplest recipe:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Make a directory to store your Prolog programs, for example&nbsp;<code>\/home\/username\/prolog\/<\/code><\/li><li>Create an empty text file with a name of your choice and extension&nbsp;<code>.pl<\/code>&nbsp;in this directory; for example&nbsp;<code>socrates.pl<\/code>. You can use any&nbsp;ASCII&nbsp;text editor of your preference (e.g. vi, vim, gedit, emacs).<\/li><\/ul>\n\n\n\n<p>The above steps may look as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ali@ali:~\/ALI\/PROLOG$ mkdir prolog\nali@ali:~\/ALI\/PROLOG$ cd prolog\nali@ali:~\/ALI\/PROLOG\/prolog$ gedit socrates.pl<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Edit your first program with use of the editor. The program may be as follows:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>man(plato).\nman(socrates).\n\nmortal(X):- man(X).<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Open a shell window and go to the directory where you store your Prolog programs (e.g.&nbsp;<code>\/home\/username\/prolog\/<\/code>).<\/li><li>Start the Prolog interpreter\/compiler. In most cases you just write&nbsp;<code>prolog<\/code>, or&nbsp;<code>swipl<\/code>. The prompt changes to&nbsp;<code>?-<\/code>. Disappointed? No graphical environment? Do not worry. Intelligent people quickly get used to this modest environment and appreciate the ideas behind Prolog.<\/li><li>Now you have to load your program. This can be done by typing&nbsp;<code>[programname].<\/code>&nbsp;command in the shell window. Rememebr that the name is not followed by&nbsp;<code>.pl<\/code>&nbsp;and after the closing bracket there must be a full stop. For example type&nbsp;<code>[socrates]<\/code><\/li><li>In case there are no errors you can start using the program by asking questions. You just type a predicate name (one appearing in the program) with the appropriate number of arguments in parentheses; the arguments must be separated by commas. Remember to always add full stop.<\/li><\/ul>\n\n\n\n<p>The above steps may look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ali@ali:~\/ALI\/PROLOG\/prolog$ prolog\nWelcome to SWI-Prolog (Multi-threaded, Version 5.6.14)\nCopyright (c) 1990-2006 University of Amsterdam.\nSWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to redistribute it under certain conditions.\nPlease visit http:\/\/www.swi-prolog.org for details.\n\nFor help, use ?- help(Topic). or ?- apropos(Word).\n\n?- &#91;socrates].\n% socrates compiled 0.00 sec, 864 bytes\nYes\n\n?- <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>If you expect more than one answer, use the colon (<code>;<\/code>) to produce them.<\/li><\/ul>\n\n\n\n<p>This may look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>?- man(socrates).\nYes\n\n?- man(X).\nX = plato ;\nX = socrates ;\nNo\n\n?- mortal(socrates).\nYes\n\n?- mortal(X).\nX = plato ;\nX = socrates ;\nNo\n\n?- <\/code><\/pre>\n\n\n\n<p>Names like&nbsp;<code>socrates<\/code>, i.e. ones starting with a lower case letter are constants (proper names); variable names starts with an upper case letter. Both of them are parameters of predicates (relations) such as&nbsp;<code>man<\/code>&nbsp;or&nbsp;<code>mortal<\/code>. Every&nbsp;<em>fact<\/em>&nbsp;(a simple statement) such as&nbsp;<code>man(socrates).<\/code>&nbsp;is followed by a full stop. Every clause such as&nbsp;<code>mortal(X):- man(X).<\/code>&nbsp;represents and&nbsp;<em>inference rule<\/em>&nbsp;and is also followed by a full stop.<\/p>\n\n\n\n<p>You can modify your program with the editor. After saving it always re-load it.<\/p>\n\n\n\n<p>*To stop the Prolog interpreter\/compiler type in the&nbsp;<code>halt.<\/code>&nbsp;command.<\/p>\n\n\n\n<p>To obtain help you may type in one of the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>?- help.\n    Explain the basics of the help system. \n\n?- help(topic).\n    Show help-page on the specified topic. \n\n?- apropos(string).\n    Show all topics holding `string' in their summary documentation.<\/code><\/pre>\n\n\n\n<p>Have a fun by modifying the program and asking more and more complex questions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To start using SWI-Prolog under Linux you need to have it installed. Fortunately, SWI-Prolog is a standard package in every reasonable Linux distribution. In Ubuntu, you can use the Synaptic Package Manager to check (if unsure) or install (if not performed yet) the SWI-Prolog. In order to start using Prolog follow the simplest recipe: Make &hellip; <a href=\"https:\/\/home.agh.edu.pl\/~ligeza\/courses\/prolog\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Prolog<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17],"tags":[],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-courses","category-prolog"],"_links":{"self":[{"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":6,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":339,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/posts\/277\/revisions\/339"}],"wp:attachment":[{"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/home.agh.edu.pl\/~ligeza\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}