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