src/HOL/IMP/Abs_State.thy
author nipkow
Tue, 12 Feb 2013 11:54:29 +0100
changeset 52218 e7b54119c436
parent 52177 3371f5ee4ace
child 52496 00b45c7e831f
permissions -rw-r--r--
tuned top
nipkow@48490
     1
(* Author: Tobias Nipkow *)
nipkow@48490
     2
nipkow@48490
     3
theory Abs_State
nipkow@48490
     4
imports Abs_Int0
nipkow@48490
     5
begin
nipkow@48490
     6
nipkow@50411
     7
subsubsection "Set-based lattices"
nipkow@48490
     8
nipkow@48490
     9
instantiation com :: vars
nipkow@48490
    10
begin
nipkow@48490
    11
nipkow@48490
    12
fun vars_com :: "com \<Rightarrow> vname set" where
nipkow@48490
    13
"vars com.SKIP = {}" |
nipkow@48490
    14
"vars (x::=e) = {x} \<union> vars e" |
nipkow@48490
    15
"vars (c1;c2) = vars c1 \<union> vars c2" |
nipkow@48490
    16
"vars (IF b THEN c1 ELSE c2) = vars b \<union> vars c1 \<union> vars c2" |
nipkow@48490
    17
"vars (WHILE b DO c) = vars b \<union> vars c"
nipkow@48490
    18
nipkow@48490
    19
instance ..
nipkow@48490
    20
nipkow@48490
    21
end
nipkow@48490
    22
nipkow@48490
    23
nipkow@48490
    24
lemma finite_avars: "finite(vars(a::aexp))"
nipkow@48490
    25
by(induction a) simp_all
nipkow@48490
    26
nipkow@48490
    27
lemma finite_bvars: "finite(vars(b::bexp))"
nipkow@48490
    28
by(induction b) (simp_all add: finite_avars)
nipkow@48490
    29
nipkow@48490
    30
lemma finite_cvars: "finite(vars(c::com))"
nipkow@48490
    31
by(induction c) (simp_all add: finite_avars finite_bvars)
nipkow@48490
    32
nipkow@48490
    33
nipkow@50411
    34
class L =
nipkow@50411
    35
fixes L :: "vname set \<Rightarrow> 'a set"
nipkow@48490
    36
nipkow@48490
    37
nipkow@50411
    38
instantiation acom :: (L)L
nipkow@48490
    39
begin
nipkow@48490
    40
nipkow@50411
    41
definition L_acom where
nipkow@50411
    42
"L X = {C. vars(strip C) \<subseteq> X \<and> (\<forall>a \<in> set(annos C). a \<in> L X)}"
nipkow@48490
    43
nipkow@48490
    44
instance ..
nipkow@48490
    45
nipkow@48490
    46
end
nipkow@48490
    47
nipkow@48490
    48
nipkow@50411
    49
instantiation option :: (L)L
nipkow@48490
    50
begin
nipkow@48490
    51
nipkow@50411
    52
definition L_option where
nipkow@50411
    53
"L X = {opt. case opt of None \<Rightarrow> True | Some x \<Rightarrow> x \<in> L X}"
nipkow@48490
    54
nipkow@50411
    55
lemma L_option_simps[simp]: "None \<in> L X" "(Some x \<in> L X) = (x \<in> L X)"
nipkow@50411
    56
by(simp_all add: L_option_def)
nipkow@48490
    57
nipkow@48490
    58
instance ..
nipkow@48490
    59
nipkow@48490
    60
end
nipkow@48490
    61
nipkow@50411
    62
class semilatticeL = join + L +
nipkow@52218
    63
fixes top :: "vname set \<Rightarrow> 'a"
nipkow@50411
    64
assumes join_ge1 [simp]: "x \<in> L X \<Longrightarrow> y \<in> L X \<Longrightarrow> x \<sqsubseteq> x \<squnion> y"
nipkow@50411
    65
and join_ge2 [simp]: "x \<in> L X \<Longrightarrow> y \<in> L X \<Longrightarrow> y \<sqsubseteq> x \<squnion> y"
nipkow@48490
    66
and join_least[simp]: "x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<squnion> y \<sqsubseteq> z"
nipkow@52218
    67
and top[simp]: "x \<in> L X \<Longrightarrow> x \<sqsubseteq> top X"
nipkow@52218
    68
and top_in_L[simp]: "top X \<in> L X"
nipkow@50411
    69
and join_in_L[simp]: "x \<in> L X \<Longrightarrow> y \<in> L X \<Longrightarrow> x \<squnion> y \<in> L X"
nipkow@48490
    70
nipkow@50592
    71
notation (input) top ("\<top>\<^bsub>_\<^esub>")
nipkow@50592
    72
notation (latex output) top ("\<top>\<^bsub>\<^raw:\isa{>_\<^raw:}>\<^esub>")
nipkow@48490
    73
nipkow@50411
    74
instantiation option :: (semilatticeL)semilatticeL
nipkow@48490
    75
begin
nipkow@48490
    76
nipkow@48490
    77
definition top_option where "top c = Some(top c)"
nipkow@48490
    78
nipkow@48490
    79
instance proof
nipkow@48490
    80
  case goal1 thus ?case by(cases x, simp, cases y, simp_all)
nipkow@48490
    81
next
nipkow@48490
    82
  case goal2 thus ?case by(cases y, simp, cases x, simp_all)
nipkow@48490
    83
next
nipkow@48490
    84
  case goal3 thus ?case by(cases z, simp, cases y, simp, cases x, simp_all)
nipkow@48490
    85
next
nipkow@48490
    86
  case goal4 thus ?case by(cases x, simp_all add: top_option_def)
nipkow@48490
    87
next
nipkow@48490
    88
  case goal5 thus ?case by(simp add: top_option_def)
nipkow@48490
    89
next
nipkow@50411
    90
  case goal6 thus ?case by(simp add: L_option_def split: option.splits)
nipkow@48490
    91
qed
nipkow@48490
    92
nipkow@48490
    93
end
nipkow@48490
    94
nipkow@48490
    95
nipkow@48490
    96
subsection "Abstract State with Computable Ordering"
nipkow@48490
    97
nipkow@48490
    98
hide_type  st  --"to avoid long names"
nipkow@48490
    99
nipkow@48490
   100
text{* A concrete type of state with computable @{text"\<sqsubseteq>"}: *}
nipkow@48490
   101
nipkow@48490
   102
datatype 'a st = FunDom "vname \<Rightarrow> 'a" "vname set"
nipkow@48490
   103
nipkow@50359
   104
fun "fun" where "fun (FunDom f X) = f"
nipkow@50359
   105
fun dom where "dom (FunDom f X) = X"
nipkow@48490
   106
nipkow@48490
   107
definition "show_st S = (\<lambda>x. (x, fun S x)) ` dom S"
nipkow@48490
   108
nipkow@48490
   109
value [code] "show_st (FunDom (\<lambda>x. 1::int) {''a'',''b''})"
nipkow@48490
   110
nipkow@48490
   111
definition "show_acom = map_acom (Option.map show_st)"
nipkow@52218
   112
definition "show_acom_opt = Option.map show_acom"
nipkow@48490
   113
nipkow@48490
   114
definition "update F x y = FunDom ((fun F)(x:=y)) (dom F)"
nipkow@48490
   115
nipkow@48490
   116
lemma fun_update[simp]: "fun (update S x y) = (fun S)(x:=y)"
nipkow@48490
   117
by(rule ext)(auto simp: update_def)
nipkow@48490
   118
nipkow@48490
   119
lemma dom_update[simp]: "dom (update S x y) = dom S"
nipkow@48490
   120
by(simp add: update_def)
nipkow@48490
   121
nipkow@48490
   122
definition "\<gamma>_st \<gamma> F = {f. \<forall>x\<in>dom F. f x \<in> \<gamma>(fun F x)}"
nipkow@48490
   123
nipkow@48490
   124
nipkow@48490
   125
instantiation st :: (preord) preord
nipkow@48490
   126
begin
nipkow@48490
   127
nipkow@48490
   128
definition le_st :: "'a st \<Rightarrow> 'a st \<Rightarrow> bool" where
nipkow@48490
   129
"F \<sqsubseteq> G = (dom F = dom G \<and> (\<forall>x \<in> dom F. fun F x \<sqsubseteq> fun G x))"
nipkow@48490
   130
nipkow@48490
   131
instance
nipkow@48490
   132
proof
nipkow@48490
   133
  case goal2 thus ?case by(auto simp: le_st_def)(metis preord_class.le_trans)
nipkow@48490
   134
qed (auto simp: le_st_def)
nipkow@48490
   135
nipkow@48490
   136
end
nipkow@48490
   137
nipkow@48490
   138
instantiation st :: (join) join
nipkow@48490
   139
begin
nipkow@48490
   140
nipkow@48490
   141
definition join_st :: "'a st \<Rightarrow> 'a st \<Rightarrow> 'a st" where
nipkow@48490
   142
"F \<squnion> G = FunDom (\<lambda>x. fun F x \<squnion> fun G x) (dom F)"
nipkow@48490
   143
nipkow@48490
   144
instance ..
nipkow@48490
   145
nipkow@48490
   146
end
nipkow@48490
   147
nipkow@50411
   148
instantiation st :: (type) L
nipkow@48490
   149
begin
nipkow@48490
   150
nipkow@50411
   151
definition L_st :: "vname set \<Rightarrow> 'a st set" where
nipkow@50411
   152
"L X = {F. dom F = X}"
nipkow@48490
   153
nipkow@48490
   154
instance ..
nipkow@48490
   155
nipkow@48490
   156
end
nipkow@48490
   157
nipkow@50411
   158
instantiation st :: (semilattice) semilatticeL
nipkow@48490
   159
begin
nipkow@48490
   160
nipkow@52218
   161
definition top_st where "top X = FunDom (\<lambda>x. \<top>) X"
nipkow@48490
   162
nipkow@48490
   163
instance
nipkow@48490
   164
proof
nipkow@50411
   165
qed (auto simp: le_st_def join_st_def top_st_def L_st_def)
nipkow@48490
   166
nipkow@48490
   167
end
nipkow@48490
   168
nipkow@48490
   169
nipkow@50414
   170
text{* Trick to make code generator happy. *}
nipkow@50414
   171
lemma [code]: "L = (L :: _ \<Rightarrow> _ st set)"
nipkow@50414
   172
by(rule refl)
nipkow@50414
   173
(* L is not used in the code but is part of a type class that is used.
nipkow@50414
   174
   Hence the code generator wants to build a dictionary with L in it.
nipkow@50414
   175
   But L is not executable. This looping defn makes it look as if it were. *)
nipkow@50414
   176
nipkow@50414
   177
nipkow@48490
   178
lemma mono_fun: "F \<sqsubseteq> G \<Longrightarrow> x : dom F \<Longrightarrow> fun F x \<sqsubseteq> fun G x"
nipkow@48490
   179
by(auto simp: le_st_def)
nipkow@48490
   180
nipkow@48490
   181
lemma mono_update[simp]:
nipkow@48490
   182
  "a1 \<sqsubseteq> a2 \<Longrightarrow> S1 \<sqsubseteq> S2 \<Longrightarrow> update S1 x a1 \<sqsubseteq> update S2 x a2"
nipkow@48490
   183
by(auto simp add: le_st_def update_def)
nipkow@48490
   184
nipkow@48490
   185
nipkow@50411
   186
locale Gamma = Val_abs where \<gamma>=\<gamma> for \<gamma> :: "'av::semilattice \<Rightarrow> val set"
nipkow@48490
   187
begin
nipkow@48490
   188
nipkow@50512
   189
abbreviation \<gamma>\<^isub>s :: "'av st \<Rightarrow> state set"
nipkow@50512
   190
where "\<gamma>\<^isub>s == \<gamma>_st \<gamma>"
nipkow@48490
   191
nipkow@48490
   192
abbreviation \<gamma>\<^isub>o :: "'av st option \<Rightarrow> state set"
nipkow@50512
   193
where "\<gamma>\<^isub>o == \<gamma>_option \<gamma>\<^isub>s"
nipkow@48490
   194
nipkow@48490
   195
abbreviation \<gamma>\<^isub>c :: "'av st option acom \<Rightarrow> state set acom"
nipkow@48490
   196
where "\<gamma>\<^isub>c == map_acom \<gamma>\<^isub>o"
nipkow@48490
   197
nipkow@50512
   198
lemma gamma_s_Top[simp]: "\<gamma>\<^isub>s (top c) = UNIV"
nipkow@48490
   199
by(auto simp: top_st_def \<gamma>_st_def)
nipkow@48490
   200
nipkow@48490
   201
lemma gamma_o_Top[simp]: "\<gamma>\<^isub>o (top c) = UNIV"
nipkow@48490
   202
by (simp add: top_option_def)
nipkow@48490
   203
nipkow@50512
   204
lemma mono_gamma_s: "f \<sqsubseteq> g \<Longrightarrow> \<gamma>\<^isub>s f \<subseteq> \<gamma>\<^isub>s g"
nipkow@48490
   205
apply(simp add:\<gamma>_st_def subset_iff le_st_def split: if_splits)
nipkow@48490
   206
by (metis mono_gamma subsetD)
nipkow@48490
   207
nipkow@48490
   208
lemma mono_gamma_o:
nipkow@48490
   209
  "S1 \<sqsubseteq> S2 \<Longrightarrow> \<gamma>\<^isub>o S1 \<subseteq> \<gamma>\<^isub>o S2"
nipkow@50512
   210
by(induction S1 S2 rule: le_option.induct)(simp_all add: mono_gamma_s)
nipkow@48490
   211
nipkow@48490
   212
lemma mono_gamma_c: "C1 \<sqsubseteq> C2 \<Longrightarrow> \<gamma>\<^isub>c C1 \<le> \<gamma>\<^isub>c C2"
nipkow@48490
   213
by (induction C1 C2 rule: le_acom.induct) (simp_all add:mono_gamma_o)
nipkow@48490
   214
nipkow@48490
   215
lemma in_gamma_option_iff:
nipkow@48490
   216
  "x : \<gamma>_option r u \<longleftrightarrow> (\<exists>u'. u = Some u' \<and> x : r u')"
nipkow@48490
   217
by (cases u) auto
nipkow@48490
   218
nipkow@48490
   219
end
nipkow@48490
   220
nipkow@48490
   221
end