progs/scala/re-bit2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 06 Feb 2022 00:02:04 +0000
changeset 416 57182b36ec01
parent 360 e752d84225ec
child 436 222333d2bdc2
permissions -rw-r--r--
more with the paper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.annotation.tailrec   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
     5
// standard regular expressions
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
abstract class Rexp 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     7
case object ZERO extends Rexp
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     8
case object ONE extends Rexp
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class CHAR(c: Char) extends Rexp
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class STAR(r: Rexp) extends Rexp 
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    13
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    14
abstract class Bit
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    15
case object Z extends Bit
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    16
case object S extends Bit
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    17
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    18
type Bits = List[Bit]
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    19
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    20
// annotated regular expressions
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    21
abstract class ARexp 
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    22
case object AZERO extends ARexp
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    23
case class AONE(bs: Bits) extends ARexp
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    24
case class ACHAR(bs: Bits, c: Char) extends ARexp
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    25
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    26
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    27
case class ASTAR(bs: Bits, r: ARexp) extends ARexp 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    28
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    29
// an abbreviation for binary alternatives
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    30
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
abstract class Val
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    33
case object Empty extends Val
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
case class Chr(c: Char) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
case class Sequ(v1: Val, v2: Val) extends Val
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    36
case class Left(v: Val) extends Val
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    37
case class Right(v: Val) extends Val
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
case class Stars(vs: List[Val]) extends Val
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    39
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
// some convenience for typing in regular expressions
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    42
def charlist2rexp(s: List[Char]): Rexp = s match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    43
  case Nil => ONE
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  case c::Nil => CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
}
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    47
implicit def string2rexp(s: String) : Rexp = charlist2rexp(s.toList)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
implicit def RexpOps(r: Rexp) = new {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  def | (s: Rexp) = ALT(r, s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  def % = STAR(r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  def ~ (s: Rexp) = SEQ(r, s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
implicit def stringOps(s: String) = new {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  def | (r: Rexp) = ALT(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  def | (r: String) = ALT(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  def % = STAR(s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  def ~ (r: Rexp) = SEQ(s, r)
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
    60
  def ~ (r: String) = SEQ(s, r)
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    61
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    62
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    63
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    64
// Bitcoded + Annotation
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    65
//=======================
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
    66
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    67
//erase function: extracts the Rexp from ARexp
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    68
def erase(r:ARexp): Rexp = r match{
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    69
  case AZERO => ZERO
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    70
  case AONE(_) => ONE
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    71
  case ACHAR(bs, c) => CHAR(c)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    72
  case AALTS(bs, Nil) => ZERO
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    73
  case AALTS(bs, r::Nil) => erase(r)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    74
  case AALTS(bs, r::rs) => ALT(erase(r), erase(AALTS(bs, rs)))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    75
  case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    76
  case ASTAR(cs, ASTAR(ds, r))=> STAR(erase(r))
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    77
  case ASTAR(cs, r)=> STAR(erase(r))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    78
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    79
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    80
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    81
  case AZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    82
  case AONE(cs) => AONE(bs ++ cs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    83
  case ACHAR(cs, c) => ACHAR(bs ++ cs, c)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    84
  case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    85
  case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    86
  case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    87
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    88
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    89
def internalise(r: Rexp) : ARexp = r match {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    90
  case ZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    91
  case ONE => AONE(Nil)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    92
  case CHAR(c) => ACHAR(Nil, c)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
    93
  case ALT(r1, r2) => AALT(Nil, fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    94
  case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    95
  case STAR(r) => ASTAR(Nil, internalise(r))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
294
c1de75d20aa4 added_lit
Christian Urban <urbanc@in.tum.de>
parents: 286
diff changeset
    98
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
    99
internalise(("a" | "ab") ~ ("b" | ""))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   100
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   101
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   102
// decoding of a value from a bitsequence
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   103
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   104
  case (ONE, bs) => (Empty, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   105
  case (CHAR(c), bs) => (Chr(c), bs)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   106
  case (ALT(r1, r2), Z::bs) => {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   107
    val (v, bs1) = decode_aux(r1, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   108
    (Left(v), bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   109
  }
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   110
  case (ALT(r1, r2), S::bs) => {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   111
    val (v, bs1) = decode_aux(r2, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   112
    (Right(v), bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   113
  }
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   114
  case (SEQ(r1, r2), bs) => {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   115
    val (v1, bs1) = decode_aux(r1, bs)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   116
    val (v2, bs2) = decode_aux(r2, bs1)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   117
    (Sequ(v1, v2), bs2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   118
  }
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   119
  case (STAR(r1), Z::bs) => {
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   120
    val (v, bs1) = decode_aux(r1, bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   121
    val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   122
    (Stars(v::vs), bs2)
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   123
  }
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   124
  case (STAR(_), S::bs) => (Stars(Nil), bs)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   125
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   126
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   127
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   128
  case (v, Nil) => v
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   129
  case _ => throw new Exception("Not decodable")
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   130
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   131
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   132
def encode(v: Val) : Bits = v match {
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   133
  case Empty => Nil
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   134
  case Chr(c) => Nil
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   135
  case Left(v) => Z :: encode(v)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   136
  case Right(v) => S :: encode(v)
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   137
  case Sequ(v1, v2) => encode(v1) ::: encode(v2)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   138
  case Stars(Nil) => List(S)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   139
  case Stars(v::vs) =>  Z :: encode(v) ::: encode(Stars(vs))
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   140
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   141
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   142
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   143
// nullable function: tests whether the an (annotated) 
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   144
// regular expression can recognise the empty string
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   145
def bnullable (r: ARexp) : Boolean = r match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   146
  case AZERO => false
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   147
  case AONE(_) => true
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   148
  case ACHAR(_,_) => false
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   149
  case AALTS(_, rs) => rs.exists(bnullable)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   150
  case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   151
  case ASTAR(_, _) => true
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   152
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   153
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   154
def bmkeps(r: ARexp) : Bits = r match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   155
  case AONE(bs) => bs
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   156
  case AALTS(bs, r::Nil) => bs ++ bmkeps(r) 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   157
  case AALTS(bs, r::rs) => 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   158
    if (bnullable(r)) bs ++ bmkeps(r) else bmkeps(AALTS(bs, rs))  
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   159
  case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   160
  case ASTAR(bs, r) => bs ++ List(S)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
// derivative of a regular expression w.r.t. a character
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   164
def bder(c: Char, r: ARexp) : ARexp = r match {
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   165
  case AZERO => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   166
  case AONE(_) => AZERO
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   167
  case ACHAR(bs, d) => if (c == d) AONE(bs) else AZERO
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   168
  case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   169
  case ASEQ(bs, r1, r2) => 
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   170
    if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   171
    else ASEQ(bs, bder(c, r1), r2)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   172
  case ASTAR(bs, r) => ASEQ(bs, fuse(List(Z), bder(c, r)), ASTAR(Nil, r))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   175
// derivative w.r.t. a string (iterates bder)
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   176
@tailrec
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   177
def bders (s: List[Char], r: ARexp) : ARexp = s match {
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
  case Nil => r
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   179
  case c::s => bders(s, bder(c, r))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   182
// main unsimplified lexing function (produces a value)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   183
def blex(r: ARexp, s: List[Char]) : Bits = s match {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   184
  case Nil => if (bnullable(r)) bmkeps(r) else throw new Exception("Not matched")
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   185
  case c::cs => blex(bder(c, r), cs)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   188
def pre_blexing(r: ARexp, s: String)  : Bits = blex(r, s.toList)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   189
def blexing(r: Rexp, s: String) : Val = decode(r, pre_blexing(internalise(r), s))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   190
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   191
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   192
// example by Tudor
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   193
//val reg = ((("a".%)) ~ ("b" | "c")).%
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   194
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   195
//println(blexing(reg, "aab"))
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   196
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   197
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   198
//=======================
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   199
// simplification 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   200
//
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   201
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   202
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   203
def flts(rs: List[ARexp]) : List[ARexp] = rs match {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   204
  case Nil => Nil
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   205
  case AZERO :: rs => flts(rs)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   206
  case AALTS(bs, rs1) :: rs => rs1.map(fuse(bs, _)) ++ flts(rs)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   207
  case r1 :: rs => r1 :: flts(rs)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   208
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   209
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   210
def distinctBy[B, C](xs: List[B], 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   211
                     f: B => C, 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   212
                     acc: List[C] = Nil): List[B] = xs match {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   213
  case Nil => Nil
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   214
  case x::xs => {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   215
    val res = f(x)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   216
    if (acc.contains(res)) distinctBy(xs, f, acc)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   217
    else x::distinctBy(xs, f, res::acc)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   218
  }
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   219
} 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   220
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   221
def flats(rs: List[ARexp]): List[ARexp] = rs match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   222
      case Nil => Nil
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   223
      case AZERO :: rs1 => flats(rs1)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   224
      case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   225
      case r1 :: rs2 => r1 :: flats(rs2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   226
    }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   227
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   228
def projectFirstChild(rs: List[ARexp]) : List[ARexp] = rs match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   229
    case (ASEQ(bs, r1, r2p)::rs1) => r1::projectFirstChild(rs1)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   230
    case Nil => Nil
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   231
  }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   232
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   233
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   234
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   235
def distinctBy2(xs: List[ARexp], acc: List[Rexp] = Nil): List[ARexp] = xs match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   236
    case Nil => Nil
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   237
    case (x::xs) => {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   238
      val res = erase(x)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   239
      if(acc.contains(res))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   240
        distinctBy2(xs, acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   241
      else
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   242
        x match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   243
          case ASEQ(bs0, AALTS(bs1, rs), r2) => 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   244
            val newTerms =  distinctBy2(rs.map(r1 => ASEQ(Nil, r1, r2)), acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   245
            val rsPrime = projectFirstChild(newTerms)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   246
            newTerms match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   247
              case Nil => distinctBy2(xs, acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   248
              case t::Nil => ASEQ(bs0, fuse(bs1, rsPrime.head), r2)::distinctBy2(xs, erase(t)::acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   249
              case ts => ASEQ(bs0, AALTS(bs1, rsPrime), r2)::distinctBy2(xs, newTerms.map(erase(_)):::acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   250
            }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   251
          case x => x::distinctBy2(xs, res::acc)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   252
        }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   253
    }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   254
  } 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   255
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   256
/*
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   257
def strongBsimp(r: ARexp): ARexp =
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   258
  {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   259
    r match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   260
      case ASEQ(bs1, r1, r2) => (strongBsimp(r1), strongBsimp(r2)) match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   261
          case (AZERO, _) => AZERO
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   262
          case (_, AZERO) => AZERO
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   263
          case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   264
          case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   265
      }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   266
      case AALTS(bs1, rs) => {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   267
            val rs_simp = rs.map(strongBsimp(_))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   268
            val flat_res = flats(rs_simp)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   269
            val dist_res = distinctBy2(flat_res)//distinctBy(flat_res, erase)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   270
            dist_res match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   271
              case Nil => AZERO
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   272
              case s :: Nil => fuse(bs1, s)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   273
              case rs => AALTS(bs1, rs)  
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   274
            }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   275
          
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   276
      }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   277
      case r => r
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   278
    }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   279
  }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   280
*/
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   281
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   282
def bsimp(r: ARexp): ARexp = r match {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   283
  case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   284
      case (AZERO, _) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   285
      case (_, AZERO) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   286
      case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   287
      //case (AALTS(bs, rs), r) => 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   288
      //   AALTS(Nil, rs.map(ASEQ(bs1 ++ bs, _, r)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   289
      //case (ASEQ(bs2, r1, ASTAR(bs3, r2)), ASTAR(bs4, r3)) if erase(r2) == erase(r3) =>
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   290
      //  ASEQ(bs1 ++ bs2, r1, ASTAR(bs3, r2))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   291
      //case (ASEQ(bs2, r1, r2), r3)  =>
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   292
      //      ASEQ(bs1 ++ bs2, r1, ASEQ(Nil, r2, r3))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   293
      case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   294
  }
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   295
  case AALTS(bs1, rs) => (flts(rs.map(bsimp))).distinctBy(erase) match {
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   296
      case Nil => AZERO
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   297
      case r::Nil => fuse(bs1, r)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   298
      case rs => AALTS(bs1, rs)
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   299
  }
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   300
  //case (ASTAR(bs1, ASTAR(bs2, r))) => bsimp(ASTAR(bs1 ++ bs2, r))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   301
  case r => r
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   302
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   303
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   304
def bders_simp(r: ARexp, s: List[Char]) : ARexp = s match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   305
  case Nil => r
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   306
  case c::cs => bders_simp(bsimp(bder(c, r)), cs)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   307
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   308
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   309
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   310
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   311
  case Nil => if (bnullable(r)) bmkeps(r) 
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   312
              else throw new Exception("Not matched")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   313
  case c::cs => blex(bsimp(bder(c, r)), cs)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   314
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   315
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   316
def blexing_simp(r: Rexp, s: String) : Val = 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   317
  decode(r, blex_simp(internalise(r), s.toList))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   318
360
e752d84225ec updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   319
//println(blexing_simp(reg, "aab"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   320
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   321
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   322
// extracts a string from value
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   323
def flatten(v: Val) : String = v match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   324
  case Empty => ""
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   325
  case Chr(c) => c.toString
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   326
  case Left(v) => flatten(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   327
  case Right(v) => flatten(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   328
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   329
  case Stars(vs) => vs.map(flatten).mkString
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   330
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   331
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   332
// extracts an environment from a value
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   333
def env(v: Val) : List[(String, String)] = v match {
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   334
  case Empty => Nil
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   335
  case Chr(c) => Nil
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   336
  case Left(v) => env(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   337
  case Right(v) => env(v)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   338
  case Sequ(v1, v2) => env(v1) ::: env(v2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   339
  case Stars(vs) => vs.flatMap(env)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   340
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   341
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   342
def nullable(r: Rexp) : Boolean = r match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   343
  case ZERO => false
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   344
  case ONE => true
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   345
  case CHAR(_) => false
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   346
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   347
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   348
  case STAR(_) => true
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   349
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   350
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   351
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   352
def pder(c: Char, r: Rexp) : Set[Rexp] = r match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   353
  case ZERO => Set()
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   354
  case ONE => Set()
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   355
  case CHAR(d) => if (c == d) Set(ONE) else Set()
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   356
  case ALT(r1, r2) => pder(c, r1) ++ pder(c, r2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   357
  case SEQ(r1, r2) => {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   358
    (for (pr1 <- pder(c, r1)) yield SEQ(pr1, r2)) ++
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   359
    (if (nullable(r1)) pder(c, r2) else Set())
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   360
  }  
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   361
  case STAR(r1) => {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   362
    for (pr1 <- pder(c, r1)) yield SEQ(pr1, STAR(r1))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   363
  }
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   364
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   365
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   366
def pders(s: List[Char], rs: Set[Rexp]) : Set[Rexp] = s match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   367
  case Nil => rs
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   368
  case c::s => pders(s, rs.flatMap(pder(c, _)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   369
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   370
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   371
def size(r: Rexp): Int = r match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   372
  case ZERO => 1
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   373
  case ONE => 1
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   374
  case CHAR(_) => 1
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   375
  case ALT(r1, r2) => 1 + size(r1) + size (r2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   376
  case SEQ(r1, r2) => 1 + size(r1) + size (r2)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   377
  case STAR(r1) => 1 + size(r1)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   378
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   379
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   380
def pp(r: ARexp): String = r match {
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   381
  case ASEQ(_, ACHAR(_, a1),ASEQ(_, r1, r2)) => s"${a1}${pp(r1)}${pp(r2)}"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   382
  case ASEQ(_, ACHAR(_, a1),ACHAR(_, a2)) => s"${a1}${a2}"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   383
  case AZERO => "0"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   384
  case AONE(_) => "1"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   385
  case ACHAR(_, a) => a.toString
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   386
  case AALTS(_, rs) => s"ALTs(${rs.map(pp(_)).mkString(",")})"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   387
  case ASEQ(_, r1, r2) => s"SEQ(${pp(r1)}, ${pp(r2)})"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   388
  case ASTAR(_, r1) => s"(${pp(r1)})*"
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   389
}
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   390
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   391
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   392
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   393
// Some Tests
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   394
//============
416
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   395
val ST = STAR(STAR("a"))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   396
println(pp(internalise(ST)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   397
println(bders(("a" * 1).toList, internalise(ST)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   398
println(bders_simp(internalise(ST), ("a" * 1).toList))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   399
println(pp(bders(("a" * 1).toList, internalise(ST))))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   400
println(pp(bders_simp(internalise(ST), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   401
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   402
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   403
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   404
println(pp(bders_simp(internalise(ST), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   405
println(pp(bders_simp(internalise(ST), ("a" * 2).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   406
println(pp(bders_simp(internalise(ST), ("a" * 3).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   407
println(pp(bders_simp(internalise(ST), ("a" * 4).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   408
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   409
println(blexing(ST, "a" * 1))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   410
println(blexing_simp(ST, "a" * 1))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   411
println(blexing(ST, "a" * 2))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   412
println(blexing_simp(ST, "a" * 2))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   413
println(blexing(ST, "a" * 3))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   414
println(blexing_simp(ST, "a" * 3))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   415
println(blexing(ST, "a" * 4))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   416
println(blexing_simp(ST, "a" * 4))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   417
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   418
val STARREG = ((STAR("a") | STAR("aaa")) | STAR("aaaaa")).%
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   419
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   420
println(blexing(STARREG, "a" * 3))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   421
println(blexing_simp(STARREG, "a" * 3))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   422
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   423
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   424
size(STARREG)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   425
size(erase(bders_simp(internalise(STARREG), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   426
size(erase(bders_simp(internalise(STARREG), ("a" * 2).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   427
size(erase(bders_simp(internalise(STARREG), ("a" * 3).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   428
size(erase(bders_simp(internalise(STARREG), ("a" * 4).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   429
size(erase(bders_simp(internalise(STARREG), ("a" * 5).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   430
size(erase(bders_simp(internalise(STARREG), ("a" * 6).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   431
size(erase(bders_simp(internalise(STARREG), ("a" * 7).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   432
size(erase(bders_simp(internalise(STARREG), ("a" * 8).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   433
size(erase(bders_simp(internalise(STARREG), ("a" * 9).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   434
size(erase(bders_simp(internalise(STARREG), ("a" * 100).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   435
size(erase(bders_simp(internalise(STARREG), ("a" * 101).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   436
size(erase(bders_simp(internalise(STARREG), ("a" * 102).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   437
size(erase(bders_simp(internalise(STARREG), ("a" * 103).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   438
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   439
println(bders_simp(internalise(STARREG), ("a" * 1).toList))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   440
println(bders_simp(internalise(STARREG), ("a" * 2).toList))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   441
println(bders_simp(internalise(STARREG), ("a" * 3).toList))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   442
println(bders_simp(internalise(STARREG), ("a" * 4).toList))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   443
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   444
println(erase(bders_simp(internalise(STARREG), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   445
println(erase(bders_simp(internalise(STARREG), ("a" * 2).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   446
println(erase(bders_simp(internalise(STARREG), ("a" * 3).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   447
println(erase(bders_simp(internalise(STARREG), ("a" * 4).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   448
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   449
println(pp(internalise(STARREG)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   450
println(pp(bders_simp(internalise(STARREG), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   451
println(pp(bders_simp(internalise(STARREG), ("a" * 2).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   452
println(pp(bders_simp(internalise(STARREG), ("a" * 3).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   453
println(pp(bders_simp(internalise(STARREG), ("a" * 4).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   454
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   455
val STARR = (STAR("a") | STAR("aa") | 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   456
             STAR("aaa") | STAR("aaaa") | 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   457
             STAR("aaaaa") | STAR("aaaaaa") | 
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   458
             STAR("aaaaaaa") | STAR("aaaaaaaa")).%
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   459
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   460
size(STARR)
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   461
size(erase(bders_simp(internalise(STARR), ("a" * 1).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   462
size(erase(bders_simp(internalise(STARR), ("a" * 2).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   463
size(erase(bders_simp(internalise(STARR), ("a" * 3).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   464
size(erase(bders_simp(internalise(STARR), ("a" * 4).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   465
size(erase(bders_simp(internalise(STARR), ("a" * 5).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   466
size(erase(bders_simp(internalise(STARR), ("a" * 6).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   467
size(erase(bders_simp(internalise(STARR), ("a" * 7).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   468
size(erase(bders_simp(internalise(STARR), ("a" * 8).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   469
size(erase(bders_simp(internalise(STARR), ("a" * 9).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   470
size(erase(bders_simp(internalise(STARR), ("a" * 1000).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   471
size(erase(bders_simp(internalise(STARR), ("a" * 1001).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   472
size(erase(bders_simp(internalise(STARR), ("a" * 1002).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   473
size(erase(bders_simp(internalise(STARR), ("a" * 1010).toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   474
size(erase(bders_simp(internalise(STARR), ("a" * 1010 ++ "b").toList)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   475
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   476
val r0 = ("a" | "ab") ~ ("b" | "")
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   477
println(pre_blexing(internalise(r0), "ab"))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   478
println(blexing(r0, "ab"))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   479
println(blexing_simp(r0, "ab"))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   480
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   481
println(pders("a".toList, Set(r0)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   482
println(pders("ab".toList, Set(r0)))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   483
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   484
val r00 = ("a" ~ ("b" | "")) | ("ab" ~ ("b" | ""))
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   485
57182b36ec01 more with the paper
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
   486
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   487
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   488
/*
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   489
def time_needed[T](i: Int, code: => T) = {
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   490
  val start = System.nanoTime()
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   491
  for (j <- 1 to i) code
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   492
  val end = System.nanoTime()
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   493
  (end - start)/(i * 1.0e9)
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   494
}
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   495
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   496
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   497
val evil1 = (("a").%) ~ "b"
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   498
val evil2 = (((("a").%).%).%) ~ "b"
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   499
val evil3 = (("a"~"a") | ("a")).%
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   500
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   501
for(i <- 1 to 10000 by 1000) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   502
    println(time_needed(1, blex_simp(internalise(evil1), ("a"*i + "b").toList)))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   503
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   504
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   505
for(i <- 1 to 10000 by 1000) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   506
    println(time_needed(1, blexing_simp(evil1, "a"*i + "b")))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   507
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   508
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   509
for(i <- 1 to 10000 by 1000) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   510
    println(time_needed(1, blexing_simp(evil2, "a"*i + "b")))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   511
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   512
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   513
for(i <- 1 to 10000 by 1000) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   514
    println(time_needed(1, blexing_simp(evil3, "a"*i)))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   515
}
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   516
*/
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   517
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   518
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   519
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   520
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   521
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   522
/////////////////////////
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   523
/////////////////////////
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   524
///// Below not relevant
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   525
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   526
/*
204
cd9e40280784 added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 159
diff changeset
   527
val rf = ("a" | "ab") ~ ("ab" | "")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   528
println(pre_blexing(internalise(rf), "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   529
println(blexing(rf, "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   530
println(blexing_simp(rf, "ab"))
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   531
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   532
val r0 = ("a" | "ab") ~ ("b" | "")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   533
println(pre_blexing(internalise(r0), "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   534
println(blexing(r0, "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   535
println(blexing_simp(r0, "ab"))
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   536
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   537
val r1 = ("a" | "ab") ~ ("bcd" | "cd")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   538
println(blexing(r1, "abcd"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   539
println(blexing_simp(r1, "abcd"))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   540
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   541
println(blexing((("" | "a") ~ ("ab" | "b")), "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   542
println(blexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   543
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   544
println(blexing((("" | "a") ~ ("b" | "ab")), "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   545
println(blexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   546
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   547
println(blexing((("" | "a") ~ ("c" | "ab")), "ab"))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   548
println(blexing_simp((("" | "a") ~ ("c" | "ab")), "ab"))
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   549
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   550
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   551
// Sulzmann's tests
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   552
//==================
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   553
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   554
val sulzmann = ("a" | "b" | "ab").%
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   555
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   556
println(blexing(sulzmann, "a" * 10))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   557
println(blexing_simp(sulzmann, "a" * 10))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   558
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   559
for (i <- 0 to 4000 by 500) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   560
  println(i + ": " + "%.5f".format(time_needed(1, blexing_simp(sulzmann, "a" * i))))
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   561
}
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   562
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   563
for (i <- 0 to 15 by 5) {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   564
  println(i + ": " + "%.5f".format(time_needed(1, blexing_simp(sulzmann, "ab" * i))))
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   565
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   566
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   567
*/
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   568
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   569
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   570
// some automatic testing
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   571
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   572
/*
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   573
def clear() = {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   574
  print("")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   575
  //print("\33[H\33[2J")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   576
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   577
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   578
def merge[A](l1: LazyList[A], l2: LazyList[A], l3: LazyList[A]) : LazyList[A] =
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   579
  l1.head #:: l2.head #:: l3.head #:: merge(l1.tail, l2.tail, l3.tail)
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   580
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   581
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   582
// enumerates regular expressions until a certain depth
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   583
def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = {
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   584
  rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++ 
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   585
                (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   586
                (for (r1 <- rs) yield STAR(r1)))
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   587
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   588
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   589
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   590
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force.mkString("\n")
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   591
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200_000).force
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   592
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   593
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   594
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   595
import scala.util.Try
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   596
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   597
def test_mkeps(r: Rexp) = {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   598
  val res1 = Try(Some(mkeps(r))).getOrElse(None)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   599
  val res2 = Try(Some(decode(r, bmkeps(internalise(r))))).getOrElse(None) 
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   600
  if (res1 != res2) println(s"Mkeps disagrees on ${r}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   601
  if (res1 != res2) Some(r) else (None)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   602
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   603
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   604
println("Testing mkeps")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   605
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(100).exists(test_mkeps(_).isDefined)
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   606
//enum(3, "ab").map(test_mkeps).toSet
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   607
//enum(3, "abc").map(test_mkeps).toSet
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   608
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   609
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   610
//enumerates strings of length n over alphabet cs
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   611
def strs(n: Int, cs: String) : Set[String] = {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   612
  if (n == 0) Set("")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   613
  else {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   614
    val ss = strs(n - 1, cs)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   615
    ss ++
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   616
    (for (s <- ss; c <- cs.toList) yield c + s)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   617
  }
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   618
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   619
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   620
//tests lexing and lexingB
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   621
def tests_inj(ss: Set[String])(r: Rexp) = {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   622
  clear()
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   623
  println(s"Testing ${r}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   624
  for (s <- ss.par) yield {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   625
    val res1 = Try(Some(alexing(r, s))).getOrElse(None)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   626
    val res2 = Try(Some(alexing_simp(r, s))).getOrElse(None)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   627
    if (res1 != res2) println(s"Disagree on ${r} and ${s}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   628
    if (res1 != res2) println(s"   ${res1} !=  ${res2}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   629
    if (res1 != res2) Some((r, s)) else None
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   630
  }
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 158
diff changeset
   631
}
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   632
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   633
//println("Testing lexing 1")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   634
//enum(2, "ab").map(tests_inj(strs(2, "ab"))).toSet
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   635
//println("Testing lexing 2")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   636
//enum(2, "ab").map(tests_inj(strs(3, "abc"))).toSet
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   637
//println("Testing lexing 3")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   638
//enum(3, "ab").map(tests_inj(strs(3, "abc"))).toSet
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   639
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   640
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   641
def tests_alexer(ss: Set[String])(r: Rexp) = {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   642
  clear()
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   643
  println(s"Testing ${r}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   644
  for (s <- ss.par) yield {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   645
    val d = der('b', r)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   646
    val ad = bder('b', internalise(r))
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   647
    val res1 = Try(Some(encode(inj(r, 'a', alexing(d, s))))).getOrElse(None)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   648
    val res2 = Try(Some(pre_alexing(ad, s))).getOrElse(None)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   649
    if (res1 != res2) println(s"Disagree on ${r} and 'a'::${s}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   650
    if (res1 != res2) println(s"   ${res1} !=  ${res2}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   651
    if (res1 != res2) Some((r, s)) else None
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   652
  }
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   653
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   654
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   655
println("Testing alexing 1")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   656
println(enum(2, "ab").map(tests_alexer(strs(2, "ab"))).toSet)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   657
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   658
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   659
def values(r: Rexp) : Set[Val] = r match {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   660
  case ZERO => Set()
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   661
  case ONE => Set(Empty)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   662
  case CHAR(c) => Set(Chr(c))
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   663
  case ALT(r1, r2) => (for (v1 <- values(r1)) yield Left(v1)) ++ 
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   664
                      (for (v2 <- values(r2)) yield Right(v2))
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   665
  case SEQ(r1, r2) => for (v1 <- values(r1); v2 <- values(r2)) yield Sequ(v1, v2)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   666
  case STAR(r) => (Set(Stars(Nil)) ++ 
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   667
                  (for (v <- values(r)) yield Stars(List(v)))) 
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   668
    // to do more would cause the set to be infinite
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   669
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   670
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   671
def tests_bder(c: Char)(r: Rexp) = {
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   672
  val d = der(c, r)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   673
  val vals = values(d)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   674
  for (v <- vals) {
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   675
    println(s"Testing ${r} and ${v}")
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   676
    val res1 = retrieve(bder(c, internalise(r)), v)
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   677
    val res2 = encode(inj(r, c, decode(d, retrieve(internalise(der(c, r)), v))))
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   678
    if (res1 != res2) println(s"Disagree on ${r}, ${v} and der = ${d}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   679
    if (res1 != res2) println(s"   ${res1} !=  ${res2}")
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   680
    if (res1 != res2) Some((r, v)) else None
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   681
  }
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   682
}
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   683
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   684
println("Testing bder/der")
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   685
println(enum(2, "ab").map(tests_bder('a')).toSet)
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   686
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   687
val er = SEQ(ONE,CHAR('a')) 
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   688
val ev = Right(Empty) 
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   689
val ed = ALT(SEQ(ZERO,CHAR('a')),ONE)
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   690
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   691
retrieve(internalise(ed), ev) // => [true]
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   692
internalise(er)
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   693
bder('a', internalise(er))
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   694
retrieve(bder('a', internalise(er)), ev) // => []
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   695
decode(ed, List(true)) // gives the value for derivative
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   696
decode(er, List())     // gives the value for original value
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   697
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   698
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   699
val dr = STAR(CHAR('a'))
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   700
val dr_der = SEQ(ONE,STAR(CHAR('a'))) // derivative of dr
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   701
val dr_val = Sequ(Empty,Stars(List())) // value of dr_def
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   702
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   703
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   704
val res1 = retrieve(internalise(der('a', dr)), dr_val) // => [true]
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   705
val res2 = retrieve(bder('a', internalise(dr)), dr_val) // => [false, true]
286
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   706
decode(dr_der, res1) // gives the value for derivative
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   707
decode(dr, res2)     // gives the value for original value
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   708
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   709
encode(inj(dr, 'a', decode(dr_der, res1)))
804fbb227568 added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   710
359
fedc16924b76 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 294
diff changeset
   711
*/