<?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=Tbn</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=Tbn"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Special:Contributions/Tbn"/>
	<updated>2026-04-10T00:27:08Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Anonymous_Predicates&amp;diff=4343</id>
		<title>Language Reference/Terms/Anonymous Predicates</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Anonymous_Predicates&amp;diff=4343"/>
		<updated>2016-11-08T09:14:37Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Background threads */ minor typos&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;An anonymous predicate is an expression that evaluates to a predicate value. The predicate value can be bound to a variable, passed as arguments or returned as result, but the value does not have a name in any class, interface or implementation.&lt;br /&gt;
&lt;br /&gt;
Anonymous predicates have the ability to capture values from the context in which the expression occurs, this is a rather powerful ability that can be used to avoid rather excessive amount of strange/unpleasant code.&lt;br /&gt;
&lt;br /&gt;
==== Syntax ====&lt;br /&gt;
&lt;br /&gt;
Anonymous predicates are terms:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;Term&amp;gt; : one of&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;lt;AnonymousPredicate&amp;gt;&lt;br /&gt;
    ...&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An anonymous predicate is a nameless clause in curly brackets. Certain parts are optional, giving these forms:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;AnonymousPredicate&amp;gt; : one of&lt;br /&gt;
    { ( &amp;lt;Arg&amp;gt;-comma-sep-list ) = &amp;lt;Term&amp;gt; } &lt;br /&gt;
    { ( &amp;lt;Arg&amp;gt;-comma-sep-list ) = &amp;lt;Term&amp;gt; :- &amp;lt;Term&amp;gt; } &lt;br /&gt;
    { ( &amp;lt;Arg&amp;gt;-comma-sep-list ) :- &amp;lt;Term&amp;gt; }&lt;br /&gt;
    { = &amp;lt;Term&amp;gt; } &lt;br /&gt;
    { = &amp;lt;Term&amp;gt; :- &amp;lt;Term&amp;gt; } &lt;br /&gt;
    { :- &amp;lt;Term&amp;gt; }&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Leaving out the argument list means &amp;quot;the required number of arguments&amp;quot; and can be used whenever the arguments are not used.&lt;br /&gt;
&lt;br /&gt;
==== Semantics ====&lt;br /&gt;
&lt;br /&gt;
An anonymous predicate expression evaluates to a predicate value.&lt;br /&gt;
Consider this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        Inc = { (X) = X+1 },&lt;br /&gt;
        A = Inc(4),&lt;br /&gt;
        B = Inc(23),&lt;br /&gt;
        stdio::writef(&amp;quot;A = %, B = %&amp;quot;, A, B).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Inc becomes an increment predicate, so the program will write:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;A = 5, B = 24&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code in this example corresponds to this code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        Inc = inc,&lt;br /&gt;
        A = Inc(4),&lt;br /&gt;
        B = Inc(23),&lt;br /&gt;
        stdio::writef(&amp;quot;A = %, B = %&amp;quot;, A, B).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    inc : (integer X) -&amp;gt; integer R.&lt;br /&gt;
clauses&lt;br /&gt;
    inc(X) = X+1.&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Where the clause &amp;lt;vp&amp;gt;(X) = X+1&amp;lt;/vp&amp;gt; can be found in the last line.  I.e. this time in a named predicate.&lt;br /&gt;
&lt;br /&gt;
Variables that are bound outside (i.e. before the occurrence of) an anonymous predicate can be used inside the anonymous predicate.  The value of variable will be captured by the anonymous predicate.&lt;br /&gt;
&lt;br /&gt;
Variables that are bound in an anonymous predicate are local variables in the anonymous predicate.&lt;br /&gt;
&lt;br /&gt;
===== Capturing context =====&lt;br /&gt;
&lt;br /&gt;
An anonymous predicate can capture context, which means that it can refer to things that are defined in its context, especially facts and variables from the clause.&lt;br /&gt;
&lt;br /&gt;
===== Capturing Variables =====&lt;br /&gt;
&lt;br /&gt;
Anonymous predicate occurs in a clause, and this clause may contain variables. Those variables that are bound before the anonymous predicate is met can be used inside the anonymous predicate.&lt;br /&gt;
This code illustrates how a variable is captured:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    pred = (integer) -&amp;gt; integer.&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    createAdder : (integer A) -&amp;gt; pred Adder.&lt;br /&gt;
clauses&lt;br /&gt;
    createAdder(A) = { (X) = X+A }.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        Add17 = createAdder(17),&lt;br /&gt;
        A = Add17(4),&lt;br /&gt;
        B = Add17(20),&lt;br /&gt;
        stdio::writef(&amp;quot;A = %, B = %&amp;quot;, A, B).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
We call &amp;lt;vp&amp;gt;createAdder&amp;lt;/vp&amp;gt; with &amp;lt;vp&amp;gt;17&amp;lt;/vp&amp;gt; as argument. So in the &amp;lt;vp&amp;gt;createAdder&amp;lt;/vp&amp;gt; clause &amp;lt;vp&amp;gt;A&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;17&amp;lt;/vp&amp;gt;, and therefore the result is &amp;lt;vp&amp;gt;{ (X) = X+17 }&amp;lt;/vp&amp;gt;.  We say that the anonymous predicate has captured the variable &amp;lt;vp&amp;gt;A&amp;lt;/vp&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Since &amp;lt;vp&amp;gt;Add17&amp;lt;/vp&amp;gt; is a predicate that adds &amp;lt;vp&amp;gt;17&amp;lt;/vp&amp;gt; to its argument, the output of the code will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;A = 21, B = 37&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===== Capturing ellipsis (...) =====&lt;br /&gt;
&lt;br /&gt;
An anonymous predicate can capture the ellipsis variable (i.e. ...):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    ppp(...)  :-&lt;br /&gt;
        W = { () :- stdio::write(...) },&lt;br /&gt;
        qqq(W).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
&amp;lt;vp&amp;gt;W&amp;lt;/vp&amp;gt; captures the ellipsis variable. &amp;lt;vp&amp;gt;qqq&amp;lt;/vp&amp;gt; receives a zero-arity predicate, when this predicate is invoked the captured ellipsis  variable will be written to the standard output device.&lt;br /&gt;
&lt;br /&gt;
===== Capturing Facts =====&lt;br /&gt;
&lt;br /&gt;
An anonymous predicate can access facts. If it is created by a class predicate it can access class facts.  If it is created by an object predicate it can access both object and class facts.&lt;br /&gt;
Consider this code that captures a class fact:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class facts&lt;br /&gt;
    count : integer := 0.&lt;br /&gt;
clauses&lt;br /&gt;
    seq() = { () = count :- count := count+1 }.&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        A = seq(),&lt;br /&gt;
        B = seq(),&lt;br /&gt;
        stdio::writef(&amp;quot;A1 = %, &amp;quot;, A()),&lt;br /&gt;
        stdio::writef(&amp;quot;B1 = %, &amp;quot;, B()),&lt;br /&gt;
        stdio::writef(&amp;quot;A2 = %, &amp;quot;, A()),&lt;br /&gt;
        stdio::writef(&amp;quot;B2 = %&amp;quot;, B()).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Both &amp;lt;vp&amp;gt;A&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;B&amp;lt;/vp&amp;gt; increment the class fact count, so the result is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;A1 = 1, B1 = 2, A2 = 3, B2 = 4&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In object predicates we can capture object facts. So assuming that &amp;lt;vp&amp;gt;seq&amp;lt;/vp&amp;gt; is an object predicate in &amp;lt;vp&amp;gt;myClass&amp;lt;/vp&amp;gt;, this code illustrates the capture of an object fact:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;facts&lt;br /&gt;
    count : integer := 0.&lt;br /&gt;
clauses&lt;br /&gt;
    seq() = { () = count :- count := count+1 }.&lt;br /&gt;
clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        A = myClass::new():seq(),&lt;br /&gt;
        B = myClass::new():seq(),&lt;br /&gt;
        stdio::writef(&amp;quot;A1 = %, &amp;quot;, A()),&lt;br /&gt;
        stdio::writef(&amp;quot;B1 = %, &amp;quot;, B()),&lt;br /&gt;
        stdio::writef(&amp;quot;A2 = %, &amp;quot;, A()),&lt;br /&gt;
        stdio::writef(&amp;quot;B2 = %&amp;quot;, B()).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
In this case &amp;lt;vp&amp;gt;A&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;B&amp;lt;/vp&amp;gt; comes from two different objects, which each have a count fact, so the output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;A1 = 1, B1 = 1, A2 = 2, B2 = 2&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Technically, the class version actually doesn&amp;#039;t capture anything, it merely have access to the fact. Likewise, the object version doesn&amp;#039;t actually capture the fact, instead it captures This and through This it obtains access to the object facts.&lt;br /&gt;
&lt;br /&gt;
===== Capturing This =====&lt;br /&gt;
&lt;br /&gt;
As described above it is possible to capture &amp;lt;vp&amp;gt;This&amp;lt;/vp&amp;gt; and thereby gaining access to objects facts.  The same mechanism gives access to calling object predicates.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    seq() = { () = count :- inc() }.&lt;br /&gt;
&lt;br /&gt;
clauses&lt;br /&gt;
    inc() :- count := count+1.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&amp;lt;vp&amp;gt;This&amp;lt;/vp&amp;gt; can also be used directly:&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    ppp() = { () = aaa::rrr(This) }.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Nesting =====&lt;br /&gt;
&lt;br /&gt;
Anonymous predicates can be nested:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    run() :-&lt;br /&gt;
        P = { (A) = { (B) = A+B } },&lt;br /&gt;
        Q = P(3300),&lt;br /&gt;
        R = P(2200),&lt;br /&gt;
        stdio::writef(&amp;quot;Q(11) = %, &amp;quot;, Q(11)),&lt;br /&gt;
        stdio::writef(&amp;quot;R(11) = %&amp;quot;, R(11)).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
To obtain &amp;lt;vp&amp;gt;Q&amp;lt;/vp&amp;gt; we call &amp;lt;vp&amp;gt;P&amp;lt;/vp&amp;gt; with &amp;lt;vp&amp;gt;3300&amp;lt;/vp&amp;gt;, so &amp;lt;vp&amp;gt;A&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;3300&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;Q&amp;lt;/vp&amp;gt; therefore becomes &amp;lt;vp&amp;gt;{ (B) = 3300+B } }&amp;lt;/vp&amp;gt;, likewise R becomes &amp;lt;vp&amp;gt;{ (B) = 2200+B } }&amp;lt;/vp&amp;gt;.  So, the output is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;Q(11) = 3311, R(11) = 2211&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Syntactic Sugar ====&lt;br /&gt;
&lt;br /&gt;
If you don&amp;#039;t need the arguments they can be skipped.  &lt;br /&gt;
So this code-fragment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;P = { (_) :- succeed },&lt;br /&gt;
Q = { (_, _) = 0 },&lt;br /&gt;
R = { (_, _, _) = _ :- fail }&lt;br /&gt;
Can be shortened down to this:&lt;br /&gt;
P = { :- succeed },&lt;br /&gt;
Q = { = 0 },&lt;br /&gt;
R = { = _ :- fail }&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that the arguments are completely skipped. If you write &amp;lt;vp&amp;gt;()&amp;lt;/vp&amp;gt; it means zero arguments, whereas skipping the arguments means &amp;quot;a suitable amount&amp;quot; of arguments.&lt;br /&gt;
&lt;br /&gt;
==== Examples of practical usage ====&lt;br /&gt;
&lt;br /&gt;
This section shows some cases where anonymous predicates are very handy.  The examples assume that the PFC scopes core, std, stdio, list and string are open.&lt;br /&gt;
&lt;br /&gt;
===== Dummy predicates =====&lt;br /&gt;
&lt;br /&gt;
Anonymous predicates are good for creating dummy predicate values:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;ppp( { = true } ), % don&amp;#039;t filter (boolean)&lt;br /&gt;
qqq( { :- succeed } ), %  don&amp;#039;t filter (determ)&lt;br /&gt;
rrr( { = 17 } ), % all rows must have height 17&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Adaptation =====&lt;br /&gt;
&lt;br /&gt;
In cases where you need a predicate and have one that is almost suitable, you can make the adaptation using an anonymous predicate.&lt;br /&gt;
&lt;br /&gt;
===== Index adaptation =====&lt;br /&gt;
&lt;br /&gt;
Consider the predicate write3:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    write3 : (function{integer, string} Indexer).&lt;br /&gt;
clauses&lt;br /&gt;
    write3(Indexer) :-&lt;br /&gt;
        foreach I = std::fromTo(0,2) do&lt;br /&gt;
            write(Indexer(I), &amp;quot;\n&amp;quot;)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Indexer implements an &amp;quot;array&amp;quot; of strings, &amp;lt;vp&amp;gt;write3&amp;lt;/vp&amp;gt; will write the three strings found at the indexes &amp;lt;vp&amp;gt;0&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;2&amp;lt;/vp&amp;gt;.  So &amp;lt;vp&amp;gt;write3&amp;lt;/vp&amp;gt; assumes that the &amp;quot;array&amp;quot; index is zero-based.&lt;br /&gt;
However, the &amp;quot;array&amp;quot; we have uses a one-based index:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    myArray : (integer N) -&amp;gt; string Value.&lt;br /&gt;
clauses&lt;br /&gt;
    myArray(1) = &amp;quot;First&amp;quot; :- !.&lt;br /&gt;
    myArray(2) = &amp;quot;Second&amp;quot; :- !.&lt;br /&gt;
    myArray(3) = &amp;quot;Third&amp;quot; :- !.&lt;br /&gt;
    myArray(_) = _ :-&lt;br /&gt;
        raiseError().&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
But using an anonymous predicate we can easily adapt the one-based array to the zero-based usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;% myArray is 0-based, write3 requires 1-based&lt;br /&gt;
Arr = { (N) = myArray(N+1) },&lt;br /&gt;
write3(Arr)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
So we get the expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;First&lt;br /&gt;
Second&lt;br /&gt;
Third&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Parameter adaptation =====&lt;br /&gt;
&lt;br /&gt;
In this code &amp;lt;vp&amp;gt;listChildren&amp;lt;/vp&amp;gt; will call a &amp;lt;vp&amp;gt;ChildWriter&amp;lt;/vp&amp;gt; predicate for each &amp;quot;&amp;lt;vp&amp;gt;C&amp;lt;/vp&amp;gt; is the child of &amp;lt;vp&amp;gt;P&amp;lt;/vp&amp;gt;&amp;quot;-pair:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    listChildren :&lt;br /&gt;
        (predicate{string,string} ChildWriter).&lt;br /&gt;
clauses&lt;br /&gt;
    listChildren(CW) :-&lt;br /&gt;
        CW(&amp;quot;Son1&amp;quot;, &amp;quot;Father&amp;quot;),&lt;br /&gt;
        CW(&amp;quot;Son2&amp;quot;, &amp;quot;Father&amp;quot;).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
We will however prefer to list the &amp;quot;&amp;lt;vp&amp;gt;P&amp;lt;/vp&amp;gt; is the parent of &amp;lt;vp&amp;gt;C&amp;lt;/vp&amp;gt;&amp;quot; using the predicate &amp;lt;vp&amp;gt;wParent&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    wParent : (string Parent, string Child).&lt;br /&gt;
clauses&lt;br /&gt;
    wParent(P, C) :-&lt;br /&gt;
        writef(&amp;quot;% is the parent of %\n&amp;quot;, P, C).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
&amp;lt;vp&amp;gt;wParent&amp;lt;/vp&amp;gt; takes the arguments in the opposite order, but we can easily adapt using an anonymous predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Swap = { (A,B) :- wParent(B,A) },&lt;br /&gt;
listChildren(Swap)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And then the out becomes the expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;Father is the parent of Son1&lt;br /&gt;
Father is the parent of Son2&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also throw away arguments, for example when calling this predicate that only needs a &amp;lt;vp&amp;gt;Child&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    wKnowParent : (string Child).&lt;br /&gt;
clauses&lt;br /&gt;
    wKnowParent(C) :-&lt;br /&gt;
        writef(&amp;quot;We know a parent of %\n&amp;quot;, C).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
The adaptation looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Fewer = { (C,P) :- wKnowParent(C) },&lt;br /&gt;
listChildren(Fewer)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;We know a parent of Son1&lt;br /&gt;
We know a parent of Son2&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also supply dummy arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;More = { (_,P) :- addChildren(P, 1) }&lt;br /&gt;
listChildren(More)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here &amp;lt;vp&amp;gt;addChildren&amp;lt;/vp&amp;gt; will &amp;quot;add a count of children to &amp;lt;vp&amp;gt;P&amp;lt;/vp&amp;gt;&amp;quot;.  Since each invocation corresponds to one child we will call &amp;lt;vp&amp;gt;addChild&amp;lt;/vp&amp;gt; supplying &amp;lt;vp&amp;gt;1&amp;lt;/vp&amp;gt; as a &amp;quot;dummy&amp;quot; argument.  The More is thus an adaptor that both throws away an argument and supplies a dummy argument.&lt;br /&gt;
&lt;br /&gt;
===== Filters =====&lt;br /&gt;
&lt;br /&gt;
Assume this predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    writeFiltered : &lt;br /&gt;
        (string L, predicate_dt{integer} Filter).&lt;br /&gt;
clauses&lt;br /&gt;
    writeFiltered(Label, Filter) :-&lt;br /&gt;
        List = [1,2,3,4,5,6,7,8,9],&lt;br /&gt;
        FilteredList = filter(List, Filter),&lt;br /&gt;
        writef(&amp;quot;%\t%\n&amp;quot;, Label, FilteredList).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Filter is used to filter the list &amp;lt;vp&amp;gt;[1,2,3,4,5,6,7,8,9]&amp;lt;/vp&amp;gt;; the filtered list and the Label are written to the standard output.&lt;br /&gt;
&lt;br /&gt;
First we use the allow-all filter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;All = { :- succeed },&lt;br /&gt;
writeFiltered(&amp;quot;All&amp;quot;, All)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This filter simply succeeds for any element, so the output is the entire list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;All     [1,2,3,4,5,6,7,8,9]&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is just as easy to create a filter that fails for all elements and thus allow-none:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;None = { :- fail },&lt;br /&gt;
writeFiltered(&amp;quot;None&amp;quot;, None)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output from this is the empty list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;None    []&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also create filters for elements greater than &amp;lt;vp&amp;gt;3&amp;lt;/vp&amp;gt; and elements dividable by &amp;lt;vp&amp;gt;3&amp;lt;/vp&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;GreaterThan3 = { (X) :- X &amp;gt; 3 },&lt;br /&gt;
writeFiltered(&amp;quot;&amp;gt; 3&amp;quot;, GreaterThan3),&lt;br /&gt;
Rem3 = { (X) :- 0 = X rem 3 },&lt;br /&gt;
writeFiltered(&amp;quot;Rem3&amp;quot;, Rem3)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output from this is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&amp;gt; 3     [4,5,6,7,8,9]&lt;br /&gt;
Rem3    [3,6,9]&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Sorting =====&lt;br /&gt;
&lt;br /&gt;
The list package has a sort predicate. But sometimes the default order is not what you need.  Therefore the list package also has a predicate sortBy, which sorts the elements using a programmer defined compare operation.&lt;br /&gt;
Let us first consider string sorting, using this predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    writeStringsSorted :&lt;br /&gt;
        (string Label, comparator{string} Comp).&lt;br /&gt;
clauses&lt;br /&gt;
    writeStringsSorted(Label, C) :-&lt;br /&gt;
        List = [&amp;quot;John Wayne&amp;quot;, &amp;quot;Uma Thurman&amp;quot;,&lt;br /&gt;
            &amp;quot;Harrison Ford&amp;quot;, &amp;quot;Nicolas Cage&amp;quot;,&lt;br /&gt;
            &amp;quot;Elizabeth Taylor&amp;quot;, &amp;quot;Cary Grant&amp;quot;,&lt;br /&gt;
            &amp;quot;Jerry Lewis&amp;quot;, &amp;quot;Robert De Niro&amp;quot;],&lt;br /&gt;
        Sorted = sortBy(C, List),&lt;br /&gt;
        write(Label, &amp;quot;\n&amp;quot;),&lt;br /&gt;
        foreach S = list::getMember_nd(Sorted) do&lt;br /&gt;
            writef(&amp;quot;    %\n&amp;quot;, S)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
We can call the predicate with the &amp;quot;normal&amp;quot; comparator, and using an anonymous predicate we can easily sort it descending as well:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Normal = compare,&lt;br /&gt;
writeStringsSorted(&amp;quot;Normal&amp;quot;, Normal),&lt;br /&gt;
Descending = { (A,B) = compare(B,A) },&lt;br /&gt;
writeStringsSorted(&amp;quot;Descending&amp;quot;, Descending)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;Normal&lt;br /&gt;
    Cary Grant&lt;br /&gt;
    Elizabeth Taylor&lt;br /&gt;
    Harrison Ford&lt;br /&gt;
    Jerry Lewis&lt;br /&gt;
    John Wayne&lt;br /&gt;
    Nicolas Cage&lt;br /&gt;
    Robert De Niro&lt;br /&gt;
    Uma Thurman&lt;br /&gt;
Descending&lt;br /&gt;
    Uma Thurman&lt;br /&gt;
    Robert De Niro&lt;br /&gt;
    Nicolas Cage&lt;br /&gt;
    John Wayne&lt;br /&gt;
    Jerry Lewis&lt;br /&gt;
    Harrison Ford&lt;br /&gt;
    Elizabeth Taylor&lt;br /&gt;
    Cary Grant&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
Let us also sort some more complex elements. Here a person has a first name and a last name, using this domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    person = p(string First, string Last).&amp;lt;/vip&amp;gt;&lt;br /&gt;
For the demonstration we will use this test predicate:&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    writePersonsSorted : &lt;br /&gt;
        (string Label, comparator{person} Comparator).&lt;br /&gt;
clauses&lt;br /&gt;
    writePersonsSorted(Label, C) :-&lt;br /&gt;
        List = [p(&amp;quot;John&amp;quot;,&amp;quot;Wayne&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Uma&amp;quot;,&amp;quot;Thurman&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Harrison&amp;quot;,&amp;quot;Ford&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Nicolas&amp;quot;,&amp;quot;Cage&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Elizabeth&amp;quot;,&amp;quot;Taylor&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Cary&amp;quot;,&amp;quot;Grant&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Jerry&amp;quot;,&amp;quot;Lewis&amp;quot;),&lt;br /&gt;
            p(&amp;quot;Robert&amp;quot;,&amp;quot;De Niro&amp;quot;)],&lt;br /&gt;
        Sorted = sortBy(C, List),&lt;br /&gt;
        write(Label, &amp;quot;\n&amp;quot;),&lt;br /&gt;
        foreach p(F,L) = list::getMember_nd(Sorted) do&lt;br /&gt;
            writef(&amp;quot;    % %\n&amp;quot;, F, L)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Again we can sort using the normal and a descending comparator:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;Normal = compare,&lt;br /&gt;
writePersonsSorted(&amp;quot;Normal&amp;quot;, Normal),&lt;br /&gt;
Descending = { (A,B) = compare(B,A) },&lt;br /&gt;
writePersonsSorted(&amp;quot;Descending&amp;quot;, Descending)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the compare predicate uses left-to-right lexicographic order on the &amp;lt;vp&amp;gt;p&amp;lt;/vp&amp;gt;-functor, the result is the same as before:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;Normal&lt;br /&gt;
    Cary Grant&lt;br /&gt;
    Elizabeth Taylor&lt;br /&gt;
    Harrison Ford&lt;br /&gt;
    Jerry Lewis&lt;br /&gt;
    John Wayne&lt;br /&gt;
    Nicolas Cage&lt;br /&gt;
    Robert De Niro&lt;br /&gt;
    Uma Thurman&lt;br /&gt;
Descending&lt;br /&gt;
    Uma Thurman&lt;br /&gt;
    Robert De Niro&lt;br /&gt;
    Nicolas Cage&lt;br /&gt;
    John Wayne&lt;br /&gt;
    Jerry Lewis&lt;br /&gt;
    Harrison Ford&lt;br /&gt;
    Elizabeth Taylor&lt;br /&gt;
    Cary Grant&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
But with the more complex domain we can create a comparator that will sort on last name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;LN = { (p(_,L1), p(_, L2)) = compare(L1,L2) },&lt;br /&gt;
writePersonsSorted(&amp;quot;LastName&amp;quot;, LN)&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result is what we expect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;LastName&lt;br /&gt;
    Nicolas Cage&lt;br /&gt;
    Robert De Niro&lt;br /&gt;
    Harrison Ford&lt;br /&gt;
    Cary Grant&lt;br /&gt;
    Jerry Lewis&lt;br /&gt;
    Elizabeth Taylor&lt;br /&gt;
    Uma Thurman&lt;br /&gt;
    John Wayne&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Capturing context =====&lt;br /&gt;
&lt;br /&gt;
As mentioned a very powerful feature of anonymous predicates is the ability to capture context.  The examples in this section show some ways you can use this. &lt;br /&gt;
&lt;br /&gt;
===== Background threads =====&lt;br /&gt;
&lt;br /&gt;
The routine for starting a thread takes a null-ary predicate and runs it in the new thread.  But you nearly always need to pass some input data to the job in the new thread.&lt;br /&gt;
This is possible in several ways, but the absolutely simplest way is to use anonymous predicates.&lt;br /&gt;
The project &amp;lt;vp&amp;gt;bgDemo&amp;lt;/vp&amp;gt; from the Visual Prolog example collection (that can be installed from the IDE) use this method.&lt;br /&gt;
The project has a form that can start a background job and display status information from the job in a &amp;lt;vp&amp;gt;jobControl&amp;lt;/vp&amp;gt; that is added to the form.&lt;br /&gt;
A background job is a predicate that will receive a &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt;, which it can use to report status and completion degree:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    job = (jobLog Log).&amp;lt;/vip&amp;gt;&lt;br /&gt;
A jobLog looks like this:&lt;br /&gt;
&amp;lt;vip&amp;gt;interface jobLog&lt;br /&gt;
&lt;br /&gt;
properties&lt;br /&gt;
    completion : real (i).&lt;br /&gt;
&lt;br /&gt;
properties&lt;br /&gt;
    status : string (i).&lt;br /&gt;
&lt;br /&gt;
end interface jobLog&amp;lt;/vip&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
The job can report completion degree by setting the completion property (range &amp;lt;vp&amp;gt;0&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;1&amp;lt;/vp&amp;gt;).  Likewise, the status property can be used to reflect the current status of the job.&lt;br /&gt;
&lt;br /&gt;
The status and completion will be shown in the form together with a job name.&lt;br /&gt;
A job is started by calling the form&amp;#039;s &amp;lt;vp&amp;gt;addJob&amp;lt;/vp&amp;gt; predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    addJob(JobName, Job) :-&lt;br /&gt;
        JobCtrl = jobControl::new(This),&lt;br /&gt;
        JobCtrl:name := JobName,&lt;br /&gt;
        JobCtrl:show(),&lt;br /&gt;
        assert(jobCtrl_fact(JobCtrl)),&lt;br /&gt;
        arrange(),&lt;br /&gt;
        JobLog = jobLog::new(JobCtrl),&lt;br /&gt;
        Action = { :- Job(JobLog) },&lt;br /&gt;
        _ = thread::start(Action).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
In this context it is the last three lines that are interesting.  &amp;lt;vp&amp;gt;thread::start&amp;lt;/vp&amp;gt; takes a null-ary predicate as argument, but a job is a predicate that takes a &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt; as argument.  Therefore we create an anonymous predicate &amp;lt;vp&amp;gt;Action&amp;lt;/vp&amp;gt;, which takes no arguments but invokes &amp;lt;vp&amp;gt;Job&amp;lt;/vp&amp;gt; on the &amp;lt;vp&amp;gt;JobLog&amp;lt;/vp&amp;gt;.  The anonymous predicate has captured both &amp;lt;vp&amp;gt;Job&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;JobLog&amp;lt;/vp&amp;gt; from the context, and subsequently both these values are transferred to the new thread even though this thread only receives a null-ary predicate.&lt;br /&gt;
The jobs in the &amp;lt;vp&amp;gt;bgDemo&amp;lt;/vp&amp;gt; project are merely dummy jobs that only manipulate their &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt;.  One of them looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    job(Log, From, To) :-&lt;br /&gt;
        Log:status := &amp;quot;Step 1&amp;quot;,&lt;br /&gt;
        foreach N1 = std::fromTo(From, To) do&lt;br /&gt;
            Log:completion :=&lt;br /&gt;
                (N1-From) / (To-From) / 2,&lt;br /&gt;
            programControl::sleep(3)&lt;br /&gt;
        end foreach,&lt;br /&gt;
        Log:status := &amp;quot;Step 2&amp;quot;,&lt;br /&gt;
        foreach N2 = std::fromTo(From, To) do&lt;br /&gt;
            Log:completion :=&lt;br /&gt;
                (N2-From) / (To-From) / 2 + 0.5,&lt;br /&gt;
            programControl::sleep(3)&lt;br /&gt;
        end foreach,&lt;br /&gt;
        Log:status := &amp;quot;finished&amp;quot;.&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
It has two loops which run from &amp;lt;vp&amp;gt;From&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;To&amp;lt;/vp&amp;gt; and calculates the completion and sets it on the &amp;lt;vp&amp;gt;Log&amp;lt;/vp&amp;gt;.  It also sets the status text before, between and after the loops.&lt;br /&gt;
You may notice that the job does not have the proper job type, because a proper job only has one argument (the &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt;), this job has three arguments.&lt;br /&gt;
Again it is anonymous predicates that help us.  The code that adds the jobs to the form looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    onFileNew : window::menuItemListener.&lt;br /&gt;
clauses&lt;br /&gt;
    onFileNew(_Source, _MenuTag) :-&lt;br /&gt;
        JF = jobForm::display(This),&lt;br /&gt;
        Job11 = {(L) :- job1::job(L, 1, 1000)},&lt;br /&gt;
        Job12 = {(L) :- job1::job(L, 200, 600)},&lt;br /&gt;
        Job13 = {(L) :- job1::job(L, 1200, 3000)},&lt;br /&gt;
        Job14 = {(L) :- job1::job(L, 1, 1000)},&lt;br /&gt;
        JF:addJob(&amp;quot;job1.1&amp;quot;, Job11),&lt;br /&gt;
        JF:addJob(&amp;quot;job1.2&amp;quot;, Job12),&lt;br /&gt;
        JF:addJob(&amp;quot;job1.3&amp;quot;, Job13),&lt;br /&gt;
        JF:addJob(&amp;quot;job1.4&amp;quot;, Job14),&lt;br /&gt;
        ...&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
In a more realistic program, it is most likely that &amp;lt;vp&amp;gt;From&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;To&amp;lt;/vp&amp;gt; would not be constants, but rather parameters passed from some outer place.  In that case these anonymous predicates would also capture variables from the context.&lt;br /&gt;
The &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt; in the &amp;lt;vp&amp;gt;bgDemo&amp;lt;/vp&amp;gt; illustrates one more usage of anonymous predicates.  The &amp;lt;vp&amp;gt;jobLog&amp;lt;/vp&amp;gt; pass the completion and the status information to a &amp;lt;vp&amp;gt;jobControl&amp;lt;/vp&amp;gt;.  The &amp;lt;vp&amp;gt;jobControl&amp;lt;/vp&amp;gt; is a GUI control on the &amp;lt;vp&amp;gt;jobForm&amp;lt;/vp&amp;gt; capable of doing a suitable rendering of the information.  This however gives a synchronization problem, because GUI controls are not thread safe and here we want to update some controls from a background thread.  This can lead to conflicts, because it is the main thread that draws the controls.&lt;br /&gt;
The solution is to make transfer the the update of the control to the GUI thread.  We do this by posting actions to the control.&lt;br /&gt;
The implementation of the status update looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    status(Status) :-&lt;br /&gt;
        Action = { :- jobCtrl:status := Status },&lt;br /&gt;
        jobCtrl:postAction(Action).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
&amp;lt;vp&amp;gt;Action&amp;lt;/vp&amp;gt; is a null-ary predicate that will set the status in the &amp;lt;vp&amp;gt;jobCtrl&amp;lt;/vp&amp;gt;.  We post this action to the &amp;lt;vp&amp;gt;jobCtrl&amp;lt;/vp&amp;gt;.  When the &amp;lt;vp&amp;gt;jobCtrl&amp;lt;/vp&amp;gt; receives the action it invokes it and is thus updated.  This way, the actual update of the control will be performed by the GUI thread.&lt;br /&gt;
This anonymous predicate not only captures the &amp;lt;vp&amp;gt;Status&amp;lt;/vp&amp;gt; variable it also captures the &amp;lt;vp&amp;gt;jobCtrl&amp;lt;/vp&amp;gt; fact.&lt;br /&gt;
&lt;br /&gt;
===== Asynchronous callbacks =====&lt;br /&gt;
&lt;br /&gt;
Assume that we send commands to a remote service. The command execution is asynchronous, so when we execute a command we also give a callback action which will be invoked when the execution of the command is finished.  To execute a command we must call this predicate:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    executeCommand :&lt;br /&gt;
        (command Cmd, predicate{} OnDone).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
Based on this predicate we want to create a similar predicate that can execute a list of commands.  A certain command should be executed when the previous command completes.&lt;br /&gt;
We will also make our list executor asynchronous, so we supply an action that will be invoked when the entire script of commands are finished.&lt;br /&gt;
Our script executer will have the form:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    executeScript :&lt;br /&gt;
        (command* Script, predicate{} OnDone).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
If the script is empty we simply invoke the &amp;lt;vp&amp;gt;OnDone&amp;lt;/vp&amp;gt; action. &lt;br /&gt;
If the script has a command &amp;lt;vp&amp;gt;H&amp;lt;/vp&amp;gt; and a rest script &amp;lt;vp&amp;gt;T&amp;lt;/vp&amp;gt;, we must first execute &amp;lt;vp&amp;gt;H&amp;lt;/vp&amp;gt;, and when it is finished we must execute the rest of the script &amp;lt;vp&amp;gt;T&amp;lt;/vp&amp;gt;.  So the &amp;lt;vp&amp;gt;OnDone&amp;lt;/vp&amp;gt; action we supply when executing &amp;lt;vp&amp;gt;H&amp;lt;/vp&amp;gt; must execute &amp;lt;vp&amp;gt;T&amp;lt;/vp&amp;gt;.&lt;br /&gt;
All in all, the implementation can look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;clauses&lt;br /&gt;
    executeScript([], OnDone) :-&lt;br /&gt;
        OnDone().&lt;br /&gt;
    executeScript([H|T], OnDone) :-&lt;br /&gt;
        DoneH = { :- executeScript(T, OnDone) },&lt;br /&gt;
        executeCommand(H, DoneH).&amp;lt;/vip&amp;gt;&lt;br /&gt;
        &lt;br /&gt;
We have used an anonymous predicate to perform the execution of the rest of the script.  This anonymous predicate captures &amp;lt;vp&amp;gt;T&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;OnDone&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;{{LanguageReferenceSubarticle|Terms/Anonymous Predicates}}&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Language_Reference/Attributes&amp;diff=4342</id>
		<title>Language Reference/Attributes</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Language_Reference/Attributes&amp;diff=4342"/>
		<updated>2016-11-07T09:34:20Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* retired */ minor typos&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{languageReferenceNavbar|Attributes}}&lt;br /&gt;
&lt;br /&gt;
Various definitions and declarations can be annotated with attributes. This section describes the general syntax of attributes and where they can be placed. It also describes the meaning of the specific attributes.&lt;br /&gt;
&lt;br /&gt;
=== Syntax ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;Attributes&amp;gt; :&lt;br /&gt;
    [ &amp;lt;Attribute&amp;gt;-comma-sep-list ]&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;Attribute&amp;gt; : one of&lt;br /&gt;
    &amp;lt;LowerCaseIdentifier&amp;gt;&lt;br /&gt;
    &amp;lt;LowerCaseIdentifier&amp;gt; ( &amp;lt;Literal&amp;gt;-comma-sep-list )&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the literals must either be numbers or string literals.&lt;br /&gt;
&lt;br /&gt;
=== Insertion Points ===&lt;br /&gt;
&lt;br /&gt;
The attributes of interfaces, classes and implementations are right after the scope qualifications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;InterfaceDeclaration&amp;gt; :&lt;br /&gt;
    interface &amp;lt;IinterfaceName&amp;gt;&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
end interface &amp;lt;IinterfaceName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ClassDeclaration&amp;gt; :&lt;br /&gt;
    class &amp;lt;ClassName&amp;gt; &amp;lt;ConstructionType&amp;gt;-opt&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
    end class &amp;lt;ClassName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ClassImplementation&amp;gt; :&lt;br /&gt;
   implement &amp;lt;ClassName&amp;gt;&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
    end implement &amp;lt;ClassName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attributes of constants, domains, predicates, properties and facts are at the end (i.e. right before the terminating dot).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ConstantDefinition&amp;gt;: one of&lt;br /&gt;
    &amp;lt;ConstantName&amp;gt; = &amp;lt;ConstantValue&amp;gt; &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;ConstantName&amp;gt; : &amp;lt;TypeName&amp;gt; = &amp;lt;ConstantValue&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;DomainDefinition&amp;gt;:&lt;br /&gt;
    &amp;lt;DomainName&amp;gt; &amp;lt;FormalTypeParameterList&amp;gt;-opt = &amp;lt;TypeExpression&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;PredicateDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;PredicateName&amp;gt; : &amp;lt;PredicateDomain&amp;gt; &amp;lt;LinkName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;PredicateName&amp;gt; : &amp;lt;PredicateDomainName&amp;gt; &amp;lt;LinkName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;PropertyDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;PropertyName&amp;gt; : &amp;lt;PropertyType&amp;gt; &amp;lt;FlowPattern&amp;gt;-list-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;FactDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;FactVariableDeclaration&amp;gt; &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;FactFunctorDeclaration&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attributes of formal arguments are at the end.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;FormalArgument&amp;gt; :&lt;br /&gt;
    &amp;lt;TypeExpression&amp;gt; &amp;lt;ArgumentName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specific Attributes ===&lt;br /&gt;
&lt;br /&gt;
==== byVal ====&lt;br /&gt;
&lt;br /&gt;
An argument is transferred directly on the stack rather than using a pointer.  Valid for formal predicate arguments provided the &amp;lt;vp&amp;gt;language&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;stdcall&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;apicall&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;c&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    externalP : (point Point [byVal]) language apicall.&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== deprecated ====&lt;br /&gt;
&lt;br /&gt;
The declared entity is deprecated. The string literal describes how to migrate from it.  The entity still exists, but usage will cause a warning.&lt;br /&gt;
The entity will not exist in future versions of Visual Prolog.  Valid for member declarations and scopes.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    oldFashioned : (string Arg) [deprecated(&amp;quot;Use newFashion instead&amp;quot;)].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== formatString ====&lt;br /&gt;
&lt;br /&gt;
The argument is a {{lang2|Domains|Format Strings|format string}} for a subsequent ellipsis argument (i.e. &amp;lt;vp&amp;gt;...&amp;lt;/vp&amp;gt;).  Valid for one string argument of a predicate with an ellipsis argument.  The use of formatString will make the compiler check the validity of actual arguments with respect to actual format strings (where possible).&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    writef : (string Format [formatString], ...).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== in ====&lt;br /&gt;
&lt;br /&gt;
The argument is an input argument.  Valid for a formal predicate argument.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string InputArg [in]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== inline ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; alters the memory layout of a struct (i.e. a single alternative functor domain with an align qualification).  The purpose of the attribute is to ease interfacing to foreign languages and should normally not be used for pure Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; can be used in three cases:&lt;br /&gt;
&lt;br /&gt;
* inlining a struct rather than having a pointer to the struct&lt;br /&gt;
* inlining a fixed size string&lt;br /&gt;
* inlining a fixed number of bytes&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; on struct field in another struct its data will be inlined instead of having a pointer to the struct&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    point =p(integer X, integer Y).&lt;br /&gt;
&lt;br /&gt;
domains&lt;br /&gt;
    rectangle =&lt;br /&gt;
       r(&lt;br /&gt;
            point UpperLeft [inline],&lt;br /&gt;
            point LowerRight [inline]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
Since &amp;lt;vp&amp;gt;UpperLeft&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;LowerRight&amp;lt;/vp&amp;gt; are inlined the struct have the same memory layout as this one:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    rectangle2 =&lt;br /&gt;
        r2(&lt;br /&gt;
            integer UpperLeft_X,&lt;br /&gt;
            integer UpperLeft_Y,&lt;br /&gt;
            integer LowerRight_X,&lt;br /&gt;
            integer LowerRight_Y&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline(&amp;lt;size&amp;gt;)&amp;lt;/vp&amp;gt; on a &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; or a &amp;lt;vp&amp;gt;string8&amp;lt;/vp&amp;gt; field in a struct, the struct will contain a fixed size string with &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; characters (i.e. &amp;lt;vp&amp;gt;char&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;char8&amp;lt;/vp&amp;gt;, respectively).  The strings will be zero terminated if they are shorter than &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt;, but not if they have &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; characters.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    device =&lt;br /&gt;
        device(&lt;br /&gt;
            integer Id,&lt;br /&gt;
            string DeviceName [inline(12)]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&amp;lt;vp&amp;gt;DeviceName&amp;lt;/vp&amp;gt; is an inlined Unicode string of length 12.&lt;br /&gt;
The struct have the same layout as:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    device =&lt;br /&gt;
        device(&lt;br /&gt;
            integer Id,&lt;br /&gt;
            char DeviceName_01,&lt;br /&gt;
            char DeviceName_02,&lt;br /&gt;
            char DeviceName_03,&lt;br /&gt;
            char DeviceName_04,&lt;br /&gt;
            char DeviceName_05,&lt;br /&gt;
            char DeviceName_06,&lt;br /&gt;
            char DeviceName_07,&lt;br /&gt;
            char DeviceName_08,&lt;br /&gt;
            char DeviceName_09,&lt;br /&gt;
            char DeviceName_10,&lt;br /&gt;
            char DeviceName_11,&lt;br /&gt;
            char DeviceName_12&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline(&amp;lt;size&amp;gt;)&amp;lt;/vp&amp;gt; on the &amp;lt;vp&amp;gt;pointer&amp;lt;/vp&amp;gt; type the struct will contain &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; bytes, and the pointer will become a pointer to that field:&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    mark =&lt;br /&gt;
        mark(&lt;br /&gt;
            integer Position,&lt;br /&gt;
            pointer Data [8]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&amp;lt;vp&amp;gt;Data&amp;lt;/vp&amp;gt; is pointer to 8 inlined bytes&lt;br /&gt;
The struct have the same layout as:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    mark2 =&lt;br /&gt;
        mark2(&lt;br /&gt;
            integer Position,&lt;br /&gt;
            byte Data_1,&lt;br /&gt;
            byte Data_2,&lt;br /&gt;
            byte Data_3,&lt;br /&gt;
            byte Data_4,&lt;br /&gt;
            byte Data_5,&lt;br /&gt;
            byte Data_6,&lt;br /&gt;
            byte Data_7,&lt;br /&gt;
            byte Data_8&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
And &amp;lt;vp&amp;gt;Data&amp;lt;/vp&amp;gt; will point to &amp;lt;vp&amp;gt;Data_1&amp;lt;/vp&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== noDefaultConstructor ====&lt;br /&gt;
&lt;br /&gt;
Used for a class to indicate that it should not have an implicit default constructor, and can thus be used to a class that does not have any public constructors at all.  Valid for an object creating class declaration.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;class classWithoutPublicConstructors : myInterface&lt;br /&gt;
    open core&lt;br /&gt;
    [noDefaultConstructor]&lt;br /&gt;
...&lt;br /&gt;
end class classWithoutPublicConstructors&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== out ====&lt;br /&gt;
&lt;br /&gt;
The argument is an output argument. Valid for a formal predicate argument.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string OutputArg [out]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== programPoint ====&lt;br /&gt;
&lt;br /&gt;
Used for a predicate or constructor declaration to indicate that it recieves an extra input argument which describes the place in a program (program point) where this predicate was called. This additional argument has programPoint type which is declared in the PFC core class like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    programPoint = programPoint(hScopeDescriptor ClassDescriptor, string PredicateName, sourceCursor Cursor).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The clause name of such predicate or constructor should have suffix &amp;quot;_explicit&amp;quot;. Valid for a predicate or constructor declaration.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string String) [programPoint].&lt;br /&gt;
clauses&lt;br /&gt;
    pred_explicit(ProgramPoint, String) :-&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;vp&amp;gt;programPoint&amp;lt;/vp&amp;gt; attribute is described in more details in the description of [[Language Reference/Predicates#programPoint|Predicates]], and it is a vital part of the exception system, see [[Exception Handling]].&lt;br /&gt;
&lt;br /&gt;
==== retired ====&lt;br /&gt;
&lt;br /&gt;
The declared entity is retired. The string literal describes how to migrate from it.  The entity does not exist anymore.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    veryOldFashioned : (string Arg) [retired(&amp;quot;Use newFashion instead&amp;quot;)].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== sealed ====&lt;br /&gt;
&lt;br /&gt;
Used for an interface to indicate that it cannot be supported by any other interface. This allows to create more efficient codes, because the compiler provides some optimization when using the objects of such type. Valid for an object creating class declaration as a construction interface.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;interface myInterface&lt;br /&gt;
    [sealed]&lt;br /&gt;
...&lt;br /&gt;
end interface myInterface&lt;br /&gt;
&lt;br /&gt;
class myClass : myInterface&lt;br /&gt;
...&lt;br /&gt;
end class myClass&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== union ====&lt;br /&gt;
&lt;br /&gt;
Used for creating functor domains with several alternatives but no real functors.  This should only be used to mimic C/C++ union structs in low-level interfacing.  Valid for functor domain with several alternatives and alignment.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    u64var = align 4&lt;br /&gt;
        u64(unsigned64 Value64);&lt;br /&gt;
        u64_struct(unsigned Low32, unsigned High32)&lt;br /&gt;
        [union].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== used ====&lt;br /&gt;
&lt;br /&gt;
An unused local member can be marked &amp;lt;vp&amp;gt;used&amp;lt;/vp&amp;gt; to prevent the compiler to issue a warning and remove the corresponding code. Valid for members.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    seeminglyUnused : () [used].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== default ====&lt;br /&gt;
&lt;br /&gt;
{{Template:NonReleased}}&lt;br /&gt;
&lt;br /&gt;
It is problematic to update functor domains, if terms have been persisted in serialized form.  Because new programs cannot deal with old serializations.&lt;br /&gt;
&lt;br /&gt;
The two attributes &amp;lt;vp&amp;gt;default/1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;retiredFunctor/1&amp;lt;/vp&amp;gt; can help dealing with this problem.&lt;br /&gt;
&lt;br /&gt;
The last arguments of a functor can have default values:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    ppp =&lt;br /&gt;
        fct(&lt;br /&gt;
            integer A,&lt;br /&gt;
            real B,&lt;br /&gt;
            ddd C,&lt;br /&gt;
            integer* New [default([])],&lt;br /&gt;
            real New2 [default(0)]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The default attribute does not change anything within the program; it only affects the deserialization.  If during deserialization we meet the closing parenthesis too soon we supply default values for the remaining arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that this will only work for &amp;#039;&amp;#039;&amp;#039;text&amp;#039;&amp;#039;&amp;#039; deserialization.&lt;br /&gt;
&lt;br /&gt;
See also [[Functor Domain Versioning]].&lt;br /&gt;
&lt;br /&gt;
==== retiredFunctor ====&lt;br /&gt;
&lt;br /&gt;
{{Template:NonReleased}}&lt;br /&gt;
&lt;br /&gt;
Functor alternatives can be retired.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    ppp =&lt;br /&gt;
        fct(integer A, real B, ddd C) [retiredFunctor(aaa::fct_ppp)];&lt;br /&gt;
        fct2(integer A, ddd C, integer* New).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;aaa::fct_ppp&amp;lt;/vp&amp;gt; must be a predicate with this type (it can be an anonymous predicate):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    fct_ppp : (integer A, real B, ddd C) -&amp;gt; ppp NewValue.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e. it takes the arguments of the old functor and returns a value in the functor domain.&lt;br /&gt;
&lt;br /&gt;
In the program fct is does not exist at all, it is retired.  But in the deserialization fct terms can still be handled:  The arguments are parsed according to the types, and then the value is created by invoking &amp;lt;vp&amp;gt;aaa::fct_ppp&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
This method will also work for binary serializations, provided:&lt;br /&gt;
&lt;br /&gt;
* The old domain had more than one alternative (so there are functor numbers in the serialization)&lt;br /&gt;
* New alternatives are added last (to retain functor numbers) &lt;br /&gt;
&lt;br /&gt;
It is however not recommend using binary serialization for inter-session persistence.&lt;br /&gt;
&lt;br /&gt;
See also [[Functor Domain Versioning]].&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Language_Reference/Attributes&amp;diff=4341</id>
		<title>Language Reference/Attributes</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Language_Reference/Attributes&amp;diff=4341"/>
		<updated>2016-11-07T09:32:15Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* deprecated */ minor typos.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{languageReferenceNavbar|Attributes}}&lt;br /&gt;
&lt;br /&gt;
Various definitions and declarations can be annotated with attributes. This section describes the general syntax of attributes and where they can be placed. It also describes the meaning of the specific attributes.&lt;br /&gt;
&lt;br /&gt;
=== Syntax ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;Attributes&amp;gt; :&lt;br /&gt;
    [ &amp;lt;Attribute&amp;gt;-comma-sep-list ]&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;Attribute&amp;gt; : one of&lt;br /&gt;
    &amp;lt;LowerCaseIdentifier&amp;gt;&lt;br /&gt;
    &amp;lt;LowerCaseIdentifier&amp;gt; ( &amp;lt;Literal&amp;gt;-comma-sep-list )&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the literals must either be numbers or string literals.&lt;br /&gt;
&lt;br /&gt;
=== Insertion Points ===&lt;br /&gt;
&lt;br /&gt;
The attributes of interfaces, classes and implementations are right after the scope qualifications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;InterfaceDeclaration&amp;gt; :&lt;br /&gt;
    interface &amp;lt;IinterfaceName&amp;gt;&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
end interface &amp;lt;IinterfaceName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ClassDeclaration&amp;gt; :&lt;br /&gt;
    class &amp;lt;ClassName&amp;gt; &amp;lt;ConstructionType&amp;gt;-opt&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
    end class &amp;lt;ClassName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ClassImplementation&amp;gt; :&lt;br /&gt;
   implement &amp;lt;ClassName&amp;gt;&lt;br /&gt;
        &amp;lt;ScopeQualifications&amp;gt;&lt;br /&gt;
        &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;Sections&amp;gt;&lt;br /&gt;
    end implement &amp;lt;ClassName&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attributes of constants, domains, predicates, properties and facts are at the end (i.e. right before the terminating dot).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;ConstantDefinition&amp;gt;: one of&lt;br /&gt;
    &amp;lt;ConstantName&amp;gt; = &amp;lt;ConstantValue&amp;gt; &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;ConstantName&amp;gt; : &amp;lt;TypeName&amp;gt; = &amp;lt;ConstantValue&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;DomainDefinition&amp;gt;:&lt;br /&gt;
    &amp;lt;DomainName&amp;gt; &amp;lt;FormalTypeParameterList&amp;gt;-opt = &amp;lt;TypeExpression&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;PredicateDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;PredicateName&amp;gt; : &amp;lt;PredicateDomain&amp;gt; &amp;lt;LinkName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;PredicateName&amp;gt; : &amp;lt;PredicateDomainName&amp;gt; &amp;lt;LinkName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;PropertyDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;PropertyName&amp;gt; : &amp;lt;PropertyType&amp;gt; &amp;lt;FlowPattern&amp;gt;-list-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;FactDeclaration&amp;gt; :&lt;br /&gt;
    &amp;lt;FactVariableDeclaration&amp;gt; &amp;lt;Attributes&amp;gt;-opt&lt;br /&gt;
    &amp;lt;FactFunctorDeclaration&amp;gt; &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The attributes of formal arguments are at the end.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vipbnf&amp;gt;&amp;lt;FormalArgument&amp;gt; :&lt;br /&gt;
    &amp;lt;TypeExpression&amp;gt; &amp;lt;ArgumentName&amp;gt;-opt &amp;lt;Attributes&amp;gt;-opt&amp;lt;/vipbnf&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Specific Attributes ===&lt;br /&gt;
&lt;br /&gt;
==== byVal ====&lt;br /&gt;
&lt;br /&gt;
An argument is transferred directly on the stack rather than using a pointer.  Valid for formal predicate arguments provided the &amp;lt;vp&amp;gt;language&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;stdcall&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;apicall&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;c&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    externalP : (point Point [byVal]) language apicall.&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== deprecated ====&lt;br /&gt;
&lt;br /&gt;
The declared entity is deprecated. The string literal describes how to migrate from it.  The entity still exists, but usage will cause a warning.&lt;br /&gt;
The entity will not exist in future versions of Visual Prolog.  Valid for member declarations and scopes.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    oldFashioned : (string Arg) [deprecated(&amp;quot;Use newFashion instead&amp;quot;)].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== formatString ====&lt;br /&gt;
&lt;br /&gt;
The argument is a {{lang2|Domains|Format Strings|format string}} for a subsequent ellipsis argument (i.e. &amp;lt;vp&amp;gt;...&amp;lt;/vp&amp;gt;).  Valid for one string argument of a predicate with an ellipsis argument.  The use of formatString will make the compiler check the validity of actual arguments with respect to actual format strings (where possible).&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    writef : (string Format [formatString], ...).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== in ====&lt;br /&gt;
&lt;br /&gt;
The argument is an input argument.  Valid for a formal predicate argument.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string InputArg [in]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== inline ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; alters the memory layout of a struct (i.e. a single alternative functor domain with an align qualification).  The purpose of the attribute is to ease interfacing to foreign languages and should normally not be used for pure Visual Prolog.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; can be used in three cases:&lt;br /&gt;
&lt;br /&gt;
* inlining a struct rather than having a pointer to the struct&lt;br /&gt;
* inlining a fixed size string&lt;br /&gt;
* inlining a fixed number of bytes&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline&amp;lt;/vp&amp;gt; on struct field in another struct its data will be inlined instead of having a pointer to the struct&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    point =p(integer X, integer Y).&lt;br /&gt;
&lt;br /&gt;
domains&lt;br /&gt;
    rectangle =&lt;br /&gt;
       r(&lt;br /&gt;
            point UpperLeft [inline],&lt;br /&gt;
            point LowerRight [inline]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
Since &amp;lt;vp&amp;gt;UpperLeft&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;LowerRight&amp;lt;/vp&amp;gt; are inlined the struct have the same memory layout as this one:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    rectangle2 =&lt;br /&gt;
        r2(&lt;br /&gt;
            integer UpperLeft_X,&lt;br /&gt;
            integer UpperLeft_Y,&lt;br /&gt;
            integer LowerRight_X,&lt;br /&gt;
            integer LowerRight_Y&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline(&amp;lt;size&amp;gt;)&amp;lt;/vp&amp;gt; on a &amp;lt;vp&amp;gt;string&amp;lt;/vp&amp;gt; or a &amp;lt;vp&amp;gt;string8&amp;lt;/vp&amp;gt; field in a struct, the struct will contain a fixed size string with &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; characters (i.e. &amp;lt;vp&amp;gt;char&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;char8&amp;lt;/vp&amp;gt;, respectively).  The strings will be zero terminated if they are shorter than &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt;, but not if they have &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; characters.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    device =&lt;br /&gt;
        device(&lt;br /&gt;
            integer Id,&lt;br /&gt;
            string DeviceName [inline(12)]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&amp;lt;vp&amp;gt;DeviceName&amp;lt;/vp&amp;gt; is an inlined Unicode string of length 12.&lt;br /&gt;
The struct have the same layout as:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    device =&lt;br /&gt;
        device(&lt;br /&gt;
            integer Id,&lt;br /&gt;
            char DeviceName_01,&lt;br /&gt;
            char DeviceName_02,&lt;br /&gt;
            char DeviceName_03,&lt;br /&gt;
            char DeviceName_04,&lt;br /&gt;
            char DeviceName_05,&lt;br /&gt;
            char DeviceName_06,&lt;br /&gt;
            char DeviceName_07,&lt;br /&gt;
            char DeviceName_08,&lt;br /&gt;
            char DeviceName_09,&lt;br /&gt;
            char DeviceName_10,&lt;br /&gt;
            char DeviceName_11,&lt;br /&gt;
            char DeviceName_12&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
When using &amp;lt;vp&amp;gt;inline(&amp;lt;size&amp;gt;)&amp;lt;/vp&amp;gt; on the &amp;lt;vp&amp;gt;pointer&amp;lt;/vp&amp;gt; type the struct will contain &amp;lt;vp&amp;gt;&amp;lt;size&amp;gt;&amp;lt;/vp&amp;gt; bytes, and the pointer will become a pointer to that field:&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    mark =&lt;br /&gt;
        mark(&lt;br /&gt;
            integer Position,&lt;br /&gt;
            pointer Data [8]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&amp;lt;vp&amp;gt;Data&amp;lt;/vp&amp;gt; is pointer to 8 inlined bytes&lt;br /&gt;
The struct have the same layout as:&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    mark2 =&lt;br /&gt;
        mark2(&lt;br /&gt;
            integer Position,&lt;br /&gt;
            byte Data_1,&lt;br /&gt;
            byte Data_2,&lt;br /&gt;
            byte Data_3,&lt;br /&gt;
            byte Data_4,&lt;br /&gt;
            byte Data_5,&lt;br /&gt;
            byte Data_6,&lt;br /&gt;
            byte Data_7,&lt;br /&gt;
            byte Data_8&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
And &amp;lt;vp&amp;gt;Data&amp;lt;/vp&amp;gt; will point to &amp;lt;vp&amp;gt;Data_1&amp;lt;/vp&amp;gt;.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== noDefaultConstructor ====&lt;br /&gt;
&lt;br /&gt;
Used for a class to indicate that it should not have an implicit default constructor, and can thus be used to a class that does not have any public constructors at all.  Valid for an object creating class declaration.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;class classWithoutPublicConstructors : myInterface&lt;br /&gt;
    open core&lt;br /&gt;
    [noDefaultConstructor]&lt;br /&gt;
...&lt;br /&gt;
end class classWithoutPublicConstructors&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== out ====&lt;br /&gt;
&lt;br /&gt;
The argument is an output argument. Valid for a formal predicate argument.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string OutputArg [out]).&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== programPoint ====&lt;br /&gt;
&lt;br /&gt;
Used for a predicate or constructor declaration to indicate that it recieves an extra input argument which describes the place in a program (program point) where this predicate was called. This additional argument has programPoint type which is declared in the PFC core class like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    programPoint = programPoint(hScopeDescriptor ClassDescriptor, string PredicateName, sourceCursor Cursor).&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The clause name of such predicate or constructor should have suffix &amp;quot;_explicit&amp;quot;. Valid for a predicate or constructor declaration.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    pred : (string String) [programPoint].&lt;br /&gt;
clauses&lt;br /&gt;
    pred_explicit(ProgramPoint, String) :-&lt;br /&gt;
        ...&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;vp&amp;gt;programPoint&amp;lt;/vp&amp;gt; attribute is described in more details in the description of [[Language Reference/Predicates#programPoint|Predicates]], and it is a vital part of the exception system, see [[Exception Handling]].&lt;br /&gt;
&lt;br /&gt;
==== retired ====&lt;br /&gt;
&lt;br /&gt;
The declared entity is retired. The string literal describes how to migrate from it.  The entity does not exist anymore.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    veryOldFasioned : (string Arg) [retired(&amp;quot;Use newFasion instead&amp;quot;)].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== sealed ====&lt;br /&gt;
&lt;br /&gt;
Used for an interface to indicate that it cannot be supported by any other interface. This allows to create more efficient codes, because the compiler provides some optimization when using the objects of such type. Valid for an object creating class declaration as a construction interface.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;interface myInterface&lt;br /&gt;
    [sealed]&lt;br /&gt;
...&lt;br /&gt;
end interface myInterface&lt;br /&gt;
&lt;br /&gt;
class myClass : myInterface&lt;br /&gt;
...&lt;br /&gt;
end class myClass&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== union ====&lt;br /&gt;
&lt;br /&gt;
Used for creating functor domains with several alternatives but no real functors.  This should only be used to mimic C/C++ union structs in low-level interfacing.  Valid for functor domain with several alternatives and alignment.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    u64var = align 4&lt;br /&gt;
        u64(unsigned64 Value64);&lt;br /&gt;
        u64_struct(unsigned Low32, unsigned High32)&lt;br /&gt;
        [union].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== used ====&lt;br /&gt;
&lt;br /&gt;
An unused local member can be marked &amp;lt;vp&amp;gt;used&amp;lt;/vp&amp;gt; to prevent the compiler to issue a warning and remove the corresponding code. Valid for members.&lt;br /&gt;
&lt;br /&gt;
{{Example|&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    seeminglyUnused : () [used].&amp;lt;/vip&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== default ====&lt;br /&gt;
&lt;br /&gt;
{{Template:NonReleased}}&lt;br /&gt;
&lt;br /&gt;
It is problematic to update functor domains, if terms have been persisted in serialized form.  Because new programs cannot deal with old serializations.&lt;br /&gt;
&lt;br /&gt;
The two attributes &amp;lt;vp&amp;gt;default/1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;retiredFunctor/1&amp;lt;/vp&amp;gt; can help dealing with this problem.&lt;br /&gt;
&lt;br /&gt;
The last arguments of a functor can have default values:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    ppp =&lt;br /&gt;
        fct(&lt;br /&gt;
            integer A,&lt;br /&gt;
            real B,&lt;br /&gt;
            ddd C,&lt;br /&gt;
            integer* New [default([])],&lt;br /&gt;
            real New2 [default(0)]&lt;br /&gt;
        ).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The default attribute does not change anything within the program; it only affects the deserialization.  If during deserialization we meet the closing parenthesis too soon we supply default values for the remaining arguments.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Notice&amp;#039;&amp;#039;&amp;#039; that this will only work for &amp;#039;&amp;#039;&amp;#039;text&amp;#039;&amp;#039;&amp;#039; deserialization.&lt;br /&gt;
&lt;br /&gt;
See also [[Functor Domain Versioning]].&lt;br /&gt;
&lt;br /&gt;
==== retiredFunctor ====&lt;br /&gt;
&lt;br /&gt;
{{Template:NonReleased}}&lt;br /&gt;
&lt;br /&gt;
Functor alternatives can be retired.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    ppp =&lt;br /&gt;
        fct(integer A, real B, ddd C) [retiredFunctor(aaa::fct_ppp)];&lt;br /&gt;
        fct2(integer A, ddd C, integer* New).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;aaa::fct_ppp&amp;lt;/vp&amp;gt; must be a predicate with this type (it can be an anonymous predicate):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;predicates&lt;br /&gt;
    fct_ppp : (integer A, real B, ddd C) -&amp;gt; ppp NewValue.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e. it takes the arguments of the old functor and returns a value in the functor domain.&lt;br /&gt;
&lt;br /&gt;
In the program fct is does not exist at all, it is retired.  But in the deserialization fct terms can still be handled:  The arguments are parsed according to the types, and then the value is created by invoking &amp;lt;vp&amp;gt;aaa::fct_ppp&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
This method will also work for binary serializations, provided:&lt;br /&gt;
&lt;br /&gt;
* The old domain had more than one alternative (so there are functor numbers in the serialization)&lt;br /&gt;
* New alternatives are added last (to retain functor numbers) &lt;br /&gt;
&lt;br /&gt;
It is however not recommend using binary serialization for inter-session persistence.&lt;br /&gt;
&lt;br /&gt;
See also [[Functor Domain Versioning]].&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Collection_library&amp;diff=4340</id>
		<title>Collection library</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Collection_library&amp;diff=4340"/>
		<updated>2016-11-03T13:14:11Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Keyword recognition */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A collection is a value that contains a number (i.e. from zero to infinite) of other values.&lt;br /&gt;
&lt;br /&gt;
Lists, sets, dictionaries, arrays, hash tables, (nondeterm) fact databases, etc are all collections.&lt;br /&gt;
&lt;br /&gt;
A collection can also contain one value, but data types that by nature always contains exactly one value is not a collection.  So a list with one element is a collection, but a variable which also contains one element is not a collection.&lt;br /&gt;
&lt;br /&gt;
[[PFC]] contains an object oriented collection library with both persistent and modifiable collections.  The collection library is based around a set of generic interfaces defining various conceptual collection types, and a number of classes implementing these interfaces.&lt;br /&gt;
&lt;br /&gt;
== Persistent and Modifiable ==&lt;br /&gt;
&lt;br /&gt;
Collections can be persistent or modifiable:&lt;br /&gt;
&lt;br /&gt;
; Persistent&lt;br /&gt;
: A persistent collection never changes members.  Meaning that when you insert or remove a member from persistent collection, you will receive a new collection and the old one will stay unchanged.  Prolog lists is a characteristic example of persistent data types: When you match a list against a pattern to get the head and the tail, the original list stays intact; when you create a list from a new head and an existing list, both the new and the old list will exist.&lt;br /&gt;
&lt;br /&gt;
; Modifiable&lt;br /&gt;
: Members can be added to and removed from a modifiable collection.  Meaning that when you insert or remove a member from a modifiable collection, the old value of the collection is lost only the one exists.  I.e. modifiable collections are updated destructively.  Visual Prolog fact databases is a characteristic example of modifiable data types.  When you assert or retract facts the database is modified and the original fact database is no longer accessible.&lt;br /&gt;
&lt;br /&gt;
== Collection Interfaces &amp;amp; Classes ==&lt;br /&gt;
&lt;br /&gt;
There is no fundamental difference in retrieving data from persistent and modifiable collection.  The difference lies in the updating of the collections.&lt;br /&gt;
&lt;br /&gt;
Therefore retrieval is done through interfaces that are shared by persistent and modifiable collections, whereas updating is done through separate persistent and modifiable interfaces.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; is most fundamental interface for retrieving inspecting collections all collections supports this interface.  It contains methods:&lt;br /&gt;
* &amp;lt;vp&amp;gt;isEmpty&amp;lt;/vp&amp;gt; determining whether the collection is empty&lt;br /&gt;
* &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; testing whether a certain element is in the collection&lt;br /&gt;
* &amp;lt;vp&amp;gt;tryGetFirst&amp;lt;/vp&amp;gt; obtaining the first element in the collection.&lt;br /&gt;
* &amp;lt;vp&amp;gt;getAll_nd&amp;lt;/vp&amp;gt; enumerating all elements in the collection.&lt;br /&gt;
* &amp;lt;vp&amp;gt;asList&amp;lt;/vp&amp;gt; getting the collection in list form.&lt;br /&gt;
&lt;br /&gt;
Notice that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; means different things in different kinds of collections.  In a hash table it may for example be the element with the first hash key.&lt;br /&gt;
&lt;br /&gt;
Likewise, the order in which &amp;lt;vp&amp;gt;getAll_nd&amp;lt;/vp&amp;gt; enumerates its elements depends on the collection kind.&lt;br /&gt;
&lt;br /&gt;
Also notice that the performance of some operations may be very poor in some kind of collections. &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; is for example a bad operation on a priority queue, because a priority queue not designed to determine membership.&lt;br /&gt;
&lt;br /&gt;
All persistent collections also support the interface &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and all modifiable collections supports the interface &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt; have predicates with same names but different types, because the persistent version returns a new collection and the modifiable version doesn&amp;#039;t return anything.  These interfaces have methods:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;insert&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;insertList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;insertCollection&amp;lt;/vp&amp;gt; for inserting elements&lt;br /&gt;
* &amp;lt;vp&amp;gt;remove&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;removeList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;removeCollection&amp;lt;/vp&amp;gt; for removing elements&lt;br /&gt;
* &amp;lt;vp&amp;gt;tryRemoveFirst&amp;lt;/vp&amp;gt; for removing the first element&lt;br /&gt;
* &amp;lt;vp&amp;gt;clear&amp;lt;/vp&amp;gt; for removing all elements in the collection&lt;br /&gt;
&lt;br /&gt;
=== set ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;set&amp;#039;&amp;#039; is a collection of elements, which cannot contain duplicates.  The fundamental set operation is test of membership.  As such a set simply supports the &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; interface and one of the interfaces &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;setP_redBlack&amp;lt;/vp&amp;gt; implements persistent sets based on a red black tree representation.&lt;br /&gt;
* &amp;lt;vp&amp;gt;setM_redBlack&amp;lt;/vp&amp;gt; implements modifiable sets based on a red black tree representation.&lt;br /&gt;
&lt;br /&gt;
=== queue ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is a collection of elements which are ordered.  The fundamental operation is getting the first element is the queue.  Membership testing is typically inefficient.  The interfaces of queues is similar to that of sets, supporting &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; and one of the interfaces &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.  The names are different to stress the difference in intension and efficeincy.  And in future they may evolve differently.  PFC contains two kinds of queues:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is a &amp;quot;regular&amp;quot; FIFO (first-in-first-out) queue.&lt;br /&gt;
** &amp;lt;vp&amp;gt;queueP_fact&amp;lt;/vp&amp;gt; implements a persistent FIFO queue based on a fact containing an algebraic queue&lt;br /&gt;
** &amp;lt;vp&amp;gt;queueM_fact&amp;lt;/vp&amp;gt; implements a modifiable FIFO queue based on a nondeterm fact database&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is a queue where the first element is alwasy the element with hightst priority (i.e. the least, the greatest or based on a custom comparison).&lt;br /&gt;
** &amp;lt;vp&amp;gt;priorityQueueP_leftist&amp;lt;/vp&amp;gt; implements a persistent priority queue&lt;br /&gt;
** &amp;lt;vp&amp;gt;priorityQueueM_leftist&amp;lt;/vp&amp;gt; implements a modifiable priority queue&lt;br /&gt;
Both are based on a leftist tree (see [[wikipedia:Leftist tree]]).&lt;br /&gt;
&lt;br /&gt;
=== map ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;map&amp;#039;&amp;#039;&amp;#039; is a function from &amp;#039;&amp;#039;keys&amp;#039;&amp;#039; to &amp;#039;&amp;#039;values&amp;#039;&amp;#039;.  Not all keys need have a value.  The basic operations is setting, getting and try-getting the value associated with a certain key.  When the value of a key is set the previous value of that key is &amp;quot;lost&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A map is a collection of (key,value)-tuples, so the &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; predicates all deal with such tuples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;map&amp;lt;/vp&amp;gt; is the basic interface for inspecting maps (persistent as well as modifiable).  It supports &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; of (key,value)-tuples and extends with predicates/properties:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;get&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;tryGet&amp;lt;/vp&amp;gt; for getting the value associated with a key&lt;br /&gt;
* &amp;lt;vp&amp;gt;getKey_nd&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;getValue_nd&amp;lt;/vp&amp;gt; for iterating the keys and values&lt;br /&gt;
* &amp;lt;vp&amp;gt;keyList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;valueList&amp;lt;/vp&amp;gt; for obtaining all the keys and values as lists.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;mapP&amp;lt;/vp&amp;gt; (persistent maps) and &amp;lt;vp&amp;gt;mapM&amp;lt;/vp&amp;gt; (modifiable maps) supports &amp;lt;vp&amp;gt;map&amp;lt;/vp&amp;gt; and either &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;, and extends with:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;set&amp;lt;/vp&amp;gt; for setting the value associated with a key (the previous value associated with that key is &amp;quot;lost&amp;quot;)&lt;br /&gt;
* &amp;lt;vp&amp;gt;removeKey&amp;lt;/vp&amp;gt; for removing a key and its associated value&lt;br /&gt;
* &amp;lt;vp&amp;gt;removeKeyList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;removeKeyCollection&amp;lt;/vp&amp;gt; for removing a number of keys and their associated values)&lt;br /&gt;
&lt;br /&gt;
PFC contains persistent and modifiable map classes based on red-black trees:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;mapP_redBlack&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;mapM_redBlack&amp;lt;/vp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Usage examples ==&lt;br /&gt;
&lt;br /&gt;
The examples in this section are inspired by the examples in &amp;#039;&amp;#039;The C5 Generic Collection Library for C# and CLI&amp;#039;&amp;#039;, Niels Kokholm and Peter Sestoft, IT University Technical Report Series TR-2006-76, 2006.&lt;br /&gt;
&lt;br /&gt;
=== Keyword recognition ===&lt;br /&gt;
&lt;br /&gt;
It is often necessary to decide whether a word is member of some fixed collection of words.  For example the keywords of a programming language, or frequent and therefore insignificant (so called stop-words) when indexing text.&lt;br /&gt;
&lt;br /&gt;
Basically, we want to implement a class with a single deterministic predicate that can determine whether a word is a keyword:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class vipKeyword&lt;br /&gt;
predicates&lt;br /&gt;
    isKeyword : (string Word) determ.&lt;br /&gt;
end class vipKeyword&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a obvious choice to base the implementation on a set of keywords, because member-ship testing is the fundamantal operation of sets. The implementation can look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement vipKeyword&lt;br /&gt;
clauses&lt;br /&gt;
    isKeyword(Word) :-&lt;br /&gt;
        keywordSet:contains(Word).&lt;br /&gt;
&lt;br /&gt;
class facts&lt;br /&gt;
    keywordSet : setM{string Keyword} := initKeywordSet().&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    initKeywordSet : () -&amp;gt; setM{string Keyword}.&lt;br /&gt;
clauses&lt;br /&gt;
    initKeywordSet() = S :-&lt;br /&gt;
        S = setM_redBlack::new(),&lt;br /&gt;
        S:insertList(keywords).&lt;br /&gt;
&lt;br /&gt;
constants&lt;br /&gt;
    keywords : string* = [&amp;quot;as&amp;quot;, &amp;quot;class&amp;quot;, &amp;quot;clauses&amp;quot;, ...].&lt;br /&gt;
end implement vipKeyword&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;isKeyword&amp;lt;/vp&amp;gt; simply determines whether the the &amp;lt;vp&amp;gt;keywordSet&amp;lt;/vp&amp;gt; &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; the &amp;lt;vp&amp;gt;Word&amp;lt;/vp&amp;gt; in question.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;keywordSet&amp;lt;/vp&amp;gt; is a (modifiable) set of strings, which is initialized to contain the actual keywords from the &amp;lt;vp&amp;gt;keywords&amp;lt;/vp&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Notice that you should be careful when initializing class facts like it is done above.  Such initialization is done in an unpredictable order before entering the goal of the program.  So first of all it is difficult to know which other parts of the program that has already been initialized.  Secondly, the exception system is not initialized properly so exceptions will be reported in a rather low-level fasion.&lt;br /&gt;
&lt;br /&gt;
=== Text Concordance ===&lt;br /&gt;
&lt;br /&gt;
A text concordance is a list of words in a text and the lines on which the word occurs, i.e. an index of text.&lt;br /&gt;
&lt;br /&gt;
We will represent that concordance as a map from &amp;lt;vp&amp;gt;Word&amp;lt;/vp&amp;gt; to a set of &amp;lt;vp&amp;gt;Linenumber&amp;lt;/vp&amp;gt;&amp;#039;s, where word is a string and line is an integer.  For convenience we will define a domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    concordance = mapM{string Word, setM{integer Linenumber}}.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To build a concordance from a stream (typically a file), we iterate the lines in the file, and the words of the line.  For each word we insert it in the relevant line number set, which will have to be created if it does not already exist:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    build : (inputStream Input) -&amp;gt; concordance Concordance.&lt;br /&gt;
clauses&lt;br /&gt;
    build(Input) = Concordance :-&lt;br /&gt;
        Concordance = mapM_redBlack::new(),&lt;br /&gt;
        foreach&lt;br /&gt;
            tuple(Line, Linenumber) = getLine_nd(Input, 1),&lt;br /&gt;
            Word = getWord_nd(Line)&lt;br /&gt;
        do&lt;br /&gt;
            if LinenumberSet = Concordance:tryGet(Word) then&lt;br /&gt;
            else&lt;br /&gt;
                LinenumberSet = setM_redBlack::new(),&lt;br /&gt;
                Concordance:set(Word, LinenumberSet)&lt;br /&gt;
            end if,&lt;br /&gt;
            LinenumberSet:insert(Linenumber)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Though not important for the main topic the iteration predicates are here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    getLine_nd : (inputStream Input, integer NextLine) -&amp;gt; tuple{string Line, integer Linenumber} nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getLine_nd(Input, _NextLine) = _ :-&lt;br /&gt;
        Input:endOfStream(),&lt;br /&gt;
        !,&lt;br /&gt;
        fail.&lt;br /&gt;
&lt;br /&gt;
    getLine_nd(Input, NextLine) = tuple(Line, NextLine) :-&lt;br /&gt;
        Line = Input:readLine().&lt;br /&gt;
&lt;br /&gt;
    getLine_nd(Input, NextLine) = getLine_nd(Input, NextLine+1).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    getWord_nd : (string Line) -&amp;gt; string Word nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getWord_nd(Line) = getWord2_nd(First, Rest) :-&lt;br /&gt;
        string::frontToken(Line, First, Rest).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    getWord2_nd : (string First, string Rest) -&amp;gt; string Word nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getWord2_nd(First, _Rest) = string::toLowerCase(First) :-&lt;br /&gt;
        string::length(First) &amp;gt; 2, % only words with more than 2 letters&lt;br /&gt;
        not(regexp::search(&amp;quot;[^a-zA-Z]&amp;quot;, First, _, _)). % not a word if it contains non-letters&lt;br /&gt;
&lt;br /&gt;
    getWord2_nd(_First, Rest) = getWord_nd(Rest).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From a concordance we can print an index, by iterating the &amp;lt;vp&amp;gt;(Word, LinenumberSet)&amp;lt;/vp&amp;gt; tuples in the &amp;lt;vp&amp;gt;Concordance&amp;lt;/vp&amp;gt;, and the &amp;lt;vp&amp;gt;Linenumber&amp;lt;/vp&amp;gt;&amp;#039;s in the &amp;lt;vp&amp;gt;LinenumberSet&amp;lt;/vp&amp;gt;&amp;#039;s.  Since both the maps and the sets are based on red-black trees everything is automatically sorted for us:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    print : (outputStream Output, concordance Concordance).&lt;br /&gt;
clauses&lt;br /&gt;
    print(Output, Concordance) :-&lt;br /&gt;
        foreach tuple(Word, LinenumberSet) = Concordance:getAll_nd() do&lt;br /&gt;
            Output:writef(&amp;quot;%:\n&amp;quot;, Word),&lt;br /&gt;
            foreach Linenumber = LinenumberSet:getAll_nd() do&lt;br /&gt;
                Output:writef(&amp;quot;   %\n&amp;quot;, Linenumber)&lt;br /&gt;
            end foreach,&lt;br /&gt;
            Output:write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* [http://www.itu.dk/research/c5/ The C5 Generic Collection Library for C# and CLI]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Data types and handling]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Collection_library&amp;diff=4339</id>
		<title>Collection library</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Collection_library&amp;diff=4339"/>
		<updated>2016-11-03T12:40:42Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* queue */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A collection is a value that contains a number (i.e. from zero to infinite) of other values.&lt;br /&gt;
&lt;br /&gt;
Lists, sets, dictionaries, arrays, hash tables, (nondeterm) fact databases, etc are all collections.&lt;br /&gt;
&lt;br /&gt;
A collection can also contain one value, but data types that by nature always contains exactly one value is not a collection.  So a list with one element is a collection, but a variable which also contains one element is not a collection.&lt;br /&gt;
&lt;br /&gt;
[[PFC]] contains an object oriented collection library with both persistent and modifiable collections.  The collection library is based around a set of generic interfaces defining various conceptual collection types, and a number of classes implementing these interfaces.&lt;br /&gt;
&lt;br /&gt;
== Persistent and Modifiable ==&lt;br /&gt;
&lt;br /&gt;
Collections can be persistent or modifiable:&lt;br /&gt;
&lt;br /&gt;
; Persistent&lt;br /&gt;
: A persistent collection never changes members.  Meaning that when you insert or remove a member from persistent collection, you will receive a new collection and the old one will stay unchanged.  Prolog lists is a characteristic example of persistent data types: When you match a list against a pattern to get the head and the tail, the original list stays intact; when you create a list from a new head and an existing list, both the new and the old list will exist.&lt;br /&gt;
&lt;br /&gt;
; Modifiable&lt;br /&gt;
: Members can be added to and removed from a modifiable collection.  Meaning that when you insert or remove a member from a modifiable collection, the old value of the collection is lost only the one exists.  I.e. modifiable collections are updated destructively.  Visual Prolog fact databases is a characteristic example of modifiable data types.  When you assert or retract facts the database is modified and the original fact database is no longer accessible.&lt;br /&gt;
&lt;br /&gt;
== Collection Interfaces &amp;amp; Classes ==&lt;br /&gt;
&lt;br /&gt;
There is no fundamental difference in retrieving data from persistent and modifiable collection.  The difference lies in the updating of the collections.&lt;br /&gt;
&lt;br /&gt;
Therefore retrieval is done through interfaces that are shared by persistent and modifiable collections, whereas updating is done through separate persistent and modifiable interfaces.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; is most fundamental interface for retrieving inspecting collections all collections supports this interface.  It contains methods:&lt;br /&gt;
* &amp;lt;vp&amp;gt;isEmpty&amp;lt;/vp&amp;gt; determining whether the collection is empty&lt;br /&gt;
* &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; testing whether a certain element is in the collection&lt;br /&gt;
* &amp;lt;vp&amp;gt;tryGetFirst&amp;lt;/vp&amp;gt; obtaining the first element in the collection.&lt;br /&gt;
* &amp;lt;vp&amp;gt;getAll_nd&amp;lt;/vp&amp;gt; enumerating all elements in the collection.&lt;br /&gt;
* &amp;lt;vp&amp;gt;asList&amp;lt;/vp&amp;gt; getting the collection in list form.&lt;br /&gt;
&lt;br /&gt;
Notice that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; means different things in different kinds of collections.  In a hash table it may for example be the element with the first hash key.&lt;br /&gt;
&lt;br /&gt;
Likewise, the order in which &amp;lt;vp&amp;gt;getAll_nd&amp;lt;/vp&amp;gt; enumerates its elements depends on the collection kind.&lt;br /&gt;
&lt;br /&gt;
Also notice that the performance of some operations may be very poor in some kind of collections. &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; is for example a bad operation on a priority queue, because a priority queue not designed to determine membership.&lt;br /&gt;
&lt;br /&gt;
All persistent collections also support the interface &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and all modifiable collections supports the interface &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.  &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt; have predicates with same names but different types, because the persistent version returns a new collection and the modifiable version doesn&amp;#039;t return anything.  These interfaces have methods:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;insert&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;insertList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;insertCollection&amp;lt;/vp&amp;gt; for inserting elements&lt;br /&gt;
* &amp;lt;vp&amp;gt;remove&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;removeList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;removeCollection&amp;lt;/vp&amp;gt; for removing elements&lt;br /&gt;
* &amp;lt;vp&amp;gt;tryRemoveFirst&amp;lt;/vp&amp;gt; for removing the first element&lt;br /&gt;
* &amp;lt;vp&amp;gt;clear&amp;lt;/vp&amp;gt; for removing all elements in the collection&lt;br /&gt;
&lt;br /&gt;
=== set ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;set&amp;#039;&amp;#039; is a collection of elements, which cannot contain duplicates.  The fundamental set operation is test of membership.  As such a set simply supports the &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; interface and one of the interfaces &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;setP_redBlack&amp;lt;/vp&amp;gt; implements persistent sets based on a red black tree representation.&lt;br /&gt;
* &amp;lt;vp&amp;gt;setM_redBlack&amp;lt;/vp&amp;gt; implements modifiable sets based on a red black tree representation.&lt;br /&gt;
&lt;br /&gt;
=== queue ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is a collection of elements which are ordered.  The fundamental operation is getting the first element is the queue.  Membership testing is typically inefficient.  The interfaces of queues is similar to that of sets, supporting &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; and one of the interfaces &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;.  The names are different to stress the difference in intension and efficeincy.  And in future they may evolve differently.  PFC contains two kinds of queues:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;queue&amp;#039;&amp;#039; is a &amp;quot;regular&amp;quot; FIFO (first-in-first-out) queue.&lt;br /&gt;
** &amp;lt;vp&amp;gt;queueP_fact&amp;lt;/vp&amp;gt; implements a persistent FIFO queue based on a fact containing an algebraic queue&lt;br /&gt;
** &amp;lt;vp&amp;gt;queueM_fact&amp;lt;/vp&amp;gt; implements a modifiable FIFO queue based on a nondeterm fact database&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; is a queue where the first element is alwasy the element with hightst priority (i.e. the least, the greatest or based on a custom comparison).&lt;br /&gt;
** &amp;lt;vp&amp;gt;priorityQueueP_leftist&amp;lt;/vp&amp;gt; implements a persistent priority queue&lt;br /&gt;
** &amp;lt;vp&amp;gt;priorityQueueM_leftist&amp;lt;/vp&amp;gt; implements a modifiable priority queue&lt;br /&gt;
Both are based on a leftist tree (see [[wikipedia:Leftist tree]]).&lt;br /&gt;
&lt;br /&gt;
=== map ===&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;map&amp;#039;&amp;#039;&amp;#039; is a function from &amp;#039;&amp;#039;keys&amp;#039;&amp;#039; to &amp;#039;&amp;#039;values&amp;#039;&amp;#039;.  Not all keys need have a value.  The basic operations is setting, getting and try-getting the value associated with a certain key.  When the value of a key is set the previous value of that key is &amp;quot;lost&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A map is a collection of (key,value)-tuples, so the &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; predicates all deal with such tuples.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;map&amp;lt;/vp&amp;gt; is the basic interface for inspecting maps (persistent as well as modifiable).  It supports &amp;lt;vp&amp;gt;collection&amp;lt;/vp&amp;gt; of (key,value)-tuples and extends with predicates/properties:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;get&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;tryGet&amp;lt;/vp&amp;gt; for getting the value associated with a key&lt;br /&gt;
* &amp;lt;vp&amp;gt;getKey_nd&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;getValue_nd&amp;lt;/vp&amp;gt; for iterating the keys and values&lt;br /&gt;
* &amp;lt;vp&amp;gt;keyList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;valueList&amp;lt;/vp&amp;gt; for obtaining all the keys and values as lists.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;mapP&amp;lt;/vp&amp;gt; (persistent maps) and &amp;lt;vp&amp;gt;mapM&amp;lt;/vp&amp;gt; (modifiable maps) supports &amp;lt;vp&amp;gt;map&amp;lt;/vp&amp;gt; and either &amp;lt;vp&amp;gt;persistent&amp;lt;/vp&amp;gt; or &amp;lt;vp&amp;gt;modifiable&amp;lt;/vp&amp;gt;, and extends with:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;set&amp;lt;/vp&amp;gt; for setting the value associated with a key (the previous value associated with that key is &amp;quot;lost&amp;quot;)&lt;br /&gt;
* &amp;lt;vp&amp;gt;removeKey&amp;lt;/vp&amp;gt; for removing a key and its associated value&lt;br /&gt;
* &amp;lt;vp&amp;gt;removeKeyList&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;removeKeyCollection&amp;lt;/vp&amp;gt; for removing a number of keys and their associated values)&lt;br /&gt;
&lt;br /&gt;
PFC contains persistent and modifiable map classes based on red-black trees:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;mapP_redBlack&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;mapM_redBlack&amp;lt;/vp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Usage examples ==&lt;br /&gt;
&lt;br /&gt;
The examples in this section are inspired by the examples in &amp;#039;&amp;#039;The C5 Generic Collection Library for C# and CLI&amp;#039;&amp;#039;, Niels Kokholm and Peter Sestoft, IT University Technical Report Series TR-2006-76, 2006.&lt;br /&gt;
&lt;br /&gt;
=== Keyword recognition ===&lt;br /&gt;
&lt;br /&gt;
It is often necessary to decide whether a word is member of some fixed collection of words.  For example the keywords of a programming language, or frequent and therefore insignificant (so called stop-words) when indexing text.&lt;br /&gt;
&lt;br /&gt;
Basically, we want to implement a class with a single deterministic predicate that can determine whether a word is a keyword:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class vipKeyword&lt;br /&gt;
predicates&lt;br /&gt;
    isKeyword : (string Word) determ.&lt;br /&gt;
end class vipKeyword&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a obvious choice to base the implementation on a set of keywords, because member-ship testing is the fundamantal operation of sets. The implementation can look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;implement vipKeyword&lt;br /&gt;
clauses&lt;br /&gt;
    isKeyword(Word) :-&lt;br /&gt;
        keywordSet:contains(Word).&lt;br /&gt;
&lt;br /&gt;
class facts&lt;br /&gt;
    keywordSet : setM{string Keyword} := initKeywordSet().&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    initKeywordSet : () -&amp;gt; setM{string Keyword}.&lt;br /&gt;
clauses&lt;br /&gt;
    initKeywordSet() = S :-&lt;br /&gt;
        S = setM_redBlack::new(),&lt;br /&gt;
        S:insertList(keywords).&lt;br /&gt;
&lt;br /&gt;
constants&lt;br /&gt;
    keywords : string* = [&amp;quot;as&amp;quot;, &amp;quot;class&amp;quot;, &amp;quot;clauses&amp;quot;, ...].&lt;br /&gt;
end implement vipKeyword&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;isKeyword&amp;lt;/vp&amp;gt; simply determines whether the the &amp;lt;vp&amp;gt;keywordSet&amp;lt;/vp&amp;gt; &amp;lt;vp&amp;gt;contains&amp;lt;/vp&amp;gt; the &amp;lt;vp&amp;gt;Word&amp;lt;/vp&amp;gt; in question.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vp&amp;gt;keywordSet&amp;lt;/vp&amp;gt; is a (modifiable) set of strings, which is initialized to contain the actual keywords from the &amp;lt;vp&amp;gt;keywords&amp;lt;/vp&amp;gt; constant.&lt;br /&gt;
&lt;br /&gt;
Notice that you should be carefull when initializing class facts like it is done above.  Such initialization is done in an unpredictable order before entering the goal of the program.  So first of all it is difficult to know which other parts of the program that has already been initialized.  Secondly, the exception system is not initialized properly so exceptions will be reported in a rather low-level fasion.&lt;br /&gt;
&lt;br /&gt;
=== Text Concordance ===&lt;br /&gt;
&lt;br /&gt;
A text concordance is a list of words in a text and the lines on which the word occurs, i.e. an index of text.&lt;br /&gt;
&lt;br /&gt;
We will represent that concordance as a map from &amp;lt;vp&amp;gt;Word&amp;lt;/vp&amp;gt; to a set of &amp;lt;vp&amp;gt;Linenumber&amp;lt;/vp&amp;gt;&amp;#039;s, where word is a string and line is an integer.  For convenience we will define a domain:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;domains&lt;br /&gt;
    concordance = mapM{string Word, setM{integer Linenumber}}.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To build a concordance from a stream (typically a file), we iterate the lines in the file, and the words of the line.  For each word we insert it in the relevant line number set, which will have to be created if it does not already exist:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    build : (inputStream Input) -&amp;gt; concordance Concordance.&lt;br /&gt;
clauses&lt;br /&gt;
    build(Input) = Concordance :-&lt;br /&gt;
        Concordance = mapM_redBlack::new(),&lt;br /&gt;
        foreach&lt;br /&gt;
            tuple(Line, Linenumber) = getLine_nd(Input, 1),&lt;br /&gt;
            Word = getWord_nd(Line)&lt;br /&gt;
        do&lt;br /&gt;
            if LinenumberSet = Concordance:tryGet(Word) then&lt;br /&gt;
            else&lt;br /&gt;
                LinenumberSet = setM_redBlack::new(),&lt;br /&gt;
                Concordance:set(Word, LinenumberSet)&lt;br /&gt;
            end if,&lt;br /&gt;
            LinenumberSet:insert(Linenumber)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Though not important for the main topic the iteration predicates are here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    getLine_nd : (inputStream Input, integer NextLine) -&amp;gt; tuple{string Line, integer Linenumber} nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getLine_nd(Input, _NextLine) = _ :-&lt;br /&gt;
        Input:endOfStream(),&lt;br /&gt;
        !,&lt;br /&gt;
        fail.&lt;br /&gt;
&lt;br /&gt;
    getLine_nd(Input, NextLine) = tuple(Line, NextLine) :-&lt;br /&gt;
        Line = Input:readLine().&lt;br /&gt;
&lt;br /&gt;
    getLine_nd(Input, NextLine) = getLine_nd(Input, NextLine+1).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    getWord_nd : (string Line) -&amp;gt; string Word nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getWord_nd(Line) = getWord2_nd(First, Rest) :-&lt;br /&gt;
        string::frontToken(Line, First, Rest).&lt;br /&gt;
&lt;br /&gt;
class predicates&lt;br /&gt;
    getWord2_nd : (string First, string Rest) -&amp;gt; string Word nondeterm.&lt;br /&gt;
clauses&lt;br /&gt;
    getWord2_nd(First, _Rest) = string::toLowerCase(First) :-&lt;br /&gt;
        string::length(First) &amp;gt; 2, % only words with more than 2 letters&lt;br /&gt;
        not(regexp::search(&amp;quot;[^a-zA-Z]&amp;quot;, First, _, _)). % not a word if it contains non-letters&lt;br /&gt;
&lt;br /&gt;
    getWord2_nd(_First, Rest) = getWord_nd(Rest).&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From a concordance we can print an index, by iterating the &amp;lt;vp&amp;gt;(Word, LinenumberSet)&amp;lt;/vp&amp;gt; tuples in the &amp;lt;vp&amp;gt;Concordance&amp;lt;/vp&amp;gt;, and the &amp;lt;vp&amp;gt;Linenumber&amp;lt;/vp&amp;gt;&amp;#039;s in the &amp;lt;vp&amp;gt;LinenumberSet&amp;lt;/vp&amp;gt;&amp;#039;s.  Since both the maps and the sets are based on red-black trees everything is automatically sorted for us:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;class predicates&lt;br /&gt;
    print : (outputStream Output, concordance Concordance).&lt;br /&gt;
clauses&lt;br /&gt;
    print(Output, Concordance) :-&lt;br /&gt;
        foreach tuple(Word, LinenumberSet) = Concordance:getAll_nd() do&lt;br /&gt;
            Output:writef(&amp;quot;%:\n&amp;quot;, Word),&lt;br /&gt;
            foreach Linenumber = LinenumberSet:getAll_nd() do&lt;br /&gt;
                Output:writef(&amp;quot;   %\n&amp;quot;, Linenumber)&lt;br /&gt;
            end foreach,&lt;br /&gt;
            Output:write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
        end foreach.&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* [http://www.itu.dk/research/c5/ The C5 Generic Collection Library for C# and CLI]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Data types and handling]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=4338</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=4338"/>
		<updated>2016-11-03T11:53:29Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Inheritance */&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 well. An 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 types. The 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 efficiently, but it handles the inheritance even more efficiently.&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;
&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Object system]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=4337</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=4337"/>
		<updated>2016-11-03T11:51:27Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Supports (Type Extension) */&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 well. An 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 types. The 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;
&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Object system]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Introduction_to_Classes_and_Objects&amp;diff=4336</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=4336"/>
		<updated>2016-11-03T11:50:48Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Supports (Type Extension) */&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 well. An 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;
&lt;br /&gt;
[[ru:Введение в Классы и Объекты]]&lt;br /&gt;
[[Category:Object system]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Compound_and_List_Domains&amp;diff=4335</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=4335"/>
		<updated>2016-11-03T10:32:31Z</updated>

		<summary type="html">&lt;p&gt;Tbn: /* Multi-Level Compound Domains */&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, &amp;lt;vp&amp;gt;date(&amp;quot;April&amp;quot;, 14, 1960)&amp;lt;/vp&amp;gt; matches &amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt; and binds &amp;lt;vp&amp;gt;X&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;date(&amp;quot;April&amp;quot;,14,1960)&amp;lt;/vp&amp;gt;. Also &amp;lt;vp&amp;gt;date(&amp;quot;April&amp;quot;, 14, 1960)&amp;lt;/vp&amp;gt; matches &amp;lt;vp&amp;gt;date(Mo, Da, Yr)&amp;lt;/vp&amp;gt; and binds &amp;lt;vp&amp;gt;Mo&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;&amp;quot;April&amp;quot;&amp;lt;/vp&amp;gt;, &amp;lt;vp&amp;gt;Da&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;14&amp;lt;/vp&amp;gt;, and &amp;lt;vp&amp;gt;Yr&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;1960&amp;lt;/vp&amp;gt;.&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 Name, string Ph_num, birthday 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;, 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;, 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 Name [out], string Ph_num [out], birthday Birthday [out]) multi.&lt;br /&gt;
        get_months_birthdays : ().&lt;br /&gt;
        convert_month : (string Name, integer Num [out]) determ.&lt;br /&gt;
        check_birthday_month : (integer Month_num, birthday Birthday) determ.&lt;br /&gt;
        write_person : (name 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, author 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;
&lt;br /&gt;
[[ru:Cоставные Домены и Списки]]&lt;br /&gt;
[[Category:Data types and handling]]&lt;/div&gt;</summary>
		<author><name>Tbn</name></author>
	</entry>
</feed>