progs/display/nfa.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 28 Apr 2017 11:01:25 +0100
changeset 487 ffbc65112d48
parent 485 progs/nfa.scala@21dec9df46ba
child 491 7a0182c66403
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     1
// NFAs in Scala using partial functions (returning
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     2
// sets of states)
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     3
import scala.util.Try
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
482
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     5
// type abbreviation for partial functions
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     6
type :=>[A, B] = PartialFunction[A, B]
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     8
// return an empty set when not defined
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     9
def applyOrElse[A, B](f: A :=> Set[B], x: A) : Set[B] =
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    10
  Try(f(x)) getOrElse Set[B]()
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    11
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    12
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    13
// NFAs
487
ffbc65112d48 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    14
case class NFA[A, C](starts: Set[A],           // starting states
ffbc65112d48 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    15
                     delta: (A, C) :=> Set[A], // transition function
ffbc65112d48 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    16
                     fins:  A => Boolean) {    // final states 
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    18
  // given a state and a character, what is the set of 
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    19
  // next states? if there is none => empty set
482
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    20
  def next(q: A, c: C) : Set[A] = 
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    21
    applyOrElse(delta, (q, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
482
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    23
  def nexts(qs: Set[A], c: C) : Set[A] =
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    24
    qs.flatMap(next(_, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    26
  // given some states and a string, what is the set 
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    27
  // of next states?
482
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    28
  def deltas(qs: Set[A], s: List[C]) : Set[A] = s match {
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    29
    case Nil => qs
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    30
    case c::cs => deltas(nexts(qs, c), cs)
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
485
21dec9df46ba updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    33
  // is a string accepted by an NFA?
482
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
  def accepts(s: List[C]) : Boolean = 
74149519e436 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    35
    deltas(starts, s).exists(fins)
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
}