src/Pure/goal.ML
author wenzelm
Wed Nov 16 17:45:26 2005 +0100 (2005-11-16)
changeset 18180 db712213504d
parent 18145 6757627acf59
child 18207 9edbeda7e39a
permissions -rw-r--r--
norm_hhf: no normalization of protected props;
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@17980
     2
    ID:         $Id$
wenzelm@17980
     3
    Author:     Makarius and Lawrence C Paulson
wenzelm@17980
     4
wenzelm@18139
     5
Goals in tactical theorem proving.
wenzelm@17980
     6
*)
wenzelm@17980
     7
wenzelm@17980
     8
signature BASIC_GOAL =
wenzelm@17980
     9
sig
wenzelm@17980
    10
  val SELECT_GOAL: tactic -> int -> tactic
wenzelm@17980
    11
end;
wenzelm@17980
    12
wenzelm@17980
    13
signature GOAL =
wenzelm@17980
    14
sig
wenzelm@17980
    15
  include BASIC_GOAL
wenzelm@17980
    16
  val init: cterm -> thm
wenzelm@18027
    17
  val protect: thm -> thm
wenzelm@17980
    18
  val conclude: thm -> thm
wenzelm@17980
    19
  val finish: thm -> thm
wenzelm@18027
    20
  val norm_hhf: thm -> thm
wenzelm@18180
    21
  val norm_hhf': thm -> thm
wenzelm@18119
    22
  val compose_hhf: thm -> int -> thm -> thm Seq.seq
wenzelm@18119
    23
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@18027
    24
  val comp_hhf: thm -> thm -> thm
wenzelm@17980
    25
  val prove_multi: theory -> string list -> term list -> term list ->
wenzelm@17980
    26
    (thm list -> tactic) -> thm list
wenzelm@18139
    27
  val prove: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@18027
    28
  val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@17980
    29
end;
wenzelm@17980
    30
wenzelm@17980
    31
structure Goal: GOAL =
wenzelm@17980
    32
struct
wenzelm@17980
    33
wenzelm@18027
    34
(** goals **)
wenzelm@18027
    35
wenzelm@18027
    36
(*
wenzelm@18027
    37
  -------- (init)
wenzelm@18027
    38
  C ==> #C
wenzelm@18027
    39
*)
wenzelm@18027
    40
fun init ct = Drule.instantiate' [] [SOME ct] Drule.protectI;
wenzelm@17980
    41
wenzelm@17980
    42
(*
wenzelm@18119
    43
   C
wenzelm@18119
    44
  --- (protect)
wenzelm@18119
    45
  #C
wenzelm@17980
    46
*)
wenzelm@18027
    47
fun protect th = th COMP Drule.incr_indexes th Drule.protectI;
wenzelm@17980
    48
wenzelm@17980
    49
(*
wenzelm@18027
    50
  A ==> ... ==> #C
wenzelm@18027
    51
  ---------------- (conclude)
wenzelm@17980
    52
  A ==> ... ==> C
wenzelm@17980
    53
*)
wenzelm@17980
    54
fun conclude th =
wenzelm@17980
    55
  (case SINGLE (Thm.bicompose false (false, th, Thm.nprems_of th) 1)
wenzelm@18027
    56
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    57
    SOME th' => th'
wenzelm@17980
    58
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    59
wenzelm@17980
    60
(*
wenzelm@18027
    61
  #C
wenzelm@18027
    62
  --- (finish)
wenzelm@18027
    63
   C
wenzelm@17983
    64
*)
wenzelm@17980
    65
fun finish th =
wenzelm@17980
    66
  (case Thm.nprems_of th of
wenzelm@17980
    67
    0 => conclude th
wenzelm@17980
    68
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    69
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    70
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    71
wenzelm@17980
    72
wenzelm@18027
    73
wenzelm@18027
    74
(** results **)
wenzelm@18027
    75
wenzelm@18027
    76
(* HHF normal form *)
wenzelm@17980
    77
wenzelm@18180
    78
val norm_hhf_ss =
wenzelm@18180
    79
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18180
    80
    addsimps [Drule.norm_hhf_eq];
wenzelm@18180
    81
wenzelm@18180
    82
val norm_hhf_ss' = norm_hhf_ss addeqcongs [Drule.protect_cong];
wenzelm@18180
    83
wenzelm@18180
    84
fun gen_norm_hhf protected =
wenzelm@17980
    85
  (not o Drule.is_norm_hhf o Thm.prop_of) ?
wenzelm@18180
    86
    Drule.fconv_rule (MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE))
wenzelm@18180
    87
      (if protected then norm_hhf_ss else norm_hhf_ss'))
wenzelm@17980
    88
  #> Thm.adjust_maxidx_thm
wenzelm@17980
    89
  #> Drule.gen_all;
wenzelm@17980
    90
wenzelm@18180
    91
val norm_hhf = gen_norm_hhf true;
wenzelm@18180
    92
val norm_hhf' = gen_norm_hhf false;
wenzelm@18180
    93
wenzelm@18027
    94
wenzelm@18027
    95
(* composition of normal results *)
wenzelm@18027
    96
wenzelm@18027
    97
fun compose_hhf tha i thb =
wenzelm@18145
    98
  Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
wenzelm@18027
    99
wenzelm@18119
   100
fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
wenzelm@18119
   101
wenzelm@18027
   102
fun comp_hhf tha thb =
wenzelm@18119
   103
  (case Seq.chop (2, compose_hhf tha 1 thb) of
wenzelm@18119
   104
    ([th], _) => th
wenzelm@18119
   105
  | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
wenzelm@18119
   106
  | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
wenzelm@17986
   107
wenzelm@17986
   108
wenzelm@18027
   109
wenzelm@18027
   110
(** tactical theorem proving **)
wenzelm@18027
   111
wenzelm@18119
   112
(* prove_multi *)
wenzelm@17986
   113
wenzelm@18119
   114
fun prove_multi thy xs asms props tac =
wenzelm@17980
   115
  let
wenzelm@17980
   116
    val prop = Logic.mk_conjunction_list props;
wenzelm@17980
   117
    val statement = Logic.list_implies (asms, prop);
wenzelm@18139
   118
    val frees = Term.add_frees statement [];
wenzelm@17980
   119
    val fixed_frees = filter_out (member (op =) xs o #1) frees;
wenzelm@18139
   120
    val fixed_tfrees = fold (Term.add_tfreesT o #2) fixed_frees [];
wenzelm@17980
   121
    val params = List.mapPartial (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
wenzelm@17980
   122
wenzelm@17980
   123
    fun err msg = raise ERROR_MESSAGE
wenzelm@17980
   124
      (msg ^ "\nThe error(s) above occurred for the goal statement:\n" ^
wenzelm@17980
   125
        Sign.string_of_term thy (Term.list_all_free (params, statement)));
wenzelm@17980
   126
wenzelm@17980
   127
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
wenzelm@17980
   128
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@17980
   129
wenzelm@17980
   130
    val _ = cert_safe statement;
wenzelm@17980
   131
    val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
wenzelm@17980
   132
wenzelm@17980
   133
    val cparams = map (cert_safe o Free) params;
wenzelm@17980
   134
    val casms = map cert_safe asms;
wenzelm@18027
   135
    val prems = map (norm_hhf o Thm.assume) casms;
wenzelm@17980
   136
wenzelm@17980
   137
    val goal = init (cert_safe prop);
wenzelm@18119
   138
    val goal' = case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.";
wenzelm@17980
   139
    val raw_result = finish goal' handle THM (msg, _, _) => err msg;
wenzelm@17980
   140
wenzelm@17980
   141
    val prop' = Thm.prop_of raw_result;
wenzelm@17986
   142
    val _ = conditional (not (Pattern.matches thy (Envir.beta_norm prop, prop'))) (fn () =>
wenzelm@17980
   143
      err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
wenzelm@17980
   144
  in
wenzelm@17980
   145
    Drule.conj_elim_precise (length props) raw_result
wenzelm@17980
   146
    |> map
wenzelm@17980
   147
      (Drule.implies_intr_list casms
wenzelm@17980
   148
        #> Drule.forall_intr_list cparams
wenzelm@18119
   149
        #> norm_hhf
wenzelm@18139
   150
        #> Thm.varifyT' fixed_tfrees
wenzelm@18139
   151
        #-> K Drule.zero_var_indexes)
wenzelm@17980
   152
  end;
wenzelm@17980
   153
wenzelm@17980
   154
wenzelm@18119
   155
(* prove *)
wenzelm@17980
   156
wenzelm@17980
   157
fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
wenzelm@17980
   158
wenzelm@17980
   159
wenzelm@18027
   160
(* prove_raw -- no checks, no normalization of result! *)
wenzelm@18027
   161
wenzelm@18027
   162
fun prove_raw casms cprop tac =
wenzelm@18027
   163
  (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
wenzelm@18027
   164
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@18027
   165
  | NONE => raise ERROR_MESSAGE "Tactic failed.");
wenzelm@18027
   166
wenzelm@18027
   167
wenzelm@17980
   168
(* SELECT_GOAL *)
wenzelm@17980
   169
wenzelm@17980
   170
(*Tactical for restricting the effect of a tactic to subgoal i.  Works
wenzelm@17980
   171
  by making a new state from subgoal i, applying tac to it, and
wenzelm@17980
   172
  composing the resulting thm with the original state.*)
wenzelm@17980
   173
wenzelm@17980
   174
fun SELECT tac i st =
wenzelm@18145
   175
  init (Thm.adjust_maxidx (Thm.cprem_of st i))
wenzelm@17980
   176
  |> tac
wenzelm@17980
   177
  |> Seq.maps (fn st' => Thm.bicompose false (false, conclude st', Thm.nprems_of st') i st);
wenzelm@17980
   178
wenzelm@17980
   179
fun SELECT_GOAL tac i st =
wenzelm@17980
   180
  let val n = Thm.nprems_of st in
wenzelm@17980
   181
    if 1 <= i andalso i <= n then
wenzelm@17980
   182
      if n = 1 then tac st else SELECT tac i st
wenzelm@17980
   183
    else Seq.empty
wenzelm@17980
   184
  end;
wenzelm@17980
   185
wenzelm@17980
   186
end;
wenzelm@17980
   187
wenzelm@17980
   188
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   189
open BasicGoal;