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