src/Pure/Isar/proof.ML
author wenzelm
Wed Apr 27 23:02:43 2011 +0200 (2011-04-27)
changeset 42496 65ec88b369fd
parent 42360 da8817d01e7c
child 42717 0bbb56867091
permissions -rw-r--r--
more precise positions via binding;
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@30757
    22
  val bind_terms: (indexname * term option) list -> state -> state
wenzelm@26251
    23
  val put_thms: bool -> string * thm list option -> state -> state
wenzelm@6091
    24
  val the_facts: state -> thm list
wenzelm@9469
    25
  val the_fact: state -> thm
wenzelm@17450
    26
  val put_facts: thm list option -> state -> state
wenzelm@6891
    27
  val assert_forward: state -> state
wenzelm@17359
    28
  val assert_chain: state -> state
wenzelm@9469
    29
  val assert_forward_or_chain: state -> state
wenzelm@5820
    30
  val assert_backward: state -> state
wenzelm@8206
    31
  val assert_no_chain: state -> state
wenzelm@5820
    32
  val enter_forward: state -> state
wenzelm@24543
    33
  val goal_message: (unit -> Pretty.T) -> state -> state
wenzelm@32738
    34
  val verbose: bool Unsynchronized.ref
wenzelm@12423
    35
  val pretty_state: int -> state -> Pretty.T list
wenzelm@10360
    36
  val pretty_goals: bool -> state -> Pretty.T list
wenzelm@17112
    37
  val refine: Method.text -> state -> state Seq.seq
wenzelm@17112
    38
  val refine_end: Method.text -> state -> state Seq.seq
wenzelm@18908
    39
  val refine_insert: thm list -> state -> state
wenzelm@20208
    40
  val goal_tac: thm -> int -> tactic
wenzelm@17359
    41
  val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq
wenzelm@33288
    42
  val raw_goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    43
  val goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    44
  val simple_goal: state -> {context: context, goal: thm}
wenzelm@38721
    45
  val status_markup: state -> Markup.T
wenzelm@36323
    46
  val let_bind: (term list * term) list -> state -> state
wenzelm@36323
    47
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36505
    48
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36505
    49
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36323
    50
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36323
    51
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wenzelm@20224
    52
  val assm: Assumption.export ->
wenzelm@36323
    53
    (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    54
  val assm_cmd: Assumption.export ->
wenzelm@28084
    55
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    56
  val assume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    57
  val assume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    58
  val presume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    59
  val presume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    60
  val def: (Thm.binding * ((binding * mixfix) * (term * term list))) list -> state -> state
wenzelm@36323
    61
  val def_cmd: (Attrib.binding * ((binding * mixfix) * (string * string list))) list -> state -> state
wenzelm@17359
    62
  val chain: state -> state
wenzelm@17359
    63
  val chain_facts: thm list -> state -> state
wenzelm@36323
    64
  val note_thmss: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wenzelm@36323
    65
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    66
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    67
  val from_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    68
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    69
  val with_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    70
  val using: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    71
  val using_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    72
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    73
  val unfolding_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@42496
    74
  val invoke_case: string * binding option list * attribute list -> state -> state
wenzelm@42496
    75
  val invoke_case_cmd: string * binding option list * Attrib.src list -> state -> state
wenzelm@17359
    76
  val begin_block: state -> state
wenzelm@17359
    77
  val next_block: state -> state
wenzelm@20309
    78
  val end_block: state -> state
wenzelm@40960
    79
  val begin_notepad: Proof.context -> state
wenzelm@40960
    80
  val end_notepad: state -> Proof.context
wenzelm@17112
    81
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@17112
    82
  val defer: int option -> state -> state Seq.seq
wenzelm@17112
    83
  val prefer: int -> state -> state Seq.seq
wenzelm@17112
    84
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    85
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@17359
    86
  val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) ->
wenzelm@18728
    87
    (theory -> 'a -> attribute) ->
wenzelm@17359
    88
    (context * 'b list -> context * (term list list * (context -> context))) ->
wenzelm@29383
    89
    string -> Method.text option -> (thm list list -> state -> state) ->
haftmann@29581
    90
    ((binding * 'a list) * 'b) list -> state -> state
wenzelm@29383
    91
  val local_qed: Method.text option * bool -> state -> state
wenzelm@21442
    92
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36323
    93
    (term * term list) list list -> context -> state
wenzelm@36323
    94
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
    95
    (string * string list) list list -> context -> state
wenzelm@20363
    96
  val global_qed: Method.text option * bool -> state -> context
wenzelm@29383
    97
  val local_terminal_proof: Method.text * Method.text option -> state -> state
wenzelm@29383
    98
  val local_default_proof: state -> state
wenzelm@29383
    99
  val local_immediate_proof: state -> state
wenzelm@29383
   100
  val local_skip_proof: bool -> state -> state
wenzelm@29383
   101
  val local_done_proof: state -> state
wenzelm@20363
   102
  val global_terminal_proof: Method.text * Method.text option -> state -> context
wenzelm@20363
   103
  val global_default_proof: state -> context
wenzelm@20363
   104
  val global_immediate_proof: state -> context
wenzelm@29383
   105
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   106
  val global_done_proof: state -> context
wenzelm@29383
   107
  val have: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   108
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   109
  val have_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   110
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29383
   111
  val show: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   112
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   113
  val show_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   114
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29385
   115
  val schematic_goal: state -> bool
wenzelm@28452
   116
  val is_relevant: state -> bool
wenzelm@29385
   117
  val local_future_proof: (state -> ('a * state) Future.future) ->
wenzelm@29385
   118
    state -> 'a Future.future * state
wenzelm@29385
   119
  val global_future_proof: (state -> ('a * Proof.context) Future.future) ->
wenzelm@29385
   120
    state -> 'a Future.future * context
wenzelm@29385
   121
  val local_future_terminal_proof: Method.text * Method.text option -> bool -> state -> state
wenzelm@29385
   122
  val global_future_terminal_proof: Method.text * Method.text option -> bool -> state -> context
wenzelm@5820
   123
end;
wenzelm@5820
   124
wenzelm@13377
   125
structure Proof: PROOF =
wenzelm@5820
   126
struct
wenzelm@5820
   127
wenzelm@33031
   128
type context = Proof.context;
wenzelm@17112
   129
type method = Method.method;
wenzelm@16813
   130
wenzelm@5820
   131
wenzelm@5820
   132
(** proof state **)
wenzelm@5820
   133
wenzelm@17359
   134
(* datatype state *)
wenzelm@5820
   135
wenzelm@17112
   136
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   137
wenzelm@17359
   138
datatype state =
wenzelm@17359
   139
  State of node Stack.T
wenzelm@17359
   140
and node =
wenzelm@7176
   141
  Node of
wenzelm@7176
   142
   {context: context,
wenzelm@7176
   143
    facts: thm list option,
wenzelm@7176
   144
    mode: mode,
wenzelm@17359
   145
    goal: goal option}
wenzelm@17359
   146
and goal =
wenzelm@17359
   147
  Goal of
wenzelm@29346
   148
   {statement: (string * Position.T) * term list list * term,
wenzelm@28410
   149
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   150
    messages: (unit -> Pretty.T) list,    (*persistent messages (hints etc.)*)
wenzelm@25958
   151
    using: thm list,                      (*goal facts*)
wenzelm@25958
   152
    goal: thm,                            (*subgoals ==> statement*)
wenzelm@17859
   153
    before_qed: Method.text option,
wenzelm@18124
   154
    after_qed:
wenzelm@29383
   155
      (thm list list -> state -> state) *
wenzelm@20363
   156
      (thm list list -> context -> context)};
wenzelm@17359
   157
wenzelm@24543
   158
fun make_goal (statement, messages, using, goal, before_qed, after_qed) =
wenzelm@24543
   159
  Goal {statement = statement, messages = messages, using = using, goal = goal,
wenzelm@17859
   160
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   161
wenzelm@7176
   162
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   163
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   164
wenzelm@17359
   165
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   166
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   167
wenzelm@21727
   168
val init_context =
wenzelm@42360
   169
  Proof_Context.set_stmt true #>
wenzelm@42360
   170
  Proof_Context.map_naming (K Proof_Context.local_naming);
wenzelm@21727
   171
wenzelm@21466
   172
fun init ctxt =
wenzelm@21727
   173
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   174
wenzelm@17359
   175
fun current (State st) = Stack.top st |> (fn Node node => node);
wenzelm@21274
   176
fun map_current f (State st) = State (Stack.map_top (map_node f) st);
wenzelm@28278
   177
fun map_all f (State st) = State (Stack.map_all (map_node f) st);
wenzelm@12045
   178
wenzelm@5820
   179
wenzelm@5820
   180
wenzelm@5820
   181
(** basic proof state operations **)
wenzelm@5820
   182
wenzelm@17359
   183
(* block structure *)
wenzelm@17359
   184
wenzelm@17359
   185
fun open_block (State st) = State (Stack.push st);
wenzelm@17359
   186
wenzelm@18678
   187
fun close_block (State st) = State (Stack.pop st)
wenzelm@18678
   188
  handle Empty => error "Unbalanced block parentheses";
wenzelm@17359
   189
wenzelm@17359
   190
fun level (State st) = Stack.level st;
wenzelm@17359
   191
wenzelm@17359
   192
fun assert_bottom b state =
wenzelm@17359
   193
  let val b' = (level state <= 2) in
wenzelm@18678
   194
    if b andalso not b' then error "Not at bottom of proof!"
wenzelm@18678
   195
    else if not b andalso b' then error "Already at bottom of proof!"
wenzelm@17359
   196
    else state
wenzelm@17359
   197
  end;
wenzelm@17359
   198
wenzelm@17359
   199
wenzelm@5820
   200
(* context *)
wenzelm@5820
   201
wenzelm@17359
   202
val context_of = #context o current;
wenzelm@42360
   203
val theory_of = Proof_Context.theory_of o context_of;
wenzelm@5820
   204
wenzelm@17359
   205
fun map_context f =
wenzelm@17359
   206
  map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   207
wenzelm@17359
   208
fun map_context_result f state =
wenzelm@17859
   209
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   210
wenzelm@28278
   211
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   212
wenzelm@37949
   213
fun propagate_ml_env state = map_contexts
wenzelm@37949
   214
  (Context.proof_map (ML_Env.inherit (Context.Proof (context_of state)))) state;
wenzelm@37949
   215
wenzelm@42360
   216
val bind_terms = map_context o Proof_Context.bind_terms;
wenzelm@42360
   217
val put_thms = map_context oo Proof_Context.put_thms;
wenzelm@5820
   218
wenzelm@5820
   219
wenzelm@5820
   220
(* facts *)
wenzelm@5820
   221
wenzelm@17359
   222
val get_facts = #facts o current;
wenzelm@17359
   223
wenzelm@17359
   224
fun the_facts state =
wenzelm@17359
   225
  (case get_facts state of SOME facts => facts
wenzelm@18678
   226
  | NONE => error "No current facts available");
wenzelm@5820
   227
wenzelm@9469
   228
fun the_fact state =
wenzelm@17359
   229
  (case the_facts state of [thm] => thm
wenzelm@18678
   230
  | _ => error "Single theorem expected");
wenzelm@7605
   231
wenzelm@17359
   232
fun put_facts facts =
wenzelm@19078
   233
  map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wenzelm@33386
   234
  put_thms true (Auto_Bind.thisN, facts);
wenzelm@5820
   235
wenzelm@17359
   236
fun these_factss more_facts (named_factss, state) =
wenzelm@19482
   237
  (named_factss, state |> put_facts (SOME (maps snd named_factss @ more_facts)));
wenzelm@5820
   238
wenzelm@17359
   239
fun export_facts inner outer =
wenzelm@17359
   240
  (case get_facts inner of
wenzelm@20309
   241
    NONE => put_facts NONE outer
wenzelm@17359
   242
  | SOME thms =>
wenzelm@17359
   243
      thms
wenzelm@42360
   244
      |> Proof_Context.export (context_of inner) (context_of outer)
wenzelm@20309
   245
      |> (fn ths => put_facts (SOME ths) outer));
wenzelm@5820
   246
wenzelm@5820
   247
wenzelm@5820
   248
(* mode *)
wenzelm@5820
   249
wenzelm@17359
   250
val get_mode = #mode o current;
wenzelm@5820
   251
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   252
wenzelm@17359
   253
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   254
wenzelm@5820
   255
fun assert_mode pred state =
wenzelm@5820
   256
  let val mode = get_mode state in
wenzelm@5820
   257
    if pred mode then state
wenzelm@18678
   258
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   259
  end;
wenzelm@5820
   260
wenzelm@19308
   261
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   262
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   263
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   264
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   265
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   266
wenzelm@17359
   267
val enter_forward = put_mode Forward;
wenzelm@17359
   268
val enter_chain = put_mode Chain;
wenzelm@17359
   269
val enter_backward = put_mode Backward;
wenzelm@5820
   270
wenzelm@17359
   271
wenzelm@17359
   272
(* current goal *)
wenzelm@17359
   273
wenzelm@17359
   274
fun current_goal state =
wenzelm@17359
   275
  (case current state of
wenzelm@17359
   276
    {context, goal = SOME (Goal goal), ...} => (context, goal)
wenzelm@18678
   277
  | _ => error "No current goal!");
wenzelm@5820
   278
wenzelm@17359
   279
fun assert_current_goal g state =
wenzelm@17359
   280
  let val g' = can current_goal state in
wenzelm@18678
   281
    if g andalso not g' then error "No goal in this block!"
wenzelm@18678
   282
    else if not g andalso g' then error "Goal present in this block!"
wenzelm@17359
   283
    else state
wenzelm@17359
   284
  end;
wenzelm@6776
   285
wenzelm@17359
   286
fun put_goal goal = map_current (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   287
wenzelm@17859
   288
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   289
wenzelm@17359
   290
wenzelm@17359
   291
(* nested goal *)
wenzelm@5820
   292
wenzelm@17359
   293
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
wenzelm@17359
   294
      let
wenzelm@24543
   295
        val Goal {statement, messages, using, goal, before_qed, after_qed} = goal;
wenzelm@24543
   296
        val goal' = make_goal (g (statement, messages, using, goal, before_qed, after_qed));
wenzelm@17359
   297
      in State (make_node (f context, facts, mode, SOME goal'), nodes) end
wenzelm@17359
   298
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@17359
   299
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@17359
   300
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@17359
   301
  | map_goal _ _ state = state;
wenzelm@5820
   302
wenzelm@24556
   303
fun set_goal goal = map_goal I (fn (statement, _, using, _, before_qed, after_qed) =>
wenzelm@24556
   304
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@19188
   305
wenzelm@24543
   306
fun goal_message msg = map_goal I (fn (statement, messages, using, goal, before_qed, after_qed) =>
wenzelm@24543
   307
  (statement, msg :: messages, using, goal, before_qed, after_qed));
wenzelm@24543
   308
wenzelm@24556
   309
fun using_facts using = map_goal I (fn (statement, _, _, goal, before_qed, after_qed) =>
wenzelm@24556
   310
  (statement, [], using, goal, before_qed, after_qed));
wenzelm@17359
   311
wenzelm@17359
   312
local
wenzelm@17359
   313
  fun find i state =
wenzelm@17359
   314
    (case try current_goal state of
wenzelm@17359
   315
      SOME (ctxt, goal) => (ctxt, (i, goal))
wenzelm@18678
   316
    | NONE => find (i + 1) (close_block state handle ERROR _ => error "No goal present"));
wenzelm@17359
   317
in val find_goal = find 0 end;
wenzelm@17359
   318
wenzelm@17359
   319
fun get_goal state =
wenzelm@17359
   320
  let val (ctxt, (_, {using, goal, ...})) = find_goal state
wenzelm@17359
   321
  in (ctxt, (using, goal)) end;
wenzelm@5820
   322
wenzelm@5820
   323
wenzelm@5820
   324
wenzelm@12423
   325
(** pretty_state **)
wenzelm@5820
   326
wenzelm@42360
   327
val verbose = Proof_Context.verbose;
nipkow@13698
   328
skalberg@15531
   329
fun pretty_facts _ _ NONE = []
skalberg@15531
   330
  | pretty_facts s ctxt (SOME ths) =
wenzelm@32091
   331
      [Pretty.big_list (s ^ "this:") (map (Display.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   332
wenzelm@17359
   333
fun pretty_state nr state =
wenzelm@5820
   334
  let
wenzelm@39051
   335
    val {context = ctxt, facts, mode, goal = _} = current state;
wenzelm@5820
   336
wenzelm@5820
   337
    fun levels_up 0 = ""
wenzelm@17359
   338
      | levels_up 1 = "1 level up"
wenzelm@17359
   339
      | levels_up i = string_of_int i ^ " levels up";
wenzelm@5820
   340
wenzelm@11556
   341
    fun subgoals 0 = ""
wenzelm@17359
   342
      | subgoals 1 = "1 subgoal"
wenzelm@17359
   343
      | subgoals n = string_of_int n ^ " subgoals";
wenzelm@17359
   344
wenzelm@17359
   345
    fun description strs =
wenzelm@28375
   346
      (case filter_out (fn s => s = "") strs of [] => ""
wenzelm@17359
   347
      | strs' => enclose " (" ")" (commas strs'));
wenzelm@12146
   348
wenzelm@39051
   349
    fun prt_goal (SOME (_, (i,
wenzelm@29416
   350
      {statement = ((_, pos), _, _), messages, using, goal, before_qed = _, after_qed = _}))) =
wenzelm@17359
   351
          pretty_facts "using " ctxt
wenzelm@17359
   352
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@21442
   353
          [Pretty.str ("goal" ^
wenzelm@21442
   354
            description [levels_up (i div 2), subgoals (Thm.nprems_of goal)] ^ ":")] @
wenzelm@39125
   355
          Goal_Display.pretty_goals ctxt goal @
wenzelm@25958
   356
          (map (fn msg => Position.setmp_thread_data pos msg ()) (rev messages))
wenzelm@17359
   357
      | prt_goal NONE = [];
wenzelm@6848
   358
wenzelm@17359
   359
    val prt_ctxt =
wenzelm@42360
   360
      if ! verbose orelse mode = Forward then Proof_Context.pretty_context ctxt
wenzelm@42360
   361
      else if mode = Backward then Proof_Context.pretty_ctxt ctxt
wenzelm@7575
   362
      else [];
wenzelm@17359
   363
  in
wenzelm@17359
   364
    [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@17359
   365
      (if ! verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")),
wenzelm@17359
   366
      Pretty.str ""] @
wenzelm@17359
   367
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@17359
   368
    (if ! verbose orelse mode = Forward then
wenzelm@39051
   369
       pretty_facts "" ctxt facts @ prt_goal (try find_goal state)
wenzelm@39051
   370
     else if mode = Chain then pretty_facts "picking " ctxt facts
wenzelm@17359
   371
     else prt_goal (try find_goal state))
wenzelm@17359
   372
  end;
wenzelm@5820
   373
wenzelm@12085
   374
fun pretty_goals main state =
wenzelm@39125
   375
  let
wenzelm@39129
   376
    val (_, (_, goal)) = get_goal state;
wenzelm@39129
   377
    val ctxt = context_of state
wenzelm@39125
   378
      |> Config.put Goal_Display.show_main_goal main
wenzelm@39125
   379
      |> Config.put Goal_Display.goals_total false;
wenzelm@39129
   380
  in Goal_Display.pretty_goals ctxt goal end;
wenzelm@10320
   381
wenzelm@5820
   382
wenzelm@5820
   383
wenzelm@5820
   384
(** proof steps **)
wenzelm@5820
   385
wenzelm@17359
   386
(* refine via method *)
wenzelm@5820
   387
wenzelm@8234
   388
local
wenzelm@8234
   389
wenzelm@16146
   390
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   391
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   392
wenzelm@16146
   393
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   394
wenzelm@16146
   395
fun goal_cases st =
berghofe@34916
   396
  Rule_Cases.make_common (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair []) (goals st));
wenzelm@16146
   397
wenzelm@32193
   398
fun apply_method current_context meth state =
wenzelm@6848
   399
  let
wenzelm@24556
   400
    val (goal_ctxt, (_, {statement, messages = _, using, goal, before_qed, after_qed})) =
wenzelm@24543
   401
      find_goal state;
wenzelm@25958
   402
    val ctxt = if current_context then context_of state else goal_ctxt;
wenzelm@16146
   403
  in
wenzelm@32193
   404
    Method.apply meth ctxt using goal |> Seq.map (fn (meth_cases, goal') =>
wenzelm@6848
   405
      state
wenzelm@16146
   406
      |> map_goal
wenzelm@42360
   407
          (Proof_Context.add_cases false (no_goal_cases goal @ goal_cases goal') #>
wenzelm@42360
   408
           Proof_Context.add_cases true meth_cases)
wenzelm@24556
   409
          (K (statement, [], using, goal', before_qed, after_qed)))
wenzelm@16146
   410
  end;
wenzelm@5820
   411
wenzelm@19188
   412
fun select_goals n meth state =
wenzelm@19224
   413
  state
wenzelm@19224
   414
  |> (#2 o #2 o get_goal)
wenzelm@21687
   415
  |> ALLGOALS Goal.conjunction_tac
wenzelm@19224
   416
  |> Seq.maps (fn goal =>
wenzelm@19188
   417
    state
wenzelm@21687
   418
    |> Seq.lift set_goal (Goal.extract 1 n goal |> Seq.maps (Goal.conjunction_tac 1))
wenzelm@19188
   419
    |> Seq.maps meth
wenzelm@19188
   420
    |> Seq.maps (fn state' => state'
wenzelm@19188
   421
      |> Seq.lift set_goal (Goal.retrofit 1 n (#2 (#2 (get_goal state'))) goal))
wenzelm@32193
   422
    |> Seq.maps (apply_method true (K Method.succeed)));
wenzelm@19188
   423
wenzelm@17112
   424
fun apply_text cc text state =
wenzelm@17112
   425
  let
wenzelm@17112
   426
    val thy = theory_of state;
wenzelm@17112
   427
wenzelm@32193
   428
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@32193
   429
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@32193
   430
      | eval (Method.Source_i src) = apply_method cc (Method.method_i thy src)
wenzelm@17112
   431
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   432
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   433
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@19188
   434
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt)
wenzelm@19188
   435
      | eval (Method.SelectGoals (n, txt)) = select_goals n (eval txt);
wenzelm@17112
   436
  in eval text state end;
wenzelm@17112
   437
wenzelm@8234
   438
in
wenzelm@8234
   439
wenzelm@17112
   440
val refine = apply_text true;
wenzelm@17112
   441
val refine_end = apply_text false;
wenzelm@32856
   442
fun refine_insert ths = Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   443
wenzelm@8234
   444
end;
wenzelm@8234
   445
wenzelm@5820
   446
wenzelm@17359
   447
(* refine via sub-proof *)
wenzelm@17359
   448
wenzelm@30557
   449
fun finish_tac 0 = K all_tac
wenzelm@30557
   450
  | finish_tac n =
wenzelm@30557
   451
      Goal.norm_hhf_tac THEN'
wenzelm@30557
   452
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   453
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@30557
   454
          Tactic.etac Drule.protectI i THEN finish_tac (n - 1) i
wenzelm@30557
   455
        else finish_tac (n - 1) (i + 1));
wenzelm@30557
   456
wenzelm@20208
   457
fun goal_tac rule =
wenzelm@30557
   458
  Goal.norm_hhf_tac THEN'
wenzelm@30557
   459
  Tactic.rtac rule THEN'
wenzelm@30557
   460
  finish_tac (Thm.nprems_of rule);
wenzelm@11816
   461
wenzelm@19915
   462
fun refine_goals print_rule inner raw_rules state =
wenzelm@19915
   463
  let
wenzelm@19915
   464
    val (outer, (_, goal)) = get_goal state;
wenzelm@20208
   465
    fun refine rule st = (print_rule outer rule; FINDGOAL (goal_tac rule) st);
wenzelm@19915
   466
  in
wenzelm@19915
   467
    raw_rules
wenzelm@42360
   468
    |> Proof_Context.goal_export inner outer
wenzelm@20309
   469
    |> (fn rules => Seq.lift set_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   470
  end;
wenzelm@17359
   471
wenzelm@6932
   472
wenzelm@17359
   473
(* conclude_goal *)
wenzelm@6932
   474
wenzelm@28627
   475
fun conclude_goal ctxt goal propss =
wenzelm@5820
   476
  let
wenzelm@42360
   477
    val thy = Proof_Context.theory_of ctxt;
wenzelm@24920
   478
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@32091
   479
    val string_of_thm = Display.string_of_thm ctxt;
wenzelm@5820
   480
wenzelm@17359
   481
    val ngoals = Thm.nprems_of goal;
wenzelm@19774
   482
    val _ = ngoals = 0 orelse error (Pretty.string_of (Pretty.chunks
wenzelm@39125
   483
      (Goal_Display.pretty_goals ctxt goal @
wenzelm@23639
   484
        [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])));
wenzelm@20224
   485
wenzelm@20224
   486
    val extra_hyps = Assumption.extra_hyps ctxt goal;
wenzelm@20224
   487
    val _ = null extra_hyps orelse
wenzelm@20224
   488
      error ("Additional hypotheses:\n" ^ cat_lines (map string_of_term extra_hyps));
wenzelm@5820
   489
wenzelm@23418
   490
    fun lost_structure () = error ("Lost goal structure:\n" ^ string_of_thm goal);
wenzelm@23418
   491
berghofe@23782
   492
    val th = Goal.conclude
wenzelm@23806
   493
      (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
berghofe@23782
   494
      handle THM _ => lost_structure ();
wenzelm@23418
   495
    val goal_propss = filter_out null propss;
wenzelm@23418
   496
    val results =
wenzelm@23418
   497
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   498
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@23418
   499
      handle THM _ => lost_structure ();
wenzelm@23418
   500
    val _ = Unify.matches_list thy (flat goal_propss) (map Thm.prop_of (flat results)) orelse
wenzelm@19774
   501
      error ("Proved a different theorem:\n" ^ string_of_thm th);
wenzelm@28627
   502
    val _ = Thm.check_shyps (Variable.sorts_of ctxt) th;
wenzelm@23418
   503
wenzelm@23418
   504
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   505
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   506
      | recover_result [] [] = []
wenzelm@23418
   507
      | recover_result _ _ = lost_structure ();
wenzelm@23418
   508
  in recover_result propss results end;
wenzelm@5820
   509
wenzelm@5820
   510
wenzelm@33288
   511
(* goal views -- corresponding to methods *)
wenzelm@33288
   512
wenzelm@33288
   513
fun raw_goal state =
wenzelm@33288
   514
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   515
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   516
wenzelm@33288
   517
val goal = raw_goal o refine_insert [];
wenzelm@33288
   518
wenzelm@33288
   519
fun simple_goal state =
wenzelm@33288
   520
  let
wenzelm@33288
   521
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   522
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   523
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   524
wenzelm@38721
   525
fun status_markup state =
wenzelm@38721
   526
  (case try goal state of
wenzelm@38721
   527
    SOME {goal, ...} => Markup.proof_state (Thm.nprems_of goal)
wenzelm@38721
   528
  | NONE => Markup.empty);
wenzelm@38721
   529
wenzelm@33288
   530
wenzelm@5820
   531
wenzelm@5820
   532
(*** structured proof commands ***)
wenzelm@5820
   533
wenzelm@17112
   534
(** context elements **)
wenzelm@5820
   535
wenzelm@36324
   536
(* let bindings *)
wenzelm@5820
   537
wenzelm@16813
   538
local
wenzelm@16813
   539
wenzelm@17359
   540
fun gen_bind bind args state =
wenzelm@5820
   541
  state
wenzelm@5820
   542
  |> assert_forward
wenzelm@36324
   543
  |> map_context (bind true args #> snd)
wenzelm@17359
   544
  |> put_facts NONE;
wenzelm@5820
   545
wenzelm@16813
   546
in
wenzelm@16813
   547
wenzelm@42360
   548
val let_bind = gen_bind Proof_Context.match_bind_i;
wenzelm@42360
   549
val let_bind_cmd = gen_bind Proof_Context.match_bind;
wenzelm@5820
   550
wenzelm@16813
   551
end;
wenzelm@16813
   552
wenzelm@5820
   553
wenzelm@36505
   554
(* concrete syntax *)
wenzelm@36505
   555
wenzelm@36505
   556
local
wenzelm@36505
   557
wenzelm@36507
   558
fun gen_write prep_arg mode args =
wenzelm@36507
   559
  assert_forward
wenzelm@42360
   560
  #> map_context (fn ctxt => ctxt |> Proof_Context.notation true mode (map (prep_arg ctxt) args))
wenzelm@36507
   561
  #> put_facts NONE;
wenzelm@36505
   562
wenzelm@36505
   563
in
wenzelm@36505
   564
wenzelm@36505
   565
val write = gen_write (K I);
wenzelm@36505
   566
wenzelm@36505
   567
val write_cmd =
wenzelm@36505
   568
  gen_write (fn ctxt => fn (c, mx) =>
wenzelm@42360
   569
    (Proof_Context.read_const ctxt false (Mixfix.mixfixT mx) c, mx));
wenzelm@36505
   570
wenzelm@36505
   571
end;
wenzelm@36505
   572
wenzelm@36505
   573
wenzelm@17359
   574
(* fix *)
wenzelm@9196
   575
wenzelm@12714
   576
local
wenzelm@12714
   577
wenzelm@30763
   578
fun gen_fix prep_vars args =
wenzelm@17359
   579
  assert_forward
wenzelm@42360
   580
  #> map_context (fn ctxt => snd (Proof_Context.add_fixes (prep_vars ctxt args) ctxt))
wenzelm@17359
   581
  #> put_facts NONE;
wenzelm@5820
   582
wenzelm@16813
   583
in
wenzelm@16813
   584
wenzelm@36323
   585
val fix = gen_fix (K I);
wenzelm@42360
   586
val fix_cmd = gen_fix (fn ctxt => fn args => fst (Proof_Context.read_vars args ctxt));
wenzelm@5820
   587
wenzelm@16813
   588
end;
wenzelm@16813
   589
wenzelm@5820
   590
wenzelm@17359
   591
(* assume etc. *)
wenzelm@5820
   592
wenzelm@16813
   593
local
wenzelm@16813
   594
wenzelm@17112
   595
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   596
  state
wenzelm@5820
   597
  |> assert_forward
wenzelm@17112
   598
  |> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args))
wenzelm@17359
   599
  |> these_factss [] |> #2;
wenzelm@6932
   600
wenzelm@16813
   601
in
wenzelm@16813
   602
wenzelm@42360
   603
val assm = gen_assume Proof_Context.add_assms_i (K I);
wenzelm@42360
   604
val assm_cmd = gen_assume Proof_Context.add_assms Attrib.attribute;
wenzelm@36323
   605
val assume = assm Assumption.assume_export;
wenzelm@36323
   606
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   607
val presume = assm Assumption.presume_export;
wenzelm@36323
   608
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   609
wenzelm@16813
   610
end;
wenzelm@16813
   611
wenzelm@7271
   612
wenzelm@17359
   613
(* def *)
wenzelm@11891
   614
wenzelm@16813
   615
local
wenzelm@16813
   616
wenzelm@20913
   617
fun gen_def prep_att prep_vars prep_binds args state =
wenzelm@11891
   618
  let
wenzelm@11891
   619
    val _ = assert_forward state;
wenzelm@17112
   620
    val thy = theory_of state;
wenzelm@11891
   621
wenzelm@20913
   622
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@20913
   623
    val name_atts = map (apsnd (map (prep_att thy))) raw_name_atts;
wenzelm@11891
   624
  in
wenzelm@20913
   625
    state
wenzelm@20913
   626
    |> map_context_result (prep_vars (map (fn (x, mx) => (x, NONE, mx)) raw_vars))
wenzelm@20913
   627
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   628
    |-> (fn vars =>
wenzelm@20913
   629
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@35624
   630
      #-> (fn rhss => map_context_result (Local_Defs.add_defs (vars ~~ (name_atts ~~ rhss)))))
wenzelm@20913
   631
    |-> (put_facts o SOME o map (#2 o #2))
wenzelm@11891
   632
  end;
wenzelm@11891
   633
wenzelm@16813
   634
in
wenzelm@16813
   635
wenzelm@42360
   636
val def = gen_def (K I) Proof_Context.cert_vars Proof_Context.match_bind_i;
wenzelm@42360
   637
val def_cmd = gen_def Attrib.attribute Proof_Context.read_vars Proof_Context.match_bind;
wenzelm@11891
   638
wenzelm@16813
   639
end;
wenzelm@16813
   640
wenzelm@11891
   641
wenzelm@8374
   642
wenzelm@17112
   643
(** facts **)
wenzelm@5820
   644
wenzelm@17359
   645
(* chain *)
wenzelm@5820
   646
wenzelm@24011
   647
fun clean_facts ctxt =
wenzelm@24050
   648
  put_facts (SOME (filter_out Thm.is_dummy (the_facts ctxt))) ctxt;
wenzelm@24011
   649
wenzelm@17359
   650
val chain =
wenzelm@17359
   651
  assert_forward
wenzelm@24011
   652
  #> clean_facts
wenzelm@17359
   653
  #> enter_chain;
wenzelm@5820
   654
wenzelm@17359
   655
fun chain_facts facts =
wenzelm@17359
   656
  put_facts (SOME facts)
wenzelm@17359
   657
  #> chain;
wenzelm@5820
   658
wenzelm@5820
   659
wenzelm@17359
   660
(* note etc. *)
wenzelm@17112
   661
haftmann@28965
   662
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   663
wenzelm@17112
   664
local
wenzelm@17112
   665
wenzelm@30760
   666
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   667
  state
wenzelm@17112
   668
  |> assert_forward
wenzelm@42360
   669
  |> map_context_result (Proof_Context.note_thmss ""
wenzelm@30760
   670
    (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state)) args))
wenzelm@17112
   671
  |> these_factss (more_facts state)
wenzelm@17359
   672
  ||> opt_chain
wenzelm@17359
   673
  |> opt_result;
wenzelm@17112
   674
wenzelm@17112
   675
in
wenzelm@17112
   676
wenzelm@36323
   677
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@42360
   678
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute Proof_Context.get_fact;
wenzelm@17112
   679
wenzelm@36323
   680
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@42360
   681
val from_thmss_cmd = gen_thmss (K []) chain #2 Attrib.attribute Proof_Context.get_fact o no_binding;
wenzelm@17359
   682
wenzelm@36323
   683
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@42360
   684
val with_thmss_cmd = gen_thmss the_facts chain #2 Attrib.attribute Proof_Context.get_fact o no_binding;
wenzelm@30760
   685
wenzelm@30760
   686
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   687
wenzelm@17112
   688
end;
wenzelm@17112
   689
wenzelm@17112
   690
wenzelm@18548
   691
(* using/unfolding *)
wenzelm@17112
   692
wenzelm@17112
   693
local
wenzelm@17112
   694
wenzelm@30760
   695
fun gen_using f g prep_atts prep_fact args state =
wenzelm@17112
   696
  state
wenzelm@17112
   697
  |> assert_backward
wenzelm@21442
   698
  |> map_context_result
wenzelm@42360
   699
    (Proof_Context.note_thmss ""
wenzelm@30760
   700
      (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state))
wenzelm@30760
   701
        (no_binding args)))
wenzelm@18843
   702
  |> (fn (named_facts, state') =>
wenzelm@24556
   703
    state' |> map_goal I (fn (statement, _, using, goal, before_qed, after_qed) =>
wenzelm@18843
   704
      let
wenzelm@18843
   705
        val ctxt = context_of state';
wenzelm@19482
   706
        val ths = maps snd named_facts;
wenzelm@24556
   707
      in (statement, [], f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   708
wenzelm@24050
   709
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   710
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   711
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   712
wenzelm@17112
   713
in
wenzelm@17112
   714
wenzelm@36323
   715
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@42360
   716
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute Proof_Context.get_fact;
wenzelm@36323
   717
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@42360
   718
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute Proof_Context.get_fact;
wenzelm@17112
   719
wenzelm@17112
   720
end;
wenzelm@17112
   721
wenzelm@17112
   722
wenzelm@17112
   723
(* case *)
wenzelm@17112
   724
wenzelm@17112
   725
local
wenzelm@17112
   726
wenzelm@30419
   727
fun qualified_binding a =
wenzelm@30419
   728
  Binding.qualify true (Long_Name.qualifier a) (Binding.name (Long_Name.base_name a));
wenzelm@30419
   729
wenzelm@17112
   730
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   731
  let
wenzelm@17112
   732
    val atts = map (prep_att (theory_of state)) raw_atts;
wenzelm@19078
   733
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@42360
   734
      ctxt |> Proof_Context.apply_case (Proof_Context.get_case ctxt name xs));
wenzelm@30419
   735
    val assumptions = asms |> map (fn (a, ts) => ((qualified_binding a, atts), map (rpair []) ts));
wenzelm@17112
   736
  in
wenzelm@17112
   737
    state'
wenzelm@36323
   738
    |> assume assumptions
wenzelm@33386
   739
    |> bind_terms Auto_Bind.no_facts
wenzelm@36323
   740
    |> `the_facts |-> (fn thms => note_thmss [((Binding.name name, []), [(thms, [])])])
wenzelm@17112
   741
  end;
wenzelm@17112
   742
wenzelm@17112
   743
in
wenzelm@17112
   744
wenzelm@36323
   745
val invoke_case = gen_invoke_case (K I);
wenzelm@36323
   746
val invoke_case_cmd = gen_invoke_case Attrib.attribute;
wenzelm@17112
   747
wenzelm@17112
   748
end;
wenzelm@17112
   749
wenzelm@17112
   750
wenzelm@17112
   751
wenzelm@17359
   752
(** proof structure **)
wenzelm@17359
   753
wenzelm@17359
   754
(* blocks *)
wenzelm@17359
   755
wenzelm@17359
   756
val begin_block =
wenzelm@17359
   757
  assert_forward
wenzelm@17359
   758
  #> open_block
wenzelm@17359
   759
  #> put_goal NONE
wenzelm@17359
   760
  #> open_block;
wenzelm@17359
   761
wenzelm@17359
   762
val next_block =
wenzelm@17359
   763
  assert_forward
wenzelm@17359
   764
  #> close_block
wenzelm@17359
   765
  #> open_block
wenzelm@17359
   766
  #> put_goal NONE
wenzelm@17359
   767
  #> put_facts NONE;
wenzelm@17359
   768
wenzelm@17359
   769
fun end_block state =
wenzelm@17359
   770
  state
wenzelm@17359
   771
  |> assert_forward
wenzelm@40960
   772
  |> assert_bottom false
wenzelm@17359
   773
  |> close_block
wenzelm@17359
   774
  |> assert_current_goal false
wenzelm@17359
   775
  |> close_block
wenzelm@17359
   776
  |> export_facts state;
wenzelm@17359
   777
wenzelm@17359
   778
wenzelm@40960
   779
(* global notepad *)
wenzelm@40960
   780
wenzelm@40960
   781
val begin_notepad =
wenzelm@40960
   782
  init
wenzelm@40960
   783
  #> open_block
wenzelm@40960
   784
  #> map_context (Variable.set_body true)
wenzelm@40960
   785
  #> open_block;
wenzelm@40960
   786
wenzelm@40960
   787
val end_notepad =
wenzelm@40960
   788
  assert_forward
wenzelm@40960
   789
  #> assert_bottom true
wenzelm@40960
   790
  #> close_block
wenzelm@40960
   791
  #> assert_current_goal false
wenzelm@40960
   792
  #> close_block
wenzelm@40960
   793
  #> context_of;
wenzelm@40960
   794
wenzelm@40960
   795
wenzelm@17359
   796
(* sub-proofs *)
wenzelm@17359
   797
wenzelm@17359
   798
fun proof opt_text =
wenzelm@17359
   799
  assert_backward
wenzelm@17859
   800
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   801
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   802
wenzelm@23360
   803
fun end_proof bot txt state =
wenzelm@23360
   804
  state
wenzelm@23360
   805
  |> assert_forward
wenzelm@23360
   806
  |> assert_bottom bot
wenzelm@23360
   807
  |> close_block
wenzelm@23360
   808
  |> assert_current_goal true
wenzelm@23360
   809
  |> using_facts []
wenzelm@23360
   810
  |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@32193
   811
  |> Seq.maps (refine (Method.finish_text txt));
wenzelm@17359
   812
wenzelm@29383
   813
fun check_result msg sq =
wenzelm@29383
   814
  (case Seq.pull sq of
wenzelm@29383
   815
    NONE => error msg
wenzelm@29383
   816
  | SOME (s, _) => s);
wenzelm@29383
   817
wenzelm@29383
   818
fun check_finish sq = check_result "Failed to finish proof" sq;
wenzelm@29383
   819
wenzelm@17359
   820
wenzelm@17359
   821
(* unstructured refinement *)
wenzelm@17359
   822
wenzelm@32193
   823
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i)));
wenzelm@32193
   824
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i)));
wenzelm@17359
   825
wenzelm@17359
   826
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@17359
   827
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   828
wenzelm@17359
   829
wenzelm@17359
   830
wenzelm@17112
   831
(** goals **)
wenzelm@17112
   832
wenzelm@17359
   833
(* generic goals *)
wenzelm@17359
   834
wenzelm@19774
   835
local
wenzelm@19774
   836
wenzelm@36322
   837
val is_var =
wenzelm@36322
   838
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   839
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   840
wenzelm@36322
   841
fun implicit_vars props =
wenzelm@19846
   842
  let
wenzelm@36354
   843
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   844
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   845
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   846
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   847
wenzelm@19774
   848
fun refine_terms n =
wenzelm@30510
   849
  refine (Method.Basic (K (RAW_METHOD
wenzelm@19774
   850
    (K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@32193
   851
      (HEADGOAL (CONJUNCTS (ALLGOALS (rtac Drule.termI))))))))))
wenzelm@19774
   852
  #> Seq.hd;
wenzelm@19774
   853
wenzelm@19774
   854
in
wenzelm@17359
   855
wenzelm@17859
   856
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   857
  let
wenzelm@17359
   858
    val thy = theory_of state;
wenzelm@23418
   859
    val cert = Thm.cterm_of thy;
wenzelm@17359
   860
    val chaining = can assert_chain state;
wenzelm@25958
   861
    val pos = Position.thread_data ();
wenzelm@17359
   862
wenzelm@17359
   863
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   864
      state
wenzelm@5936
   865
      |> assert_forward_or_chain
wenzelm@5936
   866
      |> enter_forward
wenzelm@17359
   867
      |> open_block
wenzelm@17859
   868
      |> map_context_result (fn ctxt => swap (prepp (ctxt, raw_propp)));
wenzelm@19482
   869
    val props = flat propss;
wenzelm@15703
   870
wenzelm@36322
   871
    val vars = implicit_vars props;
wenzelm@36322
   872
    val propss' = vars :: propss;
wenzelm@23418
   873
    val goal_propss = filter_out null propss';
wenzelm@29346
   874
    val goal =
wenzelm@29346
   875
      cert (Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss))
wenzelm@28627
   876
      |> Thm.weaken_sorts (Variable.sorts_of (context_of goal_state));
wenzelm@29346
   877
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@18124
   878
    val after_qed' = after_qed |>> (fn after_local =>
wenzelm@18124
   879
      fn results => map_context after_ctxt #> after_local results);
wenzelm@5820
   880
  in
wenzelm@17359
   881
    goal_state
wenzelm@21727
   882
    |> map_context (init_context #> Variable.set_body true)
wenzelm@28410
   883
    |> put_goal (SOME (make_goal (statement, [], [], Goal.init goal, before_qed, after_qed')))
wenzelm@42360
   884
    |> map_context (Proof_Context.auto_bind_goal props)
wenzelm@21565
   885
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@17359
   886
    |> put_facts NONE
wenzelm@17359
   887
    |> open_block
wenzelm@17359
   888
    |> put_goal NONE
wenzelm@5820
   889
    |> enter_backward
wenzelm@23418
   890
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@32193
   891
    |> null props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   892
  end;
wenzelm@5820
   893
wenzelm@29090
   894
fun generic_qed after_ctxt state =
wenzelm@12503
   895
  let
wenzelm@36354
   896
    val (goal_ctxt, {statement = (_, stmt, _), goal, after_qed, ...}) = current_goal state;
wenzelm@8617
   897
    val outer_state = state |> close_block;
wenzelm@8617
   898
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   899
wenzelm@18503
   900
    val props =
wenzelm@19774
   901
      flat (tl stmt)
wenzelm@19915
   902
      |> Variable.exportT_terms goal_ctxt outer_ctxt;
wenzelm@17359
   903
    val results =
wenzelm@28627
   904
      tl (conclude_goal goal_ctxt goal stmt)
wenzelm@42360
   905
      |> burrow (Proof_Context.export goal_ctxt outer_ctxt);
wenzelm@17359
   906
  in
wenzelm@17359
   907
    outer_state
wenzelm@29090
   908
    |> map_context (after_ctxt props)
wenzelm@34239
   909
    |> pair (after_qed, results)
wenzelm@17359
   910
  end;
wenzelm@17359
   911
wenzelm@19774
   912
end;
wenzelm@19774
   913
wenzelm@9469
   914
wenzelm@17359
   915
(* local goals *)
wenzelm@17359
   916
wenzelm@17859
   917
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   918
  let
wenzelm@21362
   919
    val thy = theory_of state;
wenzelm@21362
   920
    val ((names, attss), propp) =
wenzelm@21362
   921
      Attrib.map_specs (prep_att thy) stmt |> split_list |>> split_list;
wenzelm@17359
   922
wenzelm@18124
   923
    fun after_qed' results =
wenzelm@18808
   924
      local_results ((names ~~ attss) ~~ results)
wenzelm@17359
   925
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@18124
   926
      #> after_qed results;
wenzelm@5820
   927
  in
wenzelm@17359
   928
    state
wenzelm@21362
   929
    |> generic_goal prepp kind before_qed (after_qed', K I) propp
wenzelm@19900
   930
    |> tap (Variable.warn_extra_tfrees (context_of state) o context_of)
wenzelm@5820
   931
  end;
wenzelm@5820
   932
wenzelm@29383
   933
fun local_qeds txt =
wenzelm@29383
   934
  end_proof false txt
wenzelm@42360
   935
  #> Seq.map (generic_qed Proof_Context.auto_bind_facts #->
wenzelm@29090
   936
    (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
   937
wenzelm@29383
   938
fun local_qed txt = local_qeds txt #> check_finish;
wenzelm@29383
   939
wenzelm@5820
   940
wenzelm@17359
   941
(* global goals *)
wenzelm@17359
   942
wenzelm@21442
   943
fun global_goal prepp before_qed after_qed propp ctxt =
wenzelm@21362
   944
  init ctxt
wenzelm@42360
   945
  |> generic_goal (prepp #> Proof_Context.auto_fixes) "" before_qed (K I, after_qed) propp;
wenzelm@17359
   946
wenzelm@42360
   947
val theorem = global_goal Proof_Context.bind_propp_schematic_i;
wenzelm@42360
   948
val theorem_cmd = global_goal Proof_Context.bind_propp_schematic;
wenzelm@12065
   949
wenzelm@17359
   950
fun global_qeds txt =
wenzelm@17359
   951
  end_proof true txt
wenzelm@29090
   952
  #> Seq.map (generic_qed (K I) #> (fn (((_, after_qed), results), state) =>
wenzelm@29383
   953
    after_qed results (context_of state)));
wenzelm@17112
   954
wenzelm@29383
   955
fun global_qed txt = global_qeds txt #> check_finish;
wenzelm@12959
   956
wenzelm@5820
   957
wenzelm@17359
   958
(* concluding steps *)
wenzelm@17112
   959
wenzelm@29383
   960
fun terminal_proof qed initial terminal =
wenzelm@29383
   961
  proof (SOME initial) #> Seq.maps (qed terminal) #> check_finish;
wenzelm@17112
   962
wenzelm@29383
   963
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@17112
   964
val local_default_proof = local_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   965
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   966
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   967
val local_done_proof = terminal_proof local_qeds Method.done_text (NONE, false);
wenzelm@17112
   968
wenzelm@29383
   969
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@17112
   970
val global_default_proof = global_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   971
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   972
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   973
val global_done_proof = terminal_proof global_qeds Method.done_text (NONE, false);
wenzelm@5820
   974
wenzelm@6896
   975
wenzelm@17359
   976
(* common goal statements *)
wenzelm@17359
   977
wenzelm@17359
   978
local
wenzelm@17359
   979
wenzelm@17859
   980
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@33389
   981
  local_goal (Proof_Display.print_results int) prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
   982
wenzelm@17859
   983
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
   984
  let
wenzelm@32738
   985
    val testing = Unsynchronized.ref false;
wenzelm@32738
   986
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
   987
    fun fail_msg ctxt =
wenzelm@29383
   988
      "Local statement will fail to refine any pending goal" ::
wenzelm@33389
   989
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
   990
      |> cat_lines;
wenzelm@6896
   991
wenzelm@17359
   992
    fun print_results ctxt res =
wenzelm@33389
   993
      if ! testing then () else Proof_Display.print_results int ctxt res;
wenzelm@17359
   994
    fun print_rule ctxt th =
wenzelm@17359
   995
      if ! testing then rule := SOME th
wenzelm@41181
   996
      else if int then
wenzelm@41181
   997
        writeln (Markup.markup Markup.state (Proof_Display.string_of_rule ctxt "Successful" th))
wenzelm@17359
   998
      else ();
wenzelm@17359
   999
    val test_proof =
wenzelm@29383
  1000
      try (local_skip_proof true)
wenzelm@39616
  1001
      |> Unsynchronized.setmp testing true
wenzelm@42042
  1002
      |> Exn.interruptible_capture;
wenzelm@6896
  1003
wenzelm@18124
  1004
    fun after_qed' results =
wenzelm@19482
  1005
      refine_goals print_rule (context_of state) (flat results)
wenzelm@29383
  1006
      #> check_result "Failed to refine any pending goal"
wenzelm@29383
  1007
      #> after_qed results;
wenzelm@17359
  1008
  in
wenzelm@17359
  1009
    state
wenzelm@17859
  1010
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@21565
  1011
    |> int ? (fn goal_state =>
wenzelm@17359
  1012
      (case test_proof goal_state of
wenzelm@23963
  1013
        Exn.Result (SOME _) => goal_state
wenzelm@23963
  1014
      | Exn.Result NONE => error (fail_msg (context_of goal_state))
wenzelm@42042
  1015
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1016
  end;
wenzelm@17359
  1017
wenzelm@17359
  1018
in
wenzelm@17359
  1019
wenzelm@42360
  1020
val have = gen_have (K I) Proof_Context.bind_propp_i;
wenzelm@42360
  1021
val have_cmd = gen_have Attrib.attribute Proof_Context.bind_propp;
wenzelm@42360
  1022
val show = gen_show (K I) Proof_Context.bind_propp_i;
wenzelm@42360
  1023
val show_cmd = gen_show Attrib.attribute Proof_Context.bind_propp;
wenzelm@6896
  1024
wenzelm@5820
  1025
end;
wenzelm@17359
  1026
wenzelm@28410
  1027
wenzelm@29385
  1028
wenzelm@29385
  1029
(** future proofs **)
wenzelm@29385
  1030
wenzelm@29385
  1031
(* relevant proof states *)
wenzelm@29385
  1032
wenzelm@29385
  1033
fun is_schematic t =
wenzelm@29385
  1034
  Term.exists_subterm Term.is_Var t orelse
wenzelm@29385
  1035
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@29385
  1036
wenzelm@29385
  1037
fun schematic_goal state =
wenzelm@29385
  1038
  let val (_, (_, {statement = (_, _, prop), ...})) = find_goal state
wenzelm@29385
  1039
  in is_schematic prop end;
wenzelm@29385
  1040
wenzelm@29385
  1041
fun is_relevant state =
wenzelm@29385
  1042
  (case try find_goal state of
wenzelm@29385
  1043
    NONE => true
wenzelm@29385
  1044
  | SOME (_, (_, {statement = (_, _, prop), goal, ...})) =>
wenzelm@29385
  1045
      is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1046
wenzelm@29385
  1047
wenzelm@29385
  1048
(* full proofs *)
wenzelm@28410
  1049
wenzelm@29346
  1050
local
wenzelm@29346
  1051
wenzelm@29385
  1052
fun future_proof done get_context fork_proof state =
wenzelm@28410
  1053
  let
wenzelm@29381
  1054
    val _ = assert_backward state;
wenzelm@28410
  1055
    val (goal_ctxt, (_, goal)) = find_goal state;
wenzelm@32786
  1056
    val {statement as (kind, _, prop), messages, using, goal, before_qed, after_qed} = goal;
wenzelm@32062
  1057
    val goal_txt = prop :: map Thm.term_of (Assumption.all_assms_of goal_ctxt);
wenzelm@28410
  1058
wenzelm@29383
  1059
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1060
wenzelm@29346
  1061
    val prop' = Logic.protect prop;
wenzelm@29346
  1062
    val statement' = (kind, [[], [prop']], prop');
wenzelm@29346
  1063
    val goal' = Thm.adjust_maxidx_thm (Thm.maxidx_of goal)
wenzelm@29346
  1064
      (Drule.comp_no_flatten (goal, Thm.nprems_of goal) 1 Drule.protectI);
wenzelm@29385
  1065
wenzelm@33386
  1066
    fun after_local' [[th]] = put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@42360
  1067
    fun after_global' [[th]] = Proof_Context.put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@29385
  1068
    val after_qed' = (after_local', after_global');
wenzelm@42360
  1069
    val this_name = Proof_Context.full_name goal_ctxt (Binding.name Auto_Bind.thisN);
wenzelm@28410
  1070
wenzelm@28973
  1071
    val result_ctxt =
wenzelm@28973
  1072
      state
wenzelm@32062
  1073
      |> map_contexts (fold Variable.declare_term goal_txt)
wenzelm@29385
  1074
      |> map_goal I (K (statement', messages, using, goal', before_qed, after_qed'))
wenzelm@29346
  1075
      |> fork_proof;
wenzelm@28973
  1076
wenzelm@29385
  1077
    val future_thm = result_ctxt |> Future.map (fn (_, x) =>
wenzelm@42360
  1078
      Proof_Context.get_fact_single (get_context x) (Facts.named this_name));
wenzelm@32062
  1079
    val finished_goal = Goal.future_result goal_ctxt future_thm prop';
wenzelm@28973
  1080
    val state' =
wenzelm@28973
  1081
      state
wenzelm@28973
  1082
      |> map_goal I (K (statement, messages, using, finished_goal, NONE, after_qed))
wenzelm@29385
  1083
      |> done;
wenzelm@28973
  1084
  in (Future.map #1 result_ctxt, state') end;
wenzelm@28410
  1085
wenzelm@29385
  1086
in
wenzelm@29385
  1087
wenzelm@29385
  1088
fun local_future_proof x = future_proof local_done_proof context_of x;
wenzelm@29385
  1089
fun global_future_proof x = future_proof global_done_proof I x;
wenzelm@29385
  1090
wenzelm@17359
  1091
end;
wenzelm@29346
  1092
wenzelm@29385
  1093
wenzelm@29385
  1094
(* terminal proofs *)
wenzelm@29385
  1095
wenzelm@29385
  1096
local
wenzelm@29385
  1097
wenzelm@29385
  1098
fun future_terminal_proof proof1 proof2 meths int state =
wenzelm@38218
  1099
  if int orelse is_relevant state orelse not (Goal.local_future_enabled ())
wenzelm@29385
  1100
  then proof1 meths state
wenzelm@41677
  1101
  else snd (proof2 (fn state' =>
wenzelm@41677
  1102
    Goal.fork_name "Proof.future_terminal_proof" (fn () => ((), proof1 meths state'))) state);
wenzelm@29385
  1103
wenzelm@29385
  1104
in
wenzelm@29385
  1105
wenzelm@29385
  1106
fun local_future_terminal_proof x =
wenzelm@29385
  1107
  future_terminal_proof local_terminal_proof local_future_proof x;
wenzelm@29385
  1108
wenzelm@29385
  1109
fun global_future_terminal_proof x =
wenzelm@29385
  1110
  future_terminal_proof global_terminal_proof global_future_proof x;
wenzelm@29350
  1111
wenzelm@29346
  1112
end;
wenzelm@29346
  1113
wenzelm@29385
  1114
end;
wenzelm@29385
  1115