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