src/Pure/Isar/proof.ML
author wenzelm
Fri Jul 22 11:01:10 2016 +0200 (2016-07-22)
changeset 63541 e308f15cc8a7
parent 63475 31016a88197b
child 64420 2efc128370fa
permissions -rw-r--r--
more markup for unstructured calculation;
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@60948
   518
      Context.subthy_id (Thm.theory_id_of_thm 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@60723
   525
      (Goal.conclude (if length (flat propss) > 1 then Thm.close_derivation goal else goal)
wenzelm@60550
   526
        handle THM _ => err_lost ())
wenzelm@58950
   527
      |> Drule.flexflex_unique (SOME ctxt)
wenzelm@61508
   528
      |> Thm.check_shyps ctxt
wenzelm@54993
   529
      |> Thm.check_hyps (Context.Proof ctxt);
wenzelm@23418
   530
wenzelm@23418
   531
    val goal_propss = filter_out null propss;
wenzelm@23418
   532
    val results =
wenzelm@23418
   533
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   534
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@60550
   535
      handle THM _ => err_lost ();
wenzelm@58950
   536
    val _ =
wenzelm@58950
   537
      Unify.matches_list (Context.Proof ctxt) (flat goal_propss) (map Thm.prop_of (flat results))
wenzelm@61268
   538
        orelse error ("Proved a different theorem:\n" ^ Thm.string_of_thm ctxt th);
wenzelm@23418
   539
wenzelm@23418
   540
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   541
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   542
      | recover_result [] [] = []
wenzelm@60550
   543
      | recover_result _ _ = err_lost ();
wenzelm@23418
   544
  in recover_result propss results end;
wenzelm@5820
   545
wenzelm@49859
   546
val finished_goal_error = "Failed to finish proof";
wenzelm@49859
   547
wenzelm@49889
   548
fun finished_goal pos state =
wenzelm@49859
   549
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@49859
   550
    if Thm.no_prems goal then Seq.Result state
wenzelm@49889
   551
    else
wenzelm@49889
   552
      Seq.Error (fn () =>
wenzelm@49889
   553
        finished_goal_error ^ Position.here pos ^ ":\n" ^
wenzelm@49889
   554
          Proof_Display.string_of_goal ctxt goal)
wenzelm@49859
   555
  end;
wenzelm@49846
   556
wenzelm@5820
   557
wenzelm@33288
   558
(* goal views -- corresponding to methods *)
wenzelm@33288
   559
wenzelm@33288
   560
fun raw_goal state =
wenzelm@63249
   561
  let val (ctxt, (using, goal)) = get_goal state
wenzelm@63249
   562
  in {context = ctxt, facts = using, goal = goal} end;
wenzelm@33288
   563
wenzelm@33288
   564
val goal = raw_goal o refine_insert [];
wenzelm@33288
   565
wenzelm@33288
   566
fun simple_goal state =
wenzelm@33288
   567
  let
wenzelm@63249
   568
    val (_, (using, _)) = get_goal state;
wenzelm@63249
   569
    val (ctxt, (_, goal)) = get_goal (refine_insert using state);
wenzelm@33288
   570
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   571
wenzelm@49866
   572
fun method_error kind pos state =
wenzelm@49866
   573
  Seq.single (Proof_Display.method_error kind pos (raw_goal state));
wenzelm@49861
   574
wenzelm@33288
   575
wenzelm@5820
   576
wenzelm@5820
   577
(*** structured proof commands ***)
wenzelm@5820
   578
wenzelm@17112
   579
(** context elements **)
wenzelm@5820
   580
wenzelm@36324
   581
(* let bindings *)
wenzelm@5820
   582
wenzelm@16813
   583
local
wenzelm@16813
   584
wenzelm@17359
   585
fun gen_bind bind args state =
wenzelm@5820
   586
  state
wenzelm@5820
   587
  |> assert_forward
wenzelm@36324
   588
  |> map_context (bind true args #> snd)
wenzelm@47068
   589
  |> reset_facts;
wenzelm@5820
   590
wenzelm@16813
   591
in
wenzelm@16813
   592
wenzelm@60377
   593
val let_bind = gen_bind Proof_Context.match_bind;
wenzelm@60377
   594
val let_bind_cmd = gen_bind Proof_Context.match_bind_cmd;
wenzelm@5820
   595
wenzelm@16813
   596
end;
wenzelm@16813
   597
wenzelm@5820
   598
wenzelm@36505
   599
(* concrete syntax *)
wenzelm@36505
   600
wenzelm@36505
   601
local
wenzelm@36505
   602
wenzelm@62771
   603
fun read_arg (c, mx) ctxt =
wenzelm@62771
   604
  (case Proof_Context.read_const {proper = false, strict = false} ctxt c of
wenzelm@62771
   605
    Free (x, _) =>
wenzelm@62771
   606
      let
wenzelm@62771
   607
        val ctxt' =
wenzelm@62771
   608
          ctxt |> is_none (Variable.default_type ctxt x) ?
wenzelm@62959
   609
            Variable.declare_constraints (Free (x, Mixfix.default_constraint mx));
wenzelm@62771
   610
        val t = Free (#1 (Proof_Context.inferred_param x ctxt'));
wenzelm@62771
   611
      in ((t, mx), ctxt') end
wenzelm@62771
   612
  | t => ((t, mx), ctxt));
wenzelm@62771
   613
wenzelm@36507
   614
fun gen_write prep_arg mode args =
wenzelm@36507
   615
  assert_forward
wenzelm@62771
   616
  #> map_context (fold_map prep_arg args #-> Proof_Context.notation true mode)
wenzelm@47068
   617
  #> reset_facts;
wenzelm@36505
   618
wenzelm@36505
   619
in
wenzelm@36505
   620
wenzelm@62771
   621
val write = gen_write pair;
wenzelm@55955
   622
val write_cmd = gen_write read_arg;
wenzelm@36505
   623
wenzelm@36505
   624
end;
wenzelm@36505
   625
wenzelm@36505
   626
wenzelm@17359
   627
(* fix *)
wenzelm@9196
   628
wenzelm@12714
   629
local
wenzelm@12714
   630
wenzelm@60469
   631
fun gen_fix add_fixes raw_fixes =
wenzelm@17359
   632
  assert_forward
wenzelm@60469
   633
  #> map_context (snd o add_fixes raw_fixes)
wenzelm@47068
   634
  #> reset_facts;
wenzelm@5820
   635
wenzelm@16813
   636
in
wenzelm@16813
   637
wenzelm@60469
   638
val fix = gen_fix Proof_Context.add_fixes;
wenzelm@60469
   639
val fix_cmd = gen_fix Proof_Context.add_fixes_cmd;
wenzelm@5820
   640
wenzelm@16813
   641
end;
wenzelm@16813
   642
wenzelm@5820
   643
wenzelm@61654
   644
(* assume *)
wenzelm@5820
   645
wenzelm@16813
   646
local
wenzelm@16813
   647
wenzelm@63057
   648
fun gen_assume prep_statement prep_att export raw_fixes raw_prems raw_concls state =
wenzelm@61654
   649
  let
wenzelm@61654
   650
    val ctxt = context_of state;
wenzelm@61654
   651
wenzelm@61654
   652
    val bindings = map (apsnd (map (prep_att ctxt)) o fst) raw_concls;
wenzelm@63056
   653
    val ((params, prems_propss, concl_propss, result_binds), _) =
wenzelm@63057
   654
      prep_statement raw_fixes raw_prems (map snd raw_concls) ctxt;
wenzelm@63056
   655
    val propss = (map o map) (Logic.close_prop params (flat prems_propss)) concl_propss;
wenzelm@61654
   656
  in
wenzelm@61654
   657
    state
wenzelm@61654
   658
    |> assert_forward
wenzelm@61654
   659
    |> map_context_result (fn ctxt =>
wenzelm@61654
   660
      ctxt
wenzelm@61654
   661
      |> (fold o fold) Variable.declare_term propss
wenzelm@61654
   662
      |> fold Variable.maybe_bind_term result_binds
wenzelm@61654
   663
      |> fold_burrow (Assumption.add_assms export o map (Thm.cterm_of ctxt)) propss
wenzelm@61654
   664
      |-> (fn premss =>
wenzelm@61654
   665
        Proof_Context.note_thmss "" (bindings ~~ (map o map) (fn th => ([th], [])) premss)))
wenzelm@61654
   666
    |> these_factss [] |> #2
wenzelm@61654
   667
  end;
wenzelm@6932
   668
wenzelm@16813
   669
in
wenzelm@16813
   670
wenzelm@63057
   671
val assm = gen_assume Proof_Context.cert_statement (K I);
wenzelm@63057
   672
val assm_cmd = gen_assume Proof_Context.read_statement Attrib.attribute_cmd;
wenzelm@61654
   673
wenzelm@36323
   674
val assume = assm Assumption.assume_export;
wenzelm@36323
   675
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@61654
   676
wenzelm@36323
   677
val presume = assm Assumption.presume_export;
wenzelm@36323
   678
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   679
wenzelm@16813
   680
end;
wenzelm@16813
   681
wenzelm@7271
   682
wenzelm@17359
   683
(* def *)
wenzelm@11891
   684
wenzelm@16813
   685
local
wenzelm@16813
   686
wenzelm@60379
   687
fun gen_def prep_att prep_var prep_binds args state =
wenzelm@11891
   688
  let
wenzelm@11891
   689
    val _ = assert_forward state;
wenzelm@20913
   690
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@47815
   691
    val name_atts = map (apsnd (map (prep_att (context_of state)))) raw_name_atts;
wenzelm@11891
   692
  in
wenzelm@20913
   693
    state
wenzelm@60379
   694
    |> map_context_result (fold_map (fn (x, mx) => prep_var (x, NONE, mx)) raw_vars)
wenzelm@20913
   695
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   696
    |-> (fn vars =>
wenzelm@20913
   697
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@49748
   698
      #-> (fn rhss =>
wenzelm@49748
   699
        let
wenzelm@49748
   700
          val defs = (vars ~~ (name_atts ~~ rhss)) |> map (fn ((x, mx), ((a, atts), rhs)) =>
wenzelm@63041
   701
            ((x, mx), ((Thm.def_binding_optional x a, atts), rhs)));
wenzelm@63344
   702
        in map_context_result (Local_Defs.define defs) end))
wenzelm@47068
   703
    |-> (set_facts o map (#2 o #2))
wenzelm@11891
   704
  end;
wenzelm@11891
   705
wenzelm@16813
   706
in
wenzelm@16813
   707
wenzelm@60379
   708
val def = gen_def (K I) Proof_Context.cert_var Proof_Context.match_bind;
wenzelm@60379
   709
val def_cmd = gen_def Attrib.attribute_cmd Proof_Context.read_var Proof_Context.match_bind_cmd;
wenzelm@11891
   710
wenzelm@16813
   711
end;
wenzelm@16813
   712
wenzelm@11891
   713
wenzelm@8374
   714
wenzelm@17112
   715
(** facts **)
wenzelm@5820
   716
wenzelm@17359
   717
(* chain *)
wenzelm@5820
   718
wenzelm@17359
   719
val chain =
wenzelm@17359
   720
  assert_forward
wenzelm@63251
   721
  #> (fn state => set_facts (Method.clean_facts (the_facts state)) state)
wenzelm@17359
   722
  #> enter_chain;
wenzelm@5820
   723
wenzelm@17359
   724
fun chain_facts facts =
wenzelm@47068
   725
  set_facts facts
wenzelm@17359
   726
  #> chain;
wenzelm@5820
   727
wenzelm@5820
   728
wenzelm@17359
   729
(* note etc. *)
wenzelm@17112
   730
wenzelm@63352
   731
fun empty_bindings args = map (pair Binding.empty_atts) args;
wenzelm@17112
   732
wenzelm@17112
   733
local
wenzelm@17112
   734
wenzelm@30760
   735
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   736
  state
wenzelm@17112
   737
  |> assert_forward
wenzelm@47815
   738
  |> map_context_result (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@47815
   739
    (Attrib.map_facts_refs (map (prep_atts ctxt)) (prep_fact ctxt) args))
wenzelm@17112
   740
  |> these_factss (more_facts state)
wenzelm@17359
   741
  ||> opt_chain
wenzelm@17359
   742
  |> opt_result;
wenzelm@17112
   743
wenzelm@17112
   744
in
wenzelm@17112
   745
wenzelm@36323
   746
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@47815
   747
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   748
wenzelm@63352
   749
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o empty_bindings;
wenzelm@47815
   750
val from_thmss_cmd =
wenzelm@63352
   751
  gen_thmss (K []) chain #2 Attrib.attribute_cmd Proof_Context.get_fact o empty_bindings;
wenzelm@17359
   752
wenzelm@63352
   753
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o empty_bindings;
wenzelm@47815
   754
val with_thmss_cmd =
wenzelm@63352
   755
  gen_thmss the_facts chain #2 Attrib.attribute_cmd Proof_Context.get_fact o empty_bindings;
wenzelm@30760
   756
wenzelm@30760
   757
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   758
wenzelm@17112
   759
end;
wenzelm@17112
   760
wenzelm@17112
   761
wenzelm@60371
   762
(* facts during goal refinement *)
wenzelm@60371
   763
wenzelm@60371
   764
local
wenzelm@60371
   765
wenzelm@60371
   766
fun gen_supply prep_att prep_fact args state =
wenzelm@60371
   767
  state
wenzelm@60371
   768
  |> assert_backward
wenzelm@60371
   769
  |> map_context (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@60371
   770
       (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) args) |> snd);
wenzelm@60371
   771
wenzelm@60371
   772
in
wenzelm@60371
   773
wenzelm@60371
   774
val supply = gen_supply (K I) (K I);
wenzelm@60371
   775
val supply_cmd = gen_supply Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@60371
   776
wenzelm@60371
   777
end;
wenzelm@60371
   778
wenzelm@60371
   779
wenzelm@18548
   780
(* using/unfolding *)
wenzelm@17112
   781
wenzelm@17112
   782
local
wenzelm@17112
   783
wenzelm@45390
   784
fun gen_using f g prep_att prep_fact args state =
wenzelm@17112
   785
  state
wenzelm@17112
   786
  |> assert_backward
wenzelm@21442
   787
  |> map_context_result
wenzelm@47815
   788
    (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@63352
   789
      (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) (empty_bindings args)))
wenzelm@18843
   790
  |> (fn (named_facts, state') =>
wenzelm@60611
   791
    state' |> map_goal (fn (goal_ctxt, statement, using, goal, before_qed, after_qed) =>
wenzelm@18843
   792
      let
wenzelm@18843
   793
        val ctxt = context_of state';
wenzelm@19482
   794
        val ths = maps snd named_facts;
wenzelm@60611
   795
      in (goal_ctxt, statement, f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   796
wenzelm@24050
   797
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   798
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   799
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   800
wenzelm@17112
   801
in
wenzelm@17112
   802
wenzelm@36323
   803
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@47815
   804
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@36323
   805
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@47815
   806
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   807
wenzelm@17112
   808
end;
wenzelm@17112
   809
wenzelm@17112
   810
wenzelm@17112
   811
(* case *)
wenzelm@17112
   812
wenzelm@17112
   813
local
wenzelm@17112
   814
wenzelm@60565
   815
fun gen_case internal prep_att ((raw_binding, raw_atts), ((name, pos), xs)) state =
wenzelm@17112
   816
  let
wenzelm@60565
   817
    val ctxt = context_of state;
wenzelm@60565
   818
wenzelm@60565
   819
    val binding = if Binding.is_empty raw_binding then Binding.make (name, pos) else raw_binding;
wenzelm@60565
   820
    val atts = map (prep_att ctxt) raw_atts;
wenzelm@60565
   821
wenzelm@19078
   822
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@57486
   823
      ctxt |> Proof_Context.apply_case (Proof_Context.check_case ctxt internal (name, pos) xs));
wenzelm@53380
   824
    val assumptions =
wenzelm@63003
   825
      asms |> map (fn (a, ts) => ((Binding.qualify_name true binding a, []), map (rpair []) ts));
wenzelm@17112
   826
  in
wenzelm@17112
   827
    state'
wenzelm@61654
   828
    |> assume [] [] assumptions
wenzelm@60401
   829
    |> map_context (fold Variable.unbind_term Auto_Bind.no_facts)
wenzelm@60565
   830
    |> `the_facts |-> (fn thms => note_thmss [((binding, atts), [(thms, [])])])
wenzelm@17112
   831
  end;
wenzelm@17112
   832
wenzelm@17112
   833
in
wenzelm@17112
   834
wenzelm@60565
   835
val case_ = gen_case true (K I);
wenzelm@60565
   836
val case_cmd = gen_case false Attrib.attribute_cmd;
wenzelm@17112
   837
wenzelm@17112
   838
end;
wenzelm@17112
   839
wenzelm@17112
   840
wenzelm@63039
   841
(* define *)
wenzelm@63039
   842
wenzelm@63039
   843
local
wenzelm@63039
   844
wenzelm@63057
   845
fun gen_define prep_stmt prep_att raw_decls raw_fixes raw_defs state =
wenzelm@63039
   846
  let
wenzelm@63039
   847
    val _ = assert_forward state;
wenzelm@63039
   848
    val ctxt = context_of state;
wenzelm@63039
   849
wenzelm@63039
   850
    (*vars*)
wenzelm@63056
   851
    val ((vars, propss, _, binds'), vars_ctxt) =
wenzelm@63057
   852
      prep_stmt (raw_decls @ raw_fixes) (map snd raw_defs) ctxt;
wenzelm@63056
   853
    val (decls, fixes) = chop (length raw_decls) vars;
wenzelm@63056
   854
    val show_term = Syntax.string_of_term vars_ctxt;
wenzelm@63039
   855
wenzelm@63039
   856
    (*defs*)
wenzelm@63056
   857
    fun match_defs (((b, _, mx), (_, Free (x, T))) :: more_decls) ((((y, U), t), _) :: more_defs) =
wenzelm@63352
   858
          if x = y then ((b, mx), (Binding.empty_atts, t)) :: match_defs more_decls more_defs
wenzelm@63056
   859
          else
wenzelm@63056
   860
            error ("Mismatch of declaration " ^ show_term (Free (x, T)) ^ " wrt. definition " ^
wenzelm@63056
   861
              show_term (Free (y, U)))
wenzelm@63056
   862
      | match_defs [] [] = []
wenzelm@63056
   863
      | match_defs more_decls more_defs =
wenzelm@63056
   864
          error ("Mismatch of declarations " ^ commas (map (show_term o #2 o #2) more_decls) ^
wenzelm@63056
   865
            (if null more_decls then "" else " ") ^
wenzelm@63056
   866
            "wrt. definitions " ^ commas (map (show_term o Free o #1 o #1) more_defs));
wenzelm@63056
   867
wenzelm@63395
   868
    val derived_def = Local_Defs.derived_def ctxt (K  []) {conditional = false};
wenzelm@63056
   869
    val defs1 = map (derived_def o Logic.close_prop (map #2 fixes) []) (flat propss);
wenzelm@63056
   870
    val defs2 = match_defs decls defs1;
wenzelm@63344
   871
    val (defs3, defs_ctxt) = Local_Defs.define defs2 ctxt;
wenzelm@63039
   872
wenzelm@63039
   873
    (*notes*)
wenzelm@63039
   874
    val def_thmss =
wenzelm@63056
   875
      map (fn (((_, prove), ((b, _), _)), (_, (_, th))) => (b, prove defs_ctxt th))
wenzelm@63039
   876
        (defs1 ~~ defs2 ~~ defs3)
wenzelm@63039
   877
      |> unflat (map snd raw_defs);
wenzelm@63039
   878
    val notes =
wenzelm@63039
   879
      map2 (fn ((a, raw_atts), _) => fn def_thms =>
wenzelm@63041
   880
        ((Thm.def_binding_optional (Binding.conglomerate (map #1 def_thms)) a,
wenzelm@63039
   881
          map (prep_att defs_ctxt) raw_atts), map (fn (_, th) => ([th], [])) def_thms))
wenzelm@63039
   882
        raw_defs def_thmss;
wenzelm@63039
   883
  in
wenzelm@63039
   884
    state
wenzelm@63039
   885
    |> map_context (K defs_ctxt #> fold Variable.bind_term binds')
wenzelm@63039
   886
    |> note_thmss notes
wenzelm@63039
   887
  end;
wenzelm@63039
   888
wenzelm@63039
   889
in
wenzelm@63039
   890
wenzelm@63057
   891
val define = gen_define Proof_Context.cert_stmt (K I);
wenzelm@63057
   892
val define_cmd = gen_define Proof_Context.read_stmt Attrib.attribute_cmd;
wenzelm@63039
   893
wenzelm@63039
   894
end;
wenzelm@63039
   895
wenzelm@63039
   896
wenzelm@17112
   897
wenzelm@17359
   898
(** proof structure **)
wenzelm@17359
   899
wenzelm@17359
   900
(* blocks *)
wenzelm@17359
   901
wenzelm@17359
   902
val begin_block =
wenzelm@17359
   903
  assert_forward
wenzelm@17359
   904
  #> open_block
wenzelm@47068
   905
  #> reset_goal
wenzelm@17359
   906
  #> open_block;
wenzelm@17359
   907
wenzelm@17359
   908
val next_block =
wenzelm@17359
   909
  assert_forward
wenzelm@17359
   910
  #> close_block
wenzelm@17359
   911
  #> open_block
wenzelm@47068
   912
  #> reset_goal
wenzelm@47068
   913
  #> reset_facts;
wenzelm@17359
   914
wenzelm@17359
   915
fun end_block state =
wenzelm@17359
   916
  state
wenzelm@17359
   917
  |> assert_forward
wenzelm@40960
   918
  |> assert_bottom false
wenzelm@17359
   919
  |> close_block
wenzelm@17359
   920
  |> assert_current_goal false
wenzelm@17359
   921
  |> close_block
wenzelm@17359
   922
  |> export_facts state;
wenzelm@17359
   923
wenzelm@17359
   924
wenzelm@40960
   925
(* global notepad *)
wenzelm@40960
   926
wenzelm@40960
   927
val begin_notepad =
wenzelm@40960
   928
  init
wenzelm@40960
   929
  #> open_block
wenzelm@40960
   930
  #> map_context (Variable.set_body true)
wenzelm@40960
   931
  #> open_block;
wenzelm@40960
   932
wenzelm@40960
   933
val end_notepad =
wenzelm@40960
   934
  assert_forward
wenzelm@40960
   935
  #> assert_bottom true
wenzelm@40960
   936
  #> close_block
wenzelm@40960
   937
  #> assert_current_goal false
wenzelm@40960
   938
  #> close_block
wenzelm@40960
   939
  #> context_of;
wenzelm@40960
   940
wenzelm@40960
   941
wenzelm@17359
   942
(* sub-proofs *)
wenzelm@17359
   943
wenzelm@17359
   944
fun proof opt_text =
wenzelm@61841
   945
  Seq.APPEND
wenzelm@61841
   946
    (assert_backward
wenzelm@61841
   947
      #> refine (the_default Method.standard_text (Method.text opt_text))
wenzelm@61841
   948
      #> Seq.map_result
wenzelm@61841
   949
        (using_facts []
wenzelm@61841
   950
          #> enter_forward
wenzelm@61841
   951
          #> open_block
wenzelm@61841
   952
          #> reset_goal),
wenzelm@61841
   953
     method_error "initial" (Method.position opt_text));
wenzelm@49863
   954
wenzelm@49889
   955
fun end_proof bot (prev_pos, (opt_text, immed)) =
wenzelm@49889
   956
  let
wenzelm@49889
   957
    val (finish_text, terminal_pos, finished_pos) =
wenzelm@49889
   958
      (case opt_text of
wenzelm@49889
   959
        NONE => (Method.finish_text (NONE, immed), Position.none, prev_pos)
wenzelm@55709
   960
      | SOME (text, (pos, end_pos)) => (Method.finish_text (SOME text, immed), pos, end_pos));
wenzelm@49889
   961
  in
wenzelm@49889
   962
    Seq.APPEND (fn state =>
wenzelm@49889
   963
      state
wenzelm@49889
   964
      |> assert_forward
wenzelm@49889
   965
      |> assert_bottom bot
wenzelm@49889
   966
      |> close_block
wenzelm@49889
   967
      |> assert_current_goal true
wenzelm@49889
   968
      |> using_facts []
wenzelm@49889
   969
      |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@61841
   970
      |> Seq.maps_results (refine finish_text),
wenzelm@61841
   971
      method_error "terminal" terminal_pos)
wenzelm@49889
   972
    #> Seq.maps_results (Seq.single o finished_goal finished_pos)
wenzelm@49889
   973
  end;
wenzelm@17359
   974
wenzelm@29383
   975
fun check_result msg sq =
wenzelm@29383
   976
  (case Seq.pull sq of
wenzelm@29383
   977
    NONE => error msg
wenzelm@29383
   978
  | SOME (s, _) => s);
wenzelm@29383
   979
wenzelm@17359
   980
wenzelm@17359
   981
(* unstructured refinement *)
wenzelm@17359
   982
wenzelm@49865
   983
fun defer i =
wenzelm@49865
   984
  assert_no_chain #>
wenzelm@61841
   985
  refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i)));
wenzelm@49865
   986
wenzelm@49865
   987
fun prefer i =
wenzelm@49865
   988
  assert_no_chain #>
wenzelm@61841
   989
  refine_singleton (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i)));
wenzelm@17359
   990
wenzelm@61841
   991
fun apply (text, (pos, _)) =
wenzelm@61841
   992
  Seq.APPEND (assert_backward #> refine text #> Seq.map_result (using_facts []),
wenzelm@61841
   993
    method_error "" pos);
wenzelm@49866
   994
wenzelm@61841
   995
fun apply_end (text, (pos, _)) =
wenzelm@61841
   996
  Seq.APPEND (assert_forward #> refine_end text, method_error "" pos);
wenzelm@49863
   997
wenzelm@17359
   998
wenzelm@17359
   999
wenzelm@17112
  1000
(** goals **)
wenzelm@17112
  1001
wenzelm@17359
  1002
(* generic goals *)
wenzelm@17359
  1003
wenzelm@19774
  1004
local
wenzelm@19774
  1005
wenzelm@36322
  1006
val is_var =
wenzelm@36322
  1007
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
  1008
  can (dest_Var o Logic.dest_term);
wenzelm@36322
  1009
wenzelm@36322
  1010
fun implicit_vars props =
wenzelm@19846
  1011
  let
wenzelm@36354
  1012
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
  1013
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
  1014
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
  1015
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
  1016
wenzelm@19774
  1017
fun refine_terms n =
wenzelm@61841
  1018
  refine_singleton (Method.Basic (fn ctxt => Method.CONTEXT_TACTIC o
wenzelm@58002
  1019
    K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@61841
  1020
      (HEADGOAL (CONJUNCTS (ALLGOALS (resolve_tac ctxt [Drule.termI]))))))));
wenzelm@19774
  1021
wenzelm@19774
  1022
in
wenzelm@17359
  1023
wenzelm@60415
  1024
fun generic_goal kind before_qed after_qed goal_ctxt goal_propss result_binds state =
wenzelm@5820
  1025
  let
wenzelm@17359
  1026
    val chaining = can assert_chain state;
wenzelm@25958
  1027
    val pos = Position.thread_data ();
wenzelm@17359
  1028
wenzelm@60415
  1029
    val goal_props = flat goal_propss;
wenzelm@60415
  1030
    val vars = implicit_vars goal_props;
wenzelm@60415
  1031
    val propss' = vars :: goal_propss;
wenzelm@23418
  1032
    val goal_propss = filter_out null propss';
wenzelm@60551
  1033
wenzelm@29346
  1034
    val goal =
wenzelm@59616
  1035
      Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss)
wenzelm@60406
  1036
      |> Thm.cterm_of goal_ctxt
wenzelm@61508
  1037
      |> Thm.weaken_sorts' goal_ctxt;
wenzelm@60415
  1038
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@60402
  1039
wenzelm@60383
  1040
    val after_qed' = after_qed |>> (fn after_local => fn results =>
wenzelm@60408
  1041
      map_context (fold Variable.maybe_bind_term result_binds) #> after_local results);
wenzelm@5820
  1042
  in
wenzelm@60551
  1043
    state
wenzelm@60551
  1044
    |> assert_forward_or_chain
wenzelm@60551
  1045
    |> enter_forward
wenzelm@60551
  1046
    |> open_block
wenzelm@60551
  1047
    |> enter_backward
wenzelm@60551
  1048
    |> map_context
wenzelm@60551
  1049
      (K goal_ctxt
wenzelm@60551
  1050
        #> init_context
wenzelm@60551
  1051
        #> Variable.set_body true
wenzelm@60551
  1052
        #> Proof_Context.auto_bind_goal goal_props)
wenzelm@58892
  1053
    |> set_goal (make_goal (statement, [], Goal.init goal, before_qed, after_qed'))
wenzelm@21565
  1054
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@47068
  1055
    |> reset_facts
wenzelm@23418
  1056
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@61841
  1057
    |> null goal_props ? refine_singleton (Method.Basic Method.assumption)
wenzelm@5820
  1058
  end;
wenzelm@5820
  1059
wenzelm@60408
  1060
fun generic_qed state =
wenzelm@60409
  1061
  let
wenzelm@60415
  1062
    val (goal_ctxt, {statement = (_, propss, _), goal, after_qed, ...}) =
wenzelm@60415
  1063
      current_goal state;
wenzelm@60415
  1064
    val results = tl (conclude_goal goal_ctxt goal propss);
wenzelm@60415
  1065
  in state |> close_block |> pair (after_qed, (goal_ctxt, results)) end;
wenzelm@17359
  1066
wenzelm@19774
  1067
end;
wenzelm@19774
  1068
wenzelm@9469
  1069
wenzelm@17359
  1070
(* local goals *)
wenzelm@17359
  1071
wenzelm@63057
  1072
fun local_goal print_results prep_statement prep_att strict_asm
wenzelm@60414
  1073
    kind before_qed after_qed raw_fixes raw_assumes raw_shows state =
wenzelm@17359
  1074
  let
wenzelm@60415
  1075
    val ctxt = context_of state;
wenzelm@60415
  1076
wenzelm@60555
  1077
    val add_assumes =
wenzelm@60555
  1078
      Assumption.add_assms
wenzelm@60555
  1079
        (if strict_asm then Assumption.assume_export else Assumption.presume_export);
wenzelm@61654
  1080
wenzelm@61659
  1081
    (*params*)
wenzelm@61654
  1082
    val ((params, assumes_propss, shows_propss, result_binds), params_ctxt) = ctxt
wenzelm@63057
  1083
      |> prep_statement raw_fixes (map snd raw_assumes) (map snd raw_shows);
wenzelm@60406
  1084
wenzelm@61659
  1085
    (*prems*)
wenzelm@61659
  1086
    val (assumes_bindings, shows_bindings) =
wenzelm@61659
  1087
      apply2 (map (apsnd (map (prep_att ctxt)) o fst)) (raw_assumes, raw_shows);
wenzelm@61654
  1088
    val (that_fact, goal_ctxt) = params_ctxt
wenzelm@61654
  1089
      |> fold_burrow add_assumes ((map o map) (Thm.cterm_of params_ctxt) assumes_propss)
wenzelm@61654
  1090
      |> (fn (premss, ctxt') =>
wenzelm@61654
  1091
          let
wenzelm@61654
  1092
            val prems = Assumption.local_prems_of ctxt' ctxt;
wenzelm@61654
  1093
            val ctxt'' =
wenzelm@61654
  1094
              ctxt'
wenzelm@61654
  1095
              |> not (null assumes_propss) ?
wenzelm@61654
  1096
                (snd o Proof_Context.note_thmss ""
wenzelm@61654
  1097
                  [((Binding.name Auto_Bind.thatN, []), [(prems, [])])])
wenzelm@61654
  1098
              |> (snd o Proof_Context.note_thmss ""
wenzelm@61654
  1099
                  (assumes_bindings ~~ map (map (fn th => ([th], []))) premss))
wenzelm@61654
  1100
          in (prems, ctxt'') end);
wenzelm@60402
  1101
wenzelm@61659
  1102
    (*result*)
wenzelm@63371
  1103
    val results_bindings = map (apfst Binding.default_pos) shows_bindings;
wenzelm@61659
  1104
    fun after_qed' (result_ctxt, results) state' =
wenzelm@61659
  1105
      let
wenzelm@61659
  1106
        val ctxt' = context_of state';
wenzelm@61659
  1107
        val export0 =
wenzelm@61659
  1108
          Assumption.export false result_ctxt ctxt' #>
wenzelm@61659
  1109
          fold_rev (fn (x, v) => Thm.forall_intr_name (x, Thm.cterm_of params_ctxt v)) params #>
wenzelm@61659
  1110
          Raw_Simplifier.norm_hhf_protect ctxt';
wenzelm@61659
  1111
        val export = map export0 #> Variable.export result_ctxt ctxt';
wenzelm@61659
  1112
      in
wenzelm@61659
  1113
        state'
wenzelm@63371
  1114
        |> local_results (results_bindings ~~ burrow export results)
wenzelm@61659
  1115
        |-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@61659
  1116
        |> after_qed (result_ctxt, results)
wenzelm@61659
  1117
      end;
wenzelm@60461
  1118
  in
wenzelm@60461
  1119
    state
wenzelm@61654
  1120
    |> generic_goal kind before_qed (after_qed', K I) goal_ctxt shows_propss result_binds
wenzelm@60461
  1121
    |> pair that_fact
wenzelm@60461
  1122
  end;
wenzelm@5820
  1123
wenzelm@49866
  1124
fun local_qeds arg =
wenzelm@49866
  1125
  end_proof false arg
wenzelm@60408
  1126
  #> Seq.map_result (generic_qed #-> (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
  1127
wenzelm@49889
  1128
fun local_qed arg =
wenzelm@49889
  1129
  local_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@29383
  1130
wenzelm@5820
  1131
wenzelm@17359
  1132
(* global goals *)
wenzelm@17359
  1133
wenzelm@60415
  1134
fun global_goal prep_propp before_qed after_qed propp ctxt =
wenzelm@60402
  1135
  let
wenzelm@60415
  1136
    val (propss, binds) =
wenzelm@60415
  1137
      prep_propp (Proof_Context.set_mode Proof_Context.mode_schematic ctxt) propp;
wenzelm@60415
  1138
    val goal_ctxt = ctxt
wenzelm@60415
  1139
      |> (fold o fold) Variable.auto_fixes propss
wenzelm@60415
  1140
      |> fold Variable.bind_term binds;
wenzelm@61109
  1141
    fun after_qed' (result_ctxt, results) ctxt' = ctxt'
wenzelm@61109
  1142
      |> Proof_Context.restore_naming ctxt
wenzelm@61109
  1143
      |> after_qed (burrow (Proof_Context.export result_ctxt ctxt') results);
wenzelm@60415
  1144
  in
wenzelm@60415
  1145
    ctxt
wenzelm@60415
  1146
    |> init
wenzelm@60415
  1147
    |> generic_goal "" before_qed (K I, after_qed') goal_ctxt propss []
wenzelm@60415
  1148
  end;
wenzelm@45327
  1149
wenzelm@60388
  1150
val theorem = global_goal Proof_Context.cert_propp;
wenzelm@60388
  1151
val theorem_cmd = global_goal Proof_Context.read_propp;
wenzelm@12065
  1152
wenzelm@49866
  1153
fun global_qeds arg =
wenzelm@49866
  1154
  end_proof true arg
wenzelm@60408
  1155
  #> Seq.map_result (generic_qed #> (fn (((_, after_qed), results), state) =>
wenzelm@49859
  1156
    after_qed results (context_of state)));
wenzelm@17112
  1157
wenzelm@49889
  1158
fun global_qed arg =
wenzelm@49889
  1159
  global_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@12959
  1160
wenzelm@5820
  1161
wenzelm@49907
  1162
(* terminal proof steps *)
wenzelm@17112
  1163
wenzelm@49890
  1164
local
wenzelm@49890
  1165
wenzelm@49846
  1166
fun terminal_proof qeds initial terminal =
wenzelm@61841
  1167
  proof (SOME initial) #> Seq.maps_results (qeds (#2 (#2 initial), terminal))
wenzelm@49863
  1168
  #> Seq.the_result "";
wenzelm@49848
  1169
wenzelm@49890
  1170
in
wenzelm@49890
  1171
wenzelm@29383
  1172
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@60618
  1173
val local_default_proof = local_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@49889
  1174
val local_immediate_proof = local_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1175
val local_done_proof = terminal_proof local_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@17112
  1176
wenzelm@29383
  1177
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@60618
  1178
val global_default_proof = global_terminal_proof ((Method.standard_text, Position.no_range), NONE);
wenzelm@49889
  1179
val global_immediate_proof = global_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1180
val global_done_proof = terminal_proof global_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@5820
  1181
wenzelm@49890
  1182
end;
wenzelm@49890
  1183
wenzelm@6896
  1184
wenzelm@49907
  1185
(* skip proofs *)
wenzelm@49907
  1186
wenzelm@49907
  1187
fun local_skip_proof int state =
wenzelm@49907
  1188
  local_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1189
  Skip_Proof.report (context_of state);
wenzelm@49907
  1190
wenzelm@49907
  1191
fun global_skip_proof int state =
wenzelm@49907
  1192
  global_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1193
  Skip_Proof.report (context_of state);
wenzelm@49907
  1194
wenzelm@49907
  1195
wenzelm@17359
  1196
(* common goal statements *)
wenzelm@17359
  1197
wenzelm@60406
  1198
fun internal_goal print_results mode =
wenzelm@63057
  1199
  local_goal print_results
wenzelm@63057
  1200
    (fn a => fn b => fn c => Proof_Context.cert_statement a b c o Proof_Context.set_mode mode) (K I);
wenzelm@60406
  1201
wenzelm@17359
  1202
local
wenzelm@17359
  1203
wenzelm@63057
  1204
fun gen_have prep_statement prep_att strict_asm before_qed after_qed fixes assumes shows int =
wenzelm@56932
  1205
  local_goal (Proof_Display.print_results int (Position.thread_data ()))
wenzelm@63057
  1206
    prep_statement prep_att strict_asm "have" before_qed after_qed fixes assumes shows;
wenzelm@6896
  1207
wenzelm@63057
  1208
fun gen_show prep_statement prep_att strict_asm before_qed after_qed fixes assumes shows int state =
wenzelm@17359
  1209
  let
wenzelm@32738
  1210
    val testing = Unsynchronized.ref false;
wenzelm@32738
  1211
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
  1212
    fun fail_msg ctxt =
wenzelm@50315
  1213
      "Local statement fails to refine any pending goal" ::
wenzelm@33389
  1214
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
  1215
      |> cat_lines;
wenzelm@6896
  1216
wenzelm@56932
  1217
    val pos = Position.thread_data ();
wenzelm@17359
  1218
    fun print_results ctxt res =
wenzelm@46728
  1219
      if ! testing then ()
wenzelm@56932
  1220
      else Proof_Display.print_results int pos ctxt res;
wenzelm@17359
  1221
    fun print_rule ctxt th =
wenzelm@17359
  1222
      if ! testing then rule := SOME th
wenzelm@41181
  1223
      else if int then
wenzelm@56933
  1224
        Proof_Display.string_of_rule ctxt "Successful" th
wenzelm@56933
  1225
        |> Markup.markup Markup.text_fold
wenzelm@59184
  1226
        |> Output.state
wenzelm@17359
  1227
      else ();
wenzelm@17359
  1228
    val test_proof =
wenzelm@50315
  1229
      local_skip_proof true
wenzelm@39616
  1230
      |> Unsynchronized.setmp testing true
wenzelm@42042
  1231
      |> Exn.interruptible_capture;
wenzelm@6896
  1232
wenzelm@60415
  1233
    fun after_qed' (result_ctxt, results) state' = state'
wenzelm@60415
  1234
      |> refine_goals print_rule result_ctxt (flat results)
wenzelm@60415
  1235
      |> check_result "Failed to refine any pending goal"
wenzelm@60415
  1236
      |> after_qed (result_ctxt, results);
wenzelm@17359
  1237
  in
wenzelm@17359
  1238
    state
wenzelm@63057
  1239
    |> local_goal print_results prep_statement prep_att strict_asm
wenzelm@60414
  1240
      "show" before_qed after_qed' fixes assumes shows
wenzelm@60461
  1241
    ||> int ? (fn goal_state =>
wenzelm@49907
  1242
      (case test_proof (map_context (Context_Position.set_visible false) goal_state) of
wenzelm@50315
  1243
        Exn.Res _ => goal_state
wenzelm@42042
  1244
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1245
  end;
wenzelm@17359
  1246
wenzelm@17359
  1247
in
wenzelm@17359
  1248
wenzelm@63057
  1249
val have = gen_have Proof_Context.cert_statement (K I);
wenzelm@63057
  1250
val have_cmd = gen_have Proof_Context.read_statement Attrib.attribute_cmd;
wenzelm@63057
  1251
val show = gen_show Proof_Context.cert_statement (K I);
wenzelm@63057
  1252
val show_cmd = gen_show Proof_Context.read_statement Attrib.attribute_cmd;
wenzelm@6896
  1253
wenzelm@5820
  1254
end;
wenzelm@17359
  1255
wenzelm@28410
  1256
wenzelm@29385
  1257
wenzelm@29385
  1258
(** future proofs **)
wenzelm@29385
  1259
wenzelm@29385
  1260
(* relevant proof states *)
wenzelm@29385
  1261
wenzelm@29385
  1262
fun schematic_goal state =
wenzelm@60611
  1263
  let val (_, {statement = (_, _, prop), ...}) = find_goal state
wenzelm@51553
  1264
  in Goal.is_schematic prop end;
wenzelm@29385
  1265
wenzelm@29385
  1266
fun is_relevant state =
wenzelm@29385
  1267
  (case try find_goal state of
wenzelm@29385
  1268
    NONE => true
wenzelm@60611
  1269
  | SOME (_, {statement = (_, _, prop), goal, ...}) =>
wenzelm@51553
  1270
      Goal.is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1271
wenzelm@29385
  1272
wenzelm@29385
  1273
(* full proofs *)
wenzelm@28410
  1274
wenzelm@29346
  1275
local
wenzelm@29346
  1276
wenzelm@49912
  1277
structure Result = Proof_Data
wenzelm@49912
  1278
(
wenzelm@49912
  1279
  type T = thm option;
wenzelm@59150
  1280
  fun init _ = NONE;
wenzelm@49912
  1281
);
wenzelm@49912
  1282
wenzelm@49912
  1283
fun the_result ctxt =
wenzelm@49912
  1284
  (case Result.get ctxt of
wenzelm@49912
  1285
    NONE => error "No result of forked proof"
wenzelm@49912
  1286
  | SOME th => th);
wenzelm@49912
  1287
wenzelm@49912
  1288
val set_result = Result.put o SOME;
wenzelm@49912
  1289
val reset_result = Result.put NONE;
wenzelm@49912
  1290
wenzelm@51318
  1291
in
wenzelm@51318
  1292
wenzelm@51318
  1293
fun future_proof fork_proof state =
wenzelm@28410
  1294
  let
wenzelm@29381
  1295
    val _ = assert_backward state;
wenzelm@60611
  1296
    val (goal_ctxt, goal_info) = find_goal state;
wenzelm@60415
  1297
    val {statement as (kind, _, prop), using, goal, before_qed, after_qed} = goal_info;
wenzelm@28410
  1298
wenzelm@29383
  1299
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1300
wenzelm@60415
  1301
    val after_qed' =
wenzelm@60415
  1302
      (fn (_, [[th]]) => map_context (set_result th),
wenzelm@60415
  1303
       fn (_, [[th]]) => set_result th);
wenzelm@28973
  1304
    val result_ctxt =
wenzelm@28973
  1305
      state
wenzelm@49912
  1306
      |> map_context reset_result
wenzelm@60611
  1307
      |> map_goal (K (goal_ctxt, (kind, [[], [prop]], prop), using, goal, before_qed, after_qed'))
wenzelm@29346
  1308
      |> fork_proof;
wenzelm@28973
  1309
wenzelm@51318
  1310
    val future_thm = Future.map (the_result o snd) result_ctxt;
wenzelm@60411
  1311
    val finished_goal = Goal.protect 0 (Goal.future_result goal_ctxt future_thm prop);
wenzelm@28973
  1312
    val state' =
wenzelm@28973
  1313
      state
wenzelm@60611
  1314
      |> map_goal (K (goal_ctxt, statement, using, finished_goal, NONE, after_qed));
wenzelm@51318
  1315
  in (Future.map fst result_ctxt, state') end;
wenzelm@29385
  1316
wenzelm@17359
  1317
end;
wenzelm@29346
  1318
wenzelm@29385
  1319
wenzelm@51662
  1320
(* terminal proofs *)  (* FIXME avoid toplevel imitation -- include in PIDE/document *)
wenzelm@29385
  1321
wenzelm@29385
  1322
local
wenzelm@29385
  1323
wenzelm@60403
  1324
fun future_terminal_proof proof1 proof2 done state =
wenzelm@53189
  1325
  if Goal.future_enabled 3 andalso not (is_relevant state) then
wenzelm@51318
  1326
    state |> future_proof (fn state' =>
wenzelm@51662
  1327
      let
wenzelm@51662
  1328
        val pos = Position.thread_data ();
wenzelm@51662
  1329
        val props = Markup.command_timing :: (Markup.nameN, "by") :: Position.properties_of pos;
wenzelm@51662
  1330
      in
wenzelm@53192
  1331
        Execution.fork {name = "Proof.future_terminal_proof", pos = pos, pri = ~1}
wenzelm@51662
  1332
          (fn () => ((), Timing.protocol props proof2 state'))
wenzelm@51662
  1333
      end) |> snd |> done
wenzelm@51318
  1334
  else proof1 state;
wenzelm@29385
  1335
wenzelm@29385
  1336
in
wenzelm@29385
  1337
wenzelm@51318
  1338
fun local_future_terminal_proof meths =
wenzelm@51605
  1339
  future_terminal_proof
wenzelm@51318
  1340
    (local_terminal_proof meths)
wenzelm@51318
  1341
    (local_terminal_proof meths #> context_of) local_done_proof;
wenzelm@29385
  1342
wenzelm@51318
  1343
fun global_future_terminal_proof meths =
wenzelm@51605
  1344
  future_terminal_proof
wenzelm@51318
  1345
    (global_terminal_proof meths)
wenzelm@51318
  1346
    (global_terminal_proof meths) global_done_proof;
wenzelm@29350
  1347
wenzelm@29346
  1348
end;
wenzelm@29346
  1349
wenzelm@29385
  1350
end;