How to define this regular language in Dafny?

131 views Asked by At

There is a question in the book "Automata Theory and Applications" that talks about the language

equation describing a language L that consists of all strings w containing only the characters 'a' and 'b' such that there exists a nonempty string x containing only the characters 'a' and 'b' such that w equals the character 'a' followed by the string x followed by the character 'a'

How can I define such a language in Dafny?

1

There are 1 answers

0
James Wilcox On BEST ANSWER

Here is a straightforward transcription of the definition of L into Dafny.

predicate ContainsOnlyAsAndBs(s: string)
{
  forall c | c in s :: c == 'a' || c == 'b'
}

predicate L(w: string)
  requires ContainsOnlyAsAndBs(w)
{
  exists x | ContainsOnlyAsAndBs(x) && x != [] :: w == ['a'] + x + ['a']
}

Note that in Dafny, the type string is defined to be seq<char>. So we can use the usual built-in methods of manipulating sequences on strings.