updated
authorChristian Urban <urbanc@in.tum.de>
Sun, 30 Sep 2018 12:01:14 +0100
changeset 564 b5d57d7064bb
parent 563 bddf14e026b3
child 565 2be8c4c77418
updated
progs/pow.scala
progs/re2.scala
progs/re3.scala
slides/slides02.pdf
slides/slides02.tex
--- a/progs/pow.scala	Fri Sep 28 13:54:18 2018 +0100
+++ b/progs/pow.scala	Sun Sep 30 12:01:14 2018 +0100
@@ -1,5 +1,5 @@
 def concat(A: Set[String], B: Set[String]) : Set[String] =
-  for (x <-A ; y <- B) yield x ++ y
+  for (x <- A ; y <- B) yield x ++ y
 
 def pow(A: Set[String], n: Int) : Set[String] = n match {
   case 0 => Set("")
@@ -33,3 +33,7 @@
 
 val A = Set("a", "b", "")
 pow(A, 5).size   
+
+
+for (n <- (0 to 12).toList) 
+  yield pow(A, n).size
\ No newline at end of file
--- a/progs/re2.scala	Fri Sep 28 13:54:18 2018 +0100
+++ b/progs/re2.scala	Sun Sep 30 12:01:14 2018 +0100
@@ -45,6 +45,7 @@
 
 
 //optional regular expression: one or zero times
+//this regular expression is still defined in terms of ALT
 def OPT(r: Rexp) = ALT(r, ONE)
 
 
@@ -73,11 +74,11 @@
 
 
 //test: (a*)* b
-for (i <- 1 to 20) {
+for (i <- 1 to 21) {
   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
 }
 
-for (i <- 1 to 20) {
+for (i <- 1 to 21) {
   println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
 }
 
@@ -95,19 +96,29 @@
 }
 
 // EVIL1(n) has now a constant size, no matter
-// what n is 
+// what n is; also the derivative only grows 
+// moderately 
 
 size(EVIL1(1))  // 7
 size(EVIL1(3))  // 7
 size(EVIL1(5))  // 7
 size(EVIL1(7))  // 7
 
+size(ders("".toList, EVIL1(5)))       // 7
+size(ders("a".toList, EVIL1(5)))      // 16
+size(ders("aa".toList, EVIL1(5)))     // 35
+size(ders("aaa".toList, EVIL1(5)))    // 59
+size(ders("aaaa".toList, EVIL1(5)))   // 88
+size(ders("aaaaa".toList, EVIL1(5)))  // 122
+size(ders("aaaaaa".toList, EVIL1(5))) // 151
 
 // but the size of the derivatives can still grow 
-// quite dramatically
+// quite dramatically in case of EVIL2
 
-size(ders("".toList, EVIL2))     // 5
-size(ders("a".toList, EVIL2))    // 12
-size(ders("aa".toList, EVIL2))   // 28
-size(ders("aaa".toList, EVIL2))  // 58
-size(ders("aaaa".toList, EVIL2)) // 116
+size(ders("".toList, EVIL2))       // 5
+size(ders("a".toList, EVIL2))      // 12
+size(ders("aa".toList, EVIL2))     // 28
+size(ders("aaa".toList, EVIL2))    // 58
+size(ders("aaaa".toList, EVIL2))   // 116
+size(ders("aaaaa".toList, EVIL2))  // 230
+size(ders("aaaaaa".toList, EVIL2)) // 456
--- a/progs/re3.scala	Fri Sep 28 13:54:18 2018 +0100
+++ b/progs/re3.scala	Sun Sep 30 12:01:14 2018 +0100
@@ -1,4 +1,4 @@
-// Version with simplification during derivatives;
+// A version with simplification of derivatives;
 // this keeps the regular expressions small, which
 // is good for run-time
  
@@ -13,31 +13,6 @@
 case class NTIMES(r: Rexp, n: Int) extends Rexp 
 
 
-// string of a regular expressions - for testing purposes 
-def string(r: Rexp) : String = r match {
-  case ZERO => "0"
-  case ONE => "1"
-  case CHAR(c) => c.toString 
-  case ALT(r1, r2) => s"(${string(r1)} + ${string(r2)})"
-  case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}"
-  case SEQ(ONE, CHAR(c)) => s"1${c}"
-  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
-  case STAR(r) => s"(${string(r)})*"
-  case NTIMES(r, n) =>  s"(${string(r)}){${n}}"
-}
-
-// size of a regular expressions - for testing purposes 
-def size(r: Rexp) : Int = r match {
-  case ZERO => 1
-  case ONE => 1
-  case CHAR(_) => 1
-  case ALT(r1, r2) => 1 + size(r1) + size(r2)
-  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
-  case STAR(r) => 1 + size(r)
-  case NTIMES(r, _) => 1 + size(r)
-}
-
-
 
 // nullable function: tests whether the regular 
 // expression can recognise the empty string
@@ -65,38 +40,27 @@
     if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
 }
 
-
-
-def simp(r: Rexp, seen: Set[Rexp]) : (Rexp, Set[Rexp]) = r match {
-  case ALT(r1, r2) => {
-    val (r1s, seen1) = simp(r1, seen)
-    val (r2s, seen2) = simp(r2, seen1)
-    (r1s, r2s) match {
-      case (ZERO, r2s) => (r2s, seen2)
-      case (r1s, ZERO) => (r1s, seen2)
-      case (r1s, r2s) => (ALT(r1s, r2s), seen2)
-    }
+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) =>  {
-    val (r1s, _) = simp(r1, Set())
-    val (r2s, _) = simp(r2, Set())
-    if (seen.contains(SEQ(r1s, r2s))) (ZERO, seen)
-    else (r1s, r2s) match {
-      case (ZERO, _) => (ZERO, seen)
-      case (_, ZERO) => (ZERO, seen)
-      case (ONE, r2s) => (r2s, seen + r2s)
-      case (r1s, ONE) => (r1s, seen + r1s)
-      case (r1s, r2s) => (SEQ(r1s, r2s), seen + SEQ(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 r => if (seen.contains(r)) (ZERO, seen) else (r, seen + r)
+  case r => r
 }
 
 
 // derivative w.r.t. a string (iterates der)
 def ders (s: List[Char], r: Rexp) : Rexp = s match {
   case Nil => r
-  case c::s => ders(s, simp(der(c, r), Set())._1)
+  case c::s => ders(s, simp(der(c, r)))
 }
 
 
@@ -108,75 +72,6 @@
 def OPT(r: Rexp) = ALT(r, ONE)
 
 
-
-
-
-
-
-
-
-def time_needed[T](i: Int, code: => T) = {
-  val start = System.nanoTime()
-  for (j <- 1 to i) code
-  val end = System.nanoTime()
-  (end - start)/(i * 1.0e9)
-}
-
-
-// star example
-val Tstar = STAR(STAR(STAR(CHAR('a'))))
-
-string(ders("".toList, Tstar))
-size(ders("".toList, Tstar))      // 4
-string(ders("a".toList, Tstar))
-size(ders("a".toList, Tstar))     // 11
-string(ders("aa".toList, Tstar))
-size(ders("aa".toList, Tstar))    // 11
-string(ders("aaa".toList, Tstar))
-size(ders("aaa".toList, Tstar))   // 11
-string(ders("aaaa".toList, Tstar))
-size(ders("aaaa".toList, Tstar))  // 11
-string(ders("aaaa".toList, Tstar))
-size(ders("aaaaa".toList, Tstar)) // 11
-string(ders("aaaab".toList, Tstar))
-size(ders("aaaaab".toList, Tstar)) // 1
-
-// test: ("a" | "aa")*
-val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
-
-for (i <- 1 to 100 by 1) {
-  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + 
-	  " size: " + size(ders(("a" * i).toList, EVIL3)))
-}
-
-
-println("start " + string(EVIL3) + "    " + size(EVIL3))
-val t1  = der('a', EVIL3)
-println(string(t1) + "    " + size(t1))
-val t1s = simp(t1, Set())._1 
-println("simplified " + string(t1s) + "    " + size(t1s))
-val t2  = der('a', t1s)
-println(string(t2) + "    " + size(t2))
-val t2s = simp(t2, Set())._1 
-println("simplified " + string(t2s) + "    " + size(t2s))
-val t3  = der('a', t2s)
-println(string(t3) + "    " + size(t3))
-val t3s = simp(t3, Set())._1 
-println("simplified " + string(t3s) + "    " + size(t3s))
-val t4  = der('a', t3s)
-val t4s = simp(t4, Set())._1 
-
-
-
-
-
-
-
-
-println(string(t4) + "    " + size(t4))
-println("simplified " + string(t4s) + "    " + size(t4s))
-
-
 // Test Cases
 
 //evil regular expressions: (a?){n} a{n}  and (a*)* b
@@ -193,11 +88,11 @@
 
 
 //test: (a?{n}) (a{n})
-for (i <- 1 to 8001 by 1000) {
+for (i <- 1 to 7001 by 1000) {
   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
 }
 
-for (i <- 1 to 8001 by 1000) {
+for (i <- 1 to 7001 by 1000) {
   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
 }
 
@@ -211,7 +106,6 @@
 }
 
 
-
 // size of a regular expressions - for testing purposes 
 def size(r: Rexp) : Int = r match {
   case ZERO => 1
@@ -235,28 +129,13 @@
 size(ders("aaaaa".toList, EVIL2)) // 8
 
 
+// test: ("a" | "aa")*
+val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
+
+for (i <- 1 to 29 by 1) {
+  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + 
+	  " size: " + size(ders(("a" * i).toList, EVIL3)))
+}
 
 
 
-// Examples from the Sulzmann paper
-val sulzmann = STAR(ALT(ALT(CHAR('a'), CHAR('b')), SEQ(CHAR('a'), CHAR('b'))))
-
-
-for (i <- 1 to 4501 by 500) {
-  println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "a" * i))))
-}
-
-for (i <- 1 to 4501 by 500) {
-  println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "ab" * i))))
-}
-
-size(ders("".toList, EVIL2))      // 5
-size(ders("a".toList, EVIL2))     // 8
-size(ders("aa".toList, EVIL2))    // 8
-size(ders("aaa".toList, EVIL2))   // 8
-size(ders("aaaa".toList, EVIL2))  // 8
-size(ders("aaaaa".toList, EVIL2)) // 8
-
-
-
-(((1 + 1a) ~ ((a + aa))*) + (((0 + 1) ~ ((a + aa))*) + ((1 + 1a) ~ ((a + aa))*)))
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex	Fri Sep 28 13:54:18 2018 +0100
+++ b/slides/slides02.tex	Sun Sep 30 12:01:14 2018 +0100
@@ -35,7 +35,7 @@
   \begin{center}
   \begin{tabular}{ll}
   Email:  & christian.urban at kcl.ac.uk\\
-  Office: & N7.07 (North Wing, Bush House)\\
+  Office: & N\liningnums{7.07} (North Wing, Bush House)\\
   Slides: & KEATS (also homework is there)
   \end{tabular}
   \end{center}