【发布时间】:2021-10-25 23:59:52
【问题描述】:
我想知道如何翻译这个 Prolog 代码以在 Datalog 中工作。
我认为它不适用于 Datalog,因为我们不允许在 Datalog 中使用 nullable(rule(z,[d])) 之类的东西。另外,我不知道 Datalog 是否允许列表。也许另一种选择是将规则写为字符串,但我不知道Datalog是否允许字符串以及是否所有需要的字符串函数都可用。
% rules for nullable
% If we have the rule X -> Y where X does not appear in Y and each component of Y is nullable, then X is nullable
% We need that X does not appear in Y to avoid circular loops (If X is nullable it would be because of a non-circular definition so we are not omitting any case)
nullable(X) :- variable(X), rule(X,Y), \+ member(X, Y), nullable(Y). % The Y here is a list so we need to define nullable(Y) for lists which is one below
% The empty list is always a nullable list
nullable([]).
% A list is nullable if all of its components are nullable
nullable([X|Y]) :- nullable(X), nullable(Y).
% A rule X -> Y is nullable if Y is nullable
nullable(rule(_,Y)) :- nullable(Y).
关于代码的上下文。 此序言代码确定上下文无关文法是否可以为空。
这意味着所有规则(例如生产 S -> ABC | XYZ,规则是: [ABC, XYZ ] ) 如果其中任何一个可以为空,则 整个规则可以为空。这相当于OrLattice。
eq Rule.getNDecl().nullable() { for (int i = 0; i < getNumProd(); i++) { if (getProd(i).nullable()) return true; } return false; }这意味着生产中的所有终端和非终端(例如 一个产生式 S -> ABC, 符号是 ABC ) 如果它们都可以为空 那么整个规则是可以为空的。这相当于 AndLattice。
eq Prod.nullable() { for (int i = 0; i < getNumSymbol(); i++) { if (!getSymbol(i).nullable()) return false; } return true; }Epsilon 终端可以为空
eq Terminal.nullable() { return false; }如果它的使用是可空的,则非终端是可空的
eq NUse.nullable(){ return decl().nullable(); }
Paper (free to download)(第 14-15 页)
【问题讨论】:
-
这个问题可以使用更多的上下文。这个 Prolog 代码来自哪里?它有什么作用?您是否已经将其他部分移植到 Datalog?您为什么首先要在 Datalog 中使用它?
-
@IsabelleNewbie 我添加了上下文。