| author | wenzelm | 
| Tue, 27 Oct 2009 10:54:25 +0100 | |
| changeset 33219 | a69147d95957 | 
| parent 32960 | 69916a850301 | 
| child 35102 | cc7a0b9f938c | 
| permissions | -rw-r--r-- | 
| 11565 | 1 | (* Title: HOL/NanoJava/Example.thy | 
| 2 | Author: David von Oheimb | |
| 3 | Copyright 2001 Technische Universitaet Muenchen | |
| 4 | *) | |
| 5 | ||
| 6 | header "Example" | |
| 7 | ||
| 16417 | 8 | theory Example imports Equivalence begin | 
| 11565 | 9 | |
| 10 | text {*
 | |
| 11 | ||
| 12 | \begin{verbatim}
 | |
| 13 | class Nat {
 | |
| 14 | ||
| 15 | Nat pred; | |
| 16 | ||
| 17 | Nat suc() | |
| 18 |     { Nat n = new Nat(); n.pred = this; return n; }
 | |
| 19 | ||
| 20 | Nat eq(Nat n) | |
| 21 |     { if (this.pred != null) if (n.pred != null) return this.pred.eq(n.pred);
 | |
| 22 | else return n.pred; // false | |
| 23 | else if (n.pred != null) return this.pred; // false | |
| 24 | else return this.suc(); // true | |
| 25 | } | |
| 26 | ||
| 27 | Nat add(Nat n) | |
| 28 |     { if (this.pred != null) return this.pred.add(n.suc()); else return n; }
 | |
| 29 | ||
| 30 | public static void main(String[] args) // test x+1=1+x | |
| 31 |     {
 | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
21020diff
changeset | 32 | Nat one = new Nat().suc(); | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
21020diff
changeset | 33 | Nat x = new Nat().suc().suc().suc().suc(); | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
21020diff
changeset | 34 | Nat ok = x.suc().eq(x.add(one)); | 
| 11565 | 35 | System.out.println(ok != null); | 
| 36 | } | |
| 37 | } | |
| 38 | \end{verbatim}
 | |
| 39 | ||
| 40 | *} | |
| 41 | ||
| 42 | axioms This_neq_Par [simp]: "This \<noteq> Par" | |
| 43 | Res_neq_This [simp]: "Res \<noteq> This" | |
| 44 | ||
| 45 | ||
| 46 | subsection "Program representation" | |
| 47 | ||
| 48 | consts N    :: cname ("Nat") (* with mixfix because of clash with NatDef.Nat *)
 | |
| 49 | consts pred :: fname | |
| 50 | consts suc :: mname | |
| 51 | add :: mname | |
| 52 | consts any :: vname | |
| 53 | syntax dummy:: expr ("<>")
 | |
| 54 | one :: expr | |
| 55 | translations | |
| 56 | "<>" == "LAcc any" | |
| 57 |       "one" == "{Nat}new Nat..suc(<>)"
 | |
| 58 | ||
| 59 | text {* The following properties could be derived from a more complete
 | |
| 60 | program model, which we leave out for laziness. *} | |
| 61 | ||
| 62 | axioms Nat_no_subclasses [simp]: "D \<preceq>C Nat = (D=Nat)" | |
| 63 | ||
| 64 | axioms method_Nat_add [simp]: "method Nat add = Some | |
| 65 | \<lparr> par=Class Nat, res=Class Nat, lcl=[], | |
| 66 | bdy= If((LAcc This..pred)) | |
| 67 |           (Res :== {Nat}(LAcc This..pred)..add({Nat}LAcc Par..suc(<>))) 
 | |
| 68 | Else Res :== LAcc Par \<rparr>" | |
| 69 | ||
| 70 | axioms method_Nat_suc [simp]: "method Nat suc = Some | |
| 71 | \<lparr> par=NT, res=Class Nat, lcl=[], | |
| 72 | bdy= Res :== new Nat;; LAcc Res..pred :== LAcc This \<rparr>" | |
| 73 | ||
| 74 | axioms field_Nat [simp]: "field Nat = empty(pred\<mapsto>Class Nat)" | |
| 75 | ||
| 76 | lemma init_locs_Nat_add [simp]: "init_locs Nat add s = s" | |
| 77 | by (simp add: init_locs_def init_vars_def) | |
| 78 | ||
| 79 | lemma init_locs_Nat_suc [simp]: "init_locs Nat suc s = s" | |
| 80 | by (simp add: init_locs_def init_vars_def) | |
| 81 | ||
| 82 | lemma upd_obj_new_obj_Nat [simp]: | |
| 83 | "upd_obj a pred v (new_obj a Nat s) = hupd(a\<mapsto>(Nat, empty(pred\<mapsto>v))) s" | |
| 84 | by (simp add: new_obj_def init_vars_def upd_obj_def Let_def) | |
| 85 | ||
| 86 | ||
| 87 | subsection "``atleast'' relation for interpretation of Nat ``values''" | |
| 88 | ||
| 89 | consts Nat_atleast :: "state \<Rightarrow> val \<Rightarrow> nat \<Rightarrow> bool" ("_:_ \<ge> _" [51, 51, 51] 50)
 | |
| 90 | primrec "s:x\<ge>0 = (x\<noteq>Null)" | |
| 91 | "s:x\<ge>Suc n = (\<exists>a. x=Addr a \<and> heap s a \<noteq> None \<and> s:get_field s a pred\<ge>n)" | |
| 92 | ||
| 93 | lemma Nat_atleast_lupd [rule_format, simp]: | |
| 21020 | 94 | "\<forall>s v::val. lupd(x\<mapsto>y) s:v \<ge> n = (s:v \<ge> n)" | 
| 11565 | 95 | apply (induct n) | 
| 96 | by auto | |
| 97 | ||
| 98 | lemma Nat_atleast_set_locs [rule_format, simp]: | |
| 21020 | 99 | "\<forall>s v::val. set_locs l s:v \<ge> n = (s:v \<ge> n)" | 
| 11565 | 100 | apply (induct n) | 
| 101 | by auto | |
| 102 | ||
| 11772 | 103 | lemma Nat_atleast_del_locs [rule_format, simp]: | 
| 21020 | 104 | "\<forall>s v::val. del_locs s:v \<ge> n = (s:v \<ge> n)" | 
| 11565 | 105 | apply (induct n) | 
| 106 | by auto | |
| 107 | ||
| 108 | lemma Nat_atleast_NullD [rule_format]: "s:Null \<ge> n \<longrightarrow> False" | |
| 109 | apply (induct n) | |
| 110 | by auto | |
| 111 | ||
| 112 | lemma Nat_atleast_pred_NullD [rule_format]: | |
| 113 | "Null = get_field s a pred \<Longrightarrow> s:Addr a \<ge> n \<longrightarrow> n = 0" | |
| 114 | apply (induct n) | |
| 115 | by (auto dest: Nat_atleast_NullD) | |
| 116 | ||
| 117 | lemma Nat_atleast_mono [rule_format]: | |
| 118 | "\<forall>a. s:get_field s a pred \<ge> n \<longrightarrow> heap s a \<noteq> None \<longrightarrow> s:Addr a \<ge> n" | |
| 119 | apply (induct n) | |
| 120 | by auto | |
| 121 | ||
| 122 | lemma Nat_atleast_newC [rule_format]: | |
| 21020 | 123 | "heap s aa = None \<Longrightarrow> \<forall>v::val. s:v \<ge> n \<longrightarrow> hupd(aa\<mapsto>obj) s:v \<ge> n" | 
| 11565 | 124 | apply (induct n) | 
| 125 | apply auto | |
| 126 | apply (case_tac "aa=a") | |
| 127 | apply auto | |
| 128 | apply (tactic "smp_tac 1 1") | |
| 129 | apply (case_tac "aa=a") | |
| 130 | apply auto | |
| 131 | done | |
| 132 | ||
| 133 | ||
| 134 | subsection "Proof(s) using the Hoare logic" | |
| 135 | ||
| 12742 | 136 | theorem add_homomorph_lb: | 
| 11565 | 137 |   "{} \<turnstile> {\<lambda>s. s:s<This> \<ge> X \<and> s:s<Par> \<ge> Y} Meth(Nat,add) {\<lambda>s. s:s<Res> \<ge> X+Y}"
 | 
| 12742 | 138 | apply (rule hoare_ehoare.Meth) (* 1 *) | 
| 11565 | 139 | apply clarsimp | 
| 140 | apply (rule_tac P'= "\<lambda>Z s. (s:s<This> \<ge> fst Z \<and> s:s<Par> \<ge> snd Z) \<and> D=Nat" and | |
| 12934 
6003b4f916c0
Clarification wrt. use of polymorphic variants of Hoare logic rules
 oheimb parents: 
12742diff
changeset | 141 | Q'= "\<lambda>Z s. s:s<Res> \<ge> fst Z+snd Z" in AxSem.Conseq) | 
| 11565 | 142 | prefer 2 | 
| 143 | apply (clarsimp simp add: init_locs_def init_vars_def) | |
| 144 | apply rule | |
| 145 | apply (case_tac "D = Nat", simp_all, rule_tac [2] cFalse) | |
| 12934 
6003b4f916c0
Clarification wrt. use of polymorphic variants of Hoare logic rules
 oheimb parents: 
12742diff
changeset | 146 | apply (rule_tac P = "\<lambda>Z Cm s. s:s<This> \<ge> fst Z \<and> s:s<Par> \<ge> snd Z" in AxSem.Impl1) | 
| 12742 | 147 | apply (clarsimp simp add: body_def) (* 4 *) | 
| 11565 | 148 | apply (rename_tac n m) | 
| 149 | apply (rule_tac Q = "\<lambda>v s. (s:s<This> \<ge> n \<and> s:s<Par> \<ge> m) \<and> | |
| 150 | (\<exists>a. s<This> = Addr a \<and> v = get_field s a pred)" in hoare_ehoare.Cond) | |
| 151 | apply (rule hoare_ehoare.FAcc) | |
| 152 | apply (rule eConseq1) | |
| 153 | apply (rule hoare_ehoare.LAcc) | |
| 154 | apply fast | |
| 155 | apply auto | |
| 156 | prefer 2 | |
| 157 | apply (rule hoare_ehoare.LAss) | |
| 158 | apply (rule eConseq1) | |
| 159 | apply (rule hoare_ehoare.LAcc) | |
| 160 | apply (auto dest: Nat_atleast_pred_NullD) | |
| 161 | apply (rule hoare_ehoare.LAss) | |
| 162 | apply (rule_tac | |
| 163 | Q = "\<lambda>v s. (\<forall>m. n = Suc m \<longrightarrow> s:v \<ge> m) \<and> s:s<Par> \<ge> m" and | |
| 164 | R = "\<lambda>T P s. (\<forall>m. n = Suc m \<longrightarrow> s:T \<ge> m) \<and> s:P \<ge> Suc m" | |
| 12742 | 165 | in hoare_ehoare.Call) (* 13 *) | 
| 11565 | 166 | apply (rule hoare_ehoare.FAcc) | 
| 167 | apply (rule eConseq1) | |
| 168 | apply (rule hoare_ehoare.LAcc) | |
| 169 | apply clarify | |
| 170 | apply (drule sym, rotate_tac -1, frule (1) trans) | |
| 171 | apply simp | |
| 172 | prefer 2 | |
| 173 | apply clarsimp | |
| 12742 | 174 | apply (rule hoare_ehoare.Meth) (* 17 *) | 
| 11565 | 175 | apply clarsimp | 
| 176 | apply (case_tac "D = Nat", simp_all, rule_tac [2] cFalse) | |
| 12934 
6003b4f916c0
Clarification wrt. use of polymorphic variants of Hoare logic rules
 oheimb parents: 
12742diff
changeset | 177 | apply (rule AxSem.Conseq) | 
| 11565 | 178 | apply rule | 
| 12742 | 179 | apply (rule hoare_ehoare.Asm) (* 20 *) | 
| 11565 | 180 | apply (rule_tac a = "((case n of 0 \<Rightarrow> 0 | Suc m \<Rightarrow> m),m+1)" in UN_I, rule+) | 
| 181 | apply (clarsimp split add: nat.split_asm dest!: Nat_atleast_mono) | |
| 182 | apply rule | |
| 12742 | 183 | apply (rule hoare_ehoare.Call) (* 21 *) | 
| 11565 | 184 | apply (rule hoare_ehoare.LAcc) | 
| 185 | apply rule | |
| 186 | apply (rule hoare_ehoare.LAcc) | |
| 187 | apply clarify | |
| 12742 | 188 | apply (rule hoare_ehoare.Meth) (* 24 *) | 
| 11565 | 189 | apply clarsimp | 
| 190 | apply (case_tac "D = Nat", simp_all, rule_tac [2] cFalse) | |
| 12934 
6003b4f916c0
Clarification wrt. use of polymorphic variants of Hoare logic rules
 oheimb parents: 
12742diff
changeset | 191 | apply (rule AxSem.Impl1) | 
| 11565 | 192 | apply (clarsimp simp add: body_def) | 
| 12742 | 193 | apply (rule hoare_ehoare.Comp) (* 26 *) | 
| 11565 | 194 | prefer 2 | 
| 195 | apply (rule hoare_ehoare.FAss) | |
| 196 | prefer 2 | |
| 197 | apply rule | |
| 198 | apply (rule hoare_ehoare.LAcc) | |
| 199 | apply (rule hoare_ehoare.LAcc) | |
| 200 | apply (rule hoare_ehoare.LAss) | |
| 201 | apply (rule eConseq1) | |
| 12742 | 202 | apply (rule hoare_ehoare.NewC) (* 32 *) | 
| 11565 | 203 | apply (auto dest!: new_AddrD elim: Nat_atleast_newC) | 
| 204 | done | |
| 205 | ||
| 206 | ||
| 207 | end |