The Driver Loop and Instantiation

SICP > Metalinguistic Abstraction > Logic Programming > Implementing the Query System > The Driver Loop and Instantiation
Previous: Implementing the Query System Next: The Evaluator

    The driver loop for the query system repeatedly reads input expressions. If the expression is a rule or assertion to be added to the data base, then the information is added. Otherwise the expression is assumed to be a query. The driver passes this query to the evaluator qeval together with an initial frame stream consisting of a single empty frame. The result of the evaluation is a stream of frames generated by satisfying the query with variable values found in the data base. These frames are used to form a new stream consisting of copies of the original query in which the variables are instantiated with values supplied by the stream of frames, and this final stream is printed at the terminal:

    (define input-prompt ";;; Query input:")
    (define output-prompt ";;; Query results:")
    
    (define (query-driver-loop)
      (prompt-for-input input-prompt)
      (let ((q (query-syntax-process (read))))
        (cond ((assertion-to-be-added? q)
               (add-rule-or-assertion! (add-assertion-body q))
               (newline)
               (display "Assertion added to data base.")
               (query-driver-loop))
              (else
               (newline)
               (display output-prompt)
               (display-stream
                (stream-map
                 (lambda (frame)
                   (instantiate q
                                frame
                                (lambda (v f)
                                  (contract-question-mark v))))
                 (qeval q (singleton-stream '()))))
               (query-driver-loop)))))
    
    Here, as in the other evaluators in this chapter, we use an abstract syntax for the expressions of the query language. The implementation of the expression syntax, including the predicate assertion-to-be-added? and the selector add-assertion-body, is given in section [*]. Addruleor-assertion! is defined in section [*].

    Before doing any processing on an input expression, the driver loop transforms it syntactically into a form that makes the processing more efficient. This involves changing the representation of pattern variables. When the query is instantiated, any variables that remain unbound are transformed back to the input representation before being printed. These transformations are performed by the two procedures query-syntaxprocess and contract-question-mark (section  [*]).

    To instantiate an expression, we copy it, replacing any variables in the expression by their values in a given frame. The values are themselves instantiated, since they could contain variables (for example, if ?x in exp is bound to ?y as the result of unification and ?y is in turn bound to 5). The action to take if a variable cannot be instantiated is given by a procedural argument to instantiate.

    (define (instantiate exp frame unbound-var-handler)
      (define (copy exp)
        (cond ((var? exp)
               (let ((binding (binding-in-frame exp frame)))
                 (if binding
                     (copy (binding-value binding))
                     (unbound-var-handler exp frame))))
              ((pair? exp)
               (cons (copy (car exp)) (copy (cdr exp))))
              (else exp)))
      (copy exp))
    
    The procedures that manipulate bindings are defined in section [*].

    Previous: Implementing the Query System Next: The Evaluator

      webmaster@arsdigita.org