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