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