-1

Ivan Bratko の本「Programming for Artificial Intelligence」で Prolog DCG 文法* と ** 構文木を勉強しています。

定義された言語に属する文字列から解析ツリーを作成する DCG 文法を提供する次の例には、いくつかの問題があります。

定義された言語はロボット アームの動きのリストであり、上と下の 2 つのタイプしかあり得ないため、 [、上、、上、上]は私の DCG 文法で定義された言語に属します。

このプログラムは、特定の文字列に関連付けられた解析ツリーを解釈し、ロボット アームが横切った距離を意味する意味/2述語も提供します(これは値 +1 を上に移動することに関連付けられ、値 -1 を移動に関連付けます)。下)

たとえば、リスト[up,up,down,up,up]の場合、mean/2述語は +3 距離を計算します

これは私の例のコードです(うまくいきます):

move(move(Step)) --> step(Step).
move(move(Step, Move)) --> step(Step), move(Move).

step(step(up)) --> [up].
step(step(down)) --> [down].

/* Take the parse tree of a string that belong to the language defined by the DCC grammar and
   calculate the total distance whereas that an up move give a +1 distance and a down move
   give -1 distance
*/
meaning(move(Step, Move), Dist):- 
                                  /* Calculate the distance given by the current step node 
                                     (which can be +1 or -1) */
                  meaning(Step, D1),
                  /* Calculate the distance given by the current moove node
                                     (which must be calculated recursively on a subtree having 
                                      a moove node as root */
                  meaning(Move, D2),
                  /* The final distance series of moves is the distance of the
                                     current step + the distance diven from the moves in the
                                     moove subtree */
                  Dist is (D1 + D2).

meaning(step(Step), Dist):- meaning(Step, Dist).
meaning(move(Step), Dist):- meaning(Step, Dist).

% step(up) means that the distance is +1
meaning(step(up), 1).

% step(down) means that the distance is -1
meaning(step(down), -1).

だから私は構文木を取り、移動の合計距離を計算する意味/2述語を持っています。

したがって、1 つの移動 (ステップ) に関連付けられた距離値を rappresent する 2 つのBASE CASEがあります。これは、アップ ステップの場合は +1、ダウン ステップの場合は -1 です。

meaning(step(up), 1).
meaning(step(down), -1).

意味/2は、DCG 文法で定義されているように、moveノードをルートとして持つ解析ツリーを使用します。このルートには、ステップノードである左の子があります (したがって、単一の移動であるため、特定の距離があります)。それに関連付けられた +1 または -1) および移動ノードである右の子 (したがって、別のサブツリーを rappresent する)

したがって、合計距離は、+1 または -1 である現在のステップの距離 (現在のムーブルートの左側の子) + 右側のサブツリーの距離の合計です。

私はこれが正しいと思います、そしてこれは私にとってかなり明確です

私が理解していないのは、コード内のこれら 2 つの述語を表しているものです。

meaning(step(Step), Dist):- meaning(Step, Dist).
meaning(move(Step), Dist):- meaning(Step, Dist).

私はそれらを私の推論に固執することはできません:-(

4

1 に答える 1