<?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=Gukalov</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=Gukalov"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Special:Contributions/Gukalov"/>
	<updated>2026-04-09T14:46:18Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3849</id>
		<title>Fundamental Prolog Part 2</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3849"/>
		<updated>2013-09-18T16:26:19Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Recursion Using Functors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial you will learn about some more fundamental ideas of Prolog programming; continuing from where we left off in the Part 1 of this tutorial (Fundamental Prolog). In this tutorial, we are going to concentrate on how data is &amp;#039;&amp;#039;modeled&amp;#039;&amp;#039; in Prolog, before we can perform actions on them. Hence, there are not much examples concerning code execution here. We are assuming that you are already familiar with the issues concerning &amp;#039;&amp;#039;&amp;#039;execution strategy&amp;#039;&amp;#039;&amp;#039; and how &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; convert the logic of a Prolog program into the desired &amp;#039;&amp;#039;&amp;#039;results&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As in the [[Fundamental Prolog Part 1|Part 1]] of this tutorial, we shall continue to use the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; environment to develop and learn Prolog. We shall get into the &amp;#039;&amp;#039;&amp;#039;Visual Prolog IDE&amp;#039;&amp;#039;&amp;#039; only in the latter parts of this series.&lt;br /&gt;
&lt;br /&gt;
==Functors==&lt;br /&gt;
&lt;br /&gt;
In Part 1 of the tutorial, all the people were represented as &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; etc. Now &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; are just the names of the individuals. The values of the names are simple data types or &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;. In the case of the names of the people, the &amp;#039;&amp;#039;&amp;#039;simple domain&amp;#039;&amp;#039;&amp;#039; was a &amp;#039;&amp;#039;&amp;#039;string&amp;#039;&amp;#039;&amp;#039;. Other &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; would be &amp;#039;&amp;#039;&amp;#039;numbers&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;123&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;3.14&amp;#039;&amp;#039;&amp;#039;), &amp;#039;&amp;#039;&amp;#039;symbols&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;xyz&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;chil_10&amp;#039;&amp;#039;&amp;#039;), and &amp;#039;&amp;#039;&amp;#039;characters&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;5&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;c&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
However, individuals are represented by more characteristics than just their names. What if we need to represent all those characteristics together, instead of representing just their names? That means, we need some mechanism to represent &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;; a collection of simpler &amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039; held together.&lt;br /&gt;
&lt;br /&gt;
In the first part of this tutorial, we had tried putting together several characteristics of individuals together, (like the &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;&amp;#039;gender&amp;#039;&amp;#039;&amp;#039;), by inserting facts into the PIE system that concentrated on &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; instead of relationships. Thus we had given the following facts to the system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, there is another elegant method to sharpen our focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; being represented.&lt;br /&gt;
&lt;br /&gt;
We can package both the Name and the Gender into a package using a formalization known as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039;. The entire package can then be represented using a logical variable inside any Prolog clause, just like any other variables. For example, the above facts can be generically represented in a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the above statement is neither a fact nor a predicate. Logically, it states that there is some &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; called &amp;#039;&amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039; in the system, each of which has two characteristics, represented by the logical variables Name and Gender. The word &amp;#039;&amp;#039;person&amp;#039;&amp;#039; is known as a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, and the variables are its &amp;#039;&amp;#039;&amp;#039;arguments&amp;#039;&amp;#039;&amp;#039;. We shall now package our facts using these &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; always has a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, we shall henceforth use the term &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in this tutorial, to represent the respective &amp;#039;&amp;#039;&amp;#039;compound domain.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
For now, let us modify the first example of the previous tutorial, so that we use our newly defined &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Please note that in the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; (Prolog Inference Engine) that we are using, we can directly use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; without any prior intimation to the Prolog engine (for example. we need NOT define a predicate or a fact called &amp;#039;&amp;#039;person&amp;#039;&amp;#039; for our code to work).&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig1.png]]&lt;br /&gt;
&lt;br /&gt;
If you study the two facts for the father relationship, you would notice that the persons described are richer than what was done before (The earlier code is commented out in-between the tokens &amp;#039;&amp;#039;&amp;#039;/*&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;*/&amp;#039;&amp;#039;&amp;#039; .). This time, each person is described with both the person&amp;#039;s name &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; his/her gender, using the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whereas in the earlier tutorial (Fundamental Prolog (Part1)) we were only using the person&amp;#039;s name.&lt;br /&gt;
&lt;br /&gt;
After you have written the new code, ensure that the PIE engine is reset. Use &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reset.&amp;#039;&amp;#039;&amp;#039; Then, you re-consult the code using &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reconsult.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
As before, on a blank line in the &amp;#039;&amp;#039;&amp;#039;Dialog&amp;#039;&amp;#039;&amp;#039; window type a goal (without the &amp;quot;?&amp;quot; sign- in front).&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig2.png]]&lt;br /&gt;
&lt;br /&gt;
When the caret is placed at the end of the line, press the &amp;#039;&amp;#039;&amp;#039;Enter&amp;#039;&amp;#039;&amp;#039; key on your keyboard. &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; will now consider the text from the beginning of the line to the caret as a goal to execute. You should see the following result:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3.png]]&lt;br /&gt;
&lt;br /&gt;
Now, you will notice that the results that are yielded are richer than what you got previously (See Tutorial 04: Fundamental Prolog (Part 1)).&lt;br /&gt;
&lt;br /&gt;
==Refining Further==&lt;br /&gt;
&lt;br /&gt;
If you take a look at the grandfather predicate, you would notice that it has a subtle bug: A person would have two grandfathers: one from the mother&amp;#039;s side and one from the father&amp;#039;s side, but the grandfather predicate as defined earlier would only yield the grandfather on the father&amp;#039;s side.&lt;br /&gt;
&lt;br /&gt;
Hence the grandfather predicate should be re-written as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, TheGrandFather):-&lt;br /&gt;
     parent(Person, ParentOfPerson), &lt;br /&gt;
     father(ParentOfPerson, TheGrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this predicate the logic states that a father of &amp;#039;&amp;#039;any&amp;#039;&amp;#039; parent could be the grandfather of the person in consideration.&lt;br /&gt;
&lt;br /&gt;
For that predicate to work, we need to define a predicate called father using the person &amp;#039;&amp;#039;&amp;#039;functor.&amp;#039;&amp;#039;&amp;#039; This predicate would troll through a database of facts explaining the &amp;quot;&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;&amp;quot; defined in the system. This is a more elegant method for finding out the fathers, instead of presenting them as facts (as shown previously) because later on we can extend this concept to find out &amp;quot;&amp;#039;&amp;#039;mothers&amp;#039;&amp;#039;&amp;quot; in a similar fashion.&lt;br /&gt;
&lt;br /&gt;
This can be done in either of the following ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/*1st version */&lt;br /&gt;
father(P, F) :-&lt;br /&gt;
    parent(P, F), &lt;br /&gt;
    F = person(_, &amp;quot;male&amp;quot;).    %Line 2&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* 2nd version */&lt;br /&gt;
father(P, person(Name, &amp;quot;male&amp;quot;)) :-&lt;br /&gt;
    parent(P, person(Name, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logic behind both the versions is the same, but the manner in which the logic is indicated to the Prolog engine is different. In the first version, the code in Prolog systematically examines each parent fact asserted into the code, and sees if the first logical variable (P) matches that passed down from the predicate head. If the variable does match, it checks if the second argument consists of a person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whose second argument is the string literal &amp;quot;male&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
This example shows one important feature of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: The arguments of a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; can be taken apart and examined using regular Prolog variables and bound values (like the string literal in this example) If you see Line 2 (Look at the side of the comment &amp;#039;&amp;#039;%Line 2&amp;#039;&amp;#039; in the code for the first version), you would notice that we have used an anonymous variable (the underscore) for the first argument of the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as we are (in that predicate) not interested in the name of the father.&lt;br /&gt;
&lt;br /&gt;
The second version also does the same thing as the first one. But this time, as the set of parent facts is examined; on arriving at the correct value for P, the code halts and returns the correct &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; data back to the predicate head, &amp;#039;&amp;#039;provided the second argument of that &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is the string literal &amp;quot;male&amp;quot;&amp;#039;&amp;#039;. If you note, the functor was NOT bound to any intermediate Prolog variable, as it was done in the first version.&lt;br /&gt;
&lt;br /&gt;
The second version is much terser than the first one, and sometimes this method of writing the code can be less legible to beginners.&lt;br /&gt;
&lt;br /&gt;
Let us now use this code in a complete example, where we will give a suitable set of person&amp;#039;&amp;#039;&amp;#039;&amp;#039; facts:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3b.png]]&lt;br /&gt;
&lt;br /&gt;
Now, after you re-consult the above code into &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, and give the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(person&amp;#039;(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), W)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the results would be as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3c.png]]&lt;br /&gt;
&lt;br /&gt;
==Functors and Predicates==&lt;br /&gt;
&lt;br /&gt;
Technically, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; represents a logical function that binds several domains together. In simpler words, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is a mechanism that teaches the Prolog Engine how to put together data from its component parts. It effectively puts parts of the data into a common box. You can also retrieve the parts subsequently, whenever required (like seen the previous examples). It may look like a Prolog fact or a predicate call, but it is not. It&amp;#039;s just a piece of data, which you can handle in much the same way as a string or a number.&lt;br /&gt;
&lt;br /&gt;
Please note that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has nothing to do with a function in other programming languages. It does not stand for some computation to be performed. It simply identifies the &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; and holds its arguments together.&lt;br /&gt;
&lt;br /&gt;
When you study the above example, you would notice that nothing special need to be done when logical variables are used to stand in for data represented by &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. The logical variable that is used to stand in for such data, is written just like any other logical variable: A word starting with a capital letter. Thus, if we take a look at the &amp;#039;&amp;#039;grandFather&amp;#039;&amp;#039; predicate in the example in this tutorial; you would notice that nothing has changed when you compare it with the same predicate that was defined in the earlier tutorial (Fundamental Prolog (part 1)). After all, the logic of that predicate has not changed. So, you would not find any changes to the variables used inside that predicate.&lt;br /&gt;
&lt;br /&gt;
The biggest advantage of using &amp;#039;&amp;#039;&amp;#039;a functor&amp;#039;&amp;#039;&amp;#039; is that you are free to change the internal arguments of the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; without changing much of the predicates that uses such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as you keep revising the code.&lt;br /&gt;
&lt;br /&gt;
That means, if you decide (in a later version of the code you are writing) to have one more argument for the &amp;#039;&amp;#039;person&amp;#039;&amp;#039;, you still need not change anything in the &amp;#039;&amp;#039;grandfather&amp;#039;&amp;#039; predicate.&lt;br /&gt;
&lt;br /&gt;
==Functors as Arguments==&lt;br /&gt;
&lt;br /&gt;
In the previous section, the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; was having two arguments: the Name and the Gender. Both happened to be simple domains i.e. &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039; such as &amp;quot;Bill&amp;quot; and, &amp;quot;male&amp;quot;. However, there is nothing that prevents us from putting a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; itself as an &amp;#039;&amp;#039;&amp;#039;argument&amp;#039;&amp;#039;&amp;#039; of another &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Suppose, you wanted to define a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; for a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; (i.e. a husband and a wife). This is an example of how you would use such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple = couple(person(Husband, &amp;quot;male&amp;quot;), person(Wife, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, you would notice that the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is defined by two &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, each of which is a mixture of &amp;#039;&amp;#039;&amp;#039;variables&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039;. This can be done to reflect the logic of the data being represented. The logic used here is that a husband is always a &amp;quot;male&amp;quot; and wife is always a &amp;quot;female&amp;quot;; and a couple consists of a husband and a wife. All of which is consistent with the most common interpretation of what one means by a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Though in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; you cannot predefine the kind of grammar &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can actually have; in &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039; you can make such definitions. The advantage of defining a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in such a manner is that later, when you create actual data using this &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, the Prolog engine will always check if the data being created was consistent with the grammar that data was supposed to adhere to.&lt;br /&gt;
&lt;br /&gt;
This comes to another characteristic of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has been defined with two arguments and the &amp;#039;&amp;#039;&amp;#039;position&amp;#039;&amp;#039;&amp;#039; of these arguments reflects their logical association. It was explained in the first part of this tutorial, that the positions of the arguments of predicates have to be formalized by the programmer, who designs the code. Once formalized; the same positional formalization should be consistently used.&lt;br /&gt;
&lt;br /&gt;
The same strategy applies to the creation of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; too: In the case of the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, it so happened that we decided to represent the husband as the first argument, and the wife as the second one. Once this is done, then whenever data is created using such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, then we need to ensure that the husband is always at the first position, and the wife in the second (in spite of what the women&amp;#039;s lib people may say!)&lt;br /&gt;
&lt;br /&gt;
Now, when we look back on the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, someone can correctly argue that if were so sure that the first argument is always a husband (who is always a male), and the second one is always a wife (who is always a female) then what is the need for making the arguments of the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; also as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;? Hence, an example with a more simplified &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; would be as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple=couple(Husband, Wife), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence; let us revert back to using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; for the husband and wife... but this time we will use them, because we are not sure which position is for the husband and which is for the wife.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(Couple):-&lt;br /&gt;
     Couple = couple(person(PersonsName, PersonsGender), person(SpouseName, SpouseGender)), &lt;br /&gt;
     ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, both the following examples make logical sense:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(C1):-&lt;br /&gt;
    C1=couple(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
/* or */&lt;br /&gt;
myPredicate(C2):-&lt;br /&gt;
    C2=couple(person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It should be pointed out that in the PIE (and many other Prolog Engines), there is no way to indicate whether a functor will receive simple domain or compound domain arguments, by looking at the variables that define the functor. This stems from the fact that in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; are directly used, without being declared anywhere, other than in the mind of the programmer.&lt;br /&gt;
&lt;br /&gt;
For example, if you wanted to use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; would as easily accept a logical variable such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;regardingAPerson(Somebody):-&lt;br /&gt;
    Somebody = &lt;br /&gt;
        person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
            person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                   person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        ), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which actually does not make any logical sense, in the current context. Luckily, &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039;, does a much better job of differentiating between &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;, so, when you get to the latter tutorials, you would not have any such problems.&lt;br /&gt;
&lt;br /&gt;
==Recursion Using Functors==&lt;br /&gt;
&lt;br /&gt;
When data is described using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, it can be treated like any other piece of data. For example, you can write a predicate that can be made to recursively search through data which used &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Let us get back briefly to the ancestor predicate, which we had used in the first part of the tutorial.&lt;br /&gt;
&lt;br /&gt;
In order to determine if somebody is an ancestor of someone else, we used a recursive definition for the predicate, i.e. a definition that is defined in terms of itself. like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ancestor(Person, Ancestor) :- parent(Person, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This declaration states that a parent is an ancestor, and that an ancestor to a parent is also an ancestor. If you examine the variables in the above definition, it can very well stand for either &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;compound domains.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Let us define the data thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;)).&lt;br /&gt;
parent(person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we now ask &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; to solve the following goal, &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;P=person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), ancestor(P, Who)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...this is what we will get:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig4.png]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; engine has recursively examined the parent facts to reveal the two possible solutions to the answer. By the way, in the above query, you would also notice that we have bound a variable P to the person &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; when specifying the goal to &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;. This was done to make the code more readable but it also demonstrates the fact that we can bind a piece of data specified as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; into any Prolog variable.&lt;br /&gt;
&lt;br /&gt;
==Strategies for Using Functors==&lt;br /&gt;
&lt;br /&gt;
Software will be only as good as allowed by the modeled data. By the term &amp;#039;&amp;#039;modeling&amp;#039;&amp;#039;, we mean the establishment of a relationship between the subset of the outside real-world that is being tackled with the internal data structures of the software. If the modeling of data is poor, then it is likely that the software will also be poor; or at best, inefficient. This is true of any software written in any programming language. That was the reason in the latter part of the earlier tutorial (Fundamental Prolog (part 1)), we had tried to focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; and tried to correct the situation by inserting richer facts. Similarly, we introduced the concept of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; in this tutorial to get even more clarity on the data regarding &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; that are being modeled.&lt;br /&gt;
&lt;br /&gt;
The advantage of Prolog is that it allows easy &amp;#039;&amp;#039;&amp;#039;description&amp;#039;&amp;#039;&amp;#039; of the real-world data in a form that can be internally utilized by the code in an efficient manner. Simultaneously, it also makes the code very readable by fellow programmers who are working together on a project.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Functors&amp;#039;&amp;#039;&amp;#039; can be used to create any type of &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; to help in this modeling process. You would have to carefully examine the various parts of real-world data that you plan to process and convert them using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data types that you would encounter in future tutorials) keeping in mind their usage in all critical parts of the software. Some data structures that were useful in one area of the software may prove to be a hindrance in other areas.&lt;br /&gt;
&lt;br /&gt;
You should take a holistic approach while zeroing in on the &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data structures) you plan to use in your software. Pause and look around at all the corners of your software. Only then should you code the necessary &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Just thinking carefully about the data structures is not the only thing that is required. You would also need to (often simultaneously) write/modify the various goals and sub-goals (i.e. predicates) and then use the data developed thus far in those predicates. The &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; of attaining those goals and sub-goals would make your software work and you can get valuable feedback with which you can further refine your data structures.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we have not fleshed out a software design holistically. We have only nibbled at bits and pieces of data which establishes the &amp;#039;&amp;#039;&amp;#039;feel&amp;#039;&amp;#039;&amp;#039; for some kind of real-world data concerning relationship between people (&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;grandparents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;families&amp;#039;&amp;#039;, &amp;#039;&amp;#039;ancestors&amp;#039;&amp;#039; etc.) In forthcoming tutorials, we will use this experience to establish a &amp;#039;&amp;#039;&amp;#039;red thread&amp;#039;&amp;#039;&amp;#039; that connects all these tutorials together; eventually ending up with a useful software that requires such data structures.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In this lesson we learnt that data can comprise of &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;, or they can be &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; represented using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. We found that the arguments of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; must be positionally consistent with the logic they were meant to represent. We understood that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; need not have just simple domains as arguments, but it can also have other &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; as arguments. We then learnt that data represented as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can be used like any regular Prolog variable, and we can even perform all operations; including recursion, on such data.&lt;br /&gt;
&lt;br /&gt;
We also learnt that we must spend time modeling our subset of the real world for which we are developing the software, and get a feel for the data. We should simultaneously experiment with the predicates that you may wish to use with the data that is being modeled, so that they can be refined further.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Fundamental Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3848</id>
		<title>Fundamental Prolog Part 2</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3848"/>
		<updated>2013-09-18T16:25:50Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Refining Further */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial you will learn about some more fundamental ideas of Prolog programming; continuing from where we left off in the Part 1 of this tutorial (Fundamental Prolog). In this tutorial, we are going to concentrate on how data is &amp;#039;&amp;#039;modeled&amp;#039;&amp;#039; in Prolog, before we can perform actions on them. Hence, there are not much examples concerning code execution here. We are assuming that you are already familiar with the issues concerning &amp;#039;&amp;#039;&amp;#039;execution strategy&amp;#039;&amp;#039;&amp;#039; and how &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; convert the logic of a Prolog program into the desired &amp;#039;&amp;#039;&amp;#039;results&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As in the [[Fundamental Prolog Part 1|Part 1]] of this tutorial, we shall continue to use the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; environment to develop and learn Prolog. We shall get into the &amp;#039;&amp;#039;&amp;#039;Visual Prolog IDE&amp;#039;&amp;#039;&amp;#039; only in the latter parts of this series.&lt;br /&gt;
&lt;br /&gt;
==Functors==&lt;br /&gt;
&lt;br /&gt;
In Part 1 of the tutorial, all the people were represented as &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; etc. Now &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; are just the names of the individuals. The values of the names are simple data types or &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;. In the case of the names of the people, the &amp;#039;&amp;#039;&amp;#039;simple domain&amp;#039;&amp;#039;&amp;#039; was a &amp;#039;&amp;#039;&amp;#039;string&amp;#039;&amp;#039;&amp;#039;. Other &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; would be &amp;#039;&amp;#039;&amp;#039;numbers&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;123&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;3.14&amp;#039;&amp;#039;&amp;#039;), &amp;#039;&amp;#039;&amp;#039;symbols&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;xyz&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;chil_10&amp;#039;&amp;#039;&amp;#039;), and &amp;#039;&amp;#039;&amp;#039;characters&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;5&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;c&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
However, individuals are represented by more characteristics than just their names. What if we need to represent all those characteristics together, instead of representing just their names? That means, we need some mechanism to represent &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;; a collection of simpler &amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039; held together.&lt;br /&gt;
&lt;br /&gt;
In the first part of this tutorial, we had tried putting together several characteristics of individuals together, (like the &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;&amp;#039;gender&amp;#039;&amp;#039;&amp;#039;), by inserting facts into the PIE system that concentrated on &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; instead of relationships. Thus we had given the following facts to the system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, there is another elegant method to sharpen our focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; being represented.&lt;br /&gt;
&lt;br /&gt;
We can package both the Name and the Gender into a package using a formalization known as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039;. The entire package can then be represented using a logical variable inside any Prolog clause, just like any other variables. For example, the above facts can be generically represented in a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the above statement is neither a fact nor a predicate. Logically, it states that there is some &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; called &amp;#039;&amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039; in the system, each of which has two characteristics, represented by the logical variables Name and Gender. The word &amp;#039;&amp;#039;person&amp;#039;&amp;#039; is known as a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, and the variables are its &amp;#039;&amp;#039;&amp;#039;arguments&amp;#039;&amp;#039;&amp;#039;. We shall now package our facts using these &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; always has a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, we shall henceforth use the term &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in this tutorial, to represent the respective &amp;#039;&amp;#039;&amp;#039;compound domain.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
For now, let us modify the first example of the previous tutorial, so that we use our newly defined &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Please note that in the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; (Prolog Inference Engine) that we are using, we can directly use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; without any prior intimation to the Prolog engine (for example. we need NOT define a predicate or a fact called &amp;#039;&amp;#039;person&amp;#039;&amp;#039; for our code to work).&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig1.png]]&lt;br /&gt;
&lt;br /&gt;
If you study the two facts for the father relationship, you would notice that the persons described are richer than what was done before (The earlier code is commented out in-between the tokens &amp;#039;&amp;#039;&amp;#039;/*&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;*/&amp;#039;&amp;#039;&amp;#039; .). This time, each person is described with both the person&amp;#039;s name &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; his/her gender, using the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whereas in the earlier tutorial (Fundamental Prolog (Part1)) we were only using the person&amp;#039;s name.&lt;br /&gt;
&lt;br /&gt;
After you have written the new code, ensure that the PIE engine is reset. Use &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reset.&amp;#039;&amp;#039;&amp;#039; Then, you re-consult the code using &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reconsult.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
As before, on a blank line in the &amp;#039;&amp;#039;&amp;#039;Dialog&amp;#039;&amp;#039;&amp;#039; window type a goal (without the &amp;quot;?&amp;quot; sign- in front).&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig2.png]]&lt;br /&gt;
&lt;br /&gt;
When the caret is placed at the end of the line, press the &amp;#039;&amp;#039;&amp;#039;Enter&amp;#039;&amp;#039;&amp;#039; key on your keyboard. &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; will now consider the text from the beginning of the line to the caret as a goal to execute. You should see the following result:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3.png]]&lt;br /&gt;
&lt;br /&gt;
Now, you will notice that the results that are yielded are richer than what you got previously (See Tutorial 04: Fundamental Prolog (Part 1)).&lt;br /&gt;
&lt;br /&gt;
==Refining Further==&lt;br /&gt;
&lt;br /&gt;
If you take a look at the grandfather predicate, you would notice that it has a subtle bug: A person would have two grandfathers: one from the mother&amp;#039;s side and one from the father&amp;#039;s side, but the grandfather predicate as defined earlier would only yield the grandfather on the father&amp;#039;s side.&lt;br /&gt;
&lt;br /&gt;
Hence the grandfather predicate should be re-written as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, TheGrandFather):-&lt;br /&gt;
     parent(Person, ParentOfPerson), &lt;br /&gt;
     father(ParentOfPerson, TheGrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this predicate the logic states that a father of &amp;#039;&amp;#039;any&amp;#039;&amp;#039; parent could be the grandfather of the person in consideration.&lt;br /&gt;
&lt;br /&gt;
For that predicate to work, we need to define a predicate called father using the person &amp;#039;&amp;#039;&amp;#039;functor.&amp;#039;&amp;#039;&amp;#039; This predicate would troll through a database of facts explaining the &amp;quot;&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;&amp;quot; defined in the system. This is a more elegant method for finding out the fathers, instead of presenting them as facts (as shown previously) because later on we can extend this concept to find out &amp;quot;&amp;#039;&amp;#039;mothers&amp;#039;&amp;#039;&amp;quot; in a similar fashion.&lt;br /&gt;
&lt;br /&gt;
This can be done in either of the following ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/*1st version */&lt;br /&gt;
father(P, F) :-&lt;br /&gt;
    parent(P, F), &lt;br /&gt;
    F = person(_, &amp;quot;male&amp;quot;).    %Line 2&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* 2nd version */&lt;br /&gt;
father(P, person(Name, &amp;quot;male&amp;quot;)) :-&lt;br /&gt;
    parent(P, person(Name, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logic behind both the versions is the same, but the manner in which the logic is indicated to the Prolog engine is different. In the first version, the code in Prolog systematically examines each parent fact asserted into the code, and sees if the first logical variable (P) matches that passed down from the predicate head. If the variable does match, it checks if the second argument consists of a person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whose second argument is the string literal &amp;quot;male&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
This example shows one important feature of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: The arguments of a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; can be taken apart and examined using regular Prolog variables and bound values (like the string literal in this example) If you see Line 2 (Look at the side of the comment &amp;#039;&amp;#039;%Line 2&amp;#039;&amp;#039; in the code for the first version), you would notice that we have used an anonymous variable (the underscore) for the first argument of the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as we are (in that predicate) not interested in the name of the father.&lt;br /&gt;
&lt;br /&gt;
The second version also does the same thing as the first one. But this time, as the set of parent facts is examined; on arriving at the correct value for P, the code halts and returns the correct &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; data back to the predicate head, &amp;#039;&amp;#039;provided the second argument of that &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is the string literal &amp;quot;male&amp;quot;&amp;#039;&amp;#039;. If you note, the functor was NOT bound to any intermediate Prolog variable, as it was done in the first version.&lt;br /&gt;
&lt;br /&gt;
The second version is much terser than the first one, and sometimes this method of writing the code can be less legible to beginners.&lt;br /&gt;
&lt;br /&gt;
Let us now use this code in a complete example, where we will give a suitable set of person&amp;#039;&amp;#039;&amp;#039;&amp;#039; facts:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3b.png]]&lt;br /&gt;
&lt;br /&gt;
Now, after you re-consult the above code into &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, and give the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(person&amp;#039;(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), W)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the results would be as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3c.png]]&lt;br /&gt;
&lt;br /&gt;
==Functors and Predicates==&lt;br /&gt;
&lt;br /&gt;
Technically, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; represents a logical function that binds several domains together. In simpler words, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is a mechanism that teaches the Prolog Engine how to put together data from its component parts. It effectively puts parts of the data into a common box. You can also retrieve the parts subsequently, whenever required (like seen the previous examples). It may look like a Prolog fact or a predicate call, but it is not. It&amp;#039;s just a piece of data, which you can handle in much the same way as a string or a number.&lt;br /&gt;
&lt;br /&gt;
Please note that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has nothing to do with a function in other programming languages. It does not stand for some computation to be performed. It simply identifies the &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; and holds its arguments together.&lt;br /&gt;
&lt;br /&gt;
When you study the above example, you would notice that nothing special need to be done when logical variables are used to stand in for data represented by &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. The logical variable that is used to stand in for such data, is written just like any other logical variable: A word starting with a capital letter. Thus, if we take a look at the &amp;#039;&amp;#039;grandFather&amp;#039;&amp;#039; predicate in the example in this tutorial; you would notice that nothing has changed when you compare it with the same predicate that was defined in the earlier tutorial (Fundamental Prolog (part 1)). After all, the logic of that predicate has not changed. So, you would not find any changes to the variables used inside that predicate.&lt;br /&gt;
&lt;br /&gt;
The biggest advantage of using &amp;#039;&amp;#039;&amp;#039;a functor&amp;#039;&amp;#039;&amp;#039; is that you are free to change the internal arguments of the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; without changing much of the predicates that uses such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as you keep revising the code.&lt;br /&gt;
&lt;br /&gt;
That means, if you decide (in a later version of the code you are writing) to have one more argument for the &amp;#039;&amp;#039;person&amp;#039;&amp;#039;, you still need not change anything in the &amp;#039;&amp;#039;grandfather&amp;#039;&amp;#039; predicate.&lt;br /&gt;
&lt;br /&gt;
==Functors as Arguments==&lt;br /&gt;
&lt;br /&gt;
In the previous section, the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; was having two arguments: the Name and the Gender. Both happened to be simple domains i.e. &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039; such as &amp;quot;Bill&amp;quot; and, &amp;quot;male&amp;quot;. However, there is nothing that prevents us from putting a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; itself as an &amp;#039;&amp;#039;&amp;#039;argument&amp;#039;&amp;#039;&amp;#039; of another &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Suppose, you wanted to define a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; for a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; (i.e. a husband and a wife). This is an example of how you would use such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple = couple(person(Husband, &amp;quot;male&amp;quot;), person(Wife, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, you would notice that the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is defined by two &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, each of which is a mixture of &amp;#039;&amp;#039;&amp;#039;variables&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039;. This can be done to reflect the logic of the data being represented. The logic used here is that a husband is always a &amp;quot;male&amp;quot; and wife is always a &amp;quot;female&amp;quot;; and a couple consists of a husband and a wife. All of which is consistent with the most common interpretation of what one means by a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Though in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; you cannot predefine the kind of grammar &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can actually have; in &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039; you can make such definitions. The advantage of defining a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in such a manner is that later, when you create actual data using this &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, the Prolog engine will always check if the data being created was consistent with the grammar that data was supposed to adhere to.&lt;br /&gt;
&lt;br /&gt;
This comes to another characteristic of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has been defined with two arguments and the &amp;#039;&amp;#039;&amp;#039;position&amp;#039;&amp;#039;&amp;#039; of these arguments reflects their logical association. It was explained in the first part of this tutorial, that the positions of the arguments of predicates have to be formalized by the programmer, who designs the code. Once formalized; the same positional formalization should be consistently used.&lt;br /&gt;
&lt;br /&gt;
The same strategy applies to the creation of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; too: In the case of the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, it so happened that we decided to represent the husband as the first argument, and the wife as the second one. Once this is done, then whenever data is created using such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, then we need to ensure that the husband is always at the first position, and the wife in the second (in spite of what the women&amp;#039;s lib people may say!)&lt;br /&gt;
&lt;br /&gt;
Now, when we look back on the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, someone can correctly argue that if were so sure that the first argument is always a husband (who is always a male), and the second one is always a wife (who is always a female) then what is the need for making the arguments of the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; also as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;? Hence, an example with a more simplified &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; would be as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple=couple(Husband, Wife), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence; let us revert back to using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; for the husband and wife... but this time we will use them, because we are not sure which position is for the husband and which is for the wife.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(Couple):-&lt;br /&gt;
     Couple = couple(person(PersonsName, PersonsGender), person(SpouseName, SpouseGender)), &lt;br /&gt;
     ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, both the following examples make logical sense:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(C1):-&lt;br /&gt;
    C1=couple(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
/* or */&lt;br /&gt;
myPredicate(C2):-&lt;br /&gt;
    C2=couple(person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It should be pointed out that in the PIE (and many other Prolog Engines), there is no way to indicate whether a functor will receive simple domain or compound domain arguments, by looking at the variables that define the functor. This stems from the fact that in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; are directly used, without being declared anywhere, other than in the mind of the programmer.&lt;br /&gt;
&lt;br /&gt;
For example, if you wanted to use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; would as easily accept a logical variable such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;regardingAPerson(Somebody):-&lt;br /&gt;
    Somebody = &lt;br /&gt;
        person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
            person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                   person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        ), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which actually does not make any logical sense, in the current context. Luckily, &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039;, does a much better job of differentiating between &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;, so, when you get to the latter tutorials, you would not have any such problems.&lt;br /&gt;
&lt;br /&gt;
==Recursion Using Functors==&lt;br /&gt;
&lt;br /&gt;
When data is described using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, it can be treated like any other piece of data. For example, you can write a predicate that can be made to recursively search through data which used &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Let us get back briefly to the ancestor predicate, which we had used in the first part of the tutorial.&lt;br /&gt;
&lt;br /&gt;
In order to determine if somebody is an ancestor of someone else, we used a recursive definition for the predicate, i.e. a definition that is defined in terms of itself. like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ancestor(Person, Ancestor) :- parent(Person, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This declaration states that a parent is an ancestor, and that an ancestor to a parent is also an ancestor. If you examine the variables in the above definition, it can very well stand for either &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;compound domains.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Let us define the data thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;)).&lt;br /&gt;
parent(person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we now ask &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; to solve the following goal, &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;P=person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), ancestor(P, Who)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...this is what we will get:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig4.jpg]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; engine has recursively examined the parent facts to reveal the two possible solutions to the answer. By the way, in the above query, you would also notice that we have bound a variable P to the person &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; when specifying the goal to &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;. This was done to make the code more readable but it also demonstrates the fact that we can bind a piece of data specified as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; into any Prolog variable.&lt;br /&gt;
&lt;br /&gt;
==Strategies for Using Functors==&lt;br /&gt;
&lt;br /&gt;
Software will be only as good as allowed by the modeled data. By the term &amp;#039;&amp;#039;modeling&amp;#039;&amp;#039;, we mean the establishment of a relationship between the subset of the outside real-world that is being tackled with the internal data structures of the software. If the modeling of data is poor, then it is likely that the software will also be poor; or at best, inefficient. This is true of any software written in any programming language. That was the reason in the latter part of the earlier tutorial (Fundamental Prolog (part 1)), we had tried to focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; and tried to correct the situation by inserting richer facts. Similarly, we introduced the concept of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; in this tutorial to get even more clarity on the data regarding &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; that are being modeled.&lt;br /&gt;
&lt;br /&gt;
The advantage of Prolog is that it allows easy &amp;#039;&amp;#039;&amp;#039;description&amp;#039;&amp;#039;&amp;#039; of the real-world data in a form that can be internally utilized by the code in an efficient manner. Simultaneously, it also makes the code very readable by fellow programmers who are working together on a project.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Functors&amp;#039;&amp;#039;&amp;#039; can be used to create any type of &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; to help in this modeling process. You would have to carefully examine the various parts of real-world data that you plan to process and convert them using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data types that you would encounter in future tutorials) keeping in mind their usage in all critical parts of the software. Some data structures that were useful in one area of the software may prove to be a hindrance in other areas.&lt;br /&gt;
&lt;br /&gt;
You should take a holistic approach while zeroing in on the &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data structures) you plan to use in your software. Pause and look around at all the corners of your software. Only then should you code the necessary &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Just thinking carefully about the data structures is not the only thing that is required. You would also need to (often simultaneously) write/modify the various goals and sub-goals (i.e. predicates) and then use the data developed thus far in those predicates. The &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; of attaining those goals and sub-goals would make your software work and you can get valuable feedback with which you can further refine your data structures.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we have not fleshed out a software design holistically. We have only nibbled at bits and pieces of data which establishes the &amp;#039;&amp;#039;&amp;#039;feel&amp;#039;&amp;#039;&amp;#039; for some kind of real-world data concerning relationship between people (&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;grandparents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;families&amp;#039;&amp;#039;, &amp;#039;&amp;#039;ancestors&amp;#039;&amp;#039; etc.) In forthcoming tutorials, we will use this experience to establish a &amp;#039;&amp;#039;&amp;#039;red thread&amp;#039;&amp;#039;&amp;#039; that connects all these tutorials together; eventually ending up with a useful software that requires such data structures.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In this lesson we learnt that data can comprise of &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;, or they can be &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; represented using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. We found that the arguments of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; must be positionally consistent with the logic they were meant to represent. We understood that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; need not have just simple domains as arguments, but it can also have other &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; as arguments. We then learnt that data represented as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can be used like any regular Prolog variable, and we can even perform all operations; including recursion, on such data.&lt;br /&gt;
&lt;br /&gt;
We also learnt that we must spend time modeling our subset of the real world for which we are developing the software, and get a feel for the data. We should simultaneously experiment with the predicates that you may wish to use with the data that is being modeled, so that they can be refined further.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Fundamental Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig4.png&amp;diff=3847</id>
		<title>File:Fundamental Prolog 2 fig4.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig4.png&amp;diff=3847"/>
		<updated>2013-09-18T16:25:09Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3c.png&amp;diff=3846</id>
		<title>File:Fundamental Prolog 2 fig3c.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3c.png&amp;diff=3846"/>
		<updated>2013-09-18T16:24:54Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3b.png&amp;diff=3845</id>
		<title>File:Fundamental Prolog 2 fig3b.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3b.png&amp;diff=3845"/>
		<updated>2013-09-18T16:24:38Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3.png&amp;diff=3844</id>
		<title>File:Fundamental Prolog 2 fig3.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig3.png&amp;diff=3844"/>
		<updated>2013-09-18T16:23:56Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3843</id>
		<title>Fundamental Prolog Part 2</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3843"/>
		<updated>2013-09-18T16:22:50Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Functors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial you will learn about some more fundamental ideas of Prolog programming; continuing from where we left off in the Part 1 of this tutorial (Fundamental Prolog). In this tutorial, we are going to concentrate on how data is &amp;#039;&amp;#039;modeled&amp;#039;&amp;#039; in Prolog, before we can perform actions on them. Hence, there are not much examples concerning code execution here. We are assuming that you are already familiar with the issues concerning &amp;#039;&amp;#039;&amp;#039;execution strategy&amp;#039;&amp;#039;&amp;#039; and how &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; convert the logic of a Prolog program into the desired &amp;#039;&amp;#039;&amp;#039;results&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As in the [[Fundamental Prolog Part 1|Part 1]] of this tutorial, we shall continue to use the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; environment to develop and learn Prolog. We shall get into the &amp;#039;&amp;#039;&amp;#039;Visual Prolog IDE&amp;#039;&amp;#039;&amp;#039; only in the latter parts of this series.&lt;br /&gt;
&lt;br /&gt;
==Functors==&lt;br /&gt;
&lt;br /&gt;
In Part 1 of the tutorial, all the people were represented as &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; etc. Now &amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot; and &amp;quot;Pam&amp;quot; are just the names of the individuals. The values of the names are simple data types or &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;. In the case of the names of the people, the &amp;#039;&amp;#039;&amp;#039;simple domain&amp;#039;&amp;#039;&amp;#039; was a &amp;#039;&amp;#039;&amp;#039;string&amp;#039;&amp;#039;&amp;#039;. Other &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; would be &amp;#039;&amp;#039;&amp;#039;numbers&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;123&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;3.14&amp;#039;&amp;#039;&amp;#039;), &amp;#039;&amp;#039;&amp;#039;symbols&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;xyz&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;chil_10&amp;#039;&amp;#039;&amp;#039;), and &amp;#039;&amp;#039;&amp;#039;characters&amp;#039;&amp;#039;&amp;#039; (e.g.: &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;5&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;&amp;amp;#39;c&amp;amp;#39;&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
However, individuals are represented by more characteristics than just their names. What if we need to represent all those characteristics together, instead of representing just their names? That means, we need some mechanism to represent &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;; a collection of simpler &amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039; held together.&lt;br /&gt;
&lt;br /&gt;
In the first part of this tutorial, we had tried putting together several characteristics of individuals together, (like the &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;&amp;#039;gender&amp;#039;&amp;#039;&amp;#039;), by inserting facts into the PIE system that concentrated on &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; instead of relationships. Thus we had given the following facts to the system:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, there is another elegant method to sharpen our focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; being represented.&lt;br /&gt;
&lt;br /&gt;
We can package both the Name and the Gender into a package using a formalization known as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039;. The entire package can then be represented using a logical variable inside any Prolog clause, just like any other variables. For example, the above facts can be generically represented in a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the above statement is neither a fact nor a predicate. Logically, it states that there is some &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; called &amp;#039;&amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039; in the system, each of which has two characteristics, represented by the logical variables Name and Gender. The word &amp;#039;&amp;#039;person&amp;#039;&amp;#039; is known as a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, and the variables are its &amp;#039;&amp;#039;&amp;#039;arguments&amp;#039;&amp;#039;&amp;#039;. We shall now package our facts using these &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
As a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; always has a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, we shall henceforth use the term &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in this tutorial, to represent the respective &amp;#039;&amp;#039;&amp;#039;compound domain.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
For now, let us modify the first example of the previous tutorial, so that we use our newly defined &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;person&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Please note that in the &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; (Prolog Inference Engine) that we are using, we can directly use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; without any prior intimation to the Prolog engine (for example. we need NOT define a predicate or a fact called &amp;#039;&amp;#039;person&amp;#039;&amp;#039; for our code to work).&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig1.png]]&lt;br /&gt;
&lt;br /&gt;
If you study the two facts for the father relationship, you would notice that the persons described are richer than what was done before (The earlier code is commented out in-between the tokens &amp;#039;&amp;#039;&amp;#039;/*&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;*/&amp;#039;&amp;#039;&amp;#039; .). This time, each person is described with both the person&amp;#039;s name &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; his/her gender, using the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whereas in the earlier tutorial (Fundamental Prolog (Part1)) we were only using the person&amp;#039;s name.&lt;br /&gt;
&lt;br /&gt;
After you have written the new code, ensure that the PIE engine is reset. Use &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reset.&amp;#039;&amp;#039;&amp;#039; Then, you re-consult the code using &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reconsult.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
As before, on a blank line in the &amp;#039;&amp;#039;&amp;#039;Dialog&amp;#039;&amp;#039;&amp;#039; window type a goal (without the &amp;quot;?&amp;quot; sign- in front).&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig2.png]]&lt;br /&gt;
&lt;br /&gt;
When the caret is placed at the end of the line, press the &amp;#039;&amp;#039;&amp;#039;Enter&amp;#039;&amp;#039;&amp;#039; key on your keyboard. &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; will now consider the text from the beginning of the line to the caret as a goal to execute. You should see the following result:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3.png]]&lt;br /&gt;
&lt;br /&gt;
Now, you will notice that the results that are yielded are richer than what you got previously (See Tutorial 04: Fundamental Prolog (Part 1)).&lt;br /&gt;
&lt;br /&gt;
==Refining Further==&lt;br /&gt;
&lt;br /&gt;
If you take a look at the grandfather predicate, you would notice that it has a subtle bug: A person would have two grandfathers: one from the mother&amp;#039;s side and one from the father&amp;#039;s side, but the grandfather predicate as defined earlier would only yield the grandfather on the father&amp;#039;s side.&lt;br /&gt;
&lt;br /&gt;
Hence the grandfather predicate should be re-written as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, TheGrandFather):-&lt;br /&gt;
     parent(Person, ParentOfPerson), &lt;br /&gt;
     father(ParentOfPerson, TheGrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this predicate the logic states that a father of &amp;#039;&amp;#039;any&amp;#039;&amp;#039; parent could be the grandfather of the person in consideration.&lt;br /&gt;
&lt;br /&gt;
For that predicate to work, we need to define a predicate called father using the person &amp;#039;&amp;#039;&amp;#039;functor.&amp;#039;&amp;#039;&amp;#039; This predicate would troll through a database of facts explaining the &amp;quot;&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;&amp;quot; defined in the system. This is a more elegant method for finding out the fathers, instead of presenting them as facts (as shown previously) because later on we can extend this concept to find out &amp;quot;&amp;#039;&amp;#039;mothers&amp;#039;&amp;#039;&amp;quot; in a similar fashion.&lt;br /&gt;
&lt;br /&gt;
This can be done in either of the following ways:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/*1st version */&lt;br /&gt;
father(P, F) :-&lt;br /&gt;
    parent(P, F), &lt;br /&gt;
    F = person(_, &amp;quot;male&amp;quot;).    %Line 2&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* 2nd version */&lt;br /&gt;
father(P, person(Name, &amp;quot;male&amp;quot;)) :-&lt;br /&gt;
    parent(P, person(Name, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The logic behind both the versions is the same, but the manner in which the logic is indicated to the Prolog engine is different. In the first version, the code in Prolog systematically examines each parent fact asserted into the code, and sees if the first logical variable (P) matches that passed down from the predicate head. If the variable does match, it checks if the second argument consists of a person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, whose second argument is the string literal &amp;quot;male&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
This example shows one important feature of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: The arguments of a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; can be taken apart and examined using regular Prolog variables and bound values (like the string literal in this example) If you see Line 2 (Look at the side of the comment &amp;#039;&amp;#039;%Line 2&amp;#039;&amp;#039; in the code for the first version), you would notice that we have used an anonymous variable (the underscore) for the first argument of the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as we are (in that predicate) not interested in the name of the father.&lt;br /&gt;
&lt;br /&gt;
The second version also does the same thing as the first one. But this time, as the set of parent facts is examined; on arriving at the correct value for P, the code halts and returns the correct &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; data back to the predicate head, &amp;#039;&amp;#039;provided the second argument of that &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is the string literal &amp;quot;male&amp;quot;&amp;#039;&amp;#039;. If you note, the functor was NOT bound to any intermediate Prolog variable, as it was done in the first version.&lt;br /&gt;
&lt;br /&gt;
The second version is much terser than the first one, and sometimes this method of writing the code can be less legible to beginners.&lt;br /&gt;
&lt;br /&gt;
Let us now use this code in a complete example, where we will give a suitable set of person&amp;#039;&amp;#039;&amp;#039;&amp;#039; facts:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3b.jpg]]&lt;br /&gt;
&lt;br /&gt;
Now, after you re-consult the above code into &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, and give the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(person&amp;#039;(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), W)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the results would be as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig3c.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Functors and Predicates==&lt;br /&gt;
&lt;br /&gt;
Technically, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; represents a logical function that binds several domains together. In simpler words, a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is a mechanism that teaches the Prolog Engine how to put together data from its component parts. It effectively puts parts of the data into a common box. You can also retrieve the parts subsequently, whenever required (like seen the previous examples). It may look like a Prolog fact or a predicate call, but it is not. It&amp;#039;s just a piece of data, which you can handle in much the same way as a string or a number.&lt;br /&gt;
&lt;br /&gt;
Please note that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has nothing to do with a function in other programming languages. It does not stand for some computation to be performed. It simply identifies the &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; and holds its arguments together.&lt;br /&gt;
&lt;br /&gt;
When you study the above example, you would notice that nothing special need to be done when logical variables are used to stand in for data represented by &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. The logical variable that is used to stand in for such data, is written just like any other logical variable: A word starting with a capital letter. Thus, if we take a look at the &amp;#039;&amp;#039;grandFather&amp;#039;&amp;#039; predicate in the example in this tutorial; you would notice that nothing has changed when you compare it with the same predicate that was defined in the earlier tutorial (Fundamental Prolog (part 1)). After all, the logic of that predicate has not changed. So, you would not find any changes to the variables used inside that predicate.&lt;br /&gt;
&lt;br /&gt;
The biggest advantage of using &amp;#039;&amp;#039;&amp;#039;a functor&amp;#039;&amp;#039;&amp;#039; is that you are free to change the internal arguments of the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; without changing much of the predicates that uses such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; as you keep revising the code.&lt;br /&gt;
&lt;br /&gt;
That means, if you decide (in a later version of the code you are writing) to have one more argument for the &amp;#039;&amp;#039;person&amp;#039;&amp;#039;, you still need not change anything in the &amp;#039;&amp;#039;grandfather&amp;#039;&amp;#039; predicate.&lt;br /&gt;
&lt;br /&gt;
==Functors as Arguments==&lt;br /&gt;
&lt;br /&gt;
In the previous section, the person &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; was having two arguments: the Name and the Gender. Both happened to be simple domains i.e. &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039; such as &amp;quot;Bill&amp;quot; and, &amp;quot;male&amp;quot;. However, there is nothing that prevents us from putting a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; itself as an &amp;#039;&amp;#039;&amp;#039;argument&amp;#039;&amp;#039;&amp;#039; of another &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Suppose, you wanted to define a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; for a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; (i.e. a husband and a wife). This is an example of how you would use such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple = couple(person(Husband, &amp;quot;male&amp;quot;), person(Wife, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, you would notice that the &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; is defined by two &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, each of which is a mixture of &amp;#039;&amp;#039;&amp;#039;variables&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039;. This can be done to reflect the logic of the data being represented. The logic used here is that a husband is always a &amp;quot;male&amp;quot; and wife is always a &amp;quot;female&amp;quot;; and a couple consists of a husband and a wife. All of which is consistent with the most common interpretation of what one means by a &amp;#039;&amp;#039;couple&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Though in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; you cannot predefine the kind of grammar &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can actually have; in &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039; you can make such definitions. The advantage of defining a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; in such a manner is that later, when you create actual data using this &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, the Prolog engine will always check if the data being created was consistent with the grammar that data was supposed to adhere to.&lt;br /&gt;
&lt;br /&gt;
This comes to another characteristic of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;: the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; has been defined with two arguments and the &amp;#039;&amp;#039;&amp;#039;position&amp;#039;&amp;#039;&amp;#039; of these arguments reflects their logical association. It was explained in the first part of this tutorial, that the positions of the arguments of predicates have to be formalized by the programmer, who designs the code. Once formalized; the same positional formalization should be consistently used.&lt;br /&gt;
&lt;br /&gt;
The same strategy applies to the creation of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; too: In the case of the &amp;#039;&amp;#039;couple&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, it so happened that we decided to represent the husband as the first argument, and the wife as the second one. Once this is done, then whenever data is created using such a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, then we need to ensure that the husband is always at the first position, and the wife in the second (in spite of what the women&amp;#039;s lib people may say!)&lt;br /&gt;
&lt;br /&gt;
Now, when we look back on the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, someone can correctly argue that if were so sure that the first argument is always a husband (who is always a male), and the second one is always a wife (who is always a female) then what is the need for making the arguments of the &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; also as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;? Hence, an example with a more simplified &amp;#039;&amp;#039;couple &amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; would be as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(ACouple):-&lt;br /&gt;
    ACouple=couple(Husband, Wife), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hence; let us revert back to using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; for the husband and wife... but this time we will use them, because we are not sure which position is for the husband and which is for the wife.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(Couple):-&lt;br /&gt;
     Couple = couple(person(PersonsName, PersonsGender), person(SpouseName, SpouseGender)), &lt;br /&gt;
     ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039;, both the following examples make logical sense:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;myPredicate(C1):-&lt;br /&gt;
    C1=couple(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)), &lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
/* or */&lt;br /&gt;
myPredicate(C2):-&lt;br /&gt;
    C2=couple(person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It should be pointed out that in the PIE (and many other Prolog Engines), there is no way to indicate whether a functor will receive simple domain or compound domain arguments, by looking at the variables that define the functor. This stems from the fact that in &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; are directly used, without being declared anywhere, other than in the mind of the programmer.&lt;br /&gt;
&lt;br /&gt;
For example, if you wanted to use a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(Name, Gender)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; would as easily accept a logical variable such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;regardingAPerson(Somebody):-&lt;br /&gt;
    Somebody = &lt;br /&gt;
        person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
            person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                person(&amp;quot;Pam&amp;quot;, &lt;br /&gt;
                   person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;)&lt;br /&gt;
                )&lt;br /&gt;
            )&lt;br /&gt;
        ), &lt;br /&gt;
    ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which actually does not make any logical sense, in the current context. Luckily, &amp;#039;&amp;#039;&amp;#039;Visual Prolog&amp;#039;&amp;#039;&amp;#039;, does a much better job of differentiating between &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039;, so, when you get to the latter tutorials, you would not have any such problems.&lt;br /&gt;
&lt;br /&gt;
==Recursion Using Functors==&lt;br /&gt;
&lt;br /&gt;
When data is described using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;, it can be treated like any other piece of data. For example, you can write a predicate that can be made to recursively search through data which used &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Let us get back briefly to the ancestor predicate, which we had used in the first part of the tutorial.&lt;br /&gt;
&lt;br /&gt;
In order to determine if somebody is an ancestor of someone else, we used a recursive definition for the predicate, i.e. a definition that is defined in terms of itself. like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ancestor(Person, Ancestor) :- parent(Person, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This declaration states that a parent is an ancestor, and that an ancestor to a parent is also an ancestor. If you examine the variables in the above definition, it can very well stand for either &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;compound domains.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Let us define the data thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;), person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;)).&lt;br /&gt;
parent(person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we now ask &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; to solve the following goal, &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;P=person(&amp;quot;pam&amp;quot;, &amp;quot;female&amp;quot;), ancestor(P, Who)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...this is what we will get:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_2_fig4.jpg]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039; engine has recursively examined the parent facts to reveal the two possible solutions to the answer. By the way, in the above query, you would also notice that we have bound a variable P to the person &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; when specifying the goal to &amp;#039;&amp;#039;&amp;#039;PIE&amp;#039;&amp;#039;&amp;#039;. This was done to make the code more readable but it also demonstrates the fact that we can bind a piece of data specified as a &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; into any Prolog variable.&lt;br /&gt;
&lt;br /&gt;
==Strategies for Using Functors==&lt;br /&gt;
&lt;br /&gt;
Software will be only as good as allowed by the modeled data. By the term &amp;#039;&amp;#039;modeling&amp;#039;&amp;#039;, we mean the establishment of a relationship between the subset of the outside real-world that is being tackled with the internal data structures of the software. If the modeling of data is poor, then it is likely that the software will also be poor; or at best, inefficient. This is true of any software written in any programming language. That was the reason in the latter part of the earlier tutorial (Fundamental Prolog (part 1)), we had tried to focus on the &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; and tried to correct the situation by inserting richer facts. Similarly, we introduced the concept of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; in this tutorial to get even more clarity on the data regarding &amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039; that are being modeled.&lt;br /&gt;
&lt;br /&gt;
The advantage of Prolog is that it allows easy &amp;#039;&amp;#039;&amp;#039;description&amp;#039;&amp;#039;&amp;#039; of the real-world data in a form that can be internally utilized by the code in an efficient manner. Simultaneously, it also makes the code very readable by fellow programmers who are working together on a project.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Functors&amp;#039;&amp;#039;&amp;#039; can be used to create any type of &amp;#039;&amp;#039;&amp;#039;compound domain&amp;#039;&amp;#039;&amp;#039; to help in this modeling process. You would have to carefully examine the various parts of real-world data that you plan to process and convert them using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data types that you would encounter in future tutorials) keeping in mind their usage in all critical parts of the software. Some data structures that were useful in one area of the software may prove to be a hindrance in other areas.&lt;br /&gt;
&lt;br /&gt;
You should take a holistic approach while zeroing in on the &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; (and other data structures) you plan to use in your software. Pause and look around at all the corners of your software. Only then should you code the necessary &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Just thinking carefully about the data structures is not the only thing that is required. You would also need to (often simultaneously) write/modify the various goals and sub-goals (i.e. predicates) and then use the data developed thus far in those predicates. The &amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039; of attaining those goals and sub-goals would make your software work and you can get valuable feedback with which you can further refine your data structures.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we have not fleshed out a software design holistically. We have only nibbled at bits and pieces of data which establishes the &amp;#039;&amp;#039;&amp;#039;feel&amp;#039;&amp;#039;&amp;#039; for some kind of real-world data concerning relationship between people (&amp;#039;&amp;#039;parents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;grandparents&amp;#039;&amp;#039;, &amp;#039;&amp;#039;families&amp;#039;&amp;#039;, &amp;#039;&amp;#039;ancestors&amp;#039;&amp;#039; etc.) In forthcoming tutorials, we will use this experience to establish a &amp;#039;&amp;#039;&amp;#039;red thread&amp;#039;&amp;#039;&amp;#039; that connects all these tutorials together; eventually ending up with a useful software that requires such data structures.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In this lesson we learnt that data can comprise of &amp;#039;&amp;#039;&amp;#039;simple domains&amp;#039;&amp;#039;&amp;#039;, or they can be &amp;#039;&amp;#039;&amp;#039;compound domains&amp;#039;&amp;#039;&amp;#039; represented using &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039;. We found that the arguments of &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; must be positionally consistent with the logic they were meant to represent. We understood that a &amp;#039;&amp;#039;&amp;#039;functor&amp;#039;&amp;#039;&amp;#039; need not have just simple domains as arguments, but it can also have other &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; as arguments. We then learnt that data represented as &amp;#039;&amp;#039;&amp;#039;functors&amp;#039;&amp;#039;&amp;#039; can be used like any regular Prolog variable, and we can even perform all operations; including recursion, on such data.&lt;br /&gt;
&lt;br /&gt;
We also learnt that we must spend time modeling our subset of the real world for which we are developing the software, and get a feel for the data. We should simultaneously experiment with the predicates that you may wish to use with the data that is being modeled, so that they can be refined further.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Fundamental Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig2.png&amp;diff=3842</id>
		<title>File:Fundamental Prolog 2 fig2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig2.png&amp;diff=3842"/>
		<updated>2013-09-18T16:20:51Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig1.png&amp;diff=3841</id>
		<title>File:Fundamental Prolog 2 fig1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_2_fig1.png&amp;diff=3841"/>
		<updated>2013-09-18T16:20:20Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_1&amp;diff=3840</id>
		<title>Fundamental Prolog Part 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_1&amp;diff=3840"/>
		<updated>2013-09-18T15:46:56Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* PIE: Prolog Inference Engine */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this &amp;#039;&amp;#039;&amp;#039;Prolog tutorial&amp;#039;&amp;#039;&amp;#039; you will learn about the very fundamental ideas of Prolog language programming.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog is object oriented, strictly typed and mode checked.&lt;br /&gt;
You will of course have to master all this to write Visual Prolog programs.&lt;br /&gt;
But in this tutorial we will focus on the &amp;#039;&amp;#039;core&amp;#039;&amp;#039; of the code, i.e. the Prolog code when disregarding classes, types and modes.&lt;br /&gt;
&lt;br /&gt;
For this purpose we will use the PIE example that is included in the Visual Prolog distribution.&lt;br /&gt;
&lt;br /&gt;
PIE is a &amp;quot;classical&amp;quot; Prolog interpreter, by using this you can learn and experiment with Prolog without at all being concerned with classes, types, etc.&lt;br /&gt;
&lt;br /&gt;
== Horn Clause Logic ==&lt;br /&gt;
&lt;br /&gt;
Visual Prolog and other Prolog language dialects are based on Horn Clause logic.&lt;br /&gt;
Horn Clause logic is a formal system for reasoning about things and the way they relate to each other.&lt;br /&gt;
&lt;br /&gt;
In natural language I can express a statement like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;John is the father of Bill.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here I 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 Horn Clause Logic I can formalize this statement 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;
I have chosen to represent the persons by their names (which are string literals).&lt;br /&gt;
In a more complex world this would not be sufficient because many people have same name.&lt;br /&gt;
But for now we will be content with this simple formalization.&lt;br /&gt;
&lt;br /&gt;
With formalizations like the one above I can state any kind of family relation between any persons.&lt;br /&gt;
But for this to become really interesting I will also have to formalize &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;rules&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;X is the grandfather of Z, if X is the father of Y and Y is the father of Z&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;Y&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Z&amp;lt;/vp&amp;gt; are persons.&lt;br /&gt;
In Horn Clause Logic I can formalize this rule like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, GrandFather) :-&lt;br /&gt;
    father(Person, Father), father(Father, GrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I have chosen to use &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;variable&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; names that help understanding better than &amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;Y&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Z&amp;lt;/vp&amp;gt;.&lt;br /&gt;
I have also introduced a predicate for the grandfather relation.&lt;br /&gt;
Again I have chosen that the grandfather should be the second argument.&lt;br /&gt;
It is wise to be consistent like that, i.e. that the arguments of the different predicates follow some common principle.&lt;br /&gt;
When reading rules you should interpret :- as &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and the comma that separates the relations as &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Statements like &amp;quot;John is the father of Bill&amp;quot; are called &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;facts&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, while statements like &amp;quot;&amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt; is the grandfather of &amp;lt;vp&amp;gt;Z&amp;lt;/vp&amp;gt;, if &amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt; is the father of &amp;lt;vp&amp;gt;Y&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Y&amp;lt;/vp&amp;gt; is the father of &amp;lt;vp&amp;gt;Z&amp;lt;/vp&amp;gt;&amp;quot; are called &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;rules&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
With facts and rules we are ready to formulate theories.&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;theory&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; is a collection of facts and rules.&lt;br /&gt;
Let me state a little theory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
father(&amp;quot;Pam&amp;quot;, &amp;quot;Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
grandFather(Person, GrandFather) :-&lt;br /&gt;
    father(Person, Father),&lt;br /&gt;
    father(Father, GrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The purpose of the theory is to answer questions like these:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Is John the father of Sue?&lt;br /&gt;
Who is the father of Pam?&lt;br /&gt;
Is John the grandfather of Pam?&lt;br /&gt;
...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such questions are called goals.&lt;br /&gt;
And they can be formalized like this (respectively):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(&amp;quot;Sue&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
?- father(&amp;quot;Pam&amp;quot;, X).&lt;br /&gt;
?- grandFather(&amp;quot;Pam&amp;quot;, &amp;quot;John&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such questions are called &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;goal clauses&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; or simply &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;goals&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
Together &amp;#039;&amp;#039;facts&amp;#039;&amp;#039;, &amp;#039;&amp;#039;rules&amp;#039;&amp;#039; and &amp;#039;&amp;#039;goals&amp;#039;&amp;#039; are called Horn clauses, hence the name Horn Clause Logic.&lt;br /&gt;
&lt;br /&gt;
Some goals like the first and last are answered with a simple &amp;#039;&amp;#039;yes&amp;#039;&amp;#039; or &amp;#039;&amp;#039;no&amp;#039;&amp;#039;.&lt;br /&gt;
For other goals like the second we seek a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;solution&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, like &amp;lt;vp&amp;gt;X = &amp;quot;Bill&amp;quot;&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Some goals may even have many solutions.&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(X, Y).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
has two solutions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;X = &amp;quot;Bill&amp;quot;, Y = &amp;quot;John&amp;quot;.&lt;br /&gt;
X = &amp;quot;Pam&amp;quot;, Y = &amp;quot;Bill&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A Prolog program is a theory and a goal.&lt;br /&gt;
When the program starts it tries to find a solution to the goal in the theory.&lt;br /&gt;
&lt;br /&gt;
== PIE: Prolog Inference Engine ==&lt;br /&gt;
&lt;br /&gt;
Now we will try the little example above in PIE, the Prolog Inference Engine that comes with Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
Before we start you should install and build the PIE example.&lt;br /&gt;
&lt;br /&gt;
*Select &amp;quot;Install Examples&amp;quot; in the Windows start menu (&amp;#039;&amp;#039;&amp;#039;Start -&amp;gt; Visual Prolog -&amp;gt; Install Examples&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
*Open the PIE project in the IDE and &amp;#039;&amp;#039;&amp;#039;run&amp;#039;&amp;#039;&amp;#039; the program, as it is described in the [[Ide/Environment Overview|Environment Overview]].&lt;br /&gt;
&lt;br /&gt;
When the program starts it will look like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_1_PIE.png|Prolog Inference Engine]]&lt;br /&gt;
&lt;br /&gt;
Select &amp;#039;&amp;#039;&amp;#039;File -&amp;gt; New&amp;#039;&amp;#039;&amp;#039; and enter the father and grandFather clauses above:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_1_GrandFather.png|Grand Father example]]&lt;br /&gt;
&lt;br /&gt;
While the editor window is active choose &amp;#039;&amp;#039;&amp;#039;Engine -&amp;gt; Reconsult&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
This will load the file into the engine.&lt;br /&gt;
In the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Dialog&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; window you should receive a message like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Reconsulted from: ....\pie\Exe\FILE0.PRO&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Reconsult loads whatever is in the editor, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;without&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; saving the contents to the file, if you want to save the contents use &amp;#039;&amp;#039;&amp;#039;File -&amp;gt; Save&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;File -&amp;gt; Consult&amp;#039;&amp;#039;&amp;#039; will load the disc contents of the file regardless of whether the file is opened for editing or not.&lt;br /&gt;
&lt;br /&gt;
Once you have &amp;quot;consulted&amp;quot; the theory, you can use it to answer goals.&lt;br /&gt;
&lt;br /&gt;
On a blank line in the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Dialog&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; window type a goal (without the &amp;#039;&amp;#039;&amp;#039;?-&amp;#039;&amp;#039;&amp;#039; in front).&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_1_goal1.png|Typing a goal]]&lt;br /&gt;
&lt;br /&gt;
When the caret is placed at the end of the line, press the &amp;#039;&amp;#039;&amp;#039;Enter&amp;#039;&amp;#039;&amp;#039; key on your keyboard.&lt;br /&gt;
PIE will now consider the text from the beginning of the line to the caret as a goal to execute.&lt;br /&gt;
You should see a result like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:Fundamental_Prolog_1_goal2.png|Goal result]]&lt;br /&gt;
&lt;br /&gt;
== Extending the family theory ==&lt;br /&gt;
&lt;br /&gt;
It is straight forward to extend the family theory above with predicates like mother and grandMother.&lt;br /&gt;
You should try that yourself.&lt;br /&gt;
You should also add more persons.&lt;br /&gt;
I suggest that you use persons from your own family, because that makes it lot easier to validate, whether some person is indeed the grandMother of some other person, etc.&lt;br /&gt;
&lt;br /&gt;
Given mother and father we can also define a parent predicate.&lt;br /&gt;
You are a parent if you are a mother; you are also a parent if you are a father.&lt;br /&gt;
Therefore we can define parent using two clauses like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(Person, Parent) :- mother(Person, Parent).&lt;br /&gt;
parent(Person, Parent) :- father(Person, Parent).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule reads (recall that the second argument corresponds to the predicate name):&lt;br /&gt;
&lt;br /&gt;
Parent is the parent of Person, if Parent is the mother of Person&lt;br /&gt;
&lt;br /&gt;
You can also define the parent relation using semicolon &amp;quot;;&amp;quot; which means or, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(Person, Parent) :-&lt;br /&gt;
    mother(Person, Parent);&lt;br /&gt;
    father(Person, Parent).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This rule reads:&lt;br /&gt;
&lt;br /&gt;
Parent is the parent of Person, if Parent is the mother of Person &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Parent is the father of Person&lt;br /&gt;
&lt;br /&gt;
I will however advise you to use semicolon as little as possible (or actually not at all).&lt;br /&gt;
There are several reasons for this:&lt;br /&gt;
&lt;br /&gt;
*The typographical difference &amp;quot;,&amp;quot; and &amp;quot;;&amp;quot; is very small, but the semantic difference is rather big.  &amp;quot;;&amp;quot; is often a source of confusion, since it is easily misinterpreted as &amp;quot;,&amp;quot;, especially when it is on the end of a long line.&lt;br /&gt;
*Visual Prolog only allows to use semicolon on the outermost level (PIE will allow arbitrarily deep nesting).&lt;br /&gt;
&lt;br /&gt;
Try creating a sibling predicate! Did that give problems?&lt;br /&gt;
&lt;br /&gt;
You might find that siblings are found &amp;#039;&amp;#039;&amp;#039;twice&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
At least if you say: Two persons are siblings if they have same mother, two persons are also siblings if they have same father.&lt;br /&gt;
I.e. if you have rules like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;sibling(Person, Sibling) :- mother(Person, Mother), mother(Sibling, Mother).&lt;br /&gt;
sibling(Person, Sibling) :- father(Person, Father), father(Sibling, Father).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first rule reads:&lt;br /&gt;
&lt;br /&gt;
Sibling is the sibling of Person, if Mother is the mother of Person &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; Mother is the mother of Sibling&lt;br /&gt;
&lt;br /&gt;
The reason that you receive siblings twice is that most siblings both have same father and mother, and therefore they fulfill both requirements above.&lt;br /&gt;
And therefore they are found twice.&lt;br /&gt;
&lt;br /&gt;
We shall not deal with this problem now; currently we will just accept that some rules give too many results.&lt;br /&gt;
&lt;br /&gt;
A fullBlodedSibling predicate does not have the same problem, because it will require that both the father and the mother are the same:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;fullBlodedSibling(Person, Sibling) :-&lt;br /&gt;
    mother(Person, Mother),&lt;br /&gt;
    mother(Sibling, Mother),&lt;br /&gt;
    father(Person, Father),&lt;br /&gt;
    father(Sibling, Father).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Prolog is a Programming Language ==&lt;br /&gt;
&lt;br /&gt;
From the description so far you might think that Prolog is an expert system, rather than a programming language.&lt;br /&gt;
And indeed Prolog can be used as an expert system, but it is designed to be a programming language.&lt;br /&gt;
&lt;br /&gt;
We miss two important ingredients to turn Horn Clause logic into a programming language:&lt;br /&gt;
&lt;br /&gt;
*Rigid search order/program control&lt;br /&gt;
*Side effects&lt;br /&gt;
&lt;br /&gt;
== Program Control ==&lt;br /&gt;
&lt;br /&gt;
When you try to find a solution to a goal like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(X, Y).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can do it in many ways.&lt;br /&gt;
For example, you might just consider at the second fact in the theory and then you have a solution.&lt;br /&gt;
&lt;br /&gt;
But Prolog does not use a &amp;quot;random&amp;quot; search strategy, instead it always use the same strategy.&lt;br /&gt;
The system maintains a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;current goal&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, which is always solved from &amp;#039;&amp;#039;&amp;#039;left to right&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
I.e. if the current goal is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- grandFather(X, Y), mother(Y, Z).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then the system will always try to solve the sub-goal grandFather(X, Y) before it solves mother(Y, Z), if the first (i.e. left-most) sub-goal cannot be solved then there is no solution to the overall problem and then the second sub-goal is not tried at all.&lt;br /&gt;
&lt;br /&gt;
When solving a particular sub-goal, the facts and rules are always tried from &amp;#039;&amp;#039;&amp;#039;top to bottom&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When a sub-goal is solved by using a rule, the right hand side replaces the sub-goal in the current goal.&lt;br /&gt;
&lt;br /&gt;
I.e. if the current goal is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- grandFather(X, Y), mother(Y, Z).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we are using the rule&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, GrandFather) :- father(Person, Father), father(Father, GrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to solve the first sub-goal, then the resulting current goal will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(X, Father), father(Father, Y), mother(Y, Z).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that some variables in the rule have been replaced by variables from the sub-goal.&lt;br /&gt;
I will explain this in details later.&lt;br /&gt;
&lt;br /&gt;
Given this evaluation strategy you can interpret clauses much more &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;procedural&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Consider this rule:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;grandFather(Person, GrandFather) :- father(Person, Father), father(Father, GrandFather).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given the strict evaluation we can read this rule like this:&lt;br /&gt;
&lt;br /&gt;
To solve grandFather(Person, GrandFather) &amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039; solve father(Person, Father) &amp;#039;&amp;#039;&amp;#039;and then &amp;#039;&amp;#039;&amp;#039;solve father(Father, GrandFather).&lt;br /&gt;
&lt;br /&gt;
Or even like this:&lt;br /&gt;
&lt;br /&gt;
When grandFather(Person, GrandFather) is &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;called&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, first &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;call&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; father(Person, Father) and then &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;call&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; father(Father, GrandFather).&lt;br /&gt;
&lt;br /&gt;
With this &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;procedural &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;reading you can see that predicates correspond to procedures/subroutines in other languages.&lt;br /&gt;
The main difference is that a Prolog predicate can return several solutions to a single invocation or even &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;fail&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
This will be discussed in details in the next sections.&lt;br /&gt;
&lt;br /&gt;
== Failing ==&lt;br /&gt;
&lt;br /&gt;
A predicate invocation might not have any solution in the theory.&lt;br /&gt;
For example calling parent(&amp;quot;Hans&amp;quot;, X) has no solution as there are no parent facts or rules that applies to &amp;quot;Hans&amp;quot;.&lt;br /&gt;
We say that the predicate call &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;fails&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
If the goal fails then there is simply no solution to the goal in the theory.&lt;br /&gt;
The next section will explain how failing is treated in the general case, i.e. when it is not the goal that fails.&lt;br /&gt;
&lt;br /&gt;
== Backtracking ==&lt;br /&gt;
&lt;br /&gt;
In the procedural interpretation of a Prolog program &amp;quot;or&amp;quot; is treated in a rather special way.&lt;br /&gt;
Consider the clause&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(Person, Parent) :-&lt;br /&gt;
    mother(Person, Parent);&lt;br /&gt;
    father(Person, Parent).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the logical reading we interpreted this clause as:&lt;br /&gt;
&lt;br /&gt;
Parent&lt;br /&gt;
is the parent of Person &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Parent is the mother of&lt;br /&gt;
Person &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
Parent is the father of Person.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;or&amp;quot; introduces two possible solutions to an invocation of the parent predicate.&lt;br /&gt;
Prolog handles such multiple choices by first trying one choice and later (if necessary) &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtracking&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; to the next alternative choice, etc.&lt;br /&gt;
&lt;br /&gt;
During the execution of a program a lot of alternative choices (known as &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtrack points&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;) might exist from earlier predicate calls.&lt;br /&gt;
If some predicate call fails, then we will backtrack to the last backtrack point we met and try the alternative solution instead.&lt;br /&gt;
If no further backtrack points exists then the overall goal has failed, meaning that there was no solution to it.&lt;br /&gt;
&lt;br /&gt;
With this in mind we can interpret the clause above like this:&lt;br /&gt;
&lt;br /&gt;
When parent(Person, Parent) is called first record a backtrack point to the second alternative solution (i.e. to the call to father(Person, Parent)) and then call mother(Person, Parent)&lt;br /&gt;
&lt;br /&gt;
A predicate that has several classes behave in a similar fashion.&lt;br /&gt;
Consider the clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
father(&amp;quot;Pam&amp;quot;, &amp;quot;Bill&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When father is invoked we first record a backtrack point to the second clause, and then try the first clause.&lt;br /&gt;
&lt;br /&gt;
If there are three or more choices we still only create one backtrack point, but that backtrack point will start by creating another backtrack point.&lt;br /&gt;
Consider the clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
father(&amp;quot;Pam&amp;quot;, &amp;quot;Bill&amp;quot;).&lt;br /&gt;
father(&amp;quot;Jack&amp;quot;, &amp;quot;Bill&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When father is invoked, we first record a backtrack point.&lt;br /&gt;
And then we try the first clause.&lt;br /&gt;
The backtrack point we create points to some code, which will itself create a backtrack point (namely to the third clause) and then try the second clause.&lt;br /&gt;
Thus all choice points have only two choices, but one choice might itself involve a choice.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Example&amp;#039;&amp;#039;&amp;#039; To illustrate how programs are executed I will go through an example in details.&lt;br /&gt;
Consider these clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;mother(&amp;quot;Bill&amp;quot;, &amp;quot;Lisa&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
father(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
father(&amp;quot;Pam&amp;quot;, &amp;quot;Bill&amp;quot;).&lt;br /&gt;
father(&amp;quot;Jack&amp;quot;, &amp;quot;Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
parent(Person, Parent) :-&lt;br /&gt;
    mother(Person, Parent);&lt;br /&gt;
    father(Person, Parent).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And then consider this goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(AA, BB), parent(BB, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This goal states that we want to find three persons AA, BB and CC, such that BB is the father of AA and CC is a parent of BB.&lt;br /&gt;
&lt;br /&gt;
As mentioned we always solve the goals from left to right, so first we call the father predicate.&lt;br /&gt;
When executing the father predicate we first create a backtrack point to the second clause, and then use the first clause.&lt;br /&gt;
&lt;br /&gt;
Using the first clause we find that AA is &amp;quot;Bill&amp;quot; and BB is &amp;quot;John&amp;quot;.&lt;br /&gt;
So we now effectively have the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;John&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So we call parent, which gives the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- mother(&amp;quot;John&amp;quot;, CC); father(&amp;quot;John&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that the variables in the clause have been replaced with the actual parameters of the call (exactly like when you call subroutines in other languages).&lt;br /&gt;
&lt;br /&gt;
The current goal is an &amp;quot;or&amp;quot; goal, so we create a backtrack point to the second alternative and pursuit the first.&lt;br /&gt;
We now have two active backtrack points, one to the second alternative in the parent clause, and one to the second clause in the father predicate.&lt;br /&gt;
&lt;br /&gt;
After the creation of this backtrack point we are left with the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- mother(&amp;quot;John&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So we call the mother predicate.&lt;br /&gt;
The mother predicate fails when the first argument is &amp;quot;John&amp;quot; (because it has no clauses that match this value in the first argument).&lt;br /&gt;
&lt;br /&gt;
In case of failure we backtrack to the last backtrack point we created.&lt;br /&gt;
So we will now pursuit the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(&amp;quot;John&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When calling father this time, we will again first create a backtrack point to the second father clause.&lt;br /&gt;
&lt;br /&gt;
Recall that we also still have a backtrack point to the second clause of the father predicate, which corresponds to the first call in the original goal.&lt;br /&gt;
&lt;br /&gt;
We now try to use the first father clause on the goal, but that fails, because the first arguments do not match (i.e. &amp;quot;John&amp;quot; does not match &amp;quot;Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Therefore we backtrack to the second clause, but before we use this clause we create a backtrack point to the third clause.&lt;br /&gt;
&lt;br /&gt;
The second clause also fails, since &amp;quot;John&amp;quot; does not match &amp;quot;Pam&amp;quot;, so we backtrack to the third clause.&lt;br /&gt;
This also fails, since &amp;quot;John&amp;quot; does not match &amp;quot;Jack&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Now we must backtrack all the way back to the first father call in the original goal; here we created a backtrack point to the second father clause.&lt;br /&gt;
&lt;br /&gt;
Using the second clause we find that AA is &amp;quot;Pam&amp;quot; and BB is &amp;quot;Bill&amp;quot;.&lt;br /&gt;
So we now effectively have the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;Bill&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When calling parent we now get:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- mother(&amp;quot;Bill&amp;quot;, CC); father(&amp;quot;Bill&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again we create a backtrack point to the second alternative and pursuit the first:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- mother(&amp;quot;Bill&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This goal succeeds with CC being &amp;quot;Lisa&amp;quot;.&lt;br /&gt;
So now we have found a solution to the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;AA = &amp;quot;Pam&amp;quot;, BB = &amp;quot;Bill&amp;quot;, CC = &amp;quot;Lisa&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When trying to find additional solutions we backtrack to the last backtrack point, which was the second alternative in the parent predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- father(&amp;quot;Bill&amp;quot;, CC).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This goal will also succeed with CC being &amp;quot;John&amp;quot;.&lt;br /&gt;
So now we have found one more solution to the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;AA = &amp;quot;Pam&amp;quot;, BB = &amp;quot;Bill&amp;quot;, CC = &amp;quot;John&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we try to find more solutions we will find:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;AA = &amp;quot;Jack&amp;quot;, BB = &amp;quot;Bill&amp;quot;, CC = &amp;quot;John&amp;quot;.&lt;br /&gt;
AA = &amp;quot;Jack&amp;quot;, BB = &amp;quot;Bill&amp;quot;, CC = &amp;quot;Lisa&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that we will experience that everything will eventually fail leaving no more backtrack points.&lt;br /&gt;
So all in all there are four solutions to the goal.&lt;br /&gt;
&lt;br /&gt;
== Improving the Family Theory ==&lt;br /&gt;
&lt;br /&gt;
If you continue to work with the family relation above, you will probably find out that you have problems with relations like &amp;#039;&amp;#039;brother&amp;#039;&amp;#039; and &amp;#039;&amp;#039;sister&amp;#039;&amp;#039;, because it is rather difficult to determine the sex of a person (unless the person is a father or mother).&lt;br /&gt;
&lt;br /&gt;
The problem is that we have chosen a bad way to formalize our theory.&lt;br /&gt;
&lt;br /&gt;
The reason that we arrived at this theory is because we started by considering the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;relations&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; between the entities.&lt;br /&gt;
If we instead first focus on the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;entities&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, then the result will naturally become different.&lt;br /&gt;
&lt;br /&gt;
Our main entities are persons.&lt;br /&gt;
Persons have a name (in this simple context will still assume that the name identifies the person, in a real scale program this would not be true).&lt;br /&gt;
Persons also have a sex.&lt;br /&gt;
Persons have many other properties, but none of them have any interest in our context.&lt;br /&gt;
&lt;br /&gt;
Therefore we define a person predicate, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;person(&amp;quot;Bill&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;John&amp;quot;, &amp;quot;male&amp;quot;).&lt;br /&gt;
person(&amp;quot;Pam&amp;quot;, &amp;quot;female&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first argument of the person predicate is the name and the second is the sex.&lt;br /&gt;
&lt;br /&gt;
Instead of using mother and father as facts, I will choose to have parent as facts and mother and father as rules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;parent(&amp;quot;Bill&amp;quot;, &amp;quot;John&amp;quot;).&lt;br /&gt;
parent(&amp;quot;Pam&amp;quot;, &amp;quot;Bill&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
father(Person, Father) :- parent(Person, Father), person(Father, &amp;quot;male&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when father is a &amp;quot;derived&amp;quot; relation like this, it is impossible to state &amp;#039;&amp;#039;female&amp;#039;&amp;#039; fathers.&lt;br /&gt;
So this theory also has a built-in consistency on this point, which did not exist in the other formulation.&lt;br /&gt;
&lt;br /&gt;
== Recursion ==&lt;br /&gt;
&lt;br /&gt;
Most family relations are easy to construct given the principles above.&lt;br /&gt;
But when it comes to &amp;quot;infinite&amp;quot; relations like &amp;#039;&amp;#039;ancestor&amp;#039;&amp;#039; we need something more.&lt;br /&gt;
If we follow the principle above, we should define ancestor like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ancestor(Person, Ancestor) :- parent(Person, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), parent(P1, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), parent(P1, P2), parent(P2, Ancestor).&lt;br /&gt;
...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main problem is that this line of clauses never ends.&lt;br /&gt;
The way to overcome this problem is to use a recursive definition, i.e. a definition that is defined in terms of itself. like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ancestor(Person, Ancestor) :- parent(Person, Ancestor).&lt;br /&gt;
ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This declaration states that a parent is an ancestor, and that an ancestor to a parent is also an ancestor.&lt;br /&gt;
&lt;br /&gt;
If you are not already familiar with recursion you might find it tricky (in several senses).&lt;br /&gt;
Recursion is however fundamental to Prolog programming.&lt;br /&gt;
You will use it again and again, so eventually you will find it completely natural.&lt;br /&gt;
&lt;br /&gt;
Let us try to execute an ancestor goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- ancestor(&amp;quot;Pam&amp;quot;, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We create a backtrack point to the second ancestor clause, and then we use the first, finding the new goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;Pam&amp;quot;, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This succeeds with the solution:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;AA = &amp;quot;Bill&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we try to find another solution by using our backtrack point to the second ancestor clause.&lt;br /&gt;
This gives the new&lt;br /&gt;
goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;Pam&amp;quot;, P1), ancestor(P1, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again &amp;quot;Bill&amp;quot; is the&lt;br /&gt;
parent of &amp;quot;Pam&amp;quot;,&lt;br /&gt;
so we find P1=&lt;br /&gt;
&amp;quot;Bill&amp;quot;, and then we have the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- ancestor(&amp;quot;Bill&amp;quot;, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To solve this goal we first create a backtrack point to the second ancestor clause and then we use the first one.&lt;br /&gt;
This gives the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;Bill&amp;quot;, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This goal has the gives the solution:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;AA = &amp;quot;John&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So now we have found two ancestors of &amp;quot;Pam&amp;quot;:&lt;br /&gt;
* &amp;quot;Bill&amp;quot; and&lt;br /&gt;
* &amp;quot;John&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
If we use the backtrack point to the second ancestor clause we get the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- parent(&amp;quot;Bill&amp;quot;, P1), ancestor(P1, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we will again find that&lt;br /&gt;
&amp;quot;John&amp;quot; is the parent of&lt;br /&gt;
&amp;quot;Bill&amp;quot;, and thus that&lt;br /&gt;
P1 is&lt;br /&gt;
&amp;quot;John&amp;quot;.&lt;br /&gt;
This gives the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- ancestor(&amp;quot;John&amp;quot;, AA).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you pursuit this goal you will find that it will not have any solution.&lt;br /&gt;
So all in all we can only find two ancestors of&lt;br /&gt;
&amp;quot;Pam&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Recursion is very powerful but it can also be a bit hard to control.&lt;br /&gt;
Two things are important to remember:&lt;br /&gt;
&lt;br /&gt;
*the recursion must make progress&lt;br /&gt;
*the recursion must terminate&lt;br /&gt;
&lt;br /&gt;
In the code above the first clause ensures that the recursion can terminate, because this clause is not recursive (i.e. it makes no calls to the predicate itself).&lt;br /&gt;
&lt;br /&gt;
In the second clause (which is recursive) we have made sure, that we go one ancestor-step further back, before making the recursive call.&lt;br /&gt;
I.e. we have ensured that we make some progress in the problem.&lt;br /&gt;
&lt;br /&gt;
== Side Effects ==&lt;br /&gt;
&lt;br /&gt;
Besides a strict evaluation order Prolog also has side effects.&lt;br /&gt;
For example Prolog has a number of predefined predicates for reading and writing.&lt;br /&gt;
&lt;br /&gt;
The following goal will write the found ancestors of &amp;quot;Pam&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- ancestor(&amp;quot;Pam&amp;quot;, AA), write(&amp;quot;Ancestor of Pam : &amp;quot;, AA), nl().&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ancestor call will find an ancestor of &amp;quot;Pam&amp;quot; in AA.&lt;br /&gt;
&lt;br /&gt;
The write call will write the string literal &amp;quot;Ancestor of Pam : &amp;quot;, and then it will write the value of AA.&lt;br /&gt;
&lt;br /&gt;
The nl call will shift to a new line in the output.&lt;br /&gt;
&lt;br /&gt;
When running programs in PIE, PIE itself writes solutions, so the overall effect is that your output and PIE&amp;#039;s own output will be mixed.&lt;br /&gt;
This might of course not be desirable.&lt;br /&gt;
&lt;br /&gt;
A very simple way to avoid PIE&amp;#039;s own output is to make sure that the goal has no solutions.&lt;br /&gt;
Consider the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;?- ancestor(&amp;quot;Pam&amp;quot;, AA), write(&amp;quot;Ancestor of Pam : &amp;quot;, AA), nl(), fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
fail is a predefined call that always fails (i.e. it has no solutions).&lt;br /&gt;
&lt;br /&gt;
The first three predicate calls have exactly the same effect as above: an ancestor is found (if such one exists, of course) and then it is written.&lt;br /&gt;
But then we call fail this will of course &amp;#039;&amp;#039; fail&amp;#039;&amp;#039;. Therefore we must pursuit a backtrack point if we have any.&lt;br /&gt;
&lt;br /&gt;
When pursuing this backtrack point, we will find another ancestor (if such one exists) and write that, and then we will fail again.&lt;br /&gt;
And so forth.&lt;br /&gt;
&lt;br /&gt;
So, we will find and write all ancestors. and eventually there will be no more backtrack points, and then the complete goal will fail.&lt;br /&gt;
&lt;br /&gt;
There are a few important points to notice here:&lt;br /&gt;
&lt;br /&gt;
*The goal itself did not have a single solution, but nevertheless all the solutions we wanted was given as side effects.&lt;br /&gt;
*Side effects in failing computations are not undone.&lt;br /&gt;
&lt;br /&gt;
These points are two sides of the same thing.&lt;br /&gt;
But they represent different level of optimism.&lt;br /&gt;
The first optimistically states some possibilities that you can use, while the second is more pessimistic and states that you should be aware about using side effects, because they are not undone even if the current goal does not lead to any solution.&lt;br /&gt;
&lt;br /&gt;
Anybody, who learns Prolog, will sooner or later experience unexpected output coming from failing parts of the program.&lt;br /&gt;
Perhaps, this little advice can help you:  Separate the &amp;quot;calculating&amp;quot; code from the code that performs input/output.&lt;br /&gt;
&lt;br /&gt;
In our examples above all the stated predicate are &amp;quot;calculating&amp;quot; predicates.&lt;br /&gt;
They all calculate some family relation.&lt;br /&gt;
If you need to write out, for example, &amp;quot;parents&amp;quot;, create a separate predicate for writing parents and let that predicate call the &amp;quot;calculating&amp;quot; parent predicate.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
In this tutorial we have looked at some of the basic features of Prolog.&lt;br /&gt;
You have seen &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;facts&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;rules&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;goals&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
You learned about the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;execution strategy &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;for Prolog including the notion of &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;failing&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;backtracking&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
You have also seen that backtracking can give &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;many results &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;to a single question.&lt;br /&gt;
And finally you have been introduced to &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;side effects&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[Fundamental Prolog Part 2]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Fundamental Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_PIE.png&amp;diff=3839</id>
		<title>File:Fundamental Prolog 1 PIE.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_PIE.png&amp;diff=3839"/>
		<updated>2013-09-18T15:45:54Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Fundamental Prolog 1 PIE.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Prolog Inference Engine (PIE)&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_GrandFather.png&amp;diff=3838</id>
		<title>File:Fundamental Prolog 1 GrandFather.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_GrandFather.png&amp;diff=3838"/>
		<updated>2013-09-18T15:45:41Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Fundamental Prolog 1 GrandFather.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundaamental Prolog 1: Grandfather&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_goal2.png&amp;diff=3837</id>
		<title>File:Fundamental Prolog 1 goal2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_goal2.png&amp;diff=3837"/>
		<updated>2013-09-18T15:45:27Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Fundamental Prolog 1 goal2.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Prolog 1: goal2&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_goal1.png&amp;diff=3836</id>
		<title>File:Fundamental Prolog 1 goal1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Fundamental_Prolog_1_goal1.png&amp;diff=3836"/>
		<updated>2013-09-18T15:45:13Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Fundamental Prolog 1 goal1.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Prolog 1: goal1&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3835</id>
		<title>Ide/Environment Overview</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3835"/>
		<updated>2013-09-18T15:26:06Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Debugging */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{ideNavbar|Environment Overview}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial I will give an overview of the Integrated Development Environment (IDE).The IDE is used to create, develop and maintain your Visual Prolog projects.&lt;br /&gt;
&lt;br /&gt;
Briefly speaking you will use the IDE for the following tasks in a project life cycle:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Creation&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is created in the IDE, at creation time you choose important properties of your project, such as whether the project is an executable or a DLL, whether it uses GUI or is text based, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Building&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is built, i.e. compiled and linked, from the IDE.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Browsing&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE and the compiler collect information about the project, which is utilized in various ways for quick localization of entities, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Development&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;During the development and maintenance of the project, the IDE is used to add and remove source files and GUI entities to the project and to edit these.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Debugging&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE is also used for debugging the project. The debugger is used to follow program execution and exploit the program state when the program runs.In the sequel we will look more detailed at each of these things.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that the tutorial will start from scratch and create a project that is used for the whole tutorial. The reader is encouraged to do the same. Also notice that clicking on reduced pictures will show them in full scale in a separate window.&lt;br /&gt;
&lt;br /&gt;
==Creation==&lt;br /&gt;
&lt;br /&gt;
First we will create a project by selecting &amp;#039;&amp;#039;&amp;#039;Project -&amp;gt; New...&amp;#039;&amp;#039;&amp;#039; in the menu. In response to this you are presented to a dialog containing various properties of the project.&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectSettingsS.png]]&lt;br /&gt;
&lt;br /&gt;
I have chosen that my project should have the name &amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The project name is also used as the name of the target that is produced.  In this case the target is an &amp;#039;&amp;#039;exe&amp;#039;&amp;#039; file so the target name will be &amp;#039;&amp;#039;tut01.exe&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
I have also chosen that the target should be a GUI program, i.e. a program with a graphical user interface.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Base Directory&amp;#039;&amp;#039;&amp;#039; is the &amp;quot;base&amp;quot; of all your projects; you should choose a place that is convenient for you.&lt;br /&gt;
&lt;br /&gt;
The new project will be created in a &amp;#039;&amp;#039;&amp;#039;Sub-Directory&amp;#039;&amp;#039;&amp;#039; of the base directory. By default this directory have the same name as the project itself.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Package Prefix&amp;#039;&amp;#039;&amp;#039; is used when creating new &amp;quot;packages&amp;quot; in the system (a later tutorial will explain about the package notion). In case your source files will ever be shared with somebody else it is a good idea if the prefix does not conflict with other prefixes. Therefore it is a good idea to use a prefix that start with a reverse Internet domain name, like &amp;#039;&amp;#039;com/visual-prolog &amp;#039;&amp;#039;(which is the reverse of &amp;#039;&amp;#039;visual-prolog.com&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
For the moment you do not need to consider the remaining tabs and options.&lt;br /&gt;
&lt;br /&gt;
Now create the project by pressing &amp;#039;&amp;#039;&amp;#039;OK&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The IDE will now look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_totalS.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, which contains information about the entities in your project. &lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039;, which will contain various status and progress messages.&lt;br /&gt;
&lt;br /&gt;
==Building==&lt;br /&gt;
&lt;br /&gt;
Before we make any modifications, we will build (i.e. compile and link) the project. In the &amp;#039;&amp;#039;&amp;#039;Build&amp;#039;&amp;#039;&amp;#039; menu you find commands for building, compiling and executing the project.&lt;br /&gt;
&lt;br /&gt;
If you choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039; the project is first build, so that it is an up to date version you execute. Therefore I will choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If you have not registered Visual Prolog you will be presented to a special screen telling you so. I will suggest that you register, but you can also choose &amp;quot;Continue Evaluation&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039; the IDE writes which files are compiled, etc.&lt;br /&gt;
&lt;br /&gt;
If the build process succeeds, which it does in this case, the created program is executed. As a result you will see a little &amp;#039;&amp;#039;doing-nothing &amp;#039;&amp;#039;GUI program. You might notice that the program looks a bit like the IDE itself. This is no coincidence since the IDE is actually a Visual Prolog program.&lt;br /&gt;
&lt;br /&gt;
Later in this tutorial we will also see what happens, if the compiler or the linker detects errors in the build process.&lt;br /&gt;
&lt;br /&gt;
==Browsing==&lt;br /&gt;
&lt;br /&gt;
Right now we will turn our eyes to the &amp;#039;&amp;#039;&amp;#039;Project Tree&amp;#039;&amp;#039;&amp;#039; in the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, and explore that a bit. The tree itself is presented in a standard Windows tree control so you should already be familiar with its usage. Here we shall concentrate on the contents of the tree.&lt;br /&gt;
&lt;br /&gt;
Right now the project tree will look like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow1.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
The top node represents the project, and the project directory.&lt;br /&gt;
&lt;br /&gt;
Right below that is the logical node &amp;#039;&amp;#039;&amp;#039;$(ProDir)&amp;#039;&amp;#039;&amp;#039; which represents the directory in which Visual Prolog is installed. This directory contains (as we shall shortly see) libraries and library code from the Visual Prolog system.&lt;br /&gt;
&lt;br /&gt;
Then comes another directory &amp;#039;&amp;#039;&amp;#039;TaskWindow,&amp;#039;&amp;#039;&amp;#039; which is a subdirectory of the project directory. This directory contains all the code needed to produce the Task Window, its menu and toolbar and the about dialog.&lt;br /&gt;
&lt;br /&gt;
Finally, you see a number of files. Visual Prolog uses the following conventions:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ph&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;package headers&amp;#039;&amp;#039;&amp;#039;. A package is a collection of classes and interfaces that are intended to be used as a chunk.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pack&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;packages&amp;#039;&amp;#039;&amp;#039;. They contain the implementation/definition of the corresponding .ph files.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;i&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;interface&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cl&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class declaration&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pro&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class implementation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you expand the main.cl node completely you will see the following:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
This sub-tree shows that the file &amp;#039;&amp;#039;&amp;#039;main.cl&amp;#039;&amp;#039;&amp;#039; contains a class called &amp;#039;&amp;#039;&amp;#039;main&amp;#039;&amp;#039;&amp;#039;, which contains two predicates called classinfo and run respectively.  Predicates are subroutines, but they will not be considered further in this tutorial.&lt;br /&gt;
&lt;br /&gt;
If we collapse this node again and instead expand the &amp;#039;&amp;#039;&amp;#039;TaskWindow&amp;#039;&amp;#039;&amp;#039; node, you will see this tree:&lt;br /&gt;
&lt;br /&gt;
You will notice several new kinds of nodes:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;dlg&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;dialog&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;frm&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;form&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;win&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;window&amp;#039;&amp;#039;&amp;#039; (task window or conventional PFC GUI window)&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;mnu&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;menu&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ico&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;icon&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you continue to explore you might also meet:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow3.png|Project Tree: TaskWindow]]&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ctl&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;controls&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;tb&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;toolbars&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cur&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;cursors&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;bmp&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;bitmaps&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;lib&amp;#039;&amp;#039;&amp;#039; that are &amp;#039;&amp;#039;&amp;#039;libraries&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you right click on a node a context menu will appear, with commands that have relevance for that particular node.&lt;br /&gt;
&lt;br /&gt;
If you double click on a node the corresponding entity will be brought up in a suitable editor. All code is edited in a text editor, but windows resources like dialogs and menus are edited in graphical editors. We will look closer to graphical editors below, here we shall try to bring up a text editor.&lt;br /&gt;
&lt;br /&gt;
Some entities are represented twice in the tree, because they have both a declaration and a definition/implementation. For example, the predicate run in the class &amp;#039;&amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]] [[Image:tut01_projectWindow4.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
Try double clicking on each of the run nodes, and notice that two editors open showing the declaration and the definition of the run predicate, respectively.&lt;br /&gt;
&lt;br /&gt;
The IDE has other facilities for browsing to specific entities, but these will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Development==&lt;br /&gt;
&lt;br /&gt;
Now lets try to modify the project a bit. Since we have not yet concerned ourselves with how to program in Visual Prolog, we will keep the changes simple.&lt;br /&gt;
&lt;br /&gt;
We will start by deliberately introducing an error, that way you will not be completely surprised if you make some thing wrong, at the same time you can see the error window.&lt;br /&gt;
&lt;br /&gt;
First you should locate the clauses for the run predicate in the file &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039;. If you double click the last of the two run nodes in the project tree, an editor opens with the caret placed like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_editRun1S.png]]&lt;br /&gt;
&lt;br /&gt;
Try inserting a fail like this (notice the comma after show):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
        TaskWindow = taskWindow::new(),&lt;br /&gt;
        TaskWindow:show(),&lt;br /&gt;
        fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that we try to build the project again: simply press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination. The system will now save and compile the file, but since we have made an error, the &amp;#039;&amp;#039;&amp;#039;Error Window&amp;#039;&amp;#039;&amp;#039; will open:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_errorS.png|Error Wondow]]&lt;br /&gt;
&lt;br /&gt;
We will consider what the error message actually means. Instead you should try to double-click on the error message. You will notice that the editor gets focus again and that the caret points exactly to the fail we inserted.&lt;br /&gt;
&lt;br /&gt;
Reestablish the code without the fail and build the project again.&lt;br /&gt;
&lt;br /&gt;
Next we will try to make a modification in the about dialog. It is not a very sensible change, but it will illustrate a number of things.&lt;br /&gt;
&lt;br /&gt;
First we will open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, to do this you double click the dialog in the project tree. The node you have to double click is shown here:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindowAbout.png|Project Window: About Dialog]]&lt;br /&gt;
&lt;br /&gt;
Once you have clicked it you will see the dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, two toolbars and properties window.&lt;br /&gt;
&lt;br /&gt;
We will add a button to the dialog. You click on the &amp;quot;button&amp;quot; on the controls toolbar, and then you click in the dialog next to the project icon in the &amp;#039;&amp;#039;&amp;#039;AboutDialog&amp;#039;&amp;#039;&amp;#039; window. As a result you can see the properties of the new control and you can edit them. I have changed the &amp;#039;&amp;#039;&amp;#039;Text&amp;#039;&amp;#039;&amp;#039; to &amp;quot;Press Me&amp;quot;, this is the text that will appear on the button. I can also see the &amp;#039;&amp;#039;&amp;#039;Name&amp;#039;&amp;#039;&amp;#039; field, the IDE will identify this particular control in various contexts by this Name.&lt;br /&gt;
&lt;br /&gt;
Now it should look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_dialogEditorAbout2.png|Dialog Editor: About Dialog with new button]]&lt;br /&gt;
&lt;br /&gt;
Now we want to do something whenever the button is pressed:&lt;br /&gt;
&lt;br /&gt;
[[Image:Tut01_dialogEditorAboutClick.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The IDE has suggested the name onPushButton for the handling predicate. If you double-click the line corresponding to the event, an editor will open positioned at the newly inserted code.&lt;br /&gt;
&lt;br /&gt;
We will change the button text when the button is pressed. To do this we modify the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onPushButtonClick : button::clickResponder.&lt;br /&gt;
clauses&lt;br /&gt;
    onPushButtonClick(_Source) = button::defaultAction() :-&lt;br /&gt;
        pushButton_ctl:setText(&amp;quot;Pressed&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now try to build and execute the program again (i.e. press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination).  If you have followed the steps carefully this should be a success. In the program you should of course open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog, by selecting the menu entry &amp;#039;&amp;#039;&amp;#039;Help -&amp;gt; About&amp;#039;&amp;#039;&amp;#039;; and then press the new button.&lt;br /&gt;
&lt;br /&gt;
This is how it looked in my application:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_AboutPressMe.png|About: Press Me]][[Image:tut01_AboutPressed.png|About Pressed]]&lt;br /&gt;
&lt;br /&gt;
==Debugging==&lt;br /&gt;
&lt;br /&gt;
The development environment contains a debugger. With the debugger you can trace the execution of your program and examine the program state.&lt;br /&gt;
&lt;br /&gt;
It does not make much sense to go into details with debugging before you have learned the Visual Prolog language. But I will briefly explain the debugger anyway, because it can be very useful for understanding and experimenting with the language.&lt;br /&gt;
&lt;br /&gt;
To start the debugger, select &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Run&amp;#039;&amp;#039;&amp;#039; in the menu.  If the project is not up-to-date it will first be build, after that the debugging session will start.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; you can stop the debugging at any time by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Stop Debugging&amp;#039;&amp;#039;&amp;#039; in the menu.&lt;br /&gt;
&lt;br /&gt;
When debugging starts the IDE will first load the debug information and then it will enter the program. The program execution is stopped just before the goal is executed. To indicate this, the goal is opened in an editor window, and the blue arrow points to the goal:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_debug1.png|Editor: Current Position]]&lt;br /&gt;
&lt;br /&gt;
You can single step in the program using the &amp;#039;&amp;#039;&amp;#039;Step Into &amp;#039;&amp;#039;&amp;#039;and &amp;#039;&amp;#039;&amp;#039;Step Over&amp;#039;&amp;#039;&amp;#039; commands in the &amp;#039;&amp;#039;&amp;#039;Debug &amp;#039;&amp;#039;&amp;#039;menu.&lt;br /&gt;
&lt;br /&gt;
Try &amp;#039;&amp;#039;&amp;#039;Step Into&amp;#039;&amp;#039;&amp;#039;: this will open another editor with the code of mainExe::run, and the arrow will point to the entry of this code.&lt;br /&gt;
&lt;br /&gt;
In the view menu you can open various debugger windows, which I will briefly explain.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Run Stack&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_runStack.png|Run Stack]]&lt;br /&gt;
&lt;br /&gt;
contains a &amp;quot;picture&amp;quot; of the run stack. In principle, the run stack contains a number of lines corresponding to calls that has been made. However, optimization (so called last call optimization) might have removed some entries.&lt;br /&gt;
&lt;br /&gt;
The run stack not only shows the calls, it also shows trap points and backtrack points, these is described in a later tutorial and there I also describe the run stack window in more details.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Local Variables&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_localVariables.png|Local Variables]]&lt;br /&gt;
&lt;br /&gt;
contains the local variables corresponding to the selection in the run stack window. Some variables might not yet have received a value, these are show as an underscore (i.e. like originalraiser in the above picture).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_factsWindow.png|Facts]]&lt;br /&gt;
&lt;br /&gt;
Contains the global state of the program, this state is stored in fact databases. &amp;#039;&amp;#039;Fact databases &amp;#039;&amp;#039;are a unique Visual Prolog feature, which will be described in a later tutorial. You can also add objects from the &amp;#039;&amp;#039;&amp;#039;Local Variables &amp;#039;&amp;#039;&amp;#039;window to the &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window, that way you can track the state changes in interesting objects.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Breakpoints &amp;#039;&amp;#039;&amp;#039;window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_breakPointsWindow.png]]&lt;br /&gt;
&lt;br /&gt;
shows the current breakpoints in the program (I have set open breakpoint so that the window would not be empty). You can set/remove breakpoints by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Toggle Breakpoint&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The remaining debug windows are for low level debugging and will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
The Integrated Development Environment (IDE) makes it possible for you to:&lt;br /&gt;
&lt;br /&gt;
*create&lt;br /&gt;
&lt;br /&gt;
*build&lt;br /&gt;
&lt;br /&gt;
*maintain, and&lt;br /&gt;
&lt;br /&gt;
*debug&lt;br /&gt;
&lt;br /&gt;
your projects. It contains a number of tools, editor and wizards that will help you in your tasks.&lt;br /&gt;
&lt;br /&gt;
[[ru:Среда_разработки]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_runStack.png&amp;diff=3834</id>
		<title>File:Tut01 runStack.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_runStack.png&amp;diff=3834"/>
		<updated>2013-09-18T15:24:48Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 runStack.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Debug: Run stack&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_localVariables.png&amp;diff=3833</id>
		<title>File:Tut01 localVariables.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_localVariables.png&amp;diff=3833"/>
		<updated>2013-09-18T15:24:23Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 localVariables.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Debug: Variables window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_factsWindow.png&amp;diff=3832</id>
		<title>File:Tut01 factsWindow.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_factsWindow.png&amp;diff=3832"/>
		<updated>2013-09-18T15:24:02Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 factsWindow.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Debug: Facts window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_debug1.png&amp;diff=3831</id>
		<title>File:Tut01 debug1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_debug1.png&amp;diff=3831"/>
		<updated>2013-09-18T15:23:40Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 debug1.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Debug: editor window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_breakPointsWindow.png&amp;diff=3830</id>
		<title>File:Tut01 breakPointsWindow.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_breakPointsWindow.png&amp;diff=3830"/>
		<updated>2013-09-18T15:23:21Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 breakPointsWindow.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Debug: Breakpoints window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_AboutPressed.png&amp;diff=3829</id>
		<title>File:Tut01 AboutPressed.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_AboutPressed.png&amp;diff=3829"/>
		<updated>2013-09-18T15:09:35Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 AboutPressed.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Running about dialog: Pressed&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_AboutPressMe.png&amp;diff=3828</id>
		<title>File:Tut01 AboutPressMe.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_AboutPressMe.png&amp;diff=3828"/>
		<updated>2013-09-18T15:09:23Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 AboutPressMe.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Running about dialog: Press Me&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_dialogEditorAboutClick.png&amp;diff=3827</id>
		<title>File:Tut01 dialogEditorAboutClick.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_dialogEditorAboutClick.png&amp;diff=3827"/>
		<updated>2013-09-18T14:59:20Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3826</id>
		<title>Ide/Environment Overview</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3826"/>
		<updated>2013-09-18T14:58:53Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Development */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{ideNavbar|Environment Overview}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial I will give an overview of the Integrated Development Environment (IDE).The IDE is used to create, develop and maintain your Visual Prolog projects.&lt;br /&gt;
&lt;br /&gt;
Briefly speaking you will use the IDE for the following tasks in a project life cycle:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Creation&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is created in the IDE, at creation time you choose important properties of your project, such as whether the project is an executable or a DLL, whether it uses GUI or is text based, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Building&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is built, i.e. compiled and linked, from the IDE.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Browsing&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE and the compiler collect information about the project, which is utilized in various ways for quick localization of entities, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Development&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;During the development and maintenance of the project, the IDE is used to add and remove source files and GUI entities to the project and to edit these.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Debugging&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE is also used for debugging the project. The debugger is used to follow program execution and exploit the program state when the program runs.In the sequel we will look more detailed at each of these things.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that the tutorial will start from scratch and create a project that is used for the whole tutorial. The reader is encouraged to do the same. Also notice that clicking on reduced pictures will show them in full scale in a separate window.&lt;br /&gt;
&lt;br /&gt;
==Creation==&lt;br /&gt;
&lt;br /&gt;
First we will create a project by selecting &amp;#039;&amp;#039;&amp;#039;Project -&amp;gt; New...&amp;#039;&amp;#039;&amp;#039; in the menu. In response to this you are presented to a dialog containing various properties of the project.&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectSettingsS.png]]&lt;br /&gt;
&lt;br /&gt;
I have chosen that my project should have the name &amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The project name is also used as the name of the target that is produced.  In this case the target is an &amp;#039;&amp;#039;exe&amp;#039;&amp;#039; file so the target name will be &amp;#039;&amp;#039;tut01.exe&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
I have also chosen that the target should be a GUI program, i.e. a program with a graphical user interface.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Base Directory&amp;#039;&amp;#039;&amp;#039; is the &amp;quot;base&amp;quot; of all your projects; you should choose a place that is convenient for you.&lt;br /&gt;
&lt;br /&gt;
The new project will be created in a &amp;#039;&amp;#039;&amp;#039;Sub-Directory&amp;#039;&amp;#039;&amp;#039; of the base directory. By default this directory have the same name as the project itself.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Package Prefix&amp;#039;&amp;#039;&amp;#039; is used when creating new &amp;quot;packages&amp;quot; in the system (a later tutorial will explain about the package notion). In case your source files will ever be shared with somebody else it is a good idea if the prefix does not conflict with other prefixes. Therefore it is a good idea to use a prefix that start with a reverse Internet domain name, like &amp;#039;&amp;#039;com/visual-prolog &amp;#039;&amp;#039;(which is the reverse of &amp;#039;&amp;#039;visual-prolog.com&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
For the moment you do not need to consider the remaining tabs and options.&lt;br /&gt;
&lt;br /&gt;
Now create the project by pressing &amp;#039;&amp;#039;&amp;#039;OK&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The IDE will now look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_totalS.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, which contains information about the entities in your project. &lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039;, which will contain various status and progress messages.&lt;br /&gt;
&lt;br /&gt;
==Building==&lt;br /&gt;
&lt;br /&gt;
Before we make any modifications, we will build (i.e. compile and link) the project. In the &amp;#039;&amp;#039;&amp;#039;Build&amp;#039;&amp;#039;&amp;#039; menu you find commands for building, compiling and executing the project.&lt;br /&gt;
&lt;br /&gt;
If you choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039; the project is first build, so that it is an up to date version you execute. Therefore I will choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If you have not registered Visual Prolog you will be presented to a special screen telling you so. I will suggest that you register, but you can also choose &amp;quot;Continue Evaluation&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039; the IDE writes which files are compiled, etc.&lt;br /&gt;
&lt;br /&gt;
If the build process succeeds, which it does in this case, the created program is executed. As a result you will see a little &amp;#039;&amp;#039;doing-nothing &amp;#039;&amp;#039;GUI program. You might notice that the program looks a bit like the IDE itself. This is no coincidence since the IDE is actually a Visual Prolog program.&lt;br /&gt;
&lt;br /&gt;
Later in this tutorial we will also see what happens, if the compiler or the linker detects errors in the build process.&lt;br /&gt;
&lt;br /&gt;
==Browsing==&lt;br /&gt;
&lt;br /&gt;
Right now we will turn our eyes to the &amp;#039;&amp;#039;&amp;#039;Project Tree&amp;#039;&amp;#039;&amp;#039; in the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, and explore that a bit. The tree itself is presented in a standard Windows tree control so you should already be familiar with its usage. Here we shall concentrate on the contents of the tree.&lt;br /&gt;
&lt;br /&gt;
Right now the project tree will look like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow1.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
The top node represents the project, and the project directory.&lt;br /&gt;
&lt;br /&gt;
Right below that is the logical node &amp;#039;&amp;#039;&amp;#039;$(ProDir)&amp;#039;&amp;#039;&amp;#039; which represents the directory in which Visual Prolog is installed. This directory contains (as we shall shortly see) libraries and library code from the Visual Prolog system.&lt;br /&gt;
&lt;br /&gt;
Then comes another directory &amp;#039;&amp;#039;&amp;#039;TaskWindow,&amp;#039;&amp;#039;&amp;#039; which is a subdirectory of the project directory. This directory contains all the code needed to produce the Task Window, its menu and toolbar and the about dialog.&lt;br /&gt;
&lt;br /&gt;
Finally, you see a number of files. Visual Prolog uses the following conventions:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ph&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;package headers&amp;#039;&amp;#039;&amp;#039;. A package is a collection of classes and interfaces that are intended to be used as a chunk.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pack&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;packages&amp;#039;&amp;#039;&amp;#039;. They contain the implementation/definition of the corresponding .ph files.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;i&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;interface&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cl&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class declaration&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pro&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class implementation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you expand the main.cl node completely you will see the following:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
This sub-tree shows that the file &amp;#039;&amp;#039;&amp;#039;main.cl&amp;#039;&amp;#039;&amp;#039; contains a class called &amp;#039;&amp;#039;&amp;#039;main&amp;#039;&amp;#039;&amp;#039;, which contains two predicates called classinfo and run respectively.  Predicates are subroutines, but they will not be considered further in this tutorial.&lt;br /&gt;
&lt;br /&gt;
If we collapse this node again and instead expand the &amp;#039;&amp;#039;&amp;#039;TaskWindow&amp;#039;&amp;#039;&amp;#039; node, you will see this tree:&lt;br /&gt;
&lt;br /&gt;
You will notice several new kinds of nodes:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;dlg&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;dialog&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;frm&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;form&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;win&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;window&amp;#039;&amp;#039;&amp;#039; (task window or conventional PFC GUI window)&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;mnu&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;menu&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ico&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;icon&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you continue to explore you might also meet:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow3.png|Project Tree: TaskWindow]]&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ctl&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;controls&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;tb&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;toolbars&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cur&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;cursors&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;bmp&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;bitmaps&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;lib&amp;#039;&amp;#039;&amp;#039; that are &amp;#039;&amp;#039;&amp;#039;libraries&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you right click on a node a context menu will appear, with commands that have relevance for that particular node.&lt;br /&gt;
&lt;br /&gt;
If you double click on a node the corresponding entity will be brought up in a suitable editor. All code is edited in a text editor, but windows resources like dialogs and menus are edited in graphical editors. We will look closer to graphical editors below, here we shall try to bring up a text editor.&lt;br /&gt;
&lt;br /&gt;
Some entities are represented twice in the tree, because they have both a declaration and a definition/implementation. For example, the predicate run in the class &amp;#039;&amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]] [[Image:tut01_projectWindow4.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
Try double clicking on each of the run nodes, and notice that two editors open showing the declaration and the definition of the run predicate, respectively.&lt;br /&gt;
&lt;br /&gt;
The IDE has other facilities for browsing to specific entities, but these will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Development==&lt;br /&gt;
&lt;br /&gt;
Now lets try to modify the project a bit. Since we have not yet concerned ourselves with how to program in Visual Prolog, we will keep the changes simple.&lt;br /&gt;
&lt;br /&gt;
We will start by deliberately introducing an error, that way you will not be completely surprised if you make some thing wrong, at the same time you can see the error window.&lt;br /&gt;
&lt;br /&gt;
First you should locate the clauses for the run predicate in the file &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039;. If you double click the last of the two run nodes in the project tree, an editor opens with the caret placed like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_editRun1S.png]]&lt;br /&gt;
&lt;br /&gt;
Try inserting a fail like this (notice the comma after show):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
        TaskWindow = taskWindow::new(),&lt;br /&gt;
        TaskWindow:show(),&lt;br /&gt;
        fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that we try to build the project again: simply press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination. The system will now save and compile the file, but since we have made an error, the &amp;#039;&amp;#039;&amp;#039;Error Window&amp;#039;&amp;#039;&amp;#039; will open:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_errorS.png|Error Wondow]]&lt;br /&gt;
&lt;br /&gt;
We will consider what the error message actually means. Instead you should try to double-click on the error message. You will notice that the editor gets focus again and that the caret points exactly to the fail we inserted.&lt;br /&gt;
&lt;br /&gt;
Reestablish the code without the fail and build the project again.&lt;br /&gt;
&lt;br /&gt;
Next we will try to make a modification in the about dialog. It is not a very sensible change, but it will illustrate a number of things.&lt;br /&gt;
&lt;br /&gt;
First we will open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, to do this you double click the dialog in the project tree. The node you have to double click is shown here:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindowAbout.png|Project Window: About Dialog]]&lt;br /&gt;
&lt;br /&gt;
Once you have clicked it you will see the dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, two toolbars and properties window.&lt;br /&gt;
&lt;br /&gt;
We will add a button to the dialog. You click on the &amp;quot;button&amp;quot; on the controls toolbar, and then you click in the dialog next to the project icon in the &amp;#039;&amp;#039;&amp;#039;AboutDialog&amp;#039;&amp;#039;&amp;#039; window. As a result you can see the properties of the new control and you can edit them. I have changed the &amp;#039;&amp;#039;&amp;#039;Text&amp;#039;&amp;#039;&amp;#039; to &amp;quot;Press Me&amp;quot;, this is the text that will appear on the button. I can also see the &amp;#039;&amp;#039;&amp;#039;Name&amp;#039;&amp;#039;&amp;#039; field, the IDE will identify this particular control in various contexts by this Name.&lt;br /&gt;
&lt;br /&gt;
Now it should look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_dialogEditorAbout2.png|Dialog Editor: About Dialog with new button]]&lt;br /&gt;
&lt;br /&gt;
Now we want to do something whenever the button is pressed:&lt;br /&gt;
&lt;br /&gt;
[[Image:Tut01_dialogEditorAboutClick.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The IDE has suggested the name onPushButton for the handling predicate. If you double-click the line corresponding to the event, an editor will open positioned at the newly inserted code.&lt;br /&gt;
&lt;br /&gt;
We will change the button text when the button is pressed. To do this we modify the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onPushButtonClick : button::clickResponder.&lt;br /&gt;
clauses&lt;br /&gt;
    onPushButtonClick(_Source) = button::defaultAction() :-&lt;br /&gt;
        pushButton_ctl:setText(&amp;quot;Pressed&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now try to build and execute the program again (i.e. press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination).  If you have followed the steps carefully this should be a success. In the program you should of course open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog, by selecting the menu entry &amp;#039;&amp;#039;&amp;#039;Help -&amp;gt; About&amp;#039;&amp;#039;&amp;#039;; and then press the new button.&lt;br /&gt;
&lt;br /&gt;
This is how it looked in my application:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_AboutPressMe.png|About: Press Me]][[Image:tut01_AboutPressed.png|About Pressed]]&lt;br /&gt;
&lt;br /&gt;
==Debugging==&lt;br /&gt;
&lt;br /&gt;
The development environment contains a debugger. With the debugger you can trace the execution of your program and examine the program state.&lt;br /&gt;
&lt;br /&gt;
It does not make much sense to go into details with debugging before you have learned the Visual Prolog language. But I will briefly explain the debugger anyway, because it can be very useful for understanding and experimenting with the language.&lt;br /&gt;
&lt;br /&gt;
To start the debugger, select &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Run&amp;#039;&amp;#039;&amp;#039; in the menu.  If the project is not up-to-date it will first be build, after that the debugging session will start.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; you can stop the debugging at any time by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Stop Debugging&amp;#039;&amp;#039;&amp;#039; in the menu.&lt;br /&gt;
&lt;br /&gt;
When debugging starts the IDE will first load the debug information and then it will enter the program. The program execution is stopped just before the goal is executed. To indicate this, the goal is opened in an editor window, and the blue arrow points to the goal:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_debug1.png|Editor: Current Position]]&lt;br /&gt;
&lt;br /&gt;
You can single step in the program using the &amp;#039;&amp;#039;&amp;#039;Step Into &amp;#039;&amp;#039;&amp;#039;and &amp;#039;&amp;#039;&amp;#039;Step Over&amp;#039;&amp;#039;&amp;#039; commands in the &amp;#039;&amp;#039;&amp;#039;Debug &amp;#039;&amp;#039;&amp;#039;menu.&lt;br /&gt;
&lt;br /&gt;
Try &amp;#039;&amp;#039;&amp;#039;Step Into&amp;#039;&amp;#039;&amp;#039;: this will open another editor with the code of mainExe::run, and the arrow will point to the entry of this code.&lt;br /&gt;
&lt;br /&gt;
In the view menu you can open various debugger windows, which I will briefly explain.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Run Stack&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_runStack.png|Run Stack]]&lt;br /&gt;
&lt;br /&gt;
contains a &amp;quot;picture&amp;quot; of the run stack. Exactly what you see depends on your settings in &amp;#039;&amp;#039;&amp;#039;Tools -&amp;gt; Options... -&amp;gt; Debugger&amp;#039;&amp;#039;&amp;#039;.  In principle, the run stack contains a number of lines corresponding to calls that has been made. However, optimization (so called last call optimization) might have removed some entries.&lt;br /&gt;
&lt;br /&gt;
The run stack not only shows the calls, it also shows trap points and backtrack points, these is described in a later tutorial and there I also describe the run stack window in more details.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Local Variables&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_localVariables.png|Local Variables]]&lt;br /&gt;
&lt;br /&gt;
contains the local variables corresponding to the selection in the run stack window. Some variables might not yet have received a value, these are show as an underscore (i.e. like originalraiser in the above picture).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_factsWindow.png|Facts]]&lt;br /&gt;
&lt;br /&gt;
Contains the global state of the program, this state is stored in fact databases. &amp;#039;&amp;#039;Fact databases &amp;#039;&amp;#039;are a unique Visual Prolog feature, which will be described in a later tutorial. You can also add objects from the &amp;#039;&amp;#039;&amp;#039;Local Variables &amp;#039;&amp;#039;&amp;#039;window to the &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window, that way you can track the state changes in interesting objects.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Breakpoints &amp;#039;&amp;#039;&amp;#039;window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_breakPointsWindow.png]]&lt;br /&gt;
&lt;br /&gt;
shows the current breakpoints in the program (I have set open breakpoint so that the window would not be empty). You can set/remove breakpoints by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Toggle Breakpoint&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The remaining debug windows are for low level debugging and will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
The Integrated Development Environment (IDE) makes it possible for you to:&lt;br /&gt;
&lt;br /&gt;
*create&lt;br /&gt;
&lt;br /&gt;
*build&lt;br /&gt;
&lt;br /&gt;
*maintain, and&lt;br /&gt;
&lt;br /&gt;
*debug&lt;br /&gt;
&lt;br /&gt;
your projects. It contains a number of tools, editor and wizards that will help you in your tasks.&lt;br /&gt;
&lt;br /&gt;
[[ru:Среда_разработки]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_dialogEditorAbout2.png&amp;diff=3825</id>
		<title>File:Tut01 dialogEditorAbout2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_dialogEditorAbout2.png&amp;diff=3825"/>
		<updated>2013-09-18T14:50:39Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 dialogEditorAbout2.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: About dialog in dialog editor&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindowAbout.png&amp;diff=3824</id>
		<title>File:Tut01 projectWindowAbout.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindowAbout.png&amp;diff=3824"/>
		<updated>2013-09-18T14:49:40Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 projectWindowAbout.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: About dialog in project window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_errorS.png&amp;diff=3823</id>
		<title>File:Tut01 errorS.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_errorS.png&amp;diff=3823"/>
		<updated>2013-09-18T14:41:58Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 errorS.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Error window&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_editRun1S.png&amp;diff=3822</id>
		<title>File:Tut01 editRun1S.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_editRun1S.png&amp;diff=3822"/>
		<updated>2013-09-18T14:41:30Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 editRun1S.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Go to run predicate in editor&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow4.png&amp;diff=3821</id>
		<title>File:Tut01 projectWindow4.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow4.png&amp;diff=3821"/>
		<updated>2013-09-18T14:33:21Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 projectWindow4.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Project Window 4&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow3.png&amp;diff=3820</id>
		<title>File:Tut01 projectWindow3.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow3.png&amp;diff=3820"/>
		<updated>2013-09-18T14:32:29Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 projectWindow3.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Project Window 3&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow2.png&amp;diff=3819</id>
		<title>File:Tut01 projectWindow2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow2.png&amp;diff=3819"/>
		<updated>2013-09-18T14:31:00Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 projectWindow2.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Project Window 2&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow1.png&amp;diff=3818</id>
		<title>File:Tut01 projectWindow1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectWindow1.png&amp;diff=3818"/>
		<updated>2013-09-18T14:30:02Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:Tut01 projectWindow1.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Environment Overview: Project Window 1&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectSettingsS.png&amp;diff=3817</id>
		<title>File:Tut01 projectSettingsS.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_projectSettingsS.png&amp;diff=3817"/>
		<updated>2013-09-18T14:11:06Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:Tut01_totalS.png&amp;diff=3816</id>
		<title>File:Tut01 totalS.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:Tut01_totalS.png&amp;diff=3816"/>
		<updated>2013-09-18T14:07:30Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3814</id>
		<title>Ide/Environment Overview</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Ide/Environment_Overview&amp;diff=3814"/>
		<updated>2013-09-18T13:40:04Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Creation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{ideNavbar|Environment Overview}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial I will give an overview of the Integrated Development Environment (IDE).The IDE is used to create, develop and maintain your Visual Prolog projects.&lt;br /&gt;
&lt;br /&gt;
Briefly speaking you will use the IDE for the following tasks in a project life cycle:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Creation&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is created in the IDE, at creation time you choose important properties of your project, such as whether the project is an executable or a DLL, whether it uses GUI or is text based, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Building&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The project is built, i.e. compiled and linked, from the IDE.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Browsing&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE and the compiler collect information about the project, which is utilized in various ways for quick localization of entities, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Development&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;During the development and maintenance of the project, the IDE is used to add and remove source files and GUI entities to the project and to edit these.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Debugging&amp;#039;&amp;#039;&amp;#039;:&amp;lt;br&amp;gt;The IDE is also used for debugging the project. The debugger is used to follow program execution and exploit the program state when the program runs.In the sequel we will look more detailed at each of these things.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that the tutorial will start from scratch and create a project that is used for the whole tutorial. The reader is encouraged to do the same. Also notice that clicking on reduced pictures will show them in full scale in a separate window.&lt;br /&gt;
&lt;br /&gt;
==Creation==&lt;br /&gt;
&lt;br /&gt;
First we will create a project by selecting &amp;#039;&amp;#039;&amp;#039;Project -&amp;gt; New...&amp;#039;&amp;#039;&amp;#039; in the menu. In response to this you are presented to a dialog containing various properties of the project.&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectSettingsS.png]]&lt;br /&gt;
&lt;br /&gt;
I have chosen that my project should have the name &amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The project name is also used as the name of the target that is produced.  In this case the target is an &amp;#039;&amp;#039;exe&amp;#039;&amp;#039; file so the target name will be &amp;#039;&amp;#039;tut01.exe&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
I have also chosen that the target should be a GUI program, i.e. a program with a graphical user interface.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Base Directory&amp;#039;&amp;#039;&amp;#039; is the &amp;quot;base&amp;quot; of all your projects; you should choose a place that is convenient for you.&lt;br /&gt;
&lt;br /&gt;
The new project will be created in a &amp;#039;&amp;#039;&amp;#039;Sub-Directory&amp;#039;&amp;#039;&amp;#039; of the base directory. By default this directory have the same name as the project itself.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Package Prefix&amp;#039;&amp;#039;&amp;#039; is used when creating new &amp;quot;packages&amp;quot; in the system (a later tutorial will explain about the package notion). In case your source files will ever be shared with somebody else it is a good idea if the prefix does not conflict with other prefixes. Therefore it is a good idea to use a prefix that start with a reverse Internet domain name, like &amp;#039;&amp;#039;com/visual-prolog &amp;#039;&amp;#039;(which is the reverse of &amp;#039;&amp;#039;visual-prolog.com&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
For the moment you do not need to consider the remaining tabs and options.&lt;br /&gt;
&lt;br /&gt;
Now create the project by pressing &amp;#039;&amp;#039;&amp;#039;OK&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The IDE will now look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_totalS.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, which contains information about the entities in your project. &lt;br /&gt;
You have the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039;, which will contain various status and progress messages.&lt;br /&gt;
&lt;br /&gt;
==Building==&lt;br /&gt;
&lt;br /&gt;
Before we make any modifications, we will build (i.e. compile and link) the project. In the &amp;#039;&amp;#039;&amp;#039;Build&amp;#039;&amp;#039;&amp;#039; menu you find commands for building, compiling and executing the project.&lt;br /&gt;
&lt;br /&gt;
If you choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039; the project is first build, so that it is an up to date version you execute. Therefore I will choose &amp;#039;&amp;#039;&amp;#039;Execute&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
If you have not registered Visual Prolog you will be presented to a special screen telling you so. I will suggest that you register, but you can also choose &amp;quot;Continue Evaluation&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;&amp;#039;&amp;#039;Messages Window&amp;#039;&amp;#039;&amp;#039; the IDE writes which files are compiled, etc.&lt;br /&gt;
&lt;br /&gt;
If the build process succeeds, which it does in this case, the created program is executed. As a result you will see a little &amp;#039;&amp;#039;doing-nothing &amp;#039;&amp;#039;GUI program. You might notice that the program looks a bit like the IDE itself. This is no coincidence since the IDE is actually a Visual Prolog program.&lt;br /&gt;
&lt;br /&gt;
Later in this tutorial we will also see what happens, if the compiler or the linker detects errors in the build process.&lt;br /&gt;
&lt;br /&gt;
==Browsing==&lt;br /&gt;
&lt;br /&gt;
Right now we will turn our eyes to the &amp;#039;&amp;#039;&amp;#039;Project Tree&amp;#039;&amp;#039;&amp;#039; in the &amp;#039;&amp;#039;&amp;#039;Project Window&amp;#039;&amp;#039;&amp;#039;, and explore that a bit. The tree itself is presented in a standard Windows tree control so you should already be familiar with its usage. Here we shall concentrate on the contents of the tree.&lt;br /&gt;
&lt;br /&gt;
Right now the project tree will look like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow1.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
The top node represents the project, and the project directory.&lt;br /&gt;
&lt;br /&gt;
Right below that is the logical node &amp;#039;&amp;#039;&amp;#039;$(ProDir)&amp;#039;&amp;#039;&amp;#039; which represents the directory in which Visual Prolog is installed. This directory contains (as we shall shortly see) libraries and library code from the Visual Prolog system.&lt;br /&gt;
&lt;br /&gt;
Then comes another directory &amp;#039;&amp;#039;&amp;#039;TaskWindow,&amp;#039;&amp;#039;&amp;#039; which is a subdirectory of the project directory. This directory contains all the code needed to produce the Task Window, its menu and toolbar and the about dialog.&lt;br /&gt;
&lt;br /&gt;
Finally, you see a number of files. Visual Prolog uses the following conventions:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ph&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;package headers&amp;#039;&amp;#039;&amp;#039;. A package is a collection of classes and interfaces that are intended to be used as a chunk.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pack&amp;#039;&amp;#039;&amp;#039; files are &amp;#039;&amp;#039;&amp;#039;packages&amp;#039;&amp;#039;&amp;#039;. They contain the implementation/definition of the corresponding .ph files.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;i&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;interface&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cl&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class declaration&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;pro&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;class implementation&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you expand the main.cl node completely you will see the following:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
This sub-tree shows that the file &amp;#039;&amp;#039;&amp;#039;main.cl&amp;#039;&amp;#039;&amp;#039; contains a class called &amp;#039;&amp;#039;&amp;#039;main&amp;#039;&amp;#039;&amp;#039;, which contains two predicates called classinfo and run respectively.  Predicates are subroutines, but they will not be considered further in this tutorial.&lt;br /&gt;
&lt;br /&gt;
If we collapse this node again and instead expand the &amp;#039;&amp;#039;&amp;#039;TaskWindow&amp;#039;&amp;#039;&amp;#039; node, you will see this tree:&lt;br /&gt;
&lt;br /&gt;
You will notice several new kinds of nodes:&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;dlg&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;dialog&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;frm&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;form&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;win&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;window&amp;#039;&amp;#039;&amp;#039; (task window or conventional PFC GUI window)&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;mnu&amp;#039;&amp;#039;&amp;#039; file contains a &amp;#039;&amp;#039;&amp;#039;menu&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ico&amp;#039;&amp;#039;&amp;#039; file contains an &amp;#039;&amp;#039;&amp;#039;icon&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you continue to explore you might also meet:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow3.png|Project Tree: TaskWindow]]&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;ctl&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;controls&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;tb&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;toolbars&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;cur&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;cursors&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;bmp&amp;#039;&amp;#039;&amp;#039; files containing &amp;#039;&amp;#039;&amp;#039;bitmaps&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*.&amp;#039;&amp;#039;&amp;#039;lib&amp;#039;&amp;#039;&amp;#039; that are &amp;#039;&amp;#039;&amp;#039;libraries&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
If you right click on a node a context menu will appear, with commands that have relevance for that particular node.&lt;br /&gt;
&lt;br /&gt;
If you double click on a node the corresponding entity will be brought up in a suitable editor. All code is edited in a text editor, but windows resources like dialogs and menus are edited in graphical editors. We will look closer to graphical editors below, here we shall try to bring up a text editor.&lt;br /&gt;
&lt;br /&gt;
Some entities are represented twice in the tree, because they have both a declaration and a definition/implementation. For example, the predicate run in the class &amp;#039;&amp;#039;&amp;#039;tut01&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindow2.png|Project Tree]] [[Image:tut01_projectWindow4.png|Project Tree]]&lt;br /&gt;
&lt;br /&gt;
Try double clicking on each of the run nodes, and notice that two editors open showing the declaration and the definition of the run predicate, respectively.&lt;br /&gt;
&lt;br /&gt;
The IDE has other facilities for browsing to specific entities, but these will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Development==&lt;br /&gt;
&lt;br /&gt;
Now lets try to modify the project a bit. Since we have not yet concerned ourselves with how to program in Visual Prolog, we will keep the changes simple.&lt;br /&gt;
&lt;br /&gt;
We will start by deliberately introducing an error, that way you will not be completely surprised if you make some thing wrong, at the same time you can see the error window.&lt;br /&gt;
&lt;br /&gt;
First you should locate the clauses for the run predicate in the file &amp;#039;&amp;#039;&amp;#039;main.pro&amp;#039;&amp;#039;&amp;#039;. If you double click the last of the two run nodes in the project tree, an editor opens with the caret placed like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_editRun1S.png]]&lt;br /&gt;
&lt;br /&gt;
Try inserting a fail like this (notice the comma after show):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
        TaskWindow = taskWindow::new(),&lt;br /&gt;
        TaskWindow:show(),&lt;br /&gt;
        fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that we try to build the project again: simply press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination. The system will now save and compile the file, but since we have made an error, the &amp;#039;&amp;#039;&amp;#039;Error Window&amp;#039;&amp;#039;&amp;#039; will open:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_errorS.png|Error Wondow]]&lt;br /&gt;
&lt;br /&gt;
We will consider what the error message actually means. Instead you should try to double-click on the error message. You will notice that the editor gets focus again and that the caret points exactly to the fail we inserted.&lt;br /&gt;
&lt;br /&gt;
Reestablish the code without the fail and build the project again.&lt;br /&gt;
&lt;br /&gt;
Next we will try to make a modification in the about dialog. It is not a very sensible change, but it will illustrate a number of things.&lt;br /&gt;
&lt;br /&gt;
First we will open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, to do this you double click the dialog in the project tree. The node you have to double click is shown here:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_projectWindowAbout.png|Project Window: About Dialog]]&lt;br /&gt;
&lt;br /&gt;
Once you have clicked it you will see the dialog in the &amp;#039;&amp;#039;&amp;#039;Dialog Editor&amp;#039;&amp;#039;&amp;#039;, two toolbars and properties window.&lt;br /&gt;
&lt;br /&gt;
We will add a button to the dialog. You click on the &amp;quot;button&amp;quot; on the controls toolbar, and then you click in the dialog next to the project icon in the &amp;#039;&amp;#039;&amp;#039;AboutDialog&amp;#039;&amp;#039;&amp;#039; window. As a result you can see the properties of the new control and you can edit them. I have changed the &amp;#039;&amp;#039;&amp;#039;Text&amp;#039;&amp;#039;&amp;#039; to &amp;quot;Press Me&amp;quot;, this is the text that will appear on the button. I can also see the &amp;#039;&amp;#039;&amp;#039;Name&amp;#039;&amp;#039;&amp;#039; field, the IDE will identify this particular control in various contexts by this Name.&lt;br /&gt;
&lt;br /&gt;
Now it should look something like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_dialogEditorAbout2.png|Dialog Editor: About Dialog with new button]]&lt;br /&gt;
&lt;br /&gt;
Now we want to do something whenever the button is pressed. To do this we will open the &amp;#039;&amp;#039;&amp;#039;Code Expert&amp;#039;&amp;#039;&amp;#039;: Right click the dialog and select &amp;#039;&amp;#039;&amp;#039;Code Expert&amp;#039;&amp;#039;&amp;#039;. The code expert will open and I have located my button.&lt;br /&gt;
&lt;br /&gt;
The blue dot indicates that the control is not handled. The IDE has suggested the name onPushButton for the handling predicate. We will choose &amp;#039;&amp;#039;&amp;#039;Add&amp;#039;&amp;#039;&amp;#039; to add this predicate to our code and to bind it to this control.&lt;br /&gt;
&lt;br /&gt;
If you double-click the line corresponding to the control in the code expert, an editor will open positioned at the newly inserted code.&lt;br /&gt;
&lt;br /&gt;
We will change the button text when the button is pressed. To do this we modify the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onPushButtonClick : button::clickResponder.&lt;br /&gt;
clauses&lt;br /&gt;
    onPushButtonClick(_Source) = button::defaultAction() :-&lt;br /&gt;
        pushButton_ctl:setText(&amp;quot;Pressed&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now try to build and execute the program again (i.e. press the &amp;#039;&amp;#039;&amp;#039;Ctrl+Shift+B&amp;#039;&amp;#039;&amp;#039; key combination).  If you have followed the steps carefully this should be a success. In the program you should of course open the &amp;#039;&amp;#039;&amp;#039;About&amp;#039;&amp;#039;&amp;#039; dialog, by selecting the menu entry &amp;#039;&amp;#039;&amp;#039;Help -&amp;gt; About&amp;#039;&amp;#039;&amp;#039;; and then press the new button.&lt;br /&gt;
&lt;br /&gt;
This is how it looked in my application:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_AboutPressMe.png|About: Press Me]][[Image:tut01_AboutPressed.png|About Pressed]]&lt;br /&gt;
&lt;br /&gt;
==Debugging==&lt;br /&gt;
&lt;br /&gt;
The development environment contains a debugger. With the debugger you can trace the execution of your program and examine the program state.&lt;br /&gt;
&lt;br /&gt;
It does not make much sense to go into details with debugging before you have learned the Visual Prolog language. But I will briefly explain the debugger anyway, because it can be very useful for understanding and experimenting with the language.&lt;br /&gt;
&lt;br /&gt;
To start the debugger, select &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Run&amp;#039;&amp;#039;&amp;#039; in the menu.  If the project is not up-to-date it will first be build, after that the debugging session will start.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; you can stop the debugging at any time by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Stop Debugging&amp;#039;&amp;#039;&amp;#039; in the menu.&lt;br /&gt;
&lt;br /&gt;
When debugging starts the IDE will first load the debug information and then it will enter the program. The program execution is stopped just before the goal is executed. To indicate this, the goal is opened in an editor window, and the blue arrow points to the goal:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_debug1.png|Editor: Current Position]]&lt;br /&gt;
&lt;br /&gt;
You can single step in the program using the &amp;#039;&amp;#039;&amp;#039;Step Into &amp;#039;&amp;#039;&amp;#039;and &amp;#039;&amp;#039;&amp;#039;Step Over&amp;#039;&amp;#039;&amp;#039; commands in the &amp;#039;&amp;#039;&amp;#039;Debug &amp;#039;&amp;#039;&amp;#039;menu.&lt;br /&gt;
&lt;br /&gt;
Try &amp;#039;&amp;#039;&amp;#039;Step Into&amp;#039;&amp;#039;&amp;#039;: this will open another editor with the code of mainExe::run, and the arrow will point to the entry of this code.&lt;br /&gt;
&lt;br /&gt;
In the view menu you can open various debugger windows, which I will briefly explain.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Run Stack&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_runStack.png|Run Stack]]&lt;br /&gt;
&lt;br /&gt;
contains a &amp;quot;picture&amp;quot; of the run stack. Exactly what you see depends on your settings in &amp;#039;&amp;#039;&amp;#039;Tools -&amp;gt; Options... -&amp;gt; Debugger&amp;#039;&amp;#039;&amp;#039;.  In principle, the run stack contains a number of lines corresponding to calls that has been made. However, optimization (so called last call optimization) might have removed some entries.&lt;br /&gt;
&lt;br /&gt;
The run stack not only shows the calls, it also shows trap points and backtrack points, these is described in a later tutorial and there I also describe the run stack window in more details.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Local Variables&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_localVariables.png|Local Variables]]&lt;br /&gt;
&lt;br /&gt;
contains the local variables corresponding to the selection in the run stack window. Some variables might not yet have received a value, these are show as an underscore (i.e. like originalraiser in the above picture).&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_factsWindow.png|Facts]]&lt;br /&gt;
&lt;br /&gt;
Contains the global state of the program, this state is stored in fact databases. &amp;#039;&amp;#039;Fact databases &amp;#039;&amp;#039;are a unique Visual Prolog feature, which will be described in a later tutorial. You can also add objects from the &amp;#039;&amp;#039;&amp;#039;Local Variables &amp;#039;&amp;#039;&amp;#039;window to the &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; window, that way you can track the state changes in interesting objects.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Breakpoints &amp;#039;&amp;#039;&amp;#039;window:&lt;br /&gt;
&lt;br /&gt;
[[Image:tut01_breakPointsWindow.png]]&lt;br /&gt;
&lt;br /&gt;
shows the current breakpoints in the program (I have set open breakpoint so that the window would not be empty). You can set/remove breakpoints by selecting &amp;#039;&amp;#039;&amp;#039;Debug -&amp;gt; Toggle Breakpoint&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The remaining debug windows are for low level debugging and will not be considered here.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
The Integrated Development Environment (IDE) makes it possible for you to:&lt;br /&gt;
&lt;br /&gt;
*create&lt;br /&gt;
&lt;br /&gt;
*build&lt;br /&gt;
&lt;br /&gt;
*maintain, and&lt;br /&gt;
&lt;br /&gt;
*debug&lt;br /&gt;
&lt;br /&gt;
your projects. It contains a number of tools, editor and wizards that will help you in your tasks.&lt;br /&gt;
&lt;br /&gt;
[[ru:Среда_разработки]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_03.png&amp;diff=3811</id>
		<title>File:FundamentalVisualPrologData 03.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_03.png&amp;diff=3811"/>
		<updated>2013-09-17T16:40:25Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologData 03.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - the Data Layer 03&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_04.png&amp;diff=3810</id>
		<title>File:FundamentalVisualPrologData 04.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_04.png&amp;diff=3810"/>
		<updated>2013-09-17T16:39:08Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologData 04.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - the Data Layer 04&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_02.png&amp;diff=3809</id>
		<title>File:FundamentalVisualPrologData 02.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_02.png&amp;diff=3809"/>
		<updated>2013-09-17T16:38:28Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologData 02.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - the Data Layer 02&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_01.png&amp;diff=3808</id>
		<title>File:FundamentalVisualPrologData 01.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologData_01.png&amp;diff=3808"/>
		<updated>2013-09-17T16:37:39Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologData 01.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - the Data Layer 01&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog_-_the_Business_Logical_Layer&amp;diff=3807</id>
		<title>Fundamental Visual Prolog - the Business Logical Layer</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog_-_the_Business_Logical_Layer&amp;diff=3807"/>
		<updated>2013-09-17T16:18:37Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Connecting the Business Logic Layer to the GUI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we shall modify the earlier simple &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; (Graphical User Interface) front end to the family tutorial which was developed in the tutorial &amp;quot;Fundamental Visual Prolog - GUI&amp;quot;. We shall isolate the logic of the code from the rest of the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; code. This logic would be reposed into an object belonging to its own package. We shall see that this method of isolating the code would enable us to clearly think about the logical issues separately from that of the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Download&amp;#039;&amp;#039;&amp;#039; source files of the example project used in this tutorial:&lt;br /&gt;
&lt;br /&gt;
* Visual Prolog 7.4 and 7.3 install examples in the IDE:&lt;br /&gt;
*:&amp;#039;&amp;#039;&amp;#039;Help -&amp;gt; Install Examples...&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* [http://download.pdc.dk/vip/72/tutorial/family3.zip Visual Prolog 7.2 version].&lt;br /&gt;
* [http://download.pdc.dk/vip/71/tutorial/family3.zip Visual Prolog 7.1 version].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Initial Preparations==&lt;br /&gt;
&lt;br /&gt;
In this tutorial, you would need to setup the project&amp;#039;s &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; exactly as explained in the &amp;quot;Fundamental Visual Prolog - GUI&amp;quot; tutorial in a series of sequential steps before the section titled &amp;quot;Getting the Main Code into the Program&amp;quot;. We shall assume that you have performed those &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; construction procedures correctly, and hence they are not repeated here.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Important:&amp;#039;&amp;#039;&amp;#039; We are also assuming that you have created the AncestorDialog dialog in the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; and given the code, so that that module has defined a predicate called getName to retrieve the name of the ancestor entered in that dialog.&lt;br /&gt;
&lt;br /&gt;
We will assume that the project is entitled &amp;#039;&amp;#039;&amp;#039;family3&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; of the program is technically known as the presentation layer&amp;#039;&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&amp;#039; Earlier, the code for the logic was intermingled within that presentation layer. Now, we shall take that logic and put it into its own package within the project. That package would act as a business logic layer.&lt;br /&gt;
&lt;br /&gt;
Note that we are using the phrase &amp;#039;&amp;#039;&amp;quot;business logic&amp;quot;&amp;#039;&amp;#039; instead of merely the &amp;#039;&amp;#039;&amp;quot;logic&amp;quot;&amp;#039;&amp;#039;, because it indicates the program&amp;#039;s &amp;#039;&amp;#039;&amp;#039;business&amp;#039;&amp;#039;&amp;#039; side. In short, the raison-de-etre of the program&amp;lt;!--webbot bot=&amp;quot;PurpleText&amp;quot; PREVIEW=&amp;quot;French&amp;quot; --&amp;gt;is taken care of by that layer.&lt;br /&gt;
&lt;br /&gt;
As Visual Prolog is a strongly object-oriented language, the business logic would now be represented by an object. This tutorial cannot get into all the details of object oriented programming present in Visual Prolog. They are separately explained in the tutorial &amp;quot;Introduction to Classes and Objects&amp;quot; and you are requested to read that tutorial for details. However, for completeness sake, some ideas regarding object-oriented would be mentioned in this tutorial also, wherever relevant. But before all that, let us complete the program in a step-by-step fashion.&lt;br /&gt;
&lt;br /&gt;
==Creating the Business Logic Layer==&lt;br /&gt;
&lt;br /&gt;
Once you have setup the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; of the &amp;#039;&amp;#039;&amp;#039;family3&amp;#039;&amp;#039;&amp;#039; project, the main project tree would look something like this:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig1.png]]&lt;br /&gt;
&lt;br /&gt;
Now right click at the top of the tree and from the menu that is presented click on the New item as shown below:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig2.png]]&lt;br /&gt;
&lt;br /&gt;
From the Create Project Item dialog that is presented, select Package (the first item in the list on the left hand side) and give the name of the package as &amp;#039;&amp;#039;&amp;#039;familyBLL&amp;#039;&amp;#039;&amp;#039; (as a mnemonic for &amp;#039;&amp;#039;Family Business Logic Layer&amp;#039;&amp;#039;; you should ideally choose something which is meaningful).&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig3.png]]&lt;br /&gt;
&lt;br /&gt;
On clicking Create the project tree will now look like this:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig4.png]]&lt;br /&gt;
&lt;br /&gt;
If you now take a peep into the folder on your hard-disk, where these files are stored, you would notice that the IDE has created a separate folder (called familyBLL) within the main project folder to store all the files of this particular package.&lt;br /&gt;
&lt;br /&gt;
Now, build the project to ensure that everything is in order.&lt;br /&gt;
&lt;br /&gt;
==Working on the Business Logic Layer==&lt;br /&gt;
&lt;br /&gt;
Let us now put the logic in place. We shall create and design a familyBL class in the familyBLL package to take full advantage of the object-oriented features of Visual  Prolog. In this particular tutorial, not all the features are demonstrable. However, we shall be using one of the main features of an object-oriented system; namely that of data encapsulation. (Complete details on object-oriented features of Visual Prolog are available in the &amp;quot;Introduction to Classes and Objects&amp;quot; tutorial).&lt;br /&gt;
&lt;br /&gt;
The term data encapsulation represents hiding the data within the code of a particular module in such a manner that it is not directly available for reading or modification from other modules of the program. Data access is strictly regulated and is indirectly available for reading and modifications through predicates of the class.&lt;br /&gt;
&lt;br /&gt;
This concept avoids one of the most commonest programming errors seen in many programming languages. When data are kept lying around (metaphorically) for all programmers working on the project to read or write; it often becomes too tempting for programmers to write quick &amp;#039;n dirty code to access and modify such data. This can be problematic especially in asynchronous programs such as &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; programs, where data can change unpredictably; often as per the behavior of the people using the program.&lt;br /&gt;
&lt;br /&gt;
Another advantage of data encapsulation is that internal data representation systems can often be modified within a class, without the rest of the code in the project getting affected.&lt;br /&gt;
&lt;br /&gt;
==Creating the Business Logic Class==&lt;br /&gt;
&lt;br /&gt;
In the Project Tree, right click on the familyBLL package folder. From the menu that opens up, click on &amp;#039;&amp;#039;&amp;#039;New&amp;#039;&amp;#039;&amp;#039;, as seen below:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig5.png]]&lt;br /&gt;
&lt;br /&gt;
From the Create Project Item dialog that is presented, create a class called &amp;#039;&amp;#039;&amp;#039;familyBL&amp;#039;&amp;#039;&amp;#039;. Ensure that the Creates Objects check box is switched on, as seen below:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig6.png]]&lt;br /&gt;
&lt;br /&gt;
The moment you click on the &amp;#039;&amp;#039;&amp;#039;Create&amp;#039;&amp;#039;&amp;#039;  button, the IDE will present the &amp;#039;&amp;#039;&amp;#039;familyBL.pro&amp;#039;&amp;#039;&amp;#039; file to you for editing. Replace the default code from implement to end implement to the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement familyBL&lt;br /&gt;
open core&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
   fileName : string.&lt;br /&gt;
clauses&lt;br /&gt;
   new(Filename) :-&lt;br /&gt;
      filename := Filename,&lt;br /&gt;
      file::consult(Filename, familyDB).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
   save() :-&lt;br /&gt;
      file::save(filename, familyDB).&lt;br /&gt;
&lt;br /&gt;
facts - familyDB&lt;br /&gt;
   person : (string Name, gender Gender).&lt;br /&gt;
   parent : (string Person, string Parent).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
   father(Person, Father) :-&lt;br /&gt;
      parent(Person, Father),&lt;br /&gt;
      person(Father, male()).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
   grandfather(Person, Grandfather) :-&lt;br /&gt;
      parent(Person, Parent),&lt;br /&gt;
      father(Parent, Grandfather).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
   ancestor(Person, Ancestor) :-&lt;br /&gt;
      parent(Person, Ancestor).&lt;br /&gt;
   ancestor(Person, Ancestor) :-&lt;br /&gt;
      parent(Person, P1),&lt;br /&gt;
      ancestor(P1, Ancestor).&lt;br /&gt;
&lt;br /&gt;
end implement&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, click on the IDE File|Open menu item, open the file familyBL.i, and change the default code seen there with this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface familyBL&lt;br /&gt;
open core&lt;br /&gt;
&lt;br /&gt;
domains&lt;br /&gt;
   gender = female(); male().&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
   father : (string Person, string Father) nondeterm (o,o) (i,o).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
   grandFather : (string Person, string Grandfather) nondeterm (o,o) .&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
   ancestor : (string Person, string Ancestor) nondeterm  (i,o).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
   save: ().&lt;br /&gt;
&lt;br /&gt;
end interface&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more file automatically created by the IDE requires to be modified. Click on the File|Open file menu item in the IDE, and open the familyBL.cl file.&lt;br /&gt;
&lt;br /&gt;
Before the predicates section you see in that file, you would need to insert the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;constructors&lt;br /&gt;
  new : (string Filename).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After inserting the above code, the file will look like this:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig7.png]]&lt;br /&gt;
&lt;br /&gt;
Now build the project and check if everything is okay. It should be, and at this point, even though the code is in place, the project would technically compile into an empty project as we have not put in the &amp;#039;&amp;#039;glue&amp;#039;&amp;#039; to connect the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; to the business logic layer. But before that, let us understand why the code for the familyBL class was put into three different files.&lt;br /&gt;
&lt;br /&gt;
==Understanding the Business Logic Class==&lt;br /&gt;
&lt;br /&gt;
If you compare the code that we have written so far with that we wrote in the earlier tutorial, you may arrive at these conclusions:&lt;br /&gt;
&lt;br /&gt;
*The code was broken up into three sections.&lt;br /&gt;
*Some predicates and domains were declared in the .i file.&lt;br /&gt;
*All implementations were put inside the .pro file.&lt;br /&gt;
*The .cl file contained the declaration of a special kind of predicate called a constructor.&lt;br /&gt;
&lt;br /&gt;
When we asked the IDE to create a class for us, we indicated that the class would generate &amp;#039;&amp;#039;objects&amp;#039;&amp;#039;. It is important to differentiate the difference between a class and an object.&lt;br /&gt;
&lt;br /&gt;
Metaphorically, a class could be looked upon as a cafeteria that gives out packaged pizzas. Just having such a cafeteria would not ensure that there would be pizzas coming out! One would need to explicitly &amp;#039;&amp;#039;instruct&amp;#039;&amp;#039; the cafeteria to send out the pizzas.&lt;br /&gt;
&lt;br /&gt;
Just like the cafeteria, which has the capability of creating pizzas, a class has the capability of creating objects. An &amp;#039;&amp;#039;object&amp;#039;&amp;#039; contains &amp;#039;&amp;#039;code&amp;#039;&amp;#039; and&amp;#039;&amp;#039; data&amp;#039;&amp;#039; that is put together as per the implementation found in the class. Hence the class is analogous to the cafeteria. It can produce the objects. But not just yet!&lt;br /&gt;
&lt;br /&gt;
Just because a class was coded in the project; it does not automatically imply that the project would automatically contain objects of that class. In every class, which is designed to generate objects, there would have to be one or more special  constructor/s, which will construct a prepackaged object containing all the relevant code and data for working with the object.&lt;br /&gt;
&lt;br /&gt;
Please note that Visual Prolog also allows creation of classes that does not generate objects, and the code within those classes mainly serve the purpose of modularization. This tutorial is not the right place for discussing such classes that do not create objects.&lt;br /&gt;
&lt;br /&gt;
The constructors of a class are written in the .cl file of the said class. Predicates that are publicly accessible to the entire class as a whole (as opposed to the objects that the class can create) will also be present in that file. This can be seen in the utility predicate called classInfo, which is also declared in that file.&lt;br /&gt;
&lt;br /&gt;
Each object that is created would require to have some publicly accessible predicates to manipulate the data which is encapsulated within it. Such predicates are declared separately in the .i file of the relevant class.&lt;br /&gt;
&lt;br /&gt;
Finally, the implementation file (.pro extension) will contain the actual code. For all practical purposes, none of that code would really be seen by the other modules.&lt;br /&gt;
&lt;br /&gt;
All this work may seem a little laborious. In fact, usually, the opposite is true: the clear cut manner, in which object-oriented programming has been implemented in Visual Prolog, can easily be put to use practically. For example, it is often a good strategy to assign different packages to different programmers, who would stitch up a complex project together with the minimum amount of hurdles, using object-oriented methodologies.&lt;br /&gt;
&lt;br /&gt;
Now having understood all of that, we have to remember one important thing: the project still does not do anything useful simply because the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; is not connected to the business logic layer. We shall now proceed to rectify that hurdle.&lt;br /&gt;
&lt;br /&gt;
==Connecting the Business Logic Layer to the GUI==&lt;br /&gt;
&lt;br /&gt;
In the Project Tree, right-click on the TaskWindow.win entry, and select &amp;#039;&amp;#039;&amp;#039;Open&amp;#039;&amp;#039;&amp;#039; as shown below.&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig8.png]]&lt;br /&gt;
&lt;br /&gt;
In the Code Expert dialog, ensure that default handlers for the id_file_open, id_Query_father, id_Query_grand_father, id_Query_ancestor_of menu items are set, as seen below:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualPrologBusiness_fig9.png]]&lt;br /&gt;
&lt;br /&gt;
The event handler for id_file_exit for exiting the application correctly would have already been set by the IDE, so you do not have to set it again.&lt;br /&gt;
&lt;br /&gt;
Before we populate the event handlers with the correct code, the presentation layer should contain one fact that will be used to store the object that the program is handling at any point in time.&lt;br /&gt;
&lt;br /&gt;
Hence in the TaskWindow.pro file, the following code must be given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;facts&lt;br /&gt;
  familyBL_db : (familyBL BL) determ.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above fact will contain the object representing the family&amp;#039;s business logic layer.&lt;br /&gt;
&lt;br /&gt;
For the id_file_open menu item event handler give the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    onFileOpen(_Source, _MenuTag) :-&lt;br /&gt;
        Filename = vpiCommonDialogs::getFileName(&amp;quot;*.txt&amp;quot;,&lt;br /&gt;
           [&amp;quot;Family data files (*.txt)&amp;quot;,&amp;quot;*.txt&amp;quot;,&lt;br /&gt;
            &amp;quot;All files&amp;quot;, &amp;quot;*.*&amp;quot;],&lt;br /&gt;
            &amp;quot;Load family database&amp;quot;,&lt;br /&gt;
            [], &amp;quot;.&amp;quot;, _),&lt;br /&gt;
        !,&lt;br /&gt;
        BL = familyBL::new(Filename),&lt;br /&gt;
        retractAll(familyBL_db(_)),&lt;br /&gt;
        assert(familyBL_db(BL)),&lt;br /&gt;
        stdio::writef(&amp;quot;Database % loaded\n&amp;quot;, Filename).&lt;br /&gt;
onFileOpen(_Source, _MenuTag).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before we write the other event handlers, we need to insert a utility predicate as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
   tryGetFamilyBL : () -&amp;gt; familyBL BL determ.&lt;br /&gt;
clauses&lt;br /&gt;
   tryGetFamilyBL() = BL :-&lt;br /&gt;
      familyBL_db(BL),&lt;br /&gt;
      !.&lt;br /&gt;
   tryGetFamilyBL() = _ :-&lt;br /&gt;
      stdio::write(&amp;quot;No family database loaded\n&amp;quot;),&lt;br /&gt;
      fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above utility predicate is needed for gracefully handling situations, where the user may be attempting to query the family database before the database itself is loaded into the program. As we had indicated in an earlier tutorial, a &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; allows usage of the program as per the user&amp;#039;s directions. The sequence of events that the user may undertake can never be fully known by the programmer. In fact, it is a good practice to allow the user to decide what set of activities is to be carried out in a &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; program.&lt;br /&gt;
&lt;br /&gt;
As the program developed in this tutorial also adheres to such an approach, in our little program it is impossible to be really sure whether the user did load the database or not, when the querying is being done. There is always the possibility that the database was not loaded. Hence the program is instructed to use the above utility predicate for giving a graceful message to the user in the case there was no database loaded into the memory.&lt;br /&gt;
&lt;br /&gt;
Now let us concentrate on the rest of the event handlers.&lt;br /&gt;
&lt;br /&gt;
For the id_Query_Father menu item, the following event handler is to be given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    onQueryFather(_Source, _MenuTag) :-&lt;br /&gt;
        stdio::write(&amp;quot;\nfather test\n&amp;quot;),&lt;br /&gt;
        foreach BL = tryGetFamilyBL(), BL:father(X,Y) do&lt;br /&gt;
            stdio::writef(&amp;quot;% is the father of %\n&amp;quot;, Y, X)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the id_Query_Grand_Father menu item, the following event handler code is to be given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    onQueryGrandFather(_MenuTag) = handled(0) :-&lt;br /&gt;
        stdio::write(&amp;quot;\ngrandFather test\n&amp;quot;),&lt;br /&gt;
        foreach BL = tryGetFamilyBL(), BL:grandfather(X, Y) do&lt;br /&gt;
            stdio::writef(&amp;quot;% is the grandfather of %\n&amp;quot;, Y, X)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And lastly, for the id_Query_Ancestor&amp;#039;&amp;#039;,&amp;#039;&amp;#039; the following event handler code is to be given:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    onQueryAncestorOf(_Source, _MenuTag) :-&lt;br /&gt;
        X = ancestorDialog::getName(thisWin),&lt;br /&gt;
        stdio::writef(&amp;quot;\nancestor of % test\n&amp;quot;, X),&lt;br /&gt;
        foreach BL = tryGetFamilyBL(), BL:ancestor(X, Y) do&lt;br /&gt;
            stdio::writef(&amp;quot;% is the ancestor of %\n&amp;quot;, Y, X)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you notice, all the above three event handlers attempts to retrieve the object under consideration using the tryGetFamilyBL() utility predicate, instead of directly accessing the facts section. In the case the database was not loaded, the utility predicate will fail after giving a graceful message in the Messages window.&lt;br /&gt;
&lt;br /&gt;
==Understanding the Listeners==&lt;br /&gt;
&lt;br /&gt;
If you examine the event handlers that we have now developed, and compare them with those which were developed in the &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; tutorial, you would notice that there are only small differences.&lt;br /&gt;
&lt;br /&gt;
The most significant one is in the first event handler (onFileOpen) where the program constructs a new object of the program&amp;#039;s business layer class and stores that object into the facts section of the module.&lt;br /&gt;
&lt;br /&gt;
Another key point to be noted is the use of publicly accessible methods of the business layer object, which has been stored in the facts section of TaskWindow.pro module.&lt;br /&gt;
&lt;br /&gt;
For example, in the following code snippet, the program retrieves a business logic layer object, signified by the variable BL, and then executes the publicly accessible predicate called grandfather of that object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
        ....&lt;br /&gt;
        foreach BL = tryGetFamilyBL(), BL:grandfather(X, Y) do&lt;br /&gt;
            stdio::writef(&amp;quot;% is the grandfather of %\n&amp;quot;, Y, X)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The colon operator &amp;quot;&amp;#039;&amp;#039;&amp;#039;:&amp;#039;&amp;#039;&amp;#039;&amp;quot; between the variable and the predicate is an indicator of that logic. The colon operator which retrieves the publicly accessible predicate of an object is different from the double-colon operator &amp;quot;::&amp;quot;, which retrieves a publicly accessible predicate of the entire class. This is seen in the stdio::writef(...) predicate call in the above code snippet. Note that the double-colon operator cannot be used on objects and neither can the colon operator be used on classes.&lt;br /&gt;
&lt;br /&gt;
==Running the Code==&lt;br /&gt;
&lt;br /&gt;
At this point, we can now compile and run the program. If there are any errors, then I would request you to re-read this code and maybe the earlier tutorial also and check for any errors in your program. One point to be noted is that while setting the event handlers, do &amp;#039;&amp;#039;not&amp;#039;&amp;#039; change an event handler name for the event for which a hander was &amp;#039;&amp;#039;already&amp;#039;&amp;#039; set earlier.&lt;br /&gt;
&lt;br /&gt;
When you run this program, you would notice that the program functions exactly in the same manner as before. This is one of the fundamental beauties of object oriented programming: it allows quite a lot of internal coding changes without the final functionality changing.&lt;br /&gt;
&lt;br /&gt;
==Some More Details==&lt;br /&gt;
&lt;br /&gt;
In this system of object-oriented programming class facts are changed into object facts, as each object would encapsulate its own facts.&lt;br /&gt;
&lt;br /&gt;
Let us use once again the cafeteria example and make an analogy of &amp;#039;&amp;#039;facts&amp;#039;&amp;#039; with the pizza toppings. This situation is therefore like stating that the toppings on each pizza would be dispatched along with each individual pizza. The toppings would not be left behind at the cafeteria else all the customers would have to come back to the cafeteria to use the toppings! Fortunately, it does make more sense to send the toppings along with the pizza. So is the case with object-oriented programming. It often makes sense to package the facts into the &amp;#039;&amp;#039;objects&amp;#039;&amp;#039; of a class rather than put the facts into the class itself.&lt;br /&gt;
&lt;br /&gt;
When we use objects to represent the data, the maintenance of the data becomes much easier. If we do not use an object-oriented approach we may require to clean up the remnants of the earlier data by using the retract predicate (retract is a built-in predicate of Visual Prolog). However, retract is not needed now because a new object is created on load and the old object will be automatically removed by the program using a process called garbage-collection. This happens automatically in the background during some known safe conditions without affecting the user.&lt;br /&gt;
&lt;br /&gt;
When declaring predicates the flows of the parameters to a predicate must be specifically added to predicate declarations, the way we did in the .i file. The anyflow flow-pattern cannot be used on global entities.&lt;br /&gt;
&lt;br /&gt;
In the code implemented in the business layer, we had implemented a predicate for saving the data back to a file. If you note, the save predicate does not need a file name, because the file name is stored in the object. It is left as an exercise to you, to connect this functionality to some suitable &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039; event handlers.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
&lt;br /&gt;
In this tutorial we learnt that a finer separation of business logic and its data from the presentation (i.e. &amp;#039;&amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;&amp;#039;) is not only possible but is strongly recommended. Visual Prolog can intelligently assist us create a package containing all the classes that are needed to manage this separation. In this particular tutorial, one class was developed in the package, which achieved this objective. Such a class could create objects that encapsulated both the data and business logic. Other modules are able to function only on a need-to-know basis regarding the business logic layer using such objects. This makes the maintenance and modular development of software much more easier and less error prone.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Fundamental Visual Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig8.png&amp;diff=3806</id>
		<title>File:FundamentalVisualPrologBusiness fig8.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig8.png&amp;diff=3806"/>
		<updated>2013-09-17T16:07:37Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig8.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 08&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig9.png&amp;diff=3805</id>
		<title>File:FundamentalVisualPrologBusiness fig9.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig9.png&amp;diff=3805"/>
		<updated>2013-09-17T16:03:24Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig9.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 09&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig6.png&amp;diff=3804</id>
		<title>File:FundamentalVisualPrologBusiness fig6.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig6.png&amp;diff=3804"/>
		<updated>2013-09-17T15:35:34Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig6.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 06&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig5.png&amp;diff=3803</id>
		<title>File:FundamentalVisualPrologBusiness fig5.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig5.png&amp;diff=3803"/>
		<updated>2013-09-17T15:34:21Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig5.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 05&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig4.png&amp;diff=3802</id>
		<title>File:FundamentalVisualPrologBusiness fig4.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig4.png&amp;diff=3802"/>
		<updated>2013-09-17T15:29:18Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig1.png&amp;diff=3801</id>
		<title>File:FundamentalVisualPrologBusiness fig1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig1.png&amp;diff=3801"/>
		<updated>2013-09-17T15:28:08Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig3.png&amp;diff=3799</id>
		<title>File:FundamentalVisualPrologBusiness fig3.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig3.png&amp;diff=3799"/>
		<updated>2013-09-17T15:18:26Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig3.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 03&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig2.png&amp;diff=3798</id>
		<title>File:FundamentalVisualPrologBusiness fig2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualPrologBusiness_fig2.png&amp;diff=3798"/>
		<updated>2013-09-17T15:17:19Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualPrologBusiness fig2.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fundamental Visual Prolog - Business 02&lt;br /&gt;
&lt;br /&gt;
{{Tutorial Image}}&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualProlog8.png&amp;diff=3795</id>
		<title>File:FundamentalVisualProlog8.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=File:FundamentalVisualProlog8.png&amp;diff=3795"/>
		<updated>2013-09-17T06:57:39Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: uploaded a new version of &amp;quot;Image:FundamentalVisualProlog8.png&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog&amp;diff=3794</id>
		<title>Fundamental Visual Prolog</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog&amp;diff=3794"/>
		<updated>2013-09-17T06:55:29Z</updated>

		<summary type="html">&lt;p&gt;Gukalov: /* Step 4: Rebuild the Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we will introduce a program that is developed on the Visual Prolog platform. The essential algorithms of the tutorial are the same as those that were developed in the tutorial [[Fundamental Prolog Part 2]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Differences in Visual Prolog and Traditional Prolog==&lt;br /&gt;
&lt;br /&gt;
The differences between traditional Prolog and Visual Prolog can be broadly divided into these categories:&lt;br /&gt;
&lt;br /&gt;
*Program structure differences: There are distinct, yet easy to understand differences between the structure used in traditional Prolog and that used in Visual Prolog. It essentially comprises of understanding how to mark out the declarations from the definitions, and to indicate the main &amp;#039;&amp;#039;Goal&amp;#039;&amp;#039; that the program has to seek using specific keywords.&lt;br /&gt;
&lt;br /&gt;
*File considerations: Visual Prolog gives various facilities to organize the structure of the program into different kinds of files.&lt;br /&gt;
&lt;br /&gt;
*Scope access issues: A Visual Prolog program can pick up functionality developed in other modules using a concept called &amp;#039;&amp;#039;scope identification&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Object orientation: A Visual Prolog program can be written as an object-oriented program, using classic object oriented features.&lt;br /&gt;
&lt;br /&gt;
===Program Structure Differences===&lt;br /&gt;
&lt;br /&gt;
====Declarations and Definitions====&lt;br /&gt;
&lt;br /&gt;
In Prolog, when we need to use a predicate we simply do so without any prior intimation to the Prolog engine about our intention. For example; in the earlier tutorial the grandFather predicate&amp;#039;s clause was directly written down using the traditional Prolog&amp;#039;s predicate head and body construction. We did not bother to inform the engine explicitly in the code that such a predicate construction is to be expected later.&lt;br /&gt;
&lt;br /&gt;
Similarly, when a compound domain is to be used in traditional Prolog, we can use it without first forewarning the Prolog engine about our intention to use the domain. We simply use a domain, the moment we feel the need for it.&lt;br /&gt;
&lt;br /&gt;
However, in Visual Prolog, before writing down the code for the clause body of a predicate, we would first need to declare the existence of such a predicate to the compiler. Similarly, before using any domains, they need to be declared and their presence is to be informed to the compiler.&lt;br /&gt;
&lt;br /&gt;
The reason such forewarnings are required in Visual Prolog is essentially to ensure that runtime exceptions are converted into compile time errors as far as possible.&lt;br /&gt;
&lt;br /&gt;
By &amp;#039;&amp;#039;&amp;quot;runtime exceptions&amp;quot;&amp;#039;&amp;#039;, we mean the issues that turn up only at the time of running the program that were compiled with it. For example, if you had intended to use an integer as the argument of a functor, and instead of the integer you had erroneously used a real number, it would become a &amp;#039;&amp;#039;runtime&amp;#039;&amp;#039; error (in programs written using most other compilers, but not Visual Prolog) and the program would fail there.&lt;br /&gt;
&lt;br /&gt;
When you also declare the predicates and domains that are defined, such kind of positional grammar (which argument belongs to which domain), etc. is made available to the compiler. Therefore, when Visual Prolog performs a compilation, it checks the program quite thoroughly to weed out such grammatical and other mistakes.&lt;br /&gt;
&lt;br /&gt;
Because of this feature in Visual Prolog, the overall efficiency of a programmer improves. The programmer does not have to wait till the program is actually executed to detect a bug. In fact, those of you, who are experienced in programming, would understand what a life-saver this can be; for often, the particular sequence of events that is causing a &amp;#039;&amp;#039;runtime exception&amp;#039;&amp;#039; to happen at run-time may be so elusive that the bug may actually turn up after several years, or it may manifest itself in some crucially critical or other embarrassing situations!&lt;br /&gt;
&lt;br /&gt;
All this automatically implies that the compiler &amp;#039;&amp;#039;&amp;#039;has&amp;#039;&amp;#039;&amp;#039; to be given explicit instructions regarding the predicates and domains that exist in the code using appropriate declarations before the same are defined.&lt;br /&gt;
&lt;br /&gt;
====Keywords====&lt;br /&gt;
&lt;br /&gt;
A Visual Prolog program consists of Prolog code which is punctuated into different &amp;#039;&amp;#039;&amp;#039;sections&amp;#039;&amp;#039;&amp;#039; by appropriate keywords that inform the compiler the code it has to generate. For example, there are keywords that differentiate the &amp;#039;&amp;#039;declarations&amp;#039;&amp;#039; from the &amp;#039;&amp;#039;definitions&amp;#039;&amp;#039; of predicates and domains. Usually, each section is preceded by a keyword. There is normally no keyword which signifies the ending of a particular section. The presence of another keyword indicates the ending of the previous section, and the starting of the next one.&lt;br /&gt;
&lt;br /&gt;
The exception to this rule, are the keywords &amp;quot;implement&amp;quot; and &amp;quot;end implement&amp;quot; The code contained between these two keywords indicates the code to be used for a particular class. Those of you who do not understand the concept of a &amp;quot;class&amp;quot; can, for now (i.e. in this tutorial), think of it as a module or a section of the overall program code.&lt;br /&gt;
&lt;br /&gt;
For the purpose of this tutorial, we&amp;#039;ll introduce only the following keywords (given below). We are also giving the purpose behind these keywords, and the actual syntax can be easily learnt from the documentation. There are other keywords also in Visual Prolog, and those can easily be picked up in later tutorials and the documentation.&lt;br /&gt;
&lt;br /&gt;
The list of the keywords that you need to know in this tutorial is the following:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;implement&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;end implement&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:Among all the keywords discussed here, this is the only one, which exists as a pair. Visual Prolog treats the code written between these two keywords as the code that belongs to one class. The name of the class MUST be given after the implement keyword.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;open&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This keyword is used to extend the &amp;#039;&amp;#039;scope visibility&amp;#039;&amp;#039; of the class. It is to be used just after the implement keyword.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;constants&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This keyword is used to mark a section of the code that defines some commonly used values in the program code. For example, if the string literal &amp;quot;PDC Prolog&amp;quot; is to be used in multiple locations throughout the code, then you can define a mnemonic (a short-form, easily remembered word) for the same thus:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;lt;vip&amp;gt;constants&lt;br /&gt;
    pdc = &amp;quot;PDC Prolog&amp;quot;.&amp;lt;/vip&amp;gt;&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:Note that the definition of a constant ends in a period (.). Unlike a Prolog variable, a constant should be a word starting with a lower case letter.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This keyword is used to mark a section declaring the domains that would be used in the code. There are many variations for the syntax of such domain declarations, and they cater to the all the possible kinds of domains that would be used later on in the code. As this tutorial is a basic one, we shall not get into the finer details of the domain declarations that can be possible.&lt;br /&gt;
&lt;br /&gt;
:To summarize here, you would be declaring the functor that would be used for the domain and the kind of domains that would form its arguments. Functors and compound domains were explained in detail in the previous part of the Tutorial.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;class facts&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This keyword designates a section, which declares the facts that would be used later on in the code of the program. Each fact is declared with the name used to signify the fact and the arguments that are used for the respective facts along with the domains that those arguments belong to.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;class predicates&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This section contains the declarations of the predicates that would be later defined in the clauses section of the code. Once again, the names that would be used for these predicates along with the arguments and the domains, to which the arguments belong to, would be indicated in this section.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;clauses&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:Of all the sections that are present in a Visual Prolog code, this section is the one that closely mimics a traditional Prolog program. It contains the actual &amp;#039;&amp;#039;definitions&amp;#039;&amp;#039; of the previously declared predicates. And you would find that the predicates used here would follow the syntax as declared in the &amp;#039;&amp;#039;class predicates&amp;#039;&amp;#039; section.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;goal&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
:This section defines the main entry point into a Visual Prolog program. A more detailed explanation is given below.&lt;br /&gt;
&lt;br /&gt;
====Goal====&lt;br /&gt;
&lt;br /&gt;
In traditional Prolog, whenever a predicate is defined in the code, the Prolog Engine can be instructed to start the code execution from that predicate onwards. However, that is not the case with Visual Prolog. Being a &amp;#039;&amp;#039;compiler&amp;#039;&amp;#039; it has the responsibility of producing efficiently executing code for the program you write. This code would not be actually executing at the same time the &amp;#039;&amp;#039;compiler&amp;#039;&amp;#039; is doing its work. Hence, the &amp;#039;&amp;#039;compiler&amp;#039;&amp;#039; needs to know beforehand the exact predicate from which the code execution would start, so that later on when the program is called to perform, it can do so from the correct starting point. As you may have guessed, the compiled program can run independently without the Visual Prolog compiler or the IDE itself.&lt;br /&gt;
&lt;br /&gt;
In order to do that, there is a special section indicated by the keyword &amp;#039;&amp;#039;Goal&amp;#039;&amp;#039;. Think of it as a special predicate that you would write without arguments. That predicate is the one from which the entire program execution will start.&lt;br /&gt;
&lt;br /&gt;
===File Considerations===&lt;br /&gt;
&lt;br /&gt;
Many times, it may become cumbersome to put all the parts of the program into one file itself. It may even make the program unreadable and sometimes incorrect. Visual Prolog has the capability of dividing the program code into separate files using the IDE (Integrated Development Environment) and it is possible to write neat pieces of code into separate files using that IDE. When it is done in that manner, things that are to be used commonly can be accessed across files. E.g. If you have a domain that is to be used in multiple files, then the declaration of that domain is done in a separate file, and that file is then accessed from other files.&lt;br /&gt;
&lt;br /&gt;
However, for the purpose of simplifying this particular tutorial, we shall predominantly be using only one file to develop the program code. In the course of constructing the program, the IDE would automatically create some more files which we can ignore for the time being. We can learn about them in future tutorials.&lt;br /&gt;
&lt;br /&gt;
===Scope Access Issues===&lt;br /&gt;
&lt;br /&gt;
Visual Prolog divides the total program code into separate parts, each part defining one class. In object oriented languages, a class is a package of code and its associated data which is put together. This requires more explanation which would be available in later tutorials. As noted earlier, for those of you who are not familiar with object oriented programs, you can think of a &amp;#039;&amp;#039;class&amp;#039;&amp;#039; loosely as being synonymous to &amp;#039;&amp;#039;modules&amp;#039;&amp;#039;. Normally, Visual Prolog defines each class in its own separate file.&lt;br /&gt;
&lt;br /&gt;
During the program execution, it often so happens that the program may need to invoke a predicate that is actually defined in another class (file). Similarly, data (constants) or domains defined in a class may need to be accessed from a different file.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog allows such cross class code/data references using a concept called scope access. This can be understood by an example. Suppose there is a predicate called pred1 defined in a class called class1 (which is written down in another file using the IDE), and we need to call that predicate in the clause body of some other predicate, pred2 in a different file (say class2) then this is how pred1 would be called within the clause body of pred2 :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    pred3:-&lt;br /&gt;
        ...&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    pred2:-&lt;br /&gt;
        class1::pred1, % pred1 is not known in this class. It is defined in some other class, hence a class qualifier is needed&lt;br /&gt;
        pred3,&lt;br /&gt;
        ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, you can see that the clause body of pred2 calls two predicates pred1 and pred3. As pred1 is defined in another file (which defines class1), the word class1 with the token &amp;#039;&amp;#039;&amp;#039;::&amp;#039;&amp;#039;&amp;#039; (two colons) precedes the word pred1. This can be called as a class qualifier.But the predicate pred3 is defined within the same file as pred2 itself. Hence there is no need to indicate class2:: prior to the predicate call for pred3.&lt;br /&gt;
&lt;br /&gt;
This behavior is technically explained thus: The &amp;#039;&amp;#039;scope visibility&amp;#039;&amp;#039; of pred3 is within the same scope of pred2. Hence there is no need to clarify that pred3 is from the same class as pred2. The compiler would automatically look for the definition of pred3 within the same scope area as defined for class2.&lt;br /&gt;
&lt;br /&gt;
The scope area of a particular class definition is restricted to the class that is implemented in a particular file (i.e. code that is written within the implement &amp;#039;&amp;#039;-&amp;#039;&amp;#039; end implement keywords). The predicates defined therein can call each other without the class qualifier and the double colon (::) token preceding it.&lt;br /&gt;
&lt;br /&gt;
The scope area of a class can be extended by using the open keyword. This keyword informs the compiler to bring in names (of predicates / constants / domains) that were defined in other files. If a scope area is extended, then we need not write the class qualifier with the double colon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;open class1&lt;br /&gt;
            ...&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    pred3:-&lt;br /&gt;
        ...&lt;br /&gt;
        !.&lt;br /&gt;
&lt;br /&gt;
    pred2:-&lt;br /&gt;
        pred1, %Note: &amp;quot;class1::&amp;quot; qualifier is not needed anymore, as the scope area is extended using the &amp;#039;open&amp;#039; keyword&lt;br /&gt;
        pred3,&lt;br /&gt;
        ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Object Orientation===&lt;br /&gt;
&lt;br /&gt;
The current version of Visual Prolog is a strongly object oriented language. The ENTIRE code which is developed for a program is put into appropriate classes, as needed. This happens by default, even if you are not interested in the object oriented features of the language. You would notice this feature in the example given in this tutorial also. The code is inserted into a class called &amp;quot;family1&amp;quot; even though we would eventually not use any objects created from that class. Instead, we would use publicly accessible predicates of the code within that class directly.&lt;br /&gt;
&lt;br /&gt;
This tutorial will not handle the object oriented features of the language. Future tutorials would extend this concept, and even actually use the object oriented features.&lt;br /&gt;
&lt;br /&gt;
==A Full Fledged Example: family1.prj6==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Download:&amp;#039;&amp;#039;&amp;#039; source files of the example project used in this tutorial:&lt;br /&gt;
&lt;br /&gt;
* Visual Prolog 7.4 and 7.3 install examples in the IDE:&lt;br /&gt;
*:&amp;#039;&amp;#039;&amp;#039;Help -&amp;gt; Install Examples...&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* [http://download.pdc.dk/vip/72/tutorial_examples/fundamental_visual_prolog/family1.zip Visual Prolog 7.2 version]&lt;br /&gt;
* [http://download.pdc.dk/vip/71/examples/tutorial_examples/fundamental_visual_prolog/family1.zip Visual Prolog 7.1 version]&lt;br /&gt;
&lt;br /&gt;
Let us now put all the knowledge we&amp;#039;ve gathered together to create our first Visual Prolog program. This will contain the same basic logic that was explored in the Tutorial &amp;quot;Fundamental Prolog. Part 2&amp;quot;. All the code that is to be written for this tutorial is shown below. The actual code writing will be done using the Visual Prolog IDE (Integrated Development Environment). There are some more files that are needed for the tutorial, but those would automatically be generated and maintained by the IDE. The step by step instructions (using screenshots) for utilizing the IDE to develop the program will shortly follow.&lt;br /&gt;
&lt;br /&gt;
But first of all, let us acquaint ourselves with the main code of the program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement main&lt;br /&gt;
    open core&lt;br /&gt;
&lt;br /&gt;
    domains&lt;br /&gt;
        gender = female(); male().&lt;br /&gt;
&lt;br /&gt;
    class facts - familyDB&lt;br /&gt;
        person : (string Name, gender Gender).&lt;br /&gt;
        parent : (string Person, string Parent).&lt;br /&gt;
&lt;br /&gt;
    class predicates&lt;br /&gt;
        father : (string Person, string Father) nondeterm anyflow.&lt;br /&gt;
    clauses&lt;br /&gt;
        father(Person, Father) :-&lt;br /&gt;
            parent(Person, Father),&lt;br /&gt;
            person(Father, male()).&lt;br /&gt;
&lt;br /&gt;
    class predicates&lt;br /&gt;
        grandFather : (string Person, string GrandFather) nondeterm (o,o).&lt;br /&gt;
    clauses&lt;br /&gt;
        grandFather(Person, GrandFather) :-&lt;br /&gt;
            parent(Person, Parent),&lt;br /&gt;
            father(Parent, GrandFather).&lt;br /&gt;
&lt;br /&gt;
    class predicates&lt;br /&gt;
        ancestor : (string Person, string Ancestor) nondeterm (i,o).&lt;br /&gt;
    clauses&lt;br /&gt;
        ancestor(Person, Ancestor) :-&lt;br /&gt;
            parent(Person, Ancestor).&lt;br /&gt;
        ancestor(Person, Ancestor) :-&lt;br /&gt;
            parent(Person, P1),&lt;br /&gt;
            ancestor(P1, Ancestor).&lt;br /&gt;
&lt;br /&gt;
    class predicates&lt;br /&gt;
        reconsult : (string FileName).&lt;br /&gt;
    clauses&lt;br /&gt;
        reconsult(FileName) :-&lt;br /&gt;
            retractFactDB(familyDB),&lt;br /&gt;
            file::consult(FileName, familyDB).&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        run():-&lt;br /&gt;
            console::init(),&lt;br /&gt;
            stdIO::write(&amp;quot;Load data\n&amp;quot;),&lt;br /&gt;
            reconsult(&amp;quot;..\\fa.txt&amp;quot;),&lt;br /&gt;
            stdIO::write(&amp;quot;\nfather test\n&amp;quot;),&lt;br /&gt;
            father(X, Y),&lt;br /&gt;
                stdIO::writef(&amp;quot;% is the father of %\n&amp;quot;, Y, X),&lt;br /&gt;
            fail.&lt;br /&gt;
        run():-&lt;br /&gt;
            stdIO::write(&amp;quot;\ngrandFather test\n&amp;quot;),&lt;br /&gt;
            grandFather(X, Y),&lt;br /&gt;
                stdIO::writef(&amp;quot;% is the grandfather of %\n&amp;quot;, Y, X),&lt;br /&gt;
            fail.&lt;br /&gt;
        run():-&lt;br /&gt;
            stdIO::write(&amp;quot;\nancestor of Pam test\n&amp;quot;),&lt;br /&gt;
            X = &amp;quot;Pam&amp;quot;,&lt;br /&gt;
            ancestor(X, Y),&lt;br /&gt;
                stdIO::writef(&amp;quot;% is the ancestor of %\n&amp;quot;, Y, X),&lt;br /&gt;
            fail.&lt;br /&gt;
        run():-&lt;br /&gt;
            stdIO::write(&amp;quot;End of test\n&amp;quot;).&lt;br /&gt;
end implement main&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    mainExe::run(main::run).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step 1: Create a New Console Project in the IDE===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 1a.&amp;#039;&amp;#039;&amp;#039; After starting the Visual Prolog IDE, click on the New menu item from the Project menu.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 1b.&amp;#039;&amp;#039;&amp;#039; A dialog would be presented to you. Enter all the relevant information. Ensure that the Project Kind is &amp;#039;&amp;#039;Console&amp;#039;&amp;#039; and NOT &amp;#039;&amp;#039;GUI&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualProlog2.png]]&lt;br /&gt;
&lt;br /&gt;
===Step 2: Build an Empty Project===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 2a.&amp;#039;&amp;#039;&amp;#039; When the project is just created, the IDE would display the following project window. At this point in time, it does not have any clue about what files the project is dependent on. It does, however create the basic source code files for the project.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 2b.&amp;#039;&amp;#039;&amp;#039; Use the Build menu item of the Build menu, to compile and link the empty project to create an executable file which basically does not do anything. (At this point it time, it would not generate any errors either)&lt;br /&gt;
&lt;br /&gt;
While building the project, take a look at the messages that are dynamically displayed in the &amp;#039;&amp;#039;&amp;#039;Messages&amp;#039;&amp;#039;&amp;#039; window of the IDE. (In the case the &amp;#039;&amp;#039;&amp;#039;Messages&amp;#039;&amp;#039;&amp;#039; window is not visible, you can turn it on using the Window menu of the IDE) You will notice that the IDE intelligently pulls in ALL the necessary PFC (Prolog Foundation Classes) modules into your project. PFC classes are those classes which contains the basic functionality on which your programs would be built.&lt;br /&gt;
&lt;br /&gt;
===Step 3: Populate the main.pro with the Actual Code===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 3a.&amp;#039;&amp;#039;&amp;#039; The default code inserted by the IDE is very basic and does not do anything useful. You would need to delete the entire code and copy and paste the actual code of main.pro (given in this tutorial) into that window.&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualProlog5.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Step 3b.&amp;#039;&amp;#039;&amp;#039; After you perform the copy-paste operation, the main.pro window would now look as shown below:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualProlog6.png]]&lt;br /&gt;
&lt;br /&gt;
===Step 4: Rebuild the Code===&lt;br /&gt;
&lt;br /&gt;
Re-invoke the Build menu command, and the IDE will notice that the source code has changed, and will take measures to appropriately recompile the correct sections. If you use the Build All menu command, then &amp;#039;&amp;#039;ALL&amp;#039;&amp;#039; the modules would be recompiled. For large programs this can consume some time. Build All is often used ONLY at the end of a series of smaller compilations; just to ensure that everything is in ship-shape order.&lt;br /&gt;
&lt;br /&gt;
During the &amp;#039;&amp;#039;Build&amp;#039;&amp;#039; process, the IDE not only does the compilation; but it also determines whether the project may need any other missing PFC modules, and inserts those and re-starts the compilation process if required. This can be seen in the messages appearing in the Messages window, where you&amp;#039;ll notice the IDE was forced to build the project &amp;#039;&amp;#039;twice&amp;#039;&amp;#039; because of some additional include statements.&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualProlog7.png]]&lt;br /&gt;
&lt;br /&gt;
===Step 5: Execute the Program===&lt;br /&gt;
&lt;br /&gt;
We now have our first true application complied using Visual Prolog. In order to test the application we&amp;#039;ve just compiled, you can run the program using the Build | Run &amp;#039;&amp;#039;in&amp;#039;&amp;#039; Window menu command. However, in our case this would result in an error. The reason is that our little program is trying to read a text file (fa.txt) for its functioning. That text file contains the actual data regarding persons which our program will process. We&amp;#039;ll come to the syntax of that file a bit later.&lt;br /&gt;
&lt;br /&gt;
We need to now write that text file using some text editor and place it in the same directory where the executable is now residing. Normally, the executable would reside in the sub-folder called exe from the main project folder.&lt;br /&gt;
&lt;br /&gt;
Let us create such a file using the IDE itself. Use the File | New menu command. The Create Project Item window will appear. Select Text file from the list on the left hand side. Then select the appropriate directory where the file is to reside (The same directory where the executable file family1.exe is present). Then, give the filename &amp;#039;&amp;#039;fa.txt&amp;#039;&amp;#039; and click on the Create button of the dialog. Till the filename is given, the Create button would be grayed (disabled). Make sure that the checkbox: Add to project as module is checked on. The advantage of adding it to the project is that it would be available for any subsequent debugging, etc.&lt;br /&gt;
&lt;br /&gt;
The contents of the file would be as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    person(&amp;quot;Judith&amp;quot;,female()).&lt;br /&gt;
        person(&amp;quot;Bill&amp;quot;,male()).&lt;br /&gt;
        person(&amp;quot;John&amp;quot;,male()).&lt;br /&gt;
        person(&amp;quot;Pam&amp;quot;,female()).&lt;br /&gt;
        parent(&amp;quot;John&amp;quot;,&amp;quot;Judith&amp;quot;).&lt;br /&gt;
        parent(&amp;quot;Bill&amp;quot;,&amp;quot;John&amp;quot;).&lt;br /&gt;
        parent(&amp;quot;Pam&amp;quot;,&amp;quot;Bill&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Though it is a data file, you would notice that the syntax of the file is VERY similar to regular Prolog code! Even though the program is now compiled and is now in a binary format; you can change the output by simply changing the crucial data the program is processing. And that data is written into this text file using the same syntax as Prolog. Thus Visual Prolog emulates the &amp;#039;&amp;#039;dynamic&amp;#039;&amp;#039; code writing capability of traditional Prolog. Albeit, not all features are available but at least complicated domains such as those in this example can definitely be given.&lt;br /&gt;
&lt;br /&gt;
The syntax used in fa.txt MUST be compatible with the domain definitions of the program. E.g. The functor used for defining a person MUST be &amp;#039;&amp;#039;person&amp;#039;&amp;#039; and not anything else; else appropriate compound domain representing that functor would not get initialized. (We had covered the topic of functors and compound domains in an earlier tutorial).&lt;br /&gt;
&lt;br /&gt;
Now, when you run the program using the Build | Run in the Window menu command, this is what you&amp;#039;ll see:&lt;br /&gt;
&lt;br /&gt;
:[[Image:FundamentalVisualProlog8.png]]&lt;br /&gt;
&lt;br /&gt;
The program processes the information in the file fa.txt and works out the logical sequence of the parentages of people given there.&lt;br /&gt;
&lt;br /&gt;
===A Round up of the Program===&lt;br /&gt;
&lt;br /&gt;
The logic of the program is very simple. We had discussed it in an earlier tutorial, where we explained how the correct representation of data can help yield the appropriate results in a Prolog program. Let us now discuss the manner in which this logic works.&lt;br /&gt;
&lt;br /&gt;
At start up, the main predicate will be invoked directly. This in turns branches off to the run predicate. The first thing the run predicate will do is to read in the data which is written in the file fa.txt. Once the data is in place, the program systematically progresses from one test to another to process the data and write out the conclusions of that test onto the console.&lt;br /&gt;
&lt;br /&gt;
The mechanisms for processing the data are standard mechanisms of backtracking and recursions. This tutorial is not the place for a detailed explanation of how Prolog works, but here is a short summary of the internal workings.&lt;br /&gt;
&lt;br /&gt;
When the run predicate is calling the father predicate, it does not stop at the first satisfactory ending of the father predicate. The invoking of a fail at the end of the predicate, forces the Prolog engine to seek another pass through the father predicate. Such a behavior is called &amp;#039;&amp;#039;backtracking&amp;#039;&amp;#039;, because the Prolog engine literally seems to backtrack over code which had earlier executed.&lt;br /&gt;
&lt;br /&gt;
This happens recursively (i.e. as a repetitive process or cyclically), and at each cycle the father predicate yields the next result. Eventually all the possible definitions of &amp;quot;fathers&amp;quot; given in the data are exhausted, and the run predicate would have no choice but carry over to the next clause body of the run predicate.&lt;br /&gt;
&lt;br /&gt;
The father predicate (as well as some other predicates) has been declared to be non-deterministic. The keyword nondeterm can be used to declare non-deterministic predicates; i.e. predicates that can yield multiple answers using backtracking.&lt;br /&gt;
&lt;br /&gt;
If the no keyword is used in a predicate declaration, then the predicate has got the procedure mode, which can give only one solution, and the father predicate will stop after yielding the first result and cannot be re-entered into. Also, one must be careful in the usage of the cut (!) in the clause bodies of such non-deterministic predicates. If there is a cut at the end of the clause body of the father predicate, yet again; the predicate will yield only one solution (even if it has been declared to be non-deterministic).&lt;br /&gt;
&lt;br /&gt;
The anyflow flow-pattern tells the compiler that the parameters that are given for the predicate could be either free or bound, with no restrictions whatsoever. That means, with the same definition of the father predicate, it would be possible to both yield the father&amp;#039;s name (in case the name of the offspring was bound) OR the offspring&amp;#039;s name (in case the name of the father was bound). It can also handle situations where both are bound; in which case the predicate will check if such a relationship exists in the data.&lt;br /&gt;
&lt;br /&gt;
And; to state the obvious; the anyflow flow pattern can also accommodate a situation where both the parameters of the father predicate are free variables. In such a case, the father predicate will yield the various combinations of father+offspring present in the data the program has learnt (through consulting the fa.txt file) to the enquirer. The last usage is what is performed in the run predicate, as seen in the code snippet below. You would notice that the variables X and Y given to the father predicate are not bound when the father predicate is called.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
        console::init(),&lt;br /&gt;
        stdIO::write(&amp;quot;Load  data\n&amp;quot;),&lt;br /&gt;
        reconsult(&amp;quot;fa.txt&amp;quot;),&lt;br /&gt;
        stdIO::write(&amp;quot;\nfather test\n&amp;quot;),&lt;br /&gt;
        father(X,Y),&lt;br /&gt;
        stdIO::writef(&amp;quot;% is the father of %\n&amp;quot;, Y, X),&lt;br /&gt;
        fail.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In this lesson we learnt that programs written in Visual Prolog are often very similar to those written in traditional Prolog. There are several keywords that are used to differentiate the various parts of a Visual Prolog source code. Though Visual Prolog is an object-oriented language, it is possible to develop code which avoids the language&amp;#039;s object-oriented features. A complete console-based application (family1) was developed in this tutorial, which explains, how to create such a program.&lt;br /&gt;
&lt;br /&gt;
We also found that it is possible to emulate the dynamic working of a traditional Prolog program by keeping part of the code as a data file outside the main binary compiled application. The syntax of such a data file closely follows Prolog syntax.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[[ru:Основы Системы Visual Prolog]]&lt;br /&gt;
[[Category:Fundamental Visual Prolog Tutorial]]&lt;/div&gt;</summary>
		<author><name>Gukalov</name></author>
	</entry>
</feed>