/usr/share/doc/swi-prolog-doc/Manual/compare.html is in swi-prolog-doc 5.6.59-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>SWI-Prolog 5.6.59 Reference Manual: Section 4.6</TITLE><LINK REL=home HREF="index.html">
<LINK REL=contents HREF="Contents.html">
<LINK REL=index HREF="DocIndex.html">
<LINK REL=previous HREF="typetest.html">
<LINK REL=next HREF="control.html">
<STYLE type="text/css">
/* Style sheet for SWI-Prolog latex2html
*/
dd.defbody
{ margin-bottom: 1em;
}
dt.pubdef
{ background-color: #c5e1ff;
}
pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}
div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}
div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}
div.author
{ text-align: center;
font-style: italic;
}
div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}
div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}
div.toc-h1
{ font-size: 200%;
font-weight: bold;
}
div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}
div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}
div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}
span.sec-nr
{
}
span.sec-title
{
}
span.pred-ext
{ font-weight: bold;
}
span.pred-tag
{ float: right;
font-size: 80%;
font-style: italic;
color: #202020;
}
/* Footnotes */
sup.fn { color: blue; text-decoration: underline; }
span.fn-text { display: none; }
sup.fn span {display: none;}
sup:hover span
{ display: block !important;
position: absolute; top: auto; left: auto; width: 80%;
color: #000; background: white;
border: 2px solid;
padding: 5px; margin: 10px; z-index: 100;
font-size: smaller;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="white">
<DIV class="navigate"><A class="nav" href="index.html"><IMG SRC="home.gif" BORDER=0 ALT="Home"></A>
<A class="nav" href="Contents.html"><IMG SRC="index.gif" BORDER=0 ALT="Contents"></A>
<A class="nav" href="DocIndex.html"><IMG SRC="yellow_pages.gif" BORDER=0 ALT="Index"></A>
<A class="nav" href="typetest.html"><IMG SRC="prev.gif" BORDER=0 ALT="Previous"></A>
<A class="nav" href="control.html"><IMG SRC="next.gif" BORDER=0 ALT="Next"></A>
</DIV>
<H2><A NAME="sec:4.6"><SPAN class="sec-nr">4.6</SPAN> <SPAN class="sec-title">Comparison
and Unification of Terms</SPAN></A></H2>
<A NAME="sec:compare"></A>
<P>Although unification is mostly done implicitely while matching the
head of a predicate, it is also provided by the predicate =/2.
<DL>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="=/2"><VAR>+Term1</VAR> <STRONG>=</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
Unify <VAR>Term1</VAR> with <VAR>Term2</VAR>. True if the unification
succeeds. For behaviour on cyclic terms see the Prolog flag
<A class="flag" href="flags.html#flag:occurs_check">occurs_check</A>. It
acts as if defined by the following rule.
<PRE class="code">
=(Term, Term).
</PRE>
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="\=/2"><VAR>+Term1</VAR> <STRONG>\=</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
Equivalent to <CODE><CODE>\+</CODE>Term1 = Term2</CODE>. See also <A NAME="idx:dif2:485"></A><A class="pred" href="coroutining.html#dif/2">dif/2</A>.
</DD>
</DL>
<H3><A NAME="sec:4.6.1"><SPAN class="sec-nr">4.6.1</SPAN> <SPAN class="sec-title">Standard
Order of Terms</SPAN></A></H3>
<A NAME="sec:standardorder"></A>
<P>Comparison and unification of arbitrary terms. Terms are ordered in
the so called ``standard order''. This order is defined as follows:
<P><OL>
<LI><VAR><VAR>Variables</VAR> < <VAR>Numbers</VAR> < <VAR>Atoms</VAR>
< <VAR>Strings</VAR> < <VAR>Compound Terms</VAR></VAR><SUP class="fn">26<SPAN class="fn-text">Strings
might be considered atoms in future versions. See also <A class="sec" href="strings.html">section
4.23</A></SPAN></SUP>
<LI>Variables are sorted by address. Attaching attributes (see <A class="sec" href="attvar.html">section
6.1</A>) does not affect the ordering.
<LI><VAR>Atoms</VAR> are compared alphabetically.
<LI><VAR>Strings</VAR> are compared alphabetically.
<LI><VAR>Numbers</VAR> are compared by value. Mixed integer/float are
compared as floats. If the comparison is equal, the float is considered
the smaller value. If the Prolog flag <A class="flag" href="flags.html#flag:iso">iso</A>
is defined, all floating point numbers precede all integers.
<LI><VAR>Compound</VAR> terms are first checked on their arity, then on
their functor-name (alphabetically) and finally recursively on their
arguments, leftmost argument first.
</OL>
<DL>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="==/2"><VAR>+Term1</VAR> <STRONG>==</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if <VAR>Term1</VAR> is equivalent to <VAR>Term2</VAR>. A variable
is only identical to a sharing variable.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="\==/2"><VAR>+Term1</VAR> <STRONG>\==</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
Equivalent to <CODE><CODE>\+</CODE>Term1 == Term2</CODE>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="@</2"><VAR>+Term1</VAR> <STRONG>@<</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if <VAR>Term1</VAR> is before <VAR>Term2</VAR> in the standard
order of terms.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="@=</2"><VAR>+Term1</VAR> <STRONG>@=<</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if both terms are equal (<A class="pred" href="compare.html#==/2">==/2</A>)
or <VAR>Term1</VAR> is before <VAR>Term2</VAR> in the standard order of
terms.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="@>/2"><VAR>+Term1</VAR> <STRONG>@></STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if <VAR>Term1</VAR> is after <VAR>Term2</VAR> in the standard order
of terms.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="@>=/2"><VAR>+Term1</VAR> <STRONG>@>=</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if both terms are equal (<A class="pred" href="compare.html#==/2">==/2</A>)
or <VAR>Term1</VAR> is after <VAR>Term2</VAR> in the standard order of
terms.</DD>
<DT class="pubdef"><A NAME="compare/3"><STRONG>compare</STRONG>(<VAR>?Order,
+Term1, +Term2</VAR>)</A></DT>
<DD class="defbody">
Determine or test the <VAR>Order</VAR> between two terms in the standard
order of terms. <VAR>Order</VAR> is one of <CODE><CODE><</CODE></CODE>, <CODE><CODE>></CODE></CODE>
or <CODE><CODE>=</CODE></CODE>, with the obvious meaning.
</DD>
</DL>
<H3><A NAME="sec:4.6.2"><SPAN class="sec-nr">4.6.2</SPAN> <SPAN class="sec-title">Special
unification and comparison predicates</SPAN></A></H3>
<A NAME="sec:unifyspecial"></A>
<P>This section describes special purpose variations on Prolog
unification. The predicate <A NAME="idx:unifywithoccurscheck2:486"></A><A class="pred" href="compare.html#unify_with_occurs_check/2">unify_with_occurs_check/2</A>
provides sound unification and is part of the ISO standard. The
predicates <A NAME="idx:subsumes2:487"></A><A class="pred" href="compare.html#subsumes/2">subsumes/2</A>
and
<A NAME="idx:subsumeschk2:488"></A><A class="pred" href="compare.html#subsumes_chk/2">subsumes_chk/2</A>
define `one-sided-unification' and are found in many Prolog systems.
Finally, <A NAME="idx:unifiable3:489"></A><A class="pred" href="compare.html#unifiable/3">unifiable/3</A>
is a `what-if' version of unification that is often qused a building
block in constraint reasoners.
<DL>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="unify_with_occurs_check/2"><STRONG>unify_with_occurs_check</STRONG>(<VAR>+Term1,
+Term2</VAR>)</A></DT>
<DD class="defbody">
As <A class="pred" href="compare.html#=/2">=/2</A>, but using <EM>sound-unification</EM>.
That is, a variable only unifies to a term if this term does not contain
the variable itself. To illustrate this, consider the two goals below:
<PRE class="code">
1 ?- A = f(A).
A = f(f(f(f(f(f(f(f(f(f(...))))))))))
2 ?- unify_with_occurs_check(A, f(A)).
No
</PRE>
<P><A NAME="idx:occurscheck:490"></A>I.e. the first creates a <EM>cyclic-term</EM>,
which is printed as an infinitely nested f/1 term (see the <CODE>max_depth</CODE>
option of <A NAME="idx:writeterm2:491"></A><A class="pred" href="termrw.html#write_term/2">write_term/2</A>).
The second executes logically sound unification and thus fails. Note
that the behaviour of unification through
<A class="pred" href="compare.html#=/2">=/2</A> as well as implicit
unification in the head can be changed using the Prolog flag <A class="flag" href="flags.html#flag:occurs_check">occurs_check</A>.</DD>
<DT class="pubdef"><A NAME="=@=/2"><VAR>+Term1</VAR> <STRONG>=@=</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
True if <VAR>Term1</VAR> is `structurally equal' to <VAR>Term2</VAR>.
Structural equivalence is weaker than equivalence (<A class="pred" href="compare.html#==/2">==/2</A>),
but stronger than unification (<A class="pred" href="compare.html#=/2">=/2</A>).
Two terms are structurally equal if their tree representation is
identical and they have the same `pattern' of variables. Examples:
<BLOCKQUOTE>
<TABLE BORDER=0 FRAME=void RULES=groups>
<TR VALIGN=top><TD ALIGN=right><TT>a</TT></TD><TD ALIGN=center><TT>=@=</TT></TD><TD><TT>A</TT></TD><TD ALIGN=center>false </TD></TR>
<TR VALIGN=top><TD ALIGN=right><TT>A</TT></TD><TD ALIGN=center><TT>=@=</TT></TD><TD><TT>B</TT></TD><TD ALIGN=center>true </TD></TR>
<TR VALIGN=top><TD ALIGN=right><TT>x(A,A)</TT></TD><TD ALIGN=center><TT>=@=</TT></TD><TD><TT>x(B,C)</TT></TD><TD ALIGN=center>false </TD></TR>
<TR VALIGN=top><TD ALIGN=right><TT>x(A,A)</TT></TD><TD ALIGN=center><TT>=@=</TT></TD><TD><TT>x(B,B)</TT></TD><TD ALIGN=center>true </TD></TR>
<TR VALIGN=top><TD ALIGN=right><TT>x(A,B)</TT></TD><TD ALIGN=center><TT>=@=</TT></TD><TD><TT>x(C,D)</TT></TD><TD ALIGN=center>true </TD></TR>
</TABLE>
</BLOCKQUOTE>
<P><A NAME="idx:varianttermcomparison:492"></A>The predicates <A class="pred" href="compare.html#=@=/2">=@=/2</A>
and <A class="pred" href="compare.html#\=@=/2">\=@=/2</A> are
cycle-safe. Attributed variables are considered structurally equal iff
their attributes are structurally equal. This predicate is known by the
name <SPAN class="pred-ext">variant/2</SPAN> in some other Prolog
systems.</DD>
<DT class="pubdef"><A NAME="\=@=/2"><VAR>+Term1</VAR> <STRONG>\=@=</STRONG> <VAR>+Term2</VAR></A></DT>
<DD class="defbody">
Equivalent to <CODE>`<CODE>\+</CODE>Term1 =@= Term2'</CODE>.</DD>
<DT class="pubdef"><A NAME="subsumes/2"><STRONG>subsumes</STRONG>(<VAR>+Generic, @Specific</VAR>)</A></DT>
<DD class="defbody">
A term is told to <EM>subsume</EM> another term if instantiation in the
generic term produces the specific term. The subsumption relation is
also called <EM>one sided unification</EM> or <EM>semi-unification</EM>.
It behaves as if defined by<SUP class="fn">27<SPAN class="fn-text">This
implementation relies on the fact that <A NAME="idx:termvariables2:493"></A><A class="pred" href="manipterm.html#term_variables/2">term_variables/2</A>
orders its variables based on depth-first left-to-right traversal of the
term.</SPAN></SUP>
<PRE class="code">
subsumes(General, Specific) :-
term_variables(Specific, SVars),
General = Specific,
term_variables(SVars, SVars2),
SVars == SVars2.
</PRE>
</DD>
<DT class="pubdef"><A NAME="subsumes_chk/2"><STRONG>subsumes_chk</STRONG>(<VAR>+Generic, @Specific</VAR>)</A></DT>
<DD class="defbody">
Equivalent to <CODE>\+ \+ subsumes(Generic, Specific)</CODE>.</DD>
<DT class="pubdef"><A NAME="unifiable/3"><STRONG>unifiable</STRONG>(<VAR>@X, @Y,
-Unifier</VAR>)</A></DT>
<DD class="defbody">
If <VAR>X</VAR> and <VAR>Y</VAR> can unify, unify <VAR>Unifier</VAR>
with a list of
<VAR>Var</VAR> = <VAR>Value</VAR>, representing the bindings required to
make <VAR>X</VAR> and <VAR>Y</VAR> equivalent.<SUP class="fn">28<SPAN class="fn-text">This
predicate was introduced for the implementation of <A NAME="idx:dif2:494"></A><A class="pred" href="coroutining.html#dif/2">dif/2</A>
and <A NAME="idx:when2:495"></A><A class="pred" href="coroutining.html#when/2">when/2</A>
after discussion with Tom Schrijvers and Bart Demoen. None of us is
really happy with the name and therefore suggestions for a new name are
welcome.</SPAN></SUP> This predicate can handle cyclic terms. Attributed
variables are handles as normal variables. Associated hooks are <EM>not</EM>
executed.</DD>
<DT class="pubdef"><A NAME="?=/2"><STRONG>?=</STRONG>(<VAR>@Term1, @Term2</VAR>)</A></DT>
<DD class="defbody">
Decide whether the equality of <VAR>Term1</VAR> and <VAR>Term2</VAR> can
be compared safely, i.e. whether the result of <CODE>Term1 == Term2</CODE>
can change due to further instantiation of either term. It is defined as
by <CODE>?=(A,B) :- (A==B ; A \= B), !.</CODE> See also
<A NAME="idx:dif2:496"></A><A class="pred" href="coroutining.html#dif/2">dif/2</A>.
</DD>
</DL>
<P></BODY></HTML>
|