<?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=Kari+Rastas</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=Kari+Rastas"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Special:Contributions/Kari_Rastas"/>
	<updated>2026-05-01T20:23:24Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Category:VpPuZzle&amp;diff=2055</id>
		<title>Category:VpPuZzle</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Category:VpPuZzle&amp;diff=2055"/>
		<updated>2009-11-28T08:55:15Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: spelling error corrected&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{spbCopyright}}&lt;br /&gt;
&lt;br /&gt;
VpPuZzle is set of agreements to build VIP-based applications on the basis of the standard VIP packages placed into DLLs. The VIP package fitted to PZL-technology agreements is named pzl-Component . The VIP project (executable or DLL), which contains pzlComponents, organized in a special way, is named pzl-Container . The pzl-container, which is the DLL, is named dll-container.&lt;br /&gt;
The VpPuZzle is created to work with the Visual Prolog 7.1 and above.&lt;br /&gt;
You need the only minimal knowledge in Visual Prolog programming system to [[Pzl-Component in 10 steps|&amp;#039;&amp;#039;create and run the pzl-component&amp;#039;&amp;#039;]].&lt;br /&gt;
&lt;br /&gt;
The pzl-technology includes: &lt;br /&gt;
*The set of statically linked libraries, which support the pzl-system run&lt;br /&gt;
*Classes, which support the communication with the Pzl-system&lt;br /&gt;
*The Elementary PzlStudio application, which gives the possibility&lt;br /&gt;
**to include the pzl-system into any application created in Visual Prolog.&lt;br /&gt;
**To create the dll-containers&lt;br /&gt;
**To create pzl-components and put them into the pzl-containers&lt;br /&gt;
**to perform other activities, which support the development process.&lt;br /&gt;
&lt;br /&gt;
=The Information regarding the pzl-technology=&lt;br /&gt;
The general concept, which is the basis of the Pzl-technology, described in  &amp;#039;&amp;#039;&amp;#039;[[VpPuZzle. Observe. Part 1|VpPuZzle. Observe]]&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
&lt;br /&gt;
The agreements, which pzl-Components must fit, described in the manual &amp;#039;&amp;#039;&amp;#039;[[Pzl-Component]]&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The programmer may communicate with the Pzl-system by means of  predicates of classes, which are described in:&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;[[Class PzlPort]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;[[Class Pzl]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The description of the Elementary PzlStudio placed to the document &amp;#039;&amp;#039;&amp;#039;[[Elementary PzlStudio]]&amp;#039;&amp;#039;&amp;#039;. Parts of this document may be found also separately.&lt;br /&gt;
&lt;br /&gt;
=The example of the building the Pzl-container and the Pzl-Component=&lt;br /&gt;
&lt;br /&gt;
In the document [[Pzl-Component in 10 steps|&amp;#039;&amp;#039;&amp;quot;Pzl-Component in 10 steps&amp;quot;&amp;#039;&amp;#039;]] it is described in details how to build the Dll-container &amp;#039;&amp;#039;MyDemoContainer&amp;#039;&amp;#039; and the Dll-Component &amp;#039;&amp;#039;MyHelloWorld&amp;#039;&amp;#039; together with the demonstration of the run directly from the development environment &amp;#039;&amp;#039;Elementary PzlStudio&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
To run this example you need to install the appropriate set of tools related to pzl-technology.&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Class_PzlPort&amp;diff=2054</id>
		<title>Class PzlPort</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Class_PzlPort&amp;diff=2054"/>
		<updated>2009-11-28T08:53:35Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: spelling errors corrected&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{spbCopyright}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;package PzlPort&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The class pzlPort is the core of the VpPuzzle system and supports the use of pzl-components. This class must be included to the project, which generates the main executable application(.EXE).&lt;br /&gt;
&lt;br /&gt;
The calls of the predicates of the class PzlPort are possible only from the classes of the main executable application project. Components placed to the dll-containers can not make a calls of the predicats of the class PzlPort.&lt;br /&gt;
&lt;br /&gt;
=Predicate summary=&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot; style=&amp;quot;float:center;background:#F9F9F9;color:black;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|[[#core::classInfo/0|classInfo]].&lt;br /&gt;
get the information regarding the class&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::init/0|init:()]].&lt;br /&gt;
Initialization of the VpPuZzle system&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::setComponentRegisterFileName/1|setComponentRegisterFileName:(string ComponentRegisterFileName)]].&lt;br /&gt;
Set the name of the component registry file&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::isInitialized/0|isInitialized:()  determ]].&lt;br /&gt;
Checking of the initialization status of the VpPuZzle system&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Additionally used resources=&lt;br /&gt;
The mandatory use:&lt;br /&gt;
*the pzlPort_XX.lib library must be used, where  XX - license qualifier. The library in fact contains the implementation of the class PzlPort and also the library includes the implementation of the handling of the pzl-container of the main executable application.&lt;br /&gt;
*the package pzlConfig must be used. The package PzlConfig participates in the initialization procedure.&lt;br /&gt;
&lt;br /&gt;
=Predicate details=&lt;br /&gt;
==core::classInfo/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;classInfo:().&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
get the information regarding the class&lt;br /&gt;
|full=&lt;br /&gt;
The predicate is the tipical predicate, which is generated when any class generated automatically by the IDE of the Visual Prolog System&lt;br /&gt;
}}&lt;br /&gt;
==pzlPort::init/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;init:().&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Initialization of the VpPuZzle system&lt;br /&gt;
|full=&lt;br /&gt;
The predicate must be the first predicate, which is used while the accessing to the Pzl-system. It must be called only once. After initialization the calls to other classes of the Pzl system become possible. &lt;br /&gt;
&lt;br /&gt;
The initialization of the pzl-system may be performed immediately after the main application start. It also may be performed at any moment when the pzl-system is expected to be used.&lt;br /&gt;
The de-initialization of the pzl-system is impossible.&lt;br /&gt;
&lt;br /&gt;
The repetitions of the initialization are ignored.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
  run():-&lt;br /&gt;
    pzlPort::init(),&lt;br /&gt;
    TaskWindow = taskWindow::new(),&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
==pzlPort::setComponentRegisterFileName/1==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;setComponentRegisterFileName:(string ComponentRegisterFileName).&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Set the name of the component registry file&lt;br /&gt;
|full=&lt;br /&gt;
The file ComponentRegisterFileName is one of the places, where the information regarding the pzl-components at the given computer is stored. The other place of store is the Windows registry. When the searching process runs the component registry file is used first. If the file doesn&amp;#039;t exist or the component is not registered there, then the search process continued in the Windows registry.&lt;br /&gt;
&lt;br /&gt;
The information in the component registry file is stored in the Visual Prolog term format (save-consult data format). Before the component may be used at the given computer, it must be registered at this computer. The application, which uses the pzl-technology may avoid the use of the component registry file or the application may assign the use of the file by using the predicate setComponentRegisterFileName/1. &lt;br /&gt;
&lt;br /&gt;
The parameter &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039; must be the full path to the file, or it may be relative to the directory, where the main application is placed.&lt;br /&gt;
The file extension is not strongly defined in the pzl-system. The recommended file extension is  - &amp;#039;&amp;#039;&amp;#039;.PZR&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The component registration file of the application may be assigned only once. While the repetitive assignment the existence of the previous assignment is not checked.&lt;br /&gt;
&lt;br /&gt;
The component registration data is load only once while the first access to an any pzl-component. Thus the second (and further) file assignment is not make influence on the component registration.&lt;br /&gt;
&lt;br /&gt;
The content validation of the file  &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039; while the predicate &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;setComponentRegisterFileName&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; performing is not used. But the file must exist.&lt;br /&gt;
&lt;br /&gt;
It is possible to get the name of the component registration file, assigned at the pzl-system by the use of the predicate call &amp;lt;vip&amp;gt;RegistryFileStatus=pzl::getComponentRegisterFileName()&amp;lt;/vip&amp;gt; The predicate pzl::getComponentRegisterFileName() returns the term, which reflects the status of the assignment of the file. If the file name has been assigned successfully, then the name will be returned as the parameter of the term.&lt;br /&gt;
|except=&lt;br /&gt;
If there is no file with the given name &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039;, then the exception is generated with the text&lt;br /&gt;
&amp;lt;vip&amp;gt;&amp;quot;The file &amp;lt;ComponentRegisterFileName&amp;gt; can not be found&amp;quot;&amp;lt;/vip&amp;gt;&lt;br /&gt;
While checking the file existence the system exception are possible and they are stored with the message &amp;lt;vip&amp;gt;&amp;quot;System Error while checking the file &amp;lt;ComponentRegisterFileName&amp;gt; existence&amp;quot;&amp;lt;/vip&amp;gt;&lt;br /&gt;
The error code will be stored also.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
  runProfile:(window TaskWindow,string RegistryFileName,string InitialComponent).&lt;br /&gt;
clauses&lt;br /&gt;
  runProfile(_TaskWindow,RegistryFileName,_InitialComponent):-&lt;br /&gt;
    not(RegistryFileName=&amp;quot;&amp;quot;),&lt;br /&gt;
    trap(pzlPort::setComponentRegisterFileName(RegistryFileName),_Err,fail()),&lt;br /&gt;
    fail.&lt;br /&gt;
  runProfile(TaskWindow,RegistryFileName,InitialComponent):-&lt;br /&gt;
    RegistryFileStatus=pzl::getComponentRegisterFileName(),&lt;br /&gt;
    notifyUser(TaskWindow,RegistryFileStatus,RegistryFileName),&lt;br /&gt;
    runStudioStarter(TaskWindow,InitialComponent).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==pzlPort::isInitialized/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;isInitialized:()  determ.&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Checking of the initialization status of the VpPuZzle system&lt;br /&gt;
|full=&lt;br /&gt;
Makes it possible to check whether the initialization of the pzl-system was performed. If the result of the check is positive, then predicate succeeds.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
  run():-&lt;br /&gt;
    not(isInitialazed()),&lt;br /&gt;
    pzlPort::init(),&lt;br /&gt;
    fail.&lt;br /&gt;
  run():-&lt;br /&gt;
    TaskWindow = taskWindow::new(),&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
=References=&lt;br /&gt;
[[Category:VpPuZzle]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Class_PzlPort&amp;diff=2053</id>
		<title>Class PzlPort</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Class_PzlPort&amp;diff=2053"/>
		<updated>2009-11-28T08:49:57Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: /* pzlPort::setComponentRegisterFileName/1 */  some spelling errors corrected&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{spbCopyright}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;package PzlPort&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The class pzlPort is the core of the VpPuzzle system and supports the use of pzl-components. This class must be included to the project, which generates the main executable applicaion(.EXE).&lt;br /&gt;
&lt;br /&gt;
The calls of the predicates of the class PzlPort are possible only from the classes of the main executable applicaion project. Components placed to the dll-containers can not make a calls of the predicats of the class PzlPort.&lt;br /&gt;
&lt;br /&gt;
=Predicate summary=&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;1&amp;quot; style=&amp;quot;float:center;background:#F9F9F9;color:black;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|[[#core::classInfo/0|classInfo]].&lt;br /&gt;
get the information regarding the class&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::init/0|init:()]].&lt;br /&gt;
Initialization of the VpPuZzle system&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::setComponentRegisterFileName/1|setComponentRegisterFileName:(string ComponentRegisterFileName)]].&lt;br /&gt;
Set the name of the component registry file&lt;br /&gt;
|-&lt;br /&gt;
|[[#pzlPort::isInitialized/0|isInitialized:()  determ]].&lt;br /&gt;
Checking of the initialization status of the VpPuZzle system&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Additionally used resources=&lt;br /&gt;
The mandatory use:&lt;br /&gt;
*the pzlPort_XX.lib library must be used, where  XX - license qualifier. The library in fact contains the implementation of the class PzlPort and also the library includes the implementation of the handling of the pzl-container of the main executable application.&lt;br /&gt;
*the package pzlConfig must be used. The package PzlConfig participates in the initialization procedure.&lt;br /&gt;
&lt;br /&gt;
=Predicate details=&lt;br /&gt;
==core::classInfo/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;classInfo:().&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
get the information regarding the class&lt;br /&gt;
|full=&lt;br /&gt;
The predicate is the tipical predicate, which is generated when any class generated automatically by the IDE of the Visual Prolog System&lt;br /&gt;
}}&lt;br /&gt;
==pzlPort::init/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;init:().&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Initialization of the VpPuZzle system&lt;br /&gt;
|full=&lt;br /&gt;
The predicate must be the first predicate, which is used while the accessing to the Pzl-system. It must be called only once. After initialization the calls to other classes of the Pzl system become possible. &lt;br /&gt;
&lt;br /&gt;
The initialization of the pzl-system may be performed immidiately after the main applicaion start. It also may be performed at any moment when the pzl-system is expected to be used.&lt;br /&gt;
The deinitialization of the pzl-system is impossible.&lt;br /&gt;
&lt;br /&gt;
The repetitions of the initialization are ignored.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
  run():-&lt;br /&gt;
    pzlPort::init(),&lt;br /&gt;
    TaskWindow = taskWindow::new(),&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
==pzlPort::setComponentRegisterFileName/1==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;setComponentRegisterFileName:(string ComponentRegisterFileName).&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Set the name of the component registry file&lt;br /&gt;
|full=&lt;br /&gt;
The file ComponentRegisterFileName is one of the places, where the information regarding the pzl-components at the given computer is stored. The other place of store is the Windows registry. When the searching process runs the component registry file is used first. If the file doesn&amp;#039;t exist or the component is not registered there, then the search process continued in the Windows registry.&lt;br /&gt;
&lt;br /&gt;
The information in the component registry file is stored in the Visual Prolog term format (save-consult data format). Before the component may be used at the given computer, it must be registered at this computer. The application, which uses the pzl-technology may avoid the use of the component registry file or the application may assign the use of the file by using the predicate setComponentRegisterFileName/1. &lt;br /&gt;
&lt;br /&gt;
The parameter &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039; must be the full path to the file, or it may be relative to the directory, where the main application is placed.&lt;br /&gt;
The file extension is not strongly defined in the pzl-system. The recommended file extension is  - &amp;#039;&amp;#039;&amp;#039;.PZR&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The component registration file of the application may be assigned only once. While the repetitive assignment the existence of the previous assignment is not checked.&lt;br /&gt;
&lt;br /&gt;
The component registration data is load only once while the first access to an any pzl-component. Thus the second (and further) file assignment is not make influence on the component registration.&lt;br /&gt;
&lt;br /&gt;
The content validation of the file  &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039; while the predicate &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;setComponentRegisterFileName&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; performing is not used. But the file must exist.&lt;br /&gt;
&lt;br /&gt;
It is possible to get the name of the component registration file, assigned at the pzl-system by the use of the predicate call &amp;lt;vip&amp;gt;RegistryFileStatus=pzl::getComponentRegisterFileName()&amp;lt;/vip&amp;gt; The predicate pzl::getComponentRegisterFileName() returns the term, which reflects the status of the assignment of the file. If the file name has been assigned successfully, then the name will be returned as the parameter of the term.&lt;br /&gt;
|except=&lt;br /&gt;
If there is no file with the given name &amp;#039;&amp;#039;ComponentRegisterFileName&amp;#039;&amp;#039;, then the exception is genereated with the text&lt;br /&gt;
&amp;lt;vip&amp;gt;&amp;quot;The file &amp;lt;ComponentRegisterFileName&amp;gt; can not be found&amp;quot;&amp;lt;/vip&amp;gt;&lt;br /&gt;
While checking the file existence the system exception are possible and they are stored with the message &amp;lt;vip&amp;gt;&amp;quot;System Error while checking the file &amp;lt;ComponentRegisterFileName&amp;gt; existence&amp;quot;&amp;lt;/vip&amp;gt;&lt;br /&gt;
The error code will be stored also.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
class predicates&lt;br /&gt;
  runProfile:(window TaskWindow,string RegistryFileName,string InitialComponent).&lt;br /&gt;
clauses&lt;br /&gt;
  runProfile(_TaskWindow,RegistryFileName,_InitialComponent):-&lt;br /&gt;
    not(RegistryFileName=&amp;quot;&amp;quot;),&lt;br /&gt;
    trap(pzlPort::setComponentRegisterFileName(RegistryFileName),_Err,fail()),&lt;br /&gt;
    fail.&lt;br /&gt;
  runProfile(TaskWindow,RegistryFileName,InitialComponent):-&lt;br /&gt;
    RegistryFileStatus=pzl::getComponentRegisterFileName(),&lt;br /&gt;
    notifyUser(TaskWindow,RegistryFileStatus,RegistryFileName),&lt;br /&gt;
    runStudioStarter(TaskWindow,InitialComponent).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==pzlPort::isInitialized/0==&lt;br /&gt;
{{predicate&lt;br /&gt;
|decl=&lt;br /&gt;
&amp;lt;vip&amp;gt;isInitialized:()  determ.&amp;lt;/vip&amp;gt;&lt;br /&gt;
|short=&lt;br /&gt;
Checking of the initialization status of the VpPuZzle system&lt;br /&gt;
|full=&lt;br /&gt;
Makes it possible to check whether the initialization of the pzl-system was performed. If the result of the check is positive, then predicate succeeds.&lt;br /&gt;
|example=&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
  run():-&lt;br /&gt;
    not(isInitialazed()),&lt;br /&gt;
    pzlPort::init(),&lt;br /&gt;
    fail.&lt;br /&gt;
  run():-&lt;br /&gt;
    TaskWindow = taskWindow::new(),&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
=References=&lt;br /&gt;
[[Category:VpPuZzle]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=3rd:Main_Page&amp;diff=1888</id>
		<title>3rd:Main Page</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=3rd:Main_Page&amp;diff=1888"/>
		<updated>2009-05-11T06:52:34Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: spelling error&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The namespace &amp;#039;&amp;#039;&amp;#039;3rd&amp;#039;&amp;#039;&amp;#039; is dedicated to descriptions of 3rd party packages, add-on&amp;#039;s, applications, etc, which are available to fellow programmers.&lt;br /&gt;
&lt;br /&gt;
Articles are in the &amp;#039;&amp;#039;&amp;#039;3rd&amp;#039;&amp;#039;&amp;#039; namespace if the title has this format: &amp;#039;&amp;#039;&amp;#039;3rd:&amp;lt;title&amp;gt;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
It is advantageous, if you give your package/add-on/... a &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039;, and then use &amp;#039;&amp;#039;&amp;#039;3rd:&amp;lt;name&amp;gt;&amp;#039;&amp;#039;&amp;#039; as article title.  (The name can have several words).&lt;br /&gt;
&lt;br /&gt;
Remember to write:&lt;br /&gt;
* How to obtain the package/add-on/...&lt;br /&gt;
* Which license conditions that applies&lt;br /&gt;
* Whether the package/add-on/... is free or not&lt;br /&gt;
&lt;br /&gt;
Also check-out http://www.arsaniit.com/prolog-tools&lt;br /&gt;
&lt;br /&gt;
=== Contents ===&lt;br /&gt;
&lt;br /&gt;
* [[3rd:QDBM|QDBM]] a key-&amp;gt;value database (GNU license).&lt;br /&gt;
* [[3rd:Scintilla Editor Control|Scintilla Editor Control]] custom control wraps the scintilla editor.&lt;br /&gt;
* [[3rd:SCS - Simple Client Server|SCS - Simple Client Server]] a package for creating client server solutions.&lt;br /&gt;
* [[3rd:VPcURL|VPcURL]] provides Visual Prolog bindings to the cURL library, which is a library for HTTP and FTP.&lt;br /&gt;
* [[3rd:Tools &amp;amp; Support|Tools &amp;amp; Support]] is an article about useful non-Visual Prolog tools and support sites.&lt;br /&gt;
* [[3rd:RS232|RS232]] packages for communicating via the RS232-interface of a computer.&lt;br /&gt;
* [[3rd:Fast Articifial Neural Networks (FANN)|Fast Articifial Neural Networks (FANN)]] bindings to the FANN library.&lt;br /&gt;
* [[3rd:VIC - VIP7 IDE companion]] VIC provides 2 major tools, and a few little utilities, to help with the development of VIP7 projects.&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Talk:CGI_Applications_in_Visual_Prolog&amp;diff=1004</id>
		<title>Talk:CGI Applications in Visual Prolog</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Talk:CGI_Applications_in_Visual_Prolog&amp;diff=1004"/>
		<updated>2008-07-14T14:05:54Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: missing example program download link in the CGI tutorials&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Seems that the download link of the examples is missing (or at least I did not manage to locate it). &lt;br /&gt;
&lt;br /&gt;
I would be &amp;quot;good&amp;quot; if the downloads of the examples and tutorials would be presented in a &amp;quot;standardized&amp;quot; way. Either in the beginning or end, same place in all, and made better recognizable with some &amp;quot;good-looking&amp;quot; image.&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=3rd:QDBM&amp;diff=864</id>
		<title>3rd:QDBM</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=3rd:QDBM&amp;diff=864"/>
		<updated>2008-01-09T08:04:45Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: The article uses often the term I (writer) so the writers name is needed or the article has to be changed in a non personal &amp;quot;mode&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Written by Gildas Menier.&lt;br /&gt;
&lt;br /&gt;
For the management of key-&amp;gt;values in Visual Prolog, you may use factsDB, chainDB or some sql based solution. Each solution has benefits and drawbacks. For some reasons, I had to find another solution.&lt;br /&gt;
&lt;br /&gt;
Mikio Hirabayashi has built a very nice API called &amp;#039;QDBM&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
http://qdbm.sourceforge.net/ &lt;br /&gt;
&lt;br /&gt;
see also http://qdbm.sourceforge.net/benchmark.pdf &lt;br /&gt;
&lt;br /&gt;
QDBM is well known in the Unix world, but has been ported to Win32. Practically, it is a disk-based Hashtable + Binary tree + inverted list package. QDBM offers many solutions called &amp;#039;depot&amp;#039;, &amp;#039;curia&amp;#039;, &amp;#039;villa&amp;#039;, &amp;#039;odeum&amp;#039; and is well worth a look if you want to manage large amount of data. &lt;br /&gt;
&lt;br /&gt;
In the enclosed project, you&amp;#039;ll find a small &amp;#039;depot&amp;#039; wrapper for Visual Prolog. &lt;br /&gt;
&lt;br /&gt;
The example creates 1.000.000 (key, values) and then ask for 1.000.000 keys at random. All the key/values are stored on disk in a single file. &lt;br /&gt;
&lt;br /&gt;
Using a collection of instances of the visual prolog class &amp;#039;depot&amp;#039;, you can manage a very large hashtable on disk. &lt;br /&gt;
&lt;br /&gt;
I do not provide a full documentation since the real documentation can be found on the above web site. Only the port of depot is functional in this lib. &lt;br /&gt;
&lt;br /&gt;
I tried to simplify the use of this lib as much as possible, and tried not to get too far away from the original API syntax: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;NewDepot = depot::new(&amp;quot;dtest&amp;quot;), % create a depot db&lt;br /&gt;
NewDepot:dbopen(&amp;quot;create&amp;quot;), % open it (create mode)&lt;br /&gt;
NewDepot:dbput_string(Key, Value), % put a key,value&lt;br /&gt;
NewDepot:dbclose(), % close the file&amp;lt;/vip&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
and you can access the db as follows: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ReadDepot = depot::new(&amp;quot;dtest&amp;quot;), % &lt;br /&gt;
ReadDepot:dbopen(&amp;quot;read&amp;quot;), % open it in read mode&lt;br /&gt;
Value = ReadDepot:dbget_string(Key), % tries to find the value for a key, fails if no value&lt;br /&gt;
ReadDepot:dbclose(), %&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More predicates are provided. Please, have a look on the documentation of QDBM/depot and on the depot.pro file. &lt;br /&gt;
&lt;br /&gt;
The project doesn&amp;#039;t include a copy of qdbm (because of the allowed posting size); I encourage you to download the most up-to-date version on the site above. It is provided under the GNU Lesser GPL. &lt;br /&gt;
Just copy the three following files into your exe directory:&lt;br /&gt;
&lt;br /&gt;
* libiconv-2.dll &lt;br /&gt;
* mgwz.dll &lt;br /&gt;
* qdbm.dll &lt;br /&gt;
&lt;br /&gt;
you will find the package there: &lt;br /&gt;
&lt;br /&gt;
http://qdbm.sourceforge.net/win/&lt;br /&gt;
&lt;br /&gt;
Example project/download in [http://discuss.visual-prolog.com/viewtopic.php?t=6594 Discussion forum: (how to) deal with large amounts of (key,value)]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog&amp;diff=697</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=697"/>
		<updated>2007-11-21T06:18:38Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: /* Keywords */  bold&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalVisualPrologNavbar}}&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 Part 2 of the &amp;quot;Fundamental Prolog&amp;quot; Tutorial.&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 file. It is defined in some other file, 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.&amp;lt;img alt src=&amp;quot;../tut11/images/tut11_dot.gif&amp;quot; border=&amp;quot;0&amp;quot; width=&amp;quot;1&amp;quot; height=&amp;quot;1&amp;quot;&amp;gt;&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. It has to be written into the file called &amp;#039;&amp;#039;family1.pro&amp;#039;&amp;#039;. 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 family1&lt;br /&gt;
    open core&lt;br /&gt;
&lt;br /&gt;
constants&lt;br /&gt;
    className = &amp;quot;family1&amp;quot;.&lt;br /&gt;
        classVersion = &amp;quot;$JustDate: $$Revision: $&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    classInfo(className, classVersion).&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 anyflow.&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 anyflow.&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 family1&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
mainExe::run(family1::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;
[[Image:FundamentalVisualProlog1.jpg]]&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 UI Strategy 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.jpg]]&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 family1.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 family1.pro (given in this tutorial) into that window.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualProlog5.jpg]]&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 family1.pro window would now look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualProlog6.jpg]]&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.jpg]]&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.jpg]]&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:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog&amp;diff=695</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=695"/>
		<updated>2007-11-20T17:13:51Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: /* Keywords */  added indentions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalVisualPrologNavbar}}&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 Part 2 of the &amp;quot;Fundamental Prolog&amp;quot; Tutorial.&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;
implement and end implement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
open&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
constants&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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;vip&amp;gt;constants&lt;br /&gt;
pdc=&amp;quot;PDC Prolog&amp;quot;.&amp;lt;/vip&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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
domains&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class facts&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;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.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;This section defines the main entry point into a Visual Prolog program. A more detailed explanation is given below.&amp;lt;/blockquote&amp;gt;&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 file. It is defined in some other file, 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.&amp;lt;img alt src=&amp;quot;../tut11/images/tut11_dot.gif&amp;quot; border=&amp;quot;0&amp;quot; width=&amp;quot;1&amp;quot; height=&amp;quot;1&amp;quot;&amp;gt;&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. It has to be written into the file called &amp;#039;&amp;#039;family1.pro&amp;#039;&amp;#039;. 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 family1&lt;br /&gt;
    open core&lt;br /&gt;
&lt;br /&gt;
constants&lt;br /&gt;
    className = &amp;quot;family1&amp;quot;.&lt;br /&gt;
        classVersion = &amp;quot;$JustDate: $$Revision: $&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    classInfo(className, classVersion).&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 anyflow.&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 anyflow.&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 family1&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
mainExe::run(family1::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;
[[Image:FundamentalVisualProlog1.jpg]]&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 UI Strategy 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.jpg]]&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 family1.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 family1.pro (given in this tutorial) into that window.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualProlog5.jpg]]&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 family1.pro window would now look as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualProlog6.jpg]]&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.jpg]]&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.jpg]]&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:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Compound_and_List_Domains&amp;diff=454</id>
		<title>Compound and List Domains</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Compound_and_List_Domains&amp;diff=454"/>
		<updated>2007-10-04T07:17:31Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In this tutorial we will introduce compound domains (sometimes called algebraic data structures). Compound domains are used to handle several pieces of data as a single entity. Lists is an example of compound domains. Lists are used so much that they have even received special syntactical sugaring.&lt;br /&gt;
&lt;br /&gt;
Compound and list domains are created using built-in domains and other compound and list domains. Visual Prolog help describes built-in domains: integral, real, string, symbol, char, string8, pointer, binary, boolean, object.&lt;br /&gt;
&lt;br /&gt;
==Compound Domains and Functors==&lt;br /&gt;
&lt;br /&gt;
Compound domains allow you to treat several pieces of information as a single item in such a way that you can easily pick them apart again. Consider, for instance, the date &amp;quot;October 15, 2003&amp;quot;. It consists of three pieces of information – the month, day, and year – but it is useful to treat the whole thing as a single data with a treelike structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;        DATE&lt;br /&gt;
         /|\&lt;br /&gt;
        / | \&lt;br /&gt;
       /  |  \&lt;br /&gt;
      /   |   \&lt;br /&gt;
  October 15  2003&amp;lt;/pre&amp;gt;You can do this by declaring a domain containing the compound domain &amp;#039;&amp;#039;&amp;#039;date&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    date_cmp = date(string Month, unsigned Day, unsigned Year).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and then simply writing e.g.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;D = date(&amp;quot;October&amp;quot;, 15, 2003),&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This looks like a Prolog fact, but it isn&amp;#039;t here – it is just a value, which you can handle in much the same way as a string or number. It begins with a name, usually called a functor(in this case date), followed by three arguments.&lt;br /&gt;
&lt;br /&gt;
Note carefully that a functor in Visual Prolog has nothing to do with a function in other programming languages. &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;A functor does not stand for some computation to be performed.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; it is just a name that identifies a kind of compound value and holds its arguments together.&lt;br /&gt;
&lt;br /&gt;
The arguments of a compound value can themselves be compound. For instance, you might think of someone&amp;#039;s birthday as an information structure like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;            birthday&lt;br /&gt;
             /    \&lt;br /&gt;
            /      \&lt;br /&gt;
           /        \&lt;br /&gt;
          /          \&lt;br /&gt;
         /            \&lt;br /&gt;
   person             date&lt;br /&gt;
    /  \              / | \&lt;br /&gt;
   /    \            /  |  \&lt;br /&gt;
&amp;quot;Per&amp;quot; &amp;quot;Schultze&amp;quot;  &amp;quot;Apr&amp;quot; 14 1960&amp;lt;/pre&amp;gt;In Prolog you would write this as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;birthday(person(&amp;quot;Per&amp;quot;, &amp;quot;Schultze&amp;quot;), date(&amp;quot;Apr&amp;quot;,14,1960)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, there are two sub-parts in the compound birthday value: the argument  person(&amp;quot;Per&amp;quot;, &amp;quot;Schultze&amp;quot;) and the argument date(&amp;quot;Apr&amp;quot;, 14, 1960). The functors of these values are  person and date.&lt;br /&gt;
&lt;br /&gt;
==Unification of Compound Domains==&lt;br /&gt;
&lt;br /&gt;
A compound value can unify either with a simple variable or with a compound value that matches it(perhaps containing variables as parts of its internal structure). This means you can use a compound value to pass the whole collection of items as a single value, and then use unification to pick them apart. For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;date(&amp;quot;April&amp;quot;, 14, 1960)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
matches X and binds X to date(&amp;quot;April&amp;quot;,14,1960). Also&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;date(&amp;quot;April&amp;quot;, 14, 1960)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
matches date(Mo,Da,Yr) and binds Mo to &amp;quot;April&amp;quot;, Da to 14, and Yr to 1960.&lt;br /&gt;
&lt;br /&gt;
===Using the Equal Sign to Unify Compound Domains===&lt;br /&gt;
&lt;br /&gt;
Visual Prolog performs unification in two places. The first is when a call or goal matches the head of a clause. The second is across the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;equal&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;(=) sign, which is actually an infix predicate(a predicate that is located &amp;#039;&amp;#039;between&amp;#039;&amp;#039; its arguments rather than &amp;#039;&amp;#039;before&amp;#039;&amp;#039; them).&lt;br /&gt;
&lt;br /&gt;
Visual Prolog will make the necessary bindings to unify the values on both sides of the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;equal&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; sign. This is useful for finding the values of arguments within a compound value. For example, the following code excerpt tests if two people have the same last name, then gives the second person the same address as the first.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    domains&lt;br /&gt;
        person = person(name, address).&lt;br /&gt;
        name = name(string First, string Last).&lt;br /&gt;
        address = addr(street, string City, string State).&lt;br /&gt;
        street = street(integer Number, string Street_name).&lt;br /&gt;
&lt;br /&gt;
    predicates&lt;br /&gt;
        run :().&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        run():-&lt;br /&gt;
            console::init(),&lt;br /&gt;
            P1 =  person(name(&amp;quot;Jim&amp;quot;, &amp;quot;Smith&amp;quot;),&lt;br /&gt;
                addr(street(5, &amp;quot;1st st&amp;quot;), &amp;quot;igo&amp;quot;, &amp;quot;CA&amp;quot;)),&lt;br /&gt;
            P1 = person(name(_, &amp;quot;Smith&amp;quot;), Address),&lt;br /&gt;
            P2 = person(name(&amp;quot;Jane&amp;quot;, &amp;quot;Smith&amp;quot;), Address),&lt;br /&gt;
            !,&lt;br /&gt;
            stdio::write(&amp;quot;P1 = &amp;quot;, P1, &amp;quot;\n&amp;quot;),&lt;br /&gt;
            stdio::write(&amp;quot;P2 = &amp;quot;, P2, &amp;quot;\n&amp;quot;)&lt;br /&gt;
            ;&lt;br /&gt;
            stdio::write(&amp;quot;No solution&amp;quot;).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    my::run.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Treating Several Items as One==&lt;br /&gt;
&lt;br /&gt;
Compound values can be regarded and treated as single values in your Prolog clauses, which greatly simplifies programming. Consider, for example, the fact&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, book(&amp;quot;From Here to Eternity&amp;quot;, &amp;quot;James Jones&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in which you state that John owns the book &amp;quot;From Here to Eternity&amp;quot;, written by &amp;quot;James Jones&amp;quot;. Likewise, you could write&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, horse(&amp;quot;Blacky&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which can be interpreted as&lt;br /&gt;
&lt;br /&gt;
John owns a horse named Blacky.&lt;br /&gt;
&lt;br /&gt;
The compound values in these two examples are&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;book(&amp;quot;From Here to Eternity&amp;quot;, &amp;quot;James Jones&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;horse(&amp;quot;Blacky&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you had instead written two facts:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, &amp;quot;From Here to Eternity&amp;quot;).&lt;br /&gt;
owns(&amp;quot;John&amp;quot;, &amp;quot;Blacky&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you would not have been able to decide whether Blacky was the title of a book or the name of a horse. On the other hand, you can use the first component of a compound value – the functor – to distinguish between different kinds of values. This example used the functors book and horse to indicate the difference between the values.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Remember&amp;#039;&amp;#039;&amp;#039;: Compound values consist of a functor and the arguments belonging to that functor, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;functor(argument1, argument2, ..., argumentN)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===An Example Using Compound Domains===&lt;br /&gt;
&lt;br /&gt;
An important feature of compound domains allows you to easily pass a group of values as one argument. Consider a case where you are keeping a telephone database. In your database, you want to include your friends&amp;#039; and family members&amp;#039; birthdays. Here is a section of code you might have come up with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    phone_list :(string First, string Last, string Phone, string Month, intege Day, integer Year) determ.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    phone_list(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;, &amp;quot;422-0208&amp;quot;, &amp;quot;aug&amp;quot;, 3, 1955).&lt;br /&gt;
    phone_list(&amp;quot;Chris&amp;quot;, &amp;quot;Grahm&amp;quot;, &amp;quot;433-9906&amp;quot;, &amp;quot;may&amp;quot;, 12, 1962).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Examine the data, noticing the six arguments in the fact phone_list; five of these arguments can be broken down into two compound domains, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;       person                birthday&lt;br /&gt;
        /   \                /  |  \&lt;br /&gt;
       /     \              /   |   \&lt;br /&gt;
First Name  Last Name    Month Day Year&amp;lt;/pre&amp;gt;It might be more useful to represent your facts so that they reflect these compound domains. Going back a step, you can see that person is a relationship, and the first and last names are the arguments. Also, birthday is a relationship with three arguments: month, day, and year. The Prolog representation of these relationships is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, &amp;quot;From Here to Eternity&amp;quot;).&lt;br /&gt;
owns(&amp;quot;John&amp;quot;, &amp;quot;Blacky&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can now rewrite your small database to include these compound domains as part of your database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    name = person(string First, string Last).&lt;br /&gt;
    birthday = b_date(string Month, integer Day, integer Year).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    phone_list :(name, string Ph_num, birthday) determ.&lt;br /&gt;
clauses&lt;br /&gt;
    phone_list(person(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;), &amp;quot;422-0208&amp;quot;,&lt;br /&gt;
          b_date(&amp;quot;aug&amp;quot;, 3, 1955)).&lt;br /&gt;
    phone_list(person(&amp;quot;Chris&amp;quot;, &amp;quot;Grahm&amp;quot;), &amp;quot;433-9906&amp;quot;,&lt;br /&gt;
          b_date(&amp;quot;may&amp;quot;, 12, 1962)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this program, two compound &amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039; declarations were introduced. We go into more detail about these compound data structures later in this chapter. For now, we will concentrate on the benefits of using such compound domains.&lt;br /&gt;
&lt;br /&gt;
The phone_list predicate now contains three arguments, as opposed to the previous six. Sometimes breaking up your data into compound values will clarify your program and might help process the data.&lt;br /&gt;
&lt;br /&gt;
Now add some rules to program. Suppose you want to create a list of people whose birthdays are in the current month. Here is the program code to accomplish this task; this program uses the standard predicate date to get the current date from the computer&amp;#039;s internal clock. Predicate date will return the current year, month, and day from your computer&amp;#039;s clock.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    domains&lt;br /&gt;
        name = person(string First, string Last).&lt;br /&gt;
        birthday = b_date(string Month, integer Day, integer Year).&lt;br /&gt;
    predicates&lt;br /&gt;
        phone_list :(name, string Ph_num, birthday) multi(o,o,o).&lt;br /&gt;
        get_months_birthdays :().&lt;br /&gt;
        convert_month :(string Name, integer Num) determ(i,o).&lt;br /&gt;
        check_birthday_month :(integer Month_num, birthday) determ.&lt;br /&gt;
        write_person :(name).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        get_months_birthdays() :-&lt;br /&gt;
            stdio::write(&amp;quot;****** This Month&amp;#039;s Birthday List *****\n&amp;quot;),&lt;br /&gt;
            stdio::write(&amp;quot; First Name\t\t Last Name\n&amp;quot;),&lt;br /&gt;
            stdio::write(&amp;quot;***********************************\n&amp;quot;),&lt;br /&gt;
            CurTime = time::new(),&lt;br /&gt;
            CurTime:getDate(_, ThisMonth, _),&lt;br /&gt;
            phone_list(Person, _, Date),&lt;br /&gt;
            check_birthday_month(ThisMonth, Date),&lt;br /&gt;
            write_person(Person),&lt;br /&gt;
            fail.&lt;br /&gt;
        get_months_birthdays() :-&lt;br /&gt;
            stdio::write(&amp;quot;\n Press any key to continue:\n&amp;quot;),&lt;br /&gt;
            _ = console::readChar().&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        write_person(person(FirstName, LastName)) :-&lt;br /&gt;
            stdio::write(&amp;quot;  &amp;quot;, FirstName, &amp;quot;\t\t &amp;quot;,  LastName, &amp;quot;\n&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        check_birthday_month(Mon, b_date(Month, _, _)) :-&lt;br /&gt;
            convert_month(Month, Month1),&lt;br /&gt;
            Mon = Month1.&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        phone_list(person(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;), &amp;quot;11-1111&amp;quot;, b_date(&amp;quot;Jan&amp;quot;, 3, 1955)).&lt;br /&gt;
        phone_list(person(&amp;quot;Benjamin&amp;quot;, &amp;quot;Thomas&amp;quot;), &amp;quot;222-2222&amp;quot;, b_date(&amp;quot;Feb&amp;quot;, 5, 1965)).&lt;br /&gt;
        phone_list(person(&amp;quot;Ray&amp;quot;, &amp;quot;William&amp;quot;), &amp;quot;333-3333&amp;quot;, b_date(&amp;quot;Mar&amp;quot;, 3, 1955)).&lt;br /&gt;
        phone_list(person(&amp;quot;Tomas&amp;quot;, &amp;quot;Alfred&amp;quot;), &amp;quot;444-4444&amp;quot;, b_date(&amp;quot;Apr&amp;quot;, 29, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Chris&amp;quot;, &amp;quot;Gralm&amp;quot;), &amp;quot;555-5555&amp;quot;, b_date(&amp;quot;May&amp;quot;, 12, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Dastin&amp;quot;, &amp;quot;Robert&amp;quot;), &amp;quot;666-6666&amp;quot;, b_date(&amp;quot;Jun&amp;quot;, 17, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Anna&amp;quot;, &amp;quot;Friend&amp;quot;), &amp;quot;777-7777&amp;quot;, b_date(&amp;quot;Jul&amp;quot;, 2, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Naomi&amp;quot;, &amp;quot;Friend&amp;quot;), &amp;quot;888-8888&amp;quot;, b_date(&amp;quot;Aug&amp;quot;, 10, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Christina&amp;quot;, &amp;quot;Lynn&amp;quot;), &amp;quot;999-9999&amp;quot;, b_date(&amp;quot;Sep&amp;quot;, 25, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Kathy&amp;quot;, &amp;quot;Ann&amp;quot;), &amp;quot;110-1010&amp;quot;, b_date(&amp;quot;Oct&amp;quot;, 20, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Elizabeth&amp;quot;, &amp;quot;Ann&amp;quot;), &amp;quot;110-1111&amp;quot;, b_date(&amp;quot;Nov&amp;quot;, 9, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Aaron&amp;quot;, &amp;quot;Friend&amp;quot;), &amp;quot;110-1212&amp;quot;, b_date(&amp;quot;Dec&amp;quot;, 31, 1975)).&lt;br /&gt;
        phone_list(person(&amp;quot;Jenifer&amp;quot;, &amp;quot;Faitlin&amp;quot;), &amp;quot;888-8888&amp;quot;, b_date(&amp;quot;Aug&amp;quot;, 14, 1975)).&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        convert_month(&amp;quot;Jan&amp;quot;, 1).&lt;br /&gt;
        convert_month(&amp;quot;Feb&amp;quot;, 2).&lt;br /&gt;
        convert_month(&amp;quot;Mar&amp;quot;, 3).&lt;br /&gt;
        convert_month(&amp;quot;Apr&amp;quot;, 4).&lt;br /&gt;
        convert_month(&amp;quot;May&amp;quot;, 5).&lt;br /&gt;
        convert_month(&amp;quot;Jun&amp;quot;, 6).&lt;br /&gt;
        convert_month(&amp;quot;Jul&amp;quot;, 7).&lt;br /&gt;
        convert_month(&amp;quot;Aug&amp;quot;, 8).&lt;br /&gt;
        convert_month(&amp;quot;Sep&amp;quot;, 9).&lt;br /&gt;
        convert_month(&amp;quot;Oct&amp;quot;, 10).&lt;br /&gt;
        convert_month(&amp;quot;Nov&amp;quot;, 11).&lt;br /&gt;
        convert_month(&amp;quot;Dec&amp;quot;, 12).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::get_months_birthdays().&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
How do compound domains help in this program? This should be easy to see when you examine the code. Most of the processing goes on in the get_months_birthdays predicate.&lt;br /&gt;
&lt;br /&gt;
*First, the program makes a window to display the results.&lt;br /&gt;
&lt;br /&gt;
*After this, it writes a header in the window to help interpret the results.&lt;br /&gt;
&lt;br /&gt;
*Next, in get_months_birthdays, the program uses the built-in predicate date to obtain the current month.&lt;br /&gt;
&lt;br /&gt;
*After this, the program is all set to search the database and list the people who were born in the current month. The first thing to do is find the first person in the database. The call phone_list(Person, _, Date) binds the person&amp;#039;s first and last names to the variable Person by binding the entire functor person to Person. It also binds the person&amp;#039;s birthday to the variable Date.&amp;lt;br /&amp;gt;&lt;br /&gt;
Notice that you only need to use one variable to store a person&amp;#039;s complete name, and one variable to hold the birthday. This is the power of using compound domains.&lt;br /&gt;
&lt;br /&gt;
*Your program can now pass around a person&amp;#039;s birthday simply by passing on the variable Date. This happens in the next subgoal, where the program passes the current month(represented by an integer) and the birthday(of the person it is processing) to the predicate check_birthday_month.&lt;br /&gt;
&lt;br /&gt;
*Look closely at what happens. Visual Prolog calls the predicate check_birthday_month with two variables: The first variable is bound to an integer, and the second is bound to a birthday term. In the head of the rule that defines check_birthday_month, the first argument, This_month, is matched with the variable Mon. The second argument, Date, is matched against b_date(Month, _,_).&amp;lt;br /&amp;gt;&lt;br /&gt;
Since all you are concerned with is the month of a person&amp;#039;s birthday, you have used the anonymous variable for both the day and the year of birth.&lt;br /&gt;
&lt;br /&gt;
*The predicate check_birthday_month first converts the string for the month into an integer value. Once this is done, Visual Prolog can compare the value of the current month with the value of the person&amp;#039;s birthday month. If this comparison succeeds, then the subgoal check_birthday_month succeeds, and processing can continue. If the comparison fails(the person currently being processed was not born in the current month), Visual Prolog begins to backtrack to look for another solution to the problem.&lt;br /&gt;
&lt;br /&gt;
*The next subgoal to process is write_person. The person currently being processed has a birthday this month, so it is OK to print that person&amp;#039;s name in the report. After printing the information, the clause fails, which forces backtracking.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;Backtracking always goes up to the most recent non-deterministic call and tries to re-satisfy that call.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; In this program, the last non-deterministic call processed is the call to phone_list. It is here that the program looks up another person to be processed. If there are no more people in the database to process, the current clause fails; Visual Prolog then attempts to satisfy this call by looking further down in the database. Since there is another clause that defines get_months_birthdays, Visual Prolog tries to satisfy the call to get_months_birthdays by satisfying the subgoals to this other clause.&lt;br /&gt;
&lt;br /&gt;
==Declaring Compound Domains==&lt;br /&gt;
&lt;br /&gt;
In this section, we show you how instances of compound domains are defined. After compiling a program that contains the following relationships:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, book(&amp;quot;From Here to Eternity&amp;quot;, &amp;quot;James Jones&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, horse(&amp;quot;Blacky&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you could query the system with this goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns(&amp;quot;John&amp;quot;, X))&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The variable X can be bound to a different type: a book, a horse, or perhaps other types you define. Because of your definition of the owns predicate, you can no longer employ the old predicate declaration of owns:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns :(string, string).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second argument no longer refers to the string domain. Instead, you must formulate a new declaration to the predicate, such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;owns :(name, articles).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can describe the articles domain in the domains section as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    articles = book(string Title, string Author);&lt;br /&gt;
            horse(string Name).&lt;br /&gt;
        /* Articles are books or horses */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The semicolon is read as &amp;#039;&amp;#039;or&amp;#039;&amp;#039;. In this case, two alternatives are possible: A book can be identified by its title and author, or a horse can be identified by its name. The domains title, author, and name are all of the standard domain string.&lt;br /&gt;
&lt;br /&gt;
More alternatives can easily be added to the domains declaration. For example, articles could also include a boat, a house, or a bankbook. For a boat, you can make do with a functor that has no arguments attached to it. On the other hand, you might want to give a bank balance as a figure within the bankbook. The domains declaration of  articles is therefore extended to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    articles = book(string Title, string Author);&lt;br /&gt;
            horse(string Name); boat; bankbook(real Balance).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is a full program that shows how compound domains from the domain articles can be used in facts that define the predicate owns.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    domains&lt;br /&gt;
        articles =&lt;br /&gt;
            book(string Title, string Author) ;&lt;br /&gt;
            horse(string Name) ; boat ; bankbook(real Balance).&lt;br /&gt;
    predicates&lt;br /&gt;
        owns :(string Name, articles) nondeterm(i,o) determ(i,i).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        owns(&amp;quot;John&amp;quot;, book(&amp;quot;A friend of the family&amp;quot;, &amp;quot;Irwin Shaw&amp;quot;)).&lt;br /&gt;
        owns(&amp;quot;John&amp;quot;, horse(&amp;quot;Blacky&amp;quot;)).&lt;br /&gt;
        owns(&amp;quot;John&amp;quot;, boat).&lt;br /&gt;
        owns(&amp;quot;John&amp;quot;, bankbook(1000)).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::owns(&amp;quot;John&amp;quot;, Thing),&lt;br /&gt;
    stdio::write(&amp;quot;Thing: &amp;quot;, Thing, &amp;quot;\n&amp;quot;),&lt;br /&gt;
    fail&lt;br /&gt;
    ;&lt;br /&gt;
    stdio::write(&amp;quot;The end.&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now load the program into Visual Development Environment and run it in window. Visual Prolog responds with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Thing: book(&amp;quot;A friend of the family&amp;quot;,&amp;quot;Irwin Shaw&amp;quot;)&lt;br /&gt;
Thing: horse(&amp;quot;Blacky&amp;quot;)&lt;br /&gt;
Thing: boat()&lt;br /&gt;
Thing: bankbook(1000)&lt;br /&gt;
The end.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Writing Domain Declarations: a Summary===&lt;br /&gt;
&lt;br /&gt;
This is a generic representation of how to write compound domain declarations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domain = alternative1(D, D, ...);&lt;br /&gt;
               alternative2(D, D, ...);&lt;br /&gt;
...&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, alternative1 and  alternative2 are arbitrary(but different) functors. The notation(D, D, ...) represents a list of domain names that are either declared elsewhere or are one of the standard domain types(such as string, integer, real, etc).&lt;br /&gt;
&lt;br /&gt;
====&amp;#039;&amp;#039;&amp;#039;Note&amp;#039;&amp;#039;&amp;#039;:====&lt;br /&gt;
&lt;br /&gt;
*The alternatives are separated by semicolons.&lt;br /&gt;
&lt;br /&gt;
*Every alternative consists of a functor and, possibly, a list of domains for the corresponding arguments.&lt;br /&gt;
&lt;br /&gt;
*If the functor has no arguments, you can write it as alternativeN or alternativeN( ) in your programs.&lt;br /&gt;
&lt;br /&gt;
===Multi-Level Compound Domains===&lt;br /&gt;
&lt;br /&gt;
Visual Prolog allows you to construct compound domains on several levels. For example, in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;book(&amp;quot;The Ugly Duckling&amp;quot;, &amp;quot;Andersen&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
instead of using the author&amp;#039;s last name, you could use a new structure that describes the author in more detail, including both the author&amp;#039;s first and last names. By calling the functor for the resulting new compound domain author, you can change the description of the book to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;book(&amp;quot;The Ugly Duckling&amp;quot;, author(&amp;quot;Hans Christian&amp;quot;, &amp;quot;Andersen&amp;quot;)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the old domain declaration&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;book(string Title, string Author).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the second argument of the book functor is Author that can only include a single name, so it is no longer sufficient. You must now specify that an author is also a compound domain made up of the author&amp;#039;s first and last name. You do this with the domain definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;author = author(string First_name, string Last_name).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which leads to the following declarations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    articles = book(string Title, string Author); ...&lt;br /&gt;
        /* First level */&lt;br /&gt;
    author = author(string First_name, string Last_name).&lt;br /&gt;
        /* Second level */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When using compound domains on different levels in this way, it is often helpful to draw a &amp;quot;tree&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;       book&lt;br /&gt;
       /  \&lt;br /&gt;
      /    \&lt;br /&gt;
  title   author&lt;br /&gt;
           /   \&lt;br /&gt;
          /     \&lt;br /&gt;
   First_name  Last_name&amp;lt;/pre&amp;gt;A domain declaration describes only one level of the tree at a time, and not the whole tree. For instance, a book can&amp;#039;t be defined with the following domain declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* Not allowed */&lt;br /&gt;
book = book(string Title, author(string First_name, string Last_name)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compound Mixed-Domain Declarations==&lt;br /&gt;
&lt;br /&gt;
In this section, we discuss three different types of domain declarations you can add to your programs. These declarations allow you to use predicates that&lt;br /&gt;
&lt;br /&gt;
*take an argument, more than one type of more than one possible type&lt;br /&gt;
&lt;br /&gt;
*take a variable number of arguments, each of a specified type&lt;br /&gt;
&lt;br /&gt;
*take a variable number of arguments, some of which might be of more than one possible type&lt;br /&gt;
&lt;br /&gt;
===Multiple-Type Arguments===&lt;br /&gt;
&lt;br /&gt;
To allow a Visual Prolog predicate to accept an argument that gives information of different types, you must add a functor declaration. In the following example, the your_age clause will accept an argument of type age, which can be a string, a real, or an integer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    age = i(integer); r(real); s(string).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    your_age :(age).&lt;br /&gt;
clauses&lt;br /&gt;
    your_age(i(Age)) :- stdio::write(Age).&lt;br /&gt;
    your_age(r(Age)) :- stdio::write(Age).&lt;br /&gt;
    your_age(s(Age)) :- stdio::write(Age).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog does not allow the following domain declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* Not permitted. */&lt;br /&gt;
domains&lt;br /&gt;
    age = integer; real; string.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Lists===&lt;br /&gt;
&lt;br /&gt;
Suppose you are keeping track of the different classes a professor might teach. You might produce the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    teacher :(string First_name, string Last_name, string Class) determ.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    teacher(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;, &amp;quot;english1&amp;quot;).&lt;br /&gt;
    teacher(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;, &amp;quot;math1&amp;quot;).&lt;br /&gt;
    teacher(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;, &amp;quot;history1&amp;quot;).&lt;br /&gt;
    teacher(&amp;quot;Mary&amp;quot;, &amp;quot;Maker&amp;quot;, &amp;quot;history2&amp;quot;).&lt;br /&gt;
    teacher(&amp;quot;Mary&amp;quot;, &amp;quot;Maker&amp;quot;, &amp;quot;math2&amp;quot;).&lt;br /&gt;
    teacher(&amp;quot;Chris&amp;quot;, &amp;quot;Grahm&amp;quot;, &amp;quot;geometry&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, you need to repeat the teacher&amp;#039;s name for each class he or she teaches. For each class, you need to add another fact to the database. Although this is perfectly OK in this situation, you might find a school where there are hundreds of classes; this type of data structure would get a little tedious. Here, it would be helpful if you could create an argument to a predicate that could take on one &amp;#039;&amp;#039;or more&amp;#039;&amp;#039; values.&lt;br /&gt;
&lt;br /&gt;
A list in Prolog does just that. In the following code, the argument class is declared to be of a list type. We show here how a list is represented in Prolog.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    classes = string*.   /* declare a list domain */&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    teacher :(string First_name,  string Last_name,  classes Classes) determ.&lt;br /&gt;
clauses&lt;br /&gt;
    teacher(&amp;quot;Ed&amp;quot;, &amp;quot;Willis&amp;quot;, [&amp;quot;english1&amp;quot;, &amp;quot;math1&amp;quot;, &amp;quot;history1&amp;quot;]).&lt;br /&gt;
    teacher(&amp;quot;Mary&amp;quot;, &amp;quot;Maker&amp;quot;, [&amp;quot;history2&amp;quot;, &amp;quot;math2&amp;quot;]).&lt;br /&gt;
    teacher(&amp;quot;Chris&amp;quot;, &amp;quot;Grahm&amp;quot;, [&amp;quot;geometry&amp;quot;]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the code is more concise and easier to read than in the preceding one. Notice the domains declaration. The asterisk(*) means that classes is a list of strings. You can just as easily declare a list of integers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    integer_list = integer*.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you declare a domain, it is easy to use it; just place it as an argument to a predicate declared in the predicates section. Here is an example of using an integer list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    integer_list = integer*.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    test_scores :&lt;br /&gt;
        (string First_name,&lt;br /&gt;
          string Last_name,&lt;br /&gt;
          integer_list Test_Scores)&lt;br /&gt;
          determ.&lt;br /&gt;
clauses&lt;br /&gt;
    test_scores(&amp;quot;Lisa&amp;quot;, &amp;quot;Lavender&amp;quot;, [86, 91, 75]).&lt;br /&gt;
    test_scores(&amp;quot;Libby&amp;quot;, &amp;quot;Dazzner&amp;quot;, [79, 75]).&lt;br /&gt;
    test_scores(&amp;quot;Jeff&amp;quot;, &amp;quot;Zheutlin&amp;quot;, []).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the case of &amp;#039;&amp;#039;Jeff Zheutlin&amp;#039;&amp;#039;, notice that a list doesn&amp;#039;t need to contain any elements at all.&lt;br /&gt;
&lt;br /&gt;
One more example shows how one can use lists to describe the family-tree.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    tree_list = tree*.&lt;br /&gt;
    tree = tree(string Text, tree_list TreeList).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    family :(tree) determ.&lt;br /&gt;
clauses&lt;br /&gt;
    family(tree(&amp;quot;Grandmother&amp;quot;,&lt;br /&gt;
            [tree(&amp;quot;John&amp;quot;,&lt;br /&gt;
                [tree(&amp;quot;Eric&amp;quot;, []),&lt;br /&gt;
                 tree(&amp;quot;Mark&amp;quot;, []),&lt;br /&gt;
                 tree(&amp;quot;Leonard&amp;quot;, []) ] ),&lt;br /&gt;
             tree(&amp;quot;Ellen&amp;quot;, [])&lt;br /&gt;
            ]&lt;br /&gt;
        )).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
These are the important points covered in this tutorial:&lt;br /&gt;
&lt;br /&gt;
*A Visual Prolog program can contain many types of values: simple and compound, standard and user-defined.&lt;br /&gt;
&lt;br /&gt;
*Simple values are numbers, strings, etc.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;Compound domains&amp;#039;&amp;#039; allow you to treat several pieces of information as a single item. A compound domain consists of a name(known as a functor) and one or more arguments. You can define a domain with several alternative functors.&lt;br /&gt;
&lt;br /&gt;
*A functor in Visual Prolog is not the same thing as a function in other programming languages. &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;A functor does not stand for some computation to be performed.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; It is just a name that identifies a kind of a compound domain and holds its arguments together.&lt;br /&gt;
&lt;br /&gt;
*Compound values can be regarded and treated as single values; you use the functor to distinguish between different kinds of compound values. Visual Prolog allows you to construct compound values on several levels; the arguments of a compound value can also be compound values. With compound mixed domain declarations, you can use predicates that:&lt;br /&gt;
&lt;br /&gt;
*take an argument of more than one possible type(functor declaration).&lt;br /&gt;
&lt;br /&gt;
*take a variable number of arguments, each of a specified type(list declaration).&lt;br /&gt;
&lt;br /&gt;
*take a variable number of arguments, some of which might be of more than one possible type.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=CGI_Applications&amp;diff=451</id>
		<title>CGI Applications</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=CGI_Applications&amp;diff=451"/>
		<updated>2007-10-03T09:40:36Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{CGI Applications Navbar}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;CGI&amp;#039;&amp;#039;&amp;#039; stands for [[wikipedia:Common Gateway Interface|&amp;#039;&amp;#039;&amp;#039;C&amp;#039;&amp;#039;&amp;#039;ommon &amp;#039;&amp;#039;&amp;#039;G&amp;#039;&amp;#039;&amp;#039;ateway &amp;#039;&amp;#039;&amp;#039;I&amp;#039;&amp;#039;&amp;#039;nterface]]. It is a specification recommended by the World Wide Web Consortium for one kind of information streaming in and out of web servers.&lt;br /&gt;
&lt;br /&gt;
As you maybe know, HTML browsers (such as Internet Explorer, Netscape etc.) send and receive information from web servers. But the experience of the world wide web is not a simple act of just reading some hyper-linked HTML files. There is interaction. Data moves both in and out of a browser, including data filled into forms in the browser by the user. There are many things the web server cannot handle on its own. How does it then handle such interactive data?&lt;br /&gt;
&lt;br /&gt;
A web server is a primitive creature actually. It usually serves out HTML files; and other files of mime-types that it recognizes, back to the browsers. It usually cannot do advanced stuff, like accessing a database in the background and sending database queries back to the browsers. CGI was invented to increase the functionality of web servers. It allows web servers to talk to CGI applications residing on the same machine as the web server itself. CGI applications do the job that the web server by itself is unable to do. For example; accessing databases in the background or conducting sophisticated searches. Web servers that use CGI, are usually able to send more information to the browsers than those that do not use CGI.&lt;br /&gt;
&lt;br /&gt;
===What is the significance of a &amp;#039;common gateway?&amp;#039;===&lt;br /&gt;
&lt;br /&gt;
The word &amp;#039;common&amp;#039; signifies that the convention is accepted by all web servers. The word &amp;#039;gateway&amp;#039; represents the fact that data is obtained, as if by stepping through a &amp;#039;door&amp;#039; or a gateway into other processes that lay on the other side. Metaphorically, a web server stands on one side of the gateway and a CGI application stand on the other side and both of them &amp;#039;&amp;#039;talk&amp;#039;&amp;#039; to each other using CGI.&lt;br /&gt;
&lt;br /&gt;
===What is streaming?===&lt;br /&gt;
&lt;br /&gt;
By the term streaming, we mean that the information is processed one character at a time in a long queue. Think of a pipe whose diameter allows only one character to pass through at any instance. The characters passing through the pipe will emerge at the other pipe end, only one character at a time, in the form of a stream.&lt;br /&gt;
&lt;br /&gt;
Just like in a pipe, where water cannot go streaming in both directions; a stream in the programming world cannot be both readable and writable simultaneously. Either characters come out from the stream (which means the stream is a readable stream), or characters are placed into the stream (a writable stream) but the same stream cannot be used for both purposes by a single program.&lt;br /&gt;
&lt;br /&gt;
In the case of CGI applications, the characters are in &amp;#039;&amp;#039;&amp;#039;ANSI&amp;#039;&amp;#039;&amp;#039; format, and so they are 8-bit characters. This point has to be noted later on, as Visual Prolog by default uses &amp;#039;&amp;#039;&amp;#039;UNICODE&amp;#039;&amp;#039;&amp;#039; characters, which are 16-bit. There are predicates available in Visual Prolog that collect streamed ANSI characters together, so processing streams would not be a problem. Eventually, it must be pointed out, our experience of the world wide web happens because of the streaming characters arriving through the Internet pipeline into our browsers one character at a time. We just don&amp;#039;t notice it though, because once the individual characters reach the browser, they are quickly assembled together.&lt;br /&gt;
&lt;br /&gt;
===What is a mime-type?===&lt;br /&gt;
&lt;br /&gt;
Think of mime-type as a file extension. It tells the web server the kind of files it and its clients (i.e., browsers) can handle. Most web servers determine the mime-type by the file extensions it sees. When a browser interacts with a web server, it initially informs the web server about the mime-type handling capabilities the browser has, along with the resource (file of a particular mime-type, etc.) it wants the web server to give. The web server sends over the requisite file to the browser for the browser to display. Examples are: .html, .jpg, .gif, etc. Non-recognized mime-types are either not sent to the browser by the web server, or are sent as plain ASCII or as HTML (the actual behavior depends on the web server).&lt;br /&gt;
&lt;br /&gt;
As the information is sent as a stream to the browser, the web server embeds some headers at the start of the stream, containing the mime-type. See Illustration 1 for a typical example of what gets sent across from a web server to a browser. Once these files with mime-types reach the browser, it analyzes the headers, deduces the mime-type the data represents, and as per its own internal table of mime-types it can handle, it displays the data accordingly.&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog06.jpg|frame|Illustration 1]]&lt;br /&gt;
&lt;br /&gt;
===What is a header?===&lt;br /&gt;
&lt;br /&gt;
A header, in the context of this tutorial, is a line of text followed by a carriage return embedded in the stream before the actual data begins. In Illustration 1, the lines you see above the data, are the headers that were passed by the web server to the browser. There is a blank line separating the headers from the actual data.&lt;br /&gt;
&lt;br /&gt;
Notice the &amp;#039;Content-type:...&amp;#039; header in the illustration. That line indicates the mime-type of the data. This particular line is specifically relevant to CGI applications, as when a web server calls on the services of a CGI application to provide data to be passed on to a browser, it is clueless on the mime-type of the data that the CGI application would be giving. Hence, by convention, the CGI application is responsible for pass this particular header before the rest of the data (separated by a blank line). Note this point carefully when we actually get around to programming CGI applications, because forgetting either the Content-type header or the blank line (between the set of headers and the actual data), or both is a common occurrence and it can lead to a lot of agony.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===What is a CGI application?===&lt;br /&gt;
&lt;br /&gt;
A CGI application or a gateway program (see Illustration 2) is an executable file (usually an .exe file), which does not receive any information via the keyboard or the mouse, i.e., it is not an interactive application (a GUI application would be an interactive application). Such applications are known as console applications. They receive their inputs using the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream and they put out their outputs into the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream.&amp;lt;br /&amp;gt;&lt;br /&gt;
The rest of the headers are added by the web server after a CGI application does its work, and the compiled information is then passed to the browser.&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog07.jpg|frame|Illustration 2]]&lt;br /&gt;
&lt;br /&gt;
In case of CGI applications, once you start them, they terminate on its own without any inputs from you. You can say that they work &amp;#039;&amp;#039;automatically&amp;#039;&amp;#039; like the old DOS command line programs such as xcopy, or format, etc.&lt;br /&gt;
&lt;br /&gt;
A CGI application, however, is not just any console application. It would expect the inputs as per the CGI specification and its outputs should also conform to the CGI specification.&lt;br /&gt;
&lt;br /&gt;
In certain situations, a CGI application can be written as a .DLL file; but such variations are web server dependent. There are even more exotic variations possible. For example, even batch files (.bat files) can be made into simple CGI applications, provided the web server is configured to accept such files as CGI applications. Let us not get into those variations. For the purpose of this tutorial we&amp;#039;ll assume that a CGI application is an executable, console application (.exe file), adhering to the CGI specifications.&lt;br /&gt;
&lt;br /&gt;
===What are &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; and &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; streams?===&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; is the name given in various programming languages, to a special stream that acts as the input stream from the underlying OS (Operating System). Programs (such as those written in Visual Prolog) can read the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream, and use the characters given by that stream as their input. Similarly, &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; is a stream managed by the OS into which programs will output their characters into.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; and &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; streams can be daisy chained (i.e joined end to end) from different programs. For example, a program &amp;#039;A&amp;#039; can write into its &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream, and then call a program &amp;#039;B&amp;#039;, which in turn will use the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream to pick up what was written by the program &amp;#039;A&amp;#039;. This is essentially the transfer mechanism used by web servers: in the previous example, the program &amp;#039;A&amp;#039; represents the web server and the program &amp;#039;B&amp;#039; is the CGI application. The &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; and &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; streams are often referred together as the console.&lt;br /&gt;
&lt;br /&gt;
===How does the flow of information take place, using CGI?===&lt;br /&gt;
&lt;br /&gt;
The information flow starts from a request placed by the client (i.e., the browser) with the web server. As noted before, the client tells the web server about the various capabilities it has, and also speciies the information that it requires from the web server. See Illustration 3, which shows what gets sent by a browser (the client) to the web server.&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog08.jpg|frame|Illustration 3]]&lt;br /&gt;
&lt;br /&gt;
The first line contains the method field. This indicates the desired HTTP method, the resource being requested, the protocol version, etc., i.e., GET /Tests/file.html HTTP/1.0&amp;#039;&amp;#039;.&amp;#039;&amp;#039; Though /Test/file.html looks like a path, it is actually known as a URL (Uniform Resource Locator) and it represents a whole lot of things; including actual filenames and directories on the machine where the web server is located.&lt;br /&gt;
&lt;br /&gt;
Other common methods are &amp;#039;&amp;#039;&amp;#039;POST&amp;#039;&amp;#039;&amp;#039; (for sending data from the client to the server (not as part of the URL)) and &amp;#039;&amp;#039;&amp;#039;HEAD&amp;#039;&amp;#039;&amp;#039; (retrieve only header information about the resource).Other fields contain information about the client. The &amp;#039;&amp;#039;&amp;#039;Accept:&amp;#039;&amp;#039;&amp;#039; fields tell the server the different types of data that the client can accept. These are sent as MIME Content-type messages. Thus Accept: text/plain means that the client can accept plain text files, and so on. This MIME type is important, as this is how web servers tell clients the type of data being sent. The other headers are informational: &amp;#039;&amp;#039;&amp;#039;User-Agent:&amp;#039;&amp;#039;&amp;#039; - a program making the request, &amp;#039;&amp;#039;&amp;#039;From:&amp;#039;&amp;#039;&amp;#039; - who is making the request, &amp;#039;&amp;#039;&amp;#039;Referer:&amp;#039;&amp;#039;&amp;#039; - gives the URL of the document making this request, and so on. The server responds by returning the desired data. It first sends server response headers, which communicate information about the state of the transaction, the type of data being sent, and any additional information. This is followed by a blank line and then the actual data being sent to the client. We saw this, in Illustration 1. Where does the CGI come into this?, - you may wonder. Well, let us re-visit the requested resource, which is in the above example /Tests/file.htm&amp;#039;&amp;#039;l,&amp;#039;&amp;#039; if instead of that, it was /cgi-bin/helloworld.exe. Many web servers are configured to let the partial URL /cgi-bin/ indicate that helloword.exe is a CGI program, which the WEB server must invoke.&lt;br /&gt;
&lt;br /&gt;
Hence helloword.exe would be called by the web server and it would pass on some information to helloworld.exe using some environment variables and the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream (which is the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream for helloworld.exe). The web server will then wait till the CGI application, helloworld.exe, finishes its work.&lt;br /&gt;
&lt;br /&gt;
Once helloworld.exe ceases execution, the web server would gather the data written by helloworld.exe (including the Content-type header), and send it to the waiting browser.&lt;br /&gt;
&lt;br /&gt;
Phew! All that should ideally happen in the smallest period possible.&lt;br /&gt;
&lt;br /&gt;
Thankfully, all web servers are designed to do its job well and so we do not have to worry about the assembly work performed by a web server after it invokes a CGI application. Let us concentrate instead on how a CGI application should read information from the web server and then pass its data back to the web server, after doing its work.&lt;br /&gt;
&lt;br /&gt;
===What is an environment variable?===&lt;br /&gt;
&lt;br /&gt;
All OS (Operating Systems) allow programmers to insert name=value relationships in what are known as environment variables, somewhere in the computer&amp;#039;s RAM (Random Access Memory). For example; the PATH setting in DOS is an environment variable. You can examine the various environment variables that are set for DOS (or Windows XP or Windows 2000 for that matter) using the SET command. Just give SET command on a command line prompt, and the console will display all the currently available environment variables. (In the displayed list, you would see PATH also).&lt;br /&gt;
&lt;br /&gt;
The best part of the concept of environment variables is that you can cook up your very own variables and assign values to them. For example; if you want to keep a password around somewhere, you could do this using the SET command at the OS command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;SET password=xyze&amp;lt;/pre&amp;gt;Though the value is a string, you don&amp;#039;t have to give quotes. All values are internally stored as strings, and they are retrieved by Visual Prolog programs as strings. The significance of environment variables in CGI applications is that a web server uses various environment variables also to pass information to the CGI application that it invokes. Here is an example.&lt;br /&gt;
&lt;br /&gt;
We learnt that a web server places a lot of data on the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream before calling a CGI application. How does the CGI application know the number of characters to read from the flowing stream? To facilitate this, the web server places the content length into an environment variable called, CONTENT_LENGTH, and using that environment variable&amp;#039;s value the CGI application can read the right amount of characters from the stream. Let us examine some Visual Prolog code from cgi.pro (as part of the CGI pack that came with Visual Prolog) that does this:&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    retrieve_POST_string() = Result :- % Get the length&lt;br /&gt;
        LenStr = environment::getVariable(&amp;#039;CONTENT_LENGTH&amp;#039;),&lt;br /&gt;
        Len = toTerm(LenStr),&lt;br /&gt;
        %Read the stream to the desired length only&lt;br /&gt;
        Stream = console::getConsoleInputStream(),&lt;br /&gt;
        Stream:setMode(stream::ansi(ansi())),&lt;br /&gt;
        %The stream is in ANSI mode!&lt;br /&gt;
        Result = Stream:readString(Len).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Apart from CONTENT_LENGTH, most web servers set the following &amp;#039;&amp;#039;standard&amp;#039;&amp;#039; environment variables before starting a CGI application. A few additions and subtractions may be possible to these environment variables, depending on the web server that you use. The CGI application, in turn can then look for those variables and use the values that are contained therein. The reader is requested to read about these environment variables elsewhere, else this tutorial would become too lengthy. Of these, the environment variable HTTP_REFERER can be useful in implementing some simple security measures (described later in Section B of the tutorial).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;PATH_INFO&lt;br /&gt;
PATH_TRANSLATED&lt;br /&gt;
REMOTE_HOST&lt;br /&gt;
REMOTE_ADDR&lt;br /&gt;
GATEWAY_INTERFACE&lt;br /&gt;
SCRIPT_NAME&lt;br /&gt;
REQUEST_METHOD&lt;br /&gt;
HTTP_ACCEPT&lt;br /&gt;
HTTP_ACCEPT_CHARSET&lt;br /&gt;
HTTP_ACCEPT_ENCODING&lt;br /&gt;
HTTP_ACCEPT_LANGUAGE&lt;br /&gt;
HTTP_FROM HTTP_HOST&lt;br /&gt;
HTTP_REFERER&lt;br /&gt;
HTTP_USER_AGENT&lt;br /&gt;
HTTP_COOKIE&lt;br /&gt;
QUERY_STRING&lt;br /&gt;
SERVER_SOFTWARE&lt;br /&gt;
SERVER_NAME&lt;br /&gt;
SERVER_PROTOCOL&lt;br /&gt;
SERVER_PORT&lt;br /&gt;
CONTENT_TYPE&lt;br /&gt;
CONTENT_LENGTH&lt;br /&gt;
USER_NAME&lt;br /&gt;
USER_PASSWORD&lt;br /&gt;
AUTH_TYPE&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===How do I test CGI applications?===&lt;br /&gt;
&lt;br /&gt;
You would need to have a web server installed, along with your CGI application in order to test it. You would also need support HTML files, which will trigger the CGI application through the web server. They will obviously vary from one application to another. You&amp;#039;ll become aware of such support files, when you study the examples.&lt;br /&gt;
&lt;br /&gt;
As indicated somewhere before, a CGI application has to be placed in the same machine, where the web server is itself running. For reasons of security, a web server is able to fetch files from only a specific set of directories (the set is known as the web-path) on the host machine. Therefore, you should place a CGI application files (CGI executable file and HTML files) into one of these directories.If that was not the case, some malicious user may try to fetch files that should not have been retrievable.&lt;br /&gt;
&lt;br /&gt;
Similarly, a web server can only invoke a CGI application if it is placed in the right directory. You cannot place the CGI application anywhere on the web-path (well, a few web server do allow that too, but not all). Now, all web servers have a configuration method (a separate configuration file, or an .ini file or the windows registry, etc.) to configure its various settings. One of the crucial settings is the location of the CGI applications. When you zero in on a web server for your CGI application, the first thing you should study is how that particular web server configures its CGI application directories.&lt;br /&gt;
&lt;br /&gt;
===Which web server to use?===&lt;br /&gt;
&lt;br /&gt;
As indicated before, web servers are not complicated pieces of software. There are many web servers available free of cost. A large list can be seen at: http://www.serverwatch.com/stypes/index.php/d2Vi . That list includes both commercial software as well as free ones. The complexity of the web server is usually the configuration of the web-path and CGI application directory/directories of the web server.&lt;br /&gt;
&lt;br /&gt;
The TCP-IP protocol should be installed in the computer where the web server is to be setup (It usually would be, if you are able to browse the Internet from that computer) Once you have successfully installed a web server, you should be able to get data from the web server by supplying either the IP address or the domain name of the machine where the web server is installed. Please note that the CGI programs should work fine with the most of web servers including the IIS5 web server which is usually included with Windows 2000 by Microsoft.&lt;br /&gt;
&lt;br /&gt;
===Where to place the CGI application?===&lt;br /&gt;
&lt;br /&gt;
Ensure that the CGI application is placed correctly in such a web server&amp;#039;s appropriate script directory. This will vary from a web server to a web server. In this tutorial, we will assume that you have placed it in such a directory that it can be invoked using a browser on the server machine with the following URL: &amp;#039;&amp;#039;&amp;#039;&amp;lt;nowiki&amp;gt;http://localhost/cgi-bin/&amp;lt;/nowiki&amp;gt;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;lt;APPNAME&amp;gt;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
For example, if you have a CGI application called example1.exe, then it should be accessible with the following URL, on the server machine: &amp;#039;&amp;#039;&amp;#039;&amp;lt;nowiki&amp;gt;http://localhost/cgi-bin/example1.exe&amp;lt;/nowiki&amp;gt;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
===Can you show me a very simple CGI application created using Visual Prolog?===&lt;br /&gt;
&lt;br /&gt;
Yes, indeed! You should unzip the file example1.zip that is included into cgitutorial.zip into a convenient place and examine the Visual Prolog files therein. But, you can create such an application from scratch too by following the instructions given below.&lt;br /&gt;
&lt;br /&gt;
Start a new Visual Prolog project. Note that the UI Strategy is Console:&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog01.png]]&lt;br /&gt;
&lt;br /&gt;
The main Project tree would get displayed. It will include all the necessary files that the VDE has intelligently decided you may require.&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog02.jpg]]&lt;br /&gt;
&lt;br /&gt;
Now, click on the main &amp;#039;&amp;#039;Build&amp;#039;&amp;#039; menu, and in that menu, select &amp;#039;&amp;#039;Build&amp;#039;&amp;#039; or press Alt-F9. You&amp;#039;ll notice something magical, as the build process continues. As the project is being built, the VDE will pull in all the relevant PFC (Prolog Foundation Classes) files that were not visible earlier in the Project Tree. After the build process is over, the Project Tree will now look like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog03.jpg]]&lt;br /&gt;
&lt;br /&gt;
Congratulations! You have finished 95% of the job. Now you would be having a console application in the EXE folder of your project, which currently does nothing. We need now to add the relevant code to transform the application into a CGI application&lt;br /&gt;
&lt;br /&gt;
As explained in the earlier sections of this tutorial, a CGI application expects some input from the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream (Along with some environment variables), then it does its work and sends out the output through the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream. As this is a very simple CGI application (our very first one) we&amp;#039;ll ignore all input arriving into our CGI application from the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream, (which the web server may be sending to the CGI application). Instead, we&amp;#039;ll make our little application send some output.&lt;br /&gt;
&lt;br /&gt;
Double click on the file &amp;#039;&amp;#039;&amp;#039;example1.pro&amp;#039;&amp;#039;&amp;#039; in the Project Tree, and go towards the end of the code:&lt;br /&gt;
&lt;br /&gt;
[[Image:cgiApplicationsInVisualProlog04.jpg]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll notice that the main goal of the program invokes a predicate called run from the PFC module mainEXE. That module in turns invokes the predicate called run() from example1.pro, so let us have a look at that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
console::init(),&lt;br /&gt;
succeed().  % place your own code here&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We&amp;#039;ll modify that code to make it look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run():-&lt;br /&gt;
        console::init(), %Line 1&lt;br /&gt;
        cgi::init(stream::ansi(core::ansi)), %Line 2&lt;br /&gt;
        stdIO::write(&amp;quot;Content-type:   text/html\n&amp;quot;), %Line 3&lt;br /&gt;
        stdIO::write(&amp;quot;\n&amp;quot;), %Line 4&lt;br /&gt;
        stdIO::write(&amp;quot;&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;quot;). %Line 5&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us now go through the code, line by line:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Line 1:&amp;#039;&amp;#039;&amp;#039; is a Visual Prolog requirement. All work using the &amp;#039;&amp;#039;console&amp;#039;&amp;#039;, should first start with console::init() .&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Line 2:&amp;#039;&amp;#039;&amp;#039; captures the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream object to CGI output. As explained in the paragraph of this tutorial, the CGI application is expected to send its output to the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Line 3:&amp;#039;&amp;#039;&amp;#039; Now the CGI application is prepared to send the first line back to the calling web server. As explained in the paragraph of this tutorial, the data has to be preceded by some headers. The header which is mandatory is the one that sets the mime-type of the data. Here, it states that the Content-type of the data is of the mime-type text/html. Notice that the line ends in a carriage return (\n).&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Line 4:&amp;#039;&amp;#039;&amp;#039; To separate the headers from the rest of the data a blank line is needed. It is mandatory as per the CGI specification. So the CGI application now sends a blank line (\n) there. Forgetting to do this is very common in CGI applications (Read the paragraph of this tutorial for more), and it can lead to the malfunctioning of the application.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Line 5:&amp;#039;&amp;#039;&amp;#039; In the end the actual data is written out to the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039;. In this example, it is simply creating an HTML file and sending that. The HTML file contains just two words : &amp;#039;&amp;#039;&amp;#039;Hello World!&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
===How to test Example1.exe?===&lt;br /&gt;
&lt;br /&gt;
Place it in the script directory of your web server, as explained earlier. Then access the following URL on the same machine as the web server &amp;#039;&amp;#039;&amp;#039;&amp;lt;nowiki&amp;gt;http://localhost/cgi-bin/example1.exe&amp;lt;/nowiki&amp;gt;&amp;#039;&amp;#039;&amp;#039; using a suitable browser.&lt;br /&gt;
&lt;br /&gt;
===What is the functionality of such a primitive application?===&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Hello World&amp;#039;&amp;#039; CGI application (Example1) is not actually as primitive as it seems. It is simple alright, but it can serve as the seed for much sophisticated stuff. Take the last line (Line 6 explained in Paragraph of this tutorial). There the complete HTML is created and then dispatched over the &amp;#039;&amp;#039;stdout&amp;#039;&amp;#039; stream to the web server, who in turn, sends it to the requesting browser. If you introduce a database access at that point, and add some more predicates for conditional processing of the data from the background databases, you can easily build a content management system&lt;br /&gt;
&lt;br /&gt;
===What about inputs?===&lt;br /&gt;
&lt;br /&gt;
As indicated earlier, we have ignored whatever inputs the web server may have given to our little application. If the CGI application needed some inputs, then the &amp;#039;&amp;#039;stdin&amp;#039;&amp;#039; stream would have to be read, and processed for information. Also, the environment variables set at the time of the CGI application operation would also have to be examined. This can be a little complex. Luckily for us, we do not have to do much programming in Visual Prolog when developing CGI applications. For example, to know what data the CGI application receives from the web server, all we have to do is to use the cgi::getString() predicate. (We have to include the cgi.pack from the PFC into our project). And if we need the data in a more convenient form, then we can use the cgi::getParamList() predicate. The individual pieces of data are assembled by cgi::getParamList() into name, value pairs similar to environment variables.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=DLL_Memory_Handling&amp;diff=450</id>
		<title>DLL Memory Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=DLL_Memory_Handling&amp;diff=450"/>
		<updated>2007-10-03T09:34:31Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Heap]] memory that cross a [[wikipedia::/Dynamic-link_library|DLL]] boundary should be handled with care, because the two sides may treat memory differently. In Visual Prolog a [[Garbage Collection|garbage collector]] handles the heap.&lt;br /&gt;
 &lt;br /&gt;
== Vip/Vip ==&lt;br /&gt;
&lt;br /&gt;
When the caller and callee are both in Visual Prolog (and compiled with the same version of Visual Prolog), then memory can simply be passed back and forth as you wish, because both caller and callee will use the same garbage collector for the heap management.  So in this case the DLL boundary is completely transparent.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039;: If caller and callee are compiled with different version of Visual Prolog, then they are &amp;#039;&amp;#039;&amp;#039;foreign&amp;#039;&amp;#039;&amp;#039; to each other and memory must be handled accordingly.&lt;br /&gt;
&lt;br /&gt;
== Foreign/Vip ==&lt;br /&gt;
&lt;br /&gt;
Visual Prolog&amp;#039;s garbage collector does not know whether a foreign program uses a piece of memory or not, and therefore it may reclaim the memory even though the foreign part of the program still needs it (most likely this will result in an access violation at some later point).  Therefore you must decide on a memory handling scheme. Basically there are two schemes to choose.&lt;br /&gt;
&lt;br /&gt;
=== Caller provides memory ===&lt;br /&gt;
&lt;br /&gt;
This scheme is used in most Windows API calls and in most C/C++ context. The principle is that the caller allocates the memory and the callee copies the result into that memory. &lt;br /&gt;
&lt;br /&gt;
In you getString case the vip declaration would look like this: &lt;br /&gt;
&amp;lt;vip&amp;gt;class dllExport&lt;br /&gt;
predicates&lt;br /&gt;
    getString : (string ResultBuffer) language stdcall.&lt;br /&gt;
end class dllExport&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that the string parameter is an input argument. This is because the caller must provide a buffer for the result. (Very often there would also be another argument stating the size of the buffer, such that the DLL will not write outside the buffer). &lt;br /&gt;
&lt;br /&gt;
The corresponding implementation could look like this: &lt;br /&gt;
&amp;lt;vip&amp;gt;implement dllExport&lt;br /&gt;
clauses&lt;br /&gt;
    getString(Buffer) :-&lt;br /&gt;
        Result = &amp;quot;ResultString&amp;quot;,&lt;br /&gt;
        ByteCount = sizeOfDomain(char) * (sting::length(Result)+1),&lt;br /&gt;
        memory::copy(uncheckedConvert(pointer, Buffer), uncheckedConvert(pointer, Result), ByteCount).&lt;br /&gt;
end implement dllExport&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last two lines in the clause should be turned into a separate predicate (in fact, it should ought to be in PFC already, but I can&amp;#039;t seem to find it). &lt;br /&gt;
&lt;br /&gt;
=== Callee provides a clean-up routine ===&lt;br /&gt;
 &lt;br /&gt;
An alternative approach is to allow callee memory to float into the caller, and then give the caller a predicate to call once the memory should be released. &lt;br /&gt;
&lt;br /&gt;
In the getString example it could look like this:&lt;br /&gt;
&amp;lt;vip&amp;gt;class dllExport&lt;br /&gt;
predicates&lt;br /&gt;
    getString : () -&amp;gt; string ResultBuffer language stdcall.&lt;br /&gt;
predicates&lt;br /&gt;
    releaseString : (string ToRelease) language stdcall.&lt;br /&gt;
end class dllExport&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case the caller will simply receive the string from the DLL, but once the caller is done with the string it must call releaseString to have it released. &lt;br /&gt;
&lt;br /&gt;
In the implementation we have to make sure that the memory is not garbage collected until releaseString has been called. We can achieve this by asserting the string in a class fact and retract it when it is released, because memory that can be reached from a class fact is never garbage collected. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement dllExport&lt;br /&gt;
class facts&lt;br /&gt;
    keepAlive : (pointer ToKeepAlive).&lt;br /&gt;
clauses&lt;br /&gt;
    getString() Result :-&lt;br /&gt;
        Result = &amp;quot;ResultString&amp;quot;,&lt;br /&gt;
        assert(keepAlive(uncheckedConvert(pointer, Result))).&lt;br /&gt;
clauses&lt;br /&gt;
    releaseString(ToRelease) :-&lt;br /&gt;
        retractAll(keepAlive(uncheckedConvert(pointer, ToRelease))).&lt;br /&gt;
end implement dllExport&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The reason that I assert pointers and not strings, is because otherwise the retract might retract a wrong string, because strings are retracted based on their value (i.e. the characters in the string). Pointers are also retracted based on their value, but their value is a memory address.&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog_-_GUI&amp;diff=449</id>
		<title>Fundamental Visual Prolog - GUI</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Fundamental_Visual_Prolog_-_GUI&amp;diff=449"/>
		<updated>2007-10-03T09:26:20Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{FundamentalVisualPrologNavbar}}&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we shall add a simple [[wikipedia:Graphical_User_Interface|Graphical User Interface]] (GUI) front end to the family project, which was developed in an earlier tutorial. We shall learn to do create and edit most of the GUI components of a simple Windows program directly within the Visual Prolog IDE (Integrated Development Environment). We shall learn a bit about handling of GUI events (like clicking a button on a specific part of the GUI, etc.), and how they work within a Visual Prolog program. We shall also learn about modal dialogs with two examples: one, which is inbuilt by the Windows operating system, and one, which we shall build for ourselves for our program.&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;
==Introduction to a GUI==&lt;br /&gt;
&lt;br /&gt;
A GUI is simply an acronym for Graphical User Interface. In the Windows operating system environment the term represents the familiar windows with the menu bar, toolbar, the little buttons on the top right hand corner of a window, etc. Each of those elements is known as a GUI component, and in well designed programs, these components work as per conventionally accepted norms.&lt;br /&gt;
&lt;br /&gt;
In programming terms, a GUI does two things. It uses complex graphical routines to put (and restore) graphical images on the relevant parts of the computer monitor. (E.g. the little square with an X on it on the top right corner of a window). It also controls the behaviour of the mouse and other input devices over these graphical areas. Mercifully, both these detailed programming are done by the operating system. As a programmer one need not get down on our knees to program such graphic, mouse and keyboard functions. Windows does it for us. It has given an API (Application Programming Interface) which can be used to setup the GUI required for any program.&lt;br /&gt;
&lt;br /&gt;
Furthermore, Visual Prolog has added one more layer - the PFC (Prolog Foundation Classes) to help out not only with the GUI but with other areas of programming too.&lt;br /&gt;
&lt;br /&gt;
And even more; the Visual Prolog IDE can be used to visually create the mock-ups of the final GUI that would be used by the program that you would be developing. In this tutorial we shall set out to do just that. We will be using the logic of the family example that was tackled in a previous tutorial.&lt;br /&gt;
&lt;br /&gt;
There is a lot of difference between a GUI and console programs. Though both console programs and GUI programs always starts at a fixed entry point (from the &amp;#039;&amp;#039;Goal&amp;#039;&amp;#039; procedure), that is where the similarity ends. Console programs do not show graphical elements, so they have the user input either from start-up parameters or from direct questions setup by the programmer in advance. The sequence and direction of activities in the program would have to be determined by the programmer. The user cannot modify that sequence. In a console program, the program is started from the &amp;#039;&amp;#039;Goal&amp;#039;&amp;#039; procedure and it is then worked logically from that point onwards, straitjacketing the user through a series of logical steps set out by the programmer.&lt;br /&gt;
&lt;br /&gt;
With GUI programs, the programmer can choose to be flexible. The user can be given options what should be done on which part of the program. For example; if you start any conventional Windows program, it normally will not press you to do this or do that. You can laze around casually flipping through the menus without really clicking on any of them. Consider this scenario: The File menu will have several menu items. You can take the mouse cursor through each one of them without really being compelled to click on any of the menu item you may find there. In fact, one of the common methods by which people get to grips with a piece of software is by such casual browsing over the GUI of a program.&lt;br /&gt;
&lt;br /&gt;
Anyway, to cut a long story short; the programmer has to use a different strategy when programming GUI programs. On one hand, the programming is easier because there is no rigidity or regimentation in the activities of the program that will be carried out.&lt;br /&gt;
&lt;br /&gt;
But on the other hand, the programmer needs to know how each of the GUI components works, what are conventionally accepted practices and how to adhere to those practices so that the user is not given unnecessary surprises when using the program. Also, sometimes it would be difficult to predict the consequences that may follow due to some GUI event being triggered. Hence the programmer has to carefully isolate the logic so that it remains valid irrespective of how the GUI was used, and carefully accommodate those situations, where the program&amp;#039;s logic may be unable to accommodate (using polite, easy to understand warning dialogs, etc.).&lt;br /&gt;
&lt;br /&gt;
==GUI: Responding to Events==&lt;br /&gt;
&lt;br /&gt;
In a GUI program, all the GUI components wait for inputs from the keyboard and/or mouse (and/or other pointing devices such as digitizer puck, etc.).&lt;br /&gt;
&lt;br /&gt;
The information from such input devices (click of a mouse and other such GUI activity) is known as an &amp;#039;&amp;#039;event&amp;#039;&amp;#039;. There are other &amp;#039;&amp;#039;events&amp;#039;&amp;#039; to which a GUI responds - some of them are internally generated by the operating system such as ticking of the internal clock, interaction with other programs running concurrently, ActiveX events, etc. Depending on what the program is expected to do, the programmer earmarks appropriate portions of code to respond to relevant &amp;#039;&amp;#039;events&amp;#039;&amp;#039;. PFC GUI package provides the set of listeners and responders to handle GUI events.&lt;br /&gt;
&lt;br /&gt;
When the program responds to an event, the program can either &amp;#039;&amp;#039;listen&amp;#039;&amp;#039; the event or &amp;#039;&amp;#039;handle&amp;#039;&amp;#039; the event. If an &amp;#039;&amp;#039;event&amp;#039;&amp;#039; is listened, then it means the program can just do something. For example the &amp;#039;&amp;#039;show event&amp;#039;&amp;#039; comes, when a window is shown, hence program can make some initialization here. If an &amp;#039;&amp;#039;event&amp;#039;&amp;#039; is responded, then it means the program can change the behaviour of the system. For example, on the &amp;#039;&amp;#039;close event&amp;#039;&amp;#039; the program can respond either to close window or to keep it open.&lt;br /&gt;
&lt;br /&gt;
Please notice also that the GUI program actually does nothing but waits till a GUI event is invoked.&lt;br /&gt;
&lt;br /&gt;
One important feature of event handling must be understood here:&lt;br /&gt;
&lt;br /&gt;
Because the code of a listener and a responder wait till an event has happened; it may seem as if there is no obvious logic to it. Other modules need not even know what is happening privately within the class which was actually handling a GUI event. In our earlier tutorial, we noticed that all the logic was well contained and logically connected to one another. But in case of GUI programs, parts of the code (the one that handles GUI events) would be &amp;#039;&amp;#039;fragmented&amp;#039;&amp;#039; and placed under different listeners and responders.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we shall go about taking the same logic of the family example which was dealt in an earlier tutorial, and then use that logic via a GUI interface.&lt;br /&gt;
&lt;br /&gt;
==Starting a GUI Project==&lt;br /&gt;
&lt;br /&gt;
Let us start at the very beginning (a very good place to start!). When we create the project in the Visual Prolog IDE, make sure that the UI strategy is set to &amp;#039;&amp;#039;&amp;#039;PFC GUI&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The IDE then creates the initial set of modules required to handle the GUI along with the GUI component resources: the main menu, one top toolbar, one bottom status bar, the About dialog and the main Task Window of the program.&lt;br /&gt;
&lt;br /&gt;
Just after you create a project in this fashion, you can immediately compile it to get an empty GUI program. At this stage, the program actually would not do anything. However, it will have all the basic functionality expected in a GUI program. Visual Prolog has simply constructed the essential skeleton of a working GUI program and has provided some features commonly required.&lt;br /&gt;
&lt;br /&gt;
For example; within the main window (known here as the Task Window) of the application Visual Prolog gives another window titled Messages. This window is used internally to act as the &amp;#039;&amp;#039;console&amp;#039;&amp;#039;. When the programmer uses the stdio::write(...) predicate in the program, the output would get directed to this Messages window. If Visual Prolog has not redirected the output of PFC class stdio to the Messages window, those strings would not be seen, as a GUI environment does not have a console area by default.&lt;br /&gt;
&lt;br /&gt;
In a console application, the console is always available as a &amp;#039;&amp;#039;blackboard&amp;#039;&amp;#039; onto which the programmer can deposit output. That is why those applications are known as &amp;#039;&amp;#039;console&amp;#039;&amp;#039; applications. We saw that in an earlier tutorial that we could simply write to the &amp;#039;&amp;#039;console&amp;#039;&amp;#039; in such an application using stdio::write(...) predicate.&lt;br /&gt;
&lt;br /&gt;
When you run our compiled GUI program at this stage, you would notice that you can flip through the menus, re-size the outer main window (or Task Window&amp;#039;&amp;#039;)&amp;#039;&amp;#039; of the program, double click on the Messages window to zoom it full extents within the Task Window, etc. Visual Prolog even gives a small pop-up menu for the Messages window which is often convenient. Right click anywhere inside the Messages window, and a small menu would open up with which you can clear the contents of the Messages window and do other activities.&lt;br /&gt;
&lt;br /&gt;
But at this point this simple GUI program does not have any logical functionality that we desire. We need to do some further work to achieve this functionality.&lt;br /&gt;
&lt;br /&gt;
Before we embark on populating the project with the actual working logic of the program, we shall create and/or modify some GUI components that we need. Under normal circumstances, it is here where the programmer would have to spend some time thinking out a strategy for the GUI of the program. A clear list of GUI components would have to be made, and only then one should embark on the creation and/or modification of the components. This activity should not be carried out in an unplanned manner. But for the sake of simplicity, we shall proceed in this tutorial with the assumption that this planning process is over.&lt;br /&gt;
&lt;br /&gt;
All GUI components are stored as separate resource files during the coding phase. In most other programming languages; these resource files are separately compiled and then included into the main code during the linking process. Visual Prolog deals with all this resource compilation and linkage issues automatically without unnecessarily bothering the user.&lt;br /&gt;
&lt;br /&gt;
==Creating a Modal Dialog==&lt;br /&gt;
&lt;br /&gt;
Now we shall add another GUI component that we would need later on in our program. It is a dialog box which will be used to feed the name of a person to the program for the program to process. In the Project tree (where all the modules and resources are listed in a clearly laid out tree menu), right click on the Task Window and from the context menu, select New in Existing Package...&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui02.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Create Project Item dialog would be presented. Ensure that Dialog is selected from the left hand side and on the right hand side enter the AncestorDialog.&lt;br /&gt;
&lt;br /&gt;
A default dialog is created for you to edit. As we will not be implementing a Help function for this dialog, we will click on that particular GUI component (i.e. the Help button) and we shall delete that by pressing the DEL key.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui04.jpg]]&lt;br /&gt;
&lt;br /&gt;
Then the other two buttons should be selected and should be shifted about on the dialog box in order to make the dialog look presentable. The final result would look something as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui05.jpg]]&lt;br /&gt;
&lt;br /&gt;
Using the dialog editing controls, we shall now insert a static text. (The word &amp;#039;&amp;#039;static&amp;#039;&amp;#039; implies a non-clickable GUI element). Refer the previous image. On clicking the static text creation button, we can mark out the rectangular area, where our text would appear as seen below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui06.jpg]]&lt;br /&gt;
&lt;br /&gt;
On releasing the mouse you will see the control on the dialog. Left click on the just created control and the properties panel would show the properties of the created control. Under the Text field, write &amp;quot;Person&amp;quot;: as seen below. That would be the text which would appear inside the dialog.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui07.jpg]]&lt;br /&gt;
&lt;br /&gt;
In a similar fashion, we shall use the dialog editing controls to insert an editable text field or an edit control, as seen below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui08.jpg]]&lt;br /&gt;
&lt;br /&gt;
This time, we shall leave the text field empty and instead change the name to something meaningful (instead of the default name which is given by the IDE) as seen below. This name is used within the program code to refer to this edit field in the program&amp;#039;s code as fact variable.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui09.jpg]]&lt;br /&gt;
&lt;br /&gt;
This is how finally our dialog would look like:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui24.jpg]]&lt;br /&gt;
&lt;br /&gt;
We have one last step which is needed to be performed. Each dialog has a property known as a &amp;#039;&amp;#039;Visit Order&amp;#039;&amp;#039;. That is the order by which the controls (i.e. GUI components that interact with the user) on that dialog are visited, when the user clicks on the TAB key. The phrase &amp;quot;&amp;#039;&amp;#039;visit a control&amp;#039;&amp;#039;&amp;quot; means that the said control receives the &amp;#039;&amp;#039;input focus&amp;#039;&amp;#039;. (But then that is more jargon!) The phrase &amp;#039;&amp;#039;the control receiving the input focus&amp;#039;&amp;#039;, means the said control which is immediately receptive to inputs from the keyboard. For example; if the edit field had the input focus one would see the blinking caret within the field ready to accept characters entered through the keyboard.&lt;br /&gt;
&lt;br /&gt;
In order to change the visit order, right click on the dialog, and click on Visit Order in the context menu that opens up.&lt;br /&gt;
&lt;br /&gt;
Small buttons will appear on some of the GUI components of the dialog box as shown in the figure below. As seen here, the text edit control (&amp;#039;&amp;#039;idc_ancestordialog_personname&amp;#039;&amp;#039;) has a visit order of 3 whereas the visit order number for the OK button is 1.&lt;br /&gt;
&lt;br /&gt;
This means that when the dialog is presented to the user, the text edit will not be the one which will receive keyboard characters immediately. When the user presses the TAB button, then the focus will shift to the Cancel button, and only on pressing the TAB button one more time would the focus will go to the edit control. Only then the text edit control would receive keyboard input.&lt;br /&gt;
&lt;br /&gt;
In short, the edit field can be said to have the last &amp;#039;&amp;#039;visit order&amp;#039;&amp;#039; number, with respect to all the controls in the dialog.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui25.jpg]]&lt;br /&gt;
&lt;br /&gt;
We need to change this. Click on the small button labeled 3. This will open up the Visit Order dialog box as shown above. You can change the order using the &amp;#039;&amp;#039;+&amp;#039;&amp;#039; and &amp;#039;&amp;#039;-&amp;#039;&amp;#039; buttons. In our case, we will use the change the visit order so that &amp;#039;&amp;#039;idc_ancestordialog_personname&amp;#039;&amp;#039; has a visit order of 1. Now, when the dialog gets used in the program; the input focus will first be on the edit control.&lt;br /&gt;
&lt;br /&gt;
Please note that in dialogs, there is another property called the &amp;#039;&amp;#039;default push button&amp;#039;&amp;#039; which can be set separately from the visit order. An event for the button that has been set to be the &amp;#039;&amp;#039;default push button&amp;#039;&amp;#039; will be triggered on pressing the ENTER key irrespective of which control had the &amp;#039;&amp;#039;input focus&amp;#039;&amp;#039;. Usually, the default push button is set to be the OK button.&lt;br /&gt;
&lt;br /&gt;
When you right-click on the dialog, you can set the overall dialog attributes using the following dialog. If you notice, the Type of the dialog is set to Modal. The term &amp;#039;&amp;#039;modal&amp;#039;&amp;#039; reflects the fact that whenever the dialog is presented to the user, the GUI would stop responding to other parts of the program, when the dialog is visible. Only when the dialog is disposed off by the user (by pressing the OK or Cancel button) the GUI will become responsive once again to all GUI activity.&lt;br /&gt;
&lt;br /&gt;
When you click on the dialog, you can set the overall dialog properties on the properties panel. If you notice, the Type of the dialog is set to Modal. The term modal reflects the fact that whenever the dialog is presented to the user, the GUI would stop responding to other parts of the program, when the dialog is visible. Only when the dialog is disposed off by the user (by pressing the OK or Cancel button) the GUI will become responsive once again to all GUI activity.&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;modeless&amp;#039;&amp;#039; dialog on the other hand does not make this restriction. The entire GUI interface is responsive even if the dialog is active. Programming a &amp;#039;&amp;#039;modeless&amp;#039;&amp;#039; dialog requires a little bit more thought and it is not touched upon in this tutorial.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui30.jpg]]&lt;br /&gt;
&lt;br /&gt;
Using the same Dialog Attributes (see figure above), you should also change the title to the &amp;quot;Ancestor of ...&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Modifying the Menu==&lt;br /&gt;
&lt;br /&gt;
Now we shall modify the main menu of the program. As noted before, the IDE would have provided a default menu consisting of some standard menu items which are found in many programs. We need to edit that menu to suit our program&amp;#039;s functionality. Using the project tree, double click on the TaskMenu.mnu item as seen below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui10.jpg]]&lt;br /&gt;
&lt;br /&gt;
This will start the Menu Editor. The main dialog of the Editor is seen below. Click on the Edit button after ensuring that the TaskMenu.mnu is selected in the project window.&lt;br /&gt;
&lt;br /&gt;
This will open up the following Menu Item Attributes dialog, as shown above. We will change the name from &amp;#039;&amp;#039;&amp;amp;amp;Edit&amp;#039;&amp;#039; to &amp;#039;&amp;#039;&amp;amp;amp;Query&amp;#039;&amp;#039;. The ampersand sign (&amp;amp;amp;) before Q indicates the letter which would get underlined in the menu. Users can use that alphabet to quickly access the menu.&lt;br /&gt;
&lt;br /&gt;
You can test this feature by clicking on the Test button in the above dialog. The top menu of the IDE will be temporarily replaced by the menu you are designing. You can then browse the menu just to get a feel of it. You can open it submenus, etc. To exit the test mode, you can click somewhere in the IDE window out of the tested menu.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui12.jpg]]&lt;br /&gt;
&lt;br /&gt;
We shall now return back to the main TaskMenu dialog. Double click on the &amp;#039;&amp;#039;&amp;amp;amp;Query&amp;#039;&amp;#039; entry, and you would notice that it still contains the menu items of the old Edit menu (Undo, Redo, Cut, Copy and Paste). Delete all the menu items you see within for that main menu entry (&amp;amp;amp;Query). Also change the Constant Prefix setting to &amp;#039;&amp;#039;id_query&amp;#039;&amp;#039;. The constant prefix is used internally by the IDE to construct the constants that would represent the various menu items. Those constants would be used internally in the code to refer to the menu items.&lt;br /&gt;
&lt;br /&gt;
Now we shall add some menu items under the main Query menu item. To do that, click on the New &amp;#039;&amp;#039;&amp;#039;SubItem&amp;#039;&amp;#039;&amp;#039; item and enter the information you see in the following figure:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui14.jpg]]&lt;br /&gt;
&lt;br /&gt;
You would note that the Constant is automatically created for you depending on the text you enter for the menu item. In the above example it would be &amp;#039;&amp;#039;id_query_father&amp;#039;&amp;#039; as seen in the previous figure. In a similar fashion, create menu entries for &amp;amp;amp;Grandfather and &amp;amp;amp;Ancestor of ...&lt;br /&gt;
&lt;br /&gt;
Note that by convention an ellipsis (three dots) is used at the end of those menu items which will yield another dialog before any work is carried out. In the above example, &amp;amp;amp;Father and &amp;amp;amp;Grandfather menu items will not have the ellipsis. But the &amp;amp;amp;Ancestor of ... entry will have the ellipsis, because on invoking that menu item, a dialog would open up before any activity gets done.&lt;br /&gt;
&lt;br /&gt;
One last step is remaining in the editing of the TaskWindow menu. By default, when the IDE creates the menu, the File|Open menu is disabled. You would have to locate that menu item, and enable it by removing the checkmark from that menu item&amp;#039;s Menu Item Attributes dialog, as seen below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui16.jpg]]&lt;br /&gt;
&lt;br /&gt;
By the way, in the previous figure you would note that you can set the accelerator (i.e. the hot-key) which can be invoked by the user to quickly invoke the same function as the menu item. In the above example, it would be the F8 function key. It should also be noted that the menu item for opening a file is indicated here as &amp;amp;amp;Open (without an ellipsis). You should correct that so that it reads &amp;amp;amp;Open ... to suit the ellipsis convention.&lt;br /&gt;
&lt;br /&gt;
When you close the TaskMenu creation dialog, the IDE would confirm whether you want to save the menu. Click on Save.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui15.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Modifying the Toolbar==&lt;br /&gt;
&lt;br /&gt;
The toolbar of the application is another useful GUI component. Usually, it would contain buttons representing some of the functions of various menu items. In short, those buttons act as short cuts to the menu. We shall now go about editing the program&amp;#039;s toolbar. A default toolbar is created for the program by the Visual Prolog IDE when you first create the project. From the Project Tree, double-click on the ProjectToolbar.tb.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui26.jpg]]&lt;br /&gt;
&lt;br /&gt;
This will invoke the toolbar editor. Note that the top portion represents the toolbar that you are editing and the bottom portion indicates the various controls that are available for you to edit those toolbar components.&lt;br /&gt;
&lt;br /&gt;
In the toolbar, you would notice a set of buttons with predefined iconic images (as commonly accepted in GUI programs) If so desired, you can change those iconic images too but in this tutorial we shall not get into that fine detail. It should be indicated here that the Visual Prolog IDE does contain a nifty little icon editing program right inside it. For larger images, the IDE opens MS Paint for editing.&lt;br /&gt;
&lt;br /&gt;
The buttons have been mapped out to a set of menu-item functions. But as we have now edited the menu items, we shall also edit the toolbar buttons and map it to the correct locations.&lt;br /&gt;
&lt;br /&gt;
Firstly, we need to remove the buttons representing cut, copy and paste as our program does not have those capabilities. Hence select those buttons and delete them from the toolbar. After deletion the toolbar should look as follows:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui28.jpg]]&lt;br /&gt;
&lt;br /&gt;
We shall now map the Undo, Redo and the Help buttons to represent the following menu items: Query|Father&amp;#039;&amp;#039;..&amp;#039;&amp;#039;., Query|Grandfather... and Query|Ancestor of ...&lt;br /&gt;
&lt;br /&gt;
(As noted before, we would not be changing the images of those buttons in this tutorial.)&lt;br /&gt;
&lt;br /&gt;
Double click on the Undo toolbar button and in the Button Attributes dialog that is presented, change the Constant that internally represents the toolbar button from &amp;#039;&amp;#039;id_edit_undo&amp;#039;&amp;#039; to &amp;#039;&amp;#039;id_query_father&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui29.jpg]]&lt;br /&gt;
&lt;br /&gt;
In the same dialog box, you should change the Status Text from:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Undo;Undo&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Query fathers;List of all fathers listed in the database&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The semicolon in the above string breaks up the string into two parts. The first part is displayed as a tool-tip on the button itself and the second one will appear in the status-line of the main window.&lt;br /&gt;
&lt;br /&gt;
In a similar fashion; change the Redo button&amp;#039;s constant so that it is now having the value of &amp;#039;&amp;#039;id_query_grandfather&amp;#039;&amp;#039;. And the Help button&amp;#039;s constant should get the value of &amp;#039;&amp;#039;id_query_ancestor_of&amp;#039;&amp;#039;. The status-line of these two buttons also should be suitably modified.&lt;br /&gt;
&lt;br /&gt;
==Getting the Main Code into the Program==&lt;br /&gt;
&lt;br /&gt;
We have finished all the work for all the GUI functionality that we need from the program. Now we shall start inserting the logic of the code. Before we do that, let us understand some important differences between the way programming used to be done and how it would now be done for a GUI program.&lt;br /&gt;
&lt;br /&gt;
When a user uses a GUI program, it is like the user is using a room. The user comes into the room through the main door all right but once inside the room; the user is free to decide which part of the room would be put to use. Undoubtedly, the programmer has the last say on how each of those parts would actually work. But by and large the user is free to decide which part of the program to activate. The &amp;#039;&amp;#039;Goal&amp;#039;&amp;#039; procedure is still present in a GUI program but here all it does is to just set up the stage for the user to work on. It is like the main door to the room in our analogy.&lt;br /&gt;
&lt;br /&gt;
What does this imply for the programmer? The logic of the code earlier was contained inside one module. Now it will be spread over several modules, depending on the logic which is controlled by corresponding GUI components.&lt;br /&gt;
&lt;br /&gt;
Let us add some basic logical code that is needed by the program. Open TaskWindow.pro and locate the editing caret just after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;classInfo(className, classVersion).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
...once the editing caret is there, then insert the following code into that location:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;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 anyflow.&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 anyflow.&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).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code is the logical core of the program. In this tutorial, we would directly be inserting it into the module TaskWindow.pro because we want you to see how the GUI components reach into the logical core to perform various actions. In more complex examples; the logical core would be residing separately, sometimes spread over several modules. The GUI modules (such as &amp;#039;&amp;#039;TaskWindow&amp;#039;&amp;#039;, etc.) would then be referring it to those modules separately by increasing the &amp;#039;&amp;#039;scope&amp;#039;&amp;#039; of the module (discussed in an earlier tutorial).&lt;br /&gt;
&lt;br /&gt;
Actually, it would be good practice to keep the logical core of the program separate from all GUI related modules as far as possible, but in this tutorial we shall knowingly ignore this sound advice.&lt;br /&gt;
&lt;br /&gt;
==Encapsulating the Interactive Code==&lt;br /&gt;
&lt;br /&gt;
Now that the very core logic has been taken care of, we need to insert the interactive bits. In the Project Tree right click on TaskWindow.win entry. This entry represents the Windows resource for that main TaskWindow. All clicks, menu events, etc. happening within that TaskWindow will have to be handled using event handlers written for that window. On right clicking at that selection, a small context menu would open up as shown below. Select Code Expert from that menu.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui17.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Dialog and Window Expert (shown below) helps you set default listeners and responders for controls (interactive GUI components) of a particular window or a dialog. The blue filled circle indicates that there is no listener or responder for the indicated control. The green tick-mark indicates that a listener or responder is present.&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui18.jpg]]&lt;br /&gt;
&lt;br /&gt;
Using the above dialog, ensure that an event handler is set for the menu item represented by the constant &amp;#039;&amp;#039;id_file_open&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Now, in the Project tree click on the TaskWindow.pro module so that it becomes selected, and using the Build menu just compile that module. If the TaskWindow.pro module is not selected then the Compile menu item in the &amp;#039;&amp;#039;Build&amp;#039;&amp;#039; menu would be disabled, so beware!&lt;br /&gt;
&lt;br /&gt;
When Visual Prolog compiles a module, it reorganizes the project tree so that all the pertinent predicates/domains, etc. get laid out cleanly for us to access. Hence, we would be using this feature to ensure that the IDE lists out all the predicates that are declared in the TaskWindow.pro module. We can then navigate to the predicate we want.&lt;br /&gt;
&lt;br /&gt;
Note that the IDE intelligently recognizes that TaskWindow.pro may need some additional modules, and it compiles the module in two passes.&lt;br /&gt;
&lt;br /&gt;
After the compilation is over; when we refer to the TaskWindow predicates, in the project tree you would notice that all the predicates are seen:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui19.jpg]]&lt;br /&gt;
&lt;br /&gt;
You will find that the Predicates portion of the taskwindow.pro module would reveal the onFileOpen predicate -- which was absent earlier.&lt;br /&gt;
&lt;br /&gt;
When you double-click on that predicate, it will open the editor directly at the location of the clause of the onFileOpen predicate. In case there are multiple clauses, then it would take the cursor to the first clause body.&lt;br /&gt;
&lt;br /&gt;
The onFileOpen predicate is called a &amp;#039;&amp;#039;listener&amp;#039;&amp;#039;. As a programmer you need not call this predicate on your own. Windows will automatically call it when the appropriate GUI component is activated (in this case the menu item is clicked)&lt;br /&gt;
&lt;br /&gt;
By default the following code would have been inserted for this event handler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;onFileOpen(_Source, _MenuTag).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace that code with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    onFileOpen(_Source, _MenuTag) :-&lt;br /&gt;
         Filename = vpiCommonDialogs::getFileName(&lt;br /&gt;
              &amp;quot;*.txt&amp;quot;, [&amp;quot;Family data files (*.txt)&amp;quot;,&amp;quot;*.txt&amp;quot;,&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;
         reconsult(Filename),&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;
If you examine the above code, you would notice that we have simply added a clause body above the one given by the IDE. The first clause body will open up the standard Windows dialog box from where the database can be loaded. The second clause body acts as a fail-safe mechanism in case the person cancels that dialog box.&lt;br /&gt;
&lt;br /&gt;
If we now build the program at this stage and run it, you would be able to load a &amp;#039;&amp;#039;family&amp;#039;&amp;#039; database into the program using the File|Open menu item. To test this, you can use the same fa.txt database that was developed for the console version of this program. A message appearing in the Messages window will inform you in case the program was able to load a database successfully.&lt;br /&gt;
&lt;br /&gt;
Note that though the dialog would be asking for &amp;#039;&amp;#039;&amp;quot;&amp;#039;&amp;#039;.txt&amp;#039;&amp;#039;&amp;quot;&amp;#039;&amp;#039; files, they should not be confused with common text files used in computers. The files that are to be used are to be formatted exactly as per our programs requirements. Any other file would result in an error.&lt;br /&gt;
&lt;br /&gt;
If the file gets loaded correctly, then the stdIO::writef(...) predicate is called to indicate that result. A GUI program does not have a regular console but Visual Prolog GUI programs automatically provide a Messages which acts as a stdout console. Hence the stdIO::writef(...) predicate results gets presented in the Messages. (If you had closed the window, then you would not be able to se the result). You can even re-size or zoom the Messages window, to suit your taste.&lt;br /&gt;
&lt;br /&gt;
Now you should go back to the Dialog and Window Expert, and ensure that the listeners for the Query|Father, Query|Grandfather and Query|Ancestor of ... menu items are also set as shown below:&lt;br /&gt;
&lt;br /&gt;
[[Image:FundamentalVisualPrologGui22.jpg]]&lt;br /&gt;
&lt;br /&gt;
Now for the Query|Father menu item, add the following clause body before the default clause body inserted automatically by Visual Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onQueryFather : window::menuItemListener.&lt;br /&gt;
clauses&lt;br /&gt;
    onQueryFather(_Source, _MenuTag):-&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;
    onQueryFather(_Source, _MenuTag).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For the Query|Grandfather... menu item, add the following clause body before the default clause body inserted automatically by Visual Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onQueryGrandfather : window::menuItemListener.&lt;br /&gt;
clauses&lt;br /&gt;
onQueryGrandFather(_Source, _MenuTag) :-&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;
    onQueryGrandFather(_Source, _MenuTag).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Understanding What we have Done So Far==&lt;br /&gt;
&lt;br /&gt;
Let us now pause our tutorial and take a breather (phew!). &amp;quot;Where is the encapsulation?&amp;quot;, - you should ask. Well, we have broken up the code into two parts. There is the non-interactive logical core which we took care of earlier. But those parts which requires inputs from the user, has been squirrelled away in separate portions into &amp;#039;&amp;#039;different&amp;#039;&amp;#039; event handlers. As far as the rest of the program is concerned, it need not be even aware where those event handlers are actually written in the program. Now, let us insert some more interactive code into yet another event handler.&lt;br /&gt;
&lt;br /&gt;
For the Query|Ancestor of ... menu item, add the following clause body before the default clause body inserted automatically by Visual Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onQueryAncestorOf : window::menuItemListener.&lt;br /&gt;
clauses&lt;br /&gt;
    onQueryAncestorOf(_Source, _MenuTag) :-&lt;br /&gt;
     X = ancestorDialog::tryGetName(This),&lt;br /&gt;
          stdIO::writef(&amp;quot;\nancestor of % test\n&amp;quot;, X),&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;
    onQueryAncestorOf(_Source, _MenuTag).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The strategy for the above code is to acquire a string from a modal dialog box presented by the ancestorDialog which we had created earlier. The above predicate assumes that there is a globally accessible predicate called tryGetName available in the &amp;#039;&amp;#039;ancestorDialog&amp;#039;&amp;#039; module which will return the name of the person whose ancestors we are seeking.&lt;br /&gt;
&lt;br /&gt;
As was seen in the onFileOpen Event Handler, we had sought a string (the filename) returned from a modal dialog. The modal dialog itself was invoked by the predicate vpiCommonDialogs::getFileName(...) It is the same strategy that we are adopting for obtaining a string from the ancestorDialog. The only difference is that vpiCommonDialogs::getFileName(...) gave a built-in standard Windows modal file dialog. But for our home grown ancestorDialog, we would have to do some more coding as we shall shortly see.&lt;br /&gt;
&lt;br /&gt;
On compiling the program, there will be one error:&lt;br /&gt;
&lt;br /&gt;
error c229: Undeclared identifier &amp;#039;ancestorDialog::tryGetName/1-&amp;gt;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The reason for this error is because the onQueryAncestorOf predicate is expecting this globally accessible predicate called tryGetName from the module ancestorDialog.pro. But we haven&amp;#039;t written that yet! Let us now get down to rectifying that error.&lt;br /&gt;
&lt;br /&gt;
This predicate is defined within one module, but called from another module. Hence we need to ensure that the declaration is kept not in the .pro l parts of the program. The class declaration file of a module (extension .cl) is one such place. Therefore, let us now open ancestorDialog.cl and insert the following piece of code. (It is a declaration statin called tryGetName is implemented within the module, and that predicate can be called from other modules too.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
trygetName : (window Parent) -&amp;gt; string Name determ.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In ancestorDialog.pro, let us now insert the core logic relevant to that module. Just the way we did for Taskwindow.pro, it is inserted just after the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;classInfo(className, classVersion).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here is the code to be inserted:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
optionalString = none(); one(string Value).&lt;br /&gt;
class facts&lt;br /&gt;
name : optionalString := none().&lt;br /&gt;
clauses&lt;br /&gt;
tryGetName(Parent) = Name :-&lt;br /&gt;
          name := none(),&lt;br /&gt;
          _ = ancestorDialog::display(Parent),&lt;br /&gt;
          one(Name) = name.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now there is one last issue which is remaining. We need to change the event handler for the OK button. This is required, so that the dialog would assert the value entered by the user into the &amp;#039;&amp;#039;name&amp;#039;&amp;#039; class facts. If that is not done, then the above predicate will find an empty string.&lt;br /&gt;
&lt;br /&gt;
The default code given by Visual Prolog is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onOkClick : button::clickResponder.&lt;br /&gt;
clauses&lt;br /&gt;
    onOkClick(_Source) = button::defaultAction.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code now will have to be changed into the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onOkClick : button::clickResponder.&lt;br /&gt;
clauses&lt;br /&gt;
    onOkClick(_Source) = button::defaultAction :-&lt;br /&gt;
        Name = idc_ancestordialog_personname:getText(),&lt;br /&gt;
        name := one(Name).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have finally finished our program. If you now compile and run the program, you should not get any errors.&lt;br /&gt;
&lt;br /&gt;
==Running the Program==&lt;br /&gt;
&lt;br /&gt;
Once you start the program, you would notice that no activity gets performed immediately, the way it was done in the console program which we had written earlier. As explained somewhere before, starting up a GUI program is like entering a room where the user is free to do things in whichever order he/she chooses. The room simply waits for inputs from the user in whichever order the user decides to give.&lt;br /&gt;
&lt;br /&gt;
Analogously, in our little program, we can invoke the Query|Query Father menu without loading any data. It would not yield any results or any error either because our main logic also takes care of the situation where the data is absent. We can invoke the File|Open... menu item at our choice, and load the family database (the same one which was used in the earlier tutorial).&lt;br /&gt;
&lt;br /&gt;
After that, we can test the Query|Query Father, Query|Query Ancestor and Query|Ancestor of... to see the same results that were obtained in the console program. The results would be displayed in the Messages. (Be careful not to close the Messages window, else the results would not get displayed). The advantage of a GUI program would be recognized when we find that we can run our queries any number of times. Also, we are free to load different data at any point in time.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we have gone through the basics of GUI and how a GUI program can easily be developed in Visual Prolog. We found that the IDE (Integrated Development Environment) given by Visual Prolog allows us complete control over all the GUI components we may wish to use. We were able to edit menus, dialogs and toolbars to suit the functionality that we desire.&lt;br /&gt;
&lt;br /&gt;
Then we were able to modularize the Prolog code and insert it into different parts of the program, so that they are safely encapsulated into different listeners and responders. The non-interactive logical core was kept separately.&lt;br /&gt;
&lt;br /&gt;
A GUI program thus developed can then be used in a very flexible manner, with no compulsion to the user regarding the sequence of activities to be performed with the program.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=COM_Memory_Handling_in_Visual_Prolog&amp;diff=448</id>
		<title>COM Memory Handling in Visual Prolog</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=COM_Memory_Handling_in_Visual_Prolog&amp;diff=448"/>
		<updated>2007-10-03T09:17:23Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The [[wikipedia:Component_object_model|Component Object Model]] (COM) implies usage of COM memory. When we use an external COM component or create our own COM component, we should handle parameters correctly.&lt;br /&gt;
&lt;br /&gt;
The IDE generates necessary memory conversions, allocations and releasing on creating of COM package using component&amp;#039;s type library. So normally, you do not have to consider the memory handling. However, sometimes some code is written manually, and then the rules must be known.&lt;br /&gt;
&lt;br /&gt;
The possible situations are:&lt;br /&gt;
&lt;br /&gt;
*Call a COM predicate, which returns a parameter in COM memory;&lt;br /&gt;
&lt;br /&gt;
*Call a COM predicate with input parameter;&lt;br /&gt;
&lt;br /&gt;
*Call a COM predicate with input mutable parameter;&lt;br /&gt;
&lt;br /&gt;
*Implement a COM predicate, which gives us a parameter in COM memory;&lt;br /&gt;
&lt;br /&gt;
*Implement a COM predicate, which returns a parameter in COM memory;&lt;br /&gt;
&lt;br /&gt;
*Implement a COM Predicate. In/Out Parameter;&lt;br /&gt;
&lt;br /&gt;
Please notice that integral parameters do not need special memory handling, but strings and structure domains require such memory handling.&lt;br /&gt;
&lt;br /&gt;
==Call a COM Predicate. Handle Returned Memory==&lt;br /&gt;
&lt;br /&gt;
When we use an external COM component, we can call a predicate, which returns COM memory to us. Usually we want to use the returned information for some time; hence we should copy the returned memory and free the COM memory then. Once the data is copied to Prolog memory it is controlled by the garbage collector.&lt;br /&gt;
&lt;br /&gt;
In the example below, the returned parameter has string type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    p() = Parameter:-&lt;br /&gt;
        HResult = someComPredicate(ComParameter),&lt;br /&gt;
        HResult = s_ok,&lt;br /&gt;
        !,&lt;br /&gt;
        Parameter= string::createCopy(ComParameter),&lt;br /&gt;
        comMemory::taskMemFree(ComParameter).&lt;br /&gt;
    p() = &amp;quot;&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Call a COM Predicate. Fill Input Parameter==&lt;br /&gt;
&lt;br /&gt;
When we use an external COM component, we can call a predicate, which requires our information. We can use any memory for such input parameters. If we use COM memory, then we should free it after the predicate invocation.&lt;br /&gt;
&lt;br /&gt;
In the example below the parameter has myDom type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    myDom = myDom(integer X, string S).&lt;br /&gt;
clauses&lt;br /&gt;
    q(myDom(X, S)) :-&lt;br /&gt;
        HResult = someComPredicate(myDom(X, S)),&lt;br /&gt;
        check(HResult).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The IDE uses COM memory for input parameters when generating COM code. The IDE also generates the code to release the COM memory.&lt;br /&gt;
&lt;br /&gt;
==Call a COM Predicate. Fill Mutable Parameter==&lt;br /&gt;
&lt;br /&gt;
When we use an external COM component, we can call a predicate, which requires our information and which changes it. We should use COM memory for such parameters. After the call we should free the result COM memory.&lt;br /&gt;
&lt;br /&gt;
In the example below, the parameter has myDom type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    myDom = myDom(integer X, string S).&lt;br /&gt;
clauses&lt;br /&gt;
    q(myDom(X, S)) = myDom(NewX, NewS):-&lt;br /&gt;
        ComParameterPtr= comMemory::taskMemAlloc(sizeofdomain(myDom)),&lt;br /&gt;
        ComS= comMemory::allocString(S),&lt;br /&gt;
        memory::copy(ComParameterPtr,&lt;br /&gt;
            uncheckedConvert(pointer,myDom(X, ComS)),&lt;br /&gt;
            sizeofdomain(myDom)),&lt;br /&gt;
        ComParameter= uncheckedConvert(myDom, ComParameterPtr)&lt;br /&gt;
        HResult = someComPredicate(ComParameter),&lt;br /&gt;
        ComParameter= myDom(NewX, NewComS),&lt;br /&gt;
        NewS= string::createCopy(NewComS),&lt;br /&gt;
        comMemory::taskMemFree(NewComS),&lt;br /&gt;
        comMemory::taskMemFree(ComParameter),&lt;br /&gt;
        check(HResult).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please notice that inner fields can be changed, so it is necessary to find new values and release them.&lt;br /&gt;
&lt;br /&gt;
==Implement a COM Predicate. Handle Input Memory==&lt;br /&gt;
&lt;br /&gt;
When we create our own COM component or when we implement a callback predicate, we can receive a parameter in COM memory.&lt;br /&gt;
&lt;br /&gt;
It is important to notice that this memory should &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; be released.&lt;br /&gt;
&lt;br /&gt;
But if we are going to use the information for some time, then we should copy the memory.&lt;br /&gt;
&lt;br /&gt;
In the example below, the parameter has bstr type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    someComPredicate(ComParameter) = s_ok :-&lt;br /&gt;
        trap(somePredicate(basicString::toString(ComParameter)),&lt;br /&gt;
            TraceId, processTraceId(TraceId)),&lt;br /&gt;
        !.&lt;br /&gt;
    someComPredicate(_) = e_fail.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    somePredicate(Parameter) :-&lt;br /&gt;
        %some implementation here&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also notice that Prolog predicates should be trapped, as the COM system cannot process Prolog fail or Prolog exception.&lt;br /&gt;
&lt;br /&gt;
==Implement a COM Predicate. Return Memory==&lt;br /&gt;
&lt;br /&gt;
When we create our own COM component or when we implement a callback predicate, we can be asked about a parameter. Such a parameter must be allocated in COM memory.&lt;br /&gt;
&lt;br /&gt;
So, we should allocate COM memory and fill it with proper values.&lt;br /&gt;
&lt;br /&gt;
In the example below the parameter has myDom type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    myDom = myDom(integer X, string S).&lt;br /&gt;
clauses&lt;br /&gt;
    someComPredicate(ComParameter) = s_ok :-&lt;br /&gt;
        trap(&lt;br /&gt;
            (&lt;br /&gt;
                myDom(X, S) = somePredicate(),&lt;br /&gt;
                ComParameterPtr = comMemory::taskMemAlloc(sizeofdomain(myDom)),&lt;br /&gt;
                ComS= comMemory::allocString(S),&lt;br /&gt;
                memory::copy(ComParameterPtr,&lt;br /&gt;
                    uncheckedConvert(pointer,myDom(X, ComS)),&lt;br /&gt;
                    sizeofdomain(myDom)),&lt;br /&gt;
                ComParameter= uncheckedConvert(myDom, ComParameterPtr)&lt;br /&gt;
            ),&lt;br /&gt;
          TraceId, processTraceId(TraceId)),&lt;br /&gt;
        !.&lt;br /&gt;
    someComPredicate(uncheckedConvert(myDom, null)) = e_fail.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    somePredicate() = Parameter :-&lt;br /&gt;
        %some implementation here&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can notice that inner parameters of structure domains should be handled as well.&lt;br /&gt;
&lt;br /&gt;
Please also notice that Prolog predicates should be trapped, as COM system cannot process Prolog fail or Prolog exception.&lt;br /&gt;
&lt;br /&gt;
==Implement a COM Predicate. In/Out Parameter==&lt;br /&gt;
&lt;br /&gt;
When we create our own COM component or when we implement a callback predicate, we can receive a memory, which can be changed (i.e. [in, out]).&lt;br /&gt;
&lt;br /&gt;
Then we should allocate COM memory, fill it with proper values, and release the replaced COM memory.&lt;br /&gt;
&lt;br /&gt;
In the example below the parameter has myDom type.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    myDom = myDom(integer X, string S).&lt;br /&gt;
clauses&lt;br /&gt;
    someComPredicate(ComParameter) = s_ok :-&lt;br /&gt;
        trap(&lt;br /&gt;
            (&lt;br /&gt;
                myDom(X, ComS)= ComParameter&lt;br /&gt;
                S = string::createCopy(ComS),&lt;br /&gt;
                myDom(NewX, NewS) = somePredicate(myDom(X, S)),&lt;br /&gt;
                ComParameterPtr= uncheckedConvert(pointer, ComParameter),&lt;br /&gt;
                comMemory::taskMemFree(ComS),&lt;br /&gt;
                NewComS = comMemory::allocString(NewS),&lt;br /&gt;
                memory::copy(ComParameterPtr,&lt;br /&gt;
                    uncheckedConvert(pointer,myDom(NewX, NewComS)),&lt;br /&gt;
                    sizeofdomain(myDom))&lt;br /&gt;
            ),&lt;br /&gt;
            TraceId, processTraceId(TraceId)),&lt;br /&gt;
        !.&lt;br /&gt;
    someComPredicate(_) = e_fail.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    somePredicate(Parameter) = NewParameter :-&lt;br /&gt;
        %some implementation here&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please notice the replaced parameter is released after invocation of the Prolog predicate. Hence, an exception in an implementation of somePredicate will not crash the mutable input parameter.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Lists_and_Recursion&amp;diff=447</id>
		<title>Lists and Recursion</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Lists_and_Recursion&amp;diff=447"/>
		<updated>2007-10-02T21:10:03Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: /* Parsing by Difference Lists */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;List processing – handling sequences of elements – is a powerful technique in Prolog. In this tutorial, we explain what lists are and how to declare them, and then give several examples that show how you might use list processing in your own applications. We also define two well known Prolog predicates – member and append – while looking at list processing from both a recursive and a procedural standpoint.&lt;br /&gt;
&lt;br /&gt;
After that, we introduce findall, a Visual Prolog standard predicate that enables you to find and collect all solutions to a single goal. We round out this tutorial with a discussion of compound lists – combinations of different types of elements – and an example of parsing by difference lists.&lt;br /&gt;
&lt;br /&gt;
==What Is a List?==&lt;br /&gt;
&lt;br /&gt;
In Prolog, &amp;#039;&amp;#039;a list&amp;#039;&amp;#039; is an object that contains an arbitrary number of other objects within it. Lists correspond roughly to arrays in other languages, but, unlike an array, a list does not require you to declare how big it will be before you use it.&lt;br /&gt;
&lt;br /&gt;
A list that contains the numbers 1, 2, and 3 is written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[ 1, 2, 3 ]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The order of the elements in this list matters:&lt;br /&gt;
&lt;br /&gt;
*Number &amp;quot;1&amp;quot; is the first element,&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;2&amp;quot; - the second,&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;3&amp;quot; - the third.&lt;br /&gt;
&lt;br /&gt;
The list [ 1, 2, 3 ] is different from the list [ 1, 3, 2 ].&lt;br /&gt;
&lt;br /&gt;
Each item contained in the list is known as an element. To form a list data structure, you separate the elements of a list with commas and then enclose them in square brackets. Here are some examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[&amp;quot;dog&amp;quot;, &amp;quot;cat&amp;quot;, &amp;quot;canary&amp;quot;]&lt;br /&gt;
[&amp;quot;valerie ann&amp;quot;, &amp;quot;jennifer caitlin&amp;quot;, &amp;quot;benjamin thomas&amp;quot;]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same element can be present in the list several times, for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[ 1, 2, 1, 3, 1 ]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Declaring Lists===&lt;br /&gt;
&lt;br /&gt;
To declare the domain for a list of integers, you use the domains declaration, like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    integer_list = integer*.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The asterisk means &amp;quot;list of&amp;quot;; that is, integer* means &amp;quot;list of integers.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Note that the word &amp;#039;&amp;#039;&amp;quot;list&amp;quot;&amp;#039;&amp;#039; has no special meaning in Visual Prolog. You could equally well have called your list domain &amp;#039;&amp;#039;zanzibar&amp;#039;&amp;#039;. It is the asterisk, not the name that signifies a list domain.&lt;br /&gt;
&lt;br /&gt;
The elements in a list can be anything, including other lists. However, all elements in a list must belong to the same domain, and in addition to the declaration of the list domain, there must be a &amp;#039;&amp;#039;&amp;#039;domains&amp;#039;&amp;#039;&amp;#039; declaration for the elements:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    element_list = elements*.&lt;br /&gt;
    elements = ....&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here &amp;#039;&amp;#039;elements&amp;#039;&amp;#039; must be equated to a single domain type (for example, integer, real, or symbol) or to a set of alternatives marked with different functors. Visual Prolog does not allow you to mix standard types in a list. For example, the following declarations would not properly indicate a list made up of integers, reals, and symbols:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;element_list = elements*.&lt;br /&gt;
elements =&lt;br /&gt;
    integer;&lt;br /&gt;
    real;&lt;br /&gt;
    symbol.&lt;br /&gt;
        /* Incorrect */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The way to declare a list made up of integers, reals, and symbols is to define a single domain comprising all three types, with functors to show which type a particular element belongs to. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;element_list = elements*.&lt;br /&gt;
elements =&lt;br /&gt;
    i(integer);&lt;br /&gt;
    r(real);&lt;br /&gt;
    s(symbol).&lt;br /&gt;
        /* the functors are i, r, and s */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(For more information about this, refer to &amp;quot;Compound Lists&amp;quot; later in this tutorial.)&lt;br /&gt;
&lt;br /&gt;
====Heads and Tails====&lt;br /&gt;
&lt;br /&gt;
A list is really a recursive compound object. It consists of two parts: the head, of a list, which is the first element, and the tail, which is a list comprising all the subsequent elements.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;The tail of a list is always a list; the head of a list is an element.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
For example,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;the head of [a, b, c] is a&lt;br /&gt;
the tail of [a, b, c] is [b, c]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What happens when you get down to a one-element list? The answer is that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;the head of [c] is c&lt;br /&gt;
the tail of [c] is []&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;If you take the first element from the tail of a list enough times, you will eventually get down to an empty list (&amp;#039;&amp;#039;&amp;#039;[ ]&amp;#039;&amp;#039;&amp;#039;).&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;The empty list cannot be broken into head and tail.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
This means that, conceptually, lists have a tree structure just like other compound objects. The tree structure of [a, b, c, d] is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;    list&lt;br /&gt;
   /    \&lt;br /&gt;
  a    list&lt;br /&gt;
      /    \&lt;br /&gt;
     b    list&lt;br /&gt;
         /    \&lt;br /&gt;
        c    list&lt;br /&gt;
            /    \&lt;br /&gt;
           d      []&amp;lt;/pre&amp;gt;Further, a one-element list such as [a] is not the same as the element that it contains, because [a] is really the compound data structure shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;    list&lt;br /&gt;
   /    \&lt;br /&gt;
  a     []&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==List Processing==&lt;br /&gt;
&lt;br /&gt;
Prolog provides a way to make a head and a tail of a list explicit. Instead of separating elements with commas, you can separate the head and tail with a vertical bar (|). For instance,&lt;br /&gt;
&lt;br /&gt;
[a, b, c] is equivalent to [a|[b, c]]&lt;br /&gt;
&lt;br /&gt;
and, continuing the process,&lt;br /&gt;
&lt;br /&gt;
[a|[b,c]] is equivalent to [a|[b|[c]]]&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
which is equivalent to [a|[b|[c|[]]]]&lt;br /&gt;
&lt;br /&gt;
You can even use both kinds of separators in the same list, provided the vertical bar is the last separator. So, if you really want to, you can write [a, b, c, d] as [a, b|[c, d]]. Table 1 gives more examples.&amp;#039;&amp;#039;Table 1: Heads and Tails of Lists&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
List Head Tail [&amp;#039;a&amp;#039;, &amp;#039;b&amp;#039;, &amp;#039;c&amp;#039;] &amp;#039;a&amp;#039; [&amp;#039;b&amp;#039;, &amp;#039;c&amp;#039;] [ &amp;#039;a&amp;#039; ] &amp;#039;a&amp;#039; [] /* an empty list */ [ ] undefined undefined [[1, 2, 3], [2, 3, 4], []] [1, 2, 3] [[2, 3, 4], []]Table 2 gives several examples of list unification.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Table 2: Unification of Lists&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
List 1 List 2 Variable Binding [X, Y, Z] [egbert, eats, icecream] X=egbert, Y=eats, Z=icecream] [7] [X | Y] X=7, Y=[] [1, 2, 3, 4] [X, Y | Z] X=1, Y=2, Z=[3,4] [1, 2] [3 | X] fail&lt;br /&gt;
&lt;br /&gt;
==Using Lists==&lt;br /&gt;
&lt;br /&gt;
Because a list is really a recursive compound data structure, you need recursive algorithms to process it. The most basic way to process a list is to work through it, doing something to each element until you reach the end.&lt;br /&gt;
&lt;br /&gt;
An algorithm of this kind usually needs two clauses. One of them says what to do with an ordinary list (one that can be divided into a head and a tail). The other says what to do with an empty list.&lt;br /&gt;
&lt;br /&gt;
===Writing Lists===&lt;br /&gt;
&lt;br /&gt;
For example, if you just want to print out the elements of the list, here is what you do:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    predicates&lt;br /&gt;
        write_a_list : (integer*).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        write_a_list([]).&lt;br /&gt;
            /* If the list is empty, do nothing more. */&lt;br /&gt;
        write_a_list([H|T]):-&lt;br /&gt;
            /* Match the head to H and the tail to T, then... */&lt;br /&gt;
            stdio::write(H),stdio::nl,&lt;br /&gt;
            write_a_list(T).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::write_a_list([1, 2, 3]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here are the two write_a_list clauses described in natural language:&lt;br /&gt;
&lt;br /&gt;
*To write an empty list, do nothing.&lt;br /&gt;
&lt;br /&gt;
*Otherwise, to write a list, write its head (which is a single element), then write its tail (a list).&lt;br /&gt;
&lt;br /&gt;
The first time through, the goal is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::write_a_list([1, 2, 3]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This matches the second clause, with H=1 and T=[2, 3]; this writes 1 and then calls write_a_list recursively with the tail of the list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::write_a_list([2, 3]).&lt;br /&gt;
        /* This is write_a_list(T). */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This recursive call matches the second clause, this time with H=2 and T=[3], so it writes 2 and again calls write_a_list recursively:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::write_a_list([3]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, which clause will this goal match? Recall that, even though the list [3] has only one element; it does have a head and tail; the head is 3 and the tail is []. So, again the goal matches the second clause, with H=3 and T=[]. Hence, 3 is written and write_a_list is called recursively like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::write_a_list([]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you see why this program needs the first clause. The second clause will not match this goal because [] cannot be divided into head and tail. So, if the first clause were not there, the goal would fail. As it is, the first clause matches and the goal succeeds without doing anything further.&lt;br /&gt;
&lt;br /&gt;
===Counting List Elements===&lt;br /&gt;
&lt;br /&gt;
Now consider how you might find out how many elements are in a list. What is the length of a list, anyway? Here is a simple logical definition:&lt;br /&gt;
&lt;br /&gt;
The length of [] is 0.&amp;lt;br /&amp;gt;&lt;br /&gt;
The length of any other list is 1 plus the length of its tail.&lt;br /&gt;
&lt;br /&gt;
Can you implement this? In Prolog it is very easy. It takes just two clauses:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    predicates&lt;br /&gt;
        length_of : (A*, integer) procedure(i,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        length_of([], 0).&lt;br /&gt;
        length_of([_|T], L):-&lt;br /&gt;
            length_of(T, TailLength),&lt;br /&gt;
            L = TailLength + 1.&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::length_of([1, 2, 3], L),&lt;br /&gt;
    stdio::write(L).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Take a look at the second clause first. Crucially, [_|T] will match any nonempty list, binding T to the tail of the list. The value of the head is unimportant; as long as it exists, it can be counted it as one element.&lt;br /&gt;
&lt;br /&gt;
So the goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::length_of([1, 2, 3], L)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will match the second clause, with T=[2, 3]. The next step is to compute the length of T. When this is done (never mind how), TailLength will get the value 2, and the computer can then add 1 to it and bind L to 3.So how is the middle step executed? That step was to find the length of [2, 3] by satisfying the goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::length_of([2, 3], TailLength)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words, length_of calls itself recursively. This goal matches the second clause, binding&lt;br /&gt;
&lt;br /&gt;
*[3] in the goal to T in the clause and&lt;br /&gt;
&lt;br /&gt;
*TailLength in the goal to L in the clause.&lt;br /&gt;
&lt;br /&gt;
Recall that TailLength in the goal will not interfere with TailLengt&amp;#039;&amp;#039;h&amp;#039;&amp;#039; in the clause, because &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;each recursive invocation of a clause has its own set of variables&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
So now the problem is to find the length of [3], which will be 1, and then add 1 to that to get the length of [2, 3], which will be 2. So far, so good.&lt;br /&gt;
&lt;br /&gt;
Likewise, length_of will call itself recursively again to get the length of [3]. The tail of [3] is [], so T is bound to [], and the problem is to get the length of [], then add 1 to it, giving the length of [3].&lt;br /&gt;
&lt;br /&gt;
This time it&amp;#039;s easy. The goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::length_of([], TailLength)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
matches the &amp;#039;&amp;#039;first&amp;#039;&amp;#039; clause, binding TailLength to 0. So now the computer can add 1 to that, giving the length of [3], and return to the calling clause. This, in turn, will add 1 again, giving the length of [2, 3], and return to the clause that called it; this original clause will add 1 again, giving the length of [1, 2, 3].&lt;br /&gt;
&lt;br /&gt;
Confused yet? We hope not. In the following brief illustration we&amp;#039;ll summarize the calls. We&amp;#039;ve used subscripts to indicate that similarly named variables in different clauses – or different invocations of the same clause – are distinct. Please notice that you do not need to implement such predicate in your own code, you can use &amp;#039;&amp;#039;&amp;#039;list::length&amp;#039;&amp;#039;&amp;#039; predicate from PFC.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::length_of([1, 2, 3], L1).&lt;br /&gt;
my::length_of([2, 3], L2).&lt;br /&gt;
my::length_of([3], L3).&lt;br /&gt;
my::length_of([], 0).&lt;br /&gt;
L3 =  0+1 = 1.&lt;br /&gt;
L2 = L3+1 = 2.&lt;br /&gt;
L1 = L2+1 = 3.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Tail Recursion===&lt;br /&gt;
&lt;br /&gt;
You probably noticed that length_of is not, and can&amp;#039;t be, tail-recursive, because the recursive call is not the last step in its clause. Can you create a tail-recursive list-length predicate? Yes, but it will take some effort.&lt;br /&gt;
&lt;br /&gt;
The problem with length_of is that you can&amp;#039;t compute the length of a list until you&amp;#039;ve already computed the length of the tail. It turns out there&amp;#039;s a way around this. You&amp;#039;ll need a list-length predicate with three arguments.&lt;br /&gt;
&lt;br /&gt;
*One is the list, which the computer will whittle away on each call until it eventually becomes empty, just as before.&lt;br /&gt;
&lt;br /&gt;
*Another is a free argument that will ultimately contain the result (the length).&lt;br /&gt;
&lt;br /&gt;
*The third is a counter that starts out as 0 and increments on each call.&lt;br /&gt;
&lt;br /&gt;
When the list is finally empty, you&amp;#039;ll unify the counter with the (up to then) unbound result.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    predicates&lt;br /&gt;
        length_of : (A*, integer, integer)&lt;br /&gt;
            procedure(i,o,i).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        length_of([], Result, Result).&lt;br /&gt;
        length_of([_|T], Result, Counter):-&lt;br /&gt;
            NewCounter = Counter + 1,&lt;br /&gt;
            length_of(T, Result, NewCounter).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::length_of([1, 2, 3], L, 0), /* start with Counter = 0 */&lt;br /&gt;
    stdio::write(&amp;quot; L = &amp;quot;, L).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This version of the length_of predicate is more complicated, and in many ways less logical, than the previous one. We&amp;#039;ve presented it merely to show you that, by devious means, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;you can often find a tail-recursive algorithm for a problem that seems to demand a different type of recursion&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
====Another Example – Modifying the List====&lt;br /&gt;
&lt;br /&gt;
Sometimes you want to take a list and create another list from it. You do this by working through the list element by element, replacing each element with a computed value. For example, here is a program that takes a list of numbers and adds 1 to each of them:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    predicates&lt;br /&gt;
        add1 : (integer*, integer*) procedure(i,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        add1([], []).&lt;br /&gt;
            /* boundary condition */&lt;br /&gt;
        add1([Head|Tail],[Head1|Tail1]):-&lt;br /&gt;
            /* separate the head */&lt;br /&gt;
            /* from the rest of the list */&lt;br /&gt;
            Head1 = Head+1,&lt;br /&gt;
            /* add 1 to the first element */&lt;br /&gt;
            add1(Tail, Tail1).&lt;br /&gt;
            /* call element with the rest of the list */&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::add1([1,2,3,4], NewList),&lt;br /&gt;
    stdio::write(NewList)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To paraphrase this in natural language:&lt;br /&gt;
&lt;br /&gt;
To add 1 to all the elements of the empty list,&amp;lt;br /&amp;gt;&lt;br /&gt;
just produce another empty list.&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
To add 1 to all the elements of any other list,&amp;lt;br /&amp;gt;&lt;br /&gt;
add 1 to the head and make it the head of the result, and then&amp;lt;br /&amp;gt;&lt;br /&gt;
add 1 to each element of the tail and make that the tail of the result.&lt;br /&gt;
&lt;br /&gt;
Load the program, and run the goal with the specified goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;add1([1,2,3,4], NewList).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The goal will return&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code language=&amp;quot;text&amp;quot;&amp;gt;NewList=[2,3,4,5]&lt;br /&gt;
1 Solution&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h6&amp;gt;Tail Recursion Again&amp;lt;/h6&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Is add1 tail-recursive? If you&amp;#039;re accustomed to using Lisp or Pascal, you might think it isn&amp;#039;t, because you think of it as performing the following operations:&lt;br /&gt;
&lt;br /&gt;
*Split the list into &amp;#039;&amp;#039;Head&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Add 1 to &amp;#039;&amp;#039;Head&amp;#039;&amp;#039;, giving &amp;#039;&amp;#039;Head1&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Recursively add 1 to all the elements of &amp;#039;&amp;#039;Tail&amp;#039;&amp;#039;, giving &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Combine &amp;#039;&amp;#039;Head1&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039;, giving the resulting list.&lt;br /&gt;
&lt;br /&gt;
This isn&amp;#039;t tail-recursive, because the recursive call is not the last step.&lt;br /&gt;
&lt;br /&gt;
But – and this is important – &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;that is not how Prolog does it&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. In Visual Prolog, add1 is tail-recursive, because its steps are really the following:&lt;br /&gt;
&lt;br /&gt;
*Bind the head and tail of the original list to &amp;#039;&amp;#039;Head&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Bind the head and tail of the result to &amp;#039;&amp;#039;Head1&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039;. (&amp;#039;&amp;#039;Head1&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039; do not have values yet.)&lt;br /&gt;
&lt;br /&gt;
*Add 1 to &amp;#039;&amp;#039;Head&amp;#039;&amp;#039;, giving &amp;#039;&amp;#039;Head1&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
*Recursively add 1 to all the elements of &amp;#039;&amp;#039;Tail&amp;#039;&amp;#039;, giving &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
When this is done, &amp;#039;&amp;#039;Head1&amp;#039;&amp;#039; and &amp;#039;&amp;#039;Tail1&amp;#039;&amp;#039; are &amp;#039;&amp;#039;already&amp;#039;&amp;#039; the head and tail of the result; there is no separate operation of combining them. So the recursive call really is the last step.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h6&amp;gt;More on Modifying Lists&amp;lt;/h6&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, you don&amp;#039;t actually need to put in a replacement for every element. Here&amp;#039;s a program that scans a list of numbers and copies it, leaving out the negative numbers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
    predicates&lt;br /&gt;
        discard_negatives : (integer*, integer*) procedure(i,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
        discard_negatives([], []).&lt;br /&gt;
        discard_negatives([H|T], ProcessedTail):-&lt;br /&gt;
            H &amp;lt; 0,&lt;br /&gt;
            !,    /* If H is negative, just skip it */&lt;br /&gt;
            discard_negatives(T, ProcessedTail).&lt;br /&gt;
        discard_negatives([H|T], [H|ProcessedTail]):-&lt;br /&gt;
            discard_negatives(T, ProcessedTail).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::discard_negatives ([2, -45, 3, 468], X),&lt;br /&gt;
    stdio::write(X).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, the goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::discard_negatives([2, -45, 3, 468], X)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
gives&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;X=[2, 3, 468].&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And here&amp;#039;s a predicate that copies the elements of a list, making each element occur twice:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;doubletalk([], []).&lt;br /&gt;
doubletalk([H|T], [H, H|DoubledTail]) :-&lt;br /&gt;
    doubletalk(T, DoubledTail).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===List Membership===&lt;br /&gt;
&lt;br /&gt;
Suppose you have a list with the names &amp;#039;&amp;#039;John&amp;#039;&amp;#039;, &amp;#039;&amp;#039;Leonard&amp;#039;&amp;#039;, &amp;#039;&amp;#039;Eric&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;Frank&amp;#039;&amp;#039; and would like to use Visual Prolog to investigate if a given name is in this list. In other words, you must express the relation &amp;quot;membership&amp;quot; between two arguments: a name and a list of names. This corresponds to the predicate&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;isMember : (name, name*).&lt;br /&gt;
    /* &amp;quot;name&amp;quot; is a member of &amp;quot;name*&amp;quot; */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the e01.pro program, the first clause investigates the head of the list. If the head of the list is equal to the name you&amp;#039;re searching for, then you can conclude that Name is a member of the list. Since the tail of the list is of no interest, it is indicated by the anonymous variable. Thanks to this first clause, the goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;my::isMember(&amp;quot;john&amp;quot;, [&amp;quot;john&amp;quot;, &amp;quot;leonard&amp;quot;, &amp;quot;eric&amp;quot;, &amp;quot;frank&amp;quot;])&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is satisfied.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;/* Program e01.pro */&lt;br /&gt;
class my&lt;br /&gt;
    predicates&lt;br /&gt;
       isMember : (A, A*) determ.&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
    clauses&lt;br /&gt;
       isMember(Name, [Name|_]) :-&lt;br /&gt;
          !.&lt;br /&gt;
       isMember(Name, [_|Tail]):-&lt;br /&gt;
          isMember(Name,Tail).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
   console::init(),&lt;br /&gt;
   my::isMember(&amp;quot;john&amp;quot;, [&amp;quot;john&amp;quot;, &amp;quot;leonard&amp;quot;, &amp;quot;eric&amp;quot;, &amp;quot;frank&amp;quot;]),&lt;br /&gt;
   !,&lt;br /&gt;
   stdio::write(&amp;quot;Success&amp;quot;)&lt;br /&gt;
   ;&lt;br /&gt;
   stdio::write(&amp;quot;No solution&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the head of the list is not equal to Name, you need to investigate whether Name can be found in the tail of the list.&lt;br /&gt;
&lt;br /&gt;
In English:&lt;br /&gt;
&lt;br /&gt;
Name is a member of the list if Name is the first element of the list, or&amp;lt;br /&amp;gt;&lt;br /&gt;
Name is a member of the list if Name is a member of the tail.&lt;br /&gt;
&lt;br /&gt;
The second clause of isMember relates to this relationship. In Visual Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;isMember(Name, [_|Tail]) :-&lt;br /&gt;
    isMember(Name, Tail).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Appending One List to Another: Declarative and Procedural Programming===&lt;br /&gt;
&lt;br /&gt;
As given, the member predicate of the e01.pro program works in two ways. Consider its clauses once again:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;member(Name, [Name|_]).&lt;br /&gt;
member(Name, [_|Tail]) :-&lt;br /&gt;
    member(Name, Tail).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can look at these clauses from two different points of view: declarative and procedural.&lt;br /&gt;
&lt;br /&gt;
*From a declarative viewpoint, the clauses say:Name is a member of a list if the head is equal to Name;&amp;lt;br /&amp;gt;&lt;br /&gt;
if not, Name is a member of the list if it is a member of the tail.&lt;br /&gt;
&lt;br /&gt;
*From a procedural viewpoint, the two clauses could be interpreted as saying:To find a member of a list, find its head;&amp;lt;br /&amp;gt;&lt;br /&gt;
otherwise, find a member of its tail.&lt;br /&gt;
&lt;br /&gt;
These two points of view correspond to the goals&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;member(2, [1, 2, 3, 4]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;member(X, [1, 2, 3, 4]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In effect, the first goal asks Visual Prolog to check whether something is true; the second asks Visual Prolog to find all members of the list [1,2,3,4]. Don&amp;#039;t be confused by this. The member predicate is the same in both cases, but its behavior may be viewed from different angles.&lt;br /&gt;
&lt;br /&gt;
====Recursion from a Procedural Viewpoint====&lt;br /&gt;
&lt;br /&gt;
The beauty of Prolog is that, often, when you construct the clauses for a predicate from one point of view, they&amp;#039;ll work from the other. To see this duality, in this next example you&amp;#039;ll construct a predicate to append one list to another. You&amp;#039;ll define the predicate append with three arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;append(List1, List2, List3).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This combines List1 and List2 to form List3. Once again you are using recursion (this time from a procedural point of view).&lt;br /&gt;
&lt;br /&gt;
If List1 is empty, the result of appending &amp;#039;&amp;#039; Lis&amp;#039;&amp;#039;t&amp;#039;&amp;#039;1&amp;#039;&amp;#039; and List2 will be the same as List2. In Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;append([], List2, List2).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If List1 is not empty, you can combine List1 and List2 to form &amp;#039;&amp;#039;List3&amp;#039;&amp;#039; by making the head of List1 the head of List3. (In the following code, the variable H is used as the head of both List1 and List3.) The tail of List3 is L3, which is composed of the rest of List1 (namely, L1) and all of List2. In Prolog:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;append([H|L1], List2, [H|L3]) :-&lt;br /&gt;
   append(L1, List2, L3).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The append predicate operates as follows: While List1 is not empty, the recursive rule transfers one element at a time to List3. When List1 is empty, the first clause ensures that List2 hooks onto the back of List3.&lt;br /&gt;
&lt;br /&gt;
====One Predicate Can Have Different Uses====&lt;br /&gt;
&lt;br /&gt;
Looking at append from a declarative point of view, you have defined a relation between three lists. This relation also holds if List1 and List3 are known but List2 isn&amp;#039;t. However, it also holds true if only List3 is known. For example, to find which two lists could be appended to form a known list, you could use a goal of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;append(L1, L2, [1, 2, 4]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this goal, Visual Prolog will find these solutions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;1L1=[], L2=[1,2,4]&lt;br /&gt;
L1=[1], L2=[2,4]L1=[1,2], L2=[4]L1=[1,2,4], L2=[]4 Solutions&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also use append to find which list you could append to [3,4] to form the list [1,2,3,4]. Try giving the goal&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;append(L1, [3,4], [1,2,3,4]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog finds the solution&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;L1=[1,2].&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This append predicate has defined a relation between an &amp;#039;&amp;#039; input set&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;output set&amp;#039;&amp;#039; in such a way that the relation applies both ways. Given that relation, you can ask&lt;br /&gt;
&lt;br /&gt;
Which output corresponds to this given input?&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
Which input corresponds to this given output?&lt;br /&gt;
&lt;br /&gt;
The status of the arguments to a given predicate when you call that predicate is referred to as a &amp;#039;&amp;#039;flow pattern&amp;#039;&amp;#039;. An argument that is bound or instantiated at the time of the call is an input argument, signified by (i); a free argument is an output argument, signified by (o).&lt;br /&gt;
&lt;br /&gt;
The append predicate has the ability to handle any flow pattern you provide. However, not all predicates have the capability of being called with different flow patterns. When a Prolog clause is able to handle multiple flow patterns, it is known as an invertible clause. Many of list predicate can be found in the &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; class.&lt;br /&gt;
&lt;br /&gt;
==Finding All the Solutions at Once==&lt;br /&gt;
&lt;br /&gt;
Backtracking and recursion are two ways to perform repetitive processes. Recursion won out because, unlike backtracking, it can pass information (through arguments) from one recursive call to the next. Because of this, a recursive procedure can keep track of partial results or counters as it goes along.&lt;br /&gt;
&lt;br /&gt;
But there&amp;#039;s one thing backtracking can do that recursion can&amp;#039;t do – namely, find all the alternative solutions to a goal. So you may find yourself in a quandary: You need all the solutions to a goal, but you need them all at once, as part of a single compound data structure. What do you do?&lt;br /&gt;
&lt;br /&gt;
Fortunately, Visual Prolog provides a way out of this impasse. The built-in construction list comprehension takes a goal as one of its arguments and collects all of the solutions to that goal into an output single list. list comprehension takes two arguments:&lt;br /&gt;
&lt;br /&gt;
*The first argument, VarName, specifies which argument in the specified predicate is to be collected into a list.&lt;br /&gt;
&lt;br /&gt;
*The second, mypredicate, indicates the predicate from which the values will be collected.&lt;br /&gt;
&lt;br /&gt;
*The output ListParam, is a variable that holds the list of values collected through backtracking.&lt;br /&gt;
&lt;br /&gt;
The e02.pro program uses list comprehension to print the average age of a group of people.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;        /* Program e02.pro */&lt;br /&gt;
class my&lt;br /&gt;
domains&lt;br /&gt;
    name = string.&lt;br /&gt;
    address = string.&lt;br /&gt;
    age = integer.&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    person : (name, address, age)&lt;br /&gt;
        nondeterm anyflow.&lt;br /&gt;
    sumlist : (age*, age, integer)&lt;br /&gt;
        procedure(i,o,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
clauses&lt;br /&gt;
    sumlist([],0,0).&lt;br /&gt;
    sumlist([H|T], Sum, N):-&lt;br /&gt;
        sumlist(T, S1, N1),&lt;br /&gt;
        Sum=H+S1, N=1+N1.&lt;br /&gt;
    person(&amp;quot;Sherlock Holmes&amp;quot;,&lt;br /&gt;
        &amp;quot;22B Baker Street&amp;quot;, 42).&lt;br /&gt;
    person(&amp;quot;Pete Spiers&amp;quot;,&lt;br /&gt;
        &amp;quot;Apt. 22, 21st Street&amp;quot;, 36).&lt;br /&gt;
    person(&amp;quot;Mary Darrow&amp;quot;,&lt;br /&gt;
        &amp;quot;Suite 2, Omega Home&amp;quot;, 51).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    L = [ Age || my::person(_, _, Age)],&lt;br /&gt;
    my::sumlist(L, Sum, N),&lt;br /&gt;
    Ave = Sum/N,&lt;br /&gt;
    stdio::write(&amp;quot;Average=&amp;quot;, Ave, &amp;quot;. &amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The list comprehension clause in this program creates a list L, which is a collection of all the ages obtained from the predicate person. If you wanted to collect a list of all the people who are 42 years old, you could give the following subgoal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;List = [ Who || my::person(Who, _, 42) ]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following code will create the list of positive items:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;List = [ X || X = list::getMember_nd([2,-8,-3,6]), X &amp;gt; 0]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Compound Lists==&lt;br /&gt;
&lt;br /&gt;
A list of integers can be simply declared as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;integer*&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same is true for a list of real numbers, a list of symbols, or a list of strings.&lt;br /&gt;
&lt;br /&gt;
However, it is often valuable to store a combination of different types of elements within a list, such as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[2, 3, 5.12, [&amp;quot;food&amp;quot;, &amp;quot;goo&amp;quot;], &amp;quot;new&amp;quot;].&lt;br /&gt;
        /* Not correct Visual Prolog*/&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Compound lists&amp;#039;&amp;#039; are lists that contain more than one type of element. You need special declarations to handle lists of multiple-type elements, because Visual Prolog requires that all elements in a list belong to the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;same&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; domain. The way to create a list in Prolog that stores these different types of elements is to use functors, because a domain can contain &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;more than one&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; data type as arguments to functors.&lt;br /&gt;
&lt;br /&gt;
The following is an example of a domain declaration for a list that can contain an integer, a character, a string, or a list of any of these:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
        /* the functors are l, i, c, and s */&lt;br /&gt;
    llist = l(list); i(integer); c(char); s(string).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The list&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[ 2, 9, [&amp;quot;food&amp;quot;, &amp;quot;goo&amp;quot;], &amp;quot;new&amp;quot; ]&lt;br /&gt;
        /* Not correct Visual Prolog */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
would be written in Visual Prolog as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[i(2), i(9), l([s(&amp;quot;food&amp;quot;), s(&amp;quot;goo&amp;quot;)]), s(&amp;quot;new&amp;quot;)]&lt;br /&gt;
        /* Correct Visual Prolog */&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of append shows how to use this domain declaration in a typical list-manipulation program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class my&lt;br /&gt;
domains&lt;br /&gt;
    llist = l(list); i(integer); c(char); s(string).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    append : (A*,A*,A*) procedure (i,i,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my&lt;br /&gt;
clauses&lt;br /&gt;
    append([], L, L).&lt;br /&gt;
    append([X|L1], L2, [X|L3]):-&lt;br /&gt;
        append(L1, L2, L3).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my::append([my::s(&amp;quot;likes&amp;quot;),&lt;br /&gt;
        my::l([my::s(&amp;quot;bill&amp;quot;), my::s(&amp;quot;mary&amp;quot;)])],&lt;br /&gt;
        [my::s(&amp;quot;bill&amp;quot;), my::s(&amp;quot;sue&amp;quot;)], Ans),&lt;br /&gt;
    stdio::write(&amp;quot;FIRST LIST: &amp;quot;, Ans,&amp;quot;\n\n&amp;quot;),&lt;br /&gt;
    my::append([my::l([my::s(&amp;quot;This&amp;quot;),&lt;br /&gt;
        my::s(&amp;quot;is&amp;quot;),my::s(&amp;quot;a&amp;quot;),my::s(&amp;quot;list&amp;quot;)]),&lt;br /&gt;
        my::s(&amp;quot;bee&amp;quot;)], [my::c(&amp;#039;c&amp;#039;)], Ans2),&lt;br /&gt;
    stdio::write(&amp;quot;SECOND LIST: &amp;quot;, Ans2, &amp;quot;\n\n&amp;amp;quot).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Parsing by Difference Lists===&lt;br /&gt;
&lt;br /&gt;
The ch07e10.pro program demonstrates [[wikipedia:parsing|parsing]] by &amp;#039;&amp;#039;difference lists&amp;#039;&amp;#039;. The process of parsing by difference lists works by reducing the problem; in this example we transform a string of input into a Prolog structure that can be used or evaluated later.&lt;br /&gt;
&lt;br /&gt;
The parser in this example is for a very primitive computer language. Although this example is very advanced for this point in the tutorial, we decided to put it here because parsing is one of the areas where Visual Prolog is very powerful. If you do not feel ready for this topic, you can skip this example and continue reading the tutorial without any loss of continuity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;#include @&amp;quot;pfc\exception\exception.ph&amp;quot;&lt;br /&gt;
#include @&amp;quot;pfc\string\string.ph&amp;quot;&lt;br /&gt;
#include @&amp;quot;pfc\console\console.ph&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class my_t&lt;br /&gt;
    predicates&lt;br /&gt;
        tokl : (string, string*) procedure (i,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my_t&lt;br /&gt;
    clauses&lt;br /&gt;
        tokl(Str, [H|T]) :-&lt;br /&gt;
            string::fronttoken(Str, H, Str1), !,&lt;br /&gt;
            tokl(Str1, T).&lt;br /&gt;
        tokl(_, []).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
/* * * * * * * * * * * * * * * * * *&lt;br /&gt;
* This second part of the program&lt;br /&gt;
* is the parser *&lt;br /&gt;
* * * * * * * * * * * * * * * * * */&lt;br /&gt;
class my_p&lt;br /&gt;
    domains&lt;br /&gt;
        program = program(statement_list).&lt;br /&gt;
        statement_list = statement*.&lt;br /&gt;
        /* * * * * * * * * * * * * * * *&lt;br /&gt;
        * Definition of what constitutes&lt;br /&gt;
        * a statement *&lt;br /&gt;
        * * * * * * * * * * * * * * * */&lt;br /&gt;
        statement =&lt;br /&gt;
            if_Then_Else(exp, statement, statement);&lt;br /&gt;
            if_Then(exp, statement);&lt;br /&gt;
            while(exp, statement);&lt;br /&gt;
            assign(id, exp).&lt;br /&gt;
        /* * * * * * * * * * * * * *&lt;br /&gt;
        * Definition of expression *&lt;br /&gt;
        * * * * * * * *  * * * * * */&lt;br /&gt;
        exp = plus(exp, exp);&lt;br /&gt;
            minus(exp, exp);&lt;br /&gt;
            var(id);&lt;br /&gt;
            int(integer).&lt;br /&gt;
       id = string.&lt;br /&gt;
&lt;br /&gt;
    predicates&lt;br /&gt;
        s_program : (string*, program) procedure (i,o).&lt;br /&gt;
        s_statement : (string*, string*, statement) determ (i,o,o).&lt;br /&gt;
        s_statement_list : (string*, string*, statement_list) determ (i,o,o).&lt;br /&gt;
        s_exp : (string*, string*, exp) determ (i,o,o).&lt;br /&gt;
        s_exp1 : (string*, string*, exp, exp) determ (i,o,i,o).&lt;br /&gt;
        s_exp2 : (string*, string*, exp) determ (i,o,o).&lt;br /&gt;
end class&lt;br /&gt;
&lt;br /&gt;
implement my_p&lt;br /&gt;
    clauses&lt;br /&gt;
        s_program(List1, program(StatementList)):-&lt;br /&gt;
            s_statement_list(List1, _, StatementList),&lt;br /&gt;
            !.&lt;br /&gt;
        s_program(_, program([])).&lt;br /&gt;
&lt;br /&gt;
    clauses&lt;br /&gt;
        s_statement_list([], [], []) :- !.&lt;br /&gt;
        s_statement_list(&lt;br /&gt;
            List1, List4, [Statement|Program]) :-&lt;br /&gt;
            s_statement(List1, List2, Statement),&lt;br /&gt;
            List2=[&amp;quot;;&amp;quot;|List3],&lt;br /&gt;
            s_statement_list(List3, List4, Program).&lt;br /&gt;
        s_statement([&amp;quot;if&amp;quot;|List1], List7,&lt;br /&gt;
            if_then_else(Exp,&lt;br /&gt;
            Statement1, Statement2)):-&lt;br /&gt;
            s_exp(List1, List2, Exp),&lt;br /&gt;
            List2=[&amp;quot;then&amp;quot;|List3],&lt;br /&gt;
            s_statement(List3, List4, Statement1),&lt;br /&gt;
            List4=[&amp;quot;else&amp;quot;|List5],!,&lt;br /&gt;
            s_statement(List5, List6, Statement2),&lt;br /&gt;
            List6=[&amp;quot;fi&amp;quot;|List7].&lt;br /&gt;
        s_statement([&amp;quot;if&amp;quot;|List1], List5,&lt;br /&gt;
            if_then(Exp, Statement)) :- !,&lt;br /&gt;
            s_exp(List1, List2, Exp),&lt;br /&gt;
            List2=[&amp;quot;then&amp;quot;|List3],&lt;br /&gt;
            s_statement(List3, List4, Statement),&lt;br /&gt;
            List4=[&amp;quot;fi&amp;quot;|List5].&lt;br /&gt;
        s_statement([&amp;quot;do&amp;quot;|List1], List4,&lt;br /&gt;
            while(Exp, Statement)) :- !,&lt;br /&gt;
            s_statement(List1, List2, Statement),&lt;br /&gt;
            List2=[&amp;quot;while&amp;quot;|List3],&lt;br /&gt;
            s_exp(List3, List4, Exp).&lt;br /&gt;
        s_statement([ID|List1], List3,&lt;br /&gt;
            assign(Id,Exp)) :-&lt;br /&gt;
            string::isname(ID),&lt;br /&gt;
            List1=[&amp;quot;=&amp;quot;|List2],&lt;br /&gt;
            s_exp(List2, List3, Exp).&lt;br /&gt;
        s_exp(List1, List3, Exp):-&lt;br /&gt;
            s_exp2(List1, List2, Exp1),&lt;br /&gt;
            s_exp1(List2, List3, Exp1, Exp).&lt;br /&gt;
        s_exp1([&amp;quot;+&amp;quot;|List1], List3, Exp1, Exp) :- !,&lt;br /&gt;
            s_exp2(List1, List2, Exp2),&lt;br /&gt;
            s_exp1(List2, List3, plus(Exp1, Exp2), Exp).&lt;br /&gt;
        s_exp1([&amp;quot;-&amp;quot;|List1], List3, Exp1, Exp) :- !,&lt;br /&gt;
            s_exp2(List1, List2, Exp2),&lt;br /&gt;
            s_exp1(List2, List3, minus(Exp1, Exp2), Exp).&lt;br /&gt;
        s_exp1(List, List, Exp, Exp).&lt;br /&gt;
        s_exp2([Int|Rest], Rest, int(I)) :-&lt;br /&gt;
            trap(I = toTerm(Int),Error,&lt;br /&gt;
                exception::clear_fail(Error)),&lt;br /&gt;
            !.&lt;br /&gt;
        s_exp2([Id|Rest], Rest, var(Id)) :-&lt;br /&gt;
            string::isname(Id).&lt;br /&gt;
end implement&lt;br /&gt;
&lt;br /&gt;
goal&lt;br /&gt;
    console::init(),&lt;br /&gt;
    my_t::tokl(&amp;quot;b=2; if b then a=1 else a=2 fi; do a=a-1 while a;&amp;quot;, Ans),&lt;br /&gt;
    stdio::write(Ans),&lt;br /&gt;
    my_p::s_program(Ans, Res),&lt;br /&gt;
    stdio::write(Res).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Load and run this program, then enter the following goal:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    my_t::tokl(&amp;quot;b=2; if b then a=1 else a=2 fi; do a=a-1 while a;&amp;quot;, Ans),&lt;br /&gt;
    my_p::s_program(Ans, Res).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Visual Prolog will return the program structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Ans = [&amp;quot;b&amp;quot;,&amp;quot;=&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;;&amp;quot;,&amp;quot;if&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;then&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;=&amp;quot;,&amp;quot;1&amp;quot;,&lt;br /&gt;
    &amp;quot;else&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;=&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;fi&amp;quot;,&amp;quot;;&amp;quot;,&amp;quot;do&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;=&amp;quot;,&amp;quot;a&amp;quot;,&lt;br /&gt;
    &amp;quot;-&amp;quot;,&amp;quot;1&amp;quot;,&amp;quot;while&amp;quot;,&amp;quot;a&amp;quot;,&amp;quot;;&amp;quot;],&lt;br /&gt;
Res=program([assign(&amp;quot;b&amp;quot;,int(2)),&lt;br /&gt;
    if_then_else(var(&amp;quot;b&amp;quot;),assign(&amp;quot;a&amp;quot;,int(1)), assign(&amp;quot;a&amp;quot;,int(2))),&lt;br /&gt;
    while(var(&amp;quot;a&amp;quot;),assign(&amp;quot;a&amp;quot;,minus(var(&amp;quot;a&amp;quot;),int(1))))&lt;br /&gt;
    ])&lt;br /&gt;
1 Solution&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The transformation in this example is divided into two stages: scanning and parsing. The tokl predicate is the scanner; it accepts a string and converts it into a list of tokens. All the predicates with names beginning in s_ are parser predicates. In this example the input text is a Pascal-like program made up of Pascal-like statements. This programming language only understands certain statements: IF THEN ELSE, IF THEN, DO WHILE, and ASSIGNMENT. Statements are made up of expressions and other statements. Expressions are addition, subtraction, variables, and integers.&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s how this example works:&lt;br /&gt;
&lt;br /&gt;
*The first scanner clause, s_program, takes a list of tokens and tests if it can be transformed into a list of statements.&lt;br /&gt;
&lt;br /&gt;
*The predicate s_statement_list takes this same list of tokens and tests if the tokens can be divided up into individual statements, each ending with a semicolon.&lt;br /&gt;
&lt;br /&gt;
*The predicate s_statement tests if the first tokens of the token list make up a legal statement. If so, the statement is returned in a structure and the remaining tokens are returned back to s_statement_list.&lt;br /&gt;
&lt;br /&gt;
*The four clauses of the s_statement correspond to the four types of statements the parser understands. If the first s_statement clause is unable to transform the list of tokens into an IF THEN ELSE statement, the clause fails and backtracks to the next s_statement clause, which tries to transform the list of tokens into an IF THEN statement. If that clause fails, the next one tries to transform the list of tokens into a DO WHILE statement.&lt;br /&gt;
&lt;br /&gt;
*If the first three s_statement clauses fail, the last clause for that predicate tests if the statement does assignment. This clause tests for assignment by testing if the first term is a symbol, the second term is &amp;quot;=&amp;quot;, and the next terms make up a simple math expression.&lt;br /&gt;
&lt;br /&gt;
*The s_exp, s_exp1, and s_exp2 predicates work the same way, by testing if the first terms are expressions and – if so – returning the remainder of the terms and an expression structure back to s_statement.&lt;br /&gt;
&lt;br /&gt;
==Summary==&lt;br /&gt;
&lt;br /&gt;
These are the important points covered in this tutorial:&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;Lists&amp;#039;&amp;#039; can contain an arbitrary number of elements; you declare them by adding an asterisk at the end of a previously defined domain.&lt;br /&gt;
&lt;br /&gt;
*A list is a recursive compound object that consists of a head and a tail. The head is the first element and the tail is the rest of the list (without the first element). The tail of a list is always a list; the head of a list is an element. A list can contain zero or more elements; the empty list is written [].&lt;br /&gt;
&lt;br /&gt;
*The elements in a list can be anything, including other lists; all elements in a list must belong to the same domain. The domain declaration for the elements must be of this form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
   element_list = elements*.&lt;br /&gt;
   elements = ....&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where elements = one of the standard domains (integer, real, etc.) or a set of alternatives marked with different functors (int(integer); rl(real); smb(symbol); etc.). You can only mix types in a list in Visual Prolog by enclosing them in compound objects/functors.&lt;br /&gt;
&lt;br /&gt;
*You can use separators (commas, [, and |) to make the head and tail of a list explicit; for example, the list&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[a, b, c, d]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
can be written as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;[a|[b, c, d]] or[a, b|[c, d]] or[a, b, c|[d]] or[a|[b|[c, d]]] or[a|[b|[c|[d]]]] or even[a|[b|[c|[d|[]]]]]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*List processing consists of recursively removing the head of the list (and usually doing something with it) until the list is an empty list.&lt;br /&gt;
&lt;br /&gt;
*The list predicates can be found in the &amp;#039;&amp;#039;&amp;#039;list&amp;#039;&amp;#039;&amp;#039; class.&lt;br /&gt;
&lt;br /&gt;
*Visual Prolog provides a built-in construction,  list comprehension, which takes a goal as one of its arguments and collects all of the solutions to that goal into a single list. It has the syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Result = [ Argument || myPredicate(Argument) ]&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Because Visual Prolog requires that all elements in a list belong to the same domain, you use functors to create a list that stores different types of elements.&lt;br /&gt;
&lt;br /&gt;
*The process of &amp;#039;&amp;#039;parsing by difference lists&amp;#039;&amp;#039; works by reducing the problem; the example in this tutorial transforms a string of input into a Prolog structure that can be used or evaluated later.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=446</id>
		<title>Introduction to Classes and Objects</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=446"/>
		<updated>2007-10-02T20:47:20Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: /* Object Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The purpose of the tutorial is to introduce you to the notions of object orientation as met in Visual Prolog, and to give you a quick exemplified introduction to the syntax.&lt;br /&gt;
&lt;br /&gt;
==Object Model==&lt;br /&gt;
&lt;br /&gt;
The main semantic entities in the Visual Prolog [[wikipedia:Object_model|object model]] are [[wikipedia:Object_%28computer_science%29|objects]], object types and classes. The main syntactic notions to deal with these entities are interfaces, class declarations and implementations.&lt;br /&gt;
&lt;br /&gt;
An [[wikipedia:Interface_%28computer_science%29|interface]] is a named set of predicate declarations. Interfaces describe the &amp;quot;interface&amp;quot; of objects (hence the name), i.e. the access that you have to an object from outside the object. Interfaces represent &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object types&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Consider this interface definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface person&lt;br /&gt;
predicates&lt;br /&gt;
    getName : () -&amp;gt; string Name.&lt;br /&gt;
    setName : (string Name).&lt;br /&gt;
end interface person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the definition of an interface named person. All objects of the person type have two predicates getName and  setName, with declarations as stated above.&lt;br /&gt;
&lt;br /&gt;
Interfaces only define types of objects; it is classes that create the objects. A class has a &amp;#039;&amp;#039;declaration&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;implementation&amp;#039;&amp;#039;. A class that &amp;#039;&amp;#039;constructs&amp;#039;&amp;#039; person objects, might be declared like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the declaration of a class named person, which constructs objects of the person type.  The class has a &amp;#039;&amp;#039;constructor&amp;#039;&amp;#039; named new, which given a Name will construct an object (i.e. of the person type).&lt;br /&gt;
&lt;br /&gt;
The class will also need an implementation, which might look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :- name := Name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the implementation of the class named  person. The implementation must provide a definition for each of the public predicates and constructors, i.e. for new, getName and setName.  The implementation can also locally declare and define extra entities, which are only accessible in the implementation itself. In this case, the class declares a &amp;#039;&amp;#039;fact variable&amp;#039;&amp;#039; named name, which is used to store the name of the person.&lt;br /&gt;
&lt;br /&gt;
Each object has its own instance of the fact variable  name. And the code of the clauses above will all reference that particular instance of the fact variable. We say that the predicates are &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and that the fact is an &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
==&amp;#039;&amp;#039;&amp;#039;Class Entities&amp;#039;&amp;#039;&amp;#039;==&lt;br /&gt;
&lt;br /&gt;
A class can also have entities that are shared among all objects of the class. Let us for example extend the example above with code that counts the number of person objects that are created. This count will increase every time an object is created and never decrease.&lt;br /&gt;
&lt;br /&gt;
I will extend the declaration of the class with a predicate (i.e. getCreatedCount) that can return the current count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    getCreatedCount : () -&amp;gt; unsigned Count.&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;class predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in the class declaration, whereas publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in interfaces. This is a rule without exceptions: it is not possible to declare object predicates in a class declaration, and it is not possible to declare class predicates in an interface.&lt;br /&gt;
&lt;br /&gt;
The predicate will need a definition in the implementation of the class; I will also need a fact for storing the count. This fact must be a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; (i.e. shared among all the objects). In the &amp;#039;&amp;#039;&amp;#039; implementation&amp;#039;&amp;#039;&amp;#039; of a class you can declare and define private object entities as well as private class entities. To declare class entities, you prefix the corresponding declaration section with the keyword  class. All in all our class person  can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
class facts&lt;br /&gt;
    createdCount : unsigned := 0.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getCreatedCount() = createdCount.&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :-&lt;br /&gt;
        name := Name,&lt;br /&gt;
        createdCount := createdCount+1.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e. I have added a class fact createdCount, which is initialized to zero. I have also added a clause for the predicate getCreatedCount, which returns the current value of createdCount. Finally, I have added the code in the constructor, which increments the  createdCount.&lt;br /&gt;
&lt;br /&gt;
Notice that in the constructor, two assignments have the same shape, but one updates the object state, whereas the other updates the class state.&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
&lt;br /&gt;
A special variant of classes does not produce objects at all, and therefore they act as &amp;quot;modules&amp;quot; rather than classes. A non-object constructing class (or simply a module) is declared by omitting the object type in the declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class io % no type here&lt;br /&gt;
predicates&lt;br /&gt;
    write : (string ToWrite).&lt;br /&gt;
    write : (unsigned ToWrite).&lt;br /&gt;
end class io&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class that does not produce objects (obviously) cannot contain object entities, neither can it have constructors.&lt;br /&gt;
&lt;br /&gt;
==Creation and Access==&lt;br /&gt;
&lt;br /&gt;
Given the code above I can create a &amp;#039;&amp;#039;goal&amp;#039;&amp;#039; that creates an object and uses the io-class (whose implementation I shall not consider here) to write the name of the person.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    Name = P:getName(),&lt;br /&gt;
    io::write(Name).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the first line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;constructor&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; new in the class  person. The created object is bound to the variable P. In the next line I bind the variable Name to the result of invoking the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate getName on P. In the last line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate write in the class io.&lt;br /&gt;
&lt;br /&gt;
Notice that names in classes are referenced using two colons, e.g. person::new. Also notice that object predicates are referenced using one colon, e.g. P:getName.&lt;br /&gt;
&lt;br /&gt;
Finally, you should notice that constructors are functions that return an object, even though they are not declared like functions: The return type is subsumed from the class declaration.&lt;br /&gt;
&lt;br /&gt;
==Interfaces are Object Types==&lt;br /&gt;
&lt;br /&gt;
As mentioned above interfaces are object types. This should be taken literally, you can use interfaces in the same places where you can use non-object types. For example, in predicate declarations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class mail&lt;br /&gt;
predicates&lt;br /&gt;
    sendMessage : (person Recipient, string Message).&lt;br /&gt;
end class mail&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The predicate mail::sendMessage takes a person and a string as arguments.&lt;br /&gt;
&lt;br /&gt;
==Multiple Implementations==&lt;br /&gt;
&lt;br /&gt;
You can create several completely different classes that all create person objects. You simply declare and implement more classes that construct person objects. The implementation of the classes can be very different, for example I can create a class that stores the person in a database. This is the declaration of such a class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class personInDB : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string DatabaseName, string Name).&lt;br /&gt;
end class personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tutorial is not concerned with actual implementations of this and that.  But I hope the code below shows that objects of a certain object type, can have completely different implementation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement personInDB&lt;br /&gt;
facts&lt;br /&gt;
    db : myDatabase.&lt;br /&gt;
    personID : unsigned.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(DatabaseName, Name) :-&lt;br /&gt;
        db := myDatabase::getDB(DatabaseName),&lt;br /&gt;
        personID := db:storePerson(Name).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = db:getPersonName(personID).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- db:setPersonName(personID, Name).&lt;br /&gt;
end implement personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that not only the internal behavior is completely different, but the internal state also has a completely different structure and contents.&lt;br /&gt;
&lt;br /&gt;
==Subsumption Polymorphism==&lt;br /&gt;
&lt;br /&gt;
Objects of the same type can be used in the same context, no matter how different their implementations are. I can, for example, send a message to a person using the mail class declared above, no matter if that person is constructed by person or personInDB:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P1 = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P1, &amp;quot;Hi John, ...&amp;quot;),&lt;br /&gt;
    P2 = personInDB::new(&amp;quot;Paul&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P2, &amp;quot;Hi Paul, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This behavior is known as subsumption: Objects constructed by one class, are as good as objects constructed by another class in a certain context, as long as both objects have the type required by that context.&lt;br /&gt;
&lt;br /&gt;
You will notice that the predicate  mail::sendMessage can equally well take objects from any person class, so this predicate is polymorphic in a certain sense (Subsumption Polymorphism).&lt;br /&gt;
&lt;br /&gt;
==Supports (Type Extension)==&lt;br /&gt;
&lt;br /&gt;
Let us imagine that my program also deals with a special kind of persons, namely the users of the program. Users are persons with a name, but there is more to them, they also have a password. I want to create a new object type/interface for users, which states that a user is a person, with a password. For this I use the supports qualification:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface user supports person&lt;br /&gt;
predicates&lt;br /&gt;
    trySetPassword : (string Old, string New, string Confirm) determ.&lt;br /&gt;
    validatePassword : (string Password) determ.&lt;br /&gt;
end interface user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is stated that user supports person. This has two effects:&lt;br /&gt;
&lt;br /&gt;
*It means that user objects will have to provide the predicates declared in the person interface (i.e. getName and setName).&lt;br /&gt;
&lt;br /&gt;
*It also means that an object of type  user is also an object of type person, and can therefore also be used in contexts that expect a person object.I.e. if we assume that I have a user class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class user : user&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name, string Password).&lt;br /&gt;
end class user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then objects of that class can be used by mail::sendMessage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = user::new(&amp;quot;Benny&amp;quot;, &amp;quot;MyCatBobby&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P, &amp;quot;Hi Benny, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An interface can support several other interfaces, meaning that:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other types as wellAn interface can also support one or more interfaces, which themselves support one or more interfaces, and so forth. And also in that case:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the indirectly as well as directly supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other indirect as well as direct typesThe supports qualifications generate a subtype hierarchy: we say that user is a subtype of  person.&lt;br /&gt;
&lt;br /&gt;
==Object: the Ultimate Object Super-Type==&lt;br /&gt;
&lt;br /&gt;
An interface that does not explicitly support any interfaces, implicitly supports the interface object.  object is an implicitly defined interface that has no contents (i.e. no predicates). Any object supports the object interface directly or indirectly, so any object has type object. And therefore object is the super type of all object types.&lt;br /&gt;
&lt;br /&gt;
==Inheritance==&lt;br /&gt;
&lt;br /&gt;
When I want to implement the class user, I will, of course, like to take an advantage of one of our person classes. Let us assume that the user class should resemble the class person, except of course that it also deals with the password. I would like our class user to &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherit&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; the implementation of the  person part from class person. This is achieved by the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
    inherits&lt;br /&gt;
    person&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    trySetPassword(Old, New, Confirm) :-&lt;br /&gt;
        validatePassword(Old),&lt;br /&gt;
        New = Confirm,&lt;br /&gt;
        password := New.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    validatePassword(Password) :-&lt;br /&gt;
        password = Password.&lt;br /&gt;
end implement user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This implementation states that it &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherits&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; person, which has the following effects:&lt;br /&gt;
&lt;br /&gt;
*A person object is embedded into each constructed user object.&lt;br /&gt;
&lt;br /&gt;
*All predicates from the person interface can be inherited directly from class person to class user.When you inherit a predicate you do not directly state the implementation of it, instead the implementation from the class that you inherit from is used.&lt;br /&gt;
&lt;br /&gt;
The inheritance can to a certain extend be explained as syntactic sugaring.  At least I could have achieved exactly the same effect with the following code (the clauses for the password predicates remains as above):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
facts&lt;br /&gt;
    person : person.&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person := person_class::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = person:getName().&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- person:setName(Name).&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
end implement user_class&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this code I do not inherit from class person, instead I create a person object and store it in a fact variable. And instead of inheriting the code for getName and setName I have made trivial implementations of these predicates, which simply delegate the task to the object in the fact variable. A section with the keyword delegate can be used as shortcut for such delegation&lt;br /&gt;
&lt;br /&gt;
This code has very much the same effect, but there are some notable differences:&lt;br /&gt;
&lt;br /&gt;
*First of all I have written more code.&lt;br /&gt;
&lt;br /&gt;
*The person object is not embedded in the user object, instead there is a reference to it. (Also here there are two memory allocations instead of one).&lt;br /&gt;
&lt;br /&gt;
*In the last situation I can &amp;#039;&amp;#039;dynamically&amp;#039;&amp;#039; change the value in the fact variable to another object; simply by assigning a new object to the fact variable. For example, to an object of class  personInDB.You should also notice that the second implementation have an extra level of indirection in the call. Visual Prolog handles such indirections rather efficient, but it handles the inheritance even more efficient.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog has multiple inheritance, meaning that you can inherit from many classes simultaneously.&lt;br /&gt;
&lt;br /&gt;
==Final Remarks==&lt;br /&gt;
&lt;br /&gt;
Above I have introduced you to the most fundamental concepts of the object system in Visual Prolog. There are other interesting features in the object system, but these will not be covered here, for example:&lt;br /&gt;
&lt;br /&gt;
*Objects can support further interfaces in the implementation.&lt;br /&gt;
&lt;br /&gt;
*Finalizers that are executed on storage reclaim.&lt;br /&gt;
&lt;br /&gt;
*Object predicate values, a seamless counterpart to C# delegates.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=423</id>
		<title>Introduction to Classes and Objects</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=423"/>
		<updated>2007-09-22T08:59:17Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The purpose of the tutorial is to introduce you to the notions of object orientation as met in Visual Prolog, and to give you a quick exemplified introduction to the syntax.&lt;br /&gt;
&lt;br /&gt;
==Object Model==&lt;br /&gt;
&lt;br /&gt;
The main semantic entities in the Visual Prolog [http://en.wikipedia.org/wiki/Object_model object model] are [http://en.wikipedia.org/wiki/Object_%28computer_science%29 objects], object types and classes. The main syntactic notions to deal with these entities are interfaces, class declarations and implementations.&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interface] is a named set of predicate declarations. Interfaces describe the &amp;quot;interface&amp;quot; of objects (hence the name), i.e. the access that you have to an object from outside the object. Interfaces represent &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object types&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Consider this interface definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface person&lt;br /&gt;
predicates&lt;br /&gt;
    getName : () -&amp;gt; string Name.&lt;br /&gt;
    setName : (string Name).&lt;br /&gt;
end interface person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the definition of an interface named person. All objects of the person type have two predicates getName and  setName, with declarations as stated above.&lt;br /&gt;
&lt;br /&gt;
Interfaces only define types of objects; it is classes that create the objects. A class has a &amp;#039;&amp;#039;declaration&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;implementation&amp;#039;&amp;#039;. A class that &amp;#039;&amp;#039;constructs&amp;#039;&amp;#039; person objects, might be declared like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the declaration of a class named person, which constructs objects of the person type.  The class has a &amp;#039;&amp;#039;constructor&amp;#039;&amp;#039; named new, which given a Name will construct an object (i.e. of the person type).&lt;br /&gt;
&lt;br /&gt;
The class will also need an implementation, which might look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :- name := Name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the implementation of the class named  person. The implementation must provide a definition for each of the public predicates and constructors, i.e. for new, getName and setName.  The implementation can also locally declare and define extra entities, which are only accessible in the implementation itself. In this case, the class declares a &amp;#039;&amp;#039;fact variable&amp;#039;&amp;#039; named name, which is used to store the name of the person.&lt;br /&gt;
&lt;br /&gt;
Each object has its own instance of the fact variable  name. And the code of the clauses above will all reference that particular instance of the fact variable. We say that the predicates are &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and that the fact is an &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
==&amp;#039;&amp;#039;&amp;#039;Class Entities&amp;#039;&amp;#039;&amp;#039;==&lt;br /&gt;
&lt;br /&gt;
A class can also have entities that are shared among all objects of the class. Let us for example extend the example above with code that counts the number of person objects that are created. This count will increase every time an object is created and never decrease.&lt;br /&gt;
&lt;br /&gt;
I will extend the declaration of the class with a predicate (i.e. getCreatedCount) that can return the current count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    getCreatedCount : () -&amp;gt; unsigned Count.&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;class predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in the class declaration, whereas publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in interfaces. This is a rule without exceptions: it is not possible to declare object predicates in a class declaration, and it is not possible to declare class predicates in an interface.&lt;br /&gt;
&lt;br /&gt;
The predicate will need a definition in the implementation of the class; I will also need a fact for storing the count. This fact must be a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; (i.e. shared among all the objects). In the &amp;#039;&amp;#039;&amp;#039; implementation&amp;#039;&amp;#039;&amp;#039; of a class you can declare and define private object entities as well as private class entities. To declare class entities, you prefix the corresponding declaration section with the keyword  class. All in all our class person  can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
class facts&lt;br /&gt;
    createdCount : unsigned := 0.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getCreatedCount() = createdCount.&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :-&lt;br /&gt;
        name := Name,&lt;br /&gt;
        createdCount := createdCount+1.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e. I have added a class fact createdCount, which is initialized to zero. I have also added a clause for the predicate getCreatedCount, which returns the current value of createdCount. Finally, I have added the code in the constructor, which increments the  createdCount.&lt;br /&gt;
&lt;br /&gt;
Notice that in the constructor, two assignments have the same shape, but one updates the object state, whereas the other updates the class state.&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
&lt;br /&gt;
A special variant of classes does not produce objects at all, and therefore they act as &amp;quot;modules&amp;quot; rather than classes. A non-object constructing class (or simply a module) is declared by omitting the object type in the declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class io % no type here&lt;br /&gt;
predicates&lt;br /&gt;
    write : (string ToWrite).&lt;br /&gt;
    write : (unsigned ToWrite).&lt;br /&gt;
end class io&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class that does not produce objects (obviously) cannot contain object entities, neither can it have constructors.&lt;br /&gt;
&lt;br /&gt;
==Creation and Access==&lt;br /&gt;
&lt;br /&gt;
Given the code above I can create a &amp;#039;&amp;#039;goal&amp;#039;&amp;#039; that creates an object and uses the io-class (whose implementation I shall not consider here) to write the name of the person.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    Name = P:getName(),&lt;br /&gt;
    io::write(Name).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the first line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;constructor&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; new in the class  person. The created object is bound to the variable P. In the next line I bind the variable Name to the result of invoking the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate getName on P. In the last line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate write in the class io.&lt;br /&gt;
&lt;br /&gt;
Notice that names in classes are referenced using two colons, e.g. person::new. Also notice that object predicates are referenced using one colon, e.g. P:getName.&lt;br /&gt;
&lt;br /&gt;
Finally, you should notice that constructors are functions that return an object, even though they are not declared like functions: The return type is subsumed from the class declaration.&lt;br /&gt;
&lt;br /&gt;
==Interfaces are Object Types==&lt;br /&gt;
&lt;br /&gt;
As mentioned above interfaces are object types. This should be taken literally, you can use interfaces in the same places where you can use non-object types. For example, in predicate declarations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class mail&lt;br /&gt;
predicates&lt;br /&gt;
    sendMessage : (person Recipient, string Message).&lt;br /&gt;
end class mail&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The predicate mail::sendMessage takes a person and a string as arguments.&lt;br /&gt;
&lt;br /&gt;
==Multiple Implementations==&lt;br /&gt;
&lt;br /&gt;
You can create several completely different classes that all create person objects. You simply declare and implement more classes that construct person objects. The implementation of the classes can be very different, for example I can create a class that stores the person in a database. This is the declaration of such a class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class personInDB : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string DatabaseName, string Name).&lt;br /&gt;
end class personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tutorial is not concerned with actual implementations of this and that.  But I hope the code below shows that objects of a certain object type, can have completely different implementation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement personInDB&lt;br /&gt;
facts&lt;br /&gt;
    db : myDatabase.&lt;br /&gt;
    personID : unsigned.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(DatabaseName, Name) :-&lt;br /&gt;
        db := myDatabase::getDB(DatabaseName),&lt;br /&gt;
        personID := db:storePerson(Name).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = db:getPersonName(personID).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- db:setPersonName(personID, Name).&lt;br /&gt;
end implement personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that not only the internal behavior is completely different, but the internal state also has a completely different structure and contents.&lt;br /&gt;
&lt;br /&gt;
==Subsumption Polymorphism==&lt;br /&gt;
&lt;br /&gt;
Objects of the same type can be used in the same context, no matter how different their implementations are. I can, for example, send a message to a person using the mail class declared above, no matter if that person is constructed by person or personInDB:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P1 = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P1, &amp;quot;Hi John, ...&amp;quot;),&lt;br /&gt;
    P2 = personInDB::new(&amp;quot;Paul&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P2, &amp;quot;Hi Paul, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This behavior is known as subsumption: Objects constructed by one class, are as good as objects constructed by another class in a certain context, as long as both objects have the type required by that context.&lt;br /&gt;
&lt;br /&gt;
You will notice that the predicate  mail::sendMessage can equally well take objects from any person class, so this predicate is polymorphic in a certain sense (Subsumption Polymorphism).&lt;br /&gt;
&lt;br /&gt;
==Supports (Type Extension)==&lt;br /&gt;
&lt;br /&gt;
Let us imagine that my program also deals with a special kind of persons, namely the users of the program. Users are persons with a name, but there is more to them, they also have a password. I want to create a new object type/interface for users, which states that a user is a person, with a password. For this I use the supports qualification:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface user supports person&lt;br /&gt;
predicates&lt;br /&gt;
    trySetPassword : (string Old, string New, string Confirm) determ.&lt;br /&gt;
    validatePassword : (string Password) determ.&lt;br /&gt;
end interface user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is stated that user supports person. This has two effects:&lt;br /&gt;
&lt;br /&gt;
*It means that user objects will have to provide the predicates declared in the person interface (i.e. getName and setName).&lt;br /&gt;
&lt;br /&gt;
*It also means that an object of type  user is also an object of type person, and can therefore also be used in contexts that expect a person object.I.e. if we assume that I have a user class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class user : user&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name, string Password).&lt;br /&gt;
end class user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then objects of that class can be used by mail::sendMessage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = user::new(&amp;quot;Benny&amp;quot;, &amp;quot;MyCatBobby&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P, &amp;quot;Hi Benny, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An interface can support several other interfaces, meaning that:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other types as wellAn interface can also support one or more interfaces, which themselves support one or more interfaces, and so forth. And also in that case:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the indirectly as well as directly supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other indirect as well as direct typesThe supports qualifications generate a subtype hierarchy: we say that user is a subtype of  person.&lt;br /&gt;
&lt;br /&gt;
==Object: the Ultimate Object Super-Type==&lt;br /&gt;
&lt;br /&gt;
An interface that does not explicitly support any interfaces, implicitly supports the interface object.  object is an implicitly defined interface that has no contents (i.e. no predicates). Any object supports the object interface directly or indirectly, so any object has type object. And therefore object is the super type of all object types.&lt;br /&gt;
&lt;br /&gt;
==Inheritance==&lt;br /&gt;
&lt;br /&gt;
When I want to implement the class user, I will, of course, like to take an advantage of one of our person classes. Let us assume that the user class should resemble the class person, except of course that it also deals with the password. I would like our class user to &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherit&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; the implementation of the  person part from class person. This is achieved by the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
    inherits&lt;br /&gt;
    person&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    trySetPassword(Old, New, Confirm) :-&lt;br /&gt;
        validatePassword(Old),&lt;br /&gt;
        New = Confirm,&lt;br /&gt;
        password := New.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    validatePassword(Password) :-&lt;br /&gt;
        password = Password.&lt;br /&gt;
end implement user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This implementation states that it &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherits&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; person, which has the following effects:&lt;br /&gt;
&lt;br /&gt;
*A person object is embedded into each constructed user object.&lt;br /&gt;
&lt;br /&gt;
*All predicates from the person interface can be inherited directly from class person to class user.When you inherit a predicate you do not directly state the implementation of it, instead the implementation from the class that you inherit from is used.&lt;br /&gt;
&lt;br /&gt;
The inheritance can to a certain extend be explained as syntactic sugaring.  At least I could have achieved exactly the same effect with the following code (the clauses for the password predicates remains as above):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
facts&lt;br /&gt;
    person : person.&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person := person_class::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = person:getName().&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- person:setName(Name).&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
end implement user_class&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this code I do not inherit from class person, instead I create a person object and store it in a fact variable. And instead of inheriting the code for getName and setName I have made trivial implementations of these predicates, which simply delegate the task to the object in the fact variable. A section with the keyword delegate can be used as shortcut for such delegation&lt;br /&gt;
&lt;br /&gt;
This code has very much the same effect, but there are some notable differences:&lt;br /&gt;
&lt;br /&gt;
*First of all I have written more code.&lt;br /&gt;
&lt;br /&gt;
*The person object is not embedded in the user object, instead there is a reference to it. (Also here there are two memory allocations instead of one).&lt;br /&gt;
&lt;br /&gt;
*In the last situation I can &amp;#039;&amp;#039;dynamically&amp;#039;&amp;#039; change the value in the fact variable to another object; simply by assigning a new object to the fact variable. For example, to an object of class  personInDB.You should also notice that the second implementation have an extra level of indirection in the call. Visual Prolog handles such indirections rather efficient, but it handles the inheritance even more efficient.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog has multiple inheritance, meaning that you can inherit from many classes simultaneously.&lt;br /&gt;
&lt;br /&gt;
==Final Remarks==&lt;br /&gt;
&lt;br /&gt;
Above I have introduced you to the most fundamental concepts of the object system in Visual Prolog. There are other interesting features in the object system, but these will not be covered here, for example:&lt;br /&gt;
&lt;br /&gt;
*Objects can support further interfaces in the implementation.&lt;br /&gt;
&lt;br /&gt;
*Finalizers that are executed on storage reclaim.&lt;br /&gt;
&lt;br /&gt;
*Object predicate values, a seamless counterpart to C# delegates.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=422</id>
		<title>Introduction to Classes and Objects</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=422"/>
		<updated>2007-09-22T08:56:50Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: Added a couple of external links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The purpose of the tutorial is to introduce you to the notions of object orientation as met in Visual Prolog, and to give you a quick exemplified introduction to the syntax.&lt;br /&gt;
&lt;br /&gt;
==Object Model==&lt;br /&gt;
&lt;br /&gt;
The main semantic entities in the Visual Prolog [http://en.wikipedia.org/wiki/Object_model object model] are [objects http://en.wikipedia.org/wiki/Object_%28computer_science%29], object types and classes. The main syntactic notions to deal with these entities are interfaces, class declarations and implementations.&lt;br /&gt;
&lt;br /&gt;
An interface is a named set of predicate declarations. Interfaces describe the &amp;quot;interface&amp;quot; of objects (hence the name), i.e. the access that you have to an object from outside the object. Interfaces represent &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object types&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Consider this interface definition:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface person&lt;br /&gt;
predicates&lt;br /&gt;
    getName : () -&amp;gt; string Name.&lt;br /&gt;
    setName : (string Name).&lt;br /&gt;
end interface person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the definition of an interface named person. All objects of the person type have two predicates getName and  setName, with declarations as stated above.&lt;br /&gt;
&lt;br /&gt;
Interfaces only define types of objects; it is classes that create the objects. A class has a &amp;#039;&amp;#039;declaration&amp;#039;&amp;#039; and an &amp;#039;&amp;#039;implementation&amp;#039;&amp;#039;. A class that &amp;#039;&amp;#039;constructs&amp;#039;&amp;#039; person objects, might be declared like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the declaration of a class named person, which constructs objects of the person type.  The class has a &amp;#039;&amp;#039;constructor&amp;#039;&amp;#039; named new, which given a Name will construct an object (i.e. of the person type).&lt;br /&gt;
&lt;br /&gt;
The class will also need an implementation, which might look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :- name := Name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is the implementation of the class named  person. The implementation must provide a definition for each of the public predicates and constructors, i.e. for new, getName and setName.  The implementation can also locally declare and define extra entities, which are only accessible in the implementation itself. In this case, the class declares a &amp;#039;&amp;#039;fact variable&amp;#039;&amp;#039; named name, which is used to store the name of the person.&lt;br /&gt;
&lt;br /&gt;
Each object has its own instance of the fact variable  name. And the code of the clauses above will all reference that particular instance of the fact variable. We say that the predicates are &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and that the fact is an &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
==&amp;#039;&amp;#039;&amp;#039;Class Entities&amp;#039;&amp;#039;&amp;#039;==&lt;br /&gt;
&lt;br /&gt;
A class can also have entities that are shared among all objects of the class. Let us for example extend the example above with code that counts the number of person objects that are created. This count will increase every time an object is created and never decrease.&lt;br /&gt;
&lt;br /&gt;
I will extend the declaration of the class with a predicate (i.e. getCreatedCount) that can return the current count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class person : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name).&lt;br /&gt;
&lt;br /&gt;
predicates&lt;br /&gt;
    getCreatedCount : () -&amp;gt; unsigned Count.&lt;br /&gt;
end class person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;class predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in the class declaration, whereas publicly accessible &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;object predicates&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; are declared in interfaces. This is a rule without exceptions: it is not possible to declare object predicates in a class declaration, and it is not possible to declare class predicates in an interface.&lt;br /&gt;
&lt;br /&gt;
The predicate will need a definition in the implementation of the class; I will also need a fact for storing the count. This fact must be a &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class fact&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; (i.e. shared among all the objects). In the &amp;#039;&amp;#039;&amp;#039; implementation&amp;#039;&amp;#039;&amp;#039; of a class you can declare and define private object entities as well as private class entities. To declare class entities, you prefix the corresponding declaration section with the keyword  class. All in all our class person  can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement person&lt;br /&gt;
class facts&lt;br /&gt;
    createdCount : unsigned := 0.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getCreatedCount() = createdCount.&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    name : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name) :-&lt;br /&gt;
        name := Name,&lt;br /&gt;
        createdCount := createdCount+1.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = name.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- name := Name.&lt;br /&gt;
end implement person&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e. I have added a class fact createdCount, which is initialized to zero. I have also added a clause for the predicate getCreatedCount, which returns the current value of createdCount. Finally, I have added the code in the constructor, which increments the  createdCount.&lt;br /&gt;
&lt;br /&gt;
Notice that in the constructor, two assignments have the same shape, but one updates the object state, whereas the other updates the class state.&lt;br /&gt;
&lt;br /&gt;
==Modules==&lt;br /&gt;
&lt;br /&gt;
A special variant of classes does not produce objects at all, and therefore they act as &amp;quot;modules&amp;quot; rather than classes. A non-object constructing class (or simply a module) is declared by omitting the object type in the declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class io % no type here&lt;br /&gt;
predicates&lt;br /&gt;
    write : (string ToWrite).&lt;br /&gt;
    write : (unsigned ToWrite).&lt;br /&gt;
end class io&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class that does not produce objects (obviously) cannot contain object entities, neither can it have constructors.&lt;br /&gt;
&lt;br /&gt;
==Creation and Access==&lt;br /&gt;
&lt;br /&gt;
Given the code above I can create a &amp;#039;&amp;#039;goal&amp;#039;&amp;#039; that creates an object and uses the io-class (whose implementation I shall not consider here) to write the name of the person.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    Name = P:getName(),&lt;br /&gt;
    io::write(Name).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the first line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;constructor&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; new in the class  person. The created object is bound to the variable P. In the next line I bind the variable Name to the result of invoking the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; object&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate getName on P. In the last line I call the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; class&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; predicate write in the class io.&lt;br /&gt;
&lt;br /&gt;
Notice that names in classes are referenced using two colons, e.g. person::new. Also notice that object predicates are referenced using one colon, e.g. P:getName.&lt;br /&gt;
&lt;br /&gt;
Finally, you should notice that constructors are functions that return an object, even though they are not declared like functions: The return type is subsumed from the class declaration.&lt;br /&gt;
&lt;br /&gt;
==Interfaces are Object Types==&lt;br /&gt;
&lt;br /&gt;
As mentioned above interfaces are object types. This should be taken literally, you can use interfaces in the same places where you can use non-object types. For example, in predicate declarations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class mail&lt;br /&gt;
predicates&lt;br /&gt;
    sendMessage : (person Recipient, string Message).&lt;br /&gt;
end class mail&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The predicate mail::sendMessage takes a person and a string as arguments.&lt;br /&gt;
&lt;br /&gt;
==Multiple Implementations==&lt;br /&gt;
&lt;br /&gt;
You can create several completely different classes that all create person objects. You simply declare and implement more classes that construct person objects. The implementation of the classes can be very different, for example I can create a class that stores the person in a database. This is the declaration of such a class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class personInDB : person&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string DatabaseName, string Name).&lt;br /&gt;
end class personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tutorial is not concerned with actual implementations of this and that.  But I hope the code below shows that objects of a certain object type, can have completely different implementation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement personInDB&lt;br /&gt;
facts&lt;br /&gt;
    db : myDatabase.&lt;br /&gt;
    personID : unsigned.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(DatabaseName, Name) :-&lt;br /&gt;
        db := myDatabase::getDB(DatabaseName),&lt;br /&gt;
        personID := db:storePerson(Name).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = db:getPersonName(personID).&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- db:setPersonName(personID, Name).&lt;br /&gt;
end implement personInDB&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that not only the internal behavior is completely different, but the internal state also has a completely different structure and contents.&lt;br /&gt;
&lt;br /&gt;
==Subsumption Polymorphism==&lt;br /&gt;
&lt;br /&gt;
Objects of the same type can be used in the same context, no matter how different their implementations are. I can, for example, send a message to a person using the mail class declared above, no matter if that person is constructed by person or personInDB:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P1 = person::new(&amp;quot;John&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P1, &amp;quot;Hi John, ...&amp;quot;),&lt;br /&gt;
    P2 = personInDB::new(&amp;quot;Paul&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P2, &amp;quot;Hi Paul, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This behavior is known as subsumption: Objects constructed by one class, are as good as objects constructed by another class in a certain context, as long as both objects have the type required by that context.&lt;br /&gt;
&lt;br /&gt;
You will notice that the predicate  mail::sendMessage can equally well take objects from any person class, so this predicate is polymorphic in a certain sense (Subsumption Polymorphism).&lt;br /&gt;
&lt;br /&gt;
==Supports (Type Extension)==&lt;br /&gt;
&lt;br /&gt;
Let us imagine that my program also deals with a special kind of persons, namely the users of the program. Users are persons with a name, but there is more to them, they also have a password. I want to create a new object type/interface for users, which states that a user is a person, with a password. For this I use the supports qualification:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;interface user supports person&lt;br /&gt;
predicates&lt;br /&gt;
    trySetPassword : (string Old, string New, string Confirm) determ.&lt;br /&gt;
    validatePassword : (string Password) determ.&lt;br /&gt;
end interface user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is stated that user supports person. This has two effects:&lt;br /&gt;
&lt;br /&gt;
*It means that user objects will have to provide the predicates declared in the person interface (i.e. getName and setName).&lt;br /&gt;
&lt;br /&gt;
*It also means that an object of type  user is also an object of type person, and can therefore also be used in contexts that expect a person object.I.e. if we assume that I have a user class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class user : user&lt;br /&gt;
constructors&lt;br /&gt;
    new : (string Name, string Password).&lt;br /&gt;
end class user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then objects of that class can be used by mail::sendMessage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;goal&lt;br /&gt;
    P = user::new(&amp;quot;Benny&amp;quot;, &amp;quot;MyCatBobby&amp;quot;),&lt;br /&gt;
    mail::sendMessage(P, &amp;quot;Hi Benny, ...&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An interface can support several other interfaces, meaning that:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other types as wellAn interface can also support one or more interfaces, which themselves support one or more interfaces, and so forth. And also in that case:&lt;br /&gt;
&lt;br /&gt;
*the objects of that type must provide all the predicates in the indirectly as well as directly supported interfaces&lt;br /&gt;
&lt;br /&gt;
*the objects of that type have all the other indirect as well as direct typesThe supports qualifications generate a subtype hierarchy: we say that user is a subtype of  person.&lt;br /&gt;
&lt;br /&gt;
==Object: the Ultimate Object Super-Type==&lt;br /&gt;
&lt;br /&gt;
An interface that does not explicitly support any interfaces, implicitly supports the interface object.  object is an implicitly defined interface that has no contents (i.e. no predicates). Any object supports the object interface directly or indirectly, so any object has type object. And therefore object is the super type of all object types.&lt;br /&gt;
&lt;br /&gt;
==Inheritance==&lt;br /&gt;
&lt;br /&gt;
When I want to implement the class user, I will, of course, like to take an advantage of one of our person classes. Let us assume that the user class should resemble the class person, except of course that it also deals with the password. I would like our class user to &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherit&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; the implementation of the  person part from class person. This is achieved by the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
    inherits&lt;br /&gt;
    person&lt;br /&gt;
&lt;br /&gt;
facts&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    trySetPassword(Old, New, Confirm) :-&lt;br /&gt;
        validatePassword(Old),&lt;br /&gt;
        New = Confirm,&lt;br /&gt;
        password := New.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    validatePassword(Password) :-&lt;br /&gt;
        password = Password.&lt;br /&gt;
end implement user&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This implementation states that it &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inherits&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; person, which has the following effects:&lt;br /&gt;
&lt;br /&gt;
*A person object is embedded into each constructed user object.&lt;br /&gt;
&lt;br /&gt;
*All predicates from the person interface can be inherited directly from class person to class user.When you inherit a predicate you do not directly state the implementation of it, instead the implementation from the class that you inherit from is used.&lt;br /&gt;
&lt;br /&gt;
The inheritance can to a certain extend be explained as syntactic sugaring.  At least I could have achieved exactly the same effect with the following code (the clauses for the password predicates remains as above):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement user&lt;br /&gt;
facts&lt;br /&gt;
    person : person.&lt;br /&gt;
    password : string.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    new(Name, Password) :-&lt;br /&gt;
        person := person_class::new(Name),&lt;br /&gt;
        password := Password.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    getName() = person:getName().&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    setName(Name) :- person:setName(Name).&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
end implement user_class&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this code I do not inherit from class person, instead I create a person object and store it in a fact variable. And instead of inheriting the code for getName and setName I have made trivial implementations of these predicates, which simply delegate the task to the object in the fact variable. A section with the keyword delegate can be used as shortcut for such delegation&lt;br /&gt;
&lt;br /&gt;
This code has very much the same effect, but there are some notable differences:&lt;br /&gt;
&lt;br /&gt;
*First of all I have written more code.&lt;br /&gt;
&lt;br /&gt;
*The person object is not embedded in the user object, instead there is a reference to it. (Also here there are two memory allocations instead of one).&lt;br /&gt;
&lt;br /&gt;
*In the last situation I can &amp;#039;&amp;#039;dynamically&amp;#039;&amp;#039; change the value in the fact variable to another object; simply by assigning a new object to the fact variable. For example, to an object of class  personInDB.You should also notice that the second implementation have an extra level of indirection in the call. Visual Prolog handles such indirections rather efficient, but it handles the inheritance even more efficient.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog has multiple inheritance, meaning that you can inherit from many classes simultaneously.&lt;br /&gt;
&lt;br /&gt;
==Final Remarks==&lt;br /&gt;
&lt;br /&gt;
Above I have introduced you to the most fundamental concepts of the object system in Visual Prolog. There are other interesting features in the object system, but these will not be covered here, for example:&lt;br /&gt;
&lt;br /&gt;
*Objects can support further interfaces in the implementation.&lt;br /&gt;
&lt;br /&gt;
*Finalizers that are executed on storage reclaim.&lt;br /&gt;
&lt;br /&gt;
*Object predicate values, a seamless counterpart to C# delegates.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Turbo_Prolog&amp;diff=418</id>
		<title>Turbo Prolog</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Turbo_Prolog&amp;diff=418"/>
		<updated>2007-09-12T19:33:33Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: added an explanation an explanation for example codes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Turbo Prolog was marketed in the 80&amp;#039;s by Borland. Turbo Prolog was created by the founders of PDC.&lt;br /&gt;
&lt;br /&gt;
==Turbo Prolog example codes / programs in internet==&lt;br /&gt;
&lt;br /&gt;
Turbo Prolog code can with relative little work be &amp;quot;translated&amp;quot; to Visual Prolog. So the old Turbo Prolog examples which can be found in Internet are worth studying.&lt;br /&gt;
&lt;br /&gt;
[http://perso.orange.fr/colin.barker/tpro1/tpro.htm Colin Barker*s Turbo Prolog Goodies]&lt;br /&gt;
&lt;br /&gt;
Frank Bergmann has several Turbo Prolog examples&lt;br /&gt;
*[http://www.fraber.de/games/chess/index.html Chess]&lt;br /&gt;
*[http://www.fraber.de/games/queens/index.html Queens]&lt;br /&gt;
*[http://www.fraber.de/games/world/index.html Tarskies World]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Turbo_Prolog&amp;diff=407</id>
		<title>Turbo Prolog</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Turbo_Prolog&amp;diff=407"/>
		<updated>2007-09-12T12:15:45Z</updated>

		<summary type="html">&lt;p&gt;Kari Rastas: New page: Turbo Prolog was marketed in the 80&amp;#039;s by Borland. Turbo Prolog was created by the founders of PDC.  ==Turbo Proglog example codes / programs in internet==  [http://perso.orange.fr/colin.ba...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Turbo Prolog was marketed in the 80&amp;#039;s by Borland. Turbo Prolog was created by the founders of PDC.&lt;br /&gt;
&lt;br /&gt;
==Turbo Proglog example codes / programs in internet==&lt;br /&gt;
&lt;br /&gt;
[http://perso.orange.fr/colin.barker/tpro1/tpro.htm Colin Barker*s Turbo Prolog Goodies]&lt;br /&gt;
&lt;br /&gt;
Frank Bergmann has several Turbo Prolog examples&lt;br /&gt;
*[http://www.fraber.de/games/chess/index.html Chess]&lt;br /&gt;
*[http://www.fraber.de/games/queens/index.html Queens]&lt;br /&gt;
*[http://www.fraber.de/games/world/index.html Tarskies World]&lt;/div&gt;</summary>
		<author><name>Kari Rastas</name></author>
	</entry>
</feed>