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