src/HOL/Hoare/Pointers.thy
author kleing
Wed, 19 Jun 2002 12:39:41 +0200
changeset 13224 6f0928a942d1
parent 13133 03d20664cb79
child 13287 e4134f9eb4dc
permissions -rw-r--r--
LBV instantiantion refactored, streamlined
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13133
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     1
(*  Title:      HOL/Hoare/Pointers.thy
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     2
    ID:         $Id$
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     4
    Copyright   2002 TUM
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     5
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     6
How to use Hoare logic to verify pointer manipulating programs.
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     7
The old idea: the store is a global mapping from pointers to values.
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     8
Pointers are modelled by type 'a option, where None is Nil.
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
     9
Thus the heap is of type 'a \<leadsto> 'a (see theory Map).
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    10
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    11
The List reversal example is taken from Richard Bornat's paper
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    12
Proving pointer programs in Hoare logic
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    13
What's new? We formalize the foundations, ie the abstraction from the pointer
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    14
chains to HOL lists. This is merely axiomatized by Bornat.
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    15
*)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    16
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    17
theory Pointers = Hoare:
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    18
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    19
section"The heap"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    20
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    21
subsection"Paths in the heap"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    22
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    23
consts
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    24
 path :: "('a \<leadsto> 'a) \<Rightarrow> 'a option \<Rightarrow> 'a list \<Rightarrow> 'a option \<Rightarrow> bool"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    25
primrec
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    26
"path h x [] y = (x = y)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    27
"path h x (a#as) y = (x = Some a \<and> path h (h a) as y)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    28
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    29
(* useful?
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    30
lemma [simp]: "!x. reach h x (as @ [a]) (h a) = reach h x as (Some a)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    31
apply(induct_tac as)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    32
apply(clarsimp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    33
apply(clarsimp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    34
done
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    35
*)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    36
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    37
subsection "Lists on the heap"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    38
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    39
constdefs
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    40
 list :: "('a \<leadsto> 'a) \<Rightarrow> 'a option \<Rightarrow> 'a list \<Rightarrow> bool"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    41
"list h x as == path h x as None"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    42
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    43
lemma [simp]: "list h x [] = (x = None)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    44
by(simp add:list_def)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    45
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    46
lemma [simp]: "list h x (a#as) = (x = Some a \<and> list h (h a) as)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    47
by(simp add:list_def)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    48
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    49
lemma [simp]: "list h None as = (as = [])"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    50
by(case_tac as, simp_all)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    51
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    52
lemma [simp]: "list h (Some a) as = (\<exists>bs. as = a#bs \<and> list h (h a) bs)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    53
by(case_tac as, simp_all, fast)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    54
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    55
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    56
declare fun_upd_apply[simp del]fun_upd_same[simp] fun_upd_other[simp]
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    57
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    58
lemma list_unique: "\<And>x bs. list h x as \<Longrightarrow> list h x bs \<Longrightarrow> as = bs"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    59
by(induct as, simp, clarsimp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    60
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    61
lemma list_app: "\<And>x. list h x (as@bs) = (\<exists>y. path h x as y \<and> list h y bs)"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    62
by(induct as, simp, clarsimp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    63
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    64
lemma list_hd_not_in_tl: "list h (h a) as \<Longrightarrow> a \<notin> set as"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    65
apply (clarsimp simp add:in_set_conv_decomp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    66
apply(frule list_app[THEN iffD1])
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    67
apply(fastsimp dest:list_app[THEN iffD1] list_unique)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    68
done
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    69
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    70
lemma list_distinct: "\<And>x. list h x as \<Longrightarrow> distinct as"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    71
apply(induct as, simp)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    72
apply(fastsimp dest:list_hd_not_in_tl)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    73
done
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    74
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    75
theorem notin_list_update[rule_format,simp]:
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    76
 "\<forall>x. a \<notin> set as \<longrightarrow> list (h(a := y)) x as = list h x as"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    77
apply(induct_tac as)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    78
apply simp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    79
apply(simp add:fun_upd_apply)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    80
done
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    81
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    82
lemma [simp]: "list h (h a) as \<Longrightarrow> list (h(a := y)) (h a) as"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    83
by(simp add:list_hd_not_in_tl)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    84
(* Note that the opposite direction does NOT hold:
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    85
   If    h = (a \<mapsto> Some a)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    86
   then  list (h(a := None)) (h a) [a]
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    87
   but   not list h (h a) [] (because h is cyclic)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    88
*)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    89
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    90
section"Hoare logic"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    91
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    92
(* This should already be done in Hoare.thy, which should be converted to
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    93
Isar *)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    94
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    95
method_setup vcg_simp_tac = {*
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    96
  Method.no_args
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    97
    (Method.SIMPLE_METHOD' HEADGOAL (hoare_tac Asm_full_simp_tac)) *}
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    98
  "verification condition generator plus simplification"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
    99
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   100
subsection"List reversal"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   101
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   102
lemma "|- VARS tl p q r. 
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   103
  {list tl p As \<and> list tl q Bs \<and> set As \<inter> set Bs = {}}
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   104
  WHILE p ~= None
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   105
  INV {\<exists>As' Bs'. list tl p As' \<and> list tl q Bs' \<and> set As' \<inter> set Bs' = {} \<and>
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   106
                 rev As' @ Bs' = rev As @ Bs}
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   107
  DO r := p; p := tl(the p); tl := tl(the r := q); q := r OD
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   108
  {list tl q (rev As @ Bs)}"
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   109
apply vcg_simp_tac
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   110
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   111
apply(rule_tac x = As in exI)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   112
apply simp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   113
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   114
prefer 2
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   115
apply clarsimp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   116
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   117
apply clarify
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   118
apply(rename_tac As' b Bs')
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   119
apply(frule list_distinct)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   120
apply clarsimp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   121
apply(rename_tac As'')
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   122
apply(rule_tac x = As'' in exI)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   123
apply simp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   124
apply(rule_tac x = "b#Bs'" in exI)
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   125
apply simp
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   126
done
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   127
03d20664cb79 A new theory of pointer program examples
nipkow
parents:
diff changeset
   128
end