(Lispex)sicp.io
4.9 · 규칙 기반 질의

규칙은 질의 하나를 더 작은 목표들의 수열로 바꾼다.

유한 질의 평가기는 사실을 훑고 규칙을 펼친 뒤 각 목표에서 얻은 프레임을 다음 목표로 전달할 수 있습니다.

생각해 볼 질문

규칙은 다음 목표가 필요로 하는 중간 바인딩을 어떻게 보존할까요?

  • Represent facts, queries, variables, and a rule as quoted data
  • Return one binding frame for each matching assertion
  • Expand a rule head into an ordered sequence of body goals
  • Carry every successful frame into the following goal

The first program compares the parent predicate and fixed ada position with each assertion. Every match returns a frame containing one child binding. scan-assertions reaches the end of the finite facts list, so both matching frames remain in assertion order.

The second program binds grand from the rule head, then solve-goals processes the two parent goals in order. The first goal produces middle values ben and dia. Each frame becomes input to the second goal, which finds cy and eli. This deliberately narrow evaluator supports parent goals in one finite rule body. It does not implement variable renaming, negation, recursive rules, duplicate removal, or general unification.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(begin
  (define facts
    '((parent ada ben)
      (parent ben cy)
      (parent ada dia)
      (parent dia eli)))
  (define query '(parent ada (var child)))
  (define (match-assertion query assertion)
    (if (and (eq? (car query) (car assertion))
             (eq? (cadr query) (cadr assertion)))
        (list (cons (cadr (caddr query)) (caddr assertion)))
        #f))
  (define (scan-assertions remaining answers)
    (if (null? remaining)
        (reverse answers)
        (let ((frame (match-assertion query (car remaining))))
          (scan-assertions (cdr remaining)
                           (if frame (cons frame answers) answers)))))
  (scan-assertions facts '()))
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 679 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    The assertion scan returns (((child . ben)) ((child . dia))). The rule expansion returns (cy eli).

    실행 흐름에서 볼 점

    In the first run, follow each assertion comparison and the two child-frame extensions. In the second run, follow the grand binding into the first goal, then confirm that the ben and dia middle frames are each passed into the second goal before child becomes cy or eli. These bounded traces cover only this finite database and narrow rule evaluator.

    직접 해보기

    힌트를 보기 전에 프로그램을 바꿔 보세요.

    Add (parent cy fox), then change the second query to (grandparent ben (var who)). Predict the returned answer.

    힌트 하나 보기

    The first goal binds middle to cy, and the new fact lets the second goal bind child to fox.