src/HOL/IMP/Abs_Int0.thy
author nipkow
Wed, 28 Sep 2011 09:55:11 +0200
changeset 45963 054a9ac0d7ef
child 45998 d2eb07a1e01b
permissions -rw-r--r--
Added Hoare-like Abstract Interpretation
nipkow@45963
     1
(* Author: Tobias Nipkow *)
nipkow@45963
     2
nipkow@45963
     3
theory Abs_Int0
nipkow@45963
     4
imports Abs_State
nipkow@45963
     5
begin
nipkow@45963
     6
nipkow@45963
     7
subsection "Computable Abstract Interpretation"
nipkow@45963
     8
nipkow@45963
     9
text{* Abstract interpretation over type @{text astate} instead of
nipkow@45963
    10
functions. *}
nipkow@45963
    11
nipkow@45963
    12
locale Abs_Int = Val_abs +
nipkow@45963
    13
fixes pfp :: "('a st up acom \<Rightarrow> 'a st up acom) \<Rightarrow> 'a st up acom \<Rightarrow> 'a st up acom"
nipkow@45963
    14
assumes pfp: "\<forall>c. strip(f c) = strip c \<Longrightarrow> f(pfp f c) \<sqsubseteq> pfp f c"
nipkow@45963
    15
and strip_pfp: "\<forall>c. strip(f c) = strip c \<Longrightarrow> strip(pfp f c) = strip c"
nipkow@45963
    16
begin
nipkow@45963
    17
nipkow@45963
    18
fun aval' :: "aexp \<Rightarrow> 'a st \<Rightarrow> 'a" where
nipkow@45963
    19
"aval' (N n) _ = num' n" |
nipkow@45963
    20
"aval' (V x) S = lookup S x" |
nipkow@45963
    21
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
nipkow@45963
    22
nipkow@45963
    23
fun step :: "'a st up \<Rightarrow> 'a st up acom \<Rightarrow> 'a st up acom" where
nipkow@45963
    24
"step S (SKIP {P}) = (SKIP {S})" |
nipkow@45963
    25
"step S (x ::= e {P}) =
nipkow@45963
    26
  x ::= e {case S of Bot \<Rightarrow> Bot | Up S \<Rightarrow> Up(update S x (aval' e S))}" |
nipkow@45963
    27
"step S (c1; c2) = step S c1; step (post c1) c2" |
nipkow@45963
    28
"step S (IF b THEN c1 ELSE c2 {P}) =
nipkow@45963
    29
  (let c1' = step S c1; c2' = step S c2
nipkow@45963
    30
   in IF b THEN c1' ELSE c2' {post c1 \<squnion> post c2})" |
nipkow@45963
    31
"step S ({Inv} WHILE b DO c {P}) =
nipkow@45963
    32
   {S \<squnion> post c} WHILE b DO step Inv c {Inv}"
nipkow@45963
    33
nipkow@45963
    34
lemma strip_step[simp]: "strip(step S c) = strip c"
nipkow@45963
    35
by(induct c arbitrary: S) (simp_all add: Let_def)
nipkow@45963
    36
nipkow@45963
    37
definition AI :: "com \<Rightarrow> 'a st up acom" where
nipkow@45963
    38
"AI c = pfp (step Top) (bot_acom c)"
nipkow@45963
    39
nipkow@45963
    40
nipkow@45963
    41
subsubsection "Monotonicity"
nipkow@45963
    42
nipkow@45963
    43
lemma mono_aval': "S \<sqsubseteq> S' \<Longrightarrow> aval' e S \<sqsubseteq> aval' e S'"
nipkow@45963
    44
by(induction e) (auto simp: le_st_def lookup_def mono_plus')
nipkow@45963
    45
nipkow@45963
    46
lemma mono_update: "a \<sqsubseteq> a' \<Longrightarrow> S \<sqsubseteq> S' \<Longrightarrow> update S x a \<sqsubseteq> update S' x a'"
nipkow@45963
    47
by(auto simp add: le_st_def lookup_def update_def)
nipkow@45963
    48
nipkow@45963
    49
lemma step_mono: "S \<sqsubseteq> S' \<Longrightarrow> step S c \<sqsubseteq> step S' c"
nipkow@45963
    50
apply(induction c arbitrary: S S')
nipkow@45963
    51
apply (auto simp: Let_def mono_update mono_aval' le_join_disj split: up.split)
nipkow@45963
    52
done
nipkow@45963
    53
nipkow@45963
    54
subsubsection "Soundness"
nipkow@45963
    55
nipkow@45963
    56
lemma aval'_sound: "s <:f S \<Longrightarrow> aval a s <: aval' a S"
nipkow@45963
    57
by (induct a) (auto simp: rep_num' rep_plus' rep_st_def lookup_def)
nipkow@45963
    58
nipkow@45963
    59
lemma in_rep_update: "\<lbrakk> s <:f S; i <: a \<rbrakk> \<Longrightarrow> s(x := i) <:f update S x a"
nipkow@45963
    60
by(simp add: rep_st_def lookup_update)
nipkow@45963
    61
nipkow@45963
    62
lemma step_sound:
nipkow@45963
    63
  "step S c \<sqsubseteq> c \<Longrightarrow> (strip c,s) \<Rightarrow> t \<Longrightarrow> s <:up S \<Longrightarrow> t <:up post c"
nipkow@45963
    64
proof(induction c arbitrary: S s t)
nipkow@45963
    65
  case SKIP thus ?case
nipkow@45963
    66
    by simp (metis skipE up_fun_in_rep_le)
nipkow@45963
    67
next
nipkow@45963
    68
  case Assign thus ?case
nipkow@45963
    69
    apply (auto simp del: fun_upd_apply simp: split: up.splits)
nipkow@45963
    70
    by (metis aval'_sound fun_in_rep_le in_rep_update)
nipkow@45963
    71
next
nipkow@45963
    72
  case Semi thus ?case by simp blast
nipkow@45963
    73
next
nipkow@45963
    74
  case (If b c1 c2 S0) thus ?case
nipkow@45963
    75
    apply(auto simp: Let_def)
nipkow@45963
    76
    apply (metis up_fun_in_rep_le)+
nipkow@45963
    77
    done
nipkow@45963
    78
next
nipkow@45963
    79
  case (While Inv b c P)
nipkow@45963
    80
  from While.prems have inv: "step Inv c \<sqsubseteq> c"
nipkow@45963
    81
    and "post c \<sqsubseteq> Inv" and "S \<sqsubseteq> Inv" and "Inv \<sqsubseteq> P" by(auto simp: Let_def)
nipkow@45963
    82
  { fix s t have "(WHILE b DO strip c,s) \<Rightarrow> t \<Longrightarrow> s <:up Inv \<Longrightarrow> t <:up Inv"
nipkow@45963
    83
    proof(induction "WHILE b DO strip c" s t rule: big_step_induct)
nipkow@45963
    84
      case WhileFalse thus ?case by simp
nipkow@45963
    85
    next
nipkow@45963
    86
      case (WhileTrue s1 s2 s3)
nipkow@45963
    87
      from WhileTrue.hyps(5)[OF up_fun_in_rep_le[OF While.IH[OF inv `(strip c, s1) \<Rightarrow> s2` `s1 <:up Inv`] `post c \<sqsubseteq> Inv`]]
nipkow@45963
    88
      show ?case .
nipkow@45963
    89
    qed
nipkow@45963
    90
  }
nipkow@45963
    91
  thus ?case using While.prems(2)
nipkow@45963
    92
    by simp (metis `s <:up S` `S \<sqsubseteq> Inv` `Inv \<sqsubseteq> P` up_fun_in_rep_le)
nipkow@45963
    93
qed
nipkow@45963
    94
nipkow@45963
    95
lemma AI_sound: "(c,s) \<Rightarrow> t \<Longrightarrow> t <:up post(AI c)"
nipkow@45963
    96
by(fastforce simp: AI_def strip_pfp in_rep_Top_up intro: step_sound pfp)
nipkow@45963
    97
nipkow@45963
    98
end
nipkow@45963
    99
nipkow@45963
   100
nipkow@45963
   101
end