src/Pure/Isar/proof.ML
author wenzelm
Wed Jul 01 21:29:57 2015 +0200 (2015-07-01)
changeset 60623 be39fe6c5620
parent 60618 4c79543cc376
child 60723 757cad5a3fe9
permissions -rw-r--r--
support for subgoal focus command;
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
bulwahn@40642
    19
  val map_context_result : (context -> 'a * context) -> state -> 'a * state
wenzelm@28278
    20
  val map_contexts: (context -> context) -> state -> state
wenzelm@37949
    21
  val propagate_ml_env: 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@47068
    25
  val set_facts: thm list -> state -> state
wenzelm@47068
    26
  val reset_facts: state -> state
wenzelm@6891
    27
  val assert_forward: state -> state
wenzelm@17359
    28
  val assert_chain: state -> state
wenzelm@9469
    29
  val assert_forward_or_chain: state -> state
wenzelm@5820
    30
  val assert_backward: state -> state
wenzelm@8206
    31
  val assert_no_chain: state -> state
wenzelm@5820
    32
  val enter_forward: state -> state
wenzelm@60369
    33
  val enter_chain: state -> state
wenzelm@60369
    34
  val enter_backward: state -> state
wenzelm@58798
    35
  val has_bottom_goal: state -> bool
wenzelm@60623
    36
  val using_facts: thm list -> state -> state
wenzelm@60403
    37
  val pretty_state: state -> Pretty.T list
wenzelm@17112
    38
  val refine: Method.text -> state -> state Seq.seq
wenzelm@17112
    39
  val refine_end: Method.text -> state -> state Seq.seq
wenzelm@18908
    40
  val refine_insert: thm list -> state -> state
wenzelm@60623
    41
  val refine_primitive: (Proof.context -> thm -> thm) -> state -> state
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@58011
    65
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36323
    66
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@58011
    67
  val from_thmss_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36323
    68
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@58011
    69
  val with_thmss_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@60371
    70
  val supply: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wenzelm@60371
    71
  val supply_cmd: (Attrib.binding * (Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36323
    72
  val using: ((thm list * attribute list) list) list -> state -> state
wenzelm@58011
    73
  val using_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@36323
    74
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wenzelm@58011
    75
  val unfolding_cmd: ((Facts.ref * Token.src list) list) list -> state -> state
wenzelm@60565
    76
  val case_: Thm.binding * ((string * Position.T) * binding option list) -> state -> state
wenzelm@60565
    77
  val case_cmd: Attrib.binding * ((string * Position.T) * binding option list) -> state -> state
wenzelm@17359
    78
  val begin_block: state -> state
wenzelm@17359
    79
  val next_block: state -> state
wenzelm@20309
    80
  val end_block: state -> state
wenzelm@49042
    81
  val begin_notepad: context -> state
wenzelm@49042
    82
  val end_notepad: state -> context
wenzelm@17112
    83
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@49889
    84
  val proof_results: Method.text_range option -> state -> state Seq.result Seq.seq
wenzelm@49865
    85
  val defer: int -> state -> state
wenzelm@49865
    86
  val prefer: int -> state -> state
wenzelm@17112
    87
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    88
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@49889
    89
  val apply_results: Method.text_range -> state -> state Seq.result Seq.seq
wenzelm@49889
    90
  val apply_end_results: Method.text_range -> state -> state Seq.result Seq.seq
wenzelm@49889
    91
  val local_qed: Method.text_range option * bool -> state -> state
wenzelm@21442
    92
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36323
    93
    (term * term list) list list -> context -> state
wenzelm@36323
    94
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
    95
    (string * string list) list list -> context -> state
wenzelm@49889
    96
  val global_qed: Method.text_range option * bool -> state -> context
wenzelm@49889
    97
  val local_terminal_proof: Method.text_range * Method.text_range option -> state -> state
wenzelm@29383
    98
  val local_default_proof: state -> state
wenzelm@29383
    99
  val local_immediate_proof: state -> state
wenzelm@29383
   100
  val local_skip_proof: bool -> state -> state
wenzelm@29383
   101
  val local_done_proof: state -> state
wenzelm@49889
   102
  val global_terminal_proof: Method.text_range * Method.text_range option -> state -> context
wenzelm@20363
   103
  val global_default_proof: state -> context
wenzelm@20363
   104
  val global_immediate_proof: state -> context
wenzelm@29383
   105
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   106
  val global_done_proof: state -> context
wenzelm@60406
   107
  val internal_goal: (context -> (string * string) * (string * thm list) list -> unit) ->
wenzelm@60555
   108
    Proof_Context.mode -> bool -> string -> Method.text option ->
wenzelm@60415
   109
    (context * thm list list -> state -> state) ->
wenzelm@60406
   110
    (binding * typ option * mixfix) list ->
wenzelm@60414
   111
    (Thm.binding * (term * term list) list) list ->
wenzelm@60461
   112
    (Thm.binding * (term * term list) list) list -> state -> thm list * state
wenzelm@60555
   113
  val have: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wenzelm@60406
   114
    (binding * typ option * mixfix) list ->
wenzelm@60414
   115
    (Thm.binding * (term * term list) list) list ->
wenzelm@60461
   116
    (Thm.binding * (term * term list) list) list -> bool -> state -> thm list * state
wenzelm@60555
   117
  val have_cmd: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wenzelm@60406
   118
    (binding * string option * mixfix) list ->
wenzelm@60414
   119
    (Attrib.binding * (string * string list) list) list ->
wenzelm@60461
   120
    (Attrib.binding * (string * string list) list) list -> bool -> state -> thm list * state
wenzelm@60555
   121
  val show: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wenzelm@60406
   122
    (binding * typ option * mixfix) list ->
wenzelm@60414
   123
    (Thm.binding * (term * term list) list) list ->
wenzelm@60461
   124
    (Thm.binding * (term * term list) list) list -> bool -> state -> thm list * state
wenzelm@60555
   125
  val show_cmd: bool -> Method.text option -> (context * thm list list -> state -> state) ->
wenzelm@60406
   126
    (binding * string option * mixfix) list ->
wenzelm@60414
   127
    (Attrib.binding * (string * string list) list) list ->
wenzelm@60461
   128
    (Attrib.binding * (string * string list) list) list -> bool -> state -> thm list * state
wenzelm@29385
   129
  val schematic_goal: state -> bool
wenzelm@51226
   130
  val is_relevant: state -> bool
wenzelm@51318
   131
  val future_proof: (state -> ('a * context) future) -> state -> 'a future * state
wenzelm@60403
   132
  val local_future_terminal_proof: Method.text_range * Method.text_range option -> state -> state
wenzelm@60403
   133
  val global_future_terminal_proof: Method.text_range * Method.text_range option -> state -> context
wenzelm@5820
   134
end;
wenzelm@5820
   135
wenzelm@13377
   136
structure Proof: PROOF =
wenzelm@5820
   137
struct
wenzelm@5820
   138
wenzelm@33031
   139
type context = Proof.context;
wenzelm@17112
   140
type method = Method.method;
wenzelm@16813
   141
wenzelm@5820
   142
wenzelm@5820
   143
(** proof state **)
wenzelm@5820
   144
wenzelm@17359
   145
(* datatype state *)
wenzelm@5820
   146
wenzelm@17112
   147
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   148
wenzelm@17359
   149
datatype state =
wenzelm@17359
   150
  State of node Stack.T
wenzelm@17359
   151
and node =
wenzelm@7176
   152
  Node of
wenzelm@7176
   153
   {context: context,
wenzelm@7176
   154
    facts: thm list option,
wenzelm@7176
   155
    mode: mode,
wenzelm@17359
   156
    goal: goal option}
wenzelm@17359
   157
and goal =
wenzelm@17359
   158
  Goal of
wenzelm@60415
   159
   {statement: (string * Position.T) * term list list * term,
wenzelm@60415
   160
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   161
    using: thm list,                      (*goal facts*)
wenzelm@25958
   162
    goal: thm,                            (*subgoals ==> statement*)
wenzelm@17859
   163
    before_qed: Method.text option,
wenzelm@18124
   164
    after_qed:
wenzelm@60550
   165
      (context * thm list list -> state -> state) *
wenzelm@60550
   166
      (context * thm list list -> context -> context)};
wenzelm@17359
   167
wenzelm@58892
   168
fun make_goal (statement, using, goal, before_qed, after_qed) =
wenzelm@58892
   169
  Goal {statement = statement, using = using, goal = goal,
wenzelm@17859
   170
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   171
wenzelm@7176
   172
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   173
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   174
wenzelm@17359
   175
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   176
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   177
wenzelm@21727
   178
val init_context =
wenzelm@42360
   179
  Proof_Context.set_stmt true #>
wenzelm@47005
   180
  Proof_Context.map_naming (K Name_Space.local_naming);
wenzelm@21727
   181
wenzelm@21466
   182
fun init ctxt =
wenzelm@21727
   183
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   184
wenzelm@58796
   185
fun top (State stack) = Stack.top stack |> (fn Node node => node);
wenzelm@58796
   186
fun map_top f (State stack) = State (Stack.map_top (map_node f) stack);
wenzelm@58796
   187
fun map_all f (State stack) = State (Stack.map_all (map_node f) stack);
wenzelm@12045
   188
wenzelm@5820
   189
wenzelm@5820
   190
wenzelm@5820
   191
(** basic proof state operations **)
wenzelm@5820
   192
wenzelm@17359
   193
(* block structure *)
wenzelm@17359
   194
wenzelm@58796
   195
fun open_block (State stack) = State (Stack.push stack);
wenzelm@17359
   196
wenzelm@58796
   197
fun close_block (State stack) = State (Stack.pop stack)
wenzelm@47060
   198
  handle List.Empty => error "Unbalanced block parentheses";
wenzelm@17359
   199
wenzelm@58796
   200
fun level (State stack) = Stack.level stack;
wenzelm@17359
   201
wenzelm@17359
   202
fun assert_bottom b state =
wenzelm@47065
   203
  let val b' = level state <= 2 in
wenzelm@47065
   204
    if b andalso not b' then error "Not at bottom of proof"
wenzelm@47065
   205
    else if not b andalso b' then error "Already at bottom of proof"
wenzelm@17359
   206
    else state
wenzelm@17359
   207
  end;
wenzelm@17359
   208
wenzelm@17359
   209
wenzelm@5820
   210
(* context *)
wenzelm@5820
   211
wenzelm@49011
   212
val context_of = #context o top;
wenzelm@42360
   213
val theory_of = Proof_Context.theory_of o context_of;
wenzelm@5820
   214
wenzelm@17359
   215
fun map_context f =
wenzelm@49011
   216
  map_top (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   217
wenzelm@17359
   218
fun map_context_result f state =
wenzelm@17859
   219
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   220
wenzelm@28278
   221
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   222
wenzelm@37949
   223
fun propagate_ml_env state = map_contexts
wenzelm@37949
   224
  (Context.proof_map (ML_Env.inherit (Context.Proof (context_of state)))) state;
wenzelm@37949
   225
wenzelm@42360
   226
val put_thms = map_context oo Proof_Context.put_thms;
wenzelm@5820
   227
wenzelm@5820
   228
wenzelm@5820
   229
(* facts *)
wenzelm@5820
   230
wenzelm@49011
   231
val get_facts = #facts o top;
wenzelm@17359
   232
wenzelm@17359
   233
fun the_facts state =
wenzelm@60550
   234
  (case get_facts state of
wenzelm@60550
   235
    SOME facts => facts
wenzelm@18678
   236
  | NONE => error "No current facts available");
wenzelm@5820
   237
wenzelm@9469
   238
fun the_fact state =
wenzelm@60550
   239
  (case the_facts state of
wenzelm@60550
   240
    [thm] => thm
wenzelm@18678
   241
  | _ => error "Single theorem expected");
wenzelm@7605
   242
wenzelm@17359
   243
fun put_facts facts =
wenzelm@49011
   244
  map_top (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wenzelm@33386
   245
  put_thms true (Auto_Bind.thisN, facts);
wenzelm@5820
   246
wenzelm@47068
   247
val set_facts = put_facts o SOME;
wenzelm@47068
   248
val reset_facts = put_facts NONE;
wenzelm@47068
   249
wenzelm@17359
   250
fun these_factss more_facts (named_factss, state) =
wenzelm@47068
   251
  (named_factss, state |> set_facts (maps snd named_factss @ more_facts));
wenzelm@5820
   252
wenzelm@17359
   253
fun export_facts inner outer =
wenzelm@17359
   254
  (case get_facts inner of
wenzelm@47068
   255
    NONE => reset_facts outer
wenzelm@17359
   256
  | SOME thms =>
wenzelm@17359
   257
      thms
wenzelm@42360
   258
      |> Proof_Context.export (context_of inner) (context_of outer)
wenzelm@47068
   259
      |> (fn ths => set_facts ths outer));
wenzelm@5820
   260
wenzelm@5820
   261
wenzelm@5820
   262
(* mode *)
wenzelm@5820
   263
wenzelm@49011
   264
val get_mode = #mode o top;
wenzelm@49011
   265
fun put_mode mode = map_top (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   266
wenzelm@17359
   267
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   268
wenzelm@5820
   269
fun assert_mode pred state =
wenzelm@5820
   270
  let val mode = get_mode state in
wenzelm@5820
   271
    if pred mode then state
wenzelm@18678
   272
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   273
  end;
wenzelm@5820
   274
wenzelm@19308
   275
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   276
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   277
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   278
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   279
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   280
wenzelm@17359
   281
val enter_forward = put_mode Forward;
wenzelm@17359
   282
val enter_chain = put_mode Chain;
wenzelm@17359
   283
val enter_backward = put_mode Backward;
wenzelm@5820
   284
wenzelm@17359
   285
wenzelm@17359
   286
(* current goal *)
wenzelm@17359
   287
wenzelm@17359
   288
fun current_goal state =
wenzelm@49011
   289
  (case top state of
wenzelm@60415
   290
    {context = ctxt, goal = SOME (Goal goal), ...} => (ctxt, goal)
wenzelm@47065
   291
  | _ => error "No current goal");
wenzelm@5820
   292
wenzelm@17359
   293
fun assert_current_goal g state =
wenzelm@17359
   294
  let val g' = can current_goal state in
wenzelm@47065
   295
    if g andalso not g' then error "No goal in this block"
wenzelm@47065
   296
    else if not g andalso g' then error "Goal present in this block"
wenzelm@17359
   297
    else state
wenzelm@17359
   298
  end;
wenzelm@6776
   299
wenzelm@49011
   300
fun put_goal goal = map_top (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   301
wenzelm@47068
   302
val set_goal = put_goal o SOME;
wenzelm@47068
   303
val reset_goal = put_goal NONE;
wenzelm@47068
   304
wenzelm@17859
   305
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   306
wenzelm@17359
   307
wenzelm@58798
   308
(* bottom goal *)
wenzelm@58798
   309
wenzelm@58798
   310
fun has_bottom_goal (State stack) =
wenzelm@58798
   311
  let
wenzelm@58798
   312
    fun bottom [Node {goal = SOME _, ...}, Node {goal = NONE, ...}] = true
wenzelm@58798
   313
      | bottom [Node {goal, ...}] = is_some goal
wenzelm@58798
   314
      | bottom [] = false
wenzelm@58798
   315
      | bottom (_ :: rest) = bottom rest;
wenzelm@58798
   316
  in bottom (op :: (Stack.dest stack)) end;
wenzelm@58798
   317
wenzelm@58798
   318
wenzelm@17359
   319
(* nested goal *)
wenzelm@5820
   320
wenzelm@60611
   321
fun map_goal f (State stack) =
wenzelm@58797
   322
  (case Stack.dest stack of
wenzelm@60611
   323
    (Node {context = ctxt, facts, mode, goal = SOME goal}, node :: nodes) =>
wenzelm@17359
   324
      let
wenzelm@58892
   325
        val Goal {statement, using, goal, before_qed, after_qed} = goal;
wenzelm@60611
   326
        val (ctxt', statement', using', goal', before_qed', after_qed') =
wenzelm@60611
   327
          f (ctxt, statement, using, goal, before_qed, after_qed);
wenzelm@60611
   328
        val goal' = make_goal (statement', using', goal', before_qed', after_qed');
wenzelm@60611
   329
      in State (Stack.make (make_node (ctxt', facts, mode, SOME goal')) (node :: nodes)) end
wenzelm@60611
   330
  | (top_node, node :: nodes) =>
wenzelm@47431
   331
      let
wenzelm@60611
   332
        val State stack' = map_goal f (State (Stack.make node nodes));
wenzelm@58797
   333
        val (node', nodes') = Stack.dest stack';
wenzelm@60611
   334
      in State (Stack.make top_node (node' :: nodes')) end
wenzelm@58797
   335
  | _ => State stack);
wenzelm@5820
   336
wenzelm@60550
   337
fun provide_goal goal =
wenzelm@60611
   338
  map_goal (fn (ctxt, statement, using, _, before_qed, after_qed) =>
wenzelm@60611
   339
    (ctxt, statement, using, goal, before_qed, after_qed));
wenzelm@19188
   340
wenzelm@60550
   341
fun using_facts using =
wenzelm@60611
   342
  map_goal (fn (ctxt, statement, _, goal, before_qed, after_qed) =>
wenzelm@60611
   343
    (ctxt, statement, using, goal, before_qed, after_qed));
wenzelm@17359
   344
wenzelm@60611
   345
fun find_goal state =
wenzelm@60611
   346
  (case try current_goal state of
wenzelm@60611
   347
    SOME ctxt_goal => ctxt_goal
wenzelm@60611
   348
  | NONE => find_goal (close_block state handle ERROR _ => error "No proof goal"));
wenzelm@17359
   349
wenzelm@17359
   350
fun get_goal state =
wenzelm@60611
   351
  let val (ctxt, {using, goal, ...}) = find_goal state
wenzelm@17359
   352
  in (ctxt, (using, goal)) end;
wenzelm@5820
   353
wenzelm@5820
   354
wenzelm@5820
   355
wenzelm@12423
   356
(** pretty_state **)
wenzelm@5820
   357
wenzelm@59185
   358
local
wenzelm@59185
   359
skalberg@15531
   360
fun pretty_facts _ _ NONE = []
wenzelm@59185
   361
  | pretty_facts ctxt s (SOME ths) = [Proof_Display.pretty_goal_facts ctxt s ths];
wenzelm@59185
   362
wenzelm@59185
   363
fun pretty_sep prts [] = prts
wenzelm@59185
   364
  | pretty_sep [] prts = prts
wenzelm@59185
   365
  | pretty_sep prts1 prts2 = prts1 @ [Pretty.str ""] @ prts2;
wenzelm@59185
   366
wenzelm@59185
   367
in
wenzelm@6756
   368
wenzelm@60403
   369
fun pretty_state state =
wenzelm@5820
   370
  let
wenzelm@49011
   371
    val {context = ctxt, facts, mode, goal = _} = top state;
wenzelm@42717
   372
    val verbose = Config.get ctxt Proof_Context.verbose;
wenzelm@5820
   373
wenzelm@60611
   374
    fun prt_goal (SOME (_, {statement = _, using, goal, before_qed = _, after_qed = _})) =
wenzelm@59185
   375
          pretty_sep
wenzelm@59185
   376
            (pretty_facts ctxt "using"
wenzelm@59185
   377
              (if mode <> Backward orelse null using then NONE else SOME using))
wenzelm@59185
   378
            ([Proof_Display.pretty_goal_header goal] @ Goal_Display.pretty_goals ctxt goal)
wenzelm@17359
   379
      | prt_goal NONE = [];
wenzelm@6848
   380
wenzelm@17359
   381
    val prt_ctxt =
wenzelm@42717
   382
      if verbose orelse mode = Forward then Proof_Context.pretty_context ctxt
wenzelm@42360
   383
      else if mode = Backward then Proof_Context.pretty_ctxt ctxt
wenzelm@7575
   384
      else [];
wenzelm@56912
   385
wenzelm@56912
   386
    val position_markup = Position.markup (Position.thread_data ()) Markup.position;
wenzelm@17359
   387
  in
wenzelm@56912
   388
    [Pretty.block
wenzelm@60551
   389
      [Pretty.mark_str (position_markup, "proof"), Pretty.str (" (" ^ mode_name mode ^ ")")]] @
wenzelm@17359
   390
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@42717
   391
    (if verbose orelse mode = Forward then
wenzelm@59185
   392
       pretty_sep (pretty_facts ctxt "" facts) (prt_goal (try find_goal state))
wenzelm@51584
   393
     else if mode = Chain then pretty_facts ctxt "picking" facts
wenzelm@17359
   394
     else prt_goal (try find_goal state))
wenzelm@17359
   395
  end;
wenzelm@5820
   396
wenzelm@59185
   397
end;
wenzelm@59185
   398
wenzelm@5820
   399
wenzelm@5820
   400
wenzelm@5820
   401
(** proof steps **)
wenzelm@5820
   402
wenzelm@17359
   403
(* refine via method *)
wenzelm@5820
   404
wenzelm@8234
   405
local
wenzelm@8234
   406
wenzelm@16146
   407
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   408
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   409
wenzelm@16146
   410
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   411
wenzelm@59970
   412
fun goal_cases ctxt st =
wenzelm@59970
   413
  Rule_Cases.make_common ctxt (Thm.prop_of st) (map (rpair [] o rpair []) (goals st));
wenzelm@16146
   414
wenzelm@58007
   415
fun apply_method text ctxt state =
wenzelm@60611
   416
  find_goal state |> (fn (goal_ctxt, {statement, using, goal, before_qed, after_qed}) =>
wenzelm@58007
   417
    Method.evaluate text ctxt using goal
wenzelm@58004
   418
    |> Seq.map (fn (meth_cases, goal') =>
wenzelm@60611
   419
      let
wenzelm@60611
   420
        val goal_ctxt' = goal_ctxt
wenzelm@60611
   421
          |> Proof_Context.update_cases_legacy (no_goal_cases goal @ goal_cases ctxt goal')
wenzelm@60611
   422
          |> Proof_Context.update_cases meth_cases;
wenzelm@60611
   423
        val state' = state
wenzelm@60611
   424
          |> map_goal (K (goal_ctxt', statement, using, goal', before_qed, after_qed));
wenzelm@60611
   425
      in state' end));
wenzelm@5820
   426
wenzelm@8234
   427
in
wenzelm@8234
   428
wenzelm@58007
   429
fun refine text state = apply_method text (context_of state) state;
wenzelm@58007
   430
fun refine_end text state = apply_method text (#1 (find_goal state)) state;
wenzelm@58004
   431
wenzelm@58004
   432
fun refine_insert ths =
wenzelm@58004
   433
  Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   434
wenzelm@60623
   435
fun refine_primitive r =  (* FIXME Seq.Error!? *)
wenzelm@60623
   436
  Seq.hd o refine (Method.Basic (fn ctxt => fn _ => EMPTY_CASES (PRIMITIVE (r ctxt))));
wenzelm@60623
   437
wenzelm@8234
   438
end;
wenzelm@8234
   439
wenzelm@5820
   440
wenzelm@17359
   441
(* refine via sub-proof *)
wenzelm@17359
   442
wenzelm@46466
   443
local
wenzelm@46466
   444
wenzelm@54742
   445
fun finish_tac _ 0 = K all_tac
wenzelm@54742
   446
  | finish_tac ctxt n =
wenzelm@54742
   447
      Goal.norm_hhf_tac ctxt THEN'
wenzelm@30557
   448
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   449
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@59498
   450
          eresolve_tac ctxt [Drule.protectI] i THEN finish_tac ctxt (n - 1) i
wenzelm@54742
   451
        else finish_tac ctxt (n - 1) (i + 1));
wenzelm@30557
   452
wenzelm@54742
   453
fun goal_tac ctxt rule =
wenzelm@54742
   454
  Goal.norm_hhf_tac ctxt THEN'
wenzelm@59498
   455
  resolve_tac ctxt [rule] THEN'
wenzelm@54742
   456
  finish_tac ctxt (Thm.nprems_of rule);
wenzelm@11816
   457
wenzelm@46466
   458
fun FINDGOAL tac st =
wenzelm@46466
   459
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@46466
   460
  in find 1 (Thm.nprems_of st) st end;
wenzelm@46466
   461
wenzelm@60595
   462
fun protect_prem i th =
wenzelm@60595
   463
  Thm.bicompose NONE {flatten = false, match = false, incremented = true}
wenzelm@60595
   464
    (false, Drule.incr_indexes th Drule.protectD, 1) i th
wenzelm@60595
   465
  |> Seq.hd;
wenzelm@60595
   466
wenzelm@60595
   467
fun protect_prems th =
wenzelm@60595
   468
  fold_rev protect_prem (1 upto Thm.nprems_of th) th;
wenzelm@60595
   469
wenzelm@46466
   470
in
wenzelm@46466
   471
wenzelm@60415
   472
fun refine_goals print_rule result_ctxt result state =
wenzelm@19915
   473
  let
wenzelm@60415
   474
    val (goal_ctxt, (_, goal)) = get_goal state;
wenzelm@60550
   475
    fun refine rule st =
wenzelm@60550
   476
      (print_rule goal_ctxt rule; FINDGOAL (goal_tac goal_ctxt rule) st);
wenzelm@19915
   477
  in
wenzelm@60415
   478
    result
wenzelm@60595
   479
    |> map (Raw_Simplifier.norm_hhf result_ctxt #> protect_prems)
wenzelm@60415
   480
    |> Proof_Context.goal_export result_ctxt goal_ctxt
wenzelm@47068
   481
    |> (fn rules => Seq.lift provide_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   482
  end;
wenzelm@17359
   483
wenzelm@46466
   484
end;
wenzelm@46466
   485
wenzelm@6932
   486
wenzelm@49846
   487
(* conclude goal *)
wenzelm@49846
   488
wenzelm@28627
   489
fun conclude_goal ctxt goal propss =
wenzelm@5820
   490
  let
wenzelm@42360
   491
    val thy = Proof_Context.theory_of ctxt;
wenzelm@5820
   492
wenzelm@54981
   493
    val _ =
wenzelm@59582
   494
      Theory.subthy (Thm.theory_of_thm goal, thy) orelse
wenzelm@59582
   495
        error "Bad background theory of goal state";
wenzelm@49860
   496
    val _ = Thm.no_prems goal orelse error (Proof_Display.string_of_goal ctxt goal);
wenzelm@20224
   497
wenzelm@60550
   498
    fun err_lost () = error ("Lost goal structure:\n" ^ Display.string_of_thm ctxt goal);
wenzelm@5820
   499
wenzelm@54567
   500
    val th =
wenzelm@54567
   501
      (Goal.conclude (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
wenzelm@60550
   502
        handle THM _ => err_lost ())
wenzelm@58950
   503
      |> Drule.flexflex_unique (SOME ctxt)
wenzelm@54567
   504
      |> Thm.check_shyps (Variable.sorts_of ctxt)
wenzelm@54993
   505
      |> Thm.check_hyps (Context.Proof ctxt);
wenzelm@23418
   506
wenzelm@23418
   507
    val goal_propss = filter_out null propss;
wenzelm@23418
   508
    val results =
wenzelm@23418
   509
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   510
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@60550
   511
      handle THM _ => err_lost ();
wenzelm@58950
   512
    val _ =
wenzelm@58950
   513
      Unify.matches_list (Context.Proof ctxt) (flat goal_propss) (map Thm.prop_of (flat results))
wenzelm@58950
   514
        orelse error ("Proved a different theorem:\n" ^ Display.string_of_thm ctxt th);
wenzelm@23418
   515
wenzelm@23418
   516
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   517
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   518
      | recover_result [] [] = []
wenzelm@60550
   519
      | recover_result _ _ = err_lost ();
wenzelm@23418
   520
  in recover_result propss results end;
wenzelm@5820
   521
wenzelm@49859
   522
val finished_goal_error = "Failed to finish proof";
wenzelm@49859
   523
wenzelm@49889
   524
fun finished_goal pos state =
wenzelm@49859
   525
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@49859
   526
    if Thm.no_prems goal then Seq.Result state
wenzelm@49889
   527
    else
wenzelm@49889
   528
      Seq.Error (fn () =>
wenzelm@49889
   529
        finished_goal_error ^ Position.here pos ^ ":\n" ^
wenzelm@49889
   530
          Proof_Display.string_of_goal ctxt goal)
wenzelm@49859
   531
  end;
wenzelm@49846
   532
wenzelm@5820
   533
wenzelm@33288
   534
(* goal views -- corresponding to methods *)
wenzelm@33288
   535
wenzelm@33288
   536
fun raw_goal state =
wenzelm@33288
   537
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   538
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   539
wenzelm@33288
   540
val goal = raw_goal o refine_insert [];
wenzelm@33288
   541
wenzelm@33288
   542
fun simple_goal state =
wenzelm@33288
   543
  let
wenzelm@33288
   544
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   545
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   546
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   547
wenzelm@38721
   548
fun status_markup state =
wenzelm@38721
   549
  (case try goal state of
wenzelm@50201
   550
    SOME {goal, ...} => Markup.proof_state (Thm.nprems_of goal)
wenzelm@38721
   551
  | NONE => Markup.empty);
wenzelm@38721
   552
wenzelm@49866
   553
fun method_error kind pos state =
wenzelm@49866
   554
  Seq.single (Proof_Display.method_error kind pos (raw_goal state));
wenzelm@49861
   555
wenzelm@33288
   556
wenzelm@5820
   557
wenzelm@5820
   558
(*** structured proof commands ***)
wenzelm@5820
   559
wenzelm@17112
   560
(** context elements **)
wenzelm@5820
   561
wenzelm@36324
   562
(* let bindings *)
wenzelm@5820
   563
wenzelm@16813
   564
local
wenzelm@16813
   565
wenzelm@17359
   566
fun gen_bind bind args state =
wenzelm@5820
   567
  state
wenzelm@5820
   568
  |> assert_forward
wenzelm@36324
   569
  |> map_context (bind true args #> snd)
wenzelm@47068
   570
  |> reset_facts;
wenzelm@5820
   571
wenzelm@16813
   572
in
wenzelm@16813
   573
wenzelm@60377
   574
val let_bind = gen_bind Proof_Context.match_bind;
wenzelm@60377
   575
val let_bind_cmd = gen_bind Proof_Context.match_bind_cmd;
wenzelm@5820
   576
wenzelm@16813
   577
end;
wenzelm@16813
   578
wenzelm@5820
   579
wenzelm@36505
   580
(* concrete syntax *)
wenzelm@36505
   581
wenzelm@36505
   582
local
wenzelm@36505
   583
wenzelm@36507
   584
fun gen_write prep_arg mode args =
wenzelm@36507
   585
  assert_forward
wenzelm@42360
   586
  #> map_context (fn ctxt => ctxt |> Proof_Context.notation true mode (map (prep_arg ctxt) args))
wenzelm@47068
   587
  #> reset_facts;
wenzelm@36505
   588
wenzelm@55955
   589
fun read_arg ctxt (c, mx) =
wenzelm@56002
   590
  (case Proof_Context.read_const {proper = false, strict = false} ctxt c of
wenzelm@55955
   591
    Free (x, _) =>
wenzelm@55955
   592
      let val T = Proof_Context.infer_type ctxt (x, Mixfix.mixfixT mx)
wenzelm@55955
   593
      in (Free (x, T), mx) end
wenzelm@55955
   594
  | t => (t, mx));
wenzelm@55955
   595
wenzelm@36505
   596
in
wenzelm@36505
   597
wenzelm@36505
   598
val write = gen_write (K I);
wenzelm@55955
   599
val write_cmd = gen_write read_arg;
wenzelm@36505
   600
wenzelm@36505
   601
end;
wenzelm@36505
   602
wenzelm@36505
   603
wenzelm@17359
   604
(* fix *)
wenzelm@9196
   605
wenzelm@12714
   606
local
wenzelm@12714
   607
wenzelm@60469
   608
fun gen_fix add_fixes raw_fixes =
wenzelm@17359
   609
  assert_forward
wenzelm@60469
   610
  #> map_context (snd o add_fixes raw_fixes)
wenzelm@47068
   611
  #> reset_facts;
wenzelm@5820
   612
wenzelm@16813
   613
in
wenzelm@16813
   614
wenzelm@60469
   615
val fix = gen_fix Proof_Context.add_fixes;
wenzelm@60469
   616
val fix_cmd = gen_fix Proof_Context.add_fixes_cmd;
wenzelm@5820
   617
wenzelm@16813
   618
end;
wenzelm@16813
   619
wenzelm@5820
   620
wenzelm@17359
   621
(* assume etc. *)
wenzelm@5820
   622
wenzelm@16813
   623
local
wenzelm@16813
   624
wenzelm@17112
   625
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   626
  state
wenzelm@5820
   627
  |> assert_forward
wenzelm@47815
   628
  |> map_context_result (asm exp (Attrib.map_specs (map (prep_att (context_of state))) args))
wenzelm@17359
   629
  |> these_factss [] |> #2;
wenzelm@6932
   630
wenzelm@16813
   631
in
wenzelm@16813
   632
wenzelm@60377
   633
val assm = gen_assume Proof_Context.add_assms (K I);
wenzelm@60377
   634
val assm_cmd = gen_assume Proof_Context.add_assms_cmd Attrib.attribute_cmd;
wenzelm@36323
   635
val assume = assm Assumption.assume_export;
wenzelm@36323
   636
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   637
val presume = assm Assumption.presume_export;
wenzelm@36323
   638
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   639
wenzelm@16813
   640
end;
wenzelm@16813
   641
wenzelm@7271
   642
wenzelm@17359
   643
(* def *)
wenzelm@11891
   644
wenzelm@16813
   645
local
wenzelm@16813
   646
wenzelm@60379
   647
fun gen_def prep_att prep_var prep_binds args state =
wenzelm@11891
   648
  let
wenzelm@11891
   649
    val _ = assert_forward state;
wenzelm@20913
   650
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@47815
   651
    val name_atts = map (apsnd (map (prep_att (context_of state)))) raw_name_atts;
wenzelm@11891
   652
  in
wenzelm@20913
   653
    state
wenzelm@60379
   654
    |> map_context_result (fold_map (fn (x, mx) => prep_var (x, NONE, mx)) raw_vars)
wenzelm@20913
   655
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   656
    |-> (fn vars =>
wenzelm@20913
   657
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@49748
   658
      #-> (fn rhss =>
wenzelm@49748
   659
        let
wenzelm@49748
   660
          val defs = (vars ~~ (name_atts ~~ rhss)) |> map (fn ((x, mx), ((a, atts), rhs)) =>
wenzelm@49748
   661
            ((x, mx), ((Thm.def_binding_optional x a, atts), rhs)));
wenzelm@49748
   662
        in map_context_result (Local_Defs.add_defs defs) end))
wenzelm@47068
   663
    |-> (set_facts o map (#2 o #2))
wenzelm@11891
   664
  end;
wenzelm@11891
   665
wenzelm@16813
   666
in
wenzelm@16813
   667
wenzelm@60379
   668
val def = gen_def (K I) Proof_Context.cert_var Proof_Context.match_bind;
wenzelm@60379
   669
val def_cmd = gen_def Attrib.attribute_cmd Proof_Context.read_var Proof_Context.match_bind_cmd;
wenzelm@11891
   670
wenzelm@16813
   671
end;
wenzelm@16813
   672
wenzelm@11891
   673
wenzelm@8374
   674
wenzelm@17112
   675
(** facts **)
wenzelm@5820
   676
wenzelm@17359
   677
(* chain *)
wenzelm@5820
   678
wenzelm@24011
   679
fun clean_facts ctxt =
wenzelm@47068
   680
  set_facts (filter_out Thm.is_dummy (the_facts ctxt)) ctxt;
wenzelm@24011
   681
wenzelm@17359
   682
val chain =
wenzelm@17359
   683
  assert_forward
wenzelm@24011
   684
  #> clean_facts
wenzelm@17359
   685
  #> enter_chain;
wenzelm@5820
   686
wenzelm@17359
   687
fun chain_facts facts =
wenzelm@47068
   688
  set_facts facts
wenzelm@17359
   689
  #> chain;
wenzelm@5820
   690
wenzelm@5820
   691
wenzelm@17359
   692
(* note etc. *)
wenzelm@17112
   693
haftmann@28965
   694
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   695
wenzelm@17112
   696
local
wenzelm@17112
   697
wenzelm@30760
   698
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   699
  state
wenzelm@17112
   700
  |> assert_forward
wenzelm@47815
   701
  |> map_context_result (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@47815
   702
    (Attrib.map_facts_refs (map (prep_atts ctxt)) (prep_fact ctxt) args))
wenzelm@17112
   703
  |> these_factss (more_facts state)
wenzelm@17359
   704
  ||> opt_chain
wenzelm@17359
   705
  |> opt_result;
wenzelm@17112
   706
wenzelm@17112
   707
in
wenzelm@17112
   708
wenzelm@36323
   709
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@47815
   710
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   711
wenzelm@36323
   712
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@47815
   713
val from_thmss_cmd =
wenzelm@47815
   714
  gen_thmss (K []) chain #2 Attrib.attribute_cmd Proof_Context.get_fact o no_binding;
wenzelm@17359
   715
wenzelm@36323
   716
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@47815
   717
val with_thmss_cmd =
wenzelm@47815
   718
  gen_thmss the_facts chain #2 Attrib.attribute_cmd Proof_Context.get_fact o no_binding;
wenzelm@30760
   719
wenzelm@30760
   720
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   721
wenzelm@17112
   722
end;
wenzelm@17112
   723
wenzelm@17112
   724
wenzelm@60371
   725
(* facts during goal refinement *)
wenzelm@60371
   726
wenzelm@60371
   727
local
wenzelm@60371
   728
wenzelm@60371
   729
fun gen_supply prep_att prep_fact args state =
wenzelm@60371
   730
  state
wenzelm@60371
   731
  |> assert_backward
wenzelm@60371
   732
  |> map_context (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@60371
   733
       (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) args) |> snd);
wenzelm@60371
   734
wenzelm@60371
   735
in
wenzelm@60371
   736
wenzelm@60371
   737
val supply = gen_supply (K I) (K I);
wenzelm@60371
   738
val supply_cmd = gen_supply Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@60371
   739
wenzelm@60371
   740
end;
wenzelm@60371
   741
wenzelm@60371
   742
wenzelm@18548
   743
(* using/unfolding *)
wenzelm@17112
   744
wenzelm@17112
   745
local
wenzelm@17112
   746
wenzelm@45390
   747
fun gen_using f g prep_att prep_fact args state =
wenzelm@17112
   748
  state
wenzelm@17112
   749
  |> assert_backward
wenzelm@21442
   750
  |> map_context_result
wenzelm@47815
   751
    (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@47815
   752
      (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) (no_binding args)))
wenzelm@18843
   753
  |> (fn (named_facts, state') =>
wenzelm@60611
   754
    state' |> map_goal (fn (goal_ctxt, statement, using, goal, before_qed, after_qed) =>
wenzelm@18843
   755
      let
wenzelm@18843
   756
        val ctxt = context_of state';
wenzelm@19482
   757
        val ths = maps snd named_facts;
wenzelm@60611
   758
      in (goal_ctxt, statement, f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   759
wenzelm@24050
   760
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   761
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   762
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   763
wenzelm@17112
   764
in
wenzelm@17112
   765
wenzelm@36323
   766
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@47815
   767
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@36323
   768
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@47815
   769
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   770
wenzelm@17112
   771
end;
wenzelm@17112
   772
wenzelm@17112
   773
wenzelm@17112
   774
(* case *)
wenzelm@17112
   775
wenzelm@17112
   776
local
wenzelm@17112
   777
wenzelm@60565
   778
fun gen_case internal prep_att ((raw_binding, raw_atts), ((name, pos), xs)) state =
wenzelm@17112
   779
  let
wenzelm@60565
   780
    val ctxt = context_of state;
wenzelm@60565
   781
wenzelm@60565
   782
    val binding = if Binding.is_empty raw_binding then Binding.make (name, pos) else raw_binding;
wenzelm@60565
   783
    val atts = map (prep_att ctxt) raw_atts;
wenzelm@60565
   784
wenzelm@19078
   785
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@57486
   786
      ctxt |> Proof_Context.apply_case (Proof_Context.check_case ctxt internal (name, pos) xs));
wenzelm@53380
   787
    val assumptions =
wenzelm@60565
   788
      asms |> map (fn (a, ts) => ((Binding.qualified true a binding, []), map (rpair []) ts));
wenzelm@17112
   789
  in
wenzelm@17112
   790
    state'
wenzelm@36323
   791
    |> assume assumptions
wenzelm@60401
   792
    |> map_context (fold Variable.unbind_term Auto_Bind.no_facts)
wenzelm@60565
   793
    |> `the_facts |-> (fn thms => note_thmss [((binding, atts), [(thms, [])])])
wenzelm@17112
   794
  end;
wenzelm@17112
   795
wenzelm@17112
   796
in
wenzelm@17112
   797
wenzelm@60565
   798
val case_ = gen_case true (K I);
wenzelm@60565
   799
val case_cmd = gen_case false Attrib.attribute_cmd;
wenzelm@17112
   800
wenzelm@17112
   801
end;
wenzelm@17112
   802
wenzelm@17112
   803
wenzelm@17112
   804
wenzelm@17359
   805
(** proof structure **)
wenzelm@17359
   806
wenzelm@17359
   807
(* blocks *)
wenzelm@17359
   808
wenzelm@17359
   809
val begin_block =
wenzelm@17359
   810
  assert_forward
wenzelm@17359
   811
  #> open_block
wenzelm@47068
   812
  #> reset_goal
wenzelm@17359
   813
  #> open_block;
wenzelm@17359
   814
wenzelm@17359
   815
val next_block =
wenzelm@17359
   816
  assert_forward
wenzelm@17359
   817
  #> close_block
wenzelm@17359
   818
  #> open_block
wenzelm@47068
   819
  #> reset_goal
wenzelm@47068
   820
  #> reset_facts;
wenzelm@17359
   821
wenzelm@17359
   822
fun end_block state =
wenzelm@17359
   823
  state
wenzelm@17359
   824
  |> assert_forward
wenzelm@40960
   825
  |> assert_bottom false
wenzelm@17359
   826
  |> close_block
wenzelm@17359
   827
  |> assert_current_goal false
wenzelm@17359
   828
  |> close_block
wenzelm@17359
   829
  |> export_facts state;
wenzelm@17359
   830
wenzelm@17359
   831
wenzelm@40960
   832
(* global notepad *)
wenzelm@40960
   833
wenzelm@40960
   834
val begin_notepad =
wenzelm@40960
   835
  init
wenzelm@40960
   836
  #> open_block
wenzelm@40960
   837
  #> map_context (Variable.set_body true)
wenzelm@40960
   838
  #> open_block;
wenzelm@40960
   839
wenzelm@40960
   840
val end_notepad =
wenzelm@40960
   841
  assert_forward
wenzelm@40960
   842
  #> assert_bottom true
wenzelm@40960
   843
  #> close_block
wenzelm@40960
   844
  #> assert_current_goal false
wenzelm@40960
   845
  #> close_block
wenzelm@40960
   846
  #> context_of;
wenzelm@40960
   847
wenzelm@40960
   848
wenzelm@17359
   849
(* sub-proofs *)
wenzelm@17359
   850
wenzelm@17359
   851
fun proof opt_text =
wenzelm@17359
   852
  assert_backward
wenzelm@60618
   853
  #> refine (the_default Method.standard_text opt_text)
wenzelm@60551
   854
  #> Seq.map
wenzelm@60551
   855
    (using_facts []
wenzelm@60551
   856
      #> enter_forward
wenzelm@60551
   857
      #> open_block
wenzelm@60551
   858
      #> reset_goal);
wenzelm@17359
   859
wenzelm@49866
   860
fun proof_results arg =
wenzelm@49866
   861
  Seq.APPEND (proof (Method.text arg) #> Seq.make_results,
wenzelm@49866
   862
    method_error "initial" (Method.position arg));
wenzelm@49863
   863
wenzelm@49889
   864
fun end_proof bot (prev_pos, (opt_text, immed)) =
wenzelm@49889
   865
  let
wenzelm@49889
   866
    val (finish_text, terminal_pos, finished_pos) =
wenzelm@49889
   867
      (case opt_text of
wenzelm@49889
   868
        NONE => (Method.finish_text (NONE, immed), Position.none, prev_pos)
wenzelm@55709
   869
      | SOME (text, (pos, end_pos)) => (Method.finish_text (SOME text, immed), pos, end_pos));
wenzelm@49889
   870
  in
wenzelm@49889
   871
    Seq.APPEND (fn state =>
wenzelm@49889
   872
      state
wenzelm@49889
   873
      |> assert_forward
wenzelm@49889
   874
      |> assert_bottom bot
wenzelm@49889
   875
      |> close_block
wenzelm@49889
   876
      |> assert_current_goal true
wenzelm@49889
   877
      |> using_facts []
wenzelm@49889
   878
      |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@49889
   879
      |> Seq.maps (refine finish_text)
wenzelm@49889
   880
      |> Seq.make_results, method_error "terminal" terminal_pos)
wenzelm@49889
   881
    #> Seq.maps_results (Seq.single o finished_goal finished_pos)
wenzelm@49889
   882
  end;
wenzelm@17359
   883
wenzelm@29383
   884
fun check_result msg sq =
wenzelm@29383
   885
  (case Seq.pull sq of
wenzelm@29383
   886
    NONE => error msg
wenzelm@29383
   887
  | SOME (s, _) => s);
wenzelm@29383
   888
wenzelm@17359
   889
wenzelm@17359
   890
(* unstructured refinement *)
wenzelm@17359
   891
wenzelm@49865
   892
fun defer i =
wenzelm@49865
   893
  assert_no_chain #>
wenzelm@49865
   894
  refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i))) #> Seq.hd;
wenzelm@49865
   895
wenzelm@49865
   896
fun prefer i =
wenzelm@49865
   897
  assert_no_chain #>
wenzelm@49865
   898
  refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i))) #> Seq.hd;
wenzelm@17359
   899
wenzelm@17359
   900
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@49866
   901
wenzelm@17359
   902
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   903
wenzelm@55709
   904
fun apply_results (text, (pos, _)) =
wenzelm@55709
   905
  Seq.APPEND (apply text #> Seq.make_results, method_error "" pos);
wenzelm@49866
   906
wenzelm@55709
   907
fun apply_end_results (text, (pos, _)) =
wenzelm@55709
   908
  Seq.APPEND (apply_end text #> Seq.make_results, method_error "" pos);
wenzelm@49863
   909
wenzelm@17359
   910
wenzelm@17359
   911
wenzelm@17112
   912
(** goals **)
wenzelm@17112
   913
wenzelm@17359
   914
(* generic goals *)
wenzelm@17359
   915
wenzelm@19774
   916
local
wenzelm@19774
   917
wenzelm@36322
   918
val is_var =
wenzelm@36322
   919
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   920
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   921
wenzelm@36322
   922
fun implicit_vars props =
wenzelm@19846
   923
  let
wenzelm@36354
   924
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   925
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   926
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   927
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   928
wenzelm@19774
   929
fun refine_terms n =
wenzelm@59953
   930
  refine (Method.Basic (fn ctxt => EMPTY_CASES o
wenzelm@58002
   931
    K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@59498
   932
      (HEADGOAL (CONJUNCTS (ALLGOALS (resolve_tac ctxt [Drule.termI]))))))))
wenzelm@19774
   933
  #> Seq.hd;
wenzelm@19774
   934
wenzelm@19774
   935
in
wenzelm@17359
   936
wenzelm@60415
   937
fun generic_goal kind before_qed after_qed goal_ctxt goal_propss result_binds state =
wenzelm@5820
   938
  let
wenzelm@17359
   939
    val chaining = can assert_chain state;
wenzelm@25958
   940
    val pos = Position.thread_data ();
wenzelm@17359
   941
wenzelm@60415
   942
    val goal_props = flat goal_propss;
wenzelm@60415
   943
    val vars = implicit_vars goal_props;
wenzelm@60415
   944
    val propss' = vars :: goal_propss;
wenzelm@23418
   945
    val goal_propss = filter_out null propss';
wenzelm@60551
   946
wenzelm@29346
   947
    val goal =
wenzelm@59616
   948
      Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss)
wenzelm@60406
   949
      |> Thm.cterm_of goal_ctxt
wenzelm@60383
   950
      |> Thm.weaken_sorts (Variable.sorts_of goal_ctxt);
wenzelm@60415
   951
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@60402
   952
wenzelm@60383
   953
    val after_qed' = after_qed |>> (fn after_local => fn results =>
wenzelm@60408
   954
      map_context (fold Variable.maybe_bind_term result_binds) #> after_local results);
wenzelm@5820
   955
  in
wenzelm@60551
   956
    state
wenzelm@60551
   957
    |> assert_forward_or_chain
wenzelm@60551
   958
    |> enter_forward
wenzelm@60551
   959
    |> open_block
wenzelm@60551
   960
    |> enter_backward
wenzelm@60551
   961
    |> map_context
wenzelm@60551
   962
      (K goal_ctxt
wenzelm@60551
   963
        #> init_context
wenzelm@60551
   964
        #> Variable.set_body true
wenzelm@60551
   965
        #> Proof_Context.auto_bind_goal goal_props)
wenzelm@58892
   966
    |> set_goal (make_goal (statement, [], Goal.init goal, before_qed, after_qed'))
wenzelm@21565
   967
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@47068
   968
    |> reset_facts
wenzelm@23418
   969
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@60415
   970
    |> null goal_props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   971
  end;
wenzelm@5820
   972
wenzelm@60408
   973
fun generic_qed state =
wenzelm@60409
   974
  let
wenzelm@60415
   975
    val (goal_ctxt, {statement = (_, propss, _), goal, after_qed, ...}) =
wenzelm@60415
   976
      current_goal state;
wenzelm@60415
   977
    val results = tl (conclude_goal goal_ctxt goal propss);
wenzelm@60415
   978
  in state |> close_block |> pair (after_qed, (goal_ctxt, results)) end;
wenzelm@17359
   979
wenzelm@19774
   980
end;
wenzelm@19774
   981
wenzelm@9469
   982
wenzelm@17359
   983
(* local goals *)
wenzelm@17359
   984
wenzelm@60408
   985
local
wenzelm@60408
   986
wenzelm@60408
   987
fun export_binds ctxt' ctxt binds =
wenzelm@60408
   988
  let
wenzelm@60408
   989
    val rhss =
wenzelm@60408
   990
      map (the_list o snd) binds
wenzelm@60408
   991
      |> burrow (Variable.export_terms ctxt' ctxt)
wenzelm@60408
   992
      |> map (try the_single);
wenzelm@60408
   993
  in map fst binds ~~ rhss end;
wenzelm@60408
   994
wenzelm@60408
   995
in
wenzelm@60408
   996
wenzelm@60555
   997
fun local_goal print_results prep_att prep_propp prep_var strict_asm
wenzelm@60414
   998
    kind before_qed after_qed raw_fixes raw_assumes raw_shows state =
wenzelm@17359
   999
  let
wenzelm@60415
  1000
    val ctxt = context_of state;
wenzelm@60415
  1001
wenzelm@60555
  1002
    val add_assumes =
wenzelm@60555
  1003
      Assumption.add_assms
wenzelm@60555
  1004
        (if strict_asm then Assumption.assume_export else Assumption.presume_export);
wenzelm@60414
  1005
    val (assumes_bindings, shows_bindings) =
wenzelm@60415
  1006
      apply2 (map (apsnd (map (prep_att ctxt)) o fst)) (raw_assumes, raw_shows);
wenzelm@60414
  1007
    val (assumes_propp, shows_propp) = apply2 (map snd) (raw_assumes, raw_shows);
wenzelm@17359
  1008
wenzelm@60461
  1009
    val (goal_ctxt, goal_propss, result_binds, that_fact) =
wenzelm@60402
  1010
      let
wenzelm@60414
  1011
        (*fixed variables*)
wenzelm@60406
  1012
        val ((xs', vars), fix_ctxt) = ctxt
wenzelm@60414
  1013
          |> fold_map prep_var raw_fixes
wenzelm@60406
  1014
          |-> (fn vars => Proof_Context.add_fixes vars ##>> pair vars);
wenzelm@60406
  1015
wenzelm@60414
  1016
        (*propositions with term bindings*)
wenzelm@60414
  1017
        val (all_propss, binds) = prep_propp fix_ctxt (assumes_propp @ shows_propp);
wenzelm@60414
  1018
        val (assumes_propss, shows_propss) = chop (length assumes_propp) all_propss;
wenzelm@60408
  1019
wenzelm@60414
  1020
        (*params*)
wenzelm@60417
  1021
        val (ps, params_ctxt) = fix_ctxt
wenzelm@60414
  1022
          |> (fold o fold) Variable.declare_term all_propss
wenzelm@60408
  1023
          |> fold Variable.bind_term binds
wenzelm@60406
  1024
          |> fold_map Proof_Context.inferred_param xs';
wenzelm@60406
  1025
        val xs = map (Variable.check_name o #1) vars;
wenzelm@60407
  1026
        val params = xs ~~ map Free ps;
wenzelm@60406
  1027
wenzelm@60417
  1028
        (*prems*)
wenzelm@60461
  1029
        val (that_fact, goal_ctxt) = params_ctxt
wenzelm@60555
  1030
          |> fold_burrow add_assumes ((map o map) (Thm.cterm_of params_ctxt) assumes_propss)
wenzelm@60461
  1031
          |> (fn (premss, ctxt') =>
wenzelm@60461
  1032
              let
wenzelm@60461
  1033
                val prems = Assumption.local_prems_of ctxt' ctxt;
wenzelm@60461
  1034
                val ctxt'' =
wenzelm@60461
  1035
                  ctxt'
wenzelm@60461
  1036
                  |> not (null assumes_propss) ?
wenzelm@60461
  1037
                    (snd o Proof_Context.note_thmss ""
wenzelm@60461
  1038
                      [((Binding.name Auto_Bind.thatN, []), [(prems, [])])])
wenzelm@60461
  1039
                  |> (snd o Proof_Context.note_thmss ""
wenzelm@60461
  1040
                      (assumes_bindings ~~ map (map (fn th => ([th], []))) premss))
wenzelm@60461
  1041
              in (prems, ctxt'') end);
wenzelm@60417
  1042
wenzelm@60414
  1043
        val _ = Variable.warn_extra_tfrees fix_ctxt goal_ctxt;
wenzelm@60414
  1044
wenzelm@60414
  1045
        (*result term bindings*)
wenzelm@60414
  1046
        val shows_props = flat shows_propss;
wenzelm@60408
  1047
        val binds' =
wenzelm@60414
  1048
          (case try List.last shows_props of
wenzelm@60408
  1049
            NONE => []
wenzelm@60408
  1050
          | SOME prop => map (apsnd (SOME o Auto_Bind.abs_params prop)) binds);
wenzelm@60408
  1051
        val result_binds =
wenzelm@60414
  1052
          (Auto_Bind.facts goal_ctxt shows_props @ binds')
wenzelm@60408
  1053
          |> (map o apsnd o Option.map) (fold_rev Term.dependent_lambda_name params)
wenzelm@60414
  1054
          |> export_binds goal_ctxt ctxt;
wenzelm@60461
  1055
      in (goal_ctxt, shows_propss, result_binds, that_fact) end;
wenzelm@60402
  1056
wenzelm@60415
  1057
    fun after_qed' (result_ctxt, results) state' = state'
wenzelm@60415
  1058
      |> local_results (shows_bindings ~~ burrow (Proof_Context.export result_ctxt ctxt) results)
wenzelm@60415
  1059
      |-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@60415
  1060
      |> after_qed (result_ctxt, results);
wenzelm@60461
  1061
  in
wenzelm@60461
  1062
    state
wenzelm@60461
  1063
    |> generic_goal kind before_qed (after_qed', K I) goal_ctxt goal_propss result_binds
wenzelm@60461
  1064
    |> pair that_fact
wenzelm@60461
  1065
  end;
wenzelm@5820
  1066
wenzelm@60408
  1067
end;
wenzelm@60408
  1068
wenzelm@49866
  1069
fun local_qeds arg =
wenzelm@49866
  1070
  end_proof false arg
wenzelm@60408
  1071
  #> Seq.map_result (generic_qed #-> (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
  1072
wenzelm@49889
  1073
fun local_qed arg =
wenzelm@49889
  1074
  local_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@29383
  1075
wenzelm@5820
  1076
wenzelm@17359
  1077
(* global goals *)
wenzelm@17359
  1078
wenzelm@60415
  1079
fun global_goal prep_propp before_qed after_qed propp ctxt =
wenzelm@60402
  1080
  let
wenzelm@60415
  1081
    val (propss, binds) =
wenzelm@60415
  1082
      prep_propp (Proof_Context.set_mode Proof_Context.mode_schematic ctxt) propp;
wenzelm@60415
  1083
    val goal_ctxt = ctxt
wenzelm@60415
  1084
      |> (fold o fold) Variable.auto_fixes propss
wenzelm@60415
  1085
      |> fold Variable.bind_term binds;
wenzelm@60415
  1086
    fun after_qed' (result_ctxt, results) ctxt' =
wenzelm@60415
  1087
      after_qed (burrow (Proof_Context.export result_ctxt ctxt') results) ctxt';
wenzelm@60415
  1088
  in
wenzelm@60415
  1089
    ctxt
wenzelm@60415
  1090
    |> init
wenzelm@60415
  1091
    |> generic_goal "" before_qed (K I, after_qed') goal_ctxt propss []
wenzelm@60415
  1092
  end;
wenzelm@45327
  1093
wenzelm@60388
  1094
val theorem = global_goal Proof_Context.cert_propp;
wenzelm@60388
  1095
val theorem_cmd = global_goal Proof_Context.read_propp;
wenzelm@12065
  1096
wenzelm@49866
  1097
fun global_qeds arg =
wenzelm@49866
  1098
  end_proof true arg
wenzelm@60408
  1099
  #> Seq.map_result (generic_qed #> (fn (((_, after_qed), results), state) =>
wenzelm@49859
  1100
    after_qed results (context_of state)));
wenzelm@17112
  1101
wenzelm@49889
  1102
fun global_qed arg =
wenzelm@49889
  1103
  global_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@12959
  1104
wenzelm@5820
  1105
wenzelm@49907
  1106
(* terminal proof steps *)
wenzelm@17112
  1107
wenzelm@49890
  1108
local
wenzelm@49890
  1109
wenzelm@49846
  1110
fun terminal_proof qeds initial terminal =
wenzelm@49889
  1111
  proof_results (SOME initial) #> Seq.maps_results (qeds (#2 (#2 initial), terminal))
wenzelm@49863
  1112
  #> Seq.the_result "";
wenzelm@49848
  1113
wenzelm@49890
  1114
in
wenzelm@49890
  1115
wenzelm@29383
  1116
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@60618
  1117
val local_default_proof = local_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@49889
  1118
val local_immediate_proof = local_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1119
val local_done_proof = terminal_proof local_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@17112
  1120
wenzelm@29383
  1121
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@60618
  1122
val global_default_proof = global_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@49889
  1123
val global_immediate_proof = global_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1124
val global_done_proof = terminal_proof global_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@5820
  1125
wenzelm@49890
  1126
end;
wenzelm@49890
  1127
wenzelm@6896
  1128
wenzelm@49907
  1129
(* skip proofs *)
wenzelm@49907
  1130
wenzelm@49907
  1131
fun local_skip_proof int state =
wenzelm@49907
  1132
  local_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1133
  Skip_Proof.report (context_of state);
wenzelm@49907
  1134
wenzelm@49907
  1135
fun global_skip_proof int state =
wenzelm@49907
  1136
  global_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1137
  Skip_Proof.report (context_of state);
wenzelm@49907
  1138
wenzelm@49907
  1139
wenzelm@17359
  1140
(* common goal statements *)
wenzelm@17359
  1141
wenzelm@60406
  1142
fun internal_goal print_results mode =
wenzelm@60406
  1143
  local_goal print_results (K I) (Proof_Context.cert_propp o Proof_Context.set_mode mode)
wenzelm@60406
  1144
    Proof_Context.cert_var;
wenzelm@60406
  1145
wenzelm@17359
  1146
local
wenzelm@17359
  1147
wenzelm@60406
  1148
fun gen_have prep_att prep_propp prep_var
wenzelm@60555
  1149
    strict_asm before_qed after_qed fixes assumes shows int =
wenzelm@56932
  1150
  local_goal (Proof_Display.print_results int (Position.thread_data ()))
wenzelm@60555
  1151
    prep_att prep_propp prep_var strict_asm "have" before_qed after_qed fixes assumes shows;
wenzelm@6896
  1152
wenzelm@60406
  1153
fun gen_show prep_att prep_propp prep_var
wenzelm@60555
  1154
    strict_asm before_qed after_qed fixes assumes shows int state =
wenzelm@17359
  1155
  let
wenzelm@32738
  1156
    val testing = Unsynchronized.ref false;
wenzelm@32738
  1157
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
  1158
    fun fail_msg ctxt =
wenzelm@50315
  1159
      "Local statement fails to refine any pending goal" ::
wenzelm@33389
  1160
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
  1161
      |> cat_lines;
wenzelm@6896
  1162
wenzelm@56932
  1163
    val pos = Position.thread_data ();
wenzelm@17359
  1164
    fun print_results ctxt res =
wenzelm@46728
  1165
      if ! testing then ()
wenzelm@56932
  1166
      else Proof_Display.print_results int pos ctxt res;
wenzelm@17359
  1167
    fun print_rule ctxt th =
wenzelm@17359
  1168
      if ! testing then rule := SOME th
wenzelm@41181
  1169
      else if int then
wenzelm@56933
  1170
        Proof_Display.string_of_rule ctxt "Successful" th
wenzelm@56933
  1171
        |> Markup.markup Markup.text_fold
wenzelm@59184
  1172
        |> Output.state
wenzelm@17359
  1173
      else ();
wenzelm@17359
  1174
    val test_proof =
wenzelm@50315
  1175
      local_skip_proof true
wenzelm@39616
  1176
      |> Unsynchronized.setmp testing true
wenzelm@42042
  1177
      |> Exn.interruptible_capture;
wenzelm@6896
  1178
wenzelm@60415
  1179
    fun after_qed' (result_ctxt, results) state' = state'
wenzelm@60415
  1180
      |> refine_goals print_rule result_ctxt (flat results)
wenzelm@60415
  1181
      |> check_result "Failed to refine any pending goal"
wenzelm@60415
  1182
      |> after_qed (result_ctxt, results);
wenzelm@17359
  1183
  in
wenzelm@17359
  1184
    state
wenzelm@60555
  1185
    |> local_goal print_results prep_att prep_propp prep_var strict_asm
wenzelm@60414
  1186
      "show" before_qed after_qed' fixes assumes shows
wenzelm@60461
  1187
    ||> int ? (fn goal_state =>
wenzelm@49907
  1188
      (case test_proof (map_context (Context_Position.set_visible false) goal_state) of
wenzelm@50315
  1189
        Exn.Res _ => goal_state
wenzelm@42042
  1190
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1191
  end;
wenzelm@17359
  1192
wenzelm@17359
  1193
in
wenzelm@17359
  1194
wenzelm@60406
  1195
val have = gen_have (K I) Proof_Context.cert_propp Proof_Context.cert_var;
wenzelm@60406
  1196
val have_cmd = gen_have Attrib.attribute_cmd Proof_Context.read_propp Proof_Context.read_var;
wenzelm@60406
  1197
val show = gen_show (K I) Proof_Context.cert_propp Proof_Context.cert_var;
wenzelm@60406
  1198
val show_cmd = gen_show Attrib.attribute_cmd Proof_Context.read_propp Proof_Context.read_var;
wenzelm@6896
  1199
wenzelm@5820
  1200
end;
wenzelm@17359
  1201
wenzelm@28410
  1202
wenzelm@29385
  1203
wenzelm@29385
  1204
(** future proofs **)
wenzelm@29385
  1205
wenzelm@29385
  1206
(* relevant proof states *)
wenzelm@29385
  1207
wenzelm@29385
  1208
fun schematic_goal state =
wenzelm@60611
  1209
  let val (_, {statement = (_, _, prop), ...}) = find_goal state
wenzelm@51553
  1210
  in Goal.is_schematic prop end;
wenzelm@29385
  1211
wenzelm@29385
  1212
fun is_relevant state =
wenzelm@29385
  1213
  (case try find_goal state of
wenzelm@29385
  1214
    NONE => true
wenzelm@60611
  1215
  | SOME (_, {statement = (_, _, prop), goal, ...}) =>
wenzelm@51553
  1216
      Goal.is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1217
wenzelm@29385
  1218
wenzelm@29385
  1219
(* full proofs *)
wenzelm@28410
  1220
wenzelm@29346
  1221
local
wenzelm@29346
  1222
wenzelm@49912
  1223
structure Result = Proof_Data
wenzelm@49912
  1224
(
wenzelm@49912
  1225
  type T = thm option;
wenzelm@59150
  1226
  fun init _ = NONE;
wenzelm@49912
  1227
);
wenzelm@49912
  1228
wenzelm@49912
  1229
fun the_result ctxt =
wenzelm@49912
  1230
  (case Result.get ctxt of
wenzelm@49912
  1231
    NONE => error "No result of forked proof"
wenzelm@49912
  1232
  | SOME th => th);
wenzelm@49912
  1233
wenzelm@49912
  1234
val set_result = Result.put o SOME;
wenzelm@49912
  1235
val reset_result = Result.put NONE;
wenzelm@49912
  1236
wenzelm@51318
  1237
in
wenzelm@51318
  1238
wenzelm@51318
  1239
fun future_proof fork_proof state =
wenzelm@28410
  1240
  let
wenzelm@29381
  1241
    val _ = assert_backward state;
wenzelm@60611
  1242
    val (goal_ctxt, goal_info) = find_goal state;
wenzelm@60415
  1243
    val {statement as (kind, _, prop), using, goal, before_qed, after_qed} = goal_info;
wenzelm@28410
  1244
wenzelm@29383
  1245
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1246
wenzelm@60415
  1247
    val after_qed' =
wenzelm@60415
  1248
      (fn (_, [[th]]) => map_context (set_result th),
wenzelm@60415
  1249
       fn (_, [[th]]) => set_result th);
wenzelm@28973
  1250
    val result_ctxt =
wenzelm@28973
  1251
      state
wenzelm@49912
  1252
      |> map_context reset_result
wenzelm@60611
  1253
      |> map_goal (K (goal_ctxt, (kind, [[], [prop]], prop), using, goal, before_qed, after_qed'))
wenzelm@29346
  1254
      |> fork_proof;
wenzelm@28973
  1255
wenzelm@51318
  1256
    val future_thm = Future.map (the_result o snd) result_ctxt;
wenzelm@60411
  1257
    val finished_goal = Goal.protect 0 (Goal.future_result goal_ctxt future_thm prop);
wenzelm@28973
  1258
    val state' =
wenzelm@28973
  1259
      state
wenzelm@60611
  1260
      |> map_goal (K (goal_ctxt, statement, using, finished_goal, NONE, after_qed));
wenzelm@51318
  1261
  in (Future.map fst result_ctxt, state') end;
wenzelm@29385
  1262
wenzelm@17359
  1263
end;
wenzelm@29346
  1264
wenzelm@29385
  1265
wenzelm@51662
  1266
(* terminal proofs *)  (* FIXME avoid toplevel imitation -- include in PIDE/document *)
wenzelm@29385
  1267
wenzelm@29385
  1268
local
wenzelm@29385
  1269
wenzelm@60403
  1270
fun future_terminal_proof proof1 proof2 done state =
wenzelm@53189
  1271
  if Goal.future_enabled 3 andalso not (is_relevant state) then
wenzelm@51318
  1272
    state |> future_proof (fn state' =>
wenzelm@51662
  1273
      let
wenzelm@51662
  1274
        val pos = Position.thread_data ();
wenzelm@51662
  1275
        val props = Markup.command_timing :: (Markup.nameN, "by") :: Position.properties_of pos;
wenzelm@51662
  1276
      in
wenzelm@53192
  1277
        Execution.fork {name = "Proof.future_terminal_proof", pos = pos, pri = ~1}
wenzelm@51662
  1278
          (fn () => ((), Timing.protocol props proof2 state'))
wenzelm@51662
  1279
      end) |> snd |> done
wenzelm@51318
  1280
  else proof1 state;
wenzelm@29385
  1281
wenzelm@29385
  1282
in
wenzelm@29385
  1283
wenzelm@51318
  1284
fun local_future_terminal_proof meths =
wenzelm@51605
  1285
  future_terminal_proof
wenzelm@51318
  1286
    (local_terminal_proof meths)
wenzelm@51318
  1287
    (local_terminal_proof meths #> context_of) local_done_proof;
wenzelm@29385
  1288
wenzelm@51318
  1289
fun global_future_terminal_proof meths =
wenzelm@51605
  1290
  future_terminal_proof
wenzelm@51318
  1291
    (global_terminal_proof meths)
wenzelm@51318
  1292
    (global_terminal_proof meths) global_done_proof;
wenzelm@29350
  1293
wenzelm@29346
  1294
end;
wenzelm@29346
  1295
wenzelm@29385
  1296
end;