progs/enfa.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 10:59:52 +0100
changeset 632 b7b91158eded
parent 623 47a299e7010f
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 :load dfa.scala 
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
     3
//        :load nfa.scala in REPL)
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
     5
// a fixpoint construction
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import scala.annotation.tailrec
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
@tailrec
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def fixpT[A](f: A => A, x: A): A = {
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  val fx = f(x)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  if (fx == x) x else fixpT(f, fx) 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
}
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// translates eNFAs directly into NFAs 
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    14
def eNFA[A, C](starts: Set[A],                     // the starting states
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    15
               delta: (A, Option[C]) :=> Set[A],   // the epsilon-transitions
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    16
               fins: A => Boolean) : NFA[A, C] = { // the final states 
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  // epsilon transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  def enext(q: A) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
    applyOrElse(delta, (q, None))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  def enexts(qs: Set[A]) : Set[A] = 
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    23
    qs | qs.flatMap(enext(_))     // | is the set-union in Scala
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  // epsilon closure
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  def ecl(qs: Set[A]) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
    fixpT(enexts, qs)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  // "normal" transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  def next(q: A, c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
    applyOrElse(delta, (q, Some(c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  def nexts(qs: Set[A], c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    ecl(ecl(qs).flatMap(next(_, c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  // result NFA
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  NFA(ecl(starts), 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
      { case (q, c) => nexts(Set(q), c) }, 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
      q => ecl(Set(q)) exists fins)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    41
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    42
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    43
// eNFA examples
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    44
val enfa_trans1 : (State, Option[Char]) :=> Set[State] =
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    45
  { case (Q0, Some('a')) => Set(Q0)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    46
    case (Q0, None) => Set(Q1, Q2)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    47
    case (Q1, Some('a')) => Set(Q1)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    48
    case (Q2, Some('b')) => Set(Q2) 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    49
  }
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    50
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    51
val enfa1 = eNFA(Set[State](Q0), enfa_trans1, Set[State](Q2))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    52
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    53
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    54
//
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    55
case object R1 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    56
case object R2 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    57
case object R3 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    58
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    59
val enfa_trans2 : (State, Option[Char]) :=> Set[State] =
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    60
  { case (R1, Some('b')) => Set(R3)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    61
    case (R1, None) => Set(R2)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    62
    case (R2, Some('a')) => Set(R1, R3) 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    63
  }
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    64
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    65
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    66
val enfa2 = eNFA(Set[State](R1), enfa_trans1, Set[State](R3))