Fact rule query Edinburgh Prolog fact term rule
Fact, rule, queryの構文 (Edinburgh Prologの場合) <fact> : : = <term>. <rule> : : = <term> : - <terms>. <query> : : = <terms>. <term> : : = <number> | <atom> | <variable> | <atom> (<terms>) <terms> : : = <term> | <term>, <terms>
Fact, ruleの例 link(fortran, algol 60). link(algol 60, cpl). link(cpl, bcpl). link(bcpl, c). link(c, cplus). link(algol 60, simula 67). link(simula 67, cplus). link(simula 67, smalltalk 80). path(L, L). path(L, M) : - link(L, X), path(X, M).
? - link(L, N), link(M, N). L=fortran M=fortran N=algol 60 ? - link(L, N), link(M, N), ¥+(L=M). L=c M=simula 67 N=cplus ; L=simula 67 M=c N=cplus ; no ? - ¥+(L=M), link(L, N), link(M, N). no (続き)
単一化(unification) • 2つのtermを等しくするような変数への最も一般的な 置換(most general unifier)を求めること • Unificationは規則の適用ができるかどうかを判定す るために行われる。 • Unificationを行う組み込みの述語として=がある。 (例)? - f (X, b) = f (a, Y). X=a Y=b (参考文献 1) John A. Robinson. “A machine-oriented logic based on the resolution principle”. Journal of the ACM, 12(1): 23– 41, 1965. (参考文献 2) Alberto Martelli and Ugo Montanari, “An efficient unification algorithm”, ACM TOPLAS 4(2), pp. 258 -282, 1982. (参考)片方のみに変数がある場合はパターンマッチング(pattern matching)。
例 suffix([b], L), prefix(L, [a, b, c]) append(_1, [b], L), prefix(L, [a, b, c]) {_1 -> [_3|_4 ], L->[_3|_5]} {_1 -> [ ], L->[b]} prefix([b], [a, b, c]) append(_4, [b], _5), prefix([_3|_5], [a, b, c]) append([b], _2, [a, b, c]) {_4 -> [ ], _5 ->[b]} backtrack prefix([_3, b], [a, b, c]) append([_3, b], _6, [a, b, c]) {_3 -> a} append([b], _6, [b, c]) append([ ], _6, [c]) yes {_6 -> [c]} …
- Slides: 24