src/Pure/Isar/proof.ML
author Walther Neuper <walther.neuper@jku.at>
Tue, 05 Jan 2021 17:29:39 +0100
changeset 60141 538e96acb633
parent 60140 8bb9b4a2f575
child 60143 10ef314ef4ae
permissions -rw-r--r--
step 4.7: cannot fill 2 gaps tracing Outer_Syntax .. Output.report

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