<?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=Taras.evt</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=Taras.evt"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Special:Contributions/Taras.evt"/>
	<updated>2026-05-24T06:09:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog&amp;diff=3883</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=3883"/>
		<updated>2014-03-23T19:42:25Z</updated>

		<summary type="html">&lt;p&gt;Taras.evt: The code states, that the file is in the directory, which is one level up from execution path. The executable is in &amp;quot;\Exe\&amp;quot;, therefore fa.txt should be in the root of project (alongside of main.pro).&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 project file main.pro 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>Taras.evt</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Prolog_Part_2&amp;diff=3882</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=3882"/>
		<updated>2014-03-23T17:52:38Z</updated>

		<summary type="html">&lt;p&gt;Taras.evt: Fix typo in code&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;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>Taras.evt</name></author>
	</entry>
</feed>