src/HOL/IMP/Abs_Int_ITP/Abs_Int1_ITP.thy
author haftmann
Sun, 10 Nov 2013 15:05:06 +0100
changeset 55747 45a5523d4a63
parent 54152 a1119cf551e8
child 58760 6ab1c7cb0b8d
permissions -rw-r--r--
qualifed popular user space names
nipkow@45963
     1
(* Author: Tobias Nipkow *)
nipkow@45963
     2
nipkow@48465
     3
theory Abs_Int1_ITP
nipkow@48465
     4
imports Abs_State_ITP
nipkow@45963
     5
begin
nipkow@45963
     6
nipkow@45963
     7
subsection "Computable Abstract Interpretation"
nipkow@45963
     8
nipkow@46494
     9
text{* Abstract interpretation over type @{text st} instead of
nipkow@45963
    10
functions. *}
nipkow@45963
    11
nipkow@47174
    12
context Gamma
nipkow@45963
    13
begin
nipkow@45963
    14
nipkow@46934
    15
fun aval' :: "aexp \<Rightarrow> 'av st \<Rightarrow> 'av" where
nipkow@46910
    16
"aval' (N n) S = num' n" |
nipkow@45963
    17
"aval' (V x) S = lookup S x" |
nipkow@45963
    18
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
nipkow@45963
    19
wenzelm@54152
    20
lemma aval'_sound: "s : \<gamma>\<^sub>f S \<Longrightarrow> aval a s : \<gamma>(aval' a S)"
nipkow@47162
    21
by (induction a) (auto simp: gamma_num' gamma_plus' \<gamma>_st_def lookup_def)
nipkow@46494
    22
nipkow@46494
    23
end
nipkow@46494
    24
nipkow@46934
    25
text{* The for-clause (here and elsewhere) only serves the purpose of fixing
nipkow@46934
    26
the name of the type parameter @{typ 'av} which would otherwise be renamed to
nipkow@46934
    27
@{typ 'a}. *}
nipkow@46934
    28
nipkow@47174
    29
locale Abs_Int = Gamma where \<gamma>=\<gamma> for \<gamma> :: "'av::SL_top \<Rightarrow> val set"
nipkow@46494
    30
begin
nipkow@46494
    31
nipkow@46934
    32
fun step' :: "'av st option \<Rightarrow> 'av st option acom \<Rightarrow> 'av st option acom" where
nipkow@46518
    33
"step' S (SKIP {P}) = (SKIP {S})" |
nipkow@46518
    34
"step' S (x ::= e {P}) =
nipkow@46494
    35
  x ::= e {case S of None \<Rightarrow> None | Some S \<Rightarrow> Some(update S x (aval' e S))}" |
nipkow@53183
    36
"step' S (c1;; c2) = step' S c1;; step' (post c1) c2" |
nipkow@46518
    37
"step' S (IF b THEN c1 ELSE c2 {P}) =
nipkow@46518
    38
  (let c1' = step' S c1; c2' = step' S c2
nipkow@45963
    39
   in IF b THEN c1' ELSE c2' {post c1 \<squnion> post c2})" |
nipkow@46518
    40
"step' S ({Inv} WHILE b DO c {P}) =
nipkow@46518
    41
   {S \<squnion> post c} WHILE b DO step' Inv c {Inv}"
nipkow@45963
    42
nipkow@46934
    43
definition AI :: "com \<Rightarrow> 'av st option acom option" where
wenzelm@54152
    44
"AI = lpfp\<^sub>c (step' \<top>)"
nipkow@45998
    45
nipkow@45998
    46
nipkow@46518
    47
lemma strip_step'[simp]: "strip(step' S c) = strip c"
nipkow@45963
    48
by(induct c arbitrary: S) (simp_all add: Let_def)
nipkow@45963
    49
nipkow@45963
    50
nipkow@45998
    51
text{* Soundness: *}
nipkow@45963
    52
nipkow@46910
    53
lemma in_gamma_update:
wenzelm@54152
    54
  "\<lbrakk> s : \<gamma>\<^sub>f S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^sub>f(update S x a)"
nipkow@46910
    55
by(simp add: \<gamma>_st_def lookup_update)
nipkow@45963
    56
nipkow@46494
    57
text{* The soundness proofs are textually identical to the ones for the step
nipkow@46494
    58
function operating on states as functions. *}
nipkow@46494
    59
nipkow@46939
    60
lemma step_preserves_le:
wenzelm@54152
    61
  "\<lbrakk> S \<subseteq> \<gamma>\<^sub>o S'; c \<le> \<gamma>\<^sub>c c' \<rbrakk> \<Longrightarrow> step S c \<le> \<gamma>\<^sub>c (step' S' c')"
nipkow@47162
    62
proof(induction c arbitrary: c' S S')
nipkow@46939
    63
  case SKIP thus ?case by(auto simp:SKIP_le map_acom_SKIP)
nipkow@45963
    64
next
nipkow@45963
    65
  case Assign thus ?case
nipkow@46939
    66
    by (fastforce simp: Assign_le  map_acom_Assign intro: aval'_sound in_gamma_update
nipkow@46494
    67
      split: option.splits del:subsetD)
nipkow@45963
    68
next
nipkow@48689
    69
  case Seq thus ?case apply (auto simp: Seq_le map_acom_Seq)
nipkow@46494
    70
    by (metis le_post post_map_acom)
nipkow@45963
    71
next
nipkow@47162
    72
  case (If b c1 c2 P)
nipkow@47162
    73
  then obtain c1' c2' P' where
nipkow@47162
    74
      "c' = IF b THEN c1' ELSE c2' {P'}"
wenzelm@54152
    75
      "P \<subseteq> \<gamma>\<^sub>o P'" "c1 \<le> \<gamma>\<^sub>c c1'" "c2 \<le> \<gamma>\<^sub>c c2'"
nipkow@46939
    76
    by (fastforce simp: If_le map_acom_If)
wenzelm@54152
    77
  moreover have "post c1 \<subseteq> \<gamma>\<^sub>o(post c1' \<squnion> post c2')"
wenzelm@54152
    78
    by (metis (no_types) `c1 \<le> \<gamma>\<^sub>c c1'` join_ge1 le_post mono_gamma_o order_trans post_map_acom)
wenzelm@54152
    79
  moreover have "post c2 \<subseteq> \<gamma>\<^sub>o(post c1' \<squnion> post c2')"
wenzelm@54152
    80
    by (metis (no_types) `c2 \<le> \<gamma>\<^sub>c c2'` join_ge2 le_post mono_gamma_o order_trans post_map_acom)
wenzelm@54152
    81
  ultimately show ?case using `S \<subseteq> \<gamma>\<^sub>o S'` by (simp add: If.IH subset_iff)
nipkow@45963
    82
next
nipkow@47162
    83
  case (While I b c1 P)
nipkow@47162
    84
  then obtain c1' I' P' where
nipkow@47162
    85
    "c' = {I'} WHILE b DO c1' {P'}"
wenzelm@54152
    86
    "I \<subseteq> \<gamma>\<^sub>o I'" "P \<subseteq> \<gamma>\<^sub>o P'" "c1 \<le> \<gamma>\<^sub>c c1'"
nipkow@46939
    87
    by (fastforce simp: map_acom_While While_le)
wenzelm@54152
    88
  moreover have "S \<union> post c1 \<subseteq> \<gamma>\<^sub>o (S' \<squnion> post c1')"
wenzelm@54152
    89
    using `S \<subseteq> \<gamma>\<^sub>o S'` le_post[OF `c1 \<le> \<gamma>\<^sub>c c1'`, simplified]
nipkow@46910
    90
    by (metis (no_types) join_ge1 join_ge2 le_sup_iff mono_gamma_o order_trans)
nipkow@46494
    91
  ultimately show ?case by (simp add: While.IH subset_iff)
nipkow@45963
    92
qed
nipkow@45963
    93
wenzelm@54152
    94
lemma AI_sound: "AI c = Some c' \<Longrightarrow> CS c \<le> \<gamma>\<^sub>c c'"
nipkow@46494
    95
proof(simp add: CS_def AI_def)
wenzelm@54152
    96
  assume 1: "lpfp\<^sub>c (step' \<top>) c = Some c'"
nipkow@46518
    97
  have 2: "step' \<top> c' \<sqsubseteq> c'" by(rule lpfpc_pfp[OF 1])
wenzelm@54152
    98
  have 3: "strip (\<gamma>\<^sub>c (step' \<top> c')) = c"
nipkow@46494
    99
    by(simp add: strip_lpfpc[OF _ 1])
wenzelm@54152
   100
  have "lfp (step UNIV) c \<le> \<gamma>\<^sub>c (step' \<top> c')"
nipkow@46770
   101
  proof(rule lfp_lowerbound[simplified,OF 3])
wenzelm@54152
   102
    show "step UNIV (\<gamma>\<^sub>c (step' \<top> c')) \<le> \<gamma>\<^sub>c (step' \<top> c')"
nipkow@46939
   103
    proof(rule step_preserves_le[OF _ _])
wenzelm@54152
   104
      show "UNIV \<subseteq> \<gamma>\<^sub>o \<top>" by simp
wenzelm@54152
   105
      show "\<gamma>\<^sub>c (step' \<top> c') \<le> \<gamma>\<^sub>c c'" by(rule mono_gamma_c[OF 2])
nipkow@46494
   106
    qed
nipkow@46494
   107
  qed
wenzelm@54152
   108
  from this 2 show "lfp (step UNIV) c \<le> \<gamma>\<^sub>c c'"
nipkow@46910
   109
    by (blast intro: mono_gamma_c order_trans)
nipkow@46494
   110
qed
nipkow@45963
   111
nipkow@45963
   112
end
nipkow@45963
   113
nipkow@45963
   114
nipkow@45998
   115
subsubsection "Monotonicity"
nipkow@45998
   116
nipkow@45998
   117
locale Abs_Int_mono = Abs_Int +
nipkow@45998
   118
assumes mono_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> plus' a1 a2 \<sqsubseteq> plus' b1 b2"
nipkow@45998
   119
begin
nipkow@45998
   120
nipkow@45998
   121
lemma mono_aval': "S \<sqsubseteq> S' \<Longrightarrow> aval' e S \<sqsubseteq> aval' e S'"
nipkow@45998
   122
by(induction e) (auto simp: le_st_def lookup_def mono_plus')
nipkow@45998
   123
nipkow@45998
   124
lemma mono_update: "a \<sqsubseteq> a' \<Longrightarrow> S \<sqsubseteq> S' \<Longrightarrow> update S x a \<sqsubseteq> update S' x a'"
nipkow@45998
   125
by(auto simp add: le_st_def lookup_def update_def)
nipkow@45998
   126
nipkow@47024
   127
lemma mono_step': "S \<sqsubseteq> S' \<Longrightarrow> c \<sqsubseteq> c' \<Longrightarrow> step' S c \<sqsubseteq> step' S' c'"
nipkow@47024
   128
apply(induction c c' arbitrary: S S' rule: le_acom.induct)
nipkow@47024
   129
apply (auto simp: Let_def mono_update mono_aval' mono_post le_join_disj
nipkow@47024
   130
            split: option.split)
nipkow@45998
   131
done
nipkow@45998
   132
nipkow@45963
   133
end
nipkow@45998
   134
nipkow@47029
   135
nipkow@47029
   136
subsubsection "Ascending Chain Condition"
nipkow@47029
   137
nipkow@47029
   138
abbreviation "strict r == r \<inter> -(r^-1)"
nipkow@47029
   139
abbreviation "acc r == wf((strict r)^-1)"
nipkow@47029
   140
nipkow@47029
   141
lemma strict_inv_image: "strict(inv_image r f) = inv_image (strict r) f"
nipkow@47029
   142
by(auto simp: inv_image_def)
nipkow@47029
   143
nipkow@47029
   144
lemma acc_inv_image:
nipkow@47029
   145
  "acc r \<Longrightarrow> acc (inv_image r f)"
nipkow@47029
   146
by (metis converse_inv_image strict_inv_image wf_inv_image)
nipkow@47029
   147
nipkow@47029
   148
text{* ACC for option type: *}
nipkow@47029
   149
nipkow@47029
   150
lemma acc_option: assumes "acc {(x,y::'a::preord). x \<sqsubseteq> y}"
nipkow@47183
   151
shows "acc {(x,y::'a::preord option). x \<sqsubseteq> y}"
nipkow@47029
   152
proof(auto simp: wf_eq_minimal)
nipkow@47029
   153
  fix xo :: "'a option" and Qo assume "xo : Qo"
nipkow@47029
   154
  let ?Q = "{x. Some x \<in> Qo}"
nipkow@47029
   155
  show "\<exists>yo\<in>Qo. \<forall>zo. yo \<sqsubseteq> zo \<and> ~ zo \<sqsubseteq> yo \<longrightarrow> zo \<notin> Qo" (is "\<exists>zo\<in>Qo. ?P zo")
nipkow@47029
   156
  proof cases
nipkow@47029
   157
    assume "?Q = {}"
nipkow@47029
   158
    hence "?P None" by auto
nipkow@47029
   159
    moreover have "None \<in> Qo" using `?Q = {}` `xo : Qo`
nipkow@47029
   160
      by auto (metis not_Some_eq)
nipkow@47029
   161
    ultimately show ?thesis by blast
nipkow@47029
   162
  next
nipkow@47029
   163
    assume "?Q \<noteq> {}"
nipkow@47029
   164
    with assms show ?thesis
nipkow@47029
   165
      apply(auto simp: wf_eq_minimal)
nipkow@47029
   166
      apply(erule_tac x="?Q" in allE)
nipkow@47029
   167
      apply auto
nipkow@47029
   168
      apply(rule_tac x = "Some z" in bexI)
nipkow@47029
   169
      by auto
nipkow@47029
   170
  qed
nipkow@47029
   171
qed
nipkow@47029
   172
nipkow@47029
   173
text{* ACC for abstract states, via measure functions. *}
nipkow@47029
   174
nipkow@47029
   175
(*FIXME mv*)
nipkow@47029
   176
lemma setsum_strict_mono1:
nipkow@47029
   177
fixes f :: "'a \<Rightarrow> 'b::{comm_monoid_add, ordered_cancel_ab_semigroup_add}"
nipkow@47029
   178
assumes "finite A" and "ALL x:A. f x \<le> g x" and "EX a:A. f a < g a"
nipkow@47029
   179
shows "setsum f A < setsum g A"
nipkow@47029
   180
proof-
nipkow@47029
   181
  from assms(3) obtain a where a: "a:A" "f a < g a" by blast
nipkow@47029
   182
  have "setsum f A = setsum f ((A-{a}) \<union> {a})"
nipkow@47029
   183
    by(simp add:insert_absorb[OF `a:A`])
nipkow@47029
   184
  also have "\<dots> = setsum f (A-{a}) + setsum f {a}"
nipkow@47029
   185
    using `finite A` by(subst setsum_Un_disjoint) auto
nipkow@47029
   186
  also have "setsum f (A-{a}) \<le> setsum g (A-{a})"
nipkow@47029
   187
    by(rule setsum_mono)(simp add: assms(2))
nipkow@47029
   188
  also have "setsum f {a} < setsum g {a}" using a by simp
nipkow@47029
   189
  also have "setsum g (A - {a}) + setsum g {a} = setsum g((A-{a}) \<union> {a})"
nipkow@47029
   190
    using `finite A` by(subst setsum_Un_disjoint[symmetric]) auto
nipkow@47029
   191
  also have "\<dots> = setsum g A" by(simp add:insert_absorb[OF `a:A`])
nipkow@47029
   192
  finally show ?thesis by (metis add_right_mono add_strict_left_mono)
nipkow@47029
   193
qed
nipkow@47029
   194
nipkow@47029
   195
lemma measure_st: assumes "(strict{(x,y::'a::SL_top). x \<sqsubseteq> y})^-1 <= measure m"
nipkow@47183
   196
and "\<forall>x y::'a::SL_top. x \<sqsubseteq> y \<and> y \<sqsubseteq> x \<longrightarrow> m x = m y"
nipkow@47183
   197
shows "(strict{(S,S'::'a::SL_top st). S \<sqsubseteq> S'})^-1 \<subseteq>
nipkow@47029
   198
  measure(%fd. \<Sum>x| x\<in>set(dom fd) \<and> ~ \<top> \<sqsubseteq> fun fd x. m(fun fd x)+1)"
nipkow@47029
   199
proof-
nipkow@47029
   200
  { fix S S' :: "'a st" assume "S \<sqsubseteq> S'" "~ S' \<sqsubseteq> S"
nipkow@47029
   201
    let ?X = "set(dom S)" let ?Y = "set(dom S')"
nipkow@47029
   202
    let ?f = "fun S" let ?g = "fun S'"
nipkow@47029
   203
    let ?X' = "{x:?X. ~ \<top> \<sqsubseteq> ?f x}" let ?Y' = "{y:?Y. ~ \<top> \<sqsubseteq> ?g y}"
nipkow@47029
   204
    from `S \<sqsubseteq> S'` have "ALL y:?Y'\<inter>?X. ?f y \<sqsubseteq> ?g y"
nipkow@47029
   205
      by(auto simp: le_st_def lookup_def)
nipkow@47029
   206
    hence 1: "ALL y:?Y'\<inter>?X. m(?g y)+1 \<le> m(?f y)+1"
nipkow@47029
   207
      using assms(1,2) by(fastforce)
nipkow@47029
   208
    from `~ S' \<sqsubseteq> S` obtain u where u: "u : ?X" "~ lookup S' u \<sqsubseteq> ?f u"
nipkow@47029
   209
      by(auto simp: le_st_def)
nipkow@47029
   210
    hence "u : ?X'" by simp (metis preord_class.le_trans top)
nipkow@47029
   211
    have "?Y'-?X = {}" using `S \<sqsubseteq> S'` by(fastforce simp: le_st_def lookup_def)
nipkow@47029
   212
    have "?Y'\<inter>?X <= ?X'" apply auto
nipkow@47029
   213
      apply (metis `S \<sqsubseteq> S'` le_st_def lookup_def preord_class.le_trans)
nipkow@47029
   214
      done
nipkow@47029
   215
    have "(\<Sum>y\<in>?Y'. m(?g y)+1) = (\<Sum>y\<in>(?Y'-?X) \<union> (?Y'\<inter>?X). m(?g y)+1)"
nipkow@47029
   216
      by (metis Un_Diff_Int)
nipkow@47029
   217
    also have "\<dots> = (\<Sum>y\<in>?Y'\<inter>?X. m(?g y)+1)"
nipkow@47029
   218
      using `?Y'-?X = {}` by (metis Un_empty_left)
nipkow@47029
   219
    also have "\<dots> < (\<Sum>x\<in>?X'. m(?f x)+1)"
nipkow@47029
   220
    proof cases
nipkow@47029
   221
      assume "u \<in> ?Y'"
nipkow@47029
   222
      hence "m(?g u) < m(?f u)" using assms(1) `S \<sqsubseteq> S'` u
nipkow@47029
   223
        by (fastforce simp: le_st_def lookup_def)
nipkow@47029
   224
      have "(\<Sum>y\<in>?Y'\<inter>?X. m(?g y)+1) < (\<Sum>y\<in>?Y'\<inter>?X. m(?f y)+1)"
nipkow@47029
   225
        using `u:?X` `u:?Y'` `m(?g u) < m(?f u)`
nipkow@47029
   226
        by(fastforce intro!: setsum_strict_mono1[OF _ 1])
nipkow@47029
   227
      also have "\<dots> \<le> (\<Sum>y\<in>?X'. m(?f y)+1)"
nipkow@47029
   228
        by(simp add: setsum_mono3[OF _ `?Y'\<inter>?X <= ?X'`])
nipkow@47029
   229
      finally show ?thesis .
nipkow@47029
   230
    next
nipkow@47029
   231
      assume "u \<notin> ?Y'"
nipkow@47029
   232
      with `?Y'\<inter>?X <= ?X'` have "?Y'\<inter>?X - {u} <= ?X' - {u}" by blast
nipkow@47029
   233
      have "(\<Sum>y\<in>?Y'\<inter>?X. m(?g y)+1) = (\<Sum>y\<in>?Y'\<inter>?X - {u}. m(?g y)+1)"
nipkow@47029
   234
      proof-
nipkow@47029
   235
        have "?Y'\<inter>?X = ?Y'\<inter>?X - {u}" using `u \<notin> ?Y'` by auto
nipkow@47029
   236
        thus ?thesis by metis
nipkow@47029
   237
      qed
nipkow@47029
   238
      also have "\<dots> < (\<Sum>y\<in>?Y'\<inter>?X-{u}. m(?g y)+1) + (\<Sum>y\<in>{u}. m(?f y)+1)" by simp
nipkow@47029
   239
      also have "(\<Sum>y\<in>?Y'\<inter>?X-{u}. m(?g y)+1) \<le> (\<Sum>y\<in>?Y'\<inter>?X-{u}. m(?f y)+1)"
nipkow@47029
   240
        using 1 by(blast intro: setsum_mono)
nipkow@47029
   241
      also have "\<dots> \<le> (\<Sum>y\<in>?X'-{u}. m(?f y)+1)"
nipkow@47029
   242
        by(simp add: setsum_mono3[OF _ `?Y'\<inter>?X-{u} <= ?X'-{u}`])
nipkow@47029
   243
      also have "\<dots> + (\<Sum>y\<in>{u}. m(?f y)+1)= (\<Sum>y\<in>(?X'-{u}) \<union> {u}. m(?f y)+1)"
nipkow@47029
   244
        using `u:?X'` by(subst setsum_Un_disjoint[symmetric]) auto
nipkow@47029
   245
      also have "\<dots> = (\<Sum>x\<in>?X'. m(?f x)+1)"
nipkow@47029
   246
        using `u : ?X'` by(simp add:insert_absorb)
nipkow@47029
   247
      finally show ?thesis by (blast intro: add_right_mono)
nipkow@47029
   248
    qed
nipkow@47029
   249
    finally have "(\<Sum>y\<in>?Y'. m(?g y)+1) < (\<Sum>x\<in>?X'. m(?f x)+1)" .
nipkow@47029
   250
  } thus ?thesis by(auto simp add: measure_def inv_image_def)
nipkow@47029
   251
qed
nipkow@47029
   252
nipkow@47029
   253
text{* ACC for acom. First the ordering on acom is related to an ordering on
nipkow@47029
   254
lists of annotations. *}
nipkow@47029
   255
nipkow@47029
   256
(* FIXME mv and add [simp] *)
nipkow@47029
   257
lemma listrel_Cons_iff:
nipkow@47029
   258
  "(x#xs, y#ys) : listrel r \<longleftrightarrow> (x,y) \<in> r \<and> (xs,ys) \<in> listrel r"
nipkow@47029
   259
by (blast intro:listrel.Cons)
nipkow@47029
   260
nipkow@47029
   261
lemma listrel_app: "(xs1,ys1) : listrel r \<Longrightarrow> (xs2,ys2) : listrel r
nipkow@47029
   262
  \<Longrightarrow> (xs1@xs2, ys1@ys2) : listrel r"
nipkow@47029
   263
by(auto simp add: listrel_iff_zip)
nipkow@47029
   264
nipkow@47029
   265
lemma listrel_app_same_size: "size xs1 = size ys1 \<Longrightarrow> size xs2 = size ys2 \<Longrightarrow>
nipkow@47029
   266
  (xs1@xs2, ys1@ys2) : listrel r \<longleftrightarrow>
nipkow@47029
   267
  (xs1,ys1) : listrel r \<and> (xs2,ys2) : listrel r"
nipkow@47029
   268
by(auto simp add: listrel_iff_zip)
nipkow@47029
   269
nipkow@47029
   270
lemma listrel_converse: "listrel(r^-1) = (listrel r)^-1"
nipkow@47029
   271
proof-
nipkow@47029
   272
  { fix xs ys
nipkow@47029
   273
    have "(xs,ys) : listrel(r^-1) \<longleftrightarrow> (ys,xs) : listrel r"
nipkow@47029
   274
      apply(induct xs arbitrary: ys)
nipkow@47029
   275
      apply (fastforce simp: listrel.Nil)
nipkow@47029
   276
      apply (fastforce simp: listrel_Cons_iff)
nipkow@47029
   277
      done
nipkow@47029
   278
  } thus ?thesis by auto
nipkow@47029
   279
qed
nipkow@47029
   280
nipkow@47029
   281
(* It would be nice to get rid of refl & trans and build them into the proof *)
nipkow@47029
   282
lemma acc_listrel: fixes r :: "('a*'a)set" assumes "refl r" and "trans r"
nipkow@47029
   283
and "acc r" shows "acc (listrel r - {([],[])})"
nipkow@47029
   284
proof-
nipkow@47029
   285
  have refl: "!!x. (x,x) : r" using `refl r` unfolding refl_on_def by blast
nipkow@47029
   286
  have trans: "!!x y z. (x,y) : r \<Longrightarrow> (y,z) : r \<Longrightarrow> (x,z) : r"
nipkow@47029
   287
    using `trans r` unfolding trans_def by blast
nipkow@47029
   288
  from assms(3) obtain mx :: "'a set \<Rightarrow> 'a" where
nipkow@47029
   289
    mx: "!!S x. x:S \<Longrightarrow> mx S : S \<and> (\<forall>y. (mx S,y) : strict r \<longrightarrow> y \<notin> S)"
nipkow@47029
   290
    by(simp add: wf_eq_minimal) metis
nipkow@47029
   291
  let ?R = "listrel r - {([], [])}"
nipkow@47029
   292
  { fix Q and xs :: "'a list"
nipkow@47029
   293
    have "xs \<in> Q \<Longrightarrow> \<exists>ys. ys\<in>Q \<and> (\<forall>zs. (ys, zs) \<in> strict ?R \<longrightarrow> zs \<notin> Q)"
nipkow@47029
   294
      (is "_ \<Longrightarrow> \<exists>ys. ?P Q ys")
nipkow@47029
   295
    proof(induction xs arbitrary: Q rule: length_induct)
nipkow@47029
   296
      case (1 xs)
nipkow@47029
   297
      { have "!!ys Q. size ys < size xs \<Longrightarrow> ys : Q \<Longrightarrow> EX ms. ?P Q ms"
nipkow@47029
   298
          using "1.IH" by blast
nipkow@47029
   299
      } note IH = this
nipkow@47029
   300
      show ?case
nipkow@47029
   301
      proof(cases xs)
nipkow@47029
   302
        case Nil with `xs : Q` have "?P Q []" by auto
nipkow@47029
   303
        thus ?thesis by blast
nipkow@47029
   304
      next
nipkow@47029
   305
        case (Cons x ys)
nipkow@47029
   306
        let ?Q1 = "{a. \<exists>bs. size bs = size ys \<and> a#bs : Q}"
nipkow@47029
   307
        have "x : ?Q1" using `xs : Q` Cons by auto
nipkow@47029
   308
        from mx[OF this] obtain m1 where
nipkow@47029
   309
          1: "m1 \<in> ?Q1 \<and> (\<forall>y. (m1,y) \<in> strict r \<longrightarrow> y \<notin> ?Q1)" by blast
nipkow@47029
   310
        then obtain ms1 where "size ms1 = size ys" "m1#ms1 : Q" by blast+
nipkow@47029
   311
        hence "size ms1 < size xs" using Cons by auto
nipkow@47029
   312
        let ?Q2 = "{bs. \<exists>m1'. (m1',m1):r \<and> (m1,m1'):r \<and> m1'#bs : Q \<and> size bs = size ms1}"
nipkow@47029
   313
        have "ms1 : ?Q2" using `m1#ms1 : Q` by(blast intro: refl)
nipkow@47029
   314
        from IH[OF `size ms1 < size xs` this]
nipkow@47029
   315
        obtain ms where 2: "?P ?Q2 ms" by auto
nipkow@47029
   316
        then obtain m1' where m1': "(m1',m1) : r \<and> (m1,m1') : r \<and> m1'#ms : Q"
nipkow@47029
   317
          by blast
nipkow@47029
   318
        hence "\<forall>ab. (m1'#ms,ab) : strict ?R \<longrightarrow> ab \<notin> Q" using 1 2
nipkow@47029
   319
          apply (auto simp: listrel_Cons_iff)
nipkow@47029
   320
          apply (metis `length ms1 = length ys` listrel_eq_len trans)
nipkow@47029
   321
          by (metis `length ms1 = length ys` listrel_eq_len trans)
nipkow@47029
   322
        with m1' show ?thesis by blast
nipkow@47029
   323
      qed
nipkow@47029
   324
    qed
nipkow@47029
   325
  }
nipkow@47029
   326
  thus ?thesis unfolding wf_eq_minimal by (metis converse_iff)
nipkow@47029
   327
qed
nipkow@47029
   328
nipkow@47029
   329
nipkow@47029
   330
lemma le_iff_le_annos: "c1 \<sqsubseteq> c2 \<longleftrightarrow>
nipkow@47029
   331
 (annos c1, annos c2) : listrel{(x,y). x \<sqsubseteq> y} \<and> strip c1 = strip c2"
nipkow@47029
   332
apply(induct c1 c2 rule: le_acom.induct)
nipkow@47117
   333
apply (auto simp: listrel.Nil listrel_Cons_iff listrel_app size_annos_same2)
nipkow@47029
   334
apply (metis listrel_app_same_size size_annos_same)+
nipkow@47029
   335
done
nipkow@47029
   336
nipkow@47029
   337
lemma le_acom_subset_same_annos:
nipkow@47029
   338
 "(strict{(c,c'::'a::preord acom). c \<sqsubseteq> c'})^-1 \<subseteq>
nipkow@47029
   339
  (strict(inv_image (listrel{(a,a'::'a). a \<sqsubseteq> a'} - {([],[])}) annos))^-1"
nipkow@47029
   340
by(auto simp: le_iff_le_annos)
nipkow@47029
   341
nipkow@47029
   342
lemma acc_acom: "acc {(a,a'::'a::preord). a \<sqsubseteq> a'} \<Longrightarrow>
nipkow@47029
   343
  acc {(c,c'::'a acom). c \<sqsubseteq> c'}"
nipkow@47029
   344
apply(rule wf_subset[OF _ le_acom_subset_same_annos])
nipkow@47029
   345
apply(rule acc_inv_image[OF acc_listrel])
nipkow@47029
   346
apply(auto simp: refl_on_def trans_def intro: le_trans)
nipkow@47029
   347
done
nipkow@47029
   348
nipkow@47029
   349
text{* Termination of the fixed-point finders, assuming monotone functions: *}
nipkow@47029
   350
nipkow@47029
   351
lemma pfp_termination:
nipkow@47029
   352
fixes x0 :: "'a::preord"
nipkow@47029
   353
assumes mono: "\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" and "acc {(x::'a,y). x \<sqsubseteq> y}"
nipkow@47029
   354
and "x0 \<sqsubseteq> f x0" shows "EX x. pfp f x0 = Some x"
nipkow@47029
   355
proof(simp add: pfp_def, rule wf_while_option_Some[where P = "%x. x \<sqsubseteq> f x"])
nipkow@47029
   356
  show "wf {(x, s). (s \<sqsubseteq> f s \<and> \<not> f s \<sqsubseteq> s) \<and> x = f s}"
nipkow@47029
   357
    by(rule wf_subset[OF assms(2)]) auto
nipkow@47029
   358
next
nipkow@47029
   359
  show "x0 \<sqsubseteq> f x0" by(rule assms)
nipkow@47029
   360
next
nipkow@47029
   361
  fix x assume "x \<sqsubseteq> f x" thus "f x \<sqsubseteq> f(f x)" by(rule mono)
nipkow@47029
   362
qed
nipkow@47029
   363
nipkow@47029
   364
lemma lpfpc_termination:
nipkow@47029
   365
  fixes f :: "(('a::SL_top)option acom \<Rightarrow> 'a option acom)"
nipkow@47029
   366
  assumes "acc {(x::'a,y). x \<sqsubseteq> y}" and "\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y"
nipkow@47029
   367
  and "\<And>c. strip(f c) = strip c"
wenzelm@54152
   368
  shows "\<exists>c'. lpfp\<^sub>c f c = Some c'"
wenzelm@54152
   369
unfolding lpfp\<^sub>c_def
nipkow@47029
   370
apply(rule pfp_termination)
nipkow@47029
   371
  apply(erule assms(2))
nipkow@47029
   372
 apply(rule acc_acom[OF acc_option[OF assms(1)]])
nipkow@47029
   373
apply(simp add: bot_acom assms(3))
nipkow@47029
   374
done
nipkow@47029
   375
nipkow@47162
   376
context Abs_Int_mono
nipkow@47162
   377
begin
nipkow@47162
   378
nipkow@47162
   379
lemma AI_Some_measure:
nipkow@47162
   380
assumes "(strict{(x,y::'a). x \<sqsubseteq> y})^-1 <= measure m"
nipkow@47162
   381
and "\<forall>x y::'a. x \<sqsubseteq> y \<and> y \<sqsubseteq> x \<longrightarrow> m x = m y"
nipkow@47162
   382
shows "\<exists>c'. AI c = Some c'"
nipkow@47162
   383
unfolding AI_def
nipkow@47162
   384
apply(rule lpfpc_termination)
nipkow@47162
   385
apply(rule wf_subset[OF wf_measure measure_st[OF assms]])
nipkow@47162
   386
apply(erule mono_step'[OF le_refl])
nipkow@47162
   387
apply(rule strip_step')
nipkow@47162
   388
done
nipkow@47029
   389
nipkow@45998
   390
end
nipkow@47162
   391
nipkow@47162
   392
end