/* File: dfa2.pl System: SWI-Prolog 5.2.8 Author: MM Date: 10/02/2003 Title: Explore if the DFA defined is well defined. DFA accepts words that start with a. Alphabet={a,b}, States={0,1,2} Favorable={1} start is state 0. Transition function is: delta(0,a)=1 delta(0,b)=2 delta(1,a)=1 delta(1,b)=1 delta(2,a)=2 delta(2,b)=2 Load your file: ?- ['C://swimm/dfa2.pl']. Yes See if for state 0 there are exactly two arrows. That is delta(0,a) and delta(0,b) are defined. ?- check_state(0). Yes ?- check_dfa. 0good 1good 2bad *************************************************/ /* This is the definition of your DFA in Prolog. */ delta(0,a,1). delta(0,b,2). delta(1,a,1). delta(1,b,1). delta(2,a,2). delta(2,b,2). % tale this out in order to check also a "bad" situation start(0). favorable(1). /* A state appears in the delta function as argument 1 or 3. */ state(I):- delta(I,_,_) ; delta(_,_,I). /* Make the list of the states */ list_states(Res):- findall(X,state(X),L),sort(L,Res). /* Check if a state is correct from the DFA point of view */ check_state(S):- findall(X,delta(S,a,X),L1),length(L1,1), findall(X,delta(S,b,X),L2),length(L2,1). /* Check if all states are correct from the DFA point of view */ check_dfa:- forall((list_states(L),member(X,L)), (nl,write(X),check_state(X) -> write('good') ; write('bad')) ).