src/Pure/goal.ML
author wenzelm
Tue Sep 29 11:49:22 2009 +0200 (2009-09-29)
changeset 32738 15bb09ca0378
parent 32365 9b74d0339c44
child 32788 a65deb8f9434
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@29345
     2
    Author:     Makarius
wenzelm@17980
     3
wenzelm@18139
     4
Goals in tactical theorem proving.
wenzelm@17980
     5
*)
wenzelm@17980
     6
wenzelm@17980
     7
signature BASIC_GOAL =
wenzelm@17980
     8
sig
wenzelm@32738
     9
  val parallel_proofs: int Unsynchronized.ref
wenzelm@17980
    10
  val SELECT_GOAL: tactic -> int -> tactic
wenzelm@23418
    11
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@23414
    12
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@32365
    13
  val PARALLEL_CHOICE: tactic list -> tactic
wenzelm@32365
    14
  val PARALLEL_GOALS: tactic -> tactic
wenzelm@17980
    15
end;
wenzelm@17980
    16
wenzelm@17980
    17
signature GOAL =
wenzelm@17980
    18
sig
wenzelm@17980
    19
  include BASIC_GOAL
wenzelm@17980
    20
  val init: cterm -> thm
wenzelm@18027
    21
  val protect: thm -> thm
wenzelm@17980
    22
  val conclude: thm -> thm
wenzelm@32197
    23
  val check_finished: Proof.context -> thm -> unit
wenzelm@32197
    24
  val finish: Proof.context -> thm -> thm
wenzelm@21604
    25
  val norm_result: thm -> thm
wenzelm@29448
    26
  val future_enabled: unit -> bool
wenzelm@32061
    27
  val local_future_enabled: unit -> bool
wenzelm@28973
    28
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@23356
    29
  val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@20290
    30
  val prove_multi: Proof.context -> string list -> term list -> term list ->
wenzelm@20290
    31
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@28446
    32
  val prove_future: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    33
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    34
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    35
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@26713
    36
  val prove_global: theory -> string list -> term list -> term ->
wenzelm@26713
    37
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@19184
    38
  val extract: int -> int -> thm -> thm Seq.seq
wenzelm@19184
    39
  val retrofit: int -> int -> thm -> thm -> thm Seq.seq
wenzelm@23418
    40
  val conjunction_tac: int -> tactic
wenzelm@23414
    41
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    42
  val recover_conjunction_tac: tactic
wenzelm@21687
    43
  val norm_hhf_tac: int -> tactic
wenzelm@21687
    44
  val compose_hhf_tac: thm -> int -> tactic
wenzelm@23237
    45
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    46
end;
wenzelm@17980
    47
wenzelm@17980
    48
structure Goal: GOAL =
wenzelm@17980
    49
struct
wenzelm@17980
    50
wenzelm@18027
    51
(** goals **)
wenzelm@18027
    52
wenzelm@18027
    53
(*
wenzelm@18027
    54
  -------- (init)
wenzelm@18027
    55
  C ==> #C
wenzelm@18027
    56
*)
wenzelm@20290
    57
val init =
wenzelm@22902
    58
  let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI))
wenzelm@20290
    59
  in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end;
wenzelm@17980
    60
wenzelm@17980
    61
(*
wenzelm@18119
    62
   C
wenzelm@18119
    63
  --- (protect)
wenzelm@18119
    64
  #C
wenzelm@17980
    65
*)
wenzelm@29345
    66
fun protect th = Drule.comp_no_flatten (th, 0) 1 Drule.protectI;
wenzelm@17980
    67
wenzelm@17980
    68
(*
wenzelm@18027
    69
  A ==> ... ==> #C
wenzelm@18027
    70
  ---------------- (conclude)
wenzelm@17980
    71
  A ==> ... ==> C
wenzelm@17980
    72
*)
wenzelm@29345
    73
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    74
wenzelm@17980
    75
(*
wenzelm@18027
    76
  #C
wenzelm@18027
    77
  --- (finish)
wenzelm@18027
    78
   C
wenzelm@17983
    79
*)
wenzelm@32197
    80
fun check_finished ctxt th =
wenzelm@17980
    81
  (case Thm.nprems_of th of
wenzelm@32197
    82
    0 => ()
wenzelm@17980
    83
  | n => raise THM ("Proof failed.\n" ^
wenzelm@32197
    84
      Pretty.string_of (Pretty.chunks
wenzelm@32197
    85
        (Goal_Display.pretty_goals ctxt {total = true, main = true, maxgoals = n} th)) ^
wenzelm@32197
    86
      "\n" ^ string_of_int n ^ " unsolved goal(s)!", 0, [th]));
wenzelm@32197
    87
wenzelm@32197
    88
fun finish ctxt th = (check_finished ctxt th; conclude th);
wenzelm@17980
    89
wenzelm@17980
    90
wenzelm@18027
    91
wenzelm@18027
    92
(** results **)
wenzelm@18027
    93
wenzelm@28340
    94
(* normal form *)
wenzelm@28340
    95
wenzelm@21604
    96
val norm_result =
wenzelm@21604
    97
  Drule.flexflex_unique
wenzelm@21604
    98
  #> MetaSimplifier.norm_hhf_protect
wenzelm@21604
    99
  #> Thm.strip_shyps
wenzelm@21604
   100
  #> Drule.zero_var_indexes;
wenzelm@21604
   101
wenzelm@21604
   102
wenzelm@29448
   103
(* future_enabled *)
wenzelm@29448
   104
wenzelm@32738
   105
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@29448
   106
wenzelm@29448
   107
fun future_enabled () =
wenzelm@32255
   108
  Multithreading.enabled () andalso Future.is_worker () andalso ! parallel_proofs >= 1;
wenzelm@32061
   109
wenzelm@32061
   110
fun local_future_enabled () = future_enabled () andalso ! parallel_proofs >= 2;
wenzelm@29448
   111
wenzelm@29448
   112
wenzelm@28446
   113
(* future_result *)
wenzelm@28340
   114
wenzelm@28446
   115
fun future_result ctxt result prop =
wenzelm@28340
   116
  let
wenzelm@28340
   117
    val thy = ProofContext.theory_of ctxt;
wenzelm@28340
   118
    val _ = Context.reject_draft thy;
wenzelm@28340
   119
    val cert = Thm.cterm_of thy;
wenzelm@28340
   120
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   121
wenzelm@30473
   122
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   123
    val As = map Thm.term_of assms;
wenzelm@28340
   124
wenzelm@28340
   125
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   126
    val fixes = map cert xs;
wenzelm@28340
   127
wenzelm@28340
   128
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   129
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   130
wenzelm@28340
   131
    val global_prop =
wenzelm@32056
   132
      cert (Term.map_types Logic.varifyT (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   133
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   134
    val global_result = result |> Future.map
wenzelm@28973
   135
      (Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   136
        Drule.implies_intr_list assms #>
wenzelm@28973
   137
        Drule.forall_intr_list fixes #>
wenzelm@28973
   138
        Thm.generalize (map #1 tfrees, []) 0);
wenzelm@28340
   139
    val local_result =
wenzelm@32056
   140
      Thm.future global_result global_prop
wenzelm@28340
   141
      |> Thm.instantiate (instT, [])
wenzelm@28340
   142
      |> Drule.forall_elim_list fixes
wenzelm@28340
   143
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   144
  in local_result end;
wenzelm@28340
   145
wenzelm@28340
   146
wenzelm@18027
   147
wenzelm@18027
   148
(** tactical theorem proving **)
wenzelm@18027
   149
wenzelm@23356
   150
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   151
wenzelm@23356
   152
fun prove_internal casms cprop tac =
wenzelm@20250
   153
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@32197
   154
    SOME th => Drule.implies_intr_list casms
wenzelm@32197
   155
      (finish (Syntax.init_pretty_global (Thm.theory_of_thm th)) th)
wenzelm@20250
   156
  | NONE => error "Tactic failed.");
wenzelm@20250
   157
wenzelm@20250
   158
wenzelm@28340
   159
(* prove_common etc. *)
wenzelm@17986
   160
wenzelm@28340
   161
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   162
  let
wenzelm@21516
   163
    val thy = ProofContext.theory_of ctxt;
wenzelm@26939
   164
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   165
wenzelm@28355
   166
    val pos = Position.thread_data ();
wenzelm@20250
   167
    fun err msg = cat_error msg
wenzelm@20250
   168
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   169
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@28355
   170
        (case Position.str_of pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   171
wenzelm@20250
   172
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   173
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   174
    val casms = map cert_safe asms;
wenzelm@20250
   175
    val cprops = map cert_safe props;
wenzelm@17980
   176
wenzelm@20250
   177
    val (prems, ctxt') = ctxt
wenzelm@20250
   178
      |> Variable.add_fixes_direct xs
wenzelm@27218
   179
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   180
      |> Assumption.add_assumes casms
wenzelm@26713
   181
      ||> Variable.set_body true;
wenzelm@28619
   182
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   183
wenzelm@28619
   184
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   185
wenzelm@28340
   186
    fun result () =
wenzelm@28340
   187
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@19774
   188
        NONE => err "Tactic failed."
wenzelm@28340
   189
      | SOME st =>
wenzelm@32197
   190
          let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
wenzelm@28619
   191
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   192
            then Thm.check_shyps sorts res
wenzelm@28340
   193
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   194
          end);
wenzelm@28340
   195
    val res =
wenzelm@29448
   196
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
wenzelm@29088
   197
      then result ()
wenzelm@32062
   198
      else future_result ctxt' (Future.fork_pri ~1 result) (Thm.term_of stmt);
wenzelm@17980
   199
  in
wenzelm@28340
   200
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   201
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   202
    |> Variable.export ctxt' ctxt
wenzelm@20250
   203
    |> map Drule.zero_var_indexes
wenzelm@17980
   204
  end;
wenzelm@17980
   205
wenzelm@28341
   206
val prove_multi = prove_common true;
wenzelm@17980
   207
wenzelm@28446
   208
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@28340
   209
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   210
wenzelm@20056
   211
fun prove_global thy xs asms prop tac =
wenzelm@26713
   212
  Drule.standard (prove (ProofContext.init thy) xs asms prop tac);
wenzelm@18027
   213
wenzelm@18027
   214
wenzelm@17980
   215
wenzelm@21687
   216
(** goal structure **)
wenzelm@21687
   217
wenzelm@21687
   218
(* nested goals *)
wenzelm@18207
   219
wenzelm@19184
   220
fun extract i n st =
wenzelm@19184
   221
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   222
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   223
   else
wenzelm@23418
   224
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   225
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   226
wenzelm@19221
   227
fun retrofit i n st' st =
wenzelm@19221
   228
  (if n = 1 then st
wenzelm@23418
   229
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   230
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   231
wenzelm@17980
   232
fun SELECT_GOAL tac i st =
wenzelm@19191
   233
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   234
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   235
wenzelm@21687
   236
wenzelm@21687
   237
(* multiple goals *)
wenzelm@21687
   238
wenzelm@23418
   239
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   240
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   241
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   242
wenzelm@23418
   243
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   244
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   245
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   246
    else no_tac));
wenzelm@21687
   247
wenzelm@23418
   248
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   249
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   250
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   251
wenzelm@23418
   252
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   253
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   254
wenzelm@23414
   255
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   256
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   257
    THEN tac
wenzelm@23418
   258
    THEN recover_conjunction_tac);
wenzelm@23414
   259
wenzelm@21687
   260
fun CONJUNCTS tac =
wenzelm@21687
   261
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   262
    THEN tac
wenzelm@23418
   263
    THEN recover_conjunction_tac);
wenzelm@21687
   264
wenzelm@21687
   265
wenzelm@21687
   266
(* hhf normal form *)
wenzelm@21687
   267
wenzelm@21687
   268
val norm_hhf_tac =
wenzelm@21687
   269
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   270
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   271
    if Drule.is_norm_hhf t then all_tac
wenzelm@28619
   272
    else MetaSimplifier.rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   273
wenzelm@25301
   274
fun compose_hhf_tac th i st =
wenzelm@25301
   275
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   276
wenzelm@23237
   277
wenzelm@23237
   278
(* non-atomic goal assumptions *)
wenzelm@23237
   279
wenzelm@23356
   280
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   281
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   282
  | non_atomic _ = false;
wenzelm@23356
   283
wenzelm@23237
   284
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   285
  let
wenzelm@23237
   286
    val ((_, goal'), ctxt') = Variable.focus goal ctxt;
wenzelm@23237
   287
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   288
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   289
    val tacs = Rs |> map (fn R =>
wenzelm@23237
   290
      Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   291
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   292
wenzelm@32365
   293
wenzelm@32365
   294
(* parallel tacticals *)
wenzelm@32365
   295
wenzelm@32365
   296
(*parallel choice of single results*)
wenzelm@32365
   297
fun PARALLEL_CHOICE tacs st =
wenzelm@32365
   298
  (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
wenzelm@32365
   299
    NONE => Seq.empty
wenzelm@32365
   300
  | SOME st' => Seq.single st');
wenzelm@32365
   301
wenzelm@32365
   302
(*parallel refinement of non-schematic goal by single results*)
wenzelm@32365
   303
fun PARALLEL_GOALS tac st =
wenzelm@32365
   304
  let
wenzelm@32365
   305
    val st0 = Thm.adjust_maxidx_thm ~1 st;
wenzelm@32365
   306
    val _ = Thm.maxidx_of st0 >= 0 andalso
wenzelm@32365
   307
      raise THM ("PARALLEL_GOALS: schematic goal state", 0, [st]);
wenzelm@32365
   308
wenzelm@32365
   309
    exception FAILED;
wenzelm@32365
   310
    fun try_tac g =
wenzelm@32365
   311
      (case SINGLE tac g of
wenzelm@32365
   312
        NONE => raise FAILED
wenzelm@32365
   313
      | SOME g' => g');
wenzelm@32365
   314
wenzelm@32365
   315
    val goals = Drule.strip_imp_prems (Thm.cprop_of st0);
wenzelm@32365
   316
    val results = Par_List.map (try_tac o init) goals;
wenzelm@32365
   317
  in ALLGOALS (fn i => retrofit i 1 (nth results (i - 1))) st0 end
wenzelm@32365
   318
  handle FAILED => Seq.empty;
wenzelm@32365
   319
wenzelm@18207
   320
end;
wenzelm@18207
   321
wenzelm@32365
   322
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   323
open Basic_Goal;