progs/app6.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 29 Oct 2023 00:06:30 +0100
changeset 947 dc31a099dc98
parent 565 2be8c4c77418
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
     1
def simp(r: Rexp) : Rexp = r match {
262
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
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
     6
      case (r1s, r2s) => 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
     7
        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
     8
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
     9
  }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    10
  case SEQ(r1, r2) => {
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    11
    (simp(r1), simp(r2)) match {
399
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 (_, ZERO) => ZERO
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    14
      case (ONE, r2s) => r2s
5c1fbb39c93e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 343
diff changeset
    15
      case (r1s, ONE) => r1s
343
539b2e88f5b9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 262
diff changeset
    16
      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
    17
    }
ee4304bc6350 updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    18
  }
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
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
    22
def ders(s: List[Char], r: Rexp) : Rexp = s match {
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    23
  case Nil => r
435
e08431a78f66 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
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