如果我们用列表来表示feature structure unification and subsumption,人们将如何在minikanren中定义feature structures?
一般行为是这样的(我认为):
(run* (q) (unifyo '(a b) '(a b) q))) => (a b)
(run* (q) (unifyo '(x (a b)) '(x (c d)) q)) => (x (a b) (c d)) (x (c d) (a b))
(run* (q) (unifyo '(x (a b)) '(x (a d)) q)) => () ; fails because '(a b) is
; incompatible with '(a d)
(run* (q)
(fresh (y) (unifyo '(x (a b)) `(x ,y) q)) => (x (a b)))
(run* (q) (unifyo q '(x (a b)) '(x (a b) (c d)))) => (x (c d))
以下代码可以正常工作,但是使用run *运行时,向后统一会卡住:
;; unifies f1 with l2
(define unify-f-with-list°
(lambda (f1 l2 out)
(conde
[(== '() l2) (== `(,f1) out)]
[(fresh (la ld a2 d2 a1 d1 res)
(=/= '() l2)
(== `(,la . ,ld) l2)
(== `(,a2 . ,d2) la)
(== `(,a1 . ,d1) f1)
(conde
[(== a2 a1)
(== `(,res . ,ld) out)
(unify° f1 la res)]
[(fresh ()
(=/= a2 a1) ;; if not
(== `(,la . ,res) out)
(unify-f-with-list° f1 ld res))]))])))
(define unify-list-with-list°
(lambda (l1 l2 out)
(conde
[(== '() l1) (== l2 out)]
[(== '() l2) (== l1 out)]
[(fresh (a1 d1 res)
(=/= '() l1)
(== `(,a1 . ,d1) l1)
(unify-f-with-list° a1 l2 res)
(unify-list-with-list° d1 res out))])))
(define unify°
(lambda (f1 f2 out)
(conde
[(== f1 f2) (== f1 out)]
[(fresh (a1 d1 a2 d2)
(=/= f1 f2)
(== `(,a1 . ,d1) f1)
(== `(,a2 . ,d2) f2)
(== a1 a2)
(fresh (res)
(unify-list-with-list° d1 d2 res)
(== `(,a1 . ,res) out)))])))
答案 0 :(得分:1)
您可以通过修改minikanren实现中的统一代码来实现此目的。
但是我建议不要使用列表来表示要素结构,相反,您可以定义一个新记录类型,而不是保存一个始终以新变量结尾的列表,并且其中一个将代表要素结构。然后,您仍然可以使用列表和其他对象,并使这些新对象可用。
当统一代码看到两个特征结构时,将需要递归地统一所有匹配的键,并扩展每个匹配键的“其余”部分以包含其他特征结构所独有的字段(无破坏性突变)。