--- a/exps/both.scala Fri Feb 01 14:31:38 2019 +0000
+++ b/exps/both.scala Mon Feb 04 12:29:23 2019 +0000
@@ -91,6 +91,17 @@
}
+// string of a regular expressions - for testing purposes
+ def string(r: Rexp): String = r match {
+ case ZERO => "0"
+ case ONE => "1"
+ case PRED(_) => "_"
+ case ALTS(rs) => rs.map(string).mkString("[", "|", "]")
+ case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
+ case STAR(r) => s"{${string(r)}}*"
+ case RECD(x, r) => s"(${x}! ${string(r)})"
+ }
+
//--------------------------------------------------------------------------------------------------------
// START OF NON-BITCODE PART
//
@@ -384,12 +395,15 @@
}
case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match {
case Nil => AZERO
- case s :: Nil => fuse(bs1, s)
+ case r :: Nil => fuse(bs1, r)
case rs => AALTS(bs1, rs)
}
+ //case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1))
case r => r
}
+
+
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
case Nil => r
case c::s => bders_simp(s, bsimp(bder(c, r)))
@@ -410,6 +424,43 @@
+// INCLUDING SIMPLIFICATION UNDER STARS
+
+def bsimp_full(r: ARexp): ARexp = r match {
+ case ASEQ(bs1, r1, r2) => (bsimp_full(r1), bsimp_full(r2)) match {
+ case (AZERO, _) => AZERO
+ case (_, AZERO) => AZERO
+ case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
+ case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
+ }
+ case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp_full)), erase) match {
+ case Nil => AZERO
+ case r :: Nil => fuse(bs1, r)
+ case rs => AALTS(bs1, rs)
+ }
+ case ASTAR(bs1, r1) => ASTAR(bs1, bsimp_full(r1))
+ case r => r
+}
+
+def bders_simp_full(s: List[Char], r: ARexp) : ARexp = s match {
+ case Nil => r
+ case c::s => bders_simp_full(s, bsimp_full(bder(c, r)))
+}
+
+def blex_simp_full(r: ARexp, s: List[Char]) : Bits = s match {
+ case Nil => if (bnullable(r)) bmkeps(r)
+ else throw new Exception("Not matched")
+ case c::cs => blex_simp_full(bsimp_full(bder(c, r)), cs)
+}
+
+
+def blexing_simp_full(r: Rexp, s: String) : Val =
+ decode(r, blex_simp_full(internalise(r), s.toList))
+
+
+def btokenise_simp_full(r: Rexp, s: String) = env(blexing_simp_full(r, s)).map(esc2)
+
+
// Testing
//============
@@ -424,9 +475,10 @@
def timeR[T](code: => T) = {
val start = System.nanoTime()
+ for (i <- 1 to 10) code
val result = code
val end = System.nanoTime()
- (result, (end - start)/1.0e9)
+ (result, (end - start))
}
//size: of a Aregx for testing purposes
@@ -520,28 +572,48 @@
println(btokenise_simp(WHILE_REGS, fib_prog))
println("equal? " + (tokenise_simp(WHILE_REGS, fib_prog) == btokenise_simp(WHILE_REGS, fib_prog)))
-for (i <- 1 to 10) {
+for (i <- 1 to 20) {
print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i)))
- println(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
+ print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
+ println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
}
println("Original " + size(WHILE_REGS))
-println("Size Bit " + asize(bders_simp((fib_prog * 10).toList, internalise(WHILE_REGS))))
-println("Size Old " + size(ders_simp((fib_prog * 10).toList, WHILE_REGS)))
+println("Size Bit " + asize(bders_simp((fib_prog * 10).toList, internalise(WHILE_REGS))))
+println("Size Bitf " + asize(bders_simp_full((fib_prog * 10).toList, internalise(WHILE_REGS))))
+println("Size Old " + size(ders_simp((fib_prog * 10).toList, WHILE_REGS)))
+//System.exit(0)
+
println("Internal sizes test OK or strange")
+def perc(p1: Double, p2: Double) : String =
+ f"${(((p1 - p2) / p2) * 100.0) }%5.0f" + "%"
+
def ders_test(n: Int, s: List[Char], r: Rexp, a: ARexp) : (Rexp, ARexp) = s match {
case Nil => (r, a)
case c::s => {
- val (rd, tr) = timeR(simp(der(c, r))._1)
- val (ad, ta) = timeR(bsimp(bder(c, a)))
+ // derivative
+ val (rd1, tr1) = timeR(der(c, r))
+ val (ad1, ta1) = timeR(bder(c, a))
+ val trs1 = f"${tr1}%.5f"
+ val tas1 = f"${ta1}%.5f"
+ if (tr1 < ta1) println(s"Time strange der (step) ${n} ${perc(ta1, tr1)} sizes der ${size(rd1)} ${asize(ad1)}")
+ //simplification
+ val (rd, tr) = timeR(simp(rd1)._1)
+ val (ad, ta) = timeR(bsimp(ad1))
val trs = f"${tr}%.5f"
val tas = f"${ta}%.5f"
- if (tr < ta) println("Time strange (step) " + n + " " + trs + " " + tas + " sizes " + size(rd) + " " + asize(ad))
- if (size(rd) < asize(ad)) println("Size strange (step) " + n + " " + size(rd) + " " + asize(ad))
- //if (size(rd) >= asize(ad)) println(" Size OK " + size(rd) + " " + asize(ad))
+ //full simplification
+ val (adf, taf) = timeR(bsimp_full(ad1))
+ if (tr < ta) println(s"Time strange simp (step) ${n} ${perc(ta, tr)} sizes simp ${size(rd)} ${asize(ad)}")
+ if (n == 1749 || n == 1734) {
+ println{s"Aregex before bder (size: ${asize(a)})\n ${string(erase(a))}"}
+ println{s"Aregex after bder (size: ${asize(ad1)})\n ${string(erase(ad1))}"}
+ println{s"Aregex after bsimp (size: ${asize(ad)})\n ${string(erase(ad))}"}
+ println{s"Aregex after bsimp_full (size: ${asize(adf)})\n ${string(erase(adf))}"}
+ }
ders_test(n + 1, s, rd, ad)
}
}