thys4/posix/Lexer.thy
author Chengsong
Fri, 30 Dec 2022 17:37:51 +0000
changeset 639 80cc6dc4c98b
parent 587 3198605ac648
permissions -rw-r--r--
until chap 7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     1
   
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     2
theory Lexer
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     3
  imports PosixSpec 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     4
begin
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     5
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     6
section \<open>The Lexer Functions by Sulzmann and Lu  (without simplification)\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     7
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     8
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
     9
  mkeps :: "rexp \<Rightarrow> val"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    10
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    11
  "mkeps(ONE) = Void"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    12
| "mkeps(SEQ r1 r2) = Seq (mkeps r1) (mkeps r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    13
| "mkeps(ALT r1 r2) = (if nullable(r1) then Left (mkeps r1) else Right (mkeps r2))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    14
| "mkeps(STAR r) = Stars []"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    15
| "mkeps(NTIMES r n) = Stars (replicate n (mkeps r))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    16
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    17
fun injval :: "rexp \<Rightarrow> char \<Rightarrow> val \<Rightarrow> val"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    18
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    19
  "injval (CH d) c Void = Char d"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    20
| "injval (ALT r1 r2) c (Left v1) = Left(injval r1 c v1)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    21
| "injval (ALT r1 r2) c (Right v2) = Right(injval r2 c v2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    22
| "injval (SEQ r1 r2) c (Seq v1 v2) = Seq (injval r1 c v1) v2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    23
| "injval (SEQ r1 r2) c (Left (Seq v1 v2)) = Seq (injval r1 c v1) v2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    24
| "injval (SEQ r1 r2) c (Right v2) = Seq (mkeps r1) (injval r2 c v2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    25
| "injval (STAR r) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    26
| "injval (NTIMES r n) c (Seq v (Stars vs)) = Stars ((injval r c v) # vs)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    27
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    28
fun 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    29
  lexer :: "rexp \<Rightarrow> string \<Rightarrow> val option"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    30
where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    31
  "lexer r [] = (if nullable r then Some(mkeps r) else None)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    32
| "lexer r (c#s) = (case (lexer (der c r) s) of  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    33
                    None \<Rightarrow> None
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    34
                  | Some(v) \<Rightarrow> Some(injval r c v))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    35
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    36
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    37
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    38
section \<open>Mkeps, Injval Properties\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    39
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    40
lemma mkeps_flat:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    41
  assumes "nullable(r)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    42
  shows "flat (mkeps r) = []"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    43
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    44
  by (induct rule: mkeps.induct) (auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    45
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    46
lemma Prf_NTimes_empty:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    47
  assumes "\<forall>v \<in> set vs. \<Turnstile> v : r \<and> flat v = []" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    48
  and     "length vs = n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    49
  shows "\<Turnstile> Stars vs : NTIMES r n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    50
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    51
  by (metis Prf.intros(7) empty_iff eq_Nil_appendI list.set(1))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    52
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    53
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    54
lemma mkeps_nullable:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    55
  assumes "nullable(r)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    56
  shows "\<Turnstile> mkeps r : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    57
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    58
  apply (induct rule: mkeps.induct) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    59
  apply(auto intro: Prf.intros split: if_splits)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    60
  apply (metis Prf.intros(7) append_is_Nil_conv empty_iff list.set(1) list.size(3))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    61
  apply(rule Prf_NTimes_empty)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    62
  apply(auto simp add: mkeps_flat) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    63
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    64
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    65
lemma Prf_injval_flat:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    66
  assumes "\<Turnstile> v : der c r" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    67
  shows "flat (injval r c v) = c # (flat v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    68
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    69
apply(induct c r arbitrary: v rule: der.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    70
apply(auto elim!: Prf_elims intro: mkeps_flat split: if_splits)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    71
done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    72
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    73
lemma Prf_injval:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    74
  assumes "\<Turnstile> v : der c r" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    75
  shows "\<Turnstile> (injval r c v) : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    76
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    77
apply(induct r arbitrary: c v rule: rexp.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    78
apply(auto intro!: Prf.intros mkeps_nullable elim!: Prf_elims split: if_splits)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    79
(* Star *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    80
apply(simp add: Prf_injval_flat)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    81
(* NTimes *)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    82
  apply(case_tac x2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    83
    apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    84
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    85
  apply(subst append.simps(2)[symmetric])
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    86
  apply(rule Prf.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    87
  apply(auto simp add: Prf_injval_flat)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    88
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    89
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    90
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    91
text \<open>Mkeps and injval produce, or preserve, Posix values.\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    92
lemma mkepsPosixSeq_pf2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    93
  shows " \<And>x1 x2 s v. \<lbrakk>\<And>s v. s \<in> der c x1 \<rightarrow> v \<Longrightarrow> (c # s) \<in> x1 \<rightarrow> injval x1 c v;
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    94
                       \<And>s v. s \<in> der c x2 \<rightarrow> v \<Longrightarrow> (c # s) \<in> x2 \<rightarrow> injval x2 c v; 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    95
                       s \<in> der c (SEQ x1 x2) \<rightarrow> v\<rbrakk> \<Longrightarrow> (c # s) \<in> (SEQ x1 x2) \<rightarrow> (injval (SEQ x1 x2) c v) "
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    96
  apply(case_tac "v ")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    97
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    98
  apply (metis Posix1a Posix_elims(4) Prf_elims(2) der.simps(5) val.distinct(3) val.distinct(5) val.distinct(7))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
    99
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   100
  apply (metis Posix1a Posix_elims(4) Prf_elims(2) der.simps(5) val.distinct(11) val.distinct(13) val.distinct(15))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   101
  sorry
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   102
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   103
lemma Posix_mkeps:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   104
  assumes "nullable r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   105
  shows "[] \<in> r \<rightarrow> mkeps r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   106
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   107
apply(induct r rule: nullable.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   108
apply(auto intro: Posix.intros simp add: nullable_correctness Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   109
apply(subst append.simps(1)[symmetric])
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   110
apply(rule Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   111
apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   112
by (simp add: Posix_NTIMES2 pow_empty_iff)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   113
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   114
lemma Posix_injval:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   115
  assumes "s \<in> (der c r) \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   116
  shows "(c # s) \<in> r \<rightarrow> (injval r c v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   117
using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   118
proof(induct r arbitrary: s v rule: rexp.induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   119
  case ZERO
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   120
  have "s \<in> der c ZERO \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   121
  then have "s \<in> ZERO \<rightarrow> v" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   122
  then have "False" by cases
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   123
  then show "(c # s) \<in> ZERO \<rightarrow> (injval ZERO c v)" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   124
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   125
  case ONE
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   126
  have "s \<in> der c ONE \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   127
  then have "s \<in> ZERO \<rightarrow> v" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   128
  then have "False" by cases
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   129
  then show "(c # s) \<in> ONE \<rightarrow> (injval ONE c v)" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   130
next 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   131
  case (CH d)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   132
  consider (eq) "c = d" | (ineq) "c \<noteq> d" by blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   133
  then show "(c # s) \<in> (CH d) \<rightarrow> (injval (CH d) c v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   134
  proof (cases)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   135
    case eq
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   136
    have "s \<in> der c (CH d) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   137
    then have "s \<in> ONE \<rightarrow> v" using eq by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   138
    then have eqs: "s = [] \<and> v = Void" by cases simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   139
    show "(c # s) \<in> CH d \<rightarrow> injval (CH d) c v" using eq eqs 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   140
    by (auto intro: Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   141
  next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   142
    case ineq
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   143
    have "s \<in> der c (CH d) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   144
    then have "s \<in> ZERO \<rightarrow> v" using ineq by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   145
    then have "False" by cases
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   146
    then show "(c # s) \<in> CH d \<rightarrow> injval (CH d) c v" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   147
  qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   148
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   149
  case (ALT r1 r2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   150
  have IH1: "\<And>s v. s \<in> der c r1 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r1 \<rightarrow> injval r1 c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   151
  have IH2: "\<And>s v. s \<in> der c r2 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r2 \<rightarrow> injval r2 c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   152
  have "s \<in> der c (ALT r1 r2) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   153
  then have "s \<in> ALT (der c r1) (der c r2) \<rightarrow> v" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   154
  then consider (left) v' where "v = Left v'" "s \<in> der c r1 \<rightarrow> v'" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   155
              | (right) v' where "v = Right v'" "s \<notin> L (der c r1)" "s \<in> der c r2 \<rightarrow> v'" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   156
              by cases auto
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   157
  then show "(c # s) \<in> ALT r1 r2 \<rightarrow> injval (ALT r1 r2) c v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   158
  proof (cases)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   159
    case left
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   160
    have "s \<in> der c r1 \<rightarrow> v'" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   161
    then have "(c # s) \<in> r1 \<rightarrow> injval r1 c v'" using IH1 by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   162
    then have "(c # s) \<in> ALT r1 r2 \<rightarrow> injval (ALT r1 r2) c (Left v')" by (auto intro: Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   163
    then show "(c # s) \<in> ALT r1 r2 \<rightarrow> injval (ALT r1 r2) c v" using left by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   164
  next 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   165
    case right
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   166
    have "s \<notin> L (der c r1)" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   167
    then have "c # s \<notin> L r1" by (simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   168
    moreover 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   169
    have "s \<in> der c r2 \<rightarrow> v'" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   170
    then have "(c # s) \<in> r2 \<rightarrow> injval r2 c v'" using IH2 by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   171
    ultimately have "(c # s) \<in> ALT r1 r2 \<rightarrow> injval (ALT r1 r2) c (Right v')" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   172
      by (auto intro: Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   173
    then show "(c # s) \<in> ALT r1 r2 \<rightarrow> injval (ALT r1 r2) c v" using right by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   174
  qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   175
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   176
  case (SEQ r1 r2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   177
  have IH1: "\<And>s v. s \<in> der c r1 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r1 \<rightarrow> injval r1 c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   178
  have IH2: "\<And>s v. s \<in> der c r2 \<rightarrow> v \<Longrightarrow> (c # s) \<in> r2 \<rightarrow> injval r2 c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   179
  have "s \<in> der c (SEQ r1 r2) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   180
  then consider 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   181
        (left_nullable) v1 v2 s1 s2 where 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   182
        "v = Left (Seq v1 v2)"  "s = s1 @ s2" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   183
        "s1 \<in> der c r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2" "nullable r1" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   184
        "\<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 (der c r1) \<and> s\<^sub>4 \<in> L r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   185
      | (right_nullable) v1 s1 s2 where 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   186
        "v = Right v1" "s = s1 @ s2"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   187
        "s \<in> der c r2 \<rightarrow> v1" "nullable r1" "s1 @ s2 \<notin> L (SEQ (der c r1) r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   188
      | (not_nullable) v1 v2 s1 s2 where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   189
        "v = Seq v1 v2" "s = s1 @ s2" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   190
        "s1 \<in> der c r1 \<rightarrow> v1" "s2 \<in> r2 \<rightarrow> v2" "\<not>nullable r1" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   191
        "\<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 (der c r1) \<and> s\<^sub>4 \<in> L r2)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   192
        by (force split: if_splits elim!: Posix_elims simp add: Sequ_def der_correctness Der_def)   
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   193
  then show "(c # s) \<in> SEQ r1 r2 \<rightarrow> injval (SEQ r1 r2) c v" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   194
    proof (cases)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   195
      case left_nullable
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   196
      have "s1 \<in> der c r1 \<rightarrow> v1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   197
      then have "(c # s1) \<in> r1 \<rightarrow> injval r1 c v1" using IH1 by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   198
      moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   199
      have "\<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 (der c r1) \<and> s\<^sub>4 \<in> L r2)" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   200
      then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> L r1 \<and> s\<^sub>4 \<in> L r2)" by (simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   201
      ultimately have "((c # s1) @ s2) \<in> SEQ r1 r2 \<rightarrow> Seq (injval r1 c v1) v2" using left_nullable by (rule_tac Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   202
      then show "(c # s) \<in> SEQ r1 r2 \<rightarrow> injval (SEQ r1 r2) c v" using left_nullable by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   203
    next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   204
      case right_nullable
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   205
      have "nullable r1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   206
      then have "[] \<in> r1 \<rightarrow> (mkeps r1)" by (rule Posix_mkeps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   207
      moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   208
      have "s \<in> der c r2 \<rightarrow> v1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   209
      then have "(c # s) \<in> r2 \<rightarrow> (injval r2 c v1)" using IH2 by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   210
      moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   211
      have "s1 @ s2 \<notin> L (SEQ (der c r1) r2)" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   212
      then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = c # s \<and> [] @ s\<^sub>3 \<in> L r1 \<and> s\<^sub>4 \<in> L r2)" using right_nullable
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   213
        by(auto simp add: der_correctness Der_def append_eq_Cons_conv Sequ_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   214
      ultimately have "([] @ (c # s)) \<in> SEQ r1 r2 \<rightarrow> Seq (mkeps r1) (injval r2 c v1)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   215
      by(rule Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   216
      then show "(c # s) \<in> SEQ r1 r2 \<rightarrow> injval (SEQ r1 r2) c v" using right_nullable by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   217
    next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   218
      case not_nullable
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   219
      have "s1 \<in> der c r1 \<rightarrow> v1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   220
      then have "(c # s1) \<in> r1 \<rightarrow> injval r1 c v1" using IH1 by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   221
      moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   222
      have "\<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 (der c r1) \<and> s\<^sub>4 \<in> L r2)" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   223
      then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> L r1 \<and> s\<^sub>4 \<in> L r2)" by (simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   224
      ultimately have "((c # s1) @ s2) \<in> SEQ r1 r2 \<rightarrow> Seq (injval r1 c v1) v2" using not_nullable 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   225
        by (rule_tac Posix.intros) (simp_all) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   226
      then show "(c # s) \<in> SEQ r1 r2 \<rightarrow> injval (SEQ r1 r2) c v" using not_nullable by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   227
    qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   228
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   229
  case (STAR r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   230
  have IH: "\<And>s v. s \<in> der c r \<rightarrow> v \<Longrightarrow> (c # s) \<in> r \<rightarrow> injval r c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   231
  have "s \<in> der c (STAR r) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   232
  then consider
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   233
      (cons) v1 vs s1 s2 where 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   234
        "v = Seq v1 (Stars vs)" "s = s1 @ s2" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   235
        "s1 \<in> der c r \<rightarrow> v1" "s2 \<in> (STAR r) \<rightarrow> (Stars vs)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   236
        "\<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 (der c r) \<and> s\<^sub>4 \<in> L (STAR r))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   237
        apply(auto elim!: Posix_elims(1-5) simp add: der_correctness Der_def intro: Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   238
        apply(rotate_tac 3)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   239
        apply(erule_tac Posix_elims(6))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   240
        apply (simp add: Posix.intros(6))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   241
        using Posix.intros(7) by blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   242
    then show "(c # s) \<in> STAR r \<rightarrow> injval (STAR r) c v" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   243
    proof (cases)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   244
      case cons
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   245
          have "s1 \<in> der c r \<rightarrow> v1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   246
          then have "(c # s1) \<in> r \<rightarrow> injval r c v1" using IH by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   247
        moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   248
          have "s2 \<in> STAR r \<rightarrow> Stars vs" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   249
        moreover 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   250
          have "(c # s1) \<in> r \<rightarrow> injval r c v1" by fact 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   251
          then have "flat (injval r c v1) = (c # s1)" by (rule Posix1)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   252
          then have "flat (injval r c v1) \<noteq> []" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   253
        moreover 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   254
          have "\<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 (der c r) \<and> s\<^sub>4 \<in> L (STAR r))" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   255
          then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> L r \<and> s\<^sub>4 \<in> L (STAR r))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   256
            by (simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   257
        ultimately 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   258
        have "((c # s1) @ s2) \<in> STAR r \<rightarrow> Stars (injval r c v1 # vs)" by (rule Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   259
        then show "(c # s) \<in> STAR r \<rightarrow> injval (STAR r) c v" using cons by(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   260
      qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   261
next
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   262
  case (NTIMES r n)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   263
  have IH: "\<And>s v. s \<in> der c r \<rightarrow> v \<Longrightarrow> (c # s) \<in> r \<rightarrow> injval r c v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   264
  have "s \<in> der c (NTIMES r n) \<rightarrow> v" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   265
  then consider
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   266
      (cons) v1 vs s1 s2 where 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   267
        "v = Seq v1 (Stars vs)" "s = s1 @ s2" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   268
        "s1 \<in> der c r \<rightarrow> v1" "s2 \<in> (NTIMES r (n - 1)) \<rightarrow> (Stars vs)" "0 < n"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   269
        "\<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 (der c r) \<and> s\<^sub>4 \<in> L (NTIMES r (n - 1)))" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   270
    
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   271
    apply(auto elim: Posix_elims simp add: der_correctness Der_def intro: Posix.intros split: if_splits)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   272
    apply(erule Posix_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   273
    apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   274
    apply(subgoal_tac "\<exists>vss. v2 = Stars vss")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   275
    apply(clarify)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   276
    apply(drule_tac x="vss" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   277
    apply(drule_tac x="s1" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   278
    apply(drule_tac x="s2" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   279
     apply(simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   280
    apply(erule Posix_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   281
     apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   282
      done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   283
    then show "(c # s) \<in> (NTIMES r n) \<rightarrow> injval (NTIMES r n) c v" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   284
    proof (cases)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   285
      case cons
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   286
          have "s1 \<in> der c r \<rightarrow> v1" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   287
          then have "(c # s1) \<in> r \<rightarrow> injval r c v1" using IH by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   288
        moreover
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   289
          have "s2 \<in> (NTIMES r (n - 1)) \<rightarrow> Stars vs" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   290
        moreover 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   291
          have "(c # s1) \<in> r \<rightarrow> injval r c v1" by fact 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   292
          then have "flat (injval r c v1) = (c # s1)" by (rule Posix1)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   293
          then have "flat (injval r c v1) \<noteq> []" by simp
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   294
        moreover 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   295
          have "\<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 (der c r) \<and> s\<^sub>4 \<in> L (NTIMES r (n - 1)))" by fact
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   296
          then have "\<not> (\<exists>s\<^sub>3 s\<^sub>4. s\<^sub>3 \<noteq> [] \<and> s\<^sub>3 @ s\<^sub>4 = s2 \<and> (c # s1) @ s\<^sub>3 \<in> L r \<and> s\<^sub>4 \<in> L (NTIMES r (n - 1)))"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   297
            by (simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   298
        ultimately 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   299
        have "((c # s1) @ s2) \<in> NTIMES r n \<rightarrow> Stars (injval r c v1 # vs)" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   300
           apply (rule_tac Posix.intros)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   301
               apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   302
              apply(case_tac n)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   303
            apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   304
           using Posix_elims(1) NTIMES.prems apply auto[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   305
             apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   306
             done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   307
        then show "(c # s) \<in> NTIMES r n \<rightarrow> injval (NTIMES r n) c v" using cons by(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   308
      qed  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   309
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   310
qed
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   311
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   312
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   313
section \<open>Lexer Correctness\<close>
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   314
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   315
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   316
lemma lexer_correct_None:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   317
  shows "s \<notin> L r \<longleftrightarrow> lexer r s = None"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   318
  apply(induct s arbitrary: r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   319
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   320
  apply(simp add: nullable_correctness)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   321
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   322
  apply(drule_tac x="der a r" in meta_spec) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   323
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   324
  apply(auto simp add: der_correctness Der_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   325
done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   326
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   327
lemma lexer_correct_Some:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   328
  shows "s \<in> L r \<longleftrightarrow> (\<exists>v. lexer r s = Some(v) \<and> s \<in> r \<rightarrow> v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   329
  apply(induct s arbitrary : r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   330
  apply(simp only: lexer.simps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   331
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   332
  apply(simp add: nullable_correctness Posix_mkeps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   333
  apply(drule_tac x="der a r" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   334
  apply(simp (no_asm_use) add: der_correctness Der_def del: lexer.simps) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   335
  apply(simp del: lexer.simps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   336
  apply(simp only: lexer.simps)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   337
  apply(case_tac "lexer (der a r) s = None")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   338
   apply(auto)[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   339
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   340
  apply(erule exE)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   341
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   342
  apply(rule iffI)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   343
  apply(simp add: Posix_injval)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   344
  apply(simp add: Posix1(1))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   345
done 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   346
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   347
lemma lexer_correctness:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   348
  shows "(lexer r s = Some v) \<longleftrightarrow> s \<in> r \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   349
  and   "(lexer r s = None) \<longleftrightarrow> \<not>(\<exists>v. s \<in> r \<rightarrow> v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   350
using Posix1(1) Posix_determ lexer_correct_None lexer_correct_Some apply fastforce
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   351
using Posix1(1) lexer_correct_None lexer_correct_Some by blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   352
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   353
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   354
subsection {* A slight reformulation of the lexer algorithm using stacked functions*}
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   355
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   356
fun flex :: "rexp \<Rightarrow> (val \<Rightarrow> val) => string \<Rightarrow> (val \<Rightarrow> val)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   357
  where
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   358
  "flex r f [] = f"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   359
| "flex r f (c#s) = flex (der c r) (\<lambda>v. f (injval r c v)) s"  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   360
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   361
lemma flex_fun_apply:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   362
  shows "g (flex r f s v) = flex r (g o f) s v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   363
  apply(induct s arbitrary: g f r v)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   364
  apply(simp_all add: comp_def)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   365
  by meson
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   366
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   367
lemma flex_fun_apply2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   368
  shows "g (flex r id s v) = flex r g s v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   369
  by (simp add: flex_fun_apply)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   370
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   371
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   372
lemma flex_append:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   373
  shows "flex r f (s1 @ s2) = flex (ders s1 r) (flex r f s1) s2"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   374
  apply(induct s1 arbitrary: s2 r f)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   375
  apply(simp_all)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   376
  done  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   377
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   378
lemma lexer_flex:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   379
  shows "lexer r s = (if nullable (ders s r) 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   380
                      then Some(flex r id s (mkeps (ders s r))) else None)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   381
  apply(induct s arbitrary: r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   382
  apply(simp_all add: flex_fun_apply)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   383
  done  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   384
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   385
lemma Posix_flex:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   386
  assumes "s2 \<in> (ders s1 r) \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   387
  shows "(s1 @ s2) \<in> r \<rightarrow> flex r id s1 v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   388
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   389
  apply(induct s1 arbitrary: r v s2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   390
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   391
  apply(simp)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   392
  apply(drule_tac x="der a r" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   393
  apply(drule_tac x="v" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   394
  apply(drule_tac x="s2" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   395
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   396
  using  Posix_injval
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   397
  apply(drule_tac Posix_injval)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   398
  apply(subst (asm) (5) flex_fun_apply)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   399
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   400
  done
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   401
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   402
lemma injval_inj:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   403
  assumes "\<Turnstile> a : (der c r)" "\<Turnstile> v : (der c r)" "injval r c a = injval r c v" 
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   404
  shows "a = v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   405
  using  assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   406
  apply(induct r arbitrary: a c v)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   407
       apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   408
  using Prf_elims(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   409
  using Prf_elims(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   410
     apply(case_tac "c = x")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   411
      apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   412
  using Prf_elims(4) apply auto[1]
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   413
  using Prf_elims(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   414
    prefer 2
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   415
  apply (smt Prf_elims(3) injval.simps(2) injval.simps(3) val.distinct(25) val.inject(3) val.inject(4))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   416
  apply(case_tac "nullable r1")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   417
    apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   418
    apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   419
     apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   420
     apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   421
      apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   422
      apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   423
     apply (metis Prf_injval_flat list.distinct(1) mkeps_flat)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   424
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   425
     apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   426
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   427
  using Prf_injval_flat mkeps_flat apply fastforce
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   428
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   429
     apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   430
   apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   431
  apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   432
     apply(erule Prf_elims)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   433
  apply(auto)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   434
   apply (smt Prf_elims(6) injval.simps(7) list.inject val.inject(5))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   435
  apply (smt Prf_elims(6) injval.simps(7) list.inject val.inject(5))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   436
  by (smt (verit, best) Prf_elims(1) Prf_elims(2) Prf_elims(7) injval.simps(8) list.inject val.simps(5))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   437
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   438
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   439
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   440
lemma uu:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   441
  assumes "(c # s) \<in> r \<rightarrow> injval r c v" "\<Turnstile> v : (der c r)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   442
  shows "s \<in> der c r \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   443
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   444
  apply -
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   445
  apply(subgoal_tac "lexer r (c # s) = Some (injval r c v)")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   446
  prefer 2
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   447
  using lexer_correctness(1) apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   448
  apply(simp add: )
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   449
  apply(case_tac  "lexer (der c r) s")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   450
   apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   451
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   452
  apply(case_tac "s \<in> der c r \<rightarrow> a")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   453
   prefer 2
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   454
   apply (simp add: lexer_correctness(1))
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   455
  apply(subgoal_tac "\<Turnstile> a : (der c r)")
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   456
   prefer 2
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   457
  using Posix1a apply blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   458
  using injval_inj by blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   459
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   460
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   461
lemma Posix_flex2:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   462
  assumes "(s1 @ s2) \<in> r \<rightarrow> flex r id s1 v" "\<Turnstile> v : ders s1 r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   463
  shows "s2 \<in> (ders s1 r) \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   464
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   465
  apply(induct s1 arbitrary: r v s2 rule: rev_induct)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   466
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   467
  apply(simp)  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   468
  apply(drule_tac x="r" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   469
  apply(drule_tac x="injval (ders xs r) x v" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   470
  apply(drule_tac x="x#s2" in meta_spec)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   471
  apply(simp add: flex_append ders_append)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   472
  using Prf_injval uu by blast
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   473
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   474
lemma Posix_flex3:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   475
  assumes "s1 \<in> r \<rightarrow> flex r id s1 v" "\<Turnstile> v : ders s1 r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   476
  shows "[] \<in> (ders s1 r) \<rightarrow> v"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   477
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   478
  by (simp add: Posix_flex2)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   479
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   480
lemma flex_injval:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   481
  shows "flex (der a r) (injval r a) s v = injval r a (flex (der a r) id s v)"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   482
  by (simp add: flex_fun_apply)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   483
  
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   484
lemma Prf_flex:
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   485
  assumes "\<Turnstile> v : ders s r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   486
  shows "\<Turnstile> flex r id s v : r"
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   487
  using assms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   488
  apply(induct s arbitrary: v r)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   489
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   490
  apply(simp)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   491
  by (simp add: Prf_injval flex_injval)
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   492
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   493
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   494
unused_thms
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   495
3198605ac648 bsimp idempotency
Chengsong
parents:
diff changeset
   496
end