Partial.scala
author Chengsong
Wed, 03 Jul 2019 20:46:03 +0100
changeset 41 a1f90febbc7f
parent 0 902326e1615a
child 92 aaa2f2b52baf
permissions -rwxr-xr-x
example
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Chengsong
parents:
diff changeset
     1
import RexpRelated._
Chengsong
parents:
diff changeset
     2
import RexpRelated.Rexp._
Chengsong
parents:
diff changeset
     3
import Spiral._
Chengsong
parents:
diff changeset
     4
object Partial{
Chengsong
parents:
diff changeset
     5
  def dot_prod(rs: Set[Rexp], r: Rexp): Set[Rexp] = r match {
Chengsong
parents:
diff changeset
     6
    case ZERO => Set()
Chengsong
parents:
diff changeset
     7
    case ONE => rs
Chengsong
parents:
diff changeset
     8
    case r => rs.map((re) => if (re == ONE) r else SEQ(re, r)  )   
Chengsong
parents:
diff changeset
     9
  }
Chengsong
parents:
diff changeset
    10
  def cir_prod(l: Lin, t: Rexp): Lin = t match {//remember this Lin is different from the Lin in Antimirov's paper. Here it does not mean the set of all subsets of linear forms that does not contain zero, but rather the type a set of linear forms
Chengsong
parents:
diff changeset
    11
    case ZERO => Set()
Chengsong
parents:
diff changeset
    12
    case ONE => l
Chengsong
parents:
diff changeset
    13
    case t => l.map( m => m._2 match {case ZERO => m case ONE => (m._1, t) case p => (m._1, SEQ(p, t)) }  )
Chengsong
parents:
diff changeset
    14
  }
Chengsong
parents:
diff changeset
    15
  def lf(r: Rexp): Lin = r match {
Chengsong
parents:
diff changeset
    16
    case ZERO => Set()
Chengsong
parents:
diff changeset
    17
    case ONE => Set()
Chengsong
parents:
diff changeset
    18
    case CHAR(f) => {
Chengsong
parents:
diff changeset
    19
      //val Some(c) = alphabet.find(f) 
Chengsong
parents:
diff changeset
    20
      Set((f, ONE))
Chengsong
parents:
diff changeset
    21
    }
Chengsong
parents:
diff changeset
    22
    case ALTS(rs) => {
Chengsong
parents:
diff changeset
    23
      rs.foldLeft(Set[Mon]())((acc, r) => acc ++ lf(r))
Chengsong
parents:
diff changeset
    24
    }
Chengsong
parents:
diff changeset
    25
    case STAR(r1) => cir_prod(lf(r1), STAR(r1)) //may try infix notation later......
Chengsong
parents:
diff changeset
    26
    case SEQ(r1, r2) =>{
Chengsong
parents:
diff changeset
    27
      if (nullable(r1))
Chengsong
parents:
diff changeset
    28
        cir_prod(lf(r1), r2) ++ lf(r2)
Chengsong
parents:
diff changeset
    29
      else
Chengsong
parents:
diff changeset
    30
        cir_prod(lf(r1), r2) 
Chengsong
parents:
diff changeset
    31
    }
Chengsong
parents:
diff changeset
    32
  }
Chengsong
parents:
diff changeset
    33
  def lfs(r: Set[Rexp]): Lin = {
Chengsong
parents:
diff changeset
    34
    r.foldLeft(Set[Mon]())((acc, r) => acc ++ lf(r))
Chengsong
parents:
diff changeset
    35
  }
Chengsong
parents:
diff changeset
    36
Chengsong
parents:
diff changeset
    37
  def pder(x: Char, t: Rexp): Set[Rexp] = {
Chengsong
parents:
diff changeset
    38
    val lft = lf(t)
Chengsong
parents:
diff changeset
    39
    (lft.filter(mon => if(mon._1 == x) true else false)).map(mon => mon._2)
Chengsong
parents:
diff changeset
    40
  }
Chengsong
parents:
diff changeset
    41
  def pders_single(s: List[Char], t: Rexp) : Set[Rexp] = s match {
Chengsong
parents:
diff changeset
    42
    case x::xs => pders(xs, pder(x, t))
Chengsong
parents:
diff changeset
    43
    case Nil => Set(t)
Chengsong
parents:
diff changeset
    44
  }
Chengsong
parents:
diff changeset
    45
  def pders(s: List[Char], ts: Set[Rexp]) : Set[Rexp] = s match {
Chengsong
parents:
diff changeset
    46
    case x::xs => pders(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
Chengsong
parents:
diff changeset
    47
    case Nil => ts 
Chengsong
parents:
diff changeset
    48
  }
Chengsong
parents:
diff changeset
    49
  def pderss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]() )( (acc, s) => pders_single(s, t) ++ acc )
Chengsong
parents:
diff changeset
    50
  def pdera(t: Rexp): Set[Rexp] = lf(t).map(mon => mon._2)
Chengsong
parents:
diff changeset
    51
  //all implementation of partial derivatives that involve set union are potentially buggy
Chengsong
parents:
diff changeset
    52
  //because they don't include the original regular term before they are pdered.
Chengsong
parents:
diff changeset
    53
  //now only pderas is fixed.
Chengsong
parents:
diff changeset
    54
  def pderas(t: Set[Rexp], d: Int): Set[Rexp] = if(d > 0) pderas(lfs(t).map(mon => mon._2), d - 1) ++ t else lfs(t).map(mon => mon._2) ++ t//repeated application of pderas over the newest set of pders.
Chengsong
parents:
diff changeset
    55
  
Chengsong
parents:
diff changeset
    56
  def sigma_lf(l: Set[Mon]) : Rexp = ALTS(l.map(mon => SEQ(CHAR(mon._1),mon._2)).toList)
Chengsong
parents:
diff changeset
    57
  def sigma(rs: Set[Rexp]) : Rexp = ALTS(rs.toList)
Chengsong
parents:
diff changeset
    58
  def o(r: Rexp) = if (nullable(r)) ONE else ZERO
Chengsong
parents:
diff changeset
    59
  def nlf(t: Rexp) : Rexp = ALTS(List( o(t), sigma_lf(lf(t)) ))
Chengsong
parents:
diff changeset
    60
  def pdp(x: Char, r: Rexp) : Set[Rexp] = r match {
Chengsong
parents:
diff changeset
    61
    case ZERO => Set[Rexp]()
Chengsong
parents:
diff changeset
    62
    case ONE => Set[Rexp]()
Chengsong
parents:
diff changeset
    63
    case CHAR(f) => if(x == f) Set(ONE) else Set[Rexp]()
Chengsong
parents:
diff changeset
    64
    case ALTS(rs) => rs.foldLeft(Set[Rexp]())((acc, r) => acc ++ pdp(x, r))
Chengsong
parents:
diff changeset
    65
    case STAR(r1) => pdp(x, r).map(a => SEQ(a, STAR(r1)))
Chengsong
parents:
diff changeset
    66
    case SEQ(a0, b) => if(nullable(a0)) pdp(x, a0).map(a => SEQ(a, b)) ++ pdp(x, b) else pdp(x, a0).map(a => SEQ(a, b))
Chengsong
parents:
diff changeset
    67
  }
Chengsong
parents:
diff changeset
    68
  def pdps(s: List[Char], ts: Set[Rexp]): Set[Rexp] = s match {
Chengsong
parents:
diff changeset
    69
    case x::xs => pdps(xs, ts.foldLeft(Set[Rexp]())((acc, t) => acc ++ pder(x, t)))
Chengsong
parents:
diff changeset
    70
    case Nil => ts   
Chengsong
parents:
diff changeset
    71
  }
Chengsong
parents:
diff changeset
    72
  def pdpss(ss: List[List[Char]], t: Rexp): Set[Rexp] = ss.foldLeft( Set[Rexp]())((acc, s) => pdps(s, Set(t)) ++ acc)
Chengsong
parents:
diff changeset
    73
Chengsong
parents:
diff changeset
    74
  //this function currently  the problem of counting the same char as different ones 
Chengsong
parents:
diff changeset
    75
  //because they are defined as "pred"
Chengsong
parents:
diff changeset
    76
  def subterms(r: Rexp): Set[Rexp] = {//excluding zeros.
Chengsong
parents:
diff changeset
    77
    r match {
Chengsong
parents:
diff changeset
    78
      case ZERO => {
Chengsong
parents:
diff changeset
    79
        Set[Rexp]()
Chengsong
parents:
diff changeset
    80
      }
Chengsong
parents:
diff changeset
    81
      case SEQ(r1, r2) => {
Chengsong
parents:
diff changeset
    82
        Set(r1, r2) ++ subterms(r1) ++ subterms(r2)
Chengsong
parents:
diff changeset
    83
      }
Chengsong
parents:
diff changeset
    84
      case ALTS(rs) => {
Chengsong
parents:
diff changeset
    85
        rs.toSet ++ rs.foldLeft(Set[Rexp]())((acc, r) => acc ++ subterms(r))
Chengsong
parents:
diff changeset
    86
      }
Chengsong
parents:
diff changeset
    87
      case STAR(r) => {
Chengsong
parents:
diff changeset
    88
        Set(r) ++ subterms(r)
Chengsong
parents:
diff changeset
    89
      }
Chengsong
parents:
diff changeset
    90
      case r => Set(r)
Chengsong
parents:
diff changeset
    91
    }
Chengsong
parents:
diff changeset
    92
  }
Chengsong
parents:
diff changeset
    93
  def comp(s: List[Char], t: Rexp) = {
Chengsong
parents:
diff changeset
    94
    //var r = internalise(t)
Chengsong
parents:
diff changeset
    95
    //var setr = Set(t)
Chengsong
parents:
diff changeset
    96
    
Chengsong
parents:
diff changeset
    97
    /*for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
    98
      val mamaipi = bsimp(bder(s(i), r))
Chengsong
parents:
diff changeset
    99
      val mamaimapi = pdps(List(s(i)), setr)
Chengsong
parents:
diff changeset
   100
      //compare dersimp and pder w.r.t each character in string s
Chengsong
parents:
diff changeset
   101
      println("current string: " + s.slice(0,i + 1))
Chengsong
parents:
diff changeset
   102
      println("der+simp: ")
Chengsong
parents:
diff changeset
   103
      println(aregx_tree(mamaipi))
Chengsong
parents:
diff changeset
   104
      println("pder: ")
Chengsong
parents:
diff changeset
   105
      mamaimapi.foreach(m => println(regx_tree(m)))
Chengsong
parents:
diff changeset
   106
      r = mamaipi
Chengsong
parents:
diff changeset
   107
      setr = mamaimapi 
Chengsong
parents:
diff changeset
   108
    }*/
Chengsong
parents:
diff changeset
   109
    for(i <- 1 to 10)
Chengsong
parents:
diff changeset
   110
      println(pderas(Set(t), i).size, i)
Chengsong
parents:
diff changeset
   111
    //val alphabet_star_t = pderas(Set(t), 10)
Chengsong
parents:
diff changeset
   112
    //println("all possible elements in pder (probably...): ")
Chengsong
parents:
diff changeset
   113
    //alphabet_star_t.foreach(r => println(regx_tree(r)))
Chengsong
parents:
diff changeset
   114
  }
Chengsong
parents:
diff changeset
   115
}
Chengsong
parents:
diff changeset
   116
/*    val delta = lfs(t).map(mon => mon._2)                
Chengsong
parents:
diff changeset
   117
    if(delta.size == t.size)
Chengsong
parents:
diff changeset
   118
    {
Chengsong
parents:
diff changeset
   119
      println(delta.size)
Chengsong
parents:
diff changeset
   120
      println("steady: "+delta.size)
Chengsong
parents:
diff changeset
   121
      return delta
Chengsong
parents:
diff changeset
   122
    }
Chengsong
parents:
diff changeset
   123
      
Chengsong
parents:
diff changeset
   124
    else
Chengsong
parents:
diff changeset
   125
    {
Chengsong
parents:
diff changeset
   126
      val a = pderas(delta)
Chengsong
parents:
diff changeset
   127
      println("increment: "+delta.size+a.size)
Chengsong
parents:
diff changeset
   128
      return a
Chengsong
parents:
diff changeset
   129
    }*/