src/Pure/Isar/proof.ML
author wenzelm
Sat Oct 15 00:08:08 2005 +0200 (2005-10-15)
changeset 17859 f1d298e33760
parent 17756 d4a35f82fbb4
child 17903 4d7fe1816ab1
permissions -rw-r--r--
goal statements: before_qed argument;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    ID:         $Id$
wenzelm@5820
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     4
wenzelm@15757
     5
The Isar/VM proof language interpreter.
wenzelm@5820
     6
*)
wenzelm@5820
     7
wenzelm@5820
     8
signature PROOF =
wenzelm@5820
     9
sig
wenzelm@17756
    10
  type context (*= Context.proof*)
wenzelm@17756
    11
  type method (*= Method.method*)
wenzelm@5820
    12
  type state
wenzelm@5820
    13
  exception STATE of string * state
wenzelm@17359
    14
  val init: context -> state
wenzelm@17359
    15
  val level: state -> int
wenzelm@17359
    16
  val assert_bottom: bool -> state -> state
wenzelm@5820
    17
  val context_of: state -> context
wenzelm@5820
    18
  val theory_of: state -> theory
wenzelm@16450
    19
  val sign_of: state -> theory    (*obsolete*)
wenzelm@13377
    20
  val map_context: (context -> context) -> state -> state
wenzelm@7924
    21
  val warn_extra_tfrees: state -> state -> state
wenzelm@17359
    22
  val put_thms: 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@17359
    32
  val get_goal: state -> context * (thm list * thm)
wenzelm@16539
    33
  val show_main_goal: bool ref
wenzelm@6529
    34
  val verbose: bool 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@17359
    39
  val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq
wenzelm@5936
    40
  val match_bind: (string list * string) list -> state -> state
wenzelm@5936
    41
  val match_bind_i: (term list * term) list -> state -> state
wenzelm@8617
    42
  val let_bind: (string list * string) list -> state -> state
wenzelm@8617
    43
  val let_bind_i: (term list * term) list -> state -> state
wenzelm@17359
    44
  val fix: (string list * string option) list -> state -> state
wenzelm@17359
    45
  val fix_i: (string list * typ option) list -> state -> state
wenzelm@17359
    46
  val assm: ProofContext.exporter ->
wenzelm@17359
    47
    ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
wenzelm@17359
    48
    state -> state
wenzelm@17359
    49
  val assm_i: ProofContext.exporter ->
wenzelm@17359
    50
    ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@17359
    51
    -> state -> state
wenzelm@17359
    52
  val assume: ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
wenzelm@17359
    53
    state -> state
wenzelm@17359
    54
  val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@17359
    55
    -> state -> state
wenzelm@17359
    56
  val presume: ((string * Attrib.src list) * (string * (string list * string list)) list) list
wenzelm@17359
    57
    -> state -> state
wenzelm@17359
    58
  val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list
wenzelm@17359
    59
    -> state -> state
wenzelm@17359
    60
  val def: string * Attrib.src list -> string *  (string * string list) -> state -> state
wenzelm@17359
    61
  val def_i: string * context attribute list -> string * (term * term list) -> state -> state
wenzelm@17359
    62
  val chain: state -> state
wenzelm@17359
    63
  val chain_facts: thm list -> state -> state
wenzelm@17359
    64
  val get_thmss: state -> (thmref * Attrib.src list) list -> thm list
wenzelm@14564
    65
  val simple_note_thms: string -> thm list -> state -> state
wenzelm@17359
    66
  val note_thmss: ((string * Attrib.src list) *
wenzelm@17112
    67
    (thmref * Attrib.src list) list) list -> state -> state
wenzelm@17359
    68
  val note_thmss_i: ((string * context attribute list) *
wenzelm@9196
    69
    (thm list * context attribute list) list) list -> state -> state
wenzelm@17112
    70
  val from_thmss: ((thmref * Attrib.src list) list) list -> state -> state
wenzelm@17112
    71
  val from_thmss_i: ((thm list * context attribute list) list) list -> state -> state
wenzelm@17112
    72
  val with_thmss: ((thmref * Attrib.src list) list) list -> state -> state
wenzelm@17112
    73
  val with_thmss_i: ((thm list * context attribute list) list) list -> state -> state
wenzelm@17112
    74
  val using_thmss: ((thmref * Attrib.src list) list) list -> state -> state
wenzelm@12930
    75
  val using_thmss_i: ((thm list * context attribute list) list) list -> state -> state
wenzelm@17112
    76
  val invoke_case: string * string option list * Attrib.src list -> state -> state
wenzelm@17112
    77
  val invoke_case_i: string * string option list * context attribute list -> state -> state
wenzelm@17359
    78
  val begin_block: state -> state
wenzelm@17359
    79
  val next_block: state -> state
wenzelm@17359
    80
  val end_block: state -> state Seq.seq
wenzelm@17112
    81
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@17112
    82
  val defer: int option -> state -> state Seq.seq
wenzelm@17112
    83
  val prefer: int -> state -> state Seq.seq
wenzelm@17112
    84
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    85
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@17359
    86
  val goal_names: string option -> string -> string list -> string
wenzelm@17359
    87
  val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) ->
wenzelm@17359
    88
    (theory -> 'a -> context attribute) ->
wenzelm@17359
    89
    (context * 'b list -> context * (term list list * (context -> context))) ->
wenzelm@17859
    90
    string -> Method.text option ->
wenzelm@17859
    91
    (context * thm list -> thm list list -> state -> state Seq.seq) ->
wenzelm@17359
    92
    ((string * 'a list) * 'b) list -> state -> state
wenzelm@17359
    93
  val local_qed: Method.text option * bool -> state -> state Seq.seq
wenzelm@17359
    94
  val global_goal: (context -> (string * string) * (string * thm list) list -> unit) ->
wenzelm@17450
    95
    (theory -> 'a -> theory attribute) ->
wenzelm@17450
    96
    (context * 'b list -> context * (term list list * (context -> context))) ->
wenzelm@17859
    97
    string -> Method.text option ->
wenzelm@17859
    98
    (context * thm list -> thm list list -> theory -> theory) ->
wenzelm@17859
    99
    string option -> string * 'a list -> ((string * 'a list) * 'b) list -> context -> state
wenzelm@17359
   100
  val global_qed: Method.text option * bool -> state -> theory * context
wenzelm@17359
   101
  val local_terminal_proof: Method.text * Method.text option -> state -> state Seq.seq
wenzelm@17359
   102
  val local_default_proof: state -> state Seq.seq
wenzelm@17359
   103
  val local_immediate_proof: state -> state Seq.seq
wenzelm@17359
   104
  val local_done_proof: state -> state Seq.seq
wenzelm@17359
   105
  val local_skip_proof: bool -> state -> state Seq.seq
wenzelm@17359
   106
  val global_terminal_proof: Method.text * Method.text option -> state -> theory * context
wenzelm@17359
   107
  val global_default_proof: state -> theory * context
wenzelm@17359
   108
  val global_immediate_proof: state -> theory * context
wenzelm@17359
   109
  val global_done_proof: state -> theory * context
wenzelm@17359
   110
  val global_skip_proof: bool -> state -> theory * context
wenzelm@17859
   111
  val have: Method.text option ->
wenzelm@17859
   112
    (context * thm list -> thm list list -> state -> state Seq.seq) ->
wenzelm@17359
   113
    ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
wenzelm@17359
   114
    bool -> state -> state
wenzelm@17859
   115
  val have_i: Method.text option ->
wenzelm@17859
   116
    (context * thm list -> thm list list -> state -> state Seq.seq) ->
wenzelm@17359
   117
    ((string * context attribute list) * (term * (term list * term list)) list) list ->
wenzelm@17359
   118
    bool -> state -> state
wenzelm@17859
   119
  val show: Method.text option ->
wenzelm@17859
   120
    (context * thm list -> thm list list -> state -> state Seq.seq) ->
wenzelm@17359
   121
    ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
wenzelm@17359
   122
    bool -> state -> state
wenzelm@17859
   123
  val show_i: Method.text option ->
wenzelm@17859
   124
    (context * thm list -> thm list list -> state -> state Seq.seq) ->
wenzelm@17359
   125
    ((string * context attribute list) * (term * (term list * term list)) list) list ->
wenzelm@17359
   126
    bool -> state -> state
wenzelm@17859
   127
  val theorem: string -> Method.text option ->
wenzelm@17859
   128
    (context * thm list -> thm list list -> theory -> theory) ->
wenzelm@17359
   129
    string option -> string * Attrib.src list ->
wenzelm@17359
   130
    ((string * Attrib.src list) * (string * (string list * string list)) list) list ->
wenzelm@17359
   131
    context -> state
wenzelm@17859
   132
  val theorem_i: string -> Method.text option ->
wenzelm@17859
   133
    (context * thm list -> thm list list -> theory -> theory) ->
wenzelm@17359
   134
    string option -> string * theory attribute list ->
wenzelm@17359
   135
    ((string * theory attribute list) * (term * (term list * term list)) list) list ->
wenzelm@17359
   136
    context -> state
wenzelm@5820
   137
end;
wenzelm@5820
   138
wenzelm@13377
   139
structure Proof: PROOF =
wenzelm@5820
   140
struct
wenzelm@5820
   141
wenzelm@16813
   142
type context = ProofContext.context;
wenzelm@17112
   143
type method = Method.method;
wenzelm@16813
   144
wenzelm@5820
   145
wenzelm@5820
   146
(** proof state **)
wenzelm@5820
   147
wenzelm@17359
   148
(* datatype state *)
wenzelm@5820
   149
wenzelm@17112
   150
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   151
wenzelm@17359
   152
datatype state =
wenzelm@17359
   153
  State of node Stack.T
wenzelm@17359
   154
and node =
wenzelm@7176
   155
  Node of
wenzelm@7176
   156
   {context: context,
wenzelm@7176
   157
    facts: thm list option,
wenzelm@7176
   158
    mode: mode,
wenzelm@17359
   159
    goal: goal option}
wenzelm@17359
   160
and goal =
wenzelm@17359
   161
  Goal of
wenzelm@17359
   162
   {statement: string * term list list,     (*goal kind and statement*)
wenzelm@17359
   163
    using: thm list,                        (*goal facts*)
wenzelm@17359
   164
    goal: thm,                              (*subgoals ==> statement*)
wenzelm@17859
   165
    before_qed: Method.text option,
wenzelm@17359
   166
    after_qed:   (* FIXME results only *)
wenzelm@17359
   167
      (context * thm list -> thm list list -> state -> state Seq.seq) *
wenzelm@17359
   168
      (context * thm list -> thm list list -> theory -> theory)};
wenzelm@17359
   169
wenzelm@17359
   170
exception STATE of string * state;
wenzelm@17359
   171
wenzelm@17859
   172
fun make_goal (statement, using, goal, before_qed, after_qed) =
wenzelm@17859
   173
  Goal {statement = statement, using = using, goal = goal,
wenzelm@17859
   174
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   175
wenzelm@7176
   176
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   177
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   178
wenzelm@17359
   179
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   180
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   181
wenzelm@17359
   182
fun init ctxt = State (Stack.init (make_node (ctxt, NONE, Forward, NONE)));
wenzelm@5820
   183
wenzelm@17359
   184
fun current (State st) = Stack.top st |> (fn Node node => node);
wenzelm@17359
   185
fun map_current f (State st) = State (Stack.map (map_node f) st);
wenzelm@12045
   186
wenzelm@5820
   187
wenzelm@5820
   188
wenzelm@5820
   189
(** basic proof state operations **)
wenzelm@5820
   190
wenzelm@17359
   191
(* block structure *)
wenzelm@17359
   192
wenzelm@17359
   193
fun open_block (State st) = State (Stack.push st);
wenzelm@17359
   194
wenzelm@17359
   195
fun close_block (state as State st) = State (Stack.pop st)
wenzelm@17359
   196
  handle Empty => raise STATE ("Unbalanced block parentheses", state);
wenzelm@17359
   197
wenzelm@17359
   198
fun level (State st) = Stack.level st;
wenzelm@17359
   199
wenzelm@17359
   200
fun assert_bottom b state =
wenzelm@17359
   201
  let val b' = (level state <= 2) in
wenzelm@17359
   202
    if b andalso not b' then raise STATE ("Not at bottom of proof!", state)
wenzelm@17359
   203
    else if not b andalso b' then raise STATE ("Already at bottom of proof!", state)
wenzelm@17359
   204
    else state
wenzelm@17359
   205
  end;
wenzelm@17359
   206
wenzelm@17359
   207
wenzelm@5820
   208
(* context *)
wenzelm@5820
   209
wenzelm@17359
   210
val context_of = #context o current;
wenzelm@5820
   211
val theory_of = ProofContext.theory_of o context_of;
wenzelm@16450
   212
val sign_of = theory_of;
wenzelm@5820
   213
wenzelm@17359
   214
fun map_context f =
wenzelm@17359
   215
  map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   216
wenzelm@17359
   217
fun map_context_result f state =
wenzelm@17859
   218
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   219
wenzelm@7924
   220
val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of;
wenzelm@10809
   221
val add_binds_i = map_context o ProofContext.add_binds_i;
wenzelm@6091
   222
val put_thms = map_context o ProofContext.put_thms;
wenzelm@8374
   223
val get_case = ProofContext.get_case o context_of;
wenzelm@5820
   224
wenzelm@5820
   225
wenzelm@5820
   226
(* facts *)
wenzelm@5820
   227
wenzelm@17359
   228
val get_facts = #facts o current;
wenzelm@17359
   229
wenzelm@17359
   230
fun the_facts state =
wenzelm@17359
   231
  (case get_facts state of SOME facts => facts
wenzelm@17359
   232
  | NONE => raise STATE ("No current facts available", state));
wenzelm@5820
   233
wenzelm@9469
   234
fun the_fact state =
wenzelm@17359
   235
  (case the_facts state of [thm] => thm
wenzelm@17359
   236
  | _ => raise STATE ("Single theorem expected", state));
wenzelm@7605
   237
wenzelm@17359
   238
fun put_facts facts =
wenzelm@17359
   239
  map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
wenzelm@17359
   240
  #> put_thms (AutoBind.thisN, facts);
wenzelm@5820
   241
wenzelm@17359
   242
fun these_factss more_facts (named_factss, state) =
wenzelm@17359
   243
  (named_factss, state |> put_facts (SOME (List.concat (map snd named_factss) @ more_facts)));
wenzelm@5820
   244
wenzelm@17359
   245
fun export_facts inner outer =
wenzelm@17359
   246
  (case get_facts inner of
wenzelm@17359
   247
    NONE => Seq.single (put_facts NONE outer)
wenzelm@17359
   248
  | SOME thms =>
wenzelm@17359
   249
      thms
wenzelm@17359
   250
      |> Seq.map_list (ProofContext.export false (context_of inner) (context_of outer))
wenzelm@17359
   251
      |> Seq.map (fn ths => put_facts (SOME ths) outer));
wenzelm@5820
   252
wenzelm@5820
   253
wenzelm@5820
   254
(* mode *)
wenzelm@5820
   255
wenzelm@17359
   256
val get_mode = #mode o current;
wenzelm@5820
   257
fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   258
wenzelm@17359
   259
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   260
wenzelm@5820
   261
fun assert_mode pred state =
wenzelm@5820
   262
  let val mode = get_mode state in
wenzelm@5820
   263
    if pred mode then state
wenzelm@12010
   264
    else raise STATE ("Illegal application of proof command in "
wenzelm@12010
   265
      ^ quote (mode_name mode) ^ " mode", state)
wenzelm@5820
   266
  end;
wenzelm@5820
   267
wenzelm@5820
   268
val assert_forward = assert_mode (equal Forward);
wenzelm@17359
   269
val assert_chain = assert_mode (equal Chain);
wenzelm@17112
   270
val assert_forward_or_chain = assert_mode (equal Forward orf equal Chain);
wenzelm@5820
   271
val assert_backward = assert_mode (equal Backward);
wenzelm@17112
   272
val assert_no_chain = assert_mode (not_equal Chain);
wenzelm@5820
   273
wenzelm@17359
   274
val enter_forward = put_mode Forward;
wenzelm@17359
   275
val enter_chain = put_mode Chain;
wenzelm@17359
   276
val enter_backward = put_mode Backward;
wenzelm@5820
   277
wenzelm@17359
   278
wenzelm@17359
   279
(* current goal *)
wenzelm@17359
   280
wenzelm@17359
   281
fun current_goal state =
wenzelm@17359
   282
  (case current state of
wenzelm@17359
   283
    {context, goal = SOME (Goal goal), ...} => (context, goal)
wenzelm@17359
   284
  | _ => raise STATE ("No current goal!", state));
wenzelm@5820
   285
wenzelm@17359
   286
fun assert_current_goal g state =
wenzelm@17359
   287
  let val g' = can current_goal state in
wenzelm@17359
   288
    if g andalso not g' then raise STATE ("No goal in this block!", state)
wenzelm@17359
   289
    else if not g andalso g' then raise STATE ("Goal present in this block!", state)
wenzelm@17359
   290
    else state
wenzelm@17359
   291
  end;
wenzelm@6776
   292
wenzelm@17359
   293
fun put_goal goal = map_current (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   294
wenzelm@17859
   295
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   296
wenzelm@17359
   297
wenzelm@17359
   298
(* nested goal *)
wenzelm@5820
   299
wenzelm@17359
   300
fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
wenzelm@17359
   301
      let
wenzelm@17859
   302
        val Goal {statement, using, goal, before_qed, after_qed} = goal;
wenzelm@17859
   303
        val goal' = make_goal (g (statement, using, goal, before_qed, after_qed));
wenzelm@17359
   304
      in State (make_node (f context, facts, mode, SOME goal'), nodes) end
wenzelm@17359
   305
  | map_goal f g (State (nd, node :: nodes)) =
wenzelm@17359
   306
      let val State (node', nodes') = map_goal f g (State (node, nodes))
wenzelm@17359
   307
      in map_context f (State (nd, node' :: nodes')) end
wenzelm@17359
   308
  | map_goal _ _ state = state;
wenzelm@5820
   309
wenzelm@17859
   310
fun using_facts using = map_goal I (fn (statement, _, goal, before_qed, after_qed) =>
wenzelm@17859
   311
  (statement, using, goal, before_qed, after_qed));
wenzelm@17359
   312
wenzelm@17359
   313
local
wenzelm@17359
   314
  fun find i state =
wenzelm@17359
   315
    (case try current_goal state of
wenzelm@17359
   316
      SOME (ctxt, goal) => (ctxt, (i, goal))
wenzelm@17359
   317
    | NONE => find (i + 1) (close_block state
wenzelm@17359
   318
        handle STATE _ => raise STATE ("No goal present", state)));
wenzelm@17359
   319
in val find_goal = find 0 end;
wenzelm@17359
   320
wenzelm@17359
   321
fun get_goal state =
wenzelm@17359
   322
  let val (ctxt, (_, {using, goal, ...})) = find_goal state
wenzelm@17359
   323
  in (ctxt, (using, goal)) end;
wenzelm@5820
   324
wenzelm@5820
   325
wenzelm@5820
   326
wenzelm@12423
   327
(** pretty_state **)
wenzelm@5820
   328
nipkow@13869
   329
val show_main_goal = ref false;
wenzelm@16539
   330
val verbose = ProofContext.verbose;
nipkow@13698
   331
wenzelm@14876
   332
val pretty_goals_local = Display.pretty_goals_aux o ProofContext.pp;
wenzelm@12085
   333
skalberg@15531
   334
fun pretty_facts _ _ NONE = []
skalberg@15531
   335
  | pretty_facts s ctxt (SOME ths) =
wenzelm@12055
   336
      [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""];
wenzelm@6756
   337
wenzelm@17359
   338
fun pretty_state nr state =
wenzelm@5820
   339
  let
wenzelm@17359
   340
    val {context, facts, mode, goal = _} = current state;
wenzelm@17359
   341
    val ref (_, _, bg) = Display.current_goals_markers;
wenzelm@5820
   342
wenzelm@5820
   343
    fun levels_up 0 = ""
wenzelm@17359
   344
      | levels_up 1 = "1 level up"
wenzelm@17359
   345
      | levels_up i = string_of_int i ^ " levels up";
wenzelm@5820
   346
wenzelm@11556
   347
    fun subgoals 0 = ""
wenzelm@17359
   348
      | subgoals 1 = "1 subgoal"
wenzelm@17359
   349
      | subgoals n = string_of_int n ^ " subgoals";
wenzelm@17359
   350
wenzelm@17359
   351
    fun description strs =
wenzelm@17359
   352
      (case filter_out (equal "") strs of [] => ""
wenzelm@17359
   353
      | strs' => enclose " (" ")" (commas strs'));
wenzelm@12146
   354
wenzelm@17859
   355
    fun prt_goal (SOME (ctxt, (i, {statement = (kind, _), using, goal, before_qed, after_qed}))) =
wenzelm@17359
   356
          pretty_facts "using " ctxt
wenzelm@17359
   357
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@17359
   358
          [Pretty.str ("goal" ^ description [kind, levels_up (i div 2),
wenzelm@17359
   359
              subgoals (Thm.nprems_of goal)] ^ ":")] @
wenzelm@17359
   360
          pretty_goals_local ctxt bg (true, ! show_main_goal) (! Display.goals_limit) goal
wenzelm@17359
   361
      | prt_goal NONE = [];
wenzelm@6848
   362
wenzelm@17359
   363
    val prt_ctxt =
wenzelm@17359
   364
      if ! verbose orelse mode = Forward then ProofContext.pretty_context context
wenzelm@17359
   365
      else if mode = Backward then ProofContext.pretty_asms context
wenzelm@7575
   366
      else [];
wenzelm@17359
   367
  in
wenzelm@17359
   368
    [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@17359
   369
      (if ! verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")),
wenzelm@17359
   370
      Pretty.str ""] @
wenzelm@17359
   371
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@17359
   372
    (if ! verbose orelse mode = Forward then
wenzelm@17359
   373
       pretty_facts "" context facts @ prt_goal (try find_goal state)
wenzelm@17359
   374
     else if mode = Chain then pretty_facts "picking " context facts
wenzelm@17359
   375
     else prt_goal (try find_goal state))
wenzelm@17359
   376
  end;
wenzelm@5820
   377
wenzelm@12085
   378
fun pretty_goals main state =
wenzelm@17359
   379
  let val (ctxt, (_, {goal, ...})) = find_goal state
wenzelm@17359
   380
  in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) goal end;
wenzelm@10320
   381
wenzelm@5820
   382
wenzelm@5820
   383
wenzelm@5820
   384
(** proof steps **)
wenzelm@5820
   385
wenzelm@17359
   386
(* refine via method *)
wenzelm@5820
   387
wenzelm@8234
   388
local
wenzelm@8234
   389
wenzelm@16146
   390
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   391
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   392
wenzelm@16146
   393
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   394
wenzelm@16146
   395
fun goal_cases st =
wenzelm@16450
   396
  RuleCases.make true NONE (Thm.theory_of_thm st, Thm.prop_of st) (goals st);
wenzelm@16146
   397
wenzelm@16450
   398
fun check_theory thy state =
wenzelm@16450
   399
  if subthy (thy, theory_of state) then state
wenzelm@16450
   400
  else raise STATE ("Bad theory of method result: " ^ Context.str_of_thy thy, state);
wenzelm@5820
   401
wenzelm@17112
   402
fun apply_method current_context meth_fun state =
wenzelm@6848
   403
  let
wenzelm@17859
   404
    val (goal_ctxt, (_, {statement, using, goal, before_qed, after_qed})) = find_goal state;
wenzelm@17112
   405
    val meth = meth_fun (if current_context then context_of state else goal_ctxt);
wenzelm@16146
   406
  in
wenzelm@17359
   407
    Method.apply meth using goal |> Seq.map (fn (goal', meth_cases) =>
wenzelm@6848
   408
      state
wenzelm@17359
   409
      |> check_theory (Thm.theory_of_thm goal')
wenzelm@16146
   410
      |> map_goal
wenzelm@17359
   411
          (ProofContext.add_cases (no_goal_cases goal @ goal_cases goal' @ meth_cases))
wenzelm@17859
   412
          (K (statement, using, goal', before_qed, after_qed)))
wenzelm@16146
   413
  end;
wenzelm@5820
   414
wenzelm@17112
   415
fun apply_text cc text state =
wenzelm@17112
   416
  let
wenzelm@17112
   417
    val thy = theory_of state;
wenzelm@17112
   418
wenzelm@17112
   419
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@17112
   420
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@17112
   421
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   422
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   423
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@17112
   424
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt);
wenzelm@17112
   425
  in eval text state end;
wenzelm@17112
   426
wenzelm@8234
   427
in
wenzelm@8234
   428
wenzelm@17112
   429
val refine = apply_text true;
wenzelm@17112
   430
val refine_end = apply_text false;
wenzelm@8234
   431
wenzelm@8234
   432
end;
wenzelm@8234
   433
wenzelm@5820
   434
wenzelm@17359
   435
(* refine via sub-proof *)
wenzelm@17359
   436
wenzelm@17359
   437
local
wenzelm@9469
   438
wenzelm@11816
   439
fun refine_tac rule =
wenzelm@12703
   440
  Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) =>
wenzelm@11816
   441
    if can Logic.dest_goal (Logic.strip_assums_concl goal) then
wenzelm@11816
   442
      Tactic.etac Drule.triv_goal i
wenzelm@11816
   443
    else all_tac));
wenzelm@11816
   444
wenzelm@17359
   445
fun refine_goal print_rule inner raw_rule state =
wenzelm@17859
   446
  let val (outer, (_, {statement, using, goal, before_qed, after_qed})) = find_goal state in
wenzelm@17359
   447
    raw_rule
wenzelm@17359
   448
    |> ProofContext.export true inner outer
wenzelm@17359
   449
    |> Seq.maps (fn rule =>
wenzelm@17359
   450
      (print_rule outer rule;
wenzelm@17359
   451
        goal
wenzelm@17359
   452
        |> FINDGOAL (refine_tac rule)
wenzelm@17859
   453
        |> Seq.map (fn goal' =>
wenzelm@17859
   454
          map_goal I (K (statement, using, goal', before_qed, after_qed)) state)))
wenzelm@17359
   455
  end;
wenzelm@6932
   456
wenzelm@17359
   457
in
wenzelm@12146
   458
wenzelm@17359
   459
fun refine_goals print_rule inner raw_rules =
wenzelm@17359
   460
  Seq.EVERY (map (refine_goal print_rule inner) raw_rules);
wenzelm@17359
   461
wenzelm@17359
   462
end;
wenzelm@6932
   463
wenzelm@6932
   464
wenzelm@17359
   465
(* conclude_goal *)
wenzelm@6932
   466
wenzelm@17359
   467
fun conclude_goal state props goal =
wenzelm@5820
   468
  let
wenzelm@5820
   469
    val ctxt = context_of state;
wenzelm@5820
   470
    fun err msg = raise STATE (msg, state);
wenzelm@5820
   471
wenzelm@17359
   472
    val ngoals = Thm.nprems_of goal;
wenzelm@17359
   473
    val _ = conditional (ngoals > 0) (fn () =>
wenzelm@17359
   474
      err (Pretty.string_of (Pretty.chunks
wenzelm@17359
   475
        (pretty_goals_local ctxt "" (true, ! show_main_goal) (! Display.goals_limit) goal @
wenzelm@12085
   476
          [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")]))));
wenzelm@5820
   477
wenzelm@17359
   478
    val {hyps, thy, ...} = Thm.rep_thm goal;
wenzelm@17450
   479
    val bad_hyps = fold (remove (op aconv)) (ProofContext.assms_of ctxt) hyps;
wenzelm@12146
   480
wenzelm@17359
   481
    val th = goal RS Drule.rev_triv_goal;
wenzelm@17359
   482
    val ths = Drule.conj_elim_precise (length props) th
wenzelm@12146
   483
      handle THM _ => err "Main goal structure corrupted";
wenzelm@5820
   484
  in
wenzelm@12146
   485
    conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^
wenzelm@12146
   486
        cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps)));
wenzelm@17359
   487
    conditional (exists (not o Pattern.matches thy) (props ~~ map Thm.prop_of ths)) (fn () =>
wenzelm@12146
   488
      err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th)));
wenzelm@12146
   489
    ths
wenzelm@5820
   490
  end;
wenzelm@5820
   491
wenzelm@5820
   492
wenzelm@5820
   493
wenzelm@5820
   494
(*** structured proof commands ***)
wenzelm@5820
   495
wenzelm@17112
   496
(** context elements **)
wenzelm@5820
   497
wenzelm@17112
   498
(* bindings *)
wenzelm@5820
   499
wenzelm@16813
   500
local
wenzelm@16813
   501
wenzelm@17359
   502
fun gen_bind bind args state =
wenzelm@5820
   503
  state
wenzelm@5820
   504
  |> assert_forward
wenzelm@17359
   505
  |> map_context (bind args)
wenzelm@17359
   506
  |> put_facts NONE;
wenzelm@5820
   507
wenzelm@16813
   508
in
wenzelm@16813
   509
wenzelm@8617
   510
val match_bind = gen_bind (ProofContext.match_bind false);
wenzelm@8617
   511
val match_bind_i = gen_bind (ProofContext.match_bind_i false);
wenzelm@8617
   512
val let_bind = gen_bind (ProofContext.match_bind true);
wenzelm@8617
   513
val let_bind_i = gen_bind (ProofContext.match_bind_i true);
wenzelm@5820
   514
wenzelm@16813
   515
end;
wenzelm@16813
   516
wenzelm@5820
   517
wenzelm@17359
   518
(* fix *)
wenzelm@9196
   519
wenzelm@12714
   520
local
wenzelm@12714
   521
wenzelm@17859
   522
fun gen_fix fix_ctxt args =
wenzelm@17359
   523
  assert_forward
wenzelm@17859
   524
  #> map_context (fix_ctxt args)
wenzelm@17359
   525
  #> put_facts NONE;
wenzelm@5820
   526
wenzelm@16813
   527
in
wenzelm@16813
   528
wenzelm@5820
   529
val fix = gen_fix ProofContext.fix;
wenzelm@5820
   530
val fix_i = gen_fix ProofContext.fix_i;
wenzelm@5820
   531
wenzelm@16813
   532
end;
wenzelm@16813
   533
wenzelm@5820
   534
wenzelm@17359
   535
(* assume etc. *)
wenzelm@5820
   536
wenzelm@16813
   537
local
wenzelm@16813
   538
wenzelm@17112
   539
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   540
  state
wenzelm@5820
   541
  |> assert_forward
wenzelm@17112
   542
  |> map_context_result (asm exp (Attrib.map_specs (prep_att (theory_of state)) args))
wenzelm@17359
   543
  |> these_factss [] |> #2;
wenzelm@6932
   544
wenzelm@16813
   545
in
wenzelm@16813
   546
wenzelm@17112
   547
val assm      = gen_assume ProofContext.assume Attrib.local_attribute;
wenzelm@17112
   548
val assm_i    = gen_assume ProofContext.assume_i (K I);
wenzelm@17112
   549
val assume    = assm ProofContext.export_assume;
wenzelm@17112
   550
val assume_i  = assm_i ProofContext.export_assume;
wenzelm@17112
   551
val presume   = assm ProofContext.export_presume;
wenzelm@11917
   552
val presume_i = assm_i ProofContext.export_presume;
wenzelm@5820
   553
wenzelm@16813
   554
end;
wenzelm@16813
   555
wenzelm@7271
   556
wenzelm@17359
   557
(* def *)
wenzelm@11891
   558
wenzelm@16813
   559
local
wenzelm@16813
   560
wenzelm@17112
   561
fun gen_def fix prep_att prep_term prep_pats
wenzelm@17112
   562
    (raw_name, raw_atts) (x, (raw_rhs, raw_pats)) state =
wenzelm@11891
   563
  let
wenzelm@11891
   564
    val _ = assert_forward state;
wenzelm@17112
   565
    val thy = theory_of state;
wenzelm@11891
   566
    val ctxt = context_of state;
wenzelm@11891
   567
wenzelm@11891
   568
    val rhs = prep_term ctxt raw_rhs;
wenzelm@11891
   569
    val T = Term.fastype_of rhs;
wenzelm@11891
   570
    val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats;
wenzelm@17112
   571
    val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T));
wenzelm@11891
   572
wenzelm@17112
   573
    val name = if raw_name = "" then Thm.def_name x else raw_name;
wenzelm@17112
   574
    val atts = map (prep_att thy) raw_atts;
wenzelm@11891
   575
  in
wenzelm@11891
   576
    state
skalberg@15531
   577
    |> fix [([x], NONE)]
wenzelm@11891
   578
    |> match_bind_i [(pats, rhs)]
wenzelm@11917
   579
    |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])]
wenzelm@11891
   580
  end;
wenzelm@11891
   581
wenzelm@16813
   582
in
wenzelm@16813
   583
wenzelm@17112
   584
val def = gen_def fix Attrib.local_attribute ProofContext.read_term ProofContext.read_term_pats;
wenzelm@17112
   585
val def_i = gen_def fix_i (K I) ProofContext.cert_term ProofContext.cert_term_pats;
wenzelm@11891
   586
wenzelm@16813
   587
end;
wenzelm@16813
   588
wenzelm@11891
   589
wenzelm@8374
   590
wenzelm@17112
   591
(** facts **)
wenzelm@5820
   592
wenzelm@17359
   593
(* chain *)
wenzelm@5820
   594
wenzelm@17359
   595
val chain =
wenzelm@17359
   596
  assert_forward
wenzelm@17359
   597
  #> tap the_facts
wenzelm@17359
   598
  #> enter_chain;
wenzelm@5820
   599
wenzelm@17359
   600
fun chain_facts facts =
wenzelm@17359
   601
  put_facts (SOME facts)
wenzelm@17359
   602
  #> chain;
wenzelm@5820
   603
wenzelm@5820
   604
wenzelm@17359
   605
(* note etc. *)
wenzelm@17112
   606
wenzelm@17166
   607
fun no_binding args = map (pair ("", [])) args;
wenzelm@17112
   608
wenzelm@17112
   609
local
wenzelm@17112
   610
wenzelm@17359
   611
fun gen_thmss note_ctxt more_facts opt_chain opt_result prep_atts args state =
wenzelm@17112
   612
  state
wenzelm@17112
   613
  |> assert_forward
wenzelm@17112
   614
  |> map_context_result (note_ctxt (Attrib.map_facts (prep_atts (theory_of state)) args))
wenzelm@17112
   615
  |> these_factss (more_facts state)
wenzelm@17359
   616
  ||> opt_chain
wenzelm@17359
   617
  |> opt_result;
wenzelm@17112
   618
wenzelm@17112
   619
in
wenzelm@17112
   620
wenzelm@17359
   621
val note_thmss = gen_thmss ProofContext.note_thmss (K []) I #2 Attrib.local_attribute;
wenzelm@17359
   622
val note_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) I #2 (K I);
wenzelm@17112
   623
wenzelm@17112
   624
val from_thmss =
wenzelm@17359
   625
  gen_thmss ProofContext.note_thmss (K []) chain #2 Attrib.local_attribute o no_binding;
wenzelm@17359
   626
val from_thmss_i = gen_thmss ProofContext.note_thmss_i (K []) chain #2 (K I) o no_binding;
wenzelm@17112
   627
wenzelm@17112
   628
val with_thmss =
wenzelm@17359
   629
  gen_thmss ProofContext.note_thmss the_facts chain #2 Attrib.local_attribute o no_binding;
wenzelm@17359
   630
val with_thmss_i = gen_thmss ProofContext.note_thmss_i the_facts chain #2 (K I) o no_binding;
wenzelm@17359
   631
wenzelm@17359
   632
val local_results = gen_thmss ProofContext.note_thmss_i (K []) I I (K I);
wenzelm@17359
   633
fun global_results kind =
wenzelm@17359
   634
  swap oo (PureThy.note_thmss_i (Drule.kind kind) o map (apsnd Thm.simple_fact));
wenzelm@17359
   635
wenzelm@17359
   636
fun get_thmss state srcs = the_facts (note_thmss [(("", []), srcs)] state);
wenzelm@17359
   637
fun simple_note_thms name thms = note_thmss_i [((name, []), [(thms, [])])];
wenzelm@17112
   638
wenzelm@17112
   639
end;
wenzelm@17112
   640
wenzelm@17112
   641
wenzelm@17112
   642
(* using *)
wenzelm@17112
   643
wenzelm@17112
   644
local
wenzelm@17112
   645
wenzelm@17112
   646
fun gen_using_thmss note prep_atts args state =
wenzelm@17112
   647
  state
wenzelm@17112
   648
  |> assert_backward
wenzelm@17166
   649
  |> map_context_result (note (Attrib.map_facts (prep_atts (theory_of state)) (no_binding args)))
wenzelm@17859
   650
  |-> (fn named_facts => map_goal I (fn (statement, using, goal, before_qed, after_qed) =>
wenzelm@17859
   651
    (statement, using @ List.concat (map snd named_facts), goal, before_qed, after_qed)));
wenzelm@17112
   652
wenzelm@17112
   653
in
wenzelm@17112
   654
wenzelm@17112
   655
val using_thmss = gen_using_thmss ProofContext.note_thmss Attrib.local_attribute;
wenzelm@17112
   656
val using_thmss_i = gen_using_thmss ProofContext.note_thmss_i (K I);
wenzelm@17112
   657
wenzelm@17112
   658
end;
wenzelm@17112
   659
wenzelm@17112
   660
wenzelm@17112
   661
(* case *)
wenzelm@17112
   662
wenzelm@17112
   663
local
wenzelm@17112
   664
wenzelm@17112
   665
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   666
  let
wenzelm@17112
   667
    val atts = map (prep_att (theory_of state)) raw_atts;
wenzelm@17359
   668
    val ((lets, asms), state') =
wenzelm@17112
   669
      map_context_result (ProofContext.apply_case (get_case state name xs)) state;
wenzelm@17112
   670
    val assumptions = asms |> map (fn (a, ts) =>
wenzelm@17112
   671
      ((NameSpace.qualified name a, atts), map (rpair ([], [])) ts));
wenzelm@17112
   672
  in
wenzelm@17112
   673
    state'
wenzelm@17112
   674
    |> add_binds_i lets
wenzelm@17359
   675
    |> map_context (ProofContext.qualified_names #> ProofContext.no_base_names)
wenzelm@17112
   676
    |> assume_i assumptions
wenzelm@17112
   677
    |> map_context (ProofContext.restore_naming (context_of state))
wenzelm@17112
   678
    |> `the_facts |-> simple_note_thms name
wenzelm@17112
   679
  end;
wenzelm@17112
   680
wenzelm@17112
   681
in
wenzelm@17112
   682
wenzelm@17112
   683
val invoke_case = gen_invoke_case Attrib.local_attribute;
wenzelm@17112
   684
val invoke_case_i = gen_invoke_case (K I);
wenzelm@17112
   685
wenzelm@17112
   686
end;
wenzelm@17112
   687
wenzelm@17112
   688
wenzelm@17112
   689
wenzelm@17359
   690
(** proof structure **)
wenzelm@17359
   691
wenzelm@17359
   692
(* blocks *)
wenzelm@17359
   693
wenzelm@17359
   694
val begin_block =
wenzelm@17359
   695
  assert_forward
wenzelm@17359
   696
  #> open_block
wenzelm@17359
   697
  #> put_goal NONE
wenzelm@17359
   698
  #> open_block;
wenzelm@17359
   699
wenzelm@17359
   700
val next_block =
wenzelm@17359
   701
  assert_forward
wenzelm@17359
   702
  #> close_block
wenzelm@17359
   703
  #> open_block
wenzelm@17359
   704
  #> put_goal NONE
wenzelm@17359
   705
  #> put_facts NONE;
wenzelm@17359
   706
wenzelm@17359
   707
fun end_block state =
wenzelm@17359
   708
  state
wenzelm@17359
   709
  |> assert_forward
wenzelm@17359
   710
  |> close_block
wenzelm@17359
   711
  |> assert_current_goal false
wenzelm@17359
   712
  |> close_block
wenzelm@17359
   713
  |> export_facts state;
wenzelm@17359
   714
wenzelm@17359
   715
wenzelm@17359
   716
(* sub-proofs *)
wenzelm@17359
   717
wenzelm@17359
   718
fun proof opt_text =
wenzelm@17359
   719
  assert_backward
wenzelm@17859
   720
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   721
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   722
wenzelm@17359
   723
fun end_proof bot txt =
wenzelm@17359
   724
  assert_forward
wenzelm@17359
   725
  #> assert_bottom bot
wenzelm@17359
   726
  #> close_block
wenzelm@17359
   727
  #> assert_current_goal true
wenzelm@17359
   728
  #> using_facts []
wenzelm@17859
   729
  #> `before_qed #-> (refine o the_default Method.succeed_text)
wenzelm@17859
   730
  #> Seq.maps (refine (Method.finish_text txt));
wenzelm@17359
   731
wenzelm@17359
   732
wenzelm@17359
   733
(* unstructured refinement *)
wenzelm@17359
   734
wenzelm@17359
   735
fun defer i = assert_no_chain #> refine (Method.Basic (K (Method.defer i)));
wenzelm@17359
   736
fun prefer i = assert_no_chain #> refine (Method.Basic (K (Method.prefer i)));
wenzelm@17359
   737
wenzelm@17359
   738
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@17359
   739
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   740
wenzelm@17359
   741
wenzelm@17359
   742
wenzelm@17112
   743
(** goals **)
wenzelm@17112
   744
wenzelm@17359
   745
(* goal names *)
wenzelm@17359
   746
wenzelm@17359
   747
fun prep_names prep_att stmt =
wenzelm@17359
   748
  let
wenzelm@17359
   749
    val (names_attss, propp) = split_list (Attrib.map_specs prep_att stmt);
wenzelm@17359
   750
    val (names, attss) = split_list names_attss;
wenzelm@17359
   751
  in ((names, attss), propp) end;
wenzelm@5820
   752
wenzelm@17359
   753
fun goal_names target name names =
wenzelm@17450
   754
  (case target of NONE => "" | SOME "" => "" | SOME loc => " (in " ^ loc ^ ")") ^
wenzelm@17359
   755
  (if name = "" then "" else " " ^ name) ^
wenzelm@17359
   756
  (case filter_out (equal "") names of [] => ""
wenzelm@17359
   757
  | nms => " " ^ enclose "(" ")" (space_implode " and " (Library.take (7, nms) @
wenzelm@17359
   758
      (if length nms > 7 then ["..."] else []))));
wenzelm@17359
   759
wenzelm@16813
   760
wenzelm@17359
   761
(* generic goals *)
wenzelm@17359
   762
wenzelm@17359
   763
fun check_tvars props state =
wenzelm@17359
   764
  (case fold Term.add_tvars props [] of [] => ()
wenzelm@17359
   765
  | tvars => raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
wenzelm@17359
   766
      commas (map (ProofContext.string_of_typ (context_of state) o TVar) tvars), state));
wenzelm@8991
   767
wenzelm@17359
   768
fun check_vars props state =
wenzelm@17359
   769
  (case fold Term.add_vars props [] of [] => ()
wenzelm@17359
   770
  | vars => warning ("Goal statement contains unbound schematic variable(s): " ^
wenzelm@17359
   771
      commas (map (ProofContext.string_of_term (context_of state) o Var) vars)));
wenzelm@17359
   772
wenzelm@17859
   773
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   774
  let
wenzelm@17359
   775
    val thy = theory_of state;
wenzelm@17359
   776
    val chaining = can assert_chain state;
wenzelm@17359
   777
wenzelm@17359
   778
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   779
      state
wenzelm@5936
   780
      |> assert_forward_or_chain
wenzelm@5936
   781
      |> enter_forward
wenzelm@17359
   782
      |> open_block
wenzelm@17859
   783
      |> map_context_result (fn ctxt => swap (prepp (ctxt, raw_propp)));
wenzelm@15703
   784
skalberg@15570
   785
    val props = List.concat propss;
wenzelm@17359
   786
    val goal = Drule.mk_triv_goal (Thm.cterm_of thy (Logic.mk_conjunction_list props));
wenzelm@10553
   787
wenzelm@17359
   788
    val after_qed' = after_qed |>> (fn after_local => fn raw_results => fn results =>
wenzelm@17359
   789
      map_context after_ctxt
wenzelm@17359
   790
      #> after_local raw_results results);
wenzelm@5820
   791
  in
wenzelm@17359
   792
    goal_state
wenzelm@17359
   793
    |> tap (check_tvars props)
wenzelm@17359
   794
    |> tap (check_vars props)
wenzelm@17859
   795
    |> put_goal (SOME (make_goal ((kind, propss), [], goal, before_qed, after_qed')))
wenzelm@17359
   796
    |> map_context (ProofContext.add_cases (AutoBind.cases thy props))
wenzelm@17359
   797
    |> map_context (ProofContext.auto_bind_goal props)
wenzelm@17359
   798
    |> K chaining ? (`the_facts #-> using_facts)
wenzelm@17359
   799
    |> put_facts NONE
wenzelm@17359
   800
    |> open_block
wenzelm@17359
   801
    |> put_goal NONE
wenzelm@5820
   802
    |> enter_backward
wenzelm@17450
   803
    |> K (null props) ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   804
  end;
wenzelm@5820
   805
wenzelm@17359
   806
fun generic_qed state =
wenzelm@12503
   807
  let
wenzelm@17859
   808
    val (goal_ctxt, {statement = (_, stmt), using, goal, before_qed = _, after_qed}) =
wenzelm@17859
   809
      current_goal state;
wenzelm@8617
   810
    val outer_state = state |> close_block;
wenzelm@8617
   811
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   812
wenzelm@17359
   813
    val raw_props = List.concat stmt;
wenzelm@17359
   814
    val raw_results = conclude_goal state raw_props goal;
wenzelm@17359
   815
    val props = ProofContext.generalize goal_ctxt outer_ctxt raw_props;
wenzelm@17359
   816
    val results =
wenzelm@17359
   817
      Seq.map_list (ProofContext.export false goal_ctxt outer_ctxt) raw_results
wenzelm@17359
   818
      |> Seq.map (Library.unflat stmt);
wenzelm@17359
   819
  in
wenzelm@17359
   820
    outer_state
wenzelm@17359
   821
    |> map_context (ProofContext.auto_bind_facts props)
wenzelm@17359
   822
    |> pair (after_qed, ((goal_ctxt, raw_results), results))
wenzelm@17359
   823
  end;
wenzelm@17359
   824
wenzelm@9469
   825
wenzelm@17359
   826
(* local goals *)
wenzelm@17359
   827
wenzelm@17859
   828
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   829
  let
wenzelm@17359
   830
    val ((names, attss), propp) = prep_names (prep_att (theory_of state)) stmt;
wenzelm@17359
   831
wenzelm@17359
   832
    fun after_qed' raw_results results =
wenzelm@17359
   833
      local_results ((names ~~ attss) ~~ map Thm.simple_fact results)
wenzelm@17359
   834
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@17359
   835
      #> after_qed raw_results results;
wenzelm@5820
   836
  in
wenzelm@17359
   837
    state
wenzelm@17859
   838
    |> generic_goal prepp (kind ^ goal_names NONE "" names) before_qed (after_qed', K (K I)) propp
wenzelm@17359
   839
    |> warn_extra_tfrees state
wenzelm@5820
   840
  end;
wenzelm@5820
   841
wenzelm@17359
   842
fun local_qed txt =
wenzelm@17359
   843
  end_proof false txt
wenzelm@17359
   844
  #> Seq.map generic_qed
wenzelm@17359
   845
  #> Seq.maps (uncurry (fn (after_qed, (raw_results, results)) =>
wenzelm@17359
   846
      Seq.lifts (#1 after_qed raw_results) results));
wenzelm@5820
   847
wenzelm@5820
   848
wenzelm@17359
   849
(* global goals *)
wenzelm@17359
   850
wenzelm@17359
   851
fun global_goal print_results prep_att prepp
wenzelm@17859
   852
    kind before_qed after_qed target (name, raw_atts) stmt ctxt =
wenzelm@17359
   853
  let
wenzelm@17359
   854
    val thy = ProofContext.theory_of ctxt;
wenzelm@17359
   855
    val thy_ctxt = ProofContext.init thy;
wenzelm@17359
   856
wenzelm@17359
   857
    val atts = map (prep_att thy) raw_atts;
wenzelm@17359
   858
    val ((names, attss), propp) = prep_names (prep_att thy) stmt;
wenzelm@17359
   859
wenzelm@17450
   860
    fun store_results prfx res =
wenzelm@17450
   861
      K (prfx <> "") ? (Sign.add_path prfx #> Sign.no_base_names)
wenzelm@17450
   862
      #> global_results kind ((names ~~ attss) ~~ res)
wenzelm@17359
   863
      #-> (fn res' =>
wenzelm@17359
   864
        (print_results thy_ctxt ((kind, name), res') : unit;
wenzelm@17359
   865
          #2 o global_results kind [((name, atts), List.concat (map #2 res'))]))
wenzelm@17450
   866
      #> Sign.restore_naming thy;
wenzelm@17359
   867
wenzelm@17450
   868
    fun after_qed' raw_results results =
wenzelm@17450
   869
      (case target of NONE => I
wenzelm@17450
   870
      | SOME prfx => store_results (NameSpace.base prfx)
wenzelm@17450
   871
          (map (map (ProofContext.export_standard ctxt thy_ctxt
wenzelm@17450
   872
            #> Drule.strip_shyps_warning)) results))
wenzelm@17450
   873
      #> after_qed raw_results results;
wenzelm@17359
   874
  in
wenzelm@17359
   875
    init ctxt
wenzelm@17450
   876
    |> generic_goal (prepp #> ProofContext.auto_fix) (kind ^ goal_names target name names)
wenzelm@17859
   877
      before_qed (K (K Seq.single), after_qed') propp
wenzelm@17359
   878
  end;
wenzelm@12065
   879
wenzelm@17112
   880
fun check_result msg state sq =
wenzelm@17112
   881
  (case Seq.pull sq of
wenzelm@17112
   882
    NONE => raise STATE (msg, state)
wenzelm@17112
   883
  | SOME res => Seq.cons res);
wenzelm@17112
   884
wenzelm@17359
   885
fun global_qeds txt =
wenzelm@17359
   886
  end_proof true txt
wenzelm@17359
   887
  #> Seq.map generic_qed
wenzelm@17359
   888
  #> Seq.maps (fn ((after_qed, (raw_results, results)), state) =>
wenzelm@17359
   889
    Seq.lift (#2 after_qed raw_results) results (theory_of state)
wenzelm@17359
   890
    |> Seq.map (rpair (context_of state)))
wenzelm@17112
   891
  |> Seq.DETERM;   (*backtracking may destroy theory!*)
wenzelm@17112
   892
wenzelm@17359
   893
fun global_qed txt state =
wenzelm@17112
   894
  state
wenzelm@17359
   895
  |> global_qeds txt
wenzelm@17112
   896
  |> check_result "Failed to finish proof" state
wenzelm@17112
   897
  |> Seq.hd;
wenzelm@12959
   898
wenzelm@5820
   899
wenzelm@17359
   900
(* concluding steps *)
wenzelm@17112
   901
wenzelm@17359
   902
fun local_terminal_proof (text, opt_text) =
wenzelm@17359
   903
  proof (SOME text) #> Seq.maps (local_qed (opt_text, true));
wenzelm@17112
   904
wenzelm@17112
   905
val local_default_proof = local_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   906
val local_immediate_proof = local_terminal_proof (Method.this_text, NONE);
wenzelm@17359
   907
val local_done_proof = proof (SOME Method.done_text) #> Seq.maps (local_qed (NONE, false));
wenzelm@17359
   908
fun local_skip_proof int = local_terminal_proof (Method.sorry_text int, NONE);
wenzelm@17112
   909
wenzelm@17359
   910
fun global_term_proof immed (text, opt_text) state =
wenzelm@5820
   911
  state
wenzelm@17112
   912
  |> proof (SOME text)
wenzelm@17112
   913
  |> check_result "Terminal proof method failed" state
wenzelm@17359
   914
  |> Seq.maps (global_qeds (opt_text, immed))
wenzelm@17112
   915
  |> check_result "Failed to finish proof (after successful terminal method)" state
wenzelm@17112
   916
  |> Seq.hd;
wenzelm@17112
   917
wenzelm@17112
   918
val global_terminal_proof = global_term_proof true;
wenzelm@17112
   919
val global_default_proof = global_terminal_proof (Method.default_text, NONE);
wenzelm@17112
   920
val global_immediate_proof = global_terminal_proof (Method.this_text, NONE);
wenzelm@17112
   921
val global_done_proof = global_term_proof false (Method.done_text, NONE);
wenzelm@17359
   922
fun global_skip_proof int = global_terminal_proof (Method.sorry_text int, NONE);
wenzelm@5820
   923
wenzelm@6896
   924
wenzelm@17359
   925
(* common goal statements *)
wenzelm@17359
   926
wenzelm@17359
   927
local
wenzelm@17359
   928
wenzelm@17859
   929
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@17859
   930
  local_goal (ProofDisplay.print_results int) prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
   931
wenzelm@17859
   932
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
   933
  let
wenzelm@17359
   934
    val testing = ref false;
wenzelm@17359
   935
    val rule = ref (NONE: thm option);
wenzelm@17359
   936
    fun fail_msg ctxt =
wenzelm@17359
   937
      "Local statement will fail to solve any pending goal" ::
wenzelm@17359
   938
      (case ! rule of NONE => [] | SOME th => [ProofDisplay.string_of_rule ctxt "Failed" th])
wenzelm@17359
   939
      |> cat_lines;
wenzelm@6896
   940
wenzelm@17359
   941
    fun print_results ctxt res =
wenzelm@17359
   942
      if ! testing then () else ProofDisplay.print_results int ctxt res;
wenzelm@17359
   943
    fun print_rule ctxt th =
wenzelm@17359
   944
      if ! testing then rule := SOME th
wenzelm@17359
   945
      else if int then priority (ProofDisplay.string_of_rule ctxt "Successful" th)
wenzelm@17359
   946
      else ();
wenzelm@17359
   947
    val test_proof =
wenzelm@17359
   948
      (Seq.pull oo local_skip_proof) true
wenzelm@17359
   949
      |> setmp testing true
wenzelm@17359
   950
      |> setmp proofs 0
wenzelm@17359
   951
      |> transform_error
wenzelm@17359
   952
      |> capture;
wenzelm@6896
   953
wenzelm@17359
   954
    fun after_qed' raw_results results =
wenzelm@17359
   955
      refine_goals print_rule (context_of state) (List.concat results)
wenzelm@17359
   956
      #> Seq.maps (after_qed raw_results results);
wenzelm@17359
   957
  in
wenzelm@17359
   958
    state
wenzelm@17859
   959
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@17359
   960
    |> K int ? (fn goal_state =>
wenzelm@17359
   961
      (case test_proof goal_state of
wenzelm@17359
   962
        Result (SOME _) => goal_state
wenzelm@17359
   963
      | Result NONE => raise STATE (fail_msg (context_of goal_state), goal_state)
wenzelm@17359
   964
      | Exn Interrupt => raise Interrupt
wenzelm@17359
   965
      | Exn exn => raise EXCEPTION (exn, fail_msg (context_of goal_state))))
wenzelm@17359
   966
  end;
wenzelm@17359
   967
wenzelm@17859
   968
fun gen_theorem prep_att prepp kind before_qed after_qed target a =
wenzelm@17859
   969
  global_goal ProofDisplay.present_results prep_att prepp kind before_qed after_qed target a;
wenzelm@17359
   970
wenzelm@17359
   971
in
wenzelm@17359
   972
wenzelm@17359
   973
val have = gen_have Attrib.local_attribute ProofContext.bind_propp;
wenzelm@17359
   974
val have_i = gen_have (K I) ProofContext.bind_propp_i;
wenzelm@17359
   975
val show = gen_show Attrib.local_attribute ProofContext.bind_propp;
wenzelm@17359
   976
val show_i = gen_show (K I) ProofContext.bind_propp_i;
wenzelm@17359
   977
val theorem = gen_theorem Attrib.global_attribute ProofContext.bind_propp_schematic;
wenzelm@17359
   978
val theorem_i = gen_theorem (K I) ProofContext.bind_propp_schematic_i;
wenzelm@6896
   979
wenzelm@5820
   980
end;
wenzelm@17359
   981
wenzelm@17359
   982
end;