AlwaysLink

From OpenCog
Jump to: navigation, search

The AlwaysLink is used to assure that that given sub-pattern always occurs in every instance (every grounding or "match") during pattern matching. It can be thought of as a kind of ""for-all" link (see ForAllLink), asking that when the indicated pattern occurs in the AtomSpace, it does so in all of it's variants. It is conjugate to PresentLink, which demands that at least one instance be present.

Unlike the ForAllLink, the AlwaysLink does not bind it's variables, leaving them as free variables. This allows the AlwaysLink to be composed with other patterns having the same variable names, so as to form more complex patterns.

See link comparison for a tabular summary of these various properties.

Example

The following can be used to find those baskets containing only red balls:

(define get-baskets-with-only-red-balls
  (Bind
     (VariableList
        (TypedVariable (Variable "basket") (Type 'ConceptNode))
        (TypedVariable (Variable "ball")   (Type 'ConceptNode))
     )
     (And
        ; Look at every basket ...
        (Inheritance (Variable "basket") (Concept "basket"))

        ; Look at the balls in the basket ...
        (Member (Variable "ball") (Variable "basket"))

        ; Always means that *every* ball in the basket MUST
        ; be red! Any single failure to satisfy this invalidates
        ; the entire search.
        (Always (Evaluation (Predicate "is red") (Variable "ball")))
     )

     ; Report the basket which has only red balls in it.
     (Variable "basket")))

A working example demonstrating the above can be found in /examples/pattern-matcher/always.scm.