progs/automata/enfa.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 14 Dec 2020 19:22:12 +0000
changeset 818 6928a677d26f
parent 779 5385c8342f02
child 884 183bfb52d26e
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
733
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 623
diff changeset
     2
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 623
diff changeset
     3
import $file.dfa, dfa._ 
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 623
diff changeset
     4
import $file.nfa, nfa._ 
022e2cb1668d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 623
diff changeset
     5
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
     6
// a fixpoint construction
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
import scala.annotation.tailrec
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
@tailrec
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
     9
def fixpT[S](f: S => S, x: S): S = {
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  val fx = f(x)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  if (fx == x) x else fixpT(f, fx) 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
}
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// translates eNFAs directly into NFAs 
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    15
def eNFA[A, C](starts: Set[A],                     // the starting states
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    16
               delta: (A, Option[C]) :=> Set[A],   // the epsilon-transitions
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    17
               fins: A => Boolean) : NFA[A, C] = { // the final states 
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  // epsilon transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  def enext(q: A) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    applyOrElse(delta, (q, None))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  def enexts(qs: Set[A]) : Set[A] = 
488
598741d39d21 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    24
    qs | qs.flatMap(enext(_))     // | is the set-union in Scala
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  // epsilon closure
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  def ecl(qs: Set[A]) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    fixpT(enexts, qs)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  // "normal" transitions
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  def next(q: A, c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    applyOrElse(delta, (q, Some(c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  def nexts(qs: Set[A], c: C) : Set[A] = 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    ecl(ecl(qs).flatMap(next(_, c)))
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  // result NFA
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  NFA(ecl(starts), 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
      { case (q, c) => nexts(Set(q), c) }, 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
      q => ecl(Set(q)) exists fins)
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
487
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
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    44
// eNFA examples
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    45
val enfa_trans1 : (State, Option[Char]) :=> Set[State] =
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    46
  { case (Q0, Some('a')) => Set(Q0)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    47
    case (Q0, None) => Set(Q1, Q2)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    48
    case (Q1, Some('a')) => Set(Q1)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    49
    case (Q2, Some('b')) => Set(Q2) 
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
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    52
val nfa1 = 
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    53
  eNFA(Set[State](Q0), enfa_trans1, Set[State](Q2))
487
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
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    56
// more examples
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    57
case object R1 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    58
case object R2 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    59
case object R3 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    60
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    61
val enfa_trans2 : (State, Option[Char]) :=> Set[State] =
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    62
  { case (R1, Some('b')) => Set(R3)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    63
    case (R1, None) => Set(R2)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    64
    case (R2, Some('a')) => Set(R1, R3) 
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
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    67
779
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    68
val nfa2 = 
5385c8342f02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 753
diff changeset
    69
  eNFA(Set[State](R1), enfa_trans1, Set[State](R3))