src/Pure/Isar/proof.ML
author wenzelm
Sat Jul 24 12:14:53 2010 +0200 (2010-07-24)
changeset 37949 48a874444164
parent 37186 349e9223c685
child 38218 1408f753bd75
permissions -rw-r--r--
moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
theory loader: reduced warnings -- thy database can be bypassed in certain situations;
Proof/Local_Theory.propagate_ml_env;
ML use function: propagate ML environment as well;
pervasive type generic_theory;
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
wenzelm@28278
    19
  val map_contexts: (context -> context) -> state -> state
wenzelm@37949
    20
  val propagate_ml_env: state -> state
wenzelm@30757
    21
  val bind_terms: (indexname * term option) list -> state -> state
wenzelm@26251
    22
  val put_thms: bool -> string * thm list option -> state -> state
wenzelm@6091
    23
  val the_facts: state -> thm list
wenzelm@9469
    24
  val the_fact: state -> thm
wenzelm@17450
    25
  val put_facts: thm list option -> state -> state
wenzelm@6891
    26
  val assert_forward: state -> state
wenzelm@17359
    27
  val assert_chain: state -> state
wenzelm@9469
    28
  val assert_forward_or_chain: state -> state
wenzelm@5820
    29
  val assert_backward: state -> state
wenzelm@8206
    30
  val assert_no_chain: state -> state
wenzelm@5820
    31
  val enter_forward: state -> state
wenzelm@24543
    32
  val goal_message: (unit -> Pretty.T) -> state -> state
wenzelm@32738
    33
  val show_main_goal: bool Unsynchronized.ref
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@36323
    45
  val let_bind: (term list * term) list -> state -> state
wenzelm@36323
    46
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36505
    47
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36505
    48
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36323
    49
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36323
    50
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wenzelm@20224
    51
  val assm: Assumption.export ->
wenzelm@36323
    52
    (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    53
  val assm_cmd: Assumption.export ->
wenzelm@28084
    54
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    55
  val assume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    56
  val assume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    57
  val presume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    58
  val presume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    59
  val def: (Thm.binding * ((binding * mixfix) * (term * term list))) list -> state -> state
wenzelm@36323
    60
  val def_cmd: (Attrib.binding * ((binding * mixfix) * (string * string list))) list -> state -> state
wenzelm@17359
    61
  val chain: state -> state
wenzelm@17359
    62
  val chain_facts: thm list -> state -> state
wenzelm@36323
    63
  val get_thmss_cmd: state -> (Facts.ref * Attrib.src list) list -> thm list
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@36323
    74
  val invoke_case: string * string option list * attribute list -> state -> state
wenzelm@36323
    75
  val invoke_case_cmd: string * string 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@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@17359
    86
    (context * 'b list -> context * (term list list * (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@33165
   167
  ProofContext.set_stmt true #>
wenzelm@33165
   168
  ProofContext.map_naming (K ProofContext.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@5820
   201
val theory_of = ProofContext.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@30757
   214
val bind_terms = map_context o ProofContext.bind_terms;
wenzelm@26251
   215
val put_thms = map_context oo ProofContext.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@20309
   242
      |> ProofContext.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
wenzelm@32738
   325
val show_main_goal = Unsynchronized.ref false;
wenzelm@16539
   326
val verbose = ProofContext.verbose;
nipkow@13698
   327
skalberg@15531
   328
fun pretty_facts _ _ NONE = []
skalberg@15531
   329
  | pretty_facts s ctxt (SOME ths) =
wenzelm@32091
   330
      [Pretty.big_list (s ^ "this:") (map (Display.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   331
wenzelm@17359
   332
fun pretty_state nr state =
wenzelm@5820
   333
  let
wenzelm@17359
   334
    val {context, facts, mode, goal = _} = current state;
wenzelm@5820
   335
wenzelm@5820
   336
    fun levels_up 0 = ""
wenzelm@17359
   337
      | levels_up 1 = "1 level up"
wenzelm@17359
   338
      | levels_up i = string_of_int i ^ " levels up";
wenzelm@5820
   339
wenzelm@11556
   340
    fun subgoals 0 = ""
wenzelm@17359
   341
      | subgoals 1 = "1 subgoal"
wenzelm@17359
   342
      | subgoals n = string_of_int n ^ " subgoals";
wenzelm@17359
   343
wenzelm@17359
   344
    fun description strs =
wenzelm@28375
   345
      (case filter_out (fn s => s = "") strs of [] => ""
wenzelm@17359
   346
      | strs' => enclose " (" ")" (commas strs'));
wenzelm@12146
   347
wenzelm@29416
   348
    fun prt_goal (SOME (ctxt, (i,
wenzelm@29416
   349
      {statement = ((_, pos), _, _), messages, using, goal, before_qed = _, after_qed = _}))) =
wenzelm@17359
   350
          pretty_facts "using " ctxt
wenzelm@17359
   351
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@21442
   352
          [Pretty.str ("goal" ^
wenzelm@21442
   353
            description [levels_up (i div 2), subgoals (Thm.nprems_of goal)] ^ ":")] @
wenzelm@32187
   354
          Goal_Display.pretty_goals ctxt
wenzelm@32167
   355
            {total = true, main = ! show_main_goal, maxgoals = ! Display.goals_limit} 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@17359
   360
      if ! verbose orelse mode = Forward then ProofContext.pretty_context context
wenzelm@18670
   361
      else if mode = Backward then ProofContext.pretty_ctxt context
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@17359
   369
       pretty_facts "" context facts @ prt_goal (try find_goal state)
wenzelm@17359
   370
     else if mode = Chain then pretty_facts "picking " context 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@32167
   375
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@32187
   376
    Goal_Display.pretty_goals ctxt
wenzelm@32167
   377
      {total = false, main = main, maxgoals = ! Display.goals_limit} goal
wenzelm@32167
   378
  end;
wenzelm@10320
   379
wenzelm@5820
   380
wenzelm@5820
   381
wenzelm@5820
   382
(** proof steps **)
wenzelm@5820
   383
wenzelm@17359
   384
(* refine via method *)
wenzelm@5820
   385
wenzelm@8234
   386
local
wenzelm@8234
   387
wenzelm@16146
   388
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   389
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   390
wenzelm@16146
   391
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   392
wenzelm@16146
   393
fun goal_cases st =
berghofe@34916
   394
  Rule_Cases.make_common (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair []) (goals st));
wenzelm@16146
   395
wenzelm@32193
   396
fun apply_method current_context meth state =
wenzelm@6848
   397
  let
wenzelm@24556
   398
    val (goal_ctxt, (_, {statement, messages = _, using, goal, before_qed, after_qed})) =
wenzelm@24543
   399
      find_goal state;
wenzelm@25958
   400
    val ctxt = if current_context then context_of state else goal_ctxt;
wenzelm@16146
   401
  in
wenzelm@32193
   402
    Method.apply meth ctxt using goal |> Seq.map (fn (meth_cases, goal') =>
wenzelm@6848
   403
      state
wenzelm@16146
   404
      |> map_goal
wenzelm@18475
   405
          (ProofContext.add_cases false (no_goal_cases goal @ goal_cases goal') #>
wenzelm@18475
   406
           ProofContext.add_cases true meth_cases)
wenzelm@24556
   407
          (K (statement, [], using, goal', before_qed, after_qed)))
wenzelm@16146
   408
  end;
wenzelm@5820
   409
wenzelm@19188
   410
fun select_goals n meth state =
wenzelm@19224
   411
  state
wenzelm@19224
   412
  |> (#2 o #2 o get_goal)
wenzelm@21687
   413
  |> ALLGOALS Goal.conjunction_tac
wenzelm@19224
   414
  |> Seq.maps (fn goal =>
wenzelm@19188
   415
    state
wenzelm@21687
   416
    |> Seq.lift set_goal (Goal.extract 1 n goal |> Seq.maps (Goal.conjunction_tac 1))
wenzelm@19188
   417
    |> Seq.maps meth
wenzelm@19188
   418
    |> Seq.maps (fn state' => state'
wenzelm@19188
   419
      |> Seq.lift set_goal (Goal.retrofit 1 n (#2 (#2 (get_goal state'))) goal))
wenzelm@32193
   420
    |> Seq.maps (apply_method true (K Method.succeed)));
wenzelm@19188
   421
wenzelm@17112
   422
fun apply_text cc text state =
wenzelm@17112
   423
  let
wenzelm@17112
   424
    val thy = theory_of state;
wenzelm@17112
   425
wenzelm@32193
   426
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@32193
   427
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@32193
   428
      | eval (Method.Source_i src) = apply_method cc (Method.method_i thy src)
wenzelm@17112
   429
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   430
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   431
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@19188
   432
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt)
wenzelm@19188
   433
      | eval (Method.SelectGoals (n, txt)) = select_goals n (eval txt);
wenzelm@17112
   434
  in eval text state end;
wenzelm@17112
   435
wenzelm@8234
   436
in
wenzelm@8234
   437
wenzelm@17112
   438
val refine = apply_text true;
wenzelm@17112
   439
val refine_end = apply_text false;
wenzelm@32856
   440
fun refine_insert ths = Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   441
wenzelm@8234
   442
end;
wenzelm@8234
   443
wenzelm@5820
   444
wenzelm@17359
   445
(* refine via sub-proof *)
wenzelm@17359
   446
wenzelm@30557
   447
fun finish_tac 0 = K all_tac
wenzelm@30557
   448
  | finish_tac n =
wenzelm@30557
   449
      Goal.norm_hhf_tac THEN'
wenzelm@30557
   450
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   451
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@30557
   452
          Tactic.etac Drule.protectI i THEN finish_tac (n - 1) i
wenzelm@30557
   453
        else finish_tac (n - 1) (i + 1));
wenzelm@30557
   454
wenzelm@20208
   455
fun goal_tac rule =
wenzelm@30557
   456
  Goal.norm_hhf_tac THEN'
wenzelm@30557
   457
  Tactic.rtac rule THEN'
wenzelm@30557
   458
  finish_tac (Thm.nprems_of rule);
wenzelm@11816
   459
wenzelm@19915
   460
fun refine_goals print_rule inner raw_rules state =
wenzelm@19915
   461
  let
wenzelm@19915
   462
    val (outer, (_, goal)) = get_goal state;
wenzelm@20208
   463
    fun refine rule st = (print_rule outer rule; FINDGOAL (goal_tac rule) st);
wenzelm@19915
   464
  in
wenzelm@19915
   465
    raw_rules
wenzelm@20309
   466
    |> ProofContext.goal_export inner outer
wenzelm@20309
   467
    |> (fn rules => Seq.lift set_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   468
  end;
wenzelm@17359
   469
wenzelm@6932
   470
wenzelm@17359
   471
(* conclude_goal *)
wenzelm@6932
   472
wenzelm@28627
   473
fun conclude_goal ctxt goal propss =
wenzelm@5820
   474
  let
wenzelm@28627
   475
    val thy = ProofContext.theory_of ctxt;
wenzelm@24920
   476
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@32091
   477
    val string_of_thm = Display.string_of_thm ctxt;
wenzelm@5820
   478
wenzelm@17359
   479
    val ngoals = Thm.nprems_of goal;
wenzelm@19774
   480
    val _ = ngoals = 0 orelse error (Pretty.string_of (Pretty.chunks
wenzelm@32187
   481
      (Goal_Display.pretty_goals ctxt
wenzelm@32167
   482
          {total = true, main = ! show_main_goal, maxgoals = ! Display.goals_limit} goal @
wenzelm@23639
   483
        [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])));
wenzelm@20224
   484
wenzelm@20224
   485
    val extra_hyps = Assumption.extra_hyps ctxt goal;
wenzelm@20224
   486
    val _ = null extra_hyps orelse
wenzelm@20224
   487
      error ("Additional hypotheses:\n" ^ cat_lines (map string_of_term extra_hyps));
wenzelm@5820
   488
wenzelm@23418
   489
    fun lost_structure () = error ("Lost goal structure:\n" ^ string_of_thm goal);
wenzelm@23418
   490
berghofe@23782
   491
    val th = Goal.conclude
wenzelm@23806
   492
      (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
berghofe@23782
   493
      handle THM _ => lost_structure ();
wenzelm@23418
   494
    val goal_propss = filter_out null propss;
wenzelm@23418
   495
    val results =
wenzelm@23418
   496
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   497
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@23418
   498
      handle THM _ => lost_structure ();
wenzelm@23418
   499
    val _ = Unify.matches_list thy (flat goal_propss) (map Thm.prop_of (flat results)) orelse
wenzelm@19774
   500
      error ("Proved a different theorem:\n" ^ string_of_thm th);
wenzelm@28627
   501
    val _ = Thm.check_shyps (Variable.sorts_of ctxt) th;
wenzelm@23418
   502
wenzelm@23418
   503
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   504
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   505
      | recover_result [] [] = []
wenzelm@23418
   506
      | recover_result _ _ = lost_structure ();
wenzelm@23418
   507
  in recover_result propss results end;
wenzelm@5820
   508
wenzelm@5820
   509
wenzelm@33288
   510
(* goal views -- corresponding to methods *)
wenzelm@33288
   511
wenzelm@33288
   512
fun raw_goal state =
wenzelm@33288
   513
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   514
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   515
wenzelm@33288
   516
val goal = raw_goal o refine_insert [];
wenzelm@33288
   517
wenzelm@33288
   518
fun simple_goal state =
wenzelm@33288
   519
  let
wenzelm@33288
   520
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   521
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   522
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   523
wenzelm@33288
   524
wenzelm@5820
   525
wenzelm@5820
   526
(*** structured proof commands ***)
wenzelm@5820
   527
wenzelm@17112
   528
(** context elements **)
wenzelm@5820
   529
wenzelm@36324
   530
(* let bindings *)
wenzelm@5820
   531
wenzelm@16813
   532
local
wenzelm@16813
   533
wenzelm@17359
   534
fun gen_bind bind args state =
wenzelm@5820
   535
  state
wenzelm@5820
   536
  |> assert_forward
wenzelm@36324
   537
  |> map_context (bind true args #> snd)
wenzelm@17359
   538
  |> put_facts NONE;
wenzelm@5820
   539
wenzelm@16813
   540
in
wenzelm@16813
   541
wenzelm@36324
   542
val let_bind = gen_bind ProofContext.match_bind_i;
wenzelm@36324
   543
val let_bind_cmd = gen_bind ProofContext.match_bind;
wenzelm@5820
   544
wenzelm@16813
   545
end;
wenzelm@16813
   546
wenzelm@5820
   547
wenzelm@36505
   548
(* concrete syntax *)
wenzelm@36505
   549
wenzelm@36505
   550
local
wenzelm@36505
   551
wenzelm@36507
   552
fun gen_write prep_arg mode args =
wenzelm@36507
   553
  assert_forward
wenzelm@36507
   554
  #> map_context (fn ctxt => ctxt |> ProofContext.notation true mode (map (prep_arg ctxt) args))
wenzelm@36507
   555
  #> put_facts NONE;
wenzelm@36505
   556
wenzelm@36505
   557
in
wenzelm@36505
   558
wenzelm@36505
   559
val write = gen_write (K I);
wenzelm@36505
   560
wenzelm@36505
   561
val write_cmd =
wenzelm@36505
   562
  gen_write (fn ctxt => fn (c, mx) =>
wenzelm@36505
   563
    (ProofContext.read_const ctxt false (Syntax.mixfixT mx) c, mx));
wenzelm@36505
   564
wenzelm@36505
   565
end;
wenzelm@36505
   566
wenzelm@36505
   567
wenzelm@17359
   568
(* fix *)
wenzelm@9196
   569
wenzelm@12714
   570
local
wenzelm@12714
   571
wenzelm@30763
   572
fun gen_fix prep_vars args =
wenzelm@17359
   573
  assert_forward
wenzelm@30763
   574
  #> map_context (fn ctxt => snd (ProofContext.add_fixes (prep_vars ctxt args) ctxt))
wenzelm@17359
   575
  #> put_facts NONE;
wenzelm@5820
   576
wenzelm@16813
   577
in
wenzelm@16813
   578
wenzelm@36323
   579
val fix = gen_fix (K I);
wenzelm@36323
   580
val fix_cmd = gen_fix (fn ctxt => fn args => fst (ProofContext.read_vars args ctxt));
wenzelm@5820
   581
wenzelm@16813
   582
end;
wenzelm@16813
   583
wenzelm@5820
   584
wenzelm@17359
   585
(* assume etc. *)
wenzelm@5820
   586
wenzelm@16813
   587
local
wenzelm@16813
   588
wenzelm@17112
   589
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   590
  state
wenzelm@5820
   591
  |> assert_forward
wenzelm@17112
   592
  |> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args))
wenzelm@17359
   593
  |> these_factss [] |> #2;
wenzelm@6932
   594
wenzelm@16813
   595
in
wenzelm@16813
   596
wenzelm@36323
   597
val assm = gen_assume ProofContext.add_assms_i (K I);
wenzelm@36323
   598
val assm_cmd = gen_assume ProofContext.add_assms Attrib.attribute;
wenzelm@36323
   599
val assume = assm Assumption.assume_export;
wenzelm@36323
   600
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   601
val presume = assm Assumption.presume_export;
wenzelm@36323
   602
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   603
wenzelm@16813
   604
end;
wenzelm@16813
   605
wenzelm@7271
   606
wenzelm@17359
   607
(* def *)
wenzelm@11891
   608
wenzelm@16813
   609
local
wenzelm@16813
   610
wenzelm@20913
   611
fun gen_def prep_att prep_vars prep_binds args state =
wenzelm@11891
   612
  let
wenzelm@11891
   613
    val _ = assert_forward state;
wenzelm@17112
   614
    val thy = theory_of state;
wenzelm@11891
   615
wenzelm@20913
   616
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@20913
   617
    val name_atts = map (apsnd (map (prep_att thy))) raw_name_atts;
wenzelm@11891
   618
  in
wenzelm@20913
   619
    state
wenzelm@20913
   620
    |> map_context_result (prep_vars (map (fn (x, mx) => (x, NONE, mx)) raw_vars))
wenzelm@20913
   621
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   622
    |-> (fn vars =>
wenzelm@20913
   623
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@35624
   624
      #-> (fn rhss => map_context_result (Local_Defs.add_defs (vars ~~ (name_atts ~~ rhss)))))
wenzelm@20913
   625
    |-> (put_facts o SOME o map (#2 o #2))
wenzelm@11891
   626
  end;
wenzelm@11891
   627
wenzelm@16813
   628
in
wenzelm@16813
   629
wenzelm@36323
   630
val def = gen_def (K I) ProofContext.cert_vars ProofContext.match_bind_i;
wenzelm@36323
   631
val def_cmd = gen_def Attrib.attribute ProofContext.read_vars ProofContext.match_bind;
wenzelm@11891
   632
wenzelm@16813
   633
end;
wenzelm@16813
   634
wenzelm@11891
   635
wenzelm@8374
   636
wenzelm@17112
   637
(** facts **)
wenzelm@5820
   638
wenzelm@17359
   639
(* chain *)
wenzelm@5820
   640
wenzelm@24011
   641
fun clean_facts ctxt =
wenzelm@24050
   642
  put_facts (SOME (filter_out Thm.is_dummy (the_facts ctxt))) ctxt;
wenzelm@24011
   643
wenzelm@17359
   644
val chain =
wenzelm@17359
   645
  assert_forward
wenzelm@24011
   646
  #> clean_facts
wenzelm@17359
   647
  #> enter_chain;
wenzelm@5820
   648
wenzelm@17359
   649
fun chain_facts facts =
wenzelm@17359
   650
  put_facts (SOME facts)
wenzelm@17359
   651
  #> chain;
wenzelm@5820
   652
wenzelm@5820
   653
wenzelm@17359
   654
(* note etc. *)
wenzelm@17112
   655
haftmann@28965
   656
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   657
wenzelm@17112
   658
local
wenzelm@17112
   659
wenzelm@30760
   660
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   661
  state
wenzelm@17112
   662
  |> assert_forward
wenzelm@30761
   663
  |> map_context_result (ProofContext.note_thmss ""
wenzelm@30760
   664
    (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state)) args))
wenzelm@17112
   665
  |> these_factss (more_facts state)
wenzelm@17359
   666
  ||> opt_chain
wenzelm@17359
   667
  |> opt_result;
wenzelm@17112
   668
wenzelm@17112
   669
in
wenzelm@17112
   670
wenzelm@36323
   671
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@36323
   672
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute ProofContext.get_fact;
wenzelm@17112
   673
wenzelm@36323
   674
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@36323
   675
val from_thmss_cmd = gen_thmss (K []) chain #2 Attrib.attribute ProofContext.get_fact o no_binding;
wenzelm@17359
   676
wenzelm@36323
   677
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@36323
   678
val with_thmss_cmd = gen_thmss the_facts chain #2 Attrib.attribute ProofContext.get_fact o no_binding;
wenzelm@30760
   679
wenzelm@30760
   680
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   681
wenzelm@36323
   682
fun get_thmss_cmd state srcs = the_facts (note_thmss_cmd [((Binding.empty, []), srcs)] state);
wenzelm@17112
   683
wenzelm@17112
   684
end;
wenzelm@17112
   685
wenzelm@17112
   686
wenzelm@18548
   687
(* using/unfolding *)
wenzelm@17112
   688
wenzelm@17112
   689
local
wenzelm@17112
   690
wenzelm@30760
   691
fun gen_using f g prep_atts prep_fact args state =
wenzelm@17112
   692
  state
wenzelm@17112
   693
  |> assert_backward
wenzelm@21442
   694
  |> map_context_result
wenzelm@30761
   695
    (ProofContext.note_thmss ""
wenzelm@30760
   696
      (Attrib.map_facts_refs (prep_atts (theory_of state)) (prep_fact (context_of state))
wenzelm@30760
   697
        (no_binding args)))
wenzelm@18843
   698
  |> (fn (named_facts, state') =>
wenzelm@24556
   699
    state' |> map_goal I (fn (statement, _, using, goal, before_qed, after_qed) =>
wenzelm@18843
   700
      let
wenzelm@18843
   701
        val ctxt = context_of state';
wenzelm@19482
   702
        val ths = maps snd named_facts;
wenzelm@24556
   703
      in (statement, [], f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end));
wenzelm@18548
   704
wenzelm@24050
   705
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   706
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   707
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   708
wenzelm@17112
   709
in
wenzelm@17112
   710
wenzelm@36323
   711
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@36323
   712
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute ProofContext.get_fact;
wenzelm@36323
   713
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@36323
   714
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute ProofContext.get_fact;
wenzelm@17112
   715
wenzelm@17112
   716
end;
wenzelm@17112
   717
wenzelm@17112
   718
wenzelm@17112
   719
(* case *)
wenzelm@17112
   720
wenzelm@17112
   721
local
wenzelm@17112
   722
wenzelm@30419
   723
fun qualified_binding a =
wenzelm@30419
   724
  Binding.qualify true (Long_Name.qualifier a) (Binding.name (Long_Name.base_name a));
wenzelm@30419
   725
wenzelm@17112
   726
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   727
  let
wenzelm@17112
   728
    val atts = map (prep_att (theory_of state)) raw_atts;
wenzelm@19078
   729
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@19078
   730
      ctxt |> ProofContext.apply_case (ProofContext.get_case ctxt name xs));
wenzelm@30419
   731
    val assumptions = asms |> map (fn (a, ts) => ((qualified_binding a, atts), map (rpair []) ts));
wenzelm@17112
   732
  in
wenzelm@17112
   733
    state'
wenzelm@36323
   734
    |> assume assumptions
wenzelm@33386
   735
    |> bind_terms Auto_Bind.no_facts
wenzelm@36323
   736
    |> `the_facts |-> (fn thms => note_thmss [((Binding.name name, []), [(thms, [])])])
wenzelm@17112
   737
  end;
wenzelm@17112
   738
wenzelm@17112
   739
in
wenzelm@17112
   740
wenzelm@36323
   741
val invoke_case = gen_invoke_case (K I);
wenzelm@36323
   742
val invoke_case_cmd = gen_invoke_case Attrib.attribute;
wenzelm@17112
   743
wenzelm@17112
   744
end;
wenzelm@17112
   745
wenzelm@17112
   746
wenzelm@17112
   747
wenzelm@17359
   748
(** proof structure **)
wenzelm@17359
   749
wenzelm@17359
   750
(* blocks *)
wenzelm@17359
   751
wenzelm@17359
   752
val begin_block =
wenzelm@17359
   753
  assert_forward
wenzelm@17359
   754
  #> open_block
wenzelm@17359
   755
  #> put_goal NONE
wenzelm@17359
   756
  #> open_block;
wenzelm@17359
   757
wenzelm@17359
   758
val next_block =
wenzelm@17359
   759
  assert_forward
wenzelm@17359
   760
  #> close_block
wenzelm@17359
   761
  #> open_block
wenzelm@17359
   762
  #> put_goal NONE
wenzelm@17359
   763
  #> put_facts NONE;
wenzelm@17359
   764
wenzelm@17359
   765
fun end_block state =
wenzelm@17359
   766
  state
wenzelm@17359
   767
  |> assert_forward
wenzelm@17359
   768
  |> close_block
wenzelm@17359
   769
  |> assert_current_goal false
wenzelm@17359
   770
  |> close_block
wenzelm@17359
   771
  |> export_facts state;
wenzelm@17359
   772
wenzelm@17359
   773
wenzelm@17359
   774
(* sub-proofs *)
wenzelm@17359
   775
wenzelm@17359
   776
fun proof opt_text =
wenzelm@17359
   777
  assert_backward
wenzelm@17859
   778
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   779
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   780
wenzelm@23360
   781
fun end_proof bot txt state =
wenzelm@23360
   782
  state
wenzelm@23360
   783
  |> assert_forward
wenzelm@23360
   784
  |> assert_bottom bot
wenzelm@23360
   785
  |> close_block
wenzelm@23360
   786
  |> assert_current_goal true
wenzelm@23360
   787
  |> using_facts []
wenzelm@23360
   788
  |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@32193
   789
  |> Seq.maps (refine (Method.finish_text txt));
wenzelm@17359
   790
wenzelm@29383
   791
fun check_result msg sq =
wenzelm@29383
   792
  (case Seq.pull sq of
wenzelm@29383
   793
    NONE => error msg
wenzelm@29383
   794
  | SOME (s, _) => s);
wenzelm@29383
   795
wenzelm@29383
   796
fun check_finish sq = check_result "Failed to finish proof" sq;
wenzelm@29383
   797
wenzelm@17359
   798
wenzelm@17359
   799
(* unstructured refinement *)
wenzelm@17359
   800
wenzelm@32193
   801
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i)));
wenzelm@32193
   802
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i)));
wenzelm@17359
   803
wenzelm@17359
   804
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@17359
   805
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   806
wenzelm@17359
   807
wenzelm@17359
   808
wenzelm@17112
   809
(** goals **)
wenzelm@17112
   810
wenzelm@17359
   811
(* generic goals *)
wenzelm@17359
   812
wenzelm@19774
   813
local
wenzelm@19774
   814
wenzelm@36322
   815
val is_var =
wenzelm@36322
   816
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   817
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   818
wenzelm@36322
   819
fun implicit_vars props =
wenzelm@19846
   820
  let
wenzelm@36354
   821
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   822
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   823
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   824
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   825
wenzelm@19774
   826
fun refine_terms n =
wenzelm@30510
   827
  refine (Method.Basic (K (RAW_METHOD
wenzelm@19774
   828
    (K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@32193
   829
      (HEADGOAL (CONJUNCTS (ALLGOALS (rtac Drule.termI))))))))))
wenzelm@19774
   830
  #> Seq.hd;
wenzelm@19774
   831
wenzelm@19774
   832
in
wenzelm@17359
   833
wenzelm@17859
   834
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   835
  let
wenzelm@17359
   836
    val thy = theory_of state;
wenzelm@23418
   837
    val cert = Thm.cterm_of thy;
wenzelm@17359
   838
    val chaining = can assert_chain state;
wenzelm@25958
   839
    val pos = Position.thread_data ();
wenzelm@17359
   840
wenzelm@17359
   841
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   842
      state
wenzelm@5936
   843
      |> assert_forward_or_chain
wenzelm@5936
   844
      |> enter_forward
wenzelm@17359
   845
      |> open_block
wenzelm@17859
   846
      |> map_context_result (fn ctxt => swap (prepp (ctxt, raw_propp)));
wenzelm@19482
   847
    val props = flat propss;
wenzelm@15703
   848
wenzelm@36322
   849
    val vars = implicit_vars props;
wenzelm@36322
   850
    val propss' = vars :: propss;
wenzelm@23418
   851
    val goal_propss = filter_out null propss';
wenzelm@29346
   852
    val goal =
wenzelm@29346
   853
      cert (Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss))
wenzelm@28627
   854
      |> Thm.weaken_sorts (Variable.sorts_of (context_of goal_state));
wenzelm@29346
   855
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@18124
   856
    val after_qed' = after_qed |>> (fn after_local =>
wenzelm@18124
   857
      fn results => map_context after_ctxt #> after_local results);
wenzelm@5820
   858
  in
wenzelm@17359
   859
    goal_state
wenzelm@21727
   860
    |> map_context (init_context #> Variable.set_body true)
wenzelm@28410
   861
    |> put_goal (SOME (make_goal (statement, [], [], Goal.init goal, before_qed, after_qed')))
wenzelm@17359
   862
    |> map_context (ProofContext.auto_bind_goal props)
wenzelm@21565
   863
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@17359
   864
    |> put_facts NONE
wenzelm@17359
   865
    |> open_block
wenzelm@17359
   866
    |> put_goal NONE
wenzelm@5820
   867
    |> enter_backward
wenzelm@23418
   868
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@32193
   869
    |> null props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   870
  end;
wenzelm@5820
   871
wenzelm@29090
   872
fun generic_qed after_ctxt state =
wenzelm@12503
   873
  let
wenzelm@36354
   874
    val (goal_ctxt, {statement = (_, stmt, _), goal, after_qed, ...}) = current_goal state;
wenzelm@8617
   875
    val outer_state = state |> close_block;
wenzelm@8617
   876
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   877
wenzelm@18503
   878
    val props =
wenzelm@19774
   879
      flat (tl stmt)
wenzelm@19915
   880
      |> Variable.exportT_terms goal_ctxt outer_ctxt;
wenzelm@17359
   881
    val results =
wenzelm@28627
   882
      tl (conclude_goal goal_ctxt goal stmt)
wenzelm@20309
   883
      |> burrow (ProofContext.export goal_ctxt outer_ctxt);
wenzelm@17359
   884
  in
wenzelm@17359
   885
    outer_state
wenzelm@29090
   886
    |> map_context (after_ctxt props)
wenzelm@34239
   887
    |> pair (after_qed, results)
wenzelm@17359
   888
  end;
wenzelm@17359
   889
wenzelm@19774
   890
end;
wenzelm@19774
   891
wenzelm@9469
   892
wenzelm@17359
   893
(* local goals *)
wenzelm@17359
   894
wenzelm@17859
   895
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   896
  let
wenzelm@21362
   897
    val thy = theory_of state;
wenzelm@21362
   898
    val ((names, attss), propp) =
wenzelm@21362
   899
      Attrib.map_specs (prep_att thy) stmt |> split_list |>> split_list;
wenzelm@17359
   900
wenzelm@18124
   901
    fun after_qed' results =
wenzelm@18808
   902
      local_results ((names ~~ attss) ~~ results)
wenzelm@17359
   903
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@18124
   904
      #> after_qed results;
wenzelm@5820
   905
  in
wenzelm@17359
   906
    state
wenzelm@21362
   907
    |> generic_goal prepp kind before_qed (after_qed', K I) propp
wenzelm@19900
   908
    |> tap (Variable.warn_extra_tfrees (context_of state) o context_of)
wenzelm@5820
   909
  end;
wenzelm@5820
   910
wenzelm@29383
   911
fun local_qeds txt =
wenzelm@29383
   912
  end_proof false txt
wenzelm@29383
   913
  #> Seq.map (generic_qed ProofContext.auto_bind_facts #->
wenzelm@29090
   914
    (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
   915
wenzelm@29383
   916
fun local_qed txt = local_qeds txt #> check_finish;
wenzelm@29383
   917
wenzelm@5820
   918
wenzelm@17359
   919
(* global goals *)
wenzelm@17359
   920
wenzelm@21442
   921
fun global_goal prepp before_qed after_qed propp ctxt =
wenzelm@21362
   922
  init ctxt
wenzelm@29383
   923
  |> generic_goal (prepp #> ProofContext.auto_fixes) "" before_qed (K I, after_qed) propp;
wenzelm@17359
   924
wenzelm@36323
   925
val theorem = global_goal ProofContext.bind_propp_schematic_i;
wenzelm@36323
   926
val theorem_cmd = global_goal ProofContext.bind_propp_schematic;
wenzelm@12065
   927
wenzelm@17359
   928
fun global_qeds txt =
wenzelm@17359
   929
  end_proof true txt
wenzelm@29090
   930
  #> Seq.map (generic_qed (K I) #> (fn (((_, after_qed), results), state) =>
wenzelm@29383
   931
    after_qed results (context_of state)));
wenzelm@17112
   932
wenzelm@29383
   933
fun global_qed txt = global_qeds txt #> check_finish;
wenzelm@12959
   934
wenzelm@5820
   935
wenzelm@17359
   936
(* concluding steps *)
wenzelm@17112
   937
wenzelm@29383
   938
fun terminal_proof qed initial terminal =
wenzelm@29383
   939
  proof (SOME initial) #> Seq.maps (qed terminal) #> check_finish;
wenzelm@17112
   940
wenzelm@29383
   941
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@17112
   942
val local_default_proof = local_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   943
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   944
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   945
val local_done_proof = terminal_proof local_qeds Method.done_text (NONE, false);
wenzelm@17112
   946
wenzelm@29383
   947
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@17112
   948
val global_default_proof = global_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   949
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   950
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE);
wenzelm@29383
   951
val global_done_proof = terminal_proof global_qeds Method.done_text (NONE, false);
wenzelm@5820
   952
wenzelm@6896
   953
wenzelm@17359
   954
(* common goal statements *)
wenzelm@17359
   955
wenzelm@17359
   956
local
wenzelm@17359
   957
wenzelm@17859
   958
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@33389
   959
  local_goal (Proof_Display.print_results int) prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
   960
wenzelm@17859
   961
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
   962
  let
wenzelm@32738
   963
    val testing = Unsynchronized.ref false;
wenzelm@32738
   964
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
   965
    fun fail_msg ctxt =
wenzelm@29383
   966
      "Local statement will fail to refine any pending goal" ::
wenzelm@33389
   967
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
   968
      |> cat_lines;
wenzelm@6896
   969
wenzelm@17359
   970
    fun print_results ctxt res =
wenzelm@33389
   971
      if ! testing then () else Proof_Display.print_results int ctxt res;
wenzelm@17359
   972
    fun print_rule ctxt th =
wenzelm@17359
   973
      if ! testing then rule := SOME th
wenzelm@33389
   974
      else if int then priority (Proof_Display.string_of_rule ctxt "Successful" th)
wenzelm@17359
   975
      else ();
wenzelm@17359
   976
    val test_proof =
wenzelm@29383
   977
      try (local_skip_proof true)
wenzelm@28289
   978
      |> setmp_noncritical testing true
wenzelm@23963
   979
      |> Exn.capture;
wenzelm@6896
   980
wenzelm@18124
   981
    fun after_qed' results =
wenzelm@19482
   982
      refine_goals print_rule (context_of state) (flat results)
wenzelm@29383
   983
      #> check_result "Failed to refine any pending goal"
wenzelm@29383
   984
      #> after_qed results;
wenzelm@17359
   985
  in
wenzelm@17359
   986
    state
wenzelm@17859
   987
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@21565
   988
    |> int ? (fn goal_state =>
wenzelm@17359
   989
      (case test_proof goal_state of
wenzelm@23963
   990
        Exn.Result (SOME _) => goal_state
wenzelm@23963
   991
      | Exn.Result NONE => error (fail_msg (context_of goal_state))
wenzelm@28443
   992
      | Exn.Exn Exn.Interrupt => raise Exn.Interrupt
wenzelm@28458
   993
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
   994
  end;
wenzelm@17359
   995
wenzelm@17359
   996
in
wenzelm@17359
   997
wenzelm@36323
   998
val have = gen_have (K I) ProofContext.bind_propp_i;
wenzelm@36323
   999
val have_cmd = gen_have Attrib.attribute ProofContext.bind_propp;
wenzelm@36323
  1000
val show = gen_show (K I) ProofContext.bind_propp_i;
wenzelm@36323
  1001
val show_cmd = gen_show Attrib.attribute ProofContext.bind_propp;
wenzelm@6896
  1002
wenzelm@5820
  1003
end;
wenzelm@17359
  1004
wenzelm@28410
  1005
wenzelm@29385
  1006
wenzelm@29385
  1007
(** future proofs **)
wenzelm@29385
  1008
wenzelm@29385
  1009
(* relevant proof states *)
wenzelm@29385
  1010
wenzelm@29385
  1011
fun is_schematic t =
wenzelm@29385
  1012
  Term.exists_subterm Term.is_Var t orelse
wenzelm@29385
  1013
  Term.exists_type (Term.exists_subtype Term.is_TVar) t;
wenzelm@29385
  1014
wenzelm@29385
  1015
fun schematic_goal state =
wenzelm@29385
  1016
  let val (_, (_, {statement = (_, _, prop), ...})) = find_goal state
wenzelm@29385
  1017
  in is_schematic prop end;
wenzelm@29385
  1018
wenzelm@29385
  1019
fun is_relevant state =
wenzelm@29385
  1020
  (case try find_goal state of
wenzelm@29385
  1021
    NONE => true
wenzelm@29385
  1022
  | SOME (_, (_, {statement = (_, _, prop), goal, ...})) =>
wenzelm@29385
  1023
      is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1024
wenzelm@29385
  1025
wenzelm@29385
  1026
(* full proofs *)
wenzelm@28410
  1027
wenzelm@29346
  1028
local
wenzelm@29346
  1029
wenzelm@29385
  1030
fun future_proof done get_context fork_proof state =
wenzelm@28410
  1031
  let
wenzelm@29381
  1032
    val _ = assert_backward state;
wenzelm@28410
  1033
    val (goal_ctxt, (_, goal)) = find_goal state;
wenzelm@32786
  1034
    val {statement as (kind, _, prop), messages, using, goal, before_qed, after_qed} = goal;
wenzelm@32062
  1035
    val goal_txt = prop :: map Thm.term_of (Assumption.all_assms_of goal_ctxt);
wenzelm@28410
  1036
wenzelm@29383
  1037
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1038
wenzelm@29346
  1039
    val prop' = Logic.protect prop;
wenzelm@29346
  1040
    val statement' = (kind, [[], [prop']], prop');
wenzelm@29346
  1041
    val goal' = Thm.adjust_maxidx_thm (Thm.maxidx_of goal)
wenzelm@29346
  1042
      (Drule.comp_no_flatten (goal, Thm.nprems_of goal) 1 Drule.protectI);
wenzelm@29385
  1043
wenzelm@33386
  1044
    fun after_local' [[th]] = put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@33386
  1045
    fun after_global' [[th]] = ProofContext.put_thms false (Auto_Bind.thisN, SOME [th]);
wenzelm@29385
  1046
    val after_qed' = (after_local', after_global');
wenzelm@33386
  1047
    val this_name = ProofContext.full_name goal_ctxt (Binding.name Auto_Bind.thisN);
wenzelm@28410
  1048
wenzelm@28973
  1049
    val result_ctxt =
wenzelm@28973
  1050
      state
wenzelm@32062
  1051
      |> map_contexts (fold Variable.declare_term goal_txt)
wenzelm@29385
  1052
      |> map_goal I (K (statement', messages, using, goal', before_qed, after_qed'))
wenzelm@29346
  1053
      |> fork_proof;
wenzelm@28973
  1054
wenzelm@29385
  1055
    val future_thm = result_ctxt |> Future.map (fn (_, x) =>
wenzelm@29385
  1056
      ProofContext.get_fact_single (get_context x) (Facts.named this_name));
wenzelm@32062
  1057
    val finished_goal = Goal.future_result goal_ctxt future_thm prop';
wenzelm@28973
  1058
    val state' =
wenzelm@28973
  1059
      state
wenzelm@28973
  1060
      |> map_goal I (K (statement, messages, using, finished_goal, NONE, after_qed))
wenzelm@29385
  1061
      |> done;
wenzelm@28973
  1062
  in (Future.map #1 result_ctxt, state') end;
wenzelm@28410
  1063
wenzelm@29385
  1064
in
wenzelm@29385
  1065
wenzelm@29385
  1066
fun local_future_proof x = future_proof local_done_proof context_of x;
wenzelm@29385
  1067
fun global_future_proof x = future_proof global_done_proof I x;
wenzelm@29385
  1068
wenzelm@17359
  1069
end;
wenzelm@29346
  1070
wenzelm@29385
  1071
wenzelm@29385
  1072
(* terminal proofs *)
wenzelm@29385
  1073
wenzelm@29385
  1074
local
wenzelm@29385
  1075
wenzelm@29385
  1076
fun future_terminal_proof proof1 proof2 meths int state =
wenzelm@37186
  1077
  if not (Goal.local_future_enforced ()) andalso
wenzelm@37186
  1078
    int orelse is_relevant state orelse not (Goal.local_future_enabled ())
wenzelm@29385
  1079
  then proof1 meths state
wenzelm@37186
  1080
  else snd (proof2 (fn state' => Goal.fork (fn () => ((), proof1 meths state'))) state);
wenzelm@29385
  1081
wenzelm@29385
  1082
in
wenzelm@29385
  1083
wenzelm@29385
  1084
fun local_future_terminal_proof x =
wenzelm@29385
  1085
  future_terminal_proof local_terminal_proof local_future_proof x;
wenzelm@29385
  1086
wenzelm@29385
  1087
fun global_future_terminal_proof x =
wenzelm@29385
  1088
  future_terminal_proof global_terminal_proof global_future_proof x;
wenzelm@29350
  1089
wenzelm@29346
  1090
end;
wenzelm@29346
  1091
wenzelm@29385
  1092
end;
wenzelm@29385
  1093