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