<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.visual-prolog.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jonas+Sonn+J%C3%B8rgensen</id>
	<title>wiki.visual-prolog.com - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.visual-prolog.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jonas+Sonn+J%C3%B8rgensen"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Special:Contributions/Jonas_Sonn_J%C3%B8rgensen"/>
	<updated>2026-04-29T18:04:34Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4716</id>
		<title>Lessons/Succeed, fail and backtrack - part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4716"/>
		<updated>2020-02-05T10:50:24Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;So why is it a logical programming language? And why do we call it predicates and clauses?&lt;br /&gt;
&lt;br /&gt;
Logic is a formalized approach to&lt;br /&gt;
&lt;br /&gt;
* propositions like &amp;quot;John is the father of Bill&amp;quot;&lt;br /&gt;
* rules like &amp;quot;C is the grandfather of A if C is the father of B and B is the father of A&amp;quot;&lt;br /&gt;
* questions like &amp;quot;Who is the father of John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog can formalize logical entities.  The Visual Prolog compiler has a number of strict validation checks that will help you write the programs you intend.  But to keep things simple we will &amp;quot;work around&amp;quot; those checks in this lesson.  To do that work around you should update enter the following code in the console project:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|In a console project update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and add a &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        foreach runGoal() do&lt;br /&gt;
        end foreach.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    runGoal : () nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    runGoal().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Compared to what we have see so far you will notice the token &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; in the declaration of &amp;lt;vp&amp;gt;run2&amp;lt;/vp&amp;gt; and you will also get a warning about &amp;#039;&amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt;&amp;#039;.  We have inserted &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; to get around some compiler errors that we would later get, and instead we now get some warnings.  For now we will just ignore these warnings and in the sequel of this lesson you should just accept to write &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; and also &amp;lt;vp&amp;gt;anyflow&amp;lt;/vp&amp;gt; where told.&lt;br /&gt;
&lt;br /&gt;
Back to the main subject.  Let us start with the proposition &amp;quot;John is the father of Bill&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Here we have two &amp;quot;things&amp;quot;: John and Bill, and a &amp;quot;relation&amp;quot; between these, namely that one is the father of the other.&lt;br /&gt;
In Visual Prolog I can formalize this proposition in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
father is a predicate/relation taking two arguments, where the second is the father of the first.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;I &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;have chosen that the &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; person should be the father of the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I might as well have chosen it the other way around:  The order of the arguments is the choice of the &amp;quot;designer&amp;quot; of the formalization.&lt;br /&gt;
However, once you have chosen, you must be consistent.&lt;br /&gt;
So in &amp;#039;&amp;#039;&amp;#039;my&amp;#039;&amp;#039;&amp;#039; formalization the father must &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; be the second person.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
clauses&lt;br /&gt;
    father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have a (tiny) &amp;quot;knowledge base&amp;quot;, we will try to find the answer to some questions.  Let&amp;#039;s first see the &amp;quot;magic&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.&lt;br /&gt;
&lt;br /&gt;
Then add one more clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the proejct again.&lt;br /&gt;
&lt;br /&gt;
Finally, modify add one more clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)),&lt;br /&gt;
		stdio::write(&amp;quot;No, John is the not the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project again.}}&lt;br /&gt;
&lt;br /&gt;
To understand what is going on we need to understand the concept of &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;succed&amp;#039;&amp;#039;&amp;#039;, because there is a clause that matches this call.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;, because the clauses for the father predicate cannot match this call.  &lt;br /&gt;
The call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; because &amp;lt;vp&amp;gt;not&amp;lt;/vp&amp;gt; is  special predicate that succeeds if its argument fails and fails if the argument succeeds.&lt;br /&gt;
&lt;br /&gt;
Using the words &amp;#039;&amp;#039;succeed&amp;#039;&amp;#039; and &amp;#039;&amp;#039;fail&amp;#039; we can now explain the clauses.&lt;br /&gt;
&lt;br /&gt;
Let us first consider each of the three clauses individually (as if they were the only one)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first clause &amp;lt;vp&amp;gt;runGoal() :- father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;), stdio::write(&amp;quot;Yes, ...&amp;quot;).&amp;lt;/vp&amp;gt; works like this:  &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; succeeds &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt; succeeds, otherwise it fails.&lt;br /&gt;
&lt;br /&gt;
So when we call &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and &amp;#039;&amp;#039;if&amp;#039;&amp;#039; that succeeds (which we know it will) we will call &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; is a predicate that succeds no matter which arguments it is called with and in addition it will also write the arguments to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
So all in all, the first clause will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and also write &amp;lt;vp&amp;gt;&amp;quot;Yes, ...&amp;quot;&amp;lt;/vp&amp;gt; to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
In the second clause we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and since that call fails, the entire clause fails, and therefore we will &amp;#039;&amp;#039;not&amp;#039;&amp;#039; call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
In the third clause we first call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt;.  As mentioned above this call succeeds and therefore we will also call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
Now that we know how each clause works individually, lets look at the effect of having them all three.  This is actually the most unique feature of logical programming languages, but probably also the most tricky one to understand.  The idea is based on the logical &amp;quot;or&amp;quot;: to &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; will succeed if the first clause &amp;quot;or&amp;quot; the second clause &amp;quot;or&amp;quot; the third clause succeeds.  It is true, but it is not sufficiently detailed when actually programming in Visual Prolog.  Here I will provide a much more detailed and operative description of how it works.&lt;br /&gt;
&lt;br /&gt;
When we invoke &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;, which has three clauses.  We will first create a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; point to the two other clauses, and then excecute the first clause.&lt;br /&gt;
&lt;br /&gt;
A backtrack point is a &amp;quot;thing&amp;quot; that describes a place in our program where we had two alternative ways to fulfill our goal.  We do one thing but make a note about the other possibility.  If we at some point fail, we backtrack to the place we noted in the most recent backtrack point we created and follow the alternative way we noted in the backtrack point.  (That backtrack point is then used and thus discarded).&lt;br /&gt;
&lt;br /&gt;
So with all three &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; clauses we will first create a backtrack point to the two last clauses and then execute the first clause.  As discussed above the first clause will succeed, so we will return from &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; to the place in &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; where &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; was called from, but we have also created a backtrack point to the two last clauses of &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The code we wrote in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause will (without going into the datails) has the effect that we backtrack according to our backtrack point, so after returning from runGoal the first time we will backtrack to the last two &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clauses.&lt;br /&gt;
&lt;br /&gt;
When we backtrack to the last two clauses there is a choice between the second and the third clause, so again a backtrack point is created, this time to the third clause, and then the second clause is executed.&lt;br /&gt;
&lt;br /&gt;
As mentioned before the second clause will fail in the &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; call, when this happens we will again backtrack to where the latest backtrack point says, i.e. to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.  The third clause is the last clause so here we don&amp;#039;t have a choice point and therefore we do not have any more backtrack points left.&lt;br /&gt;
&lt;br /&gt;
The third clause will execute as described above and &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; will return to then &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; clause, and with no more backtrack points &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; will finish and eventually the entire program will finish.&lt;br /&gt;
&lt;br /&gt;
Notice that we backtracked two times in this program:&lt;br /&gt;
&lt;br /&gt;
* The first time we backtracked back into a predicate that had already returned one time, and therefore a single call to &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; returned twice to &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt;&lt;br /&gt;
*  The second backtrack was from the nested call to father in the second &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
&lt;br /&gt;
* Calls can succeed or fail.&lt;br /&gt;
* Whenever there is a choice point a backtrack point is created to the second choice and the first choice is pursuited.&lt;br /&gt;
* When a predicate fails program control is backtracked to the latest backtrack point.&lt;br /&gt;
* Backtrack may &amp;quot;jump&amp;quot; out from nested calls &lt;br /&gt;
* Backtracking may also &amp;quot;jump&amp;quot; back  into already succeeded calls&lt;br /&gt;
* So as a result of backtracking a certain predicate call may succeed/return &amp;#039;&amp;#039;&amp;#039;several&amp;#039;&amp;#039;&amp;#039; times.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4715</id>
		<title>Lessons/Succeed, fail and backtrack - part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4715"/>
		<updated>2020-02-05T10:44:32Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;So why is it a logical programming language? And why do we call it predicates and clauses?&lt;br /&gt;
&lt;br /&gt;
Logic is a formalized approach to&lt;br /&gt;
&lt;br /&gt;
* propositions like &amp;quot;John is the father of Bill&amp;quot;&lt;br /&gt;
* rules like &amp;quot;C is the grandfather of A if C is the father of B and B is the father of A&amp;quot;&lt;br /&gt;
* questions like &amp;quot;Who is the father of John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog can formalize logical entities.  The Visual Prolog compiler has a number of strict validation checks that will help you write the programs you intend.  But to keep things simple we will &amp;quot;work around&amp;quot; those checks in this lesson.  To do that work around you should update enter the following code in the console project:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|In a console project update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and add a &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        foreach runGoal() do&lt;br /&gt;
        end foreach.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    runGoal : () nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    runGoal().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Compared to what we have see so far you will notice the token &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; in the declaration of &amp;lt;vp&amp;gt;run2&amp;lt;/vp&amp;gt; and you will also get a warning about &amp;#039;&amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt;&amp;#039;.  We have inserted &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; to get around some compiler errors that we would later get, and instead we now get some warnings.  For now we will just ignore these warnings and in the sequel of this lesson you should just accept to write &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; and also &amp;lt;vp&amp;gt;anyflow&amp;lt;/vp&amp;gt; where told.&lt;br /&gt;
&lt;br /&gt;
Back to the main subject.  Let us start with the proposition &amp;quot;John is the father of Bill&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Here we have two &amp;quot;things&amp;quot;: John and Bill, and a &amp;quot;relation&amp;quot; between these, namely that one is the father of the other.&lt;br /&gt;
In Visual Prolog I can formalize this proposition in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
father is a predicate/relation taking two arguments, where the second is the father of the first.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;I &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;have chosen that the &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; person should be the father of the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I might as well have chosen it the other way around:  The order of the arguments is the choice of the &amp;quot;designer&amp;quot; of the formalization.&lt;br /&gt;
However, once you have chosen, you must be consistent.&lt;br /&gt;
So in &amp;#039;&amp;#039;&amp;#039;my&amp;#039;&amp;#039;&amp;#039; formalization the father must &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; be the second person.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
clauses&lt;br /&gt;
    father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have a (tiny) &amp;quot;knowledge base&amp;quot;, we will try to find the answer to some questions.  Let&amp;#039;s first see the &amp;quot;magic&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.&lt;br /&gt;
&lt;br /&gt;
Then add one more clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the proejct again.&lt;br /&gt;
&lt;br /&gt;
Finally, modify add one more clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)),&lt;br /&gt;
		stdio::write(&amp;quot;No, John is the not the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project again.}}&lt;br /&gt;
&lt;br /&gt;
To understand what is going on we need to understand the concept of &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;succed&amp;#039;&amp;#039;&amp;#039;, because there is a clause that matches this call.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;, because the clauses for the father predicate cannot match this call.  &lt;br /&gt;
The call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; because &amp;lt;vp&amp;gt;not&amp;lt;/vp&amp;gt; is  special predicate that succeeds if its argument fails and fails if the argument succeeds.&lt;br /&gt;
&lt;br /&gt;
Using the words &amp;#039;&amp;#039;succeed&amp;#039;&amp;#039; and &amp;#039;&amp;#039;fail&amp;#039; we can now explain the clauses.&lt;br /&gt;
&lt;br /&gt;
Let us first consider each of the three clauses individually (as if they were the only one)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first clause &amp;lt;vp&amp;gt;runGoal() :- father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;), stdio::write(&amp;quot;Yes, ...&amp;quot;).&amp;lt;/vp&amp;gt; works like this:  &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; succeeds &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt; succeeds, otherwise it fails.&lt;br /&gt;
&lt;br /&gt;
So when we call &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and &amp;#039;&amp;#039;if&amp;#039;&amp;#039; that succeeds (which we know it will) we will call &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; is a predicate that succeds no matter which arguments it is called with and in addition it will also write the arguments to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
So all in all, the first clause will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and also write &amp;lt;vp&amp;gt;&amp;quot;Yes, ...&amp;quot;&amp;lt;/vp&amp;gt; to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
In the second clause we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and since that call fails, the entire clause fails, and therefore we will &amp;#039;&amp;#039;not&amp;#039;&amp;#039; call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
In the third clause we first call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt;.  As mentioned above this call succeeds and therefore we will also call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
Now that we know how each clause works individually, lets look at the effect of having them all three.  This is actually the most unique feature of logical programming languages, but probably also the most tricky one to understand.  The idea is based on the logical &amp;quot;or&amp;quot;: to &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; will succeed if the first clause &amp;quot;or&amp;quot; the second clause &amp;quot;or&amp;quot; the third clause succeeds.  It is true, but it is not sufficiently detailed when actually programming in Visual Prolog.  Here I will provide a much more detailed and operative description of how it works.&lt;br /&gt;
&lt;br /&gt;
When we invoke &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;, which has three clauses.  We will first create a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; point to the two other clauses, and then excecute the first clause.&lt;br /&gt;
&lt;br /&gt;
A backtrack point is a &amp;quot;thing&amp;quot; that describes a place in our program where we had two alternative ways to fulfill our goal.  We do one thing but make a note about the other possibility.  If we at some point fail, we backtrack to the place we noted in the most recent backtrack point we created and follow the alternative way we noted in the backtrack point.  (That backtrack point is then used and thus discarded).&lt;br /&gt;
&lt;br /&gt;
So with all three &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; clauses we will first create a backtrack point to the two last clauses and then execute the first clause.  As discussed above the first clause will succeed, so we will return from &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; to the place in &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; where &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; was called from, but we have also created a backtrack point to the two last clauses of &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The code we wrote in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause will (without going into the datails) has the effect that we backtrack according to our backtrack point, so after returning from runGoal the first time we will backtrack to the last two &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clauses.&lt;br /&gt;
&lt;br /&gt;
When we backtrack to the last two clauses there is a choice between the second and the third clause, so again a backtrack point is created, this time to the third clause, and then the second clause is executed.&lt;br /&gt;
&lt;br /&gt;
As mentioned before the second clause will fail in the &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; call, when this happens we will again backtrack to where the latest backtrack point says, i.e. to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.  The third clause is the last clause so here we don&amp;#039;t have a choice point and therefore we do not have any more backtrack points left.&lt;br /&gt;
&lt;br /&gt;
The third clause will execute as described above and &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; will return to then &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; clause, and with no more backtrack points &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; will finish and eventually the entire program will finish.&lt;br /&gt;
&lt;br /&gt;
Notice that we backtracked two times in this program:&lt;br /&gt;
&lt;br /&gt;
* The first time we backtracked back into a predicate that had already returned one time, and therefore a single call to &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; returned twice to &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt;&lt;br /&gt;
*  The second backtrack was from the nested call to father in the secon &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
&lt;br /&gt;
* Calls can succeed or fail.&lt;br /&gt;
* Whenever there is a choice point a backtrack point is created to the second choice and the first choice is pursuited.&lt;br /&gt;
* When a predicate fails program control is backtracked to the latest backtrack point.&lt;br /&gt;
* Backtrack may &amp;quot;jump&amp;quot; out from nested calls &lt;br /&gt;
* Backtracking may also &amp;quot;jump&amp;quot; back  into already succeeded calls&lt;br /&gt;
* So as a result of backtracking a certain predicate call may succeed/return &amp;#039;&amp;#039;&amp;#039;several&amp;#039;&amp;#039;&amp;#039; times.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4714</id>
		<title>Lessons/Succeed, fail and backtrack - part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4714"/>
		<updated>2020-02-05T10:42:26Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;So why is it a logical programming language? And why do we call it predicates and clauses?&lt;br /&gt;
&lt;br /&gt;
Logic is a formalized approach to&lt;br /&gt;
&lt;br /&gt;
* propositions like &amp;quot;John is the father of Bill&amp;quot;&lt;br /&gt;
* rules like &amp;quot;C is the grandfather of A if C is the father of B and B is the father of A&amp;quot;&lt;br /&gt;
* questions like &amp;quot;Who is the father of John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog can formalize logical entities.  The Visual Prolog compiler has a number of strict validation checks that will help you write the programs you intend.  But to keep things simple we will &amp;quot;work around&amp;quot; those checks in this lesson.  To do that work around you should update enter the following code in the console project:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|In a console project update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and add a &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        foreach runGoal() do&lt;br /&gt;
        end foreach.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    runGoal : () nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    runGoal().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Compared to what we have see so far you will notice the token &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; in the declaration of &amp;lt;vp&amp;gt;run2&amp;lt;/vp&amp;gt; and you will also get a warning about &amp;#039;&amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt;&amp;#039;.  We have inserted &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; to get around some compiler errors that we would later get, and instead we now get some warnings.  For now we will just ignore these warnings and in the sequel of this lesson you should just accept to write &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; and also &amp;lt;vp&amp;gt;anyflow&amp;lt;/vp&amp;gt; where told.&lt;br /&gt;
&lt;br /&gt;
Back to the main subject.  Let us start with the proposition &amp;quot;John is the father of Bill&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Here we have two &amp;quot;things&amp;quot;: John and Bill, and a &amp;quot;relation&amp;quot; between these, namely that one is the father of the other.&lt;br /&gt;
In Visual Prolog I can formalize this proposition in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
father is a predicate/relation taking two arguments, where the second is the father of the first.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;I &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;have chosen that the &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; person should be the father of the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I might as well have chosen it the other way around:  The order of the arguments is the choice of the &amp;quot;designer&amp;quot; of the formalization.&lt;br /&gt;
However, once you have chosen, you must be consistent.&lt;br /&gt;
So in &amp;#039;&amp;#039;&amp;#039;my&amp;#039;&amp;#039;&amp;#039; formalization the father must &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; be the second person.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
clauses&lt;br /&gt;
    father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have a (tiny) &amp;quot;knowledge base&amp;quot;, we will try to find the answer to some questions.  Let&amp;#039;s first see the &amp;quot;magic&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.&lt;br /&gt;
&lt;br /&gt;
Then add one more clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the proejct again.&lt;br /&gt;
&lt;br /&gt;
Finally, modify add one more clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)),&lt;br /&gt;
		stdio::write(&amp;quot;No, John is the not the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project again.}}&lt;br /&gt;
&lt;br /&gt;
To understand what is going on we need to understand the concept of &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;succed&amp;#039;&amp;#039;&amp;#039;, because there is a clause that matches this call.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;, because the clauses for the father predicate cannot match this call.  &lt;br /&gt;
The call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; because &amp;lt;vp&amp;gt;not&amp;lt;/vp&amp;gt; is  special predicate that succeeds if its argument fails and fails if the argument succeeds.&lt;br /&gt;
&lt;br /&gt;
Using the words &amp;#039;&amp;#039;succeed&amp;#039;&amp;#039; and &amp;#039;&amp;#039;fail&amp;#039; we can now explain the clauses.&lt;br /&gt;
&lt;br /&gt;
Let us first consider each of the three clauses individually (as if they were the only one)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first clause &amp;lt;vp&amp;gt;runGoal() :- father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;), stdio::write(&amp;quot;Yes, ...&amp;quot;).&amp;lt;/vp&amp;gt; works like this:  &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; succeeds &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt; succeeds, otherwise it fails.&lt;br /&gt;
&lt;br /&gt;
So when we call &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and &amp;#039;&amp;#039;if&amp;#039;&amp;#039; that succeeds (which we know it will) we will call &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; is a predicate that succeds no matter which arguments it is called with and in addition it will also write the arguments to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
So all in all, the first clause will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and also write &amp;lt;vp&amp;gt;&amp;quot;Yes, ...&amp;quot;&amp;lt;/vp&amp;gt; to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
In the second clause we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and since that call fails, the entire clause fails, and therefore we will &amp;#039;&amp;#039;not&amp;#039;&amp;#039; call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
In the third clause we first call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt;.  As mentioned above this call succeeds and therefore we will also call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
Now that we know how each clause works individually, lets look at the effect of having them all three.  This is actually the most unique feature of logical programming languages, but probably also the most tricky one to understand.  The idea is based on the logical &amp;quot;or&amp;quot;: to &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; will succeed if the first clause &amp;quot;or&amp;quot; the second clause &amp;quot;or&amp;quot; the third clause succeeds.  It is true, but it is not sufficiently detailed when actually programming in Visual Prolog.  Here I will provide a much more detailed and operative description of how it works.&lt;br /&gt;
&lt;br /&gt;
When we invoke &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;, which has three clauses.  We will first create a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; point to the two other clauses, and then excecute the first clause.&lt;br /&gt;
&lt;br /&gt;
A backtrack point is a &amp;quot;thing&amp;quot; that describes a place in our program where we had two alternative ways to fulfill our goal.  We do one thing but make a note about the other possibility.  If we at some point fail, we backtrack to the place we noted in the most recent backtrack point we created and follow the alternative way we noted in the backtrack point.  (That backtrack point is then used and thus discarded).&lt;br /&gt;
&lt;br /&gt;
So with all three &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; clauses we will first create a backtrack point to the two last clauses and then execute the first clause.  As discussed above the first clause will succeed, so we will return from &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; to the place in &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; where &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; was called from, but we have also created a backtrack point to the two last clauses of &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The code we wrote in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause will (without going into the datails) has the effect that we backtrack according to our backtrack point, so after returning from runGoal the first time we will backtrack to the last two &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clauses.&lt;br /&gt;
&lt;br /&gt;
When we backtrack to the last two clauses there is a choice between the second and the third clause, so again a abcktrack point is created, this time to the third clause, and then the second clause is executed.&lt;br /&gt;
&lt;br /&gt;
As mentioned before the second clause will fail in the &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; call, when this happens we will again backtrack to where the latest backtrack point says, i.e. to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.  The third clause is the last clause so here we don&amp;#039;t have a choice point and therefore we do not have any more backtrack points left.&lt;br /&gt;
&lt;br /&gt;
The third clause will execute as described above and &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; will return to then &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; clause, and with no more backtrack points &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; will finish and eventually the entire program will finish.&lt;br /&gt;
&lt;br /&gt;
Notice that we backtracked two times in this program:&lt;br /&gt;
&lt;br /&gt;
* The first time we backtracked back into a predicate that had already returned one time, and therefore a single call to &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; returned twice to &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt;&lt;br /&gt;
*  The second backtrack was from the nested call to father in the secon &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
&lt;br /&gt;
* Calls can succeed or fail.&lt;br /&gt;
* Whenever there is a choice point a backtrack point is created to the second choice and the first choice is pursuited.&lt;br /&gt;
* When a predicate fails program control is backtracked to the latest backtrack point.&lt;br /&gt;
* Backtrack may &amp;quot;jump&amp;quot; out from nested calls &lt;br /&gt;
* Backtracking may also &amp;quot;jump&amp;quot; back  into already succeeded calls&lt;br /&gt;
* So as a result of backtracking a certain predicate call may succeed/return &amp;#039;&amp;#039;&amp;#039;several&amp;#039;&amp;#039;&amp;#039; times.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4713</id>
		<title>Lessons/Succeed, fail and backtrack - part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4713"/>
		<updated>2020-02-05T10:26:26Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;So why is it a logical programming language? And why do we call it predicates and clauses?&lt;br /&gt;
&lt;br /&gt;
Logic is a formalized approach to&lt;br /&gt;
&lt;br /&gt;
* propositions like &amp;quot;John is the father of Bill&amp;quot;&lt;br /&gt;
* rules like &amp;quot;C is the grandfather of A if C is the father of B and B is the father of A&amp;quot;&lt;br /&gt;
* questions like &amp;quot;Who is the father of John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog can formalize logical entities.  The Visual Prolog compiler has a number of strict validation checks that will help you write the programs you intend.  But to keep things simple we will &amp;quot;work around&amp;quot; those checks in this lesson.  To do that work around you should update enter the following code in the console project:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|In a console project update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and add a &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        foreach runGoal() do&lt;br /&gt;
        end foreach.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    runGoal : () nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    runGoal().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Compared to what we have see so far you will notice the token &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; in the declaration of &amp;lt;vp&amp;gt;run2&amp;lt;/vp&amp;gt; and you will also get a warning about &amp;#039;&amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt;&amp;#039;.  We have inserted &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; to get around some compiler errors that we would later get, and instead we now get some warnings.  For now we will just ignore these warnings and in the sequel of this lesson you should just accept to write &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; and also &amp;lt;vp&amp;gt;anyflow&amp;lt;/vp&amp;gt; where told.&lt;br /&gt;
&lt;br /&gt;
Back to the main subject.  Let us start with the proposition &amp;quot;John is the father of Bill&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Here we have two &amp;quot;things&amp;quot;: John and Bill, and a &amp;quot;relation&amp;quot; between these, namely that one is the father of the other.&lt;br /&gt;
In Visual Prolog I can formalize this proposition in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
father is a predicate/relation taking two arguments, where the second is the father of the first.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;I &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;have chosen that the &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; person should be the father of the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I might as well have chosen it the other way around:  The order of the arguments is the choice of the &amp;quot;designer&amp;quot; of the formalization.&lt;br /&gt;
However, once you have chosen, you must be consistent.&lt;br /&gt;
So in &amp;#039;&amp;#039;&amp;#039;my&amp;#039;&amp;#039;&amp;#039; formalization the father must &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; be the second person.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
clauses&lt;br /&gt;
    father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have a (tiny) &amp;quot;knowledge base&amp;quot;, we will try to find the answer to some questions.  Let&amp;#039;s first see the &amp;quot;magic&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.&lt;br /&gt;
&lt;br /&gt;
Then add one more clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the proejct again.&lt;br /&gt;
&lt;br /&gt;
Finally, modify add one more clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)),&lt;br /&gt;
		stdio::write(&amp;quot;No, John is the not the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project again.}}&lt;br /&gt;
&lt;br /&gt;
To understand what is going on we need to understand the concept of &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;succed&amp;#039;&amp;#039;&amp;#039;, because there is a clause that matches this call.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;, because the clauses for the father predicate cannot match this call.  &lt;br /&gt;
The call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; because &amp;lt;vp&amp;gt;not&amp;lt;/vp&amp;gt; is  special predicate that succeeds if its argument fails and fails if the argument succeeds.&lt;br /&gt;
&lt;br /&gt;
Using the words &amp;#039;&amp;#039;succeed&amp;#039;&amp;#039; and &amp;#039;&amp;#039;fail&amp;#039; we can now explain the clauses.&lt;br /&gt;
&lt;br /&gt;
Let us first consider each of the three clauses individually (as if they were the only one)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first clause &amp;lt;vp&amp;gt;runGoal() :- father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;), stdio::write(&amp;quot;Yes, ...&amp;quot;).&amp;lt;/vp&amp;gt; works like this:  &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; succeeds &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt; succeeds, otherwise it fails.&lt;br /&gt;
&lt;br /&gt;
So when we call &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and &amp;#039;&amp;#039;if&amp;#039;&amp;#039; that succeeds (which we know it will) we will call &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; is a predicate that succeds no matter which arguments it is called with and in addition it will also write the arguments to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
So all in all, the first clause will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and also write &amp;lt;vp&amp;gt;&amp;quot;Yes, ...&amp;quot;&amp;lt;/vp&amp;gt; to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
In the second clause we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and since that call fails, the entire clause fails, and therefore we will &amp;#039;&amp;#039;not&amp;#039;&amp;#039; call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
In the third clause we first call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt;.  As mentioned above this call succeeds and therefore we will also call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
Now that we know how each clause works individually, lets look at the effect of having them all three.  This is actually the most unique feature of logical programming languages, but probably also the most tricky one to understand.  The idea is based on the logical &amp;quot;or&amp;quot;: to &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; will succeed if the first clause &amp;quot;or&amp;quot; the second clause &amp;quot;or&amp;quot; the third clause succeeds.  It is true, but it is not sufficiently detailed when actually programming in Visual Prolog.  Here I will provide a much more detailed and operative description of how it works.&lt;br /&gt;
&lt;br /&gt;
When we invoke &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;, which has three clauses.  We will first create a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; point to the two other clauses, and then excecute the first clause.&lt;br /&gt;
&lt;br /&gt;
A backtrack point is a &amp;quot;thing&amp;quot; that describes a place in our program where we had two alternative ways to fulfill our goal.  We do one thing but make a note about the other possibility.  If we at some point fail, we backtrack to the place we noted in the most recent backtrack point we created and follow the alterative way we noted in the backtrack point.  (That backtrack point is then used and thus discarded).&lt;br /&gt;
&lt;br /&gt;
So with all three &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; clauses we will first create a backtrack point to the two last clauses and then execute the first clause.  As discussed above the first clause will succeed, so we will return from &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; to the place in &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; where &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; was called from, but we have also created a backtrack point to the two last clauses of &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The code we wrote in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause will (without going into the datails) has the effect that we backtrack according to our backtrack point, so after returning from runGoal the first time we will backtrack to the last two &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clauses.&lt;br /&gt;
&lt;br /&gt;
When we backtrack to the last two clauses there is a choice between the second and the third clause, so again a abcktrack point is created, this time to the third clause, and then the second clause is executed.&lt;br /&gt;
&lt;br /&gt;
As mentioned before the second clause will fail in the &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; call, when this happens we will again backtrack to where the latest backtrack point says, i.e. to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.  The third clause is the last clause so here we don&amp;#039;t have a choice point and therefore we do not have any more backtrack points left.&lt;br /&gt;
&lt;br /&gt;
The third clause will execute as described above and &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; will return to then &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; clause, and with no more backtrack points &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; will finish and eventually the entire program will finish.&lt;br /&gt;
&lt;br /&gt;
Notice that we backtracked two times in this program:&lt;br /&gt;
&lt;br /&gt;
* The first time we backtracked back into a predicate that had already returned one time, and therefore a single call to &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; returned twice to &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt;&lt;br /&gt;
*  The second backtrack was from the nested call to father in the secon &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
&lt;br /&gt;
* Calls can succeed or fail.&lt;br /&gt;
* Whenever there is a choice point a backtrack point is created to the second choice and the first choice is pursuited.&lt;br /&gt;
* When a predicate fails program control is backtracked to the latest backtrack point.&lt;br /&gt;
* Backtrack may &amp;quot;jump&amp;quot; out from nested calls &lt;br /&gt;
* Backtracking may also &amp;quot;jump&amp;quot; back  into already succeeded calls&lt;br /&gt;
* So as a result of backtracking a certain predicate call may succeed/return &amp;#039;&amp;#039;&amp;#039;several&amp;#039;&amp;#039;&amp;#039; times.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4712</id>
		<title>Lessons/Succeed, fail and backtrack - part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Succeed,_fail_and_backtrack_-_part_1&amp;diff=4712"/>
		<updated>2020-02-05T10:11:55Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;So why is it a logical programming language? And why do we call it predicates and clauses?&lt;br /&gt;
&lt;br /&gt;
Logic is a formalized approach to&lt;br /&gt;
&lt;br /&gt;
* propositions like &amp;quot;John is the father of Bill&amp;quot;&lt;br /&gt;
* rules like &amp;quot;C is the grandfather of A if C is the father of B and B is the father of A&amp;quot;&lt;br /&gt;
* questions like &amp;quot;Who is the father of John&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog can formalize logical entities.  The Visual Prolog compiler has a number of strict validation checks that will help you write the programs you intend.  But to keep things simple we will &amp;quot;work around&amp;quot; those checks in this lesson.  To do that work around you should update enter the following code in the console project:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|In a console project update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and add a &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        foreach runGoal() do&lt;br /&gt;
        end foreach.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    runGoal : () nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    runGoal().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Compared to what we have see so far you will notice the token &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; in the declaration of &amp;lt;vp&amp;gt;run2&amp;lt;/vp&amp;gt; and you will also get a warning about &amp;#039;&amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt;&amp;#039;.  We have inserted &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; to get around some compiler errors that we would later get, and instead we now get some warnings.  For now we will just ignore these warnings and in the sequel of this lesson you should just accept to write &amp;lt;vp&amp;gt;nondeterm&amp;lt;/vp&amp;gt; and also &amp;lt;vp&amp;gt;anyflow&amp;lt;/vp&amp;gt; where told.&lt;br /&gt;
&lt;br /&gt;
Back to the main subject.  Let us start with the proposition &amp;quot;John is the father of Bill&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Here we have two &amp;quot;things&amp;quot;: John and Bill, and a &amp;quot;relation&amp;quot; between these, namely that one is the father of the other.&lt;br /&gt;
In Visual Prolog I can formalize this proposition in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
father is a predicate/relation taking two arguments, where the second is the father of the first.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;I &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;have chosen that the &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; person should be the father of the &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
I might as well have chosen it the other way around:  The order of the arguments is the choice of the &amp;quot;designer&amp;quot; of the formalization.&lt;br /&gt;
However, once you have chosen, you must be consistent.&lt;br /&gt;
So in &amp;#039;&amp;#039;&amp;#039;my&amp;#039;&amp;#039;&amp;#039; formalization the father must &amp;#039;&amp;#039;&amp;#039;always&amp;#039;&amp;#039;&amp;#039; be the second person.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
clauses&lt;br /&gt;
    father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have a (tiny) &amp;quot;knowledge base&amp;quot;, we will try to find the answer to some questions.  Let&amp;#039;s first see the &amp;quot;magic&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.&lt;br /&gt;
&lt;br /&gt;
Then add one more clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the proejct again.&lt;br /&gt;
&lt;br /&gt;
Finally, modify add one more clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;),&lt;br /&gt;
		stdio::write(&amp;quot;Yes, John is the father of Sue&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
	runGoal() :-&lt;br /&gt;
		not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)),&lt;br /&gt;
		stdio::write(&amp;quot;No, John is the not the father of Sue&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project again.}}&lt;br /&gt;
&lt;br /&gt;
To understand what is going on we need to understand the concept of &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;succed&amp;#039;&amp;#039;&amp;#039;, because there is a clause that matches this call.  When we call &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; the call will &amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;, because the clauses for the father predicate cannot match this call.  &lt;br /&gt;
The call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; because &amp;lt;vp&amp;gt;not&amp;lt;/vp&amp;gt; is  special predicate that succeeds if its argument fails and fails if the argument succeeds.&lt;br /&gt;
&lt;br /&gt;
Using the words &amp;#039;&amp;#039;succeed&amp;#039;&amp;#039; and &amp;#039;&amp;#039;fail&amp;#039; we can now explain the clauses.&lt;br /&gt;
&lt;br /&gt;
Let us first consider each of the three clauses individually (as if they were the only one)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first clause &amp;lt;vp&amp;gt;runGoal() :- father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;), stdio::write(&amp;quot;Yes, ...&amp;quot;).&amp;lt;/vp&amp;gt; works like this:  &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; succeeds &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt; succeeds, otherwise it fails.&lt;br /&gt;
&lt;br /&gt;
So when we call &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and &amp;#039;&amp;#039;if&amp;#039;&amp;#039; that succeeds (which we know it will) we will call &amp;lt;vp&amp;gt;stdio::write(&amp;quot;Yes, ...&amp;quot;)&amp;lt;/vp&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; is a predicate that succeds no matter which arguments it is called with and in addition it will also write the arguments to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
So all in all, the first clause will &amp;#039;&amp;#039;&amp;#039;succeed&amp;#039;&amp;#039;&amp;#039; and also write &amp;lt;vp&amp;gt;&amp;quot;Yes, ...&amp;quot;&amp;lt;/vp&amp;gt; to the standard output stream.&lt;br /&gt;
&lt;br /&gt;
In the second clause we will first run &amp;lt;vp&amp;gt;father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;)&amp;lt;/vp&amp;gt; and since that call fails, the entire clause fails, and therfore we will &amp;#039;&amp;#039;not&amp;#039;&amp;#039; call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
In the third clause we first call &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt;.  As mentioned above this call succeeds and therefore we will also call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; in this clause.&lt;br /&gt;
&lt;br /&gt;
Now that we know how each clause works individually, lets look at the effect of having them all three.  This is actually the most unique feature of logical programming languages, but probably also the most tricky one to understand.  The idea is based on the logical &amp;quot;or&amp;quot;: to &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; will succeed if the first clause &amp;quot;or&amp;quot; the second clause &amp;quot;or&amp;quot; the third clause succeeds.  It is true, but it is not sufficiently detailed when actually programming in Visual Prolog.  Here I will provide a much more detailed and operative description of how it works.&lt;br /&gt;
&lt;br /&gt;
When we invoke &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;, which has three clauses.  We will first create a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; point to the two other clauses, and then excecute the first clause.&lt;br /&gt;
&lt;br /&gt;
A backtrack point is a &amp;quot;thing&amp;quot; that describes a place in our program where we had two alternative ways to fulfill our goal.  We do one thing but make a note about the other possibility.  If we at some point fail, we backtrack to the place we noted in the most recent backtrack point we created and follow the alterative way we noted in the backtrack point.  (That backtrack point is then used and thus discarded).&lt;br /&gt;
&lt;br /&gt;
So with all three &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; clauses we will first create a backtrack point to the two last clauses and then execute the first clause.  As discussed above the first clause will succeed, so we will return from &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; to the place in &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; where &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt; was called from, but we have also created a backtrack point to the two last clauses of &amp;lt;vp&amp;gt;runGoal()&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The code we wrote in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause will (without going into the datails) has the effect that we backtrack according to our backtrack point, so after returning from runGoal the first time we will backtrack to the last two &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clauses.&lt;br /&gt;
&lt;br /&gt;
When we backtrack to the last two clauses there is a choice between the second and the third clause, so again a abcktrack point is created, this time to the third clause, and then the second clause is executed.&lt;br /&gt;
&lt;br /&gt;
As mentioned before the second clause will fail in the &amp;lt;vp&amp;gt;not(father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;))&amp;lt;/vp&amp;gt; call, when this happens we will again backtrack to where the latest backtrack point says, i.e. to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.  The third clause is the last clause so here we don&amp;#039;t have a choice point and therefore we do not have any more backtrack points left.&lt;br /&gt;
&lt;br /&gt;
The third clause will execute as described above and &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; will return to then &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; clause, and with no more backtrack points &amp;lt;vp&amp;gt;run()&amp;lt;/vp&amp;gt; will finish and eventually the entire program will finish.&lt;br /&gt;
&lt;br /&gt;
Notice that we backtracked two times in this program:&lt;br /&gt;
&lt;br /&gt;
* The first time we backtracked back into a predicate that had already returned one time, and therefore a single call to &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; returned twice to &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt;&lt;br /&gt;
*  The second backtrack was from the nested call to father in the secon &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause to the third &amp;lt;vp&amp;gt;runGoal&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
&lt;br /&gt;
* Calls can succeed or fail.&lt;br /&gt;
* Whenever there is a choice point a backtrack point is created to the second choice and the first choice is pursuited.&lt;br /&gt;
* When a predicate fails program control is backtracked to the latest backtrack point.&lt;br /&gt;
* Backtrack may &amp;quot;jump&amp;quot; out from nested calls &lt;br /&gt;
* Backtracking may also &amp;quot;jump&amp;quot; back  into already succeeded calls&lt;br /&gt;
* So as a result of backtracking a certain predicate call may succeed/return &amp;#039;&amp;#039;&amp;#039;several&amp;#039;&amp;#039;&amp;#039; times.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4711</id>
		<title>Lessons/Output arguments</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4711"/>
		<updated>2020-02-05T09:54:53Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the {{Lesson|Functions}} lesson we created a function, i.e. a predicate that retruned a value.  Here we will look at output parameters, which is another way to let data flow outwards.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to a console project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    nowDateAndTime : (string Date [out], string Time [out]).&lt;br /&gt;
clauses&lt;br /&gt;
    nowDateAndTime(Date, Time) :-&lt;br /&gt;
        Now = time::now(),&lt;br /&gt;
        Date = Now:formatShortDate(),&lt;br /&gt;
        Time = Now:formatTime().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...time.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
The first thing to notice here is the use of &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; in the predicate declaration, this &amp;quot;mark&amp;quot; indicates that the corresponding argument is an output argument rather than an input argument.&lt;br /&gt;
&lt;br /&gt;
In the clause we still have a &amp;quot;position&amp;quot; corresponding to that argument, but this time we have to &amp;quot;calculate&amp;quot; and return a value.  So in the clause above &amp;lt;vp&amp;gt;Date&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Time&amp;lt;/vp&amp;gt; does not have a value when the body starts to execute, it is our responsibility to create bind values to these variables. &lt;br /&gt;
&lt;br /&gt;
In the clause body we call the function &amp;lt;vp&amp;gt;now&amp;lt;/vp&amp;gt; on the class &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; this function returns a time &amp;#039;&amp;#039;object&amp;#039;&amp;#039; that represents the time the function was called.  Here we will not go into details about objects, but the next two lines of code shows that objects have predicates associated with them and that these are referenced these using a single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; object represents a time and date, and in the last two lines we &amp;quot;request&amp;quot; the date using the short format and the time formatted as a string.  What exactly these functions does depends on the users settings on the computer (i.e. the users locale settings).&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        nowDateAndTime(NowDate, NowTime),&lt;br /&gt;
        stdio::writef(&amp;quot;The time is now % and it is the %.\n&amp;quot;, NowTime, NowDate).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.}}&lt;br /&gt;
&lt;br /&gt;
Sometimes you will accidentally use a single colon where you intended to use two and vice versa:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Try using a single colon instead of two and build the project and examine the error message.  Revert the problem and try the the opposite error.  End without errors.}}&lt;br /&gt;
&lt;br /&gt;
Output arguments can also be used in functions, and together with input arguments.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update &amp;lt;vp&amp;gt;nowDateAndTime&amp;lt;/vp&amp;gt; to be a &amp;#039;&amp;#039;&amp;#039;function&amp;#039;&amp;#039;&amp;#039; that returns the date as an output argument and the time as the function return value.&lt;br /&gt;
&lt;br /&gt;
Notice the error message you get if you don&amp;#039;t update the call in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Also update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and run the program.}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* Output arguments are declared using the &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; attribute.&lt;br /&gt;
* An output argument is unbound when the clause &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is entered and must be bound when the predicate returns.&lt;br /&gt;
* There is something called objects.&lt;br /&gt;
* Objects are values that can be passed around and held in variables.&lt;br /&gt;
* Objects have predicates associated and these are referenced with single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt; (predicates in classes are referenced using a double-colon).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4710</id>
		<title>Lessons/Output arguments</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4710"/>
		<updated>2020-02-05T09:30:24Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the {{Lesson|Functions}} lesson we created a function, i.e. a predicate that retruned a value.  Here we will look at output parameters, which is another way to let data flow outwards.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to a console project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    nowDateAndTime : (string Date [out], string Time [out]).&lt;br /&gt;
clauses&lt;br /&gt;
    nowDateAndTime(Date, Time) :-&lt;br /&gt;
        Now = time::now(),&lt;br /&gt;
        Date = Now:formatShortDate(),&lt;br /&gt;
        Time = Now:formatTime().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...time.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
The first thing to notice here is the use of &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; in the predicate declaration, this &amp;quot;mark&amp;quot; indicates that the corresponding argument is an output argument rather than an input argument.&lt;br /&gt;
&lt;br /&gt;
In the clause we still have a &amp;quot;position&amp;quot; corresponding to that argument, but this time we have to &amp;quot;calculate&amp;quot; and return a value.  So in the clause above &amp;lt;vp&amp;gt;Date&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Time&amp;lt;/vp&amp;gt; does not have a value when the body starts to execute, it is our responsibility to create bind values to these variables. &lt;br /&gt;
&lt;br /&gt;
In the clause body we call the function &amp;lt;vp&amp;gt;now&amp;lt;/vp&amp;gt; on the class &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; this function returns a time &amp;#039;&amp;#039;object&amp;#039;&amp;#039; that represents the time the function was called.  Here we will not go into details about objects, but the next two lines of code shows that objects have predicates associated with them and that these are referenced these using a single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; object represents a time and date, and in the last two lines we &amp;quot;reqest&amp;quot; the date using the short format and the time formatted as a string.  What exactly these functions does depends on the users settings on the computer (i.e. the users locale settings).&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        nowDateAndTime(NowDate, NowTime),&lt;br /&gt;
        stdio::writef(&amp;quot;The time is now % and it is the %.\n&amp;quot;, NowTime, NowDate).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.}}&lt;br /&gt;
&lt;br /&gt;
Sometimes you will accidentally use a single colon where you intended to use two and vice versa:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Try using a single colon instead of two and build the project and examine the error message.  Revert the problem and try the the opposite error.  End without errors.}}&lt;br /&gt;
&lt;br /&gt;
Output arguments can also be used in functions, and together with input arguments.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update &amp;lt;vp&amp;gt;nowDateAndTime&amp;lt;/vp&amp;gt; to be a &amp;#039;&amp;#039;&amp;#039;function&amp;#039;&amp;#039;&amp;#039; that returns the date as an output argument and the time as the function return value.&lt;br /&gt;
&lt;br /&gt;
Notice the error message you get if you don&amp;#039;t update the call in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Also update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and run the program.}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* Output arguments are declared using the &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; attribute.&lt;br /&gt;
* An output argument is unbound when the clause &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is entered and must be bound when the predicate returns.&lt;br /&gt;
* There is something called objects.&lt;br /&gt;
* Objects are values that can be passed around and held in variables.&lt;br /&gt;
* Objects have predicates associated and these are referenced with single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt; (predicates in classes are referenced using a double-colon).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4709</id>
		<title>Lessons/Output arguments</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Output_arguments&amp;diff=4709"/>
		<updated>2020-02-05T09:30:00Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the {{Lesson|Functions}} lesson we created a function, i.e. a predicate that retruned a value.  Here we will look at output parameters, which is another way to let data flow outwards.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to a console project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    nowDateAndTime : (string Date [out], string Time [out]).&lt;br /&gt;
clauses&lt;br /&gt;
    nowDateAndTime(Date, Time) :-&lt;br /&gt;
        Now = time::now(),&lt;br /&gt;
        Date = Now:formatShortDate(),&lt;br /&gt;
        Time = Now:formatTime().&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...time.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
The first thing to notice here is the use of &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; in the predicate declaration, this &amp;quot;mark&amp;quot; indicates that the corresponding argument is an output argument rather than an input argument.&lt;br /&gt;
&lt;br /&gt;
In the clause we still have a &amp;quot;position&amp;quot; correcponding to that argument, but this time we have to &amp;quot;calculate&amp;quot; and return a value.  So in the clause above &amp;lt;vp&amp;gt;Date&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Time&amp;lt;/vp&amp;gt; does not have a value when the body starts to execute, it is our responsibility to create bind values to these variables. &lt;br /&gt;
&lt;br /&gt;
In the clause body we call the function &amp;lt;vp&amp;gt;now&amp;lt;/vp&amp;gt; on the class &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; this function returns a time &amp;#039;&amp;#039;object&amp;#039;&amp;#039; that represents the time the function was called.  Here we will not go into details about objects, but the next two lines of code shows that objects have predicates associated with them and that these are referenced these using a single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;vp&amp;gt;time&amp;lt;/vp&amp;gt; object represents a time and date, and in the last two lines we &amp;quot;reqest&amp;quot; the date using the short format and the time formatted as a string.  What exactly these functions does depends on the users settings on the computer (i.e. the users locale settings).&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        nowDateAndTime(NowDate, NowTime),&lt;br /&gt;
        stdio::writef(&amp;quot;The time is now % and it is the %.\n&amp;quot;, NowTime, NowDate).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project.}}&lt;br /&gt;
&lt;br /&gt;
Sometimes you will accidentally use a single colon where you intended to use two and vice versa:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Try using a single colon instead of two and build the project and examine the error message.  Revert the problem and try the the opposite error.  End without errors.}}&lt;br /&gt;
&lt;br /&gt;
Output arguments can also be used in functions, and together with input arguments.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update &amp;lt;vp&amp;gt;nowDateAndTime&amp;lt;/vp&amp;gt; to be a &amp;#039;&amp;#039;&amp;#039;function&amp;#039;&amp;#039;&amp;#039; that returns the date as an output argument and the time as the function return value.&lt;br /&gt;
&lt;br /&gt;
Notice the error message you get if you don&amp;#039;t update the call in the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause.&lt;br /&gt;
&lt;br /&gt;
Also update the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clause and run the program.}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* Output arguments are declared using the &amp;lt;vp&amp;gt;[out]&amp;lt;/vp&amp;gt; attribute.&lt;br /&gt;
* An output argument is unbound when the clause &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is entered and must be bound when the predicate returns.&lt;br /&gt;
* There is something called objects.&lt;br /&gt;
* Objects are values that can be passed around and held in variables.&lt;br /&gt;
* Objects have predicates associated and these are referenced with single colon: &amp;lt;vp&amp;gt;&amp;lt;object&amp;gt;:&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt; (predicates in classes are referenced using a double-colon).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4708</id>
		<title>Lessons/Functions</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4708"/>
		<updated>2020-02-04T15:23:42Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the {{Lesson|Hello World!}} lesson we created a predicate that took an argument and passed it on to another predicate.  The data was flowing inwards.  Here we will extend with data that float outwards.&lt;br /&gt;
&lt;br /&gt;
Let us start by creating a function.  A function is a predicate that returns a value to place where the function was called from.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to a console project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        Result = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...string.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s start with the include statement.  In our new clause we are calling the &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; predicate in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class. However the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class is not visible in the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file.  At this point we will not concern ourselves with why this is so and what needs to be done, etc.  We will simply accept that the Compiler and IDE in collaboration knows how to solve the problem by inserting a suitable include statement in a suitable place.  Actually, this &amp;quot;automation&amp;quot; will deal with most &amp;quot;including&amp;quot; that you ever deal with.  The back side to this magic (as to all such automation magic) is that, lazy as we are, we don&amp;#039;t really get to know the &amp;quot;include stuff&amp;quot;, and the day where the magic cannot figure out what to do we don&amp;#039;t know it either (see [[Ide/Creating new Project Items#Creating a Package|Creating a Package]]).&lt;br /&gt;
&lt;br /&gt;
Now let us return to the function itself.  As mentioned a function is a predicate that returns a value.  In the declaration of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; there is an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; between the arguments and the &amp;#039;&amp;#039;return type&amp;#039;&amp;#039;.  This arrow indicated that the predicate is a function and the type after the arrow say what type the returned value has.  Again the name (i.e. &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt;) after the type mainly serves documentation purposes.&lt;br /&gt;
&lt;br /&gt;
A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;.  The execution flow is that we &amp;quot;enter&amp;quot; at the &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/vp&amp;gt;, then the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is executed and then the &amp;lt;vp&amp;gt;&amp;lt;result&amp;gt;&amp;lt;/vp&amp;gt; is evaluated/executed and returned.&lt;br /&gt;
&lt;br /&gt;
In the clause above the returned value is in the variable &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt;, which receives it value in the second line (i.e. in the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; of the clause).  What happens in the second line is actually that we call another function, namely the function &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The name &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt; is used just to illustrate that is does not have to be &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt; even though this is what is written in the declaration.  (For a real program I would say: why use two quite unrelated words for the same thing.)&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; is closely related to &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt;.  The input is the same, but &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; will write the result to the standard output stream, &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; returns the result as a string.&lt;br /&gt;
&lt;br /&gt;
Let us call our new function:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        DearGreeting = makeGreeting(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        stdio::write(DearGreeting),&lt;br /&gt;
        WorldGreeting = makeGreeting(&amp;quot;World&amp;quot;),&lt;br /&gt;
        stdio::write(WorldGreeting).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
It is worth noticing that equal is a rather special thing in Visual Prolog. In the function clause &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt; it is just a token that separates the head from the result.  But in the clause bodies it is an equal operator as you know it from mathematics and logic.  Equal is very much &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;unlike&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; an assignement operator as you may know it from other programmig languages.  Notice for example that equal is symetrical:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        string::format(&amp;quot;Hello %!\n&amp;quot;, Name) = Result.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
Notice that the result of calling a function is a value, you do not have to bind that value to a variable, you can use it directly.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
Here we have used the result of the &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; function directly as the result of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt;.  As a result we have nothing for the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt;, but fortunately the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is optional (if there is no &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; then the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token must also be left out).&lt;br /&gt;
&lt;br /&gt;
In the same way we can use the returned value from &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; directly in a call to &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;Dear&amp;quot;)),&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;World&amp;quot;)).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* A function declaration contains an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; followed by the type of the value that the function returns.&lt;br /&gt;
* A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;&lt;br /&gt;
* Or the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt;.&amp;lt;/vp&amp;gt; if there is no need for a body.&lt;br /&gt;
* The equal operator is not an assignment operator as known from some other programming lanugages (it is for example symetrical).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4707</id>
		<title>Lessons/Functions</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Functions&amp;diff=4707"/>
		<updated>2020-02-04T15:16:29Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the {{Lesson|Hello World!}} lesson we created a predicate that took an argument and passed it on to another predicate.  The data was flowing inwards.  Here we will extend with data that float outwards.&lt;br /&gt;
&lt;br /&gt;
Let us start by creating a function.  A function is a predicate that returns a value to place where the function was called from.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code to a console project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        Result = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project, and answer &amp;quot;Yes&amp;quot; to adding the &amp;#039;&amp;#039;&amp;#039;...string.ph&amp;#039;&amp;#039;&amp;#039; include statement.}}&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s start with the include statement.  In our new clause we are calling the &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; predicate in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class. However the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class is not visible in the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file.  At this point we will not concern ourselves with why this is so and what needs to be done, etc.  We will simply accept that the Compiler and IDE in collaboration knows how to solve the problem by inserting a suitable include statement in a suitable place.  Actually, this &amp;quot;automation&amp;quot; will deal with most &amp;quot;including&amp;quot; that you ever deal with.  The back side to this magic (as to all such automation magic) is that, lazy as we are, we don&amp;#039;t really get to know the &amp;quot;include stuff&amp;quot;, and the day where the magic cannot figure out what to do we don&amp;#039;t know it either (see [[Ide/Creating new Project Items#Creating a Package|Creating a Package]]).&lt;br /&gt;
&lt;br /&gt;
Now let us return to the function itself.  As mentioned a function is a predicate that returns a value.  In the declaration of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; there is an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; between the arguments and the &amp;#039;&amp;#039;return type&amp;#039;&amp;#039;.  This arrow indicated that the predicate is a function and the type after the arrow say what type the returned value has.  Again the name (i.e. &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt;) after the type mainly serves documentation purposes.&lt;br /&gt;
&lt;br /&gt;
A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;.  The execution flow is that we &amp;quot;enter&amp;quot; at the &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/vp&amp;gt;, then the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is executed and then the &amp;lt;vp&amp;gt;&amp;lt;result&amp;gt;&amp;lt;/vp&amp;gt; is evaluated/executed and returned.&lt;br /&gt;
&lt;br /&gt;
In the clause above the returned value is in the variable &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt;, which receives it value in the second line (i.e. in the &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; of the clause).  What happens in the second line is actually that we call another function, namely the function &amp;lt;vp&amp;gt;format&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
The name &amp;lt;vp&amp;gt;Result&amp;lt;/vp&amp;gt; is used just to illustrate that is does not have to be &amp;lt;vp&amp;gt;Greeting&amp;lt;/vp&amp;gt; even thoug this is what is written in the declaration.  (For a real program I would say: why use two quite unrelated words for the same thing.)&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; is closely related to &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt;.  The input is the same, but &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; will write the result to the standard output stream, &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; returns the result as a string.&lt;br /&gt;
&lt;br /&gt;
Let us call our new function:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        DearGreeting = makeGreeting(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        stdio::write(DearGreeting),&lt;br /&gt;
        WorldGreeting = makeGreeting(&amp;quot;World&amp;quot;),&lt;br /&gt;
        stdio::write(WorldGreeting).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
It is worth noticing that equal is a rather special thing in Visual Prolog. In the function clause &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt; it is just a token that separates the head from the result.  But in the clause bodies it is an equal operator as you know it from mathematics and logic.  Equal is very much &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;unlike&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; an assignement operator as you may know it from other programmig languages.  Notice for example that equal is symetrical:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = Result :-&lt;br /&gt;
        string::format(&amp;quot;Hello %!\n&amp;quot;, Name) = Result.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
Notice that the result of calling a function is a value, you do not have to bind that value to a variable, you can use it directly.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    makeGreeting : (string Name) -&amp;gt; string Greeting.&lt;br /&gt;
clauses&lt;br /&gt;
    makeGreeting(Name) = string::format(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And run the project.}}&lt;br /&gt;
&lt;br /&gt;
Here we have used the result of the &amp;lt;vp&amp;gt;string::format&amp;lt;/vp&amp;gt; function directly as the result of &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt;.  As a result we have nothing for the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt;, but fortunately the function &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; is optional (if there is no &amp;lt;vp&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/vp&amp;gt; then the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token must also be left out).&lt;br /&gt;
&lt;br /&gt;
In the same way we can use the returned value from &amp;lt;vp&amp;gt;makeGreeting&amp;lt;/vp&amp;gt; directly in a call to &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;Dear&amp;quot;)),&lt;br /&gt;
        stdio::write(makeGreeting(&amp;quot;World&amp;quot;)).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the project (in a window).}}&lt;br /&gt;
&lt;br /&gt;
Summary:&lt;br /&gt;
&lt;br /&gt;
* A function declaration contains an arrow &amp;lt;vp&amp;gt;-&amp;gt;&amp;lt;/vp&amp;gt; followed by the type of the value that the function returns.&lt;br /&gt;
* A function clause has the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt; :- &amp;lt;body&amp;gt;.&amp;lt;/vp&amp;gt;&lt;br /&gt;
* Or the form &amp;lt;vp&amp;gt;&amp;lt;head&amp;gt; = &amp;lt;result&amp;gt;.&amp;lt;/vp&amp;gt; if there is no need for a body.&lt;br /&gt;
* The equal operator is not an assignment operator as known from some other programming lanugages (it is for example symetrical).&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4706</id>
		<title>Lessons/Hello World!</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4706"/>
		<updated>2020-02-04T12:21:10Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this lesson we will just write some code disregarding the finer details.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Create a console project.&lt;br /&gt;
&lt;br /&gt;
Double-click the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file in the project tree to open it in an editor, and update the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(&amp;quot;Hello World!\n&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the program using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Run in window Alt+F5&amp;#039;&amp;#039;&amp;#039;.}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file contains other stuff, which we will currently disregard, and focus solely on the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; you just wrote.&lt;br /&gt;
&lt;br /&gt;
First we have the keyword &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;, which simly indicates that what comes after are &amp;#039;&amp;#039;&amp;#039;clauses&amp;#039;&amp;#039;&amp;#039;.  Clauses is where the actual running code is written.&lt;br /&gt;
&lt;br /&gt;
In this text I will use words like &amp;quot;clause&amp;quot; and &amp;quot;predicate&amp;quot;, which all have their origin in formal logic, but for the moment we will simply treat them as the names that happens to be chosen for things in a Prolog program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; are used to define &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;, where a &amp;#039;&amp;#039;predicate&amp;#039;&amp;#039; is a Prolog routine/subroutine/function/procedure (dear child has many names)&lt;br /&gt;
&lt;br /&gt;
This particular clause defines a predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; which takes no arguments (indicated by the empty parentheses &amp;lt;vp&amp;gt;()&amp;lt;/vp&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
After the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token we have the code that will be executed when &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is invoked/called.&lt;br /&gt;
&lt;br /&gt;
In this case we call a predicate named &amp;lt;vp&amp;gt;write&amp;lt;/vp&amp;gt; which is defined in the &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; named &amp;lt;vp&amp;gt;stdio&amp;lt;/vp&amp;gt;.  We will discuss classes later for now we will just consider &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; as &amp;quot;a long compound name&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
We supply one argument, i.e. the string literal &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, to the &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; predicate.  Notice that \n in the string literal represents a line shift.&lt;br /&gt;
&lt;br /&gt;
The clause is terminated by a period (i.e. &amp;lt;vp&amp;gt;.&amp;lt;/vp&amp;gt;).  Period is used as terminator for many kinds of entities in Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is called when the program is run, and acts as our entry point to the program.  Actually, the real entry point is the &amp;lt;vp&amp;gt;goal&amp;lt;/vp&amp;gt; a further down in the main.pro file, but that entry point is used to make varous initializations, corresponding clean-up, etc and will call the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in the right spot, so run can act like your entry point.&lt;br /&gt;
&lt;br /&gt;
So when the program is run the predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; in &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; will be called and this will (here) call &lt;br /&gt;
&amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; with the argument &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, as a result &amp;#039;&amp;#039;Hello World!&amp;#039;&amp;#039; and a line shift will be written to the console window.&lt;br /&gt;
&lt;br /&gt;
Actually, &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; writes to the standard output stream, which in a console program is initialized to be the console window.  In a default GUI program the standard output stream will write to a message window, but the standard output stream is under programmer control, so what happens when you call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; will depend of what has taken place in the program.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* The actual running code is in &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt; is the Visual Prolog name for routines/subroutines/functions/procedures.&lt;br /&gt;
* Predicates are organized in classes, and predicates from other classes is referenced using &amp;lt;vp&amp;gt;&amp;lt;class&amp;gt;::&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
* The &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in &amp;lt;vp&amp;gt;main.pro&amp;lt;/vp&amp;gt; is the (effective) entry to a program.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; can write to the standard output stream&lt;br /&gt;
&lt;br /&gt;
Now let us extend the program with an extra predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code below the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    advancedHello : (string Name).&lt;br /&gt;
clauses&lt;br /&gt;
    advancedHello(Name) :-&lt;br /&gt;
        stdio::writef(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Build Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039;}}&lt;br /&gt;
&lt;br /&gt;
When you build the project you will get a warning stating that the predicate &amp;lt;vp&amp;gt;main::advancedHello&amp;lt;/vp&amp;gt; is an unused local predicate.  Which is completely correct; we haven&amp;#039;t used it for anything.  But let us &amp;quot;decipher&amp;quot; it before we go any further.&lt;br /&gt;
&lt;br /&gt;
We notice that we again have a clause in a &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; section.  The clause is for a predicate named &amp;lt;vp&amp;gt; advancedHello&amp;lt;/vp&amp;gt; that takes one argument called &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is a variable, a variable starts with an uppercase letter and will be green in the editor (variables can also start with an underscore, but we will discuss that later).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; acts as a formal parameter representing the actual argument used in the call to &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;.  In this clause we call &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; which is a more advanced &amp;quot;writer&amp;quot; predicate.  The first argument to &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is a format string, which contains a %-character, what happens when &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is called is that the actual value of &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is substituted into the format string in the place where the %-character is and then the resulting string is written to the standard output.&lt;br /&gt;
&lt;br /&gt;
This time we also have a &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; section containing a declaration of the &amp;lt;vp&amp;gt;advacedHello&amp;lt;/vp&amp;gt; predicate.  This perticular declaration declares &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt; as a class predicate which takes one argument which must be a string.  &lt;br /&gt;
&lt;br /&gt;
In the predicate declaration the argument also have the name &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; but in the declaration the name is mainly intended for documentation purposes.  The variable name in the declaration and in the clauses can be different, or you can remove it completely.  I recommend that you always have variable names here and that you use variable names that will make it clearer for the user of the predicate what the arguments represents.&lt;br /&gt;
&lt;br /&gt;
What it means to be a &amp;#039;&amp;#039;class&amp;#039;&amp;#039; predicate and what else a predicate can be is discussed elsewhere/later.  But before we leave the subject try this little experiement:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Remove the word &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; from &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; in the declaration of advancedHello. And then build the project.&lt;br /&gt;
&lt;br /&gt;
Notice the error message and remember that you get this message when you have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then insert &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; again and build the project again.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have the advancedHello predicate we can use it in our &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        advancedHello(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        advancedHello(&amp;quot;World&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
Run the project in a window.}}&lt;br /&gt;
&lt;br /&gt;
The clause should be understandable, I just want to highlight that we have used &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; to separate the two invocations of &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;: &amp;#039;&amp;#039;&amp;#039;comma&amp;quot; to separate, &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039; to terminate. I will give a little prediction:  Later when you are familiar with Visual Prolog you will from time to time make syntax errors and (the prediction) 9 out of 10 times the syntax error will concern &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; and/or &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* Predicates must have a declaration (&amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is also declared we just havn&amp;#039;t looked at its declaration).&lt;br /&gt;
* Arguments in predicates declarations can be given names that clarify the meaning if the arguments.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; writes text to the standard output stream using a format string.&lt;br /&gt;
* calls are separated by comma and clauses (and predicate declarations) are terminated by period.&lt;br /&gt;
* If I get a syntax error it is likely to be related to comma or period.&lt;br /&gt;
* A certain other error message (can you recall it) probably means that I have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4705</id>
		<title>Lessons/Hello World!</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4705"/>
		<updated>2020-02-04T12:19:26Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: Ord&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this lesson we will just write some code disregarding the finer details.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Create a console project.&lt;br /&gt;
&lt;br /&gt;
Double-click the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file in the project tree to open it in an editor, and update the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(&amp;quot;Hello World!\n&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the program using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Run in window Alt+F5&amp;#039;&amp;#039;&amp;#039;.}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file contains other stuff, which we will currently disregard, and focus solely on the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; you just wrote.&lt;br /&gt;
&lt;br /&gt;
First we have the keyword &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;, which simly indicates that what comes after are &amp;#039;&amp;#039;&amp;#039;clauses&amp;#039;&amp;#039;&amp;#039;.  Clauses is where the actual running code is written.&lt;br /&gt;
&lt;br /&gt;
In this text I will use words like &amp;quot;clause&amp;quot; and &amp;quot;predicate&amp;quot;, which all have their origin in formal logic, but for the moment we will simply treat them as the names that happens to be chosen for things in a Prolog program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; are used to define &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;, where a &amp;#039;&amp;#039;predicate&amp;#039;&amp;#039; is a Prolog routine/subroutine/function/procedure (dear child has many names)&lt;br /&gt;
&lt;br /&gt;
This particular clause defines a predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; which takes no arguments (indicated by the empty parentheses &amp;lt;vp&amp;gt;()&amp;lt;/vp&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
After the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token we have the code that will be executed when &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is invoked/called.&lt;br /&gt;
&lt;br /&gt;
In this case we call a predicate named &amp;lt;vp&amp;gt;write&amp;lt;/vp&amp;gt; which is defined in the &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; named &amp;lt;vp&amp;gt;stdio&amp;lt;/vp&amp;gt;.  We will discuss classes later for now we will just consider &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; as &amp;quot;a long compound name&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
We supply one argument, i.e. the string literal &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, to the &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; predicate.  Notice that \n in the string literal represents a line shift.&lt;br /&gt;
&lt;br /&gt;
The clause is terminated by a period (i.e. &amp;lt;vp&amp;gt;.&amp;lt;/vp&amp;gt;).  Period is used as terminator for many kinds of entities in Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is called when the program is run, and acts as our entry point to the program.  Actually, the real entry point is the &amp;lt;vp&amp;gt;goal&amp;lt;/vp&amp;gt; a further down in the main.pro file, but that entry point is used to make varous initializations, corresponding clean-up, etc and will call the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in the right spot, so run can act like your entry point.&lt;br /&gt;
&lt;br /&gt;
So when the program is run the predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; in &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; will be called and this will (here) call &lt;br /&gt;
&amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; with the argument &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, as a result &amp;#039;&amp;#039;Hello World!&amp;#039;&amp;#039; and a line shift will be written to the console window.&lt;br /&gt;
&lt;br /&gt;
Actually, &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; writes to the standard output stream, which in a console program is initialized to be the console window.  In a default GUI program the standard output stream will write to a message window, but the standard output stream is under programmer control, so what happens when you call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; will depend of what has taken place in the program.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* The actual running code is in &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt; is the Visual Prolog name for routines/subroutines/functions/procedures.&lt;br /&gt;
* Predicates are organized in classes, and predicates from other classes is referenced using &amp;lt;vp&amp;gt;&amp;lt;class&amp;gt;::&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
* The &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in &amp;lt;vp&amp;gt;main.pro&amp;lt;/vp&amp;gt; is the (effective) entry to a program.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; can write to the standard output stream&lt;br /&gt;
&lt;br /&gt;
Now let us extend the program with an extra predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code below the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    advancedHello : (string Name).&lt;br /&gt;
clauses&lt;br /&gt;
    advancedHello(Name) :-&lt;br /&gt;
        stdio::writef(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Build Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039;}}&lt;br /&gt;
&lt;br /&gt;
When you build the project you will get a warning stating that the predicate &amp;lt;vp&amp;gt;main::advancedHello&amp;lt;/vp&amp;gt; is an unused local predicate.  Which is completely correct; we haven&amp;#039;t used it for anything.  But let us &amp;quot;decipher&amp;quot; it before we go any further.&lt;br /&gt;
&lt;br /&gt;
We notice that we again have a clause in a &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; section.  The clause is for a predicate named &amp;lt;vp&amp;gt; advancedHello&amp;lt;/vp&amp;gt; that takes one argument called &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is a variable, a variable starts with an uppercase letter and will be green in the editor (variables can also start with an underscore, but we will discuss that later).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; acts as a formal parameter representing the actual argument used in the call to &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;.  In this clause we call &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; which is a more advanced &amp;quot;writer&amp;quot; predicate.  The first argument to &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is a format string, which contains a %-character, what happens when &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is called is that the actual value of &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is substituted into the format string in the place where the %-character is and then the resulting string is written to the standard output.&lt;br /&gt;
&lt;br /&gt;
This time we also have a &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; section containing a declaration of the &amp;lt;vp&amp;gt;advacedHello&amp;lt;/vp&amp;gt; predicate.  This perticular declaration declares &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt; as a class predicate which takes one argument which must be a string.  &lt;br /&gt;
&lt;br /&gt;
In the predicate declaration the argument also have the name &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; but in the declaration the name is mainly intended for documentation purposes.  The variable name in the declaration and in the clauses can be different, or you can remove it completely.  I recommend that you always have variable names here and that you use variable names that will make it clearer for the user of the predicate what the arguments represents.&lt;br /&gt;
&lt;br /&gt;
What it means to be a &amp;#039;&amp;#039;class&amp;#039;&amp;#039; predicate and what else a predicate can be is discussed elsewhere/later.  But before we leave the subject try this little experiement:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Remove the word &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; from &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; in the delcaration of advancedHello. And then build the project.&lt;br /&gt;
&lt;br /&gt;
Notice the error message and remember that you get this message when you have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then insert &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; again and build the project again.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have the advancedHello predicate we can use it in our &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        advancedHello(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        advancedHello(&amp;quot;World&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
Run the project in a window.}}&lt;br /&gt;
&lt;br /&gt;
The clause should be understandable, I just want to highlight that we have used &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; to separate the two invocations of &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;: &amp;#039;&amp;#039;&amp;#039;comma&amp;quot; to separate, &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039; to terminate. I will give a little prediction:  Later when you are familiar with Visual Prolog you will from time to time make syntax errors and (the prediction) 9 out of 10 times the syntax error will concern &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; and/or &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* Predicates must have a declaration (&amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is also declared we just havn&amp;#039;t looked at its declaration).&lt;br /&gt;
* Arguments in predicates declarations can be given names that clarify the meaning if the arguments.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; writes text to the standard output stream using a format string.&lt;br /&gt;
* calls are separated by comma and clauses (and predicate declarations) are terminated by period.&lt;br /&gt;
* If I get a syntax error it is likely to be related to comma or period.&lt;br /&gt;
* A certain other error message (can you recall it) probably means that I have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4704</id>
		<title>Lessons/Hello World!</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lessons/Hello_World!&amp;diff=4704"/>
		<updated>2020-02-04T11:58:35Z</updated>

		<summary type="html">&lt;p&gt;Jonas Sonn Jørgensen: Har bare rettet et par ord&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this lesson we will just write some code disregarding the finer details.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Create a console project.&lt;br /&gt;
&lt;br /&gt;
Double-click the &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file in the project tree to open it in an editor, and update the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        stdio::write(&amp;quot;Hello World!\n&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run the program using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Run in window Alt+F5&amp;#039;&amp;#039;&amp;#039;.}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; file contains other stuff, which we will currently disregard, and focus solely on the &amp;lt;vp&amp;gt;run clauses&amp;lt;/vp&amp;gt; you just wrote.&lt;br /&gt;
&lt;br /&gt;
First we have the keyword &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;, which simly indicates that what comes after are &amp;#039;&amp;#039;&amp;#039;clauses&amp;#039;&amp;#039;&amp;#039;.  Clauses is where the actual running code is written.&lt;br /&gt;
&lt;br /&gt;
In this text I will use words like &amp;quot;clause&amp;quot; and &amp;quot;predicate&amp;quot;, which all have their origin in formal logic, but for the moment we will simply treat them as the names that happens to be chosen for things in a Prolog program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; are used to define &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;, where a &amp;#039;&amp;#039;predicate&amp;#039;&amp;#039; is a Prolog routine/subroutine/function/procedure (dear child has many names)&lt;br /&gt;
&lt;br /&gt;
This particular clause defines a predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; which takes no arguments (indicated by the empty parentheses &amp;lt;vp&amp;gt;()&amp;lt;/vp&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
After the &amp;lt;vp&amp;gt;:-&amp;lt;/vp&amp;gt; token we have the code that will be executed when &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is invoked/called.&lt;br /&gt;
&lt;br /&gt;
In this case we call a predicate named &amp;lt;vp&amp;gt;write&amp;lt;/vp&amp;gt; which is defined in the &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; named &amp;lt;vp&amp;gt;stdio&amp;lt;/vp&amp;gt;.  We will discuss classes later for now we will just consider &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; as &amp;quot;a long compound name&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
We supply one argument, i.e. the string literal &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, to the &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; predicate.  Notice that \n in the string literal represents a line shift.&lt;br /&gt;
&lt;br /&gt;
The clause is terminated by a period (i.e. &amp;lt;vp&amp;gt;.&amp;lt;/vp&amp;gt;).  Period is used as terminator for many kinds of entities in Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
The predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is called when the program is run, and acts as our entry point to the program.  Actually, the real entry point is the &amp;lt;vp&amp;gt;goal&amp;lt;/vp&amp;gt; a further down in the main.pro file, but that entry point is used to make varous initializations, corresponding clean-up, etc and will call the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in the right spot, so run can act like your entry point.&lt;br /&gt;
&lt;br /&gt;
So when the program is run the predicate &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; in &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039; will be called and this will (here) call &lt;br /&gt;
&amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; with the argument &amp;lt;vp&amp;gt;&amp;quot;Hello World!\n&amp;quot;&amp;lt;/vp&amp;gt;, as a result &amp;#039;&amp;#039;Hello World!&amp;#039;&amp;#039; and a line shift will be written to the console window.&lt;br /&gt;
&lt;br /&gt;
Actually, &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; writes to the standard output stream, which in a console program is initialized to be the console window.  In a default GUI program the standard output stream will write to a message window, but the standard output stream is under programmer control, so what happens when you call &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; will depend of what has taken place in the program.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* The actual running code is in &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt; is the Visual Prolog name for routines/subroutines/functions/procedures.&lt;br /&gt;
* Predicates are organized in classes, and predicates from other classes is referenced using &amp;lt;vp&amp;gt;&amp;lt;class&amp;gt;::&amp;lt;predicate&amp;gt;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
* The &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate in &amp;lt;vp&amp;gt;main.pro&amp;lt;/vp&amp;gt; is the (effective) entry to a program.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::write&amp;lt;/vp&amp;gt; can write to the standard output stream&lt;br /&gt;
&lt;br /&gt;
Now let us extend the program with an extra predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Add this code below the &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
    advancedHello : (string Name).&lt;br /&gt;
clauses&lt;br /&gt;
    advancedHello(Name) :-&lt;br /&gt;
        stdio::writef(&amp;quot;Hello %!\n&amp;quot;, Name).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Build the project using &amp;#039;&amp;#039;&amp;#039;Build -&amp;gt; Build Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039;}}&lt;br /&gt;
&lt;br /&gt;
When you build the project you will get a warning stating that the predicate &amp;lt;vp&amp;gt;main::advancedHello&amp;lt;/vp&amp;gt; is an unused local predicate.  Which is completely correct; we haven&amp;#039;t used it for anything.  But let us &amp;quot;decipher&amp;quot; it before we go any further.&lt;br /&gt;
&lt;br /&gt;
We notice that we again have a clause in a &amp;lt;vp&amp;gt;clauses&amp;lt;/vp&amp;gt; section.  The clause is for a predicate named &amp;lt;vp&amp;gt; advancedHello&amp;lt;/vp&amp;gt; that takes one argument called &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is a variable, a variable starts with an uppercase letter and will be green in the editor (variables can also start with an underscore, but we will discuss that later).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; acts as a formal parameter representing the actual argument used in the call to &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;.  In this clause we call &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; which is a more advanced &amp;quot;writer&amp;quot; predicate.  The first argument to &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is a format string, which contains a %-character, what happens when &amp;lt;vp&amp;gt;writef&amp;lt;/vp&amp;gt; is called is that the actual value of &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; is substituted into the format string in the place where the %-character is and then the resulting string is written to the standard output.&lt;br /&gt;
&lt;br /&gt;
This time we also have a &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; section containing a declaration of the &amp;lt;vp&amp;gt;advacedHello&amp;lt;/vp&amp;gt; predicate.  This perticular declaration declares &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt; as a class predicate which takes one argument which must be a string.  &lt;br /&gt;
&lt;br /&gt;
In the predicate declaration the argument also have the name &amp;lt;vp&amp;gt;Name&amp;lt;/vp&amp;gt; but in the declaration the name is mainly intended for documentation purposes.  The variable name in teh declaration and in the clauses can be different, or you can remove it completely.  I reccoment that you always have names here and that you use names that will make it clearer for the user of the predicate what the arguments represents.&lt;br /&gt;
&lt;br /&gt;
What it means to be a &amp;#039;&amp;#039;class&amp;#039;&amp;#039; predicate and what else a predicate can be is discussed elsewhere/later.  But before we leave the subject try this little experiement:&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Remove the word &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; from &amp;lt;vp&amp;gt;class predicates&amp;lt;/vp&amp;gt; in the delcaration of advancedHello. And then build the project.&lt;br /&gt;
&lt;br /&gt;
Notice the error message and remember that you get this message when you have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Then insert &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; again and build the project again.}}&lt;br /&gt;
&lt;br /&gt;
Now that we have the advancedHello predicate we can use it in our &amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; predicate.&lt;br /&gt;
&lt;br /&gt;
{{Lessons/Exercise|Update the run clause to look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        advancedHello(&amp;quot;Dear&amp;quot;),&lt;br /&gt;
        advancedHello(&amp;quot;World&amp;quot;).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
Run the project in a window.}}&lt;br /&gt;
&lt;br /&gt;
The clause should be understandable, I just want to highlight that we have used &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; to separate the two invocations of &amp;lt;vp&amp;gt;advancedHello&amp;lt;/vp&amp;gt;: &amp;#039;&amp;#039;&amp;#039;comma&amp;quot; to separate, &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039; to terminate. I will give a little prediction:  Later when you are familiar with Visual Prolog you will from time to time make syntax errors and (the prediction) 9 out of 10 times the syntax error will concern &amp;#039;&amp;#039;&amp;#039;comma&amp;#039;&amp;#039;&amp;#039; and/or &amp;#039;&amp;#039;&amp;#039;period&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
To summarize:&lt;br /&gt;
&lt;br /&gt;
* Predicates must have a declaration (&amp;lt;vp&amp;gt;run&amp;lt;/vp&amp;gt; is also declared we just havn&amp;#039;t looked at its declaration).&lt;br /&gt;
* Arguments in predicates declarations can be given names that clarify the meaning if the arguments.&lt;br /&gt;
* &amp;lt;vp&amp;gt;stdio::writef&amp;lt;/vp&amp;gt; writes text to the standard output stream using a format string.&lt;br /&gt;
* calls are separated by comma and clauses (and predicate declarations) are terminated by period.&lt;br /&gt;
* If I get a syntax error it is likely to be related to comma or period.&lt;br /&gt;
* A certain other error message (can you recall it) probably means that I have forgotten to write &amp;lt;vp&amp;gt;class&amp;lt;/vp&amp;gt; in front of &amp;lt;vp&amp;gt;predicates&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[[Category:Lessons|{{SUBPAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>Jonas Sonn Jørgensen</name></author>
	</entry>
</feed>