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