src/HOL/IMP/Abs_Int1_ivl.thy
author nipkow
Wed Sep 28 09:55:11 2011 +0200 (2011-09-28)
changeset 45111 054a9ac0d7ef
child 45113 2a0d7be998bb
permissions -rw-r--r--
Added Hoare-like Abstract Interpretation
nipkow@45111
     1
(* Author: Tobias Nipkow *)
nipkow@45111
     2
nipkow@45111
     3
theory Abs_Int1_ivl
nipkow@45111
     4
imports Abs_Int1
nipkow@45111
     5
begin
nipkow@45111
     6
nipkow@45111
     7
subsection "Interval Analysis"
nipkow@45111
     8
nipkow@45111
     9
datatype ivl = I "int option" "int option"
nipkow@45111
    10
nipkow@45111
    11
definition "rep_ivl i = (case i of
nipkow@45111
    12
  I (Some l) (Some h) \<Rightarrow> {l..h} |
nipkow@45111
    13
  I (Some l) None \<Rightarrow> {l..} |
nipkow@45111
    14
  I None (Some h) \<Rightarrow> {..h} |
nipkow@45111
    15
  I None None \<Rightarrow> UNIV)"
nipkow@45111
    16
nipkow@45111
    17
definition "num_ivl n = I (Some n) (Some n)"
nipkow@45111
    18
nipkow@45111
    19
instantiation option :: (plus)plus
nipkow@45111
    20
begin
nipkow@45111
    21
nipkow@45111
    22
fun plus_option where
nipkow@45111
    23
"Some x + Some y = Some(x+y)" |
nipkow@45111
    24
"_ + _ = None"
nipkow@45111
    25
nipkow@45111
    26
instance proof qed
nipkow@45111
    27
nipkow@45111
    28
end
nipkow@45111
    29
nipkow@45111
    30
definition empty where "empty = I (Some 1) (Some 0)"
nipkow@45111
    31
nipkow@45111
    32
fun is_empty where
nipkow@45111
    33
"is_empty(I (Some l) (Some h)) = (h<l)" |
nipkow@45111
    34
"is_empty _ = False"
nipkow@45111
    35
nipkow@45111
    36
lemma [simp]: "is_empty(I l h) =
nipkow@45111
    37
  (case l of Some l \<Rightarrow> (case h of Some h \<Rightarrow> h<l | None \<Rightarrow> False) | None \<Rightarrow> False)"
nipkow@45111
    38
by(auto split:option.split)
nipkow@45111
    39
nipkow@45111
    40
lemma [simp]: "is_empty i \<Longrightarrow> rep_ivl i = {}"
nipkow@45111
    41
by(auto simp add: rep_ivl_def split: ivl.split option.split)
nipkow@45111
    42
nipkow@45111
    43
definition "plus_ivl i1 i2 = (if is_empty i1 | is_empty i2 then empty else
nipkow@45111
    44
  case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow> I (l1+l2) (h1+h2))"
nipkow@45111
    45
nipkow@45111
    46
instantiation ivl :: SL_top
nipkow@45111
    47
begin
nipkow@45111
    48
nipkow@45111
    49
definition le_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> bool" where
nipkow@45111
    50
"le_option pos x y =
nipkow@45111
    51
 (case x of (Some i) \<Rightarrow> (case y of Some j \<Rightarrow> i\<le>j | None \<Rightarrow> pos)
nipkow@45111
    52
  | None \<Rightarrow> (case y of Some j \<Rightarrow> \<not>pos | None \<Rightarrow> True))"
nipkow@45111
    53
nipkow@45111
    54
fun le_aux where
nipkow@45111
    55
"le_aux (I l1 h1) (I l2 h2) = (le_option False l2 l1 & le_option True h1 h2)"
nipkow@45111
    56
nipkow@45111
    57
definition le_ivl where
nipkow@45111
    58
"i1 \<sqsubseteq> i2 =
nipkow@45111
    59
 (if is_empty i1 then True else
nipkow@45111
    60
  if is_empty i2 then False else le_aux i1 i2)"
nipkow@45111
    61
nipkow@45111
    62
definition min_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> int option" where
nipkow@45111
    63
"min_option pos o1 o2 = (if le_option pos o1 o2 then o1 else o2)"
nipkow@45111
    64
nipkow@45111
    65
definition max_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> int option" where
nipkow@45111
    66
"max_option pos o1 o2 = (if le_option pos o1 o2 then o2 else o1)"
nipkow@45111
    67
nipkow@45111
    68
definition "i1 \<squnion> i2 =
nipkow@45111
    69
 (if is_empty i1 then i2 else if is_empty i2 then i1
nipkow@45111
    70
  else case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow>
nipkow@45111
    71
          I (min_option False l1 l2) (max_option True h1 h2))"
nipkow@45111
    72
nipkow@45111
    73
definition "\<top> = I None None"
nipkow@45111
    74
nipkow@45111
    75
instance
nipkow@45111
    76
proof
nipkow@45111
    77
  case goal1 thus ?case
nipkow@45111
    78
    by(cases x, simp add: le_ivl_def le_option_def split: option.split)
nipkow@45111
    79
next
nipkow@45111
    80
  case goal2 thus ?case
nipkow@45111
    81
    by(cases x, cases y, cases z, auto simp: le_ivl_def le_option_def split: option.splits if_splits)
nipkow@45111
    82
next
nipkow@45111
    83
  case goal3 thus ?case
nipkow@45111
    84
    by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits)
nipkow@45111
    85
next
nipkow@45111
    86
  case goal4 thus ?case
nipkow@45111
    87
    by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits)
nipkow@45111
    88
next
nipkow@45111
    89
  case goal5 thus ?case
nipkow@45111
    90
    by(cases x, cases y, cases z, auto simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits if_splits)
nipkow@45111
    91
next
nipkow@45111
    92
  case goal6 thus ?case
nipkow@45111
    93
    by(cases x, simp add: Top_ivl_def le_ivl_def le_option_def split: option.split)
nipkow@45111
    94
qed
nipkow@45111
    95
nipkow@45111
    96
end
nipkow@45111
    97
nipkow@45111
    98
nipkow@45111
    99
instantiation ivl :: L_top_bot
nipkow@45111
   100
begin
nipkow@45111
   101
nipkow@45111
   102
definition "i1 \<sqinter> i2 = (if is_empty i1 \<or> is_empty i2 then empty else
nipkow@45111
   103
  case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow>
nipkow@45111
   104
    I (max_option False l1 l2) (min_option True h1 h2))"
nipkow@45111
   105
nipkow@45111
   106
definition "\<bottom> = empty"
nipkow@45111
   107
nipkow@45111
   108
instance
nipkow@45111
   109
proof
nipkow@45111
   110
  case goal1 thus ?case
nipkow@45111
   111
    by (simp add:meet_ivl_def empty_def meet_ivl_def le_ivl_def le_option_def max_option_def min_option_def split: ivl.splits option.splits)
nipkow@45111
   112
next
nipkow@45111
   113
  case goal2 thus ?case
nipkow@45111
   114
    by (simp add:meet_ivl_def empty_def meet_ivl_def le_ivl_def le_option_def max_option_def min_option_def split: ivl.splits option.splits)
nipkow@45111
   115
next
nipkow@45111
   116
  case goal3 thus ?case
nipkow@45111
   117
    by (cases x, cases y, cases z, auto simp add: le_ivl_def meet_ivl_def empty_def le_option_def max_option_def min_option_def split: option.splits if_splits)
nipkow@45111
   118
next
nipkow@45111
   119
  case goal4 show ?case by(cases x, simp add: bot_ivl_def empty_def le_ivl_def)
nipkow@45111
   120
qed
nipkow@45111
   121
nipkow@45111
   122
end
nipkow@45111
   123
nipkow@45111
   124
instantiation option :: (minus)minus
nipkow@45111
   125
begin
nipkow@45111
   126
nipkow@45111
   127
fun minus_option where
nipkow@45111
   128
"Some x - Some y = Some(x-y)" |
nipkow@45111
   129
"_ - _ = None"
nipkow@45111
   130
nipkow@45111
   131
instance proof qed
nipkow@45111
   132
nipkow@45111
   133
end
nipkow@45111
   134
nipkow@45111
   135
definition "minus_ivl i1 i2 = (if is_empty i1 | is_empty i2 then empty else
nipkow@45111
   136
  case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow> I (l1-h2) (h1-l2))"
nipkow@45111
   137
nipkow@45111
   138
lemma rep_minus_ivl:
nipkow@45111
   139
  "n1 : rep_ivl i1 \<Longrightarrow> n2 : rep_ivl i2 \<Longrightarrow> n1-n2 : rep_ivl(minus_ivl i1 i2)"
nipkow@45111
   140
by(auto simp add: minus_ivl_def rep_ivl_def split: ivl.splits option.splits)
nipkow@45111
   141
nipkow@45111
   142
nipkow@45111
   143
definition "filter_plus_ivl i i1 i2 = ((*if is_empty i then empty else*)
nipkow@45111
   144
  i1 \<sqinter> minus_ivl i i2, i2 \<sqinter> minus_ivl i i1)"
nipkow@45111
   145
nipkow@45111
   146
fun filter_less_ivl :: "bool \<Rightarrow> ivl \<Rightarrow> ivl \<Rightarrow> ivl * ivl" where
nipkow@45111
   147
"filter_less_ivl res (I l1 h1) (I l2 h2) =
nipkow@45111
   148
  (if is_empty(I l1 h1) \<or> is_empty(I l2 h2) then (empty, empty) else
nipkow@45111
   149
   if res
nipkow@45111
   150
   then (I l1 (min_option True h1 (h2 - Some 1)),
nipkow@45111
   151
         I (max_option False (l1 + Some 1) l2) h2)
nipkow@45111
   152
   else (I (max_option False l1 l2) h1, I l2 (min_option True h1 h2)))"
nipkow@45111
   153
nipkow@45111
   154
interpretation Rep rep_ivl
nipkow@45111
   155
proof
nipkow@45111
   156
  case goal1 thus ?case
nipkow@45111
   157
    by(auto simp: rep_ivl_def le_ivl_def le_option_def split: ivl.split option.split if_splits)
nipkow@45111
   158
next
nipkow@45111
   159
  case goal2 show ?case by(simp add: rep_ivl_def Top_ivl_def)
nipkow@45111
   160
qed
nipkow@45111
   161
nipkow@45111
   162
interpretation Val_abs rep_ivl num_ivl plus_ivl
nipkow@45111
   163
proof
nipkow@45111
   164
  case goal1 thus ?case by(simp add: rep_ivl_def num_ivl_def)
nipkow@45111
   165
next
nipkow@45111
   166
  case goal2 thus ?case
nipkow@45111
   167
    by(auto simp add: rep_ivl_def plus_ivl_def split: ivl.split option.splits)
nipkow@45111
   168
next
nipkow@45111
   169
  case goal3 thus ?case
nipkow@45111
   170
    by(auto simp: plus_ivl_def le_ivl_def le_option_def empty_def split: if_splits ivl.splits option.splits)
nipkow@45111
   171
qed
nipkow@45111
   172
nipkow@45111
   173
interpretation Rep1 rep_ivl
nipkow@45111
   174
proof
nipkow@45111
   175
  case goal1 thus ?case
nipkow@45111
   176
    by(auto simp add: rep_ivl_def meet_ivl_def empty_def min_option_def max_option_def split: ivl.split option.split)
nipkow@45111
   177
next
nipkow@45111
   178
  case goal2 show ?case by(auto simp add: bot_ivl_def rep_ivl_def empty_def)
nipkow@45111
   179
qed
nipkow@45111
   180
nipkow@45111
   181
lemma mono_minus_ivl:
nipkow@45111
   182
  "i1 \<sqsubseteq> i1' \<Longrightarrow> i2 \<sqsubseteq> i2' \<Longrightarrow> minus_ivl i1 i2 \<sqsubseteq> minus_ivl i1' i2'"
nipkow@45111
   183
apply(auto simp add: minus_ivl_def empty_def le_ivl_def le_option_def split: ivl.splits)
nipkow@45111
   184
  apply(simp split: option.splits)
nipkow@45111
   185
 apply(simp split: option.splits)
nipkow@45111
   186
apply(simp split: option.splits)
nipkow@45111
   187
done
nipkow@45111
   188
nipkow@45111
   189
nipkow@45111
   190
interpretation
nipkow@45111
   191
  Val_abs1 rep_ivl num_ivl plus_ivl filter_plus_ivl filter_less_ivl
nipkow@45111
   192
proof
nipkow@45111
   193
  case goal1 thus ?case
nipkow@45111
   194
    by(auto simp add: filter_plus_ivl_def)
nipkow@45111
   195
      (metis rep_minus_ivl add_diff_cancel add_commute)+
nipkow@45111
   196
next
nipkow@45111
   197
  case goal2 thus ?case
nipkow@45111
   198
    by(cases a1, cases a2,
nipkow@45111
   199
      auto simp: rep_ivl_def min_option_def max_option_def le_option_def split: if_splits option.splits)
nipkow@45111
   200
next
nipkow@45111
   201
  case goal3 thus ?case
nipkow@45111
   202
    by(auto simp: filter_plus_ivl_def le_prod_def mono_meet mono_minus_ivl)
nipkow@45111
   203
next
nipkow@45111
   204
  case goal4 thus ?case
nipkow@45111
   205
    apply(cases a1, cases b1, cases a2, cases b2, auto simp: le_prod_def)
nipkow@45111
   206
    by(auto simp add: empty_def le_ivl_def le_option_def min_option_def max_option_def split: option.splits)
nipkow@45111
   207
qed
nipkow@45111
   208
nipkow@45111
   209
interpretation
nipkow@45111
   210
  Abs_Int1 rep_ivl num_ivl plus_ivl filter_plus_ivl filter_less_ivl "(iter 20)"
nipkow@45111
   211
defines afilter_ivl is afilter
nipkow@45111
   212
and bfilter_ivl is bfilter
nipkow@45111
   213
and step_ivl is step
nipkow@45111
   214
and AI_ivl is AI
nipkow@45111
   215
and aval_ivl is aval'
nipkow@45111
   216
proof qed (auto simp: iter_pfp strip_iter)
nipkow@45111
   217
nipkow@45111
   218
definition "test1_ivl =
nipkow@45111
   219
 ''y'' ::= N 7;
nipkow@45111
   220
 IF Less (V ''x'') (V ''y'')
nipkow@45111
   221
 THEN ''y'' ::= Plus (V ''y'') (V ''x'')
nipkow@45111
   222
 ELSE ''x'' ::= Plus (V ''x'') (V ''y'')"
nipkow@45111
   223
nipkow@45111
   224
translations
nipkow@45111
   225
"{i..j}" <= "CONST I (CONST Some i) (CONST Some j)"
nipkow@45111
   226
"{..j}" <= "CONST I (CONST None) (CONST Some j)"
nipkow@45111
   227
"{i..}" <= "CONST I (CONST Some i) (CONST None)"
nipkow@45111
   228
"CONST UNIV" <= "CONST I (CONST None) (CONST None)"
nipkow@45111
   229
nipkow@45111
   230
value [code] "show_acom (AI_ivl test1_ivl)"
nipkow@45111
   231
nipkow@45111
   232
value [code] "show_acom (AI_const test3_const)"
nipkow@45111
   233
value [code] "show_acom (AI_ivl test3_const)"
nipkow@45111
   234
nipkow@45111
   235
value [code] "show_acom (AI_const test4_const)"
nipkow@45111
   236
value [code] "show_acom (AI_ivl test4_const)"
nipkow@45111
   237
nipkow@45111
   238
value [code] "show_acom (AI_ivl test6_const)"
nipkow@45111
   239
nipkow@45111
   240
definition "test2_ivl =
nipkow@45111
   241
 WHILE Less (V ''x'') (N 100)
nipkow@45111
   242
 DO ''x'' ::= Plus (V ''x'') (N 1)"
nipkow@45111
   243
nipkow@45111
   244
value [code] "show_acom (AI_ivl test2_ivl)"
nipkow@45111
   245
nipkow@45111
   246
definition "test3_ivl =
nipkow@45111
   247
 ''x'' ::= N 7;
nipkow@45111
   248
 WHILE Less (V ''x'') (N 100)
nipkow@45111
   249
 DO ''x'' ::= Plus (V ''x'') (N 1)"
nipkow@45111
   250
nipkow@45111
   251
value [code] "show_acom (AI_ivl test3_ivl)"
nipkow@45111
   252
value [code] "show_acom (((step_ivl \<top>)^^0) (\<bottom>\<^sub>c test3_ivl))"
nipkow@45111
   253
value [code] "show_acom (((step_ivl \<top>)^^1) (\<bottom>\<^sub>c test3_ivl))"
nipkow@45111
   254
value [code] "show_acom (((step_ivl \<top>)^^2) (\<bottom>\<^sub>c test3_ivl))"
nipkow@45111
   255
value [code] "show_acom (((step_ivl \<top>)^^3) (\<bottom>\<^sub>c test3_ivl))"
nipkow@45111
   256
value [code] "show_acom (((step_ivl \<top>)^^4) (\<bottom>\<^sub>c test3_ivl))"
nipkow@45111
   257
nipkow@45111
   258
definition "test4_ivl =
nipkow@45111
   259
 ''x'' ::= N 0; ''y'' ::= N 100; ''z'' ::= Plus (V ''x'') (V ''y'');
nipkow@45111
   260
 WHILE Less (V ''x'') (N 11)
nipkow@45111
   261
 DO (''x'' ::= Plus (V ''x'') (N 1); ''y'' ::= Plus (V ''y'') (N -1))"
nipkow@45111
   262
value [code] "show_acom(AI_ivl test4_ivl)"
nipkow@45111
   263
nipkow@45111
   264
definition "test5_ivl =
nipkow@45111
   265
 ''x'' ::= N 0; ''y'' ::= N 0;
nipkow@45111
   266
 WHILE Less (V ''x'') (N 1001)
nipkow@45111
   267
 DO (''y'' ::= V ''x''; ''x'' ::= Plus (V ''x'') (N 1))"
nipkow@45111
   268
value [code] "show_acom(AI_ivl test5_ivl)"
nipkow@45111
   269
nipkow@45111
   270
text{* Nontermination not detected: *}
nipkow@45111
   271
definition "test6_ivl =
nipkow@45111
   272
 ''x'' ::= N 0;
nipkow@45111
   273
 WHILE Less (V ''x'') (N 1) DO ''x'' ::= Plus (V ''x'') (N -1)"
nipkow@45111
   274
value [code] "show_acom(AI_ivl test6_ivl)"
nipkow@45111
   275
nipkow@45111
   276
end