author | hoelzl |
Wed, 21 Apr 2010 11:23:04 +0200 | |
changeset 36245 | af5fe3a72087 |
parent 35102 | cc7a0b9f938c |
child 39758 | b8a53e3a0ee2 |
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:
21020
diff
changeset
|
32 |
Nat one = new Nat().suc(); |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
21020
diff
changeset
|
33 |
Nat x = new Nat().suc().suc().suc().suc(); |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
21020
diff
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 |
|
35102 | 53 |
|
54 |
abbreviation |
|
55 |
dummy :: expr ("<>") |
|
56 |
where "<> == LAcc any" |
|
57 |
||
58 |
abbreviation |
|
59 |
one :: expr |
|
60 |
where "one == {Nat}new Nat..suc(<>)" |
|
11565 | 61 |
|
62 |
text {* The following properties could be derived from a more complete |
|
63 |
program model, which we leave out for laziness. *} |
|
64 |
||
65 |
axioms Nat_no_subclasses [simp]: "D \<preceq>C Nat = (D=Nat)" |
|
66 |
||
67 |
axioms method_Nat_add [simp]: "method Nat add = Some |
|
68 |
\<lparr> par=Class Nat, res=Class Nat, lcl=[], |
|
69 |
bdy= If((LAcc This..pred)) |
|
70 |
(Res :== {Nat}(LAcc This..pred)..add({Nat}LAcc Par..suc(<>))) |
|
71 |
Else Res :== LAcc Par \<rparr>" |
|
72 |
||
73 |
axioms method_Nat_suc [simp]: "method Nat suc = Some |
|
74 |
\<lparr> par=NT, res=Class Nat, lcl=[], |
|
75 |
bdy= Res :== new Nat;; LAcc Res..pred :== LAcc This \<rparr>" |
|
76 |
||
77 |
axioms field_Nat [simp]: "field Nat = empty(pred\<mapsto>Class Nat)" |
|
78 |
||
79 |
lemma init_locs_Nat_add [simp]: "init_locs Nat add s = s" |
|
80 |
by (simp add: init_locs_def init_vars_def) |
|
81 |
||
82 |
lemma init_locs_Nat_suc [simp]: "init_locs Nat suc s = s" |
|
83 |
by (simp add: init_locs_def init_vars_def) |
|
84 |
||
85 |
lemma upd_obj_new_obj_Nat [simp]: |
|
86 |
"upd_obj a pred v (new_obj a Nat s) = hupd(a\<mapsto>(Nat, empty(pred\<mapsto>v))) s" |
|
87 |
by (simp add: new_obj_def init_vars_def upd_obj_def Let_def) |
|
88 |
||
89 |
||
90 |
subsection "``atleast'' relation for interpretation of Nat ``values''" |
|
91 |
||
92 |
consts Nat_atleast :: "state \<Rightarrow> val \<Rightarrow> nat \<Rightarrow> bool" ("_:_ \<ge> _" [51, 51, 51] 50) |
|
93 |
primrec "s:x\<ge>0 = (x\<noteq>Null)" |
|
94 |
"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)" |
|
95 |
||
96 |
lemma Nat_atleast_lupd [rule_format, simp]: |
|
21020 | 97 |
"\<forall>s v::val. lupd(x\<mapsto>y) s:v \<ge> n = (s:v \<ge> n)" |
11565 | 98 |
apply (induct n) |
99 |
by auto |
|
100 |
||
101 |
lemma Nat_atleast_set_locs [rule_format, simp]: |
|
21020 | 102 |
"\<forall>s v::val. set_locs l s:v \<ge> n = (s:v \<ge> n)" |
11565 | 103 |
apply (induct n) |
104 |
by auto |
|
105 |
||
11772 | 106 |
lemma Nat_atleast_del_locs [rule_format, simp]: |
21020 | 107 |
"\<forall>s v::val. del_locs s:v \<ge> n = (s:v \<ge> n)" |
11565 | 108 |
apply (induct n) |
109 |
by auto |
|
110 |
||
111 |
lemma Nat_atleast_NullD [rule_format]: "s:Null \<ge> n \<longrightarrow> False" |
|
112 |
apply (induct n) |
|
113 |
by auto |
|
114 |
||
115 |
lemma Nat_atleast_pred_NullD [rule_format]: |
|
116 |
"Null = get_field s a pred \<Longrightarrow> s:Addr a \<ge> n \<longrightarrow> n = 0" |
|
117 |
apply (induct n) |
|
118 |
by (auto dest: Nat_atleast_NullD) |
|
119 |
||
120 |
lemma Nat_atleast_mono [rule_format]: |
|
121 |
"\<forall>a. s:get_field s a pred \<ge> n \<longrightarrow> heap s a \<noteq> None \<longrightarrow> s:Addr a \<ge> n" |
|
122 |
apply (induct n) |
|
123 |
by auto |
|
124 |
||
125 |
lemma Nat_atleast_newC [rule_format]: |
|
21020 | 126 |
"heap s aa = None \<Longrightarrow> \<forall>v::val. s:v \<ge> n \<longrightarrow> hupd(aa\<mapsto>obj) s:v \<ge> n" |
11565 | 127 |
apply (induct n) |
128 |
apply auto |
|
129 |
apply (case_tac "aa=a") |
|
130 |
apply auto |
|
131 |
apply (tactic "smp_tac 1 1") |
|
132 |
apply (case_tac "aa=a") |
|
133 |
apply auto |
|
134 |
done |
|
135 |
||
136 |
||
137 |
subsection "Proof(s) using the Hoare logic" |
|
138 |
||
12742 | 139 |
theorem add_homomorph_lb: |
11565 | 140 |
"{} \<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 | 141 |
apply (rule hoare_ehoare.Meth) (* 1 *) |
11565 | 142 |
apply clarsimp |
143 |
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:
12742
diff
changeset
|
144 |
Q'= "\<lambda>Z s. s:s<Res> \<ge> fst Z+snd Z" in AxSem.Conseq) |
11565 | 145 |
prefer 2 |
146 |
apply (clarsimp simp add: init_locs_def init_vars_def) |
|
147 |
apply rule |
|
148 |
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:
12742
diff
changeset
|
149 |
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 | 150 |
apply (clarsimp simp add: body_def) (* 4 *) |
11565 | 151 |
apply (rename_tac n m) |
152 |
apply (rule_tac Q = "\<lambda>v s. (s:s<This> \<ge> n \<and> s:s<Par> \<ge> m) \<and> |
|
153 |
(\<exists>a. s<This> = Addr a \<and> v = get_field s a pred)" in hoare_ehoare.Cond) |
|
154 |
apply (rule hoare_ehoare.FAcc) |
|
155 |
apply (rule eConseq1) |
|
156 |
apply (rule hoare_ehoare.LAcc) |
|
157 |
apply fast |
|
158 |
apply auto |
|
159 |
prefer 2 |
|
160 |
apply (rule hoare_ehoare.LAss) |
|
161 |
apply (rule eConseq1) |
|
162 |
apply (rule hoare_ehoare.LAcc) |
|
163 |
apply (auto dest: Nat_atleast_pred_NullD) |
|
164 |
apply (rule hoare_ehoare.LAss) |
|
165 |
apply (rule_tac |
|
166 |
Q = "\<lambda>v s. (\<forall>m. n = Suc m \<longrightarrow> s:v \<ge> m) \<and> s:s<Par> \<ge> m" and |
|
167 |
R = "\<lambda>T P s. (\<forall>m. n = Suc m \<longrightarrow> s:T \<ge> m) \<and> s:P \<ge> Suc m" |
|
12742 | 168 |
in hoare_ehoare.Call) (* 13 *) |
11565 | 169 |
apply (rule hoare_ehoare.FAcc) |
170 |
apply (rule eConseq1) |
|
171 |
apply (rule hoare_ehoare.LAcc) |
|
172 |
apply clarify |
|
173 |
apply (drule sym, rotate_tac -1, frule (1) trans) |
|
174 |
apply simp |
|
175 |
prefer 2 |
|
176 |
apply clarsimp |
|
12742 | 177 |
apply (rule hoare_ehoare.Meth) (* 17 *) |
11565 | 178 |
apply clarsimp |
179 |
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:
12742
diff
changeset
|
180 |
apply (rule AxSem.Conseq) |
11565 | 181 |
apply rule |
12742 | 182 |
apply (rule hoare_ehoare.Asm) (* 20 *) |
11565 | 183 |
apply (rule_tac a = "((case n of 0 \<Rightarrow> 0 | Suc m \<Rightarrow> m),m+1)" in UN_I, rule+) |
184 |
apply (clarsimp split add: nat.split_asm dest!: Nat_atleast_mono) |
|
185 |
apply rule |
|
12742 | 186 |
apply (rule hoare_ehoare.Call) (* 21 *) |
11565 | 187 |
apply (rule hoare_ehoare.LAcc) |
188 |
apply rule |
|
189 |
apply (rule hoare_ehoare.LAcc) |
|
190 |
apply clarify |
|
12742 | 191 |
apply (rule hoare_ehoare.Meth) (* 24 *) |
11565 | 192 |
apply clarsimp |
193 |
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:
12742
diff
changeset
|
194 |
apply (rule AxSem.Impl1) |
11565 | 195 |
apply (clarsimp simp add: body_def) |
12742 | 196 |
apply (rule hoare_ehoare.Comp) (* 26 *) |
11565 | 197 |
prefer 2 |
198 |
apply (rule hoare_ehoare.FAss) |
|
199 |
prefer 2 |
|
200 |
apply rule |
|
201 |
apply (rule hoare_ehoare.LAcc) |
|
202 |
apply (rule hoare_ehoare.LAcc) |
|
203 |
apply (rule hoare_ehoare.LAss) |
|
204 |
apply (rule eConseq1) |
|
12742 | 205 |
apply (rule hoare_ehoare.NewC) (* 32 *) |
11565 | 206 |
apply (auto dest!: new_AddrD elim: Nat_atleast_newC) |
207 |
done |
|
208 |
||
209 |
||
210 |
end |