next up previous
Next: 4 論理型プログラミングとProlog Up: 3 変数を含むパターン同士のマッチングUnifyの定義 Previous: 3.3 同一変数を内部に含むパターンとのバインディングの場合

3.4 ユニファイの応用例

matchのところで,バインディングを求めたあともとのデータを 復元した substitute-bindings は,変数と変数のマッチングを 考慮していなかった. そこで,再定義する.
(defun substitute-bindings (bindings pattern)
  (cond
   ((null pattern) nil)
   ((variable-p pattern)
    (let ((v (get-binding pattern bindings)))
      (if v
          (substitute-bindings
              bindings            ;; ここ
              (binding-val v))    ;;
        pattern)))
   ((atom pattern) pattern)
   (t (cons (substitute-bindings bindings
                                 (car pattern))
            (substitute-bindings bindings
                                 (cdr pattern))))))
これを用いて,二つのパターンのマッチングをとってひとつのデータに変換す るunifierを作る.

(defun unifier (x y)
  (substitute-bindings (unify x y) x))
これを用いると,
<cl> (unifier '(?x ?y a) '(?y ?x ?x)) 
(A A A) 
<cl> (unifier
        '((?a * ?x ^ 2) + (?b * ?x) + ?c)
        '(?z + (4 * 5) + 3))
((?A * 5 ^ 2) + (4 * 5) + 3)
ということができるようになる.

generated through LaTeX2HTML. M.Inaba 平成18年5月7日