author | Christian Urban <urbanc@in.tum.de> |
Wed, 15 Aug 2018 13:48:57 +0100 | |
changeset 286 | 804fbb227568 |
parent 204 | cd9e40280784 |
child 294 | c1de75d20aa4 |
permissions | -rw-r--r-- |
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 |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
12 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
13 |
|
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
14 |
abstract class ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
15 |
case object AZERO extends ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
16 |
case class AONE(bs: List[Boolean]) extends ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
17 |
case class ACHAR(bs: List[Boolean], c: Char) extends ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
18 |
case class AALT(bs: List[Boolean], r1: ARexp, r2: ARexp) extends ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
19 |
case class ASEQ(bs: List[Boolean], r1: ARexp, r2: ARexp) extends ARexp |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
20 |
case class ASTAR(bs: List[Boolean], r: ARexp) extends ARexp |
3
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
abstract class Val |
156
6a43ea9305ba
updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
81
diff
changeset
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
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
|
27 |
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
|
28 |
case class Stars(vs: List[Val]) extends Val |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
29 |
|
3
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 |
// 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
|
32 |
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
|
33 |
case Nil => ONE |
3
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
case c::Nil => CHAR(c) |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
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
|
36 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
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
|
38 |
|
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
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
|
40 |
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
|
41 |
def % = STAR(r) |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
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
|
43 |
} |
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 |
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
|
46 |
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
|
47 |
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
|
48 |
def % = STAR(s) |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
def ~ (r: Rexp) = SEQ(s, r) |
81
7ac7782a7318
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
78
diff
changeset
|
50 |
def ~ (r: String) = SEQ(s, r) |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
51 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
52 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
53 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
54 |
// nullable function: tests whether the regular |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
55 |
// expression can recognise the empty string |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
56 |
def nullable (r: Rexp) : Boolean = r match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
57 |
case ZERO => false |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
58 |
case ONE => true |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
59 |
case CHAR(_) => false |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
60 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
61 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
62 |
case STAR(_) => true |
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 |
// derivative of a regular expression w.r.t. a character |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
66 |
def der (c: Char, r: Rexp) : Rexp = r match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
67 |
case ZERO => ZERO |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
68 |
case ONE => ZERO |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
case SEQ(r1, r2) => |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
72 |
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
|
73 |
else SEQ(der(c, r1), r2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
74 |
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
|
75 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
76 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
77 |
// derivative w.r.t. a string (iterates der) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
78 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
79 |
case Nil => r |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
80 |
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
|
81 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
82 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
83 |
// mkeps and injection part |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
84 |
def mkeps(r: Rexp) : Val = r match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
85 |
case ONE => Empty |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
86 |
case ALT(r1, r2) => |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
case STAR(r) => Stars(Nil) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
90 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
91 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
92 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
case (CHAR(d), Empty) => Chr(c) |
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 |
// main lexing function (produces a value) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
104 |
// - no simplification |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
105 |
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
|
106 |
case Nil => if (nullable(r)) mkeps(r) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
107 |
else throw new Exception("Not matched") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
108 |
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
|
109 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
110 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
111 |
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
|
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 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
115 |
// Bitcoded + Annotation |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
116 |
//======================= |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
117 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
118 |
// translation into ARexps |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
119 |
def fuse(bs: List[Boolean], r: ARexp) : ARexp = r match { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
120 |
case AZERO => AZERO |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
121 |
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
|
122 |
case ACHAR(cs, c) => ACHAR(bs ++ cs, c) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
123 |
case AALT(cs, r1, r2) => AALT(bs ++ cs, r1, r2) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
127 |
|
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
128 |
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
|
129 |
case ZERO => AZERO |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
130 |
case ONE => AONE(Nil) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
131 |
case CHAR(c) => ACHAR(Nil, c) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
132 |
case ALT(r1, r2) => AALT(Nil, fuse(List(false), internalise(r1)), fuse(List(true), internalise(r2))) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
133 |
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
|
134 |
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
|
135 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
137 |
internalise(("a" | "ab") ~ ("b" | "")) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
138 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
139 |
def retrieve(r: ARexp, v: Val) : List[Boolean] = (r, v) match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
140 |
case (AONE(bs), Empty) => bs |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
141 |
case (ACHAR(bs, c), Chr(d)) => bs |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
142 |
case (AALT(bs, r1, r2), Left(v)) => bs ++ retrieve(r1, v) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
143 |
case (AALT(bs, r1, r2), Right(v)) => bs ++ retrieve(r2, v) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
144 |
case (ASEQ(bs, r1, r2), Sequ(v1, v2)) => |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
145 |
bs ++ retrieve(r1, v1) ++ retrieve(r2, v2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
146 |
case (ASTAR(bs, r), Stars(Nil)) => bs ++ List(true) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
147 |
case (ASTAR(bs, r), Stars(v :: vs)) => |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
148 |
bs ++ List(false) ++ retrieve(r, v) ++ retrieve(ASTAR(Nil, r), Stars(vs)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
149 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
150 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
151 |
|
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
152 |
def decode_aux(r: Rexp, bs: List[Boolean]) : (Val, List[Boolean]) = (r, bs) match { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
153 |
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
|
154 |
case (CHAR(c), bs) => (Chr(c), bs) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
155 |
case (ALT(r1, r2), false::bs) => { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
156 |
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
|
157 |
(Left(v), bs1) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
158 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
159 |
case (ALT(r1, r2), true::bs) => { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
160 |
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
|
161 |
(Right(v), bs1) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
162 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
(Sequ(v1, v2), bs2) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
167 |
} |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
168 |
case (STAR(r1), false::bs) => { |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
(Stars(v::vs), bs2) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
172 |
} |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
173 |
case (STAR(_), true::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
|
174 |
} |
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 |
def decode(r: Rexp, bs: List[Boolean]) = decode_aux(r, bs) match { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
177 |
case (v, Nil) => v |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
178 |
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
|
179 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
180 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
181 |
def encode(v: Val) : List[Boolean] = v match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
182 |
case Empty => Nil |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
183 |
case Chr(c) => Nil |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
184 |
case Left(v) => false :: encode(v) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
185 |
case Right(v) => true :: encode(v) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
186 |
case Sequ(v1, v2) => encode(v1) ::: encode(v2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
187 |
case Stars(Nil) => List(true) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
188 |
case Stars(v::vs) => false :: encode(v) ::: encode(Stars(vs)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
189 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
190 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
191 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
192 |
// 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
|
193 |
// expression can recognise the empty string |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
194 |
def anullable (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
|
195 |
case AZERO => false |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
196 |
case AONE(_) => true |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
197 |
case ACHAR(_,_) => false |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
198 |
case AALT(_, r1, r2) => anullable(r1) || anullable(r2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
199 |
case ASEQ(_, r1, r2) => anullable(r1) && anullable(r2) |
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
200 |
case ASTAR(_, _) => true |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
201 |
} |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
202 |
|
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
203 |
def mkepsBC(r: ARexp) : List[Boolean] = r match { |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
204 |
case AONE(bs) => bs |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
205 |
case AALT(bs, r1, r2) => |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
206 |
if (anullable(r1)) bs ++ mkepsBC(r1) else bs ++ mkepsBC(r2) |
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
207 |
case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2) |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
208 |
case ASTAR(bs, r) => bs ++ List(true) |
3
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
|
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
211 |
// derivative of a regular expression w.r.t. a character |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
212 |
def ader(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
|
213 |
case AZERO => AZERO |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
214 |
case AONE(_) => AZERO |
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
215 |
case ACHAR(bs, d) => if (c == d) AONE(bs) else AZERO |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
216 |
case AALT(bs, r1, r2) => AALT(bs, ader(c, r1), ader(c, r2)) |
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
217 |
case ASEQ(bs, r1, r2) => |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
218 |
if (anullable(r1)) AALT(bs, ASEQ(Nil, ader(c, r1), r2), fuse(mkepsBC(r1), ader(c, r2))) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
219 |
else ASEQ(bs, ader(c, r1), r2) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
220 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(false), ader(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
|
221 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
222 |
|
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
223 |
// 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
|
224 |
@tailrec |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
225 |
def aders (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
|
226 |
case Nil => r |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
227 |
case c::s => aders(s, ader(c, r)) |
3
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
228 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
229 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
230 |
// main unsimplified lexing function (produces a value) |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
231 |
def alex(r: ARexp, s: List[Char]) : List[Boolean] = s match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
232 |
case Nil => if (anullable(r)) mkepsBC(r) else throw new Exception("Not matched") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
233 |
case c::cs => alex(ader(c, r), cs) |
3
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
234 |
} |
94824659f6d7
added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
235 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
236 |
def pre_alexing(r: ARexp, s: String) : List[Boolean] = alex(r, s.toList) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
237 |
def alexing(r: Rexp, s: String) : Val = decode(r, pre_alexing(internalise(r), s)) |
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
238 |
|
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
239 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
240 |
def asimp(r: ARexp): ARexp = r match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
241 |
case ASEQ(bs1, r1, r2) => (asimp(r1), asimp(r2)) match { |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
242 |
case (AZERO, _) => AZERO |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
243 |
case (_, AZERO) => AZERO |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
244 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
245 |
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
|
246 |
} |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
247 |
case AALT(bs1, r1, r2) => (asimp(r1), asimp(r2)) match { |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
248 |
case (AZERO, r2s) => fuse(bs1, r2s) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
249 |
case (r1s, AZERO) => fuse(bs1, r1s) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
250 |
case (r1s, r2s) => AALT(bs1, r1s, r2s) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
251 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
252 |
case r => r |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
253 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
254 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
255 |
def alex_simp(r: ARexp, s: List[Char]) : List[Boolean] = s match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
256 |
case Nil => if (anullable(r)) mkepsBC(r) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
257 |
else throw new Exception("Not matched") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
258 |
case c::cs => alex(asimp(ader(c, r)), cs) |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
259 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
260 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
261 |
def alexing_simp(r: Rexp, s: String) : Val = |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
262 |
decode(r, alex_simp(internalise(r), s.toList)) |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
263 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
264 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
265 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
266 |
// extracts a string from value |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
267 |
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
|
268 |
case Empty => "" |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
269 |
case Chr(c) => c.toString |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
270 |
case Left(v) => flatten(v) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
271 |
case Right(v) => flatten(v) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
275 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
276 |
// extracts an environment from a value |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
277 |
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
|
278 |
case Empty => Nil |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
279 |
case Chr(c) => Nil |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
280 |
case Left(v) => env(v) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
281 |
case Right(v) => env(v) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
282 |
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
|
283 |
case Stars(vs) => vs.flatMap(env) |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
284 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
285 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
286 |
// Some Tests |
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
287 |
//============ |
75
f95a405c3180
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
288 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
(end - start)/(i * 1.0e9) |
75
f95a405c3180
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
294 |
} |
f95a405c3180
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
295 |
|
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
296 |
|
204
cd9e40280784
added paper about size derivatives
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
159
diff
changeset
|
297 |
val rf = ("a" | "ab") ~ ("ab" | "") |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
298 |
println(pre_alexing(internalise(rf), "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
299 |
println(alexing(rf, "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
300 |
println(alexing_simp(rf, "ab")) |
158
4e00dd2398ac
added bit-coded version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
157
diff
changeset
|
301 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
302 |
val r0 = ("a" | "ab") ~ ("b" | "") |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
303 |
println(pre_alexing(internalise(r0), "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
304 |
println(alexing(r0, "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
305 |
println(alexing_simp(r0, "ab")) |
75
f95a405c3180
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
74
diff
changeset
|
306 |
|
157
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
307 |
val r1 = ("a" | "ab") ~ ("bcd" | "cd") |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
308 |
println(alexing(r1, "abcd")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
309 |
println(alexing_simp(r1, "abcd")) |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
310 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
311 |
println(alexing((("" | "a") ~ ("ab" | "b")), "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
312 |
println(alexing_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
|
313 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
314 |
println(alexing((("" | "a") ~ ("b" | "ab")), "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
315 |
println(alexing_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
|
316 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
317 |
println(alexing((("" | "a") ~ ("c" | "ab")), "ab")) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
318 |
println(alexing_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
|
319 |
|
1fe44fb6d0a4
cleaned up scala code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
156
diff
changeset
|
320 |
|
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
321 |
// Sulzmann's tests |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
322 |
//================== |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
323 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
324 |
val sulzmann = ("a" | "b" | "ab").% |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
325 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
326 |
println(alexing(sulzmann, "a" * 10)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
327 |
println(alexing_simp(sulzmann, "a" * 10)) |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
328 |
|
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
329 |
for (i <- 1 to 4001 by 500) { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
330 |
println(i + ": " + "%.5f".format(time_needed(1, alexing_simp(sulzmann, "a" * i)))) |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
331 |
} |
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
332 |
|
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
333 |
for (i <- 1 to 16 by 5) { |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
334 |
println(i + ": " + "%.5f".format(time_needed(1, alexing_simp(sulzmann, "ab" * i)))) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
335 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
336 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
337 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
338 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
339 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
340 |
// some automatic testing |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
341 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
342 |
def clear() = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
343 |
print("") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
344 |
//print("\33[H\33[2J") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
345 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
346 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
347 |
// enumerates regular expressions until a certain depth |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
348 |
def enum(n: Int, s: String) : Stream[Rexp] = n match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
349 |
case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
350 |
case n => { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
351 |
val rs = enum(n - 1, s) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
352 |
rs #::: |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
353 |
(for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #::: |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
354 |
(for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #::: |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
355 |
(for (r1 <- rs) yield STAR(r1)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
356 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
357 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
358 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
359 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
360 |
//enum(2, "ab").size |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
361 |
//enum(3, "ab").size |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
362 |
//enum(3, "abc").size |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
363 |
//enum(4, "ab").size |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
364 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
365 |
import scala.util.Try |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
366 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
367 |
def test_mkeps(r: Rexp) = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
368 |
val res1 = Try(Some(mkeps(r))).getOrElse(None) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
369 |
val res2 = Try(Some(decode(r, mkepsBC(internalise(r))))).getOrElse(None) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
370 |
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
|
371 |
if (res1 != res2) Some(r) else (None) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
372 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
373 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
374 |
println("Testing mkeps") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
375 |
enum(2, "ab").map(test_mkeps).toSet |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
376 |
//enum(3, "ab").map(test_mkeps).toSet |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
377 |
//enum(3, "abc").map(test_mkeps).toSet |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
378 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
379 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
380 |
//enumerates strings of length n over alphabet cs |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
381 |
def strs(n: Int, cs: String) : Set[String] = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
382 |
if (n == 0) Set("") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
383 |
else { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
384 |
val ss = strs(n - 1, cs) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
385 |
ss ++ |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
386 |
(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
|
387 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
388 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
389 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
390 |
//tests lexing and lexingB |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
391 |
def tests_inj(ss: Set[String])(r: Rexp) = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
392 |
clear() |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
393 |
println(s"Testing ${r}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
394 |
for (s <- ss.par) yield { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
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
|
398 |
if (res1 != res2) println(s" ${res1} != ${res2}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
399 |
if (res1 != res2) Some((r, s)) else None |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
400 |
} |
159
940530087f30
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
158
diff
changeset
|
401 |
} |
286
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
402 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
403 |
//println("Testing lexing 1") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
404 |
//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
|
405 |
//println("Testing lexing 2") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
406 |
//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
|
407 |
//println("Testing lexing 3") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
408 |
//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
|
409 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
410 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
411 |
def tests_alexer(ss: Set[String])(r: Rexp) = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
412 |
clear() |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
413 |
println(s"Testing ${r}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
414 |
for (s <- ss.par) yield { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
415 |
val d = der('b', r) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
416 |
val ad = ader('b', internalise(r)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
417 |
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
|
418 |
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
|
419 |
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
|
420 |
if (res1 != res2) println(s" ${res1} != ${res2}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
421 |
if (res1 != res2) Some((r, s)) else None |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
422 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
423 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
424 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
425 |
println("Testing alexing 1") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
426 |
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
|
427 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
428 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
429 |
def values(r: Rexp) : Set[Val] = r match { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
430 |
case ZERO => Set() |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
431 |
case ONE => Set(Empty) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
432 |
case CHAR(c) => Set(Chr(c)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
433 |
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
|
434 |
(for (v2 <- values(r2)) yield Right(v2)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
435 |
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
|
436 |
case STAR(r) => (Set(Stars(Nil)) ++ |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
437 |
(for (v <- values(r)) yield Stars(List(v)))) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
438 |
// 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
|
439 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
440 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
441 |
def tests_ader(c: Char)(r: Rexp) = { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
442 |
val d = der(c, r) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
443 |
val vals = values(d) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
444 |
for (v <- vals) { |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
445 |
println(s"Testing ${r} and ${v}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
446 |
val res1 = retrieve(ader(c, internalise(r)), v) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
447 |
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
|
448 |
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
|
449 |
if (res1 != res2) println(s" ${res1} != ${res2}") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
450 |
if (res1 != res2) Some((r, v)) else None |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
451 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
452 |
} |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
453 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
454 |
println("Testing ader/der") |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
455 |
println(enum(2, "ab").map(tests_ader('a')).toSet) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
456 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
457 |
val er = SEQ(ONE,CHAR('a')) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
458 |
val ev = Right(Empty) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
459 |
val ed = ALT(SEQ(ZERO,CHAR('a')),ONE) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
460 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
461 |
retrieve(internalise(ed), ev) // => [true] |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
462 |
internalise(er) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
463 |
ader('a', internalise(er)) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
464 |
retrieve(ader('a', internalise(er)), ev) // => [] |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
465 |
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
|
466 |
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
|
467 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
468 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
469 |
val dr = STAR(CHAR('a')) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
470 |
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
|
471 |
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
|
472 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
473 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
474 |
val res1 = retrieve(internalise(der('a', dr)), dr_val) // => [true] |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
475 |
val res2 = retrieve(ader('a', internalise(dr)), dr_val) // => [false, true] |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
476 |
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
|
477 |
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
|
478 |
|
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
479 |
encode(inj(dr, 'a', decode(dr_der, res1))) |
804fbb227568
added proof for bitcoded algorithm
Christian Urban <urbanc@in.tum.de>
parents:
204
diff
changeset
|
480 |