exps/both.scala
changeset 330 89e6605c4ca4
parent 317 db0ff630bbb7
equal deleted inserted replaced
329:a730a5a0bab9 330:89e6605c4ca4
    51 case class AONE(bs: Bits) extends ARexp
    51 case class AONE(bs: Bits) extends ARexp
    52 case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp
    52 case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp
    53 case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
    53 case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
    54 case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
    54 case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
    55 case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
    55 case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
       
    56 
       
    57 // "faster" equality
       
    58 def equ(a1: ARexp, a2: ARexp) : Boolean = (a1, a2) match {
       
    59   case (AZERO, AZERO) => true
       
    60   case (AONE(_), AONE(_)) => true
       
    61   case (APRED(_, _, s1), APRED(_, _, s2)) => s1 == s2
       
    62   case (AALTS(_, Nil), AALTS(_, Nil)) => true
       
    63   case (AALTS(_, r1::rs1), AALTS(_, r2::rs2)) => 
       
    64     equ(r1, r2) && equ(AALTS(Nil, rs1), AALTS(Nil, rs2)) 
       
    65   case (ASEQ(_, r1, r2), ASEQ(_, r3, r4)) => equ(r1, r3) && equ(r2, r4)
       
    66   case (ASTAR(_, r1), ASTAR(_, r2)) => equ(r1, r2)
       
    67   case _ => false
       
    68 }
       
    69 
       
    70 def distinctEq(xs: List[ARexp], acc: List[ARexp] = Nil): List[ARexp] = xs match {
       
    71   case Nil => Nil
       
    72   case (x::xs) => {
       
    73     if (acc.exists(equ(_, x))) distinctEq(xs, acc)  
       
    74     else x::distinctEq(xs, x::acc)
       
    75   }
       
    76 } 
    56 
    77 
    57 // abbreviations
    78 // abbreviations
    58 def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
    79 def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
    59 
    80 
    60 // values
    81 // values
   447       case (AZERO, _) => AZERO
   468       case (AZERO, _) => AZERO
   448       case (_, AZERO) => AZERO
   469       case (_, AZERO) => AZERO
   449       case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
   470       case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
   450       case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
   471       case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
   451   }
   472   }
   452   case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match {
   473   case AALTS(bs1, rs) => distinctEq(flats(rs.map(bsimp))) match {
   453     case Nil => AZERO
   474     case Nil => AZERO
   454     case r :: Nil => fuse(bs1, r)
   475     case r :: Nil => fuse(bs1, r)
   455     case rs => AALTS(bs1, rs)  
   476     case rs => AALTS(bs1, rs)  
   456   }
   477   }
   457   //case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1))
   478   //case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1))
   477 def btokenise_simp(r: Rexp, s: String) = 
   498 def btokenise_simp(r: Rexp, s: String) = 
   478   env(blexing_simp(r, s)).map(esc2)
   499   env(blexing_simp(r, s)).map(esc2)
   479 
   500 
   480 // Quick example
   501 // Quick example
   481 
   502 
   482 val r : Rexp = ZERO | "a" 
   503 val r : Rexp = (ZERO | "a") | ZERO 
   483 
   504 
   484 lexing(r, "a")
   505 lexing(r, "a")
   485 
   506 
   486 val a0 = internalise(r)
   507 val a0 = internalise(r)
   487 val a1 = bder('a', a0)
   508 val a1 = bder('a', a0)
   773 println("fib prog tests :")
   794 println("fib prog tests :")
   774 println(tokenise_simp(WHILE_REGS, fib_prog))
   795 println(tokenise_simp(WHILE_REGS, fib_prog))
   775 println(btokenise_simp(WHILE_REGS, fib_prog))
   796 println(btokenise_simp(WHILE_REGS, fib_prog))
   776 println("equal? " + (tokenise_simp(WHILE_REGS, fib_prog) == btokenise_simp(WHILE_REGS, fib_prog)))
   797 println("equal? " + (tokenise_simp(WHILE_REGS, fib_prog) == btokenise_simp(WHILE_REGS, fib_prog)))
   777 
   798 
   778 for (i <- 1 to 20) {
   799 for (i <- 1 to 40) {
   779   print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i)))
   800   print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i)))
   780   print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
   801   print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i)))
   781   println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
   802   //println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i)))
   782   //println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i)))
   803   //println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i)))
       
   804   println("")
   783 }
   805 }
   784 
   806 
   785 
   807 
   786 println("Original " + size(WHILE_REGS))
   808 println("Original " + size(WHILE_REGS))
   787 println("Size Bit  " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))
   809 println("Size Bit  " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS))))