src/HOL/Hoare/hoare_tac.ML
author wenzelm
Wed, 23 Dec 2020 17:16:05 +0100
changeset 72985 9cc431444435
parent 72806 4fa08e083865
child 74304 1466f8a2f6dd
permissions -rw-r--r--
clarified modules: avoid multiple uses of the same ML file;
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
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
     3
    Author:     Walter Guttmann (extension to total-correctness proofs)
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     4
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     5
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
     6
*)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
     7
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
     8
signature HOARE_TAC =
55660
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
     9
sig
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    10
  val hoare_rule_tac: Proof.context -> term list * thm -> (int -> tactic) -> bool ->
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    11
    int -> tactic
55660
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
    12
  val hoare_tac: Proof.context -> (int -> tactic) -> int -> tactic
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
    13
  val hoare_tc_tac: Proof.context -> (int -> tactic) -> int -> tactic
55660
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
    14
end;
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
    15
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
    16
structure Hoare_Tac: HOARE_TAC =
55660
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
    17
struct
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    18
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    19
(*** The tactics ***)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    20
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    21
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    22
(** 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
    23
(** "?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
    24
(** 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
    25
(** 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
    26
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    27
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    28
local
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    29
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    30
(** maps (%x1 ... xn. t) to [x1,...,xn] **)
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    31
fun abs2list (Const (\<^const_name>\<open>case_prod\<close>, _) $ Abs (x, T, t)) = Free (x, T) :: abs2list t
55659
4089f6e98ab9 reduced ML warnings;
wenzelm
parents: 55414
diff changeset
    32
  | abs2list (Abs (x, 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
    33
  | abs2list _ = [];
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    34
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    35
(** maps {(x1,...,xn). t} to [x1,...,xn] **)
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    36
fun mk_vars (Const (\<^const_name>\<open>Collect\<close>,_) $ 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
    37
  | mk_vars _ = [];
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    38
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
    39
(** 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
    40
Types are also built **)
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    41
fun mk_abstupleC [] body = absfree ("x", HOLogic.unitT) body
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    42
  | mk_abstupleC [v] body = absfree (dest_Free v) body
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    43
  | mk_abstupleC (v :: w) body =
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    44
      let
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    45
        val (x, T) = dest_Free v;
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    46
        val z = mk_abstupleC w body;
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    47
        val T2 =
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    48
          (case z of
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    49
            Abs (_, T, _) => T
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    50
          | Const (_, Type (_, [_, Type (_, [T, _])])) $ _ => T);
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    51
      in
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    52
        Const (\<^const_name>\<open>case_prod\<close>,
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    53
            (T --> T2 --> HOLogic.boolT) --> HOLogic.mk_prodT (T, T2) --> HOLogic.boolT) $
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    54
          absfree (x, T) z
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 42793
diff changeset
    55
      end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    56
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    57
(** maps [x1,...,xn] to (x1,...,xn) and types**)
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    58
fun mk_bodyC [] = HOLogic.unit
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    59
  | mk_bodyC [x] = x
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    60
  | mk_bodyC (x :: xs) =
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    61
      let
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    62
        val (_, T) = dest_Free x;
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    63
        val z = mk_bodyC xs;
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    64
        val T2 =
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    65
          (case z of
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    66
            Free (_, T) => T
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    67
          | Const (\<^const_name>\<open>Pair\<close>, Type ("fun", [_, Type ("fun", [_, T])])) $ _ $ _ => T);
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    68
     in Const (\<^const_name>\<open>Pair\<close>, [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
    69
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
    70
(** maps a subgoal of the form:
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    71
    VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
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
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
    74
  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
    75
    val d = Logic.strip_assums_concl c;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    76
    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
    77
  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
    78
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    79
fun mk_CollectC tm =
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
    80
  let val Type ("fun",[t,_]) = fastype_of tm;
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    81
  in HOLogic.Collect_const t $ tm end;
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    82
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 61424
diff changeset
    83
fun inclt ty = Const (\<^const_name>\<open>Orderings.less_eq\<close>, [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
    84
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    85
in
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
    86
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
    87
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
    88
  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
    89
    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
    90
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
    91
    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
    92
    val varsT = fastype_of (mk_bodyC vars);
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    93
    val big_Collect =
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    94
      mk_CollectC (mk_abstupleC vars (Free (P, varsT --> HOLogic.boolT) $ mk_bodyC vars));
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    95
    val small_Collect =
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
    96
      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
    97
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
    98
    val MsetT = fastype_of big_Collect;
41449
7339f0e7c513 do not open ML structures;
wenzelm
parents: 38012
diff changeset
    99
    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
   100
    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
   101
    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
   102
 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
   103
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   104
end;
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   105
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   106
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   107
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   108
(** Simplifying:                                                            **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   109
(** 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
   110
(** 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
   111
(** 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
   112
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   113
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   114
(**Simp_tacs**)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   115
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   116
fun before_set2pred_simp_tac ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   117
  simp_tac (put_simpset HOL_basic_ss ctxt 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
   118
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   119
fun split_simp_tac ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   120
  simp_tac (put_simpset HOL_basic_ss ctxt 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
   121
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   122
(*****************************************************************************)
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   123
(** set_to_pred_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
   124
(** 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
   125
(** 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
   126
(** 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
   127
(** 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
   128
(** 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
   129
(** transformed.                                                            **)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   130
(** 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
   131
(** 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
   132
(*****************************************************************************)
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   133
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   134
fun set_to_pred_tac ctxt var_names = SUBGOAL (fn (_, i) =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   135
  before_set2pred_simp_tac ctxt i THEN_MAYBE
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
   136
  EVERY [
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   137
    resolve_tac ctxt [subsetI] i,
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   138
    resolve_tac ctxt [CollectI] i,
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   139
    dresolve_tac ctxt [CollectD] i,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   140
    TRY (split_all_tac ctxt i) THEN_MAYBE
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   141
     (rename_tac var_names i THEN
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   142
      full_simp_tac (put_simpset HOL_basic_ss ctxt 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
   143
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   144
(*******************************************************************************)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   145
(** basic_simp_tac is called to simplify all verification conditions. It does **)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   146
(** a light simplification by applying "mem_Collect_eq", then it calls        **)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   147
(** max_simp_tac, which solves subgoals of the form "A <= A",                 **)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   148
(** and transforms any other into predicates, applying then                   **)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   149
(** the tactic chosen by the user, which may solve the subgoal completely.    **)
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   150
(*******************************************************************************)
24475
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   151
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   152
fun max_simp_tac ctxt var_names tac =
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   153
  FIRST' [resolve_tac ctxt [subset_refl],
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   154
    set_to_pred_tac ctxt 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
   155
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   156
fun basic_simp_tac ctxt var_names tac =
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
   157
  simp_tac
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   158
    (put_simpset HOL_basic_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   159
      addsimps [mem_Collect_eq, @{thm split_conv}] addsimprocs [Record.simproc])
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   160
  THEN_MAYBE' max_simp_tac ctxt 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
   161
a297ae4ff188 added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
diff changeset
   162
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
   163
(** 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
   164
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   165
fun hoare_rule_tac ctxt (vars, Mlem) tac =
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
   166
  let
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   167
    val get_thms = Proof_Context.get_thms ctxt;
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
   168
    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
   169
    fun wlp_tac i =
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   170
      resolve_tac ctxt (get_thms \<^named_theorems>\<open>SeqRule\<close>) i THEN rule_tac false (i + 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
   171
    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
   172
      ((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
   173
        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
   174
        (FIRST [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   175
          resolve_tac ctxt (get_thms \<^named_theorems>\<open>SkipRule\<close>) i,
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   176
          resolve_tac ctxt (get_thms \<^named_theorems>\<open>AbortRule\<close>) i,
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
   177
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   178
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>BasicRule\<close>) i,
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   179
            resolve_tac ctxt [Mlem] i,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   180
            split_simp_tac ctxt i],
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
   181
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   182
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>CondRule\<close>) i,
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
   183
            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
   184
            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
   185
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   186
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>WhileRule\<close>) i,
55661
ec14796cd140 tuned whitespace;
wenzelm
parents: 55660
diff changeset
   187
            basic_simp_tac ctxt var_names tac (i + 2),
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
   188
            rule_tac true (i + 1)]]
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   189
         THEN (
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   190
           if pre_cond then basic_simp_tac ctxt var_names tac i
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 55661
diff changeset
   191
           else resolve_tac ctxt [subset_refl] i)));
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
   192
  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
   193
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
   194
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
   195
(** 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
   196
(** 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
   197
55659
4089f6e98ab9 reduced ML warnings;
wenzelm
parents: 55414
diff changeset
   198
fun hoare_tac ctxt tac = SUBGOAL (fn (goal, i) =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 44241
diff changeset
   199
  SELECT_GOAL (hoare_rule_tac ctxt (Mset ctxt goal) tac true 1) i);
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
   200
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   201
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   202
(* total correctness rules *)
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   203
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   204
fun hoare_tc_rule_tac ctxt (vars, Mlem) tac =
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   205
  let
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   206
    val get_thms = Proof_Context.get_thms ctxt;
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   207
    val var_names = map (fst o dest_Free) vars;
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   208
    fun wlp_tac i =
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   209
      resolve_tac ctxt (get_thms \<^named_theorems>\<open>SeqRuleTC\<close>) i THEN rule_tac false (i + 1)
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   210
    and rule_tac pre_cond i st = st |> (*abstraction over st prevents looping*)
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   211
      ((wlp_tac i THEN rule_tac pre_cond i)
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   212
        ORELSE
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   213
        (FIRST [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   214
          resolve_tac ctxt (get_thms \<^named_theorems>\<open>SkipRuleTC\<close>) i,
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   215
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   216
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>BasicRuleTC\<close>) i,
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   217
            resolve_tac ctxt [Mlem] i,
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   218
            split_simp_tac ctxt i],
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   219
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   220
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>CondRuleTC\<close>) i,
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   221
            rule_tac false (i + 2),
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   222
            rule_tac false (i + 1)],
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   223
          EVERY [
72985
9cc431444435 clarified modules: avoid multiple uses of the same ML file;
wenzelm
parents: 72806
diff changeset
   224
            resolve_tac ctxt (get_thms \<^named_theorems>\<open>WhileRuleTC\<close>) i,
72806
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   225
            basic_simp_tac ctxt var_names tac (i + 2),
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   226
            rule_tac true (i + 1)]]
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   227
         THEN (
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   228
           if pre_cond then basic_simp_tac ctxt var_names tac i
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   229
           else resolve_tac ctxt [subset_refl] i)));
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   230
  in rule_tac end;
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   231
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   232
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   233
fun hoare_tc_tac ctxt tac = SUBGOAL (fn (goal, i) =>
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   234
  SELECT_GOAL (hoare_tc_rule_tac ctxt (Mset ctxt goal) tac true 1) i);
4fa08e083865 Extension of session HOL/Hoare with total correctness proof system by Walter Guttmann
nipkow
parents: 69597
diff changeset
   235
55660
f0f895716a8b proper ML structure with signature;
wenzelm
parents: 55659
diff changeset
   236
end;