progs/re4.scala
changeset 422 5deefcc8cffa
parent 415 4ae59fd3b174
child 425 d14a9bdac26f
--- a/progs/re4.scala	Tue Aug 23 23:12:55 2016 +0200
+++ b/progs/re4.scala	Tue Sep 20 12:24:29 2016 +0100
@@ -1,55 +1,12 @@
-import scala.annotation.tailrec    
-import scala.language.implicitConversions
     
-abstract class Rexp {
-  def simp : Rexp = this
-}
-
+abstract class Rexp
 case object ZERO extends Rexp
 case object ONE extends Rexp
 case class CHAR(c: Char) extends Rexp
-case class ALT(r1: Rexp, r2: Rexp) extends Rexp {
-  override def simp = (r1.simp, r2.simp) match {
-    case (ZERO, r) => r
-    case (r, ZERO) => r
-    case (r, ONE) => if (nullable(r)) r else ALT(r, ONE)
-    case (ONE, r) => if (nullable(r)) r else ALT(r, ONE)
-    case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
-  }
-}
-case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
-  override def simp = (r1.simp, r2.simp) match {
-    case (ZERO, _) => ZERO
-    case (_, ZERO) => ZERO
-    case (ONE, r) => r
-    case (r, ONE) => r
-    case (r1, r2) => SEQ(r1, r2)
-  }
-}
-case class STAR(r: Rexp) extends Rexp {
-  override def simp = r.simp match {
-    case ZERO => ONE
-    case ONE => ONE
-    case r => STAR(r)
-  }
-}
-case class NTIMES(r: Rexp, n: Int) extends Rexp {
-  override def simp = if (n == 0) ONE else 
-    r.simp match {
-      case ZERO => ZERO
-      case ONE => ONE
-      case r => NTIMES(r, n)
-    }
-}
-
-// some convenience for typing in regular expressions
-def charlist2rexp(s : List[Char]) : Rexp = s match {
-  case Nil => ONE
-  case c::Nil => CHAR(c)
-  case c::s => SEQ(CHAR(c), charlist2rexp(s))
-}
-implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
-
+case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
+case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
+case class STAR(r: Rexp) extends Rexp 
+case class NTIMES(r: Rexp, n: Int) extends Rexp 
 
 // nullable function: tests whether the regular 
 // expression can recognise the empty string
@@ -77,24 +34,48 @@
     if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
 }
 
-// derivative w.r.t. a string (iterates der)
-@tailrec
-def ders (s: List[Char], r: Rexp) : Rexp = s match {
-  case Nil => r
-  case c::s => ders(s, der(c, r).simp)
+def simp(r: Rexp) : Rexp = r match {
+  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
+    case (ZERO, r2s) => r2s
+    case (r1s, ZERO) => r1s
+    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
+  }
+  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
+    case (ZERO, _) => ZERO
+    case (_, ZERO) => ZERO
+    case (ONE, r2s) => r2s
+    case (r1s, ONE) => r1s
+    case (r1s, r2s) => SEQ(r1s, r2s)
+  }
+  case NTIMES(r1, n) => simp(r1) match {
+    case ZERO => ZERO
+    case ONE => ONE
+    case r1s => NTIMES(r1s, n)
+  }
+  case r => r
 }
 
-
+// derivative w.r.t. a string (iterates der)
+def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match {
+  case (Nil, r) => r
+  case (s, ZERO) => ZERO
+  case (s, ONE) => if (s == Nil) ONE else ZERO
+  case (s, CHAR(c)) => if (s == List(c)) ONE else 
+                       if (s == Nil) CHAR(c) else ZERO
+  case (s, ALT(r1, r2)) => ALT(ders2(s, r2), ders2(s, r2))
+  case (c::s, r) => ders2(s, simp(der(c, r)))
+}
 
 // main matcher function
-def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
+def matcher(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r))
 
 
 //one or zero
 def OPT(r: Rexp) = ALT(r, ONE)
 
-def EVIL1(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
-val EVIL2 = SEQ(STAR("a"), "b")
+def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
+val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
+
 
 def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
@@ -106,7 +87,7 @@
 val i = 5000
 println(i + " " + "%.5f".format(time_needed(10, matcher(EVIL1(i), "a" * i))))
 
-for (i <- 1 to 9001 by 1000) {
+for (i <- 1 to 7000001 by 1000000) {
   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
 }