{"id":4254,"date":"2016-08-23T12:54:17","date_gmt":"2016-08-23T11:54:17","guid":{"rendered":"http:\/\/smart--grid.net\/?page_id=4254"},"modified":"2022-12-03T23:00:11","modified_gmt":"2022-12-03T22:00:11","slug":"programmation-logique","status":"publish","type":"page","link":"https:\/\/complex-systems-ai.com\/en\/algorithmic\/logic-programming\/","title":{"rendered":"Logic programming"},"content":{"rendered":"<div data-elementor-type=\"wp-page\" data-elementor-id=\"4254\" class=\"elementor elementor-4254\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4d98395 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4d98395\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-33 elementor-top-column elementor-element elementor-element-4ab0d0e\" data-id=\"4ab0d0e\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-511e75f elementor-align-justify elementor-widget elementor-widget-button\" data-id=\"511e75f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-button-wrapper\">\n\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm\" href=\"https:\/\/complex-systems-ai.com\/en\/algorithmic\/\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Algorithmic<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t<div class=\"elementor-column elementor-col-33 elementor-top-column elementor-element elementor-element-a8fa60f\" data-id=\"a8fa60f\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-a538674 elementor-align-justify elementor-widget elementor-widget-button\" data-id=\"a538674\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-button-wrapper\">\n\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm\" href=\"https:\/\/complex-systems-ai.com\/en\/\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Home page<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t<div class=\"elementor-column elementor-col-33 elementor-top-column elementor-element elementor-element-624dd2f\" data-id=\"624dd2f\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-c402b69 elementor-align-justify elementor-widget elementor-widget-button\" data-id=\"c402b69\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-button-wrapper\">\n\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm\" href=\"https:\/\/fr.wikipedia.org\/wiki\/Programmation_logique\" target=\"_blank\" rel=\"noopener\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Wiki<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4e82a9cc elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4e82a9cc\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-238fac2b\" data-id=\"238fac2b\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-72ab5b8c elementor-widget elementor-widget-text-editor\" data-id=\"72ab5b8c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/complex-systems-ai.com\/en\/algorithmic\/logic-programming\/#Programmation-logique\" >Logic programming<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/complex-systems-ai.com\/en\/algorithmic\/logic-programming\/#Regles-faits-et-buts\" >Rules, facts and goals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/complex-systems-ai.com\/en\/algorithmic\/logic-programming\/#Syntaxe-Prolog\" >Prolog syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/complex-systems-ai.com\/en\/algorithmic\/logic-programming\/#Ecrire-un-programme-Prolog\" >Write a Prolog program<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Programmation-logique\"><\/span>Logic programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><\/p>\n<p><\/p>\n<p>Logic programming is the declaration of a series of facts and rules of deduction. The execution of a program consists in proving a theorem. The program answers whether or not the theorem can be proved from the statements made beforehand.<\/p>\n<p><\/p>\n<p><\/p>\n<p>Logic programming is different from procedural programming (C, JAVA, etc.), the calculations are done in the form of logical proofs. We speak of declarative programming: the program describes a situation corresponding to a problem to be solved. Declarative programming is not incompatible with object structuring.<\/p>\n<p><\/p>\n<p><\/p>\n<p>The formulas expressed in Prolog are restricted to Horn clauses and first order logic. A Horn clause is a clause with at most one positive literal. These will not be explained but implicit in the explanations of the Prolog language.<\/p>\n<p><\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Regles-faits-et-buts\"><\/span>Rules, facts and goals<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 5px; background-color: #ffdcd3; border: 2px solid #ff7964; -moz-border-radius: 9px; -khtml-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px;\">If the Horn clause has one positive literal and at least one negative literal, they are <strong>rules<\/strong> :<br \/><em>qqbe x, y, z, \u00acchild (x, y) \u2228 \u00acchild (y, z) \u2228 grand_child (x, z)<\/em> which equals<br \/><em>child (x, y) \u2227 child (y, z) \u21d2 grand_child (x, z)<\/em><\/div>\n<p><\/p>\n<p><\/p>\n<p>is written in Prolog: <em>grandchild_of (X, Z): - child_of (X, Y), child_of (Y, Z) <\/em><\/p>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 5px; background-color: #ffdcd3; border: 2px solid #ff7964; -moz-border-radius: 9px; -khtml-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px;\">You can write Horn clauses without a negative literal (positive Horn clauses), but with only one positive literal. Those are<strong> facts<\/strong> : <em>child_of (&#039;Bernard&#039;, &#039;Bernadette&#039;). <\/em><\/div>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 5px; background-color: #ffdcd3; border: 2px solid #ff7964; -moz-border-radius: 9px; -khtml-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px;\">We can write Horn clauses without positive literals, (negative Horn clauses), but with only negative literals. Those are <strong>goals<\/strong> : <em>? -little_child_of (B, &#039;germaine&#039;), nephew (X, Y). <\/em><\/div>\n<p><\/p>\n<p><\/p>\n<p>A program is therefore a set of facts and rules, all in the form of Horn clauses. The Prolog interpreter uses this knowledge base to meet goals.<\/p>\n<p><\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Syntaxe-Prolog\"><\/span>Prolog syntax<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 5px; background-color: #d5edff; border: 2px solid #3c95e8; -moz-border-radius: 9px; -khtml-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px;\">\n<p>The syntax consists of:<\/p>\n<ul style=\"text-align: justify;\">\n<li><strong>Constants<\/strong> : integer or floating point numbers, booleans, identifiers and strings.<\/li>\n<li><strong>Variables<\/strong> : identified by character strings starting with a capital letter or an underscore (if unknown, ie of no interest to the user). Variable is in the mathematical sense, it always represents the same object and does not change value in the same branch of proof.<\/li>\n<li><strong>Predicate<\/strong> : identified by character strings starting with a lowercase letter. A predicate has an arity (the number of arguments it requires). Parameters are separated by a comma, equivalent to AND; the OR is a semicolon.<\/li>\n<\/ul>\n<\/div>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 5px; background-color: #d5edff; border: 2px solid #3c95e8; -moz-border-radius: 9px; -khtml-border-radius: 9px; -webkit-border-radius: 9px; border-radius: 9px;\">\n<p>Rules and facts:<\/p>\n<ul>\n<li style=\"text-align: justify;\">a fact is a predicate with constants as parameters: <em>parent (&#039;Bernard&#039;, &#039;Bernardo&#039;)<\/em><\/li>\n<li style=\"text-align: justify;\">a rule consists of a head (positive literal) and a tail (conjunction of negative literals ending with a period:<em> nephew (X, Y): - son (X, Z), brother_or_sister (Y, Z).<\/em><\/li>\n<\/ul>\n<\/div>\n<p><\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ecrire-un-programme-Prolog\"><\/span>Write a Prolog program<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><\/p>\n<p><\/p>\n<p>A program is a list of facts and rules. We group together the rules which have the same leading predicate and the facts which relate to the same predicate. The order in which the rules are declared can have an impact on the goals.<\/p>\n<p><\/p>\n<p><\/p>\n<div style=\"padding: 3px; border: 2px dotted #a5a5a5; background-color: #f6f9fa;\">\n<pre>child_of (&#039;roger&#039;, &#039;gertrude&#039;). child_of (&#039;gertrude&#039;, &#039;germaine&#039;). child_of (&#039;marcel&#039;, &#039;robert&#039;). brother_or_sister (&#039;gertrude&#039;, &#039;robert&#039;). nephew (X, Y): - son (X, Z), brother_or_sister (Y, Z). grand_child_of (X, Y): - child_of (X, Z), child_of (Z, Y).<\/pre>\n<\/div>\n<p><\/p>\n<p><\/p>\n<p>A constant (&#039;roger&#039;) covers the whole program while a variable only exists in the clause where it is found. So the variable X of nephew (X, Y) is not the same as that of grand_child_ of (X, Y). On the other hand, it is the same as in the conjunction which is associated with it.<\/p>\n<p><\/p>\n<p><\/p>\n<p>Once the programs loaded and thus the base of facts and rules constituted, it is possible to ask questions. This is called a demonstration or a question resolution. A question arises by replacing in a rule or making a constant or variable by a &#039;?&#039;. To find out all the rules of a type, write the name of the rule containing variables followed by a &#039;?&#039; : <em>human (H)? <\/em>is used to know all the humans of the base.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-9658 size-full\" src=\"https:\/\/complex-systems-ai.com\/wp-content\/uploads\/2020\/09\/Fundamental_Prolog_2_fig1.png\" alt=\"logic programming algorithm paradigm prolog\" width=\"465\" height=\"269\" title=\"\" srcset=\"https:\/\/complex-systems-ai.com\/wp-content\/uploads\/2020\/09\/Fundamental_Prolog_2_fig1.png 465w, https:\/\/complex-systems-ai.com\/wp-content\/uploads\/2020\/09\/Fundamental_Prolog_2_fig1-300x174.png 300w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><\/p>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Algorithms Home Wiki Logic Programming Logic programming is the statement of a series of facts and rules of deduction. Running a program... <\/p>","protected":false},"author":1,"featured_media":0,"parent":1062,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4254","page","type-page","status-publish","hentry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/pages\/4254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/comments?post=4254"}],"version-history":[{"count":4,"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/pages\/4254\/revisions"}],"predecessor-version":[{"id":17938,"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/pages\/4254\/revisions\/17938"}],"up":[{"embeddable":true,"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/pages\/1062"}],"wp:attachment":[{"href":"https:\/\/complex-systems-ai.com\/en\/wp-json\/wp\/v2\/media?parent=4254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}