src/Pure/goal.ML
author wenzelm
Thu May 10 00:39:48 2007 +0200 (2007-05-10)
changeset 22902 ac833b4bb7ee
parent 21687 f689f729afab
child 23237 ac9d126456e1
permissions -rw-r--r--
moved some Drule operations to Thm (see more_thm.ML);
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@21687
    11
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@21687
    12
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@17980
    13
end;
wenzelm@17980
    14
wenzelm@17980
    15
signature GOAL =
wenzelm@17980
    16
sig
wenzelm@17980
    17
  include BASIC_GOAL
wenzelm@17980
    18
  val init: cterm -> thm
wenzelm@18027
    19
  val protect: thm -> thm
wenzelm@17980
    20
  val conclude: thm -> thm
wenzelm@17980
    21
  val finish: thm -> thm
wenzelm@21604
    22
  val norm_result: thm -> thm
wenzelm@21604
    23
  val close_result: thm -> thm
wenzelm@20250
    24
  val prove_raw: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@20290
    25
  val prove_multi: Proof.context -> string list -> term list -> term list ->
wenzelm@20290
    26
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@20290
    27
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    28
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20056
    29
  val prove_global: theory -> string list -> term list -> term -> (thm list -> tactic) -> thm
wenzelm@19184
    30
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    31
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@21687
    32
  val conjunction_tac: int -> tactic
wenzelm@21687
    33
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@21687
    34
  val asm_rewrite_goal_tac: bool * bool * bool -> (simpset -> tactic) -> simpset -> int -> tactic
wenzelm@21687
    35
  val rewrite_goal_tac: thm list -> int -> tactic
wenzelm@21687
    36
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    37
  val compose_hhf: thm -> int -> thm -> thm Seq.seq
wenzelm@21687
    38
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@21687
    39
  val comp_hhf: thm -> thm -> thm
wenzelm@17980
    40
end;
wenzelm@17980
    41
wenzelm@17980
    42
structure Goal: GOAL =
wenzelm@17980
    43
struct
wenzelm@17980
    44
wenzelm@18027
    45
(** goals **)
wenzelm@18027
    46
wenzelm@18027
    47
(*
wenzelm@18027
    48
  -------- (init)
wenzelm@18027
    49
  C ==> #C
wenzelm@18027
    50
*)
wenzelm@20290
    51
val init =
wenzelm@22902
    52
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    53
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    54
wenzelm@17980
    55
(*
wenzelm@18119
    56
   C
wenzelm@18119
    57
  --- (protect)
wenzelm@18119
    58
  #C
wenzelm@17980
    59
*)
wenzelm@21579
    60
fun protect th = th COMP_INCR Drule.protectI;
wenzelm@17980
    61
wenzelm@17980
    62
(*
wenzelm@18027
    63
  A ==> ... ==> #C
wenzelm@18027
    64
  ---------------- (conclude)
wenzelm@17980
    65
  A ==> ... ==> C
wenzelm@17980
    66
*)
wenzelm@17980
    67
fun conclude th =
wenzelm@18497
    68
  (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
wenzelm@18027
    69
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    70
    SOME th' => th'
wenzelm@17980
    71
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    72
wenzelm@17980
    73
(*
wenzelm@18027
    74
  #C
wenzelm@18027
    75
  --- (finish)
wenzelm@18027
    76
   C
wenzelm@17983
    77
*)
wenzelm@17980
    78
fun finish th =
wenzelm@17980
    79
  (case Thm.nprems_of th of
wenzelm@17980
    80
    0 => conclude th
wenzelm@17980
    81
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    82
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    83
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    84
wenzelm@17980
    85
wenzelm@18027
    86
wenzelm@18027
    87
(** results **)
wenzelm@18027
    88
wenzelm@21604
    89
(* normal form *)
wenzelm@21604
    90
wenzelm@21604
    91
val norm_result =
wenzelm@21604
    92
  Drule.flexflex_unique
wenzelm@21604
    93
  #> MetaSimplifier.norm_hhf_protect
wenzelm@21604
    94
  #> Thm.strip_shyps
wenzelm@21604
    95
  #> Drule.zero_var_indexes;
wenzelm@21604
    96
wenzelm@21604
    97
val close_result =
wenzelm@21604
    98
  Thm.compress
wenzelm@21604
    99
  #> Drule.close_derivation;
wenzelm@21604
   100
wenzelm@21604
   101
wenzelm@18027
   102
wenzelm@18027
   103
(** tactical theorem proving **)
wenzelm@18027
   104
wenzelm@20250
   105
(* prove_raw -- no checks, no normalization of result! *)
wenzelm@20250
   106
wenzelm@20250
   107
fun prove_raw casms cprop tac =
wenzelm@20250
   108
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@20250
   109
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@20250
   110
  | NONE => error "Tactic failed.");
wenzelm@20250
   111
wenzelm@20250
   112
wenzelm@18119
   113
(* prove_multi *)
wenzelm@17986
   114
wenzelm@20056
   115
fun prove_multi ctxt xs asms props tac =
wenzelm@17980
   116
  let
wenzelm@21516
   117
    val thy = ProofContext.theory_of ctxt;
wenzelm@20056
   118
    val string_of_term = Sign.string_of_term thy;
wenzelm@20056
   119
wenzelm@20250
   120
    fun err msg = cat_error msg
wenzelm@20250
   121
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@20250
   122
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   123
wenzelm@20250
   124
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   125
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   126
    val casms = map cert_safe asms;
wenzelm@20250
   127
    val cprops = map cert_safe props;
wenzelm@17980
   128
wenzelm@20250
   129
    val (prems, ctxt') = ctxt
wenzelm@20250
   130
      |> Variable.add_fixes_direct xs
wenzelm@20250
   131
      |> fold Variable.declare_internal (asms @ props)
wenzelm@20250
   132
      |> Assumption.add_assumes casms;
wenzelm@17980
   133
wenzelm@20250
   134
    val goal = init (Conjunction.mk_conjunction_list cprops);
wenzelm@19774
   135
    val res =
wenzelm@20250
   136
      (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
wenzelm@19774
   137
        NONE => err "Tactic failed."
wenzelm@19774
   138
      | SOME res => res);
wenzelm@20250
   139
    val [results] = Conjunction.elim_precise [length props] (finish res)
wenzelm@20250
   140
      handle THM (msg, _, _) => err msg;
wenzelm@20250
   141
    val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
wenzelm@20056
   142
      orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
wenzelm@17980
   143
  in
wenzelm@20056
   144
    results
wenzelm@20290
   145
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   146
    |> Variable.export ctxt' ctxt
wenzelm@20250
   147
    |> map Drule.zero_var_indexes
wenzelm@17980
   148
  end;
wenzelm@17980
   149
wenzelm@17980
   150
wenzelm@18119
   151
(* prove *)
wenzelm@17980
   152
wenzelm@20056
   153
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
wenzelm@20056
   154
wenzelm@20056
   155
fun prove_global thy xs asms prop tac =
wenzelm@21516
   156
  Drule.standard (prove (ProofContext.init thy) xs asms prop (fn {prems, ...} => tac prems));
wenzelm@18027
   157
wenzelm@18027
   158
wenzelm@17980
   159
wenzelm@21687
   160
(** goal structure **)
wenzelm@21687
   161
wenzelm@21687
   162
(* nested goals *)
wenzelm@18207
   163
wenzelm@19184
   164
fun extract i n st =
wenzelm@19184
   165
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   166
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@19423
   167
   else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   168
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   169
wenzelm@19221
   170
fun retrofit i n st' st =
wenzelm@19221
   171
  (if n = 1 then st
wenzelm@19423
   172
   else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
wenzelm@19221
   173
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   174
wenzelm@17980
   175
fun SELECT_GOAL tac i st =
wenzelm@19191
   176
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   177
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   178
wenzelm@21687
   179
wenzelm@21687
   180
(* multiple goals *)
wenzelm@21687
   181
wenzelm@21687
   182
val conj_tac = SUBGOAL (fn (goal, i) =>
wenzelm@21687
   183
  if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@21687
   184
  else no_tac);
wenzelm@21687
   185
wenzelm@21687
   186
val conjunction_tac = TRY o REPEAT_ALL_NEW conj_tac;
wenzelm@21687
   187
wenzelm@21687
   188
val precise_conjunction_tac =
wenzelm@21687
   189
  let
wenzelm@21687
   190
    fun tac 0 i = eq_assume_tac i
wenzelm@21687
   191
      | tac 1 i = SUBGOAL (K all_tac) i
wenzelm@21687
   192
      | tac n i = conj_tac i THEN TRY (fn st => tac (n - 1) (i + 1) st);
wenzelm@21687
   193
  in TRY oo tac end;
wenzelm@21687
   194
wenzelm@21687
   195
fun CONJUNCTS tac =
wenzelm@21687
   196
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   197
    THEN tac
wenzelm@21687
   198
    THEN PRIMITIVE (Conjunction.uncurry ~1));
wenzelm@21687
   199
wenzelm@21687
   200
fun PRECISE_CONJUNCTS n tac =
wenzelm@21687
   201
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@21687
   202
    THEN tac
wenzelm@21687
   203
    THEN PRIMITIVE (Conjunction.uncurry ~1));
wenzelm@21687
   204
wenzelm@21687
   205
wenzelm@21687
   206
(* rewriting *)
wenzelm@21687
   207
wenzelm@21687
   208
(*Rewrite subgoal i only.  SELECT_GOAL avoids inefficiencies in goals_conv.*)
wenzelm@21687
   209
fun asm_rewrite_goal_tac mode prover_tac ss =
wenzelm@21687
   210
  SELECT_GOAL
wenzelm@21687
   211
    (PRIMITIVE (MetaSimplifier.rewrite_goal_rule mode (SINGLE o prover_tac) ss 1));
wenzelm@21687
   212
wenzelm@21687
   213
fun rewrite_goal_tac rews =
wenzelm@21687
   214
  let val ss = MetaSimplifier.empty_ss addsimps rews in
wenzelm@21687
   215
    fn i => fn st => asm_rewrite_goal_tac (true, false, false) (K no_tac)
wenzelm@21687
   216
      (MetaSimplifier.theory_context (Thm.theory_of_thm st) ss) i st
wenzelm@21687
   217
  end;
wenzelm@21687
   218
wenzelm@21687
   219
wenzelm@21687
   220
(* hhf normal form *)
wenzelm@21687
   221
wenzelm@21687
   222
val norm_hhf_tac =
wenzelm@21687
   223
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   224
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   225
    if Drule.is_norm_hhf t then all_tac
wenzelm@21687
   226
    else rewrite_goal_tac [Drule.norm_hhf_eq] i);
wenzelm@21687
   227
wenzelm@21687
   228
fun compose_hhf tha i thb =
wenzelm@21687
   229
  Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
wenzelm@21687
   230
wenzelm@21687
   231
fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
wenzelm@21687
   232
wenzelm@21687
   233
fun comp_hhf tha thb =
wenzelm@21687
   234
  (case Seq.chop 2 (compose_hhf tha 1 thb) of
wenzelm@21687
   235
    ([th], _) => th
wenzelm@21687
   236
  | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
wenzelm@21687
   237
  | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
wenzelm@21687
   238
wenzelm@18207
   239
end;
wenzelm@18207
   240
wenzelm@17980
   241
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   242
open BasicGoal;