src/Pure/goal.ML
author wenzelm
Fri Oct 28 22:27:55 2005 +0200 (2005-10-28)
changeset 18027 09ab79d4e8e1
parent 17986 0450847646c3
child 18119 63901a9b99dc
permissions -rw-r--r--
renamed Goal constant to prop, more general protect/unprotect interfaces;
renamed norm_hhf_rule to norm_hhf;
added comp_hhf, compose_hhf_tac, based on Drule.lift_all;
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@18027
     5
Tactical goal state.
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@18027
    21
  val comp_hhf: thm -> thm -> thm
wenzelm@18027
    22
  val compose_hhf_tac: thm list -> int -> tactic
wenzelm@17980
    23
  val prove: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@17980
    24
  val prove_multi: theory -> string list -> term list -> term list ->
wenzelm@17980
    25
    (thm list -> tactic) -> thm list
wenzelm@18027
    26
  val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@17980
    27
wenzelm@17980
    28
  (* FIXME remove *)
wenzelm@17980
    29
  val norm_hhf_plain: thm -> thm
wenzelm@17980
    30
  val prove_multi_plain: theory -> string list -> term list -> term list ->
wenzelm@17980
    31
    (thm list -> tactic) -> thm list
wenzelm@17980
    32
  val prove_plain: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@17980
    33
end;
wenzelm@17980
    34
wenzelm@17980
    35
structure Goal: GOAL =
wenzelm@17980
    36
struct
wenzelm@17980
    37
wenzelm@18027
    38
(** goals **)
wenzelm@18027
    39
wenzelm@18027
    40
(*
wenzelm@18027
    41
  -------- (init)
wenzelm@18027
    42
  C ==> #C
wenzelm@18027
    43
*)
wenzelm@18027
    44
fun init ct = Drule.instantiate' [] [SOME ct] Drule.protectI;
wenzelm@17980
    45
wenzelm@17980
    46
(*
wenzelm@18027
    47
    A ==> ... ==> C
wenzelm@18027
    48
  ------------------ (protect)
wenzelm@18027
    49
  #(A ==> ... ==> C)
wenzelm@17980
    50
*)
wenzelm@18027
    51
fun protect th = th COMP Drule.incr_indexes th Drule.protectI;
wenzelm@17980
    52
wenzelm@17980
    53
(*
wenzelm@18027
    54
  A ==> ... ==> #C
wenzelm@18027
    55
  ---------------- (conclude)
wenzelm@17980
    56
  A ==> ... ==> C
wenzelm@17980
    57
*)
wenzelm@17980
    58
fun conclude th =
wenzelm@17980
    59
  (case SINGLE (Thm.bicompose false (false, th, Thm.nprems_of th) 1)
wenzelm@18027
    60
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    61
    SOME th' => th'
wenzelm@17980
    62
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    63
wenzelm@17980
    64
(*
wenzelm@18027
    65
  #C
wenzelm@18027
    66
  --- (finish)
wenzelm@18027
    67
   C
wenzelm@17983
    68
*)
wenzelm@17980
    69
fun finish th =
wenzelm@17980
    70
  (case Thm.nprems_of th of
wenzelm@17980
    71
    0 => conclude th
wenzelm@17980
    72
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    73
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    74
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    75
wenzelm@17980
    76
wenzelm@18027
    77
wenzelm@18027
    78
(** results **)
wenzelm@18027
    79
wenzelm@18027
    80
(* HHF normal form *)
wenzelm@17980
    81
wenzelm@17980
    82
val norm_hhf_plain =  (* FIXME remove *)
wenzelm@17980
    83
  (not o Drule.is_norm_hhf o Thm.prop_of) ?
wenzelm@17980
    84
    MetaSimplifier.simplify_aux (K (K NONE)) true [Drule.norm_hhf_eq];
wenzelm@17980
    85
wenzelm@18027
    86
val norm_hhf =
wenzelm@17980
    87
  norm_hhf_plain
wenzelm@17980
    88
  #> Thm.adjust_maxidx_thm
wenzelm@17980
    89
  #> Drule.gen_all;
wenzelm@17980
    90
wenzelm@18027
    91
wenzelm@18027
    92
(* composition of normal results *)
wenzelm@18027
    93
wenzelm@18027
    94
fun compose_hhf tha i thb =
wenzelm@18027
    95
  Thm.bicompose false (false, Drule.lift_all (Thm.cgoal_of thb i) tha, 0) i thb;
wenzelm@18027
    96
wenzelm@18027
    97
fun comp_hhf tha thb =
wenzelm@18027
    98
  (case Seq.pull (compose_hhf tha 1 thb) of
wenzelm@18027
    99
    SOME (th, _) => th
wenzelm@18027
   100
  | NONE => raise THM ("comp_hhf", 1, [tha, thb]));
wenzelm@18027
   101
wenzelm@18027
   102
fun compose_hhf_tac [] _ = no_tac
wenzelm@18027
   103
  | compose_hhf_tac (th :: ths) i = PRIMSEQ (compose_hhf th i) APPEND compose_hhf_tac ths i;
wenzelm@17986
   104
wenzelm@17986
   105
wenzelm@18027
   106
wenzelm@18027
   107
(** tactical theorem proving **)
wenzelm@18027
   108
wenzelm@18027
   109
(* prove *)
wenzelm@17986
   110
wenzelm@17980
   111
local
wenzelm@17980
   112
wenzelm@17980
   113
fun gen_prove finish_thm thy xs asms props tac =
wenzelm@17980
   114
  let
wenzelm@17980
   115
    val prop = Logic.mk_conjunction_list props;
wenzelm@17980
   116
    val statement = Logic.list_implies (asms, prop);
wenzelm@17980
   117
    val frees = map Term.dest_Free (Term.term_frees statement);
wenzelm@17980
   118
    val fixed_frees = filter_out (member (op =) xs o #1) frees;
wenzelm@17980
   119
    val fixed_tfrees = foldr Term.add_typ_tfrees [] (map #2 fixed_frees);
wenzelm@17980
   120
    val params = List.mapPartial (fn x => Option.map (pair x) (AList.lookup (op =) frees x)) xs;
wenzelm@17980
   121
wenzelm@17980
   122
    fun err msg = raise ERROR_MESSAGE
wenzelm@17980
   123
      (msg ^ "\nThe error(s) above occurred for the goal statement:\n" ^
wenzelm@17980
   124
        Sign.string_of_term thy (Term.list_all_free (params, statement)));
wenzelm@17980
   125
wenzelm@17980
   126
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm t)
wenzelm@17980
   127
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@17980
   128
wenzelm@17980
   129
    val _ = cert_safe statement;
wenzelm@17980
   130
    val _ = Term.no_dummy_patterns statement handle TERM (msg, _) => err msg;
wenzelm@17980
   131
wenzelm@17980
   132
    val cparams = map (cert_safe o Free) params;
wenzelm@17980
   133
    val casms = map cert_safe asms;
wenzelm@18027
   134
    val prems = map (norm_hhf o Thm.assume) casms;
wenzelm@17980
   135
wenzelm@17980
   136
    val goal = init (cert_safe prop);
wenzelm@17980
   137
    val goal' = (case SINGLE (tac prems) goal of SOME goal' => goal' | _ => err "Tactic failed.");
wenzelm@17980
   138
    val raw_result = finish goal' handle THM (msg, _, _) => err msg;
wenzelm@17980
   139
wenzelm@17980
   140
    val prop' = Thm.prop_of raw_result;
wenzelm@17986
   141
    val _ = conditional (not (Pattern.matches thy (Envir.beta_norm prop, prop'))) (fn () =>
wenzelm@17980
   142
      err ("Proved a different theorem: " ^ Sign.string_of_term thy prop'));
wenzelm@17980
   143
  in
wenzelm@17980
   144
    Drule.conj_elim_precise (length props) raw_result
wenzelm@17980
   145
    |> map
wenzelm@17980
   146
      (Drule.implies_intr_list casms
wenzelm@17980
   147
        #> Drule.forall_intr_list cparams
wenzelm@17980
   148
        #> finish_thm fixed_tfrees)
wenzelm@17980
   149
  end;
wenzelm@17980
   150
wenzelm@17980
   151
in
wenzelm@17980
   152
wenzelm@17980
   153
fun prove_multi thy xs asms prop tac =
wenzelm@17980
   154
  gen_prove (fn fixed_tfrees => Drule.zero_var_indexes o
wenzelm@18027
   155
      (#1 o Thm.varifyT' fixed_tfrees) o norm_hhf)
wenzelm@17980
   156
    thy xs asms prop tac;
wenzelm@17980
   157
wenzelm@17980
   158
fun prove thy xs asms prop tac = hd (prove_multi thy xs asms [prop] tac);
wenzelm@17980
   159
wenzelm@17980
   160
fun prove_multi_plain thy xs asms prop tac = gen_prove (K norm_hhf_plain) thy xs asms prop tac;
wenzelm@17980
   161
fun prove_plain thy xs asms prop tac = hd (prove_multi_plain thy xs asms [prop] tac);
wenzelm@17980
   162
wenzelm@17980
   163
end;
wenzelm@17980
   164
wenzelm@17980
   165
wenzelm@18027
   166
(* prove_raw -- no checks, no normalization of result! *)
wenzelm@18027
   167
wenzelm@18027
   168
fun prove_raw casms cprop tac =
wenzelm@18027
   169
  (case SINGLE (tac (map (norm_hhf o Thm.assume) casms)) (init cprop) of
wenzelm@18027
   170
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@18027
   171
  | NONE => raise ERROR_MESSAGE "Tactic failed.");
wenzelm@18027
   172
wenzelm@18027
   173
wenzelm@17980
   174
(* SELECT_GOAL *)
wenzelm@17980
   175
wenzelm@17980
   176
(*Tactical for restricting the effect of a tactic to subgoal i.  Works
wenzelm@17980
   177
  by making a new state from subgoal i, applying tac to it, and
wenzelm@17980
   178
  composing the resulting thm with the original state.*)
wenzelm@17980
   179
wenzelm@17980
   180
fun SELECT tac i st =
wenzelm@17980
   181
  init (Thm.adjust_maxidx (List.nth (Drule.cprems_of st, i - 1)))
wenzelm@17980
   182
  |> tac
wenzelm@17980
   183
  |> Seq.maps (fn st' => Thm.bicompose false (false, conclude st', Thm.nprems_of st') i st);
wenzelm@17980
   184
wenzelm@17980
   185
fun SELECT_GOAL tac i st =
wenzelm@17980
   186
  let val n = Thm.nprems_of st in
wenzelm@17980
   187
    if 1 <= i andalso i <= n then
wenzelm@17980
   188
      if n = 1 then tac st else SELECT tac i st
wenzelm@17980
   189
    else Seq.empty
wenzelm@17980
   190
  end;
wenzelm@17980
   191
wenzelm@17980
   192
end;
wenzelm@17980
   193
wenzelm@17980
   194
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   195
open BasicGoal;