Spiral.scala
author Chengsong
Mon, 11 Nov 2019 17:37:24 +0000
changeset 91 4fd50a6844aa
parent 89 a0bcf15a7844
child 92 aaa2f2b52baf
permissions -rwxr-xr-x
got it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Chengsong
parents:
diff changeset
     1
import Element.elem
Chengsong
parents:
diff changeset
     2
import RexpRelated._
Chengsong
parents:
diff changeset
     3
import RexpRelated.Rexp._
Chengsong
parents:
diff changeset
     4
import Partial._
Chengsong
parents:
diff changeset
     5
import BRexp._
Chengsong
parents:
diff changeset
     6
import scala.collection.mutable.ListBuffer
Chengsong
parents:
diff changeset
     7
object Spiral{
Chengsong
parents:
diff changeset
     8
Chengsong
parents:
diff changeset
     9
  val space = elem(" ")
Chengsong
parents:
diff changeset
    10
  val corner = elem("+")
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
  def spiral(nEdges: Int, direction: Int): Element = {
Chengsong
parents:
diff changeset
    13
    if(nEdges == 1)
Chengsong
parents:
diff changeset
    14
      elem("+")
Chengsong
parents:
diff changeset
    15
    else {
Chengsong
parents:
diff changeset
    16
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
Chengsong
parents:
diff changeset
    17
      def verticalBar = elem('|', 1, sp.height)
Chengsong
parents:
diff changeset
    18
      def horizontalBar = elem('-', sp.width, 1)
Chengsong
parents:
diff changeset
    19
      if(direction == 0)
Chengsong
parents:
diff changeset
    20
        (corner beside horizontalBar) above sp//(sp beside space)
Chengsong
parents:
diff changeset
    21
      else if (direction == 1)
Chengsong
parents:
diff changeset
    22
        sp beside (corner above verticalBar)
Chengsong
parents:
diff changeset
    23
      else if (direction == 2)
Chengsong
parents:
diff changeset
    24
        (space beside sp) above (horizontalBar beside corner)
Chengsong
parents:
diff changeset
    25
      else
Chengsong
parents:
diff changeset
    26
        (verticalBar above corner) beside (space above sp)
Chengsong
parents:
diff changeset
    27
    }
Chengsong
parents:
diff changeset
    28
  }
Chengsong
parents:
diff changeset
    29
  val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c')
Chengsong
parents:
diff changeset
    30
  def bregx_tree(r: BRexp): Element = regx_tree(berase(r))
Chengsong
parents:
diff changeset
    31
  def regx_tree(r: Rexp): Element = aregx_tree(internalise(r))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    32
  def annotated_tree(r: ARexp): Element = {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    33
    r match {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    34
      case ACHAR(bs, d) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    35
        //val Some(d) = alphabet.find(f)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    36
        d match {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    37
          case '\n' => elem("\\n")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    38
          case '\t' => elem("\\t")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    39
          case ' ' => elem("space")
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    40
          case d => if(bs.isEmpty)  elem(d.toString) else elem(d.toString++" ") beside elem(bs.toString)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    41
        }   
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    42
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    43
      case AONE(bs) => {
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    44
        if(bs.isEmpty)  elem("ONE") else elem("ONE ") beside elem(bs.toString)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    45
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    46
      case AZERO => {
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    47
        elem("ZERO") 
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    48
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    49
      case ASEQ(bs, r1, r2) => {
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    50
        annotated_binary_print("SEQ", r1, r2, bs)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    51
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    52
      case AALTS(bs, rs) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    53
        //elem("Awaiting completion")
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    54
        annotated_list_print("ALT", rs, bs)  
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    55
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    56
      case ASTAR(bs, r) => {
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
    57
        annotated_list_print("STA", List(r), bs)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    58
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    59
    } 
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    60
  }
0
Chengsong
parents:
diff changeset
    61
  def aregx_tree(r: ARexp): Element = {
Chengsong
parents:
diff changeset
    62
    r match {
Chengsong
parents:
diff changeset
    63
      case ACHAR(bs, d) => {
Chengsong
parents:
diff changeset
    64
        //val Some(d) = alphabet.find(f)
Chengsong
parents:
diff changeset
    65
        d match {
Chengsong
parents:
diff changeset
    66
          case '\n' => elem("\\n")
Chengsong
parents:
diff changeset
    67
          case '\t' => elem("\\t")
Chengsong
parents:
diff changeset
    68
          case ' ' => elem("space")
Chengsong
parents:
diff changeset
    69
          case d => elem(d.toString)
Chengsong
parents:
diff changeset
    70
        }   
Chengsong
parents:
diff changeset
    71
      }
Chengsong
parents:
diff changeset
    72
      case AONE(bs) => {
Chengsong
parents:
diff changeset
    73
        elem("ONE")
Chengsong
parents:
diff changeset
    74
      }
Chengsong
parents:
diff changeset
    75
      case AZERO => {
Chengsong
parents:
diff changeset
    76
        elem("ZERO")
Chengsong
parents:
diff changeset
    77
      }
Chengsong
parents:
diff changeset
    78
      case ASEQ(bs, r1, r2) => {
Chengsong
parents:
diff changeset
    79
        binary_print("SEQ", r1, r2)
Chengsong
parents:
diff changeset
    80
      }
Chengsong
parents:
diff changeset
    81
      case AALTS(bs, rs) => {
Chengsong
parents:
diff changeset
    82
        //elem("Awaiting completion")
Chengsong
parents:
diff changeset
    83
        list_print("ALT", rs)
Chengsong
parents:
diff changeset
    84
      }
Chengsong
parents:
diff changeset
    85
      case ASTAR(bs, r) => {
Chengsong
parents:
diff changeset
    86
        list_print("STA", List(r))
Chengsong
parents:
diff changeset
    87
      }
Chengsong
parents:
diff changeset
    88
    }
Chengsong
parents:
diff changeset
    89
  }
Chengsong
parents:
diff changeset
    90
  val port = elem(" └-")
Chengsong
parents:
diff changeset
    91
  def list_print(name: String, rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
    92
    rs match {
Chengsong
parents:
diff changeset
    93
      case r::Nil => {
Chengsong
parents:
diff changeset
    94
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
    95
        val head = elem(name)
Chengsong
parents:
diff changeset
    96
        (head left_align (port up_align pref) ) 
Chengsong
parents:
diff changeset
    97
      }
Chengsong
parents:
diff changeset
    98
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
    99
        binary_print(name, r2, r1)
Chengsong
parents:
diff changeset
   100
      }
Chengsong
parents:
diff changeset
   101
      case r::rs1 => {
Chengsong
parents:
diff changeset
   102
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
   103
        val head = elem(name)
Chengsong
parents:
diff changeset
   104
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   105
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   106
          (head left_align ((port above link) beside pref)) left_align tail_print(rs1)    
Chengsong
parents:
diff changeset
   107
        }
Chengsong
parents:
diff changeset
   108
        else{
Chengsong
parents:
diff changeset
   109
          (head left_align (port beside pref) ) left_align tail_print(rs1)
Chengsong
parents:
diff changeset
   110
        }
Chengsong
parents:
diff changeset
   111
      }
Chengsong
parents:
diff changeset
   112
    }
Chengsong
parents:
diff changeset
   113
  }
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   114
    def annotated_list_print(name: String, rs: List[ARexp], bs: List[Bit]): Element = {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   115
    rs match {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   116
      case r::Nil => {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   117
        val pref = annotated_tree(r)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   118
        val head = if(bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   119
        (head left_align (port up_align pref) ) 
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   120
      }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   121
      case r2::r1::Nil => {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   122
        annotated_binary_print(name, r2, r1, bs)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   123
      }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   124
      case r::rs1 => {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   125
        val pref = annotated_tree(r)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   126
        val head = if (bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   127
        if (pref.height > 1){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   128
          val link = elem('|', 1, pref.height - 1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   129
          (head left_align ((port above link) beside pref)) left_align annotated_tail_print(rs1)    
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   130
        }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   131
        else{
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   132
          (head left_align (port beside pref) ) left_align annotated_tail_print(rs1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   133
        }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   134
      }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   135
    }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   136
  }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   137
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   138
  def annotated_tail_print(rs: List[ARexp]): Element = {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   139
    rs match {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   140
      case r2::r1::Nil => {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   141
        val pref = annotated_tree(r2)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   142
        val suff = annotated_tree(r1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   143
        if (pref.height > 1){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   144
          val link = elem('|', 1, pref.height - 1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   145
          ((port above link) beside pref) left_align (port up_align suff)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   146
        }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   147
        else{
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   148
          (port beside pref) left_align (port up_align suff)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   149
        } 
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   150
      }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   151
      case r2::rs1 => {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   152
        val pref = annotated_tree(r2)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   153
        
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   154
        if (pref.height > 1){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   155
          val link = elem('|', 1, pref.height - 1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   156
          ((port above link) beside pref) left_align annotated_tail_print(rs1)//(port up_align tail_print(rs1) )
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   157
        }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   158
        else{
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   159
          (port beside pref) left_align annotated_tail_print(rs1)//(port up_align tail_print(rs1))
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   160
        } 
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   161
        //pref left_align tail_print(rs1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   162
      }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   163
    }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   164
  }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   165
0
Chengsong
parents:
diff changeset
   166
  def tail_print(rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
   167
    rs match {
Chengsong
parents:
diff changeset
   168
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
   169
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
   170
        val suff = aregx_tree(r1)
Chengsong
parents:
diff changeset
   171
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   172
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   173
          ((port above link) beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   174
        }
Chengsong
parents:
diff changeset
   175
        else{
Chengsong
parents:
diff changeset
   176
          (port beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   177
        } 
Chengsong
parents:
diff changeset
   178
      }
Chengsong
parents:
diff changeset
   179
      case r2::rs1 => {
Chengsong
parents:
diff changeset
   180
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
   181
        
Chengsong
parents:
diff changeset
   182
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   183
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   184
          ((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) )
Chengsong
parents:
diff changeset
   185
        }
Chengsong
parents:
diff changeset
   186
        else{
Chengsong
parents:
diff changeset
   187
          (port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1))
Chengsong
parents:
diff changeset
   188
        } 
Chengsong
parents:
diff changeset
   189
        //pref left_align tail_print(rs1)
Chengsong
parents:
diff changeset
   190
      }
Chengsong
parents:
diff changeset
   191
    }
Chengsong
parents:
diff changeset
   192
  }
Chengsong
parents:
diff changeset
   193
Chengsong
parents:
diff changeset
   194
  def binary_print(name: String, r1: ARexp, r2: ARexp): Element = {
Chengsong
parents:
diff changeset
   195
    val pref = aregx_tree(r1)
Chengsong
parents:
diff changeset
   196
    val suff = aregx_tree(r2)
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   197
    val head = elem(name) 
0
Chengsong
parents:
diff changeset
   198
    if (pref.height > 1){
Chengsong
parents:
diff changeset
   199
      val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   200
      (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   201
    }
Chengsong
parents:
diff changeset
   202
    else{
Chengsong
parents:
diff changeset
   203
      (head left_align (port beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   204
    }
Chengsong
parents:
diff changeset
   205
  }
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   206
  def annotated_binary_print(name: String, r1: ARexp, r2: ARexp, bs: List[Bit]): Element = {
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   207
    val pref = annotated_tree(r1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   208
    val suff = annotated_tree(r2)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   209
    val head = if (bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   210
    if (pref.height > 1){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   211
      val link = elem('|', 1, pref.height - 1)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   212
      (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   213
    }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   214
    else{
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   215
      (head left_align (port beside pref) ) left_align (port up_align suff)
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   216
    }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   217
  }
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   218
0
Chengsong
parents:
diff changeset
   219
  val arr_of_size = ListBuffer.empty[Int]
2
cf169411b771 more comments
Chengsong
parents: 1
diff changeset
   220
0
Chengsong
parents:
diff changeset
   221
  def pC(r: Rexp): Set[Rexp] = {//PD's companion
Chengsong
parents:
diff changeset
   222
    r match {
Chengsong
parents:
diff changeset
   223
      case SEQ(r1, r2) => pC(r2)
Chengsong
parents:
diff changeset
   224
      case ALTS(rs) => rs.flatMap(a => pC(a) ).toSet
Chengsong
parents:
diff changeset
   225
      case CHAR(c) => Set(r)
Chengsong
parents:
diff changeset
   226
      case r => Set()
Chengsong
parents:
diff changeset
   227
    }
Chengsong
parents:
diff changeset
   228
  }
Chengsong
parents:
diff changeset
   229
  def illustration(r: Rexp, s: String){
Chengsong
parents:
diff changeset
   230
    var i_like_imperative_style = internalise(r)
Chengsong
parents:
diff changeset
   231
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   232
    for (i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   233
      val der_res =  bder(all_chars(i), i_like_imperative_style)
Chengsong
parents:
diff changeset
   234
      val simp_res = bsimp(der_res)
Chengsong
parents:
diff changeset
   235
      println("The original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.")
Chengsong
parents:
diff changeset
   236
      println(aregx_tree(i_like_imperative_style) up_align aregx_tree(der_res) up_align aregx_tree(simp_res))
Chengsong
parents:
diff changeset
   237
      //println(asize(i_like_imperative_style), asize(der_res), asize(simp_res))
Chengsong
parents:
diff changeset
   238
      arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   239
      //println(asize(simp_res), asize(simp_res) / arr_of_size(0) )
Chengsong
parents:
diff changeset
   240
      i_like_imperative_style = simp_res
Chengsong
parents:
diff changeset
   241
    }
Chengsong
parents:
diff changeset
   242
    arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   243
  }
Chengsong
parents:
diff changeset
   244
  val ran = scala.util.Random
Chengsong
parents:
diff changeset
   245
  var alphabet_size = 3
Chengsong
parents:
diff changeset
   246
  def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
Chengsong
parents:
diff changeset
   247
    if(depth == 1){
Chengsong
parents:
diff changeset
   248
      ((ran.nextInt(4) + 97).toChar).toString
Chengsong
parents:
diff changeset
   249
    }
Chengsong
parents:
diff changeset
   250
    else if(star){
Chengsong
parents:
diff changeset
   251
      STAR(balanced_seq_star_gen(depth - 1, false))
Chengsong
parents:
diff changeset
   252
    }
Chengsong
parents:
diff changeset
   253
    else{
Chengsong
parents:
diff changeset
   254
      SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
Chengsong
parents:
diff changeset
   255
    }
Chengsong
parents:
diff changeset
   256
  }
Chengsong
parents:
diff changeset
   257
  def max(i: Int, j: Int): Int = {
Chengsong
parents:
diff changeset
   258
    if(i > j)
Chengsong
parents:
diff changeset
   259
      i
Chengsong
parents:
diff changeset
   260
    else 
Chengsong
parents:
diff changeset
   261
      j
Chengsong
parents:
diff changeset
   262
  }
Chengsong
parents:
diff changeset
   263
  def random_struct_gen(depth:Int): Rexp = {
Chengsong
parents:
diff changeset
   264
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   265
    val dice2 = ran.nextInt(3)
Chengsong
parents:
diff changeset
   266
    (dice, depth) match {
Chengsong
parents:
diff changeset
   267
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   268
      case (0, i) => STAR(random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   269
      case (1, i) => SEQ(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   270
      case (2, i) => ALTS( List(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2))) )
Chengsong
parents:
diff changeset
   271
    }
Chengsong
parents:
diff changeset
   272
  }
Chengsong
parents:
diff changeset
   273
  def balanced_struct_gen(depth: Int): Rexp = {
Chengsong
parents:
diff changeset
   274
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   275
    (dice, depth) match {
Chengsong
parents:
diff changeset
   276
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   277
      case (0, i) => STAR(random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   278
      case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   279
      case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) )
Chengsong
parents:
diff changeset
   280
    }
Chengsong
parents:
diff changeset
   281
  }
6
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   282
  def random_pool(n: Int, d: Int) : Stream[Rexp] = n match {
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   283
    case 0 => (for (i <- 1 to 10) yield balanced_struct_gen(d)).toStream
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   284
    case n => {  
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   285
      val rs = random_pool(n - 1, d)
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   286
      rs #:::
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   287
      (for (i <- (1 to 10).toStream) yield balanced_struct_gen(d)) #::: 
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   288
      (for (i <- (1 to 10).toStream) yield random_struct_gen(d))
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   289
    }
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   290
  }
26b40a985622 random test failed
Chengsong
parents: 5
diff changeset
   291
0
Chengsong
parents:
diff changeset
   292
  def rd_string_gen(alp_size: Int, len: Int): String = {
Chengsong
parents:
diff changeset
   293
    if( len > 0)
Chengsong
parents:
diff changeset
   294
      ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
Chengsong
parents:
diff changeset
   295
    else
Chengsong
parents:
diff changeset
   296
      ((ran.nextInt(alp_size) + 97).toChar).toString
Chengsong
parents:
diff changeset
   297
  }
Chengsong
parents:
diff changeset
   298
  def plot(b: List[Int]){
Chengsong
parents:
diff changeset
   299
    println(b(0),b.max)
Chengsong
parents:
diff changeset
   300
Chengsong
parents:
diff changeset
   301
  }
Chengsong
parents:
diff changeset
   302
  def dep_exp(depth: List[Int]){
Chengsong
parents:
diff changeset
   303
    for(i <- depth){
Chengsong
parents:
diff changeset
   304
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   305
      val s = rd_string_gen(alphabet_size, (i-8)*(i-8)+10)
Chengsong
parents:
diff changeset
   306
      val r = random_struct_gen(i)
Chengsong
parents:
diff changeset
   307
      println("depth: "+i)
Chengsong
parents:
diff changeset
   308
      illustration(r, s) //"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb") 
Chengsong
parents:
diff changeset
   309
      //println("depth: " + i + " general stats:"+ arr_of_size(0), arr_of_size.max, arr_of_size.max/arr_of_size(0))
Chengsong
parents:
diff changeset
   310
      //println("x y label alignment")
Chengsong
parents:
diff changeset
   311
      /*for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   312
        if(s(i) == '\n')
Chengsong
parents:
diff changeset
   313
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
Chengsong
parents:
diff changeset
   314
        else if(s(i) ==  ' ')
Chengsong
parents:
diff changeset
   315
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
Chengsong
parents:
diff changeset
   316
        else
Chengsong
parents:
diff changeset
   317
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
Chengsong
parents:
diff changeset
   318
      }*/
Chengsong
parents:
diff changeset
   319
      //println(s.length + " " + arr_of_size(s.length) + " ]" + " -140")
Chengsong
parents:
diff changeset
   320
    }
Chengsong
parents:
diff changeset
   321
  }
Chengsong
parents:
diff changeset
   322
  def case_study(ss: List[String], r: Rexp){
Chengsong
parents:
diff changeset
   323
    for(s <- ss){
Chengsong
parents:
diff changeset
   324
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   325
      illustration(r, s)
Chengsong
parents:
diff changeset
   326
      println("x y label alignment")
Chengsong
parents:
diff changeset
   327
      for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   328
        if(s(i) == '\n')
Chengsong
parents:
diff changeset
   329
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
Chengsong
parents:
diff changeset
   330
        else if(s(i) ==  ' ')
Chengsong
parents:
diff changeset
   331
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
Chengsong
parents:
diff changeset
   332
        else
Chengsong
parents:
diff changeset
   333
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
Chengsong
parents:
diff changeset
   334
      }
Chengsong
parents:
diff changeset
   335
    }
Chengsong
parents:
diff changeset
   336
  }
Chengsong
parents:
diff changeset
   337
  def star_gen(dp: Int): Rexp = {
Chengsong
parents:
diff changeset
   338
    if(dp > 0)
Chengsong
parents:
diff changeset
   339
      STAR(star_gen(dp - 1))
Chengsong
parents:
diff changeset
   340
    else
Chengsong
parents:
diff changeset
   341
      "a"
Chengsong
parents:
diff changeset
   342
  }
Chengsong
parents:
diff changeset
   343
  def strs_gen(len: Int, num: Int): List[String] = {
Chengsong
parents:
diff changeset
   344
    if(num > 0){
Chengsong
parents:
diff changeset
   345
      rd_string_gen(3, len)::strs_gen(len, num - 1)
Chengsong
parents:
diff changeset
   346
    }
Chengsong
parents:
diff changeset
   347
    else{
Chengsong
parents:
diff changeset
   348
      Nil
Chengsong
parents:
diff changeset
   349
    }
Chengsong
parents:
diff changeset
   350
  }
Chengsong
parents:
diff changeset
   351
  def regx_print(r: Rexp): String = {
Chengsong
parents:
diff changeset
   352
    r match {
Chengsong
parents:
diff changeset
   353
      case ZERO =>
Chengsong
parents:
diff changeset
   354
        "ZERO"
Chengsong
parents:
diff changeset
   355
      case CHAR(c) => {
Chengsong
parents:
diff changeset
   356
         //val Some(c) = alphabet.find(f)
Chengsong
parents:
diff changeset
   357
         "\"" + c.toString + "\""
Chengsong
parents:
diff changeset
   358
      }
Chengsong
parents:
diff changeset
   359
      case ONE => {
Chengsong
parents:
diff changeset
   360
        "ONE"
Chengsong
parents:
diff changeset
   361
      }
Chengsong
parents:
diff changeset
   362
      case ALTS(rs) => {
Chengsong
parents:
diff changeset
   363
        "ALTS(List("+(rs.map(regx_print)).foldLeft("")((a, b) => if(a == "") b else a + "," + b)+"))"
Chengsong
parents:
diff changeset
   364
      }
Chengsong
parents:
diff changeset
   365
      case SEQ(r1, r2) => {
Chengsong
parents:
diff changeset
   366
        "SEQ(" + regx_print(r1) + "," + regx_print(r2) + ")"
Chengsong
parents:
diff changeset
   367
      }
Chengsong
parents:
diff changeset
   368
      case STAR(r) => {
Chengsong
parents:
diff changeset
   369
        "STAR(" + regx_print(r) + ")"
Chengsong
parents:
diff changeset
   370
      }
Chengsong
parents:
diff changeset
   371
    }
Chengsong
parents:
diff changeset
   372
  }
Chengsong
parents:
diff changeset
   373
  val mkst = "abcdefghijklmnopqrstuvwxyz"
Chengsong
parents:
diff changeset
   374
  def weak_sub_check(r: Rexp, s: String, i: Int, f: (List[Rexp], Set[Rexp]) => Boolean){
Chengsong
parents:
diff changeset
   375
    //we first compute pders over the set of all strings on the alphabet
Chengsong
parents:
diff changeset
   376
    val pd = pderas(Set(r), i + 4)
Chengsong
parents:
diff changeset
   377
    //then "b-internalise" the regular expression into a brexp(this is essentially 
Chengsong
parents:
diff changeset
   378
    //attaching a bit Z to every alts to signify that they come from the original regular expression)
Chengsong
parents:
diff changeset
   379
    var old = brternalise(r)
Chengsong
parents:
diff changeset
   380
    //this is for comparison between normal simp and the weakened version of simp
Chengsong
parents:
diff changeset
   381
    //normal simp will be performed on syncold
Chengsong
parents:
diff changeset
   382
    //weakend simp will be performed on old
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   383
    var syncold = internalise(r)
0
Chengsong
parents:
diff changeset
   384
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   385
    for (i <- 0 to s.length - 1){
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   386
      val syncder_res = bder(all_chars(i), syncold)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   387
      val syncsimp_res = super_bsimp(syncder_res)
0
Chengsong
parents:
diff changeset
   388
      //see brder for detailed explanation
Chengsong
parents:
diff changeset
   389
      //just changes bit Z to S when deriving an ALTS, 
Chengsong
parents:
diff changeset
   390
      //signifying that the structure has been "touched" and
Chengsong
parents:
diff changeset
   391
      //therefore able to be spilled in the bspill function
Chengsong
parents:
diff changeset
   392
      val der_res =  brder(all_chars(i), old)
Chengsong
parents:
diff changeset
   393
      val simp_res = br_simp(der_res)
Chengsong
parents:
diff changeset
   394
      val anatomy = bspill(simp_res)
Chengsong
parents:
diff changeset
   395
      //track if the number of regular expressions exceeds those in the PD set(remember PD means the pders over A*)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   396
      if(f(List(berase(simp_res)), pd)  == false ){
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   397
        println(size(erase(syncsimp_res)))
0
Chengsong
parents:
diff changeset
   398
        println(size(berase(simp_res)))
3
f15dccc42c7b removing PRED
Chengsong
parents: 2
diff changeset
   399
        println(bregx_tree(simp_res))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   400
        println(s)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   401
        println(i)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   402
        println(r)
0
Chengsong
parents:
diff changeset
   403
        println(anatomy.map(size).sum)
Chengsong
parents:
diff changeset
   404
        println(pd.map(size).sum)
Chengsong
parents:
diff changeset
   405
      }  
Chengsong
parents:
diff changeset
   406
      old = simp_res
Chengsong
parents:
diff changeset
   407
      syncold = syncsimp_res
Chengsong
parents:
diff changeset
   408
    }
Chengsong
parents:
diff changeset
   409
  }
Chengsong
parents:
diff changeset
   410
  def inclusion_truth(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = {
Chengsong
parents:
diff changeset
   411
    val aset = anatomy.toSet
Chengsong
parents:
diff changeset
   412
    if(aset subsetOf pd){
Chengsong
parents:
diff changeset
   413
      true
Chengsong
parents:
diff changeset
   414
    }
Chengsong
parents:
diff changeset
   415
    else{
Chengsong
parents:
diff changeset
   416
      println("inclusion not true")
Chengsong
parents:
diff changeset
   417
      false
Chengsong
parents:
diff changeset
   418
    }
Chengsong
parents:
diff changeset
   419
  }
Chengsong
parents:
diff changeset
   420
  def size_comp(anatomy: List[Rexp], pd: Set[Rexp]):Boolean = {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); true}
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   421
  def size_expansion_rate(r: List[Rexp], pd: Set[Rexp]): Boolean = if(size(r(0)) > (pd.map(size).sum) * 3 ) { false }else {true}
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   422
  def ders_simp(r: ARexp, s: List[Char]): ARexp = {
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   423
    s match {
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   424
      case Nil => r 
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   425
      case c::cs => ders_simp(bsimp(bder(c, r)), cs)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   426
    }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   427
  }
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   428
  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
   429
  val str_panda = "ccccb"
0
Chengsong
parents:
diff changeset
   430
  def check_all(){
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   431
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   432
        weak_sub_check(big_panda, str_panda, 6, size_expansion_rate)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   433
0
Chengsong
parents:
diff changeset
   434
  }
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   435
  //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
   436
  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
   437
    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
   438
    case ASEQ(bs, r1, r2) => ASEQ(bs, pushbits(r1), pushbits(r2))
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   439
    case r => r
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   440
  }
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   441
  def correctness_proof_convenient_path(){
11
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   442
    for(i <- 1 to 10000)
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   443
    {
15
Chengsong
parents: 14
diff changeset
   444
        val s = "abab"//rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3)
Chengsong
parents: 14
diff changeset
   445
        val r = ("ab"| SEQ(ONE, "ab"))//internalise(random_struct_gen(4))//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")))) //random_struct_gen(7)
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   446
        for(j <- 0 to s.length - 1){
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   447
          val ss = s.slice(0, j+ 1)
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   448
          val nangao = ders_simp(internalise(r), ss.toList)
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   449
          val easy = bsimp(bders(ss.toList, internalise(r)))
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   450
          if(!(nangao == easy || pushbits(nangao) == (easy))){
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   451
            println(j)
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   452
            println("not equal")
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   453
            println("string")
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   454
            println(ss)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   455
            println("original regex")
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   456
            println(regx_tree(r))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   457
            println("regex after ders simp")
10
2b95bcd2ac73 exp and proof
Chengsong
parents: 8
diff changeset
   458
            println(annotated_tree(nangao))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   459
            println("regex after ders")
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   460
            println(annotated_tree(bders(ss.toList, internalise(r))))//flats' fuse when opening up AALTS causes the difference
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   461
            println("regex after ders and then a single simp")
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   462
            println(annotated_tree(easy))
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   463
          }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   464
        }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   465
    }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   466
  }
13
4868c26268aa test of
Chengsong
parents: 12
diff changeset
   467
  /*    if(bts == cdbts){//test of equality code v = retrieve internalise(r) v if |- v : r
4868c26268aa test of
Chengsong
parents: 12
diff changeset
   468
      println(bts)
4868c26268aa test of
Chengsong
parents: 12
diff changeset
   469
      println(cdbts)
4868c26268aa test of
Chengsong
parents: 12
diff changeset
   470
      println("code v = retrieve internalise(r) v if |- v : r")
4868c26268aa test of
Chengsong
parents: 12
diff changeset
   471
    }
14
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   472
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   473
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   474
        val r_der_s = bders(st.toList, rg)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   475
    println(aregx_tree(r_der_s))
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   476
    val bts = retrieve(r_der_s, unsimplified_vl)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   477
    val cdbts = code(unsimplified_vl)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   478
    val simprg = bsimp(r_der_s)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   479
    val frectv = decode(erase(simprg), cdbts)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   480
    val simpbts = retrieve(simprg, frectv)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   481
    if(bts == simpbts){
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   482
      println("retrieve r v = retrieve (bsimp r) (decode bsimp(r) code(v))")
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   483
      println("bits:")
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   484
      //println(bts)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   485
      println(simpbts)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   486
      println("v = ")
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   487
      println(unsimplified_vl)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   488
      println("frect v = ")
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   489
      println(frectv)
610f14009c0b the property
Chengsong
parents: 13
diff changeset
   490
    }
17
Chengsong
parents: 16
diff changeset
   491
    *///KH8W5BXL
Chengsong
parents: 16
diff changeset
   492
  def nice_lex(r: Rexp, s: List[Char], ar: ARexp) : Val = s match {
Chengsong
parents: 16
diff changeset
   493
    case Nil => 
Chengsong
parents: 16
diff changeset
   494
    if (nullable(r)){
Chengsong
parents: 16
diff changeset
   495
      val vr = mkeps(r)
Chengsong
parents: 16
diff changeset
   496
      val bits1 = retrieve(ar, vr)
Chengsong
parents: 16
diff changeset
   497
      val av = bsimp2(ar, vr)
Chengsong
parents: 16
diff changeset
   498
      val bits2 = retrieve(av._1, av._2)
Chengsong
parents: 16
diff changeset
   499
      if(bits1 != bits2) throw new Exception("bsimp2 does not work")
Chengsong
parents: 16
diff changeset
   500
      vr
Chengsong
parents: 16
diff changeset
   501
    }  
Chengsong
parents: 16
diff changeset
   502
    else throw new Exception("Not matched")
Chengsong
parents: 16
diff changeset
   503
    case c::cs => { 
Chengsong
parents: 16
diff changeset
   504
      val vr = inj(r, c, nice_lex(der(c, r), cs, bder(c, ar)));
Chengsong
parents: 16
diff changeset
   505
      val bits1 = retrieve(ar, vr);
Chengsong
parents: 16
diff changeset
   506
      val av = bsimp2(ar, vr);
Chengsong
parents: 16
diff changeset
   507
      val bits2 = retrieve(av._1, av._2);
Chengsong
parents: 16
diff changeset
   508
      if(bits1 != bits2) throw new Exception("bsimp2 does not work");
Chengsong
parents: 16
diff changeset
   509
      vr 
Chengsong
parents: 16
diff changeset
   510
    }
Chengsong
parents: 16
diff changeset
   511
  }
Chengsong
parents: 16
diff changeset
   512
  def test_bsimp2(){
Chengsong
parents: 16
diff changeset
   513
    for(i <- 1 to 1000){
Chengsong
parents: 16
diff changeset
   514
      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
   515
      val st = rd_string_gen(3, 4)
Chengsong
parents: 16
diff changeset
   516
      val a = internalise(rg)
Chengsong
parents: 16
diff changeset
   517
      val final_derivative = ders(st.toList, rg)
Chengsong
parents: 16
diff changeset
   518
      if(nullable(final_derivative))
Chengsong
parents: 16
diff changeset
   519
        nice_lex(rg, st.toList, a)
Chengsong
parents: 16
diff changeset
   520
    }
Chengsong
parents: 16
diff changeset
   521
  }
15
Chengsong
parents: 14
diff changeset
   522
17
Chengsong
parents: 16
diff changeset
   523
  def neat_retrieve(){
Chengsong
parents: 16
diff changeset
   524
    for(i <- 1 to 1000){
Chengsong
parents: 16
diff changeset
   525
      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
   526
      val st = rd_string_gen(3, 5)
Chengsong
parents: 16
diff changeset
   527
      val final_derivative = ders(st.toList, erase(rg))
Chengsong
parents: 16
diff changeset
   528
      if(nullable(final_derivative)){
Chengsong
parents: 16
diff changeset
   529
        val unsimplified_vl = mkeps(final_derivative)
Chengsong
parents: 16
diff changeset
   530
        val arexp_for_retrieve = bders( st.toList, rg)
Chengsong
parents: 16
diff changeset
   531
        val simp_ar_vl = bsimp2(arexp_for_retrieve, unsimplified_vl)
Chengsong
parents: 16
diff changeset
   532
        val bits1 = retrieve(arexp_for_retrieve, unsimplified_vl)
Chengsong
parents: 16
diff changeset
   533
        val bits2 = retrieve(simp_ar_vl._1, simp_ar_vl._2)
Chengsong
parents: 16
diff changeset
   534
        if(bits1 != bits2){
Chengsong
parents: 16
diff changeset
   535
          println("nOOOOOOOOOO!")
15
Chengsong
parents: 14
diff changeset
   536
        }
Chengsong
parents: 14
diff changeset
   537
      }
11
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   538
    }
17
Chengsong
parents: 16
diff changeset
   539
  }
Chengsong
parents: 16
diff changeset
   540
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   541
  def radical_correctness(){
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   542
    enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   543
    random_pool(1, 5).map(tests_blexer_simp(strs(5, "abc"))).toSet
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   544
  }
15
Chengsong
parents: 14
diff changeset
   545
  def christian_def(){
Chengsong
parents: 14
diff changeset
   546
    val r = ALTS(List(SEQ(ZERO,CHAR('b')), ONE))
Chengsong
parents: 14
diff changeset
   547
    val v = Right(Empty)
Chengsong
parents: 14
diff changeset
   548
    val a = internalise(r)
17
Chengsong
parents: 16
diff changeset
   549
    val a_v = bsimp2(a,v)
15
Chengsong
parents: 14
diff changeset
   550
    println(s"Testing ${r} and ${v}")
Chengsong
parents: 14
diff changeset
   551
    println(s"internalise(r) = ${a}")
17
Chengsong
parents: 16
diff changeset
   552
    println(s"a_v = ${a_v}")
15
Chengsong
parents: 14
diff changeset
   553
    val bs1 = retrieve(a, v)
Chengsong
parents: 14
diff changeset
   554
    println(bs1)
17
Chengsong
parents: 16
diff changeset
   555
    println(s"as = ${a_v._1}")
15
Chengsong
parents: 14
diff changeset
   556
    //val bs2 = retrieve(as, decode(erase(as), bs1))
17
Chengsong
parents: 16
diff changeset
   557
    val bs3 = retrieve(a_v._1, a_v._2)//decode(erase(as), bs1) does not work
Chengsong
parents: 16
diff changeset
   558
    //println(decode(erase(as), bs1))
Chengsong
parents: 16
diff changeset
   559
    println(s"bs1=${bs1}, bs3=${bs3}")
Chengsong
parents: 16
diff changeset
   560
    //println(decode(erase(a_v._1), bs3))
15
Chengsong
parents: 14
diff changeset
   561
  }
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   562
  def christian_def2(){
89
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   563
    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
   564
    //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
   565
    val unsimp = bsimp(bder('c',a))
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   566
    val simped = bsimp(bder('c', bsimp(a))           )
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   567
    println(bsimp(a))
89
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   568
    if(unsimp != simped){
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   569
      println(s"bsimp(bder c r) = ${unsimp}, whereas bsimp(bder c bsimp r) = ${simped}")
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   570
    }
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   571
  }
15
Chengsong
parents: 14
diff changeset
   572
  def essence_posix(){
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   573
    //val s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"//rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3)
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   574
    val s0 = "a"
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   575
    val r = SEQ(STAR(ALT("a", "aa")), "b")//internalise(random_struct_gen(4))//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")))) //random_struct_gen(7)
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   576
    for(i <- 1 to 40){
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   577
      val s = s0*i
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   578
      //printf("%d  %d\n",i, size(ders(s.toList, r)))
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   579
      printf("%d  %d\n",i, asize(ders_simp( internalise(r), s.toList)))
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   580
      //println(asize(ders_simp( internalise(r), s.toList)))
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   581
    }
15
Chengsong
parents: 14
diff changeset
   582
  }
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   583
87
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   584
  def speed_test(){
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   585
    val s0 = "a"*1000
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   586
    val r = SEQ(STAR("a"), "b")
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   587
    for(i <- 1 to 30){
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   588
      val s = s0*i
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   589
      val start = System.nanoTime()
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   590
      try{
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   591
        blex_simp(internalise(r), s.toList)
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   592
      }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   593
      catch{
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   594
        case x: Exception =>
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   595
      }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   596
      val end = System.nanoTime()
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   597
      printf("%d  %f\n",i, (end - start)/1.0e9)
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   598
    }
9c52c21b5db3 changes to report
Chengsong
parents: 75
diff changeset
   599
  }
91
Chengsong
parents: 89
diff changeset
   600
  /*
Chengsong
parents: 89
diff changeset
   601
  lemma retrieve_encode_STARS:
Chengsong
parents: 89
diff changeset
   602
  assumes "∀v∈set vs. ⊨ v : r ∧ code v = retrieve (intern r) v"
Chengsong
parents: 89
diff changeset
   603
  shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)"
Chengsong
parents: 89
diff changeset
   604
  */
Chengsong
parents: 89
diff changeset
   605
  def retrieve_encode_STARS(){
Chengsong
parents: 89
diff changeset
   606
    val r =  ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('b')) ) 
Chengsong
parents: 89
diff changeset
   607
    //val v =  Stars(List(Sequ(Chr('a'), Chr('b')),  Chr('a')) )
Chengsong
parents: 89
diff changeset
   608
    val v1 = Right(Sequ(Chr('a'), Chr('b')))
Chengsong
parents: 89
diff changeset
   609
    val v2 = Left(Chr('a'))
Chengsong
parents: 89
diff changeset
   610
    val compressed1 = code(v1)
Chengsong
parents: 89
diff changeset
   611
    val compressed2 = code(v2)
Chengsong
parents: 89
diff changeset
   612
    val xompressed1 = retrieve(internalise(r), v1)
Chengsong
parents: 89
diff changeset
   613
    val xompressed2 = retrieve(internalise(r), v2)
Chengsong
parents: 89
diff changeset
   614
    println(compressed1, compressed2, xompressed1, xompressed2)
Chengsong
parents: 89
diff changeset
   615
    val v = Stars(List(v1, v2))
Chengsong
parents: 89
diff changeset
   616
    val compressed = code(v)
Chengsong
parents: 89
diff changeset
   617
    val a = ASTAR(List(), internalise(r))
Chengsong
parents: 89
diff changeset
   618
    val xompressed = retrieve(a, v)
Chengsong
parents: 89
diff changeset
   619
    println(compressed, xompressed)
Chengsong
parents: 89
diff changeset
   620
  }
Chengsong
parents: 89
diff changeset
   621
  //what does contains7 do?
Chengsong
parents: 89
diff changeset
   622
  //it causes exception
Chengsong
parents: 89
diff changeset
   623
  //relation between retreive, bder and injection
Chengsong
parents: 89
diff changeset
   624
  // a match error occurs when doing the injection
Chengsong
parents: 89
diff changeset
   625
  def contains7(){
Chengsong
parents: 89
diff changeset
   626
    val r = STAR( SEQ(CHAR('a'),CHAR('b')) )
Chengsong
parents: 89
diff changeset
   627
    val v = Sequ(Chr('b'), Stars(List(Sequ(Chr('a'), Chr('b')))))
Chengsong
parents: 89
diff changeset
   628
    val a = internalise(r)
Chengsong
parents: 89
diff changeset
   629
    val c = 'a'
Chengsong
parents: 89
diff changeset
   630
    val v_aug = inj(r, c, v)
Chengsong
parents: 89
diff changeset
   631
    println("bder c r:")
Chengsong
parents: 89
diff changeset
   632
    println(bder(c, a))
Chengsong
parents: 89
diff changeset
   633
    println("r:")
Chengsong
parents: 89
diff changeset
   634
    println(a)
Chengsong
parents: 89
diff changeset
   635
    println("bits they can both produce:")
Chengsong
parents: 89
diff changeset
   636
    println(retrieve(a, v_aug))
Chengsong
parents: 89
diff changeset
   637
  }
Chengsong
parents: 89
diff changeset
   638
  def der_seq(r:ARexp, s: List[Char],acc: List[ARexp]) : List[ARexp] = s match{
Chengsong
parents: 89
diff changeset
   639
    case Nil => acc ::: List(r)
Chengsong
parents: 89
diff changeset
   640
    case c::cs => der_seq(bder(c, r), cs, acc ::: List(r))
Chengsong
parents: 89
diff changeset
   641
  }
Chengsong
parents: 89
diff changeset
   642
  def der_seq_rev(r:ARexp, s: List[Char], acc: List[ARexp]): List[ARexp] = s match{
Chengsong
parents: 89
diff changeset
   643
    case Nil => r::acc
Chengsong
parents: 89
diff changeset
   644
    case c::cs =>der_seq_rev(r, cs, bders(s, r) :: acc  )
Chengsong
parents: 89
diff changeset
   645
  }
Chengsong
parents: 89
diff changeset
   646
  def re_close(l1: List[ARexp], l2: List[ARexp], re_init: ARexp): ARexp = l1 match {
Chengsong
parents: 89
diff changeset
   647
    case Nil => re_init
Chengsong
parents: 89
diff changeset
   648
    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
   649
    else re_close(cs, l2.tail, re_init)
Chengsong
parents: 89
diff changeset
   650
  }
Chengsong
parents: 89
diff changeset
   651
  def closed_string_der(r1: ARexp, r2: ARexp, s: String): ARexp = {
Chengsong
parents: 89
diff changeset
   652
    val l1 = der_seq(r1, s.toList, Nil)
Chengsong
parents: 89
diff changeset
   653
    val l2 = der_seq_rev(r2, s.toList, Nil)
Chengsong
parents: 89
diff changeset
   654
    val Re = re_close(l1.reverse, l2, ASEQ(List(), l1.last, l2.head))
Chengsong
parents: 89
diff changeset
   655
    print(Re)
Chengsong
parents: 89
diff changeset
   656
    val Comp = bders( s.toList, ASEQ(List(), r1, r2))
Chengsong
parents: 89
diff changeset
   657
    print(Comp  )
Chengsong
parents: 89
diff changeset
   658
    if(Re != Comp){
Chengsong
parents: 89
diff changeset
   659
      println("boooooooooooooooo!joihniuguyfuyftyftyufuyids gheioueghrigdhxilj")
Chengsong
parents: 89
diff changeset
   660
    }
Chengsong
parents: 89
diff changeset
   661
    Re
Chengsong
parents: 89
diff changeset
   662
  }
Chengsong
parents: 89
diff changeset
   663
  def newxp1(){
Chengsong
parents: 89
diff changeset
   664
    val r1 = internalise("ab"|"")
Chengsong
parents: 89
diff changeset
   665
    val r2 = internalise(("b")%)
Chengsong
parents: 89
diff changeset
   666
    val s = "abbbbb"
Chengsong
parents: 89
diff changeset
   667
    val s2= "bbbbb"
Chengsong
parents: 89
diff changeset
   668
    val s3 = "aaaaaa"
Chengsong
parents: 89
diff changeset
   669
    //closed_string_der(r1, r2, s)
Chengsong
parents: 89
diff changeset
   670
    //closed_string_der(r1, r2, s2)
Chengsong
parents: 89
diff changeset
   671
    closed_string_der(r1, r2, s3)
Chengsong
parents: 89
diff changeset
   672
  }
0
Chengsong
parents:
diff changeset
   673
  def main(args: Array[String]) {
91
Chengsong
parents: 89
diff changeset
   674
    newxp1()
Chengsong
parents: 89
diff changeset
   675
  //contains7()
Chengsong
parents: 89
diff changeset
   676
  //retrieve_encode_STARS()
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   677
    //check_all()   
7
1572760ff866 found the difference: caused by flats
Chengsong
parents: 6
diff changeset
   678
    //radical_correctness()
11
9c1ca6d6e190 The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents: 10
diff changeset
   679
    //correctness_proof_convenient_path()
15
Chengsong
parents: 14
diff changeset
   680
    //retrieve_experience()
17
Chengsong
parents: 16
diff changeset
   681
    //neat_retrieve()
72
83b021fc7d29 interesting?
Chengsong
parents: 17
diff changeset
   682
    //test_bsimp2()
91
Chengsong
parents: 89
diff changeset
   683
    //christian_def2()
16
c51178fa85fe new version of slides
Chengsong
parents: 15
diff changeset
   684
    //christian_def()
17
Chengsong
parents: 16
diff changeset
   685
    //essence_posix()
89
a0bcf15a7844 counterexample
Chengsong
parents: 87
diff changeset
   686
    //speed_test()
0
Chengsong
parents:
diff changeset
   687
  } 
Chengsong
parents:
diff changeset
   688
}
Chengsong
parents:
diff changeset
   689