src/HOL/Hoare/hoareAbort.ML
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 15531 08c8dad8e399
child 15661 9ef583b08647
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
nipkow@13857
     1
(*  Title:      HOL/Hoare/Hoare.ML
nipkow@13857
     2
    ID:         $Id$
nipkow@13857
     3
    Author:     Leonor Prensa Nieto & Tobias Nipkow
nipkow@13857
     4
    Copyright   1998 TUM
nipkow@13857
     5
nipkow@13857
     6
Derivation of the proof rules and, most importantly, the VCG tactic.
nipkow@13857
     7
*)
nipkow@13857
     8
nipkow@13857
     9
val SkipRule = thm"SkipRule";
nipkow@13857
    10
val BasicRule = thm"BasicRule";
nipkow@13857
    11
val AbortRule = thm"AbortRule";
nipkow@13857
    12
val SeqRule = thm"SeqRule";
nipkow@13857
    13
val CondRule = thm"CondRule";
nipkow@13857
    14
val WhileRule = thm"WhileRule";
nipkow@13857
    15
nipkow@13857
    16
(*** The tactics ***)
nipkow@13857
    17
nipkow@13857
    18
(*****************************************************************************)
nipkow@13857
    19
(** The function Mset makes the theorem                                     **)
nipkow@13857
    20
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
nipkow@13857
    21
(** where (x1,...,xn) are the variables of the particular program we are    **)
nipkow@13857
    22
(** working on at the moment of the call                                    **)
nipkow@13857
    23
(*****************************************************************************)
nipkow@13857
    24
nipkow@13857
    25
local open HOLogic in
nipkow@13857
    26
nipkow@13857
    27
(** maps (%x1 ... xn. t) to [x1,...,xn] **)
nipkow@13857
    28
fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t
nipkow@13857
    29
  | abs2list (Abs(x,T,t)) = [Free (x, T)]
nipkow@13857
    30
  | abs2list _ = [];
nipkow@13857
    31
nipkow@13857
    32
(** maps {(x1,...,xn). t} to [x1,...,xn] **)
nipkow@13857
    33
fun mk_vars (Const ("Collect",_) $ T) = abs2list T
nipkow@13857
    34
  | mk_vars _ = [];
nipkow@13857
    35
nipkow@13857
    36
(** abstraction of body over a tuple formed from a list of free variables. 
nipkow@13857
    37
Types are also built **)
nipkow@13857
    38
fun mk_abstupleC []     body = absfree ("x", unitT, body)
nipkow@13857
    39
  | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
nipkow@13857
    40
                               in if w=[] then absfree (n, T, body)
nipkow@13857
    41
        else let val z  = mk_abstupleC w body;
nipkow@13857
    42
                 val T2 = case z of Abs(_,T,_) => T
nipkow@13857
    43
                        | Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T;
nipkow@13857
    44
       in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT) 
nipkow@13857
    45
          $ absfree (n, T, z) end end;
nipkow@13857
    46
nipkow@13857
    47
(** maps [x1,...,xn] to (x1,...,xn) and types**)
nipkow@13857
    48
fun mk_bodyC []      = HOLogic.unit
nipkow@13857
    49
  | mk_bodyC (x::xs) = if xs=[] then x 
nipkow@13857
    50
               else let val (n, T) = dest_Free x ;
nipkow@13857
    51
                        val z = mk_bodyC xs;
nipkow@13857
    52
                        val T2 = case z of Free(_, T) => T
nipkow@13857
    53
                                         | Const ("Pair", Type ("fun", [_, Type
nipkow@13857
    54
                                            ("fun", [_, T])])) $ _ $ _ => T;
nipkow@13857
    55
                 in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) $ x $ z end;
nipkow@13857
    56
nipkow@13857
    57
fun dest_Goal (Const ("Goal", _) $ P) = P;
nipkow@13857
    58
nipkow@13857
    59
(** maps a goal of the form:
nipkow@13857
    60
        1. [| P |] ==> VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**) 
nipkow@13857
    61
fun get_vars thm = let  val c = dest_Goal (concl_of (thm));
nipkow@13857
    62
                        val d = Logic.strip_assums_concl c;
nipkow@13857
    63
                        val Const _ $ pre $ _ $ _ = dest_Trueprop d;
nipkow@13857
    64
      in mk_vars pre end;
nipkow@13857
    65
nipkow@13857
    66
nipkow@13857
    67
(** Makes Collect with type **)
nipkow@13857
    68
fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm 
nipkow@13857
    69
                      in Collect_const t $ trm end;
nipkow@13857
    70
nipkow@13857
    71
fun inclt ty = Const ("op <=", [ty,ty] ---> boolT);
nipkow@13857
    72
nipkow@13857
    73
(** Makes "Mset <= t" **)
nipkow@13857
    74
fun Mset_incl t = let val MsetT = fastype_of t 
nipkow@13857
    75
                 in mk_Trueprop ((inclt MsetT) $ Free ("Mset", MsetT) $ t) end;
nipkow@13857
    76
nipkow@13857
    77
nipkow@13857
    78
fun Mset thm = let val vars = get_vars(thm);
nipkow@13857
    79
                   val varsT = fastype_of (mk_bodyC vars);
nipkow@13857
    80
                   val big_Collect = mk_CollectC (mk_abstupleC vars 
nipkow@13857
    81
                         (Free ("P",varsT --> boolT) $ mk_bodyC vars));
nipkow@13857
    82
                   val small_Collect = mk_CollectC (Abs("x",varsT,
nipkow@13857
    83
                           Free ("P",varsT --> boolT) $ Bound 0));
nipkow@13857
    84
                   val impl = implies $ (Mset_incl big_Collect) $ 
nipkow@13857
    85
                                          (Mset_incl small_Collect);
nipkow@13857
    86
   in Tactic.prove (Thm.sign_of_thm thm) ["Mset", "P"] [] impl (K (CLASET' blast_tac 1)) end;
nipkow@13857
    87
nipkow@13857
    88
end;
nipkow@13857
    89
nipkow@13857
    90
nipkow@13857
    91
(*****************************************************************************)
nipkow@13857
    92
(** Simplifying:                                                            **)
skalberg@15531
    93
(** SOME useful lemmata, lists and simplification tactics to control which  **)
nipkow@13857
    94
(** theorems are used to simplify at each moment, so that the original      **)
nipkow@13857
    95
(** input does not suffer any unexpected transformation                     **)
nipkow@13857
    96
(*****************************************************************************)
nipkow@13857
    97
nipkow@13857
    98
Goal "-(Collect b) = {x. ~(b x)}";
nipkow@13857
    99
by (Fast_tac 1);
nipkow@13857
   100
qed "Compl_Collect";
nipkow@13857
   101
nipkow@13857
   102
nipkow@13857
   103
(**Simp_tacs**)
nipkow@13857
   104
nipkow@13857
   105
val before_set2pred_simp_tac =
nipkow@13857
   106
  (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym,Compl_Collect]));
nipkow@13857
   107
nipkow@13857
   108
val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split_conv]));
nipkow@13857
   109
nipkow@13857
   110
(*****************************************************************************)
nipkow@13857
   111
(** set2pred transforms sets inclusion into predicates implication,         **)
nipkow@13857
   112
(** maintaining the original variable names.                                **)
nipkow@13857
   113
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
nipkow@13857
   114
(** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
nipkow@13857
   115
(** are first simplified by "before_set2pred_simp_tac", that returns only   **)
nipkow@13857
   116
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
nipkow@13857
   117
(** transformed.                                                            **)
nipkow@13857
   118
(** This transformation may solve very easy subgoals due to a ligth         **)
nipkow@13857
   119
(** simplification done by (split_all_tac)                                  **)
nipkow@13857
   120
(*****************************************************************************)
nipkow@13857
   121
nipkow@13857
   122
fun set2pred i thm = let fun mk_string [] = ""
nipkow@13857
   123
                           | mk_string (x::xs) = x^" "^mk_string xs;
nipkow@13857
   124
                         val vars=get_vars(thm);
nipkow@13857
   125
                         val var_string = mk_string (map (fst o dest_Free) vars);
nipkow@13857
   126
      in ((before_set2pred_simp_tac i) THEN_MAYBE
nipkow@13857
   127
          (EVERY [rtac subsetI i, 
nipkow@13857
   128
                  rtac CollectI i,
nipkow@13857
   129
                  dtac CollectD i,
nipkow@13857
   130
                  (TRY(split_all_tac i)) THEN_MAYBE
nipkow@13857
   131
                  ((rename_tac var_string i) THEN
nipkow@13857
   132
                   (full_simp_tac (HOL_basic_ss addsimps [split_conv]) i)) ])) thm
nipkow@13857
   133
      end;
nipkow@13857
   134
nipkow@13857
   135
(*****************************************************************************)
nipkow@13857
   136
(** BasicSimpTac is called to simplify all verification conditions. It does **)
nipkow@13857
   137
(** a light simplification by applying "mem_Collect_eq", then it calls      **)
nipkow@13857
   138
(** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
nipkow@13857
   139
(** and transforms any other into predicates, applying then                 **)
nipkow@13857
   140
(** the tactic chosen by the user, which may solve the subgoal completely.  **)
nipkow@13857
   141
(*****************************************************************************)
nipkow@13857
   142
nipkow@13857
   143
fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac];
nipkow@13857
   144
nipkow@13857
   145
fun BasicSimpTac tac =
nipkow@13857
   146
  simp_tac
nipkow@13857
   147
    (HOL_basic_ss addsimps [mem_Collect_eq,split_conv] addsimprocs [record_simproc])
nipkow@13857
   148
  THEN_MAYBE' MaxSimpTac tac;
nipkow@13857
   149
nipkow@13857
   150
(** HoareRuleTac **)
nipkow@13857
   151
nipkow@13857
   152
fun WlpTac Mlem tac i =
nipkow@13857
   153
  rtac SeqRule i THEN  HoareRuleTac Mlem tac false (i+1)
nipkow@13857
   154
and HoareRuleTac Mlem tac pre_cond i st = st |>
nipkow@13857
   155
        (*abstraction over st prevents looping*)
nipkow@13857
   156
    ( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i)
nipkow@13857
   157
      ORELSE
nipkow@13857
   158
      (FIRST[rtac SkipRule i,
nipkow@13857
   159
             rtac AbortRule i,
nipkow@13857
   160
             EVERY[rtac BasicRule i,
nipkow@13857
   161
                   rtac Mlem i,
nipkow@13857
   162
                   split_simp_tac i],
nipkow@13857
   163
             EVERY[rtac CondRule i,
nipkow@13857
   164
                   HoareRuleTac Mlem tac false (i+2),
nipkow@13857
   165
                   HoareRuleTac Mlem tac false (i+1)],
nipkow@13857
   166
             EVERY[rtac WhileRule i,
nipkow@13857
   167
                   BasicSimpTac tac (i+2),
nipkow@13857
   168
                   HoareRuleTac Mlem tac true (i+1)] ] 
nipkow@13857
   169
       THEN (if pre_cond then (BasicSimpTac tac i) else rtac subset_refl i) ));
nipkow@13857
   170
nipkow@13857
   171
nipkow@13857
   172
(** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **)
nipkow@13857
   173
(** the final verification conditions                                       **)
nipkow@13857
   174
 
nipkow@13857
   175
fun hoare_tac tac i thm =
nipkow@13857
   176
  let val Mlem = Mset(thm)
nipkow@13857
   177
  in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end;