src/Pure/Isar/proof.ML
author wenzelm
Wed Aug 25 21:31:22 2010 +0200 (2010-08-25)
changeset 38721 ca8b14fa0d0d
parent 38333 3f4fadad9497
child 39051 45facd8f358e
permissions -rw-r--r--
added some proof state markup, notably number of subgoals (e.g. for indentation);
tuned;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     3
wenzelm@19000
     4
The Isar/VM proof language interpreter: maintains a structured flow of
wenzelm@19000
     5
context elements, goals, refinements, and facts.
wenzelm@5820
     6
*)
wenzelm@5820
     7
wenzelm@5820
     8
signature PROOF =
wenzelm@5820
     9
sig
wenzelm@33031
    10
  type context = Proof.context
wenzelm@23639
    11
  type method = Method.method
wenzelm@5820
    12
  type state
wenzelm@17359
    13
  val init: context -> state
wenzelm@17359
    14
  val level: state -> int
wenzelm@17359
    15
  val assert_bottom: bool -> state -> state
wenzelm@5820
    16
  val context_of: state -> context
wenzelm@5820
    17
  val theory_of: state -> theory
wenzelm@13377
    18
  val map_context: (context -> context) -> state -> state
wenzelm@28278
    19
  val map_contexts: (context -> context) -> state -> state
wenzelm@37949
    20
  val propagate_ml_env: state -> state
wenzelm@30757
    21
  val bind_terms: (indexname * term option) list -> state -> state
wenzelm@26251
    22
  val put_thms: bool -> string * thm list option -> state -> state
wenzelm@6091
    23
  val the_facts: state -> thm list
wenzelm@9469
    24
  val the_fact: state -> thm
wenzelm@17450
    25
  val put_facts: thm list option -> state -> state
wenzelm@6891
    26
  val assert_forward: state -> state
wenzelm@17359
    27
  val assert_chain: state -> state
wenzelm@9469
    28
  val assert_forward_or_chain: state -> state
wenzelm@5820
    29
  val assert_backward: state -> state
wenzelm@8206
    30
  val assert_no_chain: state -> state
wenzelm@5820
    31
  val enter_forward: state -> state
wenzelm@24543
    32
  val goal_message: (unit -> Pretty.T) -> state -> state
wenzelm@32738
    33
  val show_main_goal: bool Unsynchronized.ref
wenzelm@32738
    34
  val verbose: bool Unsynchronized.ref
wenzelm@12423
    35
  val pretty_state: int -> state -> Pretty.T list
wenzelm@10360
    36
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@17112
    37
  val refine: Method.text -> state -> state Seq.seq
wenzelm@17112
    38
  val refine_end: Method.text -> state -> state Seq.seq
wenzelm@18908
    39
  val refine_insert: thm list -> state -> state
wenzelm@20208
    40
  val goal_tac: thm -> int -> tactic
wenzelm@17359
    41
  val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq
wenzelm@33288
    42
  val raw_goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    43
  val goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    44
  val simple_goal: state -> {context: context, goal: thm}
wenzelm@38721
    45
  val status_markup: state -> Markup.T
wenzelm@36323
    46
  val let_bind: (term list * term) list -> state -> state
wenzelm@36323
    47
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36505
    48
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36505
    49
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36323
    50
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36323
    51
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wenzelm@20224
    52
  val assm: Assumption.export ->
wenzelm@36323
    53
    (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    54
  val assm_cmd: Assumption.export ->
wenzelm@28084
    55
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    56
  val assume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    57
  val assume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    58
  val presume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    59
  val presume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    60
  val def: (Thm.binding * ((binding * mixfix) * (term * term list))) list -> state -> state
wenzelm@36323
    61
  val def_cmd: (Attrib.binding * ((binding * mixfix) * (string * string list))) list -> state -> state
wenzelm@17359
    62
  val chain: state -> state
wenzelm@17359
    63
  val chain_facts: thm list -> state -> state
wenzelm@36323
    64
  val note_thmss: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wenzelm@36323
    65
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    66
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    67
  val from_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    68
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    69
  val with_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    70
  val using: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    71
  val using_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    72
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    73
  val unfolding_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    74
  val invoke_case: string * string option list * attribute list -> state -> state
wenzelm@36323
    75
  val invoke_case_cmd: string * string option list * Attrib.src list -> state -> state
wenzelm@17359
    76
  val begin_block: state -> state
wenzelm@17359
    77
  val next_block: state -> state
wenzelm@20309
    78
  val end_block: state -> state
wenzelm@17112
    79
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@17112
    80
  val defer: int option -> state -> state Seq.seq
wenzelm@17112
    81
  val prefer: int -> state -> state Seq.seq
wenzelm@17112
    82
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    83
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@17359
    84
  val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) ->
wenzelm@18728
    85
    (theory -> 'a -> attribute) ->
wenzelm@17359
    86
    (context * 'b list -> context * (term list list * (context -> context))) ->
wenzelm@29383
    87
    string -> Method.text option -> (thm list list -> state -> state) ->
haftmann@29581
    88
    ((binding * 'a list) * 'b) list -> state -> state
wenzelm@29383
    89
  val local_qed: Method.text option * bool -> state -> state
wenzelm@21442
    90
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36323
    91
    (term * term list) list list -> context -> state
wenzelm@36323
    92
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
    93
    (string * string list) list list -> context -> state
wenzelm@20363
    94
  val global_qed: Method.text option * bool -> state -> context
wenzelm@29383
    95
  val local_terminal_proof: Method.text * Method.text option -> state -> state
wenzelm@29383
    96
  val local_default_proof: state -> state
wenzelm@29383
    97
  val local_immediate_proof: state -> state
wenzelm@29383
    98
  val local_skip_proof: bool -> state -> state
wenzelm@29383
    99
  val local_done_proof: state -> state
wenzelm@20363
   100
  val global_terminal_proof: Method.text * Method.text option -> state -> context
wenzelm@20363
   101
  val global_default_proof: state -> context
wenzelm@20363
   102
  val global_immediate_proof: state -> context
wenzelm@29383
   103
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   104
  val global_done_proof: state -> context
wenzelm@29383
   105
  val have: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   106
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   107
  val have_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   108
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29383
   109
  val show: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   110
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   111
  val show_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   112
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29385
   113
  val schematic_goal: state -> bool
wenzelm@28452
   114
  val is_relevant: state -> bool
wenzelm@29385
   115
  val local_future_proof: (state -> ('a * state) Future.future) ->
wenzelm@29385
   116
    state -> 'a Future.future * state
wenzelm@29385
   117
  val global_future_proof: (state -> ('a * Proof.context) Future.future) ->
wenzelm@29385
   118
    state -> 'a Future.future * context
wenzelm@29385
   119
  val local_future_terminal_proof: Method.text * Method.text option -> bool -> state -> state
wenzelm@29385
   120
  val global_future_terminal_proof: Method.text * Method.text option -> bool -> state -> context
wenzelm@5820
   121
end;
wenzelm@5820
   122
wenzelm@13377
   123
structure Proof: PROOF =
wenzelm@5820
   124
struct
wenzelm@5820
   125
wenzelm@33031
   126
type context = Proof.context;
wenzelm@17112
   127
type method = Method.method;
wenzelm@16813
   128
wenzelm@5820
   129
wenzelm@5820
   130
(** proof state **)
wenzelm@5820
   131
wenzelm@17359
   132
(* datatype state *)
wenzelm@5820
   133
wenzelm@17112
   134
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   135
wenzelm@17359
   136
datatype state =
wenzelm@17359
   137
  State of node Stack.T
wenzelm@17359
   138
and node =
wenzelm@7176
   139
  Node of
wenzelm@7176
   140
   {context: context,
wenzelm@7176
   141
    facts: thm list option,
wenzelm@7176
   142
    mode: mode,
wenzelm@17359
   143
    goal: goal option}
wenzelm@17359
   144
and goal =
wenzelm@17359
   145
  Goal of
wenzelm@29346
   146
   {statement: (string * Position.T) * term list list * term,
wenzelm@28410
   147
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   148
    messages: (unit -> Pretty.T) list,    (*persistent messages (hints etc.)*)
wenzelm@25958
   149
    using: thm list,                      (*goal facts*)
wenzelm@25958
   150
    goal: thm,                            (*subgoals ==> statement*)
wenzelm@17859
   151
    before_qed: Method.text option,
wenzelm@18124
   152
    after_qed:
wenzelm@29383
   153
      (thm list list -> state -> state) *
wenzelm@20363
   154
      (thm list list -> context -> context)};
wenzelm@17359
   155
wenzelm@24543
   156
fun make_goal (statement, messages, using, goal, before_qed, after_qed) =
wenzelm@24543
   157
  Goal {statement = statement, messages = messages, using = using, goal = goal,
wenzelm@17859
   158
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   159
wenzelm@7176
   160
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   161
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   162
wenzelm@17359
   163
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   164
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   165
wenzelm@21727
   166
val init_context =
wenzelm@33165
   167
  ProofContext.set_stmt true #>
wenzelm@33165
   168
  ProofContext.map_naming (K ProofContext.local_naming);
wenzelm@21727
   169
wenzelm@21466
   170
fun init ctxt =
wenzelm@21727
   171
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   172
wenzelm@17359
   173
fun current (State st) = Stack.top st |> (fn Node node => node);
wenzelm@21274
   174
fun map_current f (State st) = State (Stack.map_top (map_node f) st);
wenzelm@28278
   175
fun map_all f (State st) = State (Stack.map_all (map_node f) st);
wenzelm@12045
   176
wenzelm@5820
   177
wenzelm@5820
   178
wenzelm@5820
   179
(** basic proof state operations **)
wenzelm@5820
   180
wenzelm@17359
   181
(* block structure *)
wenzelm@17359
   182
wenzelm@17359
   183
fun open_block (State st) = State (Stack.push st);
wenzelm@17359
   184
wenzelm@18678
   185
fun close_block (State st) = State (Stack.pop st)
wenzelm@18678
   186
  handle Empty => error "Unbalanced block parentheses";
wenzelm@17359
   187
wenzelm@17359
   188
fun level (State st) = Stack.level st;
wenzelm@17359
   189
wenzelm@17359
   190
fun assert_bottom b state =
wenzelm@17359
   191
  let val b' = (level state <= 2) in
wenzelm@18678
   192
    if b andalso not b' then error "Not at bottom of proof!"
wenzelm@18678
   193
    else if not b andalso b' then error "Already at bottom of proof!"
wenzelm@17359
   194
    else state
wenzelm@17359
   195
  end;
wenzelm@17359
   196
wenzelm@17359
   197
wenzelm@5820
   198
(* context *)
wenzelm@5820
   199
wenzelm@17359
   200
val context_of = #context o current;
wenzelm@5820
   201
val theory_of = ProofContext.theory_of o context_of;
wenzelm@5820
   202
wenzelm@17359
   203
fun map_context f =
wenzelm@17359
   204
  map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   205
wenzelm@17359
   206
fun map_context_result f state =
wenzelm@17859
   207
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   208
wenzelm@28278
   209
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   210
wenzelm@37949
   211
fun propagate_ml_env state = map_contexts
wenzelm@37949
   212
  (Context.proof_map (ML_Env.inherit (Context.Proof (context_of state)))) state;
wenzelm@37949
   213
wenzelm@30757
   214
val bind_terms = map_context o ProofContext.bind_terms;
wenzelm@26251
   215
val put_thms = map_context oo ProofContext.put_thms;
wenzelm@5820
   216
wenzelm@5820
   217
wenzelm@5820
   218
(* facts *)
wenzelm@5820
   219
wenzelm@17359
   220
val get_facts = #facts o current;
wenzelm@17359
   221
wenzelm@17359
   222
fun the_facts state =
wenzelm@17359
   223
  (case get_facts state of SOME facts => facts
wenzelm@18678
   224
  | NONE => error "No current facts available");
wenzelm@5820
   225
wenzelm@9469
   226
fun the_fact state =
wenzelm@17359
   227
  (case the_facts state of [thm] => thm
wenzelm@18678
   228
  | _ => error "Single theorem expected");
wenzelm@7605
   229
wenzelm@17359
   230
fun put_facts facts =
wenzelm@19078
   231
  map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wenzelm@33386
   232
  put_thms true (Auto_Bind.thisN, facts);
wenzelm@5820
   233
wenzelm@17359
   234
fun these_factss more_facts (named_factss, state) =
wenzelm@19482
   235
  (named_factss, state |> put_facts (SOME (maps snd named_factss @ more_facts)));
wenzelm@5820
   236
wenzelm@17359
   237
fun export_facts inner outer =
wenzelm@17359
   238
  (case get_facts inner of
wenzelm@20309
   239
    NONE => put_facts NONE outer
wenzelm@17359
   240
  | SOME thms =>
wenzelm@17359
   241
      thms
wenzelm@20309
   242
      |> ProofContext.export (context_of inner) (context_of outer)
wenzelm@20309
   243
      |> (fn ths => put_facts (SOME ths) outer));
wenzelm@5820
   244
wenzelm@5820
   245
wenzelm@5820
   246
(* mode *)
wenzelm@5820
   247
wenzelm@17359
   248
val get_mode = #mode o current;
wenzelm@5820
   249
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   250
wenzelm@17359
   251
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   252
wenzelm@5820
   253
fun assert_mode pred state =
wenzelm@5820
   254
  let val mode = get_mode state in
wenzelm@5820
   255
    if pred mode then state
wenzelm@18678
   256
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   257
  end;
wenzelm@5820
   258
wenzelm@19308
   259
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   260
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   261
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   262
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   263
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   264
wenzelm@17359
   265
val enter_forward = put_mode Forward;
wenzelm@17359
   266
val enter_chain = put_mode Chain;
wenzelm@17359
   267
val enter_backward = put_mode Backward;
wenzelm@5820
   268
wenzelm@17359
   269
wenzelm@17359
   270
(* current goal *)
wenzelm@17359
   271
wenzelm@17359
   272
fun current_goal state =
wenzelm@17359
   273
  (case current state of
wenzelm@17359
   274
    {context, goal = SOME (Goal goal), ...} => (context, goal)
wenzelm@18678
   275
  | _ => error "No current goal!");
wenzelm@5820
   276
wenzelm@17359
   277
fun assert_current_goal g state =
wenzelm@17359
   278
  let val g' = can current_goal state in
wenzelm@18678
   279
    if g andalso not g' then error "No goal in this block!"
wenzelm@18678
   280
    else if not g andalso g' then error "Goal present in this block!"
wenzelm@17359
   281
    else state
wenzelm@17359
   282
  end;
wenzelm@6776
   283
wenzelm@17359
   284
fun put_goal goal = map_current (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   285
wenzelm@17859
   286
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   287
wenzelm@17359
   288
wenzelm@17359
   289
(* nested goal *)
wenzelm@5820
   290
wenzelm@17359
   291
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
wenzelm@17359
   292
      let
wenzelm@24543
   293
        val Goal {statement, messages, using, goal, before_qed, after_qed} = goal;
wenzelm@24543
   294
        val goal' = make_goal (g (statement, messages, using, goal, before_qed, after_qed));
wenzelm@17359
   295
      in State (make_node (f context, facts, mode, SOME goal'), nodes) end
wenzelm@17359
   296
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@17359
   297
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@17359
   298
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@17359
   299
  | map_goal _ _ state = state;
wenzelm@5820
   300
wenzelm@24556
   301
fun set_goal goal = map_goal I (fn (statement, _, using, _, before_qed, after_qed) =>
wenzelm@24556
   302
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@19188
   303
wenzelm@24543
   304
fun goal_message msg = map_goal I (fn (statement, messages, using, goal, before_qed, after_qed) =>
wenzelm@24543
   305
  (statement, msg :: messages, using, goal, before_qed, after_qed));
wenzelm@24543
   306
wenzelm@24556
   307
fun using_facts using = map_goal I (fn (statement, _, _, goal, before_qed, after_qed) =>
wenzelm@24556
   308
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@17359
   309
wenzelm@17359
   310
local
wenzelm@17359
   311
  fun find i state =
wenzelm@17359
   312
    (case try current_goal state of
wenzelm@17359
   313
      SOME (ctxt, goal) => (ctxt, (i, goal))
wenzelm@18678
   314
    | NONE => find (i + 1) (close_block state handle ERROR _ => error "No goal present"));
wenzelm@17359
   315
in val find_goal = find 0 end;
wenzelm@17359
   316
wenzelm@17359
   317
fun get_goal state =
wenzelm@17359
   318
  let val (ctxt, (_, {using, goal, ...})) = find_goal state
wenzelm@17359
   319
  in (ctxt, (using, goal)) end;
wenzelm@5820
   320
wenzelm@5820
   321
wenzelm@5820
   322
wenzelm@12423
   323
(** pretty_state **)
wenzelm@5820
   324
wenzelm@32738
   325
val show_main_goal = Unsynchronized.ref false;
wenzelm@16539
   326
val verbose = ProofContext.verbose;
nipkow@13698
   327
skalberg@15531
   328
fun pretty_facts _ _ NONE = []
skalberg@15531
   329
  | pretty_facts s ctxt (SOME ths) =
wenzelm@32091
   330
      [Pretty.big_list (s ^ "this:") (map (Display.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   331
wenzelm@17359
   332
fun pretty_state nr state =
wenzelm@5820
   333
  let
wenzelm@17359
   334
    val {context, facts, mode, goal = _} = current state;
wenzelm@5820
   335
wenzelm@5820
   336
    fun levels_up 0 = ""
wenzelm@17359
   337
      | levels_up 1 = "1 level up"
wenzelm@17359
   338
      | levels_up i = string_of_int i ^ " levels up";
wenzelm@5820
   339
wenzelm@11556
   340
    fun subgoals 0 = ""
wenzelm@17359
   341
      | subgoals 1 = "1 subgoal"
wenzelm@17359
   342
      | subgoals n = string_of_int n ^ " subgoals";
wenzelm@17359
   343
wenzelm@17359
   344
    fun description strs =
wenzelm@28375
   345
      (case filter_out (fn s => s = "") strs of [] => ""
wenzelm@17359
   346
      | strs' => enclose " (" ")" (commas strs'));
wenzelm@12146
   347
wenzelm@29416
   348
    fun prt_goal (SOME (ctxt, (i,
wenzelm@29416
   349
      {statement = ((_, pos), _, _), messages, using, goal, before_qed = _, after_qed = _}))) =
wenzelm@17359
   350
          pretty_facts "using " ctxt
wenzelm@17359
   351
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@21442
   352
          [Pretty.str ("goal" ^
wenzelm@21442
   353
            description [levels_up (i div 2), subgoals (Thm.nprems_of goal)] ^ ":")] @
wenzelm@32187
   354
          Goal_Display.pretty_goals ctxt
wenzelm@32167
   355
            {total = true, main = ! show_main_goal, maxgoals = ! Display.goals_limit} goal @
wenzelm@25958
   356
          (map (fn msg => Position.setmp_thread_data pos msg ()) (rev messages))
wenzelm@17359
   357
      | prt_goal NONE = [];
wenzelm@6848
   358
wenzelm@17359
   359
    val prt_ctxt =
wenzelm@17359
   360
      if ! verbose orelse mode = Forward then ProofContext.pretty_context context
wenzelm@18670
   361
      else if mode = Backward then ProofContext.pretty_ctxt context
wenzelm@7575
   362
      else [];
wenzelm@17359
   363
  in
wenzelm@17359
   364
    [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@17359
   365
      (if ! verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")),
wenzelm@17359
   366
      Pretty.str ""] @
wenzelm@17359
   367
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@17359
   368
    (if ! verbose orelse mode = Forward then
wenzelm@17359
   369
       pretty_facts "" context facts @ prt_goal (try find_goal state)
wenzelm@17359
   370
     else if mode = Chain then pretty_facts "picking " context facts
wenzelm@17359
   371
     else prt_goal (try find_goal state))
wenzelm@17359
   372
  end;
wenzelm@5820
   373
wenzelm@12085
   374
fun pretty_goals main state =
wenzelm@32167
   375
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@32187
   376
    Goal_Display.pretty_goals ctxt
wenzelm@32167
   377
      {total = false, main = main, maxgoals = ! Display.goals_limit} goal
wenzelm@32167
   378
  end;
wenzelm@10320
   379
wenzelm@5820
   380
wenzelm@5820
   381
wenzelm@5820
   382
(** proof steps **)
wenzelm@5820
   383
wenzelm@17359
   384
(* refine via method *)
wenzelm@5820
   385
wenzelm@8234
   386
local
wenzelm@8234
   387
wenzelm@16146
   388
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   389
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   390
wenzelm@16146
   391
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   392
wenzelm@16146
   393
fun goal_cases st =
berghofe@34916
   394
  Rule_Cases.make_common (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair []) (goals st));
wenzelm@16146
   395
wenzelm@32193
   396
fun apply_method current_context meth state =
wenzelm@6848
   397
  let
wenzelm@24556
   398
    val (goal_ctxt, (_, {statement, messages = _, using, goal, before_qed, after_qed})) =
wenzelm@24543
   399
      find_goal state;
wenzelm@25958
   400
    val ctxt = if current_context then context_of state else goal_ctxt;
wenzelm@16146
   401
  in
wenzelm@32193
   402
    Method.apply meth ctxt using goal |> Seq.map (fn (meth_cases, goal') =>
wenzelm@6848
   403
      state
wenzelm@16146
   404
      |> map_goal
wenzelm@18475
   405
          (ProofContext.add_cases false (no_goal_cases goal @ goal_cases goal') #>
wenzelm@18475
   406
           ProofContext.add_cases true meth_cases)
wenzelm@24556
   407
          (K (statement, [], using, goal', before_qed, after_qed)))
wenzelm@16146
   408
  end;
wenzelm@5820
   409
wenzelm@19188
   410
fun select_goals n meth state =
wenzelm@19224
   411
  state
wenzelm@19224
   412
  |> (#2 o #2 o get_goal)
wenzelm@21687
   413
  |> ALLGOALS Goal.conjunction_tac
wenzelm@19224
   414
  |> Seq.maps (fn goal =>
wenzelm@19188
   415
    state
wenzelm@21687
   416
    |> Seq.lift set_goal (Goal.extract 1 n goal |> Seq.maps (Goal.conjunction_tac 1))
wenzelm@19188
   417
    |> Seq.maps meth
wenzelm@19188
   418
    |> Seq.maps (fn state' => state'
wenzelm@19188
   419
      |> Seq.lift set_goal (Goal.retrofit 1 n (#2 (#2 (get_goal state'))) goal))
wenzelm@32193
   420
    |> Seq.maps (apply_method true (K Method.succeed)));
wenzelm@19188
   421
wenzelm@17112
   422
fun apply_text cc text state =
wenzelm@17112
   423
  let
wenzelm@17112
   424
    val thy = theory_of state;
wenzelm@17112
   425
wenzelm@32193
   426
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@32193
   427
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@32193
   428
      | eval (Method.Source_i src) = apply_method cc (Method.method_i thy src)
wenzelm@17112
   429
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   430
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   431
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@19188
   432
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt)
wenzelm@19188
   433
      | eval (Method.SelectGoals (n, txt)) = select_goals n (eval txt);
wenzelm@17112
   434
  in eval text state end;
wenzelm@17112
   435
wenzelm@8234
   436
in
wenzelm@8234
   437
wenzelm@17112
   438
val refine = apply_text true;
wenzelm@17112
   439
val refine_end = apply_text false;
wenzelm@32856
   440
fun refine_insert ths = Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   441
wenzelm@8234
   442
end;
wenzelm@8234
   443
wenzelm@5820
   444
wenzelm@17359
   445
(* refine via sub-proof *)
wenzelm@17359
   446
wenzelm@30557
   447
fun finish_tac 0 = K all_tac
wenzelm@30557
   448
  | finish_tac n =
wenzelm@30557
   449
      Goal.norm_hhf_tac THEN'
wenzelm@30557
   450
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   451
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@30557
   452
          Tactic.etac Drule.protectI i THEN finish_tac (n - 1) i
wenzelm@30557
   453
        else finish_tac (n - 1) (i + 1));
wenzelm@30557
   454
wenzelm@20208
   455
fun goal_tac rule =
wenzelm@30557
   456
  Goal.norm_hhf_tac THEN'
wenzelm@30557
   457
  Tactic.rtac rule THEN'
wenzelm@30557
   458
  finish_tac (Thm.nprems_of rule);
wenzelm@11816
   459
wenzelm@19915
   460
fun refine_goals print_rule inner raw_rules state =
wenzelm@19915
   461
  let
wenzelm@19915
   462
    val (outer, (_, goal)) = get_goal state;
wenzelm@20208
   463
    fun refine rule st = (print_rule outer rule; FINDGOAL (goal_tac rule) st);
wenzelm@19915
   464
  in
wenzelm@19915
   465
    raw_rules
wenzelm@20309
   466
    |> ProofContext.goal_export inner outer
wenzelm@20309
   467
    |> (fn rules => Seq.lift set_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   468
  end;
wenzelm@17359
   469
wenzelm@6932
   470
wenzelm@17359
   471
(* conclude_goal *)
wenzelm@6932
   472
wenzelm@28627
   473
fun conclude_goal ctxt goal propss =
wenzelm@5820
   474
  let
wenzelm@28627
   475
    val thy = ProofContext.theory_of ctxt;
wenzelm@24920
   476
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@32091
   477
    val string_of_thm = Display.string_of_thm ctxt;
wenzelm@5820
   478
wenzelm@17359
   479
    val ngoals = Thm.nprems_of goal;
wenzelm@19774
   480
    val _ = ngoals = 0 orelse error (Pretty.string_of (Pretty.chunks
wenzelm@32187
   481
      (Goal_Display.pretty_goals ctxt
wenzelm@32167
   482
          {total = true, main = ! show_main_goal, maxgoals = ! Display.goals_limit} goal @
wenzelm@23639
   483
        [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])));
wenzelm@20224
   484
wenzelm@20224
   485
    val extra_hyps = Assumption.extra_hyps ctxt goal;
wenzelm@20224
   486
    val _ = null extra_hyps orelse
wenzelm@20224
   487
      error ("Additional hypotheses:\n" ^ cat_lines (map string_of_term extra_hyps));
wenzelm@5820
   488
wenzelm@23418
   489
    fun lost_structure () = error ("Lost goal structure:\n" ^ string_of_thm goal);
wenzelm@23418
   490
berghofe@23782
   491
    val th = Goal.conclude
wenzelm@23806
   492
      (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
berghofe@23782
   493
      handle THM _ => lost_structure ();
wenzelm@23418
   494
    val goal_propss = filter_out null propss;
wenzelm@23418
   495
    val results =
wenzelm@23418
   496
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   497
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@23418
   498
      handle THM _ => lost_structure ();
wenzelm@23418
   499
    val _ = Unify.matches_list thy (flat goal_propss) (map Thm.prop_of (flat results)) orelse
wenzelm@19774
   500
      error ("Proved a different theorem:\n" ^ string_of_thm th);
wenzelm@28627
   501
    val _ = Thm.check_shyps (Variable.sorts_of ctxt) th;
wenzelm@23418
   502
wenzelm@23418
   503
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   504
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   505
      | recover_result [] [] = []
wenzelm@23418
   506
      | recover_result _ _ = lost_structure ();
wenzelm@23418
   507
  in recover_result propss results end;
wenzelm@5820
   508
wenzelm@5820
   509
wenzelm@33288
   510
(* goal views -- corresponding to methods *)
wenzelm@33288
   511
wenzelm@33288
   512
fun raw_goal state =
wenzelm@33288
   513
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   514
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   515
wenzelm@33288
   516
val goal = raw_goal o refine_insert [];
wenzelm@33288
   517
wenzelm@33288
   518
fun simple_goal state =
wenzelm@33288
   519
  let
wenzelm@33288
   520
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   521
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   522
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   523
wenzelm@38721
   524
fun status_markup state =
wenzelm@38721
   525
  (case try goal state of
wenzelm@38721
   526
    SOME {goal, ...} => Markup.proof_state (Thm.nprems_of goal)
wenzelm@38721
   527
  | NONE => Markup.empty);
wenzelm@38721
   528
wenzelm@33288
   529
wenzelm@5820
   530
wenzelm@5820
   531
(*** structured proof commands ***)
wenzelm@5820
   532
wenzelm@17112
   533
(** context elements **)
wenzelm@5820
   534
wenzelm@36324
   535
(* let bindings *)
wenzelm@5820
   536
wenzelm@16813
   537
local
wenzelm@16813
   538
wenzelm@17359
   539
fun gen_bind bind args state =
wenzelm@5820
   540
  state
wenzelm@5820
   541
  |> assert_forward
wenzelm@36324
   542
  |> map_context (bind true args #> snd)
wenzelm@17359
   543
  |> put_facts NONE;
wenzelm@5820
   544
wenzelm@16813
   545
in
wenzelm@16813
   546
wenzelm@36324
   547
val let_bind = gen_bind ProofContext.match_bind_i;
wenzelm@36324
   548
val let_bind_cmd = gen_bind ProofContext.match_bind;
wenzelm@5820
   549
wenzelm@16813
   550
end;
wenzelm@16813
   551
wenzelm@5820
   552
wenzelm@36505
   553
(* concrete syntax *)
wenzelm@36505
   554
wenzelm@36505
   555
local
wenzelm@36505
   556
wenzelm@36507
   557
fun gen_write prep_arg mode args =
wenzelm@36507
   558
  assert_forward
wenzelm@36507
   559
  #> map_context (fn ctxt => ctxt |> ProofContext.notation true mode (map (prep_arg ctxt) args))
wenzelm@36507
   560
  #> put_facts NONE;
wenzelm@36505
   561
wenzelm@36505
   562
in
wenzelm@36505
   563
wenzelm@36505
   564
val write = gen_write (K I);
wenzelm@36505
   565
wenzelm@36505
   566
val write_cmd =
wenzelm@36505
   567
  gen_write (fn ctxt => fn (c, mx) =>
wenzelm@36505
   568
    (ProofContext.read_const ctxt false (Syntax.mixfixT mx) c, mx));
wenzelm@36505
   569
wenzelm@36505
   570
end;
wenzelm@36505
   571
wenzelm@36505
   572
wenzelm@17359
   573
(* fix *)
wenzelm@9196
   574
wenzelm@12714
   575
local
wenzelm@12714
   576
wenzelm@30763
   577
fun gen_fix prep_vars args =
wenzelm@17359
   578
  assert_forward
wenzelm@30763
   579
  #> map_context (fn ctxt => snd (ProofContext.add_fixes (prep_vars ctxt args) ctxt))
wenzelm@17359
   580
  #> put_facts NONE;
wenzelm@5820
   581
wenzelm@16813
   582
in
wenzelm@16813
   583
wenzelm@36323
   584
val fix = gen_fix (K I);
wenzelm@36323
   585
val fix_cmd = gen_fix (fn ctxt => fn args => fst (ProofContext.read_vars args ctxt));
wenzelm@5820
   586
wenzelm@16813
   587
end;
wenzelm@16813
   588
wenzelm@5820
   589
wenzelm@17359
   590
(* assume etc. *)
wenzelm@5820
   591
wenzelm@16813
   592
local
wenzelm@16813
   593
wenzelm@17112
   594
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   595
  state
wenzelm@5820
   596
  |> assert_forward
wenzelm@17112
   597
  |> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args))
wenzelm@17359
   598
  |> these_factss [] |> #2;
wenzelm@6932
   599
wenzelm@16813
   600
in
wenzelm@16813
   601
wenzelm@36323
   602
val assm = gen_assume ProofContext.add_assms_i (K I);
wenzelm@36323
   603
val assm_cmd = gen_assume ProofContext.add_assms Attrib.attribute;
wenzelm@36323
   604
val assume = assm Assumption.assume_export;
wenzelm@36323
   605
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   606
val presume = assm Assumption.presume_export;
wenzelm@36323
   607
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   608
wenzelm@16813
   609
end;
wenzelm@16813
   610
wenzelm@7271
   611
wenzelm@17359
   612
(* def *)
wenzelm@11891
   613
wenzelm@16813
   614
local
wenzelm@16813
   615
wenzelm@20913
   616
fun gen_def prep_att prep_vars prep_binds args state =
wenzelm@11891
   617
  let
wenzelm@11891
   618
    val _ = assert_forward state;
wenzelm@17112
   619
    val thy = theory_of state;
wenzelm@11891
   620
wenzelm@20913
   621
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@20913
   622
    val name_atts = map (apsnd (map (prep_att thy))) raw_name_atts;
wenzelm@11891
   623
  in
wenzelm@20913
   624
    state
wenzelm@20913
   625
    |> map_context_result (prep_vars (map (fn (x, mx) => (x, NONE, mx)) raw_vars))
wenzelm@20913
   626
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   627
    |-> (fn vars =>
wenzelm@20913
   628
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@35624
   629
      #-> (fn rhss => map_context_result (Local_Defs.add_defs (vars ~~ (name_atts ~~ rhss)))))
wenzelm@20913
   630
    |-> (put_facts o SOME o map (#2 o #2))
wenzelm@11891
   631
  end;
wenzelm@11891
   632
wenzelm@16813
   633
in
wenzelm@16813
   634
wenzelm@36323
   635
val def = gen_def (K I) ProofContext.cert_vars ProofContext.match_bind_i;
wenzelm@36323
   636
val def_cmd = gen_def Attrib.attribute ProofContext.read_vars ProofContext.match_bind;
wenzelm@11891
   637
wenzelm@16813
   638
end;
wenzelm@16813
   639
wenzelm@11891
   640
wenzelm@8374
   641
wenzelm@17112
   642
(** facts **)
wenzelm@5820
   643
wenzelm@17359
   644
(* chain *)
wenzelm@5820
   645
wenzelm@24011
   646
fun clean_facts ctxt =
wenzelm@24050
   647
  put_facts (SOME (filter_out Thm.is_dummy (the_facts ctxt))) ctxt;
wenzelm@24011
   648
wenzelm@17359
   649
val chain =
wenzelm@17359
   650
  assert_forward
wenzelm@24011
   651
  #> clean_facts
wenzelm@17359
   652
  #> enter_chain;
wenzelm@5820
   653
wenzelm@17359
   654
fun chain_facts facts =
wenzelm@17359
   655
  put_facts (SOME facts)
wenzelm@17359
   656
  #> chain;
wenzelm@5820
   657
wenzelm@5820
   658
wenzelm@17359
   659
(* note etc. *)
wenzelm@17112
   660
haftmann@28965
   661
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   662
wenzelm@17112
   663
local
wenzelm@17112
   664
wenzelm@30760
   665
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   666
  state
wenzelm@17112
   667
  |> assert_forward
wenzelm@30761
   668
  |> map_context_result (ProofContext.note_thmss ""
wenzelm@30760
   669
    (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state)) args))
wenzelm@17112
   670
  |> these_factss (more_facts state)
wenzelm@17359
   671
  ||> opt_chain
wenzelm@17359
   672
  |> opt_result;
wenzelm@17112
   673
wenzelm@17112
   674
in
wenzelm@17112
   675
wenzelm@36323
   676
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@36323
   677
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute ProofContext.get_fact;
wenzelm@17112
   678
wenzelm@36323
   679
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@36323
   680
val from_thmss_cmd = gen_thmss (K []) chain #2 Attrib.attribute ProofContext.get_fact o no_binding;
wenzelm@17359
   681
wenzelm@36323
   682
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@36323
   683
val with_thmss_cmd = gen_thmss the_facts chain #2 Attrib.attribute ProofContext.get_fact o no_binding;
wenzelm@30760
   684
wenzelm@30760
   685
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   686
wenzelm@17112
   687
end;
wenzelm@17112
   688
wenzelm@17112
   689
wenzelm@18548
   690
(* using/unfolding *)
wenzelm@17112
   691
wenzelm@17112
   692
local
wenzelm@17112
   693
wenzelm@30760
   694
fun gen_using f g prep_atts prep_fact args state =
wenzelm@17112
   695
  state
wenzelm@17112
   696
  |> assert_backward
wenzelm@21442
   697
  |> map_context_result
wenzelm@30761
   698
    (ProofContext.note_thmss ""
wenzelm@30760
   699
      (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state))
wenzelm@30760
   700
        (no_binding args)))
wenzelm@18843
   701
  |> (fn (named_facts, state') =>
wenzelm@24556
   702
    state' |> map_goal I (fn (statement, _, using, goal, before_qed, after_qed) =>
wenzelm@18843
   703
      let
wenzelm@18843
   704
        val ctxt = context_of state';
wenzelm@19482
   705
        val ths = maps snd named_facts;
wenzelm@24556
   706
      in (statement, [], f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   707
wenzelm@24050
   708
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   709
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   710
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   711
wenzelm@17112
   712
in
wenzelm@17112
   713
wenzelm@36323
   714
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@36323
   715
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute ProofContext.get_fact;
wenzelm@36323
   716
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@36323
   717
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute ProofContext.get_fact;
wenzelm@17112
   718
wenzelm@17112
   719
end;
wenzelm@17112
   720
wenzelm@17112
   721
wenzelm@17112
   722
(* case *)
wenzelm@17112
   723
wenzelm@17112
   724
local
wenzelm@17112
   725
wenzelm@30419
   726
fun qualified_binding a =
wenzelm@30419
   727
  Binding.qualify true (Long_Name.qualifier a) (Binding.name (Long_Name.base_name a));
wenzelm@30419
   728
wenzelm@17112
   729
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   730
  let
wenzelm@17112
   731
    val atts = map (prep_att (theory_of state)) raw_atts;
wenzelm@19078
   732
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@19078
   733
      ctxt |> ProofContext.apply_case (ProofContext.get_case ctxt name xs));
wenzelm@30419
   734
    val assumptions = asms |> map (fn (a, ts) => ((qualified_binding a, atts), map (rpair []) ts));
wenzelm@17112
   735
  in
wenzelm@17112
   736
    state'
wenzelm@36323
   737
    |> assume assumptions
wenzelm@33386
   738
    |> bind_terms Auto_Bind.no_facts
wenzelm@36323
   739
    |> `the_facts |-> (fn thms => note_thmss [((Binding.name name, []), [(thms, [])])])
wenzelm@17112
   740
  end;
wenzelm@17112
   741
wenzelm@17112
   742
in
wenzelm@17112
   743
wenzelm@36323
   744
val invoke_case = gen_invoke_case (K I);
wenzelm@36323
   745
val invoke_case_cmd = gen_invoke_case Attrib.attribute;
wenzelm@17112
   746
wenzelm@17112
   747
end;
wenzelm@17112
   748
wenzelm@17112
   749
wenzelm@17112
   750
wenzelm@17359
   751
(** proof structure **)
wenzelm@17359
   752
wenzelm@17359
   753
(* blocks *)
wenzelm@17359
   754
wenzelm@17359
   755
val begin_block =
wenzelm@17359
   756
  assert_forward
wenzelm@17359
   757
  #> open_block
wenzelm@17359
   758
  #> put_goal NONE
wenzelm@17359
   759
  #> open_block;
wenzelm@17359
   760
wenzelm@17359
   761
val next_block =
wenzelm@17359
   762
  assert_forward
wenzelm@17359
   763
  #> close_block
wenzelm@17359
   764
  #> open_block
wenzelm@17359
   765
  #> put_goal NONE
wenzelm@17359
   766
  #> put_facts NONE;
wenzelm@17359
   767
wenzelm@17359
   768
fun end_block state =
wenzelm@17359
   769
  state
wenzelm@17359
   770
  |> assert_forward
wenzelm@17359
   771
  |> close_block
wenzelm@17359
   772
  |> assert_current_goal false
wenzelm@17359
   773
  |> close_block
wenzelm@17359
   774
  |> export_facts state;
wenzelm@17359
   775
wenzelm@17359
   776
wenzelm@17359
   777
(* sub-proofs *)
wenzelm@17359
   778
wenzelm@17359
   779
fun proof opt_text =
wenzelm@17359
   780
  assert_backward
wenzelm@17859
   781
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   782
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   783
wenzelm@23360
   784
fun end_proof bot txt state =
wenzelm@23360
   785
  state
wenzelm@23360
   786
  |> assert_forward
wenzelm@23360
   787
  |> assert_bottom bot
wenzelm@23360
   788
  |> close_block
wenzelm@23360
   789
  |> assert_current_goal true
wenzelm@23360
   790
  |> using_facts []
wenzelm@23360
   791
  |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@32193
   792
  |> Seq.maps (refine (Method.finish_text txt));
wenzelm@17359
   793
wenzelm@29383
   794
fun check_result msg sq =
wenzelm@29383
   795
  (case Seq.pull sq of
wenzelm@29383
   796
    NONE => error msg
wenzelm@29383
   797
  | SOME (s, _) => s);
wenzelm@29383
   798
wenzelm@29383
   799
fun check_finish sq = check_result "Failed to finish proof" sq;
wenzelm@29383
   800
wenzelm@17359
   801
wenzelm@17359
   802
(* unstructured refinement *)
wenzelm@17359
   803
wenzelm@32193
   804
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i)));
wenzelm@32193
   805
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i)));
wenzelm@17359
   806
wenzelm@17359
   807
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@17359
   808
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   809
wenzelm@17359
   810
wenzelm@17359
   811
wenzelm@17112
   812
(** goals **)
wenzelm@17112
   813
wenzelm@17359
   814
(* generic goals *)
wenzelm@17359
   815
wenzelm@19774
   816
local
wenzelm@19774
   817
wenzelm@36322
   818
val is_var =
wenzelm@36322
   819
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   820
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   821
wenzelm@36322
   822
fun implicit_vars props =
wenzelm@19846
   823
  let
wenzelm@36354
   824
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   825
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   826
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   827
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   828
wenzelm@19774
   829
fun refine_terms n =
wenzelm@30510
   830
  refine (Method.Basic (K (RAW_METHOD
wenzelm@19774
   831
    (K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@32193
   832
      (HEADGOAL (CONJUNCTS (ALLGOALS (rtac Drule.termI))))))))))
wenzelm@19774
   833
  #> Seq.hd;
wenzelm@19774
   834
wenzelm@19774
   835
in
wenzelm@17359
   836
wenzelm@17859
   837
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   838
  let
wenzelm@17359
   839
    val thy = theory_of state;
wenzelm@23418
   840
    val cert = Thm.cterm_of thy;
wenzelm@17359
   841
    val chaining = can assert_chain state;
wenzelm@25958
   842
    val pos = Position.thread_data ();
wenzelm@17359
   843
wenzelm@17359
   844
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   845
      state
wenzelm@5936
   846
      |> assert_forward_or_chain
wenzelm@5936
   847
      |> enter_forward
wenzelm@17359
   848
      |> open_block
wenzelm@17859
   849
      |> map_context_result (fn ctxt => swap (prepp (ctxt, raw_propp)));
wenzelm@19482
   850
    val props = flat propss;
wenzelm@15703
   851
wenzelm@36322
   852
    val vars = implicit_vars props;
wenzelm@36322
   853
    val propss' = vars :: propss;
wenzelm@23418
   854
    val goal_propss = filter_out null propss';
wenzelm@29346
   855
    val goal =
wenzelm@29346
   856
      cert (Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss))
wenzelm@28627
   857
      |> Thm.weaken_sorts (Variable.sorts_of (context_of goal_state));
wenzelm@29346
   858
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@18124
   859
    val after_qed' = after_qed |>> (fn after_local =>
wenzelm@18124
   860
      fn results => map_context after_ctxt #> after_local results);
wenzelm@5820
   861
  in
wenzelm@17359
   862
    goal_state
wenzelm@21727
   863
    |> map_context (init_context #> Variable.set_body true)
wenzelm@28410
   864
    |> put_goal (SOME (make_goal (statement, [], [], Goal.init goal, before_qed, after_qed')))
wenzelm@17359
   865
    |> map_context (ProofContext.auto_bind_goal props)
wenzelm@21565
   866
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@17359
   867
    |> put_facts NONE
wenzelm@17359
   868
    |> open_block
wenzelm@17359
   869
    |> put_goal NONE
wenzelm@5820
   870
    |> enter_backward
wenzelm@23418
   871
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@32193
   872
    |> null props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   873
  end;
wenzelm@5820
   874
wenzelm@29090
   875
fun generic_qed after_ctxt state =
wenzelm@12503
   876
  let
wenzelm@36354
   877
    val (goal_ctxt, {statement = (_, stmt, _), goal, after_qed, ...}) = current_goal state;
wenzelm@8617
   878
    val outer_state = state |> close_block;
wenzelm@8617
   879
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   880
wenzelm@18503
   881
    val props =
wenzelm@19774
   882
      flat (tl stmt)
wenzelm@19915
   883
      |> Variable.exportT_terms goal_ctxt outer_ctxt;
wenzelm@17359
   884
    val results =
wenzelm@28627
   885
      tl (conclude_goal goal_ctxt goal stmt)
wenzelm@20309
   886
      |> burrow (ProofContext.export goal_ctxt outer_ctxt);
wenzelm@17359
   887
  in
wenzelm@17359
   888
    outer_state
wenzelm@29090
   889
    |> map_context (after_ctxt props)
wenzelm@34239
   890
    |> pair (after_qed, results)
wenzelm@17359
   891
  end;
wenzelm@17359
   892
wenzelm@19774
   893
end;
wenzelm@19774
   894
wenzelm@9469
   895
wenzelm@17359
   896
(* local goals *)
wenzelm@17359
   897
wenzelm@17859
   898
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   899
  let
wenzelm@21362
   900
    val thy = theory_of state;
wenzelm@21362
   901
    val ((names, attss), propp) =
wenzelm@21362
   902
      Attrib.map_specs (prep_att thy) stmt |> split_list |>> split_list;
wenzelm@17359
   903
wenzelm@18124
   904
    fun after_qed' results =
wenzelm@18808
   905
      local_results ((names ~~ attss) ~~ results)
wenzelm@17359
   906
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@18124
   907
      #> after_qed results;
wenzelm@5820
   908
  in
wenzelm@17359
   909
    state
wenzelm@21362
   910
    |> generic_goal prepp kind before_qed (after_qed', K I) propp
wenzelm@19900
   911
    |> tap (Variable.warn_extra_tfrees (context_of state) o context_of)
wenzelm@5820
   912
  end;
wenzelm@5820
   913
wenzelm@29383
   914
fun local_qeds txt =
wenzelm@29383
   915
  end_proof false txt
wenzelm@29383
   916
  #> Seq.map (generic_qed ProofContext.auto_bind_facts #->
wenzelm@29090
   917
    (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
   918
wenzelm@29383
   919
fun local_qed txt = local_qeds txt #> check_finish;
wenzelm@29383
   920
wenzelm@5820
   921
wenzelm@17359
   922
(* global goals *)
wenzelm@17359
   923
wenzelm@21442
   924
fun global_goal prepp before_qed after_qed propp ctxt =
wenzelm@21362
   925
  init ctxt
wenzelm@29383
   926
  |> generic_goal (prepp #> ProofContext.auto_fixes) "" before_qed (K I, after_qed) propp;
wenzelm@17359
   927
wenzelm@36323
   928
val theorem = global_goal ProofContext.bind_propp_schematic_i;
wenzelm@36323
   929
val theorem_cmd = global_goal ProofContext.bind_propp_schematic;
wenzelm@12065
   930
wenzelm@17359
   931
fun global_qeds txt =
wenzelm@17359
   932
  end_proof true txt
wenzelm@29090
   933
  #> Seq.map (generic_qed (K I) #> (fn (((_, after_qed), results), state) =>
wenzelm@29383
   934
    after_qed results (context_of state)));
wenzelm@17112
   935
wenzelm@29383
   936
fun global_qed txt = global_qeds txt #> check_finish;
wenzelm@12959
   937
wenzelm@5820
   938
wenzelm@17359
   939
(* concluding steps *)
wenzelm@17112
   940
wenzelm@29383
   941
fun terminal_proof qed initial terminal =
wenzelm@29383
   942
  proof (SOME initial) #> Seq.maps (qed terminal) #> check_finish;
wenzelm@17112
   943
wenzelm@29383
   944
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@17112
   945
val local_default_proof = local_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   946
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   947
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   948
val local_done_proof = terminal_proof local_qeds Method.done_text (NONE, false);
wenzelm@17112
   949
wenzelm@29383
   950
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@17112
   951
val global_default_proof = global_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   952
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   953
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   954
val global_done_proof = terminal_proof global_qeds Method.done_text (NONE, false);
wenzelm@5820
   955
wenzelm@6896
   956
wenzelm@17359
   957
(* common goal statements *)
wenzelm@17359
   958
wenzelm@17359
   959
local
wenzelm@17359
   960
wenzelm@17859
   961
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@33389
   962
  local_goal (Proof_Display.print_results int) prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
   963
wenzelm@17859
   964
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
   965
  let
wenzelm@32738
   966
    val testing = Unsynchronized.ref false;
wenzelm@32738
   967
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
   968
    fun fail_msg ctxt =
wenzelm@29383
   969
      "Local statement will fail to refine any pending goal" ::
wenzelm@33389
   970
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
   971
      |> cat_lines;
wenzelm@6896
   972
wenzelm@17359
   973
    fun print_results ctxt res =
wenzelm@33389
   974
      if ! testing then () else Proof_Display.print_results int ctxt res;
wenzelm@17359
   975
    fun print_rule ctxt th =
wenzelm@17359
   976
      if ! testing then rule := SOME th
wenzelm@33389
   977
      else if int then priority (Proof_Display.string_of_rule ctxt "Successful" th)
wenzelm@17359
   978
      else ();
wenzelm@17359
   979
    val test_proof =
wenzelm@29383
   980
      try (local_skip_proof true)
wenzelm@28289
   981
      |> setmp_noncritical testing true
wenzelm@23963
   982
      |> Exn.capture;
wenzelm@6896
   983
wenzelm@18124
   984
    fun after_qed' results =
wenzelm@19482
   985
      refine_goals print_rule (context_of state) (flat results)
wenzelm@29383
   986
      #> check_result "Failed to refine any pending goal"
wenzelm@29383
   987
      #> after_qed results;
wenzelm@17359
   988
  in
wenzelm@17359
   989
    state
wenzelm@17859
   990
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@21565
   991
    |> int ? (fn goal_state =>
wenzelm@17359
   992
      (case test_proof goal_state of
wenzelm@23963
   993
        Exn.Result (SOME _) => goal_state
wenzelm@23963
   994
      | Exn.Result NONE => error (fail_msg (context_of goal_state))
wenzelm@28443
   995
      | Exn.Exn Exn.Interrupt => raise Exn.Interrupt
wenzelm@28458
   996
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
   997
  end;
wenzelm@17359
   998
wenzelm@17359
   999
in
wenzelm@17359
  1000
wenzelm@36323
  1001
val have = gen_have (K I) ProofContext.bind_propp_i;
wenzelm@36323
  1002
val have_cmd = gen_have Attrib.attribute ProofContext.bind_propp;
wenzelm@36323
  1003
val show = gen_show (K I) ProofContext.bind_propp_i;
wenzelm@36323
  1004
val show_cmd = gen_show Attrib.attribute ProofContext.bind_propp;
wenzelm@6896
  1005
wenzelm@5820
  1006
end;
wenzelm@17359
  1007
wenzelm@28410
  1008
wenzelm@29385
  1009
wenzelm@29385
  1010
(** future proofs **)
wenzelm@29385
  1011
wenzelm@29385
  1012
(* relevant proof states *)
wenzelm@29385
  1013
wenzelm@29385
  1014
fun is_schematic t =
wenzelm@29385
  1015
  Term.exists_subterm Term.is_Var t orelse
wenzelm@29385
  1016
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@29385
  1017
wenzelm@29385
  1018
fun schematic_goal state =
wenzelm@29385
  1019
  let val (_, (_, {statement = (_, _, prop), ...})) = find_goal state
wenzelm@29385
  1020
  in is_schematic prop end;
wenzelm@29385
  1021
wenzelm@29385
  1022
fun is_relevant state =
wenzelm@29385
  1023
  (case try find_goal state of
wenzelm@29385
  1024
    NONE => true
wenzelm@29385
  1025
  | SOME (_, (_, {statement = (_, _, prop), goal, ...})) =>
wenzelm@29385
  1026
      is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1027
wenzelm@29385
  1028
wenzelm@29385
  1029
(* full proofs *)
wenzelm@28410
  1030
wenzelm@29346
  1031
local
wenzelm@29346
  1032
wenzelm@29385
  1033
fun future_proof done get_context fork_proof state =
wenzelm@28410
  1034
  let
wenzelm@29381
  1035
    val _ = assert_backward state;
wenzelm@28410
  1036
    val (goal_ctxt, (_, goal)) = find_goal state;
wenzelm@32786
  1037
    val {statement as (kind, _, prop), messages, using, goal, before_qed, after_qed} = goal;
wenzelm@32062
  1038
    val goal_txt = prop :: map Thm.term_of (Assumption.all_assms_of goal_ctxt);
wenzelm@28410
  1039
wenzelm@29383
  1040
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1041
wenzelm@29346
  1042
    val prop' = Logic.protect prop;
wenzelm@29346
  1043
    val statement' = (kind, [[], [prop']], prop');
wenzelm@29346
  1044
    val goal' = Thm.adjust_maxidx_thm (Thm.maxidx_of goal)
wenzelm@29346
  1045
      (Drule.comp_no_flatten (goal, Thm.nprems_of goal) 1 Drule.protectI);
wenzelm@29385
  1046
wenzelm@33386
  1047
    fun after_local' [[th]] = put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@33386
  1048
    fun after_global' [[th]] = ProofContext.put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@29385
  1049
    val after_qed' = (after_local', after_global');
wenzelm@33386
  1050
    val this_name = ProofContext.full_name goal_ctxt (Binding.name Auto_Bind.thisN);
wenzelm@28410
  1051
wenzelm@28973
  1052
    val result_ctxt =
wenzelm@28973
  1053
      state
wenzelm@32062
  1054
      |> map_contexts (fold Variable.declare_term goal_txt)
wenzelm@29385
  1055
      |> map_goal I (K (statement', messages, using, goal', before_qed, after_qed'))
wenzelm@29346
  1056
      |> fork_proof;
wenzelm@28973
  1057
wenzelm@29385
  1058
    val future_thm = result_ctxt |> Future.map (fn (_, x) =>
wenzelm@29385
  1059
      ProofContext.get_fact_single (get_context x) (Facts.named this_name));
wenzelm@32062
  1060
    val finished_goal = Goal.future_result goal_ctxt future_thm prop';
wenzelm@28973
  1061
    val state' =
wenzelm@28973
  1062
      state
wenzelm@28973
  1063
      |> map_goal I (K (statement, messages, using, finished_goal, NONE, after_qed))
wenzelm@29385
  1064
      |> done;
wenzelm@28973
  1065
  in (Future.map #1 result_ctxt, state') end;
wenzelm@28410
  1066
wenzelm@29385
  1067
in
wenzelm@29385
  1068
wenzelm@29385
  1069
fun local_future_proof x = future_proof local_done_proof context_of x;
wenzelm@29385
  1070
fun global_future_proof x = future_proof global_done_proof I x;
wenzelm@29385
  1071
wenzelm@17359
  1072
end;
wenzelm@29346
  1073
wenzelm@29385
  1074
wenzelm@29385
  1075
(* terminal proofs *)
wenzelm@29385
  1076
wenzelm@29385
  1077
local
wenzelm@29385
  1078
wenzelm@29385
  1079
fun future_terminal_proof proof1 proof2 meths int state =
wenzelm@38218
  1080
  if int orelse is_relevant state orelse not (Goal.local_future_enabled ())
wenzelm@29385
  1081
  then proof1 meths state
wenzelm@37186
  1082
  else snd (proof2 (fn state' => Goal.fork (fn () => ((), proof1 meths state'))) state);
wenzelm@29385
  1083
wenzelm@29385
  1084
in
wenzelm@29385
  1085
wenzelm@29385
  1086
fun local_future_terminal_proof x =
wenzelm@29385
  1087
  future_terminal_proof local_terminal_proof local_future_proof x;
wenzelm@29385
  1088
wenzelm@29385
  1089
fun global_future_terminal_proof x =
wenzelm@29385
  1090
  future_terminal_proof global_terminal_proof global_future_proof x;
wenzelm@29350
  1091
wenzelm@29346
  1092
end;
wenzelm@29346
  1093
wenzelm@29385
  1094
end;
wenzelm@29385
  1095