Spiral.scala
author Chengsong
Wed, 27 May 2020 22:23:52 +0100
changeset 151 73f990bc6843
parent 150 b51d34113d47
permissions -rwxr-xr-x
clean
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
151
Chengsong
parents: 150
diff changeset
     1
Chengsong
parents: 150
diff changeset
     2
import Spiral._
0
Chengsong
parents:
diff changeset
     3
import Element.elem
Chengsong
parents:
diff changeset
     4
import RexpRelated._
Chengsong
parents:
diff changeset
     5
import RexpRelated.Rexp._
151
Chengsong
parents: 150
diff changeset
     6
//import Partial._
0
Chengsong
parents:
diff changeset
     7
import scala.collection.mutable.ListBuffer
151
Chengsong
parents: 150
diff changeset
     8
0
Chengsong
parents:
diff changeset
     9
object Spiral{
Chengsong
parents:
diff changeset
    10
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
  val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c')
151
Chengsong
parents: 150
diff changeset
    13
  
0
Chengsong
parents:
diff changeset
    14
  val arr_of_size = ListBuffer.empty[Int]
2
cf169411b771 more comments
Chengsong
parents: 1
diff changeset
    15
151
Chengsong
parents: 150
diff changeset
    16
0
Chengsong
parents:
diff changeset
    17
  val ran = scala.util.Random
Chengsong
parents:
diff changeset
    18
  var alphabet_size = 3
Chengsong
parents:
diff changeset
    19
  def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
Chengsong
parents:
diff changeset
    20
    if(depth == 1){
Chengsong
parents:
diff changeset
    21
      ((ran.nextInt(4) + 97).toChar).toString
Chengsong
parents:
diff changeset
    22
    }
Chengsong
parents:
diff changeset
    23
    else if(star){
Chengsong
parents:
diff changeset
    24
      STAR(balanced_seq_star_gen(depth - 1, false))
Chengsong
parents:
diff changeset
    25
    }
Chengsong
parents:
diff changeset
    26
    else{
Chengsong
parents:
diff changeset
    27
      SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
Chengsong
parents:
diff changeset
    28
    }
Chengsong
parents:
diff changeset
    29
  }
Chengsong
parents:
diff changeset
    30
  def random_struct_gen(depth:Int): Rexp = {
Chengsong
parents:
diff changeset
    31
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
    32
    val dice2 = ran.nextInt(3)
151
Chengsong
parents: 150
diff changeset
    33
    val new_depth = (List(0, depth - 1 - dice2)).max
Chengsong
parents: 150
diff changeset
    34
    (dice, new_depth) match {
0
Chengsong
parents:
diff changeset
    35
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
151
Chengsong
parents: 150
diff changeset
    36
      case (0, i) => STAR(random_struct_gen(new_depth))
Chengsong
parents: 150
diff changeset
    37
      case (1, i) => SEQ(random_struct_gen(new_depth), random_struct_gen(new_depth))
Chengsong
parents: 150
diff changeset
    38
      case (2, i) => ALTS( List(random_struct_gen(new_depth), random_struct_gen(new_depth)) )
0
Chengsong
parents:
diff changeset
    39
    }
Chengsong
parents:
diff changeset
    40
  }
Chengsong
parents:
diff changeset
    41
  def balanced_struct_gen(depth: Int): Rexp = {
Chengsong
parents:
diff changeset
    42
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
    43
    (dice, depth) match {
Chengsong
parents:
diff changeset
    44
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
    45
      case (0, i) => STAR(random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
    46
      case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
    47
      case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) )
Chengsong
parents:
diff changeset
    48
    }
Chengsong
parents:
diff changeset
    49
  }
6
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    50
  def random_pool(n: Int, d: Int) : Stream[Rexp] = n match {
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    51
    case 0 => (for (i <- 1 to 10) yield balanced_struct_gen(d)).toStream
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    52
    case n => {  
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    53
      val rs = random_pool(n - 1, d)
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    54
      rs #:::
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    55
      (for (i <- (1 to 10).toStream) yield balanced_struct_gen(d)) #::: 
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    56
      (for (i <- (1 to 10).toStream) yield random_struct_gen(d))
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    57
    }
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    58
  }
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
    59
0
Chengsong
parents:
diff changeset
    60
  def rd_string_gen(alp_size: Int, len: Int): String = {
Chengsong
parents:
diff changeset
    61
    if( len > 0)
Chengsong
parents:
diff changeset
    62
      ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
Chengsong
parents:
diff changeset
    63
    else
Chengsong
parents:
diff changeset
    64
      ((ran.nextInt(alp_size) + 97).toChar).toString
Chengsong
parents:
diff changeset
    65
  }
Chengsong
parents:
diff changeset
    66
  def plot(b: List[Int]){
Chengsong
parents:
diff changeset
    67
    println(b(0),b.max)
Chengsong
parents:
diff changeset
    68
Chengsong
parents:
diff changeset
    69
  }
151
Chengsong
parents: 150
diff changeset
    70
Chengsong
parents: 150
diff changeset
    71
0
Chengsong
parents:
diff changeset
    72
  def star_gen(dp: Int): Rexp = {
Chengsong
parents:
diff changeset
    73
    if(dp > 0)
Chengsong
parents:
diff changeset
    74
      STAR(star_gen(dp - 1))
Chengsong
parents:
diff changeset
    75
    else
Chengsong
parents:
diff changeset
    76
      "a"
Chengsong
parents:
diff changeset
    77
  }
Chengsong
parents:
diff changeset
    78
  def strs_gen(len: Int, num: Int): List[String] = {
Chengsong
parents:
diff changeset
    79
    if(num > 0){
Chengsong
parents:
diff changeset
    80
      rd_string_gen(3, len)::strs_gen(len, num - 1)
Chengsong
parents:
diff changeset
    81
    }
Chengsong
parents:
diff changeset
    82
    else{
Chengsong
parents:
diff changeset
    83
      Nil
Chengsong
parents:
diff changeset
    84
    }
Chengsong
parents:
diff changeset
    85
  }
151
Chengsong
parents: 150
diff changeset
    86
0
Chengsong
parents:
diff changeset
    87
  val mkst = "abcdefghijklmnopqrstuvwxyz"
151
Chengsong
parents: 150
diff changeset
    88
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    89
  val big_panda = STAR(STAR(STAR(ALTS(List(ALTS(List(CHAR('c'), CHAR('b'))), SEQ(CHAR('c'),CHAR('c')))))))
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    90
  val str_panda = "ccccb"
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    91
93
Chengsong
parents: 92
diff changeset
    92
  def bstostick(bs: List[Bit]): Element = bs match {
Chengsong
parents: 92
diff changeset
    93
    //case b::Nil => elem(b.toString)
Chengsong
parents: 92
diff changeset
    94
    case b::bs1 => elem(b.toString) beside bstostick(bs1)
Chengsong
parents: 92
diff changeset
    95
    case Nil => elem(' ', 1, 1)
Chengsong
parents: 92
diff changeset
    96
  }
151
Chengsong
parents: 150
diff changeset
    97
  def aregex_simple_print(r: ARexp): Element = r match {
93
Chengsong
parents: 92
diff changeset
    98
    case AALTS(bs,rs) => {
Chengsong
parents: 92
diff changeset
    99
      val bitstick = bstostick(bs)
Chengsong
parents: 92
diff changeset
   100
      rs match {
Chengsong
parents: 92
diff changeset
   101
        case r::rs1 =>  
Chengsong
parents: 92
diff changeset
   102
        rs1.foldLeft( 
Chengsong
parents: 92
diff changeset
   103
        ((elem("(") left_align bitstick) beside 
151
Chengsong
parents: 150
diff changeset
   104
        aregex_simple_print(r))  )( (acc, r2) => acc beside ((elem(" + ") above elem("   ")) beside aregex_simple_print(r2) )) beside
93
Chengsong
parents: 92
diff changeset
   105
        elem(")")
Chengsong
parents: 92
diff changeset
   106
        case Nil => elem(' ', 1, 1)
Chengsong
parents: 92
diff changeset
   107
      }
Chengsong
parents: 92
diff changeset
   108
    }
Chengsong
parents: 92
diff changeset
   109
    case ASEQ(bs, r1, r2) => {
151
Chengsong
parents: 150
diff changeset
   110
      ((elem("[") left_align bstostick(bs)) beside  aregex_simple_print(r1) beside elem("~") beside aregex_simple_print(r2) beside (elem("]") above elem(" "))) 
93
Chengsong
parents: 92
diff changeset
   111
    }
Chengsong
parents: 92
diff changeset
   112
    case ASTAR(bs, r) => {
Chengsong
parents: 92
diff changeset
   113
      r match {
Chengsong
parents: 92
diff changeset
   114
        case AONE(_) | AZERO | ACHAR(_, _) => {
151
Chengsong
parents: 150
diff changeset
   115
          (elem("{") left_align bstostick(bs)) beside (aregex_simple_print(r) beside elem("}*")) 
93
Chengsong
parents: 92
diff changeset
   116
        }
Chengsong
parents: 92
diff changeset
   117
        case _ => {
151
Chengsong
parents: 150
diff changeset
   118
          (elem("{") left_align bstostick(bs)) beside (aregex_simple_print(r) beside elem("}*")) 
93
Chengsong
parents: 92
diff changeset
   119
        }
Chengsong
parents: 92
diff changeset
   120
      }
Chengsong
parents: 92
diff changeset
   121
    }
Chengsong
parents: 92
diff changeset
   122
    case AONE(bs) => {
Chengsong
parents: 92
diff changeset
   123
      elem("1") left_align bstostick(bs)
Chengsong
parents: 92
diff changeset
   124
    }
Chengsong
parents: 92
diff changeset
   125
    case ACHAR(bs, c) => {
Chengsong
parents: 92
diff changeset
   126
      elem(c, 1, 1) left_align bstostick(bs)
Chengsong
parents: 92
diff changeset
   127
    }
Chengsong
parents: 92
diff changeset
   128
    case AZERO =>
Chengsong
parents: 92
diff changeset
   129
    {
Chengsong
parents: 92
diff changeset
   130
      elem ("0") above elem(" ")
Chengsong
parents: 92
diff changeset
   131
    }
Chengsong
parents: 92
diff changeset
   132
  }
151
Chengsong
parents: 150
diff changeset
   133
  def regex_simple_print(r: Rexp): Unit = r match {
93
Chengsong
parents: 92
diff changeset
   134
    case ALTS( r::rs ) => {
Chengsong
parents: 92
diff changeset
   135
      print("(")
151
Chengsong
parents: 150
diff changeset
   136
      regex_simple_print(r)
93
Chengsong
parents: 92
diff changeset
   137
      rs.map(r2=>{      
Chengsong
parents: 92
diff changeset
   138
        print(" + ")
151
Chengsong
parents: 150
diff changeset
   139
        regex_simple_print(r2)
93
Chengsong
parents: 92
diff changeset
   140
      })
Chengsong
parents: 92
diff changeset
   141
      print(")")
Chengsong
parents: 92
diff changeset
   142
    }
Chengsong
parents: 92
diff changeset
   143
    case SEQ(r1, r2) => {
151
Chengsong
parents: 150
diff changeset
   144
      regex_simple_print(r1)
93
Chengsong
parents: 92
diff changeset
   145
      print("~")
151
Chengsong
parents: 150
diff changeset
   146
      regex_simple_print(r2)
93
Chengsong
parents: 92
diff changeset
   147
    }
Chengsong
parents: 92
diff changeset
   148
    case STAR(r) => {
Chengsong
parents: 92
diff changeset
   149
      r match {
Chengsong
parents: 92
diff changeset
   150
        case ONE | ZERO | CHAR(_) => {
151
Chengsong
parents: 150
diff changeset
   151
          regex_simple_print(r)
93
Chengsong
parents: 92
diff changeset
   152
          print("*")
Chengsong
parents: 92
diff changeset
   153
        }
Chengsong
parents: 92
diff changeset
   154
        case _ => {
Chengsong
parents: 92
diff changeset
   155
          print("[")
151
Chengsong
parents: 150
diff changeset
   156
          regex_simple_print(r)
93
Chengsong
parents: 92
diff changeset
   157
          print("]*")
Chengsong
parents: 92
diff changeset
   158
        }
Chengsong
parents: 92
diff changeset
   159
      }
Chengsong
parents: 92
diff changeset
   160
    }
Chengsong
parents: 92
diff changeset
   161
    case ONE => {
Chengsong
parents: 92
diff changeset
   162
      print("1")
Chengsong
parents: 92
diff changeset
   163
    }
Chengsong
parents: 92
diff changeset
   164
    case ZERO => {
Chengsong
parents: 92
diff changeset
   165
      print("0")
Chengsong
parents: 92
diff changeset
   166
    }
Chengsong
parents: 92
diff changeset
   167
    case CHAR(c) =>{
Chengsong
parents: 92
diff changeset
   168
      print(c)
Chengsong
parents: 92
diff changeset
   169
    }
Chengsong
parents: 92
diff changeset
   170
  }
Chengsong
parents: 92
diff changeset
   171
  //found r = SEQ(ALTS(List(CHAR(c), CHAR(a))),SEQ(ALTS(List(CHAR(a), CHAR(c))),ALTS(List(CHAR(b), CHAR(c))))) s = "aa"
Chengsong
parents: 92
diff changeset
   172
    def bsimp_print(r: ARexp): Unit = r match {
Chengsong
parents: 92
diff changeset
   173
    case ASEQ(bs, r1, r2) => 
Chengsong
parents: 92
diff changeset
   174
      {
Chengsong
parents: 92
diff changeset
   175
        println("0.r or r.0 to 0 or bs1 1.r to fuse bs++bs1 r")
151
Chengsong
parents: 150
diff changeset
   176
        aregex_simple_print(bsimp(r1))
Chengsong
parents: 150
diff changeset
   177
        aregex_simple_print(bsimp(r2))
93
Chengsong
parents: 92
diff changeset
   178
      }
Chengsong
parents: 92
diff changeset
   179
    case AALTS(bs, rs) => {
Chengsong
parents: 92
diff changeset
   180
      println("rs.map(bsimp) equals *************")
Chengsong
parents: 92
diff changeset
   181
      val rs_simp = rs.map(bsimp)
Chengsong
parents: 92
diff changeset
   182
      for(r <- rs_simp){
151
Chengsong
parents: 150
diff changeset
   183
        println(aregex_simple_print(r))
93
Chengsong
parents: 92
diff changeset
   184
      }
Chengsong
parents: 92
diff changeset
   185
      println("*************")
Chengsong
parents: 92
diff changeset
   186
      println("flts(rs_simp)")
Chengsong
parents: 92
diff changeset
   187
      val flat_res = flats(rs_simp)
Chengsong
parents: 92
diff changeset
   188
      for(r <- flat_res){
151
Chengsong
parents: 150
diff changeset
   189
        println(aregex_simple_print(r))
93
Chengsong
parents: 92
diff changeset
   190
      }
Chengsong
parents: 92
diff changeset
   191
      println("dB(flat_res)")
Chengsong
parents: 92
diff changeset
   192
      val dist_res = distinctBy(flat_res, erase)
Chengsong
parents: 92
diff changeset
   193
      for(r <- dist_res){
151
Chengsong
parents: 150
diff changeset
   194
        println(aregex_simple_print(r))
93
Chengsong
parents: 92
diff changeset
   195
      }
Chengsong
parents: 92
diff changeset
   196
      dist_res match {
Chengsong
parents: 92
diff changeset
   197
        case Nil => println("AZERO")
Chengsong
parents: 92
diff changeset
   198
        case r::Nil => println("fuse(bs, r)")
Chengsong
parents: 92
diff changeset
   199
        case rs => println("AALTS(bs, rs)")
Chengsong
parents: 92
diff changeset
   200
      }
Chengsong
parents: 92
diff changeset
   201
    }
Chengsong
parents: 92
diff changeset
   202
    case _ => println("No simp at this level")
Chengsong
parents: 92
diff changeset
   203
  }
151
Chengsong
parents: 150
diff changeset
   204
Chengsong
parents: 150
diff changeset
   205
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   206
  //simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set)
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   207
  def pushbits(r: ARexp): ARexp = r match {
11
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   208
    case AALTS(bs, rs) => AALTS(Nil, rs.map(r=>fuse(bs, pushbits(r))))
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   209
    case ASEQ(bs, r1, r2) => ASEQ(bs, pushbits(r1), pushbits(r2))
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   210
    case r => r
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   211
  }
14
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   212
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   213
17
Chengsong
parents: 16
diff changeset
   214
  def nice_lex(r: Rexp, s: List[Char], ar: ARexp) : Val = s match {
Chengsong
parents: 16
diff changeset
   215
    case Nil => 
Chengsong
parents: 16
diff changeset
   216
    if (nullable(r)){
Chengsong
parents: 16
diff changeset
   217
      val vr = mkeps(r)
Chengsong
parents: 16
diff changeset
   218
      val bits1 = retrieve(ar, vr)
Chengsong
parents: 16
diff changeset
   219
      val av = bsimp2(ar, vr)
Chengsong
parents: 16
diff changeset
   220
      val bits2 = retrieve(av._1, av._2)
Chengsong
parents: 16
diff changeset
   221
      if(bits1 != bits2) throw new Exception("bsimp2 does not work")
Chengsong
parents: 16
diff changeset
   222
      vr
Chengsong
parents: 16
diff changeset
   223
    }  
Chengsong
parents: 16
diff changeset
   224
    else throw new Exception("Not matched")
Chengsong
parents: 16
diff changeset
   225
    case c::cs => { 
Chengsong
parents: 16
diff changeset
   226
      val vr = inj(r, c, nice_lex(der(c, r), cs, bder(c, ar)));
Chengsong
parents: 16
diff changeset
   227
      val bits1 = retrieve(ar, vr);
Chengsong
parents: 16
diff changeset
   228
      val av = bsimp2(ar, vr);
Chengsong
parents: 16
diff changeset
   229
      val bits2 = retrieve(av._1, av._2);
Chengsong
parents: 16
diff changeset
   230
      if(bits1 != bits2) throw new Exception("bsimp2 does not work");
Chengsong
parents: 16
diff changeset
   231
      vr 
Chengsong
parents: 16
diff changeset
   232
    }
Chengsong
parents: 16
diff changeset
   233
  }
Chengsong
parents: 16
diff changeset
   234
  def test_bsimp2(){
Chengsong
parents: 16
diff changeset
   235
    for(i <- 1 to 1000){
Chengsong
parents: 16
diff changeset
   236
      val rg = (balanced_struct_gen(9))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) 
Chengsong
parents: 16
diff changeset
   237
      val st = rd_string_gen(3, 4)
Chengsong
parents: 16
diff changeset
   238
      val a = internalise(rg)
Chengsong
parents: 16
diff changeset
   239
      val final_derivative = ders(st.toList, rg)
Chengsong
parents: 16
diff changeset
   240
      if(nullable(final_derivative))
Chengsong
parents: 16
diff changeset
   241
        nice_lex(rg, st.toList, a)
Chengsong
parents: 16
diff changeset
   242
    }
Chengsong
parents: 16
diff changeset
   243
  }
15
Chengsong
parents: 14
diff changeset
   244
17
Chengsong
parents: 16
diff changeset
   245
  def neat_retrieve(){
Chengsong
parents: 16
diff changeset
   246
    for(i <- 1 to 1000){
Chengsong
parents: 16
diff changeset
   247
      val rg = internalise(balanced_struct_gen(6))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) 
Chengsong
parents: 16
diff changeset
   248
      val st = rd_string_gen(3, 5)
Chengsong
parents: 16
diff changeset
   249
      val final_derivative = ders(st.toList, erase(rg))
Chengsong
parents: 16
diff changeset
   250
      if(nullable(final_derivative)){
Chengsong
parents: 16
diff changeset
   251
        val unsimplified_vl = mkeps(final_derivative)
Chengsong
parents: 16
diff changeset
   252
        val arexp_for_retrieve = bders( st.toList, rg)
Chengsong
parents: 16
diff changeset
   253
        val simp_ar_vl = bsimp2(arexp_for_retrieve, unsimplified_vl)
Chengsong
parents: 16
diff changeset
   254
        val bits1 = retrieve(arexp_for_retrieve, unsimplified_vl)
Chengsong
parents: 16
diff changeset
   255
        val bits2 = retrieve(simp_ar_vl._1, simp_ar_vl._2)
Chengsong
parents: 16
diff changeset
   256
        if(bits1 != bits2){
Chengsong
parents: 16
diff changeset
   257
          println("nOOOOOOOOOO!")
15
Chengsong
parents: 14
diff changeset
   258
        }
Chengsong
parents: 14
diff changeset
   259
      }
11
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   260
    }
17
Chengsong
parents: 16
diff changeset
   261
  }
Chengsong
parents: 16
diff changeset
   262
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   263
  def radical_correctness(){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   264
    enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   265
    random_pool(1, 5).map(tests_blexer_simp(strs(5, "abc"))).toSet
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   266
  }
15
Chengsong
parents: 14
diff changeset
   267
  def christian_def(){
Chengsong
parents: 14
diff changeset
   268
    val r = ALTS(List(SEQ(ZERO,CHAR('b')), ONE))
Chengsong
parents: 14
diff changeset
   269
    val v = Right(Empty)
Chengsong
parents: 14
diff changeset
   270
    val a = internalise(r)
17
Chengsong
parents: 16
diff changeset
   271
    val a_v = bsimp2(a,v)
15
Chengsong
parents: 14
diff changeset
   272
    println(s"Testing ${r} and ${v}")
Chengsong
parents: 14
diff changeset
   273
    println(s"internalise(r) = ${a}")
17
Chengsong
parents: 16
diff changeset
   274
    println(s"a_v = ${a_v}")
15
Chengsong
parents: 14
diff changeset
   275
    val bs1 = retrieve(a, v)
Chengsong
parents: 14
diff changeset
   276
    println(bs1)
17
Chengsong
parents: 16
diff changeset
   277
    println(s"as = ${a_v._1}")
15
Chengsong
parents: 14
diff changeset
   278
    //val bs2 = retrieve(as, decode(erase(as), bs1))
17
Chengsong
parents: 16
diff changeset
   279
    val bs3 = retrieve(a_v._1, a_v._2)//decode(erase(as), bs1) does not work
Chengsong
parents: 16
diff changeset
   280
    //println(decode(erase(as), bs1))
Chengsong
parents: 16
diff changeset
   281
    println(s"bs1=${bs1}, bs3=${bs3}")
Chengsong
parents: 16
diff changeset
   282
    //println(decode(erase(a_v._1), bs3))
15
Chengsong
parents: 14
diff changeset
   283
  }
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   284
  def christian_def2(){
89
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   285
    val a = AALTS(List(S), List(AZERO, ASEQ(List(S), AALTS(List(S), List(AONE(List(S)), ACHAR(List(S), 'c'))), ACHAR(List(S),'c') )) )
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   286
    //AALTS(List(Z), List(AZERO, ASEQ(List(), AALTS(List(),List(AONE(List()), ACHAR(Nil, 'b'))), ACHAR(Nil, 'b'))  )   )
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   287
    val unsimp = bsimp(bder('c',a))
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   288
    val simped = bsimp(bder('c', bsimp(a))           )
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   289
    println(bsimp(a))
89
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   290
    if(unsimp != simped){
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   291
      println(s"bsimp(bder c r) = ${unsimp}, whereas bsimp(bder c bsimp r) = ${simped}")
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   292
    }
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   293
  }
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   294
87
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   295
  def speed_test(){
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   296
    val s0 = "a"*1000
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   297
    val r = SEQ(STAR("a"), "b")
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   298
    for(i <- 1 to 30){
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   299
      val s = s0*i
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   300
      val start = System.nanoTime()
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   301
      try{
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   302
        blex_simp(internalise(r), s.toList)
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   303
      }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   304
      catch{
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   305
        case x: Exception =>
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   306
      }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   307
      val end = System.nanoTime()
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   308
      printf("%d  %f\n",i, (end - start)/1.0e9)
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   309
    }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   310
  }
91
Chengsong
parents: 89
diff changeset
   311
  /*
Chengsong
parents: 89
diff changeset
   312
  lemma retrieve_encode_STARS:
Chengsong
parents: 89
diff changeset
   313
  assumes "∀v∈set vs. ⊨ v : r ∧ code v = retrieve (intern r) v"
Chengsong
parents: 89
diff changeset
   314
  shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
Chengsong
parents: 89
diff changeset
   315
  */
Chengsong
parents: 89
diff changeset
   316
  def retrieve_encode_STARS(){
Chengsong
parents: 89
diff changeset
   317
    val r =  ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('b')) ) 
Chengsong
parents: 89
diff changeset
   318
    //val v =  Stars(List(Sequ(Chr('a'), Chr('b')),  Chr('a')) )
Chengsong
parents: 89
diff changeset
   319
    val v1 = Right(Sequ(Chr('a'), Chr('b')))
Chengsong
parents: 89
diff changeset
   320
    val v2 = Left(Chr('a'))
Chengsong
parents: 89
diff changeset
   321
    val compressed1 = code(v1)
Chengsong
parents: 89
diff changeset
   322
    val compressed2 = code(v2)
Chengsong
parents: 89
diff changeset
   323
    val xompressed1 = retrieve(internalise(r), v1)
Chengsong
parents: 89
diff changeset
   324
    val xompressed2 = retrieve(internalise(r), v2)
Chengsong
parents: 89
diff changeset
   325
    println(compressed1, compressed2, xompressed1, xompressed2)
Chengsong
parents: 89
diff changeset
   326
    val v = Stars(List(v1, v2))
Chengsong
parents: 89
diff changeset
   327
    val compressed = code(v)
Chengsong
parents: 89
diff changeset
   328
    val a = ASTAR(List(), internalise(r))
Chengsong
parents: 89
diff changeset
   329
    val xompressed = retrieve(a, v)
Chengsong
parents: 89
diff changeset
   330
    println(compressed, xompressed)
Chengsong
parents: 89
diff changeset
   331
  }
Chengsong
parents: 89
diff changeset
   332
  //what does contains7 do?
Chengsong
parents: 89
diff changeset
   333
  //it causes exception
Chengsong
parents: 89
diff changeset
   334
  //relation between retreive, bder and injection
Chengsong
parents: 89
diff changeset
   335
  // a match error occurs when doing the injection
Chengsong
parents: 89
diff changeset
   336
  def contains7(){
Chengsong
parents: 89
diff changeset
   337
    val r = STAR( SEQ(CHAR('a'),CHAR('b')) )
Chengsong
parents: 89
diff changeset
   338
    val v = Sequ(Chr('b'), Stars(List(Sequ(Chr('a'), Chr('b')))))
Chengsong
parents: 89
diff changeset
   339
    val a = internalise(r)
Chengsong
parents: 89
diff changeset
   340
    val c = 'a'
Chengsong
parents: 89
diff changeset
   341
    val v_aug = inj(r, c, v)
Chengsong
parents: 89
diff changeset
   342
    println("bder c r:")
Chengsong
parents: 89
diff changeset
   343
    println(bder(c, a))
Chengsong
parents: 89
diff changeset
   344
    println("r:")
Chengsong
parents: 89
diff changeset
   345
    println(a)
Chengsong
parents: 89
diff changeset
   346
    println("bits they can both produce:")
Chengsong
parents: 89
diff changeset
   347
    println(retrieve(a, v_aug))
Chengsong
parents: 89
diff changeset
   348
  }
Chengsong
parents: 89
diff changeset
   349
  def der_seq(r:ARexp, s: List[Char],acc: List[ARexp]) : List[ARexp] = s match{
Chengsong
parents: 89
diff changeset
   350
    case Nil => acc ::: List(r)
Chengsong
parents: 89
diff changeset
   351
    case c::cs => der_seq(bder(c, r), cs, acc ::: List(r))
Chengsong
parents: 89
diff changeset
   352
  }
Chengsong
parents: 89
diff changeset
   353
  def der_seq_rev(r:ARexp, s: List[Char], acc: List[ARexp]): List[ARexp] = s match{
Chengsong
parents: 89
diff changeset
   354
    case Nil => r::acc
Chengsong
parents: 89
diff changeset
   355
    case c::cs =>der_seq_rev(r, cs, bders(s, r) :: acc  )
Chengsong
parents: 89
diff changeset
   356
  }
Chengsong
parents: 89
diff changeset
   357
  def re_close(l1: List[ARexp], l2: List[ARexp], re_init: ARexp): ARexp = l1 match {
Chengsong
parents: 89
diff changeset
   358
    case Nil => re_init
Chengsong
parents: 89
diff changeset
   359
    case c::cs => if(bnullable(c)) re_close(cs, l2.tail, AALTS(List(), List(re_init, fuse(mkepsBC(c), l2.head)) ) ) 
Chengsong
parents: 89
diff changeset
   360
    else re_close(cs, l2.tail, re_init)
Chengsong
parents: 89
diff changeset
   361
  }
92
Chengsong
parents: 91
diff changeset
   362
  //HERE
91
Chengsong
parents: 89
diff changeset
   363
  def closed_string_der(r1: ARexp, r2: ARexp, s: String): ARexp = {
Chengsong
parents: 89
diff changeset
   364
    val l1 = der_seq(r1, s.toList, Nil)
Chengsong
parents: 89
diff changeset
   365
    val l2 = der_seq_rev(r2, s.toList, Nil)
93
Chengsong
parents: 92
diff changeset
   366
    val Re = re_close((l1.reverse).tail, l2.tail, ASEQ(List(), l1.last, l2.head))
91
Chengsong
parents: 89
diff changeset
   367
    print(Re)
Chengsong
parents: 89
diff changeset
   368
    val Comp = bders( s.toList, ASEQ(List(), r1, r2))
Chengsong
parents: 89
diff changeset
   369
    print(Comp  )
Chengsong
parents: 89
diff changeset
   370
    if(Re != Comp){
Chengsong
parents: 89
diff changeset
   371
      println("boooooooooooooooo!joihniuguyfuyftyftyufuyids gheioueghrigdhxilj")
Chengsong
parents: 89
diff changeset
   372
    }
Chengsong
parents: 89
diff changeset
   373
    Re
Chengsong
parents: 89
diff changeset
   374
  }
Chengsong
parents: 89
diff changeset
   375
  def newxp1(){
Chengsong
parents: 89
diff changeset
   376
    val r1 = internalise("ab"|"")
Chengsong
parents: 89
diff changeset
   377
    val r2 = internalise(("b")%)
Chengsong
parents: 89
diff changeset
   378
    val s = "abbbbb"
Chengsong
parents: 89
diff changeset
   379
    val s2= "bbbbb"
Chengsong
parents: 89
diff changeset
   380
    val s3 = "aaaaaa"
Chengsong
parents: 89
diff changeset
   381
    //closed_string_der(r1, r2, s)
Chengsong
parents: 89
diff changeset
   382
    //closed_string_der(r1, r2, s2)
Chengsong
parents: 89
diff changeset
   383
    closed_string_der(r1, r2, s3)
Chengsong
parents: 89
diff changeset
   384
  }
92
Chengsong
parents: 91
diff changeset
   385
Chengsong
parents: 91
diff changeset
   386
  def string_der_test(){
Chengsong
parents: 91
diff changeset
   387
    for(i <- 0 to 10){
Chengsong
parents: 91
diff changeset
   388
      val s = rd_string_gen(alphabet_size, i).toList
Chengsong
parents: 91
diff changeset
   389
      val r = random_struct_gen(2)
Chengsong
parents: 91
diff changeset
   390
      if(ders(s, r) != ders2(s, r)){
Chengsong
parents: 91
diff changeset
   391
        println(i)
Chengsong
parents: 91
diff changeset
   392
        println(s)
Chengsong
parents: 91
diff changeset
   393
        println(r)
Chengsong
parents: 91
diff changeset
   394
        println(ders(s, r))
Chengsong
parents: 91
diff changeset
   395
        println(ders2(s,r))
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   396
        println("neq") 
92
Chengsong
parents: 91
diff changeset
   397
      }
Chengsong
parents: 91
diff changeset
   398
    }
Chengsong
parents: 91
diff changeset
   399
  }
109
Chengsong
parents: 107
diff changeset
   400
  def have_fun(){
Chengsong
parents: 107
diff changeset
   401
    val bis = List(S,S)
Chengsong
parents: 107
diff changeset
   402
    val bits = List(S,S,Z)
Chengsong
parents: 107
diff changeset
   403
    val reg = ("a" | (("a")%) )~("b")
Chengsong
parents: 107
diff changeset
   404
    val res = decode_aux(reg, bis)
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   405
    val result = decode_aux(reg, bis) 
109
Chengsong
parents: 107
diff changeset
   406
    val result1 = decode_aux(reg, List(Z))
Chengsong
parents: 107
diff changeset
   407
    println(res)
Chengsong
parents: 107
diff changeset
   408
    println(result)
Chengsong
parents: 107
diff changeset
   409
    println(bsimp(bders( "a".toList, internalise(reg))))
Chengsong
parents: 107
diff changeset
   410
    println(result1)
Chengsong
parents: 107
diff changeset
   411
  }
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   412
  def finj_test0(c: Char, r: ARexp){
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   413
    val dr = (bder(c, r))
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   414
    val sdr = bsimp(dr)
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   415
    val v = if(bnullable(sdr)) mkeps(erase(sdr)) else mkchr(erase(sdr))
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   416
    val v0 = if(bnullable(sdr)) mkeps(erase(bder(c,r))) else mkchr(erase(bder(c, r)))
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   417
    val v1 = inj(erase(r), c, v0)
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   418
    val v2 = fuzzy_inj(r, c, v)
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   419
    println("regular expression original")
151
Chengsong
parents: 150
diff changeset
   420
    println(aregex_simple_print(r))
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   421
    println("regular expression derivative")
151
Chengsong
parents: 150
diff changeset
   422
    println(aregex_simple_print(dr))
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   423
    println("regular expression simped derivative")
151
Chengsong
parents: 150
diff changeset
   424
    println(aregex_simple_print(sdr))
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   425
    println("simplified value")
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   426
    println(v)
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   427
    println("unsimplified value")
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   428
    println(v0)
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   429
    println("normal injection")
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   430
    println(v1)
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   431
    println("fuzzy injection")
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   432
    println(v2)
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   433
    println("inj "+r+c+v0)
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   434
    println("fuzzy_inj "+r+c+v)
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   435
  }
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   436
  def vunsimp_test(){
151
Chengsong
parents: 150
diff changeset
   437
    /*val bdr = AALTS(
Chengsong
parents: 150
diff changeset
   438
      List(), 
Chengsong
parents: 150
diff changeset
   439
      List(
Chengsong
parents: 150
diff changeset
   440
        AONE(List(S,Z)), 
Chengsong
parents: 150
diff changeset
   441
        ASTAR(List(Z,Z,S), ACHAR(List(),'c')), 
Chengsong
parents: 150
diff changeset
   442
        ASEQ(List(Z), ASTAR(List(S), ACHAR(List(), 'c')), ASTAR(List(S), ACHAR(List(), 'c') )    )
Chengsong
parents: 150
diff changeset
   443
      )    
Chengsong
parents: 150
diff changeset
   444
    )*/
Chengsong
parents: 150
diff changeset
   445
    val bdr = AALTS(List(),List(AALTS(List(Z),List(ASEQ(List(),
Chengsong
parents: 150
diff changeset
   446
    ASEQ(List(),AONE(List(S)),ASTAR(List(),ACHAR(List(),'c'))),ASTAR(List(),ACHAR(List(),'c'))),
Chengsong
parents: 150
diff changeset
   447
     ASEQ(List(Z),AONE(List(S)),ASTAR(List(),ACHAR(List(),'c'))))), AALTS(List(S),List(AONE(List(Z)), AZERO))))
Chengsong
parents: 150
diff changeset
   448
    val dr = erase(bdr)
Chengsong
parents: 150
diff changeset
   449
    val dv = mkeps(dr)
Chengsong
parents: 150
diff changeset
   450
    //val bdr = bder(c, internalise(r))
Chengsong
parents: 150
diff changeset
   451
    //val dv = mkeps(dr)
Chengsong
parents: 150
diff changeset
   452
    println(aregex_simple_print(bdr))
Chengsong
parents: 150
diff changeset
   453
    println(dv)
Chengsong
parents: 150
diff changeset
   454
    val target_vr = bsimp2(bdr, dv)
Chengsong
parents: 150
diff changeset
   455
    println(aregex_simple_print(target_vr._1))
Chengsong
parents: 150
diff changeset
   456
    println(target_vr._2)
Chengsong
parents: 150
diff changeset
   457
    println(vunsimp(bdr, dv)(target_vr._2))
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   458
  }
151
Chengsong
parents: 150
diff changeset
   459
Chengsong
parents: 150
diff changeset
   460
149
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   461
  def main(args: Array[String]) {
6c5920fd02a7 before changes to bsimp2
Chengsong
parents: 148
diff changeset
   462
    vunsimp_test()
148
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   463
  }
c8ef391dd6f7 vunsimp
Chengsong
parents: 123
diff changeset
   464
   
0
Chengsong
parents:
diff changeset
   465
}