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