progs/display/enfa.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 28 Sep 2018 13:54:18 +0100
changeset 563 bddf14e026b3
parent 491 d5776c6018f0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// epsilon NFAs...immediately translated into NFAs
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
     2
// (needs dfa.scala and nfa.scala)
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// fixpoint construction
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.annotation.tailrec
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
@tailrec
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def fixpT[A](f: A => A, x: A): A = {
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  val fx = f(x)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  if (fx == x) x else fixpT(f, fx) 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// translates eNFAs directly into NFAs 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def eNFA[A, C](starts: Set[A],                     // starting states
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
               delta: (A, Option[C]) :=> Set[A],   // epsilon-transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
               fins: A => Boolean) : NFA[A, C] = { // final states 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  // epsilon transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  def enext(q: A) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
    applyOrElse(delta, (q, None))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  def enexts(qs: Set[A]) : Set[A] = 
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    22
    qs | qs.flatMap(enext(_))     // | is the set-union in Scala
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  // epsilon closure
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  def ecl(qs: Set[A]) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    fixpT(enexts, qs)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  // "normal" transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  def next(q: A, c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
    applyOrElse(delta, (q, Some(c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  def nexts(qs: Set[A], c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    ecl(ecl(qs).flatMap(next(_, c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  // result NFA
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  NFA(ecl(starts), 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
      { case (q, c) => nexts(Set(q), c) }, 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
      q => ecl(Set(q)) exists fins)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}