微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Prolog 中为位串定义 DCG

如何解决在 Prolog 中为位串定义 DCG

在下周的 Prolog 考试之前,我们的讲师为我们提供了许多练习题,但我对以下问题感到困惑。有人可以帮忙吗?

对于每个整数 n > 0,

let Ln := {s ∈ {0,1}+ | s 以 1(0 + 1)n−1}

的字符串结尾

是第 n 位到最后一位为 1 的位串集合。即 Ln 由正则表达式描述

(0 + 1)∗1(0 + 1)n−1.

为三元谓词 s/3 定义 DCG,使得 s(n,s,[]) 为真 如果 s 在 Ln 中对字符串进行编码。

解决方法

#include <stdio.h>

int main()
{
    int x=20,y=10,z=5;
    printf("\n%d",x*z/z-y);
}

所以:

我们可以接受位串

...找到后缀长度:

% ---
% It turns out we need these
% ---

% "any(N,gen)" generates/accepts any bitstring of KNOWN length N
% The order of clauses is unimportant!

any(N,gen) --> { N > 0 },( `0` ; `1` ),{ Nm is N-1 },any(Nm,gen).
any(0,gen) --> [].

% "any(N,genfree)" generates/accepts any bitstring of ARBITRARY length N
% The clause for [] must be first and the 01 arbitrary choice must come
% after the recursion.

any(0,genfree) --> [].
any(N,genfree) --> any(Nm,genfree),{ N is Nm+1 }.

% ---
% Run the DCG grammer in various modes
% ---

% When generating,use 'x' instead of '1' to make the special '1' visible
% When generating with N unstated we need to make sure we "go to infinity"
% in an equitable manner. Going to infinity for the overall length and 
% dividing that length up between "any" suffix and "any" prefix sounds fair!

my_language(N,gen)     --> any(_,`1`,any(N,gen).
my_language(N,genfree) --> { between(0,inf,L),between(0,L,N),Prefix is L-N },any(Prefix,gen),gen).

% Pretending the overall length of the bitstring is unknown is quixotic.
% First it's inefficient and then it's awkward to avoid guessing longer
% and longer prefixes that are longer than the string overall and sink
% into the backtracking tarpit. Much better to use the overall bitstring 
% length!

my_language(N,accept,L)     --> { M is L-N-1 },any(M,acceptfree,L) --> { Lm is L-1,Lm,M is L-N-1 },gen).

% ===
% Printing
% ===

explain(Text,Bits,N) :-
  atom_chars(Bits,Chars),length(Suffix,append([Prefix,[X],Suffix],format("~s [~s][~s][~s]~n",[Text,Prefix,Suffix]).

% ===
% Call these. In all cases,the remainder must be the empty string because
% we use phrase/2,which is the same as phrase/3 with emtpy remainder
% ===

% accept Bits,with N unstated,and using the overall length of Bits

lang(Bits,N) :-
  var(N),nonvar(Bits),!,atom_codes(Bits,Codes),length(Codes,format("Accepting ~q of length ~q,suffix length unstated~n",[Bits,L]),phrase(my_language(N,explain("Found",N).

% accept Bits,with N stated,N) :-
  nonvar(N),need suffix of length ~q~n",N]),N).

% generate Bits,with N unstated (and thus proposable by Prolog)
   
lang(Bits,N) :- 
   var(N),var(Bits),format("Generating phrases,nothing stated~n",[]),explain("Generated",with N stated

lang(Bits,N) :- 
   integer(N),[N]),N).

...说明后缀长度:

?- lang('0101000100100001',_).
Accepting '0101000100100001' of length 16,suffix length unstated
Found [010100010010000][1][]
true ;
Found [0101000100][1][00001]
true ;
Found [0101000][1][00100001]
true ;
Found [010][1][000100100001]
true ;
Found [0][1][01000100100001]
true ;
false.

我们可以生成(越来越长)位串

...说明后缀长度:

?- lang('0101000100100001',2).
Accepting '0101000100100001' of length 16,need suffix of length 2
false.

?- lang('0101000100100001',5).
Accepting '0101000100100001' of length 16,need suffix of length 5
Found [0101000100][1][00001]
true ;
false.

...保留后缀长度并让 Prolog 提出一个:

?- lang(X,2).
Generating phrases,need suffix of length 2
Generated [][1][00]
X = '100' ;
Generated [][1][01]
X = '101' ;
Generated [][1][10]
X = '110' ;
Generated [][1][11]
X = '111' ;
Generated [0][1][00]
X = '0100' ;
Generated [0][1][01]
X = '0101' ;
Generated [0][1][10]
X = '0110' ;
Generated [0][1][11]
X = '0111' ;
Generated [1][1][00]
X = '1100' ;
Generated [1][1][01]
X = '1101' ;
Generated [1][1][10]
X = '1110' ;
Generated [1][1][11]
X = '1111' ;
Generated [00][1][00]
X = '00100' 
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。