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