fsa(W):- 
	start(Q),
	fsa(Q,W).

fsa(Q,[]):-
	final(Q),
	write('automaton accepts word in state '), write(Q),nl,!.

fsa(Q,[]):-
	write('automaton does not accept word and ends in state '), write(Q),nl.
	
fsa(Q,[S|W]):-
	d(Q,S,QN),
	write([Q,S,QN]),nl,
	fsa(QN,W).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fsa_no_loop(W):- 
	start(Q),
	fsa_no_loop(Q,W,[Q]).

fsa_no_loop(Q,[],_):-
	final(Q),
	write('automaton accepts word in state '), write(Q),nl,!.

fsa_no_loop(Q,[],_):-
	fail.
	
fsa_no_loop(Q,[S|W],QL):-
	d(Q,S,QN),
%	write([Q,S,QN]),nl,
	not_member(QN,QL),
	fsa_no_loop(QN,W,[QN|QL]).

not_member(E,[]).
	
not_member(E,[E|M]):-
	!,
	fail.

not_member(E,[_|M]):-
	not_member(E,M).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
start(bzwkl).
final(lbzwk).

d(bzwkl,b,eat_fail).
d(bzwkl,bz,wklbz).
d(bzwkl,bw,eat_fail).
d(bzwkl,bk,eat_fail).

d(wklbz,b,bwklz).
d(wklbz,bz,bzwkl).
d(wklbz,bw,impossible).
d(wklbz,bk,impossible).

d(bwklz,b,wklbz).
d(bwklz,bz,impossible).
d(bwklz,bw,klbzw).
d(bwklz,bk,wlbzk).

d(klbzw,b,eat_fail).
d(klbzw,bz,bzklw).
d(klbzw,bw,bwklz).
d(klbzw,bk,impossible).

d(wlbzk,b,eat_fail).
d(wlbzk,bz,bzwlk).
d(wlbzk,bw,impossible).
d(wlbzk,bk,bwklz).


d(bzklw,b,eat_fail).
d(bzklw,bz,klbzw).
d(bzklw,bw,impossible).
d(bzklw,bk,zlbwk).

d(bwklz,b,wklbz).
d(bwklz,bz,impossible).
d(bwklz,bw,klbzw).
d(bwklz,bk,wlbzk).

d(bzwlk,b,eat_fail).
d(bzwlk,bz,wlbzk).
d(bzwlk,bw,zlbwk).
d(bzwlk,bk,impossible).

d(zlbwk,b,bzlwk).
d(zlbwk,bz,impossible).
d(zlbwk,bw,bzwlk).
d(zlbwk,bk,bzklw).

d(bzlwk,b,zlbwk).
d(bzlwk,bz,lbzwk).
d(bzlwk,bw,impossible).
d(bzlwk,bk,impossible).

d(lbzwk,b,eat_fail).
d(lbzwk,bz,bzlwk).
d(lbzwk,bw,eat_fail).
d(lbzwk,bk,eat_fail).

d(eat_fail,b,eat_fail).
d(eat_fail,bw,eat_fail).
d(eat_fail,bz,eat_fail).
d(eat_fail,bk,eat_fail).


d(impossible,b,impossible).
d(impossible,bw,impossible).
d(impossible,bz,impossible).
d(impossible,bk,impossible).