| 
47613
 | 
     1  | 
(* Author: Tobias Nipkow *)
  | 
| 
 | 
     2  | 
  | 
| 
 | 
     3  | 
theory Abs_Int1
  | 
| 
 | 
     4  | 
imports Abs_State
  | 
| 
 | 
     5  | 
begin
  | 
| 
 | 
     6  | 
  | 
| 
 | 
     7  | 
lemma le_iff_le_annos_zip: "C1 \<sqsubseteq> C2 \<longleftrightarrow>
  | 
| 
 | 
     8  | 
 (\<forall> (a1,a2) \<in> set(zip (annos C1) (annos C2)). a1 \<sqsubseteq> a2) \<and> strip C1 = strip C2"
  | 
| 
 | 
     9  | 
by(induct C1 C2 rule: le_acom.induct) (auto simp: size_annos_same2)
  | 
| 
 | 
    10  | 
  | 
| 
 | 
    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)"
  | 
| 
 | 
    13  | 
by(auto simp add: le_iff_le_annos_zip set_zip) (metis size_annos_same2)
  | 
| 
 | 
    14  | 
  | 
| 
 | 
    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"
  | 
| 
 | 
    17  | 
by(simp add: mono_fun wt_st_def)
  | 
| 
 | 
    18  | 
  | 
| 
 | 
    19  | 
lemma wt_bot[simp]: "wt (bot c) (vars c)"
  | 
| 
 | 
    20  | 
by(simp add: wt_acom_def bot_def)
  | 
| 
 | 
    21  | 
  | 
| 
 | 
    22  | 
lemma wt_acom_simps[simp]: "wt (SKIP {P}) X \<longleftrightarrow> wt P X"
 | 
| 
 | 
    23  | 
  "wt (x ::= e {P}) X \<longleftrightarrow> x : X \<and> vars e \<subseteq> X \<and> wt P X"
 | 
| 
 | 
    24  | 
  "wt (C1;C2) X \<longleftrightarrow> wt C1 X \<and> wt C2 X"
  | 
| 
 | 
    25  | 
  "wt (IF b THEN C1 ELSE C2 {P}) X \<longleftrightarrow>
 | 
| 
 | 
    26  | 
   vars b \<subseteq> X \<and> wt C1 X \<and> wt C2 X \<and> wt P X"
  | 
| 
 | 
    27  | 
  "wt ({I} WHILE b DO C {P}) X \<longleftrightarrow>
 | 
| 
 | 
    28  | 
   wt I X \<and> vars b \<subseteq> X \<and> wt C X \<and> wt P X"
  | 
| 
 | 
    29  | 
by(auto simp add: wt_acom_def)
  | 
| 
 | 
    30  | 
  | 
| 
 | 
    31  | 
lemma wt_post[simp]: "wt c  X \<Longrightarrow> wt (post c) X"
  | 
| 
 | 
    32  | 
by(induction c)(auto simp: wt_acom_def)
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
lemma lpfp_inv:
  | 
| 
 | 
    35  | 
assumes "lpfp f x0 = Some x" and "\<And>x. P x \<Longrightarrow> P(f x)" and "P(bot x0)"
  | 
| 
 | 
    36  | 
shows "P x"
  | 
| 
 | 
    37  | 
using assms unfolding lpfp_def pfp_def
  | 
| 
 | 
    38  | 
by (metis (lifting) while_option_rule)
  | 
| 
 | 
    39  | 
  | 
| 
 | 
    40  | 
  | 
| 
 | 
    41  | 
subsection "Computable Abstract Interpretation"
  | 
| 
 | 
    42  | 
  | 
| 
 | 
    43  | 
text{* Abstract interpretation over type @{text st} instead of
 | 
| 
 | 
    44  | 
functions. *}
  | 
| 
 | 
    45  | 
  | 
| 
 | 
    46  | 
context Gamma
  | 
| 
 | 
    47  | 
begin
  | 
| 
 | 
    48  | 
  | 
| 
 | 
    49  | 
fun aval' :: "aexp \<Rightarrow> 'av st \<Rightarrow> 'av" where
  | 
| 
 | 
    50  | 
"aval' (N n) S = num' n" |
  | 
| 
 | 
    51  | 
"aval' (V x) S = fun S x" |
  | 
| 
 | 
    52  | 
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
  | 
| 
 | 
    53  | 
  | 
| 
 | 
    54  | 
lemma aval'_sound: "s : \<gamma>\<^isub>f S \<Longrightarrow> vars a \<subseteq> dom S \<Longrightarrow> aval a s : \<gamma>(aval' a S)"
  | 
| 
 | 
    55  | 
by (induction a) (auto simp: gamma_num' gamma_plus' \<gamma>_st_def)
  | 
| 
 | 
    56  | 
  | 
| 
 | 
    57  | 
end
  | 
| 
 | 
    58  | 
  | 
| 
 | 
    59  | 
text{* The for-clause (here and elsewhere) only serves the purpose of fixing
 | 
| 
 | 
    60  | 
the name of the type parameter @{typ 'av} which would otherwise be renamed to
 | 
| 
 | 
    61  | 
@{typ 'a}. *}
 | 
| 
 | 
    62  | 
  | 
| 
 | 
    63  | 
locale Abs_Int = Gamma where \<gamma>=\<gamma> for \<gamma> :: "'av::SL_top \<Rightarrow> val set"
  | 
| 
 | 
    64  | 
begin
  | 
| 
 | 
    65  | 
  | 
| 
 | 
    66  | 
fun step' :: "'av st option \<Rightarrow> 'av st option acom \<Rightarrow> 'av st option acom" where
  | 
| 
 | 
    67  | 
"step' S (SKIP {P}) = (SKIP {S})" |
 | 
| 
 | 
    68  | 
"step' S (x ::= e {P}) =
 | 
| 
 | 
    69  | 
  x ::= e {case S of None \<Rightarrow> None | Some S \<Rightarrow> Some(update S x (aval' e S))}" |
 | 
| 
 | 
    70  | 
"step' S (C1; C2) = step' S C1; step' (post C1) C2" |
  | 
| 
 | 
    71  | 
"step' S (IF b THEN C1 ELSE C2 {P}) =
 | 
| 
 | 
    72  | 
  (IF b THEN step' S C1 ELSE step' S C2 {post C1 \<squnion> post C2})" |
 | 
| 
 | 
    73  | 
"step' S ({Inv} WHILE b DO C {P}) =
 | 
| 
 | 
    74  | 
   {S \<squnion> post C} WHILE b DO step' Inv C {Inv}"
 | 
| 
 | 
    75  | 
  | 
| 
 | 
    76  | 
definition AI :: "com \<Rightarrow> 'av st option acom option" where
  | 
| 
 | 
    77  | 
"AI c = lpfp (step' (top c)) c"
  | 
| 
 | 
    78  | 
  | 
| 
 | 
    79  | 
  | 
| 
 | 
    80  | 
lemma strip_step'[simp]: "strip(step' S C) = strip C"
  | 
| 
 | 
    81  | 
by(induct C arbitrary: S) (simp_all add: Let_def)
  | 
| 
 | 
    82  | 
  | 
| 
 | 
    83  | 
  | 
| 
 | 
    84  | 
text{* Soundness: *}
 | 
| 
 | 
    85  | 
  | 
| 
 | 
    86  | 
lemma in_gamma_update:
  | 
| 
 | 
    87  | 
  "\<lbrakk> s : \<gamma>\<^isub>f S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^isub>f(update S x a)"
  | 
| 
 | 
    88  | 
by(simp add: \<gamma>_st_def)
  | 
| 
 | 
    89  | 
  | 
| 
 | 
    90  | 
theorem step_preserves_le:
  | 
| 
 | 
    91  | 
  "\<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')"
  | 
| 
 | 
    92  | 
proof(induction C arbitrary: C' S S')
  | 
| 
 | 
    93  | 
  case SKIP thus ?case by(auto simp:SKIP_le map_acom_SKIP)
  | 
| 
 | 
    94  | 
next
  | 
| 
 | 
    95  | 
  case Assign thus ?case
  | 
| 
 | 
    96  | 
    by(fastforce simp: Assign_le map_acom_Assign wt_st_def
  | 
| 
 | 
    97  | 
        intro: aval'_sound in_gamma_update split: option.splits)
  | 
| 
 | 
    98  | 
next
  | 
| 
 | 
    99  | 
  case Semi thus ?case apply (auto simp: Semi_le map_acom_Semi)
  | 
| 
 | 
   100  | 
    by (metis le_post post_map_acom wt_post)
  | 
| 
 | 
   101  | 
next
  | 
| 
 | 
   102  | 
  case (If b C1 C2 P)
  | 
| 
 | 
   103  | 
  then obtain C1' C2' P' where
  | 
| 
 | 
   104  | 
      "C' = IF b THEN C1' ELSE C2' {P'}"
 | 
| 
 | 
   105  | 
      "P \<subseteq> \<gamma>\<^isub>o P'" "C1 \<le> \<gamma>\<^isub>c C1'" "C2 \<le> \<gamma>\<^isub>c C2'"
  | 
| 
 | 
   106  | 
    by (fastforce simp: If_le map_acom_If)
  | 
| 
 | 
   107  | 
  moreover from this(1) `wt C' X` have wt: "wt C1' X" "wt C2' X"
  | 
| 
 | 
   108  | 
    by simp_all
  | 
| 
 | 
   109  | 
  moreover have "post C1 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
  | 
| 
 | 
   110  | 
    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)
  | 
| 
 | 
   111  | 
  moreover have "post C2 \<subseteq> \<gamma>\<^isub>o(post C1' \<squnion> post C2')"
  | 
| 
 | 
   112  | 
    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)
  | 
| 
 | 
   113  | 
  ultimately show ?case using `S \<subseteq> \<gamma>\<^isub>o S'` `wt S' X`
  | 
| 
 | 
   114  | 
    by (simp add: If.IH subset_iff)
  | 
| 
 | 
   115  | 
next
  | 
| 
 | 
   116  | 
  case (While I b C1 P)
  | 
| 
 | 
   117  | 
  then obtain C1' I' P' where
  | 
| 
 | 
   118  | 
    "C' = {I'} WHILE b DO C1' {P'}"
 | 
| 
 | 
   119  | 
    "I \<subseteq> \<gamma>\<^isub>o I'" "P \<subseteq> \<gamma>\<^isub>o P'" "C1 \<le> \<gamma>\<^isub>c C1'"
  | 
| 
 | 
   120  | 
    by (fastforce simp: map_acom_While While_le)
  | 
| 
 | 
   121  | 
  moreover from this(1) `wt C' X`
  | 
| 
 | 
   122  | 
  have wt: "wt C1' X" "wt I' X" by simp_all
  | 
| 
 | 
   123  | 
  moreover note compat = `wt S' X` wt_post[OF wt(1)]
  | 
| 
 | 
   124  | 
  moreover have "S \<union> post C1 \<subseteq> \<gamma>\<^isub>o (S' \<squnion> post C1')"
  | 
| 
 | 
   125  | 
    using `S \<subseteq> \<gamma>\<^isub>o S'` le_post[OF `C1 \<le> \<gamma>\<^isub>c C1'`, simplified]
  | 
| 
 | 
   126  | 
    by (metis (no_types) join_ge1[OF compat] join_ge2[OF compat] le_sup_iff mono_gamma_o order_trans)
  | 
| 
 | 
   127  | 
  ultimately show ?case by (simp add: While.IH subset_iff)
  | 
| 
 | 
   128  | 
qed
  | 
| 
 | 
   129  | 
  | 
| 
 | 
   130  | 
lemma wt_step'[simp]:
  | 
| 
 | 
   131  | 
  "\<lbrakk> wt C X; wt S X \<rbrakk> \<Longrightarrow> wt (step' S C) X"
  | 
| 
 | 
   132  | 
proof(induction C arbitrary: S)
  | 
| 
 | 
   133  | 
  case Assign thus ?case
  | 
| 
 | 
   134  | 
    by(auto simp: wt_st_def update_def split: option.splits)
  | 
| 
 | 
   135  | 
qed auto
  | 
| 
 | 
   136  | 
  | 
| 
 | 
   137  | 
theorem AI_sound: "AI c = Some C \<Longrightarrow> CS c \<le> \<gamma>\<^isub>c C"
  | 
| 
 | 
   138  | 
proof(simp add: CS_def AI_def)
  | 
| 
 | 
   139  | 
  assume 1: "lpfp (step' (top c)) c = Some C"
  | 
| 
 | 
   140  | 
  have "wt C (vars c)"
  | 
| 
 | 
   141  | 
    by(rule lpfp_inv[where P = "%C. wt C (vars c)", OF 1 _ wt_bot])
  | 
| 
 | 
   142  | 
      (erule wt_step'[OF _ wt_top])
  | 
| 
 | 
   143  | 
  have 2: "step' (top c) C \<sqsubseteq> C" by(rule lpfpc_pfp[OF 1])
  | 
| 
 | 
   144  | 
  have 3: "strip (\<gamma>\<^isub>c (step' (top c) C)) = c"
  | 
| 
 | 
   145  | 
    by(simp add: strip_lpfp[OF _ 1])
  | 
| 
 | 
   146  | 
  have "lfp (step UNIV) c \<le> \<gamma>\<^isub>c (step' (top c) C)"
  | 
| 
 | 
   147  | 
  proof(rule lfp_lowerbound[simplified,OF 3])
  | 
| 
 | 
   148  | 
    show "step UNIV (\<gamma>\<^isub>c (step' (top c) C)) \<le> \<gamma>\<^isub>c (step' (top c) C)"
  | 
| 
 | 
   149  | 
    proof(rule step_preserves_le[OF _ _ `wt C (vars c)` wt_top])
  | 
| 
 | 
   150  | 
      show "UNIV \<subseteq> \<gamma>\<^isub>o (top c)" by simp
  | 
| 
 | 
   151  | 
      show "\<gamma>\<^isub>c (step' (top c) C) \<le> \<gamma>\<^isub>c C" by(rule mono_gamma_c[OF 2])
  | 
| 
 | 
   152  | 
    qed
  | 
| 
 | 
   153  | 
  qed
  | 
| 
 | 
   154  | 
  from this 2 show "lfp (step UNIV) c \<le> \<gamma>\<^isub>c C"
  | 
| 
 | 
   155  | 
    by (blast intro: mono_gamma_c order_trans)
  | 
| 
 | 
   156  | 
qed
  | 
| 
 | 
   157  | 
  | 
| 
 | 
   158  | 
end
  | 
| 
 | 
   159  | 
  | 
| 
 | 
   160  | 
  | 
| 
 | 
   161  | 
subsubsection "Monotonicity"
  | 
| 
 | 
   162  | 
  | 
| 
 | 
   163  | 
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"
  | 
| 
 | 
   164  | 
by (metis join_ge1 join_ge2 preord_class.le_trans)
  | 
| 
 | 
   165  | 
  | 
| 
 | 
   166  | 
locale Abs_Int_mono = Abs_Int +
  | 
| 
 | 
   167  | 
assumes mono_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> plus' a1 a2 \<sqsubseteq> plus' b1 b2"
  | 
| 
 | 
   168  | 
begin
  | 
| 
 | 
   169  | 
  | 
| 
 | 
   170  | 
lemma mono_aval': "S1 \<sqsubseteq> S2 \<Longrightarrow> wt S1 X \<Longrightarrow> vars e \<subseteq> X \<Longrightarrow> aval' e S1 \<sqsubseteq> aval' e S2"
  | 
| 
 | 
   171  | 
by(induction e) (auto simp: le_st_def mono_plus' wt_st_def)
  | 
| 
 | 
   172  | 
  | 
| 
 | 
   173  | 
theorem mono_step': "wt S1 X \<Longrightarrow> wt S2 X \<Longrightarrow> wt C1 X \<Longrightarrow> wt C2 X \<Longrightarrow>
  | 
| 
 | 
   174  | 
  S1 \<sqsubseteq> S2 \<Longrightarrow> C1 \<sqsubseteq> C2 \<Longrightarrow> step' S1 C1 \<sqsubseteq> step' S2 C2"
  | 
| 
 | 
   175  | 
apply(induction C1 C2 arbitrary: S1 S2 rule: le_acom.induct)
  | 
| 
 | 
   176  | 
apply (auto simp: Let_def mono_aval' mono_post
  | 
| 
 | 
   177  | 
  le_join_disj le_join_disj[OF  wt_post wt_post]
  | 
| 
 | 
   178  | 
            split: option.split)
  | 
| 
 | 
   179  | 
done
  | 
| 
 | 
   180  | 
  | 
| 
 | 
   181  | 
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'"
  | 
| 
 | 
   182  | 
by (metis wt_top mono_step' preord_class.le_refl)
  | 
| 
 | 
   183  | 
  | 
| 
 | 
   184  | 
end
  | 
| 
 | 
   185  | 
  | 
| 
 | 
   186  | 
  | 
| 
 | 
   187  | 
subsubsection "Termination"
  | 
| 
 | 
   188  | 
  | 
| 
 | 
   189  | 
abbreviation sqless (infix "\<sqsubset>" 50) where
  | 
| 
 | 
   190  | 
"x \<sqsubset> y == x \<sqsubseteq> y \<and> \<not> y \<sqsubseteq> x"
  | 
| 
 | 
   191  | 
  | 
| 
 | 
   192  | 
lemma pfp_termination:
  | 
| 
 | 
   193  | 
fixes x0 :: "'a::preord" and m :: "'a \<Rightarrow> nat"
  | 
| 
 | 
   194  | 
assumes mono: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y"
  | 
| 
 | 
   195  | 
and m: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<sqsubset> y \<Longrightarrow> m x > m y"
  | 
| 
 | 
   196  | 
and I: "\<And>x y. I x \<Longrightarrow> I(f x)" and "I x0" and "x0 \<sqsubseteq> f x0"
  | 
| 
 | 
   197  | 
shows "EX x. pfp f x0 = Some x"
  | 
| 
 | 
   198  | 
proof(simp add: pfp_def, rule wf_while_option_Some[where P = "%x. I x & x \<sqsubseteq> f x"])
  | 
| 
 | 
   199  | 
  show "wf {(y,x). ((I x \<and> x \<sqsubseteq> f x) \<and> \<not> f x \<sqsubseteq> x) \<and> y = f x}"
 | 
| 
 | 
   200  | 
    by(rule wf_subset[OF wf_measure[of m]]) (auto simp: m I)
  | 
| 
 | 
   201  | 
next
  | 
| 
 | 
   202  | 
  show "I x0 \<and> x0 \<sqsubseteq> f x0" using `I x0` `x0 \<sqsubseteq> f x0` by blast
  | 
| 
 | 
   203  | 
next
  | 
| 
 | 
   204  | 
  fix x assume "I x \<and> x \<sqsubseteq> f x" thus "I(f x) \<and> f x \<sqsubseteq> f(f x)"
  | 
| 
 | 
   205  | 
    by (blast intro: I mono)
  | 
| 
 | 
   206  | 
qed
  | 
| 
 | 
   207  | 
  | 
| 
 | 
   208  | 
lemma lpfp_termination:
  | 
| 
 | 
   209  | 
fixes f :: "'a::preord option acom \<Rightarrow> 'a option acom"
  | 
| 
 | 
   210  | 
and m :: "'a option acom \<Rightarrow> nat" and I :: "'a option acom \<Rightarrow> bool"
  | 
| 
 | 
   211  | 
assumes "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<sqsubset> y \<Longrightarrow> m x > m y"
  | 
| 
 | 
   212  | 
and "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y"
  | 
| 
 | 
   213  | 
and "\<And>x y. I x \<Longrightarrow> I(f x)" and "I(bot c)"
  | 
| 
 | 
   214  | 
and "\<And>C. strip (f C) = strip C"
  | 
| 
 | 
   215  | 
shows "\<exists>c'. lpfp f c = Some c'"
  | 
| 
 | 
   216  | 
unfolding lpfp_def
  | 
| 
 | 
   217  | 
by(fastforce intro: pfp_termination[where I=I and m=m] assms bot_least
  | 
| 
 | 
   218  | 
   simp: assms(5))
  | 
| 
 | 
   219  | 
  | 
| 
 | 
   220  | 
  | 
| 
 | 
   221  | 
locale Abs_Int_measure =
  | 
| 
 | 
   222  | 
  Abs_Int_mono where \<gamma>=\<gamma> for \<gamma> :: "'av::SL_top \<Rightarrow> val set" +
  | 
| 
 | 
   223  | 
fixes m :: "'av \<Rightarrow> nat"
  | 
| 
 | 
   224  | 
fixes h :: "nat"
  | 
| 
 | 
   225  | 
assumes m1: "x \<sqsubseteq> y \<Longrightarrow> m x \<ge> m y"
  | 
| 
 | 
   226  | 
assumes m2: "x \<sqsubset> y \<Longrightarrow> m x > m y"
  | 
| 
 | 
   227  | 
assumes h: "m x \<le> h"
  | 
| 
 | 
   228  | 
begin
  | 
| 
 | 
   229  | 
  | 
| 
 | 
   230  | 
definition "m_st S = (\<Sum> x \<in> dom S. m(fun S x))"
  | 
| 
 | 
   231  | 
  | 
| 
 | 
   232  | 
lemma m_st1: "S1 \<sqsubseteq> S2 \<Longrightarrow> m_st S1 \<ge> m_st S2"
  | 
| 
 | 
   233  | 
proof(auto simp add: le_st_def m_st_def)
  | 
| 
 | 
   234  | 
  assume "\<forall>x\<in>dom S2. fun S1 x \<sqsubseteq> fun S2 x"
  | 
| 
 | 
   235  | 
  hence "\<forall>x\<in>dom S2. m(fun S1 x) \<ge> m(fun S2 x)" by (metis m1)
  | 
| 
 | 
   236  | 
  thus "(\<Sum>x\<in>dom S2. m (fun S2 x)) \<le> (\<Sum>x\<in>dom S2. m (fun S1 x))"
  | 
| 
 | 
   237  | 
    by (metis setsum_mono)
  | 
| 
 | 
   238  | 
qed
  | 
| 
 | 
   239  | 
  | 
| 
 | 
   240  | 
lemma m_st2: "finite(dom S1) \<Longrightarrow> S1 \<sqsubset> S2 \<Longrightarrow> m_st S1 > m_st S2"
  | 
| 
 | 
   241  | 
proof(auto simp add: le_st_def m_st_def)
  | 
| 
 | 
   242  | 
  assume "finite(dom S2)" and 0: "\<forall>x\<in>dom S2. fun S1 x \<sqsubseteq> fun S2 x"
  | 
| 
 | 
   243  | 
  hence 1: "\<forall>x\<in>dom S2. m(fun S1 x) \<ge> m(fun S2 x)" by (metis m1)
  | 
| 
 | 
   244  | 
  fix x assume "x \<in> dom S2" "\<not> fun S2 x \<sqsubseteq> fun S1 x"
  | 
| 
 | 
   245  | 
  hence 2: "\<exists>x\<in>dom S2. m(fun S1 x) > m(fun S2 x)" using 0 m2 by blast
  | 
| 
 | 
   246  | 
  from setsum_strict_mono_ex1[OF `finite(dom S2)` 1 2]
  | 
| 
 | 
   247  | 
  show "(\<Sum>x\<in>dom S2. m (fun S2 x)) < (\<Sum>x\<in>dom S2. m (fun S1 x))" .
  | 
| 
 | 
   248  | 
qed
  | 
| 
 | 
   249  | 
  | 
| 
 | 
   250  | 
  | 
| 
 | 
   251  | 
definition m_o :: "nat \<Rightarrow> 'av st option \<Rightarrow> nat" where
  | 
| 
 | 
   252  | 
"m_o d opt = (case opt of None \<Rightarrow> h*d+1 | Some S \<Rightarrow> m_st S)"
  | 
| 
 | 
   253  | 
  | 
| 
 | 
   254  | 
definition m_c :: "'av st option acom \<Rightarrow> nat" where
  | 
| 
 | 
   255  | 
"m_c c = (\<Sum>i<size(annos c). m_o (card(vars(strip c))) (annos c ! i))"
  | 
| 
 | 
   256  | 
  | 
| 
 | 
   257  | 
lemma m_st_h: "wt x X \<Longrightarrow> finite X \<Longrightarrow> m_st x \<le> h * card X"
  | 
| 
 | 
   258  | 
by(simp add: wt_st_def m_st_def)
  | 
| 
 | 
   259  | 
  (metis nat_mult_commute of_nat_id setsum_bounded[OF h])
  | 
| 
 | 
   260  | 
  | 
| 
 | 
   261  | 
lemma m_o1: "finite X \<Longrightarrow> wt o1 X \<Longrightarrow> wt o2 X \<Longrightarrow>
  | 
| 
 | 
   262  | 
  o1 \<sqsubseteq> o2 \<Longrightarrow> m_o (card X) o1 \<ge> m_o (card X) o2"
  | 
| 
 | 
   263  | 
proof(induction o1 o2 rule: le_option.induct)
  | 
| 
 | 
   264  | 
  case 1 thus ?case by (simp add: m_o_def)(metis m_st1)
  | 
| 
 | 
   265  | 
next
  | 
| 
 | 
   266  | 
  case 2 thus ?case
  | 
| 
 | 
   267  | 
    by(simp add: wt_option_def m_o_def le_SucI m_st_h split: option.splits)
  | 
| 
 | 
   268  | 
next
  | 
| 
 | 
   269  | 
  case 3 thus ?case by simp
  | 
| 
 | 
   270  | 
qed
  | 
| 
 | 
   271  | 
  | 
| 
 | 
   272  | 
lemma m_o2: "finite X \<Longrightarrow> wt o1 X \<Longrightarrow> wt o2 X \<Longrightarrow>
  | 
| 
 | 
   273  | 
  o1 \<sqsubset> o2 \<Longrightarrow> m_o (card X) o1 > m_o (card X) o2"
  | 
| 
 | 
   274  | 
proof(induction o1 o2 rule: le_option.induct)
  | 
| 
 | 
   275  | 
  case 1 thus ?case by (simp add: m_o_def wt_st_def m_st2)
  | 
| 
 | 
   276  | 
next
  | 
| 
 | 
   277  | 
  case 2 thus ?case
  | 
| 
 | 
   278  | 
    by(auto simp add: m_o_def le_imp_less_Suc m_st_h)
  | 
| 
 | 
   279  | 
next
  | 
| 
 | 
   280  | 
  case 3 thus ?case by simp
  | 
| 
 | 
   281  | 
qed
  | 
| 
 | 
   282  | 
  | 
| 
 | 
   283  | 
lemma m_c2: "wt c1 (vars(strip c1)) \<Longrightarrow> wt c2 (vars(strip c2)) \<Longrightarrow>
  | 
| 
 | 
   284  | 
  c1 \<sqsubset> c2 \<Longrightarrow> m_c c1 > m_c c2"
  | 
| 
 | 
   285  | 
proof(auto simp add: le_iff_le_annos m_c_def size_annos_same[of c1 c2] wt_acom_def)
  | 
| 
 | 
   286  | 
  let ?X = "vars(strip c2)"
  | 
| 
 | 
   287  | 
  let ?n = "card ?X"
  | 
| 
 | 
   288  | 
  assume V1: "\<forall>a\<in>set(annos c1). wt a ?X"
  | 
| 
 | 
   289  | 
    and V2: "\<forall>a\<in>set(annos c2). wt a ?X"
  | 
| 
 | 
   290  | 
    and strip_eq: "strip c1 = strip c2"
  | 
| 
 | 
   291  | 
    and 0: "\<forall>i<size(annos c2). annos c1 ! i \<sqsubseteq> annos c2 ! i"
  | 
| 
 | 
   292  | 
  hence 1: "\<forall>i<size(annos c2). m_o ?n (annos c1 ! i) \<ge> m_o ?n (annos c2 ! i)"
  | 
| 
 | 
   293  | 
    by (auto simp: all_set_conv_all_nth)
  | 
| 
 | 
   294  | 
       (metis finite_cvars m_o1 size_annos_same2)
  | 
| 
 | 
   295  | 
  fix i assume "i < size(annos c2)" "\<not> annos c2 ! i \<sqsubseteq> annos c1 ! i"
  | 
| 
 | 
   296  | 
  hence "m_o ?n (annos c1 ! i) > m_o ?n (annos c2 ! i)" (is "?P i")
  | 
| 
 | 
   297  | 
    by(metis m_o2[OF finite_cvars] V1 V2 strip_eq nth_mem size_annos_same 0)
  | 
| 
 | 
   298  | 
  hence 2: "\<exists>i < size(annos c2). ?P i" using `i < size(annos c2)` by blast
  | 
| 
 | 
   299  | 
  show "(\<Sum>i<size(annos c2). m_o ?n (annos c2 ! i))
  | 
| 
 | 
   300  | 
         < (\<Sum>i<size(annos c2). m_o ?n (annos c1 ! i))"
  | 
| 
 | 
   301  | 
    apply(rule setsum_strict_mono_ex1) using 1 2 by (auto)
  | 
| 
 | 
   302  | 
qed
  | 
| 
 | 
   303  | 
  | 
| 
 | 
   304  | 
lemma AI_Some_measure: "\<exists>C. AI c = Some C"
  | 
| 
 | 
   305  | 
unfolding AI_def
  | 
| 
 | 
   306  | 
apply(rule lpfp_termination[where I = "%C. strip C = c \<and> wt C (vars c)"
  | 
| 
 | 
   307  | 
  and m="m_c"])
  | 
| 
 | 
   308  | 
apply(simp_all add: m_c2 mono_step'_top)
  | 
| 
 | 
   309  | 
done
  | 
| 
 | 
   310  | 
  | 
| 
 | 
   311  | 
end
  | 
| 
 | 
   312  | 
  | 
| 
 | 
   313  | 
end
  |