src/HOL/IMPP/Hoare.thy
author wenzelm
Sat Sep 17 20:14:30 2005 +0200 (2005-09-17)
changeset 17477 ceb42ea2f223
parent 10834 a7897aebbffc
child 19803 aa2581752afb
permissions -rw-r--r--
converted to Isar theory format;
oheimb@8177
     1
(*  Title:      HOL/IMPP/Hoare.thy
oheimb@8177
     2
    ID:         $Id$
oheimb@8177
     3
    Author:     David von Oheimb
oheimb@8177
     4
    Copyright   1999 TUM
oheimb@8177
     5
*)
oheimb@8177
     6
wenzelm@17477
     7
header {* Inductive definition of Hoare logic for partial correctness *}
wenzelm@17477
     8
wenzelm@17477
     9
theory Hoare
wenzelm@17477
    10
imports Natural
wenzelm@17477
    11
begin
wenzelm@17477
    12
wenzelm@17477
    13
text {*
wenzelm@17477
    14
  Completeness is taken relative to completeness of the underlying logic.
wenzelm@17477
    15
wenzelm@17477
    16
  Two versions of completeness proof: nested single recursion
wenzelm@17477
    17
  vs. simultaneous recursion in call rule
wenzelm@17477
    18
*}
oheimb@8177
    19
oheimb@8177
    20
types 'a assn = "'a => state => bool"
oheimb@8177
    21
translations
wenzelm@17477
    22
  "a assn"   <= (type)"a => state => bool"
oheimb@8177
    23
oheimb@8177
    24
constdefs
oheimb@8177
    25
  state_not_singleton :: bool
wenzelm@17477
    26
  "state_not_singleton == \<exists>s t::state. s ~= t" (* at least two elements *)
oheimb@8177
    27
oheimb@8177
    28
  peek_and    :: "'a assn => (state => bool) => 'a assn" (infixr "&>" 35)
wenzelm@17477
    29
  "peek_and P p == %Z s. P Z s & p s"
oheimb@8177
    30
oheimb@8177
    31
datatype 'a triple =
wenzelm@17477
    32
  triple "'a assn"  com  "'a assn"       ("{(1_)}./ (_)/ .{(1_)}" [3,60,3] 58)
wenzelm@17477
    33
oheimb@8177
    34
consts
wenzelm@17477
    35
  triple_valid ::            "nat => 'a triple     => bool" ( "|=_:_" [0 , 58] 57)
wenzelm@17477
    36
  hoare_valids ::  "'a triple set => 'a triple set => bool" ("_||=_"  [58, 58] 57)
wenzelm@17477
    37
  hoare_derivs :: "('a triple set *  'a triple set)   set"
oheimb@8177
    38
syntax
wenzelm@17477
    39
  triples_valid::            "nat => 'a triple set => bool" ("||=_:_" [0 , 58] 57)
wenzelm@17477
    40
  hoare_valid  ::  "'a triple set => 'a triple     => bool" ("_|=_"   [58, 58] 57)
wenzelm@17477
    41
"@hoare_derivs"::  "'a triple set => 'a triple set => bool" ("_||-_"  [58, 58] 57)
wenzelm@17477
    42
"@hoare_deriv" ::  "'a triple set => 'a triple     => bool" ("_|-_"   [58, 58] 57)
oheimb@8177
    43
wenzelm@17477
    44
defs triple_valid_def: "|=n:t  ==  case t of {P}.c.{Q} =>
wenzelm@17477
    45
                                !Z s. P Z s --> (!s'. <c,s> -n-> s' --> Q Z s')"
oheimb@8177
    46
translations          "||=n:G" == "Ball G (triple_valid n)"
wenzelm@17477
    47
defs hoare_valids_def: "G||=ts   ==  !n. ||=n:G --> ||=n:ts"
oheimb@8177
    48
translations         "G |=t  " == " G||={t}"
oheimb@8177
    49
                     "G||-ts"  == "(G,ts) : hoare_derivs"
oheimb@8177
    50
                     "G |-t"   == " G||-{t}"
oheimb@8177
    51
oheimb@8177
    52
(* Most General Triples *)
wenzelm@17477
    53
constdefs MGT    :: "com => state triple"            ("{=}._.{->}" [60] 58)
oheimb@8177
    54
         "{=}.c.{->} == {%Z s0. Z = s0}. c .{%Z s1. <c,Z> -c-> s1}"
oheimb@8177
    55
wenzelm@17477
    56
inductive hoare_derivs intros
wenzelm@17477
    57
wenzelm@17477
    58
  empty:    "G||-{}"
wenzelm@17477
    59
  insert: "[| G |-t;  G||-ts |]
wenzelm@17477
    60
        ==> G||-insert t ts"
oheimb@8177
    61
wenzelm@17477
    62
  asm:      "ts <= G ==>
wenzelm@17477
    63
             G||-ts" (* {P}.BODY pn.{Q} instead of (general) t for SkipD_lemma *)
oheimb@8177
    64
wenzelm@17477
    65
  cut:   "[| G'||-ts; G||-G' |] ==> G||-ts" (* for convenience and efficiency *)
oheimb@8177
    66
wenzelm@17477
    67
  weaken: "[| G||-ts' ; ts <= ts' |] ==> G||-ts"
oheimb@8177
    68
wenzelm@17477
    69
  conseq: "!Z s. P  Z  s --> (? P' Q'. G|-{P'}.c.{Q'} &
wenzelm@17477
    70
                                   (!s'. (!Z'. P' Z' s --> Q' Z' s') --> Q Z s'))
wenzelm@17477
    71
          ==> G|-{P}.c.{Q}"
oheimb@8177
    72
oheimb@8177
    73
wenzelm@17477
    74
  Skip:  "G|-{P}. SKIP .{P}"
oheimb@8177
    75
wenzelm@17477
    76
  Ass:   "G|-{%Z s. P Z (s[X::=a s])}. X:==a .{P}"
oheimb@8177
    77
wenzelm@17477
    78
  Local: "G|-{P}. c .{%Z s. Q Z (s[Loc X::=s'<X>])}
wenzelm@17477
    79
      ==> G|-{%Z s. s'=s & P Z (s[Loc X::=a s])}. LOCAL X:=a IN c .{Q}"
oheimb@8177
    80
wenzelm@17477
    81
  Comp:  "[| G|-{P}.c.{Q};
wenzelm@17477
    82
             G|-{Q}.d.{R} |]
wenzelm@17477
    83
         ==> G|-{P}. (c;;d) .{R}"
oheimb@8177
    84
wenzelm@17477
    85
  If:    "[| G|-{P &>        b }.c.{Q};
wenzelm@17477
    86
             G|-{P &> (Not o b)}.d.{Q} |]
wenzelm@17477
    87
         ==> G|-{P}. IF b THEN c ELSE d .{Q}"
oheimb@8177
    88
wenzelm@17477
    89
  Loop:  "G|-{P &> b}.c.{P} ==>
wenzelm@17477
    90
          G|-{P}. WHILE b DO c .{P &> (Not o b)}"
oheimb@8177
    91
oheimb@8177
    92
(*
wenzelm@17477
    93
  BodyN: "(insert ({P}. BODY pn  .{Q}) G)
wenzelm@17477
    94
           |-{P}.  the (body pn) .{Q} ==>
wenzelm@17477
    95
          G|-{P}.       BODY pn  .{Q}"
oheimb@8177
    96
*)
wenzelm@17477
    97
  Body:  "[| G Un (%p. {P p}.      BODY p  .{Q p})`Procs
wenzelm@17477
    98
               ||-(%p. {P p}. the (body p) .{Q p})`Procs |]
wenzelm@17477
    99
         ==>  G||-(%p. {P p}.      BODY p  .{Q p})`Procs"
oheimb@8177
   100
wenzelm@17477
   101
  Call:     "G|-{P}. BODY pn .{%Z s. Q Z (setlocs s (getlocs s')[X::=s<Res>])}
wenzelm@17477
   102
         ==> G|-{%Z s. s'=s & P Z (setlocs s newlocs[Loc Arg::=a s])}.
wenzelm@17477
   103
             X:=CALL pn(a) .{Q}"
wenzelm@17477
   104
wenzelm@17477
   105
ML {* use_legacy_bindings (the_context ()) *}
oheimb@8177
   106
oheimb@8177
   107
end