added size bounds for partial derivatives
authorChristian Urban <urbanc@in.tum.de>
Wed, 20 Feb 2019 00:00:30 +0000
changeset 312 8b0b414e71b0
parent 311 8b8db9558ecf
child 313 3b8e3a156200
added size bounds for partial derivatives
exps/antimirov.scala
exps/both.scala
progs/scala/re.scala
progs/scala/tests.scala
thys/PDerivs.thy
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/exps/antimirov.scala	Wed Feb 20 00:00:30 2019 +0000
@@ -0,0 +1,402 @@
+
+import scala.language.implicitConversions    
+import scala.language.reflectiveCalls
+import scala.annotation.tailrec   
+import scala.util.Try
+
+def escape(raw: String) : String = {
+  import scala.reflect.runtime.universe._
+  Literal(Constant(raw)).toString
+}
+
+def esc2(r: (String, String)) = (escape(r._1), escape(r._2))
+
+
+
+// usual regular expressions
+abstract class Rexp 
+case object ZERO extends Rexp
+case object ONE extends Rexp
+case class CHAR(c: Char) extends Rexp
+case class ALTS(rs: List[Rexp]) extends Rexp 
+case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
+case class STAR(r: Rexp) extends Rexp 
+case class RECD(x: String, r: Rexp) extends Rexp
+
+// abbreviations
+def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
+
+// values
+abstract class Val
+case object Empty extends Val
+case class Chr(c: Char) extends Val
+case class Sequ(v1: Val, v2: Val) extends Val
+case class Left(v: Val) extends Val
+case class Right(v: Val) extends Val
+case class Stars(vs: List[Val]) extends Val
+case class Rec(x: String, v: Val) extends Val
+
+
+
+// some convenience for typing in regular expressions
+def charlist2rexp(s : List[Char]): Rexp = s match {
+  case Nil => ONE
+  case c::Nil => CHAR(c)
+  case c::s => SEQ(CHAR(c), charlist2rexp(s))
+}
+implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
+
+implicit def RexpOps(r: Rexp) = new {
+  def | (s: Rexp) = ALT(r, s)
+  def % = STAR(r)
+  def ~ (s: Rexp) = SEQ(r, s)
+}
+
+implicit def stringOps(s: String) = new {
+  def | (r: Rexp) = ALT(s, r)
+  def | (r: String) = ALT(s, r)
+  def % = STAR(s)
+  def ~ (r: Rexp) = SEQ(s, r)
+  def ~ (r: String) = SEQ(s, r)
+  def $ (r: Rexp) = RECD(s, r)
+}
+
+
+// string of a regular expressions - for testing purposes
+def string(r: Rexp): String = r match {
+  case ZERO => "0"
+  case ONE => "1"
+  case CHAR(c) => c.toString
+  case ALTS(rs) => rs.map(string).mkString("[", "|", "]")
+  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
+  case STAR(r) => s"{${string(r)}}*"
+  case RECD(x, r) => s"(${x}! ${string(r)})"
+}
+
+
+//--------------------------------------------------------------
+// START OF NON-BITCODE PART
+//
+
+// nullable function: tests whether the regular 
+// expression can recognise the empty string
+def nullable (r: Rexp) : Boolean = r match {
+  case ZERO => false
+  case ONE => true
+  case CHAR(_) => false
+  case ALTS(rs) => rs.exists(nullable)
+  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
+  case STAR(_) => true
+  case RECD(_, r) => nullable(r)
+}
+
+// derivative of a regular expression w.r.t. a character
+def der (c: Char, r: Rexp) : Rexp = r match {
+  case ZERO => ZERO
+  case ONE => ZERO
+  case CHAR(d) => if (c == d) ONE else ZERO
+  case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2)))
+  case SEQ(r1, r2) => 
+    if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2)))
+    else SEQ(der(c, r1), r2)
+  case STAR(r) => SEQ(der(c, r), STAR(r))
+  case RECD(_, r1) => der(c, r1)
+}
+
+
+def flatten(v: Val) : String = v match {
+  case Empty => ""
+  case Chr(c) => c.toString
+  case Left(v) => flatten(v)
+  case Right(v) => flatten(v)
+  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
+  case Stars(vs) => vs.map(flatten).mkString
+  case Rec(_, v) => flatten(v)
+}
+
+// extracts an environment from a value
+def env(v: Val) : List[(String, String)] = v match {
+  case Empty => Nil
+  case Chr(c) => Nil
+  case Left(v) => env(v)
+  case Right(v) => env(v)
+  case Sequ(v1, v2) => env(v1) ::: env(v2)
+  case Stars(vs) => vs.flatMap(env)
+  case Rec(x, v) => (x, flatten(v))::env(v)
+}
+
+
+// injection part
+def mkeps(r: Rexp) : Val = r match {
+  case ONE => Empty
+  case ALTS(List(r1, r2)) => 
+    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
+  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
+  case STAR(r) => Stars(Nil)
+  case RECD(x, r) => Rec(x, mkeps(r))
+}
+
+def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
+  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
+  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
+  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
+  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
+  case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1))
+  case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2))
+  case (CHAR(_), Empty) => Chr(c) 
+  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
+}
+
+// lexing without simplification
+def lex(r: Rexp, s: List[Char]) : Val = s match {
+  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
+  case c::cs => inj(r, c, lex(der(c, r), cs))
+}
+
+def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
+
+//println(lexing(("ab" | "ab") ~ ("b" | ONE), "ab"))
+
+// some "rectification" functions for simplification
+def F_ID(v: Val): Val = v
+def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
+def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
+def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
+  case Right(v) => Right(f2(v))
+  case Left(v) => Left(f1(v))
+}
+def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
+  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
+}
+def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
+  (v:Val) => Sequ(f1(Empty), f2(v))
+def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
+  (v:Val) => Sequ(f1(v), f2(Empty))
+def F_RECD(f: Val => Val) = (v:Val) => v match {
+  case Rec(x, v) => Rec(x, f(v))
+}
+def F_ERROR(v: Val): Val = throw new Exception("error")
+
+// simplification of regular expressions returning also an
+// rectification function; no simplification under STAR 
+def simp(r: Rexp): (Rexp, Val => Val) = r match {
+  case ALTS(List(r1, r2)) => {
+    val (r1s, f1s) = simp(r1)
+    val (r2s, f2s) = simp(r2)
+    (r1s, r2s) match {
+      case (ZERO, _) => (r2s, F_RIGHT(f2s))
+      case (_, ZERO) => (r1s, F_LEFT(f1s))
+      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
+                else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) 
+    }
+  }
+  case SEQ(r1, r2) => {
+    val (r1s, f1s) = simp(r1)
+    val (r2s, f2s) = simp(r2)
+    (r1s, r2s) match {
+      case (ZERO, _) => (ZERO, F_ERROR)
+      case (_, ZERO) => (ZERO, F_ERROR)
+      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
+      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
+      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
+    }
+  }
+  case RECD(x, r1) => {
+    val (r1s, f1s) = simp(r1)
+    (RECD(x, r1s), F_RECD(f1s))
+  }
+  case r => (r, F_ID)
+}
+
+def ders_simp(s: List[Char], r: Rexp) : Rexp = s match {
+  case Nil => r
+  case c::s => ders_simp(s, simp(der(c, r))._1)
+}
+
+
+def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
+  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
+  case c::cs => {
+    val (r_simp, f_simp) = simp(der(c, r))
+    inj(r, c, f_simp(lex_simp(r_simp, cs)))
+  }
+}
+
+def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
+
+//println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab"))
+
+
+def tokenise_simp(r: Rexp, s: String) = 
+  env(lexing_simp(r, s)).map(esc2)
+
+//--------------------------------------------------------------------
+// Partial Derivatives
+
+
+def pder(c: Char, r: Rexp): Set[Rexp] = r match {
+  case ZERO => Set()
+  case ONE => Set()
+  case CHAR(d) => if (c == d) Set(ONE) else Set()
+  case ALTS(rs) => rs.toSet.flatMap(pder(c, _))
+  case SEQ(r1, r2) =>
+    (for (pr1 <- pder(c, r1)) yield SEQ(pr1, r2)) ++
+    (if (nullable(r1)) pder(c, r2) else Set())
+  case STAR(r1) =>
+    for (pr1 <- pder(c, r1)) yield SEQ(pr1, STAR(r1))
+  case RECD(_, r1) => pder(c, r1)
+}
+
+def pders(cs: List[Char], r: Rexp): Set[Rexp] = cs match {
+  case Nil => Set(r)
+  case c::cs => pder(c, r).flatMap(pders(cs, _))
+}
+
+def pders_simp(cs: List[Char], r: Rexp): Set[Rexp] = cs match {
+  case Nil => Set(r)
+  case c::cs => pder(c, r).flatMap(pders_simp(cs, _)).map(simp(_)._1)
+}
+
+def psize(rs: Set[Rexp])  = 
+  rs.map(size).sum
+
+
+// A simple parser for regexes
+
+case class Parser(s: String) {
+  var i = 0
+  
+  def peek() = s(i)
+  def eat(c: Char) = 
+    if (c == s(i)) i = i + 1 else throw new Exception("Expected " + c + " got " + s(i))
+  def next() = { i = i + 1; s(i - 1) }
+  def more() = s.length - i > 0
+
+  def Regex() : Rexp = {
+    val t = Term();
+    if (more() && peek() == '|') {
+      eat ('|') ; 
+      ALT(t, Regex()) 
+    } 
+    else t
+  }
+
+  def Term() : Rexp = {
+    var f : Rexp = 
+      if (more() && peek() != ')' && peek() != '|') Factor() else ONE;
+    while (more() && peek() != ')' && peek() != '|') {
+      f = SEQ(f, Factor()) ;
+    }
+    f
+  }
+
+  def Factor() : Rexp = {
+    var b = Base();
+    while (more() && peek() == '*') {
+      eat('*') ;
+      b = STAR(b) ;
+    }
+    while (more() && peek() == '?') {
+      eat('?') ;
+      b = ALT(b, ONE) ;
+    }
+    while (more() && peek() == '+') {
+      eat('+') ;
+      b = SEQ(b, STAR(b)) ;
+    }
+    b
+  }
+
+  def Base() : Rexp = {
+    peek() match {
+      case '(' => { eat('(') ; val r = Regex(); eat(')') ; r }   // if groups should be groups RECD("",r) }
+      case _ => CHAR(next())
+    }
+  }
+}
+
+// two simple examples for the regex parser
+
+println("two simple examples for the regex parser")
+
+println(string(Parser("a|(bc)*").Regex()))
+println(string(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex()))
+
+
+
+//System.exit(0)
+
+//   Testing
+//============
+
+def time[T](code: => T) = {
+  val start = System.nanoTime()
+  val result = code
+  val end = System.nanoTime()
+  ((end - start)/1.0e9).toString
+  //result
+}
+
+def timeR[T](code: => T) = {
+  val start = System.nanoTime()
+  for (i <- 1 to 10) code
+  val result = code
+  val end = System.nanoTime()
+  (result, (end - start))
+}
+
+//size: of a Aregx for testing purposes 
+def size(r: Rexp) : Int = r match {
+  case ZERO => 1
+  case ONE => 1
+  case CHAR(_) => 1
+  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
+  case ALTS(rs) => 1 + rs.map(size).sum
+  case STAR(r) => 1 + size(r)
+  case RECD(_, r) => size(r)
+}
+
+//enumerates strings of length n over alphabet cs
+def strs(n: Int, cs: String) : Set[String] = {
+  if (n == 0) Set("")
+  else {
+    val ss = strs(n - 1, cs)
+    ss ++
+    (for (s <- ss; c <- cs.toList) yield c + s)
+  }
+}
+
+def enum(n: Int, s: String) : Stream[Rexp] = n match {
+  case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR)
+  case n => {  
+    val rs = enum(n - 1, s)
+    rs #:::
+    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
+    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
+    (for (r1 <- rs) yield STAR(r1))
+  }
+}
+
+
+
+
+println("Antimirov Example 5.5")
+
+val antimirov = Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex()
+val strings = strs(6, "ab")
+val pds = strings.flatMap(s => pders(s.toList, antimirov))
+val pds_simplified = pds.map(simp(_)._1)
+
+
+println("Unsimplified set")
+println(pds.map(string).mkString("\n"))
+println("Number of pds  " +  pds.size)
+println("\nSimplified set")
+println(pds_simplified.map(string).mkString("\n"))
+println("Number of pds  " +  pds_simplified.size)
+
+
+
+
+def fact(n: Int) : Int = 
+  if (n == 0) 1 else n *  fact(n - 1)
--- a/exps/both.scala	Sun Feb 17 22:15:06 2019 +0000
+++ b/exps/both.scala	Wed Feb 20 00:00:30 2019 +0000
@@ -42,6 +42,7 @@
 def CHAR(c: Char) = PRED(_ == c, c.toString)
 def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
 def PLUS(r: Rexp) = SEQ(r, STAR(r))
+val ANYCHAR = PRED(_ => true, ".")
 
 // annotated regular expressions
 abstract class ARexp 
@@ -529,6 +530,71 @@
   env(blexing2_simp(r, s)).map(esc2)
 
 
+// Parser for regexes
+
+case class Parser(s: String) {
+  var i = 0
+  
+  def peek() = s(i)
+  def eat(c: Char) = 
+    if (c == s(i)) i = i + 1 else throw new Exception("Expected " + c + " got " + s(i))
+  def next() = { i = i + 1; s(i - 1) }
+  def more() = s.length - i > 0
+
+  def Regex() : Rexp = {
+    val t = Term();
+    if (more() && peek() == '|') {
+      eat ('|') ; 
+      ALT(t, Regex()) 
+    } 
+    else t
+  }
+
+  def Term() : Rexp = {
+    var f : Rexp = 
+      if (more() && peek() != ')' && peek() != '|') Factor() else ONE;
+    while (more() && peek() != ')' && peek() != '|') {
+      f = SEQ(f, Factor()) ;
+    }
+    f
+  }
+
+  def Factor() : Rexp = {
+    var b = Base();
+    while (more() && peek() == '*') {
+      eat('*') ;
+      b = STAR(b) ;
+    }
+    while (more() && peek() == '?') {
+      eat('?') ;
+      b = ALT(b, ONE) ;
+    }
+    while (more() && peek() == '+') {
+      eat('+') ;
+      b = SEQ(b, STAR(b)) ;
+    }
+    b
+  }
+
+  def Base() : Rexp = {
+    peek() match {
+      case '(' => { eat('(') ; val r = Regex(); eat(')') ; r }   // if groups should be groups RECD("",r) }
+      case '.' => { eat('.'); ANYCHAR }
+      case _ => CHAR(next())
+    }
+  }
+}
+
+// two simple examples for the regex parser
+
+println("two simple examples for the regex parser")
+
+println(string(Parser("a|(bc)*").Regex()))
+println(string(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex()))
+
+
+
+System.exit(0)
 
 //   Testing
 //============
--- a/progs/scala/re.scala	Sun Feb 17 22:15:06 2019 +0000
+++ b/progs/scala/re.scala	Wed Feb 20 00:00:30 2019 +0000
@@ -19,6 +19,7 @@
 case class Right(v: Val) extends Val
 case class Stars(vs: List[Val]) extends Val
 case class Rec(x: String, v: Val) extends Val
+
    
 // some convenience for typing in regular expressions
 def charlist2rexp(s : List[Char]): Rexp = s match {
--- a/progs/scala/tests.scala	Sun Feb 17 22:15:06 2019 +0000
+++ b/progs/scala/tests.scala	Wed Feb 20 00:00:30 2019 +0000
@@ -241,8 +241,8 @@
 }
 
 //test case
-//println(Parser("a|(bc)*").Regex())
-
+println(Parser("a|(bc)*").Regex())
+println(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex())
 
 def process_line(line: String) : String = {
   if (line.head == '#') "#" else
--- a/thys/PDerivs.thy	Sun Feb 17 22:15:06 2019 +0000
+++ b/thys/PDerivs.thy	Wed Feb 20 00:00:30 2019 +0000
@@ -159,7 +159,7 @@
 unfolding pders_Set_def
 by (simp add: PSuf_Union pders_snoc)
 
-lemma pderivs_SEQ:
+lemma pders_SEQ:
   shows "pders s (SEQ r1 r2) \<subseteq> SEQs (pders s r1) r2 \<union> (pders_Set (PSuf s) r2)"
 proof (induct s rule: rev_induct)
   case (snoc c s)
@@ -200,7 +200,7 @@
   shows "pders_Set UNIV1 (SEQ r1 r2) \<subseteq> SEQs (pders_Set UNIV1 r1) r2 \<union> pders_Set UNIV1 r2"
 apply(rule pders_Set_subsetI)
 apply(rule subset_trans)
-apply(rule pderivs_SEQ)
+apply(rule pders_SEQ)
 using pders_Set_SEQ_aux1 pders_Set_SEQ_aux2
 apply auto
 apply blast
@@ -248,6 +248,8 @@
   shows "finite (SEQs A r)"
 using a by auto
 
+thm finite.intros
+
 lemma finite_pders_Set_UNIV1:
   shows "finite (pders_Set UNIV1 r)"
 apply(induct r)
@@ -340,64 +342,111 @@
   finally show "card (pders_Set A r) \<le> awidth r + 1" by simp
 qed
 
-(* tests *)
+(* other result by antimirov *)
+
+fun subs :: "rexp \<Rightarrow> rexp set" where
+"subs ZERO = {ZERO}" |
+"subs ONE = {ONE}" |
+"subs (CHAR a) = {CHAR a, ONE}" |
+"subs (ALT r1 r2) = (subs r1 \<union> subs r2 \<union> {ALT r1 r2})" |
+"subs (SEQ r1 r2) = (subs r1 \<union> subs r2 \<union> {SEQ r1 r2} \<union>  SEQs (subs r1) r2)" |
+"subs (STAR r1) = (subs r1 \<union> {STAR r1} \<union> SEQs (subs r1) (STAR r1))"
 
-lemma b: 
-  assumes "rd \<in> pder c r"
-  shows "size rd \<le> (Suc (size r)) * (Suc (size r))"
-  using assms
-  apply(induct r arbitrary: rd)
-  apply(auto)[3]
-  apply(case_tac "c = x")
-      apply(auto)[2]
-  prefer 2
-    apply(auto)[1]
-  oops
-  
-  
+lemma pders_subs:
+  shows "pders s r \<subseteq> subs r"
+  apply(induct r arbitrary: s)
+       apply(simp)
+      apply(simp)
+     apply(simp add: pders_CHAR)
+    apply(simp)
+    apply(rule subset_trans)
+     apply(rule pders_SEQ)
+    defer
+    apply(simp)
+    apply(rule impI)
+    apply(rule conjI)
+  apply blast
+  apply blast
+    apply(case_tac s)
+    apply(simp)
+   apply(rule subset_trans)
+  thm pders_STAR
+     apply(rule pders_STAR)
+     apply(simp)
+    apply(simp)
+  apply (smt UN_I UN_least Un_iff insertCI pders_Set_def subsetCE subsetI)
+  apply(simp)
+  apply(rule conjI)
+  apply blast
+  by (metis Un_insert_right le_supI1 pders_Set_subsetI subset_trans sup_ge2)
 
-lemma a: 
-  assumes "rd \<in> pders s (SEQ r1 r2)"
-  shows "size  rd \<le> Suc (size r1 + size r2)"
-  using assms
-  apply(induct s arbitrary: r1 r2 rd)
-   apply(simp)
-  apply(auto)
-  apply(case_tac "nullable r1")
-   apply(auto)
-  oops
-  
+fun size2 :: "rexp \<Rightarrow> nat" where
+  "size2 ZERO = 1" |
+  "size2 ONE = 1" |
+  "size2 (CHAR c) = 1" |
+  "size2 (ALT r1 r2) = Suc (size2 r1 + size2 r2)" |
+  "size2 (SEQ r1 r2) = Suc (size2 r1 + size2 r2)" |
+  "size2 (STAR r1) = Suc (size2 r1)" 
+
+
+lemma size_rexp:
+  fixes r :: rexp
+  shows "1 \<le> size2 r"
+  apply(induct r)
+  apply(simp)
+  apply(simp_all)
+  done
 
 lemma
-  shows "\<forall>rd \<in> (pders_Set UNIV r). size rd \<le> size r"
+  shows "\<forall>r1 \<in> subs r. size2 r1 \<le> Suc (size2 r * size2 r)"
   apply(induct r)
-       apply(auto)[1]
-       apply(simp add: pders_Set_def)
-      apply(simp add: pders_Set_def)
-     apply(simp add: pders_Set_def pders_CHAR)
-  using pders_CHAR apply fastforce
-    prefer 2
-    apply(simp add: pders_Set_def)
-    apply (meson Un_iff le_SucI trans_le_add1 trans_le_add2)
-  apply(simp add: pders_Set_def)
-   apply(auto)[1]
-  apply(case_tac y)
-  apply(simp)
+       apply(simp)
+      apply(simp)
+     apply(simp)
+    apply(simp)
+    apply(auto)[1]
+  apply (smt Suc_n_not_le_n add.commute distrib_left le_Suc_eq left_add_mult_distrib nat_le_linear trans_le_add1)
+  apply (smt Suc_le_mono Suc_n_not_le_n le_trans nat_le_linear power2_eq_square power2_sum semiring_normalization_rules(23) trans_le_add2)
+  apply (smt Groups.add_ac(3) Suc_n_not_le_n distrib_left le_Suc_eq left_add_mult_distrib nat_le_linear trans_le_add1)
    apply(simp)
    apply(auto)[1]
-   apply(case_tac "nullable r1")
-    apply(simp)
-    apply(auto)[1]
-     prefer 3
-     apply(simp)
-     apply(auto)[1]
-     apply(subgoal_tac "size xa \<le> size r1")
-      prefer 2
-      apply (metis UN_I pders.simps(1) pders_snoc singletonI)
-  oops
+  apply (smt Groups.add_ac(2) Suc_le_mono Suc_n_not_le_n le_add2 linear order_trans power2_eq_square power2_sum)
+  apply (smt Groups.add_ac(2) Suc_le_mono Suc_n_not_le_n left_add_mult_distrib linear mult.commute order.trans trans_le_add1)
+  apply(auto)[1]
+  apply(drule_tac x="r'" in bspec)
+   apply(simp)
+  apply(rule le_trans)
+   apply(assumption)
+  apply(simp)
+  using size_rexp
+  apply(simp)
+  done
   
+fun height :: "rexp \<Rightarrow> nat" where
+  "height ZERO = 1" |
+  "height ONE = 1" |
+  "height (CHAR c) = 1" |
+  "height (ALT r1 r2) = Suc (max (height r1) (height r2))" |
+  "height (SEQ r1 r2) = Suc (max (height r1) (height r2))" |
+  "height (STAR r1) = Suc (height r1)" 
+
+lemma height_rexp:
+  fixes r :: rexp
+  shows "1 \<le> height r"
+  apply(induct r)
+  apply(simp_all)
+  done
+
+lemma 
+  shows "\<forall>r1 \<in> subs r. height r1 \<le> Suc (height r)"
+  apply(induct r)
+  apply(auto)+
+  done  
   
-  
+
+
+(* tests *)
+
   
 
 end
\ No newline at end of file