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