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