progs/display/nfa.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 10:59:52 +0100
changeset 632 b7b91158eded
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: 483
diff changeset
     1
// NFAs in Scala using partial functions (returning
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
     2
// sets of states)
491
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
     3
//
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
     4
// needs :load dfa.scala   for states
d5776c6018f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
     5
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     7
// type abbreviation for partial functions
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     8
type :=>[A, B] = PartialFunction[A, B]
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    10
// return an empty set when not defined
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    11
def applyOrElse[A, B](f: A :=> Set[B], x: A) : Set[B] =
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    12
  Try(f(x)) getOrElse Set[B]()
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    13
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    14
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    15
// NFAs
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    16
case class NFA[A, C](starts: Set[A],           // starting states
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    17
                     delta: (A, C) :=> Set[A], // transition function
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    18
                     fins:  A => Boolean) {    // final states 
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    20
  // given a state and a character, what is the set of 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    21
  // next states? if there is none => empty set
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    22
  def next(q: A, c: C) : Set[A] = 
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    23
    applyOrElse(delta, (q, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
  def nexts(qs: Set[A], c: C) : Set[A] =
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    26
    qs.flatMap(next(_, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    28
  // given some states and a string, what is the set 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    29
  // of next states?
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    30
  def deltas(qs: Set[A], s: List[C]) : Set[A] = s match {
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    31
    case Nil => qs
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    32
    case c::cs => deltas(nexts(qs, c), cs)
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    35
  // is a string accepted by an NFA?
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    36
  def accepts(s: List[C]) : Boolean = 
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    37
    deltas(starts, s).exists(fins)
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
}