src/Pure/goal.ML
author wenzelm
Mon Jun 18 23:30:46 2007 +0200 (2007-06-18)
changeset 23414 927203ad4b3a
parent 23356 dbe3731241c3
child 23418 c195f6f13769
permissions -rw-r--r--
tuned conjunction tactics: slightly smaller proof terms;
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@23414
    11
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@21687
    12
  val CONJUNCTS: 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@23356
    24
  val prove_internal: 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@23414
    32
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@21687
    33
  val conjunction_tac: 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@23237
    40
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    41
end;
wenzelm@17980
    42
wenzelm@17980
    43
structure Goal: GOAL =
wenzelm@17980
    44
struct
wenzelm@17980
    45
wenzelm@18027
    46
(** goals **)
wenzelm@18027
    47
wenzelm@18027
    48
(*
wenzelm@18027
    49
  -------- (init)
wenzelm@18027
    50
  C ==> #C
wenzelm@18027
    51
*)
wenzelm@20290
    52
val init =
wenzelm@22902
    53
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    54
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    55
wenzelm@17980
    56
(*
wenzelm@18119
    57
   C
wenzelm@18119
    58
  --- (protect)
wenzelm@18119
    59
  #C
wenzelm@17980
    60
*)
wenzelm@21579
    61
fun protect th = th COMP_INCR Drule.protectI;
wenzelm@17980
    62
wenzelm@17980
    63
(*
wenzelm@18027
    64
  A ==> ... ==> #C
wenzelm@18027
    65
  ---------------- (conclude)
wenzelm@17980
    66
  A ==> ... ==> C
wenzelm@17980
    67
*)
wenzelm@17980
    68
fun conclude th =
wenzelm@18497
    69
  (case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1)
wenzelm@18027
    70
      (Drule.incr_indexes th Drule.protectD) of
wenzelm@17980
    71
    SOME th' => th'
wenzelm@17980
    72
  | NONE => raise THM ("Failed to conclude goal", 0, [th]));
wenzelm@17980
    73
wenzelm@17980
    74
(*
wenzelm@18027
    75
  #C
wenzelm@18027
    76
  --- (finish)
wenzelm@18027
    77
   C
wenzelm@17983
    78
*)
wenzelm@17980
    79
fun finish th =
wenzelm@17980
    80
  (case Thm.nprems_of th of
wenzelm@17980
    81
    0 => conclude th
wenzelm@17980
    82
  | n => raise THM ("Proof failed.\n" ^
wenzelm@17980
    83
      Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^
wenzelm@17980
    84
      ("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th]));
wenzelm@17980
    85
wenzelm@17980
    86
wenzelm@18027
    87
wenzelm@18027
    88
(** results **)
wenzelm@18027
    89
wenzelm@21604
    90
(* normal form *)
wenzelm@21604
    91
wenzelm@21604
    92
val norm_result =
wenzelm@21604
    93
  Drule.flexflex_unique
wenzelm@21604
    94
  #> MetaSimplifier.norm_hhf_protect
wenzelm@21604
    95
  #> Thm.strip_shyps
wenzelm@21604
    96
  #> Drule.zero_var_indexes;
wenzelm@21604
    97
wenzelm@21604
    98
val close_result =
wenzelm@21604
    99
  Thm.compress
wenzelm@21604
   100
  #> Drule.close_derivation;
wenzelm@21604
   101
wenzelm@21604
   102
wenzelm@18027
   103
wenzelm@18027
   104
(** tactical theorem proving **)
wenzelm@18027
   105
wenzelm@23356
   106
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   107
wenzelm@23356
   108
fun prove_internal casms cprop tac =
wenzelm@20250
   109
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@20250
   110
    SOME th => Drule.implies_intr_list casms (finish th)
wenzelm@20250
   111
  | NONE => error "Tactic failed.");
wenzelm@20250
   112
wenzelm@20250
   113
wenzelm@18119
   114
(* prove_multi *)
wenzelm@17986
   115
wenzelm@20056
   116
fun prove_multi ctxt xs asms props tac =
wenzelm@17980
   117
  let
wenzelm@21516
   118
    val thy = ProofContext.theory_of ctxt;
wenzelm@20056
   119
    val string_of_term = Sign.string_of_term thy;
wenzelm@20056
   120
wenzelm@20250
   121
    fun err msg = cat_error msg
wenzelm@20250
   122
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@20250
   123
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   124
wenzelm@20250
   125
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   126
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   127
    val casms = map cert_safe asms;
wenzelm@20250
   128
    val cprops = map cert_safe props;
wenzelm@17980
   129
wenzelm@20250
   130
    val (prems, ctxt') = ctxt
wenzelm@20250
   131
      |> Variable.add_fixes_direct xs
wenzelm@20250
   132
      |> fold Variable.declare_internal (asms @ props)
wenzelm@20250
   133
      |> Assumption.add_assumes casms;
wenzelm@17980
   134
wenzelm@20250
   135
    val goal = init (Conjunction.mk_conjunction_list cprops);
wenzelm@19774
   136
    val res =
wenzelm@20250
   137
      (case SINGLE (tac {prems = prems, context = ctxt'}) goal of
wenzelm@19774
   138
        NONE => err "Tactic failed."
wenzelm@19774
   139
      | SOME res => res);
wenzelm@20250
   140
    val [results] = Conjunction.elim_precise [length props] (finish res)
wenzelm@20250
   141
      handle THM (msg, _, _) => err msg;
wenzelm@20250
   142
    val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results)
wenzelm@20056
   143
      orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res));
wenzelm@17980
   144
  in
wenzelm@20056
   145
    results
wenzelm@20290
   146
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   147
    |> Variable.export ctxt' ctxt
wenzelm@20250
   148
    |> map Drule.zero_var_indexes
wenzelm@17980
   149
  end;
wenzelm@17980
   150
wenzelm@17980
   151
wenzelm@18119
   152
(* prove *)
wenzelm@17980
   153
wenzelm@20056
   154
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac);
wenzelm@20056
   155
wenzelm@20056
   156
fun prove_global thy xs asms prop tac =
wenzelm@21516
   157
  Drule.standard (prove (ProofContext.init thy) xs asms prop (fn {prems, ...} => tac prems));
wenzelm@18027
   158
wenzelm@18027
   159
wenzelm@17980
   160
wenzelm@21687
   161
(** goal structure **)
wenzelm@21687
   162
wenzelm@21687
   163
(* nested goals *)
wenzelm@18207
   164
wenzelm@19184
   165
fun extract i n st =
wenzelm@19184
   166
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   167
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@19423
   168
   else Seq.single (foldr1 Conjunction.mk_conjunction (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   169
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   170
wenzelm@19221
   171
fun retrofit i n st' st =
wenzelm@19221
   172
  (if n = 1 then st
wenzelm@19423
   173
   else st |> Drule.rotate_prems (i - 1) |> Conjunction.uncurry n |> Drule.rotate_prems (1 - i))
wenzelm@19221
   174
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   175
wenzelm@17980
   176
fun SELECT_GOAL tac i st =
wenzelm@19191
   177
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   178
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   179
wenzelm@21687
   180
wenzelm@21687
   181
(* multiple goals *)
wenzelm@21687
   182
wenzelm@23414
   183
local
wenzelm@21687
   184
wenzelm@23414
   185
fun conj_intrs n =
wenzelm@23414
   186
  let
wenzelm@23414
   187
    val cert = Thm.cterm_of ProtoPure.thy;
wenzelm@23414
   188
    val names = Name.invents Name.context "A" n;
wenzelm@23414
   189
    val As = map (fn name => cert (Free (name, propT))) names;
wenzelm@23414
   190
  in
wenzelm@23414
   191
    Thm.generalize ([], names) 0
wenzelm@23414
   192
      (Drule.implies_intr_list As (Conjunction.intr_list (map Thm.assume As)))
wenzelm@23414
   193
  end;
wenzelm@23414
   194
wenzelm@23414
   195
fun count_conjs A =
wenzelm@23414
   196
  (case try Logic.dest_conjunction A of
wenzelm@23414
   197
    NONE => 1
wenzelm@23414
   198
  | SOME (_, B) => count_conjs B + 1);
wenzelm@23414
   199
wenzelm@23414
   200
in
wenzelm@21687
   201
wenzelm@21687
   202
val precise_conjunction_tac =
wenzelm@21687
   203
  let
wenzelm@21687
   204
    fun tac 0 i = eq_assume_tac i
wenzelm@21687
   205
      | tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23414
   206
      | tac 2 i = rtac Conjunction.conjunctionI i
wenzelm@23414
   207
      | tac n i = rtac (conj_intrs n) i;
wenzelm@21687
   208
  in TRY oo tac end;
wenzelm@21687
   209
wenzelm@23414
   210
val conjunction_tac = TRY o REPEAT_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@23414
   211
  let val n = count_conjs goal
wenzelm@23414
   212
  in if n < 2 then no_tac else precise_conjunction_tac n i end));
wenzelm@23414
   213
wenzelm@23414
   214
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   215
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   216
    THEN tac
wenzelm@23414
   217
    THEN PRIMITIVE (Conjunction.uncurry ~1));
wenzelm@23414
   218
wenzelm@21687
   219
fun CONJUNCTS tac =
wenzelm@21687
   220
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   221
    THEN tac
wenzelm@21687
   222
    THEN PRIMITIVE (Conjunction.uncurry ~1));
wenzelm@21687
   223
wenzelm@23414
   224
end;
wenzelm@21687
   225
wenzelm@21687
   226
wenzelm@21687
   227
(* rewriting *)
wenzelm@21687
   228
wenzelm@21687
   229
(*Rewrite subgoal i only.  SELECT_GOAL avoids inefficiencies in goals_conv.*)
wenzelm@21687
   230
fun asm_rewrite_goal_tac mode prover_tac ss =
wenzelm@21687
   231
  SELECT_GOAL
wenzelm@21687
   232
    (PRIMITIVE (MetaSimplifier.rewrite_goal_rule mode (SINGLE o prover_tac) ss 1));
wenzelm@21687
   233
wenzelm@21687
   234
fun rewrite_goal_tac rews =
wenzelm@21687
   235
  let val ss = MetaSimplifier.empty_ss addsimps rews in
wenzelm@21687
   236
    fn i => fn st => asm_rewrite_goal_tac (true, false, false) (K no_tac)
wenzelm@21687
   237
      (MetaSimplifier.theory_context (Thm.theory_of_thm st) ss) i st
wenzelm@21687
   238
  end;
wenzelm@21687
   239
wenzelm@21687
   240
wenzelm@21687
   241
(* hhf normal form *)
wenzelm@21687
   242
wenzelm@21687
   243
val norm_hhf_tac =
wenzelm@21687
   244
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   245
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   246
    if Drule.is_norm_hhf t then all_tac
wenzelm@21687
   247
    else rewrite_goal_tac [Drule.norm_hhf_eq] i);
wenzelm@21687
   248
wenzelm@21687
   249
fun compose_hhf tha i thb =
wenzelm@21687
   250
  Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of thb i) tha, 0) i thb;
wenzelm@21687
   251
wenzelm@21687
   252
fun compose_hhf_tac th i = PRIMSEQ (compose_hhf th i);
wenzelm@21687
   253
wenzelm@21687
   254
fun comp_hhf tha thb =
wenzelm@21687
   255
  (case Seq.chop 2 (compose_hhf tha 1 thb) of
wenzelm@21687
   256
    ([th], _) => th
wenzelm@21687
   257
  | ([], _) => raise THM ("comp_hhf: no unifiers", 1, [tha, thb])
wenzelm@21687
   258
  | _  => raise THM ("comp_hhf: multiple unifiers", 1, [tha, thb]));
wenzelm@21687
   259
wenzelm@23237
   260
wenzelm@23237
   261
(* non-atomic goal assumptions *)
wenzelm@23237
   262
wenzelm@23356
   263
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   264
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   265
  | non_atomic _ = false;
wenzelm@23356
   266
wenzelm@23237
   267
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   268
  let
wenzelm@23237
   269
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   270
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   271
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   272
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   273
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   274
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   275
wenzelm@18207
   276
end;
wenzelm@18207
   277
wenzelm@17980
   278
structure BasicGoal: BASIC_GOAL = Goal;
wenzelm@17980
   279
open BasicGoal;