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