progs/scala/re-simp.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 01 Apr 2016 16:29:33 +0100
changeset 158 4e00dd2398ac
parent 157 1fe44fb6d0a4
child 166 cab1ae6f339a
permissions -rw-r--r--
added bit-coded version
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
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
abstract class Rexp 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     6
case object ZERO extends Rexp
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
     7
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
     8
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
     9
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
    10
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
    11
case class STAR(r: 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 RECD(x: String, r: Rexp) extends Rexp
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
abstract class Val
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    15
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
    16
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
    17
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
    18
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
    19
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
    20
case class Stars(vs: List[Val]) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
case class Rec(x: String, v: Val) extends Val
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// some convenience for typing in regular expressions
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
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
    25
  case Nil => ONE
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  case c::Nil => CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  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
    28
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
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
    32
  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
    33
  def % = STAR(r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  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
    35
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
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
    38
  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
    39
  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
    40
  def % = STAR(s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def ~ (r: Rexp) = SEQ(s, r)
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
    42
  def ~ (r: String) = SEQ(s, r)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def $ (r: Rexp) = RECD(s, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
// nullable function: tests whether the regular 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
// expression can recognise the empty string
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
def nullable (r: Rexp) : Boolean = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    49
  case ZERO => false
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    50
  case ONE => true
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  case CHAR(_) => false
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case STAR(_) => true
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case RECD(_, r1) => nullable(r1)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
// derivative of a regular expression w.r.t. a character
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
def der (c: Char, r: Rexp) : Rexp = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    60
  case ZERO => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    61
  case ONE => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    62
  case CHAR(d) => if (c == d) ONE else ZERO
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case SEQ(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    else SEQ(der(c, r1), r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  case STAR(r) => SEQ(der(c, r), STAR(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  case RECD(_, r1) => der(c, r1)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
// 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
    72
@tailrec
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
def ders (s: List[Char], r: Rexp) : Rexp = s match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  case Nil => r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  case c::s => ders(s, der(c, r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
// extracts a string from value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
def flatten(v: Val) : String = v match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    80
  case Empty => ""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  case Chr(c) => c.toString
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    82
  case Left(v) => flatten(v)
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    83
  case Right(v) => flatten(v)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    85
  case Stars(vs) => vs.map(flatten(_)).mkString
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  case Rec(_, v) => flatten(v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
77
4b4c677501e7 proved some basic properties (totality and trichonomity) for the orderings
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 75
diff changeset
    89
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
// extracts an environment from a value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
def env(v: Val) : List[(String, String)] = v match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
    92
  case Empty => Nil
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case Chr(c) => Nil
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    94
  case Left(v) => env(v)
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    95
  case Right(v) => env(v)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case Sequ(v1, v2) => env(v1) ::: env(v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case Stars(vs) => vs.flatMap(env)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case Rec(x, v) => (x, flatten(v))::env(v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
48
652861c9473f added a function for calculating values
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   101
// injection part
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
def mkeps(r: Rexp) : Val = r match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   103
  case ONE => Empty
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  case ALT(r1, r2) => 
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   105
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  case STAR(r) => Stars(Nil)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  case RECD(x, r) => Rec(x, mkeps(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   111
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   117
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   119
  case (CHAR(d), Empty) => Chr(c) 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   123
// main unsimplified lexing function (produces a value)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
def lex(r: Rexp, s: List[Char]) : Val = s match {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  case c::cs => inj(r, c, lex(der(c, r), cs))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
}
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   132
// some "rectification" functions for simplification
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   133
def F_ID(v: Val): Val = v
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   134
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   135
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   136
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   137
  case Right(v) => Right(f2(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   138
  case Left(v) => Left(f1(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   139
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   140
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   141
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   142
}
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   143
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Empty), f2(v))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   144
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Empty))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   145
def F_RECD(f: Val => Val) = (v:Val) => v match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   146
  case Rec(x, v) => Rec(x, f(v))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   147
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   148
def F_ERROR(v: Val): Val = throw new Exception("error")
13
62fe79ee2726 fixed the scala implementation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   149
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   150
// simplification of regular expressions returning also an
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   151
// rectification function; no simplification under STAR 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   152
def simp(r: Rexp): (Rexp, Val => Val) = r match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   153
  case ALT(r1, r2) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   154
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   155
    val (r2s, f2s) = simp(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   156
    (r1s, r2s) match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   157
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   158
      case (_, ZERO) => (r1s, F_LEFT(f1s))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   159
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   160
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   161
    }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   162
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   163
  case SEQ(r1, r2) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   164
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   165
    val (r2s, f2s) = simp(r2)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   166
    (r1s, r2s) match {
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   167
      case (ZERO, _) => (ZERO, F_ERROR)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   168
      case (_, ZERO) => (ZERO, F_ERROR)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   169
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 81
diff changeset
   170
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
34
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   171
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   172
    }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   173
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   174
  case RECD(x, r1) => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   175
    val (r1s, f1s) = simp(r1)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   176
    (RECD(x, r1s), F_RECD(f1s))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   177
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   178
  case r => (r, F_ID)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   179
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   180
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   181
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   182
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   183
  case c::cs => {
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   184
    val (r_simp, f_simp) = simp(der(c, r))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   185
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   186
  }
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   187
}
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   188
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   189
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
33065bde3bbd added a file for calculating all answers...still incomplete
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 13
diff changeset
   190
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   191
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   192
// Some Tests
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   193
//============
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   194
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   195
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
   196
  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
   197
  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
   198
  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
   199
  (end - start)/(i * 1.0e9)
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   200
}
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   201
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   202
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   203
val r0 = ("a" | "ab") ~ ("b" | "")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   204
println(lexing(r0, "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   205
println(lexing_simp(r0, "ab"))
75
f95a405c3180 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 74
diff changeset
   206
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   207
val r1 = ("a" | "ab") ~ ("bcd" | "cd")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   208
println(lexing_simp(r1, "abcd"))
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   209
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   210
println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   211
println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   212
println(lexing_simp((("" | "a") ~ ("c" | "ab")), "ab"))
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   213
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   214
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 78
diff changeset
   215
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   216
// Two Simple Tests for the While Language
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   217
//========================================
78
279d0bc48308 updated the Isabelle theories with the totality proof
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 77
diff changeset
   218
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   219
// Lexing Rules 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
def PLUS(r: Rexp) = r ~ r.%
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
val ID = SYM ~ (SYM | DIGIT).% 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
val NUM = PLUS(DIGIT)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
val SEMI: Rexp = ";"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
val WHITESPACE = PLUS(" " | "\n" | "\t")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
val RPAREN: Rexp = ")"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
val LPAREN: Rexp = "("
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
val BEGIN: Rexp = "{"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
val END: Rexp = "}"
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   234
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   236
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   237
val WHILE_REGS = (("k" $ KEYWORD) | 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
                  ("i" $ ID) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
                  ("o" $ OP) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
                  ("n" $ NUM) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
                  ("s" $ SEMI) | 
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   242
                  ("str" $ STRING) |
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
                  ("p" $ (LPAREN | RPAREN)) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
                  ("b" $ (BEGIN | END)) | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
                  ("w" $ WHITESPACE)).%
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   247
/*
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
val WHILE_REGS = (KEYWORD | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
                  ID | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
                  OP | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
                  NUM | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
                  SEMI | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
                  LPAREN | RPAREN | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
                  BEGIN | END | 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
                  WHITESPACE).%
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   256
*/
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   259
println("prog0 test")
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   260
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
val prog0 = """read n"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   262
println(env(lexing_simp(WHILE_REGS, prog0)))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   264
println("prog1 test")
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   265
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
val prog1 = """read  n; write (n)"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   267
println(env(lexing_simp(WHILE_REGS, prog1)))
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   268
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   270
// Bigger Test
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   271
//=============
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
val prog2 = """
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
i := 2;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
max := 100;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
while i < max do {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
  isprime := 1;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
  j := 2;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  while (j * j) <= i + 1  do {
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
    if i % j == 0 then isprime := 0  else skip;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
    j := j + 1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
  };
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
  if isprime == 1 then write i else skip;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
  i := i + 1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
}"""
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   285
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   286
println("prog2 test - tokens")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   287
println(env(lexing_simp(WHILE_REGS, prog2)))
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   288
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   289
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   290
val prog3 = """
41
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   291
write "fib";
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   292
read n;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   293
minus1 := 0;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   294
minus2 := 1;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   295
while n > 0 do {
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   296
  temp := minus2;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   297
  minus2 := minus1 + minus2;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   298
  minus1 := temp;
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   299
  n := n - 1
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   300
};
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   301
write "result";
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   302
write minus2
4f8a9ed0f26d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 38
diff changeset
   303
"""
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   304
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   305
println("prog3 test - tokens")
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   306
println(env(lexing_simp(WHILE_REGS, prog3)))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   308
/*
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   309
for (i <- 1 to 80) {
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
  print(i.toString + ":  ")
38
b48939cca0cf slightly polished the scala file re.scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 34
diff changeset
   311
  time(lexing_simp(WHILE_REGS, prog2 * i))
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
}
157
1fe44fb6d0a4 cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   313
*/
73
6e035162345a solved one case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 66
diff changeset
   314
158
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   315
// Sulzmann's tests
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   316
//==================
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   317
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   318
val sulzmann = ("a" | "b" | "ab").%
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   319
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   320
println(lexing_simp(sulzmann, "a" * 10))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   321
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   322
for (i <- 1 to 4501 by 500) {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   323
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "a" * i))))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   324
}
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   325
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   326
for (i <- 1 to 2001 by 500) {
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   327
  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "ab" * i))))
4e00dd2398ac added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 157
diff changeset
   328
}