author | Christian Urban <urbanc@in.tum.de> |
Tue, 11 Apr 2017 06:22:46 +0800 | |
changeset 483 | 6f508bcdaa30 |
parent 481 | acd8780bfc8b |
child 485 | bd6d999ab7b6 |
permissions | -rw-r--r-- |
480 | 1 |
// DFAs in Scala based on partial functions |
2 |
import scala.util.Try |
|
479 | 3 |
|
480 | 4 |
// type abbreviation for partial functions |
5 |
type :=>[A, B] = PartialFunction[A, B] |
|
479 | 6 |
|
483 | 7 |
case class DFA[A, C](start: A, // starting state |
8 |
delta: (A, C) :=> A, // transition (partial fun) |
|
9 |
fins: A => Boolean) { // final states |
|
479 | 10 |
|
480 | 11 |
def deltas(q: A, s: List[C]) : A = s match { |
12 |
case Nil => q |
|
13 |
case c::cs => deltas(delta(q, c), cs) |
|
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
} |
479 | 15 |
|
480 | 16 |
def accepts(s: List[C]) : Boolean = |
17 |
Try(fins(deltas(start, s))) getOrElse false |
|
479 | 18 |
} |
19 |
||
480 | 20 |
// the example shown earlier in the handout |
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
abstract class State |
480 | 22 |
case object Q0 extends State |
23 |
case object Q1 extends State |
|
24 |
case object Q2 extends State |
|
25 |
case object Q3 extends State |
|
26 |
case object Q4 extends State |
|
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
|
480 | 28 |
val delta : (State, Char) :=> State = |
29 |
{ case (Q0, 'a') => Q1 |
|
30 |
case (Q0, 'b') => Q2 |
|
31 |
case (Q1, 'a') => Q4 |
|
32 |
case (Q1, 'b') => Q2 |
|
33 |
case (Q2, 'a') => Q3 |
|
34 |
case (Q2, 'b') => Q2 |
|
35 |
case (Q3, 'a') => Q4 |
|
36 |
case (Q3, 'b') => Q0 |
|
37 |
case (Q4, 'a') => Q4 |
|
38 |
case (Q4, 'b') => Q4 } |
|
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
480 | 40 |
val dfa = DFA(Q0, delta, Set[State](Q4)) |
145
920f675b4ed1
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
480 | 42 |
dfa.accepts("bbabaab".toList) // true |
43 |
dfa.accepts("baba".toList) // false |