src/HOL/Hoare/hoare_tac.ML
author wenzelm
Fri, 13 May 2011 22:55:00 +0200
changeset 42793 88bee9f6eec7
parent 41449 7339f0e7c513
child 44241 7943b69f0188
permissions -rw-r--r--
proper Proof.context for classical tactics; reduced claset to snapshot of classical context; discontinued clasimpset;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Hoare/hoare_tac.ML
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     2
    Author:     Leonor Prensa Nieto & Tobias Nipkow
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     3
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     4
Derivation of the proof rules and, most importantly, the VCG tactic.
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     5
*)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     6
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
     7
(* FIXME structure Hoare: HOARE *)
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
     8
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     9
(*** The tactics ***)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    10
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    11
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    12
(** The function Mset makes the theorem                                     **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    13
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}",        **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    14
(** where (x1,...,xn) are the variables of the particular program we are    **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    15
(** working on at the moment of the call                                    **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    16
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    17
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    18
local
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    19
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    20
(** maps (%x1 ... xn. t) to [x1,...,xn] **)
37591
d3daea901123 merged constants "split" and "prod_case"
haftmann
parents: 37391
diff changeset
    21
fun abs2list (Const (@{const_name prod_case}, _) $ Abs (x, T, t)) = Free (x, T) :: abs2list t
37135
636e6d8645d6 normalized references to constant "split"
haftmann
parents: 35092
diff changeset
    22
  | abs2list (Abs (x, T, t)) = [Free (x, T)]
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    23
  | abs2list _ = [];
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    24
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    25
(** maps {(x1,...,xn). t} to [x1,...,xn] **)
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37591
diff changeset
    26
fun mk_vars (Const (@{const_name Collect},_) $ T) = abs2list T
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    27
  | mk_vars _ = [];
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    28
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    29
(** abstraction of body over a tuple formed from a list of free variables.
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    30
Types are also built **)
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    31
fun mk_abstupleC []     body = absfree ("x", HOLogic.unitT, body)
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    32
  | mk_abstupleC (v::w) body = let val (n,T) = dest_Free v
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    33
                               in if w=[] then absfree (n, T, body)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    34
        else let val z  = mk_abstupleC w body;
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    35
                 val T2 = case z of Abs(_,T,_) => T
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    36
                        | Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    37
       in
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    38
        Const (@{const_name prod_case},
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    39
          (T --> T2 --> HOLogic.boolT) --> HOLogic.mk_prodT (T,T2) --> HOLogic.boolT)
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    40
            $ absfree (n, T, z)
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    41
       end end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    42
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    43
(** maps [x1,...,xn] to (x1,...,xn) and types**)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    44
fun mk_bodyC []      = HOLogic.unit
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    45
  | mk_bodyC (x::xs) = if xs=[] then x
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    46
               else let val (n, T) = dest_Free x ;
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    47
                        val z = mk_bodyC xs;
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    48
                        val T2 = case z of Free(_, T) => T
37391
476270a6c2dc tuned quotes, antiquotations and whitespace
haftmann
parents: 37138
diff changeset
    49
                                         | Const (@{const_name Pair}, Type ("fun", [_, Type
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    50
                                            ("fun", [_, T])])) $ _ $ _ => T;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    51
                 in Const (@{const_name Pair}, [T, T2] ---> HOLogic.mk_prodT (T, T2)) $ x $ z end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    52
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    53
(** maps a subgoal of the form:
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    54
        VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    55
fun get_vars c =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    56
  let
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    57
    val d = Logic.strip_assums_concl c;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    58
    val Const _ $ pre $ _ $ _ = HOLogic.dest_Trueprop d;
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    59
  in mk_vars pre end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    60
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    61
fun mk_CollectC trm =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    62
  let val T as Type ("fun",[t,_]) = fastype_of trm
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    63
  in HOLogic.Collect_const t $ trm end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    64
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    65
fun inclt ty = Const (@{const_name Orderings.less_eq}, [ty,ty] ---> HOLogic.boolT);
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    66
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    67
in
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    68
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    69
fun Mset ctxt prop =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    70
  let
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    71
    val [(Mset, _), (P, _)] = Variable.variant_frees ctxt [] [("Mset", ()), ("P", ())];
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    72
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    73
    val vars = get_vars prop;
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    74
    val varsT = fastype_of (mk_bodyC vars);
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    75
    val big_Collect = mk_CollectC (mk_abstupleC vars (Free (P, varsT --> HOLogic.boolT) $ mk_bodyC vars));
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    76
    val small_Collect = mk_CollectC (Abs ("x", varsT, Free (P, varsT --> HOLogic.boolT) $ Bound 0));
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    77
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    78
    val MsetT = fastype_of big_Collect;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    79
    fun Mset_incl t = HOLogic.mk_Trueprop (inclt MsetT $ Free (Mset, MsetT) $ t);
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    80
    val impl = Logic.mk_implies (Mset_incl big_Collect, Mset_incl small_Collect);
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41449
diff changeset
    81
    val th = Goal.prove ctxt [Mset, P] [] impl (fn _ => blast_tac ctxt 1);
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
    82
 in (vars, th) end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    83
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    84
end;
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    85
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    86
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    87
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    88
(** Simplifying:                                                            **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    89
(** Some useful lemmata, lists and simplification tactics to control which  **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    90
(** theorems are used to simplify at each moment, so that the original      **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    91
(** input does not suffer any unexpected transformation                     **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    92
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    93
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    94
(**Simp_tacs**)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    95
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    96
val before_set2pred_simp_tac =
26300
03def556e26e removed duplicate lemmas;
wenzelm
parents: 24475
diff changeset
    97
  (simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym, @{thm Compl_Collect}]));
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    98
37138
ee23611b6bf2 dropped legacy theorem bindings
haftmann
parents: 37135
diff changeset
    99
val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [@{thm split_conv}]));
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   100
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   101
(*****************************************************************************)
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   102
(** set2pred_tac transforms sets inclusion into predicates implication,     **)
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   103
(** maintaining the original variable names.                                **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   104
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1"              **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   105
(** Subgoals containing intersections (A Int B) or complement sets (-A)     **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   106
(** are first simplified by "before_set2pred_simp_tac", that returns only   **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   107
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily           **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   108
(** transformed.                                                            **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   109
(** This transformation may solve very easy subgoals due to a ligth         **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   110
(** simplification done by (split_all_tac)                                  **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   111
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   112
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   113
fun set2pred_tac var_names = SUBGOAL (fn (goal, i) =>
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   114
  before_set2pred_simp_tac i THEN_MAYBE
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   115
  EVERY [
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   116
    rtac subsetI i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   117
    rtac CollectI i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   118
    dtac CollectD i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   119
    TRY (split_all_tac i) THEN_MAYBE
37138
ee23611b6bf2 dropped legacy theorem bindings
haftmann
parents: 37135
diff changeset
   120
     (rename_tac var_names i THEN full_simp_tac (HOL_basic_ss addsimps [@{thm split_conv}]) i)]);
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   121
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   122
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   123
(** BasicSimpTac is called to simplify all verification conditions. It does **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   124
(** a light simplification by applying "mem_Collect_eq", then it calls      **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   125
(** MaxSimpTac, which solves subgoals of the form "A <= A",                 **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   126
(** and transforms any other into predicates, applying then                 **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   127
(** the tactic chosen by the user, which may solve the subgoal completely.  **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   128
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   129
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   130
fun MaxSimpTac var_names tac = FIRST'[rtac subset_refl, set2pred_tac var_names THEN_MAYBE' tac];
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   131
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   132
fun BasicSimpTac var_names tac =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   133
  simp_tac
38012
3ca193a6ae5a delete structure Basic_Record; avoid `record` in names in structure Record
haftmann
parents: 37677
diff changeset
   134
    (HOL_basic_ss addsimps [mem_Collect_eq, @{thm split_conv}] addsimprocs [Record.simproc])
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   135
  THEN_MAYBE' MaxSimpTac var_names tac;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   136
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   137
28457
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   138
(** hoare_rule_tac **)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   139
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   140
fun hoare_rule_tac (vars, Mlem) tac =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   141
  let
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   142
    val var_names = map (fst o dest_Free) vars;
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   143
    fun wlp_tac i =
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   144
      rtac @{thm SeqRule} i THEN rule_tac false (i + 1)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   145
    and rule_tac pre_cond i st = st |> (*abstraction over st prevents looping*)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   146
      ((wlp_tac i THEN rule_tac pre_cond i)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   147
        ORELSE
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   148
        (FIRST [
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   149
          rtac @{thm SkipRule} i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   150
          rtac @{thm AbortRule} i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   151
          EVERY [
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   152
            rtac @{thm BasicRule} i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   153
            rtac Mlem i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   154
            split_simp_tac i],
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   155
          EVERY [
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   156
            rtac @{thm CondRule} i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   157
            rule_tac false (i + 2),
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   158
            rule_tac false (i + 1)],
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   159
          EVERY [
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   160
            rtac @{thm WhileRule} i,
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   161
            BasicSimpTac var_names tac (i + 2),
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   162
            rule_tac true (i + 1)]]
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   163
         THEN (if pre_cond then BasicSimpTac var_names tac i else rtac subset_refl i)));
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   164
  in rule_tac end;
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   165
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   166
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   167
(** tac is the tactic the user chooses to solve or simplify **)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   168
(** the final verification conditions                       **)
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   169
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   170
fun hoare_tac ctxt (tac: int -> tactic) = SUBGOAL (fn (goal, i) =>
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   171
  SELECT_GOAL (hoare_rule_tac (Mset ctxt goal) tac true 1) i);
25669513fd4c major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents: 27330
diff changeset
   172