RegexExplosionPlot/injToSimp.sc
author Chengsong
Mon, 24 Jul 2023 11:09:48 +0100
changeset 666 6da4516ea87d
child 667 660cf698eb26
permissions -rw-r--r--
more changes to figures & benchmarking
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
666
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     1
// A simple lexer inspired by work of Sulzmann & Lu
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     2
//==================================================
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     3
//
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     4
// Call the test cases with 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     5
//
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     6
//   amm lexer.sc small
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     7
//   amm lexer.sc fib
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     8
//   amm lexer.sc loops
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
     9
//   amm lexer.sc email
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    10
//
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    11
//   amm lexer.sc all
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    12
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    13
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    14
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    15
// regular expressions including records
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    16
abstract class Rexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    17
case object ZERO extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    18
case object ONE extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    19
case object ANYCHAR extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    20
case class CHAR(c: Char) extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    21
case class ALTS(r1: Rexp, r2: Rexp) extends Rexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    22
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    23
case class STAR(r: Rexp) extends Rexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    24
case class RECD(x: String, r: Rexp) extends Rexp  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    25
case class NTIMES(n: Int, r: Rexp) extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    26
case class OPTIONAL(r: Rexp) extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    27
case class NOT(r: Rexp) extends Rexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    28
                // records for extracting strings or tokens
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    29
  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    30
// values  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    31
abstract class Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    32
case object Failure extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    33
case object Empty extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    34
case class Chr(c: Char) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    35
case class Sequ(v1: Val, v2: Val) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    36
case class Left(v: Val) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    37
case class Right(v: Val) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    38
case class Stars(vs: List[Val]) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    39
case class Rec(x: String, v: Val) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    40
case class Ntime(vs: List[Val]) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    41
case class Optionall(v: Val) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    42
case class Nots(s: String) extends Val
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    43
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    44
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    45
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    46
abstract class Bit
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    47
case object Z extends Bit
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    48
case object S extends Bit
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    49
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    50
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    51
type Bits = List[Bit]
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    52
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    53
abstract class ARexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    54
case object AZERO extends ARexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    55
case class AONE(bs: Bits) extends ARexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    56
case class ACHAR(bs: Bits, c: Char) extends ARexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    57
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    58
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    59
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    60
case class ANOT(bs: Bits, r: ARexp) extends ARexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    61
case class AANYCHAR(bs: Bits) extends ARexp
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    62
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    63
import scala.util.Try
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    64
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    65
trait Generator[+T] {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    66
    self => // an alias for "this"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    67
    def generate(): T
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    68
  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    69
    def gen(n: Int) : List[T] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    70
      if (n == 0) Nil else self.generate() :: gen(n - 1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    71
    
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    72
    def map[S](f: T => S): Generator[S] = new Generator[S] {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    73
      def generate = f(self.generate())  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    74
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    75
    def flatMap[S](f: T => Generator[S]): Generator[S] = new Generator[S] {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    76
      def generate = f(self.generate()).generate()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    77
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    78
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    79
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    80
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    81
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    82
  // tests a property according to a given random generator
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    83
  def test[T](r: Generator[T], amount: Int = 100)(pred: T => Boolean) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    84
    for (_ <- 0 until amount) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    85
      val value = r.generate()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    86
      assert(pred(value), s"Test failed for: $value")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    87
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    88
    println(s"Test passed $amount times")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    89
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    90
  def test2[T, S](r: Generator[T], s: Generator[S], amount: Int = 100)(pred: (T, S) => Boolean) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    91
    for (_ <- 0 until amount) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    92
      val valueR = r.generate()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    93
      val valueS = s.generate()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    94
      assert(pred(valueR, valueS), s"Test failed for: $valueR, $valueS")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    95
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    96
    println(s"Test passed $amount times")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    97
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    98
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
    99
// random integers
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   100
val integers = new Generator[Int] {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   101
  val rand = new java.util.Random
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   102
  def generate() = rand.nextInt()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   103
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   104
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   105
// random booleans
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   106
val booleans = integers.map(_ > 0)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   107
  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   108
// random integers in the range lo and high  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   109
def range(lo: Int, hi: Int): Generator[Int] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   110
  for (x <- integers) yield (lo + x.abs % (hi - lo)).abs
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   111
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   112
// random characters
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   113
def chars_range(lo: Char, hi: Char) = range(lo, hi).map(_.toChar)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   114
val chars = chars_range('a', 'z')
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   115
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   116
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   117
def oneOf[T](xs: T*): Generator[T] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   118
  for (idx <- range(0, xs.length)) yield xs(idx)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   119
  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   120
def single[T](x: T) = new Generator[T] {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   121
  def generate() = x
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   122
}   
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   123
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   124
def pairs[T, U](t: Generator[T], u: Generator[U]): Generator[(T, U)] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   125
  for (x <- t; y <- u) yield (x, y)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   126
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   127
// lists
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   128
def emptyLists = single(Nil) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   129
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   130
def nonEmptyLists : Generator[List[Int]] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   131
  for (head <- integers; tail <- lists) yield head :: tail
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   132
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   133
def lists: Generator[List[Int]] = for {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   134
  kind <- booleans
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   135
  list <- if (kind) emptyLists else nonEmptyLists
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   136
} yield list
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   137
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   138
def char_list(len: Int): Generator[List[Char]] = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   139
  if(len <= 0) single(Nil)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   140
  else{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   141
    for { 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   142
      c <- chars_range('a', 'c')
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   143
      tail <- char_list(len - 1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   144
    } yield c :: tail
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   145
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   146
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   147
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   148
def strings(len: Int): Generator[String] = for(cs <- char_list(len)) yield cs.toString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   149
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   150
def sampleString(r: Rexp) : List[String] = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   151
  case STAR(r) => stringsFromRexp(r).flatMap(s => List("", s, s ++ s))//only generate 0, 1, 2 reptitions
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   152
  case SEQ(r1, r2) => stringsFromRexp(r1).flatMap(s1 => stringsFromRexp(r2).map(s2 => s1 ++ s2) )
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   153
  case ALTS(r1, r2) => throw new Error(s" Rexp ${r} not expected: all alternatives are supposed to have been opened up")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   154
  case ONE => "" :: Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   155
  case ZERO => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   156
  case CHAR(c) => c.toString :: Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   157
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   158
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   159
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   160
def stringsFromRexp(r: Rexp) : List[String] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   161
  breakIntoTerms(r).flatMap(r => sampleString(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   162
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   163
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   164
// (simple) binary trees
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   165
trait Tree[T]
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   166
case class Inner[T](left: Tree[T], right: Tree[T]) extends Tree[T]
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   167
case class Leaf[T](x: T) extends Tree[T]
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   168
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   169
def leafs[T](t: Generator[T]): Generator[Leaf[T]] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   170
  for (x <- t) yield Leaf(x)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   171
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   172
def inners[T](t: Generator[T]): Generator[Inner[T]] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   173
  for (l <- trees(t); r <- trees(t)) yield Inner(l, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   174
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   175
def trees[T](t: Generator[T]): Generator[Tree[T]] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   176
  for (kind <- range(0, 2);  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   177
       tree <- if (kind == 0) leafs(t) else inners(t)) yield tree
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   178
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   179
// regular expressions
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   180
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   181
// generates random leaf-regexes; prefers CHAR-regexes
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   182
def leaf_rexp() : Generator[Rexp] =
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   183
  for (kind <- range(0, 5);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   184
       c <- chars_range('a', 'd')) yield
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   185
    kind match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   186
      case 0 => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   187
      case 1 => ONE
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   188
      case _ => CHAR(c) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   189
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   190
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   191
// generates random inner regexes with maximum depth d
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   192
def inner_rexp(d: Int) : Generator[Rexp] =
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   193
  for (kind <- range(0, 3);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   194
       l <- rexp(d); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   195
       r <- rexp(d))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   196
  yield kind match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   197
    case 0 => ALTS(l, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   198
    case 1 => SEQ(l, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   199
    case 2 => STAR(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   200
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   201
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   202
// generates random regexes with maximum depth d;
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   203
// prefers inner regexes in 2/3 of the cases
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   204
def rexp(d: Int = 100): Generator[Rexp] = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   205
  for (kind <- range(0, 3);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   206
       r <- if (d <= 0 || kind == 0) leaf_rexp() else inner_rexp(d - 1)) yield r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   207
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   208
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   209
// some test functions for rexps
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   210
def height(r: Rexp) : Int = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   211
  case ZERO => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   212
  case ONE => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   213
  case CHAR(_) => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   214
  case ALTS(r1, r2) => 1 + List(height(r1), height(r2)).max
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   215
  case SEQ(r1, r2) =>  1 + List(height(r1), height(r2)).max
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   216
  case STAR(r) => 1 + height(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   217
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   218
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   219
// def size(r: Rexp) : Int = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   220
//   case ZERO => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   221
//   case ONE => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   222
//   case CHAR(_) => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   223
//   case ALTS(r1, r2) => 1 + size(r1) + size(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   224
//   case SEQ(r1, r2) =>  1 + size(r1) + size(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   225
//   case STAR(r) => 1 + size(r) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   226
// }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   227
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   228
// randomly subtracts 1 or 2 from the STAR case
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   229
def size_faulty(r: Rexp) : Int = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   230
  case ZERO => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   231
  case ONE => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   232
  case CHAR(_) => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   233
  case ALTS(r1, r2) => 1 + size_faulty(r1) + size_faulty(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   234
  case SEQ(r1, r2) =>  1 + size_faulty(r1) + size_faulty(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   235
  case STAR(r) => 1 + size_faulty(r) - range(0, 2).generate
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   236
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   237
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   238
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   239
   
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   240
// some convenience for typing in regular expressions
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   241
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   242
def charlist2rexp(s : List[Char]): Rexp = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   243
  case Nil => ONE
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   244
  case c::Nil => CHAR(c)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   245
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   246
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   247
implicit def string2rexp(s : String) : Rexp = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   248
  charlist2rexp(s.toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   249
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   250
implicit def RexpOps(r: Rexp) = new {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   251
  def | (s: Rexp) = ALTS(r, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   252
  def % = STAR(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   253
  def ~ (s: Rexp) = SEQ(r, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   254
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   255
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   256
implicit def stringOps(s: String) = new {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   257
  def | (r: Rexp) = ALTS(s, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   258
  def | (r: String) = ALTS(s, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   259
  def % = STAR(s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   260
  def ~ (r: Rexp) = SEQ(s, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   261
  def ~ (r: String) = SEQ(s, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   262
  def $ (r: Rexp) = RECD(s, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   263
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   264
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   265
def nullable(r: Rexp) : Boolean = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   266
  case ZERO => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   267
  case ONE => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   268
  case CHAR(_) => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   269
  case ANYCHAR => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   270
  case ALTS(r1, r2) => nullable(r1) || nullable(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   271
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   272
  case STAR(_) => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   273
  case RECD(_, r1) => nullable(r1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   274
  case NTIMES(n, r) => if (n == 0) true else nullable(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   275
  case OPTIONAL(r) => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   276
  case NOT(r) => !nullable(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   277
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   278
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   279
def der(c: Char, r: Rexp) : Rexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   280
  case ZERO => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   281
  case ONE => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   282
  case CHAR(d) => if (c == d) ONE else ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   283
  case ANYCHAR => ONE 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   284
  case ALTS(r1, r2) => ALTS(der(c, r1), der(c, r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   285
  case SEQ(r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   286
    if (nullable(r1)) ALTS(SEQ(der(c, r1), r2), der(c, r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   287
    else SEQ(der(c, r1), r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   288
  case STAR(r) => SEQ(der(c, r), STAR(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   289
  case RECD(_, r1) => der(c, r1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   290
  case NTIMES(n, r) => if(n > 0) SEQ(der(c, r), NTIMES(n - 1, r)) else ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   291
  case OPTIONAL(r) => der(c, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   292
  case NOT(r) =>  NOT(der(c, r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   293
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   294
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   295
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   296
// extracts a string from a value
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   297
def flatten(v: Val) : String = v match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   298
  case Empty => ""
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   299
  case Chr(c) => c.toString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   300
  case Left(v) => flatten(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   301
  case Right(v) => flatten(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   302
  case Sequ(v1, v2) => flatten(v1) ++ flatten(v2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   303
  case Stars(vs) => vs.map(flatten).mkString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   304
  case Ntime(vs) => vs.map(flatten).mkString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   305
  case Optionall(v) => flatten(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   306
  case Rec(_, v) => flatten(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   307
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   308
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   309
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   310
// extracts an environment from a value;
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   311
// used for tokenising a string
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   312
def env(v: Val) : List[(String, String)] = v match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   313
  case Empty => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   314
  case Chr(c) => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   315
  case Left(v) => env(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   316
  case Right(v) => env(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   317
  case Sequ(v1, v2) => env(v1) ::: env(v2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   318
  case Stars(vs) => vs.flatMap(env)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   319
  case Ntime(vs) => vs.flatMap(env)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   320
  case Rec(x, v) => (x, flatten(v))::env(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   321
  case Optionall(v) => env(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   322
  case Nots(s) => ("Negative", s) :: Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   323
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   324
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   325
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   326
// The injection and mkeps part of the lexer
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   327
//===========================================
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   328
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   329
def mkeps(r: Rexp) : Val = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   330
  case ONE => Empty
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   331
  case ALTS(r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   332
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   333
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   334
  case STAR(r) => Stars(Nil)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   335
  case RECD(x, r) => Rec(x, mkeps(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   336
  case NTIMES(n, r) => Ntime(List.fill(n)(mkeps(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   337
  case OPTIONAL(r) => Optionall(Empty)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   338
  case NOT(rInner) => if(nullable(rInner)) throw new Exception("error")  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   339
                         else Nots("")//Nots(s.reverse.toString)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   340
//   case NOT(ZERO) => Empty
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   341
//   case NOT(CHAR(c)) => Empty
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   342
//   case NOT(SEQ(r1, r2)) => Sequ(mkeps(NOT(r1)), mkeps(NOT(r2)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   343
//   case NOT(ALTS(r1, r2)) => if(!nullable(r1)) Left(mkeps(NOT(r1))) else Right(mkeps(NOT(r2)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   344
//   case NOT(STAR(r)) => Stars(Nil) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   345
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   346
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   347
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   348
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   349
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   350
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   351
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   352
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   353
  case (ALTS(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   354
  case (ALTS(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   355
  case (CHAR(d), Empty) => Chr(c) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   356
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   357
  case (NTIMES(n, r), Sequ(v1, Ntime(vs))) => Ntime(inj(r, c, v1)::vs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   358
  case (OPTIONAL(r), v) => Optionall(inj(r, c, v))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   359
  case (NOT(r), Nots(s)) => Nots(c.toString ++ s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   360
  case (ANYCHAR, Empty) => Chr(c)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   361
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   362
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   363
    def size(r: Rexp) : Int = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   364
      case ZERO => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   365
      case ONE => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   366
      case CHAR(_) => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   367
      case ANYCHAR => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   368
      case NOT(r0) => 1 + size(r0)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   369
      case SEQ(r1, r2) => 1 + size(r1) + size(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   370
      case ALTS(r1, r2) => 1 + List(r1, r2).map(size).sum
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   371
      case STAR(r) => 1 + size(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   372
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   373
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   374
//almost as if a zeroable in called on each subexpression, and if so, turn that sub-expression into 0
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   375
def light_simp(r: Rexp): Rexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   376
  case ALTS(r1, r2) => (light_simp(r1), light_simp(r2)) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   377
    case (ZERO, ZERO) => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   378
    case (r1p, r2p) => ALTS(r1p, r2p)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   379
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   380
  case SEQ(r1, r2) => (light_simp(r1), light_simp(r2)) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   381
    case (ZERO, _) => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   382
    case (_, ZERO) => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   383
    case (r1p, r2p) => SEQ(r1p, r2p)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   384
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   385
  case STAR(r) => STAR(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   386
  case CHAR(d) => CHAR(d)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   387
  case ONE => ONE
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   388
  case ZERO => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   389
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   390
    
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   391
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   392
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   393
def simp_lexer(r: Rexp, s: List[Char]): Val = s match{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   394
  case Nil => {//println("mkeps!"); println(r); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   395
    val v = mkeps(r); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   396
    //println(v); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   397
    v}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   398
  case c::cs => { 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   399
    val derc1 = der(c, r);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   400
    val derc = light_simp(derc1);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   401
    println("derivative size: "+ size(derc));
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   402
    val v = simp_lexer(derc, cs); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   403
    //println("Injection!");println("r: "+r); println("c: "+c); println("v:"+v); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   404
    inj(r, c, v ) }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   405
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   406
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   407
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   408
def print_lexer(r: Rexp, s: List[Char]): Val = s match{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   409
  case Nil => {//println("mkeps!"); println(r); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   410
    val v = mkeps(r); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   411
    //println(v); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   412
    v}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   413
  case c::cs => { 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   414
    val derc = der(c, r);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   415
    //val derc = light_simp(derc1);
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   416
    println("derivative size: "+ size(derc));
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   417
    val v = print_lexer(derc, cs); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   418
    //println("Injection!");println("r: "+r); println("c: "+c); println("v:"+v); 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   419
    inj(r, c, v ) }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   420
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   421
val r = STAR("a"|"aa") ~ ("c")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   422
println("unsimplified lexer's intermediate derivative sizes:")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   423
val v = print_lexer(r, "aac".toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   424
println("simplified lexer's intermediate derivative sizes:")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   425
val v2 = simp_lexer(r, "aac".toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   426
//println(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   427
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   428
// some "rectification" functions for simplification
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   429
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   430
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   431
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   432
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   433
// The Lexing Rules for the WHILE Language
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   434
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   435
  // bnullable function: tests whether the aregular 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   436
  // expression can recognise the empty string
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   437
def bnullable (r: ARexp) : Boolean = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   438
    case AZERO => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   439
    case AONE(_) => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   440
    case ACHAR(_,_) => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   441
    case AALTS(_, rs) => rs.exists(bnullable)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   442
    case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   443
    case ASTAR(_, _) => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   444
    case ANOT(_, rn) => !bnullable(rn)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   445
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   446
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   447
def mkepsBC(r: ARexp) : Bits = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   448
    case AONE(bs) => bs
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   449
    case AALTS(bs, rs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   450
      val n = rs.indexWhere(bnullable)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   451
      bs ++ mkepsBC(rs(n))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   452
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   453
    case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   454
    case ASTAR(bs, r) => bs ++ List(Z)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   455
    case ANOT(bs, rn) => bs
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   456
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   457
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   458
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   459
def bder(c: Char, r: ARexp) : ARexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   460
    case AZERO => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   461
    case AONE(_) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   462
    case ACHAR(bs, f) => if (c == f) AONE(bs) else AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   463
    case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   464
    case ASEQ(bs, r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   465
      if (bnullable(r1)) AALTS(bs, ASEQ(Nil, bder(c, r1), r2) :: fuse(mkepsBC(r1), bder(c, r2)) :: Nil )
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   466
      else ASEQ(bs, bder(c, r1), r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   467
    case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   468
    case ANOT(bs, rn) => ANOT(bs, bder(c, rn))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   469
    case AANYCHAR(bs) => AONE(bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   470
  } 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   471
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   472
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   473
    case AZERO => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   474
    case AONE(cs) => AONE(bs ++ cs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   475
    case ACHAR(cs, f) => ACHAR(bs ++ cs, f)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   476
    case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   477
    case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   478
    case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   479
    case ANOT(cs, r) => ANOT(bs ++ cs, r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   480
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   481
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   482
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   483
def internalise(r: Rexp) : ARexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   484
    case ZERO => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   485
    case ONE => AONE(Nil)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   486
    case CHAR(c) => ACHAR(Nil, c)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   487
    //case PRED(f) => APRED(Nil, f)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   488
    case ALTS(r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   489
      AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   490
    // case ALTS(r1::rs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   491
    //   val AALTS(Nil, rs2) = internalise(ALTS(rs))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   492
    //   AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   493
    // }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   494
    case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   495
    case STAR(r) => ASTAR(Nil, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   496
    case RECD(x, r) => internalise(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   497
    case NOT(r) => ANOT(Nil, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   498
    case ANYCHAR => AANYCHAR(Nil)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   499
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   500
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   501
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   502
def bsimp(r: ARexp): ARexp = 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   503
  {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   504
    r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   505
      case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   506
          case (AZERO, _) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   507
          case (_, AZERO) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   508
          case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   509
          case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   510
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   511
      case AALTS(bs1, rs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   512
            val rs_simp = rs.map(bsimp(_))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   513
            val flat_res = flats(rs_simp)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   514
            val dist_res = distinctBy(flat_res, erase)//strongDB(flat_res)//distinctBy(flat_res, erase)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   515
            dist_res match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   516
              case Nil => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   517
              case s :: Nil => fuse(bs1, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   518
              case rs => AALTS(bs1, rs)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   519
            }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   520
          
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   521
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   522
      case r => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   523
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   524
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   525
def strongBsimp(r: ARexp): ARexp =
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   526
{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   527
  r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   528
    case ASEQ(bs1, r1, r2) => (strongBsimp(r1), strongBsimp(r2)) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   529
        case (AZERO, _) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   530
        case (_, AZERO) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   531
        case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   532
        case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   533
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   534
    case AALTS(bs1, rs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   535
          val rs_simp = rs.map(strongBsimp(_))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   536
          val flat_res = flats(rs_simp)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   537
          val dist_res = distinctBy4(flat_res)//distinctBy(flat_res, erase)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   538
          dist_res match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   539
            case Nil => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   540
            case s :: Nil => fuse(bs1, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   541
            case rs => AALTS(bs1, rs)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   542
          }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   543
        
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   544
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   545
    case r => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   546
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   547
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   548
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   549
def strongBsimp5(r: ARexp): ARexp =
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   550
{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   551
  // println("was this called?")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   552
  r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   553
    case ASEQ(bs1, r1, r2) => (strongBsimp5(r1), strongBsimp5(r2)) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   554
        case (AZERO, _) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   555
        case (_, AZERO) => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   556
        case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   557
        case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   558
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   559
    case AALTS(bs1, rs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   560
        // println("alts case")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   561
          val rs_simp = rs.map(strongBsimp5(_))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   562
          val flat_res = flats(rs_simp)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   563
          var dist_res = distinctBy5(flat_res)//distinctBy(flat_res, erase)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   564
          var dist2_res = distinctBy5(dist_res)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   565
          while(dist_res != dist2_res){
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   566
            dist_res = dist2_res
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   567
            dist2_res = distinctBy5(dist_res)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   568
          }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   569
          (dist2_res) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   570
            case Nil => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   571
            case s :: Nil => fuse(bs1, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   572
            case rs => AALTS(bs1, rs)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   573
          }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   574
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   575
    case r => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   576
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   577
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   578
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   579
def bders (s: List[Char], r: ARexp) : ARexp = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   580
  case Nil => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   581
  case c::s => bders(s, bder(c, r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   582
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   583
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   584
def flats(rs: List[ARexp]): List[ARexp] = rs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   585
    case Nil => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   586
    case AZERO :: rs1 => flats(rs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   587
    case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   588
    case r1 :: rs2 => r1 :: flats(rs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   589
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   590
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   591
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   592
  case Nil => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   593
  case (x::xs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   594
    val res = f(x)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   595
    if (acc.contains(res)) distinctBy(xs, f, acc)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   596
    else x::distinctBy(xs, f, res::acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   597
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   598
} 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   599
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   600
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   601
def pruneRexp(r: ARexp, allowableTerms: List[Rexp]) : ARexp = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   602
  r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   603
    case ASEQ(bs, r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   604
      val termsTruncated = allowableTerms.collect(rt => rt match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   605
        case SEQ(r1p, r2p) if(r2p == erase(r2)) => r1p//if(r2p == erase(r2)) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   606
      })
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   607
      val pruned : ARexp = pruneRexp(r1, termsTruncated)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   608
      pruned match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   609
        case AZERO => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   610
        case AONE(bs1) => fuse(bs ++ bs1, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   611
        case pruned1 => ASEQ(bs, pruned1, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   612
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   613
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   614
    case AALTS(bs, rs) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   615
      //allowableTerms.foreach(a => println(shortRexpOutput(a)))        
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   616
      val rsp = rs.map(r => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   617
                    pruneRexp(r, allowableTerms)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   618
                  )
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   619
                  .filter(r => r != AZERO)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   620
      rsp match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   621
        case Nil => AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   622
        case r1::Nil => fuse(bs, r1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   623
        case rs1 => AALTS(bs, rs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   624
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   625
    case r => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   626
      if(allowableTerms.contains(erase(r))) r else AZERO //assert(r != AZERO)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   627
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   628
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   629
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   630
def oneSimp(r: Rexp) : Rexp = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   631
  case SEQ(ONE, r) => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   632
  case SEQ(r1, r2) => SEQ(oneSimp(r1), r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   633
  case r => r//assert r != 0 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   634
    
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   635
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   636
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   637
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   638
def distinctBy4(xs: List[ARexp], acc: List[Rexp] = Nil) : List[ARexp] = xs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   639
  case Nil => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   640
  case x :: xs =>
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   641
    //assert(acc.distinct == acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   642
    val erased = erase(x)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   643
    if(acc.contains(erased))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   644
      distinctBy4(xs, acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   645
    else{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   646
      val addToAcc =  breakIntoTerms(erased).filter(r => !acc.contains(oneSimp(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   647
      //val xp = pruneRexp(x, addToAcc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   648
      pruneRexp(x, addToAcc) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   649
        case AZERO => distinctBy4(xs, addToAcc.map(oneSimp(_)) ::: acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   650
        case xPrime => xPrime :: distinctBy4(xs, addToAcc.map(oneSimp(_)) ::: acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   651
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   652
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   653
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   654
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   655
// fun pAKC_aux :: "arexp list ⇒ arexp ⇒ rexp ⇒ arexp"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   656
//   where
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   657
// "pAKC_aux rsa r ctx = (if (L (SEQ (erase r) ( ctx) )) ⊆ (L (erase (AALTs [] rsa))) then AZERO else
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   658
//                           case r of (ASEQ bs r1 r2) ⇒ 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   659
//                             bsimp_ASEQ bs (pAKC_aux rsa r1 (SEQ  (erase r2) ( ctx) )) r2   |
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   660
//                                     (AALTs bs rs) ⇒ 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   661
//                             bsimp_AALTs bs (flts (map (λr. pAKC_aux rsa r ( ctx) ) rs) )    |
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   662
//                                     r             ⇒ r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   663
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   664
// def canonicalSeq(rs: List[Rexp], acc: Rexp) = rs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   665
//   case r::Nil => SEQ(r, acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   666
//   case Nil => acc
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   667
//   case r1::r2::Nil => SEQ(SEQ(r1, r2), acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   668
// }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   669
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   670
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   671
//the "fake" Language interpretation: just concatenates!
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   672
def L(acc: Rexp, rs: List[Rexp]) : Rexp = rs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   673
  case Nil => acc
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   674
  case r :: rs1 => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   675
    // if(acc == ONE) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   676
    //   L(r, rs1) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   677
    // else
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   678
      L(SEQ(acc, r), rs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   679
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   680
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   681
def rprint(r: Rexp) : Unit = println(shortRexpOutput(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   682
def rsprint(rs: List[Rexp]) = rs.foreach(r => println(shortRexpOutput(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   683
def aprint(a: ARexp) = println(shortRexpOutput(erase(a)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   684
def asprint(as: List[ARexp]) = as.foreach(a => println(shortRexpOutput(erase(a))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   685
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   686
def pAKC(acc: List[Rexp], r: ARexp, ctx: List[Rexp]) : ARexp = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   687
  // println("pakc")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   688
  // println(shortRexpOutput(erase(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   689
  // println("acc")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   690
  // rsprint(acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   691
  // println("ctx---------")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   692
  // rsprint(ctx)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   693
  // println("ctx---------end")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   694
  // rsprint(breakIntoTerms(L(erase(r), ctx)).map(oneSimp))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   695
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   696
  if (breakIntoTerms(L(erase(r), ctx)).map(oneSimp).forall(acc.contains)) {//acc.flatMap(breakIntoTerms
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   697
    AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   698
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   699
  else{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   700
    r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   701
      case ASEQ(bs, r1, r2) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   702
      (pAKC(acc, r1, erase(r2) :: ctx)) match{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   703
        case AZERO => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   704
          AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   705
        case AONE(bs1) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   706
          fuse(bs1, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   707
        case r1p => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   708
          ASEQ(bs, r1p, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   709
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   710
      case AALTS(bs, rs0) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   711
        // println("before pruning")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   712
        // println(s"ctx is ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   713
        // ctx.foreach(r => println(shortRexpOutput(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   714
        // println(s"rs0 is ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   715
        // rs0.foreach(r => println(shortRexpOutput(erase(r))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   716
        // println(s"acc is ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   717
        // acc.foreach(r => println(shortRexpOutput(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   718
        rs0.map(r => pAKC(acc, r, ctx)).filter(_ != AZERO) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   719
          case Nil => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   720
            // println("after pruning Nil")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   721
            AZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   722
          case r :: Nil => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   723
            // println("after pruning singleton")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   724
            // println(shortRexpOutput(erase(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   725
            r 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   726
          case rs0p => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   727
          // println("after pruning non-singleton")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   728
            AALTS(bs, rs0p)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   729
        }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   730
      case r => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   731
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   732
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   733
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   734
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   735
def distinctBy5(xs: List[ARexp], acc: List[Rexp] = Nil) : List[ARexp] = xs match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   736
  case Nil => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   737
    Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   738
  case x :: xs => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   739
    val erased = erase(x)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   740
    if(acc.contains(erased)){
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   741
      distinctBy5(xs, acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   742
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   743
    else{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   744
      val pruned = pAKC(acc, x, Nil)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   745
      val newTerms = breakIntoTerms(erase(pruned))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   746
      pruned match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   747
        case AZERO => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   748
          distinctBy5(xs, acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   749
        case xPrime => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   750
          xPrime :: distinctBy5(xs, newTerms.map(oneSimp) ::: acc)//distinctBy5(xs, addToAcc.map(oneSimp(_)) ::: acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   751
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   752
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   753
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   754
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   755
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   756
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   757
def breakIntoTerms(r: Rexp) : List[Rexp] = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   758
  case SEQ(r1, r2)  => breakIntoTerms(r1).map(r11 => SEQ(r11, r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   759
  case ALTS(r1, r2) => breakIntoTerms(r1) ::: breakIntoTerms(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   760
  case ZERO => Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   761
  case _ => r::Nil
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   762
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   763
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   764
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   765
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   766
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   767
  case (ONE, bs) => (Empty, bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   768
  case (CHAR(f), bs) => (Chr(f), bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   769
  case (ALTS(r1, r2), Z::bs1) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   770
      val (v, bs2) = decode_aux(r1, bs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   771
      (Left(v), bs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   772
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   773
  case (ALTS(r1, r2), S::bs1) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   774
      val (v, bs2) = decode_aux(r2, bs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   775
      (Right(v), bs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   776
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   777
  case (SEQ(r1, r2), bs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   778
    val (v1, bs1) = decode_aux(r1, bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   779
    val (v2, bs2) = decode_aux(r2, bs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   780
    (Sequ(v1, v2), bs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   781
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   782
  case (STAR(r1), S::bs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   783
    val (v, bs1) = decode_aux(r1, bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   784
    //(v)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   785
    val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   786
    (Stars(v::vs), bs2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   787
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   788
  case (STAR(_), Z::bs) => (Stars(Nil), bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   789
  case (RECD(x, r1), bs) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   790
    val (v, bs1) = decode_aux(r1, bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   791
    (Rec(x, v), bs1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   792
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   793
  case (NOT(r), bs) => (Nots(r.toString), bs)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   794
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   795
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   796
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   797
  case (v, Nil) => v
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   798
  case _ => throw new Exception("Not decodable")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   799
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   800
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   801
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   802
def blexing(r: Rexp, s: String) : Option[Val] = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   803
    val bit_code = blex(internalise(r), s.toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   804
    bit_code match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   805
      case Some(bits) => Some(decode(r, bits))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   806
      case None => None
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   807
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   808
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   809
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   810
def blexing_simp(r: Rexp, s: String) : Option[Val] = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   811
    val bit_code = blex_simp(internalise(r), s.toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   812
    bit_code match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   813
      case Some(bits) => Some(decode(r, bits))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   814
      case None => None
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   815
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   816
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   817
//def simpBlexer(r: Rexp, s: String) : Val = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   818
//  Try(blexing_simp(r, s)).getOrElse(Failure)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   819
//}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   820
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   821
def strong_blexing_simp(r: Rexp, s: String) : Val = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   822
  decode(r, strong_blex_simp(internalise(r), s.toList))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   823
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   824
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   825
def strong_blexing_simp5(r: Rexp, s: String) : Val = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   826
  decode(r, strong_blex_simp5(internalise(r), s.toList))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   827
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   828
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   829
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   830
def strongBlexer(r: Rexp, s: String) : Val = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   831
  Try(strong_blexing_simp(r, s)).getOrElse(Failure)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   832
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   833
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   834
def strongBlexer5(r: Rexp, s: String): Val = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   835
  Try(strong_blexing_simp5(r, s)).getOrElse(Failure)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   836
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   837
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   838
def strong_blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   839
  case Nil => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   840
    if (bnullable(r)) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   841
      //println(asize(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   842
      val bits = mkepsBC(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   843
      bits
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   844
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   845
    else 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   846
      throw new Exception("Not matched")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   847
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   848
  case c::cs => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   849
    val der_res = bder(c,r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   850
    val simp_res = strongBsimp(der_res)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   851
    strong_blex_simp(simp_res, cs)      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   852
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   853
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   854
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   855
def strong_blex_simp5(r: ARexp, s: List[Char]) : Bits = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   856
  case Nil => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   857
    if (bnullable(r)) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   858
      //println(asize(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   859
      val bits = mkepsBC(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   860
      bits
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   861
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   862
    else 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   863
      throw new Exception("Not matched")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   864
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   865
  case c::cs => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   866
    val der_res = bder(c,r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   867
    val simp_res = strongBsimp5(der_res)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   868
    strong_blex_simp5(simp_res, cs)      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   869
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   870
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   871
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   872
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   873
  def bders_simp(s: List[Char], r: ARexp) : ARexp = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   874
    case Nil => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   875
    case c::s => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   876
      //println(erase(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   877
      bders_simp(s, bsimp(bder(c, r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   878
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   879
  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   880
  def bdersStrong5(s: List[Char], r: ARexp) : ARexp = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   881
    case Nil => r
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   882
    case c::s => bdersStrong5(s, strongBsimp5(bder(c, r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   883
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   884
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   885
  def bdersSimp(s: String, r: Rexp) : ARexp = bders_simp(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   886
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   887
  def bdersStrong(s: List[Char], r: ARexp) : ARexp = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   888
    case Nil => r 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   889
    case c::s => bdersStrong(s, strongBsimp(bder(c, r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   890
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   891
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   892
  def bdersStrongRexp(s: String, r: Rexp) : ARexp = bdersStrong(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   893
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   894
  def erase(r:ARexp): Rexp = r match{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   895
    case AZERO => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   896
    case AONE(_) => ONE
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   897
    case ACHAR(bs, c) => CHAR(c)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   898
    case AALTS(bs, Nil) => ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   899
    case AALTS(bs, a::Nil) => erase(a)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   900
    case AALTS(bs, a::as) => ALTS(erase(a), erase(AALTS(bs, as)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   901
    case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   902
    case ASTAR(cs, r)=> STAR(erase(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   903
    case ANOT(bs, r) => NOT(erase(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   904
    case AANYCHAR(bs) => ANYCHAR
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   905
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   906
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   907
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   908
  def allCharSeq(r: Rexp) : Boolean = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   909
    case CHAR(c) => true
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   910
    case SEQ(r1, r2) => allCharSeq(r1) && allCharSeq(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   911
    case _ => false
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   912
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   913
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   914
  def flattenSeq(r: Rexp) : String = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   915
    case CHAR(c) => c.toString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   916
    case SEQ(r1, r2) => flattenSeq(r1) ++ flattenSeq(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   917
    case _ => throw new Error("flatten unflattenable rexp")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   918
  } 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   919
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   920
  def shortRexpOutput(r: Rexp) : String = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   921
      case CHAR(c) => c.toString
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   922
      case ONE => "1"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   923
      case ZERO => "0"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   924
      case SEQ(r1, r2) if(allCharSeq(r)) => flattenSeq(r)//"[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   925
      case SEQ(r1, r2) => "[" ++ shortRexpOutput(r1) ++ "~" ++ shortRexpOutput(r2) ++ "]"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   926
      case ALTS(r1, r2) => "(" ++ shortRexpOutput(r1) ++ "+" ++ shortRexpOutput(r2) ++ ")"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   927
      case STAR(STAR(r)) => "(..)*"// ++ shortRexpOutput(r) ++ "]*"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   928
      case STAR(r) => "STAR(" ++ shortRexpOutput(r) ++ ")"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   929
      //case RTOP => "RTOP"
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   930
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   931
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   932
  def blex(r: ARexp, s: List[Char]) : Option[Bits]= s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   933
      case Nil => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   934
        if (bnullable(r)) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   935
          val bits = mkepsBC(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   936
          Some(bits)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   937
        }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   938
        else 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   939
          None
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   940
          //throw new Exception("Not matched")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   941
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   942
      case c::cs => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   943
        val der_res = bder(c,r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   944
        //val simp_res = bsimp(der_res)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   945
        blex(der_res, cs)      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   946
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   947
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   948
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   949
  def blex_simp(r: ARexp, s: List[Char]) : Option[Bits]= s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   950
      case Nil => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   951
        if (bnullable(r)) {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   952
          val bits = mkepsBC(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   953
          Some(bits)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   954
        }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   955
        else 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   956
          None
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   957
          //throw new Exception("Not matched")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   958
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   959
      case c::cs => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   960
        val der_res = bder(c,r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   961
        val simp_res = bsimp(der_res)  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   962
        blex_simp(simp_res, cs)      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   963
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   964
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   965
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   966
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   967
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   968
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   969
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   970
    def asize(a: ARexp) = size(erase(a))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   971
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   972
//pder related
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   973
type Mon = (Char, Rexp)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   974
type Lin = Set[Mon]
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   975
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   976
def dot_prod(rs: Set[Rexp], r: Rexp): Set[Rexp] = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   977
    case ZERO => Set()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   978
    case ONE => rs
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   979
    case r => rs.map((re) => if (re == ONE) r else SEQ(re, r)  )   
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   980
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   981
  def cir_prod(l: Lin, t: Rexp): Lin = t match {//remember this Lin is different from the Lin in Antimirov's paper. Here it does not mean the set of all subsets of linear forms that does not contain zero, but rather the type a set of linear forms
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   982
    case ZERO => Set()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   983
    case ONE => l
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   984
    case t => l.map( m => m._2 match {case ZERO => m case ONE => (m._1, t) case p => (m._1, SEQ(p, t)) }  )
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   985
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   986
  def lf(r: Rexp): Lin = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   987
    case ZERO => Set()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   988
    case ONE => Set()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   989
    case CHAR(f) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   990
      //val Some(c) = alphabet.find(f) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   991
      Set((f, ONE))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   992
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   993
    case ALTS(r1, r2) => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   994
      lf(r1 ) ++ lf(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   995
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   996
    case STAR(r1) => cir_prod(lf(r1), STAR(r1)) //may try infix notation later......
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   997
    case SEQ(r1, r2) =>{
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   998
      if (nullable(r1))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
   999
        cir_prod(lf(r1), r2) ++ lf(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1000
      else
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1001
        cir_prod(lf(r1), r2) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1002
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1003
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1004
  def lfs(r: Set[Rexp]): Lin = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1005
    r.foldLeft(Set[Mon]())((acc, r) => acc ++ lf(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1006
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1007
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1008
  def pder(x: Char, t: Rexp): Set[Rexp] = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1009
    val lft = lf(t)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1010
    (lft.filter(mon => if(mon._1 == x) true else false)).map(mon => mon._2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1011
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1012
  def pders_single(s: List[Char], t: Rexp) : Set[Rexp] = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1013
    case x::xs => pders(xs, pder(x, t))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1014
    case Nil => Set(t)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1015
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1016
  def pders(s: List[Char], ts: Set[Rexp]) : Set[Rexp] = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1017
    case x::xs => pders(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1018
    case Nil => ts 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1019
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1020
  def pderss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]() )( (acc, s) => pders_single(s, t) ++ acc )
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1021
  def pdera(t: Rexp): Set[Rexp] = lf(t).map(mon => mon._2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1022
  //all implementation of partial derivatives that involve set union are potentially buggy
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1023
  //because they don't include the original regular term before they are pdered.
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1024
  //now only pderas is fixed.
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1025
  def pderas(t: Set[Rexp], d: Int): Set[Rexp] = if(d > 0) pderas(lfs(t).map(mon => mon._2), d - 1) ++ t else lfs(t).map(mon => mon._2) ++ t//repeated application of pderas over the newest set of pders.
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1026
  def pderUNIV(r: Rexp) : Set[Rexp] = pderas(Set(r), awidth(r) + 1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1027
  def awidth(r: Rexp) : Int = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1028
    case CHAR(c) => 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1029
    case SEQ(r1, r2) => awidth(r1) + awidth(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1030
    case ALTS(r1, r2) => awidth(r1) + awidth(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1031
    case ONE => 0
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1032
    case ZERO => 0
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1033
    case STAR(r) => awidth(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1034
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1035
  //def sigma_lf(l: Set[Mon]) : Rexp = ALTS(l.map(mon => SEQ(CHAR(mon._1),mon._2)).toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1036
  //def sigma(rs: Set[Rexp]) : Rexp = ALTS(rs.toList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1037
  def o(r: Rexp) = if (nullable(r)) ONE else ZERO
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1038
  //def nlf(t: Rexp) : Rexp = ALTS(List( o(t), sigma_lf(lf(t)) ))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1039
  def pdp(x: Char, r: Rexp) : Set[Rexp] = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1040
    case ZERO => Set[Rexp]()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1041
    case ONE => Set[Rexp]()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1042
    case CHAR(f) => if(x == f) Set(ONE) else Set[Rexp]()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1043
    case ALTS(r1, r2) => pdp(x, r1) ++ pdp(x, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1044
    case STAR(r1) => pdp(x, r).map(a => SEQ(a, STAR(r1)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1045
    case SEQ(a0, b) => if(nullable(a0)) pdp(x, a0).map(a => SEQ(a, b)) ++ pdp(x, b) else pdp(x, a0).map(a => SEQ(a, b))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1046
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1047
  def pdps(s: List[Char], ts: Set[Rexp]): Set[Rexp] = s match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1048
    case x::xs => pdps(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1049
    case Nil => ts   
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1050
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1051
  def pdpss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]())((acc, s) => pdps(s, Set(t)) ++ acc)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1052
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1053
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1054
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1055
def starPrint(r: Rexp) : Unit = r match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1056
        
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1057
          case SEQ(head, rstar) =>
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1058
            println(shortRexpOutput(head) ++ "~STARREG")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1059
          case STAR(rstar) =>
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1060
            println("STARREG")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1061
          case ALTS(r1, r2) =>  
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1062
            println("(")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1063
            starPrint(r1)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1064
            println("+")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1065
            starPrint(r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1066
            println(")")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1067
          case ZERO => println("0")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1068
      }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1069
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1070
// @arg(doc = "small tests")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1071
def n_astar_list(d: Int) : Rexp = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1072
  if(d == 0) STAR("a") 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1073
  else ALTS(STAR("a" * d), n_astar_list(d - 1))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1074
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1075
def n_astar_alts(d: Int) : Rexp = d match {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1076
  case 0 => n_astar_list(0)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1077
  case d => STAR(n_astar_list(d))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1078
  // case r1 :: r2 :: Nil => ALTS(r1, r2)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1079
  // case r1 :: Nil => r1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1080
  // case r :: rs => ALTS(r, n_astar_alts(rs))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1081
  // case Nil => throw new Error("should give at least 1 elem")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1082
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1083
def n_astar_aux(d: Int) = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1084
  if(d == 0) n_astar_alts(0)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1085
  else ALTS(n_astar_alts(d), n_astar_alts(d - 1))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1086
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1087
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1088
def n_astar(d: Int) : Rexp = STAR(n_astar_aux(d))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1089
//val STARREG = n_astar(3)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1090
// ( STAR("a") | 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1091
//                  ("a" | "aa").% | 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1092
//                 ( "a" | "aa" | "aaa").% 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1093
//                 ).%
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1094
                //( "a" | "aa" | "aaa" | "aaaa").% |
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1095
                //( "a" | "aa" | "aaa" | "aaaa" | "aaaaa").% 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1096
(((STAR("a") | ( STAR("aaa")) | STAR("aaaaa"| ( STAR("aaaaaaa")) | STAR("aaaaaaaaaaa"))).%).%).%
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1097
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1098
// @main
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1099
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1100
def lcm(list: Seq[Int]):Int=list.foldLeft(1:Int){
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1101
  (a, b) => b * a /
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1102
  Stream.iterate((a,b)){case (x,y) => (y, x%y)}.dropWhile(_._2 != 0).head._1.abs
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1103
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1104
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1105
def small() = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1106
  //val pderSTAR = pderUNIV(STARREG)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1107
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1108
  //val refSize = pderSTAR.map(size(_)).sum
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1109
  for(n <- 1 to 12){
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1110
    val STARREG = n_astar_alts(n)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1111
    val iMax = (lcm((1 to n).toList))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1112
    for(i <- 1 to iMax + 1 ){// 100, 400, 800, 840, 841, 900 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1113
      val prog0 = "a" * i
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1114
      //println(s"test: $prog0")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1115
      print(n)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1116
      print(" ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1117
      // print(i)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1118
      // print(" ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1119
      println(asize(bders_simp(prog0.toList, internalise(STARREG))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1120
    }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1121
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1122
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1123
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1124
def generator_test() {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1125
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1126
  // test(rexp(7), 1000) { (r: Rexp) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1127
  //   val ss = stringsFromRexp(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1128
  //   val boolList = ss.map(s => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1129
  //     val simpVal = simpBlexer(r, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1130
  //     val strongVal = strongBlexer(r, s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1131
  //     // println(simpVal)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1132
  //     // println(strongVal)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1133
  //     (simpVal == strongVal) && (simpVal != None) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1134
  //   })
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1135
  //   !boolList.exists(b => b == false)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1136
  // }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1137
  // test(single(STAR(ALTS(STAR(CHAR('c')),ALTS(CHAR('c'),ZERO)))), 100000) { (r: Rexp) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1138
  //   val ss = stringsFromRexp(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1139
  //   val boolList = ss.map(s => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1140
  //     val bdStrong = bdersStrong(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1141
  //     val bdStrong5 = bdersStrong5(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1142
  //     // println(shortRexpOutput(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1143
  //     // println(s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1144
  //     // println(strongVal)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1145
  //     // println(strongVal5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1146
  //     // (strongVal == strongVal5) 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1147
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1148
  //     if(asize(bdStrong5) > asize(bdStrong)){
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1149
  //       println(s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1150
  //       println(shortRexpOutput(erase(bdStrong5)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1151
  //       println(shortRexpOutput(erase(bdStrong)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1152
  //     }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1153
  //     asize(bdStrong5) <= asize(bdStrong)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1154
  //   })
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1155
  //   !boolList.exists(b => b == false)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1156
  // }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1157
  //*** example where bdStrong5 has a smaller size than bdStrong
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1158
  // test(single(STAR(SEQ(ALTS(SEQ(STAR(CHAR('a')),ALTS(ALTS(ONE,ZERO),SEQ(ONE,ONE))),CHAR('a')),ONE))), 1) { (r: Rexp) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1159
  //*** depth 5 example bdStrong5 larger size than bdStrong
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1160
  // test(single(STAR(SEQ(SEQ(ALTS(CHAR('b'),STAR(CHAR('b'))),CHAR('b')),(ALTS(STAR(CHAR('c')), ONE))))), 1) {(r: Rexp) =>
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1161
 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1162
 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1163
 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1164
  //sanity check from Christian's request
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1165
  // val r = ("a" | "ab") ~ ("bc" | "c")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1166
  // val a = internalise(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1167
  // val aval = blexing_simp(r, "abc")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1168
  // println(aval)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1169
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1170
  //sample counterexample:(depth 7)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1171
  //STAR(SEQ(ALTS(STAR(STAR(STAR(STAR(CHAR(c))))),ALTS(CHAR(c),CHAR(b))),ALTS(ZERO,SEQ(ALTS(ALTS(STAR(CHAR(c)),SEQ(CHAR(b),CHAR(a))),CHAR(c)),STAR(ALTS(ALTS(ONE,CHAR(a)),STAR(CHAR(c))))))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1172
  //(depth5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1173
  //STAR(SEQ(ALTS(ALTS(STAR(CHAR(b)),SEQ(ONE,CHAR(b))),SEQ(STAR(CHAR(a)),CHAR(b))),ALTS(ZERO,ALTS(STAR(CHAR(b)),STAR(CHAR(a))))))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1174
  test(rexp(4), 10000000) { (r: Rexp) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1175
  // ALTS(SEQ(SEQ(ONE,CHAR('a')),STAR(CHAR('a'))),SEQ(ALTS(CHAR('c'),ONE),STAR(ZERO))))))), 1) { (r: Rexp) => 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1176
    val ss = stringsFromRexp(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1177
    val boolList = ss.map(s => {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1178
      val bdStrong = bdersStrong(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1179
      val bdStrong5 = bdersStrong5(s.toList, internalise(r))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1180
      val bdFurther5 = strongBsimp5(bdStrong5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1181
      val sizeFurther5 = asize(bdFurther5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1182
      val pdersSet = pderUNIV(r)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1183
      val size5 = asize(bdStrong5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1184
      val size0 = asize(bdStrong)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1185
      // println(s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1186
      // println("pdersSet size")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1187
      // println(pdersSet.size)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1188
      // pdersSet.foreach(r => println(shortRexpOutput(r)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1189
      // println("after bdStrong5")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1190
      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1191
      // println(shortRexpOutput(erase(bdStrong5)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1192
      // println(breakIntoTerms(erase(bdStrong5)).size)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1193
      
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1194
      // println("after bdStrong")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1195
      // println(shortRexpOutput(erase(bdStrong)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1196
      // println(breakIntoTerms(erase(bdStrong)).size)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1197
      // println(size5, size0, sizeFurther5)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1198
      // aprint(strongBsimp5(bdStrong))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1199
      // println(asize(strongBsimp5(bdStrong5)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1200
      // println(s)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1201
      size5 <= size0 
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1202
    })
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1203
    // println(boolList)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1204
    //!boolList.exists(b => b == false)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1205
    !boolList.exists(b => b == false)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1206
  }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1207
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1208
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1209
}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1210
//small()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1211
// generator_test()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1212
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1213
// 1
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1214
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1215
// SEQ(SEQ(SEQ(ONE,CHAR('b')),STAR(CHAR('b'))),
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1216
// SEQ(ALTS(ALTS(ZERO,STAR(CHAR('b'))),
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1217
// STAR(ALTS(CHAR('a'),SEQ(SEQ(STAR(ALTS(STAR(CHAR('c')),CHAR('a'))),
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1218
// SEQ(CHAR('a'),SEQ(ALTS(CHAR('b'),ZERO),SEQ(ONE,CHAR('b'))))),ONE)))),ONE))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1219
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1220
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1221
// Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1222
// Sequ(Sequ(Sequ(Empty,Chr(b)),Stars(List(Chr(b), Chr(b)))),Sequ(Right(Stars(List(Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty)), Right(Sequ(Sequ(Stars(List(Right(Chr(a)), Right(Chr(a)))),Sequ(Chr(a),Sequ(Left(Chr(b)),Sequ(Empty,Chr(b))))),Empty))))),Empty))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1223
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1224
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1225
//def time[R](block: => R): R = {
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1226
//    val t0 = System.nanoTime()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1227
//    val result = block    // call-by-name
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1228
//    val t1 = System.nanoTime()
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1229
//    println(" " + (t1 - t0)/1e9 + "")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1230
//    result
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1231
//}
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1232
//val astarstarB  = STAR(STAR("a"))~("b")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1233
//val data_points_x_axis = List.range(0, 60000, 3000)
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1234
//for (i <- data_points_x_axis) {print(i); time { blexing(astarstarB, ("a"*i)) } }
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1235
////println(data_points_x_axis.zip( List(0,1,3,4)))
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1236
//for(i <- List.range(0, 40000, 3000)) print(i + " ")
6da4516ea87d more changes to figures & benchmarking
Chengsong
parents:
diff changeset
  1237