src/Pure/goal.ML
author wenzelm
Tue Apr 10 20:42:17 2012 +0200 (2012-04-10)
changeset 47415 c486ac962b89
parent 45458 5b5d3ee2285b
child 48992 0518bf89c777
permissions -rw-r--r--
tuned future priorities: print 0, goal ~1, execute ~2;
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@44302
   127
        (singleton o Future.forks)
wenzelm@47415
   128
          {name = name, group = NONE, deps = [], pri = ~1, interrupts = false}
wenzelm@41703
   129
          (fn () =>
wenzelm@41703
   130
            Exn.release
wenzelm@44114
   131
              (Exn.capture (Future.status o Future.interruptible_task) e before forked ~1));
wenzelm@41703
   132
    in future end) ();
wenzelm@29448
   133
wenzelm@41677
   134
fun fork e = fork_name "Goal.fork" e;
wenzelm@41677
   135
wenzelm@41677
   136
wenzelm@41703
   137
(* scheduling parameters *)
wenzelm@41703
   138
wenzelm@32738
   139
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@41819
   140
val parallel_proofs_threshold = Unsynchronized.ref 50;
wenzelm@29448
   141
wenzelm@29448
   142
fun future_enabled () =
wenzelm@41703
   143
  Multithreading.enabled () andalso ! parallel_proofs >= 1 andalso
wenzelm@41703
   144
  is_some (Future.worker_task ());
wenzelm@32061
   145
wenzelm@41703
   146
fun local_future_enabled () =
wenzelm@41703
   147
  future_enabled () andalso ! parallel_proofs >= 2 andalso
wenzelm@41819
   148
  Synchronized.value forked_proofs <
wenzelm@41819
   149
    ! parallel_proofs_threshold * Multithreading.max_threads_value ();
wenzelm@29448
   150
wenzelm@29448
   151
wenzelm@28446
   152
(* future_result *)
wenzelm@28340
   153
wenzelm@28446
   154
fun future_result ctxt result prop =
wenzelm@28340
   155
  let
wenzelm@42360
   156
    val thy = Proof_Context.theory_of ctxt;
wenzelm@28340
   157
    val _ = Context.reject_draft thy;
wenzelm@28340
   158
    val cert = Thm.cterm_of thy;
wenzelm@28340
   159
    val certT = Thm.ctyp_of thy;
wenzelm@28340
   160
wenzelm@30473
   161
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   162
    val As = map Thm.term_of assms;
wenzelm@28340
   163
wenzelm@28340
   164
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@28340
   165
    val fixes = map cert xs;
wenzelm@28340
   166
wenzelm@28340
   167
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@28340
   168
    val instT = map (fn (a, S) => (certT (TVar ((a, 0), S)), certT (TFree (a, S)))) tfrees;
wenzelm@28340
   169
wenzelm@28340
   170
    val global_prop =
wenzelm@45344
   171
      cert (Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop))))
wenzelm@32056
   172
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   173
    val global_result = result |> Future.map
wenzelm@33768
   174
      (Drule.flexflex_unique #>
wenzelm@33768
   175
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   176
        Drule.implies_intr_list assms #>
wenzelm@28973
   177
        Drule.forall_intr_list fixes #>
wenzelm@36613
   178
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   179
        Thm.strip_shyps);
wenzelm@28340
   180
    val local_result =
wenzelm@32056
   181
      Thm.future global_result global_prop
wenzelm@28340
   182
      |> Thm.instantiate (instT, [])
wenzelm@28340
   183
      |> Drule.forall_elim_list fixes
wenzelm@28340
   184
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   185
  in local_result end;
wenzelm@28340
   186
wenzelm@28340
   187
wenzelm@18027
   188
wenzelm@18027
   189
(** tactical theorem proving **)
wenzelm@18027
   190
wenzelm@23356
   191
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   192
wenzelm@23356
   193
fun prove_internal casms cprop tac =
wenzelm@20250
   194
  (case SINGLE (tac (map Assumption.assume casms)) (init cprop) of
wenzelm@32197
   195
    SOME th => Drule.implies_intr_list casms
wenzelm@32197
   196
      (finish (Syntax.init_pretty_global (Thm.theory_of_thm th)) th)
wenzelm@38875
   197
  | NONE => error "Tactic failed");
wenzelm@20250
   198
wenzelm@20250
   199
wenzelm@28340
   200
(* prove_common etc. *)
wenzelm@17986
   201
wenzelm@28340
   202
fun prove_common immediate ctxt xs asms props tac =
wenzelm@17980
   203
  let
wenzelm@42360
   204
    val thy = Proof_Context.theory_of ctxt;
wenzelm@26939
   205
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@20056
   206
wenzelm@28355
   207
    val pos = Position.thread_data ();
wenzelm@20250
   208
    fun err msg = cat_error msg
wenzelm@20250
   209
      ("The error(s) above occurred for the goal statement:\n" ^
wenzelm@28355
   210
        string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props)) ^
wenzelm@28355
   211
        (case Position.str_of pos of "" => "" | s => "\n" ^ s));
wenzelm@17980
   212
wenzelm@20250
   213
    fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   214
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   215
    val casms = map cert_safe asms;
wenzelm@20250
   216
    val cprops = map cert_safe props;
wenzelm@17980
   217
wenzelm@20250
   218
    val (prems, ctxt') = ctxt
wenzelm@20250
   219
      |> Variable.add_fixes_direct xs
wenzelm@27218
   220
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   221
      |> Assumption.add_assumes casms
wenzelm@26713
   222
      ||> Variable.set_body true;
wenzelm@28619
   223
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   224
wenzelm@28619
   225
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   226
wenzelm@28340
   227
    fun result () =
wenzelm@28340
   228
      (case SINGLE (tac {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   229
        NONE => err "Tactic failed"
wenzelm@28340
   230
      | SOME st =>
wenzelm@32197
   231
          let val res = finish ctxt' st handle THM (msg, _, _) => err msg in
wenzelm@28619
   232
            if Unify.matches_list thy [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@28619
   233
            then Thm.check_shyps sorts res
wenzelm@28340
   234
            else err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res))
wenzelm@28340
   235
          end);
wenzelm@28340
   236
    val res =
wenzelm@29448
   237
      if immediate orelse #maxidx (Thm.rep_cterm stmt) >= 0 orelse not (future_enabled ())
wenzelm@29088
   238
      then result ()
wenzelm@37186
   239
      else future_result ctxt' (fork result) (Thm.term_of stmt);
wenzelm@17980
   240
  in
wenzelm@28340
   241
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   242
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   243
    |> Variable.export ctxt' ctxt
wenzelm@20250
   244
    |> map Drule.zero_var_indexes
wenzelm@17980
   245
  end;
wenzelm@17980
   246
wenzelm@28341
   247
val prove_multi = prove_common true;
wenzelm@17980
   248
wenzelm@28446
   249
fun prove_future ctxt xs asms prop tac = hd (prove_common false ctxt xs asms [prop] tac);
wenzelm@28340
   250
fun prove ctxt xs asms prop tac = hd (prove_common true ctxt xs asms [prop] tac);
wenzelm@20056
   251
wenzelm@20056
   252
fun prove_global thy xs asms prop tac =
wenzelm@42360
   253
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   254
wenzelm@18027
   255
wenzelm@17980
   256
wenzelm@21687
   257
(** goal structure **)
wenzelm@21687
   258
wenzelm@21687
   259
(* nested goals *)
wenzelm@18207
   260
wenzelm@19184
   261
fun extract i n st =
wenzelm@19184
   262
  (if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty
wenzelm@19184
   263
   else if n = 1 then Seq.single (Thm.cprem_of st i)
wenzelm@23418
   264
   else
wenzelm@23418
   265
     Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1))))
wenzelm@20260
   266
  |> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init);
wenzelm@17980
   267
wenzelm@19221
   268
fun retrofit i n st' st =
wenzelm@19221
   269
  (if n = 1 then st
wenzelm@23418
   270
   else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n))
wenzelm@19221
   271
  |> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i;
wenzelm@18207
   272
wenzelm@17980
   273
fun SELECT_GOAL tac i st =
wenzelm@19191
   274
  if Thm.nprems_of st = 1 andalso i = 1 then tac st
wenzelm@19184
   275
  else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st;
wenzelm@17980
   276
wenzelm@21687
   277
wenzelm@21687
   278
(* multiple goals *)
wenzelm@21687
   279
wenzelm@23418
   280
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   281
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   282
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   283
wenzelm@23418
   284
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   285
  (SUBGOAL (fn (goal, i) =>
wenzelm@23418
   286
    if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i
wenzelm@23418
   287
    else no_tac));
wenzelm@21687
   288
wenzelm@23418
   289
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   290
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   291
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   292
wenzelm@23418
   293
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   294
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   295
wenzelm@23414
   296
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   297
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   298
    THEN tac
wenzelm@23418
   299
    THEN recover_conjunction_tac);
wenzelm@23414
   300
wenzelm@21687
   301
fun CONJUNCTS tac =
wenzelm@21687
   302
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   303
    THEN tac
wenzelm@23418
   304
    THEN recover_conjunction_tac);
wenzelm@21687
   305
wenzelm@21687
   306
wenzelm@21687
   307
(* hhf normal form *)
wenzelm@21687
   308
wenzelm@21687
   309
val norm_hhf_tac =
wenzelm@21687
   310
  rtac Drule.asm_rl  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   311
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   312
    if Drule.is_norm_hhf t then all_tac
wenzelm@41228
   313
    else rewrite_goal_tac Drule.norm_hhf_eqs i);
wenzelm@21687
   314
wenzelm@25301
   315
fun compose_hhf_tac th i st =
wenzelm@25301
   316
  PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st;
wenzelm@21687
   317
wenzelm@23237
   318
wenzelm@23237
   319
(* non-atomic goal assumptions *)
wenzelm@23237
   320
wenzelm@23356
   321
fun non_atomic (Const ("==>", _) $ _ $ _) = true
wenzelm@23356
   322
  | non_atomic (Const ("all", _) $ _) = true
wenzelm@23356
   323
  | non_atomic _ = false;
wenzelm@23356
   324
wenzelm@23237
   325
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   326
  let
wenzelm@42495
   327
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   328
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   329
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   330
    val tacs = Rs |> map (fn R =>
wenzelm@41228
   331
      Tactic.etac (Raw_Simplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac);
wenzelm@23237
   332
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   333
wenzelm@32365
   334
wenzelm@32365
   335
(* parallel tacticals *)
wenzelm@32365
   336
wenzelm@32365
   337
(*parallel choice of single results*)
wenzelm@32365
   338
fun PARALLEL_CHOICE tacs st =
wenzelm@32365
   339
  (case Par_List.get_some (fn tac => SINGLE tac st) tacs of
wenzelm@32365
   340
    NONE => Seq.empty
wenzelm@32365
   341
  | SOME st' => Seq.single st');
wenzelm@32365
   342
wenzelm@32365
   343
(*parallel refinement of non-schematic goal by single results*)
wenzelm@32788
   344
exception FAILED of unit;
wenzelm@42370
   345
fun PARALLEL_GOALS tac =
wenzelm@42370
   346
  Thm.adjust_maxidx_thm ~1 #>
wenzelm@42370
   347
  (fn st =>
wenzelm@42371
   348
    if not (Multithreading.enabled ()) orelse Thm.maxidx_of st >= 0 orelse Thm.nprems_of st <= 1
wenzelm@42370
   349
    then DETERM tac st
wenzelm@42370
   350
    else
wenzelm@42370
   351
      let
wenzelm@42370
   352
        fun try_tac g =
wenzelm@42370
   353
          (case SINGLE tac g of
wenzelm@42370
   354
            NONE => raise FAILED ()
wenzelm@42370
   355
          | SOME g' => g');
wenzelm@32365
   356
wenzelm@42370
   357
        val goals = Drule.strip_imp_prems (Thm.cprop_of st);
wenzelm@42370
   358
        val results = Par_List.map (try_tac o init) goals;
wenzelm@42370
   359
      in ALLGOALS (fn i => retrofit i 1 (nth results (i - 1))) st end
wenzelm@42370
   360
      handle FAILED () => Seq.empty);
wenzelm@32365
   361
wenzelm@18207
   362
end;
wenzelm@18207
   363
wenzelm@32365
   364
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   365
open Basic_Goal;