Prolog プログラムは述語の集まりです。
述語は節の集まりです。
句の形式は次のとおりです。
Head :- Body.
Head「が真なら真」という意味Bodyです。
短縮形の節があります
Head.
と同じ意味です
Head :- true.
wheretrueは常に true であるビルトインです。
Body節の部分に戻るとBody、目標は次のいずれかの形式を取ることができます ( A、B、およびCは任意の目標を示します)。
Atom % This is true if Atom is true (see below).
A, B % This is true if A is true and B is true.
(A ; B) % This is true if A is true or B is true.
\+ A % This is true if A is not true.
(A -> B ; C) % If A is true then B must be true, else C must be true.
Prolog には、評価順序 (左から右) と「カット」 (検索ツリーを剪定する) に関するいくつかの特別な規則がありますが、それはこの短いチュートリアルの範囲を超える細かい詳細です。
ここで、anAtomが真かどうかを判断するにはAtom、次の形式のいずれかを使用できます (XとYは任意の項を示します)。
true % or some other builtin with given truth rules.
X = Y % True if X and Y are successfully unified.
p(X, Y, ...) % True if p(X, Y, ...) matches the head of some clause
% and the Body is true.
用語は、本質的に、構文の一部です。
ここで重要なのは、Prolog には機能がないことです。関数型言語ではとadd(X, Y)の合計に評価される関数を定義できますが、Prolog では、ヘッドが成功した場合に との合計を表す項と一体化する述語を定義します。XYadd(X, Y, Z)ZXY
以上のことから、次のように Prolog でルールを定義できます。
add(0, Y, Y). % 0 + Y = Y.
add(Y, 0, Y). % Y + 0 = Y.
add(s(X), Y, s(Z)) :- add(X, Y, Z). % s(X) + Y = s(X + Y).
ここで、0ゼロ (!)s(X)を表し、 の後継者を表すために使用していXます。
評価を検討してくださいadd(s(s(0)), s(0), Z):
add(s(s(0)), s(0), Z) % Only the third head for add matches, so...
---> Z = s(Z0), add(s(0), s(0), Z0).
add(s(0), s(0), Z0) % Only the third head for add matches, so...
---> Z0 = s(Z1), add(0, s(0), Z1).
add(0, s(0), Z1) % Only the first head for add matches, so...
---> Z1 = s(0).
これらの統合をすべてZまとめると、Z = s(s(s(0))).
ここで、「句で複数のヘッドが一致した場合はどうなるか」または「評価パスが失敗した場合はどうなるか」という質問をすることができますが、その答えは「非決定論」、「バックトラッキング」であり、一般に、プロローグの教科書!
お役に立てれば。