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