author | Chengsong |
Wed, 05 Feb 2020 14:50:57 +0000 | |
changeset 122 | dc0cdfc5fc66 |
parent 110 | a85c0f0fcf44 |
child 123 | fb7472a29058 |
permissions | -rwxr-xr-x |
0 | 1 |
import Element.elem |
2 |
import RexpRelated._ |
|
3 |
import RexpRelated.Rexp._ |
|
4 |
import Partial._ |
|
5 |
import BRexp._ |
|
6 |
import scala.collection.mutable.ListBuffer |
|
7 |
object Spiral{ |
|
8 |
||
9 |
val space = elem(" ") |
|
10 |
val corner = elem("+") |
|
11 |
||
12 |
def spiral(nEdges: Int, direction: Int): Element = { |
|
13 |
if(nEdges == 1) |
|
14 |
elem("+") |
|
15 |
else { |
|
16 |
val sp = spiral(nEdges - 1, (direction + 3) % 4) |
|
17 |
def verticalBar = elem('|', 1, sp.height) |
|
18 |
def horizontalBar = elem('-', sp.width, 1) |
|
19 |
if(direction == 0) |
|
20 |
(corner beside horizontalBar) above sp//(sp beside space) |
|
21 |
else if (direction == 1) |
|
22 |
sp beside (corner above verticalBar) |
|
23 |
else if (direction == 2) |
|
24 |
(space beside sp) above (horizontalBar beside corner) |
|
25 |
else |
|
26 |
(verticalBar above corner) beside (space above sp) |
|
27 |
} |
|
28 |
} |
|
29 |
val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c') |
|
30 |
def bregx_tree(r: BRexp): Element = regx_tree(berase(r)) |
|
31 |
def regx_tree(r: Rexp): Element = aregx_tree(internalise(r)) |
|
5 | 32 |
def annotated_tree(r: ARexp): Element = { |
33 |
r match { |
|
34 |
case ACHAR(bs, d) => { |
|
35 |
//val Some(d) = alphabet.find(f) |
|
36 |
d match { |
|
37 |
case '\n' => elem("\\n") |
|
38 |
case '\t' => elem("\\t") |
|
39 |
case ' ' => elem("space") |
|
7 | 40 |
case d => if(bs.isEmpty) elem(d.toString) else elem(d.toString++" ") beside elem(bs.toString) |
5 | 41 |
} |
42 |
} |
|
43 |
case AONE(bs) => { |
|
7 | 44 |
if(bs.isEmpty) elem("ONE") else elem("ONE ") beside elem(bs.toString) |
5 | 45 |
} |
46 |
case AZERO => { |
|
7 | 47 |
elem("ZERO") |
5 | 48 |
} |
49 |
case ASEQ(bs, r1, r2) => { |
|
7 | 50 |
annotated_binary_print("SEQ", r1, r2, bs) |
5 | 51 |
} |
52 |
case AALTS(bs, rs) => { |
|
53 |
//elem("Awaiting completion") |
|
7 | 54 |
annotated_list_print("ALT", rs, bs) |
5 | 55 |
} |
56 |
case ASTAR(bs, r) => { |
|
7 | 57 |
annotated_list_print("STA", List(r), bs) |
5 | 58 |
} |
59 |
} |
|
60 |
} |
|
0 | 61 |
def aregx_tree(r: ARexp): Element = { |
62 |
r match { |
|
63 |
case ACHAR(bs, d) => { |
|
64 |
//val Some(d) = alphabet.find(f) |
|
65 |
d match { |
|
66 |
case '\n' => elem("\\n") |
|
67 |
case '\t' => elem("\\t") |
|
68 |
case ' ' => elem("space") |
|
69 |
case d => elem(d.toString) |
|
70 |
} |
|
71 |
} |
|
72 |
case AONE(bs) => { |
|
73 |
elem("ONE") |
|
74 |
} |
|
75 |
case AZERO => { |
|
76 |
elem("ZERO") |
|
77 |
} |
|
78 |
case ASEQ(bs, r1, r2) => { |
|
79 |
binary_print("SEQ", r1, r2) |
|
80 |
} |
|
81 |
case AALTS(bs, rs) => { |
|
82 |
//elem("Awaiting completion") |
|
83 |
list_print("ALT", rs) |
|
84 |
} |
|
85 |
case ASTAR(bs, r) => { |
|
86 |
list_print("STA", List(r)) |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
val port = elem(" └-") |
|
91 |
def list_print(name: String, rs: List[ARexp]): Element = { |
|
92 |
rs match { |
|
93 |
case r::Nil => { |
|
94 |
val pref = aregx_tree(r) |
|
95 |
val head = elem(name) |
|
96 |
(head left_align (port up_align pref) ) |
|
97 |
} |
|
98 |
case r2::r1::Nil => { |
|
99 |
binary_print(name, r2, r1) |
|
100 |
} |
|
101 |
case r::rs1 => { |
|
102 |
val pref = aregx_tree(r) |
|
103 |
val head = elem(name) |
|
104 |
if (pref.height > 1){ |
|
105 |
val link = elem('|', 1, pref.height - 1) |
|
106 |
(head left_align ((port above link) beside pref)) left_align tail_print(rs1) |
|
107 |
} |
|
108 |
else{ |
|
109 |
(head left_align (port beside pref) ) left_align tail_print(rs1) |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
7 | 114 |
def annotated_list_print(name: String, rs: List[ARexp], bs: List[Bit]): Element = { |
115 |
rs match { |
|
116 |
case r::Nil => { |
|
117 |
val pref = annotated_tree(r) |
|
118 |
val head = if(bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString) |
|
119 |
(head left_align (port up_align pref) ) |
|
120 |
} |
|
121 |
case r2::r1::Nil => { |
|
122 |
annotated_binary_print(name, r2, r1, bs) |
|
123 |
} |
|
124 |
case r::rs1 => { |
|
125 |
val pref = annotated_tree(r) |
|
126 |
val head = if (bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString) |
|
127 |
if (pref.height > 1){ |
|
128 |
val link = elem('|', 1, pref.height - 1) |
|
129 |
(head left_align ((port above link) beside pref)) left_align annotated_tail_print(rs1) |
|
130 |
} |
|
131 |
else{ |
|
132 |
(head left_align (port beside pref) ) left_align annotated_tail_print(rs1) |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
def annotated_tail_print(rs: List[ARexp]): Element = { |
|
139 |
rs match { |
|
140 |
case r2::r1::Nil => { |
|
141 |
val pref = annotated_tree(r2) |
|
142 |
val suff = annotated_tree(r1) |
|
143 |
if (pref.height > 1){ |
|
144 |
val link = elem('|', 1, pref.height - 1) |
|
145 |
((port above link) beside pref) left_align (port up_align suff) |
|
146 |
} |
|
147 |
else{ |
|
148 |
(port beside pref) left_align (port up_align suff) |
|
149 |
} |
|
150 |
} |
|
151 |
case r2::rs1 => { |
|
152 |
val pref = annotated_tree(r2) |
|
153 |
||
154 |
if (pref.height > 1){ |
|
155 |
val link = elem('|', 1, pref.height - 1) |
|
156 |
((port above link) beside pref) left_align annotated_tail_print(rs1)//(port up_align tail_print(rs1) ) |
|
157 |
} |
|
158 |
else{ |
|
159 |
(port beside pref) left_align annotated_tail_print(rs1)//(port up_align tail_print(rs1)) |
|
160 |
} |
|
161 |
//pref left_align tail_print(rs1) |
|
162 |
} |
|
163 |
} |
|
164 |
} |
|
165 |
||
0 | 166 |
def tail_print(rs: List[ARexp]): Element = { |
167 |
rs match { |
|
168 |
case r2::r1::Nil => { |
|
169 |
val pref = aregx_tree(r2) |
|
170 |
val suff = aregx_tree(r1) |
|
171 |
if (pref.height > 1){ |
|
172 |
val link = elem('|', 1, pref.height - 1) |
|
173 |
((port above link) beside pref) left_align (port up_align suff) |
|
174 |
} |
|
175 |
else{ |
|
176 |
(port beside pref) left_align (port up_align suff) |
|
177 |
} |
|
178 |
} |
|
179 |
case r2::rs1 => { |
|
180 |
val pref = aregx_tree(r2) |
|
181 |
||
182 |
if (pref.height > 1){ |
|
183 |
val link = elem('|', 1, pref.height - 1) |
|
184 |
((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) ) |
|
185 |
} |
|
186 |
else{ |
|
187 |
(port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1)) |
|
188 |
} |
|
189 |
//pref left_align tail_print(rs1) |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
def binary_print(name: String, r1: ARexp, r2: ARexp): Element = { |
|
195 |
val pref = aregx_tree(r1) |
|
196 |
val suff = aregx_tree(r2) |
|
7 | 197 |
val head = elem(name) |
0 | 198 |
if (pref.height > 1){ |
199 |
val link = elem('|', 1, pref.height - 1) |
|
200 |
(head left_align ((port above link) beside pref) ) left_align (port up_align suff) |
|
201 |
} |
|
202 |
else{ |
|
203 |
(head left_align (port beside pref) ) left_align (port up_align suff) |
|
204 |
} |
|
205 |
} |
|
7 | 206 |
def annotated_binary_print(name: String, r1: ARexp, r2: ARexp, bs: List[Bit]): Element = { |
207 |
val pref = annotated_tree(r1) |
|
208 |
val suff = annotated_tree(r2) |
|
209 |
val head = if (bs.isEmpty) elem(name) else elem(name ++ " ") beside elem(bs.toString) |
|
210 |
if (pref.height > 1){ |
|
211 |
val link = elem('|', 1, pref.height - 1) |
|
212 |
(head left_align ((port above link) beside pref) ) left_align (port up_align suff) |
|
213 |
} |
|
214 |
else{ |
|
215 |
(head left_align (port beside pref) ) left_align (port up_align suff) |
|
216 |
} |
|
217 |
} |
|
218 |
||
0 | 219 |
val arr_of_size = ListBuffer.empty[Int] |
2 | 220 |
|
0 | 221 |
def pC(r: Rexp): Set[Rexp] = {//PD's companion |
222 |
r match { |
|
223 |
case SEQ(r1, r2) => pC(r2) |
|
224 |
case ALTS(rs) => rs.flatMap(a => pC(a) ).toSet |
|
225 |
case CHAR(c) => Set(r) |
|
226 |
case r => Set() |
|
227 |
} |
|
228 |
} |
|
229 |
def illustration(r: Rexp, s: String){ |
|
230 |
var i_like_imperative_style = internalise(r) |
|
231 |
val all_chars = s.toList |
|
232 |
for (i <- 0 to s.length - 1){ |
|
233 |
val der_res = bder(all_chars(i), i_like_imperative_style) |
|
234 |
val simp_res = bsimp(der_res) |
|
235 |
println("The original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.") |
|
236 |
println(aregx_tree(i_like_imperative_style) up_align aregx_tree(der_res) up_align aregx_tree(simp_res)) |
|
237 |
//println(asize(i_like_imperative_style), asize(der_res), asize(simp_res)) |
|
238 |
arr_of_size += asize(i_like_imperative_style) |
|
239 |
//println(asize(simp_res), asize(simp_res) / arr_of_size(0) ) |
|
240 |
i_like_imperative_style = simp_res |
|
241 |
} |
|
242 |
arr_of_size += asize(i_like_imperative_style) |
|
243 |
} |
|
244 |
val ran = scala.util.Random |
|
245 |
var alphabet_size = 3 |
|
246 |
def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = { |
|
247 |
if(depth == 1){ |
|
248 |
((ran.nextInt(4) + 97).toChar).toString |
|
249 |
} |
|
250 |
else if(star){ |
|
251 |
STAR(balanced_seq_star_gen(depth - 1, false)) |
|
252 |
} |
|
253 |
else{ |
|
254 |
SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true)) |
|
255 |
} |
|
256 |
} |
|
257 |
def max(i: Int, j: Int): Int = { |
|
258 |
if(i > j) |
|
259 |
i |
|
260 |
else |
|
261 |
j |
|
262 |
} |
|
263 |
def random_struct_gen(depth:Int): Rexp = { |
|
264 |
val dice = ran.nextInt(3) |
|
265 |
val dice2 = ran.nextInt(3) |
|
266 |
(dice, depth) match { |
|
267 |
case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString |
|
268 |
case (0, i) => STAR(random_struct_gen(max(0, i - 1 - dice2))) |
|
269 |
case (1, i) => SEQ(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2))) |
|
270 |
case (2, i) => ALTS( List(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2))) ) |
|
271 |
} |
|
272 |
} |
|
273 |
def balanced_struct_gen(depth: Int): Rexp = { |
|
274 |
val dice = ran.nextInt(3) |
|
275 |
(dice, depth) match { |
|
276 |
case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString |
|
277 |
case (0, i) => STAR(random_struct_gen(depth - 1)) |
|
278 |
case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1)) |
|
279 |
case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) ) |
|
280 |
} |
|
281 |
} |
|
6 | 282 |
def random_pool(n: Int, d: Int) : Stream[Rexp] = n match { |
283 |
case 0 => (for (i <- 1 to 10) yield balanced_struct_gen(d)).toStream |
|
284 |
case n => { |
|
285 |
val rs = random_pool(n - 1, d) |
|
286 |
rs #::: |
|
287 |
(for (i <- (1 to 10).toStream) yield balanced_struct_gen(d)) #::: |
|
288 |
(for (i <- (1 to 10).toStream) yield random_struct_gen(d)) |
|
289 |
} |
|
290 |
} |
|
291 |
||
0 | 292 |
def rd_string_gen(alp_size: Int, len: Int): String = { |
293 |
if( len > 0) |
|
294 |
((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1) |
|
295 |
else |
|
296 |
((ran.nextInt(alp_size) + 97).toChar).toString |
|
297 |
} |
|
298 |
def plot(b: List[Int]){ |
|
299 |
println(b(0),b.max) |
|
300 |
||
301 |
} |
|
302 |
def dep_exp(depth: List[Int]){ |
|
303 |
for(i <- depth){ |
|
304 |
arr_of_size.clear() |
|
305 |
val s = rd_string_gen(alphabet_size, (i-8)*(i-8)+10) |
|
306 |
val r = random_struct_gen(i) |
|
307 |
println("depth: "+i) |
|
308 |
illustration(r, s) //"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb") |
|
309 |
//println("depth: " + i + " general stats:"+ arr_of_size(0), arr_of_size.max, arr_of_size.max/arr_of_size(0)) |
|
310 |
//println("x y label alignment") |
|
311 |
/*for(i <- 0 to s.length - 1){ |
|
312 |
if(s(i) == '\n') |
|
313 |
println(i+" "+arr_of_size(i)+" "+"nl"+" -140") |
|
314 |
else if(s(i) == ' ') |
|
315 |
println(i+" "+arr_of_size(i)+" "+"sp"+" -140") |
|
316 |
else |
|
317 |
println(i+" "+arr_of_size(i)+" "+s(i)+" -140") |
|
318 |
}*/ |
|
319 |
//println(s.length + " " + arr_of_size(s.length) + " ]" + " -140") |
|
320 |
} |
|
321 |
} |
|
322 |
def case_study(ss: List[String], r: Rexp){ |
|
323 |
for(s <- ss){ |
|
324 |
arr_of_size.clear() |
|
325 |
illustration(r, s) |
|
326 |
println("x y label alignment") |
|
327 |
for(i <- 0 to s.length - 1){ |
|
328 |
if(s(i) == '\n') |
|
329 |
println(i+" "+arr_of_size(i)+" "+"nl"+" -140") |
|
330 |
else if(s(i) == ' ') |
|
331 |
println(i+" "+arr_of_size(i)+" "+"sp"+" -140") |
|
332 |
else |
|
333 |
println(i+" "+arr_of_size(i)+" "+s(i)+" -140") |
|
334 |
} |
|
335 |
} |
|
336 |
} |
|
337 |
def star_gen(dp: Int): Rexp = { |
|
338 |
if(dp > 0) |
|
339 |
STAR(star_gen(dp - 1)) |
|
340 |
else |
|
341 |
"a" |
|
342 |
} |
|
343 |
def strs_gen(len: Int, num: Int): List[String] = { |
|
344 |
if(num > 0){ |
|
345 |
rd_string_gen(3, len)::strs_gen(len, num - 1) |
|
346 |
} |
|
347 |
else{ |
|
348 |
Nil |
|
349 |
} |
|
350 |
} |
|
351 |
def regx_print(r: Rexp): String = { |
|
352 |
r match { |
|
353 |
case ZERO => |
|
354 |
"ZERO" |
|
355 |
case CHAR(c) => { |
|
356 |
//val Some(c) = alphabet.find(f) |
|
357 |
"\"" + c.toString + "\"" |
|
358 |
} |
|
359 |
case ONE => { |
|
360 |
"ONE" |
|
361 |
} |
|
362 |
case ALTS(rs) => { |
|
363 |
"ALTS(List("+(rs.map(regx_print)).foldLeft("")((a, b) => if(a == "") b else a + "," + b)+"))" |
|
364 |
} |
|
365 |
case SEQ(r1, r2) => { |
|
366 |
"SEQ(" + regx_print(r1) + "," + regx_print(r2) + ")" |
|
367 |
} |
|
368 |
case STAR(r) => { |
|
369 |
"STAR(" + regx_print(r) + ")" |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
val mkst = "abcdefghijklmnopqrstuvwxyz" |
|
374 |
def weak_sub_check(r: Rexp, s: String, i: Int, f: (List[Rexp], Set[Rexp]) => Boolean){ |
|
375 |
//we first compute pders over the set of all strings on the alphabet |
|
376 |
val pd = pderas(Set(r), i + 4) |
|
377 |
//then "b-internalise" the regular expression into a brexp(this is essentially |
|
378 |
//attaching a bit Z to every alts to signify that they come from the original regular expression) |
|
379 |
var old = brternalise(r) |
|
380 |
//this is for comparison between normal simp and the weakened version of simp |
|
381 |
//normal simp will be performed on syncold |
|
382 |
//weakend simp will be performed on old |
|
5 | 383 |
var syncold = internalise(r) |
0 | 384 |
val all_chars = s.toList |
385 |
for (i <- 0 to s.length - 1){ |
|
5 | 386 |
val syncder_res = bder(all_chars(i), syncold) |
387 |
val syncsimp_res = super_bsimp(syncder_res) |
|
0 | 388 |
//see brder for detailed explanation |
389 |
//just changes bit Z to S when deriving an ALTS, |
|
390 |
//signifying that the structure has been "touched" and |
|
391 |
//therefore able to be spilled in the bspill function |
|
392 |
val der_res = brder(all_chars(i), old) |
|
393 |
val simp_res = br_simp(der_res) |
|
394 |
val anatomy = bspill(simp_res) |
|
395 |
//track if the number of regular expressions exceeds those in the PD set(remember PD means the pders over A*) |
|
5 | 396 |
if(f(List(berase(simp_res)), pd) == false ){ |
397 |
println(size(erase(syncsimp_res))) |
|
0 | 398 |
println(size(berase(simp_res))) |
3 | 399 |
println(bregx_tree(simp_res)) |
5 | 400 |
println(s) |
401 |
println(i) |
|
402 |
println(r) |
|
0 | 403 |
println(anatomy.map(size).sum) |
404 |
println(pd.map(size).sum) |
|
405 |
} |
|
406 |
old = simp_res |
|
407 |
syncold = syncsimp_res |
|
408 |
} |
|
409 |
} |
|
410 |
def inclusion_truth(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = { |
|
411 |
val aset = anatomy.toSet |
|
412 |
if(aset subsetOf pd){ |
|
413 |
true |
|
414 |
} |
|
415 |
else{ |
|
416 |
println("inclusion not true") |
|
417 |
false |
|
418 |
} |
|
419 |
} |
|
420 |
def size_comp(anatomy: List[Rexp], pd: Set[Rexp]):Boolean = {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); true} |
|
5 | 421 |
def size_expansion_rate(r: List[Rexp], pd: Set[Rexp]): Boolean = if(size(r(0)) > (pd.map(size).sum) * 3 ) { false }else {true} |
4 | 422 |
def ders_simp(r: ARexp, s: List[Char]): ARexp = { |
423 |
s match { |
|
424 |
case Nil => r |
|
425 |
case c::cs => ders_simp(bsimp(bder(c, r)), cs) |
|
426 |
} |
|
427 |
} |
|
5 | 428 |
val big_panda = STAR(STAR(STAR(ALTS(List(ALTS(List(CHAR('c'), CHAR('b'))), SEQ(CHAR('c'),CHAR('c'))))))) |
429 |
val str_panda = "ccccb" |
|
0 | 430 |
def check_all(){ |
5 | 431 |
|
432 |
weak_sub_check(big_panda, str_panda, 6, size_expansion_rate) |
|
433 |
||
0 | 434 |
} |
93 | 435 |
def bstostick(bs: List[Bit]): Element = bs match { |
436 |
//case b::Nil => elem(b.toString) |
|
437 |
case b::bs1 => elem(b.toString) beside bstostick(bs1) |
|
438 |
case Nil => elem(' ', 1, 1) |
|
439 |
} |
|
440 |
def bits_print(r: ARexp): Element = r match { |
|
441 |
case AALTS(bs,rs) => { |
|
442 |
val bitstick = bstostick(bs) |
|
443 |
rs match { |
|
444 |
case r::rs1 => |
|
445 |
rs1.foldLeft( |
|
446 |
((elem("(") left_align bitstick) beside |
|
447 |
bits_print(r)) )( (acc, r2) => acc beside ((elem(" + ") above elem(" ")) beside bits_print(r2) )) beside |
|
448 |
elem(")") |
|
449 |
case Nil => elem(' ', 1, 1) |
|
450 |
} |
|
451 |
} |
|
452 |
case ASEQ(bs, r1, r2) => { |
|
103 | 453 |
((elem("[") left_align bstostick(bs)) beside bits_print(r1) beside elem("~") beside bits_print(r2) beside (elem("]") above elem(" "))) |
93 | 454 |
} |
455 |
case ASTAR(bs, r) => { |
|
456 |
r match { |
|
457 |
case AONE(_) | AZERO | ACHAR(_, _) => { |
|
458 |
(elem("{") left_align bstostick(bs)) beside (bits_print(r) beside elem("}*")) |
|
459 |
} |
|
460 |
case _ => { |
|
461 |
(elem("{") left_align bstostick(bs)) beside (bits_print(r) beside elem("}*")) |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
case AONE(bs) => { |
|
466 |
elem("1") left_align bstostick(bs) |
|
467 |
} |
|
468 |
case ACHAR(bs, c) => { |
|
469 |
elem(c, 1, 1) left_align bstostick(bs) |
|
470 |
} |
|
471 |
case AZERO => |
|
472 |
{ |
|
473 |
elem ("0") above elem(" ") |
|
474 |
} |
|
475 |
} |
|
476 |
def happy_print(r: Rexp): Unit = r match { |
|
477 |
case ALTS( r::rs ) => { |
|
478 |
print("(") |
|
479 |
happy_print(r) |
|
480 |
rs.map(r2=>{ |
|
481 |
print(" + ") |
|
482 |
happy_print(r2) |
|
483 |
}) |
|
484 |
print(")") |
|
485 |
} |
|
486 |
case SEQ(r1, r2) => { |
|
487 |
happy_print(r1) |
|
488 |
print("~") |
|
489 |
happy_print(r2) |
|
490 |
} |
|
491 |
case STAR(r) => { |
|
492 |
r match { |
|
493 |
case ONE | ZERO | CHAR(_) => { |
|
494 |
happy_print(r) |
|
495 |
print("*") |
|
496 |
} |
|
497 |
case _ => { |
|
498 |
print("[") |
|
499 |
happy_print(r) |
|
500 |
print("]*") |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
case ONE => { |
|
505 |
print("1") |
|
506 |
} |
|
507 |
case ZERO => { |
|
508 |
print("0") |
|
509 |
} |
|
510 |
case CHAR(c) =>{ |
|
511 |
print(c) |
|
512 |
} |
|
513 |
} |
|
514 |
def bits_slide(s: String, r: Rexp){ |
|
515 |
val nangao = ders_simp(internalise(r), s.toList) |
|
516 |
val easy = bsimp(bders(s.toList, internalise(r))) |
|
517 |
println(s) |
|
518 |
println(r) |
|
519 |
happy_print(r) |
|
520 |
println() |
|
521 |
print(bits_print(nangao)) |
|
522 |
println() |
|
523 |
print(bits_print(easy)) |
|
524 |
println() |
|
525 |
} |
|
526 |
//found r = SEQ(ALTS(List(CHAR(c), CHAR(a))),SEQ(ALTS(List(CHAR(a), CHAR(c))),ALTS(List(CHAR(b), CHAR(c))))) s = "aa" |
|
527 |
def bsimp_print(r: ARexp): Unit = r match { |
|
528 |
case ASEQ(bs, r1, r2) => |
|
529 |
{ |
|
530 |
println("0.r or r.0 to 0 or bs1 1.r to fuse bs++bs1 r") |
|
531 |
bits_print(bsimp(r1)) |
|
532 |
bits_print(bsimp(r2)) |
|
533 |
} |
|
534 |
case AALTS(bs, rs) => { |
|
535 |
println("rs.map(bsimp) equals *************") |
|
536 |
val rs_simp = rs.map(bsimp) |
|
537 |
for(r <- rs_simp){ |
|
538 |
println(bits_print(r)) |
|
539 |
} |
|
540 |
println("*************") |
|
541 |
println("flts(rs_simp)") |
|
542 |
val flat_res = flats(rs_simp) |
|
543 |
for(r <- flat_res){ |
|
544 |
println(bits_print(r)) |
|
545 |
} |
|
546 |
println("dB(flat_res)") |
|
547 |
val dist_res = distinctBy(flat_res, erase) |
|
548 |
for(r <- dist_res){ |
|
549 |
println(bits_print(r)) |
|
550 |
} |
|
551 |
dist_res match { |
|
552 |
case Nil => println("AZERO") |
|
553 |
case r::Nil => println("fuse(bs, r)") |
|
554 |
case rs => println("AALTS(bs, rs)") |
|
555 |
} |
|
556 |
} |
|
557 |
case _ => println("No simp at this level") |
|
558 |
} |
|
559 |
def tellmewhy(){ |
|
560 |
//val r = SEQ(ALTS(List(CHAR('a'), CHAR('b'))),ALTS(List(ALTS(List(CHAR('a'), CHAR('a'))), STAR(CHAR('a'))))) |
|
107 | 561 |
//val r = SEQ(ALTS(List(CHAR('a'), CHAR('b'))),ALTS(List(CHAR('a'), STAR(CHAR('a')) ) )) |
562 |
val r = ("ab" | ( (("a")%) | "aa") ) |
|
93 | 563 |
//val r = ("a"|"b")~("a") |
564 |
val s = "aa" |
|
122 | 565 |
for(i <- 1 to s.length-1){ |
93 | 566 |
val ss = s.slice(0, i+1) |
107 | 567 |
val nangao = bders_simp_rf(ss.toList, internalise(r)) |
93 | 568 |
val easy = (bders(ss.toList, internalise(r))) |
569 |
println(bits_print(nangao)) |
|
107 | 570 |
println() |
93 | 571 |
println(bits_print(easy)) |
572 |
println() |
|
107 | 573 |
println(bits_print(bsimp_rf(easy))) |
574 |
println() |
|
93 | 575 |
} |
576 |
println(bits_print(bsimp(bders(s.toList, internalise(r))))) |
|
101 | 577 |
println(bits_print(ders_simp(internalise(r), s.toList))) |
93 | 578 |
} |
579 |
def find_re(){ |
|
580 |
for (i <- 1 to 10000){ |
|
581 |
val r = balanced_struct_gen(3) |
|
582 |
val s = rd_string_gen(2,1) |
|
583 |
val nangao = ders_simp(internalise(r), s.toList) |
|
584 |
val easy = bsimp(bders(s.toList, internalise(r))) |
|
585 |
if(nangao != easy){ |
|
586 |
bits_slide(s, r) |
|
587 |
} |
|
588 |
} |
|
589 |
} |
|
5 | 590 |
//simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set) |
10 | 591 |
def pushbits(r: ARexp): ARexp = r match { |
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
10
diff
changeset
|
592 |
case AALTS(bs, rs) => AALTS(Nil, rs.map(r=>fuse(bs, pushbits(r)))) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
10
diff
changeset
|
593 |
case ASEQ(bs, r1, r2) => ASEQ(bs, pushbits(r1), pushbits(r2)) |
10 | 594 |
case r => r |
595 |
} |
|
4 | 596 |
def correctness_proof_convenient_path(){ |
107 | 597 |
for(i <- 1 to 19999){ |
598 |
val s = rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3) |
|
599 |
val r = internalise(random_struct_gen(4))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) //random_struct_gen(7) |
|
4 | 600 |
for(j <- 0 to s.length - 1){ |
601 |
val ss = s.slice(0, j+ 1) |
|
107 | 602 |
val nangao = bders_simp_rf(ss.toList, r) |
603 |
val easy = bsimp_rf(bders_rf(ss.toList, r)) |
|
604 |
if(!(nangao == easy)){ |
|
4 | 605 |
println(j) |
10 | 606 |
println("not equal") |
5 | 607 |
println("string") |
4 | 608 |
println(ss) |
5 | 609 |
println("original regex") |
107 | 610 |
println(annotated_tree(r)) |
5 | 611 |
println("regex after ders simp") |
10 | 612 |
println(annotated_tree(nangao)) |
5 | 613 |
println("regex after ders") |
107 | 614 |
println(annotated_tree(bders(ss.toList, r)))//flats' fuse when opening up AALTS causes the difference |
5 | 615 |
println("regex after ders and then a single simp") |
7 | 616 |
println(annotated_tree(easy)) |
4 | 617 |
} |
618 |
} |
|
107 | 619 |
} |
4 | 620 |
} |
13 | 621 |
/* if(bts == cdbts){//test of equality code v = retrieve internalise(r) v if |- v : r |
622 |
println(bts) |
|
623 |
println(cdbts) |
|
624 |
println("code v = retrieve internalise(r) v if |- v : r") |
|
625 |
} |
|
14 | 626 |
|
627 |
||
628 |
val r_der_s = bders(st.toList, rg) |
|
629 |
println(aregx_tree(r_der_s)) |
|
630 |
val bts = retrieve(r_der_s, unsimplified_vl) |
|
631 |
val cdbts = code(unsimplified_vl) |
|
632 |
val simprg = bsimp(r_der_s) |
|
633 |
val frectv = decode(erase(simprg), cdbts) |
|
634 |
val simpbts = retrieve(simprg, frectv) |
|
635 |
if(bts == simpbts){ |
|
636 |
println("retrieve r v = retrieve (bsimp r) (decode bsimp(r) code(v))") |
|
637 |
println("bits:") |
|
638 |
//println(bts) |
|
639 |
println(simpbts) |
|
640 |
println("v = ") |
|
641 |
println(unsimplified_vl) |
|
642 |
println("frect v = ") |
|
643 |
println(frectv) |
|
644 |
} |
|
17 | 645 |
*///KH8W5BXL |
646 |
def nice_lex(r: Rexp, s: List[Char], ar: ARexp) : Val = s match { |
|
647 |
case Nil => |
|
648 |
if (nullable(r)){ |
|
649 |
val vr = mkeps(r) |
|
650 |
val bits1 = retrieve(ar, vr) |
|
651 |
val av = bsimp2(ar, vr) |
|
652 |
val bits2 = retrieve(av._1, av._2) |
|
653 |
if(bits1 != bits2) throw new Exception("bsimp2 does not work") |
|
654 |
vr |
|
655 |
} |
|
656 |
else throw new Exception("Not matched") |
|
657 |
case c::cs => { |
|
658 |
val vr = inj(r, c, nice_lex(der(c, r), cs, bder(c, ar))); |
|
659 |
val bits1 = retrieve(ar, vr); |
|
660 |
val av = bsimp2(ar, vr); |
|
661 |
val bits2 = retrieve(av._1, av._2); |
|
662 |
if(bits1 != bits2) throw new Exception("bsimp2 does not work"); |
|
663 |
vr |
|
664 |
} |
|
665 |
} |
|
666 |
def test_bsimp2(){ |
|
667 |
for(i <- 1 to 1000){ |
|
668 |
val rg = (balanced_struct_gen(9))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) |
|
669 |
val st = rd_string_gen(3, 4) |
|
670 |
val a = internalise(rg) |
|
671 |
val final_derivative = ders(st.toList, rg) |
|
672 |
if(nullable(final_derivative)) |
|
673 |
nice_lex(rg, st.toList, a) |
|
674 |
} |
|
675 |
} |
|
15 | 676 |
|
17 | 677 |
def neat_retrieve(){ |
678 |
for(i <- 1 to 1000){ |
|
679 |
val rg = internalise(balanced_struct_gen(6))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) |
|
680 |
val st = rd_string_gen(3, 5) |
|
681 |
val final_derivative = ders(st.toList, erase(rg)) |
|
682 |
if(nullable(final_derivative)){ |
|
683 |
val unsimplified_vl = mkeps(final_derivative) |
|
684 |
val arexp_for_retrieve = bders( st.toList, rg) |
|
685 |
val simp_ar_vl = bsimp2(arexp_for_retrieve, unsimplified_vl) |
|
686 |
val bits1 = retrieve(arexp_for_retrieve, unsimplified_vl) |
|
687 |
val bits2 = retrieve(simp_ar_vl._1, simp_ar_vl._2) |
|
688 |
if(bits1 != bits2){ |
|
689 |
println("nOOOOOOOOOO!") |
|
15 | 690 |
} |
691 |
} |
|
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
10
diff
changeset
|
692 |
} |
17 | 693 |
} |
694 |
||
7 | 695 |
def radical_correctness(){ |
696 |
enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet |
|
697 |
random_pool(1, 5).map(tests_blexer_simp(strs(5, "abc"))).toSet |
|
698 |
} |
|
15 | 699 |
def christian_def(){ |
700 |
val r = ALTS(List(SEQ(ZERO,CHAR('b')), ONE)) |
|
701 |
val v = Right(Empty) |
|
702 |
val a = internalise(r) |
|
17 | 703 |
val a_v = bsimp2(a,v) |
15 | 704 |
println(s"Testing ${r} and ${v}") |
705 |
println(s"internalise(r) = ${a}") |
|
17 | 706 |
println(s"a_v = ${a_v}") |
15 | 707 |
val bs1 = retrieve(a, v) |
708 |
println(bs1) |
|
17 | 709 |
println(s"as = ${a_v._1}") |
15 | 710 |
//val bs2 = retrieve(as, decode(erase(as), bs1)) |
17 | 711 |
val bs3 = retrieve(a_v._1, a_v._2)//decode(erase(as), bs1) does not work |
712 |
//println(decode(erase(as), bs1)) |
|
713 |
println(s"bs1=${bs1}, bs3=${bs3}") |
|
714 |
//println(decode(erase(a_v._1), bs3)) |
|
15 | 715 |
} |
72 | 716 |
def christian_def2(){ |
89 | 717 |
val a = AALTS(List(S), List(AZERO, ASEQ(List(S), AALTS(List(S), List(AONE(List(S)), ACHAR(List(S), 'c'))), ACHAR(List(S),'c') )) ) |
718 |
//AALTS(List(Z), List(AZERO, ASEQ(List(), AALTS(List(),List(AONE(List()), ACHAR(Nil, 'b'))), ACHAR(Nil, 'b')) ) ) |
|
719 |
val unsimp = bsimp(bder('c',a)) |
|
720 |
val simped = bsimp(bder('c', bsimp(a)) ) |
|
72 | 721 |
println(bsimp(a)) |
89 | 722 |
if(unsimp != simped){ |
72 | 723 |
println(s"bsimp(bder c r) = ${unsimp}, whereas bsimp(bder c bsimp r) = ${simped}") |
724 |
} |
|
725 |
} |
|
15 | 726 |
def essence_posix(){ |
16 | 727 |
//val s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"//rd_string_gen(alphabet_size, 3)//"abaa"//rd_string_gen(alphabet_size, 3) |
728 |
val s0 = "a" |
|
729 |
val r = SEQ(STAR(ALT("a", "aa")), "b")//internalise(random_struct_gen(4))//ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) //random_struct_gen(7) |
|
730 |
for(i <- 1 to 40){ |
|
731 |
val s = s0*i |
|
732 |
//printf("%d %d\n",i, size(ders(s.toList, r))) |
|
733 |
printf("%d %d\n",i, asize(ders_simp( internalise(r), s.toList))) |
|
734 |
//println(asize(ders_simp( internalise(r), s.toList))) |
|
735 |
} |
|
15 | 736 |
} |
16 | 737 |
|
87 | 738 |
def speed_test(){ |
739 |
val s0 = "a"*1000 |
|
740 |
val r = SEQ(STAR("a"), "b") |
|
741 |
for(i <- 1 to 30){ |
|
742 |
val s = s0*i |
|
743 |
val start = System.nanoTime() |
|
744 |
try{ |
|
745 |
blex_simp(internalise(r), s.toList) |
|
746 |
} |
|
747 |
catch{ |
|
748 |
case x: Exception => |
|
749 |
} |
|
750 |
val end = System.nanoTime() |
|
751 |
printf("%d %f\n",i, (end - start)/1.0e9) |
|
752 |
} |
|
753 |
} |
|
91 | 754 |
/* |
755 |
lemma retrieve_encode_STARS: |
|
756 |
assumes "∀v∈set vs. ⊨ v : r ∧ code v = retrieve (intern r) v" |
|
757 |
shows "code (Stars vs) = retrieve (ASTAR [] (intern r)) (Stars vs)" |
|
758 |
*/ |
|
759 |
def retrieve_encode_STARS(){ |
|
760 |
val r = ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('b')) ) |
|
761 |
//val v = Stars(List(Sequ(Chr('a'), Chr('b')), Chr('a')) ) |
|
762 |
val v1 = Right(Sequ(Chr('a'), Chr('b'))) |
|
763 |
val v2 = Left(Chr('a')) |
|
764 |
val compressed1 = code(v1) |
|
765 |
val compressed2 = code(v2) |
|
766 |
val xompressed1 = retrieve(internalise(r), v1) |
|
767 |
val xompressed2 = retrieve(internalise(r), v2) |
|
768 |
println(compressed1, compressed2, xompressed1, xompressed2) |
|
769 |
val v = Stars(List(v1, v2)) |
|
770 |
val compressed = code(v) |
|
771 |
val a = ASTAR(List(), internalise(r)) |
|
772 |
val xompressed = retrieve(a, v) |
|
773 |
println(compressed, xompressed) |
|
774 |
} |
|
775 |
//what does contains7 do? |
|
776 |
//it causes exception |
|
777 |
//relation between retreive, bder and injection |
|
778 |
// a match error occurs when doing the injection |
|
779 |
def contains7(){ |
|
780 |
val r = STAR( SEQ(CHAR('a'),CHAR('b')) ) |
|
781 |
val v = Sequ(Chr('b'), Stars(List(Sequ(Chr('a'), Chr('b'))))) |
|
782 |
val a = internalise(r) |
|
783 |
val c = 'a' |
|
784 |
val v_aug = inj(r, c, v) |
|
785 |
println("bder c r:") |
|
786 |
println(bder(c, a)) |
|
787 |
println("r:") |
|
788 |
println(a) |
|
789 |
println("bits they can both produce:") |
|
790 |
println(retrieve(a, v_aug)) |
|
791 |
} |
|
792 |
def der_seq(r:ARexp, s: List[Char],acc: List[ARexp]) : List[ARexp] = s match{ |
|
793 |
case Nil => acc ::: List(r) |
|
794 |
case c::cs => der_seq(bder(c, r), cs, acc ::: List(r)) |
|
795 |
} |
|
796 |
def der_seq_rev(r:ARexp, s: List[Char], acc: List[ARexp]): List[ARexp] = s match{ |
|
797 |
case Nil => r::acc |
|
798 |
case c::cs =>der_seq_rev(r, cs, bders(s, r) :: acc ) |
|
799 |
} |
|
800 |
def re_close(l1: List[ARexp], l2: List[ARexp], re_init: ARexp): ARexp = l1 match { |
|
801 |
case Nil => re_init |
|
802 |
case c::cs => if(bnullable(c)) re_close(cs, l2.tail, AALTS(List(), List(re_init, fuse(mkepsBC(c), l2.head)) ) ) |
|
803 |
else re_close(cs, l2.tail, re_init) |
|
804 |
} |
|
92 | 805 |
//HERE |
91 | 806 |
def closed_string_der(r1: ARexp, r2: ARexp, s: String): ARexp = { |
807 |
val l1 = der_seq(r1, s.toList, Nil) |
|
808 |
val l2 = der_seq_rev(r2, s.toList, Nil) |
|
93 | 809 |
val Re = re_close((l1.reverse).tail, l2.tail, ASEQ(List(), l1.last, l2.head)) |
91 | 810 |
print(Re) |
811 |
val Comp = bders( s.toList, ASEQ(List(), r1, r2)) |
|
812 |
print(Comp ) |
|
813 |
if(Re != Comp){ |
|
814 |
println("boooooooooooooooo!joihniuguyfuyftyftyufuyids gheioueghrigdhxilj") |
|
815 |
} |
|
816 |
Re |
|
817 |
} |
|
818 |
def newxp1(){ |
|
819 |
val r1 = internalise("ab"|"") |
|
820 |
val r2 = internalise(("b")%) |
|
821 |
val s = "abbbbb" |
|
822 |
val s2= "bbbbb" |
|
823 |
val s3 = "aaaaaa" |
|
824 |
//closed_string_der(r1, r2, s) |
|
825 |
//closed_string_der(r1, r2, s2) |
|
826 |
closed_string_der(r1, r2, s3) |
|
827 |
} |
|
92 | 828 |
|
829 |
def string_der_test(){ |
|
830 |
for(i <- 0 to 10){ |
|
831 |
val s = rd_string_gen(alphabet_size, i).toList |
|
832 |
val r = random_struct_gen(2) |
|
833 |
if(ders(s, r) != ders2(s, r)){ |
|
834 |
println(i) |
|
835 |
println(s) |
|
836 |
println(r) |
|
837 |
println(ders(s, r)) |
|
838 |
println(ders2(s,r)) |
|
839 |
println("neq") |
|
840 |
} |
|
841 |
} |
|
842 |
} |
|
109 | 843 |
def have_fun(){ |
844 |
val bis = List(S,S) |
|
845 |
val bits = List(S,S,Z) |
|
846 |
val reg = ("a" | (("a")%) )~("b") |
|
847 |
val res = decode_aux(reg, bis) |
|
848 |
val result = decode_aux(reg, bis) |
|
849 |
val result1 = decode_aux(reg, List(Z)) |
|
850 |
println(res) |
|
851 |
println(result) |
|
852 |
println(bsimp(bders( "a".toList, internalise(reg)))) |
|
853 |
println(result1) |
|
854 |
} |
|
0 | 855 |
def main(args: Array[String]) { |
93 | 856 |
//println(S.toString) |
857 |
//find_re() |
|
107 | 858 |
//tellmewhy() |
859 |
//correctness_proof_convenient_path() |
|
110 | 860 |
tellmewhy() |
861 |
//have_fun() |
|
93 | 862 |
//string_der_test() |
92 | 863 |
//comp(rd_string_gen(3,6).toList, random_struct_gen(7)) |
864 |
//newxp1() |
|
91 | 865 |
//contains7() |
866 |
//retrieve_encode_STARS() |
|
4 | 867 |
//check_all() |
7 | 868 |
//radical_correctness() |
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
10
diff
changeset
|
869 |
//correctness_proof_convenient_path() |
15 | 870 |
//retrieve_experience() |
17 | 871 |
//neat_retrieve() |
72 | 872 |
//test_bsimp2() |
91 | 873 |
//christian_def2() |
16 | 874 |
//christian_def() |
17 | 875 |
//essence_posix() |
89 | 876 |
//speed_test() |
0 | 877 |
} |
878 |
} |
|
93 | 879 |
//List( ASTAR(List(Z),ACHAR(List(),a)), AALTS(List(S),List(ACHAR(List(Z),b), ACHAR(List(S),a))) ) |