Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找有限自动机(Prolog)接受的所有K长度字_Prolog_Finite Automata - Fatal编程技术网

查找有限自动机(Prolog)接受的所有K长度字

查找有限自动机(Prolog)接受的所有K长度字,prolog,finite-automata,Prolog,Finite Automata,为了在prolog中表示M,我使用以下谓词: states /*states(Q) <=> Q is the list of automata's states*/ symbols /*symbols(Sigma) <=> Sigma is the list of automata's input symbols*/ transition /*transition(X, A, Y) <=> δ(X, A)=Y*/ startState /

为了在prolog中表示M,我使用以下谓词:

states      /*states(Q) <=> Q is the list of automata's states*/
symbols     /*symbols(Sigma) <=> Sigma is the list of automata's input symbols*/
transition  /*transition(X, A, Y) <=> δ(X, A)=Y*/
startState  /*startState(S) <=> S is the start state of automata*/
finalStates /*finalStates(F) <=> F is the list of automata's final states */
假设五个w单词被M自动机
accepted(w)
识别(接受)(w是单词的表示列表)

其中,
accepted1
w属于一种通过自动机的Q状态识别的语言

accepted1(Q, []):- finalStates(F), !, member(Q, F).
accepted1(Q, [A|W]):- transition(Q, A, Q1), accepted1(Q1, W),!.

这里的问题是:如何找到给定的M有限自动机所接受的所有正K长度的单词?

从一个声明式pov,您可以只编写一个谓词
l\u power\u n(W,K)
,它将W与∑K中的任何字符串统一起来。然后只需生成并测试目标
l\u power\u K(W,K),accepted(W).


另一种方法是设置所需长度的虚拟列表。您可以将辅助列表作为参数添加到您的
accepted1

accepted1(Q, [], []) :- finalStates(F), !, member(Q, F).
accepted1(Q, [_|K], [A|W]) :- transition(Q, A, Q1), accepted1(Q1, K, W),!.

accept_length(Q, R, Length) :-
    length(K, Length),
    accepted1(Q, K, R).
accepted1(Q, []):- finalStates(F), !, member(Q, F).
accepted1(Q, [A|W]):- transition(Q, A, Q1), accepted1(Q1, W),!.
states([q0, q1, q2]).
symbols([a, b]).
transition(q0, a, q1).
transition(q0, b, q2).
transition(q1, a, q2).
transition(q1, b, q0).
transition(q2, a, q2). % Edited to reflect image
transition(q2, b, q1). % Edited to reflect image
startState(q0).
finalStates([q2]).

accepted(W):-startState(Q0), accepted1(Q0, W).

accepted1(Q, []):- finalStates(F), !, member(Q, F).
accepted1(Q, [A|W]):- transition(Q, A, Q1), accepted1(Q1, W),!.


% in_language(Word, Length): True if Word is of length Length and is part of the language.
l_power_k([], 0):- !.
l_power_k([WHead|WTail],K):-
    symbols(Symbols), member( WHead,Symbols ),
    K1 is K-1,
    l_power_k( WTail, K1).
accepted1(Q, [], []) :- finalStates(F), !, member(Q, F).
accepted1(Q, [_|K], [A|W]) :- transition(Q, A, Q1), accepted1(Q1, K, W),!.

accept_length(Q, R, Length) :-
    length(K, Length),
    accepted1(Q, K, R).