| author | wenzelm | 
| Sun, 24 May 2020 12:38:41 +0200 | |
| changeset 71876 | ad063ac1f617 | 
| parent 69597 | ff784d5a5bfb | 
| child 71989 | bad75618fb82 | 
| permissions | -rw-r--r-- | 
| 41959 | 1  | 
(* Title: HOL/Hoare/Pointer_Examples.thy  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
2  | 
Author: Tobias Nipkow  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
3  | 
Copyright 2002 TUM  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
4  | 
|
| 41959 | 5  | 
Examples of verifications of pointer programs.  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
6  | 
*)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
7  | 
|
| 16417 | 8  | 
theory Pointer_Examples imports HeapSyntax begin  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
9  | 
|
| 24499 | 10  | 
axiomatization where unproven: "PROP A"  | 
11  | 
||
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
12  | 
section "Verifications"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
13  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
14  | 
subsection "List reversal"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
15  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
16  | 
text "A short but unreadable proof:"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
17  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
18  | 
lemma "VARS tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
19  | 
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
20  | 
WHILE p \<noteq> Null  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
21  | 
  INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
22  | 
rev ps @ qs = rev Ps @ Qs}  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
23  | 
DO r := p; p := p^.tl; r^.tl := q; q := r OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
24  | 
  {List tl q (rev Ps @ Qs)}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
25  | 
apply vcg_simp  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
26  | 
apply fastforce  | 
| 67444 | 27  | 
apply(fastforce intro:notin_List_update[THEN iffD2])  | 
28  | 
\<comment> \<open>explicit:\<close>  | 
|
29  | 
\<^cancel>\<open>apply clarify\<close>  | 
|
30  | 
\<^cancel>\<open>apply(rename_tac ps b qs)\<close>  | 
|
31  | 
\<^cancel>\<open>apply clarsimp\<close>  | 
|
32  | 
\<^cancel>\<open>apply(rename_tac ps')\<close>  | 
|
33  | 
\<^cancel>\<open>apply(fastforce intro:notin_List_update[THEN iffD2])\<close>  | 
|
34  | 
apply fastforce  | 
|
35  | 
done  | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
36  | 
|
| 69597 | 37  | 
text\<open>And now with ghost variables \<^term>\<open>ps\<close> and \<^term>\<open>qs\<close>. Even  | 
| 62042 | 38  | 
``more automatic''.\<close>  | 
| 14062 | 39  | 
|
40  | 
lemma "VARS next p ps q qs r  | 
|
41  | 
  {List next p Ps \<and> List next q Qs \<and> set Ps \<inter> set Qs = {} \<and>
 | 
|
42  | 
ps = Ps \<and> qs = Qs}  | 
|
43  | 
WHILE p \<noteq> Null  | 
|
44  | 
  INV {List next p ps \<and> List next q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
|
45  | 
rev ps @ qs = rev Ps @ Qs}  | 
|
46  | 
DO r := p; p := p^.next; r^.next := q; q := r;  | 
|
47  | 
qs := (hd ps) # qs; ps := tl ps OD  | 
|
48  | 
  {List next q (rev Ps @ Qs)}"
 | 
|
49  | 
apply vcg_simp  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
50  | 
apply fastforce  | 
| 
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
51  | 
apply fastforce  | 
| 14062 | 52  | 
done  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
53  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
54  | 
text "A longer readable version:"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
55  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
56  | 
lemma "VARS tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
57  | 
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
58  | 
WHILE p \<noteq> Null  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
59  | 
  INV {\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
60  | 
rev ps @ qs = rev Ps @ Qs}  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
61  | 
DO r := p; p := p^.tl; r^.tl := q; q := r OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
62  | 
  {List tl q (rev Ps @ Qs)}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
63  | 
proof vcg  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
64  | 
fix tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
65  | 
  assume "List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
66  | 
  thus "\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
67  | 
rev ps @ qs = rev Ps @ Qs" by fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
68  | 
next  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
69  | 
fix tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
70  | 
  assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
71  | 
rev ps @ qs = rev Ps @ Qs) \<and> p \<noteq> Null"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
72  | 
(is "(\<exists>ps qs. ?I ps qs) \<and> _")  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
73  | 
then obtain ps qs a where I: "?I ps qs \<and> p = Ref a"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
74  | 
by fast  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
75  | 
then obtain ps' where "ps = a # ps'" by fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
76  | 
hence "List (tl(p \<rightarrow> q)) (p^.tl) ps' \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
77  | 
List (tl(p \<rightarrow> q)) p (a#qs) \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
78  | 
         set ps' \<inter> set (a#qs) = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
79  | 
rev ps' @ (a#qs) = rev Ps @ Qs"  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
80  | 
using I by fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
81  | 
thus "\<exists>ps' qs'. List (tl(p \<rightarrow> q)) (p^.tl) ps' \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
82  | 
List (tl(p \<rightarrow> q)) p qs' \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
83  | 
                  set ps' \<inter> set qs' = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
84  | 
rev ps' @ qs' = rev Ps @ Qs" by fast  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
85  | 
next  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
86  | 
fix tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
87  | 
  assume "(\<exists>ps qs. List tl p ps \<and> List tl q qs \<and> set ps \<inter> set qs = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
88  | 
rev ps @ qs = rev Ps @ Qs) \<and> \<not> p \<noteq> Null"  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
89  | 
thus "List tl q (rev Ps @ Qs)" by fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
90  | 
qed  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
91  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
92  | 
|
| 62042 | 93  | 
text\<open>Finaly, the functional version. A bit more verbose, but automatic!\<close>  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
94  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
95  | 
lemma "VARS tl p q r  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
96  | 
  {islist tl p \<and> islist tl q \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
97  | 
   Ps = list tl p \<and> Qs = list tl q \<and> set Ps \<inter> set Qs = {}}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
98  | 
WHILE p \<noteq> Null  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
99  | 
  INV {islist tl p \<and> islist tl q \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
100  | 
       set(list tl p) \<inter> set(list tl q) = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
101  | 
rev(list tl p) @ (list tl q) = rev Ps @ Qs}  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
102  | 
DO r := p; p := p^.tl; r^.tl := q; q := r OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
103  | 
  {islist tl q \<and> list tl q = rev Ps @ Qs}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
104  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
105  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
106  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
107  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
108  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
109  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
110  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
111  | 
subsection "Searching in a list"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
112  | 
|
| 62042 | 113  | 
text\<open>What follows is a sequence of successively more intelligent proofs that  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
114  | 
a simple loop finds an element in a linked list.  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
115  | 
|
| 69597 | 116  | 
We start with a proof based on the \<^term>\<open>List\<close> predicate. This means it only  | 
| 62042 | 117  | 
works for acyclic lists.\<close>  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
118  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
119  | 
lemma "VARS tl p  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
120  | 
  {List tl p Ps \<and> X \<in> set Ps}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
121  | 
WHILE p \<noteq> Null \<and> p \<noteq> Ref X  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
122  | 
  INV {\<exists>ps. List tl p ps \<and> X \<in> set ps}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
123  | 
DO p := p^.tl OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
124  | 
  {p = Ref X}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
125  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
126  | 
apply blast  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
127  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
128  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
129  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
130  | 
|
| 69597 | 131  | 
text\<open>Using \<^term>\<open>Path\<close> instead of \<^term>\<open>List\<close> generalizes the correctness  | 
| 62042 | 132  | 
statement to cyclic lists as well:\<close>  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
133  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
134  | 
lemma "VARS tl p  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
135  | 
  {Path tl p Ps X}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
136  | 
WHILE p \<noteq> Null \<and> p \<noteq> X  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
137  | 
  INV {\<exists>ps. Path tl p ps X}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
138  | 
DO p := p^.tl OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
139  | 
  {p = X}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
140  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
141  | 
apply blast  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
142  | 
apply fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
143  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
144  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
145  | 
|
| 62042 | 146  | 
text\<open>Now it dawns on us that we do not need the list witness at all --- it  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
147  | 
suffices to talk about reachability, i.e.\ we can use relations directly. The  | 
| 69597 | 148  | 
first version uses a relation on \<^typ>\<open>'a ref\<close>:\<close>  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
149  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
150  | 
lemma "VARS tl p  | 
| 67613 | 151  | 
  {(p,X) \<in> {(Ref x,tl x) |x. True}\<^sup>*}
 | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
152  | 
WHILE p \<noteq> Null \<and> p \<noteq> X  | 
| 67613 | 153  | 
  INV {(p,X) \<in> {(Ref x,tl x) |x. True}\<^sup>*}
 | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
154  | 
DO p := p^.tl OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
155  | 
  {p = X}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
156  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
157  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
158  | 
apply(erule converse_rtranclE)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
159  | 
apply simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
160  | 
apply(clarsimp elim:converse_rtranclE)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
161  | 
apply(fast elim:converse_rtranclE)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
162  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
163  | 
|
| 69597 | 164  | 
text\<open>Finally, a version based on a relation on type \<^typ>\<open>'a\<close>:\<close>  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
165  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
166  | 
lemma "VARS tl p  | 
| 67613 | 167  | 
  {p \<noteq> Null \<and> (addr p,X) \<in> {(x,y). tl x = Ref y}\<^sup>*}
 | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
168  | 
WHILE p \<noteq> Null \<and> p \<noteq> Ref X  | 
| 67613 | 169  | 
  INV {p \<noteq> Null \<and> (addr p,X) \<in> {(x,y). tl x = Ref y}\<^sup>*}
 | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
170  | 
DO p := p^.tl OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
171  | 
  {p = Ref X}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
172  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
173  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
174  | 
apply(erule converse_rtranclE)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
175  | 
apply simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
176  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
177  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
178  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
179  | 
|
| 19397 | 180  | 
subsection "Splicing two lists"  | 
181  | 
||
182  | 
lemma "VARS tl p q pp qq  | 
|
183  | 
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {} \<and> size Qs \<le> size Ps}
 | 
|
184  | 
pp := p;  | 
|
185  | 
WHILE q \<noteq> Null  | 
|
186  | 
  INV {\<exists>as bs qs.
 | 
|
187  | 
distinct as \<and> Path tl p as pp \<and> List tl pp bs \<and> List tl q qs \<and>  | 
|
188  | 
    set bs \<inter> set qs = {} \<and> set as \<inter> (set bs \<union> set qs) = {} \<and>
 | 
|
189  | 
size qs \<le> size bs \<and> splice Ps Qs = as @ splice bs qs}  | 
|
190  | 
DO qq := q^.tl; q^.tl := pp^.tl; pp^.tl := q; pp := q^.tl; q := qq OD  | 
|
191  | 
  {List tl p (splice Ps Qs)}"
 | 
|
192  | 
apply vcg_simp  | 
|
193  | 
apply(rule_tac x = "[]" in exI)  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
194  | 
apply fastforce  | 
| 19397 | 195  | 
apply clarsimp  | 
196  | 
apply(rename_tac y bs qqs)  | 
|
197  | 
apply(case_tac bs) apply simp  | 
|
198  | 
apply clarsimp  | 
|
199  | 
apply(rename_tac x bbs)  | 
|
200  | 
apply(rule_tac x = "as @ [x,y]" in exI)  | 
|
201  | 
apply simp  | 
|
202  | 
apply(rule_tac x = "bbs" in exI)  | 
|
203  | 
apply simp  | 
|
204  | 
apply(rule_tac x = "qqs" in exI)  | 
|
205  | 
apply simp  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
206  | 
apply (fastforce simp:List_app)  | 
| 19397 | 207  | 
done  | 
208  | 
||
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
209  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
210  | 
subsection "Merging two lists"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
211  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
212  | 
text"This is still a bit rough, especially the proof."  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
213  | 
|
| 38353 | 214  | 
definition cor :: "bool \<Rightarrow> bool \<Rightarrow> bool"  | 
215  | 
where "cor P Q \<longleftrightarrow> (if P then True else Q)"  | 
|
| 
35416
 
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
 
haftmann 
parents: 
35316 
diff
changeset
 | 
216  | 
|
| 38353 | 217  | 
definition cand :: "bool \<Rightarrow> bool \<Rightarrow> bool"  | 
218  | 
where "cand P Q \<longleftrightarrow> (if P then Q else False)"  | 
|
| 13773 | 219  | 
|
| 38353 | 220  | 
fun merge :: "'a list * 'a list * ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list"
 | 
221  | 
where  | 
|
222  | 
"merge(x#xs,y#ys,f) = (if f x y then x # merge(xs,y#ys,f)  | 
|
223  | 
else y # merge(x#xs,ys,f))"  | 
|
224  | 
| "merge(x#xs,[],f) = x # merge(xs,[],f)"  | 
|
225  | 
| "merge([],y#ys,f) = y # merge([],ys,f)"  | 
|
226  | 
| "merge([],[],f) = []"  | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
227  | 
|
| 62042 | 228  | 
text\<open>Simplifies the proof a little:\<close>  | 
| 13773 | 229  | 
|
230  | 
lemma [simp]: "({} = insert a A \<inter> B) = (a \<notin> B & {} = A \<inter> B)"
 | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
231  | 
by blast  | 
| 13773 | 232  | 
lemma [simp]: "({} = A \<inter> insert b B) = (b \<notin> A & {} = A \<inter> B)"
 | 
233  | 
by blast  | 
|
234  | 
lemma [simp]: "({} = A \<inter> (B \<union> C)) = ({} = A \<inter> B & {} = A \<inter> C)"
 | 
|
235  | 
by blast  | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
236  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
237  | 
lemma "VARS hd tl p q r s  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
238  | 
 {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {} \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
239  | 
(p \<noteq> Null \<or> q \<noteq> Null)}  | 
| 13773 | 240  | 
IF cor (q = Null) (cand (p \<noteq> Null) (p^.hd \<le> q^.hd))  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
241  | 
THEN r := p; p := p^.tl ELSE r := q; q := q^.tl FI;  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
242  | 
s := r;  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
243  | 
WHILE p \<noteq> Null \<or> q \<noteq> Null  | 
| 67613 | 244  | 
 INV {\<exists>rs ps qs a. Path tl r rs s \<and> List tl p ps \<and> List tl q qs \<and>
 | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
245  | 
distinct(a # ps @ qs @ rs) \<and> s = Ref a \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
246  | 
merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y) =  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
247  | 
rs @ a # merge(ps,qs,\<lambda>x y. hd x \<le> hd y) \<and>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
248  | 
(tl a = p \<or> tl a = q)}  | 
| 13773 | 249  | 
DO IF cor (q = Null) (cand (p \<noteq> Null) (p^.hd \<le> q^.hd))  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
250  | 
THEN s^.tl := p; p := p^.tl ELSE s^.tl := q; q := q^.tl FI;  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
251  | 
s := s^.tl  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
252  | 
OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
253  | 
 {List tl r (merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y))}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
254  | 
apply vcg_simp  | 
| 13773 | 255  | 
apply (simp_all add: cand_def cor_def)  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
256  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
257  | 
apply (fastforce)  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
258  | 
|
| 13773 | 259  | 
apply clarsimp  | 
260  | 
apply(rule conjI)  | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
261  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
262  | 
apply(rule conjI)  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
263  | 
apply (fastforce intro!:Path_snoc intro:Path_upd[THEN iffD2] notin_List_update[THEN iffD2] simp:eq_sym_conv)  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
264  | 
apply clarsimp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
265  | 
apply(rule conjI)  | 
| 13773 | 266  | 
apply (clarsimp)  | 
267  | 
apply(rule_tac x = "rs @ [a]" in exI)  | 
|
268  | 
apply(clarsimp simp:eq_sym_conv)  | 
|
269  | 
apply(rule_tac x = "bs" in exI)  | 
|
270  | 
apply(clarsimp simp:eq_sym_conv)  | 
|
271  | 
apply(rule_tac x = "ya#bsa" in exI)  | 
|
272  | 
apply(simp)  | 
|
273  | 
apply(clarsimp simp:eq_sym_conv)  | 
|
274  | 
apply(rule_tac x = "rs @ [a]" in exI)  | 
|
275  | 
apply(clarsimp simp:eq_sym_conv)  | 
|
276  | 
apply(rule_tac x = "y#bs" in exI)  | 
|
277  | 
apply(clarsimp simp:eq_sym_conv)  | 
|
278  | 
apply(rule_tac x = "bsa" in exI)  | 
|
279  | 
apply(simp)  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
280  | 
apply (fastforce intro!:Path_snoc intro:Path_upd[THEN iffD2] notin_List_update[THEN iffD2] simp:eq_sym_conv)  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
281  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
282  | 
apply(clarsimp simp add:List_app)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
283  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
284  | 
|
| 62042 | 285  | 
text\<open>And now with ghost variables:\<close>  | 
| 13773 | 286  | 
|
| 14074 | 287  | 
lemma "VARS elem next p q r s ps qs rs a  | 
288  | 
 {List next p Ps \<and> List next q Qs \<and> set Ps \<inter> set Qs = {} \<and>
 | 
|
289  | 
(p \<noteq> Null \<or> q \<noteq> Null) \<and> ps = Ps \<and> qs = Qs}  | 
|
290  | 
IF cor (q = Null) (cand (p \<noteq> Null) (p^.elem \<le> q^.elem))  | 
|
291  | 
THEN r := p; p := p^.next; ps := tl ps  | 
|
292  | 
ELSE r := q; q := q^.next; qs := tl qs FI;  | 
|
293  | 
s := r; rs := []; a := addr s;  | 
|
294  | 
WHILE p \<noteq> Null \<or> q \<noteq> Null  | 
|
295  | 
 INV {Path next r rs s \<and> List next p ps \<and> List next q qs \<and>
 | 
|
296  | 
distinct(a # ps @ qs @ rs) \<and> s = Ref a \<and>  | 
|
297  | 
merge(Ps,Qs,\<lambda>x y. elem x \<le> elem y) =  | 
|
298  | 
rs @ a # merge(ps,qs,\<lambda>x y. elem x \<le> elem y) \<and>  | 
|
299  | 
(next a = p \<or> next a = q)}  | 
|
300  | 
DO IF cor (q = Null) (cand (p \<noteq> Null) (p^.elem \<le> q^.elem))  | 
|
301  | 
THEN s^.next := p; p := p^.next; ps := tl ps  | 
|
302  | 
ELSE s^.next := q; q := q^.next; qs := tl qs FI;  | 
|
303  | 
rs := rs @ [a]; s := s^.next; a := addr s  | 
|
304  | 
OD  | 
|
305  | 
 {List next r (merge(Ps,Qs,\<lambda>x y. elem x \<le> elem y))}"
 | 
|
306  | 
apply vcg_simp  | 
|
307  | 
apply (simp_all add: cand_def cor_def)  | 
|
308  | 
||
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
309  | 
apply (fastforce)  | 
| 14074 | 310  | 
|
311  | 
apply clarsimp  | 
|
312  | 
apply(rule conjI)  | 
|
313  | 
apply(clarsimp)  | 
|
314  | 
apply(rule conjI)  | 
|
315  | 
apply(clarsimp simp:neq_commute)  | 
|
316  | 
apply(clarsimp simp:neq_commute)  | 
|
317  | 
apply(clarsimp simp:neq_commute)  | 
|
318  | 
||
319  | 
apply(clarsimp simp add:List_app)  | 
|
320  | 
done  | 
|
321  | 
||
| 62042 | 322  | 
text\<open>The proof is a LOT simpler because it does not need  | 
| 14074 | 323  | 
instantiations anymore, but it is still not quite automatic, probably  | 
| 62042 | 324  | 
because of this wrong orientation business.\<close>  | 
| 14074 | 325  | 
|
| 62042 | 326  | 
text\<open>More of the previous proof without ghost variables can be  | 
| 14074 | 327  | 
automated, but the runtime goes up drastically. In general it is  | 
328  | 
usually more efficient to give the witness directly than to have it  | 
|
329  | 
found by proof.  | 
|
| 13773 | 330  | 
|
| 69597 | 331  | 
Now we try a functional version of the abstraction relation \<^term>\<open>Path\<close>. Since the result is not that convincing, we do not prove any of  | 
| 62042 | 332  | 
the lemmas.\<close>  | 
| 13773 | 333  | 
|
| 38353 | 334  | 
axiomatization  | 
335  | 
  ispath :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a ref \<Rightarrow> bool" and
 | 
|
336  | 
  path :: "('a \<Rightarrow> 'a ref) \<Rightarrow> 'a ref \<Rightarrow> 'a ref \<Rightarrow> 'a list"
 | 
|
| 13773 | 337  | 
|
338  | 
text"First some basic lemmas:"  | 
|
339  | 
||
340  | 
lemma [simp]: "ispath f p p"  | 
|
| 24499 | 341  | 
by (rule unproven)  | 
| 13773 | 342  | 
lemma [simp]: "path f p p = []"  | 
| 24499 | 343  | 
by (rule unproven)  | 
| 13773 | 344  | 
lemma [simp]: "ispath f p q \<Longrightarrow> a \<notin> set(path f p q) \<Longrightarrow> ispath (f(a := r)) p q"  | 
| 24499 | 345  | 
by (rule unproven)  | 
| 13773 | 346  | 
lemma [simp]: "ispath f p q \<Longrightarrow> a \<notin> set(path f p q) \<Longrightarrow>  | 
347  | 
path (f(a := r)) p q = path f p q"  | 
|
| 24499 | 348  | 
by (rule unproven)  | 
| 13773 | 349  | 
|
350  | 
text"Some more specific lemmas needed by the example:"  | 
|
351  | 
||
352  | 
lemma [simp]: "ispath (f(a := q)) p (Ref a) \<Longrightarrow> ispath (f(a := q)) p q"  | 
|
| 24499 | 353  | 
by (rule unproven)  | 
| 13773 | 354  | 
lemma [simp]: "ispath (f(a := q)) p (Ref a) \<Longrightarrow>  | 
355  | 
path (f(a := q)) p q = path (f(a := q)) p (Ref a) @ [a]"  | 
|
| 24499 | 356  | 
by (rule unproven)  | 
| 13773 | 357  | 
lemma [simp]: "ispath f p (Ref a) \<Longrightarrow> f a = Ref b \<Longrightarrow>  | 
358  | 
b \<notin> set (path f p (Ref a))"  | 
|
| 24499 | 359  | 
by (rule unproven)  | 
| 13773 | 360  | 
lemma [simp]: "ispath f p (Ref a) \<Longrightarrow> f a = Null \<Longrightarrow> islist f p"  | 
| 24499 | 361  | 
by (rule unproven)  | 
| 13773 | 362  | 
lemma [simp]: "ispath f p (Ref a) \<Longrightarrow> f a = Null \<Longrightarrow> list f p = path f p (Ref a) @ [a]"  | 
| 24499 | 363  | 
by (rule unproven)  | 
| 13773 | 364  | 
|
365  | 
lemma [simp]: "islist f p \<Longrightarrow> distinct (list f p)"  | 
|
| 24499 | 366  | 
by (rule unproven)  | 
| 13773 | 367  | 
|
368  | 
lemma "VARS hd tl p q r s  | 
|
| 67613 | 369  | 
 {islist tl p \<and> Ps = list tl p \<and> islist tl q \<and> Qs = list tl q \<and>
 | 
| 13773 | 370  | 
  set Ps \<inter> set Qs = {} \<and>
 | 
371  | 
(p \<noteq> Null \<or> q \<noteq> Null)}  | 
|
372  | 
IF cor (q = Null) (cand (p \<noteq> Null) (p^.hd \<le> q^.hd))  | 
|
373  | 
THEN r := p; p := p^.tl ELSE r := q; q := q^.tl FI;  | 
|
374  | 
s := r;  | 
|
375  | 
WHILE p \<noteq> Null \<or> q \<noteq> Null  | 
|
| 67613 | 376  | 
 INV {\<exists>rs ps qs a. ispath tl r s \<and> rs = path tl r s \<and>
 | 
377  | 
islist tl p \<and> ps = list tl p \<and> islist tl q \<and> qs = list tl q \<and>  | 
|
| 13773 | 378  | 
distinct(a # ps @ qs @ rs) \<and> s = Ref a \<and>  | 
379  | 
merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y) =  | 
|
380  | 
rs @ a # merge(ps,qs,\<lambda>x y. hd x \<le> hd y) \<and>  | 
|
381  | 
(tl a = p \<or> tl a = q)}  | 
|
382  | 
DO IF cor (q = Null) (cand (p \<noteq> Null) (p^.hd \<le> q^.hd))  | 
|
383  | 
THEN s^.tl := p; p := p^.tl ELSE s^.tl := q; q := q^.tl FI;  | 
|
384  | 
s := s^.tl  | 
|
385  | 
OD  | 
|
386  | 
 {islist tl r & list tl r = (merge(Ps,Qs,\<lambda>x y. hd x \<le> hd y))}"
 | 
|
387  | 
apply vcg_simp  | 
|
388  | 
||
389  | 
apply (simp_all add: cand_def cor_def)  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
390  | 
apply (fastforce)  | 
| 
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
391  | 
apply (fastforce simp: eq_sym_conv)  | 
| 13773 | 392  | 
apply(clarsimp)  | 
393  | 
done  | 
|
394  | 
||
395  | 
text"The proof is automatic, but requires a numbet of special lemmas."  | 
|
396  | 
||
| 19399 | 397  | 
|
398  | 
subsection "Cyclic list reversal"  | 
|
399  | 
||
400  | 
||
| 62042 | 401  | 
text\<open>We consider two algorithms for the reversal of circular lists.  | 
402  | 
\<close>  | 
|
| 19399 | 403  | 
|
404  | 
lemma circular_list_rev_I:  | 
|
405  | 
"VARS next root p q tmp  | 
|
406  | 
  {root = Ref r \<and> distPath next root (r#Ps) root}
 | 
|
407  | 
p := root; q := root^.next;  | 
|
408  | 
WHILE q \<noteq> root  | 
|
409  | 
  INV {\<exists> ps qs. distPath next p ps root \<and> distPath next q qs root \<and> 
 | 
|
410  | 
             root = Ref r \<and> r \<notin> set Ps  \<and> set ps \<inter> set qs = {} \<and> 
 | 
|
411  | 
Ps = (rev ps) @ qs }  | 
|
412  | 
DO tmp := q; q := q^.next; tmp^.next := p; p:=tmp OD;  | 
|
413  | 
root^.next := p  | 
|
414  | 
  { root = Ref r \<and> distPath next root (r#rev Ps) root}"
 | 
|
415  | 
apply (simp only:distPath_def)  | 
|
416  | 
apply vcg_simp  | 
|
417  | 
apply (rule_tac x="[]" in exI)  | 
|
418  | 
apply auto  | 
|
419  | 
apply (drule (2) neq_dP)  | 
|
420  | 
apply clarsimp  | 
|
421  | 
apply(rule_tac x="a # ps" in exI)  | 
|
422  | 
apply clarsimp  | 
|
423  | 
done  | 
|
424  | 
||
| 69597 | 425  | 
text\<open>In the beginning, we are able to assert \<^term>\<open>distPath next  | 
426  | 
root as root\<close>, with \<^term>\<open>as\<close> set to \<^term>\<open>[]\<close> or  | 
|
427  | 
\<^term>\<open>[r,a,b,c]\<close>. Note that \<^term>\<open>Path next root as root\<close> would  | 
|
| 19399 | 428  | 
additionally give us an infinite number of lists with the recurring  | 
| 69597 | 429  | 
sequence \<^term>\<open>[r,a,b,c]\<close>.  | 
| 19399 | 430  | 
|
431  | 
The precondition states that there exists a non-empty non-repeating  | 
|
| 69597 | 432  | 
path \mbox{\<^term>\<open>r # Ps\<close>} from pointer \<^term>\<open>root\<close> to itself, given that
 | 
433  | 
\<^term>\<open>root\<close> points to location \<^term>\<open>r\<close>. Pointers \<^term>\<open>p\<close> and  | 
|
434  | 
\<^term>\<open>q\<close> are then set to \<^term>\<open>root\<close> and the successor of \<^term>\<open>root\<close> respectively. If \<^term>\<open>q = root\<close>, we have circled the loop,  | 
|
435  | 
otherwise we set the \<^term>\<open>next\<close> pointer field of \<^term>\<open>q\<close> to point  | 
|
436  | 
to \<^term>\<open>p\<close>, and shift \<^term>\<open>p\<close> and \<^term>\<open>q\<close> one step forward. The  | 
|
437  | 
invariant thus states that \<^term>\<open>p\<close> and \<^term>\<open>q\<close> point to two  | 
|
438  | 
disjoint lists \<^term>\<open>ps\<close> and \<^term>\<open>qs\<close>, such that \<^term>\<open>Ps = rev ps  | 
|
439  | 
@ qs\<close>. After the loop terminates, one  | 
|
| 19399 | 440  | 
extra step is needed to close the loop. As expected, the postcondition  | 
| 69597 | 441  | 
states that the \<^term>\<open>distPath\<close> from \<^term>\<open>root\<close> to itself is now  | 
442  | 
\<^term>\<open>r # (rev Ps)\<close>.  | 
|
| 19399 | 443  | 
|
444  | 
It may come as a surprise to the reader that the simple algorithm for  | 
|
445  | 
acyclic list reversal, with modified annotations, works for cyclic  | 
|
| 62042 | 446  | 
lists as well:\<close>  | 
| 19399 | 447  | 
|
448  | 
||
449  | 
lemma circular_list_rev_II:  | 
|
450  | 
"VARS next p q tmp  | 
|
451  | 
{p = Ref r \<and> distPath next p (r#Ps) p}
 | 
|
452  | 
q:=Null;  | 
|
453  | 
WHILE p \<noteq> Null  | 
|
454  | 
INV  | 
|
455  | 
{ ((q = Null) \<longrightarrow> (\<exists>ps. distPath next p (ps) (Ref r) \<and> ps = r#Ps)) \<and>
 | 
|
456  | 
((q \<noteq> Null) \<longrightarrow> (\<exists>ps qs. distPath next q (qs) (Ref r) \<and> List next p ps \<and>  | 
|
457  | 
                   set ps \<inter> set qs = {} \<and> rev qs @ ps = Ps@[r])) \<and>
 | 
|
458  | 
\<not> (p = Null \<and> q = Null) }  | 
|
459  | 
DO tmp := p; p := p^.next; tmp^.next := q; q:=tmp OD  | 
|
460  | 
{q = Ref r \<and> distPath next q (r # rev Ps) q}"
 | 
|
461  | 
apply (simp only:distPath_def)  | 
|
462  | 
apply vcg_simp  | 
|
463  | 
apply clarsimp  | 
|
464  | 
apply clarsimp  | 
|
465  | 
apply (case_tac "(q = Null)")  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
466  | 
apply (fastforce intro: Path_is_List)  | 
| 19399 | 467  | 
apply clarsimp  | 
468  | 
apply (rule_tac x= "bs" in exI)  | 
|
469  | 
apply (rule_tac x= "y # qs" in exI)  | 
|
470  | 
apply clarsimp  | 
|
471  | 
apply (auto simp:fun_upd_apply)  | 
|
472  | 
done  | 
|
473  | 
||
474  | 
||
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
475  | 
subsection "Storage allocation"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
476  | 
|
| 38353 | 477  | 
definition new :: "'a set \<Rightarrow> 'a"  | 
478  | 
where "new A = (SOME a. a \<notin> A)"  | 
|
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
479  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
480  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
481  | 
lemma new_notin:  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
482  | 
"\<lbrakk> ~finite(UNIV::'a set); finite(A::'a set); B \<subseteq> A \<rbrakk> \<Longrightarrow> new (A) \<notin> B"  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
483  | 
apply(unfold new_def)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
484  | 
apply(rule someI2_ex)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
485  | 
apply (fast intro:ex_new_if_finite)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
486  | 
apply (fast)  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
487  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
488  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
489  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
490  | 
lemma "~finite(UNIV::'a set) \<Longrightarrow>  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
491  | 
VARS xs elem next alloc p q  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
492  | 
  {Xs = xs \<and> p = (Null::'a ref)}
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
493  | 
WHILE xs \<noteq> []  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
494  | 
  INV {islist next p \<and> set(list next p) \<subseteq> set alloc \<and>
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
495  | 
map elem (rev(list next p)) @ xs = Xs}  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
496  | 
DO q := Ref(new(set alloc)); alloc := (addr q)#alloc;  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
497  | 
q^.next := p; q^.elem := hd xs; xs := tl xs; p := q  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
498  | 
OD  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
499  | 
  {islist next p \<and> map elem (rev(list next p)) = Xs}"
 | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
500  | 
apply vcg_simp  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
501  | 
apply (clarsimp simp: subset_insert_iff neq_Nil_conv fun_upd_apply new_notin)  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
41959 
diff
changeset
 | 
502  | 
apply fastforce  | 
| 
13772
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
503  | 
done  | 
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
504  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
505  | 
|
| 
 
73d041cc6a66
Split Pointers.thy and automated one proof, which caused the runtime to explode
 
nipkow 
parents:  
diff
changeset
 | 
506  | 
end  |