juan_gandhi: (Default)
2017-01-10 02:41 pm

now, again, Krohn-Rhodes theory

 A finite monoid can be decomposed to a bunch of simple finite groups (afaik, there are 26 kinds of them) and a parallel array of flip-flops.
Since an FSM is basically a monoid, an FSM can be decomposed the same way.

And I wonder, regexes, they can be decomposed/classified the same way.

Anybody here familiar with all this? I just found it.
juan_gandhi: (VP)
2015-09-27 09:32 pm
Entry tags:

free monad for regex

Say, we have a regex, right? This is a regular language; so there must be a free monad with a pretty simple structure. May be related to State monad, I guess.
juan_gandhi: (VP)
2013-10-04 05:10 pm

so, regex again

http://ivan-gandhi.livejournal.com/2488144.html

Yes, I was convinced that it's pretty much doable.
Thank you, [livejournal.com profile] migmit, [livejournal.com profile] archaicos, [livejournal.com profile] sassa_nf, [livejournal.com profile] hamster37, [livejournal.com profile] mikkim08; good stuff.

Here's my code, based on the code by [livejournal.com profile] mstone wrote in C. Mine is in Scala.

def matches(s: String, r: String): Boolean = {
  def matchesOneChar(x:String) = !x.isEmpty && (r(0)=='.' || r(0) == x(0))
  if (r.isEmpty) s.isEmpty                                                     else
  if (!r.tail.startsWith("*")) matchesOneChar(s) && matches(s.tail, r.tail)    else
                               s.tails takeWhile (!matches(_, r drop 2)) forall matchesOneChar
}

def mustMatch   (s: String, r: String) = print(s"<<$s>> vs <<$r>>: ${if (matches(s, r)) "OK" else "*** bad!"}") 
def mustNotMatch(s: String, r: String) = print(s"<<$s>> vs <<$r>>: ${if(!matches(s, r)) "OK" else "*** bad!"}") 

    mustMatch("", "")
    mustNotMatch("", "a")
    mustMatch("a", "a*")
    mustMatch("a", ".*")
    mustMatch("", ".*")
    mustMatch("", "a*")
    mustNotMatch("b", "a*")
    mustMatch("b", ".*")
    mustMatch("b", "ba*")
    mustMatch("abba", "abba")
    mustNotMatch("", ".")
    mustMatch("a", ".")
    mustMatch("ac", "ab*c")
    mustMatch("abbbc", "ab*c")
    mustNotMatch("abbbc", "a.*b")
    mustMatch("abbbc", "a.*c")
    mustMatch("ac", ".*c")
    mustMatch("aac", "a*b*ac")



Comments?