src/HOL/IMP/Abs_Int1_parity.thy
author hoelzl
Mon, 03 Dec 2012 18:19:08 +0100
changeset 50328 25b1e8686ce0
parent 49433 1095f240146a
child 50995 3371f5ee4ace
permissions -rw-r--r--
tuned proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     1
theory Abs_Int1_parity
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     2
imports Abs_Int1
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     3
begin
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     4
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     5
subsection "Parity Analysis"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     6
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     7
datatype parity = Even | Odd | Either
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     8
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
     9
text{* Instantiation of class @{class preord} with type @{typ parity}: *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    10
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    11
instantiation parity :: preord
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    12
begin
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    13
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    14
text{* First the definition of the interface function @{text"\<sqsubseteq>"}. Note that
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    15
the header of the definition must refer to the ascii name @{const le} of the
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    16
constants as @{text le_parity} and the definition is named @{text
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    17
le_parity_def}.  Inside the definition the symbolic names can be used. *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    18
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    19
definition le_parity where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    20
"x \<sqsubseteq> y = (y = Either \<or> x=y)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    21
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    22
text{* Now the instance proof, i.e.\ the proof that the definition fulfills
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    23
the axioms (assumptions) of the class. The initial proof-step generates the
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    24
necessary proof obligations. *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    25
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    26
instance
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    27
proof
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    28
  fix x::parity show "x \<sqsubseteq> x" by(auto simp: le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    29
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    30
  fix x y z :: parity assume "x \<sqsubseteq> y" "y \<sqsubseteq> z" thus "x \<sqsubseteq> z"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    31
    by(auto simp: le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    32
qed
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    33
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    34
end
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    35
49396
73fb17ed2e08 converted wt into a set, tuned names
nipkow
parents: 49344
diff changeset
    36
text{* Instantiation of class @{class semilattice} with type @{typ parity}: *}
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    37
49396
73fb17ed2e08 converted wt into a set, tuned names
nipkow
parents: 49344
diff changeset
    38
instantiation parity :: semilattice
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    39
begin
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    40
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    41
definition join_parity where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    42
"x \<squnion> y = (if x \<sqsubseteq> y then y else if y \<sqsubseteq> x then x else Either)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    43
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    44
definition Top_parity where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    45
"\<top> = Either"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    46
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    47
text{* Now the instance proof. This time we take a lazy shortcut: we do not
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    48
write out the proof obligations but use the @{text goali} primitive to refer
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    49
to the assumptions of subgoal i and @{text "case?"} to refer to the
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    50
conclusion of subgoal i. The class axioms are presented in the same order as
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    51
in the class definition. *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    52
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    53
instance
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    54
proof
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    55
  case goal1 (*join1*) show ?case by(auto simp: le_parity_def join_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    56
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    57
  case goal2 (*join2*) show ?case by(auto simp: le_parity_def join_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    58
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    59
  case goal3 (*join least*) thus ?case by(auto simp: le_parity_def join_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    60
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    61
  case goal4 (*Top*) show ?case by(auto simp: le_parity_def Top_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    62
qed
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    63
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    64
end
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    65
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    66
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    67
text{* Now we define the functions used for instantiating the abstract
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    68
interpretation locales. Note that the Isabelle terminology is
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    69
\emph{interpretation}, not \emph{instantiation} of locales, but we use
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    70
instantiation to avoid confusion with abstract interpretation.  *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    71
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    72
fun \<gamma>_parity :: "parity \<Rightarrow> val set" where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    73
"\<gamma>_parity Even = {i. i mod 2 = 0}" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    74
"\<gamma>_parity Odd  = {i. i mod 2 = 1}" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    75
"\<gamma>_parity Either = UNIV"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    76
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    77
fun num_parity :: "val \<Rightarrow> parity" where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    78
"num_parity i = (if i mod 2 = 0 then Even else Odd)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    79
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    80
fun plus_parity :: "parity \<Rightarrow> parity \<Rightarrow> parity" where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    81
"plus_parity Even Even = Even" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    82
"plus_parity Odd  Odd  = Even" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    83
"plus_parity Even Odd  = Odd" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    84
"plus_parity Odd  Even = Odd" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    85
"plus_parity Either y  = Either" |
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    86
"plus_parity x Either  = Either"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    87
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    88
text{* First we instantiate the abstract value interface and prove that the
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    89
functions on type @{typ parity} have all the necessary properties: *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    90
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    91
interpretation Val_abs
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    92
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    93
proof txt{* of the locale axioms *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    94
  fix a b :: parity
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    95
  assume "a \<sqsubseteq> b" thus "\<gamma>_parity a \<subseteq> \<gamma>_parity b"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    96
    by(auto simp: le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    97
next txt{* The rest in the lazy, implicit way *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    98
  case goal2 show ?case by(auto simp: Top_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
    99
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   100
  case goal3 show ?case by auto
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   101
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   102
  txt{* Warning: this subproof refers to the names @{text a1} and @{text a2}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   103
  from the statement of the axiom. *}
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   104
  case goal4 thus ?case
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   105
  proof(cases a1 a2 rule: parity.exhaust[case_product parity.exhaust])
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   106
  qed (auto simp add:mod_add_eq)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   107
qed
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   108
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   109
text{* Instantiating the abstract interpretation locale requires no more
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   110
proofs (they happened in the instatiation above) but delivers the
49344
nipkow
parents: 49188
diff changeset
   111
instantiated abstract interpreter which we call @{text AI_parity}: *}
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   112
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   113
interpretation Abs_Int
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   114
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   115
defines aval_parity is aval' and step_parity is step' and AI_parity is AI
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   116
..
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   117
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   118
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   119
subsubsection "Tests"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   120
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   121
definition "test1_parity =
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   122
  ''x'' ::= N 1;
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   123
  WHILE Less (V ''x'') (N 100) DO ''x'' ::= Plus (V ''x'') (N 2)"
49396
73fb17ed2e08 converted wt into a set, tuned names
nipkow
parents: 49344
diff changeset
   124
value [code] "show_acom_opt (AI_parity test1_parity)"
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   125
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   126
definition "test2_parity =
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   127
  ''x'' ::= N 1;
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   128
  WHILE Less (V ''x'') (N 100) DO ''x'' ::= Plus (V ''x'') (N 3)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   129
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   130
definition "steps c i = (step_parity(top c) ^^ i) (bot c)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   131
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   132
value "show_acom (steps test2_parity 0)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   133
value "show_acom (steps test2_parity 1)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   134
value "show_acom (steps test2_parity 2)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   135
value "show_acom (steps test2_parity 3)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   136
value "show_acom (steps test2_parity 4)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   137
value "show_acom (steps test2_parity 5)"
49188
22f7e7b68f50 adjusted examples
nipkow
parents: 47613
diff changeset
   138
value "show_acom (steps test2_parity 6)"
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   139
value "show_acom_opt (AI_parity test2_parity)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   140
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   141
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   142
subsubsection "Termination"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   143
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   144
interpretation Abs_Int_mono
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   145
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   146
proof
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   147
  case goal1 thus ?case
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   148
  proof(cases a1 a2 b1 b2
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   149
   rule: parity.exhaust[case_product parity.exhaust[case_product parity.exhaust[case_product parity.exhaust]]]) (* FIXME - UGLY! *)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   150
  qed (auto simp add:le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   151
qed
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   152
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   153
definition m_parity :: "parity \<Rightarrow> nat" where
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   154
"m_parity x = (if x=Either then 0 else 1)"
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   155
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   156
interpretation Abs_Int_measure
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   157
where \<gamma> = \<gamma>_parity and num' = num_parity and plus' = plus_parity
49433
nipkow
parents: 49399
diff changeset
   158
and m = m_parity and h = "1"
47613
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   159
proof
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   160
  case goal1 thus ?case by(auto simp add: m_parity_def le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   161
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   162
  case goal2 thus ?case by(auto simp add: m_parity_def le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   163
next
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   164
  case goal3 thus ?case by(auto simp add: m_parity_def le_parity_def)
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   165
qed
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   166
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   167
thm AI_Some_measure
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   168
e72e44cee6f2 added revised version of Abs_Int
nipkow
parents:
diff changeset
   169
end