thys/Simplifying.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 15 May 2018 15:08:41 +0100
changeset 284 913205c1fd0d
parent 283 c4d821c6309d
child 286 804fbb227568
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Simplifying
185
841f7b9c0a6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 180
diff changeset
     2
  imports "Lexer" 
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
section {* Lexer including simplifications *}
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
fun F_RIGHT where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  "F_RIGHT f v = Right (f v)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
fun F_LEFT where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  "F_LEFT f v = Left (f v)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
fun F_ALT where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  "F_ALT f\<^sub>1 f\<^sub>2 (Right v) = Right (f\<^sub>2 v)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
| "F_ALT f\<^sub>1 f\<^sub>2 (Left v) = Left (f\<^sub>1 v)"  
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
| "F_ALT f1 f2 v = v"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
fun F_SEQ1 where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  "F_SEQ1 f\<^sub>1 f\<^sub>2 v = Seq (f\<^sub>1 Void) (f\<^sub>2 v)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
fun F_SEQ2 where 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  "F_SEQ2 f\<^sub>1 f\<^sub>2 v = Seq (f\<^sub>1 v) (f\<^sub>2 Void)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
fun F_SEQ where 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  "F_SEQ f\<^sub>1 f\<^sub>2 (Seq v\<^sub>1 v\<^sub>2) = Seq (f\<^sub>1 v\<^sub>1) (f\<^sub>2 v\<^sub>2)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
| "F_SEQ f1 f2 v = v"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
fun simp_ALT where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  "simp_ALT (ZERO, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (r\<^sub>2, F_RIGHT f\<^sub>2)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
| "simp_ALT (r\<^sub>1, f\<^sub>1) (ZERO, f\<^sub>2) = (r\<^sub>1, F_LEFT f\<^sub>1)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
| "simp_ALT (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (ALT r\<^sub>1 r\<^sub>2, F_ALT f\<^sub>1 f\<^sub>2)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
283
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
    35
(* where is ZERO *)
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
fun simp_SEQ where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  "simp_SEQ (ONE, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (r\<^sub>2, F_SEQ1 f\<^sub>1 f\<^sub>2)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
| "simp_SEQ (r\<^sub>1, f\<^sub>1) (ONE, f\<^sub>2) = (r\<^sub>1, F_SEQ2 f\<^sub>1 f\<^sub>2)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
| "simp_SEQ (r\<^sub>1, f\<^sub>1) (r\<^sub>2, f\<^sub>2) = (SEQ r\<^sub>1 r\<^sub>2, F_SEQ f\<^sub>1 f\<^sub>2)"  
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
 
154
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    41
lemma simp_SEQ_simps[simp]:
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  "simp_SEQ p1 p2 = (if (fst p1 = ONE) then (fst p2, F_SEQ1 (snd p1) (snd p2))
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
                    else (if (fst p2 = ONE) then (fst p1, F_SEQ2 (snd p1) (snd p2))
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
                    else (SEQ (fst p1) (fst p2), F_SEQ (snd p1) (snd p2))))"
154
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    45
by (induct p1 p2 rule: simp_SEQ.induct) (auto)
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
154
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    47
lemma simp_ALT_simps[simp]:
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  "simp_ALT p1 p2 = (if (fst p1 = ZERO) then (fst p2, F_RIGHT (snd p2))
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
                    else (if (fst p2 = ZERO) then (fst p1, F_LEFT (snd p1))
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
                    else (ALT (fst p1) (fst p2), F_ALT (snd p1) (snd p2))))"
154
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    51
by (induct p1 p2 rule: simp_ALT.induct) (auto)
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
fun 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  simp :: "rexp \<Rightarrow> rexp * (val \<Rightarrow> val)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  "simp (ALT r1 r2) = simp_ALT (simp r1) (simp r2)" 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
| "simp (SEQ r1 r2) = simp_SEQ (simp r1) (simp r2)" 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
| "simp r = (r, id)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
fun 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  slexer :: "rexp \<Rightarrow> string \<Rightarrow> val option"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
where
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  "slexer r [] = (if nullable r then Some(mkeps r) else None)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
| "slexer r (c#s) = (let (rs, fr) = simp (der c r) in
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
                         (case (slexer rs s) of  
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
                            None \<Rightarrow> None
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
                          | Some(v) \<Rightarrow> Some(injval r c (fr v))))"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
179
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    70
lemma slexer_better_simp:
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    71
  "slexer r (c#s) = (case (slexer (fst (simp (der c r))) s) of  
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    72
                            None \<Rightarrow> None
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    73
                          | Some(v) \<Rightarrow> Some(injval r c ((snd (simp (der c r))) v)))"
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    74
by (auto split: prod.split option.split)
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    75
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
    76
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
lemma L_fst_simp:
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  shows "L(r) = L(fst (simp r))"
154
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    79
by (induct r) (auto)
2de3cf684ba0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 151
diff changeset
    80
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
lemma Posix_simp:
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  assumes "s \<in> (fst (simp r)) \<rightarrow> v" 
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  shows "s \<in> r \<rightarrow> ((snd (simp r)) v)"
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
using assms
180
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    85
proof(induct r arbitrary: s v rule: rexp.induct)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    86
  case (ALT r1 r2 s v)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    87
  have IH1: "\<And>s v. s \<in> fst (simp r1) \<rightarrow> v \<Longrightarrow> s \<in> r1 \<rightarrow> snd (simp r1) v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    88
  have IH2: "\<And>s v. s \<in> fst (simp r2) \<rightarrow> v \<Longrightarrow> s \<in> r2 \<rightarrow> snd (simp r2) v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    89
  have as: "s \<in> fst (simp (ALT r1 r2)) \<rightarrow> v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    90
  consider (ZERO_ZERO) "fst (simp r1) = ZERO" "fst (simp r2) = ZERO"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    91
         | (ZERO_NZERO) "fst (simp r1) = ZERO" "fst (simp r2) \<noteq> ZERO"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    92
         | (NZERO_ZERO) "fst (simp r1) \<noteq> ZERO" "fst (simp r2) = ZERO"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    93
         | (NZERO_NZERO) "fst (simp r1) \<noteq> ZERO" "fst (simp r2) \<noteq> ZERO" by auto
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    94
  then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v" 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    95
    proof(cases)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    96
      case (ZERO_ZERO)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    97
      with as have "s \<in> ZERO \<rightarrow> v" by simp 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    98
      then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v" by (rule Posix_elims(1))
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
    99
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   100
      case (ZERO_NZERO)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   101
      with as have "s \<in> fst (simp r2) \<rightarrow> v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   102
      with IH2 have "s \<in> r2 \<rightarrow> snd (simp r2) v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   103
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   104
      from ZERO_NZERO have "fst (simp r1) = ZERO" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   105
      then have "L (fst (simp r1)) = {}" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   106
      then have "L r1 = {}" using L_fst_simp by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   107
      then have "s \<notin> L r1" by simp 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   108
      ultimately have "s \<in> ALT r1 r2 \<rightarrow> Right (snd (simp r2) v)" by (rule Posix_ALT2)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   109
      then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   110
      using ZERO_NZERO by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   111
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   112
      case (NZERO_ZERO)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   113
      with as have "s \<in> fst (simp r1) \<rightarrow> v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   114
      with IH1 have "s \<in> r1 \<rightarrow> snd (simp r1) v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   115
      then have "s \<in> ALT r1 r2 \<rightarrow> Left (snd (simp r1) v)" by (rule Posix_ALT1) 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   116
      then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v" using NZERO_ZERO by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   117
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   118
      case (NZERO_NZERO)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   119
      with as have "s \<in> ALT (fst (simp r1)) (fst (simp r2)) \<rightarrow> v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   120
      then consider (Left) v1 where "v = Left v1" "s \<in> (fst (simp r1)) \<rightarrow> v1"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   121
                  | (Right) v2 where "v = Right v2" "s \<in> (fst (simp r2)) \<rightarrow> v2" "s \<notin> L (fst (simp r1))"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   122
                  by (erule_tac Posix_elims(4)) 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   123
      then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   124
      proof(cases)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   125
        case (Left)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   126
        then have "v = Left v1" "s \<in> r1 \<rightarrow> (snd (simp r1) v1)" using IH1 by simp_all
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   127
        then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v" using NZERO_NZERO
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   128
          by (simp_all add: Posix_ALT1)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   129
      next 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   130
        case (Right)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   131
        then have "v = Right v2" "s \<in> r2 \<rightarrow> (snd (simp r2) v2)" "s \<notin> L r1" using IH2 L_fst_simp by simp_all
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   132
        then show "s \<in> ALT r1 r2 \<rightarrow> snd (simp (ALT r1 r2)) v" using NZERO_NZERO
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   133
          by (simp_all add: Posix_ALT2)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   134
      qed
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   135
    qed
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   136
next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   137
  case (SEQ r1 r2 s v)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   138
  have IH1: "\<And>s v. s \<in> fst (simp r1) \<rightarrow> v \<Longrightarrow> s \<in> r1 \<rightarrow> snd (simp r1) v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   139
  have IH2: "\<And>s v. s \<in> fst (simp r2) \<rightarrow> v \<Longrightarrow> s \<in> r2 \<rightarrow> snd (simp r2) v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   140
  have as: "s \<in> fst (simp (SEQ r1 r2)) \<rightarrow> v" by fact
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   141
  consider (ONE_ONE) "fst (simp r1) = ONE" "fst (simp r2) = ONE"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   142
         | (ONE_NONE) "fst (simp r1) = ONE" "fst (simp r2) \<noteq> ONE"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   143
         | (NONE_ONE) "fst (simp r1) \<noteq> ONE" "fst (simp r2) = ONE"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   144
         | (NONE_NONE) "fst (simp r1) \<noteq> ONE" "fst (simp r2) \<noteq> ONE" by auto
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   145
  then show "s \<in> SEQ r1 r2 \<rightarrow> snd (simp (SEQ r1 r2)) v" 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   146
  proof(cases)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   147
      case (ONE_ONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   148
      with as have b: "s \<in> ONE \<rightarrow> v" by simp 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   149
      from b have "s \<in> r1 \<rightarrow> snd (simp r1) v" using IH1 ONE_ONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   150
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   151
      from b have c: "s = []" "v = Void" using Posix_elims(2) by auto
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   152
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   153
      have "[] \<in> ONE \<rightarrow> Void" by (simp add: Posix_ONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   154
      then have "[] \<in> fst (simp r2) \<rightarrow> Void" using ONE_ONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   155
      then have "[] \<in> r2 \<rightarrow> snd (simp r2) Void" using IH2 by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   156
      ultimately have "([] @ []) \<in> SEQ r1 r2 \<rightarrow> Seq (snd (simp r1) Void) (snd (simp r2) Void)"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   157
        using Posix_SEQ by blast 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   158
      then show "s \<in> SEQ r1 r2 \<rightarrow> snd (simp (SEQ r1 r2)) v" using c ONE_ONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   159
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   160
      case (ONE_NONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   161
      with as have b: "s \<in> fst (simp r2) \<rightarrow> v" by simp 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   162
      from b have "s \<in> r2 \<rightarrow> snd (simp r2) v" using IH2 ONE_NONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   163
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   164
      have "[] \<in> ONE \<rightarrow> Void" by (simp add: Posix_ONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   165
      then have "[] \<in> fst (simp r1) \<rightarrow> Void" using ONE_NONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   166
      then have "[] \<in> r1 \<rightarrow> snd (simp r1) Void" using IH1 by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   167
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   168
      from ONE_NONE(1) have "L (fst (simp r1)) = {[]}" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   169
      then have "L r1 = {[]}" by (simp add: L_fst_simp[symmetric])
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   170
      ultimately have "([] @ s) \<in> SEQ r1 r2 \<rightarrow> Seq (snd (simp r1) Void) (snd (simp r2) v)"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   171
        by(rule_tac Posix_SEQ) auto
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   172
      then show "s \<in> SEQ r1 r2 \<rightarrow> snd (simp (SEQ r1 r2)) v" using ONE_NONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   173
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   174
      case (NONE_ONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   175
        with as have "s \<in> fst (simp r1) \<rightarrow> v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   176
        with IH1 have "s \<in> r1 \<rightarrow> snd (simp r1) v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   177
      moreover
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   178
        have "[] \<in> ONE \<rightarrow> Void" by (simp add: Posix_ONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   179
        then have "[] \<in> fst (simp r2) \<rightarrow> Void" using NONE_ONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   180
        then have "[] \<in> r2 \<rightarrow> snd (simp r2) Void" using IH2 by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   181
      ultimately have "(s @ []) \<in> SEQ r1 r2 \<rightarrow> Seq (snd (simp r1) v) (snd (simp r2) Void)"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   182
        by(rule_tac Posix_SEQ) auto
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   183
      then show "s \<in> SEQ r1 r2 \<rightarrow> snd (simp (SEQ r1 r2)) v" using NONE_ONE by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   184
    next
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   185
      case (NONE_NONE)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   186
      with as have "s \<in> SEQ (fst (simp r1)) (fst (simp r2)) \<rightarrow> v" by simp
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   187
      then obtain s1 s2 v1 v2 where eqs: "s = s1 @ s2" "v = Seq v1 v2"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   188
                     "s1 \<in> (fst (simp r1)) \<rightarrow> v1" "s2 \<in> (fst (simp r2)) \<rightarrow> v2"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   189
                     "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> s1 @ s\<^sub>3 \<in> L r1 \<and> s\<^sub>4 \<in> L r2)"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   190
                     by (erule_tac Posix_elims(5)) (auto simp add: L_fst_simp[symmetric]) 
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   191
      then have "s1 \<in> r1 \<rightarrow> (snd (simp r1) v1)" "s2 \<in> r2 \<rightarrow> (snd (simp r2) v2)"
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   192
        using IH1 IH2 by auto             
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   193
      then show "s \<in> SEQ r1 r2 \<rightarrow> snd (simp (SEQ r1 r2)) v" using eqs NONE_NONE
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   194
        by(auto intro: Posix_SEQ)
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   195
    qed
42ffaca7c85e isarfied the simplify theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 179
diff changeset
   196
qed (simp_all)
179
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   197
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
lemma slexer_correctness:
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  shows "slexer r s = lexer r s"
179
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   201
proof(induct s arbitrary: r)
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   202
  case Nil
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   203
  show "slexer r [] = lexer r []" by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   204
next 
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   205
  case (Cons c s r)
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   206
  have IH: "\<And>r. slexer r s = lexer r s" by fact
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   207
  show "slexer r (c # s) = lexer r (c # s)" 
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   208
   proof (cases "s \<in> L (der c r)")
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   209
     case True
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   210
       assume a1: "s \<in> L (der c r)"
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   211
       then obtain v1 where a2: "lexer (der c r) s = Some v1" "s \<in> der c r \<rightarrow> v1"
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   212
         using lexer_correct_Some by auto
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   213
       from a1 have "s \<in> L (fst (simp (der c r)))" using L_fst_simp[symmetric] by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   214
       then obtain v2 where a3: "lexer (fst (simp (der c r))) s = Some v2" "s \<in> (fst (simp (der c r))) \<rightarrow> v2"
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   215
          using lexer_correct_Some by auto
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   216
       then have a4: "slexer (fst (simp (der c r))) s = Some v2" using IH by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   217
       from a3(2) have "s \<in> der c r \<rightarrow> (snd (simp (der c r))) v2" using Posix_simp by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   218
       with a2(2) have "v1 = (snd (simp (der c r))) v2" using Posix_determ by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   219
       with a2(1) a4 show "slexer r (c # s) = lexer r (c # s)" by (auto split: prod.split)
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   220
     next 
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   221
     case False
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   222
       assume b1: "s \<notin> L (der c r)"
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   223
       then have "lexer (der c r) s = None" using lexer_correct_None by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   224
       moreover
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   225
       from b1 have "s \<notin> L (fst (simp (der c r)))" using L_fst_simp[symmetric] by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   226
       then have "lexer (fst (simp (der c r))) s = None" using lexer_correct_None by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   227
       then have "slexer (fst (simp (der c r))) s = None" using IH by simp
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   228
       ultimately show "slexer r (c # s) = lexer r (c # s)" 
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   229
         by (simp del: slexer.simps add: slexer_better_simp)
85766e408c66 improved simplifying theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 154
diff changeset
   230
   qed
283
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   231
 qed  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   232
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   233
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   234
fun simp2_ALT where
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   235
  "simp2_ALT ZERO r2 seen = (r2, seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   236
| "simp2_ALT r1 ZERO seen = (r1, seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   237
| "simp2_ALT r1 r2 seen = (ALT r1 r2, seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   238
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   239
fun simp2_SEQ where
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   240
  "simp2_SEQ ZERO r2 seen = (ZERO, seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   241
| "simp2_SEQ r1 ZERO seen = (ZERO, seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   242
| "simp2_SEQ ONE r2 seen = (r2, seen \<union> {r2})"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   243
| "simp2_SEQ r1 ONE seen = (r1, seen \<union> {r1})"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   244
| "simp2_SEQ r1 r2 seen = (SEQ r1 r2, seen \<union> {SEQ r1 r2})"  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   245
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   246
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   247
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   248
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   249
fun 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   250
  simp2 :: "rexp \<Rightarrow> rexp set \<Rightarrow> rexp * rexp set"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   251
where
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   252
  "simp2 (ALT r1 r2) seen = 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   253
      (let (r1s, seen1) = simp2 r1 seen in 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   254
       let (r2s, seen2) = simp2 r2 seen1 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   255
       in simp2_ALT r1s r2s seen2)" 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   256
| "simp2 (SEQ r1 r2) seen = 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   257
      (let (r1s, _) = simp2 r1 {} in 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   258
       let (r2s, _) = simp2 r2 {} 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   259
       in if (SEQ r1s r2s \<in> seen) then (ZERO, seen)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   260
       else simp2_SEQ r1s r2s seen)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   261
| "simp2 r seen = (if r \<in> seen then (ZERO, seen) else (r, seen \<union> {r}))"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   262
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   263
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   264
lemma simp2_ALT[simp]: 
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   265
  shows "L (fst (simp2_ALT r1 r2 seen)) = L r1 \<union> L r2"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   266
  apply(induct r1 r2 seen rule: simp2_ALT.induct)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   267
  apply(simp_all)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   268
  done
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   269
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   270
lemma test1:
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   271
  shows "snd (simp2_ALT r1 r2 rs) = rs"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   272
  apply(induct r1 r2 rs rule: simp2_ALT.induct)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   273
  apply(auto)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   274
  done
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   275
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   276
lemma test2:
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   277
  shows "rs \<subseteq> snd (simp2_SEQ r1 r2 rs)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   278
  apply(induct r1 r2 rs rule: simp2_SEQ.induct)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   279
  apply(auto)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   280
  done
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   281
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   282
lemma test3:
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   283
  shows "rs \<subseteq> snd (simp2 r rs)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   284
  apply(induct r rs rule: simp2.induct)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   285
  apply(auto split: prod.split)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   286
  apply (metis set_mp test1)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   287
  by (meson set_mp test2)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   288
  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   289
lemma test3a:
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   290
  shows "\<Union>(L ` snd (simp2 r rs)) \<subseteq> L(r) \<union> (\<Union> (L ` rs))"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   291
  apply(induct r rs rule: simp2.induct)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   292
  apply(auto split: prod.split simp add: Sequ_def)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   293
  apply (smt UN_iff Un_iff set_mp test1)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   294
    
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   295
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   296
lemma test:
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   297
  assumes "(\<Union>r' \<in> rs. L r') \<subseteq> L r"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   298
  shows "L(fst (simp2 r rs)) \<subseteq> L(r)"
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   299
  using assms
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   300
  apply(induct r arbitrary: rs)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   301
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   302
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   303
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   304
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   305
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   306
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   307
  prefer 3
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   308
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   309
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   310
  prefer 2
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   311
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   312
   apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   313
   apply(subst prod.split)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   314
  apply(rule allI)+
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   315
   apply(rule impI)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   316
  apply(subst prod.split)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   317
  apply(rule allI)+
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   318
   apply(rule impI)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   319
   apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   320
   apply(drule_tac x="rs" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   321
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   322
  apply(drule_tac x="x2" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   323
   apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   324
   apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   325
    apply(subgoal_tac "rs \<subseteq> x2a")
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   326
     prefer 2
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   327
  apply (metis order_trans prod.sel(2) test3)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   328
  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   329
  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   330
   apply(rule antisym)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   331
  prefer 2
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   332
    apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   333
    apply(rule conjI)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   334
     apply(drule meta_mp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   335
  prefer 2
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   336
      apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   337
      apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   338
    apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   339
  
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   340
  thm prod.split
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   341
   apply(auto split: prod.split)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   342
     apply(drule_tac x="rs" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   343
     apply(drule_tac x="rs" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   344
     apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   345
     apply(rule_tac x="SEQ x1 x1a" in bexI)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   346
      apply(simp add: Sequ_def)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   347
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   348
      apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   349
     apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   350
  apply(subgoal_tac "L (fst (simp2_SEQ x1 x1a rs)) \<subseteq> L x1 \<union> L x1a")
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   351
     apply(frule_tac x="{}" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   352
  apply(rotate_tac 1)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   353
     apply(frule_tac x="{}" in meta_spec)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   354
     apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   355
   
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   356
    
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   357
     apply(rule_tac x="SEQ x1 x1a" in bexI)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   358
      apply(simp add: Sequ_def)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   359
      apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   360
  apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   361
  using L.simps(2) apply blast
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   362
  prefer 3
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   363
  apply(simp only: simp2.simps)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   364
    apply(simp)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   365
  using L.simps(3) apply blast
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   366
  prefer 2
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   367
   apply(simp add: Sequ_def)
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   368
   apply(auto)[1]
c4d821c6309d updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   369
  oops  
150
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
09f81fee11ce updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
end