Spiral.scala
changeset 93 d486c12deeab
parent 92 aaa2f2b52baf
child 101 4a327e70d538
equal deleted inserted replaced
92:aaa2f2b52baf 93:d486c12deeab
   430   def check_all(){
   430   def check_all(){
   431 
   431 
   432         weak_sub_check(big_panda, str_panda, 6, size_expansion_rate)
   432         weak_sub_check(big_panda, str_panda, 6, size_expansion_rate)
   433 
   433 
   434   }
   434   }
       
   435   def bstostick(bs: List[Bit]): Element = bs match {
       
   436     //case b::Nil => elem(b.toString)
       
   437     case b::bs1 => elem(b.toString) beside bstostick(bs1)
       
   438     case Nil => elem(' ', 1, 1)
       
   439   }
       
   440   def bits_print(r: ARexp): Element = r match {
       
   441     case AALTS(bs,rs) => {
       
   442       val bitstick = bstostick(bs)
       
   443       rs match {
       
   444         case r::rs1 =>  
       
   445         rs1.foldLeft( 
       
   446         ((elem("(") left_align bitstick) beside 
       
   447         bits_print(r))  )( (acc, r2) => acc beside ((elem(" + ") above elem("   ")) beside bits_print(r2) )) beside
       
   448         elem(")")
       
   449         case Nil => elem(' ', 1, 1)
       
   450       }
       
   451     }
       
   452     case ASEQ(bs, r1, r2) => {
       
   453       ((elem("[") left_align bstostick(bs)) beside  bits_print(r1) beside elem("~") beside bits_print(r2)) 
       
   454     }
       
   455     case ASTAR(bs, r) => {
       
   456       r match {
       
   457         case AONE(_) | AZERO | ACHAR(_, _) => {
       
   458           (elem("{") left_align bstostick(bs)) beside (bits_print(r) beside elem("}*")) 
       
   459         }
       
   460         case _ => {
       
   461           (elem("{") left_align bstostick(bs)) beside (bits_print(r) beside elem("}*")) 
       
   462         }
       
   463       }
       
   464     }
       
   465     case AONE(bs) => {
       
   466       elem("1") left_align bstostick(bs)
       
   467     }
       
   468     case ACHAR(bs, c) => {
       
   469       elem(c, 1, 1) left_align bstostick(bs)
       
   470     }
       
   471     case AZERO =>
       
   472     {
       
   473       elem ("0") above elem(" ")
       
   474     }
       
   475   }
       
   476   def happy_print(r: Rexp): Unit = r match {
       
   477     case ALTS( r::rs ) => {
       
   478       print("(")
       
   479       happy_print(r)
       
   480       rs.map(r2=>{      
       
   481         print(" + ")
       
   482         happy_print(r2)
       
   483       })
       
   484       print(")")
       
   485     }
       
   486     case SEQ(r1, r2) => {
       
   487       happy_print(r1)
       
   488       print("~")
       
   489       happy_print(r2)
       
   490     }
       
   491     case STAR(r) => {
       
   492       r match {
       
   493         case ONE | ZERO | CHAR(_) => {
       
   494           happy_print(r)
       
   495           print("*")
       
   496         }
       
   497         case _ => {
       
   498           print("[")
       
   499           happy_print(r)
       
   500           print("]*")
       
   501         }
       
   502       }
       
   503     }
       
   504     case ONE => {
       
   505       print("1")
       
   506     }
       
   507     case ZERO => {
       
   508       print("0")
       
   509     }
       
   510     case CHAR(c) =>{
       
   511       print(c)
       
   512     }
       
   513   }
       
   514   def bits_slide(s: String, r: Rexp){
       
   515     val nangao = ders_simp(internalise(r), s.toList)
       
   516     val easy = bsimp(bders(s.toList, internalise(r)))
       
   517     println(s)
       
   518     println(r)
       
   519     happy_print(r)
       
   520     println()
       
   521     print(bits_print(nangao))
       
   522     println()
       
   523     print(bits_print(easy))
       
   524     println()
       
   525   }
       
   526   //found r = SEQ(ALTS(List(CHAR(c), CHAR(a))),SEQ(ALTS(List(CHAR(a), CHAR(c))),ALTS(List(CHAR(b), CHAR(c))))) s = "aa"
       
   527     def bsimp_print(r: ARexp): Unit = r match {
       
   528     case ASEQ(bs, r1, r2) => 
       
   529       {
       
   530         println("0.r or r.0 to 0 or bs1 1.r to fuse bs++bs1 r")
       
   531         bits_print(bsimp(r1))
       
   532         bits_print(bsimp(r2))
       
   533       }
       
   534     case AALTS(bs, rs) => {
       
   535       println("rs.map(bsimp) equals *************")
       
   536       val rs_simp = rs.map(bsimp)
       
   537       for(r <- rs_simp){
       
   538         println(bits_print(r))
       
   539       }
       
   540       println("*************")
       
   541       println("flts(rs_simp)")
       
   542       val flat_res = flats(rs_simp)
       
   543       for(r <- flat_res){
       
   544         println(bits_print(r))
       
   545       }
       
   546       println("dB(flat_res)")
       
   547       val dist_res = distinctBy(flat_res, erase)
       
   548       for(r <- dist_res){
       
   549         println(bits_print(r))
       
   550       }
       
   551       dist_res match {
       
   552         case Nil => println("AZERO")
       
   553         case r::Nil => println("fuse(bs, r)")
       
   554         case rs => println("AALTS(bs, rs)")
       
   555       }
       
   556     }
       
   557     case _ => println("No simp at this level")
       
   558   }
       
   559   def tellmewhy(){
       
   560     //val r = SEQ(ALTS(List(CHAR('a'), CHAR('b'))),ALTS(List(ALTS(List(CHAR('a'), CHAR('a'))), STAR(CHAR('a'))))) 
       
   561     val r = SEQ(ALTS(List(CHAR('a'), CHAR('b'))),ALTS(List(CHAR('a'), STAR(CHAR('a')) ) ))
       
   562     //val r = ("a"|"b")~("a")
       
   563     val s = "aa"
       
   564     for(i <- 0 to s.length-1){
       
   565       val ss = s.slice(0, i+1)
       
   566       val nangao = ders_simp(internalise(r), ss.toList)
       
   567       val easy = (bders(ss.toList, internalise(r)))
       
   568       println(bits_print(nangao))
       
   569       println(bits_print(easy))
       
   570       println()
       
   571       bsimp_print(easy)
       
   572     }
       
   573     println(bits_print(bsimp(bders(s.toList, internalise(r)))))
       
   574     println(bits_print(ders_simp(internalise(r), s.toList))
       
   575   }
       
   576   def find_re(){
       
   577     for (i <- 1 to 10000){
       
   578       val r = balanced_struct_gen(3)
       
   579       val s = rd_string_gen(2,1)
       
   580       val nangao = ders_simp(internalise(r), s.toList)
       
   581       val easy = bsimp(bders(s.toList, internalise(r)))
       
   582       if(nangao != easy){
       
   583         bits_slide(s, r)
       
   584       }
       
   585     }
       
   586   }
   435   //simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set)
   587   //simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set)
   436   def pushbits(r: ARexp): ARexp = r match {
   588   def pushbits(r: ARexp): ARexp = r match {
   437     case AALTS(bs, rs) => AALTS(Nil, rs.map(r=>fuse(bs, pushbits(r))))
   589     case AALTS(bs, rs) => AALTS(Nil, rs.map(r=>fuse(bs, pushbits(r))))
   438     case ASEQ(bs, r1, r2) => ASEQ(bs, pushbits(r1), pushbits(r2))
   590     case ASEQ(bs, r1, r2) => ASEQ(bs, pushbits(r1), pushbits(r2))
   439     case r => r
   591     case r => r
   440   }
   592   }
   441   def correctness_proof_convenient_path(){
   593   def correctness_proof_convenient_path(){
   442     for(i <- 1 to 10000)
       
   443     {
       
   444         val s = "abab"//rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3)
   594         val s = "abab"//rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3)
   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)
   595         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)
   446         for(j <- 0 to s.length - 1){
   596         for(j <- 0 to s.length - 1){
   447           val ss = s.slice(0, j+ 1)
   597           val ss = s.slice(0, j+ 1)
   448           val nangao = ders_simp(internalise(r), ss.toList)
   598           val nangao = ders_simp(internalise(r), ss.toList)
   460             println(annotated_tree(bders(ss.toList, internalise(r))))//flats' fuse when opening up AALTS causes the difference
   610             println(annotated_tree(bders(ss.toList, internalise(r))))//flats' fuse when opening up AALTS causes the difference
   461             println("regex after ders and then a single simp")
   611             println("regex after ders and then a single simp")
   462             println(annotated_tree(easy))
   612             println(annotated_tree(easy))
   463           }
   613           }
   464         }
   614         }
   465     }
       
   466   }
   615   }
   467   /*    if(bts == cdbts){//test of equality code v = retrieve internalise(r) v if |- v : r
   616   /*    if(bts == cdbts){//test of equality code v = retrieve internalise(r) v if |- v : r
   468       println(bts)
   617       println(bts)
   469       println(cdbts)
   618       println(cdbts)
   470       println("code v = retrieve internalise(r) v if |- v : r")
   619       println("code v = retrieve internalise(r) v if |- v : r")
   650   }
   799   }
   651   //HERE
   800   //HERE
   652   def closed_string_der(r1: ARexp, r2: ARexp, s: String): ARexp = {
   801   def closed_string_der(r1: ARexp, r2: ARexp, s: String): ARexp = {
   653     val l1 = der_seq(r1, s.toList, Nil)
   802     val l1 = der_seq(r1, s.toList, Nil)
   654     val l2 = der_seq_rev(r2, s.toList, Nil)
   803     val l2 = der_seq_rev(r2, s.toList, Nil)
   655     val Re = re_close(l1.reverse, l2, ASEQ(List(), l1.last, l2.head))
   804     val Re = re_close((l1.reverse).tail, l2.tail, ASEQ(List(), l1.last, l2.head))
   656     print(Re)
   805     print(Re)
   657     val Comp = bders( s.toList, ASEQ(List(), r1, r2))
   806     val Comp = bders( s.toList, ASEQ(List(), r1, r2))
   658     print(Comp  )
   807     print(Comp  )
   659     if(Re != Comp){
   808     if(Re != Comp){
   660       println("boooooooooooooooo!joihniuguyfuyftyftyufuyids gheioueghrigdhxilj")
   809       println("boooooooooooooooo!joihniuguyfuyftyftyufuyids gheioueghrigdhxilj")
   686       }
   835       }
   687     }
   836     }
   688   }
   837   }
   689 
   838 
   690   def main(args: Array[String]) {
   839   def main(args: Array[String]) {
   691     string_der_test()
   840     //println(S.toString)
       
   841     //find_re()
       
   842     tellmewhy()
       
   843     //string_der_test()
   692     //comp(rd_string_gen(3,6).toList, random_struct_gen(7))
   844     //comp(rd_string_gen(3,6).toList, random_struct_gen(7))
   693     //newxp1()
   845     //newxp1()
   694   //contains7()
   846   //contains7()
   695   //retrieve_encode_STARS()
   847   //retrieve_encode_STARS()
   696     //check_all()   
   848     //check_all()   
   703     //christian_def()
   855     //christian_def()
   704     //essence_posix()
   856     //essence_posix()
   705     //speed_test()
   857     //speed_test()
   706   } 
   858   } 
   707 }
   859 }
   708 
   860 //List(              ASTAR(List(Z),ACHAR(List(),a)),            AALTS(List(S),List(ACHAR(List(Z),b), ACHAR(List(S),a)))       )