progs/scala/re.scala
changeset 77 4b4c677501e7
parent 75 f95a405c3180
child 78 279d0bc48308
--- a/progs/scala/re.scala	Sat Apr 25 10:58:48 2015 +0100
+++ b/progs/scala/re.scala	Mon May 25 16:09:23 2015 +0100
@@ -130,6 +130,17 @@
   case Rec(_, v) => flatten(v)
 }
 
+def flattens(v: Val) : List[String] = v match {
+  case Void => List("")
+  case Chr(c) => List(c.toString)
+  case Left(v) => flattens(v)
+  case Right(v) => flattens(v)
+  case Sequ(v1, v2) => flattens(v1) ::: flattens(v2)
+  case Stars(vs) => vs.flatMap(flattens)
+  case Rec(_, v) => flattens(v)
+}
+
+
 // extracts an environment from a value
 def env(v: Val) : List[(String, String)] = v match {
   case Void => Nil
@@ -257,26 +268,37 @@
   case _ => false
 }     
 
-def tst(r: Rexp, c: Char) : Set[(Rexp, Val, Val, Val, Val)] = {
+def tst(r: Rexp, c: Char) : Set[(Rexp, Val, Val, Val, Val, List[String], List[String])] = {
+  val r_der = der(c, r)
+  val vs = values(r_der)
+  for (v1 <- vs; v2 <- vs; 
+       if (v1 != v2 && ord(v1, r_der, v2) && ord(inj(r, c, v2), r, inj(r, c, v1)) && 
+           (flatten(inj(r, c, v1)) == flatten(inj(r, c, v2))))) 
+  yield (r, v1, v2, inj(r, c, v1), inj(r, c, v2), flattens(inj(r, c, v1)), flattens(inj(r, c, v2)))  
+}
+
+def tst2(r: Rexp, c: Char) : Set[(Rexp, Val, Val, Val, Val, List[String], List[String])] = {
   val r_der = der(c, r)
   val vs = values(r_der)
   for (v1 <- vs; v2 <- vs; 
-       if (v1 != v2 && ord(v1, r_der, v2) && ord(inj(r, c, v2), r, inj(r, c, v1)))) 
-  yield (r, v1, v2, inj(r, c, v1), inj(r, c, v2))  
+       if (v1 != v2 && ord(v1, r_der, v2) && ord(proj(r, c, v2), r_der, proj(r, c, v1))) 
+  yield (r, v1, v2, proj(r, c, v1), proj(r, c, v2), flattens(inj(r, c, v1)), flattens(inj(r, c, v2)))  
 }
 
-def comp(r1: (Rexp, Val, Val, Val, Val), r2: (Rexp, Val, Val, Val, Val)) = size(r1._1) < size(r2._1)
+def comp(r1: (Rexp, Val, Val, Val, Val, List[String], List[String]), 
+         r2: (Rexp, Val, Val, Val, Val, List[String], List[String])) = size(r1._1) < size(r2._1)
 
 
-val smallest = enum(3, "a").flatMap(tst(_, 'a')).toList.sortWith { comp }.head
+val smallest = enum(3, "a").flatMap(tst(_, 'a')).toList.sortWith { comp }
 
 smallest match {
-  case (r, v1, v2, v3, v4) => List(pretty(r),
+  case Nil => "none"
+  case (r, v1, v2, v3, v4, s1, s2)::_ => List(pretty(r),
                                    pretty(der('a', r)),
                                    vpretty(v1),
                                    vpretty(v2),
                                    vpretty(v3),
-                                   vpretty(v4)).mkString("\n") }
+                                   vpretty(v4), s1, s2).mkString("\n") }
 
 // Lexing Rules for a Small While Language