src/Pure/goal.ML
author wenzelm
Sun Jul 05 15:02:30 2015 +0200 (2015-07-05)
changeset 60642 48dd1cefb4ae
parent 59623 920889b0788e
child 60695 757549b4bbe6
permissions -rw-r--r--
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm@17980
     1
(*  Title:      Pure/goal.ML
wenzelm@29345
     2
    Author:     Makarius
wenzelm@17980
     3
wenzelm@49061
     4
Goals in tactical theorem proving, with support for forked proofs.
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@52458
    11
  val PREFER_GOAL: tactic -> int -> tactic
wenzelm@23418
    12
  val CONJUNCTS: tactic -> int -> tactic
wenzelm@23414
    13
  val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic
wenzelm@17980
    14
end;
wenzelm@17980
    15
wenzelm@17980
    16
signature GOAL =
wenzelm@17980
    17
sig
wenzelm@17980
    18
  include BASIC_GOAL
wenzelm@17980
    19
  val init: cterm -> thm
wenzelm@52456
    20
  val protect: int -> thm -> thm
wenzelm@17980
    21
  val conclude: thm -> thm
wenzelm@49845
    22
  val check_finished: Proof.context -> thm -> thm
wenzelm@32197
    23
  val finish: Proof.context -> thm -> thm
wenzelm@54883
    24
  val norm_result: Proof.context -> thm -> thm
wenzelm@52499
    25
  val skip_proofs_enabled: unit -> bool
wenzelm@53189
    26
  val future_enabled: int -> bool
wenzelm@51423
    27
  val future_enabled_timing: Time.time -> bool
wenzelm@28973
    28
  val future_result: Proof.context -> thm future -> term -> thm
wenzelm@54883
    29
  val prove_internal: Proof.context -> cterm list -> cterm -> (thm list -> tactic) -> thm
wenzelm@51553
    30
  val is_schematic: term -> bool
wenzelm@59564
    31
  val prove_common: Proof.context -> int option -> string list -> term list -> term list ->
wenzelm@20290
    32
    ({prems: thm list, context: Proof.context} -> tactic) -> thm list
wenzelm@28446
    33
  val prove_future: Proof.context -> string list -> term list -> term ->
wenzelm@28340
    34
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@20290
    35
  val prove: Proof.context -> string list -> term list -> term ->
wenzelm@20290
    36
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51118
    37
  val prove_global_future: theory -> string list -> term list -> term ->
wenzelm@51118
    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@51551
    41
  val prove_sorry: Proof.context -> string list -> term list -> term ->
wenzelm@51551
    42
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@51551
    43
  val prove_sorry_global: theory -> string list -> term list -> term ->
wenzelm@51551
    44
    ({prems: thm list, context: Proof.context} -> tactic) -> thm
wenzelm@52458
    45
  val restrict: int -> int -> thm -> thm
wenzelm@52458
    46
  val unrestrict: int -> thm -> thm
wenzelm@23418
    47
  val conjunction_tac: int -> tactic
wenzelm@23414
    48
  val precise_conjunction_tac: int -> int -> tactic
wenzelm@23418
    49
  val recover_conjunction_tac: tactic
wenzelm@54742
    50
  val norm_hhf_tac: Proof.context -> int -> tactic
wenzelm@23237
    51
  val assume_rule_tac: Proof.context -> int -> tactic
wenzelm@17980
    52
end;
wenzelm@17980
    53
wenzelm@17980
    54
structure Goal: GOAL =
wenzelm@17980
    55
struct
wenzelm@17980
    56
wenzelm@18027
    57
(** goals **)
wenzelm@18027
    58
wenzelm@18027
    59
(*
wenzelm@18027
    60
  -------- (init)
wenzelm@18027
    61
  C ==> #C
wenzelm@18027
    62
*)
wenzelm@60642
    63
fun init C = Thm.instantiate ([], [((("A", 0), propT), C)]) Drule.protectI;
wenzelm@17980
    64
wenzelm@17980
    65
(*
wenzelm@52456
    66
  A1 ==> ... ==> An ==> C
wenzelm@52456
    67
  ------------------------ (protect n)
wenzelm@52456
    68
  A1 ==> ... ==> An ==> #C
wenzelm@17980
    69
*)
wenzelm@52456
    70
fun protect n th = Drule.comp_no_flatten (th, n) 1 Drule.protectI;
wenzelm@17980
    71
wenzelm@17980
    72
(*
wenzelm@18027
    73
  A ==> ... ==> #C
wenzelm@18027
    74
  ---------------- (conclude)
wenzelm@17980
    75
  A ==> ... ==> C
wenzelm@17980
    76
*)
wenzelm@29345
    77
fun conclude th = Drule.comp_no_flatten (th, Thm.nprems_of th) 1 Drule.protectD;
wenzelm@17980
    78
wenzelm@17980
    79
(*
wenzelm@18027
    80
  #C
wenzelm@18027
    81
  --- (finish)
wenzelm@18027
    82
   C
wenzelm@17983
    83
*)
wenzelm@32197
    84
fun check_finished ctxt th =
wenzelm@51608
    85
  if Thm.no_prems th then th
wenzelm@51608
    86
  else
wenzelm@51958
    87
    raise THM ("Proof failed.\n" ^ Pretty.string_of (Goal_Display.pretty_goal ctxt th), 0, [th]);
wenzelm@32197
    88
wenzelm@49845
    89
fun finish ctxt = check_finished ctxt #> conclude;
wenzelm@17980
    90
wenzelm@17980
    91
wenzelm@18027
    92
wenzelm@18027
    93
(** results **)
wenzelm@18027
    94
wenzelm@28340
    95
(* normal form *)
wenzelm@28340
    96
wenzelm@54883
    97
fun norm_result ctxt =
wenzelm@58950
    98
  Drule.flexflex_unique (SOME ctxt)
wenzelm@54883
    99
  #> Raw_Simplifier.norm_hhf_protect ctxt
wenzelm@21604
   100
  #> Thm.strip_shyps
wenzelm@21604
   101
  #> Drule.zero_var_indexes;
wenzelm@21604
   102
wenzelm@21604
   103
wenzelm@41703
   104
(* scheduling parameters *)
wenzelm@41703
   105
wenzelm@52499
   106
fun skip_proofs_enabled () =
wenzelm@52710
   107
  let val skip = Options.default_bool "skip_proofs" in
wenzelm@52499
   108
    if Proofterm.proofs_enabled () andalso skip then
wenzelm@52499
   109
      (warning "Proof terms enabled -- cannot skip proofs"; false)
wenzelm@52499
   110
    else skip
wenzelm@52499
   111
  end;
wenzelm@52499
   112
wenzelm@32738
   113
val parallel_proofs = Unsynchronized.ref 1;
wenzelm@29448
   114
wenzelm@53189
   115
fun future_enabled n =
wenzelm@49012
   116
  Multithreading.enabled () andalso ! parallel_proofs >= n andalso
wenzelm@41703
   117
  is_some (Future.worker_task ());
wenzelm@32061
   118
wenzelm@51423
   119
fun future_enabled_timing t =
wenzelm@53189
   120
  future_enabled 1 andalso
wenzelm@52104
   121
    Time.toReal t >= Options.default_real "parallel_subproofs_threshold";
wenzelm@29448
   122
wenzelm@29448
   123
wenzelm@28446
   124
(* future_result *)
wenzelm@28340
   125
wenzelm@28446
   126
fun future_result ctxt result prop =
wenzelm@28340
   127
  let
wenzelm@30473
   128
    val assms = Assumption.all_assms_of ctxt;
wenzelm@28340
   129
    val As = map Thm.term_of assms;
wenzelm@28340
   130
wenzelm@28340
   131
    val xs = map Free (fold Term.add_frees (prop :: As) []);
wenzelm@59623
   132
    val fixes = map (Thm.cterm_of ctxt) xs;
wenzelm@28340
   133
wenzelm@28340
   134
    val tfrees = fold Term.add_tfrees (prop :: As) [];
wenzelm@60642
   135
    val instT = map (fn (a, S) => (((a, 0), S), Thm.ctyp_of ctxt (TFree (a, S)))) tfrees;
wenzelm@28340
   136
wenzelm@28340
   137
    val global_prop =
wenzelm@59616
   138
      Logic.varify_types_global (fold_rev Logic.all xs (Logic.list_implies (As, prop)))
wenzelm@59623
   139
      |> Thm.cterm_of ctxt
wenzelm@32056
   140
      |> Thm.weaken_sorts (Variable.sorts_of ctxt);
wenzelm@28973
   141
    val global_result = result |> Future.map
wenzelm@58950
   142
      (Drule.flexflex_unique (SOME ctxt) #>
wenzelm@33768
   143
        Thm.adjust_maxidx_thm ~1 #>
wenzelm@28973
   144
        Drule.implies_intr_list assms #>
wenzelm@28973
   145
        Drule.forall_intr_list fixes #>
wenzelm@36613
   146
        Thm.generalize (map #1 tfrees, []) 0 #>
wenzelm@36613
   147
        Thm.strip_shyps);
wenzelm@28340
   148
    val local_result =
wenzelm@32056
   149
      Thm.future global_result global_prop
wenzelm@50987
   150
      |> Thm.close_derivation
wenzelm@28340
   151
      |> Thm.instantiate (instT, [])
wenzelm@28340
   152
      |> Drule.forall_elim_list fixes
wenzelm@28340
   153
      |> fold (Thm.elim_implies o Thm.assume) assms;
wenzelm@28340
   154
  in local_result end;
wenzelm@28340
   155
wenzelm@28340
   156
wenzelm@18027
   157
wenzelm@18027
   158
(** tactical theorem proving **)
wenzelm@18027
   159
wenzelm@23356
   160
(* prove_internal -- minimal checks, no normalization of result! *)
wenzelm@20250
   161
wenzelm@54883
   162
fun prove_internal ctxt casms cprop tac =
wenzelm@54883
   163
  (case SINGLE (tac (map (Assumption.assume ctxt) casms)) (init cprop) of
wenzelm@54992
   164
    SOME th => Drule.implies_intr_list casms (finish ctxt th)
wenzelm@38875
   165
  | NONE => error "Tactic failed");
wenzelm@20250
   166
wenzelm@20250
   167
wenzelm@51553
   168
(* prove variations *)
wenzelm@51553
   169
wenzelm@51553
   170
fun is_schematic t =
wenzelm@51553
   171
  Term.exists_subterm Term.is_Var t orelse
wenzelm@51553
   172
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@17986
   173
wenzelm@59564
   174
fun prove_common ctxt fork_pri xs asms props tac =
wenzelm@17980
   175
  let
wenzelm@42360
   176
    val thy = Proof_Context.theory_of ctxt;
wenzelm@20056
   177
wenzelm@51553
   178
    val schematic = exists is_schematic props;
wenzelm@59564
   179
    val immediate = is_none fork_pri;
wenzelm@53189
   180
    val future = future_enabled 1;
wenzelm@52499
   181
    val skip = not immediate andalso not schematic andalso future andalso skip_proofs_enabled ();
wenzelm@51553
   182
wenzelm@28355
   183
    val pos = Position.thread_data ();
wenzelm@55559
   184
    fun err msg =
wenzelm@55559
   185
      cat_error msg
wenzelm@55559
   186
        ("The error(s) above occurred for the goal statement" ^ Position.here pos ^ ":\n" ^
wenzelm@55559
   187
          Syntax.string_of_term ctxt (Logic.list_implies (asms, Logic.mk_conjunction_list props)));
wenzelm@17980
   188
wenzelm@59623
   189
    fun cert_safe t = Thm.cterm_of ctxt (Envir.beta_norm (Term.no_dummy_patterns t))
wenzelm@17980
   190
      handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg;
wenzelm@20250
   191
    val casms = map cert_safe asms;
wenzelm@20250
   192
    val cprops = map cert_safe props;
wenzelm@17980
   193
wenzelm@20250
   194
    val (prems, ctxt') = ctxt
wenzelm@20250
   195
      |> Variable.add_fixes_direct xs
wenzelm@27218
   196
      |> fold Variable.declare_term (asms @ props)
wenzelm@26713
   197
      |> Assumption.add_assumes casms
wenzelm@26713
   198
      ||> Variable.set_body true;
wenzelm@28619
   199
    val sorts = Variable.sorts_of ctxt';
wenzelm@17980
   200
wenzelm@28619
   201
    val stmt = Thm.weaken_sorts sorts (Conjunction.mk_conjunction_balanced cprops);
wenzelm@28340
   202
wenzelm@51553
   203
    fun tac' args st =
wenzelm@59498
   204
      if skip then ALLGOALS (Skip_Proof.cheat_tac ctxt) st before Skip_Proof.report ctxt
wenzelm@51553
   205
      else tac args st;
wenzelm@28340
   206
    fun result () =
wenzelm@51553
   207
      (case SINGLE (tac' {prems = prems, context = ctxt'}) (init stmt) of
wenzelm@38875
   208
        NONE => err "Tactic failed"
wenzelm@28340
   209
      | SOME st =>
wenzelm@54567
   210
          let
wenzelm@54981
   211
            val _ =
wenzelm@59582
   212
              Theory.subthy (Thm.theory_of_thm st, thy) orelse
wenzelm@59582
   213
                err "Bad background theory of goal state";
wenzelm@54567
   214
            val res =
wenzelm@54567
   215
              (finish ctxt' st
wenzelm@58950
   216
                |> Drule.flexflex_unique (SOME ctxt')
wenzelm@54567
   217
                |> Thm.check_shyps sorts
wenzelm@54994
   218
                |> Thm.check_hyps (Context.Proof ctxt'))
wenzelm@54567
   219
              handle THM (msg, _, _) => err msg | ERROR msg => err msg;
wenzelm@54567
   220
          in
wenzelm@58950
   221
            if Unify.matches_list (Context.Proof ctxt') [Thm.term_of stmt] [Thm.prop_of res]
wenzelm@58950
   222
            then res
wenzelm@54567
   223
            else err ("Proved a different theorem: " ^ Syntax.string_of_term ctxt' (Thm.prop_of res))
wenzelm@28340
   224
          end);
wenzelm@28340
   225
    val res =
wenzelm@53192
   226
      if immediate orelse schematic orelse not future orelse skip then result ()
wenzelm@53192
   227
      else
wenzelm@53192
   228
        future_result ctxt'
wenzelm@59564
   229
          (Execution.fork {name = "Goal.prove", pos = Position.thread_data (), pri = the fork_pri}
wenzelm@59564
   230
            result)
wenzelm@53192
   231
          (Thm.term_of stmt);
wenzelm@17980
   232
  in
wenzelm@28340
   233
    Conjunction.elim_balanced (length props) res
wenzelm@20290
   234
    |> map (Assumption.export false ctxt' ctxt)
wenzelm@20056
   235
    |> Variable.export ctxt' ctxt
wenzelm@20250
   236
    |> map Drule.zero_var_indexes
wenzelm@17980
   237
  end;
wenzelm@17980
   238
wenzelm@59564
   239
fun prove_future_pri ctxt pri xs asms prop tac =
wenzelm@59564
   240
  hd (prove_common ctxt (SOME pri) xs asms [prop] tac);
wenzelm@17980
   241
wenzelm@59564
   242
fun prove_future ctxt = prove_future_pri ctxt ~1;
wenzelm@51118
   243
wenzelm@59564
   244
fun prove ctxt xs asms prop tac = hd (prove_common ctxt NONE xs asms [prop] tac);
wenzelm@20056
   245
wenzelm@51118
   246
fun prove_global_future thy xs asms prop tac =
wenzelm@51118
   247
  Drule.export_without_context (prove_future (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51118
   248
wenzelm@20056
   249
fun prove_global thy xs asms prop tac =
wenzelm@42360
   250
  Drule.export_without_context (prove (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@18027
   251
wenzelm@51551
   252
fun prove_sorry ctxt xs asms prop tac =
wenzelm@52059
   253
  if Config.get ctxt quick_and_dirty then
wenzelm@59498
   254
    prove ctxt xs asms prop (fn _ => ALLGOALS (Skip_Proof.cheat_tac ctxt))
wenzelm@59564
   255
  else (if future_enabled 1 then prove_future_pri ctxt ~2 else prove ctxt) xs asms prop tac;
wenzelm@51551
   256
wenzelm@51551
   257
fun prove_sorry_global thy xs asms prop tac =
wenzelm@51551
   258
  Drule.export_without_context
wenzelm@51551
   259
    (prove_sorry (Proof_Context.init_global thy) xs asms prop tac);
wenzelm@51551
   260
wenzelm@18027
   261
wenzelm@17980
   262
wenzelm@21687
   263
(** goal structure **)
wenzelm@21687
   264
wenzelm@52458
   265
(* rearrange subgoals *)
wenzelm@52458
   266
wenzelm@52458
   267
fun restrict i n st =
wenzelm@52458
   268
  if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st
wenzelm@52458
   269
  then raise THM ("Goal.restrict", i, [st])
wenzelm@52458
   270
  else rotate_prems (i - 1) st |> protect n;
wenzelm@52458
   271
wenzelm@52458
   272
fun unrestrict i = conclude #> rotate_prems (1 - i);
wenzelm@52458
   273
wenzelm@52458
   274
(*with structural marker*)
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@52458
   277
  else (PRIMITIVE (restrict i 1) THEN tac THEN PRIMITIVE (unrestrict i)) st;
wenzelm@52458
   278
wenzelm@52458
   279
(*without structural marker*)
wenzelm@52458
   280
fun PREFER_GOAL tac i st =
wenzelm@52458
   281
  if i < 1 orelse i > Thm.nprems_of st then Seq.empty
wenzelm@52458
   282
  else (PRIMITIVE (rotate_prems (i - 1)) THEN tac THEN PRIMITIVE (rotate_prems (1 - i))) st;
wenzelm@17980
   283
wenzelm@21687
   284
wenzelm@21687
   285
(* multiple goals *)
wenzelm@21687
   286
wenzelm@23418
   287
fun precise_conjunction_tac 0 i = eq_assume_tac i
wenzelm@23418
   288
  | precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i
wenzelm@23418
   289
  | precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n));
wenzelm@23414
   290
wenzelm@23418
   291
val adhoc_conjunction_tac = REPEAT_ALL_NEW
wenzelm@23418
   292
  (SUBGOAL (fn (goal, i) =>
wenzelm@59498
   293
    if can Logic.dest_conjunction goal then resolve0_tac [Conjunction.conjunctionI] i
wenzelm@23418
   294
    else no_tac));
wenzelm@21687
   295
wenzelm@23418
   296
val conjunction_tac = SUBGOAL (fn (goal, i) =>
wenzelm@23418
   297
  precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE
wenzelm@23418
   298
  TRY (adhoc_conjunction_tac i));
wenzelm@21687
   299
wenzelm@23418
   300
val recover_conjunction_tac = PRIMITIVE (fn th =>
wenzelm@23418
   301
  Conjunction.uncurry_balanced (Thm.nprems_of th) th);
wenzelm@23414
   302
wenzelm@23414
   303
fun PRECISE_CONJUNCTS n tac =
wenzelm@23414
   304
  SELECT_GOAL (precise_conjunction_tac n 1
wenzelm@23414
   305
    THEN tac
wenzelm@23418
   306
    THEN recover_conjunction_tac);
wenzelm@23414
   307
wenzelm@21687
   308
fun CONJUNCTS tac =
wenzelm@21687
   309
  SELECT_GOAL (conjunction_tac 1
wenzelm@21687
   310
    THEN tac
wenzelm@23418
   311
    THEN recover_conjunction_tac);
wenzelm@21687
   312
wenzelm@21687
   313
wenzelm@21687
   314
(* hhf normal form *)
wenzelm@21687
   315
wenzelm@54742
   316
fun norm_hhf_tac ctxt =
wenzelm@59498
   317
  resolve_tac ctxt [Drule.asm_rl]  (*cheap approximation -- thanks to builtin Logic.flatten_params*)
wenzelm@21687
   318
  THEN' SUBGOAL (fn (t, i) =>
wenzelm@21687
   319
    if Drule.is_norm_hhf t then all_tac
wenzelm@54742
   320
    else rewrite_goal_tac ctxt Drule.norm_hhf_eqs i);
wenzelm@21687
   321
wenzelm@23237
   322
wenzelm@23237
   323
(* non-atomic goal assumptions *)
wenzelm@23237
   324
wenzelm@56245
   325
fun non_atomic (Const ("Pure.imp", _) $ _ $ _) = true
wenzelm@56245
   326
  | non_atomic (Const ("Pure.all", _) $ _) = true
wenzelm@23356
   327
  | non_atomic _ = false;
wenzelm@23356
   328
wenzelm@54742
   329
fun assume_rule_tac ctxt = norm_hhf_tac ctxt THEN' CSUBGOAL (fn (goal, i) =>
wenzelm@23237
   330
  let
wenzelm@42495
   331
    val ((_, goal'), ctxt') = Variable.focus_cterm goal ctxt;
wenzelm@23237
   332
    val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal';
wenzelm@23356
   333
    val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal'');
wenzelm@23237
   334
    val tacs = Rs |> map (fn R =>
wenzelm@59498
   335
      eresolve_tac ctxt [Raw_Simplifier.norm_hhf ctxt (Thm.trivial R)] THEN_ALL_NEW assume_tac ctxt);
wenzelm@23237
   336
  in fold_rev (curry op APPEND') tacs (K no_tac) i end);
wenzelm@23237
   337
wenzelm@52461
   338
end;
wenzelm@52461
   339
wenzelm@32365
   340
structure Basic_Goal: BASIC_GOAL = Goal;
wenzelm@32365
   341
open Basic_Goal;