exps/both.scala
changeset 330 89e6605c4ca4
parent 317 db0ff630bbb7
--- a/exps/both.scala	Tue Jul 23 21:21:49 2019 +0100
+++ b/exps/both.scala	Mon Jul 29 09:37:20 2019 +0100
@@ -54,6 +54,27 @@
 case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
 case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
 
+// "faster" equality
+def equ(a1: ARexp, a2: ARexp) : Boolean = (a1, a2) match {
+  case (AZERO, AZERO) => true
+  case (AONE(_), AONE(_)) => true
+  case (APRED(_, _, s1), APRED(_, _, s2)) => s1 == s2
+  case (AALTS(_, Nil), AALTS(_, Nil)) => true
+  case (AALTS(_, r1::rs1), AALTS(_, r2::rs2)) => 
+    equ(r1, r2) && equ(AALTS(Nil, rs1), AALTS(Nil, rs2)) 
+  case (ASEQ(_, r1, r2), ASEQ(_, r3, r4)) => equ(r1, r3) && equ(r2, r4)
+  case (ASTAR(_, r1), ASTAR(_, r2)) => equ(r1, r2)
+  case _ => false
+}
+
+def distinctEq(xs: List[ARexp], acc: List[ARexp] = Nil): List[ARexp] = xs match {
+  case Nil => Nil
+  case (x::xs) => {
+    if (acc.exists(equ(_, x))) distinctEq(xs, acc)  
+    else x::distinctEq(xs, x::acc)
+  }
+} 
+
 // abbreviations
 def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
 
@@ -449,7 +470,7 @@
       case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
       case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
   }
-  case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match {
+  case AALTS(bs1, rs) => distinctEq(flats(rs.map(bsimp))) match {
     case Nil => AZERO
     case r :: Nil => fuse(bs1, r)
     case rs => AALTS(bs1, rs)  
@@ -479,7 +500,7 @@
 
 // Quick example
 
-val r : Rexp = ZERO | "a" 
+val r : Rexp = (ZERO | "a") | ZERO 
 
 lexing(r, "a")
 
@@ -775,11 +796,12 @@
 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 20) {
+for (i <- 1 to 40) {
   print("Old: " + time(tokenise_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(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
   //println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i)))
+  println("")
 }