src/HOL/IMP/Abs_Int0.thy
author paulson <lp15@cam.ac.uk>
Mon May 23 15:33:24 2016 +0100 (2016-05-23)
changeset 63114 27afe7af7379
parent 61179 16775cad1a5c
child 63882 018998c00003
permissions -rw-r--r--
Lots of new material for multivariate analysis
nipkow@47613
     1
(* Author: Tobias Nipkow *)
nipkow@47613
     2
nipkow@47613
     3
theory Abs_Int0
nipkow@47613
     4
imports Abs_Int_init
nipkow@47613
     5
begin
nipkow@47613
     6
nipkow@47613
     7
subsection "Orderings"
nipkow@47613
     8
haftmann@52729
     9
text{* The basic type classes @{class order}, @{class semilattice_sup} and @{class order_top} are
nipkow@51625
    10
defined in @{theory Main}, more precisely in theories @{theory Orderings} and @{theory Lattices}.
nipkow@51625
    11
If you view this theory with jedit, just click on the names to get there. *}
nipkow@47613
    12
haftmann@52729
    13
class semilattice_sup_top = semilattice_sup + order_top
nipkow@47613
    14
nipkow@47613
    15
nipkow@51826
    16
instance "fun" :: (type, semilattice_sup_top) semilattice_sup_top ..
nipkow@47613
    17
nipkow@51359
    18
instantiation option :: (order)order
nipkow@47613
    19
begin
nipkow@47613
    20
nipkow@51359
    21
fun less_eq_option where
nipkow@51359
    22
"Some x \<le> Some y = (x \<le> y)" |
nipkow@51359
    23
"None \<le> y = True" |
nipkow@51359
    24
"Some _ \<le> None = False"
nipkow@47613
    25
nipkow@51359
    26
definition less_option where "x < (y::'a option) = (x \<le> y \<and> \<not> y \<le> x)"
nipkow@47613
    27
nipkow@51628
    28
lemma le_None[simp]: "(x \<le> None) = (x = None)"
nipkow@47613
    29
by (cases x) simp_all
nipkow@47613
    30
nipkow@51628
    31
lemma Some_le[simp]: "(Some x \<le> u) = (\<exists>y. u = Some y \<and> x \<le> y)"
nipkow@47613
    32
by (cases u) auto
nipkow@47613
    33
nipkow@61179
    34
instance
nipkow@61179
    35
proof (standard, goal_cases)
nipkow@61179
    36
  case 1 show ?case by(rule less_option_def)
nipkow@51359
    37
next
nipkow@61179
    38
  case (2 x) show ?case by(cases x, simp_all)
nipkow@47613
    39
next
nipkow@61179
    40
  case (3 x y z) thus ?case by(cases z, simp, cases y, simp, cases x, auto)
nipkow@51359
    41
next
nipkow@61179
    42
  case (4 x y) thus ?case by(cases y, simp, cases x, auto)
nipkow@47613
    43
qed
nipkow@47613
    44
nipkow@47613
    45
end
nipkow@47613
    46
nipkow@51389
    47
instantiation option :: (sup)sup
nipkow@47613
    48
begin
nipkow@47613
    49
nipkow@51389
    50
fun sup_option where
nipkow@47613
    51
"Some x \<squnion> Some y = Some(x \<squnion> y)" |
nipkow@47613
    52
"None \<squnion> y = y" |
nipkow@47613
    53
"x \<squnion> None = x"
nipkow@47613
    54
nipkow@51389
    55
lemma sup_None2[simp]: "x \<squnion> None = x"
nipkow@47613
    56
by (cases x) simp_all
nipkow@47613
    57
nipkow@47613
    58
instance ..
nipkow@47613
    59
nipkow@47613
    60
end
nipkow@47613
    61
nipkow@51826
    62
instantiation option :: (semilattice_sup_top)semilattice_sup_top
nipkow@47613
    63
begin
nipkow@47613
    64
nipkow@51359
    65
definition top_option where "\<top> = Some \<top>"
nipkow@47613
    66
nipkow@61179
    67
instance
nipkow@61179
    68
proof (standard, goal_cases)
nipkow@61179
    69
  case (4 a) show ?case by(cases a, simp_all add: top_option_def)
nipkow@47613
    70
next
nipkow@61179
    71
  case (1 x y) thus ?case by(cases x, simp, cases y, simp_all)
nipkow@47613
    72
next
nipkow@61179
    73
  case (2 x y) thus ?case by(cases y, simp, cases x, simp_all)
nipkow@47613
    74
next
nipkow@61179
    75
  case (3 x y z) thus ?case by(cases z, simp, cases y, simp, cases x, simp_all)
nipkow@47613
    76
qed
nipkow@47613
    77
nipkow@47613
    78
end
nipkow@47613
    79
nipkow@51390
    80
lemma [simp]: "(Some x < Some y) = (x < y)"
nipkow@51390
    81
by(auto simp: less_le)
nipkow@51390
    82
haftmann@52729
    83
instantiation option :: (order)order_bot
nipkow@47613
    84
begin
nipkow@47613
    85
nipkow@49396
    86
definition bot_option :: "'a option" where
nipkow@47613
    87
"\<bottom> = None"
nipkow@47613
    88
nipkow@47613
    89
instance
nipkow@61179
    90
proof (standard, goal_cases)
nipkow@61179
    91
  case 1 thus ?case by(auto simp: bot_option_def)
nipkow@47613
    92
qed
nipkow@47613
    93
nipkow@47613
    94
end
nipkow@47613
    95
nipkow@47613
    96
nipkow@47613
    97
definition bot :: "com \<Rightarrow> 'a option acom" where
nipkow@52019
    98
"bot c = annotate (\<lambda>p. None) c"
nipkow@47613
    99
nipkow@51359
   100
lemma bot_least: "strip C = c \<Longrightarrow> bot c \<le> C"
nipkow@52019
   101
by(auto simp: bot_def less_eq_acom_def)
nipkow@47613
   102
nipkow@47613
   103
lemma strip_bot[simp]: "strip(bot c) = c"
nipkow@47613
   104
by(simp add: bot_def)
nipkow@47613
   105
nipkow@47613
   106
nipkow@51722
   107
subsubsection "Pre-fixpoint iteration"
nipkow@47613
   108
nipkow@51359
   109
definition pfp :: "(('a::order) \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a option" where
nipkow@51359
   110
"pfp f = while_option (\<lambda>x. \<not> f x \<le> x) f"
nipkow@47613
   111
nipkow@51359
   112
lemma pfp_pfp: assumes "pfp f x0 = Some x" shows "f x \<le> x"
nipkow@47613
   113
using while_option_stop[OF assms[simplified pfp_def]] by simp
nipkow@47613
   114
nipkow@49464
   115
lemma while_least:
nipkow@51359
   116
fixes q :: "'a::order"
nipkow@51359
   117
assumes "\<forall>x\<in>L.\<forall>y\<in>L. x \<le> y \<longrightarrow> f x \<le> f y" and "\<forall>x. x \<in> L \<longrightarrow> f x \<in> L"
nipkow@51359
   118
and "\<forall>x \<in> L. b \<le> x" and "b \<in> L" and "f q \<le> q" and "q \<in> L"
nipkow@49464
   119
and "while_option P f b = Some p"
nipkow@51359
   120
shows "p \<le> q"
nipkow@49464
   121
using while_option_rule[OF _  assms(7)[unfolded pfp_def],
nipkow@51359
   122
                        where P = "%x. x \<in> L \<and> x \<le> q"]
nipkow@51359
   123
by (metis assms(1-6) order_trans)
nipkow@47613
   124
nipkow@51710
   125
lemma pfp_bot_least:
nipkow@51710
   126
assumes "\<forall>x\<in>{C. strip C = c}.\<forall>y\<in>{C. strip C = c}. x \<le> y \<longrightarrow> f x \<le> f y"
nipkow@51710
   127
and "\<forall>C. C \<in> {C. strip C = c} \<longrightarrow> f C \<in> {C. strip C = c}"
nipkow@51710
   128
and "f C' \<le> C'" "strip C' = c" "pfp f (bot c) = Some C"
nipkow@51710
   129
shows "C \<le> C'"
nipkow@51710
   130
by(rule while_least[OF assms(1,2) _ _ assms(3) _ assms(5)[unfolded pfp_def]])
nipkow@51710
   131
  (simp_all add: assms(4) bot_least)
nipkow@51710
   132
nipkow@49464
   133
lemma pfp_inv:
nipkow@49464
   134
  "pfp f x = Some y \<Longrightarrow> (\<And>x. P x \<Longrightarrow> P(f x)) \<Longrightarrow> P x \<Longrightarrow> P y"
nipkow@58955
   135
unfolding pfp_def by (blast intro: while_option_rule)
nipkow@47613
   136
nipkow@47613
   137
lemma strip_pfp:
nipkow@47613
   138
assumes "\<And>x. g(f x) = g x" and "pfp f x0 = Some x" shows "g x = g x0"
nipkow@49464
   139
using pfp_inv[OF assms(2), where P = "%x. g x = g x0"] assms(1) by simp
nipkow@47613
   140
nipkow@47613
   141
nipkow@47613
   142
subsection "Abstract Interpretation"
nipkow@47613
   143
nipkow@47613
   144
definition \<gamma>_fun :: "('a \<Rightarrow> 'b set) \<Rightarrow> ('c \<Rightarrow> 'a) \<Rightarrow> ('c \<Rightarrow> 'b)set" where
nipkow@47613
   145
"\<gamma>_fun \<gamma> F = {f. \<forall>x. f x \<in> \<gamma>(F x)}"
nipkow@47613
   146
nipkow@47613
   147
fun \<gamma>_option :: "('a \<Rightarrow> 'b set) \<Rightarrow> 'a option \<Rightarrow> 'b set" where
nipkow@47613
   148
"\<gamma>_option \<gamma> None = {}" |
nipkow@47613
   149
"\<gamma>_option \<gamma> (Some a) = \<gamma> a"
nipkow@47613
   150
nipkow@47613
   151
text{* The interface for abstract values: *}
nipkow@47613
   152
nipkow@52504
   153
locale Val_semilattice =
nipkow@51826
   154
fixes \<gamma> :: "'av::semilattice_sup_top \<Rightarrow> val set"
nipkow@51359
   155
  assumes mono_gamma: "a \<le> b \<Longrightarrow> \<gamma> a \<le> \<gamma> b"
nipkow@47613
   156
  and gamma_Top[simp]: "\<gamma> \<top> = UNIV"
nipkow@47613
   157
fixes num' :: "val \<Rightarrow> 'av"
nipkow@47613
   158
and plus' :: "'av \<Rightarrow> 'av \<Rightarrow> 'av"
nipkow@51036
   159
  assumes gamma_num': "i \<in> \<gamma>(num' i)"
nipkow@51036
   160
  and gamma_plus': "i1 \<in> \<gamma> a1 \<Longrightarrow> i2 \<in> \<gamma> a2 \<Longrightarrow> i1+i2 \<in> \<gamma>(plus' a1 a2)"
nipkow@47613
   161
nipkow@47613
   162
type_synonym 'av st = "(vname \<Rightarrow> 'av)"
nipkow@47613
   163
nipkow@51826
   164
text{* The for-clause (here and elsewhere) only serves the purpose of fixing
nipkow@51826
   165
the name of the type parameter @{typ 'av} which would otherwise be renamed to
nipkow@51826
   166
@{typ 'a}. *}
nipkow@51826
   167
nipkow@52504
   168
locale Abs_Int_fun = Val_semilattice where \<gamma>=\<gamma>
nipkow@51826
   169
  for \<gamma> :: "'av::semilattice_sup_top \<Rightarrow> val set"
nipkow@47613
   170
begin
nipkow@47613
   171
nipkow@47613
   172
fun aval' :: "aexp \<Rightarrow> 'av st \<Rightarrow> 'av" where
nipkow@50896
   173
"aval' (N i) S = num' i" |
nipkow@47613
   174
"aval' (V x) S = S x" |
nipkow@47613
   175
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
nipkow@47613
   176
nipkow@51807
   177
definition "asem x e S = (case S of None \<Rightarrow> None | Some S \<Rightarrow> Some(S(x := aval' e S)))"
nipkow@51694
   178
nipkow@51807
   179
definition "step' = Step asem (\<lambda>b S. S)"
nipkow@51389
   180
nipkow@51722
   181
lemma strip_step'[simp]: "strip(step' S C) = strip C"
nipkow@51722
   182
by(simp add: step'_def)
nipkow@51722
   183
nipkow@47613
   184
definition AI :: "com \<Rightarrow> 'av st option acom option" where
nipkow@49464
   185
"AI c = pfp (step' \<top>) (bot c)"
nipkow@47613
   186
nipkow@47613
   187
wenzelm@53015
   188
abbreviation \<gamma>\<^sub>s :: "'av st \<Rightarrow> state set"
wenzelm@53015
   189
where "\<gamma>\<^sub>s == \<gamma>_fun \<gamma>"
nipkow@47613
   190
wenzelm@53015
   191
abbreviation \<gamma>\<^sub>o :: "'av st option \<Rightarrow> state set"
wenzelm@53015
   192
where "\<gamma>\<^sub>o == \<gamma>_option \<gamma>\<^sub>s"
nipkow@47613
   193
wenzelm@53015
   194
abbreviation \<gamma>\<^sub>c :: "'av st option acom \<Rightarrow> state set acom"
wenzelm@53015
   195
where "\<gamma>\<^sub>c == map_acom \<gamma>\<^sub>o"
nipkow@47613
   196
wenzelm@53015
   197
lemma gamma_s_Top[simp]: "\<gamma>\<^sub>s \<top> = UNIV"
nipkow@51359
   198
by(simp add: top_fun_def \<gamma>_fun_def)
nipkow@47613
   199
wenzelm@53015
   200
lemma gamma_o_Top[simp]: "\<gamma>\<^sub>o \<top> = UNIV"
nipkow@51359
   201
by (simp add: top_option_def)
nipkow@47613
   202
wenzelm@53015
   203
lemma mono_gamma_s: "f1 \<le> f2 \<Longrightarrow> \<gamma>\<^sub>s f1 \<subseteq> \<gamma>\<^sub>s f2"
nipkow@47613
   204
by(auto simp: le_fun_def \<gamma>_fun_def dest: mono_gamma)
nipkow@47613
   205
nipkow@47613
   206
lemma mono_gamma_o:
wenzelm@53015
   207
  "S1 \<le> S2 \<Longrightarrow> \<gamma>\<^sub>o S1 \<subseteq> \<gamma>\<^sub>o S2"
nipkow@51359
   208
by(induction S1 S2 rule: less_eq_option.induct)(simp_all add: mono_gamma_s)
nipkow@47613
   209
wenzelm@53015
   210
lemma mono_gamma_c: "C1 \<le> C2 \<Longrightarrow> \<gamma>\<^sub>c C1 \<le> \<gamma>\<^sub>c C2"
nipkow@52019
   211
by (simp add: less_eq_acom_def mono_gamma_o size_annos anno_map_acom size_annos_same[of C1 C2])
nipkow@47613
   212
nipkow@51974
   213
text{* Correctness: *}
nipkow@47613
   214
wenzelm@53015
   215
lemma aval'_correct: "s : \<gamma>\<^sub>s S \<Longrightarrow> aval a s : \<gamma>(aval' a S)"
nipkow@47613
   216
by (induct a) (auto simp: gamma_num' gamma_plus' \<gamma>_fun_def)
nipkow@47613
   217
wenzelm@53015
   218
lemma in_gamma_update: "\<lbrakk> s : \<gamma>\<^sub>s S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^sub>s(S(x := a))"
nipkow@47613
   219
by(simp add: \<gamma>_fun_def)
nipkow@47613
   220
nipkow@51390
   221
lemma gamma_Step_subcomm:
wenzelm@53015
   222
  assumes "!!x e S. f1 x e (\<gamma>\<^sub>o S) \<subseteq> \<gamma>\<^sub>o (f2 x e S)"  "!!b S. g1 b (\<gamma>\<^sub>o S) \<subseteq> \<gamma>\<^sub>o (g2 b S)"
wenzelm@53015
   223
  shows "Step f1 g1 (\<gamma>\<^sub>o S) (\<gamma>\<^sub>c C) \<le> \<gamma>\<^sub>c (Step f2 g2 S C)"
nipkow@54944
   224
by (induction C arbitrary: S) (auto simp: mono_gamma_o assms)
nipkow@51390
   225
wenzelm@53015
   226
lemma step_step': "step (\<gamma>\<^sub>o S) (\<gamma>\<^sub>c C) \<le> \<gamma>\<^sub>c (step' S C)"
nipkow@51390
   227
unfolding step_def step'_def
nipkow@51694
   228
by(rule gamma_Step_subcomm)
nipkow@51974
   229
  (auto simp: aval'_correct in_gamma_update asem_def split: option.splits)
nipkow@47613
   230
wenzelm@53015
   231
lemma AI_correct: "AI c = Some C \<Longrightarrow> CS c \<le> \<gamma>\<^sub>c C"
nipkow@47613
   232
proof(simp add: CS_def AI_def)
nipkow@49464
   233
  assume 1: "pfp (step' \<top>) (bot c) = Some C"
nipkow@51359
   234
  have pfp': "step' \<top> C \<le> C" by(rule pfp_pfp[OF 1])
wenzelm@53015
   235
  have 2: "step (\<gamma>\<^sub>o \<top>) (\<gamma>\<^sub>c C) \<le> \<gamma>\<^sub>c C"  --"transfer the pfp'"
nipkow@50986
   236
  proof(rule order_trans)
wenzelm@53015
   237
    show "step (\<gamma>\<^sub>o \<top>) (\<gamma>\<^sub>c C) \<le> \<gamma>\<^sub>c (step' \<top> C)" by(rule step_step')
wenzelm@53015
   238
    show "... \<le> \<gamma>\<^sub>c C" by (metis mono_gamma_c[OF pfp'])
nipkow@47613
   239
  qed
wenzelm@53015
   240
  have 3: "strip (\<gamma>\<^sub>c C) = c" by(simp add: strip_pfp[OF _ 1] step'_def)
wenzelm@53015
   241
  have "lfp c (step (\<gamma>\<^sub>o \<top>)) \<le> \<gamma>\<^sub>c C"
wenzelm@53015
   242
    by(rule lfp_lowerbound[simplified,where f="step (\<gamma>\<^sub>o \<top>)", OF 3 2])
wenzelm@53015
   243
  thus "lfp c (step UNIV) \<le> \<gamma>\<^sub>c C" by simp
nipkow@47613
   244
qed
nipkow@47613
   245
nipkow@47613
   246
end
nipkow@47613
   247
nipkow@47613
   248
nipkow@47613
   249
subsubsection "Monotonicity"
nipkow@47613
   250
nipkow@51721
   251
locale Abs_Int_fun_mono = Abs_Int_fun +
nipkow@51359
   252
assumes mono_plus': "a1 \<le> b1 \<Longrightarrow> a2 \<le> b2 \<Longrightarrow> plus' a1 a2 \<le> plus' b1 b2"
nipkow@47613
   253
begin
nipkow@47613
   254
nipkow@51359
   255
lemma mono_aval': "S \<le> S' \<Longrightarrow> aval' e S \<le> aval' e S'"
nipkow@47613
   256
by(induction e)(auto simp: le_fun_def mono_plus')
nipkow@47613
   257
nipkow@51359
   258
lemma mono_update: "a \<le> a' \<Longrightarrow> S \<le> S' \<Longrightarrow> S(x := a) \<le> S'(x := a')"
nipkow@47613
   259
by(simp add: le_fun_def)
nipkow@47613
   260
nipkow@51359
   261
lemma mono_step': "S1 \<le> S2 \<Longrightarrow> C1 \<le> C2 \<Longrightarrow> step' S1 C1 \<le> step' S2 C2"
nipkow@51390
   262
unfolding step'_def
nipkow@51694
   263
by(rule mono2_Step)
nipkow@51807
   264
  (auto simp: mono_update mono_aval' asem_def split: option.split)
nipkow@47613
   265
nipkow@51722
   266
lemma mono_step'_top: "C \<le> C' \<Longrightarrow> step' \<top> C \<le> step' \<top> C'"
nipkow@51722
   267
by (metis mono_step' order_refl)
nipkow@51722
   268
nipkow@51722
   269
lemma AI_least_pfp: assumes "AI c = Some C" "step' \<top> C' \<le> C'" "strip C' = c"
nipkow@51722
   270
shows "C \<le> C'"
nipkow@51722
   271
by(rule pfp_bot_least[OF _ _ assms(2,3) assms(1)[unfolded AI_def]])
nipkow@51722
   272
  (simp_all add: mono_step'_top)
nipkow@51722
   273
nipkow@51722
   274
end
nipkow@51722
   275
nipkow@51722
   276
nipkow@51722
   277
instantiation acom :: (type) vars
nipkow@51722
   278
begin
nipkow@51722
   279
nipkow@51722
   280
definition "vars_acom = vars o strip"
nipkow@51722
   281
nipkow@51722
   282
instance ..
nipkow@51722
   283
nipkow@51722
   284
end
nipkow@51722
   285
nipkow@51722
   286
lemma finite_Cvars: "finite(vars(C::'a acom))"
nipkow@51722
   287
by(simp add: vars_acom_def)
nipkow@51722
   288
nipkow@51722
   289
nipkow@51722
   290
subsubsection "Termination"
nipkow@51722
   291
nipkow@51722
   292
lemma pfp_termination:
nipkow@51722
   293
fixes x0 :: "'a::order" and m :: "'a \<Rightarrow> nat"
nipkow@51722
   294
assumes mono: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
nipkow@51722
   295
and m: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x < y \<Longrightarrow> m x > m y"
nipkow@51722
   296
and I: "\<And>x y. I x \<Longrightarrow> I(f x)" and "I x0" and "x0 \<le> f x0"
nipkow@51722
   297
shows "\<exists>x. pfp f x0 = Some x"
nipkow@51722
   298
proof(simp add: pfp_def, rule wf_while_option_Some[where P = "%x. I x & x \<le> f x"])
nipkow@51722
   299
  show "wf {(y,x). ((I x \<and> x \<le> f x) \<and> \<not> f x \<le> x) \<and> y = f x}"
nipkow@51722
   300
    by(rule wf_subset[OF wf_measure[of m]]) (auto simp: m I)
nipkow@51722
   301
next
nipkow@51722
   302
  show "I x0 \<and> x0 \<le> f x0" using `I x0` `x0 \<le> f x0` by blast
nipkow@51722
   303
next
nipkow@51722
   304
  fix x assume "I x \<and> x \<le> f x" thus "I(f x) \<and> f x \<le> f(f x)"
nipkow@51722
   305
    by (blast intro: I mono)
nipkow@51722
   306
qed
nipkow@51722
   307
nipkow@52019
   308
lemma le_iff_le_annos: "C1 \<le> C2 \<longleftrightarrow>
nipkow@52019
   309
  strip C1 = strip C2 \<and> (\<forall> i<size(annos C1). annos C1 ! i \<le> annos C2 ! i)"
nipkow@52019
   310
by(simp add: less_eq_acom_def anno_def)
nipkow@51722
   311
nipkow@51722
   312
locale Measure1_fun =
nipkow@51749
   313
fixes m :: "'av::top \<Rightarrow> nat"
nipkow@51722
   314
fixes h :: "nat"
nipkow@51722
   315
assumes h: "m x \<le> h"
nipkow@51722
   316
begin
nipkow@51722
   317
wenzelm@53015
   318
definition m_s :: "'av st \<Rightarrow> vname set \<Rightarrow> nat" ("m\<^sub>s") where
nipkow@51791
   319
"m_s S X = (\<Sum> x \<in> X. m(S x))"
nipkow@51722
   320
nipkow@51791
   321
lemma m_s_h: "finite X \<Longrightarrow> m_s S X \<le> h * card X"
lp15@60974
   322
by(simp add: m_s_def) (metis mult.commute of_nat_id setsum_bounded_above[OF h])
nipkow@51722
   323
wenzelm@53015
   324
fun m_o :: "'av st option \<Rightarrow> vname set \<Rightarrow> nat" ("m\<^sub>o") where
nipkow@51791
   325
"m_o (Some S) X = m_s S X" |
nipkow@51791
   326
"m_o None X = h * card X + 1"
nipkow@51722
   327
nipkow@51791
   328
lemma m_o_h: "finite X \<Longrightarrow> m_o opt X \<le> (h*card X + 1)"
nipkow@51749
   329
by(cases opt)(auto simp add: m_s_h le_SucI dest: m_s_h)
nipkow@51722
   330
wenzelm@53015
   331
definition m_c :: "'av st option acom \<Rightarrow> nat" ("m\<^sub>c") where
nipkow@51791
   332
"m_c C = listsum (map (\<lambda>a. m_o a (vars C)) (annos C))"
nipkow@51722
   333
nipkow@51722
   334
text{* Upper complexity bound: *}
nipkow@51722
   335
lemma m_c_h: "m_c C \<le> size(annos C) * (h * card(vars C) + 1)"
nipkow@51722
   336
proof-
nipkow@51722
   337
  let ?X = "vars C" let ?n = "card ?X" let ?a = "size(annos C)"
nipkow@51791
   338
  have "m_c C = (\<Sum>i<?a. m_o (annos C ! i) ?X)"
nipkow@51783
   339
    by(simp add: m_c_def listsum_setsum_nth atLeast0LessThan)
nipkow@51722
   340
  also have "\<dots> \<le> (\<Sum>i<?a. h * ?n + 1)"
nipkow@51722
   341
    apply(rule setsum_mono) using m_o_h[OF finite_Cvars] by simp
nipkow@51722
   342
  also have "\<dots> = ?a * (h * ?n + 1)" by simp
nipkow@51722
   343
  finally show ?thesis .
nipkow@51722
   344
qed
nipkow@51722
   345
nipkow@51722
   346
end
nipkow@51722
   347
nipkow@51722
   348
nipkow@51826
   349
locale Measure_fun = Measure1_fun where m=m
nipkow@51826
   350
  for m :: "'av::semilattice_sup_top \<Rightarrow> nat" +
nipkow@51749
   351
assumes m2: "x < y \<Longrightarrow> m x > m y"
nipkow@51749
   352
begin
nipkow@51749
   353
nipkow@51785
   354
text{* The predicates @{text "top_on_ty a X"} that follow describe that any abstract
nipkow@51749
   355
state in @{text a} maps all variables in @{text X} to @{term \<top>}.
nipkow@51722
   356
This is an important invariant for the termination proof where we argue that only
nipkow@51722
   357
the finitely many variables in the program change. That the others do not change
nipkow@51722
   358
follows because they remain @{term \<top>}. *}
nipkow@51722
   359
wenzelm@53015
   360
fun top_on_st :: "'av st \<Rightarrow> vname set \<Rightarrow> bool" ("top'_on\<^sub>s") where
nipkow@51785
   361
"top_on_st S X = (\<forall>x\<in>X. S x = \<top>)"
nipkow@51722
   362
wenzelm@53015
   363
fun top_on_opt :: "'av st option \<Rightarrow> vname set \<Rightarrow> bool" ("top'_on\<^sub>o") where
nipkow@51785
   364
"top_on_opt (Some S) X = top_on_st S X" |
nipkow@51785
   365
"top_on_opt None X = True"
nipkow@51722
   366
wenzelm@53015
   367
definition top_on_acom :: "'av st option acom \<Rightarrow> vname set \<Rightarrow> bool" ("top'_on\<^sub>c") where
nipkow@51785
   368
"top_on_acom C X = (\<forall>a \<in> set(annos C). top_on_opt a X)"
nipkow@51722
   369
nipkow@51785
   370
lemma top_on_top: "top_on_opt \<top> X"
nipkow@51722
   371
by(auto simp: top_option_def)
nipkow@51722
   372
nipkow@51785
   373
lemma top_on_bot: "top_on_acom (bot c) X"
nipkow@51722
   374
by(auto simp add: top_on_acom_def bot_def)
nipkow@51722
   375
nipkow@51785
   376
lemma top_on_post: "top_on_acom C X \<Longrightarrow> top_on_opt (post C) X"
nipkow@51722
   377
by(simp add: top_on_acom_def post_in_annos)
nipkow@51722
   378
nipkow@51722
   379
lemma top_on_acom_simps:
nipkow@51785
   380
  "top_on_acom (SKIP {Q}) X = top_on_opt Q X"
nipkow@51785
   381
  "top_on_acom (x ::= e {Q}) X = top_on_opt Q X"
nipkow@52046
   382
  "top_on_acom (C1;;C2) X = (top_on_acom C1 X \<and> top_on_acom C2 X)"
nipkow@51785
   383
  "top_on_acom (IF b THEN {P1} C1 ELSE {P2} C2 {Q}) X =
nipkow@51785
   384
   (top_on_opt P1 X \<and> top_on_acom C1 X \<and> top_on_opt P2 X \<and> top_on_acom C2 X \<and> top_on_opt Q X)"
nipkow@51785
   385
  "top_on_acom ({I} WHILE b DO {P} C {Q}) X =
nipkow@51785
   386
   (top_on_opt I X \<and> top_on_acom C X \<and> top_on_opt P X \<and> top_on_opt Q X)"
nipkow@51722
   387
by(auto simp add: top_on_acom_def)
nipkow@51722
   388
nipkow@51722
   389
lemma top_on_sup:
nipkow@51785
   390
  "top_on_opt o1 X \<Longrightarrow> top_on_opt o2 X \<Longrightarrow> top_on_opt (o1 \<squnion> o2) X"
nipkow@51722
   391
apply(induction o1 o2 rule: sup_option.induct)
nipkow@51722
   392
apply(auto)
nipkow@51722
   393
done
nipkow@51722
   394
nipkow@51749
   395
lemma top_on_Step: fixes C :: "'av st option acom"
nipkow@51785
   396
assumes "!!x e S. \<lbrakk>top_on_opt S X; x \<notin> X; vars e \<subseteq> -X\<rbrakk> \<Longrightarrow> top_on_opt (f x e S) X"
nipkow@51785
   397
        "!!b S. top_on_opt S X \<Longrightarrow> vars b \<subseteq> -X \<Longrightarrow> top_on_opt (g b S) X"
nipkow@51785
   398
shows "\<lbrakk> vars C \<subseteq> -X; top_on_opt S X; top_on_acom C X \<rbrakk> \<Longrightarrow> top_on_acom (Step f g S C) X"
nipkow@51722
   399
proof(induction C arbitrary: S)
nipkow@51722
   400
qed (auto simp: top_on_acom_simps vars_acom_def top_on_post top_on_sup assms)
nipkow@51722
   401
nipkow@51722
   402
lemma m1: "x \<le> y \<Longrightarrow> m x \<ge> m y"
nipkow@51722
   403
by(auto simp: le_less m2)
nipkow@51722
   404
nipkow@51722
   405
lemma m_s2_rep: assumes "finite(X)" and "S1 = S2 on -X" and "\<forall>x. S1 x \<le> S2 x" and "S1 \<noteq> S2"
nipkow@51722
   406
shows "(\<Sum>x\<in>X. m (S2 x)) < (\<Sum>x\<in>X. m (S1 x))"
nipkow@51722
   407
proof-
nipkow@51722
   408
  from assms(3) have 1: "\<forall>x\<in>X. m(S1 x) \<ge> m(S2 x)" by (simp add: m1)
nipkow@51722
   409
  from assms(2,3,4) have "EX x:X. S1 x < S2 x"
nipkow@51722
   410
    by(simp add: fun_eq_iff) (metis Compl_iff le_neq_trans)
nipkow@51722
   411
  hence 2: "\<exists>x\<in>X. m(S1 x) > m(S2 x)" by (metis m2)
nipkow@51722
   412
  from setsum_strict_mono_ex1[OF `finite X` 1 2]
nipkow@51722
   413
  show "(\<Sum>x\<in>X. m (S2 x)) < (\<Sum>x\<in>X. m (S1 x))" .
nipkow@51722
   414
qed
nipkow@51722
   415
nipkow@51791
   416
lemma m_s2: "finite(X) \<Longrightarrow> S1 = S2 on -X \<Longrightarrow> S1 < S2 \<Longrightarrow> m_s S1 X > m_s S2 X"
nipkow@51722
   417
apply(auto simp add: less_fun_def m_s_def)
nipkow@51722
   418
apply(simp add: m_s2_rep le_fun_def)
nipkow@51722
   419
done
nipkow@51722
   420
nipkow@51785
   421
lemma m_o2: "finite X \<Longrightarrow> top_on_opt o1 (-X) \<Longrightarrow> top_on_opt o2 (-X) \<Longrightarrow>
nipkow@51791
   422
  o1 < o2 \<Longrightarrow> m_o o1 X > m_o o2 X"
nipkow@51722
   423
proof(induction o1 o2 rule: less_eq_option.induct)
nipkow@51749
   424
  case 1 thus ?case by (auto simp: m_s2 less_option_def)
nipkow@51722
   425
next
nipkow@51749
   426
  case 2 thus ?case by(auto simp: less_option_def le_imp_less_Suc m_s_h)
nipkow@51722
   427
next
nipkow@51722
   428
  case 3 thus ?case by (auto simp: less_option_def)
nipkow@51722
   429
qed
nipkow@51722
   430
nipkow@51785
   431
lemma m_o1: "finite X \<Longrightarrow> top_on_opt o1 (-X) \<Longrightarrow> top_on_opt o2 (-X) \<Longrightarrow>
nipkow@51791
   432
  o1 \<le> o2 \<Longrightarrow> m_o o1 X \<ge> m_o o2 X"
nipkow@51722
   433
by(auto simp: le_less m_o2)
nipkow@51722
   434
nipkow@51722
   435
nipkow@51785
   436
lemma m_c2: "top_on_acom C1 (-vars C1) \<Longrightarrow> top_on_acom C2 (-vars C2) \<Longrightarrow>
nipkow@51722
   437
  C1 < C2 \<Longrightarrow> m_c C1 > m_c C2"
nipkow@51783
   438
proof(auto simp add: le_iff_le_annos size_annos_same[of C1 C2] vars_acom_def less_acom_def)
nipkow@51722
   439
  let ?X = "vars(strip C2)"
nipkow@51785
   440
  assume top: "top_on_acom C1 (- vars(strip C2))"  "top_on_acom C2 (- vars(strip C2))"
nipkow@51722
   441
  and strip_eq: "strip C1 = strip C2"
nipkow@51722
   442
  and 0: "\<forall>i<size(annos C2). annos C1 ! i \<le> annos C2 ! i"
nipkow@51791
   443
  hence 1: "\<forall>i<size(annos C2). m_o (annos C1 ! i) ?X \<ge> m_o (annos C2 ! i) ?X"
nipkow@51722
   444
    apply (auto simp: all_set_conv_all_nth vars_acom_def top_on_acom_def)
nipkow@51722
   445
    by (metis (lifting, no_types) finite_cvars m_o1 size_annos_same2)
nipkow@51722
   446
  fix i assume i: "i < size(annos C2)" "\<not> annos C2 ! i \<le> annos C1 ! i"
nipkow@51785
   447
  have topo1: "top_on_opt (annos C1 ! i) (- ?X)"
nipkow@51722
   448
    using i(1) top(1) by(simp add: top_on_acom_def size_annos_same[OF strip_eq])
nipkow@51785
   449
  have topo2: "top_on_opt (annos C2 ! i) (- ?X)"
nipkow@51722
   450
    using i(1) top(2) by(simp add: top_on_acom_def size_annos_same[OF strip_eq])
nipkow@51791
   451
  from i have "m_o (annos C1 ! i) ?X > m_o (annos C2 ! i) ?X" (is "?P i")
nipkow@51722
   452
    by (metis 0 less_option_def m_o2[OF finite_cvars topo1] topo2)
nipkow@51722
   453
  hence 2: "\<exists>i < size(annos C2). ?P i" using `i < size(annos C2)` by blast
nipkow@51791
   454
  have "(\<Sum>i<size(annos C2). m_o (annos C2 ! i) ?X)
nipkow@51791
   455
         < (\<Sum>i<size(annos C2). m_o (annos C1 ! i) ?X)"
nipkow@51722
   456
    apply(rule setsum_strict_mono_ex1) using 1 2 by (auto)
nipkow@51783
   457
  thus ?thesis
nipkow@51783
   458
    by(simp add: m_c_def vars_acom_def strip_eq listsum_setsum_nth atLeast0LessThan size_annos_same[OF strip_eq])
nipkow@51722
   459
qed
nipkow@51722
   460
nipkow@51722
   461
end
nipkow@51722
   462
nipkow@51722
   463
nipkow@51722
   464
locale Abs_Int_fun_measure =
nipkow@51722
   465
  Abs_Int_fun_mono where \<gamma>=\<gamma> + Measure_fun where m=m
nipkow@51826
   466
  for \<gamma> :: "'av::semilattice_sup_top \<Rightarrow> val set" and m :: "'av \<Rightarrow> nat"
nipkow@51722
   467
begin
nipkow@51722
   468
nipkow@51785
   469
lemma top_on_step': "top_on_acom C (-vars C) \<Longrightarrow> top_on_acom (step' \<top> C) (-vars C)"
nipkow@51722
   470
unfolding step'_def
nipkow@51722
   471
by(rule top_on_Step)
nipkow@51807
   472
  (auto simp add: top_option_def asem_def split: option.splits)
nipkow@51722
   473
nipkow@51722
   474
lemma AI_Some_measure: "\<exists>C. AI c = Some C"
nipkow@51722
   475
unfolding AI_def
nipkow@51785
   476
apply(rule pfp_termination[where I = "\<lambda>C. top_on_acom C (- vars C)" and m="m_c"])
nipkow@51722
   477
apply(simp_all add: m_c2 mono_step'_top bot_least top_on_bot)
nipkow@51754
   478
using top_on_step' apply(auto simp add: vars_acom_def)
nipkow@51722
   479
done
nipkow@51722
   480
nipkow@47613
   481
end
nipkow@47613
   482
nipkow@47613
   483
text{* Problem: not executable because of the comparison of abstract states,
nipkow@52022
   484
i.e. functions, in the pre-fixpoint computation. *}
nipkow@47613
   485
nipkow@47613
   486
end