retrieve [Macro]


Purpose

The retrieve macro is used for retrieving facts from a knowledge base.

Syntax

retrieve variables query &key context generators

Arguments

The variables argument is a symbol or list of symbols each beginning with the character ?.

The query argument is an expression in the Loom query language (see Remarks below). This language has the expressive power of the first-order predicate calculus.

The context argument is the name of the context in which query is to be evaluated.

The generators argument is a list containing one or more of the terms in query. The Loom query optimizer will prefer these terms when selecting generators for the query variables. Normally, generators are terms that are expected to generate few instances.

Value

If variables is a symbol, the retrieve macro returns a list of values satisfying query. If variables is a list, a list of tuples is returned, where each tuple contains as many values as there are output variables.

Remarks

  query-expr ::= 
      ( {:AND | :OR} query-expr+ ) | 
      ( {:NOT | :FAIL} query-expr ) | 
      ( :IMPLIES query-expr query-expr ) | 
      ( {:FOR-SOME | :FOR-ALL} ( ?Var+ ) query-expr ) | 
      ( :COLLECT ( ?Var ) query-expr ) | 
      ( concept instance ) | 
      ( relation instance+ value ) | 
      ( :SAME-AS instance instance ) | 
      ( :SUBSET instance instance ) | 
      ( :PREDCALL LispPredicate value+ ) | 
      ( :ABOUT instance about-clause* )
  about-clause ::= 
      concept | 
      ( concept ) | 
      ( relation value ) | 
      ( :FILLED-BY relation value+ ) | 
      ( {:AT-LEAST | :AT-MOST | :EXACTLY} Integer relation ) | 
      ( {:ALL | :SOME | :THE} relation concept )
The query expressions appearing in the query argument above have the following syntax: Each about-term in a query :about clause has the form:

The query-expression operators have the following semantics:

A concept, relation, or instance may be either a symbol that names a Loom object, or a variable beginning with the character ?. An instance may also be a constant or a formula, where a formula is a list of the form (relation instance).

Any ?-variable not bound in variables, or in the variables of a :for-some, :for-all, or :collect clause, is assumed to be bound externally. The value of an external ?-variable should be a Loom object, i.e., a concept, relation, or instance, rather than the name of such an object.

The :filled-by-list operator cannot be used in the :about clause of a query. The Loom Grammar at the end of this manual is too general in this respect.

Concepts and relations referenced by name in a query must be defined at the time the query is compiled. Instances referenced by identifier must exist at the time the query is executed.

Examples

(retrieve ?x (Artist ?x)) ==> (|I|JOE \ldots) 
(retrieve ?x (age Joe ?x)) ==> (39) 
(retrieve (?x) (age Joe ?x) :context cl-user-theory) ==> ((39)) 
(retrieve ?x (max (age (child Joe)) ?x)) 
(retrieve ?x (distance Paris Rome ?x)) 
(retrieve (?x ?y) (wife ?x ?y)) ==> ((|I|JOE |I|SUE) (|I|FRED |I|MARY)) 
(retrieve ?x (:and (Woman ?x) (Senator ?x)) :generators ((Senator ?x))) 
(retrieve ?x (:and (pet Joe ?x) (:not (Dog ?x)))) 
(retrieve ?x (:and (Artist ?x) (:fail (:or (French ?x) 
                                           (resides ?x France))))) 
(retrieve ?x (:and (Writer ?x) (:for-some ?y (:and (author ?y ?x)  
                                                   (Best-Seller ?y))))) 
(retrieve ?x (:and (Writer ?x) (:for-all ?y (:implies (author ?y ?x)  
                                                      (Best-Seller ?y))))) 
(retrieve ?x (:and (Salesman ?x) (:same-as (home ?x) (office ?x)))) 
(retrieve ?x (:and (Salesman ?x) (:subset (customer ?x) (customer Joe)))) 
(retrieve ?x (:and (age (child Joe) ?x) (:predcall \#'oddp ?x))) 
(retrieve ?x (:about ?x Man (:at-least 2 child) (:exactly 0 son))) 
(retrieve ?x (:about ?x Man (:all child Female) (:some child Teen-Ager))) 
(retrieve ?x (:about ?x Man (:filled-by daughter Mary Sue))) 
(setq ?C (fc Artist) ?R (fr friend) ?V (fi Joe)) 
(retrieve ?x (:and (?C ?x) (?R ?x ?V) (:about ?x (:all ?R ?C))))

See Also

Last modified: Jun 1 1995