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