progs/nfa.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 28 Apr 2017 11:01:25 +0100
changeset 487 a697421eaa04
parent 485 bd6d999ab7b6
child 488 598741d39d21
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)
bd6d999ab7b6 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
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
     5
// states class
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
     6
abstract class State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
     7
case object Q0 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
     8
case object Q1 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
     9
case object Q2 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    10
case object Q3 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    11
case object Q4 extends State
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    12
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    13
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    14
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    15
// type abbreviation for partial functions
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    16
type :=>[A, B] = PartialFunction[A, B]
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    18
// return an empty set when not defined
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    19
def applyOrElse[A, B](f: A :=> Set[B], x: A) : Set[B] =
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    20
  Try(f(x)) getOrElse Set[B]()
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    21
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    22
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    23
// NFAs
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    24
case class NFA[A, C](starts: Set[A],           // starting states
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    25
                     delta: (A, C) :=> Set[A], // transition function
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    26
                     fins:  A => Boolean) {    // final states 
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 a state and a character, what is the set of 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    29
  // next states? if there is none => empty set
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    30
  def next(q: A, c: C) : Set[A] = 
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    31
    applyOrElse(delta, (q, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    33
  def nexts(qs: Set[A], c: C) : Set[A] =
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
    qs.flatMap(next(_, c))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    36
  // given some states and a string, what is the set 
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    37
  // of next states?
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    38
  def deltas(qs: Set[A], s: List[C]) : Set[A] = s match {
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    39
    case Nil => qs
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    40
    case c::cs => deltas(nexts(qs, c), cs)
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
485
bd6d999ab7b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
    43
  // is a string accepted by an NFA?
482
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
  def accepts(s: List[C]) : Boolean = 
0f6e3c5a1751 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    45
    deltas(starts, s).exists(fins)
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    46
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    47
  // depth-first version of accepts
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    48
  def search(q: A, s: List[C]) : Boolean = s match {
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    49
    case Nil => fins(q)
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    50
    case c::cs => next(q, c).exists(search(_, cs))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    51
  }
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
  def accepts2(s: List[C]) : Boolean =
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    54
    starts.exists(search(_, s))
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
}
487
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    56
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    57
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
// NFA examples
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 nfa_trans1 : (State, Char) :=> Set[State] = 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    62
  { case (Q0, 'a') => Set(Q0, Q1) 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    63
    case (Q0, 'b') => Set(Q2) 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    64
    case (Q1, 'a') => Set(Q1) 
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    65
    case (Q2, 'b') => Set(Q2) }
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
val nfa1 = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    68
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    69
nfa1.accepts("aa".toList)             // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    70
nfa1.accepts("aaaaa".toList)          // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    71
nfa1.accepts("aaaaab".toList)         // true
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    72
nfa1.accepts("aaaaabbb".toList)       // true
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    73
nfa1.accepts("aaaaabbbaaa".toList)    // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    74
nfa1.accepts("ac".toList)             // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    75
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    76
nfa1.accepts2("aa".toList)             // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    77
nfa1.accepts2("aaaaa".toList)          // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    78
nfa1.accepts2("aaaaab".toList)         // true
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    79
nfa1.accepts2("aaaaabbb".toList)       // true
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    80
nfa1.accepts2("aaaaabbbaaa".toList)    // false
a697421eaa04 updated
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    81
nfa1.accepts2("ac".toList)             // false