Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize FilterLink #3020

Merged
merged 21 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions opencog/atoms/atom_types/atom_types.script
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,12 @@ DIRECTLY_EVALUATABLE_LINK <- LINK
// for more information
IMPLICATION_LINK <- ORDERED_LINK, DIRECTLY_EVALUATABLE_LINK

// ImplicationScopeLink should be moved to PLN at earliest
// convenience. XXX FIXME.
// ImplicationScopeLink is a synonym for RuleLink, as far as I can tell.
// It is used by PLN to do more or less the same thing as RuleLink.
// Except that BackwardChainerUTest hangs when it inherits from
// RuleLink, because RuleLink::execute() does something that URE isn't
// expecting. At any rate, it should be moved there. XXX FIXME.
// IMPLICATION_SCOPE_LINK <- RULE_LINK
IMPLICATION_SCOPE_LINK <- SCOPE_LINK

INHERITANCE_LINK <- ORDERED_LINK, DIRECTLY_EVALUATABLE_LINK
Expand Down
2 changes: 0 additions & 2 deletions opencog/atoms/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ADD_LIBRARY (atomcore
Context.cc
DefineLink.cc
DeleteLink.cc
FilterLink.cc
FindUtils.cc
FreeLink.cc
FreeVariables.cc
Expand Down Expand Up @@ -58,7 +57,6 @@ INSTALL (FILES
Context.h
DefineLink.h
DeleteLink.h
FilterLink.h
FindUtils.h
FreeLink.h
FreeVariables.h
Expand Down
2 changes: 1 addition & 1 deletion opencog/atoms/core/FreeVariables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void FreeVariables::canonical_sort(const HandleSeq& hs)
// Get free variables
HandleSet fv = get_free_variables(hs);

// Ignore free variables in body not in the FreeVariables object
// Ignore free variables in the body that are not in this object.
HandleSet ignored_vars = set_symmetric_difference(fv, varset);
Context ctx(Quotation(), ignored_vars, false);

Expand Down
18 changes: 8 additions & 10 deletions opencog/atoms/core/FreeVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,14 @@ struct FreeVariables : Replacement
/// ordered link.
void find_variables(const HandleSeq& oset, bool ordered_link=true);

/// Sort the variables in a canonical order determined by their
/// positions in the given outgoing set, which is assumed ordered,
/// as outgoing sets of scopes are always ordered so far. In
/// ordered link, the ordered is determined by the outgoing set
/// order (from left to right). In unordered links, the ordered is
/// determined by some arbitrary, though semantically consistent
/// fix order. The order only depends on variable names as last
/// resort, when no semantic property can be used to break the
/// symmetry.
void canonical_sort(const HandleSeq& outgoings);
/// Sort the variables into a canonical order, so that they appear
/// in the same order as in the provided HandleSeq. That is, the
/// HandleSeq is presumed to be a list of trees, with variables
/// embeded in those trees. Each tree is walked, left-to-right,
/// depth-first. The order in which the variables are encountered
/// is the "canonical sort order". Variables that do NOT appear
/// in the HandleSeq are trimmed (removed) from this object.
void canonical_sort(const HandleSeq&);

/// Convert a variable->argument mapping into a sequence of
/// "arguments" that are in the same order as the free variables
Expand Down
9 changes: 8 additions & 1 deletion opencog/atoms/core/ScopeLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#include <string>

// #include <opencog/util/Logger.h>
#include <opencog/util/random.h>

#include <opencog/atoms/atom_types/NameServer.h>
#include <opencog/atoms/base/hash.h>
#include <opencog/atoms/core/FindUtils.h>
Expand Down Expand Up @@ -181,6 +181,13 @@ void ScopeLink::init_scoped_variables(const Handle& vardecl)

/* ================================================================= */

void ScopeLink::trim(const HandleSeq& terms)
{
_variables.trim(terms);
}

/* ================================================================= */

inline Handle append_rand_str(const Handle& var)
{
std::string new_var_name = randstr(var->get_name() + "-");
Expand Down
10 changes: 8 additions & 2 deletions opencog/atoms/core/ScopeLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ class ScopeLink : public Link
const Handle& get_vardecl(void) const { return _vardecl; }
const Handle& get_body(void) const { return _body; }

// Remove any variables that do NOT appear anywhere in the provided
// HandleSeq. This can be used to clean up vardecls, when variables
// are declared but then never used (thus making them impossible to
// ground, or having other nasty side-effects.)
void trim(const HandleSeq&);

// Return an alpha-converted copy of this atom. Optionally, new
// variable names can be provided. If none are provided, then new
// randomly generated names are created.
//
// Warning: the atomspace treats all alpha-convertible atoms as
// Warning: the AtomSpace treats all alpha-convertible atoms as
// identical; if the new copy is inserted into the atomspace, the
// original version will be returned. Alpha-converted atoms can
// only be used outside of the atomspace, for temporary operations.
// only be used outside of the AtomSpace, for temporary operations.
Handle alpha_convert() const;
Handle alpha_convert(const HandleSeq& vars) const;

Expand Down
3 changes: 2 additions & 1 deletion opencog/atoms/core/TypedVariableLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void TypedVariableLink::init()
if (VARIABLE_NODE != stype and
GLOB_NODE != stype)
throw SyntaxException(TRACE_INFO,
"Sorry, we expect type names to be variables!");
"Sorry, we expect type names to be variables! Got=%s",
to_short_string().c_str());

// Allow VARIABLE_NODE, although this is a bug in the URE,
// which should be using a SignatureLink for this case. XXX FIXME.
Expand Down
18 changes: 18 additions & 0 deletions opencog/atoms/core/Variables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,24 @@ void Variables::trim(const Handle& term)
erase(hu);
}

// Identical to above, except multiple terms are scanned.
void Variables::trim(const HandleSeq& terms)
{
// Find the vars in all of the terms.
FreeVariables fv;
fv.find_variables(terms);

// Find all vars not in any of the terms.
HandleSeq unused;
for (const Handle& hv: varseq)
if (not fv.varset_contains(hv))
unused.push_back(hv);

// Get rid of all vars not in any of the terms.
for (const Handle& hu: unused)
erase(hu);
}

/* ================================================================= */

/// Return true if the other Variables struct is equal to this one,
Expand Down
3 changes: 3 additions & 0 deletions opencog/atoms/core/Variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ struct Variables : public FreeVariables
/// Remove *all* variables that do not appear in the term.
void trim(const Handle&);

/// Remove *all* variables that do not appear in any of the terms.
void trim(const HandleSeq&);

/// Return the TypedVariableLink for the indicated variable.
/// Return just the Variable itself, if its not typed.
Handle get_type_decl(const Handle&, const Handle&) const;
Expand Down
2 changes: 2 additions & 0 deletions opencog/atoms/flow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR})

ADD_LIBRARY (atomflow
FilterLink.cc
FormulaPredicateLink.cc
NumberOfLink.cc
PromiseLink.cc
Expand All @@ -29,6 +30,7 @@ INSTALL (TARGETS atomflow EXPORT AtomSpaceTargets
)

INSTALL (FILES
FilterLink.h
FormulaPredicateLink.h
NumberOfLink.h
PromiseLink.h
Expand Down
Loading