0

I'm trying to create a Deterministic Finite Automaton(DFA) with Prolog but I'm stuck. I didn't have this problem with Haskell, so I would highly appreciate any help. So far, here is what I've done:

start(q1). final(q4). transition(q1, '.', q3). transition(q1, '0', q2). transition(q1, '1', q2). transition(q2, '.', q4). transition(q2, '0', q2). transition(q2, '1', q2). transition(q3, '.', q5). transition(q3, '0', q4). transition(q3, '1', q4). transition(q4, '.', q5). transition(q4, '0', q4). transition(q4, '1', q4). transition(q5, '0', q5). transition(q5, '1', q5). accept(symbols, startState). dfaAccept(symbols) :- start(startstate). dfaAccept([], State) :- final(State). dfaAccept([Symbol|Symbols], State) :- transition(State, Symbol, NextState), accept(Symbols, NextState). 

When I run

dfaAccept(df1, "1.01"). 

I should get true, but I'm getting false ....

1

1 Answer 1

1

I think DCGs are even better way to represent FSMs, but let's talk syntax:

You call

dfaAccept(df1, "1.01"). 

but predicate dfaAccept/2 expects the arguments in reverse order:

dfaAccept([Symbol|Symbols], State) 

df1 is also not mentioned again.

Then "1.01" may or may not be a "list of character (atoms of length 1)" depending on your Prolog. If it's SWI Prolog, you have to transmogrify it first using atom_chars/2.

And what is accept(symbols, startState). for? It's hanging around all by its lonesone.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.