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