QuoteLink

From OpenCog
Jump to: navigation, search

The QuoteLink may be used during searches (e.g. with BindLink) to quote or escape variables and functions, to prevent their evaluation. Everything under a QuoteLink is interpreted as a literal by the pattern matcher and by the evaluator/executor.

QuoteLink behaves like a quasi-quote, in lisp/scheme, and so the quote context can be escaped by using the UnquoteLink. Basically a quotation level is incremented whenever a QuoteLink is encountered, or decremented whenever a UnquoteLink is encountered. Atoms with non null quotation level are considered quoted. The only way to prevent the quotation level from incrementing or decrementing is to locally quote the QuoteLink or UnquoteLink with a LocalQuoteLink.

A related idea is the DontExecLink. The DontExecLink prevents the execution/evaluation of what it wraps; unlike the QuoteLink, it does not prevent the grounding of variables. Thus, in order to search for a GetLink or BindLink, use the DontExecLink to prevent the result of the search from being executed.

Example: Quoting GPN

For example, one can avoid the evaluation of a GroundedPredicateNode, and instead simply search for patterns that contain it, as follows:

(BindLink
   (VariableNode "$stuff")
   (EvaluationLink
      (QuoteLink (GroundedPredicateNode "scm:do_stuff"))
      (VariableNode "$stuff")
   )
   (VariableNode "$stuff")
)

which will match the following:

(EvaluationLink
   (GroundedPredicateNode "scm:do_stuff")
   (ListLink
      (ConceptNode "thing-a")
      (ConceptNode "thing-b")
   )
)

In the above, the VariableNode is grounded by

(ListLink
   (ConceptNode "thing-a")
   (ConceptNode "thing-b")
)

Example: Quoting Variables

Variables can be searched for. Thus, for example, this pattern

  (BindLink
     (VariableNode "$var-a")
     (EvaluationLink
        (PredicateNode "similar")
        (ListLink
           (QuoteLink (VariableNode "$var-a"))
           (VariableNode "$var-a")
        )
     )
     (VariableNode "$var-a")
  )

will find this grounding:

(EvaluationLink
   (PredicateNode "similar")
   (ListLink
      (VariableNode "$var-a")
      (ConceptNode "banana")
   )
)

Note that in the above, the variable is used both quoted and unquoted! That makes this example somewhat confusing. It doesn't have to be: the variable could have been given a different name (e.g. $var-b) when searching for $var-a -- but the point here is that the pattern works, even if there is a collision in the variable names.

Example: Quoting quotes

Expressions containing QuoteLinks can also be searched for, by double-quoting. So, for example, the following pattern can be used

(BindLink
   (VariableNode "$var-a")
   (EvaluationLink
      (PredicateNode "similar")
      (ListLink
         (QuoteLink (QuoteLink (VariableNode "$var-a")))
         (VariableNode "$var-a")
      )
   )
   (VariableNode "$var-a")
)

to find this binding:

(EvaluationLink
   (PredicateNode "similar")
   (ListLink
      (QuoteLink (VariableNode "$var-a"))
      (ConceptNode "banana")
   )
)

As in the previous example, the variable $var-a collides with itself. The example would be easier to read if it did not, but it does demonstrate that things still work even if there is a naming collision.

Running the examples

The above are fully-functional, valid scheme code. They work as written. To run them, start guile, and say (use-modules ((opencog query))) and then cut-n-paste the above into the guile prompt. It should all work. Recall, to run the pattern match, you need to say (cog-bind (BindLink ...)).