progs/app6.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 15 Jul 2016 10:54:09 +0100
changeset 406 0a42d73e795b
parent 399 5c1fbb39c93e
child 412 1cef3924f7a2
permissions -rw-r--r--
repl-slides
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     1
def simp(r: Rexp): Rexp = r match {
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     2
  case ALT(r1, r2) => {
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     3
    (simp(r1), simp(r2)) match {
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
     4
      case (ZERO, r2s) => r2s
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
     5
      case (r1s, ZERO) => r1s
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
     6
      case (r1s, r2s) => if (r1s == r2s) r1s else ALT(r1s, r2s)
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     7
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     8
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     9
  case SEQ(r1, r2) => {
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    10
    (simp(r1), simp(r2)) match {
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    11
      case (ZERO, _) => ZERO
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    12
      case (_, ZERO) => ZERO
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    13
      case (ONE, r2s) => r2s
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    14
      case (r1s, ONE) => r1s
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    15
      case (r1s, r2s) => SEQ(r1s, r2s)
262
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    16
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    17
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    18
  case NTIMES(r, n) => NTIMES(simp(r), n)    
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    19
  case r => r
7
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
73cf4406b773 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    22
def ders (s: List[Char], r: Rexp) : Rexp = s match {
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    23
  case Nil => r
399
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    24
  case c::s => ders(s, simp(der(c, r))) (*@\label{simpline}@*) 
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    25
}
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    26