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