src/HOL/IMP/Abs_Int1.thy
changeset 49396 73fb17ed2e08
parent 49353 023be49d7fb8
child 49402 4ac2ed30edf3
equal deleted inserted replaced
49395:323414474c1f 49396:73fb17ed2e08
    11 lemma le_iff_le_annos: "C1 \<sqsubseteq> C2 \<longleftrightarrow>
    11 lemma le_iff_le_annos: "C1 \<sqsubseteq> C2 \<longleftrightarrow>
    12   strip C1 = strip C2 \<and> (\<forall> i<size(annos C1). annos C1 ! i \<sqsubseteq> annos C2 ! i)"
    12   strip C1 = strip C2 \<and> (\<forall> i<size(annos C1). annos C1 ! i \<sqsubseteq> annos C2 ! i)"
    13 by(auto simp add: le_iff_le_annos_zip set_zip) (metis size_annos_same2)
    13 by(auto simp add: le_iff_le_annos_zip set_zip) (metis size_annos_same2)
    14 
    14 
    15 
    15 
    16 lemma mono_fun_wt[simp]: "wt F X \<Longrightarrow> F \<sqsubseteq> G \<Longrightarrow> x : X \<Longrightarrow> fun F x \<sqsubseteq> fun G x"
    16 lemma mono_fun_L[simp]: "F \<in> L X \<Longrightarrow> F \<sqsubseteq> G \<Longrightarrow> x : X \<Longrightarrow> fun F x \<sqsubseteq> fun G x"
    17 by(simp add: mono_fun wt_st_def)
    17 by(simp add: mono_fun L_st_def)
    18 
    18 
    19 lemma wt_bot[simp]: "wt (bot c) (vars c)"
    19 lemma bot_in_L[simp]: "bot c \<in> L(vars c)"
    20 by(simp add: wt_acom_def bot_def)
    20 by(simp add: L_acom_def bot_def)
    21 
    21 
    22 lemma wt_acom_simps[simp]: "wt (SKIP {P}) X \<longleftrightarrow> wt P X"
    22 lemma L_acom_simps[simp]: "SKIP {P} \<in> L X \<longleftrightarrow> P \<in> L X"
    23   "wt (x ::= e {P}) X \<longleftrightarrow> x : X \<and> vars e \<subseteq> X \<and> wt P X"
    23   "(x ::= e {P}) \<in> L X \<longleftrightarrow> x : X \<and> vars e \<subseteq> X \<and> P \<in> L X"
    24   "wt (C1;C2) X \<longleftrightarrow> wt C1 X \<and> wt C2 X"
    24   "(C1;C2) \<in> L X \<longleftrightarrow> C1 \<in> L X \<and> C2 \<in> L X"
    25   "wt (IF b THEN {P1} C1 ELSE {P2} C2 {Q}) X \<longleftrightarrow>
    25   "(IF b THEN {P1} C1 ELSE {P2} C2 {Q}) \<in> L X \<longleftrightarrow>
    26    vars b \<subseteq> X \<and> wt C1 X \<and> wt C2 X \<and> wt P1 X \<and> wt P2 X \<and> wt Q X"
    26    vars b \<subseteq> X \<and> C1 \<in> L X \<and> C2 \<in> L X \<and> P1 \<in> L X \<and> P2 \<in> L X \<and> Q \<in> L X"
    27   "wt ({I} WHILE b DO {P} C {Q}) X \<longleftrightarrow>
    27   "({I} WHILE b DO {P} C {Q}) \<in> L X \<longleftrightarrow>
    28    wt I X \<and> vars b \<subseteq> X \<and> wt P X \<and> wt C X \<and> wt Q X"
    28    I \<in> L X \<and> vars b \<subseteq> X \<and> P \<in> L X \<and> C \<in> L X \<and> Q \<in> L X"
    29 by(auto simp add: wt_acom_def)
    29 by(auto simp add: L_acom_def)
    30 
    30 
    31 lemma post_in_annos: "post C : set(annos C)"
    31 lemma post_in_annos: "post C : set(annos C)"
    32 by(induction C) auto
    32 by(induction C) auto
    33 
    33 
    34 lemma wt_post[simp]: "wt C X \<Longrightarrow> wt (post C) X"
    34 lemma post_in_L[simp]: "C \<in> L X \<Longrightarrow> post C \<in> L X"
    35 by(simp add: wt_acom_def post_in_annos)
    35 by(simp add: L_acom_def post_in_annos)
    36 
    36 
    37 lemma lpfp_inv:
    37 lemma lpfp_inv:
    38 assumes "lpfp f x0 = Some x" and "\<And>x. P x \<Longrightarrow> P(f x)" and "P(bot x0)"
    38 assumes "lpfp f x0 = Some x" and "\<And>x. P x \<Longrightarrow> P(f x)" and "P(bot x0)"
    39 shows "P x"
    39 shows "P x"
    40 using assms unfolding lpfp_def pfp_def
    40 using assms unfolding lpfp_def pfp_def
    61 
    61 
    62 text{* The for-clause (here and elsewhere) only serves the purpose of fixing
    62 text{* The for-clause (here and elsewhere) only serves the purpose of fixing
    63 the name of the type parameter @{typ 'av} which would otherwise be renamed to
    63 the name of the type parameter @{typ 'av} which would otherwise be renamed to
    64 @{typ 'a}. *}
    64 @{typ 'a}. *}
    65 
    65 
    66 locale Abs_Int = Gamma where \<gamma>=\<gamma> for \<gamma> :: "'av::SL_top \<Rightarrow> val set"
    66 locale Abs_Int = Gamma where \<gamma>=\<gamma> for \<gamma> :: "'av::semilattice \<Rightarrow> val set"
    67 begin
    67 begin
    68 
    68 
    69 fun step' :: "'av st option \<Rightarrow> 'av st option acom \<Rightarrow> 'av st option acom" where
    69 fun step' :: "'av st option \<Rightarrow> 'av st option acom \<Rightarrow> 'av st option acom" where
    70 "step' S (SKIP {P}) = (SKIP {S})" |
    70 "step' S (SKIP {P}) = (SKIP {S})" |
    71 "step' S (x ::= e {P}) =
    71 "step' S (x ::= e {P}) =
    89 lemma in_gamma_update:
    89 lemma in_gamma_update:
    90   "\<lbrakk> s : \<gamma>\<^isub>f S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^isub>f(update S x a)"
    90   "\<lbrakk> s : \<gamma>\<^isub>f S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^isub>f(update S x a)"
    91 by(simp add: \<gamma>_st_def)
    91 by(simp add: \<gamma>_st_def)
    92 
    92 
    93 theorem step_preserves_le:
    93 theorem step_preserves_le:
    94   "\<lbrakk> S \<subseteq> \<gamma>\<^isub>o S'; C \<le> \<gamma>\<^isub>c C';  wt C' X; wt S' X \<rbrakk> \<Longrightarrow> step S C \<le> \<gamma>\<^isub>c (step' S' C')"
    94   "\<lbrakk> S \<subseteq> \<gamma>\<^isub>o S'; C \<le> \<gamma>\<^isub>c C';  C' \<in> L X; S' \<in> L X \<rbrakk> \<Longrightarrow> step S C \<le> \<gamma>\<^isub>c (step' S' C')"
    95 proof(induction C arbitrary: C' S S')
    95 proof(induction C arbitrary: C' S S')
    96   case SKIP thus ?case by(auto simp:SKIP_le map_acom_SKIP)
    96   case SKIP thus ?case by(auto simp:SKIP_le map_acom_SKIP)
    97 next
    97 next
    98   case Assign thus ?case
    98   case Assign thus ?case
    99     by(fastforce simp: Assign_le map_acom_Assign wt_st_def
    99     by(fastforce simp: Assign_le map_acom_Assign L_st_def
   100         intro: aval'_sound in_gamma_update split: option.splits)
   100         intro: aval'_sound in_gamma_update split: option.splits)
   101 next
   101 next
   102   case Seq thus ?case apply (auto simp: Seq_le map_acom_Seq)
   102   case Seq thus ?case apply (auto simp: Seq_le map_acom_Seq)
   103     by (metis le_post post_map_acom wt_post)
   103     by (metis le_post post_map_acom post_in_L)
   104 next
   104 next
   105   case (If b P1 C1 P2 C2 Q)
   105   case (If b P1 C1 P2 C2 Q)
   106   then obtain P1' P2' C1' C2' Q' where
   106   then obtain P1' P2' C1' C2' Q' where
   107       "C' = IF b THEN {P1'} C1' ELSE {P2'} C2' {Q'}"
   107       "C' = IF b THEN {P1'} C1' ELSE {P2'} C2' {Q'}"
   108       "P1 \<subseteq> \<gamma>\<^isub>o P1'" "P2 \<subseteq> \<gamma>\<^isub>o P2'" "Q \<subseteq> \<gamma>\<^isub>o Q'" "C1 \<le> \<gamma>\<^isub>c C1'" "C2 \<le> \<gamma>\<^isub>c C2'"
   108       "P1 \<subseteq> \<gamma>\<^isub>o P1'" "P2 \<subseteq> \<gamma>\<^isub>o P2'" "Q \<subseteq> \<gamma>\<^isub>o Q'" "C1 \<le> \<gamma>\<^isub>c C1'" "C2 \<le> \<gamma>\<^isub>c C2'"
   109     by (fastforce simp: If_le map_acom_If)
   109     by (fastforce simp: If_le map_acom_If)
   110   moreover from this(1) `wt C' X` have wt: "wt C1' X" "wt C2' X" "wt P1' X" "wt P2' X"
   110   moreover from this(1) `C' \<in> L X`
   111     by simp_all
   111   have L: "C1' \<in> L X" "C2' \<in> L X" "P1' \<in> L X" "P2' \<in> L X" by simp_all
   112   moreover have "post C1 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
   112   moreover have "post C1 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
   113     by (metis (no_types) `C1 \<le> \<gamma>\<^isub>c C1'` join_ge1 le_post mono_gamma_o order_trans post_map_acom wt wt_post)
   113     by (metis (no_types) `C1 \<le> \<gamma>\<^isub>c C1'` join_ge1 le_post mono_gamma_o order_trans post_map_acom L post_in_L)
   114   moreover have "post C2 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
   114   moreover have "post C2 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
   115     by (metis (no_types) `C2 \<le> \<gamma>\<^isub>c C2'` join_ge2 le_post mono_gamma_o order_trans post_map_acom wt wt_post)
   115     by (metis (no_types) `C2 \<le> \<gamma>\<^isub>c C2'` join_ge2 le_post mono_gamma_o order_trans post_map_acom L post_in_L)
   116   ultimately show ?case using `S \<subseteq> \<gamma>\<^isub>o S'` `wt S' X`
   116   ultimately show ?case using `S \<subseteq> \<gamma>\<^isub>o S'` `S' \<in> L X`
   117     by (simp add: If.IH subset_iff)
   117     by (simp add: If.IH subset_iff)
   118 next
   118 next
   119   case (While I b P1 C1 Q)
   119   case (While I b P1 C1 Q)
   120   then obtain C1' I' P1' Q' where
   120   then obtain C1' I' P1' Q' where
   121     "C' = {I'} WHILE b DO {P1'} C1' {Q'}"
   121     "C' = {I'} WHILE b DO {P1'} C1' {Q'}"
   122     "I \<subseteq> \<gamma>\<^isub>o I'" "P1 \<subseteq> \<gamma>\<^isub>o P1'" "C1 \<le> \<gamma>\<^isub>c C1'" "Q \<subseteq> \<gamma>\<^isub>o Q'" 
   122     "I \<subseteq> \<gamma>\<^isub>o I'" "P1 \<subseteq> \<gamma>\<^isub>o P1'" "C1 \<le> \<gamma>\<^isub>c C1'" "Q \<subseteq> \<gamma>\<^isub>o Q'" 
   123     by (fastforce simp: map_acom_While While_le)
   123     by (fastforce simp: map_acom_While While_le)
   124   moreover from this(1) `wt C' X`
   124   moreover from this(1) `C' \<in> L X`
   125   have wt: "wt C1' X" "wt I' X" "wt P1' X" by simp_all
   125   have L: "C1' \<in> L X" "I' \<in> L X" "P1' \<in> L X" by simp_all
   126   moreover note compat = `wt S' X` wt_post[OF wt(1)]
   126   moreover note compat = `S' \<in> L X` post_in_L[OF L(1)]
   127   moreover have "S \<union> post C1 \<subseteq> \<gamma>\<^isub>o (S' \<squnion> post C1')"
   127   moreover have "S \<union> post C1 \<subseteq> \<gamma>\<^isub>o (S' \<squnion> post C1')"
   128     using `S \<subseteq> \<gamma>\<^isub>o S'` le_post[OF `C1 \<le> \<gamma>\<^isub>c C1'`, simplified]
   128     using `S \<subseteq> \<gamma>\<^isub>o S'` le_post[OF `C1 \<le> \<gamma>\<^isub>c C1'`, simplified]
   129     by (metis (no_types) join_ge1[OF compat] join_ge2[OF compat] le_sup_iff mono_gamma_o order_trans)
   129     by (metis (no_types) join_ge1[OF compat] join_ge2[OF compat] le_sup_iff mono_gamma_o order_trans)
   130   ultimately show ?case by (simp add: While.IH subset_iff)
   130   ultimately show ?case by (simp add: While.IH subset_iff)
   131 qed
   131 qed
   132 
   132 
   133 lemma wt_step'[simp]:
   133 lemma step'_in_L[simp]:
   134   "\<lbrakk> wt C X; wt S X \<rbrakk> \<Longrightarrow> wt (step' S C) X"
   134   "\<lbrakk> C \<in> L X; S \<in> L X \<rbrakk> \<Longrightarrow> (step' S C) \<in> L X"
   135 proof(induction C arbitrary: S)
   135 proof(induction C arbitrary: S)
   136   case Assign thus ?case
   136   case Assign thus ?case
   137     by(auto simp: wt_st_def update_def split: option.splits)
   137     by(auto simp: L_st_def update_def split: option.splits)
   138 qed auto
   138 qed auto
   139 
   139 
   140 theorem AI_sound: "AI c = Some C \<Longrightarrow> CS c \<le> \<gamma>\<^isub>c C"
   140 theorem AI_sound: "AI c = Some C \<Longrightarrow> CS c \<le> \<gamma>\<^isub>c C"
   141 proof(simp add: CS_def AI_def)
   141 proof(simp add: CS_def AI_def)
   142   assume 1: "lpfp (step' (top c)) c = Some C"
   142   assume 1: "lpfp (step' (top c)) c = Some C"
   143   have "wt C (vars c)"
   143   have "C \<in> L(vars c)"
   144     by(rule lpfp_inv[where P = "%C. wt C (vars c)", OF 1 _ wt_bot])
   144     by(rule lpfp_inv[where P = "%C. C \<in> L(vars c)", OF 1 _ bot_in_L])
   145       (erule wt_step'[OF _ wt_top])
   145       (erule step'_in_L[OF _ top_in_L])
   146   have 2: "step' (top c) C \<sqsubseteq> C" by(rule lpfpc_pfp[OF 1])
   146   have 2: "step' (top c) C \<sqsubseteq> C" by(rule lpfpc_pfp[OF 1])
   147   have 3: "strip (\<gamma>\<^isub>c (step' (top c) C)) = c"
   147   have 3: "strip (\<gamma>\<^isub>c (step' (top c) C)) = c"
   148     by(simp add: strip_lpfp[OF _ 1])
   148     by(simp add: strip_lpfp[OF _ 1])
   149   have "lfp c (step UNIV) \<le> \<gamma>\<^isub>c (step' (top c) C)"
   149   have "lfp c (step UNIV) \<le> \<gamma>\<^isub>c (step' (top c) C)"
   150   proof(rule lfp_lowerbound[simplified,OF 3])
   150   proof(rule lfp_lowerbound[simplified,OF 3])
   151     show "step UNIV (\<gamma>\<^isub>c (step' (top c) C)) \<le> \<gamma>\<^isub>c (step' (top c) C)"
   151     show "step UNIV (\<gamma>\<^isub>c (step' (top c) C)) \<le> \<gamma>\<^isub>c (step' (top c) C)"
   152     proof(rule step_preserves_le[OF _ _ `wt C (vars c)` wt_top])
   152     proof(rule step_preserves_le[OF _ _ `C \<in> L(vars c)` top_in_L])
   153       show "UNIV \<subseteq> \<gamma>\<^isub>o (top c)" by simp
   153       show "UNIV \<subseteq> \<gamma>\<^isub>o (top c)" by simp
   154       show "\<gamma>\<^isub>c (step' (top c) C) \<le> \<gamma>\<^isub>c C" by(rule mono_gamma_c[OF 2])
   154       show "\<gamma>\<^isub>c (step' (top c) C) \<le> \<gamma>\<^isub>c C" by(rule mono_gamma_c[OF 2])
   155     qed
   155     qed
   156   qed
   156   qed
   157   from this 2 show "lfp c (step UNIV) \<le> \<gamma>\<^isub>c C"
   157   from this 2 show "lfp c (step UNIV) \<le> \<gamma>\<^isub>c C"
   161 end
   161 end
   162 
   162 
   163 
   163 
   164 subsubsection "Monotonicity"
   164 subsubsection "Monotonicity"
   165 
   165 
   166 lemma le_join_disj: "wt y X \<Longrightarrow> wt (z::_::SL_top_wt) X \<Longrightarrow> x \<sqsubseteq> y \<or> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<squnion> z"
   166 lemma le_join_disj: "y \<in> L X \<Longrightarrow> (z::_::semilatticeL) \<in> L X \<Longrightarrow>
       
   167   x \<sqsubseteq> y \<or> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<squnion> z"
   167 by (metis join_ge1 join_ge2 preord_class.le_trans)
   168 by (metis join_ge1 join_ge2 preord_class.le_trans)
   168 
   169 
   169 locale Abs_Int_mono = Abs_Int +
   170 locale Abs_Int_mono = Abs_Int +
   170 assumes mono_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> plus' a1 a2 \<sqsubseteq> plus' b1 b2"
   171 assumes mono_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> plus' a1 a2 \<sqsubseteq> plus' b1 b2"
   171 begin
   172 begin
   172 
   173 
   173 lemma mono_aval': "S1 \<sqsubseteq> S2 \<Longrightarrow> wt S1 X \<Longrightarrow> vars e \<subseteq> X \<Longrightarrow> aval' e S1 \<sqsubseteq> aval' e S2"
   174 lemma mono_aval':
   174 by(induction e) (auto simp: le_st_def mono_plus' wt_st_def)
   175   "S1 \<sqsubseteq> S2 \<Longrightarrow> S1 \<in> L X \<Longrightarrow> vars e \<subseteq> X \<Longrightarrow> aval' e S1 \<sqsubseteq> aval' e S2"
   175 
   176 by(induction e) (auto simp: le_st_def mono_plus' L_st_def)
   176 theorem mono_step': "wt S1 X \<Longrightarrow> wt S2 X \<Longrightarrow> wt C1 X \<Longrightarrow> wt C2 X \<Longrightarrow>
   177 
       
   178 theorem mono_step': "S1 \<in> L X \<Longrightarrow> S2 \<in> L X \<Longrightarrow> C1 \<in> L X \<Longrightarrow> C2 \<in> L X \<Longrightarrow>
   177   S1 \<sqsubseteq> S2 \<Longrightarrow> C1 \<sqsubseteq> C2 \<Longrightarrow> step' S1 C1 \<sqsubseteq> step' S2 C2"
   179   S1 \<sqsubseteq> S2 \<Longrightarrow> C1 \<sqsubseteq> C2 \<Longrightarrow> step' S1 C1 \<sqsubseteq> step' S2 C2"
   178 apply(induction C1 C2 arbitrary: S1 S2 rule: le_acom.induct)
   180 apply(induction C1 C2 arbitrary: S1 S2 rule: le_acom.induct)
   179 apply (auto simp: Let_def mono_aval' mono_post
   181 apply (auto simp: Let_def mono_aval' mono_post
   180   le_join_disj le_join_disj[OF  wt_post wt_post]
   182   le_join_disj le_join_disj[OF  post_in_L post_in_L]
   181             split: option.split)
   183             split: option.split)
   182 done
   184 done
   183 
   185 
   184 lemma mono_step'_top: "wt c (vars c0) \<Longrightarrow> wt c' (vars c0) \<Longrightarrow> c \<sqsubseteq> c' \<Longrightarrow> step' (top c0) c \<sqsubseteq> step' (top c0) c'"
   186 lemma mono_step'_top: "C \<in> L(vars c) \<Longrightarrow> C' \<in> L(vars c) \<Longrightarrow>
   185 by (metis wt_top mono_step' preord_class.le_refl)
   187   C \<sqsubseteq> C' \<Longrightarrow> step' (top c) C \<sqsubseteq> step' (top c) C'"
       
   188 by (metis top_in_L mono_step' preord_class.le_refl)
   186 
   189 
   187 end
   190 end
   188 
   191 
   189 
   192 
   190 subsubsection "Termination"
   193 subsubsection "Termination"
   220 by(fastforce intro: pfp_termination[where I=I and m=m] assms bot_least
   223 by(fastforce intro: pfp_termination[where I=I and m=m] assms bot_least
   221    simp: assms(5))
   224    simp: assms(5))
   222 
   225 
   223 
   226 
   224 locale Abs_Int_measure =
   227 locale Abs_Int_measure =
   225   Abs_Int_mono where \<gamma>=\<gamma> for \<gamma> :: "'av::SL_top \<Rightarrow> val set" +
   228   Abs_Int_mono where \<gamma>=\<gamma> for \<gamma> :: "'av::semilattice \<Rightarrow> val set" +
   226 fixes m :: "'av \<Rightarrow> nat"
   229 fixes m :: "'av \<Rightarrow> nat"
   227 fixes h :: "nat"
   230 fixes h :: "nat"
   228 assumes m1: "x \<sqsubseteq> y \<Longrightarrow> m x \<ge> m y"
   231 assumes m1: "x \<sqsubseteq> y \<Longrightarrow> m x \<ge> m y"
   229 assumes m2: "x \<sqsubset> y \<Longrightarrow> m x > m y"
   232 assumes m2: "x \<sqsubset> y \<Longrightarrow> m x > m y"
   230 assumes h: "m x \<le> h"
   233 assumes h: "m x \<le> h"
   253 
   256 
   254 definition m_o :: "nat \<Rightarrow> 'av st option \<Rightarrow> nat" where
   257 definition m_o :: "nat \<Rightarrow> 'av st option \<Rightarrow> nat" where
   255 "m_o d opt = (case opt of None \<Rightarrow> h*d+1 | Some S \<Rightarrow> m_st S)"
   258 "m_o d opt = (case opt of None \<Rightarrow> h*d+1 | Some S \<Rightarrow> m_st S)"
   256 
   259 
   257 definition m_c :: "'av st option acom \<Rightarrow> nat" where
   260 definition m_c :: "'av st option acom \<Rightarrow> nat" where
   258 "m_c c = (\<Sum>i<size(annos c). m_o (card(vars(strip c))) (annos c ! i))"
   261 "m_c C = (\<Sum>i<size(annos C). m_o (card(vars(strip C))) (annos C ! i))"
   259 
   262 
   260 lemma m_st_h: "wt x X \<Longrightarrow> finite X \<Longrightarrow> m_st x \<le> h * card X"
   263 lemma m_st_h: "x \<in> L X \<Longrightarrow> finite X \<Longrightarrow> m_st x \<le> h * card X"
   261 by(simp add: wt_st_def m_st_def)
   264 by(simp add: L_st_def m_st_def)
   262   (metis nat_mult_commute of_nat_id setsum_bounded[OF h])
   265   (metis nat_mult_commute of_nat_id setsum_bounded[OF h])
   263 
   266 
   264 lemma m_o1: "finite X \<Longrightarrow> wt o1 X \<Longrightarrow> wt o2 X \<Longrightarrow>
   267 lemma m_o1: "finite X \<Longrightarrow> o1 \<in> L X \<Longrightarrow> o2 \<in> L X \<Longrightarrow>
   265   o1 \<sqsubseteq> o2 \<Longrightarrow> m_o (card X) o1 \<ge> m_o (card X) o2"
   268   o1 \<sqsubseteq> o2 \<Longrightarrow> m_o (card X) o1 \<ge> m_o (card X) o2"
   266 proof(induction o1 o2 rule: le_option.induct)
   269 proof(induction o1 o2 rule: le_option.induct)
   267   case 1 thus ?case by (simp add: m_o_def)(metis m_st1)
   270   case 1 thus ?case by (simp add: m_o_def)(metis m_st1)
   268 next
   271 next
   269   case 2 thus ?case
   272   case 2 thus ?case
   270     by(simp add: wt_option_def m_o_def le_SucI m_st_h split: option.splits)
   273     by(simp add: L_option_def m_o_def le_SucI m_st_h split: option.splits)
   271 next
   274 next
   272   case 3 thus ?case by simp
   275   case 3 thus ?case by simp
   273 qed
   276 qed
   274 
   277 
   275 lemma m_o2: "finite X \<Longrightarrow> wt o1 X \<Longrightarrow> wt o2 X \<Longrightarrow>
   278 lemma m_o2: "finite X \<Longrightarrow> o1 \<in> L X \<Longrightarrow> o2 \<in> L X \<Longrightarrow>
   276   o1 \<sqsubset> o2 \<Longrightarrow> m_o (card X) o1 > m_o (card X) o2"
   279   o1 \<sqsubset> o2 \<Longrightarrow> m_o (card X) o1 > m_o (card X) o2"
   277 proof(induction o1 o2 rule: le_option.induct)
   280 proof(induction o1 o2 rule: le_option.induct)
   278   case 1 thus ?case by (simp add: m_o_def wt_st_def m_st2)
   281   case 1 thus ?case by (simp add: m_o_def L_st_def m_st2)
   279 next
   282 next
   280   case 2 thus ?case
   283   case 2 thus ?case
   281     by(auto simp add: m_o_def le_imp_less_Suc m_st_h)
   284     by(auto simp add: m_o_def le_imp_less_Suc m_st_h)
   282 next
   285 next
   283   case 3 thus ?case by simp
   286   case 3 thus ?case by simp
   284 qed
   287 qed
   285 
   288 
   286 lemma m_c2: "wt c1 (vars(strip c1)) \<Longrightarrow> wt c2 (vars(strip c2)) \<Longrightarrow>
   289 lemma m_c2: "C1 \<in> L(vars(strip C1)) \<Longrightarrow> C2 \<in> L(vars(strip C2)) \<Longrightarrow>
   287   c1 \<sqsubset> c2 \<Longrightarrow> m_c c1 > m_c c2"
   290   C1 \<sqsubset> C2 \<Longrightarrow> m_c C1 > m_c C2"
   288 proof(auto simp add: le_iff_le_annos m_c_def size_annos_same[of c1 c2] wt_acom_def)
   291 proof(auto simp add: le_iff_le_annos m_c_def size_annos_same[of C1 C2] L_acom_def)
   289   let ?X = "vars(strip c2)"
   292   let ?X = "vars(strip C2)"
   290   let ?n = "card ?X"
   293   let ?n = "card ?X"
   291   assume V1: "\<forall>a\<in>set(annos c1). wt a ?X"
   294   assume V1: "\<forall>a\<in>set(annos C1). a \<in> L ?X"
   292     and V2: "\<forall>a\<in>set(annos c2). wt a ?X"
   295     and V2: "\<forall>a\<in>set(annos C2). a \<in> L ?X"
   293     and strip_eq: "strip c1 = strip c2"
   296     and strip_eq: "strip C1 = strip C2"
   294     and 0: "\<forall>i<size(annos c2). annos c1 ! i \<sqsubseteq> annos c2 ! i"
   297     and 0: "\<forall>i<size(annos C2). annos C1 ! i \<sqsubseteq> annos C2 ! i"
   295   hence 1: "\<forall>i<size(annos c2). m_o ?n (annos c1 ! i) \<ge> m_o ?n (annos c2 ! i)"
   298   hence 1: "\<forall>i<size(annos C2). m_o ?n (annos C1 ! i) \<ge> m_o ?n (annos C2 ! i)"
   296     by (auto simp: all_set_conv_all_nth)
   299     by (auto simp: all_set_conv_all_nth)
   297        (metis finite_cvars m_o1 size_annos_same2)
   300        (metis finite_cvars m_o1 size_annos_same2)
   298   fix i assume "i < size(annos c2)" "\<not> annos c2 ! i \<sqsubseteq> annos c1 ! i"
   301   fix i assume "i < size(annos C2)" "\<not> annos C2 ! i \<sqsubseteq> annos C1 ! i"
   299   hence "m_o ?n (annos c1 ! i) > m_o ?n (annos c2 ! i)" (is "?P i")
   302   hence "m_o ?n (annos C1 ! i) > m_o ?n (annos C2 ! i)" (is "?P i")
   300     by(metis m_o2[OF finite_cvars] V1 V2 strip_eq nth_mem size_annos_same 0)
   303     by(metis m_o2[OF finite_cvars] V1 V2 nth_mem size_annos_same[OF strip_eq] 0)
   301   hence 2: "\<exists>i < size(annos c2). ?P i" using `i < size(annos c2)` by blast
   304   hence 2: "\<exists>i < size(annos C2). ?P i" using `i < size(annos C2)` by blast
   302   show "(\<Sum>i<size(annos c2). m_o ?n (annos c2 ! i))
   305   show "(\<Sum>i<size(annos C2). m_o ?n (annos C2 ! i))
   303          < (\<Sum>i<size(annos c2). m_o ?n (annos c1 ! i))"
   306          < (\<Sum>i<size(annos C2). m_o ?n (annos C1 ! i))"
   304     apply(rule setsum_strict_mono_ex1) using 1 2 by (auto)
   307     apply(rule setsum_strict_mono_ex1) using 1 2 by (auto)
   305 qed
   308 qed
   306 
   309 
   307 lemma AI_Some_measure: "\<exists>C. AI c = Some C"
   310 lemma AI_Some_measure: "\<exists>C. AI c = Some C"
   308 unfolding AI_def
   311 unfolding AI_def
   309 apply(rule lpfp_termination[where I = "%C. strip C = c \<and> wt C (vars c)"
   312 apply(rule lpfp_termination[where I = "%C. strip C = c \<and> C \<in> L(vars c)"
   310   and m="m_c"])
   313   and m="m_c"])
   311 apply(simp_all add: m_c2 mono_step'_top)
   314 apply(simp_all add: m_c2 mono_step'_top)
   312 done
   315 done
   313 
   316 
   314 end
   317 end