src/HOL/Hoare/hoare_tac.ML
author wenzelm
Fri Feb 21 20:37:13 2014 +0100 (2014-02-21)
changeset 55660 f0f895716a8b
parent 55659 4089f6e98ab9
child 55661 ec14796cd140
permissions -rw-r--r--
proper ML structure with signature;
wenzelm@24475
     1
(*  Title:      HOL/Hoare/hoare_tac.ML
wenzelm@24475
     2
    Author:     Leonor Prensa Nieto & Tobias Nipkow
wenzelm@24475
     3
wenzelm@24475
     4
Derivation of the proof rules and, most importantly, the VCG tactic.
wenzelm@24475
     5
*)
wenzelm@24475
     6
wenzelm@55660
     7
signature HOARE =
wenzelm@55660
     8
sig
wenzelm@55660
     9
  val hoare_rule_tac: Proof.context -> term list * thm -> (int -> tactic) -> bool -> int -> tactic
wenzelm@55660
    10
  val hoare_tac: Proof.context -> (int -> tactic) -> int -> tactic
wenzelm@55660
    11
end;
wenzelm@55660
    12
wenzelm@55660
    13
structure Hoare: HOARE =
wenzelm@55660
    14
struct
wenzelm@41449
    15
wenzelm@24475
    16
(*** The tactics ***)
wenzelm@24475
    17
wenzelm@24475
    18
(*****************************************************************************)
wenzelm@24475
    19
(** The function Mset makes the theorem                                     **)
wenzelm@24475
    20
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
wenzelm@24475
    21
(** where (x1,...,xn) are the variables of the particular program we are    **)
wenzelm@24475
    22
(** working on at the moment of the call                                    **)
wenzelm@24475
    23
(*****************************************************************************)
wenzelm@24475
    24
wenzelm@41449
    25
local
wenzelm@24475
    26
wenzelm@24475
    27
(** maps (%x1 ... xn. t) to [x1,...,xn] **)
blanchet@55414
    28
fun abs2list (Const (@{const_name case_prod}, _) $ Abs (x, T, t)) = Free (x, T) :: abs2list t
wenzelm@55659
    29
  | abs2list (Abs (x, T, _)) = [Free (x, T)]
wenzelm@24475
    30
  | abs2list _ = [];
wenzelm@24475
    31
wenzelm@24475
    32
(** maps {(x1,...,xn). t} to [x1,...,xn] **)
haftmann@37677
    33
fun mk_vars (Const (@{const_name Collect},_) $ T) = abs2list T
wenzelm@24475
    34
  | mk_vars _ = [];
wenzelm@24475
    35
wenzelm@28457
    36
(** abstraction of body over a tuple formed from a list of free variables.
wenzelm@24475
    37
Types are also built **)
wenzelm@44241
    38
fun mk_abstupleC [] body = absfree ("x", HOLogic.unitT) body
wenzelm@44241
    39
  | mk_abstupleC [v] body = absfree (dest_Free v) body
wenzelm@44241
    40
  | mk_abstupleC (v :: w) body =
wenzelm@44241
    41
      let
wenzelm@44241
    42
        val (x, T) = dest_Free v;
wenzelm@44241
    43
        val z = mk_abstupleC w body;
wenzelm@44241
    44
        val T2 =
wenzelm@44241
    45
          (case z of
wenzelm@44241
    46
            Abs (_, T, _) => T
wenzelm@44241
    47
          | Const (_, Type (_, [_, Type (_, [T, _])])) $ _ => T);
wenzelm@44241
    48
      in
blanchet@55414
    49
        Const (@{const_name case_prod},
wenzelm@44241
    50
            (T --> T2 --> HOLogic.boolT) --> HOLogic.mk_prodT (T, T2) --> HOLogic.boolT) $
wenzelm@44241
    51
          absfree (x, T) z
wenzelm@44241
    52
      end;
wenzelm@24475
    53
wenzelm@24475
    54
(** maps [x1,...,xn] to (x1,...,xn) and types**)
wenzelm@24475
    55
fun mk_bodyC []      = HOLogic.unit
wenzelm@28457
    56
  | mk_bodyC (x::xs) = if xs=[] then x
wenzelm@24475
    57
               else let val (n, T) = dest_Free x ;
wenzelm@24475
    58
                        val z = mk_bodyC xs;
wenzelm@24475
    59
                        val T2 = case z of Free(_, T) => T
haftmann@37391
    60
                                         | Const (@{const_name Pair}, Type ("fun", [_, Type
wenzelm@24475
    61
                                            ("fun", [_, T])])) $ _ $ _ => T;
wenzelm@41449
    62
                 in Const (@{const_name Pair}, [T, T2] ---> HOLogic.mk_prodT (T, T2)) $ x $ z end;
wenzelm@24475
    63
wenzelm@28457
    64
(** maps a subgoal of the form:
wenzelm@28457
    65
        VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**)
wenzelm@28457
    66
fun get_vars c =
wenzelm@28457
    67
  let
wenzelm@28457
    68
    val d = Logic.strip_assums_concl c;
wenzelm@41449
    69
    val Const _ $ pre $ _ $ _ = HOLogic.dest_Trueprop d;
wenzelm@28457
    70
  in mk_vars pre end;
wenzelm@24475
    71
wenzelm@28457
    72
fun mk_CollectC trm =
wenzelm@28457
    73
  let val T as Type ("fun",[t,_]) = fastype_of trm
wenzelm@41449
    74
  in HOLogic.Collect_const t $ trm end;
wenzelm@24475
    75
wenzelm@41449
    76
fun inclt ty = Const (@{const_name Orderings.less_eq}, [ty,ty] ---> HOLogic.boolT);
wenzelm@24475
    77
wenzelm@41449
    78
in
wenzelm@24475
    79
wenzelm@28457
    80
fun Mset ctxt prop =
wenzelm@28457
    81
  let
wenzelm@28457
    82
    val [(Mset, _), (P, _)] = Variable.variant_frees ctxt [] [("Mset", ()), ("P", ())];
wenzelm@24475
    83
wenzelm@28457
    84
    val vars = get_vars prop;
wenzelm@28457
    85
    val varsT = fastype_of (mk_bodyC vars);
wenzelm@41449
    86
    val big_Collect = mk_CollectC (mk_abstupleC vars (Free (P, varsT --> HOLogic.boolT) $ mk_bodyC vars));
wenzelm@41449
    87
    val small_Collect = mk_CollectC (Abs ("x", varsT, Free (P, varsT --> HOLogic.boolT) $ Bound 0));
wenzelm@28457
    88
wenzelm@28457
    89
    val MsetT = fastype_of big_Collect;
wenzelm@41449
    90
    fun Mset_incl t = HOLogic.mk_Trueprop (inclt MsetT $ Free (Mset, MsetT) $ t);
wenzelm@28457
    91
    val impl = Logic.mk_implies (Mset_incl big_Collect, Mset_incl small_Collect);
wenzelm@42793
    92
    val th = Goal.prove ctxt [Mset, P] [] impl (fn _ => blast_tac ctxt 1);
wenzelm@28457
    93
 in (vars, th) end;
wenzelm@24475
    94
wenzelm@24475
    95
end;
wenzelm@24475
    96
wenzelm@24475
    97
wenzelm@24475
    98
(*****************************************************************************)
wenzelm@24475
    99
(** Simplifying:                                                            **)
wenzelm@24475
   100
(** Some useful lemmata, lists and simplification tactics to control which  **)
wenzelm@24475
   101
(** theorems are used to simplify at each moment, so that the original      **)
wenzelm@24475
   102
(** input does not suffer any unexpected transformation                     **)
wenzelm@24475
   103
(*****************************************************************************)
wenzelm@24475
   104
wenzelm@24475
   105
(**Simp_tacs**)
wenzelm@24475
   106
wenzelm@51717
   107
fun before_set2pred_simp_tac ctxt =
wenzelm@51717
   108
  simp_tac (put_simpset HOL_basic_ss ctxt addsimps [Collect_conj_eq RS sym, @{thm Compl_Collect}]);
wenzelm@24475
   109
wenzelm@51717
   110
fun split_simp_tac ctxt =
wenzelm@51717
   111
  simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm split_conv}]);
wenzelm@24475
   112
wenzelm@24475
   113
(*****************************************************************************)
wenzelm@28457
   114
(** set2pred_tac transforms sets inclusion into predicates implication,     **)
wenzelm@24475
   115
(** maintaining the original variable names.                                **)
wenzelm@24475
   116
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
wenzelm@24475
   117
(** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
wenzelm@24475
   118
(** are first simplified by "before_set2pred_simp_tac", that returns only   **)
wenzelm@24475
   119
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
wenzelm@24475
   120
(** transformed.                                                            **)
wenzelm@24475
   121
(** This transformation may solve very easy subgoals due to a ligth         **)
wenzelm@24475
   122
(** simplification done by (split_all_tac)                                  **)
wenzelm@24475
   123
(*****************************************************************************)
wenzelm@24475
   124
wenzelm@55659
   125
fun set2pred_tac ctxt var_names = SUBGOAL (fn (_, i) =>
wenzelm@51717
   126
  before_set2pred_simp_tac ctxt i THEN_MAYBE
wenzelm@28457
   127
  EVERY [
wenzelm@28457
   128
    rtac subsetI i,
wenzelm@28457
   129
    rtac CollectI i,
wenzelm@28457
   130
    dtac CollectD i,
wenzelm@51717
   131
    TRY (split_all_tac ctxt i) THEN_MAYBE
wenzelm@51717
   132
     (rename_tac var_names i THEN
wenzelm@51717
   133
      full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm split_conv}]) i)]);
wenzelm@24475
   134
wenzelm@24475
   135
(*****************************************************************************)
wenzelm@24475
   136
(** BasicSimpTac is called to simplify all verification conditions. It does **)
wenzelm@24475
   137
(** a light simplification by applying "mem_Collect_eq", then it calls      **)
wenzelm@24475
   138
(** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
wenzelm@24475
   139
(** and transforms any other into predicates, applying then                 **)
wenzelm@24475
   140
(** the tactic chosen by the user, which may solve the subgoal completely.  **)
wenzelm@24475
   141
(*****************************************************************************)
wenzelm@24475
   142
wenzelm@51717
   143
fun MaxSimpTac ctxt var_names tac =
wenzelm@51717
   144
  FIRST'[rtac subset_refl, set2pred_tac ctxt var_names THEN_MAYBE' tac];
wenzelm@24475
   145
wenzelm@51717
   146
fun BasicSimpTac ctxt var_names tac =
wenzelm@28457
   147
  simp_tac
wenzelm@51717
   148
    (put_simpset HOL_basic_ss ctxt
wenzelm@51717
   149
      addsimps [mem_Collect_eq, @{thm split_conv}] addsimprocs [Record.simproc])
wenzelm@51717
   150
  THEN_MAYBE' MaxSimpTac ctxt var_names tac;
wenzelm@24475
   151
wenzelm@24475
   152
wenzelm@28457
   153
(** hoare_rule_tac **)
wenzelm@28457
   154
wenzelm@51717
   155
fun hoare_rule_tac ctxt (vars, Mlem) tac =
wenzelm@28457
   156
  let
wenzelm@28457
   157
    val var_names = map (fst o dest_Free) vars;
wenzelm@28457
   158
    fun wlp_tac i =
wenzelm@28457
   159
      rtac @{thm SeqRule} i THEN rule_tac false (i + 1)
wenzelm@28457
   160
    and rule_tac pre_cond i st = st |> (*abstraction over st prevents looping*)
wenzelm@28457
   161
      ((wlp_tac i THEN rule_tac pre_cond i)
wenzelm@28457
   162
        ORELSE
wenzelm@28457
   163
        (FIRST [
wenzelm@28457
   164
          rtac @{thm SkipRule} i,
wenzelm@28457
   165
          rtac @{thm AbortRule} i,
wenzelm@28457
   166
          EVERY [
wenzelm@28457
   167
            rtac @{thm BasicRule} i,
wenzelm@28457
   168
            rtac Mlem i,
wenzelm@51717
   169
            split_simp_tac ctxt i],
wenzelm@28457
   170
          EVERY [
wenzelm@28457
   171
            rtac @{thm CondRule} i,
wenzelm@28457
   172
            rule_tac false (i + 2),
wenzelm@28457
   173
            rule_tac false (i + 1)],
wenzelm@28457
   174
          EVERY [
wenzelm@28457
   175
            rtac @{thm WhileRule} i,
wenzelm@51717
   176
            BasicSimpTac ctxt var_names tac (i + 2),
wenzelm@28457
   177
            rule_tac true (i + 1)]]
wenzelm@51717
   178
         THEN (if pre_cond then BasicSimpTac ctxt var_names tac i else rtac subset_refl i)));
wenzelm@28457
   179
  in rule_tac end;
wenzelm@28457
   180
wenzelm@28457
   181
wenzelm@28457
   182
(** tac is the tactic the user chooses to solve or simplify **)
wenzelm@28457
   183
(** the final verification conditions                       **)
wenzelm@28457
   184
wenzelm@55659
   185
fun hoare_tac ctxt tac = SUBGOAL (fn (goal, i) =>
wenzelm@51717
   186
  SELECT_GOAL (hoare_rule_tac ctxt (Mset ctxt goal) tac true 1) i);
wenzelm@28457
   187
wenzelm@55660
   188
end;
wenzelm@55660
   189