| Title: Anonymous Variable
Description: None
Copyright (c) 1999 Steven J. Metsker.
Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
An anonymous variable unifies successfully with any other
term, without binding to the term.
Anonymous variables are useful for screening out unwanted
terms. For example, if a program describes a marriage in
terms of an id, the husband, wife, and the beginning and
end dates of the marriage, its facts might look something
like:
marriage(001, balthasar, grimelda, 14560512, 14880711);
// ...
marriage(257, kevin, karla, 19790623, present);
A rule that extracts just the husband from
marriage facts is:
husband(Id, Hub) :- marriage(Id, Hub, _, _, _);
The underscores in this rule represent anonymous variables.
When the rule executes, it will unify its
marriage structure with marriage
facts, without regard to the last three terms of those
facts.
Without anonymous variables, the husband rule
would need three unused variables. Note that the following
approach would fail:
husband(Id, Hub) :-
marriage(Id, Hub, Anon, Anon, Anon); // wrong
This approach, while tempting, will not work because the
variable Anon will bind to the structures it
encounters. Issued against the example program,
Anon will first bind to grimelda .
Next, Anon will attempt to bind to
14560512 . This will fail, since
Anon will already be bound to
grimelda .
The essential behavior anonymous variables provide is that
they unify successfully without binding.
author: Steven J. Metsker version: 1.0 |